1 // 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.
13 var mactests = []struct {
18 {"01:23:45:67:89:AB", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab}, ""},
19 {"01-23-45-67-89-AB", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab}, ""},
20 {"0123.4567.89AB", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab}, ""},
21 {"ab:cd:ef:AB:CD:EF", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef}, ""},
22 {"01.02.03.04.05.06", nil, "invalid MAC address"},
23 {"01:02:03:04:05:06:", nil, "invalid MAC address"},
24 {"x1:02:03:04:05:06", nil, "invalid MAC address"},
25 {"01002:03:04:05:06", nil, "invalid MAC address"},
26 {"01:02003:04:05:06", nil, "invalid MAC address"},
27 {"01:02:03004:05:06", nil, "invalid MAC address"},
28 {"01:02:03:04005:06", nil, "invalid MAC address"},
29 {"01:02:03:04:05006", nil, "invalid MAC address"},
30 {"01-02:03:04:05:06", nil, "invalid MAC address"},
31 {"01:02-03-04-05-06", nil, "invalid MAC address"},
32 {"0123:4567:89AF", nil, "invalid MAC address"},
33 {"0123-4567-89AF", nil, "invalid MAC address"},
34 {"01:23:45:67:89:AB:CD:EF", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, ""},
35 {"01-23-45-67-89-AB-CD-EF", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, ""},
36 {"0123.4567.89AB.CDEF", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, ""},
39 func match(err error, s string) bool {
43 return err != nil && strings.Contains(err.Error(), s)
46 func TestMACParseString(t *testing.T) {
47 for i, tt := range mactests {
48 out, err := ParseMAC(tt.in)
49 if !reflect.DeepEqual(out, tt.out) || !match(err, tt.err) {
50 t.Errorf("ParseMAC(%q) = %v, %v, want %v, %v", tt.in, out, err, tt.out,
54 // Verify that serialization works too, and that it round-trips.
56 out2, err := ParseMAC(s)
58 t.Errorf("%d. ParseMAC(%q) = %v", i, s, err)
61 if !reflect.DeepEqual(out2, out) {
62 t.Errorf("%d. ParseMAC(%q) = %v, want %v", i, s, out2, out)