blob: 2354d4166126e6d2d087d2182bb72ae4aa7df3c1 [file] [log] [blame]
vinokuma04dc9f82023-07-31 15:47:49 +05301/*
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
16package application
17
18import (
19 "context"
20 "encoding/json"
21 "net"
22 "reflect"
23 "sync"
24 "testing"
25 "voltha-go-controller/internal/pkg/of"
26 "voltha-go-controller/internal/test/mocks"
27
28 "github.com/golang/mock/gomock"
29 "github.com/google/gopacket"
30 "github.com/google/gopacket/layers"
31 "github.com/stretchr/testify/assert"
32)
33
34func TestVoltApplication_GetIgnoredPorts(t *testing.T) {
35 voltDevice := &VoltDevice{
36 Name: "11c3175b-50f3-4220-9555-93df733ded1d",
37 SerialNum: "SDX6320031",
38 SouthBoundID: "68580342-6b3e-57cb-9ea4-06125594e330",
39 NniPort: "16777472",
40 Ports: sync.Map{},
41 PonPortList: sync.Map{},
42 }
43 voltPort := &VoltPort{
44 Name: "16777472",
45 Device: "SDX6320031",
46 ID: 16777472,
47 State: PortStateDown,
48 ChannelPerSubAlarmRaised: false,
49 Type: VoltPortTypeNni,
50 }
51 voltPortVnets := make([]*VoltPortVnet, 0)
52 voltPortVnet := &VoltPortVnet{
53 Device: "SDX6320031",
54 Port: "16777472",
55 MacLearning: MacLearningNone,
56 }
57 voltPortVnets = append(voltPortVnets, voltPortVnet)
58 IgnoredPorts := make(map[string][]string)
59 IgnoredPorts["SDX6320031"] = append(IgnoredPorts["SDX6320031"], "16777472")
60 tests := []struct {
61 name string
62 want map[string][]string
63 wantErr bool
64 }{
65 {
66 name: "Positive_Case_GetIgnoredPorts",
67 want: IgnoredPorts,
68 wantErr: false,
69 },
70 }
71 for _, tt := range tests {
72 t.Run(tt.name, func(t *testing.T) {
73 va := &VoltApplication{
74 DevicesDisc: sync.Map{},
75 }
76 va.DevicesDisc.Store("SDX6320031", voltDevice)
77 voltDevice.Ports.Store("16777472", voltPort)
78 voltApp := GetApplication()
79 voltApp.VnetsByPort.Store("16777472", voltPortVnets)
80 got, err := va.GetIgnoredPorts()
81 if (err != nil) != tt.wantErr {
82 t.Errorf("VoltApplication.GetIgnoredPorts() error = %v, wantErr %v", err, tt.wantErr)
83 return
84 }
85 if !reflect.DeepEqual(got, tt.want) {
86 t.Errorf("VoltApplication.GetIgnoredPorts() = %v, want %v", got, tt.want)
87 }
88 })
89 }
90}
91
92func TestDhcpNetworks_AddDhcpSession(t *testing.T) {
93 pkt := mocks.NewMockPacket(gomock.NewController(t))
94 type args struct {
95 pkt gopacket.Packet
96 session IDhcpRelaySession
97 }
98 tests := []struct {
99 name string
100 args args
101 wantErr bool
102 }{
103 {
104 name: "DhcpNetworks_AddDhcpSession",
105 args: args{
106 pkt: pkt,
107 session: &VoltPortVnet{},
108 },
109 },
110 }
111 for _, tt := range tests {
112 t.Run(tt.name, func(t *testing.T) {
113 network := make(map[uint32]*DhcpRelayVnet)
114 dn := &DhcpNetworks{
115 Networks: network,
116 }
117 pkt.EXPECT().Layer(layers.LayerTypeEthernet).Return(eth).Times(1)
118 if err := dn.AddDhcpSession(tt.args.pkt, tt.args.session); (err != nil) != tt.wantErr {
119 t.Errorf("DhcpNetworks.AddDhcpSession() error = %v, wantErr %v", err, tt.wantErr)
120 }
121 })
122 }
123}
124
125func TestDhcpNetworks_DelDhcpSession(t *testing.T) {
126 pkt := mocks.NewMockPacket(gomock.NewController(t))
127 type args struct {
128 pkt gopacket.Packet
129 session IDhcpRelaySession
130 }
131 tests := []struct {
132 name string
133 args args
134 }{
135 {
136 name: "DhcpNetworks_DelDhcpSession",
137 args: args{
138 pkt: pkt,
139 session: &VoltPortVnet{},
140 },
141 },
142 }
143 for _, tt := range tests {
144 t.Run(tt.name, func(t *testing.T) {
145 network := make(map[uint32]*DhcpRelayVnet)
146 dn := &DhcpNetworks{
147 Networks: network,
148 }
149 pkt.EXPECT().Layer(layers.LayerTypeEthernet).Return(eth).Times(1)
150 dn.DelDhcpSession(tt.args.pkt, tt.args.session)
151 })
152 }
153}
154
155func TestDhcpNetworks_AddDhcp6Session(t *testing.T) {
156 type args struct {
157 key [MaxLenDhcpv6DUID]byte
158 session IDhcpRelaySession
159 }
160 tests := []struct {
161 name string
162 args args
163 wantErr bool
164 }{
165 {
166 name: "DhcpNetworks_AddDhcp6Session",
167 args: args{
168 session: &VoltPortVnet{},
169 },
170 },
171 }
172 for _, tt := range tests {
173 t.Run(tt.name, func(t *testing.T) {
174 network := make(map[uint32]*DhcpRelayVnet)
175 dn := &DhcpNetworks{
176 Networks: network,
177 }
178 if err := dn.AddDhcp6Session(tt.args.key, tt.args.session); (err != nil) != tt.wantErr {
179 t.Errorf("DhcpNetworks.AddDhcp6Session() error = %v, wantErr %v", err, tt.wantErr)
180 }
181 })
182 }
183}
184
185func TestDhcpNetworks_DelDhcp6Session(t *testing.T) {
186 type args struct {
187 key [MaxLenDhcpv6DUID]byte
188 session IDhcpRelaySession
189 }
190 tests := []struct {
191 name string
192 args args
193 }{
194 {
195 name: "DhcpNetworks_DelDhcp6Session",
196 args: args{
197 session: &VoltPortVnet{},
198 },
199 },
200 }
201 for _, tt := range tests {
202 t.Run(tt.name, func(t *testing.T) {
203 network := make(map[uint32]*DhcpRelayVnet)
204 network[uint32(4097)] = &DhcpRelayVnet{
205 InnerVlan: uint16(4097),
206 }
207 dn := &DhcpNetworks{
208 Networks: network,
209 }
210 dn.DelDhcp6Session(tt.args.key, tt.args.session)
211 })
212 }
213}
214
215func TestDhcpNetworks_GetDhcpSession(t *testing.T) {
216 type fields struct {
217 Networks map[uint32]*DhcpRelayVnet
218 }
219 type args struct {
220 outerVlan uint16
221 innerVlan uint16
222 addr net.HardwareAddr
223 }
224 macAdd, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff")
225 tests := []struct {
226 name string
227 fields fields
228 args args
229 want IDhcpRelaySession
230 }{
231 {
232 name: "DhcpNetworks_GetDhcpSession",
233 args: args{
234 outerVlan: uint16(0),
235 innerVlan: uint16(4097),
236 addr: macAdd,
237 },
238 },
239 }
240 for _, tt := range tests {
241 t.Run(tt.name, func(t *testing.T) {
242 network := make(map[uint32]*DhcpRelayVnet)
243 network[uint32(4097)] = &DhcpRelayVnet{
244 InnerVlan: uint16(4097),
245 }
246 dn := &DhcpNetworks{
247 Networks: network,
248 }
249 got, err := dn.GetDhcpSession(tt.args.outerVlan, tt.args.innerVlan, tt.args.addr)
250 assert.NotNil(t, err)
251 assert.Nil(t, got)
252 })
253 }
254}
255
256func TestDhcpNetworks_GetDhcp6Session(t *testing.T) {
257 type fields struct {
258 Networks map[uint32]*DhcpRelayVnet
259 }
260 type args struct {
261 outerVlan uint16
262 innerVlan uint16
263 key [MaxLenDhcpv6DUID]byte
264 }
265 tests := []struct {
266 name string
267 fields fields
268 args args
269 want IDhcpRelaySession
270 wantErr bool
271 }{
272 {
273 name: "DhcpNetworks_GetDhcp6Session",
274 args: args{
275 outerVlan: uint16(0),
276 innerVlan: uint16(4097),
277 },
278 },
279 }
280 for _, tt := range tests {
281 t.Run(tt.name, func(t *testing.T) {
282 network := make(map[uint32]*DhcpRelayVnet)
283 network[uint32(4097)] = &DhcpRelayVnet{
284 InnerVlan: uint16(4097),
285 }
286 dn := &DhcpNetworks{
287 Networks: network,
288 }
289 got, err := dn.GetDhcp6Session(tt.args.outerVlan, tt.args.innerVlan, tt.args.key)
290 assert.NotNil(t, err)
291 assert.Nil(t, got)
292 })
293 }
294}
295
296func TestGetVnetForV4Nni(t *testing.T) {
297 type args struct {
298 dhcp *layers.DHCPv4
299 cvlan of.VlanType
300 svlan of.VlanType
301 pbit uint8
302 }
303 macAdd, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff")
304 tests := []struct {
305 name string
306 args args
307 want []*VoltPortVnet
308 wantErr bool
309 }{
310 {
311 name: "GetVnetForV4Nni",
312 args: args{
313 cvlan: of.VlanAny,
314 svlan: of.VlanAny,
315 dhcp: &layers.DHCPv4{
316 BaseLayer: dot1Q.BaseLayer,
317 ClientHWAddr: macAdd,
318 },
319 },
320 },
321 }
322 for _, tt := range tests {
323 t.Run(tt.name, func(t *testing.T) {
324 got, err := GetVnetForV4Nni(tt.args.dhcp, tt.args.cvlan, tt.args.svlan, tt.args.pbit)
325 assert.NotNil(t, err)
326 assert.Nil(t, got)
327 })
328 }
329}
330
331func TestGetVnetForV6Nni(t *testing.T) {
332 type args struct {
333 dhcp *layers.DHCPv6
334 cvlan of.VlanType
335 svlan of.VlanType
336 pbit uint8
337 clientMAC net.HardwareAddr
338 }
339 macAdd, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff")
340 tests := []struct {
341 name string
342 args args
343 want []*VoltPortVnet
344 want1 net.HardwareAddr
345 wantErr bool
346 }{
347 {
348 name: "GetVnetForV6Nni",
349 args: args{
350 dhcp: &layers.DHCPv6{
351 BaseLayer: dot1Q.BaseLayer,
352 Options: layers.DHCPv6Options{
353 {
354 Code: layers.DHCPv6OptClientID,
355 Data: []byte{2, 3, 4, 2, 3, 4, 2, 3, 4},
356 },
357 },
358 },
359 cvlan: of.VlanAny,
360 svlan: of.VlanAny,
361 clientMAC: macAdd,
362 },
363 },
364 }
365 for _, tt := range tests {
366 t.Run(tt.name, func(t *testing.T) {
367 got, got1, err := GetVnetForV6Nni(tt.args.dhcp, tt.args.cvlan, tt.args.svlan, tt.args.pbit, tt.args.clientMAC)
368 assert.NotNil(t, err)
369 assert.Nil(t, got)
370 assert.NotNil(t, got1)
371 })
372 }
373}
374
375func TestAddDhcpv4Option82(t *testing.T) {
376 type args struct {
377 svc *VoltService
378 rID []byte
379 dhcpv4 *layers.DHCPv4
380 }
381 tests := []struct {
382 name string
383 args args
384 }{
385 {
386 name: "AddDhcpv4Option82",
387 args: args{
388 svc: &VoltService{
389 VoltServiceCfg: VoltServiceCfg{
390 CircuitID: "test_circuit_id",
391 DataRateAttr: DSLAttrEnabled,
392 },
393 },
394 rID: []byte{1},
395 dhcpv4: &layers.DHCPv4{
396 Options: layers.DHCPOptions{
397 {
398 Type: layers.DHCPOptARPTimeout,
399 },
400 },
401 },
402 },
403 },
404 }
405 for _, tt := range tests {
406 t.Run(tt.name, func(t *testing.T) {
407 AddDhcpv4Option82(tt.args.svc, tt.args.rID, tt.args.dhcpv4)
408 })
409 }
410}
411
412func TestVoltApplication_ProcessDsDhcpv4Packet(t *testing.T) {
413 pkt := mocks.NewMockPacket(gomock.NewController(t))
414 type args struct {
415 cntx context.Context
416 device string
417 port string
418 pkt gopacket.Packet
419 }
420 tests := []struct {
421 name string
422 args args
423 }{
424 {
425 name: "VoltApplication_ProcessDsDhcpv4Packet",
426 args: args{
427 cntx: context.Background(),
428 device: test_device,
429 port: "test_port",
430 pkt: pkt,
431 },
432 },
433 }
434 for _, tt := range tests {
435 t.Run(tt.name, func(t *testing.T) {
436 va := &VoltApplication{}
437 iPv4 := &layers.IPv4{
438 Version: uint8(1),
439 }
440 uDP := &layers.UDP{
441 Length: uint16(1),
442 }
443 dHCPv4 := &layers.DHCPv4{
444 HardwareLen: uint8(1),
445 }
446 dot1Q_test := &layers.Dot1Q{
447 Priority: uint8(1),
448 }
449 pkt.EXPECT().Layer(layers.LayerTypeEthernet).Return(eth).Times(1)
450 pkt.EXPECT().Layer(layers.LayerTypeIPv4).Return(iPv4).Times(1)
451 pkt.EXPECT().Layer(layers.LayerTypeUDP).Return(uDP).Times(1)
452 pkt.EXPECT().Layer(layers.LayerTypeDHCPv4).Return(dHCPv4).Times(1)
453 pkt.EXPECT().Layer(layers.LayerTypeDot1Q).Return(dot1Q_test).Times(1)
454 pkt.EXPECT().Layers().Return(LayerTypeDot2Q).Times(1)
455 va.ProcessDsDhcpv4Packet(tt.args.cntx, tt.args.device, tt.args.port, tt.args.pkt)
456 })
457 }
458}
459
460func TestDelOption82(t *testing.T) {
461 type args struct {
462 dhcpv4 *layers.DHCPv4
463 }
464 tests := []struct {
465 name string
466 args args
467 }{
468 {
469 name: "DelOption82",
470 args: args{
471 dhcpv4: &layers.DHCPv4{
472 Options: layers.DHCPOptions{
473 {
474 Type: opt82,
475 },
476 },
477 },
478 },
479 },
480 }
481 for _, tt := range tests {
482 t.Run(tt.name, func(t *testing.T) {
483 DelOption82(tt.args.dhcpv4)
484 })
485 }
486}
487
488func TestDhcpMsgType(t *testing.T) {
489 type args struct {
490 dhcp *layers.DHCPv4
491 }
492 tests := []struct {
493 name string
494 args args
495 want layers.DHCPMsgType
496 }{
497 {
498 name: "DhcpMsgType",
499 args: args{
500 dhcp: &layers.DHCPv4{
501 Options: layers.DHCPOptions{
502 {
503 Type: layers.DHCPOptMessageType,
504 Data: []byte{1},
505 },
506 },
507 },
508 },
509 want: layers.DHCPMsgTypeDiscover,
510 },
511 }
512 for _, tt := range tests {
513 t.Run(tt.name, func(t *testing.T) {
514 if got := DhcpMsgType(tt.args.dhcp); !reflect.DeepEqual(got, tt.want) {
515 t.Errorf("DhcpMsgType() = %v, want %v", got, tt.want)
516 }
517 })
518 }
519}
520
521func TestGetIpv4Addr(t *testing.T) {
522 type args struct {
523 dhcp *layers.DHCPv4
524 }
525 tests := []struct {
526 name string
527 args args
528 want net.IP
529 want1 int64
530 }{
531 {
532 name: "GetIpv4Addr",
533 args: args{
534 dhcp: &layers.DHCPv4{
535 Options: layers.DHCPOptions{
536 {
537 Type: layers.DHCPOptLeaseTime,
538 Data: []byte{1, 2, 3, 4, 5},
539 },
540 },
541 },
542 },
543 want1: int64(16909060),
544 },
545 }
546 for _, tt := range tests {
547 t.Run(tt.name, func(t *testing.T) {
548 got, got1 := GetIpv4Addr(tt.args.dhcp)
549 if !reflect.DeepEqual(got, tt.want) {
550 t.Errorf("GetIpv4Addr() got = %v, want %v", got, tt.want)
551 }
552 if got1 != tt.want1 {
553 t.Errorf("GetIpv4Addr() got1 = %v, want %v", got1, tt.want1)
554 }
555 })
556 }
557}
558
559func TestGetIpv6Addr(t *testing.T) {
560 type args struct {
561 dhcp6 *layers.DHCPv6
562 }
563 b, err := json.Marshal(layers.DHCPv6OptIAAddr)
564 if err != nil {
565 panic(err)
566 }
567 tests := []struct {
568 name string
569 args args
570 want net.IP
571 want1 uint32
572 }{
573 {
574 name: "GetIpv6Addr_error",
575 args: args{
576 dhcp6: &layers.DHCPv6{
577 MsgType: layers.DHCPv6MsgTypeReply,
578 Options: layers.DHCPv6Options{
579 {
580 Code: layers.DHCPv6OptIANA,
581 Data: b,
582 },
583 },
584 },
585 },
586 },
587 }
588 for _, tt := range tests {
589 t.Run(tt.name, func(t *testing.T) {
590 got, got1 := GetIpv6Addr(tt.args.dhcp6)
591 if !reflect.DeepEqual(got, tt.want) {
592 t.Errorf("GetIpv6Addr() got = %v, want %v", got, tt.want)
593 }
594 if got1 != tt.want1 {
595 t.Errorf("GetIpv6Addr() got1 = %v, want %v", got1, tt.want1)
596 }
597 })
598 }
599}
600
601func TestVoltApplication_GetMacLearnerInfo(t *testing.T) {
602 type args struct {
603 cntx context.Context
604 deviceID string
605 portNumber string
606 vlanID string
607 }
608 tests := []struct {
609 name string
610 args args
611 want MacLearnerInfo
612 wantErr bool
613 }{
614 {
615 name: "VoltApplication_GetMacLearnerInfo",
616 args: args{
617 cntx: context.Background(),
618 deviceID: test_device,
619 portNumber: "test_port_number",
620 vlanID: "test_vlanID",
621 },
622 },
623 }
624 for _, tt := range tests {
625 t.Run(tt.name, func(t *testing.T) {
626 va := &VoltApplication{}
627 got, err := va.GetMacLearnerInfo(tt.args.cntx, tt.args.deviceID, tt.args.portNumber, tt.args.vlanID)
628 if (err != nil) != tt.wantErr {
629 t.Errorf("VoltApplication.GetMacLearnerInfo() error = %v, wantErr %v", err, tt.wantErr)
630 return
631 }
632 if !reflect.DeepEqual(got, tt.want) {
633 t.Errorf("VoltApplication.GetMacLearnerInfo() = %v, want %v", got, tt.want)
634 }
635 })
636 }
637}