Просмотр исходного кода

Add tests for socks dialer

tags/v2.0.0-rc1
9seconds 5 лет назад
Родитель
Сommit
88e076a283
5 измененных файлов: 124 добавлений и 20 удалений
  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 Просмотреть файл

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

+ 2
- 0
go.sum Просмотреть файл

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
 github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6 h1:4NNbNM2Iq/k57qEu7WfL67UrbPq1uFWxW4qODCohi+0=
3
 github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6 h1:4NNbNM2Iq/k57qEu7WfL67UrbPq1uFWxW4qODCohi+0=
2
 github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6/go.mod h1:J29hk+f9lJrblVIfiJOtTFk+OblBawmib4uz/VdKzlg=
4
 github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6/go.mod h1:J29hk+f9lJrblVIfiJOtTFk+OblBawmib4uz/VdKzlg=
3
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

+ 10
- 20
mtglib/network/default_test.go Просмотреть файл

3
 import (
3
 import (
4
 	"context"
4
 	"context"
5
 	"net/http"
5
 	"net/http"
6
-	"net/http/httptest"
7
-	"strings"
8
 	"testing"
6
 	"testing"
9
 
7
 
10
 	"github.com/9seconds/mtg/v2/mtglib/network"
8
 	"github.com/9seconds/mtg/v2/mtglib/network"
11
-	"github.com/mccutchen/go-httpbin/httpbin"
12
 	"github.com/stretchr/testify/suite"
9
 	"github.com/stretchr/testify/suite"
13
 )
10
 )
14
 
11
 
15
 type DefaultDialerTestSuite struct {
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
 func (suite *DefaultDialerTestSuite) SetupSuite() {
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
 	d, err := network.NewDefaultDialer(0, 0)
21
 	d, err := network.NewDefaultDialer(0, 0)
30
 
22
 
31
 	suite.NoError(err)
23
 	suite.NoError(err)
33
 	suite.d = d
25
 	suite.d = d
34
 }
26
 }
35
 
27
 
36
-func (suite *DefaultDialerTestSuite) TearDownSuite() {
37
-	suite.srv.Close()
38
-}
39
-
40
 func (suite *DefaultDialerTestSuite) TestNegativeTimeout() {
28
 func (suite *DefaultDialerTestSuite) TestNegativeTimeout() {
41
 	_, err := network.NewDefaultDialer(-1, 0)
29
 	_, err := network.NewDefaultDialer(-1, 0)
42
 
30
 
50
 }
38
 }
51
 
39
 
52
 func (suite *DefaultDialerTestSuite) TestUnsupportedProtocol() {
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
 	suite.Error(err)
45
 	suite.Error(err)
56
 }
46
 }
58
 func (suite *DefaultDialerTestSuite) TestCannotDial() {
48
 func (suite *DefaultDialerTestSuite) TestCannotDial() {
59
 	_, err := suite.d.DialContext(context.Background(),
49
 	_, err := suite.d.DialContext(context.Background(),
60
 		"tcp",
50
 		"tcp",
61
-		suite.srvAddress+suite.srvAddress)
51
+		suite.HTTPServerAddress()+suite.HTTPServerAddress())
62
 
52
 
63
 	suite.Error(err)
53
 	suite.Error(err)
64
 }
54
 }
66
 func (suite *DefaultDialerTestSuite) TestConnectOk() {
56
 func (suite *DefaultDialerTestSuite) TestConnectOk() {
67
 	conn, err := suite.d.DialContext(context.Background(),
57
 	conn, err := suite.d.DialContext(context.Background(),
68
 		"tcp",
58
 		"tcp",
69
-		suite.srvAddress)
59
+		suite.HTTPServerAddress())
70
 
60
 
71
 	suite.NoError(err)
61
 	suite.NoError(err)
72
 	suite.NotNil(conn)
62
 	suite.NotNil(conn)
74
 	conn.Close()
64
 	conn.Close()
75
 }
65
 }
76
 
66
 
77
-func (suite *DefaultDialerTestSuite) TestRequest() {
67
+func (suite *DefaultDialerTestSuite) TestHTTPRequest() {
78
 	httpClient := http.Client{
68
 	httpClient := http.Client{
79
 		Transport: &http.Transport{
69
 		Transport: &http.Transport{
80
 			DialContext: suite.d.DialContext,
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
 	suite.NoError(err)
76
 	suite.NoError(err)
87
 
77
 

+ 27
- 0
mtglib/network/init_test.go Просмотреть файл

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 Просмотреть файл

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
+}

Загрузка…
Отмена
Сохранить