blob: 66edc57478bb3a32a31807730b068cd7c47ee1e6 [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"
Akash Sonib03636c2023-10-31 12:30:59 +053032 "go.uber.org/atomic"
vinokuma04dc9f82023-07-31 15:47:49 +053033)
34
35func TestVoltApplication_GetIgnoredPorts(t *testing.T) {
36 voltDevice := &VoltDevice{
37 Name: "11c3175b-50f3-4220-9555-93df733ded1d",
38 SerialNum: "SDX6320031",
39 SouthBoundID: "68580342-6b3e-57cb-9ea4-06125594e330",
40 NniPort: "16777472",
41 Ports: sync.Map{},
42 PonPortList: sync.Map{},
43 }
44 voltPort := &VoltPort{
45 Name: "16777472",
46 Device: "SDX6320031",
47 ID: 16777472,
48 State: PortStateDown,
49 ChannelPerSubAlarmRaised: false,
50 Type: VoltPortTypeNni,
51 }
52 voltPortVnets := make([]*VoltPortVnet, 0)
53 voltPortVnet := &VoltPortVnet{
54 Device: "SDX6320031",
55 Port: "16777472",
56 MacLearning: MacLearningNone,
57 }
58 voltPortVnets = append(voltPortVnets, voltPortVnet)
59 IgnoredPorts := make(map[string][]string)
60 IgnoredPorts["SDX6320031"] = append(IgnoredPorts["SDX6320031"], "16777472")
61 tests := []struct {
62 name string
63 want map[string][]string
64 wantErr bool
65 }{
66 {
67 name: "Positive_Case_GetIgnoredPorts",
68 want: IgnoredPorts,
69 wantErr: false,
70 },
71 }
72 for _, tt := range tests {
73 t.Run(tt.name, func(t *testing.T) {
74 va := &VoltApplication{
75 DevicesDisc: sync.Map{},
76 }
77 va.DevicesDisc.Store("SDX6320031", voltDevice)
78 voltDevice.Ports.Store("16777472", voltPort)
79 voltApp := GetApplication()
80 voltApp.VnetsByPort.Store("16777472", voltPortVnets)
81 got, err := va.GetIgnoredPorts()
82 if (err != nil) != tt.wantErr {
83 t.Errorf("VoltApplication.GetIgnoredPorts() error = %v, wantErr %v", err, tt.wantErr)
84 return
85 }
86 if !reflect.DeepEqual(got, tt.want) {
87 t.Errorf("VoltApplication.GetIgnoredPorts() = %v, want %v", got, tt.want)
88 }
89 })
90 }
91}
92
93func TestDhcpNetworks_AddDhcpSession(t *testing.T) {
94 pkt := mocks.NewMockPacket(gomock.NewController(t))
95 type args struct {
96 pkt gopacket.Packet
97 session IDhcpRelaySession
98 }
99 tests := []struct {
100 name string
101 args args
102 wantErr bool
103 }{
104 {
105 name: "DhcpNetworks_AddDhcpSession",
106 args: args{
107 pkt: pkt,
108 session: &VoltPortVnet{},
109 },
110 },
111 }
112 for _, tt := range tests {
113 t.Run(tt.name, func(t *testing.T) {
114 network := make(map[uint32]*DhcpRelayVnet)
115 dn := &DhcpNetworks{
116 Networks: network,
117 }
118 pkt.EXPECT().Layer(layers.LayerTypeEthernet).Return(eth).Times(1)
119 if err := dn.AddDhcpSession(tt.args.pkt, tt.args.session); (err != nil) != tt.wantErr {
120 t.Errorf("DhcpNetworks.AddDhcpSession() error = %v, wantErr %v", err, tt.wantErr)
121 }
122 })
123 }
124}
125
126func TestDhcpNetworks_DelDhcpSession(t *testing.T) {
127 pkt := mocks.NewMockPacket(gomock.NewController(t))
128 type args struct {
129 pkt gopacket.Packet
130 session IDhcpRelaySession
131 }
132 tests := []struct {
133 name string
134 args args
135 }{
136 {
137 name: "DhcpNetworks_DelDhcpSession",
138 args: args{
139 pkt: pkt,
140 session: &VoltPortVnet{},
141 },
142 },
143 }
144 for _, tt := range tests {
145 t.Run(tt.name, func(t *testing.T) {
146 network := make(map[uint32]*DhcpRelayVnet)
147 dn := &DhcpNetworks{
148 Networks: network,
149 }
150 pkt.EXPECT().Layer(layers.LayerTypeEthernet).Return(eth).Times(1)
151 dn.DelDhcpSession(tt.args.pkt, tt.args.session)
152 })
153 }
154}
155
156func TestDhcpNetworks_AddDhcp6Session(t *testing.T) {
157 type args struct {
158 key [MaxLenDhcpv6DUID]byte
159 session IDhcpRelaySession
160 }
161 tests := []struct {
162 name string
163 args args
164 wantErr bool
165 }{
166 {
167 name: "DhcpNetworks_AddDhcp6Session",
168 args: args{
169 session: &VoltPortVnet{},
170 },
171 },
172 }
173 for _, tt := range tests {
174 t.Run(tt.name, func(t *testing.T) {
175 network := make(map[uint32]*DhcpRelayVnet)
176 dn := &DhcpNetworks{
177 Networks: network,
178 }
179 if err := dn.AddDhcp6Session(tt.args.key, tt.args.session); (err != nil) != tt.wantErr {
180 t.Errorf("DhcpNetworks.AddDhcp6Session() error = %v, wantErr %v", err, tt.wantErr)
181 }
182 })
183 }
184}
185
186func TestDhcpNetworks_DelDhcp6Session(t *testing.T) {
187 type args struct {
188 key [MaxLenDhcpv6DUID]byte
189 session IDhcpRelaySession
190 }
191 tests := []struct {
192 name string
193 args args
194 }{
195 {
196 name: "DhcpNetworks_DelDhcp6Session",
197 args: args{
198 session: &VoltPortVnet{},
199 },
200 },
201 }
202 for _, tt := range tests {
203 t.Run(tt.name, func(t *testing.T) {
204 network := make(map[uint32]*DhcpRelayVnet)
205 network[uint32(4097)] = &DhcpRelayVnet{
206 InnerVlan: uint16(4097),
207 }
208 dn := &DhcpNetworks{
209 Networks: network,
210 }
211 dn.DelDhcp6Session(tt.args.key, tt.args.session)
212 })
213 }
214}
215
216func TestDhcpNetworks_GetDhcpSession(t *testing.T) {
217 type fields struct {
218 Networks map[uint32]*DhcpRelayVnet
219 }
220 type args struct {
221 outerVlan uint16
222 innerVlan uint16
223 addr net.HardwareAddr
224 }
225 macAdd, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff")
226 tests := []struct {
227 name string
228 fields fields
229 args args
230 want IDhcpRelaySession
231 }{
232 {
233 name: "DhcpNetworks_GetDhcpSession",
234 args: args{
235 outerVlan: uint16(0),
236 innerVlan: uint16(4097),
237 addr: macAdd,
238 },
239 },
240 }
241 for _, tt := range tests {
242 t.Run(tt.name, func(t *testing.T) {
243 network := make(map[uint32]*DhcpRelayVnet)
244 network[uint32(4097)] = &DhcpRelayVnet{
245 InnerVlan: uint16(4097),
246 }
247 dn := &DhcpNetworks{
248 Networks: network,
249 }
250 got, err := dn.GetDhcpSession(tt.args.outerVlan, tt.args.innerVlan, tt.args.addr)
251 assert.NotNil(t, err)
252 assert.Nil(t, got)
253 })
254 }
255}
256
257func TestDhcpNetworks_GetDhcp6Session(t *testing.T) {
258 type fields struct {
259 Networks map[uint32]*DhcpRelayVnet
260 }
261 type args struct {
262 outerVlan uint16
263 innerVlan uint16
264 key [MaxLenDhcpv6DUID]byte
265 }
266 tests := []struct {
267 name string
268 fields fields
269 args args
270 want IDhcpRelaySession
271 wantErr bool
272 }{
273 {
274 name: "DhcpNetworks_GetDhcp6Session",
275 args: args{
276 outerVlan: uint16(0),
277 innerVlan: uint16(4097),
278 },
279 },
280 }
281 for _, tt := range tests {
282 t.Run(tt.name, func(t *testing.T) {
283 network := make(map[uint32]*DhcpRelayVnet)
284 network[uint32(4097)] = &DhcpRelayVnet{
285 InnerVlan: uint16(4097),
286 }
287 dn := &DhcpNetworks{
288 Networks: network,
289 }
290 got, err := dn.GetDhcp6Session(tt.args.outerVlan, tt.args.innerVlan, tt.args.key)
291 assert.NotNil(t, err)
292 assert.Nil(t, got)
293 })
294 }
295}
296
297func TestGetVnetForV4Nni(t *testing.T) {
298 type args struct {
299 dhcp *layers.DHCPv4
300 cvlan of.VlanType
301 svlan of.VlanType
302 pbit uint8
303 }
304 macAdd, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff")
305 tests := []struct {
306 name string
307 args args
308 want []*VoltPortVnet
309 wantErr bool
310 }{
311 {
312 name: "GetVnetForV4Nni",
313 args: args{
314 cvlan: of.VlanAny,
315 svlan: of.VlanAny,
316 dhcp: &layers.DHCPv4{
317 BaseLayer: dot1Q.BaseLayer,
318 ClientHWAddr: macAdd,
319 },
320 },
321 },
322 }
323 for _, tt := range tests {
324 t.Run(tt.name, func(t *testing.T) {
325 got, err := GetVnetForV4Nni(tt.args.dhcp, tt.args.cvlan, tt.args.svlan, tt.args.pbit)
326 assert.NotNil(t, err)
327 assert.Nil(t, got)
328 })
329 }
330}
331
332func TestGetVnetForV6Nni(t *testing.T) {
333 type args struct {
334 dhcp *layers.DHCPv6
335 cvlan of.VlanType
336 svlan of.VlanType
337 pbit uint8
338 clientMAC net.HardwareAddr
339 }
340 macAdd, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff")
341 tests := []struct {
342 name string
343 args args
344 want []*VoltPortVnet
345 want1 net.HardwareAddr
346 wantErr bool
347 }{
348 {
349 name: "GetVnetForV6Nni",
350 args: args{
351 dhcp: &layers.DHCPv6{
352 BaseLayer: dot1Q.BaseLayer,
353 Options: layers.DHCPv6Options{
354 {
355 Code: layers.DHCPv6OptClientID,
356 Data: []byte{2, 3, 4, 2, 3, 4, 2, 3, 4},
357 },
358 },
359 },
360 cvlan: of.VlanAny,
361 svlan: of.VlanAny,
362 clientMAC: macAdd,
363 },
364 },
365 }
366 for _, tt := range tests {
367 t.Run(tt.name, func(t *testing.T) {
368 got, got1, err := GetVnetForV6Nni(tt.args.dhcp, tt.args.cvlan, tt.args.svlan, tt.args.pbit, tt.args.clientMAC)
369 assert.NotNil(t, err)
370 assert.Nil(t, got)
371 assert.NotNil(t, got1)
372 })
373 }
374}
375
376func TestAddDhcpv4Option82(t *testing.T) {
377 type args struct {
378 svc *VoltService
379 rID []byte
380 dhcpv4 *layers.DHCPv4
381 }
382 tests := []struct {
383 name string
384 args args
385 }{
386 {
387 name: "AddDhcpv4Option82",
388 args: args{
389 svc: &VoltService{
390 VoltServiceCfg: VoltServiceCfg{
391 CircuitID: "test_circuit_id",
392 DataRateAttr: DSLAttrEnabled,
393 },
394 },
395 rID: []byte{1},
396 dhcpv4: &layers.DHCPv4{
397 Options: layers.DHCPOptions{
398 {
399 Type: layers.DHCPOptARPTimeout,
400 },
401 },
402 },
403 },
404 },
405 }
406 for _, tt := range tests {
407 t.Run(tt.name, func(t *testing.T) {
408 AddDhcpv4Option82(tt.args.svc, tt.args.rID, tt.args.dhcpv4)
409 })
410 }
411}
412
413func TestVoltApplication_ProcessDsDhcpv4Packet(t *testing.T) {
414 pkt := mocks.NewMockPacket(gomock.NewController(t))
415 type args struct {
416 cntx context.Context
417 device string
418 port string
419 pkt gopacket.Packet
420 }
421 tests := []struct {
422 name string
423 args args
424 }{
425 {
426 name: "VoltApplication_ProcessDsDhcpv4Packet",
427 args: args{
428 cntx: context.Background(),
429 device: test_device,
430 port: "test_port",
431 pkt: pkt,
432 },
433 },
434 }
435 for _, tt := range tests {
436 t.Run(tt.name, func(t *testing.T) {
437 va := &VoltApplication{}
438 iPv4 := &layers.IPv4{
439 Version: uint8(1),
440 }
441 uDP := &layers.UDP{
442 Length: uint16(1),
443 }
444 dHCPv4 := &layers.DHCPv4{
445 HardwareLen: uint8(1),
446 }
447 dot1Q_test := &layers.Dot1Q{
448 Priority: uint8(1),
449 }
450 pkt.EXPECT().Layer(layers.LayerTypeEthernet).Return(eth).Times(1)
451 pkt.EXPECT().Layer(layers.LayerTypeIPv4).Return(iPv4).Times(1)
452 pkt.EXPECT().Layer(layers.LayerTypeUDP).Return(uDP).Times(1)
453 pkt.EXPECT().Layer(layers.LayerTypeDHCPv4).Return(dHCPv4).Times(1)
454 pkt.EXPECT().Layer(layers.LayerTypeDot1Q).Return(dot1Q_test).Times(1)
455 pkt.EXPECT().Layers().Return(LayerTypeDot2Q).Times(1)
456 va.ProcessDsDhcpv4Packet(tt.args.cntx, tt.args.device, tt.args.port, tt.args.pkt)
457 })
458 }
459}
460
461func TestDelOption82(t *testing.T) {
462 type args struct {
463 dhcpv4 *layers.DHCPv4
464 }
465 tests := []struct {
466 name string
467 args args
468 }{
469 {
470 name: "DelOption82",
471 args: args{
472 dhcpv4: &layers.DHCPv4{
473 Options: layers.DHCPOptions{
474 {
475 Type: opt82,
476 },
477 },
478 },
479 },
480 },
481 }
482 for _, tt := range tests {
483 t.Run(tt.name, func(t *testing.T) {
484 DelOption82(tt.args.dhcpv4)
485 })
486 }
487}
488
489func TestDhcpMsgType(t *testing.T) {
490 type args struct {
491 dhcp *layers.DHCPv4
492 }
493 tests := []struct {
494 name string
495 args args
496 want layers.DHCPMsgType
497 }{
498 {
499 name: "DhcpMsgType",
500 args: args{
501 dhcp: &layers.DHCPv4{
502 Options: layers.DHCPOptions{
503 {
504 Type: layers.DHCPOptMessageType,
505 Data: []byte{1},
506 },
507 },
508 },
509 },
510 want: layers.DHCPMsgTypeDiscover,
511 },
512 }
513 for _, tt := range tests {
514 t.Run(tt.name, func(t *testing.T) {
515 if got := DhcpMsgType(tt.args.dhcp); !reflect.DeepEqual(got, tt.want) {
516 t.Errorf("DhcpMsgType() = %v, want %v", got, tt.want)
517 }
518 })
519 }
520}
521
522func TestGetIpv4Addr(t *testing.T) {
523 type args struct {
524 dhcp *layers.DHCPv4
525 }
526 tests := []struct {
527 name string
528 args args
529 want net.IP
530 want1 int64
531 }{
532 {
533 name: "GetIpv4Addr",
534 args: args{
535 dhcp: &layers.DHCPv4{
536 Options: layers.DHCPOptions{
537 {
538 Type: layers.DHCPOptLeaseTime,
539 Data: []byte{1, 2, 3, 4, 5},
540 },
541 },
542 },
543 },
544 want1: int64(16909060),
545 },
546 }
547 for _, tt := range tests {
548 t.Run(tt.name, func(t *testing.T) {
549 got, got1 := GetIpv4Addr(tt.args.dhcp)
550 if !reflect.DeepEqual(got, tt.want) {
551 t.Errorf("GetIpv4Addr() got = %v, want %v", got, tt.want)
552 }
553 if got1 != tt.want1 {
554 t.Errorf("GetIpv4Addr() got1 = %v, want %v", got1, tt.want1)
555 }
556 })
557 }
558}
559
560func TestGetIpv6Addr(t *testing.T) {
561 type args struct {
562 dhcp6 *layers.DHCPv6
563 }
564 b, err := json.Marshal(layers.DHCPv6OptIAAddr)
565 if err != nil {
566 panic(err)
567 }
568 tests := []struct {
569 name string
570 args args
571 want net.IP
572 want1 uint32
573 }{
574 {
575 name: "GetIpv6Addr_error",
576 args: args{
577 dhcp6: &layers.DHCPv6{
578 MsgType: layers.DHCPv6MsgTypeReply,
579 Options: layers.DHCPv6Options{
580 {
581 Code: layers.DHCPv6OptIANA,
582 Data: b,
583 },
584 },
585 },
586 },
587 },
588 }
589 for _, tt := range tests {
590 t.Run(tt.name, func(t *testing.T) {
591 got, got1 := GetIpv6Addr(tt.args.dhcp6)
592 if !reflect.DeepEqual(got, tt.want) {
593 t.Errorf("GetIpv6Addr() got = %v, want %v", got, tt.want)
594 }
595 if got1 != tt.want1 {
596 t.Errorf("GetIpv6Addr() got1 = %v, want %v", got1, tt.want1)
597 }
598 })
599 }
600}
601
602func TestVoltApplication_GetMacLearnerInfo(t *testing.T) {
603 type args struct {
604 cntx context.Context
605 deviceID string
606 portNumber string
607 vlanID string
608 }
Akash Soni6f369452023-09-19 11:18:28 +0530609 vpv := &VoltPortVnet{
610 Device: "SDX6320031",
611 Port: "SDX6320031-1",
612 SVlan: of.VlanAny,
613 MacAddr: BroadcastMAC,
614 }
615 sessions := map[[6]byte]IDhcpRelaySession{}
616 key := [6]byte{1, 2, 3, 4, 5, 6}
617 sessions[key] = vpv
618 network := make(map[uint32]*DhcpRelayVnet)
619 network[uint32(256)] = &DhcpRelayVnet{
620 sessions: sessions,
621 }
622 dhcpNws.Networks = network
623 svlan := of.VlanAny
624 macLearning := MacLearnerInfo{
625 DeviceID: "SDX6320031",
626 PortNumber: "SDX6320031-1",
627 VlanID: svlan.String(),
628 MacAddress: BroadcastMAC.String(),
629 }
vinokuma04dc9f82023-07-31 15:47:49 +0530630 tests := []struct {
631 name string
632 args args
633 want MacLearnerInfo
634 wantErr bool
635 }{
636 {
637 name: "VoltApplication_GetMacLearnerInfo",
638 args: args{
639 cntx: context.Background(),
Akash Soni6f369452023-09-19 11:18:28 +0530640 deviceID: "SDX6320031",
641 portNumber: "SDX6320031-1",
642 vlanID: svlan.String(),
vinokuma04dc9f82023-07-31 15:47:49 +0530643 },
Akash Soni6f369452023-09-19 11:18:28 +0530644 want: macLearning,
645 },
646 {
647 name: "VoltApplication_GetMacLearnerInfo_svlan_empty",
648 args: args{
649 cntx: context.Background(),
650 deviceID: "SDX6320031",
651 portNumber: "SDX6320031-1",
652 vlanID: "",
653 },
654 want: macLearning,
vinokuma04dc9f82023-07-31 15:47:49 +0530655 },
656 }
657 for _, tt := range tests {
658 t.Run(tt.name, func(t *testing.T) {
659 va := &VoltApplication{}
660 got, err := va.GetMacLearnerInfo(tt.args.cntx, tt.args.deviceID, tt.args.portNumber, tt.args.vlanID)
661 if (err != nil) != tt.wantErr {
662 t.Errorf("VoltApplication.GetMacLearnerInfo() error = %v, wantErr %v", err, tt.wantErr)
663 return
664 }
665 if !reflect.DeepEqual(got, tt.want) {
666 t.Errorf("VoltApplication.GetMacLearnerInfo() = %v, want %v", got, tt.want)
667 }
668 })
669 }
670}
Akash Soni6f369452023-09-19 11:18:28 +0530671
672func TestVoltApplication_GetAllocations(t *testing.T) {
673 type args struct {
674 cntx context.Context
675 deviceID string
676 }
677 allocation := []DhcpAllocation{}
678 vpv := &VoltPortVnet{
679 Device: "SDX6320031",
680 services: sync.Map{},
681 }
682 voltServ := &VoltService{
683 VoltServiceOper: VoltServiceOper{
684 Device: "SDX6320031",
685 },
686 VoltServiceCfg: VoltServiceCfg{
687 Name: "SDX6320031-1_SDX6320031-1-4096-2310-4096-65",
688 },
689 }
690 sessions := map[[6]byte]IDhcpRelaySession{}
691 key := [6]byte{1, 2, 3, 4, 5, 6}
692 sessions[key] = vpv
693 network := make(map[uint32]*DhcpRelayVnet)
694 network[uint32(256)] = &DhcpRelayVnet{
695 sessions: sessions,
696 }
697 dhcpNws.Networks = network
698 tests := []struct {
699 name string
700 args args
701 want []DhcpAllocation
702 wantErr bool
703 }{
704 {
705 name: "VoltApplication_GetAllocations",
706 args: args{
707 cntx: context.Background(),
708 deviceID: "SDX6320031",
709 },
710 want: allocation,
711 },
712 {
713 name: "GetAllocations_with_Services",
714 args: args{
715 cntx: context.Background(),
716 deviceID: "SDX6320031",
717 },
718 want: allocation,
719 },
720 }
721 for _, tt := range tests {
722 t.Run(tt.name, func(t *testing.T) {
723 va := &VoltApplication{}
724 switch tt.name {
725 case "VoltApplication_GetAllocations":
726 got, err := va.GetAllocations(tt.args.cntx, tt.args.deviceID)
727 if (err != nil) != tt.wantErr {
728 t.Errorf("VoltApplication.GetAllocations() error = %v, wantErr %v", err, tt.wantErr)
729 return
730 }
731 assert.NotNil(t, got)
732 case "GetAllocations_with_Services":
733 vpv.services.Store("SDX6320031-1_SDX6320031-1-4096-2310-4096-65", voltServ)
734 got, err := va.GetAllocations(tt.args.cntx, tt.args.deviceID)
735 if (err != nil) != tt.wantErr {
736 t.Errorf("VoltApplication.GetAllocations() error = %v, wantErr %v", err, tt.wantErr)
737 return
738 }
739 assert.NotNil(t, got)
740 }
741 })
742 }
743}
744
745func TestVoltApplication_GetAllMacLearnerInfo(t *testing.T) {
746 vpv := &VoltPortVnet{
747 Device: "SDX6320031",
748 Port: "SDX6320031-1",
749 SVlan: of.VlanAny,
750 MacAddr: BroadcastMAC,
751 }
752 sessions := map[[6]byte]IDhcpRelaySession{}
753 key := [6]byte{1, 2, 3, 4, 5, 6}
754 sessions[key] = vpv
755 network := make(map[uint32]*DhcpRelayVnet)
756 network[uint32(256)] = &DhcpRelayVnet{
757 sessions: sessions,
758 }
759 dhcpNws.Networks = network
760 svlan := of.VlanAny
761 macLearningList := []MacLearnerInfo{}
762 macLearning := MacLearnerInfo{
763 DeviceID: "SDX6320031",
764 PortNumber: "SDX6320031-1",
765 VlanID: svlan.String(),
766 MacAddress: BroadcastMAC.String(),
767 }
768 macLearningList = append(macLearningList, macLearning)
769 tests := []struct {
770 name string
771 want []MacLearnerInfo
772 wantErr bool
773 }{
774 {
775 name: "VoltApplication_GetAllMacLearnerInfo",
776 want: macLearningList,
777 },
778 }
779 for _, tt := range tests {
780 t.Run(tt.name, func(t *testing.T) {
781 va := &VoltApplication{}
782 got, err := va.GetAllMacLearnerInfo()
783 if (err != nil) != tt.wantErr {
784 t.Errorf("VoltApplication.GetAllMacLearnerInfo() error = %v, wantErr %v", err, tt.wantErr)
785 return
786 }
787 if !reflect.DeepEqual(got, tt.want) {
788 t.Errorf("VoltApplication.GetAllMacLearnerInfo() = %v, want %v", got, tt.want)
789 }
790 })
791 }
792}
Akash Sonib03636c2023-10-31 12:30:59 +0530793
794func Test_raiseDHCPv4Indication(t *testing.T) {
795 voltServ := &VoltService{
796 VoltServiceOper: VoltServiceOper{
797 Device: "SDX6320031",
798 },
799 VoltServiceCfg: VoltServiceCfg{
800 IsActivated: true,
801 Pbits: []of.PbitType{
802 of.PbitNone,
803 },
804 },
805 }
806 voltPortVnet := &VoltPortVnet{
807 Device: "SDX6320031",
808 Port: "16777472",
809 DeleteInProgress: false,
810 services: sync.Map{},
811 SVlan: 4096,
812 CVlan: 2310,
813 UniVlan: 4096,
814 SVlanTpid: 65,
815 servicesCount: atomic.NewUint64(1),
816 }
817 voltPortVnet.services.Store("SDX6320031-1_SDX6320031-1-4096-2310-4096-65", voltServ)
818 type args struct {
819 msgType layers.DHCPMsgType
820 vpv *VoltPortVnet
821 smac net.HardwareAddr
822 ip net.IP
823 pktPbit uint8
824 device string
825 leaseTime int64
826 }
827 tests := []struct {
828 name string
829 args args
830 }{
831 {
832 name: "raiseDHCPv4Indication_DHCPMsgTypeDiscover",
833 args: args{
834 msgType: layers.DHCPMsgTypeDiscover,
835 vpv: voltPortVnet,
836 device: "SDX6320031",
837 },
838 },
839 {
840 name: "raiseDHCPv4Indication_DHCPMsgTypeRequest",
841 args: args{
842 msgType: layers.DHCPMsgTypeRequest,
843 vpv: voltPortVnet,
844 },
845 },
846 {
847 name: "raiseDHCPv4Indication_DHCPMsgTypeRelease",
848 args: args{
849 msgType: layers.DHCPMsgTypeRelease,
850 vpv: voltPortVnet,
851 },
852 },
853 {
854 name: "raiseDHCPv4Indication_DHCPMsgTypeAck",
855 args: args{
856 msgType: layers.DHCPMsgTypeAck,
857 vpv: voltPortVnet,
858 },
859 },
860 {
861 name: "raiseDHCPv4Indication_DHCPMsgTypeNak",
862 args: args{
863 msgType: layers.DHCPMsgTypeNak,
864 vpv: voltPortVnet,
865 },
866 },
867 {
868 name: "raiseDHCPv4Indication_DHCPMsgTypeOffer",
869 args: args{
870 msgType: layers.DHCPMsgTypeOffer,
871 vpv: voltPortVnet,
872 },
873 },
874 }
875 for _, tt := range tests {
876 t.Run(tt.name, func(t *testing.T) {
877 raiseDHCPv4Indication(tt.args.msgType, tt.args.vpv, tt.args.smac, tt.args.ip, tt.args.pktPbit, tt.args.device, tt.args.leaseTime)
878 })
879 }
880}
881
882func Test_raiseDHCPv6Indication(t *testing.T) {
883 voltServ := &VoltService{
884 VoltServiceOper: VoltServiceOper{
885 Device: "SDX6320031",
886 },
887 VoltServiceCfg: VoltServiceCfg{
888 IsActivated: true,
889 Pbits: []of.PbitType{
890 of.PbitNone,
891 },
892 },
893 }
894 voltPortVnet := &VoltPortVnet{
895 Device: "SDX6320031",
896 Port: "16777472",
897 DeleteInProgress: false,
898 services: sync.Map{},
899 SVlan: 4096,
900 CVlan: 2310,
901 UniVlan: 4096,
902 SVlanTpid: 65,
903 servicesCount: atomic.NewUint64(1),
904 }
905 voltPortVnet.services.Store("SDX6320031-1_SDX6320031-1-4096-2310-4096-65", voltServ)
906 type args struct {
907 msgType layers.DHCPv6MsgType
908 vpv *VoltPortVnet
909 smac net.HardwareAddr
910 ip net.IP
911 pktPbit uint8
912 device string
913 leaseTime uint32
914 }
915 tests := []struct {
916 name string
917 args args
918 }{
919 {
920 name: "raiseDHCPv6Indication_DHCPv6MsgTypeSolicit",
921 args: args{
922 msgType: layers.DHCPv6MsgTypeSolicit,
923 vpv: voltPortVnet,
924 device: "SDX6320031",
925 },
926 },
927 {
928 name: "raiseDHCPv4Indication_DHCPv6MsgTypeRelease",
929 args: args{
930 msgType: layers.DHCPv6MsgTypeRelease,
931 vpv: voltPortVnet,
932 },
933 },
934 {
935 name: "raiseDHCPv4Indication_DHCPv6MsgTypeReply",
936 args: args{
937 msgType: layers.DHCPv6MsgTypeReply,
938 vpv: voltPortVnet,
939 },
940 },
941 }
942 for _, tt := range tests {
943 t.Run(tt.name, func(t *testing.T) {
944 raiseDHCPv6Indication(tt.args.msgType, tt.args.vpv, tt.args.smac, tt.args.ip, tt.args.pktPbit, tt.args.device, tt.args.leaseTime)
945 })
946 }
947}
948
949func TestVoltApplication_ProcessUDP6Packet(t *testing.T) {
950 type args struct {
951 cntx context.Context
952 device string
953 port string
954 pkt gopacket.Packet
955 }
956 pkt := mocks.NewMockPacket(gomock.NewController(t))
957 dhcpv6 := &layers.DHCPv6{
958 MsgType: layers.DHCPv6MsgTypeSolicit,
959 }
960 ipv6 := &layers.IPv6{
961 Version: EtherType8100,
962 }
963 uup := &layers.UDP{
964 SrcPort: opt82,
965 }
966 tests := []struct {
967 name string
968 args args
969 want []byte
970 }{
971 {
972 name: "ProcessUDP6Packet_DHCPv6MsgTypeSolicit",
973 args: args{
974 cntx: context.Background(),
975 device: "SDX6320031",
976 port: "16777472",
977 pkt: pkt,
978 },
979 },
980 {
981 name: "ProcessUDP6Packet_DHCPv6MsgTypeAdvertise",
982 args: args{
983 cntx: context.Background(),
984 device: "SDX6320031",
985 port: "16777472",
986 pkt: pkt,
987 },
988 },
989 {
990 name: "ProcessUDP6Packet_DHCPv6MsgTypeRelayForward",
991 args: args{
992 cntx: context.Background(),
993 device: "SDX6320031",
994 port: "16777472",
995 pkt: pkt,
996 },
997 },
998 {
999 name: "ProcessUDP6Packet_DHCPv6MsgTypeRelayReply",
1000 args: args{
1001 cntx: context.Background(),
1002 device: "SDX6320031",
1003 port: "16777472",
1004 pkt: pkt,
1005 },
1006 },
1007 }
1008 for _, tt := range tests {
1009 t.Run(tt.name, func(t *testing.T) {
1010 va := &VoltApplication{}
1011 switch tt.name {
1012 case "ProcessUDP6Packet_DHCPv6MsgTypeSolicit":
1013 pkt.EXPECT().Layer(layers.LayerTypeDHCPv6).Return(dhcpv6).Times(2)
1014 pkt.EXPECT().Data().Times(1)
1015 pkt.EXPECT().Layers().Return(LayerTypeDot2Q).Times(2)
1016 if got := va.ProcessUDP6Packet(tt.args.cntx, tt.args.device, tt.args.port, tt.args.pkt); !reflect.DeepEqual(got, tt.want) {
1017 t.Errorf("VoltApplication.ProcessUDP6Packet() = %v, want %v", got, tt.want)
1018 }
1019 case "ProcessUDP6Packet_DHCPv6MsgTypeAdvertise":
1020 dhcpv6.MsgType = layers.DHCPv6MsgTypeAdvertise
1021 pkt.EXPECT().Layer(layers.LayerTypeDHCPv6).Return(dhcpv6).Times(2)
1022 if got := va.ProcessUDP6Packet(tt.args.cntx, tt.args.device, tt.args.port, tt.args.pkt); !reflect.DeepEqual(got, tt.want) {
1023 t.Errorf("VoltApplication.ProcessUDP6Packet() = %v, want %v", got, tt.want)
1024 }
1025 case "ProcessUDP6Packet_DHCPv6MsgTypeRelayForward":
1026 dhcpv6.MsgType = layers.DHCPv6MsgTypeRelayForward
1027 pkt.EXPECT().Layer(layers.LayerTypeDHCPv6).Return(dhcpv6).Times(2)
1028 if got := va.ProcessUDP6Packet(tt.args.cntx, tt.args.device, tt.args.port, tt.args.pkt); !reflect.DeepEqual(got, tt.want) {
1029 t.Errorf("VoltApplication.ProcessUDP6Packet() = %v, want %v", got, tt.want)
1030 }
1031 case "ProcessUDP6Packet_DHCPv6MsgTypeRelayReply":
1032 dhcpv6.MsgType = layers.DHCPv6MsgTypeRelayReply
1033 pkt.EXPECT().Data().Times(1)
1034 pkt.EXPECT().Layer(layers.LayerTypeEthernet).Return(eth).Times(1)
1035 pkt.EXPECT().Layer(layers.LayerTypeIPv6).Return(ipv6).Times(1)
1036 pkt.EXPECT().Layer(layers.LayerTypeUDP).Return(uup).Times(1)
1037 if got := va.ProcessUDP6Packet(tt.args.cntx, tt.args.device, tt.args.port, tt.args.pkt); !reflect.DeepEqual(got, tt.want) {
1038 t.Errorf("VoltApplication.ProcessUDP6Packet() = %v, want %v", got, tt.want)
1039 }
1040 }
1041 })
1042 }
1043}
1044
1045func TestBuildRelayFwd(t *testing.T) {
1046 type args struct {
1047 paddr net.IP
1048 intfID []byte
1049 remoteID []byte
1050 payload []byte
1051 isOption82Enabled bool
1052 dhcpRelay bool
1053 }
1054 tests := []struct {
1055 name string
1056 args args
1057 want *layers.DHCPv6
1058 }{
1059 {
1060 name: "BuildRelayFwd",
1061 args: args{
1062 paddr: AllSystemsMulticastGroupIP,
1063 intfID: AllSystemsMulticastGroupIP,
1064 remoteID: AllSystemsMulticastGroupIP,
1065 payload: AllSystemsMulticastGroupIP,
1066 isOption82Enabled: true,
1067 dhcpRelay: true,
1068 },
1069 want: &layers.DHCPv6{},
1070 },
1071 }
1072 for _, tt := range tests {
1073 t.Run(tt.name, func(t *testing.T) {
1074 got := BuildRelayFwd(tt.args.paddr, tt.args.intfID, tt.args.remoteID, tt.args.payload, tt.args.isOption82Enabled, tt.args.dhcpRelay)
1075 assert.NotNil(t, got)
1076 })
1077 }
1078}
1079
1080func TestGetRelayReplyBytes(t *testing.T) {
1081 type args struct {
1082 dhcp6 *layers.DHCPv6
1083 }
1084 tests := []struct {
1085 name string
1086 args args
1087 want []byte
1088 }{
1089 {
1090 name: "BuildRelayFwd",
1091 args: args{
1092 dhcp6: &layers.DHCPv6{
1093 Options: make(layers.DHCPv6Options, 1),
1094 },
1095 },
1096 want: AllSystemsMulticastGroupIP,
1097 },
1098 }
1099 for _, tt := range tests {
1100 t.Run(tt.name, func(t *testing.T) {
1101 got := GetRelayReplyBytes(tt.args.dhcp6)
1102 assert.Nil(t, got)
1103 })
1104 }
1105}
1106
1107func TestVoltApplication_ProcessUsDhcpv6Packet(t *testing.T) {
1108 type args struct {
1109 cntx context.Context
1110 device string
1111 port string
1112 pkt gopacket.Packet
1113 }
1114 voltDevice := &VoltDevice{
1115 Name: "11c3175b-50f3-4220-9555-93df733ded1d",
1116 SerialNum: "SDX6320031",
1117 SouthBoundID: "68580342-6b3e-57cb-9ea4-06125594e330",
1118 NniPort: "16777472",
1119 Ports: sync.Map{},
1120 PonPortList: sync.Map{},
1121 }
1122 pkt := mocks.NewMockPacket(gomock.NewController(t))
1123 tests := []struct {
1124 name string
1125 args args
1126 }{
1127 {
1128 name: "ProcessUsDhcpv6Packet",
1129 args: args{
1130 cntx: context.Background(),
1131 device: "SDX6320031",
1132 port: "16777472",
1133 pkt: pkt,
1134 },
1135 },
1136 }
1137 for _, tt := range tests {
1138 t.Run(tt.name, func(t *testing.T) {
1139 va := &VoltApplication{
1140 DevicesDisc: sync.Map{},
1141 }
1142 va.DevicesDisc.Store("SDX6320031", voltDevice)
1143 pkt.EXPECT().Data().Times(1)
1144 pkt.EXPECT().Layers().Return(LayerTypeDot2Q).Times(2)
1145 va.ProcessUsDhcpv6Packet(tt.args.cntx, tt.args.device, tt.args.port, tt.args.pkt)
1146 })
1147 }
1148}