Procházet zdrojové kódy

Add tests for socks dialer

tags/v2.0.0-rc1
9seconds před 5 roky
rodič
revize
88e076a283
5 změnil soubory, kde provedl 124 přidání a 20 odebrání
  1. 1
    0
      go.mod
  2. 2
    0
      go.sum
  3. 10
    20
      mtglib/network/default_test.go
  4. 27
    0
      mtglib/network/init_test.go
  5. 84
    0
      mtglib/network/socks5_test.go

+ 1
- 0
go.mod Zobrazit soubor

@@ -3,6 +3,7 @@ module github.com/9seconds/mtg/v2
3 3
 go 1.16
4 4
 
5 5
 require (
6
+	github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 // indirect
6 7
 	github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6
7 8
 	github.com/libp2p/go-reuseport v0.0.2
8 9
 	github.com/mccutchen/go-httpbin v1.1.1 // indirect

+ 2
- 0
go.sum Zobrazit soubor

@@ -1,3 +1,5 @@
1
+github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
2
+github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
1 3
 github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6 h1:4NNbNM2Iq/k57qEu7WfL67UrbPq1uFWxW4qODCohi+0=
2 4
 github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6/go.mod h1:J29hk+f9lJrblVIfiJOtTFk+OblBawmib4uz/VdKzlg=
3 5
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

+ 10
- 20
mtglib/network/default_test.go Zobrazit soubor

@@ -3,29 +3,21 @@ package network_test
3 3
 import (
4 4
 	"context"
5 5
 	"net/http"
6
-	"net/http/httptest"
7
-	"strings"
8 6
 	"testing"
9 7
 
10 8
 	"github.com/9seconds/mtg/v2/mtglib/network"
11
-	"github.com/mccutchen/go-httpbin/httpbin"
12 9
 	"github.com/stretchr/testify/suite"
13 10
 )
14 11
 
15 12
 type DefaultDialerTestSuite struct {
16
-	suite.Suite
13
+	HTTPServerTestSuite
17 14
 
18
-	d          network.Dialer
19
-	srvAddress string
20
-	srv        *httptest.Server
15
+	d network.Dialer
21 16
 }
22 17
 
23 18
 func (suite *DefaultDialerTestSuite) SetupSuite() {
24
-	suite.srv = httptest.NewServer(httpbin.NewHTTPBin().Handler())
25
-	suite.srvAddress = strings.TrimPrefix(suite.srv.URL, "http://")
26
-}
19
+	suite.HTTPServerTestSuite.SetupSuite()
27 20
 
28
-func (suite *DefaultDialerTestSuite) SetupTest() {
29 21
 	d, err := network.NewDefaultDialer(0, 0)
30 22
 
31 23
 	suite.NoError(err)
@@ -33,10 +25,6 @@ func (suite *DefaultDialerTestSuite) SetupTest() {
33 25
 	suite.d = d
34 26
 }
35 27
 
36
-func (suite *DefaultDialerTestSuite) TearDownSuite() {
37
-	suite.srv.Close()
38
-}
39
-
40 28
 func (suite *DefaultDialerTestSuite) TestNegativeTimeout() {
41 29
 	_, err := network.NewDefaultDialer(-1, 0)
42 30
 
@@ -50,7 +38,9 @@ func (suite *DefaultDialerTestSuite) TestNegativeBufferSize() {
50 38
 }
51 39
 
52 40
 func (suite *DefaultDialerTestSuite) TestUnsupportedProtocol() {
53
-	_, err := suite.d.DialContext(context.Background(), "udp", suite.srvAddress)
41
+	_, err := suite.d.DialContext(context.Background(),
42
+		"udp",
43
+		suite.HTTPServerAddress())
54 44
 
55 45
 	suite.Error(err)
56 46
 }
@@ -58,7 +48,7 @@ func (suite *DefaultDialerTestSuite) TestUnsupportedProtocol() {
58 48
 func (suite *DefaultDialerTestSuite) TestCannotDial() {
59 49
 	_, err := suite.d.DialContext(context.Background(),
60 50
 		"tcp",
61
-		suite.srvAddress+suite.srvAddress)
51
+		suite.HTTPServerAddress()+suite.HTTPServerAddress())
62 52
 
63 53
 	suite.Error(err)
64 54
 }
@@ -66,7 +56,7 @@ func (suite *DefaultDialerTestSuite) TestCannotDial() {
66 56
 func (suite *DefaultDialerTestSuite) TestConnectOk() {
67 57
 	conn, err := suite.d.DialContext(context.Background(),
68 58
 		"tcp",
69
-		suite.srvAddress)
59
+		suite.HTTPServerAddress())
70 60
 
71 61
 	suite.NoError(err)
72 62
 	suite.NotNil(conn)
@@ -74,14 +64,14 @@ func (suite *DefaultDialerTestSuite) TestConnectOk() {
74 64
 	conn.Close()
75 65
 }
76 66
 
77
-func (suite *DefaultDialerTestSuite) TestRequest() {
67
+func (suite *DefaultDialerTestSuite) TestHTTPRequest() {
78 68
 	httpClient := http.Client{
79 69
 		Transport: &http.Transport{
80 70
 			DialContext: suite.d.DialContext,
81 71
 		},
82 72
 	}
83 73
 
84
-	resp, err := httpClient.Get(suite.srv.URL + "/get")
74
+	resp, err := httpClient.Get(suite.httpServer.URL + "/get")
85 75
 
86 76
 	suite.NoError(err)
87 77
 

+ 27
- 0
mtglib/network/init_test.go Zobrazit soubor

@@ -0,0 +1,27 @@
1
+package network_test
2
+
3
+import (
4
+	"net/http/httptest"
5
+	"strings"
6
+
7
+	"github.com/mccutchen/go-httpbin/httpbin"
8
+	"github.com/stretchr/testify/suite"
9
+)
10
+
11
+type HTTPServerTestSuite struct {
12
+	suite.Suite
13
+
14
+	httpServer *httptest.Server
15
+}
16
+
17
+func (suite *HTTPServerTestSuite) SetupSuite() {
18
+	suite.httpServer = httptest.NewServer(httpbin.NewHTTPBin().Handler())
19
+}
20
+
21
+func (suite *HTTPServerTestSuite) TearDownSuite() {
22
+	suite.httpServer.Close()
23
+}
24
+
25
+func (suite *HTTPServerTestSuite) HTTPServerAddress() string {
26
+	return strings.TrimPrefix(suite.httpServer.URL, "http://")
27
+}

+ 84
- 0
mtglib/network/socks5_test.go Zobrazit soubor

@@ -0,0 +1,84 @@
1
+package network_test
2
+
3
+import (
4
+	"net"
5
+	"net/http"
6
+	"net/url"
7
+	"testing"
8
+
9
+	"github.com/9seconds/mtg/v2/mtglib/network"
10
+	socks5 "github.com/armon/go-socks5"
11
+	"github.com/stretchr/testify/suite"
12
+)
13
+
14
+type Socks5TestSuite struct {
15
+	HTTPServerTestSuite
16
+
17
+	socksListener net.Listener
18
+	socksProxy    *socks5.Server
19
+}
20
+
21
+func (suite *Socks5TestSuite) SetupSuite() {
22
+	suite.HTTPServerTestSuite.SetupSuite()
23
+
24
+	socksConf := socks5.Config{
25
+		Credentials: socks5.StaticCredentials{
26
+			"user": "password",
27
+		},
28
+	}
29
+
30
+	suite.socksProxy, _ = socks5.New(&socksConf)
31
+	suite.socksListener, _ = net.Listen("tcp", "127.0.0.1:0")
32
+
33
+	go suite.socksProxy.Serve(suite.socksListener)
34
+}
35
+
36
+func (suite *Socks5TestSuite) TearDownSuite() {
37
+	suite.socksListener.Close()
38
+
39
+	suite.HTTPServerTestSuite.TearDownSuite()
40
+}
41
+
42
+func (suite *Socks5TestSuite) TestRequestFailed() {
43
+	proxyURL := &url.URL{
44
+		Scheme: "socks5",
45
+		User:   url.UserPassword("user2", "password"),
46
+		Host:   suite.socksListener.Addr().String(),
47
+	}
48
+	dialer, _ := network.NewSocks5Dialer(proxyURL, 0, 0)
49
+
50
+	httpClient := http.Client{
51
+		Transport: &http.Transport{
52
+			DialContext: dialer.DialContext,
53
+		},
54
+	}
55
+
56
+	_, err := httpClient.Get(suite.httpServer.URL + "/get")
57
+
58
+	suite.Error(err)
59
+}
60
+
61
+func (suite *Socks5TestSuite) TestRequestOk() {
62
+	proxyURL := &url.URL{
63
+		Scheme: "socks5",
64
+		User:   url.UserPassword("user", "password"),
65
+		Host:   suite.socksListener.Addr().String(),
66
+	}
67
+	dialer, _ := network.NewSocks5Dialer(proxyURL, 0, 0)
68
+
69
+	httpClient := http.Client{
70
+		Transport: &http.Transport{
71
+			DialContext: dialer.DialContext,
72
+		},
73
+	}
74
+
75
+	resp, err := httpClient.Get(suite.httpServer.URL + "/get")
76
+
77
+	suite.NoError(err)
78
+
79
+	resp.Body.Close()
80
+}
81
+
82
+func TestSocks5TestSuite(t *testing.T) {
83
+	suite.Run(t, &Socks5TestSuite{})
84
+}

Načítá se…
Zrušit
Uložit