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 parseMACTests
= []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}, ""},
38 "01:23:45:67:89:ab:cd:ef:00:00:01:23:45:67:89:ab:cd:ef:00:00",
40 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00,
41 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00,
46 "01-23-45-67-89-ab-cd-ef-00-00-01-23-45-67-89-ab-cd-ef-00-00",
48 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00,
49 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00,
54 "0123.4567.89ab.cdef.0000.0123.4567.89ab.cdef.0000",
56 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00,
57 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00,
63 func TestParseMAC(t
*testing
.T
) {
64 match
:= func(err error
, s
string) bool {
68 return err
!= nil && strings
.Contains(err
.Error(), s
)
71 for i
, tt
:= range parseMACTests
{
72 out
, err
:= ParseMAC(tt
.in
)
73 if !reflect
.DeepEqual(out
, tt
.out
) ||
!match(err
, tt
.err
) {
74 t
.Errorf("ParseMAC(%q) = %v, %v, want %v, %v", tt
.in
, out
, err
, tt
.out
, tt
.err
)
77 // Verify that serialization works too, and that it round-trips.
79 out2
, err
:= ParseMAC(s
)
81 t
.Errorf("%d. ParseMAC(%q) = %v", i
, s
, err
)
84 if !reflect
.DeepEqual(out2
, out
) {
85 t
.Errorf("%d. ParseMAC(%q) = %v, want %v", i
, s
, out2
, out
)