OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [interface_test.go] - Blame information for rev 748

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 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 net
6
 
7
import (
8
        "bytes"
9
        "reflect"
10
        "strings"
11
        "testing"
12
)
13
 
14
func sameInterface(i, j *Interface) bool {
15
        if i == nil || j == nil {
16
                return false
17
        }
18
        if i.Index == j.Index && i.Name == j.Name && bytes.Equal(i.HardwareAddr, j.HardwareAddr) {
19
                return true
20
        }
21
        return false
22
}
23
 
24
func TestInterfaces(t *testing.T) {
25
        ift, err := Interfaces()
26
        if err != nil {
27
                t.Fatalf("Interfaces failed: %v", err)
28
        }
29
        t.Logf("table: len/cap = %v/%v\n", len(ift), cap(ift))
30
 
31
        for _, ifi := range ift {
32
                ifxi, err := InterfaceByIndex(ifi.Index)
33
                if err != nil {
34
                        t.Fatalf("InterfaceByIndex(%#q) failed: %v", ifi.Index, err)
35
                }
36
                if !sameInterface(ifxi, &ifi) {
37
                        t.Fatalf("InterfaceByIndex(%#q) = %v, want %v", ifi.Index, *ifxi, ifi)
38
                }
39
                ifxn, err := InterfaceByName(ifi.Name)
40
                if err != nil {
41
                        t.Fatalf("InterfaceByName(%#q) failed: %v", ifi.Name, err)
42
                }
43
                if !sameInterface(ifxn, &ifi) {
44
                        t.Fatalf("InterfaceByName(%#q) = %v, want %v", ifi.Name, *ifxn, ifi)
45
                }
46
                t.Logf("%q: flags %q, ifindex %v, mtu %v\n", ifi.Name, ifi.Flags.String(), ifi.Index, ifi.MTU)
47
                t.Logf("\thardware address %q", ifi.HardwareAddr.String())
48
                testInterfaceAddrs(t, &ifi)
49
                testInterfaceMulticastAddrs(t, &ifi)
50
        }
51
}
52
 
53
func TestInterfaceAddrs(t *testing.T) {
54
        ifat, err := InterfaceAddrs()
55
        if err != nil {
56
                t.Fatalf("InterfaceAddrs failed: %v", err)
57
        }
58
        t.Logf("table: len/cap = %v/%v\n", len(ifat), cap(ifat))
59
        testAddrs(t, ifat)
60
}
61
 
62
func testInterfaceAddrs(t *testing.T, ifi *Interface) {
63
        ifat, err := ifi.Addrs()
64
        if err != nil {
65
                t.Fatalf("Interface.Addrs failed: %v", err)
66
        }
67
        testAddrs(t, ifat)
68
}
69
 
70
func testInterfaceMulticastAddrs(t *testing.T, ifi *Interface) {
71
        ifmat, err := ifi.MulticastAddrs()
72
        if err != nil {
73
                t.Fatalf("Interface.MulticastAddrs failed: %v", err)
74
        }
75
        testMulticastAddrs(t, ifmat)
76
}
77
 
78
func testAddrs(t *testing.T, ifat []Addr) {
79
        for _, ifa := range ifat {
80
                switch ifa.(type) {
81
                case *IPAddr, *IPNet:
82
                        t.Logf("\tinterface address %q\n", ifa.String())
83
                default:
84
                        t.Errorf("\tunexpected type: %T", ifa)
85
                }
86
        }
87
}
88
 
89
func testMulticastAddrs(t *testing.T, ifmat []Addr) {
90
        for _, ifma := range ifmat {
91
                switch ifma.(type) {
92
                case *IPAddr:
93
                        t.Logf("\tjoined group address %q\n", ifma.String())
94
                default:
95
                        t.Errorf("\tunexpected type: %T", ifma)
96
                }
97
        }
98
}
99
 
100
var mactests = []struct {
101
        in  string
102
        out HardwareAddr
103
        err string
104
}{
105
        {"01:23:45:67:89:AB", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab}, ""},
106
        {"01-23-45-67-89-AB", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab}, ""},
107
        {"0123.4567.89AB", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab}, ""},
108
        {"ab:cd:ef:AB:CD:EF", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef}, ""},
109
        {"01.02.03.04.05.06", nil, "invalid MAC address"},
110
        {"01:02:03:04:05:06:", nil, "invalid MAC address"},
111
        {"x1:02:03:04:05:06", nil, "invalid MAC address"},
112
        {"01002:03:04:05:06", nil, "invalid MAC address"},
113
        {"01:02003:04:05:06", nil, "invalid MAC address"},
114
        {"01:02:03004:05:06", nil, "invalid MAC address"},
115
        {"01:02:03:04005:06", nil, "invalid MAC address"},
116
        {"01:02:03:04:05006", nil, "invalid MAC address"},
117
        {"01-02:03:04:05:06", nil, "invalid MAC address"},
118
        {"01:02-03-04-05-06", nil, "invalid MAC address"},
119
        {"0123:4567:89AF", nil, "invalid MAC address"},
120
        {"0123-4567-89AF", nil, "invalid MAC address"},
121
        {"01:23:45:67:89:AB:CD:EF", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, ""},
122
        {"01-23-45-67-89-AB-CD-EF", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, ""},
123
        {"0123.4567.89AB.CDEF", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, ""},
124
}
125
 
126
func match(err error, s string) bool {
127
        if s == "" {
128
                return err == nil
129
        }
130
        return err != nil && strings.Contains(err.Error(), s)
131
}
132
 
133
func TestParseMAC(t *testing.T) {
134
        for _, tt := range mactests {
135
                out, err := ParseMAC(tt.in)
136
                if !reflect.DeepEqual(out, tt.out) || !match(err, tt.err) {
137
                        t.Errorf("ParseMAC(%q) = %v, %v, want %v, %v", tt.in, out, err, tt.out,
138
                                tt.err)
139
                }
140
        }
141
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.