4
0

macaddr_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package macaddr
  2. import "testing"
  3. func TestNormalize(t *testing.T) {
  4. const want = "AA:BB:CC:DD:EE:FF"
  5. tests := []struct {
  6. name string
  7. in string
  8. want string
  9. }{
  10. {"dos puntos en minúsculas", "aa:bb:cc:dd:ee:ff", want},
  11. {"dos puntos en mayúsculas", "AA:BB:CC:DD:EE:FF", want},
  12. {"con guiones", "aa-bb-cc-dd-ee-ff", want},
  13. {"12 hex sin separador", "aabbccddeeff", want},
  14. {"espacios alrededor", " aa:bb:cc:dd:ee:ff\t\n", want},
  15. {"separadores mezclados", "aa:bb-cc:dd-ee:ff", ""},
  16. {"separador incorrecto", "aa.bb.cc.dd.ee.ff", ""},
  17. {"no hex", "gg:bb:cc:dd:ee:ff", ""},
  18. {"no hex sin separador", "aabbccddeefg", ""},
  19. {"demasiado corta", "aa:bb:cc:dd:ee", ""},
  20. {"demasiado larga", "aa:bb:cc:dd:ee:ff:00", ""},
  21. {"vacía", "", ""},
  22. {"nula aceptada", "00:00:00:00:00:00", "00:00:00:00:00:00"},
  23. }
  24. for _, tc := range tests {
  25. t.Run(tc.name, func(t *testing.T) {
  26. if got := Normalize(tc.in); got != tc.want {
  27. t.Errorf("Normalize(%q) = %q, esperaba %q", tc.in, got, tc.want)
  28. }
  29. })
  30. }
  31. }
  32. func TestIsLocallyAdministered(t *testing.T) {
  33. tests := []struct {
  34. name string
  35. in string
  36. want bool
  37. }{
  38. {"02 administrada localmente", "02:11:22:33:44:55", true},
  39. {"06 administrada localmente", "06:11:22:33:44:55", true},
  40. {"0A administrada localmente", "0A:11:22:33:44:55", true},
  41. {"0E administrada localmente", "0E:11:22:33:44:55", true},
  42. {"F2 administrada localmente", "F2:11:22:33:44:55", true},
  43. {"00 universal", "00:11:22:33:44:55", false},
  44. {"04 universal", "04:11:22:33:44:55", false},
  45. {"AC universal", "AC:11:22:33:44:55", false},
  46. {"minúsculas y guiones local", "0a-bb-cc-dd-ee-ff", true},
  47. {"minúsculas y guiones universal", "ac-bb-cc-dd-ee-ff", false},
  48. {"no es MAC", "02:11:22", false},
  49. {"vacía", "", false},
  50. }
  51. for _, tc := range tests {
  52. t.Run(tc.name, func(t *testing.T) {
  53. if got := IsLocallyAdministered(tc.in); got != tc.want {
  54. t.Errorf("IsLocallyAdministered(%q) = %v, esperaba %v", tc.in, got, tc.want)
  55. }
  56. })
  57. }
  58. }
  59. func TestNormalizeLenient(t *testing.T) {
  60. const want = "AA:BB:CC:DD:EE:FF"
  61. tests := []struct {
  62. name string
  63. in string
  64. want string
  65. }{
  66. {"dos puntos en minúsculas", "aa:bb:cc:dd:ee:ff", want},
  67. {"dos puntos en mayúsculas", "AA:BB:CC:DD:EE:FF", want},
  68. {"con guiones", "aa-bb-cc-dd-ee-ff", want},
  69. {"12 hex sin separador", "aabbccddeeff", want},
  70. {"espacios alrededor", " aa:bb:cc:dd:ee:ff\t\n", want},
  71. {"cadena que no es MAC", " ab-cd ", "AB:CD"},
  72. {"vacía", "", ""},
  73. }
  74. for _, tc := range tests {
  75. t.Run(tc.name, func(t *testing.T) {
  76. if got := NormalizeLenient(tc.in); got != tc.want {
  77. t.Errorf("NormalizeLenient(%q) = %q, esperaba %q", tc.in, got, tc.want)
  78. }
  79. })
  80. }
  81. }
  82. func TestNormalizeSeparated(t *testing.T) {
  83. const want = "AA:BB:CC:DD:EE:FF"
  84. tests := []struct {
  85. name string
  86. in string
  87. want string
  88. }{
  89. {"dos puntos", "aa:bb:cc:dd:ee:ff", want},
  90. {"guiones", "AA-BB-CC-DD-EE-FF", want},
  91. {"espacios alrededor", " aa:bb:cc:dd:ee:ff ", want},
  92. {"12 hex sin separador rechazado", "aabbccddeeff", ""},
  93. {"separadores mezclados", "aa:bb-cc:dd-ee:ff", ""},
  94. {"no hex", "gg:bb:cc:dd:ee:ff", ""},
  95. {"vacía", "", ""},
  96. }
  97. for _, tc := range tests {
  98. t.Run(tc.name, func(t *testing.T) {
  99. if got := NormalizeSeparated(tc.in); got != tc.want {
  100. t.Errorf("NormalizeSeparated(%q) = %q, esperaba %q", tc.in, got, tc.want)
  101. }
  102. })
  103. }
  104. }