-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlister_tls_test.go
264 lines (244 loc) · 10.1 KB
/
lister_tls_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package net
import (
"errors"
"net"
"os"
"testing"
)
type tmpFile struct {
Filename string
}
func (tfo *tmpFile) Clean() { _ = os.RemoveAll(tfo.Filename) }
func newTmpFile(content []byte) (ret *tmpFile) {
var (
err error
fh *os.File
)
ret = new(tmpFile)
if fh, err = os.CreateTemp(os.TempDir(), ""); err != nil {
ret = nil
return
}
defer func() { _ = fh.Close() }()
ret.Filename = fh.Name()
_, _ = fh.Write(content)
return
}
func getKeyEcdsa() []byte {
return []byte(`-----BEGIN PRIVATE KEY-----
MIG2AgEAMBAGByqGSM49AgEGBSuBBAAiBIGeMIGbAgEBBDCJGaDfRSjCg2zYopdy
M7SqBKeIpcEriH3GWTtwy3hlQSiloiyGOk25Ekpt/Ha04PahZANiAARu/6BxP3/t
kYuOdvDeAKD9fsC2m3pLEOzM+ZY8phS7qg4CTFT7Yej8UTaEX1WSd4Sq5F/zmLto
BE2ulX63u0MqdUd/GU6XIpn31kDt2MVqKgprixw7Ow3zIH47KDvdwa0=
-----END PRIVATE KEY-----`)
}
func getCrtEcdsa() []byte {
return []byte(`-----BEGIN CERTIFICATE-----
MIICHjCCAaSgAwIBAgIUCUXV3GKvC6699j1lRVwby9bvbVQwCgYIKoZIzj0EAwIw
RTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGElu
dGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAgFw0yMzExMjcxNjI4MDFaGA8yMTIzMTEw
MzE2MjgwMVowRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAf
BgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDB2MBAGByqGSM49AgEGBSuB
BAAiA2IABG7/oHE/f+2Ri4528N4AoP1+wLabeksQ7Mz5ljymFLuqDgJMVPth6PxR
NoRfVZJ3hKrkX/OYu2gETa6Vfre7Qyp1R38ZTpcimffWQO3YxWoqCmuLHDs7DfMg
fjsoO93BraNTMFEwHQYDVR0OBBYEFB+2RT/hFPvsibVkD5YixFyDSMAmMB8GA1Ud
IwQYMBaAFB+2RT/hFPvsibVkD5YixFyDSMAmMA8GA1UdEwEB/wQFMAMBAf8wCgYI
KoZIzj0EAwIDaAAwZQIwUDdaraaLyrL2+Lmj0xTvPI4+zUJ2qVPcVMgzKiElDCk+
75dcyLpKakC/CIgcIzhOAjEAouUFwLdcMQClcToO2jH0n3KWIJXv/y/X3yLN/t+W
4R/MDRGuSjdIm80Rtr3DbnB7
-----END CERTIFICATE-----`)
}
func getKeyRsa() []byte {
return []byte(`-----BEGIN PRIVATE KEY-----
MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQC1qBMazIQ8/o5v
ZtDIr19s05qKB+Kii7KhUZtu/tugRhEh6l5mev/Qei34+CNcYFOQnsR/0x8wV74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BCwUAA4ICAQCuLlvHWgbQrhVj2E+kyUgbhE4nQoomxdx1AZFbO22+
fx6RN8/fR8lCMADlDGQK291pZvD6XqIJiAIrKxKnw1MzynwxXZH+6QbdWgwHR+ft
hdLypNu4/AJ8zGBXWyFam8fMMWr/WtfiVDXY7EqmIqHqD5NUdFRbSZ74y04l8WS3
DqJz8ZyiEevKaCn9ClPKOYRyTikSrelBAAkb/41WcXwEDqZQ21aZOSCpK3g/4Dwy
6p7J4E0ySFUliSGrDVXmPc70H96g4pwBBfMjwhuZIM9rnoDnH2Mavw6PPYSVS1Rl
Yl8u1rK8AAzCuppK+dQoLx/AU4RgrZygSsPa6dp7giG5lEjq/DXC5F2+LO2bSpXV
aqJNpPZ2+eO1G+30zKSfI9FShyapdi0KlVDR/1RD7f/ubQRF5imZe+9lDXaA+ZKy
axmY68Y40LEYRNH9qoqaCTmxxLwkJU4YNjK4ZRUGtRXVmjOqZVCpqF41JIIDqi/w
W0EqVBaRDYmLHNY3UJWcGSy0aaJh96Du6oojuo3MgLoJun367a8Uw42RMvCLu9GE
gf5z+NriNZFUQDACfnDynuYaa8Fj6IPXy/Y18WVsNqiIg8vYCcoNE8xpV2lP1uqj
1vtLWXn39KuQvOcFWuG0zWR6Z/YbSQaY6He4G8Tc3M2JRRmVZAuA2xRFjXHthaCL
6Q==
-----END CERTIFICATE-----`)
}
func TestLoadX509KeyPair(t *testing.T) {
const testAddress1 = "127.0.0.1:18088"
var (
err error
key *tmpFile
crt *tmpFile
nut Interface
)
key, crt = newTmpFile([]byte("123")), newTmpFile([]byte("321"))
defer func() { key.Clean(); crt.Clean() }()
nut = New().
Handler(getTestHandlerFn(false)).
ListenAndServeTLS(testAddress1, crt.Filename, key.Filename, nil)
if err = nut.Error(); err == nil {
t.Errorf("функция ListenAndServeTLS() повреждена")
}
}
func TestListenAndServeTLSOk(t *testing.T) {
const testAddress1 = "127.0.0.1:18088"
var (
err error
key *tmpFile
crt *tmpFile
nut Interface
)
key, crt = newTmpFile(getKeyEcdsa()), newTmpFile(getCrtEcdsa())
defer func() { key.Clean(); crt.Clean() }()
nut = New().
Handler(getTestHandlerFn(false)).
ListenAndServeTLS(testAddress1, crt.Filename, key.Filename, nil)
if err = nut.Error(); err != nil {
t.Errorf("функция ListenAndServeTLS(), ошибка: %v, ожидалось: %v", err, nil)
}
if err = nut.
Stop().
Error(); err != nil {
t.Errorf("функция Stop(), ошибка: %v, ожидалось: %v", err, nil)
}
}
func TestInvalidPortTLS(t *testing.T) {
const invalidAddress = ":170000"
var (
key *tmpFile
crt *tmpFile
nut Interface
)
key, crt = newTmpFile(getKeyEcdsa()), newTmpFile(getCrtEcdsa())
defer func() { key.Clean(); crt.Clean() }()
nut = New().
ListenAndServeTLS(invalidAddress, crt.Filename, key.Filename, nil)
if nut.Error() == nil {
t.Errorf("функция ListenAndServeTLS(), не корректная проверка адреса")
}
}
func TestTLSNoConfigurationError(t *testing.T) {
var wsv = New()
wsv.ListenAndServeTLSWithConfig(nil, nil)
defer wsv.Stop()
if wsv.Error() == nil {
t.Errorf("функция ListenAndServeTLSWithConfig(), не корректная проверка адреса")
}
if !errors.Is(wsv.Error(), Errors().NoConfiguration()) {
t.Errorf("функция ListenAndServeTLSWithConfig(), получена не корректная ошибка")
}
}
func TestNewListenerTLS(t *testing.T) {
var (
err error
key *tmpFile
crt *tmpFile
nut Interface
cfg *Configuration
lst net.Listener
)
key, crt = newTmpFile(getKeyEcdsa()), newTmpFile(getCrtEcdsa())
defer func() { key.Clean(); crt.Clean() }()
cfg = new(Configuration)
defaultConfiguration(cfg)
cfg.Port = 18080
nut = New().
Handler(getTestHandlerFn(false))
if lst, _, err = nut.
NewListenerTLS(cfg, nil); err == nil {
t.Errorf("функция NewListenerTLS(), функция повреждена")
}
cfg.TLSPrivateKeyPEM, cfg.TLSPublicKeyPEM, cfg.Port = key.Filename, crt.Filename, 18081
if lst, _, err = nut.
NewListenerTLS(cfg, nil); err != nil {
t.Errorf("функция NewListenerTLS(), ошибка: %v, ожидалось: %v", err, nil)
}
nut = nut.Serve(lst)
if err = nut.Error(); err != nil {
t.Errorf("функция Serve(), функция повреждена")
}
if lst, _, err = nut.
NewListenerTLS(cfg, nil); err == nil {
t.Errorf(
"функция NewListenerTLS(), ошибка: %v, ожидалось: %v",
err, errors.New("listen tcp :18081: bind: address already in use"),
)
}
if err = nut.Stop().
Error(); err != nil {
t.Errorf("функция Stop(), ошибка: %v, ожидалось: %v", err, nil)
}
}