-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures_test.go
More file actions
670 lines (582 loc) · 18.3 KB
/
features_test.go
File metadata and controls
670 lines (582 loc) · 18.3 KB
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
664
665
666
667
668
669
670
package fastwire
import (
"bytes"
"net"
"strings"
"testing"
"time"
)
// --- Send Batching Tests ---
func TestSendBatchingRoundTrip(t *testing.T) {
srvConfig := DefaultServerConfig()
srvConfig.SendBatching = true
srvHandler := newTestHandler()
srv := startTestServer(t, srvConfig, srvHandler)
cliConfig := DefaultClientConfig()
cliConfig.SendBatching = true
cliHandler := newTestHandler()
cli := connectTestClient(t, cliConfig, cliHandler, srv.Addr().String())
select {
case <-srvHandler.connectCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for connect")
}
// Send multiple messages — they should be batched into fewer datagrams.
for i := range 5 {
msg := []byte(strings.Repeat("B", i+10))
if err := cli.Connection().Send(msg, 0); err != nil {
t.Fatalf("Send %d: %v", i, err)
}
}
// Wait for all messages. Messages may arrive out-of-order relative to the
// fixed expected payload here (batching can coalesce), so we only assert
// that the right count arrives.
for i := range 5 {
select {
case <-srvHandler.messageCh:
case <-time.After(3 * time.Second):
t.Fatalf("timed out waiting for message %d", i)
}
}
}
func TestSendBatchingBidirectional(t *testing.T) {
srvConfig := DefaultServerConfig()
srvConfig.SendBatching = true
srvHandler := newTestHandler()
srv := startTestServer(t, srvConfig, srvHandler)
cliConfig := DefaultClientConfig()
cliConfig.SendBatching = true
cliHandler := newTestHandler()
cli := connectTestClient(t, cliConfig, cliHandler, srv.Addr().String())
var srvConn *Connection
select {
case srvConn = <-srvHandler.connectCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for connect")
}
// Verify the connection has batching enabled.
if !srvConn.batchEnabled {
t.Fatal("server connection should have batching enabled")
}
if !cli.Connection().batchEnabled {
t.Fatal("client connection should have batching enabled")
}
// Server → Client.
if err := srvConn.Send([]byte("batched-s2c"), 0); err != nil {
t.Fatalf("Send: %v", err)
}
select {
case ev := <-cliHandler.messageCh:
if string(ev.data) != "batched-s2c" {
t.Fatalf("message = %q", ev.data)
}
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for batched s2c message")
}
// Client → Server.
if err := cli.Connection().Send([]byte("batched-c2s"), 0); err != nil {
t.Fatalf("Send: %v", err)
}
select {
case ev := <-srvHandler.messageCh:
if string(ev.data) != "batched-c2s" {
t.Fatalf("message = %q", ev.data)
}
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for batched c2s message")
}
}
func TestSendBatchingSendImmediate(t *testing.T) {
srvConfig := DefaultServerConfig()
srvConfig.SendBatching = true
srvHandler := newTestHandler()
srv := startTestServer(t, srvConfig, srvHandler)
cliConfig := DefaultClientConfig()
cliConfig.SendBatching = true
cliHandler := newTestHandler()
cli := connectTestClient(t, cliConfig, cliHandler, srv.Addr().String())
select {
case <-srvHandler.connectCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for connect")
}
// SendImmediate should bypass the batch buffer.
if err := cli.Connection().SendImmediate([]byte("immediate"), 0); err != nil {
t.Fatalf("SendImmediate: %v", err)
}
select {
case ev := <-srvHandler.messageCh:
if string(ev.data) != "immediate" {
t.Fatalf("message = %q", ev.data)
}
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for immediate message")
}
}
func TestSendBatchingLargeMessage(t *testing.T) {
srvConfig := DefaultServerConfig()
srvConfig.SendBatching = true
srvHandler := newTestHandler()
srv := startTestServer(t, srvConfig, srvHandler)
cliConfig := DefaultClientConfig()
cliConfig.SendBatching = true
cliHandler := newTestHandler()
cli := connectTestClient(t, cliConfig, cliHandler, srv.Addr().String())
select {
case <-srvHandler.connectCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for connect")
}
// Large message that requires fragmentation should still work with batching.
largeMsg := bytes.Repeat([]byte("F"), MaxFragmentPayload*3)
if err := cli.Connection().Send(largeMsg, 0); err != nil {
t.Fatalf("Send: %v", err)
}
select {
case ev := <-srvHandler.messageCh:
if !bytes.Equal(ev.data, largeMsg) {
t.Fatalf("message len = %d, want %d", len(ev.data), len(largeMsg))
}
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for large batched message")
}
}
func TestSendBatchingDisabledOnOneSide(t *testing.T) {
// When only one side has batching, it should NOT be negotiated.
srvConfig := DefaultServerConfig()
srvConfig.SendBatching = true // server wants batching
srvHandler := newTestHandler()
srv := startTestServer(t, srvConfig, srvHandler)
cliConfig := DefaultClientConfig()
cliConfig.SendBatching = false // client does not
cliHandler := newTestHandler()
cli := connectTestClient(t, cliConfig, cliHandler, srv.Addr().String())
var srvConn *Connection
select {
case srvConn = <-srvHandler.connectCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for connect")
}
// Batching should NOT be active since client didn't request it.
if srvConn.batchEnabled {
t.Fatal("server connection should NOT have batching enabled")
}
if cli.Connection().batchEnabled {
t.Fatal("client connection should NOT have batching enabled")
}
// Messages should still work.
if err := cli.Connection().Send([]byte("no-batch"), 0); err != nil {
t.Fatalf("Send: %v", err)
}
select {
case ev := <-srvHandler.messageCh:
if string(ev.data) != "no-batch" {
t.Fatalf("message = %q", ev.data)
}
case <-time.After(2 * time.Second):
t.Fatal("timed out")
}
}
// --- Connection Migration Tests ---
func TestConnectionMigrationNegotiation(t *testing.T) {
srvConfig := DefaultServerConfig()
srvConfig.ConnectionMigration = true
srvHandler := newTestHandler()
srv := startTestServer(t, srvConfig, srvHandler)
cliConfig := DefaultClientConfig()
cliConfig.ConnectionMigration = true
cliHandler := newTestHandler()
cli := connectTestClient(t, cliConfig, cliHandler, srv.Addr().String())
var srvConn *Connection
select {
case srvConn = <-srvHandler.connectCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for connect")
}
// Verify migration token was assigned.
if srvConn.migrationToken == (MigrationToken{}) {
t.Fatal("server connection should have a non-zero migration token")
}
if cli.Connection().migrationToken == (MigrationToken{}) {
t.Fatal("client connection should have a non-zero migration token")
}
if srvConn.migrationToken != cli.Connection().migrationToken {
t.Fatal("migration tokens should match")
}
// Verify feature flag is set.
if srvConn.features&byte(FeatureConnectionMigration) == 0 {
t.Fatal("migration feature should be negotiated")
}
}
func TestConnectionMigrationMessageRoundTrip(t *testing.T) {
srvConfig := DefaultServerConfig()
srvConfig.ConnectionMigration = true
srvHandler := newTestHandler()
srv := startTestServer(t, srvConfig, srvHandler)
cliConfig := DefaultClientConfig()
cliConfig.ConnectionMigration = true
cliHandler := newTestHandler()
cli := connectTestClient(t, cliConfig, cliHandler, srv.Addr().String())
var srvConn *Connection
select {
case srvConn = <-srvHandler.connectCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for connect")
}
// Client → Server with migration token.
if err := cli.Connection().Send([]byte("migrated-c2s"), 0); err != nil {
t.Fatalf("Send: %v", err)
}
select {
case ev := <-srvHandler.messageCh:
if string(ev.data) != "migrated-c2s" {
t.Fatalf("message = %q", ev.data)
}
case <-time.After(2 * time.Second):
t.Fatal("timed out")
}
// Server → Client.
if err := srvConn.Send([]byte("migrated-s2c"), 0); err != nil {
t.Fatalf("Send: %v", err)
}
select {
case ev := <-cliHandler.messageCh:
if string(ev.data) != "migrated-s2c" {
t.Fatalf("message = %q", ev.data)
}
case <-time.After(2 * time.Second):
t.Fatal("timed out")
}
}
func TestConnectionMigrationNotNegotiatedWhenDisabled(t *testing.T) {
srvConfig := DefaultServerConfig()
srvConfig.ConnectionMigration = false
srvHandler := newTestHandler()
srv := startTestServer(t, srvConfig, srvHandler)
cliConfig := DefaultClientConfig()
cliConfig.ConnectionMigration = true
cliHandler := newTestHandler()
cli := connectTestClient(t, cliConfig, cliHandler, srv.Addr().String())
var srvConn *Connection
select {
case srvConn = <-srvHandler.connectCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for connect")
}
// Migration should NOT be negotiated.
if srvConn.features&byte(FeatureConnectionMigration) != 0 {
t.Fatal("migration should not be negotiated when server disables it")
}
// Messages should still work.
if err := cli.Connection().Send([]byte("no-migrate"), 0); err != nil {
t.Fatalf("Send: %v", err)
}
select {
case ev := <-srvHandler.messageCh:
if string(ev.data) != "no-migrate" {
t.Fatalf("message = %q", ev.data)
}
case <-time.After(2 * time.Second):
t.Fatal("timed out")
}
}
// --- Migration Authentication ---
// TestConnectionMigrationHijackRejected verifies that a forged packet prefixed
// with a known migration token does not re-route the connection's address.
// The migration token travels in cleartext, so a passive observer knowing it
// must not be able to hijack the session by sending garbage from a new source.
func TestConnectionMigrationHijackRejected(t *testing.T) {
srvConfig := DefaultServerConfig()
srvConfig.ConnectionMigration = true
srvHandler := newTestHandler()
srv := startTestServer(t, srvConfig, srvHandler)
cliConfig := DefaultClientConfig()
cliConfig.ConnectionMigration = true
cli := connectTestClient(t, cliConfig, newTestHandler(), srv.Addr().String())
var srvConn *Connection
select {
case srvConn = <-srvHandler.connectCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for connect")
}
originalAddr := srvConn.RemoteAddr()
token := srvConn.migrationToken
if token == (MigrationToken{}) {
t.Fatal("migration token not negotiated")
}
// Forge a packet from a different UDP socket: [token][garbage].
attacker, err := net.Dial("udp", srv.Addr().String())
if err != nil {
t.Fatalf("attacker dial: %v", err)
}
defer func() { _ = attacker.Close() }()
forged := make([]byte, MigrationTokenSize+128)
copy(forged, token[:])
for i := MigrationTokenSize; i < len(forged); i++ {
forged[i] = byte(i)
}
if _, err := attacker.Write(forged); err != nil {
t.Fatalf("attacker write: %v", err)
}
// Send a legitimate message from the real client. UDP ordering on loopback
// is reliable, so this packet reaches the server after the attacker's. Its
// arrival at the handler proves the attacker's packet was already processed
// — if migration auth were broken, the address swap would have happened
// before this message arrived.
if err := cli.Connection().Send([]byte("still-alive"), 0); err != nil {
t.Fatalf("Send: %v", err)
}
select {
case ev := <-srvHandler.messageCh:
if string(ev.data) != "still-alive" {
t.Fatalf("message = %q", ev.data)
}
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for post-forgery message")
}
// Hijack check: the address must still point at the legitimate client.
if got := srvConn.RemoteAddr(); got != originalAddr {
t.Fatalf("address hijacked: remote = %v, want %v", got, originalAddr)
}
}
// --- Combined Features Test ---
func TestBatchingAndMigrationCombined(t *testing.T) {
srvConfig := DefaultServerConfig()
srvConfig.SendBatching = true
srvConfig.ConnectionMigration = true
srvHandler := newTestHandler()
srv := startTestServer(t, srvConfig, srvHandler)
cliConfig := DefaultClientConfig()
cliConfig.SendBatching = true
cliConfig.ConnectionMigration = true
cliHandler := newTestHandler()
cli := connectTestClient(t, cliConfig, cliHandler, srv.Addr().String())
var srvConn *Connection
select {
case srvConn = <-srvHandler.connectCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for connect")
}
// Verify both features negotiated.
if !srvConn.batchEnabled {
t.Fatal("batching should be enabled")
}
if srvConn.migrationToken == (MigrationToken{}) {
t.Fatal("migration should be enabled")
}
// Send multiple messages in both directions.
for i := range 5 {
if err := cli.Connection().Send([]byte(strings.Repeat("C", i+1)), 0); err != nil {
t.Fatalf("Send c2s %d: %v", i, err)
}
if err := srvConn.Send([]byte(strings.Repeat("S", i+1)), 0); err != nil {
t.Fatalf("Send s2c %d: %v", i, err)
}
}
// Wait for all messages.
for range 5 {
select {
case <-srvHandler.messageCh:
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for c2s messages")
}
}
for range 5 {
select {
case <-cliHandler.messageCh:
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for s2c messages")
}
}
}
// --- I/O Coalescing Tests ---
func TestCoalesceIOServerRoundTrip(t *testing.T) {
srvConfig := DefaultServerConfig()
srvConfig.CoalesceIO = true
srvConfig.CoalesceReaders = 2
srvHandler := newTestHandler()
srv := startTestServer(t, srvConfig, srvHandler)
cliHandler := newTestHandler()
cli := connectTestClient(t, DefaultClientConfig(), cliHandler, srv.Addr().String())
select {
case <-srvHandler.connectCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for connect")
}
if err := cli.Connection().Send([]byte("coalesced"), 0); err != nil {
t.Fatalf("Send: %v", err)
}
select {
case ev := <-srvHandler.messageCh:
if string(ev.data) != "coalesced" {
t.Fatalf("message = %q", ev.data)
}
case <-time.After(2 * time.Second):
t.Fatal("timed out")
}
}
// --- Bandwidth Estimation Tests ---
func TestBandwidthEstimation(t *testing.T) {
srvHandler := newTestHandler()
srv := startTestServer(t, DefaultServerConfig(), srvHandler)
cliHandler := newTestHandler()
cli := connectTestClient(t, DefaultClientConfig(), cliHandler, srv.Addr().String())
var srvConn *Connection
select {
case srvConn = <-srvHandler.connectCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for connect")
}
// Send messages to generate traffic.
for range 20 {
if err := cli.Connection().Send([]byte(strings.Repeat("X", 100)), 0); err != nil {
t.Fatalf("Send: %v", err)
}
if err := srvConn.Send([]byte(strings.Repeat("Y", 100)), 0); err != nil {
t.Fatalf("Send: %v", err)
}
}
// Wait for messages to flow.
for range 20 {
select {
case <-srvHandler.messageCh:
case <-time.After(3 * time.Second):
t.Fatal("timed out")
}
}
for range 20 {
select {
case <-cliHandler.messageCh:
case <-time.After(3 * time.Second):
t.Fatal("timed out")
}
}
// Allow bandwidth estimators to tick.
time.Sleep(200 * time.Millisecond)
// Check stats.
srvStats := srvConn.Stats()
if srvStats.SendBandwidth <= 0 {
t.Errorf("server SendBandwidth = %f, want > 0", srvStats.SendBandwidth)
}
if srvStats.RecvBandwidth <= 0 {
t.Errorf("server RecvBandwidth = %f, want > 0", srvStats.RecvBandwidth)
}
cliStats := cli.Connection().Stats()
if cliStats.SendBandwidth <= 0 {
t.Errorf("client SendBandwidth = %f, want > 0", cliStats.SendBandwidth)
}
if cliStats.RecvBandwidth <= 0 {
t.Errorf("client RecvBandwidth = %f, want > 0", cliStats.RecvBandwidth)
}
}
// --- Feature Flags Tests ---
func TestFeaturesFromConfig(t *testing.T) {
// Both enabled.
f := featuresFromConfig(true, true)
if f&byte(FeatureSendBatching) == 0 {
t.Fatal("batching bit should be set")
}
if f&byte(FeatureConnectionMigration) == 0 {
t.Fatal("migration bit should be set")
}
// None enabled.
f = featuresFromConfig(false, false)
if f != 0 {
t.Fatalf("features = %d, want 0", f)
}
// Only batching.
f = featuresFromConfig(true, false)
if f&byte(FeatureSendBatching) == 0 {
t.Fatal("batching bit should be set")
}
if f&byte(FeatureConnectionMigration) != 0 {
t.Fatal("migration bit should not be set")
}
}
func TestHandshakeFeatureNegotiation(t *testing.T) {
// Client wants both, server wants only batching → negotiated = batching only.
srvConfig := DefaultServerConfig()
srvConfig.SendBatching = true
srvConfig.ConnectionMigration = false
srvHandler := newTestHandler()
srv := startTestServer(t, srvConfig, srvHandler)
cliConfig := DefaultClientConfig()
cliConfig.SendBatching = true
cliConfig.ConnectionMigration = true
cliHandler := newTestHandler()
cli := connectTestClient(t, cliConfig, cliHandler, srv.Addr().String())
var srvConn *Connection
select {
case srvConn = <-srvHandler.connectCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for connect")
}
if !srvConn.batchEnabled {
t.Fatal("batching should be negotiated")
}
if srvConn.features&byte(FeatureConnectionMigration) != 0 {
t.Fatal("migration should NOT be negotiated")
}
// Verify message still works.
if err := cli.Connection().Send([]byte("test"), 0); err != nil {
t.Fatalf("Send: %v", err)
}
select {
case <-srvHandler.messageCh:
case <-time.After(2 * time.Second):
t.Fatal("timed out")
}
}
// --- All Features Combined Stress Test ---
func TestAllFeaturesCombinedStress(t *testing.T) {
if testing.Short() {
t.Skip("skipping stress test in short mode")
}
srvConfig := DefaultServerConfig()
srvConfig.SendBatching = true
srvConfig.ConnectionMigration = true
srvConfig.CoalesceIO = true
srvConfig.CoalesceReaders = 2
srvConfig.Congestion = CongestionAggressive
srvHandler := newTestHandler()
srv := startTestServer(t, srvConfig, srvHandler)
const numClients = 5
const msgsPerClient = 20
clients := make([]*Client, numClients)
for i := range numClients {
cliConfig := DefaultClientConfig()
cliConfig.SendBatching = true
cliConfig.ConnectionMigration = true
cliConfig.Congestion = CongestionAggressive
h := newTestHandler()
clients[i] = connectTestClient(t, cliConfig, h, srv.Addr().String())
}
// Wait for all connections.
for range numClients {
select {
case <-srvHandler.connectCh:
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for connections")
}
}
// Send messages.
for _, cli := range clients {
for range msgsPerClient {
if err := cli.Connection().Send([]byte("stress-test-data"), 0); err != nil {
t.Fatalf("Send: %v", err)
}
}
}
// Wait for all messages.
total := numClients * msgsPerClient
received := 0
timeout := time.After(15 * time.Second)
for received < total {
select {
case <-srvHandler.messageCh:
received++
case <-timeout:
t.Fatalf("timed out: received %d/%d messages", received, total)
}
}
}