Chidambar babu | 86b3b1a | 2016-02-16 17:39:52 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Chidambar babu | 86b3b1a | 2016-02-16 17:39:52 +0530 | [diff] [blame] | 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.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.lsa.LsaHeader; |
| 25 | |
| 26 | import static org.hamcrest.CoreMatchers.is; |
| 27 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 28 | import static org.junit.Assert.assertThat; |
| 29 | |
| 30 | /** |
| 31 | * Unit test class for OspfUtil. |
| 32 | */ |
| 33 | public class OspfUtilTest { |
| 34 | |
| 35 | private final int ospfChecksumPos1 = 12; |
| 36 | private final int ospfChecksumPos2 = 13; |
| 37 | private final int lsaChecksumPos1 = 16; |
| 38 | private final int lsaChecksumPos2 = 17; |
| 39 | private final int ospfLengthPos1 = 2; |
| 40 | private final int ospfLengthPos2 = 3; |
| 41 | private final int lsaLengthPos1 = 18; |
| 42 | private final int lsaLengthPos2 = 19; |
| 43 | private final byte[] input = {0, 2}; |
| 44 | private final byte[] packet = {2, 1, 0, 52, -64, -88, 56, 1, -64, -88, 56, 1, 0, 100, 0, 100, 0, 0, 0, 0, 0, 0, |
| 45 | 0, 0, -64, -88, 56, 1, 0, 10, 1, 1, 0, 0, 0, 40, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, 1, -64, |
| 46 | -88, 56, 1}; |
| 47 | private final byte[] rLsa = {14, 16, 2, 1, -64, -88, -86, 2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48, 2, |
| 48 | 0, 0, 2, -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10, -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10}; |
| 49 | private final byte[] opaqueheader = {14, 16, 2, 1, -64, -88, -86, 2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, |
| 50 | 0, 48}; |
| 51 | private int num; |
| 52 | private int result; |
| 53 | private int input2; |
| 54 | private long input4; |
| 55 | private ChannelBuffer channelBuffer; |
| 56 | private byte[] result1; |
| 57 | private LsaHeader lsaHeader; |
| 58 | private boolean result2; |
| 59 | private long result3; |
| 60 | |
| 61 | |
| 62 | @Before |
| 63 | public void setUp() throws Exception { |
| 64 | |
| 65 | } |
| 66 | |
| 67 | @After |
| 68 | public void tearDown() throws Exception { |
| 69 | channelBuffer = null; |
| 70 | result1 = null; |
| 71 | lsaHeader = null; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * Tests byteToInteger() method. |
| 77 | */ |
| 78 | @Test |
| 79 | public void testByteToInteger() throws Exception { |
| 80 | result = OspfUtil.byteToInteger(input); |
| 81 | assertThat(result, is(notNullValue())); |
| 82 | assertThat(result, is(2)); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Tests byteToLong() method. |
| 88 | */ |
| 89 | @Test |
| 90 | public void testByteToLong() throws Exception { |
| 91 | result3 = OspfUtil.byteToLong(input); |
| 92 | assertThat(result3, is(notNullValue())); |
| 93 | assertThat(result3, is(2L)); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests byteToInteger() method. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testByteToLong1() throws Exception { |
| 101 | result3 = OspfUtil.byteToLong(input); |
| 102 | assertThat(result3, is(notNullValue())); |
| 103 | assertThat(result3, is(2L)); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Tests byteToInteger() method. |
| 108 | */ |
| 109 | @Test |
| 110 | public void testByteToInteger1() throws Exception { |
| 111 | result = OspfUtil.byteToInteger(input); |
| 112 | assertThat(result, is(notNullValue())); |
| 113 | assertThat(result, is(2)); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Tests to createRandomNumber() method. |
| 118 | */ |
| 119 | @Test |
| 120 | public void testCreateRandomNumber() throws Exception { |
| 121 | num = OspfUtil.createRandomNumber(); |
| 122 | assertThat(num, is(notNullValue())); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Tests readLsaHeader() method. |
| 127 | */ |
| 128 | @Test |
| 129 | public void testReadLsaHeader2() throws Exception { |
| 130 | channelBuffer = ChannelBuffers.copiedBuffer(packet); |
| 131 | lsaHeader = OspfUtil.readLsaHeader(channelBuffer); |
| 132 | assertThat(lsaHeader, is(notNullValue())); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Tests to readLsaHeader method. |
| 137 | */ |
| 138 | @Test |
| 139 | public void testReadLsaHeader1() throws Exception { |
| 140 | channelBuffer = ChannelBuffers.copiedBuffer(opaqueheader); |
| 141 | lsaHeader = OspfUtil.readLsaHeader(channelBuffer); |
| 142 | assertThat(lsaHeader, is(notNullValue())); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Tests convertToTwoBytes() method. |
| 147 | */ |
| 148 | @Test |
| 149 | public void testConvertToTwoBytes() throws Exception { |
| 150 | input2 = 4; |
| 151 | result1 = OspfUtil.convertToTwoBytes(input2); |
| 152 | assertThat(result1.length, is(2)); |
| 153 | input2 = 1000; |
| 154 | result1 = OspfUtil.convertToTwoBytes(input2); |
| 155 | assertThat(result1.length, is(2)); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Tests convertToThreeBytes() method. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testConvertToThreeBytes() throws Exception { |
| 163 | input2 = 1000000; |
| 164 | result1 = OspfUtil.convertToThreeBytes(input2); |
| 165 | assertThat(result1.length, is(3)); |
| 166 | input2 = 1000; |
| 167 | result1 = OspfUtil.convertToThreeBytes(input2); |
| 168 | assertThat(result1.length, is(3)); |
| 169 | input2 = 1; |
| 170 | result1 = OspfUtil.convertToThreeBytes(input2); |
| 171 | assertThat(result1.length, is(3)); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Tests convertToFourBytes() method. |
| 176 | */ |
| 177 | @Test |
| 178 | public void testConvertToFourBytes() throws Exception { |
| 179 | input4 = 214748364110L; |
| 180 | result1 = OspfUtil.convertToFourBytes(input4); |
| 181 | assertThat(result1.length, is(4)); |
| 182 | input4 = 1000000; |
| 183 | result1 = OspfUtil.convertToFourBytes(input4); |
| 184 | assertThat(result1.length, is(4)); |
| 185 | input4 = 10000; |
| 186 | result1 = OspfUtil.convertToFourBytes(input4); |
| 187 | assertThat(result1.length, is(4)); |
| 188 | input4 = 1; |
| 189 | result1 = OspfUtil.convertToFourBytes(input4); |
| 190 | assertThat(result1.length, is(4)); |
| 191 | |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Tests convertToFourBytes() method. |
| 196 | */ |
| 197 | @Test |
| 198 | public void testConvertToFourBytes1() throws Exception { |
| 199 | input4 = 2147483635; |
| 200 | result1 = OspfUtil.convertToFourBytes(this.input4); |
| 201 | assertThat(result1.length, is(4)); |
| 202 | this.input4 = 1000000; |
| 203 | result1 = OspfUtil.convertToFourBytes(this.input4); |
| 204 | assertThat(result1.length, is(4)); |
| 205 | this.input4 = 10000; |
| 206 | result1 = OspfUtil.convertToFourBytes(this.input4); |
| 207 | assertThat(result1.length, is(4)); |
| 208 | this.input4 = 1; |
| 209 | result1 = OspfUtil.convertToFourBytes(this.input4); |
| 210 | assertThat(result1.length, is(4)); |
| 211 | |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Tests addLengthAndCheckSum() method. |
| 216 | */ |
| 217 | @Test |
| 218 | public void testAddLengthAndCheckSum() throws Exception { |
| 219 | result1 = OspfUtil.addLengthAndCheckSum(packet, ospfLengthPos1, ospfLengthPos2, |
| 220 | ospfChecksumPos1, ospfChecksumPos2); |
| 221 | assertThat(result1[ospfChecksumPos1], is(packet[ospfChecksumPos1])); |
| 222 | assertThat(result1[ospfChecksumPos2], is(packet[ospfChecksumPos2])); |
| 223 | assertThat(result1[ospfLengthPos1], is(packet[ospfLengthPos1])); |
| 224 | assertThat(result1[ospfLengthPos2], is(packet[ospfLengthPos2])); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Tests addMetadata() method. |
| 229 | */ |
| 230 | @Test |
| 231 | public void testAddMetadata() throws Exception { |
| 232 | result1 = OspfUtil.addMetadata(packet, 123, Ip4Address.valueOf("1.1.1.1")); |
| 233 | assertThat(result1, is(notNullValue())); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Tests addLengthAndCheckSum() method. |
| 238 | */ |
| 239 | @Test |
| 240 | public void testAddLsaLengthAndCheckSum() throws Exception { |
| 241 | result1 = OspfUtil.addLengthAndCheckSum(rLsa, lsaLengthPos1, lsaLengthPos2, |
| 242 | lsaChecksumPos1, lsaChecksumPos2); |
| 243 | assertThat(result1[lsaLengthPos1], is(rLsa[lsaLengthPos1])); |
| 244 | assertThat(result1[lsaLengthPos2], is(rLsa[lsaLengthPos2])); |
| 245 | assertThat(result1[lsaChecksumPos1], is(rLsa[lsaChecksumPos1])); |
| 246 | assertThat(result1[lsaChecksumPos2], is(rLsa[lsaChecksumPos2])); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Tests addMetadata() method. |
| 251 | */ |
| 252 | @Test |
| 253 | public void testAddMetaData() throws Exception { |
| 254 | result1 = OspfUtil.addMetadata(packet, 1, Ip4Address.valueOf("2.2.2.2")); |
| 255 | assertThat(result1, is(notNullValue())); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Tests sameNetwork() method. |
| 260 | */ |
| 261 | @Test |
| 262 | public void testSameNetwork() throws Exception { |
| 263 | result2 = OspfUtil.sameNetwork(Ip4Address.valueOf("10.10.10.10"), Ip4Address.valueOf("10.10.10.11"), |
| 264 | Ip4Address.valueOf("255.255.255.255")); |
| 265 | assertThat(result2, is(false)); |
| 266 | result2 = OspfUtil.sameNetwork(Ip4Address.valueOf("10.10.10.10"), Ip4Address.valueOf("10.10.10.10"), |
| 267 | Ip4Address.valueOf("255.255.255.255")); |
| 268 | assertThat(result2, is(true)); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Tests isOpaqueEnabled() method. |
| 273 | */ |
| 274 | @Test |
| 275 | public void testIsOpaqueEnabled() throws Exception { |
| 276 | result2 = OspfUtil.isOpaqueEnabled(2); |
| 277 | assertThat(result2, is(false)); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Tests sameNetwork() method. |
| 282 | */ |
| 283 | @Test |
| 284 | public void testisIsOpaqueEnabled() throws Exception { |
| 285 | result2 = OspfUtil.isOpaqueEnabled(2); |
| 286 | assertThat(result2, is(false)); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Tests readLsaHeader() method. |
| 291 | */ |
| 292 | @Test |
| 293 | public void testReadLsaHeader() throws Exception { |
| 294 | byte[] header = {0, 10, 2, 1, 7, 7, 7, 7, 7, 7, 7, 7, -128, 0, 0, 2, 46, -126, 0, |
| 295 | 48, 0, 0, 0, 2, 1, 1, 1, 1, 10, 10, 10, 7, 1, 0, 0, 10, 10, 10, 10, 0, -1, -1, -1, |
| 296 | 0, 3, 0, 0, 10, 0, 10, 66, 10, 1, 0, 0, 1, 7, 7, 7, 7, -128, 0, 0, 1, -64, 79, 0, |
| 297 | 116, 0, 1, 0, 4, 0, 0, 0, 0, 0, 2, 0, 84, 0, 1, 0, 1, 1, 0, 0, 0, 0, 2, 0, 4, 10, |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 298 | 10, 10, 0, 0, 5, 0, 4, 0, 0, 0, 0, 0, 6, 0, 4, 73, -104, -106, -128, 0, 7, 0, 4, 73, |
| 299 | -104, -106, -128, 0, 8, 0, 32, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106, |
Chidambar babu | 86b3b1a | 2016-02-16 17:39:52 +0530 | [diff] [blame] | 300 | -128, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106, -128, |
| 301 | 73, -104, -106, -128, 0, 9, 0, 4, 0, 0, 0, 0}; |
| 302 | channelBuffer = ChannelBuffers.copiedBuffer(header); |
| 303 | lsaHeader = OspfUtil.readLsaHeader(channelBuffer); |
| 304 | assertThat(lsaHeader, is(notNullValue())); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Tests readLsaHeader() method. |
| 309 | */ |
| 310 | @Test |
| 311 | public void testReadreadLsaHeader() throws Exception { |
| 312 | byte[] header = {0, 2, 2, 1, -64, -88, -86, 3, -64, -88, -86, 3, -128, 0, 0, 1, 58, -100, 0, 48}; |
| 313 | channelBuffer = ChannelBuffers.copiedBuffer(header); |
| 314 | lsaHeader = OspfUtil.readLsaHeader(channelBuffer); |
| 315 | assertThat(lsaHeader, is(notNullValue())); |
| 316 | } |
| 317 | } |