github.com/zxy12/golang151_with_comment@v0.0.0-20190507085033-721809559d3c/net/mac_test.go (about) 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 39 func TestParseMAC(t *testing.T) { 40 match := func(err error, s string) bool { 41 if s == "" { 42 return err == nil 43 } 44 return err != nil && strings.Contains(err.Error(), s) 45 } 46 47 for i, tt := range parseMACTests { 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, tt.err) 51 } 52 if tt.err == "" { 53 // Verify that serialization works too, and that it round-trips. 54 s := out.String() 55 out2, err := ParseMAC(s) 56 if err != nil { 57 t.Errorf("%d. ParseMAC(%q) = %v", i, s, err) 58 continue 59 } 60 if !reflect.DeepEqual(out2, out) { 61 t.Errorf("%d. ParseMAC(%q) = %v, want %v", i, s, out2, out) 62 } 63 } 64 } 65 }