| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package l2topology |
| 4 | |
| 5 | import ( |
| 6 | "github.com/stretchr/testify/require" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestBuildTopologyOUIVendorIndex_IgnoresInvalidLinesAndKeepsFirstDuplicate(t *testing.T) { |
| 12 | index := buildTopologyOUIVendorIndex(strings.Join([]string{ |
| 13 | "# comment", |
| 14 | "08EA44\tExtreme Networks Headquarters", |
| 15 | "08EA44\tduplicate should be ignored", |
| 16 | "286FB9\tNokia Shanghai Bell Co., Ltd.", |
| 17 | "08EA4411\tExtreme Specific", |
| 18 | "bad-line-without-tab", |
| 19 | "12345\ttoo short", |
| 20 | "1234567890123\ttoo long", |
| 21 | "GGGGGG\tvalid-length non-hex prefix", |
| 22 | "ABCDEF\t", |
| 23 | "\tNo Prefix", |
| 24 | }, "\n")) |
| 25 | |
| 26 | require.Equal(t, []int{8, 6}, index.prefixLens) |
| 27 | require.Equal(t, "Extreme Networks Headquarters", index.byPrefixLen[6]["08EA44"]) |
| 28 | require.Equal(t, "Nokia Shanghai Bell Co., Ltd.", index.byPrefixLen[6]["286FB9"]) |
| 29 | require.Equal(t, "Extreme Specific", index.byPrefixLen[8]["08EA4411"]) |
| 30 | require.NotContains(t, index.byPrefixLen[6], "GGGGGG") |
| 31 | require.Len(t, index.byPrefixLen[6], 2) |
| 32 | } |
| 33 | |
| 34 | func TestLookupTopologyVendorByMACInIndex_PrefersLongestPrefixAndNormalizesMAC(t *testing.T) { |
| 35 | index := buildTopologyOUIVendorIndex(` |
| 36 | 08EA44 Extreme Networks Headquarters |
| 37 | 08EA4411 Extreme Specific |
| 38 | `) |
| 39 | |
| 40 | vendor, prefix := lookupTopologyVendorByMACInIndex(index, "08ea.4411.2233") |
| 41 | require.Equal(t, "Extreme Specific", vendor) |
| 42 | require.Equal(t, "08EA4411", prefix) |
| 43 | |
| 44 | vendor, prefix = lookupTopologyVendorByMACInIndex(index, "08-ea-44-aa-bb-cc") |
| 45 | require.Equal(t, "Extreme Networks Headquarters", vendor) |
| 46 | require.Equal(t, "08EA44", prefix) |
| 47 | } |
| 48 | |
| 49 | func TestInferTopologyVendorFromMatch_UsesDeterministicCandidateOrder(t *testing.T) { |
| 50 | match := Match{ |
| 51 | MacAddresses: []string{ |
| 52 | "28:6f:b9:00:00:22", |
| 53 | "08:ea:44:11:22:33", |
| 54 | }, |
| 55 | ChassisIDs: []string{ |
| 56 | "28:6f:b9:00:00:22", |
| 57 | "08ea.4411.2233", |
| 58 | }, |
| 59 | } |
| 60 | |
| 61 | vendor, prefix := inferTopologyVendorFromMatch(match) |
| 62 | require.Equal(t, "Extreme Networks Headquarters", vendor) |
| 63 | require.Equal(t, "08EA44", prefix) |
| 64 | } |