forked from realDragonium/Ultraviolet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_test.go
663 lines (604 loc) · 17.4 KB
/
backend_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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
package worker_test
import (
"bytes"
"errors"
"fmt"
"net"
"strings"
"sync"
"testing"
"time"
"github.com/pires/go-proxyproto"
"github.com/realDragonium/Ultraviolet/config"
"github.com/realDragonium/Ultraviolet/core"
"github.com/realDragonium/Ultraviolet/mc"
"github.com/realDragonium/Ultraviolet/module"
"github.com/realDragonium/Ultraviolet/worker"
)
var (
defaultChTimeout = 25 * time.Millisecond
)
var RequestStateInfo = []struct {
reqType mc.HandshakeState
denyAction worker.BackendAction
unknownAction worker.BackendAction
onlineAction worker.BackendAction
offlineAction worker.BackendAction
rateLimitAction worker.BackendAction
}{
{
reqType: mc.Status,
denyAction: worker.Close,
unknownAction: worker.SendStatus,
onlineAction: worker.Proxy,
offlineAction: worker.SendStatus,
rateLimitAction: worker.Close,
},
{
reqType: mc.Login,
denyAction: worker.Disconnect,
unknownAction: worker.Close,
onlineAction: worker.Proxy,
offlineAction: worker.Disconnect,
rateLimitAction: worker.Close,
},
}
var ErrNoResponse = errors.New("there was no response from worker")
var port *int16
var portLock sync.Mutex = sync.Mutex{}
func testAddr() string {
portLock.Lock()
defer portLock.Unlock()
if port == nil {
port = new(int16)
*port = 27000
}
addr := fmt.Sprintf("127.0.0.1:%d", *port)
*port++
return addr
}
func defaultOfflineStatusPacket() mc.Packet {
return defaultOfflineStatus().Marshal()
}
func defaultOfflineStatus() mc.SimpleStatus {
return mc.SimpleStatus{
Name: "Ultraviolet-ff",
Protocol: 755,
Description: "offline proxy being tested",
}
}
var ErrEmptyConnCreator = errors.New("this is a test conn creator which doesnt provide connections")
type testConnCreator struct {
}
func (creator testConnCreator) Conn() func() (net.Conn, error) {
return func() (net.Conn, error) {
return nil, ErrEmptyConnCreator
}
}
type testHandshakeModifier struct {
hasBeenCalled bool
}
func (modifier *testHandshakeModifier) Modify(hs *mc.ServerBoundHandshake, addr string) {
modifier.hasBeenCalled = true
}
type testConnectionLimiter struct {
hasBeenCalled bool
allow bool
}
func (limiter *testConnectionLimiter) Allow(reqData core.RequestData) bool {
limiter.hasBeenCalled = true
return limiter.allow
}
type testServerState struct {
hasBeenCalled bool
state core.ServerState
}
func (state *testServerState) State() core.ServerState {
state.hasBeenCalled = true
return state.state
}
type testStatusCache struct {
hasBeenCalled bool
answer mc.Packet
err error
}
func (cache *testStatusCache) Status() (mc.Packet, error) {
cache.hasBeenCalled = true
return cache.answer, cache.err
}
type stateConnCreator struct {
callAmount int
returnError bool
}
func (creator *stateConnCreator) Conn() func() (net.Conn, error) {
creator.callAmount++
if creator.returnError {
return func() (net.Conn, error) {
return nil, ErrEmptyConnCreator
}
}
return func() (net.Conn, error) {
return &net.TCPConn{}, nil
}
}
//Test Help methods
func setupBackendWorker(t *testing.T, serverCfg config.ServerConfig) worker.BackendWorker {
workerServerCfg, err := config.ServerToBackendConfig(serverCfg)
if err != nil {
t.Fatalf("error encounterd: %v", err)
}
serverwrk := worker.NewBackendWorker(workerServerCfg)
return serverwrk
}
func processRequest_TestTimeout(t *testing.T, wrk worker.BackendWorker, req worker.BackendRequest) worker.BackendAnswer {
t.Helper()
answerCh := make(chan worker.BackendAnswer)
go func() {
answer := wrk.HandleRequest(req)
answerCh <- answer
}()
select {
case answer := <-answerCh:
t.Log("worker has successfully responded")
return answer
case <-time.After(defaultChTimeout):
t.Fatal("timed out")
}
return worker.BackendAnswer{}
}
func testCloseConnection(t *testing.T, conn net.Conn) {
if _, err := conn.Write([]byte{0}); err != nil {
t.Errorf("Got an unexpected error: %v", err)
}
}
func samePk(expected, received mc.Packet) bool {
sameID := expected.ID == received.ID
sameData := bytes.Equal(expected.Data, received.Data)
return sameID && sameData
}
func netAddrToIp(addr net.Addr) string {
return strings.Split(addr.String(), ":")[0]
}
func TestBackendWorker_OfflineServer(t *testing.T) {
for _, tc := range RequestStateInfo {
t.Run(fmt.Sprintf("reqType-%v", tc.reqType), func(t *testing.T) {
serverAddr := "backend"
disconnectMessage := "Some disconnect message right here"
disconPacket := mc.ClientBoundDisconnect{
Reason: mc.Chat(disconnectMessage),
}.Marshal()
serverCfg := config.ServerConfig{
Domains: []string{serverAddr},
ProxyTo: "1",
OfflineStatus: defaultOfflineStatus(),
DisconnectMessage: disconnectMessage,
}
req := worker.BackendRequest{
core.RequestData{
Type: tc.reqType,
},
make(chan<- worker.BackendAnswer),
}
offlineStatusPk := defaultOfflineStatusPacket()
wrk := setupBackendWorker(t, serverCfg)
answer := processRequest_TestTimeout(t, wrk, req)
if answer.Action() != tc.offlineAction {
t.Errorf("expected: %v - got: %v", tc.offlineAction, answer.Action())
}
receivedPacket := answer.Response()
if tc.reqType == mc.Status {
if !samePk(offlineStatusPk, receivedPacket) {
offlineStatus, _ := mc.UnmarshalClientBoundResponse(offlineStatusPk)
receivedStatus, _ := mc.UnmarshalClientBoundResponse(receivedPacket)
t.Errorf("expected: %v - got: %v", offlineStatus, receivedStatus)
}
} else if tc.reqType == mc.Login {
if !samePk(disconPacket, receivedPacket) {
expected, _ := mc.UnmarshalClientDisconnect(disconPacket)
received, _ := mc.UnmarshalClientDisconnect(receivedPacket)
t.Errorf("expected: %v - got: %v", expected, received)
}
}
})
}
}
func TestBackendWorker_OnlineServer(t *testing.T) {
for _, tc := range RequestStateInfo {
t.Run(fmt.Sprintf("reqType-%v", tc.reqType), func(t *testing.T) {
serverAddr := "backend"
targetAddr := testAddr()
serverCfg := config.ServerConfig{
Domains: []string{serverAddr},
ProxyTo: targetAddr,
}
req := worker.BackendRequest{
ReqData: core.RequestData{
Type: tc.reqType,
},
}
listener, err := net.Listen("tcp", targetAddr)
if err != nil {
t.Fatal(err)
}
go func() {
listener.Accept()
}()
wrk := setupBackendWorker(t, serverCfg)
answer := processRequest_TestTimeout(t, wrk, req)
if answer.Action() != tc.onlineAction {
t.Fatalf("expected: %v - got: %v", tc.onlineAction, answer.Action())
}
serverConn, _ := answer.ServerConn()
testCloseConnection(t, serverConn)
if answer.ProxyCh() == nil {
t.Error("No proxy channel provided")
}
})
}
}
func TestBackendWorker_HandshakeModifier(t *testing.T) {
hsModifier := testHandshakeModifier{}
wrk := worker.BackendWorker{
HsModifier: &hsModifier,
ServerState: module.AlwaysOnlineState{},
ConnCreator: testConnCreator{},
}
req := worker.BackendRequest{
ReqData: core.RequestData{
Type: mc.Login,
Handshake: mc.ServerBoundHandshake{
ServerAddress: "Something",
},
Addr: &net.TCPAddr{
IP: net.ParseIP("1.1.1.1"),
Port: 25560,
},
},
}
answer := processRequest_TestTimeout(t, wrk, req)
if answer.Action() != worker.Proxy {
t.Fatalf("expected: %v - got: %v", worker.Proxy, answer.Action())
}
if !hsModifier.hasBeenCalled {
t.Error("expected handshake modifier to be called but wasnt")
}
}
func TestBackendWorker_ProxyBind(t *testing.T) {
for _, tc := range RequestStateInfo {
t.Run(fmt.Sprintf("reqType-%v", tc.reqType), func(t *testing.T) {
serverAddr := "backend"
targetAddr := testAddr()
proxyBind := "127.0.0.2"
serverCfg := config.ServerConfig{
Domains: []string{serverAddr},
ProxyTo: targetAddr,
ProxyBind: proxyBind,
}
req := worker.BackendRequest{
ReqData: core.RequestData{
Type: tc.reqType,
},
}
go func() {
wrk := setupBackendWorker(t, serverCfg)
answer := processRequest_TestTimeout(t, wrk, req)
answer.ServerConn() // Calling it instead of the player's goroutine
}()
listener, err := net.Listen("tcp", targetAddr)
if err != nil {
t.Fatal(err)
}
conn, err := listener.Accept() // State check call (proxy bind should be used here too)
if err != nil {
t.Fatal(err)
}
if netAddrToIp(conn.RemoteAddr()) != proxyBind {
t.Errorf("expected: %v - got: %v", proxyBind, netAddrToIp(conn.RemoteAddr()))
}
})
}
}
func TestBackendWorker_ProxyProtocol(t *testing.T) {
for _, tc := range RequestStateInfo {
t.Run(fmt.Sprintf("reqType-%v", tc.reqType), func(t *testing.T) {
serverAddr := "backend"
targetAddr := testAddr()
t.Log(targetAddr)
playerAddr := &net.TCPAddr{
IP: net.ParseIP("187.34.26.123"),
Port: 49473,
}
serverCfg := config.ServerConfig{
Domains: []string{serverAddr},
ProxyTo: targetAddr,
SendProxyProtocol: true,
}
req := worker.BackendRequest{
ReqData: core.RequestData{
Type: tc.reqType,
Addr: playerAddr,
},
}
listener, err := net.Listen("tcp", targetAddr)
if err != nil {
t.Fatal(err)
}
proxyListener := &proxyproto.Listener{Listener: listener}
connCh := make(chan net.Conn)
errorCh := make(chan error)
go func() {
for i := 0; i < 2; i++ {
conn, err := proxyListener.Accept()
if err != nil {
errorCh <- err
return
}
connCh <- conn
}
}()
go func() {
wrk := setupBackendWorker(t, serverCfg)
answer := processRequest_TestTimeout(t, wrk, req)
answer.ServerConn() // Calling it instead of the player's goroutine
}()
<-connCh // State check call (no proxy protocol in here)
select {
case err := <-errorCh:
t.Fatalf("error while accepting connection: %v", err)
case conn := <-connCh:
t.Log("connection has been created")
if conn.RemoteAddr().String() != playerAddr.String() {
t.Errorf("expected: %v - got: %v", playerAddr, conn.RemoteAddr())
}
case <-time.After(defaultChTimeout):
t.Error("timed out")
}
})
}
}
func TestBackendWorker_ConnLimiter(t *testing.T) {
tt := []struct {
allowConn bool
shouldAnswerMatch bool
processAnswer worker.BackendAnswer
}{
{
allowConn: true,
shouldAnswerMatch: false,
processAnswer: worker.NewCloseAnswer(),
},
{
allowConn: false,
shouldAnswerMatch: true,
processAnswer: worker.NewCloseAnswer(),
},
}
for _, tc := range tt {
name := fmt.Sprintf("allows connection: %v", tc.allowConn)
t.Run(name, func(t *testing.T) {
connLimiter := testConnectionLimiter{
allow: tc.allowConn,
}
wrk := worker.BackendWorker{
ServerState: module.AlwaysOnlineState{},
ConnCreator: testConnCreator{},
ConnLimiter: &connLimiter,
}
req := worker.BackendRequest{}
processRequest_TestTimeout(t, wrk, req)
if !connLimiter.hasBeenCalled {
t.Error("expected conn limiter to be called but it wasnt")
}
})
}
}
func TestBackendWorker_ServerState(t *testing.T) {
tt := []struct {
reqType mc.HandshakeState
serverState core.ServerState
expectedAction worker.BackendAction
}{
{
reqType: mc.UnknownState,
serverState: core.Online,
expectedAction: worker.Proxy,
},
{
reqType: mc.Login,
serverState: core.Offline,
expectedAction: worker.Disconnect,
},
{
reqType: mc.Status,
serverState: core.Offline,
expectedAction: worker.SendStatus,
},
}
for _, tc := range tt {
name := fmt.Sprintf("reqType:%v - serverState:%v", tc.reqType, tc.serverState)
t.Run(name, func(t *testing.T) {
serverState := testServerState{
state: tc.serverState,
}
wrk := worker.BackendWorker{
ServerState: &serverState,
ConnCreator: testConnCreator{},
}
req := worker.BackendRequest{
ReqData: core.RequestData{
Type: tc.reqType,
},
}
answer := processRequest_TestTimeout(t, wrk, req)
if answer.Action() != tc.expectedAction {
t.Errorf("expected %v but got %v instead", tc.expectedAction, answer.Action())
}
if !serverState.hasBeenCalled {
t.Error("Expected serverstate to be called but wasnt")
}
})
}
}
func TestBackendWorker_Update(t *testing.T) {
t.Run("change when update config contains new value", func(t *testing.T) {
wrk := worker.BackendWorker{}
cfg := worker.BackendConfig{
Name: "UV",
UpdateProxyProtocol: true,
SendProxyProtocol: true,
DisconnectPacket: mc.Packet{ID: 0x45, Data: []byte{0x11, 0x12, 0x13}},
OfflineStatusPacket: mc.Packet{ID: 0x45, Data: []byte{0x11, 0x12, 0x14}},
HsModifier: &testHandshakeModifier{},
ConnCreator: &stateConnCreator{},
ConnLimiter: &testConnectionLimiter{},
ServerState: &testServerState{},
StatusCache: &testStatusCache{},
}
wrk.UpdateSameGoroutine(cfg)
if wrk.Name != cfg.Name {
t.Errorf("expected: %v - got: %v", cfg.Name, wrk.Name)
}
if wrk.SendProxyProtocol != cfg.SendProxyProtocol {
t.Errorf("expected: %v - got: %v", cfg.SendProxyProtocol, wrk.SendProxyProtocol)
}
if !samePk(wrk.DisconnectPacket, cfg.DisconnectPacket) {
t.Errorf("expected: %v - got: %v", cfg.DisconnectPacket, wrk.DisconnectPacket)
}
if !samePk(wrk.OfflineStatusPacket, cfg.OfflineStatusPacket) {
t.Errorf("expected: %v - got: %v", cfg.OfflineStatusPacket, wrk.OfflineStatusPacket)
}
if wrk.HsModifier != cfg.HsModifier {
t.Errorf("expected: %v - got: %v", cfg.HsModifier, wrk.HsModifier)
}
if wrk.ConnCreator != cfg.ConnCreator {
t.Errorf("expected: %v - got: %v", cfg.ConnCreator, wrk.ConnCreator)
}
if wrk.ConnLimiter != cfg.ConnLimiter {
t.Errorf("expected: %v - got: %v", cfg.ConnLimiter, wrk.ConnLimiter)
}
if wrk.StatusCache != cfg.StatusCache {
t.Errorf("expected: %v - got: %v", cfg.StatusCache, wrk.StatusCache)
}
if wrk.ServerState != cfg.ServerState {
t.Errorf("expected: %v - got: %v", cfg.ServerState, wrk.ServerState)
}
})
t.Run("change when update config contains new value", func(t *testing.T) {
wrk := worker.BackendWorker{
Name: "UV",
SendProxyProtocol: true,
DisconnectPacket: mc.Packet{ID: 0x45, Data: []byte{0x11, 0x12, 0x13}},
OfflineStatusPacket: mc.Packet{ID: 0x45, Data: []byte{0x11, 0x12, 0x14}},
HsModifier: &testHandshakeModifier{},
ConnCreator: &stateConnCreator{},
ConnLimiter: &testConnectionLimiter{},
ServerState: &testServerState{},
StatusCache: &testStatusCache{},
}
cfg := worker.BackendConfig{}
wrk.UpdateSameGoroutine(cfg)
if wrk.Name == cfg.Name {
t.Errorf("didnt expect: %v", wrk.Name)
}
if wrk.SendProxyProtocol == cfg.SendProxyProtocol {
t.Errorf("didnt expect: %v", wrk.SendProxyProtocol)
}
if samePk(wrk.DisconnectPacket, cfg.DisconnectPacket) {
t.Errorf("didnt expect: %v", wrk.DisconnectPacket)
}
if samePk(wrk.OfflineStatusPacket, cfg.OfflineStatusPacket) {
t.Errorf("didnt expect: %v", wrk.OfflineStatusPacket)
}
if wrk.HsModifier == cfg.HsModifier {
t.Errorf("didnt expect: %v", wrk.HsModifier)
}
if wrk.ConnCreator == cfg.ConnCreator {
t.Errorf("didnt expect: %v", wrk.ConnCreator)
}
if wrk.ConnLimiter == cfg.ConnLimiter {
t.Errorf("didnt expect: %v", wrk.ConnLimiter)
}
if wrk.StatusCache == cfg.StatusCache {
t.Errorf("didnt expect: %v", wrk.StatusCache)
}
if wrk.ServerState == cfg.ServerState {
t.Errorf("didnt expect: %v", wrk.ServerState)
}
})
t.Run("can update while running", func(t *testing.T) {
wrk := worker.NewEmptyBackendWorker()
wrk.ServerState = module.AlwaysOfflineState{}
reqCh := wrk.ReqCh()
wrk.Run()
testPk := mc.ClientBoundDisconnect{
Reason: mc.String("some text here"),
}.Marshal()
cfg := worker.BackendConfig{
DisconnectPacket: testPk,
}
err := wrk.Update(cfg)
if err != nil {
t.Fatalf("got error: %v", err)
}
ansCh := make(chan worker.BackendAnswer)
reqCh <- worker.BackendRequest{
ReqData: core.RequestData{
Type: mc.Login,
},
Ch: ansCh,
}
ans := <-ansCh
if !samePk(testPk, ans.Response()) {
t.Errorf("expected: %v - got: %v", testPk, ans.Response())
}
wrk.Close()
})
}
func TestBackendFactory(t *testing.T) {
t.Run("lets the worker run", func(t *testing.T) {
cfg := config.BackendWorkerConfig{}
b := worker.BackendFactory(cfg)
reqCh := b.ReqCh()
req := worker.BackendRequest{}
select {
case reqCh <- req:
t.Log("backend is running in different goroutine")
case <-time.After(defaultChTimeout):
t.Error("backend isnt running in different goroutine...?")
}
})
t.Run("doesnt share data between goroutines", func(t *testing.T) {
msg := "some text here"
disconPk := mc.ClientBoundDisconnect{
Reason: mc.String(msg),
}.Marshal()
cfg := config.BackendWorkerConfig{
DisconnectPacket: disconPk,
StateOption: config.ALWAYS_OFFLINE,
}
b := worker.BackendFactory(cfg)
wrk, ok := b.(*worker.BackendWorker)
if !ok {
t.Fatalf("backend is different type then expected")
}
newCfg := worker.BackendConfig{
DisconnectPacket: mc.Packet{ID: 0x44, Data: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}},
}
wrk.UpdateSameGoroutine(newCfg)
reqCh := b.ReqCh()
rCh := make(chan worker.BackendAnswer)
req := worker.BackendRequest{
ReqData: core.RequestData{
Type: mc.Login,
},
Ch: rCh,
}
reqCh <- req
ans := <-rCh
if !samePK(disconPk, ans.Response()) {
t.Error("packets werent the same which might mean its sharing memory")
t.Logf("expected: %v", disconPk)
t.Logf("got: %v", ans.Response())
}
})
}