| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package l2topology |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/require" |
| 9 | ) |
| 10 | |
| 11 | func TestAdjacencyKey_NormalizesProtocolAndEndpointWhitespace(t *testing.T) { |
| 12 | raw := Adjacency{ |
| 13 | Protocol: " LLDP ", |
| 14 | SourceID: " sw1 ", |
| 15 | SourcePort: " Gi0/1 ", |
| 16 | TargetID: " sw2 ", |
| 17 | TargetPort: " Gi0/2 ", |
| 18 | } |
| 19 | |
| 20 | normalized := Adjacency{ |
| 21 | Protocol: "lldp", |
| 22 | SourceID: "sw1", |
| 23 | SourcePort: "Gi0/1", |
| 24 | TargetID: "sw2", |
| 25 | TargetPort: "Gi0/2", |
| 26 | } |
| 27 | |
| 28 | require.Equal(t, adjacencyKey(normalized), adjacencyKey(raw)) |
| 29 | } |
| 30 | |
| 31 | func TestAttachmentKey_NormalizesIDsAndMethod(t *testing.T) { |
| 32 | raw := Attachment{ |
| 33 | DeviceID: " sw1 ", |
| 34 | IfIndex: 10, |
| 35 | EndpointID: " endpoint-1 ", |
| 36 | Method: " FDB ", |
| 37 | Labels: map[string]string{ |
| 38 | "vlan_id": " 100 ", |
| 39 | }, |
| 40 | } |
| 41 | |
| 42 | normalized := Attachment{ |
| 43 | DeviceID: "sw1", |
| 44 | IfIndex: 10, |
| 45 | EndpointID: "endpoint-1", |
| 46 | Method: "fdb", |
| 47 | Labels: map[string]string{ |
| 48 | "vlan_id": "100", |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | require.Equal(t, attachmentKey(normalized), attachmentKey(raw)) |
| 53 | } |
| 54 | |
| 55 | func TestAdjacencyKey_DistinguishesEmbeddedSeparators(t *testing.T) { |
| 56 | first := Adjacency{ |
| 57 | Protocol: "lldp", |
| 58 | SourceID: "node-a", |
| 59 | SourcePort: "Gi0/1\x00Gi0/2", |
| 60 | TargetID: "node-b", |
| 61 | TargetPort: "Eth1", |
| 62 | } |
| 63 | second := Adjacency{ |
| 64 | Protocol: "lldp", |
| 65 | SourceID: "node-a\x00Gi0/1", |
| 66 | SourcePort: "Gi0/2", |
| 67 | TargetID: "node-b", |
| 68 | TargetPort: "Eth1", |
| 69 | } |
| 70 | |
| 71 | require.NotEqual(t, adjacencyKey(first), adjacencyKey(second)) |
| 72 | } |
| 73 | |
| 74 | func TestIfaceKey_DistinguishesEmbeddedSeparators(t *testing.T) { |
| 75 | first := Interface{ |
| 76 | DeviceID: "sw-a", |
| 77 | IfIndex: 1, |
| 78 | IfName: "2\x00uplink", |
| 79 | } |
| 80 | second := Interface{ |
| 81 | DeviceID: "sw-a\x001", |
| 82 | IfIndex: 2, |
| 83 | IfName: "uplink", |
| 84 | } |
| 85 | |
| 86 | require.NotEqual(t, ifaceKey(first), ifaceKey(second)) |
| 87 | } |