...
1
2
3
4
5
6
7 package lif
8
9 import (
10 "fmt"
11 "syscall"
12 "testing"
13 )
14
15 func (ll *Link) String() string {
16 return fmt.Sprintf("name=%s index=%d type=%d flags=%#x mtu=%d addr=%v", ll.Name, ll.Index, ll.Type, ll.Flags, ll.MTU, llAddr(ll.Addr))
17 }
18
19 type linkPack struct {
20 af int
21 lls []Link
22 }
23
24 func linkPacks() ([]linkPack, error) {
25 var lastErr error
26 var lps []linkPack
27 for _, af := range [...]int{syscall.AF_UNSPEC, syscall.AF_INET, syscall.AF_INET6} {
28 lls, err := Links(af, "")
29 if err != nil {
30 lastErr = err
31 continue
32 }
33 lps = append(lps, linkPack{af: af, lls: lls})
34 }
35 return lps, lastErr
36 }
37
38 func TestLinks(t *testing.T) {
39 lps, err := linkPacks()
40 if len(lps) == 0 && err != nil {
41 t.Fatal(err)
42 }
43 for _, lp := range lps {
44 n := 0
45 for _, sll := range lp.lls {
46 lls, err := Links(lp.af, sll.Name)
47 if err != nil {
48 t.Fatal(lp.af, sll.Name, err)
49 }
50 for _, ll := range lls {
51 if ll.Name != sll.Name || ll.Index != sll.Index {
52 t.Errorf("af=%s got %v; want %v", addrFamily(lp.af), &ll, &sll)
53 continue
54 }
55 t.Logf("af=%s name=%s %v", addrFamily(lp.af), sll.Name, &ll)
56 n++
57 }
58 }
59 if n != len(lp.lls) {
60 t.Errorf("af=%s got %d; want %d", addrFamily(lp.af), n, len(lp.lls))
61 continue
62 }
63 }
64 }
65
View as plain text