Home | History | Annotate | Download | only in route
      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 // +build darwin dragonfly freebsd netbsd openbsd
      6 
      7 package route
      8 
      9 import (
     10 	"reflect"
     11 	"testing"
     12 )
     13 
     14 type parseAddrsTest struct {
     15 	attrs uint
     16 	fn    func(int, []byte) (int, Addr, error)
     17 	b     []byte
     18 	as    []Addr
     19 }
     20 
     21 var parseAddrsLittleEndianTests = []parseAddrsTest{
     22 	{
     23 		sysRTA_DST | sysRTA_GATEWAY | sysRTA_NETMASK | sysRTA_BRD,
     24 		parseKernelInetAddr,
     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 			&Inet4Addr{IP: [4]byte{172, 16, 220, 180}},
     52 			nil,
     53 			nil,
     54 			nil,
     55 			nil,
     56 			&Inet4Addr{IP: [4]byte{172, 16, 220, 255}},
     57 		},
     58 	},
     59 	{
     60 		sysRTA_NETMASK | sysRTA_IFP | sysRTA_IFA,
     61 		parseKernelInetAddr,
     62 		[]byte{
     63 			0x7, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0,
     64 
     65 			0x18, 0x12, 0xa, 0x0, 0x87, 0x8, 0x0, 0x0,
     66 			0x76, 0x6c, 0x61, 0x6e, 0x35, 0x36, 0x38, 0x32,
     67 			0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
     68 
     69 			0x10, 0x2, 0x0, 0x0, 0xa9, 0xfe, 0x0, 0x1,
     70 			0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
     71 		},
     72 		[]Addr{
     73 			nil,
     74 			nil,
     75 			&Inet4Addr{IP: [4]byte{255, 255, 255, 0}},
     76 			nil,
     77 			&LinkAddr{Index: 10, Name: "vlan5682"},
     78 			&Inet4Addr{IP: [4]byte{169, 254, 0, 1}},
     79 			nil,
     80 			nil,
     81 		},
     82 	},
     83 }
     84 
     85 func TestParseAddrs(t *testing.T) {
     86 	tests := parseAddrsLittleEndianTests
     87 	if nativeEndian != littleEndian {
     88 		t.Skip("no test for non-little endian machine yet")
     89 	}
     90 
     91 	for i, tt := range tests {
     92 		as, err := parseAddrs(tt.attrs, tt.fn, tt.b)
     93 		if err != nil {
     94 			t.Error(i, err)
     95 			continue
     96 		}
     97 		as = as[:8] // the list varies between operating systems
     98 		if !reflect.DeepEqual(as, tt.as) {
     99 			t.Errorf("#%d: got %+v; want %+v", i, as, tt.as)
    100 			continue
    101 		}
    102 	}
    103 }
    104