vinokuma | 02fbfd0 | 2023-07-05 15:23:33 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-present Open Networking Foundation |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | package application |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "net" |
| 21 | "sync" |
| 22 | "testing" |
| 23 | cntlr "voltha-go-controller/internal/pkg/controller" |
| 24 | "voltha-go-controller/internal/pkg/of" |
| 25 | "voltha-go-controller/internal/test/mocks" |
| 26 | |
| 27 | "github.com/golang/mock/gomock" |
| 28 | "github.com/google/gopacket" |
| 29 | "github.com/google/gopacket/layers" |
| 30 | "github.com/stretchr/testify/assert" |
| 31 | ) |
| 32 | |
| 33 | var eth = &layers.Ethernet{ |
| 34 | SrcMAC: layers.EthernetBroadcast, |
| 35 | DstMAC: layers.EthernetBroadcast, |
| 36 | EthernetType: layers.EthernetTypeARP, |
| 37 | Length: uint16(1), |
| 38 | } |
| 39 | var dot1Q = &layers.Dot1Q{ |
| 40 | Priority: uint8(1), |
| 41 | DropEligible: true, |
| 42 | Type: layers.EthernetTypeARP, |
| 43 | } |
| 44 | var LayerTypeDot2Q = []gopacket.Layer{ |
| 45 | dot1Q, |
| 46 | } |
| 47 | |
| 48 | func TestPppoeIaPacketTask_Start(t *testing.T) { |
| 49 | type args struct { |
| 50 | ctx context.Context |
| 51 | taskID uint8 |
| 52 | } |
| 53 | tests := []struct { |
| 54 | name string |
| 55 | args args |
| 56 | wantErr bool |
| 57 | }{ |
| 58 | { |
| 59 | name: "PppoeIaPacketTask_Start", |
| 60 | args: args{ |
| 61 | ctx: context.Background(), |
| 62 | taskID: EtherType8100, |
| 63 | }, |
| 64 | }, |
| 65 | } |
| 66 | for _, tt := range tests { |
| 67 | t.Run(tt.name, func(t *testing.T) { |
| 68 | pppoe := &layers.PPPoE{ |
| 69 | Version: uint8(1), |
| 70 | } |
| 71 | pkt := mocks.NewMockPacket(gomock.NewController(t)) |
| 72 | var dpt = &PppoeIaPacketTask{ |
| 73 | pkt: pkt, |
| 74 | } |
| 75 | pkt.EXPECT().Layer(layers.LayerTypePPPoE).Return(pppoe).Times(2) |
| 76 | pkt.EXPECT().Layer(layers.LayerTypeEthernet).Return(eth).Times(1) |
| 77 | pkt.EXPECT().Layer(layers.LayerTypeDot1Q).Return(dot1Q).Times(1) |
| 78 | pkt.EXPECT().Layers().Return(LayerTypeDot2Q).Times(1) |
| 79 | err := dpt.Start(tt.args.ctx, tt.args.taskID) |
| 80 | assert.Nil(t, err) |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestVoltApplication_ProcessPPPoEIaPacket(t *testing.T) { |
| 86 | type args struct { |
| 87 | cntx context.Context |
| 88 | device string |
| 89 | port string |
| 90 | pkt gopacket.Packet |
| 91 | } |
| 92 | pkt := mocks.NewMockPacket(gomock.NewController(t)) |
| 93 | tests := []struct { |
| 94 | name string |
| 95 | args args |
| 96 | }{ |
| 97 | { |
| 98 | name: "ProcessPPPoEIaPacket_ProcessUsPppoeIaPacket", |
| 99 | args: args{ |
| 100 | cntx: context.Background(), |
| 101 | device: test_device, |
| 102 | port: "test_port", |
| 103 | pkt: pkt, |
| 104 | }, |
| 105 | }, |
| 106 | { |
| 107 | name: "pppoel_nil", |
| 108 | args: args{ |
| 109 | cntx: context.Background(), |
| 110 | device: test_device, |
| 111 | port: "test_port", |
| 112 | pkt: pkt, |
| 113 | }, |
| 114 | }, |
| 115 | { |
| 116 | name: "pppoel_invalidType", |
| 117 | args: args{ |
| 118 | cntx: context.Background(), |
| 119 | device: test_device, |
| 120 | port: "test_port", |
| 121 | pkt: pkt, |
| 122 | }, |
| 123 | }, |
| 124 | } |
| 125 | for _, tt := range tests { |
| 126 | t.Run(tt.name, func(t *testing.T) { |
| 127 | va := &VoltApplication{} |
| 128 | // dot1Q := &layers.Dot1Q{ |
| 129 | // Priority: uint8(1), |
| 130 | // DropEligible: true, |
| 131 | // Type: layers.EthernetTypeARP, |
| 132 | // } |
| 133 | // LayerTypeDot2Q := []gopacket.Layer{ |
| 134 | // dot1Q, |
| 135 | // } |
| 136 | ga := GetApplication() |
| 137 | switch tt.name { |
| 138 | case "ProcessPPPoEIaPacket_ProcessUsPppoeIaPacket": |
| 139 | ga.DevicesDisc.Store(test_device, voltDevice) |
| 140 | pkt.EXPECT().Layer(layers.LayerTypePPPoE).Return(&layers.PPPoE{ |
| 141 | Version: uint8(1), |
| 142 | }).Times(1) |
| 143 | pkt.EXPECT().Layers().Return(LayerTypeDot2Q).Times(2) |
| 144 | va.ProcessPPPoEIaPacket(tt.args.cntx, tt.args.device, tt.args.port, tt.args.pkt) |
| 145 | case "pppoel_nil": |
| 146 | pkt.EXPECT().Layer(layers.LayerTypePPPoE).Return(nil).Times(1) |
| 147 | va.ProcessPPPoEIaPacket(tt.args.cntx, tt.args.device, tt.args.port, tt.args.pkt) |
| 148 | case "pppoel_invalidType": |
| 149 | pkt.EXPECT().Layer(layers.LayerTypePPPoE).Return(&layers.ARP{}).Times(1) |
| 150 | va.ProcessPPPoEIaPacket(tt.args.cntx, tt.args.device, tt.args.port, tt.args.pkt) |
| 151 | } |
| 152 | }) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func TestVoltApplication_ProcessUsPppoeIaPacket(t *testing.T) { |
| 157 | type args struct { |
| 158 | cntx context.Context |
| 159 | device string |
| 160 | port string |
| 161 | pkt gopacket.Packet |
| 162 | } |
| 163 | pkt := mocks.NewMockPacket(gomock.NewController(t)) |
| 164 | tests := []struct { |
| 165 | name string |
| 166 | args args |
| 167 | }{ |
| 168 | { |
| 169 | name: "VoltApplication_ProcessUsPppoeIaPacket", |
| 170 | args: args{ |
| 171 | cntx: context.Background(), |
| 172 | device: test_device, |
| 173 | port: "test_port", |
| 174 | pkt: pkt, |
| 175 | }, |
| 176 | }, |
| 177 | } |
| 178 | macAdd, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff") |
| 179 | macPort := map[string]string{} |
| 180 | macPort[macAdd.String()] = test_data |
| 181 | for _, tt := range tests { |
| 182 | t.Run(tt.name, func(t *testing.T) { |
| 183 | va := &VoltApplication{ |
| 184 | macPortMap: macPort, |
| 185 | VnetsByPort: sync.Map{}, |
| 186 | DevicesDisc: sync.Map{}, |
| 187 | } |
| 188 | voltServ := &VoltService{ |
| 189 | VoltServiceOper: VoltServiceOper{ |
| 190 | Device: "SDX6320031", |
| 191 | }, |
| 192 | VoltServiceCfg: VoltServiceCfg{ |
| 193 | IsActivated: true, |
| 194 | Pbits: []of.PbitType{PbitMatchAll}, |
| 195 | }, |
| 196 | } |
| 197 | switch tt.name { |
| 198 | case "VoltApplication_ProcessUsPppoeIaPacket": |
| 199 | va.DevicesDisc.Store(test_device, voltDevice) |
| 200 | pkt.EXPECT().Layers().Return(LayerTypeDot2Q).Times(3) |
| 201 | voltPortVnet1[0].SVlan = 0 |
| 202 | voltDevice.NniPort = "1" |
| 203 | va.VnetsByPort.Store("test_port", voltPortVnet1) |
| 204 | voltPortVnet1[0].PppoeIa = true |
| 205 | voltPortVnet1[0].AllowTransparent = true |
| 206 | voltPortVnet1[0].Port = test_data |
| 207 | pendingDeleteFlow := map[string]bool{} |
| 208 | pendingDeleteFlow["test_cookie"] = true |
| 209 | voltPortVnet1[0].PendingDeleteFlow = pendingDeleteFlow |
| 210 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 211 | db = dbintf |
| 212 | eth.SrcMAC = layers.EthernetBroadcast |
| 213 | dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) |
| 214 | voltPortVnet1[0].services.Store("SDX6320031-1_SDX6320031-1-4096-2310-4096-65", voltServ) |
| 215 | pkt.EXPECT().Layer(layers.LayerTypeEthernet).Return(eth).Times(2) |
| 216 | pkt.EXPECT().Layer(layers.LayerTypePPPoE).Return(&layers.PPPoE{ |
| 217 | Version: uint8(1), |
| 218 | Code: layers.PPPoECodePADI, |
| 219 | }).Times(1) |
| 220 | pkt.EXPECT().Layer(layers.LayerTypeDot1Q).Return(dot1Q).Times(1) |
| 221 | _ = cntlr.NewController(context.Background(), mocks.NewMockApp(gomock.NewController(t))) |
| 222 | va.ProcessUsPppoeIaPacket(tt.args.cntx, tt.args.device, tt.args.port, tt.args.pkt) |
| 223 | } |
| 224 | }) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | func TestVoltApplication_ProcessDsPppoeIaPacket(t *testing.T) { |
| 229 | type args struct { |
| 230 | cntx context.Context |
| 231 | device string |
| 232 | port string |
| 233 | pkt gopacket.Packet |
| 234 | } |
| 235 | pkt := mocks.NewMockPacket(gomock.NewController(t)) |
| 236 | tests := []struct { |
| 237 | name string |
| 238 | args args |
| 239 | }{ |
| 240 | { |
| 241 | name: "VoltApplication_ProcessDsPppoeIaPacket", |
| 242 | args: args{ |
| 243 | cntx: context.Background(), |
| 244 | device: test_device, |
| 245 | port: "test_port", |
| 246 | pkt: pkt, |
| 247 | }, |
| 248 | }, |
| 249 | } |
| 250 | for _, tt := range tests { |
| 251 | t.Run(tt.name, func(t *testing.T) { |
| 252 | va := &VoltApplication{} |
| 253 | pkt.EXPECT().Layer(layers.LayerTypeEthernet).Return(eth).Times(1) |
| 254 | pkt.EXPECT().Layer(layers.LayerTypePPPoE).Return(&layers.PPPoE{ |
| 255 | Version: uint8(1), |
| 256 | Code: layers.PPPoECodePADI, |
| 257 | }).Times(1) |
| 258 | pkt.EXPECT().Layer(layers.LayerTypeDot1Q).Return(dot1Q).Times(1) |
| 259 | pkt.EXPECT().Layers().Return(LayerTypeDot2Q).Times(3) |
| 260 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 261 | db = dbintf |
| 262 | dbintf.EXPECT().PutVpv(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) |
| 263 | va.ProcessDsPppoeIaPacket(tt.args.cntx, tt.args.device, tt.args.port, tt.args.pkt) |
| 264 | }) |
| 265 | } |
| 266 | } |
vinokuma | 04dc9f8 | 2023-07-31 15:47:49 +0530 | [diff] [blame] | 267 | |
| 268 | func TestAddIaOption(t *testing.T) { |
| 269 | type args struct { |
| 270 | svc *VoltService |
| 271 | pppoe *layers.PPPoE |
| 272 | } |
| 273 | tests := []struct { |
| 274 | name string |
| 275 | args args |
| 276 | }{ |
| 277 | { |
| 278 | name: "AddIaOption", |
| 279 | args: args{ |
| 280 | svc: &VoltService{ |
| 281 | VoltServiceCfg: VoltServiceCfg{ |
| 282 | CircuitID: "test_circuit_id", |
| 283 | RemoteID: []byte{1}, |
| 284 | DataRateAttr: DSLAttrEnabled, |
| 285 | }, |
| 286 | }, |
| 287 | pppoe: &layers.PPPoE{ |
| 288 | Options: make(layers.PPPoEOptions, 1), |
| 289 | }, |
| 290 | }, |
| 291 | }, |
| 292 | } |
| 293 | for _, tt := range tests { |
| 294 | t.Run(tt.name, func(t *testing.T) { |
| 295 | AddIaOption(tt.args.svc, tt.args.pppoe) |
| 296 | }) |
| 297 | } |
| 298 | } |
Akash Soni | 230e621 | 2023-10-16 10:46:07 +0530 | [diff] [blame] | 299 | |
| 300 | func TestNewPppoeIaPacketTask(t *testing.T) { |
| 301 | type args struct { |
| 302 | pkt gopacket.Packet |
| 303 | dev string |
| 304 | port string |
| 305 | } |
| 306 | pkt := mocks.NewMockPacket(gomock.NewController(t)) |
| 307 | tests := []struct { |
| 308 | name string |
| 309 | args args |
| 310 | want *PppoeIaPacketTask |
| 311 | }{ |
| 312 | { |
| 313 | name: "NewPppoeIaPacketTask", |
| 314 | args: args{ |
| 315 | pkt: pkt, |
| 316 | dev: test_device, |
| 317 | port: "test_port", |
| 318 | }, |
| 319 | }, |
| 320 | } |
| 321 | for _, tt := range tests { |
| 322 | t.Run(tt.name, func(t *testing.T) { |
| 323 | got := NewPppoeIaPacketTask(tt.args.pkt, tt.args.dev, tt.args.port) |
| 324 | assert.NotNil(t, got) |
| 325 | }) |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | func TestPppoeIaPacketTask_Name(t *testing.T) { |
| 330 | dpt := &PppoeIaPacketTask{} |
| 331 | got := dpt.Name() |
| 332 | assert.NotNil(t, got) |
| 333 | got1 := dpt.TaskID() |
| 334 | assert.NotNil(t, got1) |
| 335 | got2 := dpt.Timestamp() |
| 336 | assert.NotNil(t, got2) |
| 337 | } |