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