Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 1 | package org.onlab.packet; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import static org.hamcrest.Matchers.is; |
| 6 | import static org.hamcrest.Matchers.not; |
| 7 | import static org.junit.Assert.assertThat; |
| 8 | import static org.junit.Assert.assertTrue; |
| 9 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 10 | |
| 11 | /** |
| 12 | * Tests for class {@link Ip6Address}. |
| 13 | */ |
| 14 | public class Ip6AddressTest { |
| 15 | /** |
| 16 | * Tests the immutability of {@link Ip6Address}. |
| 17 | */ |
| 18 | @Test |
| 19 | public void testImmutable() { |
| 20 | assertThatClassIsImmutable(Ip6Address.class); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Tests the length of the address in bytes (octets). |
| 25 | */ |
| 26 | @Test |
| 27 | public void testAddrBytelen() { |
| 28 | assertThat(Ip6Address.BYTE_LENGTH, is(16)); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Tests the length of the address in bits. |
| 33 | */ |
| 34 | @Test |
| 35 | public void testAddrBitlen() { |
| 36 | assertThat(Ip6Address.BIT_LENGTH, is(128)); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Tests default class constructor. |
| 41 | */ |
| 42 | @Test |
| 43 | public void testDefaultConstructor() { |
| 44 | Ip6Address ip6Address = new Ip6Address(); |
| 45 | assertThat(ip6Address.toString(), is("::")); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Tests valid class copy constructor. |
| 50 | */ |
| 51 | @Test |
| 52 | public void testCopyConstructor() { |
| 53 | Ip6Address fromAddr = |
| 54 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 55 | Ip6Address ip6Address = new Ip6Address(fromAddr); |
| 56 | assertThat(ip6Address.toString(), |
| 57 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 58 | |
| 59 | fromAddr = new Ip6Address("::"); |
| 60 | ip6Address = new Ip6Address(fromAddr); |
| 61 | assertThat(ip6Address.toString(), is("::")); |
| 62 | |
| 63 | fromAddr = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 64 | ip6Address = new Ip6Address(fromAddr); |
| 65 | assertThat(ip6Address.toString(), |
| 66 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Tests invalid class copy constructor for a null object to copy from. |
| 71 | */ |
| 72 | @Test(expected = NullPointerException.class) |
| 73 | public void testInvalidConstructorNullObject() { |
| 74 | Ip6Address fromAddr = null; |
| 75 | Ip6Address ip6Address = new Ip6Address(fromAddr); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Tests valid class constructor for integer values. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testConstructorForInteger() { |
| 83 | Ip6Address ip6Address = |
| 84 | new Ip6Address(0x1111222233334444L, 0x5555666677778888L); |
| 85 | assertThat(ip6Address.toString(), |
| 86 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 87 | |
| 88 | ip6Address = new Ip6Address(0L, 0L); |
| 89 | assertThat(ip6Address.toString(), is("::")); |
| 90 | |
| 91 | ip6Address = new Ip6Address(-1L, -1L); |
| 92 | assertThat(ip6Address.toString(), |
| 93 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests valid class constructor for an array value. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testConstructorForArray() { |
| 101 | final byte[] value1 = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 102 | 0x33, 0x33, 0x44, 0x44, |
| 103 | 0x55, 0x55, 0x66, 0x66, |
| 104 | 0x77, 0x77, |
| 105 | (byte) 0x88, (byte) 0x88}; |
| 106 | Ip6Address ip6Address = new Ip6Address(value1); |
| 107 | assertThat(ip6Address.toString(), |
| 108 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 109 | |
| 110 | final byte[] value2 = new byte[] {0x00, 0x00, 0x00, 0x00, |
| 111 | 0x00, 0x00, 0x00, 0x00, |
| 112 | 0x00, 0x00, 0x00, 0x00, |
| 113 | 0x00, 0x00, 0x00, 0x00}; |
| 114 | ip6Address = new Ip6Address(value2); |
| 115 | assertThat(ip6Address.toString(), is("::")); |
| 116 | |
| 117 | final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff, |
| 118 | (byte) 0xff, (byte) 0xff, |
| 119 | (byte) 0xff, (byte) 0xff, |
| 120 | (byte) 0xff, (byte) 0xff, |
| 121 | (byte) 0xff, (byte) 0xff, |
| 122 | (byte) 0xff, (byte) 0xff, |
| 123 | (byte) 0xff, (byte) 0xff, |
| 124 | (byte) 0xff, (byte) 0xff}; |
| 125 | ip6Address = new Ip6Address(value3); |
| 126 | assertThat(ip6Address.toString(), |
| 127 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Tests valid class constructor for an array value and an offset. |
| 132 | */ |
| 133 | @Test |
| 134 | public void testConstructorForArrayAndOffset() { |
| 135 | final byte[] value1 = new byte[] {11, 22, 33, // Preamble |
| 136 | 0x11, 0x11, 0x22, 0x22, |
| 137 | 0x33, 0x33, 0x44, 0x44, |
| 138 | 0x55, 0x55, 0x66, 0x66, |
| 139 | 0x77, 0x77, |
| 140 | (byte) 0x88, (byte) 0x88, |
| 141 | 44, 55}; // Extra bytes |
| 142 | Ip6Address ip6Address = new Ip6Address(value1, 3); |
| 143 | assertThat(ip6Address.toString(), |
| 144 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 145 | |
| 146 | final byte[] value2 = new byte[] {11, 22, // Preamble |
| 147 | 0x00, 0x00, 0x00, 0x00, |
| 148 | 0x00, 0x00, 0x00, 0x00, |
| 149 | 0x00, 0x00, 0x00, 0x00, |
| 150 | 0x00, 0x00, 0x00, 0x00, |
| 151 | 33}; // Extra bytes |
| 152 | ip6Address = new Ip6Address(value2, 2); |
| 153 | assertThat(ip6Address.toString(), is("::")); |
| 154 | |
| 155 | final byte[] value3 = new byte[] {11, 22, // Preamble |
| 156 | (byte) 0xff, (byte) 0xff, |
| 157 | (byte) 0xff, (byte) 0xff, |
| 158 | (byte) 0xff, (byte) 0xff, |
| 159 | (byte) 0xff, (byte) 0xff, |
| 160 | (byte) 0xff, (byte) 0xff, |
| 161 | (byte) 0xff, (byte) 0xff, |
| 162 | (byte) 0xff, (byte) 0xff, |
| 163 | (byte) 0xff, (byte) 0xff, |
| 164 | 33}; // Extra bytes |
| 165 | ip6Address = new Ip6Address(value3, 2); |
| 166 | assertThat(ip6Address.toString(), |
| 167 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Tests invalid class constructor for a null array. |
| 172 | */ |
| 173 | @Test(expected = NullPointerException.class) |
| 174 | public void testInvalidConstructorNullArray() { |
| 175 | final byte[] fromArray = null; |
| 176 | Ip6Address ip6Address = new Ip6Address(fromArray); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Tests invalid class constructor for an array that is too short. |
| 181 | */ |
| 182 | @Test(expected = IllegalArgumentException.class) |
| 183 | public void testInvalidConstructorShortArray() { |
| 184 | final byte[] fromArray = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 185 | Ip6Address ip6Address = new Ip6Address(fromArray); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Tests invalid class constructor for an array and an invalid offset. |
| 190 | */ |
| 191 | @Test(expected = IllegalArgumentException.class) |
| 192 | public void testInvalidConstructorArrayInvalidOffset() { |
| 193 | final byte[] value1 = new byte[] {11, 22, 33, // Preamble |
| 194 | 0x11, 0x11, 0x22, 0x22, |
| 195 | 0x33, 0x33, 0x44, 0x44, |
| 196 | 0x55, 0x55, 0x66, 0x66, |
| 197 | 0x77, 0x77, |
| 198 | (byte) 0x88, (byte) 0x88, |
| 199 | 44, 55}; // Extra bytes |
| 200 | Ip6Address ip6Address = new Ip6Address(value1, 6); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Tests valid class constructor for a string. |
| 205 | */ |
| 206 | @Test |
| 207 | public void testConstructorForString() { |
| 208 | Ip6Address ip6Address = |
| 209 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 210 | assertThat(ip6Address.toString(), |
| 211 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 212 | |
| 213 | ip6Address = new Ip6Address("::"); |
| 214 | assertThat(ip6Address.toString(), is("::")); |
| 215 | |
| 216 | ip6Address = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 217 | assertThat(ip6Address.toString(), |
| 218 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Tests invalid class constructor for a null string. |
| 223 | */ |
| 224 | @Test(expected = NullPointerException.class) |
| 225 | public void testInvalidConstructorNullString() { |
| 226 | String fromString = null; |
| 227 | Ip6Address ip6Address = new Ip6Address(fromString); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Tests invalid class constructor for an empty string. |
| 232 | */ |
| 233 | @Test(expected = IllegalArgumentException.class) |
| 234 | public void testInvalidConstructors() { |
| 235 | // Check constructor for invalid ID: empty string |
| 236 | Ip6Address ip6Address = new Ip6Address(""); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Tests returning the address as a byte array. |
| 241 | */ |
| 242 | @Test |
| 243 | public void testAddressToOctets() { |
| 244 | final byte[] value1 = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 245 | 0x33, 0x33, 0x44, 0x44, |
| 246 | 0x55, 0x55, 0x66, 0x66, |
| 247 | 0x77, 0x77, |
| 248 | (byte) 0x88, (byte) 0x88}; |
| 249 | Ip6Address ip6Address = |
| 250 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 251 | assertThat(ip6Address.toOctets(), is(value1)); |
| 252 | |
| 253 | final byte[] value2 = new byte[] {0x00, 0x00, 0x00, 0x00, |
| 254 | 0x00, 0x00, 0x00, 0x00, |
| 255 | 0x00, 0x00, 0x00, 0x00, |
| 256 | 0x00, 0x00, 0x00, 0x00}; |
| 257 | ip6Address = new Ip6Address("::"); |
| 258 | assertThat(ip6Address.toOctets(), is(value2)); |
| 259 | |
| 260 | final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff, |
| 261 | (byte) 0xff, (byte) 0xff, |
| 262 | (byte) 0xff, (byte) 0xff, |
| 263 | (byte) 0xff, (byte) 0xff, |
| 264 | (byte) 0xff, (byte) 0xff, |
| 265 | (byte) 0xff, (byte) 0xff, |
| 266 | (byte) 0xff, (byte) 0xff, |
| 267 | (byte) 0xff, (byte) 0xff}; |
| 268 | ip6Address = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 269 | assertThat(ip6Address.toOctets(), is(value3)); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Tests making a mask prefix for a given prefix length. |
| 274 | */ |
| 275 | @Test |
| 276 | public void testMakeMaskPrefix() { |
| 277 | Ip6Address ip6Address = Ip6Address.makeMaskPrefix(8); |
| 278 | assertThat(ip6Address.toString(), is("ff00::")); |
| 279 | |
| 280 | ip6Address = Ip6Address.makeMaskPrefix(120); |
| 281 | assertThat(ip6Address.toString(), |
| 282 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00")); |
| 283 | |
| 284 | ip6Address = Ip6Address.makeMaskPrefix(0); |
| 285 | assertThat(ip6Address.toString(), is("::")); |
| 286 | |
| 287 | ip6Address = Ip6Address.makeMaskPrefix(128); |
| 288 | assertThat(ip6Address.toString(), |
| 289 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 290 | |
| 291 | ip6Address = Ip6Address.makeMaskPrefix(64); |
| 292 | assertThat(ip6Address.toString(), is("ffff:ffff:ffff:ffff::")); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Tests making of a masked address. |
| 297 | */ |
| 298 | @Test |
| 299 | public void testMakeMaskedAddress() { |
| 300 | Ip6Address ip6Address = |
| 301 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885"); |
| 302 | Ip6Address ip6AddressMasked = |
| 303 | Ip6Address.makeMaskedAddress(ip6Address, 8); |
| 304 | assertThat(ip6AddressMasked.toString(), is("1100::")); |
| 305 | |
| 306 | ip6AddressMasked = Ip6Address.makeMaskedAddress(ip6Address, 120); |
| 307 | assertThat(ip6AddressMasked.toString(), |
| 308 | is("1111:2222:3333:4444:5555:6666:7777:8800")); |
| 309 | |
| 310 | ip6AddressMasked = Ip6Address.makeMaskedAddress(ip6Address, 0); |
| 311 | assertThat(ip6AddressMasked.toString(), is("::")); |
| 312 | |
| 313 | ip6AddressMasked = Ip6Address.makeMaskedAddress(ip6Address, 128); |
| 314 | assertThat(ip6AddressMasked.toString(), |
| 315 | is("1111:2222:3333:4444:5555:6666:7777:8885")); |
| 316 | |
| 317 | ip6AddressMasked = Ip6Address.makeMaskedAddress(ip6Address, 64); |
| 318 | assertThat(ip6AddressMasked.toString(), is("1111:2222:3333:4444::")); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Tests getting the value of an address. |
| 323 | */ |
| 324 | @Test |
| 325 | public void testGetValue() { |
| 326 | Ip6Address ip6Address = |
| 327 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 328 | assertThat(ip6Address.getValueHigh(), is(0x1111222233334444L)); |
| 329 | assertThat(ip6Address.getValueLow(), is(0x5555666677778888L)); |
| 330 | |
| 331 | ip6Address = new Ip6Address(0, 0); |
| 332 | assertThat(ip6Address.getValueHigh(), is(0L)); |
| 333 | assertThat(ip6Address.getValueLow(), is(0L)); |
| 334 | |
| 335 | ip6Address = new Ip6Address(-1L, -1L); |
| 336 | assertThat(ip6Address.getValueHigh(), is(-1L)); |
| 337 | assertThat(ip6Address.getValueLow(), is(-1L)); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Tests equality of {@link Ip6Address}. |
| 342 | */ |
| 343 | @Test |
| 344 | public void testEquality() { |
| 345 | Ip6Address addr1 = |
| 346 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 347 | Ip6Address addr2 = |
| 348 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 349 | assertThat(addr1, is(addr2)); |
| 350 | |
| 351 | addr1 = new Ip6Address("::"); |
| 352 | addr2 = new Ip6Address("::"); |
| 353 | assertThat(addr1, is(addr2)); |
| 354 | |
| 355 | addr1 = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 356 | addr2 = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 357 | assertThat(addr1, is(addr2)); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Tests non-equality of {@link Ip6Address}. |
| 362 | */ |
| 363 | @Test |
| 364 | public void testNonEquality() { |
| 365 | Ip6Address addr1 = |
| 366 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 367 | Ip6Address addr2 = |
| 368 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:888A"); |
| 369 | Ip6Address addr3 = new Ip6Address("::"); |
| 370 | Ip6Address addr4 = |
| 371 | new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 372 | assertThat(addr1, is(not(addr2))); |
| 373 | assertThat(addr3, is(not(addr2))); |
| 374 | assertThat(addr4, is(not(addr2))); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Tests comparison of {@link Ip6Address}. |
| 379 | */ |
| 380 | @Test |
| 381 | public void testComparison() { |
| 382 | Ip6Address addr1 = |
| 383 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 384 | Ip6Address addr2 = |
| 385 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 386 | Ip6Address addr3 = |
| 387 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8887"); |
| 388 | Ip6Address addr4 = |
| 389 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8889"); |
| 390 | assertTrue(addr1.compareTo(addr2) == 0); |
| 391 | assertTrue(addr1.compareTo(addr3) > 0); |
| 392 | assertTrue(addr1.compareTo(addr4) < 0); |
| 393 | |
| 394 | addr1 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 395 | addr2 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 396 | addr3 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8887"); |
| 397 | addr4 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8889"); |
| 398 | assertTrue(addr1.compareTo(addr2) == 0); |
| 399 | assertTrue(addr1.compareTo(addr3) > 0); |
| 400 | assertTrue(addr1.compareTo(addr4) < 0); |
| 401 | |
| 402 | addr1 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 403 | addr2 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 404 | addr3 = new Ip6Address("ffff:2222:3333:4443:5555:6666:7777:8888"); |
| 405 | addr4 = new Ip6Address("ffff:2222:3333:4445:5555:6666:7777:8888"); |
| 406 | assertTrue(addr1.compareTo(addr2) == 0); |
| 407 | assertTrue(addr1.compareTo(addr3) > 0); |
| 408 | assertTrue(addr1.compareTo(addr4) < 0); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Tests object string representation. |
| 413 | */ |
| 414 | @Test |
| 415 | public void testToString() { |
| 416 | Ip6Address ip6Address = |
| 417 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 418 | assertThat(ip6Address.toString(), |
| 419 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 420 | |
| 421 | ip6Address = new Ip6Address("1111::8888"); |
| 422 | assertThat(ip6Address.toString(), is("1111::8888")); |
| 423 | |
| 424 | ip6Address = new Ip6Address("1111::"); |
| 425 | assertThat(ip6Address.toString(), is("1111::")); |
| 426 | |
| 427 | ip6Address = new Ip6Address("::8888"); |
| 428 | assertThat(ip6Address.toString(), is("::8888")); |
| 429 | |
| 430 | ip6Address = new Ip6Address("::"); |
| 431 | assertThat(ip6Address.toString(), is("::")); |
| 432 | |
| 433 | ip6Address = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 434 | assertThat(ip6Address.toString(), |
| 435 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 436 | } |
| 437 | } |