1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package utils
import "testing"
func TestParseMacAndFormat(t *testing.T) {
// each input should parse and round-trip to the canonical form
canonical := map[string]string{
"aa:bb:cc:dd:ee:ff": "aa:bb:cc:dd:ee:ff", // already canonical
"AA:BB:CC:DD:EE:FF": "aa:bb:cc:dd:ee:ff", // uppercase
"8:0:27:ab:cd:ef": "08:00:27:ab:cd:ef", // macOS zero-stripped octets
" de:ad:be:ef:00:01 ": "de:ad:be:ef:00:01", // surrounding space
}
for in, want := range canonical {
mac, err := ParseMac(in)
if err != nil {
t.Errorf("ParseMac(%q) unexpected error: %v", in, err)
continue
}
if got := mac.Format(); got != want {
t.Errorf("ParseMac(%q).Format() = %q, want %q", in, got, want)
}
}
}
func TestParseMacInvalid(t *testing.T) {
for _, in := range []string{
"", // empty
"aa:bb:cc:dd:ee", // too few octets
"hello", // not a mac
"zz:zz:zz:zz:zz:zz", // non-hex digits
} {
if _, err := ParseMac(in); err == nil {
t.Errorf("ParseMac(%q) = nil error, want error", in)
}
}
}
func TestParseINet4AndFormat(t *testing.T) {
for _, ip := range []string{"192.168.1.5", "10.0.0.2", "0.0.0.0", "255.255.255.255"} {
addr, err := ParseINet4(ip)
if err != nil {
t.Errorf("ParseINet4(%q) unexpected error: %v", ip, err)
continue
}
if got := addr.Format(); got != ip {
t.Errorf("ParseINet4(%q).Format() = %q, want %q", ip, got, ip)
}
}
}
func TestParseINet4Invalid(t *testing.T) {
for _, in := range []string{
"", // empty
"1.2.3", // too few octets
"192.168.1.256", // octet overflows a byte
"::1", // IPv6, not dotted-decimal
"hello", // garbage
} {
if _, err := ParseINet4(in); err == nil {
t.Errorf("ParseINet4(%q) = nil error, want error", in)
}
}
}
|