-
Notifications
You must be signed in to change notification settings - Fork 47
/
main_test.go
444 lines (372 loc) · 10.2 KB
/
main_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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package main
import (
"bytes"
"encoding/hex"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"os"
"strconv"
"testing"
"time"
"github.com/xindong/frontd/aes256cbc"
"github.com/xindong/frontd/reuse"
"golang.org/x/net/websocket"
)
var (
_echoServerAddr = []byte("127.0.0.1:62863")
_blackHoleServerAddr = []byte("127.0.0.1:62864")
_httpServerAddr = []byte("127.0.0.1:62865")
_websocketServerAddr = []byte("127.0.0.1:62866")
_expectAESCiphertext = []byte("U2FsdGVkX19KIJ9OQJKT/yHGMrS+5SsBAAjetomptQ0=")
_secret = []byte("p0S8rX680*48")
_defaultFrontdAddr = "127.0.0.1:" + strconv.Itoa(_DefaultPort)
)
var (
// use -reuse with go test enable SO_REUSEPORT
// go test -parallel 6553 -benchtime 60s -bench BenchmarkEchoParallel -reuse
// but it seems will not working with single backend addr because of
// http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t
reuseTest = flag.Bool("reuse", false, "test reuseport dialer")
)
func TestMain(m *testing.M) {
flag.Parse()
if *reuseTest {
fmt.Println("testing SO_REUSEPORT")
}
// start echo server
go servEcho()
// start frontd
os.Setenv("SECRET", string(_secret))
os.Setenv("BACKEND_TIMEOUT", "1")
os.Setenv("MAX_HTTP_HEADER_SIZE", "1024")
os.Setenv("PPROF_PORT", "62866")
go main()
// start http server
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
if len(r.Header["X-Forwarded-For"]) > 0 {
w.Write([]byte(r.Header["X-Forwarded-For"][0]))
}
})
go http.ListenAndServe(string(_httpServerAddr), nil)
// start webapp server
http.Handle("/echo", websocket.Handler(func(ws *websocket.Conn) {
io.Copy(ws, ws)
}))
go http.ListenAndServe(string(_websocketServerAddr), nil)
rand.Seed(time.Now().UnixNano())
// wait for servers to start
time.Sleep(time.Second)
os.Exit(m.Run())
}
func servEcho() {
l, err := net.Listen("tcp", string(_echoServerAddr))
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
// Close the listener when the application closes.
defer l.Close()
fmt.Println("Listening on " + string(_echoServerAddr))
for {
// Listen for an incoming connection.
c, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
// Handle connections in a new goroutine.
go func(c net.Conn) {
defer c.Close()
_, err := io.Copy(c, c)
switch err {
case io.EOF:
err = nil
return
case nil:
return
}
panic(err)
}(c)
}
}
// TestTextDecryptAES ---
func TestTextDecryptAES(t *testing.T) {
o := aes256cbc.New()
dec, err := o.DecryptString(_secret, _expectAESCiphertext)
if err != nil {
panic(err)
}
if !bytes.Equal(dec, _echoServerAddr) {
panic(errors.New("not match"))
}
}
// TestHTTPServer ---
func TestHTTPServer(t *testing.T) {
cipherAddr, err := encryptText(_httpServerAddr, _secret)
if err != nil {
panic(err)
}
hdrs := map[string]string{
string(_hdrCipherOrigin): string(cipherAddr),
"X-Forwarded-For": "8.8.8.8, 8.8.4.4",
}
testHTTPServer(hdrs, "OK127.0.0.1")
testWebSocketServer(hdrs, "OK127.0.0.1")
}
func encryptText(plaintext, passphrase []byte) ([]byte, error) {
o := aes256cbc.New()
return o.EncryptString(passphrase, plaintext)
}
func testHTTPServer(hdrs map[string]string, expected string) {
client := &http.Client{}
req, _ := http.NewRequest("GET", "http://"+string(_defaultFrontdAddr), nil)
for k, v := range hdrs {
req.Header.Set(k, v)
}
res, err := client.Do(req)
if err != nil {
panic(err)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
if !bytes.HasPrefix(b, []byte("OK127.0.0.1")) {
panic(fmt.Errorf("http reply not match: %s", string(b)))
}
}
func testWebSocketServer(hdrs map[string]string, expected string) {
origin := "http://127.0.0.1/"
url := "ws://" + string(_defaultFrontdAddr) + "/echo"
cfg, err := websocket.NewConfig(url, origin)
if err != nil {
panic(err)
}
for k, v := range hdrs {
cfg.Header.Set(k, v)
}
ws, err := websocket.DialConfig(cfg)
if err != nil {
panic(err)
}
if _, err := ws.Write([]byte(expected)); err != nil {
panic(err)
}
var msg = make([]byte, len(expected))
var n int
if n, err = ws.Read(msg); err != nil {
panic(err)
}
if expected != string(msg[:n]) {
log.Println(string(msg[:n]))
log.Println(expected)
panic(fmt.Errorf("websocket reply not match: %s", string(msg[:n])))
}
}
// TestEchoServer ---
func TestEchoServer(t *testing.T) {
var conn net.Conn
var err error
if *reuseTest {
conn, err = reuseport.Dial("tcp", "127.0.0.1:0", string(_echoServerAddr))
} else {
conn, err = dialTimeout("tcp", string(_echoServerAddr), time.Second*time.Duration(_BackendDialTimeout))
}
if err != nil {
panic(err)
}
defer conn.Close()
n := rand.Int() % 10
for i := 0; i < n; i++ {
testEchoRound(conn)
}
}
func testEchoRound(conn net.Conn) {
conn.SetDeadline(time.Now().Add(time.Second * 10))
n := rand.Int()%2048 + 10
out := randomBytes(n)
n0, err := conn.Write(out)
if err != nil {
panic(err)
}
rcv := make([]byte, n)
n1, err := io.ReadFull(conn, rcv)
if err != nil && err != io.EOF {
panic(err)
}
if !bytes.Equal(out[:n0], rcv[:n1]) {
fmt.Println("out: ", n0, "in:", n1)
fmt.Println("out: ", hex.EncodeToString(out), "in:", hex.EncodeToString(rcv))
panic(errors.New("echo server reply is not match"))
}
}
func randomBytes(n int) []byte {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i := 0; i < n; i++ {
b[i] = byte(rand.Int())
}
return b
}
// TestProtocolDecrypt ---
func TestProtocolDecrypt(*testing.T) {
b, err := encryptText(_echoServerAddr, _secret)
if err != nil {
panic(err)
}
testProtocol(append(b, '\n'), nil)
// test cached hitted
testProtocol(append(b, '\n'), nil)
}
func testProtocol(cipherAddr, expected []byte) {
// * test decryption
var conn net.Conn
var err error
if *reuseTest {
conn, err = reuseport.Dial("tcp", "127.0.0.1:0", _defaultFrontdAddr)
} else {
conn, err = dialTimeout("tcp", _defaultFrontdAddr, time.Second*time.Duration(_BackendDialTimeout))
}
if err != nil {
panic(err)
}
defer conn.Close()
_, err = conn.Write(cipherAddr)
if err != nil {
panic(err)
}
if expected != nil {
buf := make([]byte, len(expected))
n, err := io.ReadFull(conn, buf)
if err != nil && err != io.EOF {
panic(err)
}
if !bytes.Equal(expected, buf[:n]) {
fmt.Println(buf[:n])
fmt.Println(string(buf[:n]))
fmt.Println(string(expected))
panic("expected reply not matched")
}
return
}
for i := 0; i < 5; i++ {
testEchoRound(conn)
}
}
// TestBinaryProtocolDecrypt ---
func TestBinaryProtocolDecrypt(*testing.T) {
o := aes256cbc.New()
b, err := o.Encrypt(_secret, _echoServerAddr)
if err != nil {
panic(err)
}
testProtocol(append(append([]byte{0}, byte(len(b))), b...), nil)
}
func TestBackendError(*testing.T) {
b, err := encryptText(_blackHoleServerAddr, _secret)
if err != nil {
panic(err)
}
testProtocol(append(b, '\n'), []byte("4102"))
}
func TestBackendBinEmptyCipherReadErr(*testing.T) {
testProtocol([]byte{0, 0}, []byte("4103"))
}
func TestBackendBinCipherDecryptErr(*testing.T) {
testProtocol([]byte{0, 1, 3}, []byte("4106"))
}
func TestDecryptError(*testing.T) {
testProtocol(append([]byte("2hws28"), '\n'), []byte("4106"))
testProtocol(append([]byte("MjF3MjE="), '\n'), []byte("4106"))
testProtocol(append([]byte("MjF3MjFldWhmMjh1ZTRoMjhoMzJlZDAzdzIwOWUzOTAyZWZqY2Vpd2hudmNpdXJoZXZ1aWllaGY4MjExOXZma25p6IOh5qOuMjF3MjFldWhmMjh1ZTRoMjhoMzJlZDAzdzIwOWUzOTAyZWZqY2Vpd2hudmNpdXJoZXZ1aWllaGY4MjExOXZma25p6IOh5qOuMjF3MjFldWhmMjh1ZTRoMjhoMzJlZDAzdzIwOWUzOTAyZWZqY2Vpd2hudmNpdXJoZXZ1aWllaGY4MjExOXZma25p6IOh5qOuMjF3MjFldWhmMjh1ZTRoMjhoMzJlZDAzdzIwOWUzOTAyZWZqY2Vpd2hudmNpdXJoZXZ1aWllaGY4MjExOXZma25p6IOh5qOuDQoNCjIxdzIxZXVoZjI4dWU0aDI4aDMyZWQwM3cyMDllMzkwMmVmamNlaXdobnZjaXVyaGV2dWlpZWhmODIxMTl2ZmtuaeiDoeajrjIxdzIxZXVoZjI4dWU0aDI4aDMyZWQwM3cyMDllMzkwMmVmamNlaXdobnZjaXVyaGV2dWlpZWhmODIxMTl2ZmtuaeiDoeajrjIxdzIxZXVoZjI4dWU0aDI4aDMyZWQwM3cyMDllMzkwMmVmamNlaXdobnZjaXVyaGV2dWlpZWhmODIxMTl2ZmtuaeiDoeajrg0KDQoyMXcyMWV1aGYyOHVlNGgyOGgzMmVkMDN3MjA5ZTM5MDJlZmpjZWl3aG52Y2l1cmhldnVpaWVoZjgyMTE5dmZrbmnog6Hmo64"), '\n'),
[]byte("4106"))
}
func TestBackendTimeout(*testing.T) {
b, err := encryptText([]byte("8.8.8.8:80"), _secret)
if err != nil {
panic(err)
}
testProtocol(append(b, '\n'), []byte("4101"))
}
// TODO: test error 0x07 - 0x10
// TODO: more test with and with out x-forwarded-for
// TODO: test decryption with extra bytes in packet and check data
// TODO: test decryption with seperated packet simulate loss connection and check data
// benchmarks
// TODO: benchmark 100, 1000 connect with 1k 10k 100k 1m data
func BenchmarkEncryptText(b *testing.B) {
s1 := randomBytes(255)
s2 := randomBytes(32)
for i := 0; i < b.N; i++ {
_, err := encryptText(s1, s2)
if err != nil {
panic(err)
}
}
}
func BenchmarkDecryptText(b *testing.B) {
for i := 0; i < b.N; i++ {
o := aes256cbc.New()
_, err := o.DecryptString(_secret, _expectAESCiphertext)
if err != nil {
panic(err)
}
}
}
func BenchmarkEcho(b *testing.B) {
for i := 0; i < b.N; i++ {
TestEchoServer(&testing.T{})
}
}
func BenchmarkLatency(b *testing.B) {
cipherAddr, err := encryptText(_echoServerAddr, _secret)
if err != nil {
panic(err)
}
for i := 0; i < b.N; i++ {
testProtocol(append(cipherAddr, '\n'), nil)
}
}
func BenchmarkNoHitLatency(b *testing.B) {
for i := 0; i < b.N; i++ {
TestProtocolDecrypt(&testing.T{})
}
}
func BenchmarkEchoParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
TestEchoServer(&testing.T{})
}
})
}
func BenchmarkLatencyParallel(b *testing.B) {
cipherAddr, err := encryptText(_echoServerAddr, _secret)
if err != nil {
panic(err)
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
testProtocol(append(cipherAddr, '\n'), nil)
}
})
}
func BenchmarkNoHitLatencyParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
TestProtocolDecrypt(&testing.T{})
}
})
}
// with echo server with random hanging
// * benchmark latency
// * benchmark throughput
// * benchmark copy-on-write performance BackendAddrCache
// * benchmark memory footprint