| 1 |
// Copyright 2016 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. |
| 4 |
|
| 5 |
//go:build darwin || dragonfly || freebsd || netbsd || openbsd |
| 6 |
|
| 7 |
package routebsd |
| 8 |
|
| 9 |
import ( |
| 10 |
"net/netip" |
| 11 |
"reflect" |
| 12 |
"syscall" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
type parseAddrsTest struct { |
| 17 |
attrs uint |
| 18 |
b []byte |
| 19 |
as []Addr |
| 20 |
} |
| 21 |
|
| 22 |
var parseAddrsLittleEndianTests = []parseAddrsTest{ |
| 23 |
{ |
| 24 |
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK | syscall.RTA_BRD, |
| 25 |
[]byte{ |
| 26 |
0x38, 0x12, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0, |
| 27 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 28 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 29 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 30 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 31 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 32 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 33 |
|
| 34 |
0x38, 0x12, 0x2, 0x0, 0x6, 0x3, 0x6, 0x0, |
| 35 |
0x65, 0x6d, 0x31, 0x0, 0xc, 0x29, 0x66, 0x2c, |
| 36 |
0xdc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 37 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 38 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 39 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 40 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 41 |
|
| 42 |
0x10, 0x2, 0x0, 0x0, 0xac, 0x10, 0xdc, 0xb4, |
| 43 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 44 |
|
| 45 |
0x10, 0x2, 0x0, 0x0, 0xac, 0x10, 0xdc, 0xff, |
| 46 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 47 |
}, |
| 48 |
[]Addr{ |
| 49 |
&LinkAddr{Index: 0}, |
| 50 |
&LinkAddr{Index: 2, Name: "em1", Addr: []byte{0x00, 0x0c, 0x29, 0x66, 0x2c, 0xdc}}, |
| 51 |
&InetAddr{IP: netip.AddrFrom4([4]byte{172, 16, 220, 180})}, |
| 52 |
nil, |
| 53 |
nil, |
| 54 |
nil, |
| 55 |
nil, |
| 56 |
&InetAddr{IP: netip.AddrFrom4([4]byte{172, 16, 220, 255})}, |
| 57 |
}, |
| 58 |
}, |
| 59 |
{ |
| 60 |
syscall.RTA_NETMASK | syscall.RTA_IFP | syscall.RTA_IFA, |
| 61 |
[]byte{ |
| 62 |
0x7, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0, |
| 63 |
|
| 64 |
0x18, 0x12, 0xa, 0x0, 0x87, 0x8, 0x0, 0x0, |
| 65 |
0x76, 0x6c, 0x61, 0x6e, 0x35, 0x36, 0x38, 0x32, |
| 66 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 67 |
|
| 68 |
0x10, 0x2, 0x0, 0x0, 0xa9, 0xfe, 0x0, 0x1, |
| 69 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 70 |
}, |
| 71 |
[]Addr{ |
| 72 |
nil, |
| 73 |
nil, |
| 74 |
&InetAddr{IP: netip.AddrFrom4([4]byte{255, 255, 255, 0})}, |
| 75 |
nil, |
| 76 |
&LinkAddr{Index: 10, Name: "vlan5682"}, |
| 77 |
&InetAddr{IP: netip.AddrFrom4([4]byte{169, 254, 0, 1})}, |
| 78 |
nil, |
| 79 |
nil, |
| 80 |
}, |
| 81 |
}, |
| 82 |
} |
| 83 |
|
| 84 |
func TestParseAddrs(t *testing.T) { |
| 85 |
tests := parseAddrsLittleEndianTests |
| 86 |
if nativeEndian != littleEndian { |
| 87 |
t.Skip("no test for non-little endian machine yet") |
| 88 |
} |
| 89 |
|
| 90 |
for i, tt := range tests { |
| 91 |
as, err := parseAddrs(tt.attrs, tt.b) |
| 92 |
if err != nil { |
| 93 |
t.Error(i, err) |
| 94 |
continue |
| 95 |
} |
| 96 |
as = as[:8] // the list varies between operating systems |
| 97 |
if !reflect.DeepEqual(as, tt.as) { |
| 98 |
t.Errorf("#%d: got %+v; want %+v", i, as, tt.as) |
| 99 |
continue |
| 100 |
} |
| 101 |
} |
| 102 |
} |