Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 24 | "gotest.tools/assert" |
| 25 | "net" |
| 26 | "testing" |
| 27 | ) |
| 28 | |
| 29 | func Test_IsDhcpPacket_True(t *testing.T) { |
| 30 | dhcp := &layers.DHCPv4{ |
| 31 | Operation: layers.DHCPOpReply, |
| 32 | } |
| 33 | |
| 34 | buffer := gopacket.NewSerializeBuffer() |
| 35 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 36 | err := gopacket.SerializeLayers(buffer, opts, dhcp) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | |
| 41 | dhcpPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeDHCPv4, gopacket.DecodeOptions{}) |
| 42 | |
| 43 | res := packetHandlers.IsDhcpPacket(dhcpPkt) |
| 44 | assert.Equal(t, res, true) |
| 45 | } |
| 46 | |
| 47 | func Test_IsDhcpPacket_False(t *testing.T) { |
| 48 | eth := &layers.Ethernet{ |
| 49 | DstMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x16}, |
| 50 | SrcMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x17}, |
| 51 | } |
| 52 | |
| 53 | buffer := gopacket.NewSerializeBuffer() |
| 54 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 55 | err := gopacket.SerializeLayers(buffer, opts, eth) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | |
| 60 | ethernetPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.DecodeOptions{}) |
| 61 | |
| 62 | res := packetHandlers.IsDhcpPacket(ethernetPkt) |
| 63 | assert.Equal(t, res, false) |
| 64 | } |
| 65 | |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 66 | func Test_IsIgmpPacket(t *testing.T) { |
| 67 | igmp := &igmp.IGMP{ |
| 68 | Type: layers.IGMPMembershipReportV2, //IGMPV2 Membership Report |
| 69 | Checksum: 0, |
| 70 | GroupAddress: net.IPv4(224, 0, 0, 22), |
| 71 | Version: 2, |
| 72 | } |
| 73 | buffer := gopacket.NewSerializeBuffer() |
| 74 | options := gopacket.SerializeOptions{ |
| 75 | ComputeChecksums: true, |
| 76 | FixLengths: true, |
| 77 | } |
| 78 | |
| 79 | if err := gopacket.SerializeLayers(buffer, options, igmp); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | |
| 83 | pkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeIGMP, gopacket.DecodeOptions{}) |
| 84 | |
| 85 | res := packetHandlers.IsIgmpPacket(pkt) |
| 86 | assert.Equal(t, res, true) |
| 87 | } |
| 88 | |
Matteo Scandolo | 73c488d | 2019-11-01 14:44:22 -0700 | [diff] [blame] | 89 | func Test_IsLldpPacket_True(t *testing.T) { |
| 90 | layer := &layers.LinkLayerDiscovery{ |
| 91 | PortID: layers.LLDPPortID{ |
| 92 | ID: []byte{1, 2, 3, 4}, |
| 93 | Subtype: layers.LLDPPortIDSubtypeMACAddr, |
| 94 | }, |
| 95 | ChassisID: layers.LLDPChassisID{ |
| 96 | ID: []byte{1, 2, 3, 4}, |
| 97 | Subtype: layers.LLDPChassisIDSubTypeMACAddr, |
| 98 | }, |
| 99 | } |
| 100 | |
| 101 | buffer := gopacket.NewSerializeBuffer() |
| 102 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 103 | err := gopacket.SerializeLayers(buffer, opts, layer) |
| 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | |
| 108 | packet := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeLinkLayerDiscovery, gopacket.DecodeOptions{}) |
| 109 | |
| 110 | res := packetHandlers.IsLldpPacket(packet) |
| 111 | assert.Equal(t, res, true) |
| 112 | } |
| 113 | |
| 114 | func Test_IsLldpPacket_False(t *testing.T) { |
| 115 | eth := &layers.Ethernet{ |
| 116 | DstMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x16}, |
| 117 | SrcMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x17}, |
| 118 | } |
| 119 | |
| 120 | buffer := gopacket.NewSerializeBuffer() |
| 121 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 122 | err := gopacket.SerializeLayers(buffer, opts, eth) |
| 123 | if err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | |
| 127 | ethernetPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.DecodeOptions{}) |
| 128 | |
| 129 | res := packetHandlers.IsLldpPacket(ethernetPkt) |
| 130 | assert.Equal(t, res, false) |
| 131 | } |
| 132 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 133 | func Test_IsIncomingPacket_True(t *testing.T) { |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame^] | 134 | eth := &layers.DHCPv4{ |
| 135 | Operation: layers.DHCPOpReply, |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | buffer := gopacket.NewSerializeBuffer() |
| 139 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 140 | err := gopacket.SerializeLayers(buffer, opts, eth) |
| 141 | if err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame^] | 145 | ethernetPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeDHCPv4, gopacket.DecodeOptions{}) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 146 | |
| 147 | res := packetHandlers.IsIncomingPacket(ethernetPkt) |
| 148 | assert.Equal(t, res, true) |
| 149 | } |
| 150 | |
| 151 | func Test_IsIncomingPacket_False(t *testing.T) { |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame^] | 152 | eth := &layers.DHCPv4{ |
| 153 | Operation: layers.DHCPOpRequest, |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | buffer := gopacket.NewSerializeBuffer() |
| 157 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 158 | err := gopacket.SerializeLayers(buffer, opts, eth) |
| 159 | if err != nil { |
| 160 | t.Fatal(err) |
| 161 | } |
| 162 | |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame^] | 163 | ethernetPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeDHCPv4, gopacket.DecodeOptions{}) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 164 | |
| 165 | res := packetHandlers.IsIncomingPacket(ethernetPkt) |
| 166 | assert.Equal(t, res, false) |
| 167 | } |
| 168 | |
| 169 | func Test_GetDstMacAddressFromPacket(t *testing.T) { |
| 170 | dstMac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x16} |
| 171 | eth := &layers.Ethernet{ |
| 172 | DstMAC: dstMac, |
| 173 | SrcMAC: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, 0x15, 0x17}, |
| 174 | } |
| 175 | |
| 176 | buffer := gopacket.NewSerializeBuffer() |
| 177 | opts := gopacket.SerializeOptions{FixLengths: true} |
| 178 | err := gopacket.SerializeLayers(buffer, opts, eth) |
| 179 | if err != nil { |
| 180 | t.Fatal(err) |
| 181 | } |
| 182 | |
| 183 | ethernetPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.DecodeOptions{}) |
| 184 | |
| 185 | res, err := packetHandlers.GetDstMacAddressFromPacket(ethernetPkt) |
| 186 | assert.Equal(t, err, nil) |
| 187 | assert.Equal(t, res.String(), dstMac.String()) |
| 188 | } |