Source file
src/net/mac_test.go
1
2
3
4
5 package net
6
7 import (
8 "reflect"
9 "strings"
10 "testing"
11 )
12
13 var parseMACTests = []struct {
14 in string
15 out HardwareAddr
16 err string
17 }{
18
19 {"00:00:5e:00:53:01", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
20 {"00-00-5e-00-53-01", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
21 {"0000.5e00.5301", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
22
23
24 {"02:00:5e:10:00:00:00:01", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
25 {"02-00-5e-10-00-00-00-01", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
26 {"0200.5e10.0000.0001", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
27
28
29 {
30 "00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01",
31 HardwareAddr{
32 0x00, 0x00, 0x00, 0x00,
33 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34 0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01,
35 },
36 "",
37 },
38 {
39 "00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01",
40 HardwareAddr{
41 0x00, 0x00, 0x00, 0x00,
42 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01,
44 },
45 "",
46 },
47 {
48 "0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001",
49 HardwareAddr{
50 0x00, 0x00, 0x00, 0x00,
51 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01,
53 },
54 "",
55 },
56
57 {"ab:cd:ef:AB:CD:EF", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef}, ""},
58 {"ab:cd:ef:AB:CD:EF:ab:cd", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd}, ""},
59 {
60 "ab:cd:ef:AB:CD:EF:ab:cd:ef:AB:CD:EF:ab:cd:ef:AB:CD:EF:ab:cd",
61 HardwareAddr{
62 0xab, 0xcd, 0xef, 0xab,
63 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef,
64 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd,
65 },
66 "",
67 },
68
69 {"01.02.03.04.05.06", nil, "invalid MAC address"},
70 {"01:02:03:04:05:06:", nil, "invalid MAC address"},
71 {"x1:02:03:04:05:06", nil, "invalid MAC address"},
72 {"01002:03:04:05:06", nil, "invalid MAC address"},
73 {"01:02003:04:05:06", nil, "invalid MAC address"},
74 {"01:02:03004:05:06", nil, "invalid MAC address"},
75 {"01:02:03:04005:06", nil, "invalid MAC address"},
76 {"01:02:03:04:05006", nil, "invalid MAC address"},
77 {"01-02:03:04:05:06", nil, "invalid MAC address"},
78 {"01:02-03-04-05-06", nil, "invalid MAC address"},
79 {"0123:4567:89AF", nil, "invalid MAC address"},
80 {"0123-4567-89AF", nil, "invalid MAC address"},
81 }
82
83 func TestParseMAC(t *testing.T) {
84 match := func(err error, s string) bool {
85 if s == "" {
86 return err == nil
87 }
88 return err != nil && strings.Contains(err.Error(), s)
89 }
90
91 for i, tt := range parseMACTests {
92 out, err := ParseMAC(tt.in)
93 if !reflect.DeepEqual(out, tt.out) || !match(err, tt.err) {
94 t.Errorf("ParseMAC(%q) = %v, %v, want %v, %v", tt.in, out, err, tt.out, tt.err)
95 }
96 if tt.err == "" {
97
98 s := out.String()
99 out2, err := ParseMAC(s)
100 if err != nil {
101 t.Errorf("%d. ParseMAC(%q) = %v", i, s, err)
102 continue
103 }
104 if !reflect.DeepEqual(out2, out) {
105 t.Errorf("%d. ParseMAC(%q) = %v, want %v", i, s, out2, out)
106 }
107 }
108 }
109 }
110
View as plain text