| 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 |
package routebsd |
| 6 |
|
| 7 |
import ( |
| 8 |
"net/netip" |
| 9 |
"reflect" |
| 10 |
"syscall" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
type parseAddrsOnDarwinTest struct { |
| 15 |
attrs uint |
| 16 |
b []byte |
| 17 |
as []Addr |
| 18 |
} |
| 19 |
|
| 20 |
var parseAddrsOnDarwinLittleEndianTests = []parseAddrsOnDarwinTest{ |
| 21 |
{ |
| 22 |
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK, |
| 23 |
[]byte{ |
| 24 |
0x10, 0x2, 0x0, 0x0, 0xc0, 0xa8, 0x56, 0x0, |
| 25 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 26 |
|
| 27 |
0x14, 0x12, 0x4, 0x0, 0x6, 0x0, 0x0, 0x0, |
| 28 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 29 |
0x0, 0x0, 0x0, 0x0, |
| 30 |
|
| 31 |
0x7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 32 |
}, |
| 33 |
[]Addr{ |
| 34 |
&InetAddr{IP: netip.AddrFrom4([4]byte{192, 168, 86, 0})}, |
| 35 |
&LinkAddr{Index: 4}, |
| 36 |
&InetAddr{IP: netip.AddrFrom4([4]byte{255, 255, 255, 255})}, |
| 37 |
nil, |
| 38 |
nil, |
| 39 |
nil, |
| 40 |
nil, |
| 41 |
nil, |
| 42 |
}, |
| 43 |
}, |
| 44 |
{ |
| 45 |
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK, |
| 46 |
[]byte{ |
| 47 |
0x10, 0x02, 0x00, 0x00, 0x64, 0x71, 0x00, 0x00, |
| 48 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 49 |
|
| 50 |
0x14, 0x12, 0x21, 0x00, 0x01, 0x08, 0x00, 0x00, |
| 51 |
0x75, 0x74, 0x75, 0x6e, 0x34, 0x33, 0x31, 0x39, |
| 52 |
0x00, 0x00, 0x00, 0x00, |
| 53 |
|
| 54 |
0x06, 0x02, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, |
| 55 |
}, |
| 56 |
[]Addr{ |
| 57 |
&InetAddr{IP: netip.AddrFrom4([4]byte{100, 113, 0, 0})}, |
| 58 |
&LinkAddr{Index: 33, Name: "utun4319"}, |
| 59 |
&InetAddr{IP: netip.AddrFrom4([4]byte{255, 255, 0, 0})}, |
| 60 |
nil, |
| 61 |
nil, |
| 62 |
nil, |
| 63 |
nil, |
| 64 |
nil, |
| 65 |
}, |
| 66 |
}, |
| 67 |
// route -n add -inet6 fd84:1b4e:6281:: -prefixlen 48 fe80::f22f:4bff:fe09:3bff%utun4319 |
| 68 |
// gw fe80:0000:0000:0000:f22f:4bff:fe09:3bff |
| 69 |
{ |
| 70 |
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK, |
| 71 |
[]byte{ |
| 72 |
0x1c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 73 |
0xfd, 0x84, 0x1b, 0x4e, 0x62, 0x81, 0x00, 0x00, |
| 74 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 75 |
0x00, 0x00, 0x00, 0x00, |
| 76 |
|
| 77 |
0x1c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 78 |
0xfe, 0x80, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, |
| 79 |
0xf2, 0x2f, 0x4b, 0xff, 0xfe, 0x09, 0x3b, 0xff, |
| 80 |
0x00, 0x00, 0x00, 0x00, |
| 81 |
|
| 82 |
0x0e, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 83 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, |
| 84 |
}, |
| 85 |
[]Addr{ |
| 86 |
&InetAddr{IP: netip.AddrFrom16([16]byte{0xfd, 0x84, 0x1b, 0x4e, 0x62, 0x81})}, |
| 87 |
&InetAddr{IP: netip.AddrFrom16([16]byte{0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf2, 0x2f, 0x4b, 0xff, 0xfe, 0x09, 0x3b, 0xff})}, |
| 88 |
&InetAddr{IP: netip.AddrFrom16([16]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff})}, |
| 89 |
nil, |
| 90 |
nil, |
| 91 |
nil, |
| 92 |
nil, |
| 93 |
nil, |
| 94 |
}, |
| 95 |
}, |
| 96 |
// golang/go#70528, the kernel can produce addresses of length 0 |
| 97 |
{ |
| 98 |
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK, |
| 99 |
[]byte{ |
| 100 |
0x00, 0x1e, 0x00, 0x00, |
| 101 |
|
| 102 |
0x1c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 103 |
0xfe, 0x80, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00, |
| 104 |
0xf2, 0x2f, 0x4b, 0xff, 0xfe, 0x09, 0x3b, 0xff, |
| 105 |
0x00, 0x00, 0x00, 0x00, |
| 106 |
|
| 107 |
0x0e, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 108 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, |
| 109 |
}, |
| 110 |
[]Addr{ |
| 111 |
&InetAddr{IP: netip.AddrFrom16([16]byte{})}, |
| 112 |
&InetAddr{IP: netip.AddrFrom16([16]byte{0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf2, 0x2f, 0x4b, 0xff, 0xfe, 0x09, 0x3b, 0xff})}, |
| 113 |
&InetAddr{IP: netip.AddrFrom16([16]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff})}, |
| 114 |
nil, |
| 115 |
nil, |
| 116 |
nil, |
| 117 |
nil, |
| 118 |
nil, |
| 119 |
}, |
| 120 |
}, |
| 121 |
// Additional case: golang/go/issues/70528#issuecomment-2498692877 |
| 122 |
{ |
| 123 |
syscall.RTA_DST | syscall.RTA_GATEWAY | syscall.RTA_NETMASK, |
| 124 |
[]byte{ |
| 125 |
0x84, 0x00, 0x05, 0x04, 0x01, 0x00, 0x00, 0x00, 0x03, 0x08, 0x00, 0x01, 0x15, 0x00, 0x00, 0x00, |
| 126 |
0x1B, 0x01, 0x00, 0x00, 0xF5, 0x5A, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 127 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 128 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 129 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 130 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x02, 0x00, 0x00, |
| 131 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, |
| 132 |
0x14, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 133 |
0x00, 0x00, 0x00, 0x00, |
| 134 |
}, |
| 135 |
[]Addr{ |
| 136 |
&InetAddr{IP: netip.AddrFrom4([4]byte{0x0, 0x0, 0x0, 0x0})}, |
| 137 |
nil, |
| 138 |
nil, |
| 139 |
nil, |
| 140 |
nil, |
| 141 |
nil, |
| 142 |
nil, |
| 143 |
nil, |
| 144 |
}, |
| 145 |
}, |
| 146 |
} |
| 147 |
|
| 148 |
func TestParseAddrsOnDarwin(t *testing.T) { |
| 149 |
tests := parseAddrsOnDarwinLittleEndianTests |
| 150 |
if nativeEndian != littleEndian { |
| 151 |
t.Skip("no test for non-little endian machine yet") |
| 152 |
} |
| 153 |
|
| 154 |
for i, tt := range tests { |
| 155 |
as, err := parseAddrs(tt.attrs, tt.b) |
| 156 |
if err != nil { |
| 157 |
t.Error(i, err) |
| 158 |
continue |
| 159 |
} |
| 160 |
if !reflect.DeepEqual(as, tt.as) { |
| 161 |
t.Errorf("#%d: got %+v; want %+v", i, as, tt.as) |
| 162 |
continue |
| 163 |
} |
| 164 |
} |
| 165 |
} |