blob: 6e9260eebcb247f561012f302e27a469474c3224 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Pavlin Radoslavov9de27722014-10-23 20:31:15 -070019package org.onlab.packet;
20
21import org.junit.Test;
22
23import static org.hamcrest.Matchers.is;
24import static org.hamcrest.Matchers.not;
25import static org.junit.Assert.assertThat;
26import static org.junit.Assert.assertTrue;
27import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
28
29/**
30 * Tests for class {@link Ip4Address}.
31 */
32public class Ip4AddressTest {
33 /**
34 * Tests the immutability of {@link Ip4Address}.
35 */
36 @Test
37 public void testImmutable() {
38 assertThatClassIsImmutable(Ip4Address.class);
39 }
40
41 /**
42 * Tests the length of the address in bytes (octets).
43 */
44 @Test
45 public void testAddrBytelen() {
46 assertThat(Ip4Address.BYTE_LENGTH, is(4));
47 }
48
49 /**
50 * Tests the length of the address in bits.
51 */
52 @Test
53 public void testAddrBitlen() {
54 assertThat(Ip4Address.BIT_LENGTH, is(32));
55 }
56
57 /**
58 * Tests default class constructor.
59 */
60 @Test
61 public void testDefaultConstructor() {
62 Ip4Address ip4Address = new Ip4Address();
63 assertThat(ip4Address.toString(), is("0.0.0.0"));
64 }
65
66 /**
67 * Tests valid class copy constructor.
68 */
69 @Test
70 public void testCopyConstructor() {
71 Ip4Address fromAddr = new Ip4Address("1.2.3.4");
72 Ip4Address ip4Address = new Ip4Address(fromAddr);
73 assertThat(ip4Address.toString(), is("1.2.3.4"));
74
75 fromAddr = new Ip4Address("0.0.0.0");
76 ip4Address = new Ip4Address(fromAddr);
77 assertThat(ip4Address.toString(), is("0.0.0.0"));
78
79 fromAddr = new Ip4Address("255.255.255.255");
80 ip4Address = new Ip4Address(fromAddr);
81 assertThat(ip4Address.toString(), is("255.255.255.255"));
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 Ip4Address fromAddr = null;
90 Ip4Address ip4Address = new Ip4Address(fromAddr);
91 }
92
93 /**
94 * Tests valid class constructor for an integer value.
95 */
96 @Test
97 public void testConstructorForInteger() {
98 Ip4Address ip4Address = new Ip4Address(0x01020304);
99 assertThat(ip4Address.toString(), is("1.2.3.4"));
100
101 ip4Address = new Ip4Address(0);
102 assertThat(ip4Address.toString(), is("0.0.0.0"));
103
104 ip4Address = new Ip4Address(0xffffffff);
105 assertThat(ip4Address.toString(), is("255.255.255.255"));
106 }
107
108 /**
109 * Tests valid class constructor for an array value.
110 */
111 @Test
112 public void testConstructorForArray() {
113 final byte[] value1 = new byte[] {1, 2, 3, 4};
114 Ip4Address ip4Address = new Ip4Address(value1);
115 assertThat(ip4Address.toString(), is("1.2.3.4"));
116
117 final byte[] value2 = new byte[] {0, 0, 0, 0};
118 ip4Address = new Ip4Address(value2);
119 assertThat(ip4Address.toString(), is("0.0.0.0"));
120
121 final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff,
122 (byte) 0xff, (byte) 0xff};
123 ip4Address = new Ip4Address(value3);
124 assertThat(ip4Address.toString(), is("255.255.255.255"));
125 }
126
127 /**
128 * Tests valid class constructor for an array value and an offset.
129 */
130 @Test
131 public void testConstructorForArrayAndOffset() {
132 final byte[] value1 = new byte[] {11, 22, 33, // Preamble
133 1, 2, 3, 4,
134 44, 55}; // Extra bytes
135 Ip4Address ip4Address = new Ip4Address(value1, 3);
136 assertThat(ip4Address.toString(), is("1.2.3.4"));
137
138 final byte[] value2 = new byte[] {11, 22, // Preamble
139 0, 0, 0, 0,
140 33}; // Extra bytes
141 ip4Address = new Ip4Address(value2, 2);
142 assertThat(ip4Address.toString(), is("0.0.0.0"));
143
144 final byte[] value3 = new byte[] {11, 22, // Preamble
145 (byte) 0xff, (byte) 0xff,
146 (byte) 0xff, (byte) 0xff,
147 33}; // Extra bytes
148 ip4Address = new Ip4Address(value3, 2);
149 assertThat(ip4Address.toString(), is("255.255.255.255"));
150 }
151
152 /**
153 * Tests invalid class constructor for a null array.
154 */
155 @Test(expected = NullPointerException.class)
156 public void testInvalidConstructorNullArray() {
157 final byte[] fromArray = null;
158 Ip4Address ip4Address = new Ip4Address(fromArray);
159 }
160
161 /**
162 * Tests invalid class constructor for an array that is too short.
163 */
164 @Test(expected = IllegalArgumentException.class)
165 public void testInvalidConstructorShortArray() {
166 final byte[] fromArray = new byte[] {1, 2, 3};
167 Ip4Address ip4Address = new Ip4Address(fromArray);
168 }
169
170 /**
171 * Tests invalid class constructor for an array and an invalid offset.
172 */
173 @Test(expected = IllegalArgumentException.class)
174 public void testInvalidConstructorArrayInvalidOffset() {
175 final byte[] value1 = new byte[] {11, 22, 33, // Preamble
176 1, 2, 3, 4,
177 44, 55}; // Extra bytes
178 Ip4Address ip4Address = new Ip4Address(value1, 6);
179 }
180
181 /**
182 * Tests valid class constructor for a string.
183 */
184 @Test
185 public void testConstructorForString() {
186 Ip4Address ip4Address = new Ip4Address("1.2.3.4");
187 assertThat(ip4Address.toString(), is("1.2.3.4"));
188
189 ip4Address = new Ip4Address("0.0.0.0");
190 assertThat(ip4Address.toString(), is("0.0.0.0"));
191
192 ip4Address = new Ip4Address("255.255.255.255");
193 assertThat(ip4Address.toString(), is("255.255.255.255"));
194 }
195
196 /**
197 * Tests invalid class constructor for a null string.
198 */
199 @Test(expected = NullPointerException.class)
200 public void testInvalidConstructorNullString() {
201 String fromString = null;
202 Ip4Address ip4Address = new Ip4Address(fromString);
203 }
204
205 /**
206 * Tests invalid class constructor for an empty string.
207 */
208 @Test(expected = IllegalArgumentException.class)
209 public void testInvalidConstructors() {
210 // Check constructor for invalid ID: empty string
211 Ip4Address ip4Address = new Ip4Address("");
212 }
213
214 /**
215 * Tests returning the address as a byte array.
216 */
217 @Test
218 public void testAddressToOctets() {
219 final byte[] value1 = new byte[] {1, 2, 3, 4};
220 Ip4Address ip4Address = new Ip4Address("1.2.3.4");
221 assertThat(ip4Address.toOctets(), is(value1));
222
223 final byte[] value2 = new byte[] {0, 0, 0, 0};
224 ip4Address = new Ip4Address("0.0.0.0");
225 assertThat(ip4Address.toOctets(), is(value2));
226
227 final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff,
228 (byte) 0xff, (byte) 0xff};
229 ip4Address = new Ip4Address("255.255.255.255");
230 assertThat(ip4Address.toOctets(), is(value3));
231 }
232
233 /**
234 * Tests making a mask prefix for a given prefix length.
235 */
236 @Test
237 public void testMakeMaskPrefix() {
238 Ip4Address ip4Address = Ip4Address.makeMaskPrefix(25);
239 assertThat(ip4Address.toString(), is("255.255.255.128"));
240
241 ip4Address = Ip4Address.makeMaskPrefix(0);
242 assertThat(ip4Address.toString(), is("0.0.0.0"));
243
244 ip4Address = Ip4Address.makeMaskPrefix(32);
245 assertThat(ip4Address.toString(), is("255.255.255.255"));
246 }
247
248 /**
249 * Tests making of a masked address.
250 */
251 @Test
252 public void testMakeMaskedAddress() {
253 Ip4Address ip4Address = new Ip4Address("1.2.3.5");
254 Ip4Address ip4AddressMasked =
255 Ip4Address.makeMaskedAddress(ip4Address, 24);
256 assertThat(ip4AddressMasked.toString(), is("1.2.3.0"));
257
258 ip4AddressMasked = Ip4Address.makeMaskedAddress(ip4Address, 0);
259 assertThat(ip4AddressMasked.toString(), is("0.0.0.0"));
260
261 ip4AddressMasked = Ip4Address.makeMaskedAddress(ip4Address, 32);
262 assertThat(ip4AddressMasked.toString(), is("1.2.3.5"));
263 }
264
265 /**
266 * Tests getting the value of an address.
267 */
268 @Test
269 public void testGetValue() {
270 Ip4Address ip4Address = new Ip4Address("1.2.3.4");
271 assertThat(ip4Address.getValue(), is(0x01020304));
272
273 ip4Address = new Ip4Address("0.0.0.0");
274 assertThat(ip4Address.getValue(), is(0));
275
276 ip4Address = new Ip4Address("255.255.255.255");
277 assertThat(ip4Address.getValue(), is(-1));
278 }
279
280 /**
281 * Tests equality of {@link Ip4Address}.
282 */
283 @Test
284 public void testEquality() {
285 Ip4Address addr1 = new Ip4Address("1.2.3.4");
286 Ip4Address addr2 = new Ip4Address("1.2.3.4");
287 assertThat(addr1, is(addr2));
288
289 addr1 = new Ip4Address("0.0.0.0");
290 addr2 = new Ip4Address("0.0.0.0");
291 assertThat(addr1, is(addr2));
292
293 addr1 = new Ip4Address("255.255.255.255");
294 addr2 = new Ip4Address("255.255.255.255");
295 assertThat(addr1, is(addr2));
296 }
297
298 /**
299 * Tests non-equality of {@link Ip4Address}.
300 */
301 @Test
302 public void testNonEquality() {
303 Ip4Address addr1 = new Ip4Address("1.2.3.4");
304 Ip4Address addr2 = new Ip4Address("1.2.3.5");
305 Ip4Address addr3 = new Ip4Address("0.0.0.0");
306 Ip4Address addr4 = new Ip4Address("255.255.255.255");
307 assertThat(addr1, is(not(addr2)));
308 assertThat(addr3, is(not(addr2)));
309 assertThat(addr4, is(not(addr2)));
310 }
311
312 /**
313 * Tests comparison of {@link Ip4Address}.
314 */
315 @Test
316 public void testComparison() {
317 Ip4Address addr1 = new Ip4Address("1.2.3.4");
318 Ip4Address addr2 = new Ip4Address("1.2.3.4");
319 Ip4Address addr3 = new Ip4Address("1.2.3.3");
320 Ip4Address addr4 = new Ip4Address("1.2.3.5");
321 assertTrue(addr1.compareTo(addr2) == 0);
322 assertTrue(addr1.compareTo(addr3) > 0);
323 assertTrue(addr1.compareTo(addr4) < 0);
324
325 addr1 = new Ip4Address("255.2.3.4");
326 addr2 = new Ip4Address("255.2.3.4");
327 addr3 = new Ip4Address("255.2.3.3");
328 addr4 = new Ip4Address("255.2.3.5");
329 assertTrue(addr1.compareTo(addr2) == 0);
330 assertTrue(addr1.compareTo(addr3) > 0);
331 assertTrue(addr1.compareTo(addr4) < 0);
332 }
333
334 /**
335 * Tests object string representation.
336 */
337 @Test
338 public void testToString() {
339 Ip4Address ip4Address = new Ip4Address("1.2.3.4");
340 assertThat(ip4Address.toString(), is("1.2.3.4"));
341
342 ip4Address = new Ip4Address("0.0.0.0");
343 assertThat(ip4Address.toString(), is("0.0.0.0"));
344
345 ip4Address = new Ip4Address("255.255.255.255");
346 assertThat(ip4Address.toString(), is("255.255.255.255"));
347 }
348}