Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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.onosproject.ospf.protocol.lsa.types; |
| 17 | |
| 18 | import org.jboss.netty.buffer.ChannelBuffer; |
| 19 | import org.jboss.netty.buffer.ChannelBuffers; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onosproject.ospf.controller.OspfLsaType; |
| 25 | import org.onosproject.ospf.protocol.lsa.LsaHeader; |
| 26 | |
| 27 | |
| 28 | import java.net.InetAddress; |
| 29 | import java.util.Vector; |
| 30 | |
| 31 | import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | import static org.hamcrest.Matchers.is; |
| 33 | import static org.hamcrest.Matchers.notNullValue; |
| 34 | |
| 35 | /** |
| 36 | * Unit test class for NetworkLsa. |
| 37 | */ |
| 38 | public class NetworkLsaTest { |
| 39 | |
| 40 | private Vector<String> attachedRouters = new Vector(); |
| 41 | private NetworkLsa networkLsa; |
| 42 | private Ip4Address result; |
| 43 | private Ip4Address inetAddres; |
| 44 | private byte[] inputByteArray; |
| 45 | private LsaHeader lsaHeader; |
| 46 | private ChannelBuffer channelBuffer; |
| 47 | private byte[] result1; |
| 48 | private OspfLsaType ospflsaType; |
| 49 | private int result2; |
| 50 | |
| 51 | @Before |
| 52 | public void setUp() throws Exception { |
| 53 | networkLsa = new NetworkLsa(); |
| 54 | } |
| 55 | |
| 56 | @After |
| 57 | public void tearDown() throws Exception { |
| 58 | networkLsa = null; |
| 59 | attachedRouters = null; |
| 60 | result = null; |
| 61 | inetAddres = null; |
| 62 | inputByteArray = null; |
| 63 | lsaHeader = null; |
| 64 | channelBuffer = null; |
| 65 | result1 = null; |
| 66 | ospflsaType = null; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Tests networkMask() getter method. |
| 71 | */ |
| 72 | @Test |
| 73 | public void testGetNetworkMask() throws Exception { |
| 74 | networkLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.164")); |
| 75 | result = networkLsa.networkMask(); |
| 76 | assertThat(result, is(Ip4Address.valueOf("10.226.165.164"))); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Tests networkMask() setter method. |
| 81 | */ |
| 82 | @Test |
| 83 | public void testSetNetworkMask() throws Exception { |
| 84 | networkLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.165")); |
| 85 | result = networkLsa.networkMask(); |
| 86 | result = networkLsa.networkMask(); |
| 87 | assertThat(result, is(Ip4Address.valueOf("10.226.165.165"))); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Tests addAttachedRouter() getter method. |
| 92 | */ |
| 93 | @Test |
| 94 | public void testGetAttachedRouters() throws Exception { |
| 95 | attachedRouters.add("1.1.1.1"); |
| 96 | networkLsa.addAttachedRouter(Ip4Address.valueOf("1.1.1.1")); |
| 97 | assertThat(attachedRouters, is(notNullValue())); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Tests addAttachedRouter() setter method. |
| 102 | */ |
| 103 | @Test |
| 104 | public void testSetAttachedRouters() throws Exception { |
| 105 | attachedRouters.add("1.1.1.1"); |
| 106 | networkLsa.addAttachedRouter(Ip4Address.valueOf("1.1.1.1")); |
| 107 | assertThat(attachedRouters, is(notNullValue())); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Tests addAttachedRouter() method. |
| 112 | */ |
| 113 | @Test |
| 114 | public void testAddAttachedRouter() throws Exception { |
| 115 | inetAddres = Ip4Address.valueOf(InetAddress.getLocalHost()); |
| 116 | networkLsa.addAttachedRouter(inetAddres); |
| 117 | inetAddres = Ip4Address.valueOf(InetAddress.getLocalHost()); |
| 118 | networkLsa.addAttachedRouter(inetAddres); |
| 119 | assertThat(networkLsa, is(notNullValue())); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Tests readFrom() method. |
| 124 | */ |
| 125 | |
| 126 | @Test |
| 127 | public void testReadFrom() throws Exception { |
| 128 | inputByteArray = createByteForNetworkLsa(); |
| 129 | lsaHeader = createLsaHeader(); |
| 130 | networkLsa = new NetworkLsa(lsaHeader); |
| 131 | channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray); |
| 132 | networkLsa.readFrom(channelBuffer); |
| 133 | assertThat(networkLsa, is(notNullValue())); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Tests readFrom() method. |
| 138 | */ |
| 139 | @Test(expected = Exception.class) |
| 140 | public void testReadFrom1() throws Exception { |
| 141 | byte[] temp = {0, 0, 0}; |
| 142 | inputByteArray = temp; |
| 143 | lsaHeader = createLsaHeader(); |
| 144 | networkLsa = new NetworkLsa(lsaHeader); |
| 145 | channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray); |
| 146 | networkLsa.readFrom(channelBuffer); |
| 147 | assertThat(networkLsa, is(notNullValue())); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Tests asBytes() method. |
| 152 | */ |
| 153 | @Test(expected = Exception.class) |
| 154 | public void testAsBytes() throws Exception { |
| 155 | result1 = networkLsa.asBytes(); |
| 156 | assertThat(result1, is(notNullValue())); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Tests getLsaBodyAsByteArray() method. |
| 161 | */ |
| 162 | @Test(expected = Exception.class) |
| 163 | public void testGetLsaBodyAsByteArray() throws Exception { |
| 164 | networkLsa.addAttachedRouter(Ip4Address.valueOf("1.1.1.1")); |
| 165 | networkLsa.addAttachedRouter(Ip4Address.valueOf("2.2.2.2")); |
| 166 | networkLsa.addAttachedRouter(Ip4Address.valueOf("3.3.3.3")); |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame^] | 167 | result1 = networkLsa.getLsaBodyAsByteArray(); |
Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 168 | assertThat(result1, is(notNullValue())); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Tests getLsaBodyAsByteArray() method. |
| 173 | */ |
| 174 | @Test |
| 175 | public void testGetLsaBodyAsByteArray1() throws Exception { |
| 176 | networkLsa.setNetworkMask(Ip4Address.valueOf("255.255.255.255")); |
| 177 | networkLsa.addAttachedRouter(Ip4Address.valueOf("1.1.1.1")); |
| 178 | networkLsa.addAttachedRouter(Ip4Address.valueOf("2.2.2.2")); |
| 179 | networkLsa.addAttachedRouter(Ip4Address.valueOf("3.3.3.3")); |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame^] | 180 | result1 = networkLsa.getLsaBodyAsByteArray(); |
Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 181 | assertThat(result1, is(notNullValue())); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Tests getOspfLsaType() getter method. |
| 186 | */ |
| 187 | @Test |
| 188 | public void testGetOspfLsaType() throws Exception { |
| 189 | networkLsa.setLsType(2); |
| 190 | ospflsaType = networkLsa.getOspfLsaType(); |
| 191 | assertThat(ospflsaType, is(OspfLsaType.NETWORK)); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Tests hashCode() method. |
| 196 | */ |
| 197 | @Test |
| 198 | public void testHashcode() throws Exception { |
| 199 | |
| 200 | result2 = networkLsa.hashCode(); |
| 201 | assertThat(result2, is(notNullValue())); |
| 202 | |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Utility method used by junit methods. |
| 207 | */ |
| 208 | private byte[] createByteForNetworkLsa() { |
| 209 | byte[] packet = {2, 1, 1, 52, -64, -88, 56, 1, -64, -88, 56, 1, 0, 100, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, -64, |
| 210 | -88, 56, 1, 0, 10, 1, 1, 0, 0, 0, 40, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, |
| 211 | 1}; |
| 212 | return packet; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Utility method used by junit methods. |
| 217 | */ |
| 218 | private LsaHeader createLsaHeader() { |
| 219 | lsaHeader = new LsaHeader(); |
| 220 | lsaHeader.setLsType(2); |
| 221 | lsaHeader.setLsPacketLen(48); |
| 222 | lsaHeader.setLsCheckSum(10); |
| 223 | lsaHeader.setAge(4); |
| 224 | lsaHeader.setLinkStateId("10.226.165.164"); |
| 225 | lsaHeader.setLsSequenceNo(250); |
| 226 | lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); |
| 227 | lsaHeader.setOptions(2); |
| 228 | return lsaHeader; |
| 229 | } |
| 230 | } |