blob: 58d7bdc005342f6bd65de3a3d92a151a54035434 [file] [log] [blame]
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -07001/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -07003
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
17package packetHandlers_test
18
19import (
20 "github.com/google/gopacket"
21 "github.com/google/gopacket/layers"
22 "github.com/opencord/bbsim/internal/bbsim/packetHandlers"
Matteo Scandolo618a6582020-09-09 12:21:29 -070023 "github.com/opencord/bbsim/internal/bbsim/responders/igmp"
Matteo Scandolo8a574812021-05-20 15:18:53 -070024 "github.com/opencord/bbsim/internal/common"
25 log "github.com/sirupsen/logrus"
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070026 "gotest.tools/assert"
27 "net"
28 "testing"
29)
30
Matteo Scandolo8a574812021-05-20 15:18:53 -070031func init() {
32 common.SetLogLevel(log.StandardLogger(), "error", false)
33}
34
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070035func 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
53func 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 Scandolo618a6582020-09-09 12:21:29 -070072func 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 Scandolo73c488d2019-11-01 14:44:22 -070095func 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
120func 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 Scandolof6f3a7f2019-10-11 11:19:29 -0700139func Test_IsIncomingPacket_True(t *testing.T) {
Matteo Scandolo90d08f62020-10-29 12:06:55 -0700140 eth := &layers.DHCPv4{
141 Operation: layers.DHCPOpReply,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700142 }
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 Scandolo90d08f62020-10-29 12:06:55 -0700151 ethernetPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeDHCPv4, gopacket.DecodeOptions{})
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700152
153 res := packetHandlers.IsIncomingPacket(ethernetPkt)
154 assert.Equal(t, res, true)
155}
156
157func Test_IsIncomingPacket_False(t *testing.T) {
Matteo Scandolo90d08f62020-10-29 12:06:55 -0700158 eth := &layers.DHCPv4{
159 Operation: layers.DHCPOpRequest,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700160 }
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 Scandolo90d08f62020-10-29 12:06:55 -0700169 ethernetPkt := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeDHCPv4, gopacket.DecodeOptions{})
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700170
171 res := packetHandlers.IsIncomingPacket(ethernetPkt)
172 assert.Equal(t, res, false)
173}
174
175func 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}