Skip to content

Commit

Permalink
fix: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
parvit committed Sep 18, 2024
1 parent 2e2157f commit 489ae5b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 74 deletions.
11 changes: 6 additions & 5 deletions service/service_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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])
Expand All @@ -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
})

Expand All @@ -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:
Expand Down Expand Up @@ -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
})

Expand All @@ -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
}
Expand Down
49 changes: 25 additions & 24 deletions workers/client/client_patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
56 changes: 29 additions & 27 deletions workers/gateway/gateway_interface_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
})

Expand All @@ -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()
Expand All @@ -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
}
Expand All @@ -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()
Expand All @@ -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
}
Expand All @@ -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" +
Expand Down Expand Up @@ -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() {
Expand All @@ -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" +
Expand All @@ -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" +
Expand All @@ -192,22 +194,22 @@ 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
})

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
}
Expand All @@ -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(`
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down
Loading

0 comments on commit 489ae5b

Please sign in to comment.