From 489ae5bca96c8f121aa71602596375d49841f73f Mon Sep 17 00:00:00 2001 From: Vittorio Parrella Date: Wed, 18 Sep 2024 22:02:01 +0200 Subject: [PATCH] fix: unit tests --- service/service_windows_test.go | 11 ++-- workers/client/client_patch_test.go | 49 ++++++++-------- .../gateway/gateway_interface_windows_test.go | 56 ++++++++++--------- workers/server/server_patch_test.go | 39 +++++++------ 4 files changed, 81 insertions(+), 74 deletions(-) diff --git a/service/service_windows_test.go b/service/service_windows_test.go index 5236679..ee0ee73 100644 --- a/service/service_windows_test.go +++ b/service/service_windows_test.go @@ -7,6 +7,7 @@ package service import ( "errors" "github.com/Project-Faster/monkey" + "github.com/Project-Faster/qpep/shared" "github.com/Project-Faster/qpep/workers/gateway" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" @@ -72,7 +73,7 @@ func (s *ServiceWinSuite) TestSetCurrentWorkingDir_FailPathNotExist() { } func (s *ServiceWinSuite) TestSetServiceUserPermissions() { - monkey.Patch(gateway.RunCommand, func(name string, params ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(name string, params ...string) ([]byte, error, int) { assert.Equal(s.T(), "sc.exe", name) assert.Len(s.T(), params, 3) assert.Equal(s.T(), "sdset", params[0]) @@ -87,7 +88,7 @@ func (s *ServiceWinSuite) TestSetServiceUserPermissions() { } func (s *ServiceWinSuite) TestSetServiceUserPermissions_Error() { - monkey.Patch(gateway.RunCommand, func(string, ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(string, ...string) ([]byte, error, int) { return nil, errors.New("test-error"), 1 }) @@ -97,7 +98,7 @@ func (s *ServiceWinSuite) TestSetServiceUserPermissions_Error() { } func (s *ServiceWinSuite) TestInstallDirectoryPermissions() { - monkey.Patch(gateway.RunCommand, func(name string, params ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(name string, params ...string) ([]byte, error, int) { assert.Equal(s.T(), "icacls", name) switch len(params) { case 5: @@ -126,7 +127,7 @@ func (s *ServiceWinSuite) TestInstallDirectoryPermissions() { } func (s *ServiceWinSuite) TestInstallDirectoryPermissions_ErrorFirstCmd() { - monkey.Patch(gateway.RunCommand, func(string, ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(string, ...string) ([]byte, error, int) { return nil, errors.New("test-error"), 1 }) @@ -136,7 +137,7 @@ func (s *ServiceWinSuite) TestInstallDirectoryPermissions_ErrorFirstCmd() { } func (s *ServiceWinSuite) TestInstallDirectoryPermissions_ErrorSecondCmd() { - monkey.Patch(gateway.RunCommand, func(_ string, params ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(_ string, params ...string) ([]byte, error, int) { if params[len(params)-1] == "/reset" { return nil, nil, 0 } diff --git a/workers/client/client_patch_test.go b/workers/client/client_patch_test.go index aa40ee5..9d033c1 100644 --- a/workers/client/client_patch_test.go +++ b/workers/client/client_patch_test.go @@ -6,8 +6,9 @@ import ( "context" "github.com/Project-Faster/monkey" "github.com/Project-Faster/qpep/api" - "github.com/Project-Faster/qpep/shared" + "github.com/Project-Faster/qpep/shared/configuration" "github.com/Project-Faster/qpep/windivert" + "github.com/Project-Faster/qpep/workers/gateway" "github.com/stretchr/testify/assert" "net" "net/url" @@ -271,35 +272,35 @@ func (s *ClientSuite) TestHandleServices_FailStatistics() { } func (s *ClientSuite) TestInitProxy() { - monkey.Patch(shared.SetSystemProxy, func(active bool) { + monkey.Patch(gateway.SetSystemProxy, func(active bool) { assert.True(s.T(), active) - shared.UsingProxy = true - shared.ProxyAddress, _ = url.Parse("http://127.0.0.1:8080") + gateway.UsingProxy = true + gateway.ProxyAddress, _ = url.Parse("http://127.0.0.1:8080") }) - assert.False(s.T(), shared.UsingProxy) + assert.False(s.T(), gateway.UsingProxy) initProxy() - assert.True(s.T(), shared.UsingProxy) + assert.True(s.T(), gateway.UsingProxy) - assert.NotNil(s.T(), shared.ProxyAddress) + assert.NotNil(s.T(), gateway.ProxyAddress) } func (s *ClientSuite) TestStopProxy() { - shared.UsingProxy = true - shared.ProxyAddress, _ = url.Parse("http://127.0.0.1:8080") + gateway.UsingProxy = true + gateway.ProxyAddress, _ = url.Parse("http://127.0.0.1:8080") - monkey.Patch(shared.SetSystemProxy, func(active bool) { + monkey.Patch(gateway.SetSystemProxy, func(active bool) { assert.False(s.T(), active) - shared.UsingProxy = false - shared.ProxyAddress = nil + gateway.UsingProxy = false + gateway.ProxyAddress = nil }) - assert.True(s.T(), shared.UsingProxy) + assert.True(s.T(), gateway.UsingProxy) stopProxy() - assert.False(s.T(), shared.UsingProxy) + assert.False(s.T(), gateway.UsingProxy) assert.False(s.T(), redirected) - assert.Nil(s.T(), shared.ProxyAddress) + assert.Nil(s.T(), gateway.ProxyAddress) } func (s *ClientSuite) TestInitDiverter() { @@ -337,23 +338,23 @@ func (s *ClientSuite) TestStopDiverter() { } func (s *ClientSuite) TestInitialCheckConnection() { - shared.QPepConfig.PreferProxy = false + configuration.QPepConfig.General.PreferProxy = false validateConfiguration() - monkey.Patch(shared.SetSystemProxy, func(active bool) { + monkey.Patch(gateway.SetSystemProxy, func(active bool) { assert.True(s.T(), active) - shared.UsingProxy = true - shared.ProxyAddress, _ = url.Parse("http://127.0.0.1:8080") + gateway.UsingProxy = true + gateway.ProxyAddress, _ = url.Parse("http://127.0.0.1:8080") }) - assert.False(s.T(), shared.UsingProxy) + assert.False(s.T(), gateway.UsingProxy) initialCheckConnection() - assert.False(s.T(), shared.UsingProxy) - assert.Nil(s.T(), shared.ProxyAddress) + assert.False(s.T(), gateway.UsingProxy) + assert.Nil(s.T(), gateway.ProxyAddress) initialCheckConnection() - assert.False(s.T(), shared.UsingProxy) - assert.Nil(s.T(), shared.ProxyAddress) + assert.False(s.T(), gateway.UsingProxy) + assert.Nil(s.T(), gateway.ProxyAddress) } func (s *ClientSuite) TestGatewayStatusCheck() { diff --git a/workers/gateway/gateway_interface_windows_test.go b/workers/gateway/gateway_interface_windows_test.go index fd16ba1..ae87cd0 100644 --- a/workers/gateway/gateway_interface_windows_test.go +++ b/workers/gateway/gateway_interface_windows_test.go @@ -3,10 +3,12 @@ package gateway import ( - "errors" + stderr "errors" "fmt" "github.com/Project-Faster/monkey" - stderr "github.com/Project-Faster/qpep/shared/errors" + "github.com/Project-Faster/qpep/shared" + "github.com/Project-Faster/qpep/shared/configuration" + "github.com/Project-Faster/qpep/shared/errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" "net/url" @@ -40,7 +42,7 @@ func (s *GatewayConfigSuite) AfterTest(_, _ string) { func (s *GatewayConfigSuite) TestGetSystemProxyEnabled_False() { t := s.T() - monkey.Patch(RunCommand, func(string, ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(string, ...string) ([]byte, error, int) { return []byte("0x0"), nil, 0 }) @@ -51,8 +53,8 @@ func (s *GatewayConfigSuite) TestGetSystemProxyEnabled_False() { func (s *GatewayConfigSuite) TestGetSystemProxyEnabled_False_Error() { t := s.T() - monkey.Patch(RunCommand, func(string, ...string) ([]byte, error, int) { - return nil, errors.New("test-error"), 1 + monkey.Patch(shared.RunCommand, func(string, ...string) ([]byte, error, int) { + return nil, stderr.New("test-error"), 1 }) active, url := GetSystemProxyEnabled() @@ -62,7 +64,7 @@ func (s *GatewayConfigSuite) TestGetSystemProxyEnabled_False_Error() { func (s *GatewayConfigSuite) TestGetSystemProxyEnabled_True() { t := s.T() - monkey.Patch(RunCommand, func(name string, data ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(name string, data ...string) ([]byte, error, int) { if data[len(data)-1] == "ProxyEnable" { return []byte("0x1"), nil, 0 } @@ -78,11 +80,11 @@ func (s *GatewayConfigSuite) TestGetSystemProxyEnabled_True() { func (s *GatewayConfigSuite) TestGetSystemProxyEnabled_True_Error() { t := s.T() - monkey.Patch(RunCommand, func(name string, data ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(name string, data ...string) ([]byte, error, int) { if data[len(data)-1] == "ProxyEnable" { return []byte("0x1"), nil, 0 } - return nil, errors.New("test-error"), 1 + return nil, stderr.New("test-error"), 1 }) active, url := GetSystemProxyEnabled() @@ -92,7 +94,7 @@ func (s *GatewayConfigSuite) TestGetSystemProxyEnabled_True_Error() { func (s *GatewayConfigSuite) TestGetSystemProxyEnabled_True_ErrorParse() { t := s.T() - monkey.Patch(RunCommand, func(name string, data ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(name string, data ...string) ([]byte, error, int) { if data[len(data)-1] == "ProxyEnable" { return []byte("0x1"), nil, 0 } @@ -106,7 +108,7 @@ func (s *GatewayConfigSuite) TestGetSystemProxyEnabled_True_ErrorParse() { func (s *GatewayConfigSuite) TestPreloadRegistryKeysForUsers() { t := s.T() - monkey.Patch(RunCommand, func(name string, data ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(name string, data ...string) ([]byte, error, int) { return []byte("SID\nS-1-5-21-4227727717-1300533570-3298936513-500\n" + "S-1-5-21-4227727717-1300533570-3298936513-503\n" + "S-1-5-21-4227727717-1300533570-3298936513-501\n" + @@ -139,8 +141,8 @@ func (s *GatewayConfigSuite) TestPreloadRegistryKeysForUsers() { func (s *GatewayConfigSuite) TestPreloadRegistryKeysForUsers_Error() { t := s.T() - monkey.Patch(RunCommand, func(name string, data ...string) ([]byte, error, int) { - return nil, errors.New("test-error"), 1 + monkey.Patch(shared.RunCommand, func(name string, data ...string) ([]byte, error, int) { + return nil, stderr.New("test-error"), 1 }) assert.Panics(t, func() { @@ -150,7 +152,7 @@ func (s *GatewayConfigSuite) TestPreloadRegistryKeysForUsers_Error() { func (s *GatewayConfigSuite) TestSetSystemProxy_Disabled() { t := s.T() - monkey.Patch(RunCommand, func(name string, data ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(name string, data ...string) ([]byte, error, int) { if name == "wmic" { return []byte("SID\nS-1-5-21-4227727717-1300533570-3298936513-500\n" + "S-1-5-21-4227727717-1300533570-3298936513-503\n" + @@ -169,7 +171,7 @@ func (s *GatewayConfigSuite) TestSetSystemProxy_Disabled() { func (s *GatewayConfigSuite) TestSetSystemProxy_Active() { t := s.T() - monkey.Patch(RunCommand, func(name string, data ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(name string, data ...string) ([]byte, error, int) { if name == "wmic" { return []byte("SID\nS-1-5-21-4227727717-1300533570-3298936513-500\n" + "S-1-5-21-4227727717-1300533570-3298936513-503\n" + @@ -192,9 +194,9 @@ func (s *GatewayConfigSuite) TestSetSystemProxy_Active() { func (s *GatewayConfigSuite) TestGetRouteGatewayInterfaces_ErrorRoute() { t := s.T() - monkey.Patch(RunCommand, func(name string, data ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(name string, data ...string) ([]byte, error, int) { if data[len(data)-1] == "route" { - return nil, errors.New("test-error"), 1 + return nil, stderr.New("test-error"), 1 } return nil, nil, 0 // ignored just don't execute the real command }) @@ -202,12 +204,12 @@ func (s *GatewayConfigSuite) TestGetRouteGatewayInterfaces_ErrorRoute() { interfacesList, addressList, err := getRouteGatewayInterfaces() assert.Nil(t, interfacesList) assert.Nil(t, addressList) - assert.Equal(t, stderr.ErrFailedGatewayDetect, err) + assert.Equal(t, errors.ErrFailedGatewayDetect, err) } func (s *GatewayConfigSuite) TestGetRouteGatewayInterfaces_ErrorRouteEmpty() { t := s.T() - monkey.Patch(RunCommand, func(name string, data ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(name string, data ...string) ([]byte, error, int) { if data[len(data)-1] == "route" { return []byte(``), nil, 0 } @@ -217,7 +219,7 @@ func (s *GatewayConfigSuite) TestGetRouteGatewayInterfaces_ErrorRouteEmpty() { interfacesList, addressList, err := getRouteGatewayInterfaces() assert.Nil(t, interfacesList) assert.Nil(t, addressList) - assert.Equal(t, stderr.ErrFailedGatewayDetect, err) + assert.Equal(t, errors.ErrFailedGatewayDetect, err) } var cmdTestDataRoute = []byte(` @@ -275,40 +277,40 @@ Configurazione per l'interfaccia "Loopback Pseudo-Interface 1" func (s *GatewayConfigSuite) TestGetRouteGatewayInterfaces_ErrorInterface() { t := s.T() - monkey.Patch(RunCommand, func(name string, data ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(name string, data ...string) ([]byte, error, int) { if data[len(data)-1] == "route" { return cmdTestDataRoute, nil, 0 } - return nil, errors.New("test-error"), 1 + return nil, stderr.New("test-error"), 1 }) interfacesList, addressList, err := getRouteGatewayInterfaces() assert.Nil(t, interfacesList) assert.Nil(t, addressList) - assert.Equal(t, stderr.ErrFailedGatewayDetect, err) + assert.Equal(t, errors.ErrFailedGatewayDetect, err) } func (s *GatewayConfigSuite) TestGetRouteGatewayInterfaces_ErrorConfig() { t := s.T() - monkey.Patch(RunCommand, func(name string, data ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(name string, data ...string) ([]byte, error, int) { switch data[len(data)-1] { case "route": return cmdTestDataRoute, nil, 0 case "interface": return cmdTestDataInterfaces, nil, 0 } - return nil, errors.New("test-error"), 1 + return nil, stderr.New("test-error"), 1 }) interfacesList, addressList, err := getRouteGatewayInterfaces() assert.Nil(t, interfacesList) assert.Nil(t, addressList) - assert.Equal(t, stderr.ErrFailedGatewayDetect, err) + assert.Equal(t, errors.ErrFailedGatewayDetect, err) } func (s *GatewayConfigSuite) TestGetRouteGatewayInterfaces() { t := s.T() - monkey.Patch(RunCommand, func(name string, data ...string) ([]byte, error, int) { + monkey.Patch(shared.RunCommand, func(name string, data ...string) ([]byte, error, int) { switch data[len(data)-1] { case "route": return cmdTestDataRoute, nil, 0 @@ -317,7 +319,7 @@ func (s *GatewayConfigSuite) TestGetRouteGatewayInterfaces() { case "config": return cmdTestDataConfig, nil, 0 } - return nil, errors.New("test-error"), 1 + return nil, stderr.New("test-error"), 1 }) interfacesList, addressList, err := getRouteGatewayInterfaces() diff --git a/workers/server/server_patch_test.go b/workers/server/server_patch_test.go index e263acf..25f8c85 100644 --- a/workers/server/server_patch_test.go +++ b/workers/server/server_patch_test.go @@ -4,10 +4,13 @@ package server import ( "crypto/tls" - "errors" + stderr "errors" "github.com/Project-Faster/monkey" "github.com/Project-Faster/qpep/api" - "github.com/Project-Faster/qpep/shared" + "github.com/Project-Faster/qpep/shared/configuration" + "github.com/Project-Faster/qpep/shared/errors" + "github.com/Project-Faster/qpep/shared/protocol" + "github.com/Project-Faster/qpep/workers/gateway" "github.com/Project-Faster/quic-go" "github.com/stretchr/testify/assert" "net" @@ -23,38 +26,38 @@ func (s *ServerSuite) TestValidateConfiguration() { } func (s *ServerSuite) TestValidateConfiguration_BadListenAddress() { - shared.QPepConfig.ListenHost = "ABCD" - shared.QPepConfig.ListenPort = 9090 - shared.QPepConfig.GatewayAPIPort = 9443 + configuration.QPepConfig.Server.LocalListeningAddress = "ABCD" + configuration.QPepConfig.Server.LocalListenPort = 9090 + configuration.QPepConfig.General.APIPort = 9443 - assert.PanicsWithValue(s.T(), shared.ErrConfigurationValidationFailed, func() { + assert.PanicsWithValue(s.T(), errors.ErrConfigurationValidationFailed, func() { validateConfiguration() }) } func (s *ServerSuite) TestValidateConfiguration_BadListenPort() { - shared.QPepConfig.ListenHost = "127.0.0.1" - shared.QPepConfig.ListenPort = 0 - shared.QPepConfig.GatewayAPIPort = 9443 + configuration.QPepConfig.Server.LocalListeningAddress = "127.0.0.1" + configuration.QPepConfig.Server.LocalListenPort = 0 + configuration.QPepConfig.General.APIPort = 9443 - assert.PanicsWithValue(s.T(), shared.ErrConfigurationValidationFailed, func() { + assert.PanicsWithValue(s.T(), errors.ErrConfigurationValidationFailed, func() { validateConfiguration() }) } func (s *ServerSuite) TestValidateConfiguration_BadAPIPort() { - shared.QPepConfig.ListenHost = "127.0.0.1" - shared.QPepConfig.ListenPort = 9090 - shared.QPepConfig.GatewayAPIPort = 99999 + configuration.QPepConfig.Server.LocalListeningAddress = "127.0.0.1" + configuration.QPepConfig.Server.LocalListenPort = 9090 + configuration.QPepConfig.General.APIPort = 99999 - assert.PanicsWithValue(s.T(), shared.ErrConfigurationValidationFailed, func() { + assert.PanicsWithValue(s.T(), errors.ErrConfigurationValidationFailed, func() { validateConfiguration() }) } func (s *ServerSuite) TestRunServer_BadListener() { monkey.Patch(quic.ListenAddr, func(string, *tls.Config, *quic.Config) (quic.Listener, error) { - return nil, errors.New("test-error") + return nil, stderr.New("test-error") }) s.runServerTest() @@ -63,7 +66,7 @@ func (s *ServerSuite) TestRunServer_BadListener() { func (s *ServerSuite) TestRunServer_APIConnection() { ctx, cancel := newContext() - monkey.Patch(shared.GetDefaultLanListeningAddress, func(current, gateway string) (string, []int64) { + monkey.Patch(gateway.GetDefaultLanListeningAddress, func(current, gateway string) (string, []int64) { return "127.0.0.1", nil }) @@ -78,7 +81,7 @@ func (s *ServerSuite) TestRunServer_APIConnection() { api.RunServer(ctx, cancel, false) }() - addr, _ := shared.GetDefaultLanListeningAddress("127.0.0.1", "") + addr, _ := gateway.GetDefaultLanListeningAddress("127.0.0.1", "") conn, err := openQuicSession_test("127.0.0.1", 9090) assert.Nil(s.T(), err) @@ -86,7 +89,7 @@ func (s *ServerSuite) TestRunServer_APIConnection() { stream, err := conn.OpenStream(ctx) assert.Nil(s.T(), err) - sessionHeader := shared.QPepHeader{ + sessionHeader := protocol.QPepHeader{ SourceAddr: &net.TCPAddr{ IP: net.ParseIP(addr), },