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
|
// 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 header_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"gvisor.dev/gvisor/pkg/tcpip/buffer"
"gvisor.dev/gvisor/pkg/tcpip/header"
)
func TestIPv4OptionsSerializer(t *testing.T) {
optCases := []struct {
name string
option []header.IPv4SerializableOption
expect []byte
}{
{
name: "NOP",
option: []header.IPv4SerializableOption{
&header.IPv4SerializableNOPOption{},
},
expect: []byte{1, 0, 0, 0},
},
{
name: "ListEnd",
option: []header.IPv4SerializableOption{
&header.IPv4SerializableListEndOption{},
},
expect: []byte{0, 0, 0, 0},
},
{
name: "RouterAlert",
option: []header.IPv4SerializableOption{
&header.IPv4SerializableRouterAlertOption{},
},
expect: []byte{148, 4, 0, 0},
}, {
name: "NOP and RouterAlert",
option: []header.IPv4SerializableOption{
&header.IPv4SerializableNOPOption{},
&header.IPv4SerializableRouterAlertOption{},
},
expect: []byte{1, 148, 4, 0, 0, 0, 0, 0},
},
}
for _, opt := range optCases {
t.Run(opt.name, func(t *testing.T) {
s := header.IPv4OptionsSerializer(opt.option)
l := s.Length()
if got := len(opt.expect); got != int(l) {
t.Fatalf("s.Length() = %d, want = %d", got, l)
}
b := make([]byte, l)
for i := range b {
// Fill the buffer with full bytes to ensure padding is being set
// correctly.
b[i] = 0xFF
}
if serializedLength := s.Serialize(b); serializedLength != l {
t.Fatalf("s.Serialize(_) = %d, want %d", serializedLength, l)
}
if diff := cmp.Diff(opt.expect, b); diff != "" {
t.Errorf("mismatched serialized option (-want +got):\n%s", diff)
}
})
}
}
// TestIPv4Encode checks that ipv4.Encode correctly fills out the requested
// fields when options are supplied.
func TestIPv4EncodeOptions(t *testing.T) {
tests := []struct {
name string
numberOfNops int
encodedOptions header.IPv4Options // reply should look like this
wantIHL int
}{
{
name: "valid no options",
wantIHL: header.IPv4MinimumSize,
},
{
name: "one byte options",
numberOfNops: 1,
encodedOptions: header.IPv4Options{1, 0, 0, 0},
wantIHL: header.IPv4MinimumSize + 4,
},
{
name: "two byte options",
numberOfNops: 2,
encodedOptions: header.IPv4Options{1, 1, 0, 0},
wantIHL: header.IPv4MinimumSize + 4,
},
{
name: "three byte options",
numberOfNops: 3,
encodedOptions: header.IPv4Options{1, 1, 1, 0},
wantIHL: header.IPv4MinimumSize + 4,
},
{
name: "four byte options",
numberOfNops: 4,
encodedOptions: header.IPv4Options{1, 1, 1, 1},
wantIHL: header.IPv4MinimumSize + 4,
},
{
name: "five byte options",
numberOfNops: 5,
encodedOptions: header.IPv4Options{1, 1, 1, 1, 1, 0, 0, 0},
wantIHL: header.IPv4MinimumSize + 8,
},
{
name: "thirty nine byte options",
numberOfNops: 39,
encodedOptions: header.IPv4Options{
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 0,
},
wantIHL: header.IPv4MinimumSize + 40,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
serializeOpts := header.IPv4OptionsSerializer(make([]header.IPv4SerializableOption, test.numberOfNops))
for i := range serializeOpts {
serializeOpts[i] = &header.IPv4SerializableNOPOption{}
}
paddedOptionLength := serializeOpts.Length()
ipHeaderLength := int(header.IPv4MinimumSize + paddedOptionLength)
if ipHeaderLength > header.IPv4MaximumHeaderSize {
t.Fatalf("IP header length too large: got = %d, want <= %d ", ipHeaderLength, header.IPv4MaximumHeaderSize)
}
totalLen := uint16(ipHeaderLength)
hdr := buffer.NewPrependable(int(totalLen))
ip := header.IPv4(hdr.Prepend(ipHeaderLength))
// To check the padding works, poison the last byte of the options space.
if paddedOptionLength != serializeOpts.Length() {
ip.SetHeaderLength(uint8(ipHeaderLength))
ip.Options()[paddedOptionLength-1] = 0xff
ip.SetHeaderLength(0)
}
ip.Encode(&header.IPv4Fields{
Options: serializeOpts,
})
options := ip.Options()
wantOptions := test.encodedOptions
if got, want := int(ip.HeaderLength()), test.wantIHL; got != want {
t.Errorf("got IHL of %d, want %d", got, want)
}
// cmp.Diff does not consider nil slices equal to empty slices, but we do.
if len(wantOptions) == 0 && len(options) == 0 {
return
}
if diff := cmp.Diff(wantOptions, options); diff != "" {
t.Errorf("options mismatch (-want +got):\n%s", diff)
}
})
}
}
|