Source file src/net/mac_test.go

     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  	// See RFC 7042, Section 2.1.1.
    19  	{"00:00:5e:00:53:01", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
    20  	{"00-00-5e-00-53-01", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
    21  	{"0000.5e00.5301", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
    22  	{"00005e005301", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
    23  
    24  	// See RFC 7042, Section 2.2.2.
    25  	{"02:00:5e:10:00:00:00:01", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
    26  	{"02-00-5e-10-00-00-00-01", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
    27  	{"0200.5e10.0000.0001", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
    28  	{"02005e1000000001", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
    29  
    30  	// See RFC 4391, Section 9.1.1.
    31  	{
    32  		"00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01",
    33  		HardwareAddr{
    34  			0x00, 0x00, 0x00, 0x00,
    35  			0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    36  			0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01,
    37  		},
    38  		"",
    39  	},
    40  	{
    41  		"00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01",
    42  		HardwareAddr{
    43  			0x00, 0x00, 0x00, 0x00,
    44  			0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    45  			0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01,
    46  		},
    47  		"",
    48  	},
    49  	{
    50  		"0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001",
    51  		HardwareAddr{
    52  			0x00, 0x00, 0x00, 0x00,
    53  			0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    54  			0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01,
    55  		},
    56  		"",
    57  	},
    58  	{
    59  		"00000000fe8000000000000002005e1000000001",
    60  		HardwareAddr{
    61  			0x00, 0x00, 0x00, 0x00,
    62  			0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    63  			0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01,
    64  		},
    65  		"",
    66  	},
    67  
    68  	{"ab:cd:ef:AB:CD:EF", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef}, ""},
    69  	{"ab:cd:ef:AB:CD:EF:ab:cd", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd}, ""},
    70  	{
    71  		"ab:cd:ef:AB:CD:EF:ab:cd:ef:AB:CD:EF:ab:cd:ef:AB:CD:EF:ab:cd",
    72  		HardwareAddr{
    73  			0xab, 0xcd, 0xef, 0xab,
    74  			0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef,
    75  			0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd,
    76  		},
    77  		"",
    78  	},
    79  
    80  	{"01.02.03.04.05.06", nil, "invalid MAC address"},
    81  	{"01:02:03:04:05:06:", nil, "invalid MAC address"},
    82  	{"x1:02:03:04:05:06", nil, "invalid MAC address"},
    83  	{"01002:03:04:05:06", nil, "invalid MAC address"},
    84  	{"01:02003:04:05:06", nil, "invalid MAC address"},
    85  	{"01:02:03004:05:06", nil, "invalid MAC address"},
    86  	{"01:02:03:04005:06", nil, "invalid MAC address"},
    87  	{"01:02:03:04:05006", nil, "invalid MAC address"},
    88  	{"01-02:03:04:05:06", nil, "invalid MAC address"},
    89  	{"01:02-03-04-05-06", nil, "invalid MAC address"},
    90  	{"0123:4567:89AF", nil, "invalid MAC address"},
    91  	{"0123-4567-89AF", nil, "invalid MAC address"},
    92  	{"0123456789AF0", nil, "invalid MAC address"},
    93  }
    94  
    95  func TestParseMAC(t *testing.T) {
    96  	match := func(err error, s string) bool {
    97  		if s == "" {
    98  			return err == nil
    99  		}
   100  		return err != nil && strings.Contains(err.Error(), s)
   101  	}
   102  
   103  	for i, tt := range parseMACTests {
   104  		out, err := ParseMAC(tt.in)
   105  		if !reflect.DeepEqual(out, tt.out) || !match(err, tt.err) {
   106  			t.Errorf("ParseMAC(%q) = %v, %v, want %v, %v", tt.in, out, err, tt.out, tt.err)
   107  		}
   108  		if tt.err == "" {
   109  			// Verify that serialization works too, and that it round-trips.
   110  			s := out.String()
   111  			out2, err := ParseMAC(s)
   112  			if err != nil {
   113  				t.Errorf("%d. ParseMAC(%q) = %v", i, s, err)
   114  				continue
   115  			}
   116  			if !reflect.DeepEqual(out2, out) {
   117  				t.Errorf("%d. ParseMAC(%q) = %v, want %v", i, s, out2, out)
   118  			}
   119  		}
   120  	}
   121  }
   122  

View as plain text