Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 2c03936 | 2024-02-04 18:51:52 -0500 | [diff] [blame^] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package packetHandlers_test |
| 18 | |
| 19 | import ( |
| 20 | "github.com/google/gopacket" |
| 21 | "github.com/google/gopacket/layers" |
| 22 | "github.com/opencord/bbsim/internal/bbsim/packetHandlers" |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 23 | "github.com/opencord/bbsim/internal/bbsim/responders/igmp" |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 24 | "github.com/opencord/bbsim/internal/common" |
| 25 | log "github.com/sirupsen/logrus" |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 26 | "gotest.tools/assert" |
| 27 | "net" |
| 28 | "testing" |
| 29 | ) |
| 30 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 31 | func init() { |
| 32 | common.SetLogLevel(log.StandardLogger(), "error", false) |
| 33 | } |
| 34 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 35 | func Test_IsDhcpPacket_True(t *testing.T) { |
| 36 | dhcp := &layers.DHCPv4{ |
| 37 | Operation: layers.DHCPOpReply, |
| 38 | } |
| 39 | |
| 40 | buffer := gopacket.NewSerializeBuffer() |
| 41 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 42 | err := gopacket.SerializeLayers(buffer, opts, dhcp) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | |
| 47 | dhcpPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeDHCPv4, gopacket.DecodeOptions{}) |
| 48 | |
| 49 | res := packetHandlers.IsDhcpPacket(dhcpPkt) |
| 50 | assert.Equal(t, res, true) |
| 51 | } |
| 52 | |
| 53 | func Test_IsDhcpPacket_False(t *testing.T) { |
| 54 | eth := &layers.Ethernet{ |
| 55 | DstMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x16}, |
| 56 | SrcMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x17}, |
| 57 | } |
| 58 | |
| 59 | buffer := gopacket.NewSerializeBuffer() |
| 60 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 61 | err := gopacket.SerializeLayers(buffer, opts, eth) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | |
| 66 | ethernetPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.DecodeOptions{}) |
| 67 | |
| 68 | res := packetHandlers.IsDhcpPacket(ethernetPkt) |
| 69 | assert.Equal(t, res, false) |
| 70 | } |
| 71 | |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 72 | func Test_IsIgmpPacket(t *testing.T) { |
| 73 | igmp := &igmp.IGMP{ |
| 74 | Type: layers.IGMPMembershipReportV2, //IGMPV2 Membership Report |
| 75 | Checksum: 0, |
| 76 | GroupAddress: net.IPv4(224, 0, 0, 22), |
| 77 | Version: 2, |
| 78 | } |
| 79 | buffer := gopacket.NewSerializeBuffer() |
| 80 | options := gopacket.SerializeOptions{ |
| 81 | ComputeChecksums: true, |
| 82 | FixLengths: true, |
| 83 | } |
| 84 | |
| 85 | if err := gopacket.SerializeLayers(buffer, options, igmp); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | |
| 89 | pkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeIGMP, gopacket.DecodeOptions{}) |
| 90 | |
| 91 | res := packetHandlers.IsIgmpPacket(pkt) |
| 92 | assert.Equal(t, res, true) |
| 93 | } |
| 94 | |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame] | 95 | func Test_IsLldpPacket_True(t *testing.T) { |
| 96 | layer := &layers.LinkLayerDiscovery{ |
| 97 | PortID: layers.LLDPPortID{ |
| 98 | ID: []byte{1, 2, 3, 4}, |
| 99 | Subtype: layers.LLDPPortIDSubtypeMACAddr, |
| 100 | }, |
| 101 | ChassisID: layers.LLDPChassisID{ |
| 102 | ID: []byte{1, 2, 3, 4}, |
| 103 | Subtype: layers.LLDPChassisIDSubTypeMACAddr, |
| 104 | }, |
| 105 | } |
| 106 | |
| 107 | buffer := gopacket.NewSerializeBuffer() |
| 108 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 109 | err := gopacket.SerializeLayers(buffer, opts, layer) |
| 110 | if err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | |
| 114 | packet := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeLinkLayerDiscovery, gopacket.DecodeOptions{}) |
| 115 | |
| 116 | res := packetHandlers.IsLldpPacket(packet) |
| 117 | assert.Equal(t, res, true) |
| 118 | } |
| 119 | |
| 120 | func Test_IsLldpPacket_False(t *testing.T) { |
| 121 | eth := &layers.Ethernet{ |
| 122 | DstMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x16}, |
| 123 | SrcMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x17}, |
| 124 | } |
| 125 | |
| 126 | buffer := gopacket.NewSerializeBuffer() |
| 127 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 128 | err := gopacket.SerializeLayers(buffer, opts, eth) |
| 129 | if err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | |
| 133 | ethernetPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.DecodeOptions{}) |
| 134 | |
| 135 | res := packetHandlers.IsLldpPacket(ethernetPkt) |
| 136 | assert.Equal(t, res, false) |
| 137 | } |
| 138 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 139 | func Test_IsIncomingPacket_True(t *testing.T) { |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 140 | eth := &layers.DHCPv4{ |
| 141 | Operation: layers.DHCPOpReply, |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | buffer := gopacket.NewSerializeBuffer() |
| 145 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 146 | err := gopacket.SerializeLayers(buffer, opts, eth) |
| 147 | if err != nil { |
| 148 | t.Fatal(err) |
| 149 | } |
| 150 | |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 151 | ethernetPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeDHCPv4, gopacket.DecodeOptions{}) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 152 | |
| 153 | res := packetHandlers.IsIncomingPacket(ethernetPkt) |
| 154 | assert.Equal(t, res, true) |
| 155 | } |
| 156 | |
| 157 | func Test_IsIncomingPacket_False(t *testing.T) { |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 158 | eth := &layers.DHCPv4{ |
| 159 | Operation: layers.DHCPOpRequest, |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | buffer := gopacket.NewSerializeBuffer() |
| 163 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 164 | err := gopacket.SerializeLayers(buffer, opts, eth) |
| 165 | if err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 169 | ethernetPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeDHCPv4, gopacket.DecodeOptions{}) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 170 | |
| 171 | res := packetHandlers.IsIncomingPacket(ethernetPkt) |
| 172 | assert.Equal(t, res, false) |
| 173 | } |
| 174 | |
| 175 | func Test_GetDstMacAddressFromPacket(t *testing.T) { |
| 176 | dstMac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x16} |
| 177 | eth := &layers.Ethernet{ |
| 178 | DstMAC: dstMac, |
| 179 | SrcMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x17}, |
| 180 | } |
| 181 | |
| 182 | buffer := gopacket.NewSerializeBuffer() |
| 183 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 184 | err := gopacket.SerializeLayers(buffer, opts, eth) |
| 185 | if err != nil { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | |
| 189 | ethernetPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.DecodeOptions{}) |
| 190 | |
| 191 | res, err := packetHandlers.GetDstMacAddressFromPacket(ethernetPkt) |
| 192 | assert.Equal(t, err, nil) |
| 193 | assert.Equal(t, res.String(), dstMac.String()) |
| 194 | } |