Przeglądaj źródła

Add test for generate-secret command

tags/v2.0.0-rc1
9seconds 5 lat temu
rodzic
commit
546a5849f3

+ 31
- 0
cli/base_internal_test.go Wyświetl plik

@@ -0,0 +1,31 @@
1
+package cli
2
+
3
+import (
4
+	"path/filepath"
5
+	"testing"
6
+
7
+	"github.com/stretchr/testify/suite"
8
+)
9
+
10
+type BaseTestSuite struct {
11
+	suite.Suite
12
+
13
+	b base
14
+}
15
+
16
+func (suite *BaseTestSuite) SetupTest() {
17
+	suite.b = base{}
18
+}
19
+
20
+func (suite *BaseTestSuite) TestReadConfigNok() {
21
+	suite.Error(suite.b.ReadConfig(filepath.Join("testdata", "unknown"), "dev"))
22
+}
23
+
24
+func (suite *BaseTestSuite) TestReadConfig() {
25
+	suite.NoError(suite.b.ReadConfig(filepath.Join("testdata", "minimal.toml"), "dev"))
26
+}
27
+
28
+func TestBase(t *testing.T) {
29
+	t.Parallel()
30
+	suite.Run(t, &BaseTestSuite{})
31
+}

+ 50
- 0
cli/generate_secret_test.go Wyświetl plik

@@ -0,0 +1,50 @@
1
+package cli_test
2
+
3
+import (
4
+	"strings"
5
+	"testing"
6
+
7
+	"github.com/9seconds/mtg/v2/mtglib"
8
+	"github.com/stretchr/testify/suite"
9
+)
10
+
11
+type GenerateSecretTestSuite struct {
12
+	CommonTestSuite
13
+}
14
+
15
+func (suite *GenerateSecretTestSuite) SetupTest() {
16
+	suite.CommonTestSuite.SetupTest()
17
+
18
+	suite.cli.GenerateSecret.HostName = "google.com"
19
+}
20
+
21
+func (suite *GenerateSecretTestSuite) TestDefault() {
22
+	output := suite.CaptureStdout(func() {
23
+		suite.NoError(suite.cli.GenerateSecret.Run(suite.cli, "dev"))
24
+	})
25
+	suite.True(strings.HasPrefix(output, "7"))
26
+
27
+	secret, err := mtglib.ParseSecret(output)
28
+	suite.NoError(err)
29
+	suite.True(secret.Valid())
30
+	suite.Equal("google.com", secret.Host)
31
+}
32
+
33
+func (suite *GenerateSecretTestSuite) TestHex() {
34
+	suite.cli.GenerateSecret.Hex = true
35
+
36
+	output := suite.CaptureStdout(func() {
37
+		suite.NoError(suite.cli.GenerateSecret.Run(suite.cli, "dev"))
38
+	})
39
+	suite.True(strings.HasPrefix(output, "ee"))
40
+
41
+	secret, err := mtglib.ParseSecret(output)
42
+	suite.NoError(err)
43
+	suite.True(secret.Valid())
44
+	suite.Equal("google.com", secret.Host)
45
+}
46
+
47
+func TestGenerateSecret(t *testing.T) {
48
+	t.Parallel()
49
+	suite.Run(t, &GenerateSecretTestSuite{})
50
+}

+ 112
- 0
cli/init_test.go Wyświetl plik

