mac_test.go 3.3 KB

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