golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/route/address_test.go (about) 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 route 8 9 import ( 10 "reflect" 11 "syscall" 12 "testing" 13 ) 14 15 type parseAddrsTest struct { 16 attrs uint 17 fn func(int, []byte) (int, Addr, error) 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 parseKernelInetAddr, 26 []byte{ 27 0x38, 0x12, 0x0, 0x0, 0xff, 0xff, 0xff, 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 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 34 35 0x38, 0x12, 0x2, 0x0, 0x6, 0x3, 0x6, 0x0, 36 0x65, 0x6d, 0x31, 0x0, 0xc, 0x29, 0x66, 0x2c, 37 0xdc, 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 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 42 43 0x10, 0x2, 0x0, 0x0, 0xac, 0x10, 0xdc, 0xb4, 44 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 45 46 0x10, 0x2, 0x0, 0x0, 0xac, 0x10, 0xdc, 0xff, 47 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 48 }, 49 []Addr{ 50 &LinkAddr{Index: 0}, 51 &LinkAddr{Index: 2, Name: "em1", Addr: []byte{0x00, 0x0c, 0x29, 0x66, 0x2c, 0xdc}}, 52 &Inet4Addr{IP: [4]byte{172, 16, 220, 180}}, 53 nil, 54 nil, 55 nil, 56 nil, 57 &Inet4Addr{IP: [4]byte{172, 16, 220, 255}}, 58 }, 59 }, 60 { 61 syscall.RTA_NETMASK | syscall.RTA_IFP | syscall.RTA_IFA, 62 parseKernelInetAddr, 63 []byte{ 64 0x7, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0, 65 66 0x18, 0x12, 0xa, 0x0, 0x87, 0x8, 0x0, 0x0, 67 0x76, 0x6c, 0x61, 0x6e, 0x35, 0x36, 0x38, 0x32, 68 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 69 70 0x10, 0x2, 0x0, 0x0, 0xa9, 0xfe, 0x0, 0x1, 71 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 72 }, 73 []Addr{ 74 nil, 75 nil, 76 &Inet4Addr{IP: [4]byte{255, 255, 255, 0}}, 77 nil, 78 &LinkAddr{Index: 10, Name: "vlan5682"}, 79 &Inet4Addr{IP: [4]byte{169, 254, 0, 1}}, 80 nil, 81 nil, 82 }, 83 }, 84 } 85 86 func TestParseAddrs(t *testing.T) { 87 tests := parseAddrsLittleEndianTests 88 if nativeEndian != littleEndian { 89 t.Skip("no test for non-little endian machine yet") 90 } 91 92 for i, tt := range tests { 93 as, err := parseAddrs(tt.attrs, tt.fn, tt.b) 94 if err != nil { 95 t.Error(i, err) 96 continue 97 } 98 as = as[:8] // the list varies between operating systems 99 if !reflect.DeepEqual(as, tt.as) { 100 t.Errorf("#%d: got %+v; want %+v", i, as, tt.as) 101 continue 102 } 103 } 104 }