From 16635c2f92c48b13e006f337850df2f176ea706d Mon Sep 17 00:00:00 2001 From: Vittorio Parrella Date: Tue, 8 Oct 2024 23:15:33 +0200 Subject: [PATCH] cleanup: fixed tests --- workers/client/client_patch_test.go | 22 ++++++++++++++++++++ workers/client/client_proxy_test.go | 21 ------------------- workers/client/client_test.go | 5 +++-- workers/gateway/gateway_interface_darwin.go | 23 ++++++++++++--------- workers/gateway/gateway_interface_linux.go | 15 +++++++------- 5 files changed, 45 insertions(+), 41 deletions(-) diff --git a/workers/client/client_patch_test.go b/workers/client/client_patch_test.go index 1ed1c38..9472b8d 100644 --- a/workers/client/client_patch_test.go +++ b/workers/client/client_patch_test.go @@ -7,10 +7,12 @@ import ( "github.com/Project-Faster/monkey" "github.com/Project-Faster/qpep/api" "github.com/Project-Faster/qpep/shared/configuration" + "github.com/Project-Faster/qpep/shared/errors" "github.com/Project-Faster/qpep/workers/gateway" "github.com/stretchr/testify/assert" "net" "net/url" + "reflect" "runtime" "sync" "time" @@ -376,3 +378,23 @@ func (s *ClientSuite) TestClientStatisticsUpdate_Fail() { ok := clientStatisticsUpdate("127.0.0.1", "127.0.0.1", 8080, "172.30.54.250") assert.False(s.T(), ok) } + +func (s *ClientProxyListenerSuite) TestProxyListener_FailAccept() { + listener, err := NewClientProxyListener("tcp", &net.TCPAddr{ + IP: net.ParseIP("127.0.0.1"), + Port: 9090, + }) + + assert.Nil(s.T(), err) + assert.NotNil(s.T(), listener) + + clListener := listener.(*ClientProxyListener) + monkey.PatchInstanceMethod(reflect.TypeOf(clListener.base), "AcceptTCP", + func(_ *net.TCPListener) (*net.TCPConn, error) { + return nil, errors.ErrFailed + }) + + conn, errConn := clListener.Accept() + assert.Nil(s.T(), conn) + assert.Equal(s.T(), errors.ErrFailed, errConn) +} diff --git a/workers/client/client_proxy_test.go b/workers/client/client_proxy_test.go index 9733d2f..e38d157 100644 --- a/workers/client/client_proxy_test.go +++ b/workers/client/client_proxy_test.go @@ -6,7 +6,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" "net" - "reflect" "sync" "testing" "time" @@ -98,23 +97,3 @@ func (s *ClientProxyListenerSuite) TestProxyListener_AcceptConn() { assert.Nil(s.T(), listener.Close()) } - -func (s *ClientProxyListenerSuite) TestProxyListener_FailAccept() { - listener, err := NewClientProxyListener("tcp", &net.TCPAddr{ - IP: net.ParseIP("127.0.0.1"), - Port: 9090, - }) - - assert.Nil(s.T(), err) - assert.NotNil(s.T(), listener) - - clListener := listener.(*ClientProxyListener) - monkey.PatchInstanceMethod(reflect.TypeOf(clListener.base), "AcceptTCP", - func(_ *net.TCPListener) (*net.TCPConn, error) { - return nil, errors.ErrFailed - }) - - conn, errConn := clListener.Accept() - assert.Nil(s.T(), conn) - assert.Equal(s.T(), errors.ErrFailed, errConn) -} diff --git a/workers/client/client_test.go b/workers/client/client_test.go index c4d0578..fd30187 100644 --- a/workers/client/client_test.go +++ b/workers/client/client_test.go @@ -37,8 +37,6 @@ type ClientSuite struct { } func (s *ClientSuite) BeforeTest(_, testName string) { - gateway.SetSystemProxy(false) - api.Statistics.Reset() proxyListener = nil gateway.UsingProxy = false @@ -60,6 +58,9 @@ func (s *ClientSuite) BeforeTest(_, testName string) { configuration.QPepConfig.General.WinDivertThreads = 4 configuration.QPepConfig.General.PreferProxy = true configuration.QPepConfig.General.Verbose = false + + gateway.SetSystemProxy(false) + api.Statistics.Reset() } func (s *ClientSuite) AfterTest(_, testName string) { diff --git a/workers/gateway/gateway_interface_darwin.go b/workers/gateway/gateway_interface_darwin.go index d7c9f74..f941ba2 100644 --- a/workers/gateway/gateway_interface_darwin.go +++ b/workers/gateway/gateway_interface_darwin.go @@ -7,7 +7,6 @@ import ( "github.com/Project-Faster/qpep/shared" "github.com/Project-Faster/qpep/shared/configuration" "github.com/Project-Faster/qpep/shared/logger" - "github.com/jackpal/gateway" "net" "net/url" "regexp" @@ -33,17 +32,11 @@ var ( ipRegexp = regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`) ) -func getRouteGatewayInterfaces() ([]int64, []string, error) { - defaultIP, err := gateway.DiscoverGateway() - if err != nil { - logger.Panic("Could not discover default lan address and the requested one is not suitable, error: %v", err) +func getRouteListeningAddresses() []string { + if len(defaultListeningAddress) > 0 { + return []string{defaultListeningAddress} } - logger.Info("Found default ip address: %s\n", defaultIP.String()) - return []int64{}, []string{defaultIP.String()}, nil -} - -func getRouteListeningAddresses() []string { output, err, code := shared.RunCommand("networksetup", "-listallnetworkservices") if err != nil || code != 0 { logger.Error("Could not set system proxy, error (code: %d): %v", code, err) @@ -77,6 +70,16 @@ func getRouteListeningAddresses() []string { return outAddress } +func getRouteGatewayInterfaces() ([]int64, []string, error) { + defaultIP, err := gateway.DiscoverGateway() + if err != nil { + logger.Panic("Could not discover default lan address and the requested one is not suitable, error: %v", err) + } + + logger.Info("Found default ip address: %s\n", defaultIP.String()) + return []int64{}, []string{defaultIP.String()}, nil +} + func SetSystemProxy(active bool) { config := configuration.QPepConfig.Client if !active { diff --git a/workers/gateway/gateway_interface_linux.go b/workers/gateway/gateway_interface_linux.go index 30bdfd1..66c2fcf 100644 --- a/workers/gateway/gateway_interface_linux.go +++ b/workers/gateway/gateway_interface_linux.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/Project-Faster/qpep/shared" "github.com/Project-Faster/qpep/shared/logger" - "github.com/jackpal/gateway" "net" "net/url" "strconv" @@ -16,6 +15,13 @@ var ( defaultPortsIgnored = []int{53} ) +func getRouteListeningAddresses() []string { + if defaultListeningAddress == "" { + defaultListeningAddress = "127.0.0.1" + } + return []string{defaultListeningAddress} +} + func getRouteGatewayInterfaces() ([]int64, []string, error) { defaultIP, err := gateway.DiscoverInterface() if err != nil { @@ -26,13 +32,6 @@ func getRouteGatewayInterfaces() ([]int64, []string, error) { return []int64{}, []string{defaultIP.String()}, nil } -func getRouteListeningAddresses() []string { - if defaultListeningAddress == "" { - defaultListeningAddress = "127.0.0.1" - } - return []string{defaultListeningAddress} -} - func SetSystemProxy(active bool) {} func GetSystemProxyEnabled() (bool, *url.URL) {