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