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
|
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testbench
import (
"bytes"
"net"
"testing"
"github.com/mohae/deepcopy"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
)
func TestLayerMatch(t *testing.T) {
var nilPayload *Payload
noPayload := &Payload{}
emptyPayload := &Payload{Bytes: []byte{}}
fullPayload := &Payload{Bytes: []byte{1, 2, 3}}
emptyTCP := &TCP{SrcPort: Uint16(1234), LayerBase: LayerBase{nextLayer: emptyPayload}}
fullTCP := &TCP{SrcPort: Uint16(1234), LayerBase: LayerBase{nextLayer: fullPayload}}
for _, tt := range []struct {
a, b Layer
want bool
}{
{nilPayload, nilPayload, true},
{nilPayload, noPayload, true},
{nilPayload, emptyPayload, true},
{nilPayload, fullPayload, true},
{noPayload, noPayload, true},
{noPayload, emptyPayload, true},
{noPayload, fullPayload, true},
{emptyPayload, emptyPayload, true},
{emptyPayload, fullPayload, false},
{fullPayload, fullPayload, true},
{emptyTCP, fullTCP, true},
} {
if got := tt.a.match(tt.b); got != tt.want {
t.Errorf("%s.match(%s) = %t, want %t", tt.a, tt.b, got, tt.want)
}
if got := tt.b.match(tt.a); got != tt.want {
t.Errorf("%s.match(%s) = %t, want %t", tt.b, tt.a, got, tt.want)
}
}
}
func TestLayerMergeMismatch(t *testing.T) {
tcp := &TCP{}
otherTCP := &TCP{}
ipv4 := &IPv4{}
ether := &Ether{}
for _, tt := range []struct {
a, b Layer
success bool
}{
{tcp, tcp, true},
{tcp, otherTCP, true},
{tcp, ipv4, false},
{tcp, ether, false},
{tcp, nil, true},
{otherTCP, otherTCP, true},
{otherTCP, ipv4, false},
{otherTCP, ether, false},
{otherTCP, nil, true},
{ipv4, ipv4, true},
{ipv4, ether, false},
{ipv4, nil, true},
{ether, ether, true},
{ether, nil, true},
} {
if err := tt.a.merge(tt.b); (err == nil) != tt.success {
t.Errorf("%s.merge(%s) got %s, wanted the opposite", tt.a, tt.b, err)
}
if tt.b != nil {
if err := tt.b.merge(tt.a); (err == nil) != tt.success {
t.Errorf("%s.merge(%s) got %s, wanted the opposite", tt.b, tt.a, err)
}
}
}
}
func TestLayerMerge(t *testing.T) {
zero := Uint32(0)
one := Uint32(1)
two := Uint32(2)
empty := []byte{}
foo := []byte("foo")
bar := []byte("bar")
for _, tt := range []struct {
a, b Layer
want Layer
}{
{&TCP{AckNum: nil}, &TCP{AckNum: nil}, &TCP{AckNum: nil}},
{&TCP{AckNum: nil}, &TCP{AckNum: zero}, &TCP{AckNum: zero}},
{&TCP{AckNum: nil}, &TCP{AckNum: one}, &TCP{AckNum: one}},
{&TCP{AckNum: nil}, &TCP{AckNum: two}, &TCP{AckNum: two}},
{&TCP{AckNum: nil}, nil, &TCP{AckNum: nil}},
{&TCP{AckNum: zero}, &TCP{AckNum: nil}, &TCP{AckNum: zero}},
{&TCP{AckNum: zero}, &TCP{AckNum: zero}, &TCP{AckNum: zero}},
{&TCP{AckNum: zero}, &TCP{AckNum: one}, &TCP{AckNum: one}},
{&TCP{AckNum: zero}, &TCP{AckNum: two}, &TCP{AckNum: two}},
{&TCP{AckNum: zero}, nil, &TCP{AckNum: zero}},
{&TCP{AckNum: one}, &TCP{AckNum: nil}, &TCP{AckNum: one}},
{&TCP{AckNum: one}, &TCP{AckNum: zero}, &TCP{AckNum: zero}},
{&TCP{AckNum: one}, &TCP{AckNum: one}, &TCP{AckNum: one}},
{&TCP{AckNum: one}, &TCP{AckNum: two}, &TCP{AckNum: two}},
{&TCP{AckNum: one}, nil, &TCP{AckNum: one}},
{&TCP{AckNum: two}, &TCP{AckNum: nil}, &TCP{AckNum: two}},
{&TCP{AckNum: two}, &TCP{AckNum: zero}, &TCP{AckNum: zero}},
{&TCP{AckNum: two}, &TCP{AckNum: one}, &TCP{AckNum: one}},
{&TCP{AckNum: two}, &TCP{AckNum: two}, &TCP{AckNum: two}},
{&TCP{AckNum: two}, nil, &TCP{AckNum: two}},
{&Payload{Bytes: nil}, &Payload{Bytes: nil}, &Payload{Bytes: nil}},
{&Payload{Bytes: nil}, &Payload{Bytes: empty}, &Payload{Bytes: empty}},
{&Payload{Bytes: nil}, &Payload{Bytes: foo}, &Payload{Bytes: foo}},
{&Payload{Bytes: nil}, &Payload{Bytes: bar}, &Payload{Bytes: bar}},
{&Payload{Bytes: nil}, nil, &Payload{Bytes: nil}},
{&Payload{Bytes: empty}, &Payload{Bytes: nil}, &Payload{Bytes: empty}},
{&Payload{Bytes: empty}, &Payload{Bytes: empty}, &Payload{Bytes: empty}},
{&Payload{Bytes: empty}, &Payload{Bytes: foo}, &Payload{Bytes: foo}},
{&Payload{Bytes: empty}, &Payload{Bytes: bar}, &Payload{Bytes: bar}},
{&Payload{Bytes: empty}, nil, &Payload{Bytes: empty}},
{&Payload{Bytes: foo}, &Payload{Bytes: nil}, &Payload{Bytes: foo}},
{&Payload{Bytes: foo}, &Payload{Bytes: empty}, &Payload{Bytes: empty}},
{&Payload{Bytes: foo}, &Payload{Bytes: foo}, &Payload{Bytes: foo}},
{&Payload{Bytes: foo}, &Payload{Bytes: bar}, &Payload{Bytes: bar}},
{&Payload{Bytes: foo}, nil, &Payload{Bytes: foo}},
{&Payload{Bytes: bar}, &Payload{Bytes: nil}, &Payload{Bytes: bar}},
{&Payload{Bytes: bar}, &Payload{Bytes: empty}, &Payload{Bytes: empty}},
{&Payload{Bytes: bar}, &Payload{Bytes: foo}, &Payload{Bytes: foo}},
{&Payload{Bytes: bar}, &Payload{Bytes: bar}, &Payload{Bytes: bar}},
{&Payload{Bytes: bar}, nil, &Payload{Bytes: bar}},
} {
a := deepcopy.Copy(tt.a).(Layer)
if err := a.merge(tt.b); err != nil {
t.Errorf("%s.merge(%s) = %s, wanted nil", tt.a, tt.b, err)
continue
}
if a.String() != tt.want.String() {
t.Errorf("%s.merge(%s) merge result got %s, want %s", tt.a, tt.b, a, tt.want)
}
}
}
func TestLayerStringFormat(t *testing.T) {
for _, tt := range []struct {
name string
l Layer
want string
}{
{
name: "TCP",
l: &TCP{
SrcPort: Uint16(34785),
DstPort: Uint16(47767),
SeqNum: Uint32(3452155723),
AckNum: Uint32(2596996163),
DataOffset: Uint8(5),
Flags: Uint8(20),
WindowSize: Uint16(64240),
Checksum: Uint16(0x2e2b),
},
want: "&testbench.TCP{" +
"SrcPort:34785 " +
"DstPort:47767 " +
"SeqNum:3452155723 " +
"AckNum:2596996163 " +
"DataOffset:5 " +
"Flags:20 " +
"WindowSize:64240 " +
"Checksum:11819" +
"}",
},
{
name: "UDP",
l: &UDP{
SrcPort: Uint16(34785),
DstPort: Uint16(47767),
Length: Uint16(12),
},
want: "&testbench.UDP{" +
"SrcPort:34785 " +
"DstPort:47767 " +
"Length:12" +
"}",
},
{
name: "IPv4",
l: &IPv4{
IHL: Uint8(5),
TOS: Uint8(0),
TotalLength: Uint16(44),
ID: Uint16(0),
Flags: Uint8(2),
FragmentOffset: Uint16(0),
TTL: Uint8(64),
Protocol: Uint8(6),
Checksum: Uint16(0x2e2b),
SrcAddr: Address(tcpip.Address([]byte{197, 34, 63, 10})),
DstAddr: Address(tcpip.Address([]byte{197, 34, 63, 20})),
},
want: "&testbench.IPv4{" +
"IHL:5 " +
"TOS:0 " +
"TotalLength:44 " +
"ID:0 " +
"Flags:2 " +
"FragmentOffset:0 " +
"TTL:64 " +
"Protocol:6 " +
"Checksum:11819 " +
"SrcAddr:197.34.63.10 " +
"DstAddr:197.34.63.20" +
"}",
},
{
name: "Ether",
l: &Ether{
SrcAddr: LinkAddress(tcpip.LinkAddress([]byte{0x02, 0x42, 0xc5, 0x22, 0x3f, 0x0a})),
DstAddr: LinkAddress(tcpip.LinkAddress([]byte{0x02, 0x42, 0xc5, 0x22, 0x3f, 0x14})),
Type: NetworkProtocolNumber(4),
},
want: "&testbench.Ether{" +
"SrcAddr:02:42:c5:22:3f:0a " +
"DstAddr:02:42:c5:22:3f:14 " +
"Type:4" +
"}",
},
{
name: "Payload",
l: &Payload{
Bytes: []byte("Hooray for packetimpact."),
},
want: "&testbench.Payload{Bytes:\n" +
"00000000 48 6f 6f 72 61 79 20 66 6f 72 20 70 61 63 6b 65 |Hooray for packe|\n" +
"00000010 74 69 6d 70 61 63 74 2e |timpact.|\n" +
"}",
},
} {
t.Run(tt.name, func(t *testing.T) {
if got := tt.l.String(); got != tt.want {
t.Errorf("%s.String() = %s, want: %s", tt.name, got, tt.want)
}
})
}
}
func TestConnectionMatch(t *testing.T) {
conn := Connection{
layerStates: []layerState{ðerState{}},
}
protoNum0 := tcpip.NetworkProtocolNumber(0)
protoNum1 := tcpip.NetworkProtocolNumber(1)
for _, tt := range []struct {
description string
override, received Layers
wantMatch bool
}{
{
description: "shorter override",
override: []Layer{&Ether{}},
received: []Layer{&Ether{}, &Payload{Bytes: []byte("hello")}},
wantMatch: true,
},
{
description: "longer override",
override: []Layer{&Ether{}, &Payload{Bytes: []byte("hello")}},
received: []Layer{&Ether{}},
wantMatch: false,
},
{
description: "ether layer mismatch",
override: []Layer{&Ether{Type: &protoNum0}},
received: []Layer{&Ether{Type: &protoNum1}},
wantMatch: false,
},
{
description: "both nil",
override: nil,
received: nil,
wantMatch: false,
},
{
description: "nil override",
override: nil,
received: []Layer{&Ether{}},
wantMatch: true,
},
} {
t.Run(tt.description, func(t *testing.T) {
if gotMatch := conn.match(tt.override, tt.received); gotMatch != tt.wantMatch {
t.Fatalf("conn.match(%s, %s) = %t, want %t", tt.override, tt.received, gotMatch, tt.wantMatch)
}
})
}
}
func TestLayersDiff(t *testing.T) {
for _, tt := range []struct {
x, y Layers
want string
}{
{
Layers{&Ether{Type: NetworkProtocolNumber(12)}, &TCP{DataOffset: Uint8(5), SeqNum: Uint32(5)}},
Layers{&Ether{Type: NetworkProtocolNumber(13)}, &TCP{DataOffset: Uint8(7), SeqNum: Uint32(6)}},
"Ether: Type: 12 13\n" +
" TCP: SeqNum: 5 6\n" +
" DataOffset: 5 7\n",
},
{
Layers{&Ether{Type: NetworkProtocolNumber(12)}, &UDP{SrcPort: Uint16(123)}},
Layers{&Ether{Type: NetworkProtocolNumber(13)}, &TCP{DataOffset: Uint8(7), SeqNum: Uint32(6)}},
"Ether: Type: 12 13\n" +
"(UDP doesn't match TCP)\n" +
" UDP: SrcPort: 123 \n" +
" TCP: SeqNum: 6\n" +
" DataOffset: 7\n",
},
{
Layers{&UDP{SrcPort: Uint16(123)}},
Layers{&Ether{Type: NetworkProtocolNumber(13)}, &TCP{DataOffset: Uint8(7), SeqNum: Uint32(6)}},
"(UDP doesn't match Ether)\n" +
" UDP: SrcPort: 123 \n" +
"Ether: Type: 13\n" +
"(missing matches TCP)\n",
},
{
Layers{nil, &UDP{SrcPort: Uint16(123)}},
Layers{&Ether{Type: NetworkProtocolNumber(13)}, &TCP{DataOffset: Uint8(7), SeqNum: Uint32(6)}},
"(nil matches Ether)\n" +
"(UDP doesn't match TCP)\n" +
"UDP: SrcPort: 123 \n" +
"TCP: SeqNum: 6\n" +
" DataOffset: 7\n",
},
{
Layers{&Ether{Type: NetworkProtocolNumber(13)}, &IPv4{IHL: Uint8(4)}, &TCP{DataOffset: Uint8(7), SeqNum: Uint32(6)}},
Layers{&Ether{Type: NetworkProtocolNumber(13)}, &IPv4{IHL: Uint8(6)}, &TCP{DataOffset: Uint8(7), SeqNum: Uint32(6)}},
"(Ether matches Ether)\n" +
"IPv4: IHL: 4 6\n" +
"(TCP matches TCP)\n",
},
{
Layers{&Payload{Bytes: []byte("foo")}},
Layers{&Payload{Bytes: []byte("bar")}},
"Payload: Bytes: [102 111 111] [98 97 114]\n",
},
{
Layers{&Payload{Bytes: []byte("")}},
Layers{&Payload{}},
"",
},
{
Layers{&Payload{Bytes: []byte("")}},
Layers{&Payload{Bytes: []byte("")}},
"",
},
{
Layers{&UDP{}},
Layers{&TCP{}},
"(UDP doesn't match TCP)\n" +
"(UDP)\n" +
"(TCP)\n",
},
} {
if got := tt.x.diff(tt.y); got != tt.want {
t.Errorf("%s.diff(%s) = %q, want %q", tt.x, tt.y, got, tt.want)
}
if tt.x.match(tt.y) != (tt.x.diff(tt.y) == "") {
t.Errorf("match and diff of %s and %s disagree", tt.x, tt.y)
}
if tt.y.match(tt.x) != (tt.y.diff(tt.x) == "") {
t.Errorf("match and diff of %s and %s disagree", tt.y, tt.x)
}
}
}
func TestTCPOptions(t *testing.T) {
for _, tt := range []struct {
description string
wantBytes []byte
wantLayers Layers
}{
{
description: "without payload",
wantBytes: []byte{
// IPv4 Header
0x45, 0x00, 0x00, 0x2c, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
0xf9, 0x77, 0xc0, 0xa8, 0x00, 0x02, 0xc0, 0xa8, 0x00, 0x01,
// TCP Header
0x30, 0x39, 0xd4, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x60, 0x02, 0x20, 0x00, 0xf5, 0x1c, 0x00, 0x00,
// WindowScale Option
0x03, 0x03, 0x02,
// NOP Option
0x00,
},
wantLayers: []Layer{
&IPv4{
IHL: Uint8(20),
TOS: Uint8(0),
TotalLength: Uint16(44),
ID: Uint16(1),
Flags: Uint8(0),
FragmentOffset: Uint16(0),
TTL: Uint8(64),
Protocol: Uint8(uint8(header.TCPProtocolNumber)),
Checksum: Uint16(0xf977),
SrcAddr: Address(tcpip.Address(net.ParseIP("192.168.0.2").To4())),
DstAddr: Address(tcpip.Address(net.ParseIP("192.168.0.1").To4())),
},
&TCP{
SrcPort: Uint16(12345),
DstPort: Uint16(54321),
SeqNum: Uint32(0),
AckNum: Uint32(0),
Flags: Uint8(header.TCPFlagSyn),
WindowSize: Uint16(8192),
Checksum: Uint16(0xf51c),
UrgentPointer: Uint16(0),
Options: []byte{3, 3, 2, 0},
},
&Payload{Bytes: nil},
},
},
{
description: "with payload",
wantBytes: []byte{
// IPv4 header
0x45, 0x00, 0x00, 0x37, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06,
0xf9, 0x6c, 0xc0, 0xa8, 0x00, 0x02, 0xc0, 0xa8, 0x00, 0x01,
// TCP header
0x30, 0x39, 0xd4, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x60, 0x02, 0x20, 0x00, 0xe5, 0x21, 0x00, 0x00,
// WindowScale Option
0x03, 0x03, 0x02,
// NOP Option
0x00,
// Payload: "Sample Data"
0x53, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x44, 0x61, 0x74, 0x61,
},
wantLayers: []Layer{
&IPv4{
IHL: Uint8(20),
TOS: Uint8(0),
TotalLength: Uint16(55),
ID: Uint16(1),
Flags: Uint8(0),
FragmentOffset: Uint16(0),
TTL: Uint8(64),
Protocol: Uint8(uint8(header.TCPProtocolNumber)),
Checksum: Uint16(0xf96c),
SrcAddr: Address(tcpip.Address(net.ParseIP("192.168.0.2").To4())),
DstAddr: Address(tcpip.Address(net.ParseIP("192.168.0.1").To4())),
},
&TCP{
SrcPort: Uint16(12345),
DstPort: Uint16(54321),
SeqNum: Uint32(0),
AckNum: Uint32(0),
Flags: Uint8(header.TCPFlagSyn),
WindowSize: Uint16(8192),
Checksum: Uint16(0xe521),
UrgentPointer: Uint16(0),
Options: []byte{3, 3, 2, 0},
},
&Payload{Bytes: []byte("Sample Data")},
},
},
} {
t.Run(tt.description, func(t *testing.T) {
layers := parse(parseIPv4, tt.wantBytes)
if !layers.match(tt.wantLayers) {
t.Fatalf("match failed with diff: %s", layers.diff(tt.wantLayers))
}
gotBytes, err := layers.ToBytes()
if err != nil {
t.Fatalf("ToBytes() failed on %s: %s", &layers, err)
}
if !bytes.Equal(tt.wantBytes, gotBytes) {
t.Fatalf("mismatching bytes, gotBytes: %x, wantBytes: %x", gotBytes, tt.wantBytes)
}
})
}
}
|