vinokuma | f7605fc | 2023-06-02 18:08:01 +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 ( |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame^] | 19 | "context" |
| 20 | "net" |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 21 | "testing" |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame^] | 22 | "time" |
| 23 | "voltha-go-controller/internal/pkg/of" |
| 24 | common "voltha-go-controller/internal/pkg/types" |
| 25 | "voltha-go-controller/internal/test/mocks" |
| 26 | |
| 27 | "github.com/golang/mock/gomock" |
| 28 | "github.com/google/gopacket/layers" |
| 29 | "github.com/stretchr/testify/assert" |
vinokuma | f7605fc | 2023-06-02 18:08:01 +0530 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | func TestVoltApplication_InitIgmpSrcMac(t *testing.T) { |
| 33 | tests := []struct { |
| 34 | name string |
| 35 | }{ |
| 36 | { |
| 37 | name: "test", |
| 38 | }, |
| 39 | } |
| 40 | for _, tt := range tests { |
| 41 | t.Run(tt.name, func(t *testing.T) { |
| 42 | va := &VoltApplication{} |
| 43 | va.InitIgmpSrcMac() |
| 44 | }) |
| 45 | } |
| 46 | } |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame^] | 47 | |
| 48 | func TestVoltApplication_UpdateIgmpProfile(t *testing.T) { |
| 49 | type args struct { |
| 50 | cntx context.Context |
| 51 | igmpProfileConfig *common.IGMPConfig |
| 52 | } |
| 53 | igmpConfig := &common.IGMPConfig{ |
| 54 | ProfileID: "test_profile_id", |
| 55 | FastLeave: &vgcRebooted, |
| 56 | PeriodicQuery: &isUpgradeComplete, |
| 57 | WithRAUpLink: &isUpgradeComplete, |
| 58 | WithRADownLink: &isUpgradeComplete, |
| 59 | } |
| 60 | igmpProfile_data := &IgmpProfile{ |
| 61 | ProfileID: "test_profile_id", |
| 62 | } |
| 63 | |
| 64 | tests := []struct { |
| 65 | name string |
| 66 | args args |
| 67 | wantErr bool |
| 68 | }{ |
| 69 | { |
| 70 | name: "UpdateIgmpProfile", |
| 71 | args: args{ |
| 72 | cntx: context.Background(), |
| 73 | igmpProfileConfig: igmpConfig, |
| 74 | }, |
| 75 | }, |
| 76 | { |
| 77 | name: "UpdateIgmpProfile_Profile_not_found", |
| 78 | args: args{ |
| 79 | cntx: context.Background(), |
| 80 | igmpProfileConfig: igmpConfig, |
| 81 | }, |
| 82 | wantErr: true, |
| 83 | }, |
| 84 | } |
| 85 | for _, tt := range tests { |
| 86 | t.Run(tt.name, func(t *testing.T) { |
| 87 | va := &VoltApplication{} |
| 88 | switch tt.name { |
| 89 | case "UpdateIgmpProfile": |
| 90 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 91 | db = dbintf |
| 92 | dbintf.EXPECT().PutIgmpProfile(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) |
| 93 | va.IgmpProfilesByName.Store("test_profile_id", igmpProfile_data) |
| 94 | if err := va.UpdateIgmpProfile(tt.args.cntx, tt.args.igmpProfileConfig); (err != nil) != tt.wantErr { |
| 95 | t.Errorf("VoltApplication.UpdateIgmpProfile() error = %v, wantErr %v", err, tt.wantErr) |
| 96 | } |
| 97 | case "UpdateIgmpProfile_Profile_not_found": |
| 98 | igmpConfig.ProfileID = "" |
| 99 | if err := va.UpdateIgmpProfile(tt.args.cntx, tt.args.igmpProfileConfig); (err != nil) != tt.wantErr { |
| 100 | t.Errorf("VoltApplication.UpdateIgmpProfile() error = %v, wantErr %v", err, tt.wantErr) |
| 101 | } |
| 102 | } |
| 103 | }) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestVoltApplication_resetIgmpProfileToDefault(t *testing.T) { |
| 108 | type args struct { |
| 109 | cntx context.Context |
| 110 | } |
| 111 | igmpProfile_data := &IgmpProfile{ |
| 112 | ProfileID: "test_profile_id", |
| 113 | } |
| 114 | tests := []struct { |
| 115 | name string |
| 116 | args args |
| 117 | }{ |
| 118 | { |
| 119 | name: "resetIgmpProfileToDefault", |
| 120 | args: args{ |
| 121 | cntx: context.Background(), |
| 122 | }, |
| 123 | }, |
| 124 | } |
| 125 | for _, tt := range tests { |
| 126 | t.Run(tt.name, func(t *testing.T) { |
| 127 | va := &VoltApplication{} |
| 128 | va.IgmpProfilesByName.Store("", igmpProfile_data) |
| 129 | dbintf := mocks.NewMockDBIntf(gomock.NewController(t)) |
| 130 | db = dbintf |
| 131 | dbintf.EXPECT().PutIgmpProfile(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) |
| 132 | va.resetIgmpProfileToDefault(tt.args.cntx) |
| 133 | }) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func Test_ipv4ToUint(t *testing.T) { |
| 138 | type args struct { |
| 139 | ip net.IP |
| 140 | } |
| 141 | tests := []struct { |
| 142 | name string |
| 143 | args args |
| 144 | want uint32 |
| 145 | }{ |
| 146 | { |
| 147 | name: "ipv4ToUint", |
| 148 | args: args{ |
| 149 | ip: AllSystemsMulticastGroupIP, |
| 150 | }, |
| 151 | want: 3758096385, |
| 152 | }, |
| 153 | { |
| 154 | name: "ipv4ToUint", |
| 155 | args: args{ |
| 156 | ip: nil, |
| 157 | }, |
| 158 | }, |
| 159 | } |
| 160 | for _, tt := range tests { |
| 161 | t.Run(tt.name, func(t *testing.T) { |
| 162 | if got := ipv4ToUint(tt.args.ip); got != tt.want { |
| 163 | t.Errorf("ipv4ToUint() = %v, want %v", got, tt.want) |
| 164 | } |
| 165 | }) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | func TestIgmpUsEthLayer(t *testing.T) { |
| 170 | type args struct { |
| 171 | mcip net.IP |
| 172 | } |
| 173 | tests := []struct { |
| 174 | name string |
| 175 | args args |
| 176 | want *layers.Ethernet |
| 177 | }{ |
| 178 | { |
| 179 | name: "IgmpUsEthLayer", |
| 180 | args: args{ |
| 181 | mcip: AllSystemsMulticastGroupIP, |
| 182 | }, |
| 183 | want: &layers.Ethernet{}, |
| 184 | }, |
| 185 | } |
| 186 | for _, tt := range tests { |
| 187 | t.Run(tt.name, func(t *testing.T) { |
| 188 | got := IgmpUsEthLayer(tt.args.mcip) |
| 189 | assert.NotNil(t, got) |
| 190 | }) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | func TestIgmpUsDot1qLayer(t *testing.T) { |
| 195 | type args struct { |
| 196 | vlan of.VlanType |
| 197 | priority uint8 |
| 198 | } |
| 199 | tests := []struct { |
| 200 | name string |
| 201 | args args |
| 202 | want *layers.Dot1Q |
| 203 | }{ |
| 204 | { |
| 205 | name: "IgmpUsDot1qLayer", |
| 206 | args: args{ |
| 207 | vlan: of.VlanAny, |
| 208 | priority: 0, |
| 209 | }, |
| 210 | want: &layers.Dot1Q{}, |
| 211 | }, |
| 212 | { |
| 213 | name: "IgmpDsDot1qLayer", |
| 214 | args: args{ |
| 215 | vlan: of.VlanAny, |
| 216 | priority: 0, |
| 217 | }, |
| 218 | want: &layers.Dot1Q{}, |
| 219 | }, |
| 220 | } |
| 221 | for _, tt := range tests { |
| 222 | t.Run(tt.name, func(t *testing.T) { |
| 223 | switch tt.name { |
| 224 | case "IgmpUsDot1qLayer": |
| 225 | got := IgmpUsDot1qLayer(tt.args.vlan, tt.args.priority) |
| 226 | assert.NotNil(t, got) |
| 227 | case "IgmpDsDot1qLayer": |
| 228 | got := IgmpDsDot1qLayer(tt.args.vlan, tt.args.priority) |
| 229 | assert.NotNil(t, got) |
| 230 | } |
| 231 | }) |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | func TestIgmpv2UsIpv4Layer(t *testing.T) { |
| 236 | type args struct { |
| 237 | src net.IP |
| 238 | mcip net.IP |
| 239 | } |
| 240 | tests := []struct { |
| 241 | name string |
| 242 | args args |
| 243 | want *layers.IPv4 |
| 244 | }{ |
| 245 | { |
| 246 | name: "Igmpv2UsIpv4Layer", |
| 247 | args: args{ |
| 248 | src: AllSystemsMulticastGroupIP, |
| 249 | mcip: AllSystemsMulticastGroupIP, |
| 250 | }, |
| 251 | want: &layers.IPv4{}, |
| 252 | }, |
| 253 | { |
| 254 | name: "IgmpDsIpv4Layer", |
| 255 | args: args{ |
| 256 | src: AllSystemsMulticastGroupIP, |
| 257 | mcip: net.ParseIP("0.0.0.0"), |
| 258 | }, |
| 259 | want: &layers.IPv4{}, |
| 260 | }, |
| 261 | } |
| 262 | for _, tt := range tests { |
| 263 | t.Run(tt.name, func(t *testing.T) { |
| 264 | switch tt.name { |
| 265 | case "Igmpv2UsIpv4Layer": |
| 266 | got := Igmpv2UsIpv4Layer(tt.args.src, tt.args.mcip) |
| 267 | assert.NotNil(t, got) |
| 268 | case "IgmpDsIpv4Layer": |
| 269 | got := IgmpDsIpv4Layer(tt.args.src, tt.args.mcip) |
| 270 | assert.NotNil(t, got) |
| 271 | } |
| 272 | }) |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | func TestIgmpv3UsIpv4Layer(t *testing.T) { |
| 277 | type args struct { |
| 278 | src net.IP |
| 279 | } |
| 280 | tests := []struct { |
| 281 | name string |
| 282 | args args |
| 283 | want *layers.IPv4 |
| 284 | }{ |
| 285 | { |
| 286 | name: "Igmpv3UsIpv4Layer", |
| 287 | args: args{ |
| 288 | src: AllSystemsMulticastGroupIP, |
| 289 | }, |
| 290 | want: &layers.IPv4{}, |
| 291 | }, |
| 292 | } |
| 293 | for _, tt := range tests { |
| 294 | t.Run(tt.name, func(t *testing.T) { |
| 295 | got := Igmpv3UsIpv4Layer(tt.args.src) |
| 296 | assert.NotNil(t, got) |
| 297 | }) |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | func TestIgmpDsEthLayer(t *testing.T) { |
| 302 | type args struct { |
| 303 | mcip net.IP |
| 304 | } |
| 305 | tests := []struct { |
| 306 | name string |
| 307 | args args |
| 308 | want *layers.Ethernet |
| 309 | }{ |
| 310 | { |
| 311 | name: "IgmpDsEthLayer", |
| 312 | args: args{ |
| 313 | mcip: AllSystemsMulticastGroupIP, |
| 314 | }, |
| 315 | want: &layers.Ethernet{}, |
| 316 | }, |
| 317 | } |
| 318 | for _, tt := range tests { |
| 319 | t.Run(tt.name, func(t *testing.T) { |
| 320 | got := IgmpDsEthLayer(tt.args.mcip) |
| 321 | assert.NotNil(t, got) |
| 322 | }) |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | func TestIgmpQueryv2Layer(t *testing.T) { |
| 327 | type args struct { |
| 328 | mcip net.IP |
| 329 | resptime time.Duration |
| 330 | } |
| 331 | tests := []struct { |
| 332 | name string |
| 333 | args args |
| 334 | want *layers.IGMPv1or2 |
| 335 | }{ |
| 336 | { |
| 337 | name: "IgmpQueryv2Laye", |
| 338 | args: args{ |
| 339 | mcip: AllSystemsMulticastGroupIP, |
| 340 | resptime: time.Microsecond, |
| 341 | }, |
| 342 | want: &layers.IGMPv1or2{}, |
| 343 | }, |
| 344 | } |
| 345 | for _, tt := range tests { |
| 346 | t.Run(tt.name, func(t *testing.T) { |
| 347 | got := IgmpQueryv2Layer(tt.args.mcip, tt.args.resptime) |
| 348 | assert.NotNil(t, got) |
| 349 | }) |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | func TestIgmpQueryv3Layer(t *testing.T) { |
| 354 | type args struct { |
| 355 | mcip net.IP |
| 356 | resptime time.Duration |
| 357 | } |
| 358 | tests := []struct { |
| 359 | name string |
| 360 | args args |
| 361 | want *layers.IGMP |
| 362 | }{ |
| 363 | { |
| 364 | name: "IgmpQueryv3Layer", |
| 365 | args: args{ |
| 366 | mcip: AllSystemsMulticastGroupIP, |
| 367 | resptime: time.Microsecond, |
| 368 | }, |
| 369 | want: &layers.IGMP{}, |
| 370 | }, |
| 371 | } |
| 372 | for _, tt := range tests { |
| 373 | t.Run(tt.name, func(t *testing.T) { |
| 374 | got := IgmpQueryv3Layer(tt.args.mcip, tt.args.resptime) |
| 375 | assert.NotNil(t, got) |
| 376 | }) |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | func TestIgmpReportv2Layer(t *testing.T) { |
| 381 | type args struct { |
| 382 | mcip net.IP |
| 383 | } |
| 384 | tests := []struct { |
| 385 | name string |
| 386 | args args |
| 387 | want *layers.IGMPv1or2 |
| 388 | }{ |
| 389 | { |
| 390 | name: "IgmpReportv2Layer", |
| 391 | args: args{ |
| 392 | mcip: AllSystemsMulticastGroupIP, |
| 393 | }, |
| 394 | want: &layers.IGMPv1or2{}, |
| 395 | }, |
| 396 | { |
| 397 | name: "IgmpLeavev2Layer", |
| 398 | args: args{ |
| 399 | mcip: AllSystemsMulticastGroupIP, |
| 400 | }, |
| 401 | want: &layers.IGMPv1or2{}, |
| 402 | }, |
| 403 | } |
| 404 | for _, tt := range tests { |
| 405 | t.Run(tt.name, func(t *testing.T) { |
| 406 | switch tt.name { |
| 407 | case "IgmpReportv2Layer": |
| 408 | got := IgmpReportv2Layer(tt.args.mcip) |
| 409 | assert.NotNil(t, got) |
| 410 | case "IgmpLeavev2Layer": |
| 411 | got := IgmpLeavev2Layer(tt.args.mcip) |
| 412 | assert.NotNil(t, got) |
| 413 | } |
| 414 | }) |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | func TestIgmpReportv3Layer(t *testing.T) { |
| 419 | type args struct { |
| 420 | mcip net.IP |
| 421 | incl bool |
| 422 | srclist []net.IP |
| 423 | } |
| 424 | tests := []struct { |
| 425 | name string |
| 426 | args args |
| 427 | want *layers.IGMP |
| 428 | }{ |
| 429 | { |
| 430 | name: "IgmpReportv3Layer", |
| 431 | args: args{ |
| 432 | mcip: AllSystemsMulticastGroupIP, |
| 433 | incl: true, |
| 434 | }, |
| 435 | want: &layers.IGMP{}, |
| 436 | }, |
| 437 | } |
| 438 | for _, tt := range tests { |
| 439 | t.Run(tt.name, func(t *testing.T) { |
| 440 | got := IgmpReportv3Layer(tt.args.mcip, tt.args.incl, tt.args.srclist) |
| 441 | assert.NotNil(t, got) |
| 442 | }) |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | //func TestIgmpv2QueryPacket(t *testing.T) { |
| 447 | // type args struct { |
| 448 | // mcip net.IP |
| 449 | // vlan of.VlanType |
| 450 | // selfip net.IP |
| 451 | // pbit uint8 |
| 452 | // maxResp uint32 |
| 453 | // } |
| 454 | // tests := []struct { |
| 455 | // name string |
| 456 | // args args |
| 457 | // want []byte |
| 458 | // wantErr bool |
| 459 | // }{ |
| 460 | // { |
| 461 | // name: "IgmpReportv3Layer", |
| 462 | // args: args{ |
| 463 | // vlan: 22, |
| 464 | // selfip: net.ParseIP("224.0.0.1"), |
| 465 | // pbit: 0, |
| 466 | // maxResp: 1, |
| 467 | // mcip: net.ParseIP("0.0.0.0"), |
| 468 | // }, |
| 469 | // wantErr: true, |
| 470 | // }, |
| 471 | // } |
| 472 | // for _, tt := range tests { |
| 473 | // t.Run(tt.name, func(t *testing.T) { |
| 474 | // got, err := Igmpv2QueryPacket(tt.args.mcip, tt.args.vlan, tt.args.selfip, tt.args.pbit, tt.args.maxResp) |
| 475 | // if (err != nil) != tt.wantErr { |
| 476 | // t.Errorf("Igmpv2QueryPacket() error = %v, wantErr %v", err, tt.wantErr) |
| 477 | // return |
| 478 | // } |
| 479 | // if !reflect.DeepEqual(got, tt.want) { |
| 480 | // t.Errorf("Igmpv2QueryPacket() = %v, want %v", got, tt.want) |
| 481 | // } |
| 482 | // }) |
| 483 | // } |
| 484 | // } |
| 485 | |
| 486 | func Test_getVersion(t *testing.T) { |
| 487 | type args struct { |
| 488 | ver string |
| 489 | } |
| 490 | tests := []struct { |
| 491 | name string |
| 492 | args args |
| 493 | want uint8 |
| 494 | }{ |
| 495 | { |
| 496 | name: "getVersion_IgmpVersion2", |
| 497 | args: args{ |
| 498 | ver: "2", |
| 499 | }, |
| 500 | want: IgmpVersion2, |
| 501 | }, |
| 502 | { |
| 503 | name: "getVersion_IgmpVersion2", |
| 504 | args: args{ |
| 505 | ver: "0", |
| 506 | }, |
| 507 | want: IgmpVersion3, |
| 508 | }, |
| 509 | } |
| 510 | for _, tt := range tests { |
| 511 | t.Run(tt.name, func(t *testing.T) { |
| 512 | if got := getVersion(tt.args.ver); got != tt.want { |
| 513 | t.Errorf("getVersion() = %v, want %v", got, tt.want) |
| 514 | } |
| 515 | }) |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | func TestIsIPPresent(t *testing.T) { |
| 520 | type args struct { |
| 521 | i net.IP |
| 522 | ips []net.IP |
| 523 | } |
| 524 | ips := []net.IP{} |
| 525 | ips = append(ips, AllSystemsMulticastGroupIP) |
| 526 | tests := []struct { |
| 527 | name string |
| 528 | args args |
| 529 | want bool |
| 530 | }{ |
| 531 | { |
| 532 | name: "TestIsIPPresent_True", |
| 533 | args: args{ |
| 534 | i: AllSystemsMulticastGroupIP, |
| 535 | ips: ips, |
| 536 | }, |
| 537 | want: true, |
| 538 | }, |
| 539 | { |
| 540 | name: "TestIsIPPresent_False", |
| 541 | args: args{ |
| 542 | ips: ips, |
| 543 | }, |
| 544 | want: false, |
| 545 | }, |
| 546 | } |
| 547 | for _, tt := range tests { |
| 548 | t.Run(tt.name, func(t *testing.T) { |
| 549 | if got := IsIPPresent(tt.args.i, tt.args.ips); got != tt.want { |
| 550 | t.Errorf("IsIPPresent() = %v, want %v", got, tt.want) |
| 551 | } |
| 552 | }) |
| 553 | } |
| 554 | } |