@@ -0,0 +1,112 @@
1
+package cli_test
2
+
3
+import (
4
+	"bytes"
5
+	"context"
6
+	"io"
7
+	"net"
8
+	"net/http"
9
+	"os"
10
+	"strings"
11
+	"time"
12
+
13
+	"github.com/9seconds/mtg/v2/cli"
14
+	"github.com/9seconds/mtg/v2/mtglib/network"
15
+	"github.com/jarcoal/httpmock"
16
+	"github.com/stretchr/testify/mock"
17
+	"github.com/stretchr/testify/suite"
18
+)
19
+
20
+type NetworkMock struct {
21
+	mock.Mock
22
+}
23
+
24
+func (n *NetworkMock) Dial(network, address string) (net.Conn, error) {
25
+	args := n.Called(network, address)
26
+
27
+	return args.Get(0).(net.Conn), args.Error(1)
28
+}
29
+
30
+func (n *NetworkMock) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
31
+	args := n.Called(ctx, network, address)
32
+
33
+	return args.Get(0).(net.Conn), args.Error(1)
34
+}
35
+
36
+func (n *NetworkMock) DNSResolve(network, hostname string) ([]string, error) {
37
+	args := n.Called(network, hostname)
38
+
39
+	return args.Get(0).([]string), args.Error(1)
40
+}
41
+
42
+func (n *NetworkMock) MakeHTTPClient(dialFunc network.DialFunc) *http.Client {
43
+	return n.Called(dialFunc).Get(0).(*http.Client)
44
+}
45
+
46
+func (n *NetworkMock) IdleTimeout() time.Duration {
47
+	return n.Called().Get(0).(time.Duration)
48
+}
49
+
50
+func (n *NetworkMock) HTTPTimeout() time.Duration {
51
+	return n.Called().Get(0).(time.Duration)
52
+}
53
+
54
+type CommonTestSuite struct {
55
+	suite.Suite
56
+
57
+	cli         *cli.CLI
58
+	networkMock *NetworkMock
59
+	httpClient  *http.Client
60
+}
61
+
62
+func (suite *CommonTestSuite) SetupTest() {
63
+	suite.networkMock = &NetworkMock{}
64
+	suite.httpClient = &http.Client{}
65
+	suite.cli = &cli.CLI{}
66
+
67
+	httpmock.ActivateNonDefault(suite.httpClient)
68
+
69
+	suite.networkMock.
70
+		On("MakeHTTPClient", mock.Anything).
71
+		Maybe().
72
+		Return(suite.httpClient)
73
+}
74
+
75
+func (suite *CommonTestSuite) TearDownTest() {
76
+	suite.networkMock.AssertExpectations(suite.T())
77
+	httpmock.DeactivateAndReset()
78
+}
79
+
80
+func (suite *CommonTestSuite) CaptureStdout(callback func()) string {
81
+	return suite.captureOutput(&os.Stdout, callback)
82
+}
83
+
84
+func (suite *CommonTestSuite) CaptureStderr(callback func()) string {
85
+	return suite.captureOutput(&os.Stderr, callback)
86
+}
87
+
88
+func (suite *CommonTestSuite) captureOutput(filefp **os.File, callback func()) string {
89
+	oldFp := *filefp
90
+
91
+	defer func() {
92
+		*filefp = oldFp
93
+	}()
94
+
95
+	reader, writer, _ := os.Pipe()
96
+	buf := &bytes.Buffer{}
97
+	closeChan := make(chan bool)
98
+
99
+	go func() {
100
+		io.Copy(buf, reader) // nolint: errcheck
101
+		close(closeChan)
102
+	}()
103
+
104
+	*filefp = writer
105
+
106
+	callback()
107
+
108
+	writer.Close()
109
+	<-closeChan
110
+
111
+	return strings.TrimSpace(buf.String())
112
+}

+ 2
- 0
cli/testdata/minimal.toml Wyświetl plik

@@ -0,0 +1,2 @@
1
+secret = "7mqFMMq3P2Tvvt_rPx5qhmFnb29nbGUuY29t"
2
+bind-to = "0.0.0.0:80"

+ 1
- 0
go.mod Wyświetl plik

@@ -7,6 +7,7 @@ require (
7 7
 	github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15
8 8
 	github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
9 9
 	github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6
10
+	github.com/jarcoal/httpmock v1.0.8 // indirect
10 11
 	github.com/kr/pretty v0.1.0 // indirect
11 12
 	github.com/libp2p/go-reuseport v0.0.2
12 13
 	github.com/mccutchen/go-httpbin v1.1.1

+ 2
- 0
go.sum Wyświetl plik

@@ -9,6 +9,8 @@ github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6/go.mod h1
9 9
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10 10
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
11 11
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12
+github.com/jarcoal/httpmock v1.0.8 h1:8kI16SoO6LQKgPE7PvQuV+YuD/inwHd7fOOe2zMbo4k=
13
+github.com/jarcoal/httpmock v1.0.8/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
12 14
 github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
13 15
 github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
14 16
 github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

+ 1
- 0
mtglib/network/init.go Wyświetl plik

@@ -40,4 +40,5 @@ type Network interface {
40 40
 	DNSResolve(network, hostname string) (ips []string, err error)
41 41
 	MakeHTTPClient(DialFunc) *http.Client
42 42
 	IdleTimeout() time.Duration
43
+	HTTPTimeout() time.Duration
43 44
 }

+ 8
- 4
mtglib/network/network.go Wyświetl plik

@@ -115,10 +115,6 @@ func (n *network) DNSResolve(protocol, address string) ([]string, error) {
115 115
 	return ips, nil
116 116
 }
117 117
 
118
-func (n *network) IdleTimeout() time.Duration {
119
-	return n.idleTimeout
120
-}
121
-
122 118
 func (n *network) MakeHTTPClient(dialFunc DialFunc) *http.Client {
123 119
 	if dialFunc == nil {
124 120
 		dialFunc = n.DialContext
@@ -127,6 +123,14 @@ func (n *network) MakeHTTPClient(dialFunc DialFunc) *http.Client {
127 123
 	return makeHTTPClient(n.userAgent, n.httpTimeout, dialFunc)
128 124
 }
129 125
 
126
+func (n *network) IdleTimeout() time.Duration {
127
+	return n.idleTimeout
128
+}
129
+
130
+func (n *network) HTTPTimeout() time.Duration {
131
+	return n.httpTimeout
132
+}
133
+
130 134
 func NewNetwork(dialer Dialer,
131 135
 	userAgent, dohHostname string,
132 136
 	httpTimeout, idleTimeout time.Duration) (Network, error) {

Ładowanie…
Anuluj
Zapisz