-
Notifications
You must be signed in to change notification settings - Fork 1
/
integration_test.go
1836 lines (1589 loc) · 49.6 KB
/
integration_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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package netem_test
//
// Tests in this file may run for a long time and should verify
// that the overall/typical behavior is not broken.
//
import (
"context"
"errors"
"io"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"testing"
"time"
"github.com/apex/log"
"github.com/google/go-cmp/cmp"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
"github.com/montanaflynn/stats"
"github.com/ooni/netem"
)
// TestLinkLatency ensures we can control a [Link]'s latency.
func TestLinkLatency(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
t.Log("checking whether we can control a Link's latency")
// require the [Link] to have ~200 ms of latency
lc := &netem.LinkConfig{
LeftToRightDelay: 100 * time.Millisecond,
RightToLeftDelay: 100 * time.Millisecond,
}
// create a point-to-point topology, which consists of a single
// [Link] connecting two userspace network stacks.
topology := netem.MustNewPPPTopology(
"10.0.0.2",
"10.0.0.1",
log.Log,
lc,
)
defer topology.Close()
// connect N times and estimate the RTT by sending a SYN and measuring
// the time required to get back the RST|ACK segment.
var rtts []float64
for idx := 0; idx < 10; idx++ {
start := time.Now()
conn, err := topology.Client.DialContext(context.Background(), "tcp", "10.0.0.1:443")
rtts = append(rtts, time.Since(start).Seconds())
// we expect to see ECONNREFUSED and a nil conn
if !errors.Is(err, syscall.ECONNREFUSED) {
t.Fatal(err)
}
if conn != nil {
t.Fatal("expected nil conn")
}
}
// make sure we have collected samples
if len(rtts) < 1 {
t.Fatal("expected at least one sample")
}
// we expect a median RTT which is larger than 200 ms
median, err := stats.Median(rtts)
if err != nil {
t.Fatal(err)
}
const expectation = 0.2
t.Log("median RTT", median, "expectation", expectation)
if median < expectation {
t.Fatal("median RTT is below expectation")
}
}
// TestLinkPLR ensures we can control a [Link]'s PLR.
func TestLinkPLR(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
t.Log("checking whether we can increase a Link's PLR")
// require the [Link] to have latency and losses
lc := &netem.LinkConfig{
LeftToRightDelay: 10 * time.Millisecond,
RightToLeftDelay: 10 * time.Millisecond,
RightToLeftPLR: 0.1,
}
// create a point-to-point topology, which consists of a single
// [Link] connecting two userspace network stacks.
topology := netem.MustNewPPPTopology(
"10.0.0.2",
"10.0.0.1",
log.Log,
lc,
)
defer topology.Close()
// make sure we have a deadline bound context
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// start an NDT0 server in the background (NDT0 is a stripped down
// NDT7 protocol that allows us to estimate network performance)
ready, serverErrorCh := make(chan net.Listener, 1), make(chan error, 1)
go netem.RunNDT0Server(
ctx,
topology.Server,
net.ParseIP("10.0.0.1"),
443,
log.Log,
ready,
serverErrorCh,
false,
"ndt0.local",
)
// await for the NDT0 server to be listening
listener := <-ready
defer listener.Close()
// run NDT0 client in the background and measure speed
clientErrorCh := make(chan error, 1)
perfch := make(chan *netem.NDT0PerformanceSample)
go netem.RunNDT0Client(
ctx,
topology.Client,
"10.0.0.1:443",
log.Log,
false,
clientErrorCh,
perfch,
)
// collect performance samples
var avgSpeed float64
for p := range perfch {
if p.Final {
avgSpeed = p.AvgSpeedMbps()
}
}
// make sure we have a final average download speed
if avgSpeed <= 0 {
t.Fatal("did not collect the average speed")
}
// make sure that neither the client nor the server
// reported a fundamental error
if err := <-clientErrorCh; err != nil {
t.Fatal(err)
}
if err := <-serverErrorCh; err != nil {
t.Fatal(err)
}
// With MSS=1500, RTT=10 ms, PLR=0.1 (1%) we have seen speeds
// around 1.8 - 2.4 Mbit/s. This occurred both in a development
// machine and in a single processor cloud machine.
//
// We use the single processor cloud machine as a benchmark
// for what to expect from GitHub actions. For reference, this
// machine measured ~400 Mbit/s when the link configuration
// was completely empty (meaning we used the fast link).
//
// These data inform our choices in terms of expectation in
// this test as well as in other tests.
const expectation = 10
t.Log("measured goodput", avgSpeed, "expectation", expectation)
if avgSpeed > expectation {
t.Fatal("goodput above expectation")
}
}
// TestRoutingWorksDNS verifies that routing is working for a simple
// network usage pattern such as using the DNS.
func TestRoutingWorksDNS(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
t.Logf("checking whether Router works for dnsping")
// create a star topology, which consists of a single
// [Router] connected to arbitrary hosts
topology := netem.MustNewStarTopology(log.Log)
defer topology.Close()
// attach a client to the topology
clientStack, err := topology.AddHost("10.0.0.2", "10.0.0.1", &netem.LinkConfig{})
if err != nil {
t.Fatal(err)
}
// attach a server to the topology
serverStack, err := topology.AddHost("10.0.0.1", "10.0.0.1", &netem.LinkConfig{})
if err != nil {
t.Fatal(err)
}
// run a DNS server using the server stack
dnsConfig := netem.NewDNSConfig()
dnsConfig.AddRecord("example.local.", "example.xyz.", "10.0.0.1")
dnsServer, err := netem.NewDNSServer(
log.Log,
serverStack,
"10.0.0.1",
dnsConfig,
)
if err != nil {
t.Fatal(err)
}
defer dnsServer.Close()
// perform a bunch of DNS round trips
const repetitions = 10
for idx := 0; idx < repetitions; idx++ {
query := netem.NewDNSRequestA("example.local")
before := time.Now()
resp, err := netem.DNSRoundTrip(context.Background(), clientStack, "10.0.0.1", query)
elapsed := time.Since(before)
if err != nil {
t.Fatal(err)
}
addrs, cname, err := netem.DNSParseResponse(query, resp)
if err != nil {
t.Fatal(err)
}
if cname != "example.xyz." {
t.Fatal("invalid CNAME", cname)
}
if diff := cmp.Diff([]string{"10.0.0.1"}, addrs); diff != "" {
t.Fatal(diff)
}
t.Logf("got DNS response in %v", elapsed)
}
}
// TestRoutingWorksHTTPS verifies that routing is working for a more
// complex network usage pattern such as using HTTPS.
func TestRoutingWorksHTTPS(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
t.Log("checking whether Router works for httpping")
// create a star topology, which consists of a single
// [Router] connected to arbitrary hosts
topology := netem.MustNewStarTopology(log.Log)
defer topology.Close()
// attach a client to the topology
clientStack, err := topology.AddHost("10.0.0.2", "10.0.0.1", &netem.LinkConfig{})
if err != nil {
t.Fatal(err)
}
// attach a server to the topology
serverStack, err := topology.AddHost("10.0.0.1", "10.0.0.1", &netem.LinkConfig{})
if err != nil {
t.Fatal(err)
}
// run a DNS server using the server stack
dnsConfig := netem.NewDNSConfig()
dnsConfig.AddRecord("example.local.", "example.xyz.", "10.0.0.1")
dnsServer, err := netem.NewDNSServer(
log.Log,
serverStack,
"10.0.0.1",
dnsConfig,
)
if err != nil {
t.Fatal(err)
}
defer dnsServer.Close()
// run an HTTP/HTTPS/HTTP3 server using the server stack
//
// This test used to be flaky because it could be we listened after
// the client tried to connect. To avoid this, listen in the test
// goroutine and only run Serve in the background.
mux := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
listener, err := serverStack.ListenTCP("tcp", &net.TCPAddr{
IP: net.ParseIP("10.0.0.1"),
Port: 443,
Zone: "",
})
if err != nil {
t.Fatal(err)
}
httpServer := &http.Server{
TLSConfig: serverStack.MustNewServerTLSConfig("example.local", "10.0.0.1"),
Handler: mux,
}
go httpServer.ServeTLS(listener, "", "") // empty strings mean: use TLSConfig
// perform a bunch of HTTPS round trips
const repetitions = 10
for idx := 0; idx < repetitions; idx++ {
req, err := http.NewRequest("GET", "https://example.local/", nil)
if err != nil {
t.Fatal(err)
}
txp := netem.NewHTTPTransport(clientStack)
before := time.Now()
resp, err := txp.RoundTrip(req)
elapsed := time.Since(before)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Fatal("unexpected status code", resp.StatusCode)
}
resp.Body.Close()
t.Logf("got HTTPS response in %v", elapsed)
}
// perform an HTTPS roundtrip with the literal IP as target to get
// confidence that we can safely use, e.g., https://8.8.8.8/
req, err := http.NewRequest("GET", "https://10.0.0.1", nil)
if err != nil {
t.Fatal(err)
}
txp := netem.NewHTTPTransport(clientStack)
before := time.Now()
resp, err := txp.RoundTrip(req)
elapsed := time.Since(before)
if err != nil {
t.Fatal("With literal IP:", err)
}
if resp.StatusCode != 200 {
t.Fatal("With literal IP: unexpected status code", resp.StatusCode)
}
resp.Body.Close()
t.Logf("With literal IP: got HTTPS response in %v", elapsed)
}
// TestLinkPCAP ensures we can capture PCAPs.
func TestLinkPCAP(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
t.Log("checking whether we can capture a pcap file")
// wrap the right NIC to capture PCAPs
dirname, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dirname)
filename := filepath.Join(dirname, "capture.pcap")
lc := &netem.LinkConfig{
RightNICWrapper: netem.NewPCAPDumper(filename, log.Log),
}
// create a point-to-point topology, which consists of a single
// [Link] connecting two userspace network stacks.
topology := netem.MustNewPPPTopology(
"10.0.0.2",
"10.0.0.1",
log.Log,
lc,
)
// connect N times and estimate the RTT by sending a SYN and measuring
// the time required to get back the RST|ACK segment.
for idx := 0; idx < 10; idx++ {
conn, err := topology.Client.DialContext(context.Background(), "tcp", "10.0.0.1:443")
// we expect to see ECONNREFUSED and a nil conn
if !errors.Is(err, syscall.ECONNREFUSED) {
t.Fatal(err)
}
if conn != nil {
t.Fatal("expected nil conn")
}
}
// explicitly close the topology to cause the PCAPDumper to stop.
topology.Close()
// TODO(bassosimone): this test is flaky
// open the capture file
filep, err := os.Open(filename)
if err != nil {
t.Fatal(err)
}
defer filep.Close()
reader, err := pcapgo.NewReader(filep)
if err != nil {
t.Fatal(err)
}
// walk through the packets and count them
var count int
for {
_, _, err := reader.ReadPacketData()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
t.Fatal(err)
}
count++
}
t.Log("captured", count, "packets")
if count <= 0 {
t.Fatal("we expected to capture at least one packet")
}
}
// TestDPITCPThrottleForSNI verifies we can use the DPI to throttle
// connections using specific TLS SNIs.
func TestDPITCPThrottleForSNI(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
// testcase describes a test case
type testcase struct {
// name is the name of the test case
name string
// clientSNI is the SNI used by the client
clientSNI string
// offendingSNI is the SNI that would cause throttling
offendingSNI string
// checkAvgSpeed is a function the check whether
// the speed is consistent with expectations
checkAvgSpeed func(t *testing.T, speed float64)
}
var testcases = []testcase{{
name: "when the client is using a throttled SNI",
clientSNI: "ndt0.local",
offendingSNI: "ndt0.local",
checkAvgSpeed: func(t *testing.T, speed float64) {
// See above comment regarding expected performance
// under the given RTT, MSS, and PLR constraints
const expectation = 5
if speed > expectation {
t.Fatal("goodput", speed, "above expectation", expectation)
}
},
}, {
name: "when the client is not using a throttled SNI",
clientSNI: "ndt0.xyz",
offendingSNI: "ndt0.local",
checkAvgSpeed: func(t *testing.T, speed float64) {
// See above comment regarding expected performance
// under the given RTT, MSS, and PLR constraints
const expectation = 5
if speed < expectation {
t.Fatal("goodput", speed, "below expectation", expectation)
}
},
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Log("checking for TLS flow throttling", tc.name)
// throttle the offending SNI to have high latency and high losses
dpiEngine := netem.NewDPIEngine(log.Log)
dpiEngine.AddRule(&netem.DPIThrottleTrafficForTLSSNI{
Delay: 10 * time.Millisecond,
Logger: log.Log,
PLR: 0.1,
SNI: tc.offendingSNI,
})
lc := &netem.LinkConfig{
DPIEngine: dpiEngine,
LeftToRightDelay: 100 * time.Microsecond,
RightToLeftDelay: 100 * time.Microsecond,
}
// create a point-to-point topology, which consists of a single
// [Link] connecting two userspace network stacks.
topology := netem.MustNewPPPTopology(
"10.0.0.2",
"10.0.0.1",
log.Log,
lc,
)
defer topology.Close()
// make sure we have a deadline bound context
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// add DNS server to resolve the clientSNI domain
dnsConfig := netem.NewDNSConfig()
dnsConfig.AddRecord(tc.clientSNI, "", "10.0.0.1")
dnsServer, err := netem.NewDNSServer(log.Log, topology.Server, "10.0.0.1", dnsConfig)
if err != nil {
t.Fatal(err)
}
defer dnsServer.Close()
// start an NDT0 server in the background
ready, serverErrorCh := make(chan net.Listener, 1), make(chan error, 1)
go netem.RunNDT0Server(
ctx,
topology.Server,
net.ParseIP("10.0.0.1"),
443,
log.Log,
ready,
serverErrorCh,
true,
"ndt0.local",
"ndt0.xyz",
)
// await for the NDT0 server to be listening
listener := <-ready
defer listener.Close()
// run NDT0 client in the background and measure speed
clientErrorCh := make(chan error, 1)
perfch := make(chan *netem.NDT0PerformanceSample)
go netem.RunNDT0Client(
ctx,
topology.Client,
net.JoinHostPort(tc.clientSNI, "443"),
log.Log,
true,
clientErrorCh,
perfch,
)
// collect the average speed
var avgSpeed float64
for p := range perfch {
if p.Final {
avgSpeed = p.AvgSpeedMbps()
}
}
// make sure we have collected samples
if avgSpeed <= 0 {
t.Fatal("did not collect the average speed")
}
// make sure that neither the client nor the server
// reported a fundamental error
if err := <-clientErrorCh; err != nil {
t.Fatal(err)
}
if err := <-serverErrorCh; err != nil {
t.Fatal(err)
}
t.Log("measured goodput", avgSpeed)
// make sure that the speed is consistent with expectations
tc.checkAvgSpeed(t, avgSpeed)
})
}
}
// TestDPITCPThrottleForTCPEndpoint verifies we can use the DPI to throttle
// connections using a specific TCP endpoint.
func TestDPITCPThrottleForTCPEndpoint(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
// testcase describes a test case
type testcase struct {
// name is the name of the test case
name string
// endpointAddress is the address of the endpoint to block.
endpointAddress string
// endpointPort is the port of the endpoint to block.
endpointPort uint16
// checkAvgSpeed is a function the check whether
// the speed is consistent with expectations
checkAvgSpeed func(t *testing.T, speed float64)
}
var testcases = []testcase{{
name: "when the client is using a throttled endpoint",
endpointAddress: "10.0.0.1",
endpointPort: 443,
checkAvgSpeed: func(t *testing.T, speed float64) {
// See above comment regarding expected performance
// under the given RTT, MSS, and PLR constraints
const expectation = 5
if speed > expectation {
t.Fatal("goodput", speed, "above expectation", expectation)
}
},
}, {
name: "when the client is not using a throttled endpoint",
endpointAddress: "10.0.0.1",
endpointPort: 555, // different port
checkAvgSpeed: func(t *testing.T, speed float64) {
// See above comment regarding expected performance
// under the given RTT, MSS, and PLR constraints
const expectation = 5
if speed < expectation {
t.Fatal("goodput", speed, "below expectation", expectation)
}
},
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Log("checking for TLS flow throttling", tc.name)
// throttle the offending endpoint to have high latency and high losses
dpiEngine := netem.NewDPIEngine(log.Log)
dpiEngine.AddRule(&netem.DPIThrottleTrafficForTCPEndpoint{
Delay: 10 * time.Millisecond,
Logger: log.Log,
PLR: 0.1,
ServerIPAddress: tc.endpointAddress,
ServerPort: tc.endpointPort,
})
lc := &netem.LinkConfig{
DPIEngine: dpiEngine,
LeftToRightDelay: 100 * time.Microsecond,
RightToLeftDelay: 100 * time.Microsecond,
}
// create a point-to-point topology, which consists of a single
// [Link] connecting two userspace network stacks.
topology := netem.MustNewPPPTopology(
"10.0.0.2",
"10.0.0.1",
log.Log,
lc,
)
defer topology.Close()
// make sure we have a deadline bound context
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// add DNS server to resolve the clientSNI domain
dnsConfig := netem.NewDNSConfig()
dnsConfig.AddRecord("ndt0.local", "", "10.0.0.1")
dnsServer, err := netem.NewDNSServer(log.Log, topology.Server, "10.0.0.1", dnsConfig)
if err != nil {
t.Fatal(err)
}
defer dnsServer.Close()
// start an NDT0 server in the background
ready, serverErrorCh := make(chan net.Listener, 1), make(chan error, 1)
go netem.RunNDT0Server(
ctx,
topology.Server,
net.ParseIP("10.0.0.1"),
443,
log.Log,
ready,
serverErrorCh,
true,
"ndt0.local",
"ndt0.xyz",
)
// await for the NDT0 server to be listening
listener := <-ready
defer listener.Close()
// run NDT0 client in the background and measure speed
clientErrorCh := make(chan error, 1)
perfch := make(chan *netem.NDT0PerformanceSample)
go netem.RunNDT0Client(
ctx,
topology.Client,
net.JoinHostPort("ndt0.local", "443"),
log.Log,
true,
clientErrorCh,
perfch,
)
// collect the average speed
var avgSpeed float64
for p := range perfch {
if p.Final {
avgSpeed = p.AvgSpeedMbps()
}
}
// make sure we have collected samples
if avgSpeed <= 0 {
t.Fatal("did not collect the average speed")
}
// make sure that neither the client nor the server
// reported a fundamental error
if err := <-clientErrorCh; err != nil {
t.Fatal(err)
}
if err := <-serverErrorCh; err != nil {
t.Fatal(err)
}
t.Log("measured goodput", avgSpeed)
// make sure that the speed is consistent with expectations
tc.checkAvgSpeed(t, avgSpeed)
})
}
}
// TestDPITCPResetForSNI verifies we can use the DPI to reset TCP
// connections using specific TLS SNI values.
func TestDPITCPResetForSNI(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
// testcase describes a test case
type testcase struct {
// name is the name of the test case
name string
// clientSNI is the SNI used by the client
clientSNI string
// offendingSNI is the SNI that would cause blocking
offendingSNI string
// expectSamples indicates whether we expect to see samples
expectSamples bool
// expectServerErr is the server error we expect
expectServerErr error
// expectClientErr is the client error we expect
expectClientErr error
}
var testcases = []testcase{{
name: "when the client is using a blocked SNI",
clientSNI: "ndt0.local",
offendingSNI: "ndt0.local",
expectSamples: false,
expectServerErr: syscall.ECONNRESET, // the client RSTs the server
expectClientErr: syscall.ECONNRESET, // caused by the injected segment
}, {
name: "when the client is not using a blocked SNI",
clientSNI: "ndt0.xyz",
offendingSNI: "ndt0.local",
expectSamples: true,
expectServerErr: nil,
expectClientErr: nil,
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Log("check for TLS flow RST", tc.name)
// make sure that the offending SNI causes RST
dpiEngine := netem.NewDPIEngine(log.Log)
dpiEngine.AddRule(&netem.DPIResetTrafficForTLSSNI{
Logger: log.Log,
SNI: tc.offendingSNI,
})
clientLinkConfig := &netem.LinkConfig{
DPIEngine: dpiEngine,
LeftToRightDelay: 10 * time.Millisecond,
RightToLeftDelay: 10 * time.Millisecond,
}
// Create a star topology. We MUST create such a topology because
// the rule we're using REQUIRES a router in the path.
topology := netem.MustNewStarTopology(log.Log)
defer topology.Close()
// make sure we add delay to the router<->server link because
// the DPI rule we're testing relies on a race condition.
serverLinkConfig := &netem.LinkConfig{
LeftToRightDelay: 10 * time.Millisecond,
RightToLeftDelay: 10 * time.Millisecond,
}
// create a client and a server stacks
clientStack, err := topology.AddHost("10.0.0.2", "10.0.0.1", clientLinkConfig)
if err != nil {
t.Fatal(err)
}
serverStack, err := topology.AddHost("10.0.0.1", "10.0.0.1", serverLinkConfig)
if err != nil {
t.Fatal(err)
}
// make sure we have a deadline bound context
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
defer cancel()
// add DNS server to resolve the clientSNI domain
dnsConfig := netem.NewDNSConfig()
dnsConfig.AddRecord(tc.clientSNI, "", "10.0.0.1")
dnsServer, err := netem.NewDNSServer(log.Log, serverStack, "10.0.0.1", dnsConfig)
if err != nil {
t.Fatal(err)
}
defer dnsServer.Close()
// start an NDT0 server in the background
ready, serverErrorCh := make(chan net.Listener, 1), make(chan error, 1)
go netem.RunNDT0Server(
ctx,
serverStack,
net.ParseIP("10.0.0.1"),
443,
log.Log,
ready,
serverErrorCh,
true,
"ndt0.xyz",
"ndt0.local",
)
// await for the NDT0 server to be listening
listener := <-ready
defer listener.Close()
// run NDT0 client in the background and measure speed
clientErrorCh := make(chan error, 1)
perfch := make(chan *netem.NDT0PerformanceSample)
go netem.RunNDT0Client(
ctx,
clientStack,
net.JoinHostPort(tc.clientSNI, "443"),
log.Log,
true,
clientErrorCh,
perfch,
)
// drain the performance channel
var count int
for range perfch {
count++
}
t.Log("got", count, "samples with tc.expectSamples=", tc.expectSamples)
// make sure we have seen samples if we expected samples
if tc.expectSamples && count < 1 {
t.Fatal("expected at least one sample")
}
// When we arrive here is means the client has exited but it may
// be that the server is still stuck inside accept, which happens
// when we drop SYN segments (which we could do in this test if
// we set the .Drop flag of the DPI rule).
//
// So, we need to unblock the server, just in case, by explicitly
// closing the listener. Otherwise, we'll block in the next
// statement trying to read the server's overall error.
listener.Close()
// check the error reported by server
err = <-serverErrorCh
if !errors.Is(err, tc.expectServerErr) {
t.Fatal("unexpected server error", err)
}
// check error reported by client
err = <-clientErrorCh
if !errors.Is(err, tc.expectClientErr) {
t.Fatal("unexpected client error", err)
}
})
}
}
// TestDPITCPCloseConnectionForSNI verifies we can use the DPI to close
// connections using specific TLS SNI values.
func TestDPITCPCloseConnectionForSNI(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
// testcase describes a test case
type testcase struct {
// name is the name of the test case
name string
// clientSNI is the SNI used by the client
clientSNI string
// offendingSNI is the SNI that would cause blocking
offendingSNI string
// expectSamples indicates whether we expect to see samples
expectSamples bool
// expectServerErr is the server error we expect
expectServerErr error
// expectClientErr is the client error we expect
expectClientErr error
}
var testcases = []testcase{{
name: "when the client is using a blocked SNI",
clientSNI: "ndt0.local",
offendingSNI: "ndt0.local",
expectSamples: false,
expectServerErr: io.EOF, // caused by the client seeing a FIN|ACK segment
expectClientErr: io.EOF, // caused by the server reacting to the FIN|ACK segment
}, {
name: "when the client is not using a blocked SNI",
clientSNI: "ndt0.xyz",
offendingSNI: "ndt0.local",
expectSamples: true,
expectServerErr: nil,
expectClientErr: nil,
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
t.Log("check for TLS flow FIN|ACK", tc.name)
// make sure that the offending SNI causes EOF
dpiEngine := netem.NewDPIEngine(log.Log)
dpiEngine.AddRule(&netem.DPICloseConnectionForTLSSNI{
Logger: log.Log,
SNI: tc.offendingSNI,
})
clientLinkConfig := &netem.LinkConfig{
DPIEngine: dpiEngine,
LeftToRightDelay: 10 * time.Millisecond,
RightToLeftDelay: 10 * time.Millisecond,
}
// Create a star topology. We MUST create such a topology because
// the rule we're using REQUIRES a router in the path.
topology := netem.MustNewStarTopology(log.Log)
defer topology.Close()
// make sure we add delay to the router<->server link because
// the DPI rule we're testing relies on a race condition.
serverLinkConfig := &netem.LinkConfig{
LeftToRightDelay: 10 * time.Millisecond,
RightToLeftDelay: 10 * time.Millisecond,
}
// create a client and a server stacks
clientStack, err := topology.AddHost("10.0.0.2", "10.0.0.1", clientLinkConfig)
if err != nil {
t.Fatal(err)
}
serverStack, err := topology.AddHost("10.0.0.1", "10.0.0.1", serverLinkConfig)
if err != nil {
t.Fatal(err)
}
// make sure we have a deadline bound context
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
defer cancel()
// add DNS server to resolve the clientSNI domain
dnsConfig := netem.NewDNSConfig()
dnsConfig.AddRecord(tc.clientSNI, "", "10.0.0.1")
dnsServer, err := netem.NewDNSServer(log.Log, serverStack, "10.0.0.1", dnsConfig)
if err != nil {
t.Fatal(err)
}
defer dnsServer.Close()
// start an NDT0 server in the background
ready, serverErrorCh := make(chan net.Listener, 1), make(chan error, 1)
go netem.RunNDT0Server(
ctx,