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
5package route
6
7import (
8	"testing"
9	"unsafe"
10)
11
12func TestFetchAndParseRIBOnFreeBSD(t *testing.T) {
13	for _, typ := range []RIBType{sysNET_RT_IFMALIST} {
14		var lastErr error
15		var ms []Message
16		for _, af := range []int{sysAF_UNSPEC, sysAF_INET, sysAF_INET6} {
17			rs, err := fetchAndParseRIB(af, typ)
18			if err != nil {
19				lastErr = err
20				continue
21			}
22			ms = append(ms, rs...)
23		}
24		if len(ms) == 0 && lastErr != nil {
25			t.Error(typ, lastErr)
26			continue
27		}
28		ss, err := msgs(ms).validate()
29		if err != nil {
30			t.Error(typ, err)
31			continue
32		}
33		for _, s := range ss {
34			t.Log(s)
35		}
36	}
37}
38
39func TestFetchAndParseRIBOnFreeBSD10AndAbove(t *testing.T) {
40	if _, err := FetchRIB(sysAF_UNSPEC, sysNET_RT_IFLISTL, 0); err != nil {
41		t.Skip("NET_RT_IFLISTL not supported")
42	}
43	var p uintptr
44	if kernelAlign != int(unsafe.Sizeof(p)) {
45		t.Skip("NET_RT_IFLIST vs. NET_RT_IFLISTL doesn't work for 386 emulation on amd64")
46	}
47
48	var tests = [2]struct {
49		typ  RIBType
50		b    []byte
51		msgs []Message
52		ss   []string
53	}{
54		{typ: sysNET_RT_IFLIST},
55		{typ: sysNET_RT_IFLISTL},
56	}
57	for i := range tests {
58		var lastErr error
59		for _, af := range []int{sysAF_UNSPEC, sysAF_INET, sysAF_INET6} {
60			rs, err := fetchAndParseRIB(af, tests[i].typ)
61			if err != nil {
62				lastErr = err
63				continue
64			}
65			tests[i].msgs = append(tests[i].msgs, rs...)
66		}
67		if len(tests[i].msgs) == 0 && lastErr != nil {
68			t.Error(tests[i].typ, lastErr)
69			continue
70		}
71		tests[i].ss, lastErr = msgs(tests[i].msgs).validate()
72		if lastErr != nil {
73			t.Error(tests[i].typ, lastErr)
74			continue
75		}
76		for _, s := range tests[i].ss {
77			t.Log(s)
78		}
79	}
80	for i := len(tests) - 1; i > 0; i-- {
81		if len(tests[i].ss) != len(tests[i-1].ss) {
82			t.Errorf("got %v; want %v", tests[i].ss, tests[i-1].ss)
83			continue
84		}
85		for j, s1 := range tests[i].ss {
86			s0 := tests[i-1].ss[j]
87			if s1 != s0 {
88				t.Errorf("got %s; want %s", s1, s0)
89			}
90		}
91	}
92}
93