This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anyport_test.go
140 lines (115 loc) · 4.45 KB
/
anyport_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package anyport
import (
"crypto/tls"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"net"
"testing"
)
type AnyPortTest struct {
suite.Suite
originalListener Listener
listenerMock ListenerMock
}
var (
invalidNetListener net.Listener = newFakeNetListener(-100)
fakeNetListener net.Listener = newFakeNetListener(102)
)
var (
dummyTlsConfig tls.Config
)
func TestAnyPort(t *testing.T) {
suite.Run(t, new(AnyPortTest))
}
func (test *AnyPortTest) SetupTest() {
test.originalListener = CurrentListenerInstance
test.listenerMock = ListenerMock{}
CurrentListenerInstance = &test.listenerMock
}
func (test *AnyPortTest) TearDownTest() {
CurrentListenerInstance = test.originalListener
}
func (test *AnyPortTest) Test_ListenInsecure_Errors_OnUnderlyingError() {
test.listenerMock.On("ListenInsecure", "tcp", "999.0.0.0:0").
Return(invalidNetListener, errors.New("listen error"))
_, err := ListenInsecure("999.0.0.0")
test.Assert().Error(err)
}
func (test *AnyPortTest) Test_ListenSecure_Errors_OnUnderlyingError() {
test.listenerMock.On("ListenSecure", "tcp", "999.0.0.0:0", &dummyTlsConfig).
Return(invalidNetListener, errors.New("listen error"))
_, err := ListenSecure("999.0.0.0", &dummyTlsConfig)
test.Assert().Error(err)
}
func (test *AnyPortTest) Test_ListenInsecure_Succeeds_OnUnderlyingSuccess() {
test.listenerMock.On("ListenInsecure", "tcp", "0.0.0.0:0").
Return(fakeNetListener, nil)
anyPort, err := ListenInsecure("0.0.0.0")
test.Require().NoError(err)
test.Assert().Equal(anyPort.Listener, fakeNetListener)
test.Assert().Equal(102, anyPort.PortNumber)
}
func (test *AnyPortTest) Test_ListenSecure_Succeeds_OnUnderlyingSuccess() {
test.listenerMock.On("ListenSecure", "tcp", "0.0.0.0:0", &dummyTlsConfig).
Return(fakeNetListener, nil)
anyPort, err := ListenSecure("0.0.0.0", &dummyTlsConfig)
test.Require().NoError(err)
test.Assert().Equal(anyPort.Listener, fakeNetListener)
test.Assert().Equal(102, anyPort.PortNumber)
}
func (test *AnyPortTest) Test_ListenRange_Errors_OnNoAvailablePortFound() {
anyAddr := func(string) bool { return true }
test.T().Run("Insecure", func(t *testing.T) {
test.listenerMock.On("ListenInsecure", "tcp", mock.MatchedBy(anyAddr)).
Return(invalidNetListener, errors.New("Insecure port is not available"))
_, err := ListenInsecure("0.0.0.0:100-200")
assert.Error(t, err)
})
test.T().Run("Secure", func(t *testing.T) {
test.listenerMock.On("ListenSecure", "tcp", mock.MatchedBy(anyAddr), &dummyTlsConfig).
Return(invalidNetListener, errors.New("Secure port is not available"))
_, err := ListenSecure("0.0.0.0:200-300", &dummyTlsConfig)
assert.Error(t, err)
})
}
func (test *AnyPortTest) Test_ListenRange_Errors_OnMalformedPortsRange() {
cases := []string{"123-", "a-123", "123-b", "999-1"}
sutFunctions := map[string]func(addr string) (AnyPort, error){
"Insecure": func(addr string) (AnyPort, error) {
return ListenInsecure(addr)
},
"Secure": func(addr string) (AnyPort, error) {
return ListenSecure(addr, &tls.Config{})
},
}
for sutName, sutFn := range sutFunctions {
for _, input := range cases {
testCaseName := fmt.Sprintf("%s(), case:%s", sutName, input)
test.T().Run(testCaseName, func(t *testing.T) {
_, err := sutFn("0.0.0.0:" + input)
assert.Error(t, err)
})
}
}
}
func (test *AnyPortTest) Test_ListenInsecureRange_Succeeds_OnUnderlyingSuccess() {
test.listenerMock.
On("ListenInsecure", "tcp", "0.0.0.0:100").Return(invalidNetListener, errors.New("port already in use")).Once().
On("ListenInsecure", "tcp", "0.0.0.0:101").Return(invalidNetListener, errors.New("permission denied")).Once().
On("ListenInsecure", "tcp", "0.0.0.0:102").Return(fakeNetListener, nil).Once()
anyPort, err := ListenInsecure("0.0.0.0:100-200")
test.Require().NoError(err)
test.Assert().Equal(102, anyPort.PortNumber)
}
func (test *AnyPortTest) Test_ListenSecureRange_Succeeds_OnUnderlyingSuccess() {
test.listenerMock.
On("ListenSecure", "tcp", "0.0.0.0:100", &dummyTlsConfig).Return(invalidNetListener, errors.New("port already in use")).Once().
On("ListenSecure", "tcp", "0.0.0.0:101", &dummyTlsConfig).Return(invalidNetListener, errors.New("permission denied")).Once().
On("ListenSecure", "tcp", "0.0.0.0:102", &dummyTlsConfig).Return(fakeNetListener, nil).Once()
anyPort, err := ListenSecure("0.0.0.0:100-200", &dummyTlsConfig)
test.Require().NoError(err)
test.Assert().Equal(102, anyPort.PortNumber)
}