Mani | cdb2641 | 2016-02-16 19:44:34 +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.ospfpacket.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.protocol.ospfpacket.OspfPacketHeader; |
| 25 | import org.onosproject.ospf.protocol.util.OspfPacketType; |
| 26 | |
| 27 | import java.util.Vector; |
| 28 | |
| 29 | import static org.hamcrest.CoreMatchers.is; |
| 30 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 31 | import static org.junit.Assert.assertThat; |
| 32 | |
| 33 | /** |
| 34 | * Unit test class for HelloPacket. |
| 35 | */ |
| 36 | public class HelloPacketTest { |
| 37 | |
| 38 | private boolean result1; |
| 39 | private OspfPacketType ospfPacketType; |
| 40 | private OspfPacketHeader ospfPacketHeader; |
| 41 | private HelloPacket helloPacket; |
| 42 | private Vector<String> neighborAddress = new Vector(); |
| 43 | private Ip4Address result; |
| 44 | private int result2; |
| 45 | private byte[] packet; |
| 46 | private ChannelBuffer channelBuffer; |
| 47 | private byte[] result3; |
| 48 | |
| 49 | @Before |
| 50 | public void setUp() throws Exception { |
| 51 | helloPacket = new HelloPacket(); |
| 52 | helloPacket.setAuthType(1); |
| 53 | helloPacket.setOspftype(2); |
| 54 | helloPacket.setRouterId(Ip4Address.valueOf("10.226.165.164")); |
| 55 | helloPacket.setAreaId(Ip4Address.valueOf("10.226.165.100")); |
| 56 | helloPacket.setChecksum(201); |
| 57 | helloPacket.setAuthentication(2); |
| 58 | helloPacket.setOspfPacLength(48); |
| 59 | helloPacket.setOspfVer(2); |
| 60 | |
| 61 | } |
| 62 | |
| 63 | @After |
| 64 | public void tearDown() throws Exception { |
| 65 | helloPacket = null; |
| 66 | result = null; |
| 67 | ospfPacketType = null; |
| 68 | ospfPacketHeader = null; |
| 69 | packet = null; |
| 70 | channelBuffer = null; |
| 71 | result3 = null; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Tests networkMask() getter method. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testGetNetworkMask() throws Exception { |
| 79 | helloPacket.setNetworkMask(Ip4Address.valueOf("10.226.165.164")); |
| 80 | result = helloPacket.networkMask(); |
| 81 | assertThat(result, is(notNullValue())); |
| 82 | assertThat(result, is(Ip4Address.valueOf("10.226.165.164"))); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Tests networkMask() setter method. |
| 87 | */ |
| 88 | @Test |
| 89 | public void testSetNetworkMask() throws Exception { |
| 90 | helloPacket.setNetworkMask(Ip4Address.valueOf("10.226.165.164")); |
| 91 | result = helloPacket.networkMask(); |
| 92 | assertThat(result, is(notNullValue())); |
| 93 | assertThat(result, is(Ip4Address.valueOf("10.226.165.164"))); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests bdr() setter method. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testSetBdr() throws Exception { |
| 101 | helloPacket.setBdr(Ip4Address.valueOf("10.226.165.166")); |
| 102 | result = helloPacket.bdr(); |
| 103 | assertThat(result, is(notNullValue())); |
| 104 | assertThat(result, is(Ip4Address.valueOf("10.226.165.166"))); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Tests dr() getter method. |
| 109 | */ |
| 110 | @Test |
| 111 | public void testGetDr() throws Exception { |
| 112 | helloPacket.setDr(Ip4Address.valueOf("10.226.165.167")); |
| 113 | result = helloPacket.dr(); |
| 114 | assertThat(result, is(notNullValue())); |
| 115 | assertThat(result, is(Ip4Address.valueOf("10.226.165.167"))); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Tests dr() setter method. |
| 120 | */ |
| 121 | @Test |
| 122 | public void testSetDr() throws Exception { |
| 123 | helloPacket.setDr(Ip4Address.valueOf("10.226.165.167")); |
| 124 | result = helloPacket.dr(); |
| 125 | assertThat(result, is(notNullValue())); |
| 126 | assertThat(result, is(Ip4Address.valueOf("10.226.165.167"))); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Tests addNeighbor() method. |
| 131 | */ |
| 132 | @Test |
| 133 | public void testAddNeighbor() throws Exception { |
| 134 | helloPacket.addNeighbor(Ip4Address.valueOf("10.226.165.170")); |
| 135 | result1 = helloPacket.containsNeighbour(Ip4Address.valueOf("10.226.165.170")); |
| 136 | assertThat(result1, is(true)); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Tests containsNeighbour() method. |
| 141 | */ |
| 142 | @Test |
| 143 | public void testContainsNeighbour() throws Exception { |
| 144 | helloPacket.addNeighbor(Ip4Address.valueOf("10.226.165.200")); |
| 145 | result1 = helloPacket.containsNeighbour(Ip4Address.valueOf("10.226.165.200")); |
| 146 | assertThat(result1, is(true)); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * Tests options() getter method. |
| 152 | */ |
| 153 | @Test |
| 154 | public void testGetOptions() throws Exception { |
| 155 | helloPacket.setOptions(10); |
| 156 | result2 = helloPacket.options(); |
| 157 | assertThat(result2, is(notNullValue())); |
| 158 | assertThat(result2, is(10)); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Tests options() setter method. |
| 163 | */ |
| 164 | @Test |
| 165 | public void testSetOptions() throws Exception { |
| 166 | helloPacket.setOptions(11); |
| 167 | result2 = helloPacket.options(); |
| 168 | assertThat(result2, is(notNullValue())); |
| 169 | assertThat(result2, is(11)); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Tests routerPriority() getter method. |
| 174 | */ |
| 175 | @Test |
| 176 | public void testGetRouterPriority() throws Exception { |
| 177 | helloPacket.setRouterPriority(1); |
| 178 | result2 = helloPacket.routerPriority(); |
| 179 | assertThat(result2, is(notNullValue())); |
| 180 | assertThat(result2, is(1)); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Tests routerPriority() setter method. |
| 185 | */ |
| 186 | @Test |
| 187 | public void testSetRouterPriority() throws Exception { |
| 188 | helloPacket.setRouterPriority(2); |
| 189 | result2 = helloPacket.routerPriority(); |
| 190 | assertThat(result2, is(notNullValue())); |
| 191 | assertThat(result2, is(2)); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Tests helloInterval() getter method. |
| 196 | */ |
| 197 | @Test |
| 198 | public void testGetHelloInterval() throws Exception { |
| 199 | helloPacket.setHelloInterval(10); |
| 200 | result2 = helloPacket.helloInterval(); |
| 201 | assertThat(result2, is(notNullValue())); |
| 202 | assertThat(result2, is(10)); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Tests helloInterval() setter method. |
| 207 | */ |
| 208 | @Test |
| 209 | public void testSetHelloInterval() throws Exception { |
| 210 | helloPacket.setHelloInterval(10); |
| 211 | result2 = helloPacket.helloInterval(); |
| 212 | assertThat(result2, is(notNullValue())); |
| 213 | assertThat(result2, is(10)); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Tests routerDeadInterval() getter method. |
| 218 | */ |
| 219 | @Test |
| 220 | public void testGetRouterDeadInterval() throws Exception { |
| 221 | helloPacket.setRouterDeadInterval(50); |
| 222 | result2 = helloPacket.routerDeadInterval(); |
| 223 | assertThat(result2, is(notNullValue())); |
| 224 | assertThat(result2, is(50)); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Tests routerDeadInterval() setter method. |
| 229 | */ |
| 230 | @Test |
| 231 | public void testSetRouterDeadInterval() throws Exception { |
| 232 | helloPacket.setRouterDeadInterval(50); |
| 233 | result2 = helloPacket.routerDeadInterval(); |
| 234 | assertThat(result2, is(notNullValue())); |
| 235 | assertThat(result2, is(50)); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Tests ospfMessageType() getter method. |
| 240 | */ |
| 241 | @Test |
| 242 | public void testGetOspfMessageType() throws Exception { |
| 243 | ospfPacketType = helloPacket.ospfMessageType(); |
| 244 | assertThat(ospfPacketType, is(notNullValue())); |
| 245 | assertThat(ospfPacketType, is(OspfPacketType.HELLO)); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Tests readFrom() method. |
| 250 | */ |
| 251 | @Test |
| 252 | public void testReadFrom() throws Exception { |
| 253 | ospfPacketHeader = new OspfPacketHeader(); |
| 254 | ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1")); |
| 255 | ospfPacketHeader.setAuthentication(0); |
| 256 | ospfPacketHeader.setAuthType(0); |
| 257 | ospfPacketHeader.setChecksum(12345); |
| 258 | ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10")); |
| 259 | ospfPacketHeader.setOspfPacLength(56); |
| 260 | ospfPacketHeader.setOspftype(1); |
| 261 | ospfPacketHeader.setOspfVer(2); |
| 262 | ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2")); |
| 263 | ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3")); |
| 264 | packet = createByteForHelloPacket(); |
| 265 | channelBuffer = ChannelBuffers.copiedBuffer(packet); |
| 266 | helloPacket.readFrom(channelBuffer); |
| 267 | assertThat(helloPacket, is(notNullValue())); |
| 268 | assertThat(helloPacket.ospfMessageType(), is(OspfPacketType.HELLO)); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Tests asBytes() method. |
| 273 | */ |
| 274 | @Test |
| 275 | public void testAsBytes() throws Exception { |
| 276 | result3 = helloPacket.asBytes(); |
| 277 | assertThat(result3, is(notNullValue())); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Tests getHelloHeaderAsByteArray() method. |
| 282 | */ |
| 283 | @Test |
| 284 | public void testGetHelloHeaderAsByteArray() throws Exception { |
| 285 | result3 = helloPacket.getHelloHeaderAsByteArray(); |
| 286 | assertThat(result3, is(notNullValue())); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Tests getHelloBodyAsByteArray() method. |
| 291 | */ |
| 292 | @Test |
| 293 | public void testGetHelloBodyAsByteArray() throws Exception { |
| 294 | neighborAddress.add("10.226.165.100"); |
| 295 | result3 = helloPacket.getHelloBodyAsByteArray(); |
| 296 | assertThat(result3, is(notNullValue())); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Tests getHelloBodyAsByteArray() method. |
| 301 | */ |
| 302 | @Test |
| 303 | public void testReadHelloBody() throws Exception { |
| 304 | helloPacket.getHelloBodyAsByteArray(); |
| 305 | assertThat(helloPacket, is(notNullValue())); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Tests to string method. |
| 310 | */ |
| 311 | @Test |
| 312 | public void testToString() throws Exception { |
| 313 | assertThat(helloPacket.toString(), is(notNullValue())); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Utility method used by junit methods. |
| 318 | */ |
| 319 | private byte[] createByteForHelloPacket() { |
| 320 | byte[] helloPacket = {2, 1, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, 39, 59, 0, 0, 0, 0, |
| 321 | 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 10, 2, 1, 0, 0, 0, 40, -64, -88, -86, 8, 0, 0, 0, 0}; |
| 322 | |
| 323 | return helloPacket; |
| 324 | } |
| 325 | } |