-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol_test.go
More file actions
190 lines (183 loc) · 8.04 KB
/
Copy pathcontrol_test.go
File metadata and controls
190 lines (183 loc) · 8.04 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
package mipstack
import (
"encoding/binary"
"net/netip"
"testing"
)
func TestIPv4ControlMessageMarshalAndParse(t *testing.T) {
source := netip.MustParseAddr("192.0.2.123")
outgoing := &IPv4ControlMessage{TTL: 31, TOS: 0xb8, Src: source}
control, err := outgoing.Marshal()
if err != nil {
t.Fatal(err)
}
parsedSource, options, err := parseControlMessageForWrite(control, false)
if err != nil || parsedSource != source || options != (ipPacketOptions{hopLimit: 31, trafficClass: 0xb8, hopLimitSet: true, trafficClassSet: true}) {
t.Fatalf("marshaled IPv4 control = source %v options %+v, %v", parsedSource, options, err)
}
var incoming IPv4ControlMessage
receivedControl, err := controlMessageForRead(source, options)
if err != nil {
t.Fatal(err)
}
if err = incoming.Parse(receivedControl); err != nil {
t.Fatal(err)
}
if incoming != (IPv4ControlMessage{TTL: 31, TOS: 0xb8, Dst: source}) {
t.Fatalf("parsed IPv4 control = %+v", incoming)
}
if control, err = (*IPv4ControlMessage)(nil).Marshal(); err != nil || control != nil {
t.Fatalf("nil IPv4 control marshal = %x, %v", control, err)
}
}
func TestIPv6ControlMessageMarshalAndParse(t *testing.T) {
source := netip.MustParseAddr("2001:db8::123")
outgoing := &IPv6ControlMessage{TrafficClass: 0x2e, HopLimit: 29, FlowLabel: 0xabcde, Src: source}
control, err := outgoing.Marshal()
if err != nil {
t.Fatal(err)
}
parsedSource, options, err := parseControlMessageForWrite(control, true)
if err != nil || parsedSource != source || options != (ipPacketOptions{hopLimit: 29, trafficClass: 0x2e, flowLabel: 0xabcde, hopLimitSet: true, trafficClassSet: true, flowLabelSet: true}) {
t.Fatalf("marshaled IPv6 control = source %v options %+v, %v", parsedSource, options, err)
}
var incoming IPv6ControlMessage
receivedControl, err := controlMessageForRead(source, options)
if err != nil {
t.Fatal(err)
}
if err = incoming.Parse(receivedControl); err != nil {
t.Fatal(err)
}
if incoming != (IPv6ControlMessage{TrafficClass: 0x2e, HopLimit: 29, FlowLabel: 0xabcde, Dst: source}) {
t.Fatalf("parsed IPv6 control = %+v", incoming)
}
if control, err = (*IPv6ControlMessage)(nil).Marshal(); err != nil || control != nil {
t.Fatalf("nil IPv6 control marshal = %x, %v", control, err)
}
}
func TestIPv6ZeroFlowInfoIsOmittedOnRead(t *testing.T) {
control, err := (&IPv6ControlMessage{HopLimit: 64, Dst: netip.MustParseAddr("2001:db8::124")}).marshalForRead()
if err != nil {
t.Fatal(err)
}
for remaining := control; len(remaining) != 0; {
length := int(binary.LittleEndian.Uint64(remaining[:8]))
if binary.LittleEndian.Uint32(remaining[8:12]) == linuxLevelIPv6 &&
binary.LittleEndian.Uint32(remaining[12:16]) == linuxIPv6FlowInfo {
t.Fatal("zero IPv6 flow info was emitted on receive")
}
aligned := (length + linuxControlAlignment - 1) &^ (linuxControlAlignment - 1)
remaining = remaining[aligned:]
}
}
func TestControlMessageValidation(t *testing.T) {
invalid := []struct {
name string
marshal func() ([]byte, error)
}{
{name: "IPv4 interface", marshal: func() ([]byte, error) { return (&IPv4ControlMessage{IfIndex: 1}).Marshal() }},
{name: "IPv4 source", marshal: func() ([]byte, error) { return (&IPv4ControlMessage{Src: netip.IPv6Unspecified()}).Marshal() }},
{name: "IPv4 TTL", marshal: func() ([]byte, error) { return (&IPv4ControlMessage{TTL: 256}).Marshal() }},
{name: "IPv4 TOS", marshal: func() ([]byte, error) { return (&IPv4ControlMessage{TOS: -1}).Marshal() }},
{name: "IPv6 interface", marshal: func() ([]byte, error) { return (&IPv6ControlMessage{IfIndex: 1}).Marshal() }},
{name: "IPv6 source", marshal: func() ([]byte, error) { return (&IPv6ControlMessage{Src: netip.IPv4Unspecified()}).Marshal() }},
{name: "IPv6 hop limit", marshal: func() ([]byte, error) { return (&IPv6ControlMessage{HopLimit: 256}).Marshal() }},
{name: "IPv6 traffic class", marshal: func() ([]byte, error) { return (&IPv6ControlMessage{TrafficClass: -1}).Marshal() }},
{name: "IPv6 flow label", marshal: func() ([]byte, error) { return (&IPv6ControlMessage{FlowLabel: ipv6MaximumFlowLabel + 1}).Marshal() }},
}
for _, test := range invalid {
t.Run(test.name, func(t *testing.T) {
if _, err := test.marshal(); err == nil {
t.Fatal("invalid control message marshaled successfully")
}
})
}
var ipv4 *IPv4ControlMessage
if err := ipv4.Parse(nil); err == nil {
t.Fatal("nil IPv4 control receiver parsed successfully")
}
var ipv6 *IPv6ControlMessage
if err := ipv6.Parse(nil); err == nil {
t.Fatal("nil IPv6 control receiver parsed successfully")
}
for _, address := range []netip.Addr{
netip.MustParseAddr("192.0.2.1"),
netip.MustParseAddr("2001:db8::1"),
} {
control, err := controlMessageForRead(address, ipPacketOptions{})
if err != nil {
t.Fatalf("controlMessageForRead(%v): %v", address, err)
}
parsedAddress, options, err := parseLinuxIPControlValues(control, address.Is6(), true)
if err != nil || parsedAddress != address || options.hopLimit != 0 {
t.Fatalf("zero-hop control for %v = %v, %+v, %v", address, parsedAddress, options, err)
}
}
zeroHopLimit := appendLinuxControlInt32(nil, linuxLevelIPv6, linuxIPv6HopLimit, 0)
_, options, err := parseLinuxIPControlValues(zeroHopLimit, true, false)
if err != nil || options.hopLimit != 0 || !options.hopLimitSet {
t.Fatalf("IPv6 zero hop-limit control = %+v, %v", options, err)
}
zeroTTL := appendLinuxControlInt32(nil, linuxLevelIP, linuxIPTimeToLive, 0)
if _, _, err = parseLinuxIPControlValues(zeroTTL, false, false); err == nil {
t.Fatal("IPv4 zero TTL control message parsed successfully")
}
var zeroFlowInfo [4]byte
zeroFlowControl := appendLinuxControl(nil, linuxLevelIPv6, linuxIPv6FlowInfo, zeroFlowInfo[:])
_, options, err = parseLinuxIPControlValues(zeroFlowControl, true, false)
if err != nil || options.flowLabel != 0 || !options.flowLabelSet {
t.Fatalf("IPv6 explicit zero flow label = %+v, %v", options, err)
}
conflicting := appendLinuxControlInt32(nil, linuxLevelIPv6, linuxIPv6TrafficClass, 1)
flowInfo := [4]byte{0x02, 0, 0, 1}
conflicting = appendLinuxControl(conflicting, linuxLevelIPv6, linuxIPv6FlowInfo, flowInfo[:])
if _, _, err = parseLinuxIPControlValues(conflicting, true, false); err == nil {
t.Fatal("conflicting IPv6 flow-info traffic class parsed successfully")
}
}
// FuzzControlMessageParsing keeps the public read semantics and internal write
// semantics on the same panic-free ancillary decoder.
func FuzzControlMessageParsing(f *testing.F) {
control4, _ := (&IPv4ControlMessage{TTL: 31, TOS: 0xb8, Src: netip.MustParseAddr("192.0.2.1")}).Marshal()
control6, _ := (&IPv6ControlMessage{HopLimit: 29, TrafficClass: 0x2e, FlowLabel: 0x12345, Src: netip.MustParseAddr("2001:db8::1")}).Marshal()
f.Add([]byte(nil))
f.Add(control4)
f.Add(control6)
f.Fuzz(func(t *testing.T, control []byte) {
var ipv4 IPv4ControlMessage
_ = ipv4.Parse(control)
_, _, _ = parseControlMessageForWrite(control, false)
var ipv6 IPv6ControlMessage
_ = ipv6.Parse(control)
_, _, _ = parseControlMessageForWrite(control, true)
})
}
func BenchmarkControlMessageMarshal(b *testing.B) {
address4 := netip.MustParseAddr("192.0.2.245")
address6 := netip.MustParseAddr("2001:db8::245")
benchmarks := []struct {
name string
marshal func() ([]byte, error)
}{
{name: "IPv4Send", marshal: (&IPv4ControlMessage{TTL: 31, TOS: 0xb8, Src: address4}).Marshal},
{name: "IPv4Receive", marshal: func() ([]byte, error) {
return controlMessageForRead(address4, ipPacketOptions{hopLimit: 31, trafficClass: 0xb8})
}},
{name: "IPv6Send", marshal: (&IPv6ControlMessage{HopLimit: 29, TrafficClass: 0x2e, FlowLabel: 0x12345, Src: address6}).Marshal},
{name: "IPv6Receive", marshal: func() ([]byte, error) {
return controlMessageForRead(address6, ipPacketOptions{hopLimit: 29, trafficClass: 0x2e, flowLabel: 0x12345})
}},
}
for _, benchmark := range benchmarks {
b.Run(benchmark.name, func(b *testing.B) {
b.ReportAllocs()
for iteration := 0; iteration < b.N; iteration++ {
control, err := benchmark.marshal()
if err != nil || len(control) == 0 {
b.Fatalf("Marshal = %d bytes, %v", len(control), err)
}
}
})
}
}