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