oui_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package oui
  2. import "testing"
  3. func TestLookupKnownPrefix(t *testing.T) {
  4. cases := []struct {
  5. mac string
  6. want string
  7. }{
  8. {"00:00:0C:00:00:00", "Cisco Systems, Inc"},
  9. {"00:03:93:00:00:00", "Apple, Inc."},
  10. {"24:0A:C4:00:00:00", "Espressif Inc."},
  11. {"B8:27:EB:00:00:00", "Raspberry Pi Foundation"},
  12. {"00:00:F0:00:00:00", "Samsung Electronics Co.,Ltd"},
  13. }
  14. for _, c := range cases {
  15. if got := Lookup(c.mac); got != c.want {
  16. t.Fatalf("Lookup(%q) = %q, want %q", c.mac, got, c.want)
  17. }
  18. }
  19. }
  20. func TestLookupNormalizesSeparatorsAndCase(t *testing.T) {
  21. if got := Lookup("B8-27-EB-12-34-56"); got != "Raspberry Pi Foundation" {
  22. t.Fatalf("dash: %q", got)
  23. }
  24. if got := Lookup("b827eb123456"); got != "Raspberry Pi Foundation" {
  25. t.Fatalf("bare lowercase: %q", got)
  26. }
  27. if got := Lookup("B827.EB12.3456"); got != "Raspberry Pi Foundation" {
  28. t.Fatalf("dot: %q", got)
  29. }
  30. }
  31. func TestLookupUnknownReturnsEmpty(t *testing.T) {
  32. if got := Lookup("FF:FF:FF:00:00:00"); got != "" {
  33. t.Fatalf("unknown prefix: %q", got)
  34. }
  35. if got := Lookup(""); got != "" {
  36. t.Fatalf("empty mac: %q", got)
  37. }
  38. if got := Lookup("not-a-mac"); got != "" {
  39. t.Fatalf("garbage: %q", got)
  40. }
  41. }
  42. func TestIsIoTVendor(t *testing.T) {
  43. iot := []string{"Espressif Inc.", "Tuya Smart Inc.", "Chengdu Meross Technology Co., Ltd.",
  44. "Shelly Europe LTD", "Lumi United Technology Co., Ltd", "IKEA of Sweden AB"}
  45. for _, m := range iot {
  46. if !IsIoTVendor(m) {
  47. t.Fatalf("IsIoTVendor(%q) = false, want true", m)
  48. }
  49. }
  50. nonIot := []string{"Apple, Inc.", "Cisco Systems, Inc", "Samsung Electronics Co.,Ltd", ""}
  51. for _, m := range nonIot {
  52. if IsIoTVendor(m) {
  53. t.Fatalf("IsIoTVendor(%q) = true, want false", m)
  54. }
  55. }
  56. }
  57. // A short vendor token must not swallow a longer unrelated name: a wrong
  58. // vendor match hands the device a confident, wrong icon.
  59. func TestVendorMatchingDoesNotOverreach(t *testing.T) {
  60. for _, tc := range []struct {
  61. manufacturer string
  62. iot bool
  63. }{
  64. {"Gree Electric Appliances,Inc. of Zhuhai", true},
  65. {"WiZ", true},
  66. {"BSH Hausgeräte GmbH", true},
  67. {"Tuya Smart Inc.", true},
  68. {"Espressif Inc.", true},
  69. // Real vendors that merely contain a short token.
  70. {"Greenwave Systems", false},
  71. {"Greenliant Systems", false},
  72. {"WIZnet Co., Ltd.", false},
  73. {"Illuminati Instrument Corp", false}, // contains "lumi"
  74. {"Intel Corporate", false},
  75. {"", false},
  76. } {
  77. if got := IsIoTVendor(tc.manufacturer); got != tc.iot {
  78. t.Errorf("IsIoTVendor(%q) = %v, want %v", tc.manufacturer, got, tc.iot)
  79. }
  80. }
  81. }
  82. // Surveillance vendors get the camera type, which is more than "iot".
  83. func TestCameraVendors(t *testing.T) {
  84. for _, m := range []string{"Reolink Innovation Limited", "Hangzhou Hikvision", "Dahua Technology", "EZVIZ Inc"} {
  85. if !IsCameraVendor(m) {
  86. t.Errorf("IsCameraVendor(%q) = false", m)
  87. }
  88. }
  89. for _, m := range []string{"Espressif Inc.", "Intel Corporate", ""} {
  90. if IsCameraVendor(m) {
  91. t.Errorf("IsCameraVendor(%q) = true", m)
  92. }
  93. }
  94. }
  95. // Robot vacuum brands get the vacuum type, which is more than "iot".
  96. func TestVacuumVendors(t *testing.T) {
  97. for _, m := range []string{"Beijing Roborock Technology Co., Ltd.", "iRobot Corporation", "Ecovacs Robotics Co., Ltd.", "Dreame Technology"} {
  98. if !IsVacuumVendor(m) {
  99. t.Errorf("IsVacuumVendor(%q) = false", m)
  100. }
  101. }
  102. for _, m := range []string{"Espressif Inc.", "Tuya Smart Inc.", "Reolink Innovation Limited", "Intel Corporate", ""} {
  103. if IsVacuumVendor(m) {
  104. t.Errorf("IsVacuumVendor(%q) = true", m)
  105. }
  106. }
  107. }