1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "golang.org/x/net/internal/iana"
15 var marshalAndParseExtensionTests = []struct {
21 // MPLS label stack with no label
23 proto: iana.ProtocolICMP,
25 0x20, 0x00, 0x00, 0x00,
28 0x00, 0x04, 0x01, 0x01,
32 Class: classMPLSLabelStack,
33 Type: typeIncomingMPLSLabelStack,
37 // MPLS label stack with a single label
39 proto: iana.ProtocolIPv6ICMP,
41 0x20, 0x00, 0x00, 0x00,
44 0x00, 0x08, 0x01, 0x01,
45 0x03, 0xe8, 0xe9, 0xff,
49 Class: classMPLSLabelStack,
50 Type: typeIncomingMPLSLabelStack,
62 // MPLS label stack with multiple labels
64 proto: iana.ProtocolICMP,
66 0x20, 0x00, 0x00, 0x00,
69 0x00, 0x0c, 0x01, 0x01,
70 0x03, 0xe8, 0xde, 0xfe,
71 0x03, 0xe8, 0xe1, 0xff,
75 Class: classMPLSLabelStack,
76 Type: typeIncomingMPLSLabelStack,
94 // Interface information with no attribute
96 proto: iana.ProtocolICMP,
98 0x20, 0x00, 0x00, 0x00,
101 0x00, 0x04, 0x02, 0x00,
105 Class: classInterfaceInfo,
109 // Interface information with ifIndex and name
111 proto: iana.ProtocolICMP,
113 0x20, 0x00, 0x00, 0x00,
116 0x00, 0x10, 0x02, 0x0a,
117 0x00, 0x00, 0x00, 0x10,
118 0x08, byte('e'), byte('n'), byte('1'),
119 byte('0'), byte('1'), 0x00, 0x00,
123 Class: classInterfaceInfo,
125 Interface: &net.Interface{
132 // Interface information with ifIndex, IPAddr, name and MTU
134 proto: iana.ProtocolIPv6ICMP,
136 0x20, 0x00, 0x00, 0x00,
139 0x00, 0x28, 0x02, 0x0f,
140 0x00, 0x00, 0x00, 0x0f,
141 0x00, 0x02, 0x00, 0x00,
142 0xfe, 0x80, 0x00, 0x00,
143 0x00, 0x00, 0x00, 0x00,
144 0x00, 0x00, 0x00, 0x00,
145 0x00, 0x00, 0x00, 0x01,
146 0x08, byte('e'), byte('n'), byte('1'),
147 byte('0'), byte('1'), 0x00, 0x00,
148 0x00, 0x00, 0x20, 0x00,
152 Class: classInterfaceInfo,
154 Interface: &net.Interface{
160 IP: net.ParseIP("fe80::1"),
168 func TestMarshalAndParseExtension(t *testing.T) {
169 for i, tt := range marshalAndParseExtensionTests {
170 for j, ext := range tt.exts {
173 switch ext := ext.(type) {
174 case *MPLSLabelStack:
175 b, err = ext.Marshal(tt.proto)
177 t.Errorf("#%v/%v: %v", i, j, err)
181 b, err = ext.Marshal(tt.proto)
183 t.Errorf("#%v/%v: %v", i, j, err)
187 if !reflect.DeepEqual(b, tt.obj) {
188 t.Errorf("#%v/%v: got %#v; want %#v", i, j, b, tt.obj)
193 for j, wire := range []struct {
194 data []byte // original datagram
195 inlattr int // length of padded original datagram, a hint
196 outlattr int // length of padded original datagram, a want
199 {nil, 0, -1, errNoExtension},
200 {make([]byte, 127), 128, -1, errNoExtension},
202 {make([]byte, 128), 127, -1, errNoExtension},
203 {make([]byte, 128), 128, -1, errNoExtension},
204 {make([]byte, 128), 129, -1, errNoExtension},
206 {append(make([]byte, 128), append(tt.hdr, tt.obj...)...), 127, 128, nil},
207 {append(make([]byte, 128), append(tt.hdr, tt.obj...)...), 128, 128, nil},
208 {append(make([]byte, 128), append(tt.hdr, tt.obj...)...), 129, 128, nil},
210 {append(make([]byte, 512), append(tt.hdr, tt.obj...)...), 511, -1, errNoExtension},
211 {append(make([]byte, 512), append(tt.hdr, tt.obj...)...), 512, 512, nil},
212 {append(make([]byte, 512), append(tt.hdr, tt.obj...)...), 513, -1, errNoExtension},
214 exts, l, err := parseExtensions(wire.data, wire.inlattr)
216 t.Errorf("#%v/%v: got %v; want %v", i, j, err, wire.err)
222 if l != wire.outlattr {
223 t.Errorf("#%v/%v: got %v; want %v", i, j, l, wire.outlattr)
225 if !reflect.DeepEqual(exts, tt.exts) {
226 for j, ext := range exts {
227 switch ext := ext.(type) {
228 case *MPLSLabelStack:
229 want := tt.exts[j].(*MPLSLabelStack)
230 t.Errorf("#%v/%v: got %#v; want %#v", i, j, ext, want)
232 want := tt.exts[j].(*InterfaceInfo)
233 t.Errorf("#%v/%v: got %#v; want %#v", i, j, ext, want)
242 var parseInterfaceNameTests = []struct {
246 {[]byte{0, 'e', 'n', '0'}, errInvalidExtension},
247 {[]byte{4, 'e', 'n', '0'}, nil},
248 {[]byte{7, 'e', 'n', '0', 0xff, 0xff, 0xff, 0xff}, errInvalidExtension},
249 {[]byte{8, 'e', 'n', '0', 0xff, 0xff, 0xff}, errMessageTooShort},
252 func TestParseInterfaceName(t *testing.T) {
253 ifi := InterfaceInfo{Interface: &net.Interface{}}
254 for i, tt := range parseInterfaceNameTests {
255 if _, err := ifi.parseName(tt.b); err != tt.error {
256 t.Errorf("#%d: got %v; want %v", i, err, tt.error)