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
|
// 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 stack
import (
"bytes"
"fmt"
"testing"
"gvisor.dev/gvisor/pkg/tcpip/buffer"
"gvisor.dev/gvisor/pkg/tcpip/header"
)
func TestPacketHeaderPush(t *testing.T) {
for _, test := range []struct {
name string
reserved int
link []byte
network []byte
transport []byte
data []byte
}{
{
name: "construct empty packet",
},
{
name: "construct link header only packet",
reserved: 60,
link: makeView(10),
},
{
name: "construct link and network header only packet",
reserved: 60,
link: makeView(10),
network: makeView(20),
},
{
name: "construct header only packet",
reserved: 60,
link: makeView(10),
network: makeView(20),
transport: makeView(30),
},
{
name: "construct data only packet",
data: makeView(40),
},
{
name: "construct L3 packet",
reserved: 60,
network: makeView(20),
transport: makeView(30),
data: makeView(40),
},
{
name: "construct L2 packet",
reserved: 60,
link: makeView(10),
network: makeView(20),
transport: makeView(30),
data: makeView(40),
},
} {
t.Run(test.name, func(t *testing.T) {
pk := NewPacketBuffer(PacketBufferOptions{
ReserveHeaderBytes: test.reserved,
// Make a copy of data to make sure our truth data won't be taint by
// PacketBuffer.
Data: buffer.NewViewFromBytes(test.data).ToVectorisedView(),
})
allHdrSize := len(test.link) + len(test.network) + len(test.transport)
// Check the initial values for packet.
checkInitialPacketBuffer(t, pk, PacketBufferOptions{
ReserveHeaderBytes: test.reserved,
Data: buffer.View(test.data).ToVectorisedView(),
})
// Push headers.
if v := test.transport; len(v) > 0 {
copy(pk.TransportHeader().Push(len(v)), v)
}
if v := test.network; len(v) > 0 {
copy(pk.NetworkHeader().Push(len(v)), v)
}
if v := test.link; len(v) > 0 {
copy(pk.LinkHeader().Push(len(v)), v)
}
// Check the after values for packet.
if got, want := pk.ReservedHeaderBytes(), test.reserved; got != want {
t.Errorf("After pk.ReservedHeaderBytes() = %d, want %d", got, want)
}
if got, want := pk.AvailableHeaderBytes(), test.reserved-allHdrSize; got != want {
t.Errorf("After pk.AvailableHeaderBytes() = %d, want %d", got, want)
}
if got, want := pk.HeaderSize(), allHdrSize; got != want {
t.Errorf("After pk.HeaderSize() = %d, want %d", got, want)
}
if got, want := pk.Size(), allHdrSize+len(test.data); got != want {
t.Errorf("After pk.Size() = %d, want %d", got, want)
}
// Check the after state.
checkPacketContents(t, "After ", pk, packetContents{
link: test.link,
network: test.network,
transport: test.transport,
data: test.data,
})
})
}
}
func TestPacketHeaderConsume(t *testing.T) {
for _, test := range []struct {
name string
data []byte
link int
network int
transport int
}{
{
name: "parse L2 packet",
data: concatViews(makeView(10), makeView(20), makeView(30), makeView(40)),
link: 10,
network: 20,
transport: 30,
},
{
name: "parse L3 packet",
data: concatViews(makeView(20), makeView(30), makeView(40)),
network: 20,
transport: 30,
},
} {
t.Run(test.name, func(t *testing.T) {
pk := NewPacketBuffer(PacketBufferOptions{
// Make a copy of data to make sure our truth data won't be taint by
// PacketBuffer.
Data: buffer.NewViewFromBytes(test.data).ToVectorisedView(),
})
// Check the initial values for packet.
checkInitialPacketBuffer(t, pk, PacketBufferOptions{
Data: buffer.View(test.data).ToVectorisedView(),
})
// Consume headers.
if size := test.link; size > 0 {
if _, ok := pk.LinkHeader().Consume(size); !ok {
t.Fatalf("pk.LinkHeader().Consume() = false, want true")
}
}
if size := test.network; size > 0 {
if _, ok := pk.NetworkHeader().Consume(size); !ok {
t.Fatalf("pk.NetworkHeader().Consume() = false, want true")
}
}
if size := test.transport; size > 0 {
if _, ok := pk.TransportHeader().Consume(size); !ok {
t.Fatalf("pk.TransportHeader().Consume() = false, want true")
}
}
allHdrSize := test.link + test.network + test.transport
// Check the after values for packet.
if got, want := pk.ReservedHeaderBytes(), 0; got != want {
t.Errorf("After pk.ReservedHeaderBytes() = %d, want %d", got, want)
}
if got, want := pk.AvailableHeaderBytes(), 0; got != want {
t.Errorf("After pk.AvailableHeaderBytes() = %d, want %d", got, want)
}
if got, want := pk.HeaderSize(), allHdrSize; got != want {
t.Errorf("After pk.HeaderSize() = %d, want %d", got, want)
}
if got, want := pk.Size(), len(test.data); got != want {
t.Errorf("After pk.Size() = %d, want %d", got, want)
}
// Check the after state of pk.
checkPacketContents(t, "After ", pk, packetContents{
link: test.data[:test.link],
network: test.data[test.link:][:test.network],
transport: test.data[test.link+test.network:][:test.transport],
data: test.data[allHdrSize:],
})
})
}
}
func TestPacketHeaderConsumeDataTooShort(t *testing.T) {
data := makeView(10)
pk := NewPacketBuffer(PacketBufferOptions{
// Make a copy of data to make sure our truth data won't be taint by
// PacketBuffer.
Data: buffer.NewViewFromBytes(data).ToVectorisedView(),
})
// Consume should fail if pkt.Data is too short.
if _, ok := pk.LinkHeader().Consume(11); ok {
t.Fatalf("pk.LinkHeader().Consume() = _, true; want _, false")
}
if _, ok := pk.NetworkHeader().Consume(11); ok {
t.Fatalf("pk.NetworkHeader().Consume() = _, true; want _, false")
}
if _, ok := pk.TransportHeader().Consume(11); ok {
t.Fatalf("pk.TransportHeader().Consume() = _, true; want _, false")
}
// Check packet should look the same as initial packet.
checkInitialPacketBuffer(t, pk, PacketBufferOptions{
Data: buffer.View(data).ToVectorisedView(),
})
}
// This is a very obscure use-case seen in the code that verifies packets
// before sending them out. It tries to parse the headers to verify.
// PacketHeader was initially not designed to mix Push() and Consume(), but it
// works and it's been relied upon. Include a test here.
func TestPacketHeaderPushConsumeMixed(t *testing.T) {
link := makeView(10)
network := makeView(20)
data := makeView(30)
initData := append([]byte(nil), network...)
initData = append(initData, data...)
pk := NewPacketBuffer(PacketBufferOptions{
ReserveHeaderBytes: len(link),
Data: buffer.NewViewFromBytes(initData).ToVectorisedView(),
})
// 1. Consume network header
gotNetwork, ok := pk.NetworkHeader().Consume(len(network))
if !ok {
t.Fatalf("pk.NetworkHeader().Consume(%d) = _, false; want _, true", len(network))
}
checkViewEqual(t, "gotNetwork", gotNetwork, network)
// 2. Push link header
copy(pk.LinkHeader().Push(len(link)), link)
checkPacketContents(t, "" /* prefix */, pk, packetContents{
link: link,
network: network,
data: data,
})
}
func TestPacketHeaderPushCalledAtMostOnce(t *testing.T) {
const headerSize = 10
pk := NewPacketBuffer(PacketBufferOptions{
ReserveHeaderBytes: headerSize * int(numHeaderType),
})
for _, h := range []PacketHeader{
pk.TransportHeader(),
pk.NetworkHeader(),
pk.LinkHeader(),
} {
t.Run("PushedTwice/"+h.typ.String(), func(t *testing.T) {
h.Push(headerSize)
defer func() { recover() }()
h.Push(headerSize)
t.Fatal("Second push should have panicked")
})
}
}
func TestPacketHeaderConsumeCalledAtMostOnce(t *testing.T) {
const headerSize = 10
pk := NewPacketBuffer(PacketBufferOptions{
Data: makeView(headerSize * int(numHeaderType)).ToVectorisedView(),
})
for _, h := range []PacketHeader{
pk.LinkHeader(),
pk.NetworkHeader(),
pk.TransportHeader(),
} {
t.Run("ConsumedTwice/"+h.typ.String(), func(t *testing.T) {
if _, ok := h.Consume(headerSize); !ok {
t.Fatal("First consume should succeed")
}
defer func() { recover() }()
h.Consume(headerSize)
t.Fatal("Second consume should have panicked")
})
}
}
func TestPacketHeaderPushThenConsumePanics(t *testing.T) {
const headerSize = 10
pk := NewPacketBuffer(PacketBufferOptions{
ReserveHeaderBytes: headerSize * int(numHeaderType),
})
for _, h := range []PacketHeader{
pk.TransportHeader(),
pk.NetworkHeader(),
pk.LinkHeader(),
} {
t.Run(h.typ.String(), func(t *testing.T) {
h.Push(headerSize)
defer func() { recover() }()
h.Consume(headerSize)
t.Fatal("Consume should have panicked")
})
}
}
func TestPacketHeaderConsumeThenPushPanics(t *testing.T) {
const headerSize = 10
pk := NewPacketBuffer(PacketBufferOptions{
Data: makeView(headerSize * int(numHeaderType)).ToVectorisedView(),
})
for _, h := range []PacketHeader{
pk.LinkHeader(),
pk.NetworkHeader(),
pk.TransportHeader(),
} {
t.Run(h.typ.String(), func(t *testing.T) {
h.Consume(headerSize)
defer func() { recover() }()
h.Push(headerSize)
t.Fatal("Push should have panicked")
})
}
}
func TestPacketBufferData(t *testing.T) {
for _, tc := range []struct {
name string
makePkt func(*testing.T) *PacketBuffer
data string
}{
{
name: "inbound packet",
makePkt: func(*testing.T) *PacketBuffer {
pkt := NewPacketBuffer(PacketBufferOptions{
Data: vv("aabbbbccccccDATA"),
})
pkt.LinkHeader().Consume(2)
pkt.NetworkHeader().Consume(4)
pkt.TransportHeader().Consume(6)
return pkt
},
data: "DATA",
},
{
name: "outbound packet",
makePkt: func(*testing.T) *PacketBuffer {
pkt := NewPacketBuffer(PacketBufferOptions{
ReserveHeaderBytes: 12,
Data: vv("DATA"),
})
copy(pkt.TransportHeader().Push(6), []byte("cccccc"))
copy(pkt.NetworkHeader().Push(4), []byte("bbbb"))
copy(pkt.LinkHeader().Push(2), []byte("aa"))
return pkt
},
data: "DATA",
},
} {
t.Run(tc.name, func(t *testing.T) {
// PullUp
for _, n := range []int{1, len(tc.data)} {
t.Run(fmt.Sprintf("PullUp%d", n), func(t *testing.T) {
pkt := tc.makePkt(t)
v, ok := pkt.Data().PullUp(n)
wantV := []byte(tc.data)[:n]
if !ok || !bytes.Equal(v, wantV) {
t.Errorf("pkt.Data().PullUp(%d) = %q, %t; want %q, true", n, v, ok, wantV)
}
})
}
t.Run("PullUpOutOfBounds", func(t *testing.T) {
n := len(tc.data) + 1
pkt := tc.makePkt(t)
v, ok := pkt.Data().PullUp(n)
if ok || v != nil {
t.Errorf("pkt.Data().PullUp(%d) = %q, %t; want nil, false", n, v, ok)
}
})
// DeleteFront
for _, n := range []int{1, len(tc.data)} {
t.Run(fmt.Sprintf("DeleteFront%d", n), func(t *testing.T) {
pkt := tc.makePkt(t)
pkt.Data().DeleteFront(n)
checkData(t, pkt, []byte(tc.data)[n:])
})
}
// CapLength
for _, n := range []int{0, 1, len(tc.data)} {
t.Run(fmt.Sprintf("CapLength%d", n), func(t *testing.T) {
pkt := tc.makePkt(t)
pkt.Data().CapLength(n)
want := []byte(tc.data)
if n < len(want) {
want = want[:n]
}
checkData(t, pkt, want)
})
}
// Views
t.Run("Views", func(t *testing.T) {
pkt := tc.makePkt(t)
checkData(t, pkt, []byte(tc.data))
})
// AppendView
t.Run("AppendView", func(t *testing.T) {
s := "APPEND"
pkt := tc.makePkt(t)
pkt.Data().AppendView(buffer.View(s))
checkData(t, pkt, []byte(tc.data+s))
})
// ReadFromData/VV
for _, n := range []int{0, 1, 2, 7, 10, 14, 20} {
t.Run(fmt.Sprintf("ReadFromData%d", n), func(t *testing.T) {
s := "TO READ"
otherPkt := NewPacketBuffer(PacketBufferOptions{
Data: vv(s, s),
})
s += s
pkt := tc.makePkt(t)
pkt.Data().ReadFromData(otherPkt.Data(), n)
if n < len(s) {
s = s[:n]
}
checkData(t, pkt, []byte(tc.data+s))
})
t.Run(fmt.Sprintf("ReadFromVV%d", n), func(t *testing.T) {
s := "TO READ"
srcVV := vv(s, s)
s += s
pkt := tc.makePkt(t)
pkt.Data().ReadFromVV(&srcVV, n)
if n < len(s) {
s = s[:n]
}
checkData(t, pkt, []byte(tc.data+s))
})
}
// ExtractVV
t.Run("ExtractVV", func(t *testing.T) {
pkt := tc.makePkt(t)
extractedVV := pkt.Data().ExtractVV()
got := extractedVV.ToOwnedView()
want := []byte(tc.data)
if !bytes.Equal(got, want) {
t.Errorf("pkt.Data().ExtractVV().ToOwnedView() = %q, want %q", got, want)
}
})
// Replace
t.Run("Replace", func(t *testing.T) {
s := "REPLACED"
pkt := tc.makePkt(t)
pkt.Data().Replace(vv(s))
checkData(t, pkt, []byte(s))
})
})
}
}
type packetContents struct {
link buffer.View
network buffer.View
transport buffer.View
data buffer.View
}
func checkPacketContents(t *testing.T, prefix string, pk *PacketBuffer, want packetContents) {
t.Helper()
// Headers.
checkPacketHeader(t, prefix+"pk.LinkHeader", pk.LinkHeader(), want.link)
checkPacketHeader(t, prefix+"pk.NetworkHeader", pk.NetworkHeader(), want.network)
checkPacketHeader(t, prefix+"pk.TransportHeader", pk.TransportHeader(), want.transport)
// Data.
checkData(t, pk, want.data)
// Whole packet.
checkViewEqual(t, prefix+"pk.Views()",
concatViews(pk.Views()...),
concatViews(want.link, want.network, want.transport, want.data))
// PayloadSince.
checkViewEqual(t, prefix+"PayloadSince(LinkHeader)",
PayloadSince(pk.LinkHeader()),
concatViews(want.link, want.network, want.transport, want.data))
checkViewEqual(t, prefix+"PayloadSince(NetworkHeader)",
PayloadSince(pk.NetworkHeader()),
concatViews(want.network, want.transport, want.data))
checkViewEqual(t, prefix+"PayloadSince(TransportHeader)",
PayloadSince(pk.TransportHeader()),
concatViews(want.transport, want.data))
}
func checkInitialPacketBuffer(t *testing.T, pk *PacketBuffer, opts PacketBufferOptions) {
t.Helper()
reserved := opts.ReserveHeaderBytes
if got, want := pk.ReservedHeaderBytes(), reserved; got != want {
t.Errorf("Initial pk.ReservedHeaderBytes() = %d, want %d", got, want)
}
if got, want := pk.AvailableHeaderBytes(), reserved; got != want {
t.Errorf("Initial pk.AvailableHeaderBytes() = %d, want %d", got, want)
}
if got, want := pk.HeaderSize(), 0; got != want {
t.Errorf("Initial pk.HeaderSize() = %d, want %d", got, want)
}
data := opts.Data.ToView()
if got, want := pk.Size(), len(data); got != want {
t.Errorf("Initial pk.Size() = %d, want %d", got, want)
}
checkPacketContents(t, "Initial ", pk, packetContents{
data: data,
})
}
func checkPacketHeader(t *testing.T, name string, h PacketHeader, want []byte) {
t.Helper()
checkViewEqual(t, name+".View()", h.View(), want)
}
func checkViewEqual(t *testing.T, what string, got, want buffer.View) {
t.Helper()
if !bytes.Equal(got, want) {
t.Errorf("%s = %x, want %x", what, got, want)
}
}
func checkData(t *testing.T, pkt *PacketBuffer, want []byte) {
t.Helper()
if got := concatViews(pkt.Data().Views()...); !bytes.Equal(got, want) {
t.Errorf("pkt.Data().Views() = %x, want %x", got, want)
}
if got := pkt.Data().Size(); got != len(want) {
t.Errorf("pkt.Data().Size() = %d, want %d", got, len(want))
}
t.Run("AsRange", func(t *testing.T) {
// Full range
checkRange(t, pkt.Data().AsRange(), want)
// SubRange
for _, off := range []int{0, 1, len(want), len(want) + 1} {
t.Run(fmt.Sprintf("SubRange%d", off), func(t *testing.T) {
// Empty when off is greater than the size of range.
var sub []byte
if off < len(want) {
sub = want[off:]
}
checkRange(t, pkt.Data().AsRange().SubRange(off), sub)
})
}
// Capped
for _, n := range []int{0, 1, len(want), len(want) + 1} {
t.Run(fmt.Sprintf("Capped%d", n), func(t *testing.T) {
sub := want
if n < len(sub) {
sub = sub[:n]
}
checkRange(t, pkt.Data().AsRange().Capped(n), sub)
})
}
})
}
func checkRange(t *testing.T, r Range, data []byte) {
if got, want := r.Size(), len(data); got != want {
t.Errorf("r.Size() = %d, want %d", got, want)
}
if got := r.AsView(); !bytes.Equal(got, data) {
t.Errorf("r.AsView() = %x, want %x", got, data)
}
if got := r.ToOwnedView(); !bytes.Equal(got, data) {
t.Errorf("r.ToOwnedView() = %x, want %x", got, data)
}
if got, want := r.Checksum(), header.Checksum(data, 0 /* initial */); got != want {
t.Errorf("r.Checksum() = %x, want %x", got, want)
}
}
func vv(pieces ...string) buffer.VectorisedView {
var views []buffer.View
var size int
for _, p := range pieces {
v := buffer.View([]byte(p))
size += len(v)
views = append(views, v)
}
return buffer.NewVectorisedView(size, views)
}
func makeView(size int) buffer.View {
b := byte(size)
return bytes.Repeat([]byte{b}, size)
}
func concatViews(views ...buffer.View) buffer.View {
var all buffer.View
for _, v := range views {
all = append(all, v...)
}
return all
}
|