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. 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 {"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}, ""}, 37 { 38 "01:23:45:67:89:ab:cd:ef:00:00:01:23:45:67:89:ab:cd:ef:00:00", 39 HardwareAddr{ 40 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00, 41 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00, 42 }, 43 "", 44 }, 45 { 46 "01-23-45-67-89-ab-cd-ef-00-00-01-23-45-67-89-ab-cd-ef-00-00", 47 HardwareAddr{ 48 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00, 49 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00, 50 }, 51 "", 52 }, 53 { 54 "0123.4567.89ab.cdef.0000.0123.4567.89ab.cdef.0000", 55 HardwareAddr{ 56 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00, 57 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00, 58 }, 59 "", 60 }, 61 } 62 63 func TestParseMAC(t *testing.T) { 64 match := func(err error, s string) bool { 65 if s == "" { 66 return err == nil 67 } 68 return err != nil && strings.Contains(err.Error(), s) 69 } 70 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) 75 } 76 if tt.err == "" { 77 // Verify that serialization works too, and that it round-trips. 78 s := out.String() 79 out2, err := ParseMAC(s) 80 if err != nil { 81 t.Errorf("%d. ParseMAC(%q) = %v", i, s, err) 82 continue 83 } 84 if !reflect.DeepEqual(out2, out) { 85 t.Errorf("%d. ParseMAC(%q) = %v, want %v", i, s, out2, out) 86 } 87 } 88 } 89 } 90