Skip to content

Commit

Permalink
cleanup: fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
parvit committed Oct 8, 2024
1 parent 5a658f0 commit 16635c2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
22 changes: 22 additions & 0 deletions workers/client/client_patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
21 changes: 0 additions & 21 deletions workers/client/client_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"net"
"reflect"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -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)
}
5 changes: 3 additions & 2 deletions workers/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ type ClientSuite struct {
}

func (s *ClientSuite) BeforeTest(_, testName string) {
gateway.SetSystemProxy(false)
api.Statistics.Reset()
proxyListener = nil

gateway.UsingProxy = false
Expand All @@ -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) {
Expand Down
23 changes: 13 additions & 10 deletions workers/gateway/gateway_interface_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -77,6 +70,16 @@ func getRouteListeningAddresses() []string {
return outAddress
}

func getRouteGatewayInterfaces() ([]int64, []string, error) {
defaultIP, err := gateway.DiscoverGateway()

Check failure on line 74 in workers/gateway/gateway_interface_darwin.go

View workflow job for this annotation

GitHub Actions / build-mac-os

undefined: gateway
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 {
Expand Down
15 changes: 7 additions & 8 deletions workers/gateway/gateway_interface_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()

Check failure on line 26 in workers/gateway/gateway_interface_linux.go

View workflow job for this annotation

GitHub Actions / build-linux

undefined: gateway
if err != nil {
Expand All @@ -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) {
Expand Down

0 comments on commit 16635c2

Please sign in to comment.