github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/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 mactests = []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 match(err error, s string) bool {
    40  	if s == "" {
    41  		return err == nil
    42  	}
    43  	return err != nil && strings.Contains(err.Error(), s)
    44  }
    45  
    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,
    51  				tt.err)
    52  		}
    53  		if tt.err == "" {
    54  			// Verify that serialization works too, and that it round-trips.
    55  			s := out.String()
    56  			out2, err := ParseMAC(s)
    57  			if err != nil {
    58  				t.Errorf("%d. ParseMAC(%q) = %v", i, s, err)
    59  				continue
    60  			}
    61  			if !reflect.DeepEqual(out2, out) {
    62  				t.Errorf("%d. ParseMAC(%q) = %v, want %v", i, s, out2, out)
    63  			}
    64  		}
    65  	}
    66  }