Vignesh Ethiraj | d1957c9 | 2019-11-18 11:52:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 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 | package org.opencord.igmpproxy; |
| 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.Ethernet; |
| 22 | import org.onosproject.core.CoreServiceAdapter; |
| 23 | import org.onosproject.net.flow.FlowRuleServiceAdapter; |
| 24 | import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter; |
| 25 | |
| 26 | import static org.junit.Assert.*; |
| 27 | |
| 28 | public class IgmpManagerTest extends IgmpManagerBase { |
| 29 | |
| 30 | private static final int WAIT_TIMEOUT = 1000; |
| 31 | |
| 32 | private IgmpManager igmpManager; |
| 33 | |
| 34 | // Set up the IGMP application. |
| 35 | @Before |
| 36 | public void setUp() { |
| 37 | igmpManager = new IgmpManager(); |
| 38 | igmpManager.coreService = new CoreServiceAdapter(); |
| 39 | igmpManager.mastershipService = new MockMastershipService(); |
| 40 | igmpManager.flowObjectiveService = new FlowObjectiveServiceAdapter(); |
| 41 | igmpManager.deviceService = new MockDeviceService(); |
| 42 | igmpManager.packetService = new MockPacketService(); |
| 43 | igmpManager.flowRuleService = new FlowRuleServiceAdapter(); |
| 44 | igmpManager.multicastService = new TestMulticastRouteService(); |
| 45 | // By default - we send query messages |
| 46 | SingleStateMachine.sendQuery = true; |
| 47 | } |
| 48 | |
| 49 | // Tear Down the IGMP application. |
| 50 | @After |
| 51 | public void tearDown() { |
| 52 | igmpManager.deactivate(); |
| 53 | IgmpManager.groupMemberMap.clear(); |
| 54 | StateMachine.clearMap(); |
| 55 | } |
| 56 | |
| 57 | // Checking the Default value of IGMP_ON_POD_BASIS. |
| 58 | @Test |
| 59 | public void testIsIgmpOnPodBasisDefaultValue() { |
| 60 | igmpManager.networkConfig = new TestNetworkConfigRegistry(false); |
| 61 | igmpManager.activate(); |
| 62 | assertFalse(IgmpManager.isIgmpOnPodBasis()); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | // Checking the value of IGMP_ON_POD_BASIS. |
| 67 | @Test |
| 68 | public void testIsIgmpOnPodBasisTrueValue() { |
| 69 | igmpManager.networkConfig = new TestNetworkConfigRegistry(true); |
| 70 | igmpManager.activate(); |
| 71 | assertTrue(IgmpManager.isIgmpOnPodBasis()); |
| 72 | } |
| 73 | |
| 74 | // Testing the flow of packet when isIgmpOnPodBasis value is false. |
| 75 | @Test |
| 76 | public void testIgmpOnPodBasisDefaultValue() throws InterruptedException { |
| 77 | // We need to count join messages sent on the upstream |
| 78 | SingleStateMachine.sendQuery = false; |
| 79 | |
| 80 | igmpManager.networkConfig = new TestNetworkConfigRegistry(false); |
| 81 | igmpManager.activate(); |
| 82 | |
| 83 | Ethernet firstPacket = IgmpSender.getInstance().buildIgmpV3Join(GROUP_IP, SOURCE_IP_OF_A); |
| 84 | Ethernet secondPacket = IgmpSender.getInstance().buildIgmpV3Join(GROUP_IP, SOURCE_IP_OF_B); |
| 85 | // Sending first packet and here shouldSendjoin flag will be true |
| 86 | sendPacket(firstPacket); |
| 87 | // Emitted packet is stored in list savedPackets |
| 88 | assertNotNull(savedPackets); |
| 89 | synchronized (savedPackets) { |
| 90 | savedPackets.wait(WAIT_TIMEOUT); |
| 91 | } |
| 92 | assertNotNull(savedPackets); |
| 93 | assertEquals(1, savedPackets.size()); |
| 94 | // Sending the second packet with same group ip address |
| 95 | sendPacket(secondPacket); |
| 96 | synchronized (savedPackets) { |
| 97 | savedPackets.wait(WAIT_TIMEOUT); |
| 98 | } |
| 99 | // Emitted packet is stored in list savedPackets as shouldSendJoin flag is true |
| 100 | assertEquals(2, savedPackets.size()); |
| 101 | } |
| 102 | |
| 103 | // Testing IGMP_ON_POD_BASIS value by sending two packets. |
| 104 | @Test |
| 105 | public void testIgmpOnPodBasisTrueValue() throws InterruptedException { |
| 106 | // We need to count join messages |
| 107 | SingleStateMachine.sendQuery = false; |
| 108 | |
| 109 | igmpManager.networkConfig = new TestNetworkConfigRegistry(true); |
| 110 | igmpManager.activate(); |
| 111 | |
| 112 | Ethernet firstPacket = IgmpSender.getInstance().buildIgmpV3Join(GROUP_IP, SOURCE_IP_OF_A); |
| 113 | Ethernet secondPacket = IgmpSender.getInstance().buildIgmpV3Join(GROUP_IP, SOURCE_IP_OF_B); |
| 114 | // Sending first packet and here shouldSendjoin flag will be true |
| 115 | sendPacket(firstPacket); |
| 116 | // Emitted packet is stored in list savedPackets |
| 117 | synchronized (savedPackets) { |
| 118 | savedPackets.wait(WAIT_TIMEOUT); |
| 119 | } |
| 120 | assertNotNull(savedPackets); |
| 121 | assertEquals(1, savedPackets.size()); |
| 122 | // Sending the second packet with same group ip address which will not be emitted |
| 123 | // shouldSendJoin flag will be false. |
| 124 | sendPacket(secondPacket); |
| 125 | synchronized (savedPackets) { |
| 126 | savedPackets.wait(WAIT_TIMEOUT); |
| 127 | } |
| 128 | assertEquals(1, savedPackets.size()); |
| 129 | } |
| 130 | |
| 131 | } |