Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +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.util; |
| 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.Ip4Address; |
| 22 | import org.onosproject.ospf.protocol.lsa.LsaHeader; |
| 23 | import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader; |
| 24 | import org.onosproject.ospf.protocol.lsa.types.AsbrSummaryLsa; |
| 25 | import org.onosproject.ospf.protocol.lsa.types.ExternalLsa; |
| 26 | import org.onosproject.ospf.protocol.lsa.types.NetworkLsa; |
| 27 | import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10; |
| 28 | import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa11; |
| 29 | import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa9; |
| 30 | import org.onosproject.ospf.protocol.lsa.types.RouterLsa; |
| 31 | import org.onosproject.ospf.protocol.lsa.types.SummaryLsa; |
| 32 | import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket; |
| 33 | import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket; |
| 34 | import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge; |
| 35 | import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest; |
| 36 | import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate; |
| 37 | |
| 38 | import static org.hamcrest.CoreMatchers.is; |
| 39 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 40 | import static org.hamcrest.MatcherAssert.assertThat; |
| 41 | |
| 42 | /** |
| 43 | * Unit test class for ChecksumCalculator. |
| 44 | */ |
| 45 | public class ChecksumCalculatorTest { |
| 46 | |
| 47 | private final int ospfChecksumPos1 = 12; |
| 48 | private final int ospfChecksumPos2 = 13; |
| 49 | private final int lsaChecksumPos1 = 16; |
| 50 | private final int lsaChecksumPos2 = 17; |
| 51 | private final byte[] updatePacket = {1, 1, 1, 1, 2, 4, 0, -96, 9, 9, 9, 9, 5, 5, 5, 5, 62, 125, |
| 52 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 100, 2, 10, 1, 0, 0, 1, 9, 9, 9, 9, -128, |
| 53 | 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64, -88, |
| 54 | 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4, 0, 0, 0, 1, |
| 55 | 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0, 0, 0, 0, |
| 56 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, |
| 57 | 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1}; |
| 58 | |
| 59 | private final byte[] helloPacket = {2, 1, 0, 44, -64, -88, -86, 8, 0, 0, 0, 1, 39, 59, |
| 60 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 10, 2, 1, 0, 0, 0, 40, -64, -88, |
| 61 | -86, 8, 0, 0, 0, 0}; |
| 62 | private final byte[] rlsa = {14, 16, 2, 1, -64, -88, -86, 2, -64, -88, -86, 2, -128, 0, |
| 63 | 0, 1, 74, -114, 0, 48, 2, 0, 0, 2, -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10, |
| 64 | -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10}; |
| 65 | private ChecksumCalculator checksumCalculator; |
| 66 | private boolean validate; |
| 67 | private HelloPacket hello; |
| 68 | private LsUpdate message; |
| 69 | private DdPacket message1; |
| 70 | private LsRequest message2; |
| 71 | private RouterLsa router; |
| 72 | private LsAcknowledge lsack; |
| 73 | private ExternalLsa external; |
| 74 | private NetworkLsa external1; |
| 75 | private SummaryLsa external2; |
| 76 | private AsbrSummaryLsa external3; |
| 77 | private OpaqueLsa9 external4; |
| 78 | private OpaqueLsa10 external5; |
| 79 | private OpaqueLsa11 external6; |
| 80 | private byte[] result; |
| 81 | |
| 82 | @Before |
| 83 | public void setUp() throws Exception { |
| 84 | checksumCalculator = new ChecksumCalculator(); |
| 85 | } |
| 86 | |
| 87 | @After |
| 88 | public void tearDown() throws Exception { |
| 89 | checksumCalculator = null; |
| 90 | hello = null; |
| 91 | message = null; |
| 92 | message1 = null; |
| 93 | message2 = null; |
| 94 | router = null; |
| 95 | lsack = null; |
| 96 | external = null; |
| 97 | external1 = null; |
| 98 | external2 = null; |
| 99 | external3 = null; |
| 100 | result = null; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Tests convertToSixteenBits() method. |
| 105 | */ |
| 106 | @Test |
| 107 | public void testConvertToSixteenBits() throws Exception { |
| 108 | int num = checksumCalculator.convertToSixteenBits("16cdd"); |
| 109 | assertThat(num, is(27870)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Tests isValidOspfCheckSum() method. |
| 114 | */ |
| 115 | @Test |
| 116 | public void testIsValidOspfCheckSum() throws Exception { |
| 117 | hello = new HelloPacket(); |
| 118 | hello.setOspfVer(2); |
| 119 | hello.setOspftype(1); |
| 120 | hello.setOspfPacLength(172); |
| 121 | hello.setRouterId(Ip4Address.valueOf("192.168.170.3")); |
| 122 | hello.setAreaId(Ip4Address.valueOf("0.0.0.1")); |
| 123 | hello.setChecksum(5537); |
| 124 | hello.setAuthType(0); |
| 125 | hello.setAuthentication(0); |
| 126 | validate = checksumCalculator.isValidOspfCheckSum(hello, ospfChecksumPos1, ospfChecksumPos2); |
| 127 | assertThat(validate, is(false)); |
| 128 | lsack = new LsAcknowledge(); |
| 129 | lsack.setOspfVer(2); |
| 130 | lsack.setOspftype(5); |
| 131 | lsack.setOspfPacLength(172); |
| 132 | lsack.setRouterId(Ip4Address.valueOf("192.168.170.3")); |
| 133 | lsack.setAreaId(Ip4Address.valueOf("0.0.0.1")); |
| 134 | lsack.setChecksum(37537); |
| 135 | lsack.setAuthType(0); |
| 136 | lsack.setAuthentication(0); |
| 137 | validate = checksumCalculator.isValidOspfCheckSum(lsack, ospfChecksumPos1, ospfChecksumPos2); |
| 138 | assertThat(validate, is(true)); |
| 139 | message = new LsUpdate(); |
| 140 | message.setOspfVer(2); |
| 141 | message.setOspftype(5); |
| 142 | message.setOspfPacLength(172); |
| 143 | message.setRouterId(Ip4Address.valueOf("192.168.170.3")); |
| 144 | message.setAreaId(Ip4Address.valueOf("0.0.0.1")); |
| 145 | message.setChecksum(37537); |
| 146 | message.setAuthType(0); |
| 147 | message.setAuthentication(0); |
| 148 | validate = checksumCalculator.isValidOspfCheckSum(message, ospfChecksumPos1, ospfChecksumPos2); |
| 149 | assertThat(validate, is(true)); |
| 150 | message1 = new DdPacket(); |
| 151 | message1.setOspfVer(2); |
| 152 | message1.setOspftype(5); |
| 153 | message1.setOspfPacLength(172); |
| 154 | message1.setRouterId(Ip4Address.valueOf("192.168.170.3")); |
| 155 | message1.setAreaId(Ip4Address.valueOf("0.0.0.1")); |
| 156 | message1.setChecksum(37537); |
| 157 | message1.setAuthType(0); |
| 158 | message1.setAuthentication(0); |
| 159 | validate = checksumCalculator.isValidOspfCheckSum(message1, ospfChecksumPos1, ospfChecksumPos2); |
| 160 | assertThat(validate, is(true)); |
| 161 | message2 = new LsRequest(); |
| 162 | message2.setOspfVer(2); |
| 163 | message2.setOspftype(5); |
| 164 | message2.setOspfPacLength(172); |
| 165 | message2.setRouterId(Ip4Address.valueOf("192.168.170.3")); |
| 166 | message2.setAreaId(Ip4Address.valueOf("0.0.0.1")); |
| 167 | message2.setChecksum(37537); |
| 168 | message2.setAuthType(0); |
| 169 | message2.setAuthentication(0); |
| 170 | validate = checksumCalculator.isValidOspfCheckSum(message2, ospfChecksumPos1, ospfChecksumPos2); |
| 171 | assertThat(validate, is(true)); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Tests isValidLsaCheckSum() method. |
| 176 | */ |
| 177 | @Test |
| 178 | public void testIsValidLsaCheckSum() throws Exception { |
| 179 | router = new RouterLsa(); |
| 180 | router.setAge(1); |
| 181 | router.setOptions(2); |
| 182 | router.setLsType(1); |
| 183 | router.setLinkStateId("192.168.170.3"); |
| 184 | router.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.3")); |
| 185 | router.setLsSequenceNo(2147483649L); |
| 186 | router.setLsCheckSum(49499); |
| 187 | router.setLsPacketLen(48); |
| 188 | validate = checksumCalculator.isValidLsaCheckSum(router, router.lsType(), lsaChecksumPos1, lsaChecksumPos2); |
| 189 | assertThat(validate, is(true)); |
| 190 | |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Tests isValidLsaCheckSum() method. |
| 195 | */ |
| 196 | @Test |
| 197 | public void testIsValidLsaCheckSum4() throws Exception { |
| 198 | external = new ExternalLsa(new LsaHeader()); |
| 199 | external.setAge(2); |
| 200 | external.setOptions(2); |
| 201 | external.setLsType(5); |
| 202 | external.setLinkStateId("80.212.16.0"); |
| 203 | external.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2")); |
| 204 | external.setLsSequenceNo(2147483649L); |
| 205 | external.setLsCheckSum(25125); |
| 206 | external.setLsPacketLen(36); |
| 207 | validate = checksumCalculator.isValidLsaCheckSum(external, external.lsType(), lsaChecksumPos1, lsaChecksumPos2); |
| 208 | assertThat(validate, is(false)); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Tests isValidLsaCheckSum() method. |
| 213 | */ |
| 214 | @Test(expected = Exception.class) |
| 215 | public void testIsValidLsaCheckSum5() throws Exception { |
| 216 | external1 = new NetworkLsa(); |
| 217 | external1.setAge(2); |
| 218 | external1.setOptions(2); |
| 219 | external1.setLsType(2); |
| 220 | external1.setLinkStateId("80.212.16.0"); |
| 221 | external1.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2")); |
| 222 | external1.setLsSequenceNo(2147483649L); |
| 223 | external1.setLsCheckSum(25125); |
| 224 | external1.setLsPacketLen(36); |
| 225 | validate = checksumCalculator.isValidLsaCheckSum(external1, external1.lsType(), |
| 226 | lsaChecksumPos1, lsaChecksumPos2); |
| 227 | assertThat(validate, is(false)); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Tests isValidLsaCheckSum() method. |
| 232 | */ |
| 233 | @Test |
| 234 | public void testIsValidLsaCheckSum6() throws Exception { |
| 235 | |
| 236 | external2 = new SummaryLsa(new LsaHeader()); |
| 237 | external2.setAge(2); |
| 238 | external2.setOptions(2); |
| 239 | external2.setLsType(3); |
| 240 | external2.setLinkStateId("80.212.16.0"); |
| 241 | external2.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2")); |
| 242 | external2.setLsSequenceNo(2147483649L); |
| 243 | external2.setLsCheckSum(25125); |
| 244 | external2.setLsPacketLen(36); |
| 245 | validate = checksumCalculator.isValidLsaCheckSum(external2, external2.lsType(), |
| 246 | lsaChecksumPos1, lsaChecksumPos2); |
| 247 | assertThat(validate, is(false)); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Tests isValidLsaCheckSum() method. |
| 252 | */ |
| 253 | @Test(expected = Exception.class) |
| 254 | public void testIsValidLsaCheckSum7() throws Exception { |
| 255 | external3 = new AsbrSummaryLsa(new LsaHeader()); |
| 256 | external3.setAge(2); |
| 257 | external3.setOptions(2); |
| 258 | external3.setLsType(4); |
| 259 | external3.setLinkStateId("80.212.16.0"); |
| 260 | external3.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2")); |
| 261 | external3.setLsSequenceNo(2147483649L); |
| 262 | external3.setLsCheckSum(25125); |
| 263 | external3.setLsPacketLen(36); |
| 264 | validate = checksumCalculator.isValidLsaCheckSum(external3, external3.lsType(), |
| 265 | lsaChecksumPos1, lsaChecksumPos2); |
| 266 | assertThat(validate, is(false)); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Tests isValidLsaCheckSum() method. |
| 271 | */ |
| 272 | @Test(expected = Exception.class) |
| 273 | public void testIsValidLsaCheckSum1() throws Exception { |
| 274 | external4 = new OpaqueLsa9(new OpaqueLsaHeader()); |
| 275 | external4.setAge(2); |
| 276 | external4.setOptions(2); |
| 277 | external4.setLsType(9); |
| 278 | external4.setLinkStateId("80.212.16.0"); |
| 279 | external4.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2")); |
| 280 | external4.setLsSequenceNo(2147483649L); |
| 281 | external4.setLsCheckSum(25125); |
| 282 | external4.setLsPacketLen(36); |
| 283 | validate = checksumCalculator.isValidLsaCheckSum(external4, external4.lsType(), |
| 284 | lsaChecksumPos1, lsaChecksumPos2); |
| 285 | assertThat(validate, is(false)); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Tests isValidLsaCheckSum() method. |
| 290 | */ |
| 291 | @Test(expected = Exception.class) |
| 292 | public void testIsValidLsaCheckSum2() throws Exception { |
| 293 | external5 = new OpaqueLsa10(new OpaqueLsaHeader()); |
| 294 | external5.setAge(2); |
| 295 | external5.setOptions(2); |
| 296 | external5.setLsType(10); |
| 297 | external5.setLinkStateId("80.212.16.0"); |
| 298 | external5.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2")); |
| 299 | external5.setLsSequenceNo(2147483649L); |
| 300 | external5.setLsCheckSum(25125); |
| 301 | external5.setLsPacketLen(36); |
| 302 | validate = checksumCalculator.isValidLsaCheckSum(external5, external5.lsType(), |
| 303 | lsaChecksumPos1, lsaChecksumPos2); |
| 304 | |
| 305 | assertThat(validate, is(false)); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Tests isValidLsaCheckSum() method. |
| 310 | */ |
| 311 | @Test(expected = Exception.class) |
| 312 | public void testIsValidLsaCheckSum3() throws Exception { |
| 313 | external6 = new OpaqueLsa11(new OpaqueLsaHeader()); |
| 314 | external6.setAge(2); |
| 315 | external6.setOptions(2); |
| 316 | external6.setLsType(10); |
| 317 | external6.setLinkStateId("80.212.16.0"); |
| 318 | external6.setAdvertisingRouter(Ip4Address.valueOf("192.168.170.2")); |
| 319 | external6.setLsSequenceNo(2147483649L); |
| 320 | external6.setLsCheckSum(25125); |
| 321 | external6.setLsPacketLen(36); |
| 322 | validate = checksumCalculator.isValidLsaCheckSum(external6, external6.lsType(), |
| 323 | lsaChecksumPos1, lsaChecksumPos2); |
| 324 | assertThat(validate, is(false)); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Tests validateLsaCheckSum() method. |
| 329 | */ |
| 330 | @Test |
| 331 | public void testValidateLsaCheckSum() throws Exception { |
| 332 | assertThat(checksumCalculator.validateLsaCheckSum(rlsa, lsaChecksumPos1, |
| 333 | lsaChecksumPos2), is(true)); |
| 334 | |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Tests validateOspfCheckSum() method. |
| 339 | */ |
| 340 | @Test |
| 341 | public void testValidateOspfCheckSum() throws Exception { |
| 342 | assertThat(checksumCalculator.validateOspfCheckSum(helloPacket, ospfChecksumPos1, |
| 343 | ospfChecksumPos2), is(true)); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Tests calculateLsaChecksum() method. |
| 348 | */ |
| 349 | @Test |
| 350 | public void testCalculateLsaChecksum() throws Exception { |
| 351 | result = checksumCalculator.calculateLsaChecksum(rlsa, lsaChecksumPos1, lsaChecksumPos2); |
| 352 | assertThat(result, is(notNullValue())); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Tests calculateOspfCheckSum() method. |
| 357 | */ |
| 358 | @Test |
| 359 | public void testCalculateOspfCheckSum() throws Exception { |
| 360 | result = checksumCalculator.calculateOspfCheckSum(helloPacket, ospfChecksumPos1, |
| 361 | ospfChecksumPos2); |
| 362 | assertThat(result, is(notNullValue())); |
| 363 | result = checksumCalculator.calculateOspfCheckSum(updatePacket, ospfChecksumPos1, |
| 364 | ospfChecksumPos2); |
| 365 | assertThat(result, is(notNullValue())); |
| 366 | } |
| 367 | } |