Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 18 | import com.google.common.net.InetAddresses; |
| 19 | import com.google.common.testing.EqualsTester; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 22 | import java.net.InetAddress; |
| 23 | |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 24 | import static org.hamcrest.Matchers.is; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 25 | import static org.junit.Assert.assertThat; |
| 26 | import static org.junit.Assert.assertTrue; |
| 27 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 28 | |
| 29 | /** |
| 30 | * Tests for class {@link Ip4Address}. |
| 31 | */ |
| 32 | public class Ip4AddressTest { |
| 33 | /** |
| 34 | * Tests the immutability of {@link Ip4Address}. |
| 35 | */ |
| 36 | @Test |
| 37 | public void testImmutable() { |
| 38 | assertThatClassIsImmutable(Ip4Address.class); |
| 39 | } |
| 40 | |
| 41 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 42 | * Tests the IPv4 address version constant. |
| 43 | */ |
| 44 | @Test |
| 45 | public void testAddressVersion() { |
| 46 | assertThat(Ip4Address.VERSION, is(IpAddress.Version.INET)); |
| 47 | } |
| 48 | |
| 49 | /** |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 50 | * Tests the length of the address in bytes (octets). |
| 51 | */ |
| 52 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 53 | public void testAddrByteLength() { |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 54 | assertThat(Ip4Address.BYTE_LENGTH, is(4)); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Tests the length of the address in bits. |
| 59 | */ |
| 60 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 61 | public void testAddrBitLength() { |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 62 | assertThat(Ip4Address.BIT_LENGTH, is(32)); |
| 63 | } |
| 64 | |
| 65 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 66 | * Tests returning the IP address version. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 67 | */ |
| 68 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 69 | public void testVersion() { |
| 70 | Ip4Address ipAddress; |
| 71 | |
| 72 | // IPv4 |
| 73 | ipAddress = Ip4Address.valueOf("0.0.0.0"); |
| 74 | assertThat(ipAddress.version(), is(IpAddress.Version.INET)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 78 | * Tests returning an IPv4 address as a byte array. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 79 | */ |
| 80 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 81 | public void testAddressToOctetsIPv4() { |
| 82 | Ip4Address ipAddress; |
| 83 | byte[] value; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 84 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 85 | value = new byte[] {1, 2, 3, 4}; |
| 86 | ipAddress = Ip4Address.valueOf("1.2.3.4"); |
| 87 | assertThat(ipAddress.toOctets(), is(value)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 88 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 89 | value = new byte[] {0, 0, 0, 0}; |
| 90 | ipAddress = Ip4Address.valueOf("0.0.0.0"); |
| 91 | assertThat(ipAddress.toOctets(), is(value)); |
| 92 | |
| 93 | value = new byte[] {(byte) 0xff, (byte) 0xff, |
| 94 | (byte) 0xff, (byte) 0xff}; |
| 95 | ipAddress = Ip4Address.valueOf("255.255.255.255"); |
| 96 | assertThat(ipAddress.toOctets(), is(value)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 100 | * Tests returning an IPv4 address as an integer. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 101 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 102 | @Test |
| 103 | public void testToInt() { |
| 104 | Ip4Address ipAddress; |
| 105 | |
| 106 | ipAddress = Ip4Address.valueOf("1.2.3.4"); |
| 107 | assertThat(ipAddress.toInt(), is(0x01020304)); |
| 108 | |
| 109 | ipAddress = Ip4Address.valueOf("0.0.0.0"); |
| 110 | assertThat(ipAddress.toInt(), is(0)); |
| 111 | |
| 112 | ipAddress = Ip4Address.valueOf("255.255.255.255"); |
| 113 | assertThat(ipAddress.toInt(), is(-1)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 117 | * Tests valueOf() converter for IPv4 integer value. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 118 | */ |
| 119 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 120 | public void testValueOfForIntegerIPv4() { |
| 121 | Ip4Address ipAddress; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 122 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 123 | ipAddress = Ip4Address.valueOf(0x01020304); |
| 124 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 125 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 126 | ipAddress = Ip4Address.valueOf(0); |
| 127 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 128 | |
| 129 | ipAddress = Ip4Address.valueOf(0xffffffff); |
| 130 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 134 | * Tests valueOf() converter for IPv4 byte array. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 135 | */ |
| 136 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 137 | public void testValueOfByteArrayIPv4() { |
| 138 | Ip4Address ipAddress; |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 139 | byte[] value; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 140 | |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 141 | value = new byte[] {1, 2, 3, 4}; |
| 142 | ipAddress = Ip4Address.valueOf(value); |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 143 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 144 | |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 145 | value = new byte[] {0, 0, 0, 0}; |
| 146 | ipAddress = Ip4Address.valueOf(value); |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 147 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 148 | |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 149 | value = new byte[] {(byte) 0xff, (byte) 0xff, |
| 150 | (byte) 0xff, (byte) 0xff}; |
| 151 | ipAddress = Ip4Address.valueOf(value); |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 152 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 156 | * Tests invalid valueOf() converter for a null array for IPv4. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 157 | */ |
| 158 | @Test(expected = NullPointerException.class) |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 159 | public void testInvalidValueOfNullArrayIPv4() { |
| 160 | Ip4Address ipAddress; |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 161 | byte[] value; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 162 | |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 163 | value = null; |
| 164 | ipAddress = Ip4Address.valueOf(value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 168 | * Tests invalid valueOf() converger for an array that is too short for |
| 169 | * IPv4. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 170 | */ |
| 171 | @Test(expected = IllegalArgumentException.class) |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 172 | public void testInvalidValueOfShortArrayIPv4() { |
| 173 | Ip4Address ipAddress; |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 174 | byte[] value; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 175 | |
Pavlin Radoslavov | 315d6c8 | 2014-11-04 15:36:04 -0800 | [diff] [blame] | 176 | value = new byte[] {1, 2, 3}; |
| 177 | ipAddress = Ip4Address.valueOf(value); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 181 | * Tests valueOf() converter for IPv4 byte array and an offset. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 182 | */ |
| 183 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 184 | public void testValueOfByteArrayOffsetIPv4() { |
| 185 | Ip4Address ipAddress; |
| 186 | byte[] value; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 187 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 188 | value = new byte[] {11, 22, 33, // Preamble |
| 189 | 1, 2, 3, 4, |
| 190 | 44, 55}; // Extra bytes |
| 191 | ipAddress = Ip4Address.valueOf(value, 3); |
| 192 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 193 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 194 | value = new byte[] {11, 22, // Preamble |
| 195 | 0, 0, 0, 0, |
| 196 | 33}; // Extra bytes |
| 197 | ipAddress = Ip4Address.valueOf(value, 2); |
| 198 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 199 | |
| 200 | value = new byte[] {11, 22, // Preamble |
| 201 | (byte) 0xff, (byte) 0xff, |
| 202 | (byte) 0xff, (byte) 0xff, |
| 203 | 33}; // Extra bytes |
| 204 | ipAddress = Ip4Address.valueOf(value, 2); |
| 205 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 209 | * Tests invalid valueOf() converger for an array and an invalid offset |
| 210 | * for IPv4. |
| 211 | */ |
| 212 | @Test(expected = IllegalArgumentException.class) |
| 213 | public void testInvalidValueOfArrayInvalidOffsetIPv4() { |
| 214 | Ip4Address ipAddress; |
| 215 | byte[] value; |
| 216 | |
| 217 | value = new byte[] {11, 22, 33, // Preamble |
| 218 | 1, 2, 3, 4, |
| 219 | 44, 55}; // Extra bytes |
| 220 | ipAddress = Ip4Address.valueOf(value, 6); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Tests valueOf() converter for IPv4 InetAddress. |
| 225 | */ |
| 226 | @Test |
| 227 | public void testValueOfInetAddressIPv4() { |
| 228 | Ip4Address ipAddress; |
| 229 | InetAddress inetAddress; |
| 230 | |
| 231 | inetAddress = InetAddresses.forString("1.2.3.4"); |
| 232 | ipAddress = Ip4Address.valueOf(inetAddress); |
| 233 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 234 | |
| 235 | inetAddress = InetAddresses.forString("0.0.0.0"); |
| 236 | ipAddress = Ip4Address.valueOf(inetAddress); |
| 237 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 238 | |
| 239 | inetAddress = InetAddresses.forString("255.255.255.255"); |
| 240 | ipAddress = Ip4Address.valueOf(inetAddress); |
| 241 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Tests valueOf() converter for IPv4 string. |
| 246 | */ |
| 247 | @Test |
| 248 | public void testValueOfStringIPv4() { |
| 249 | Ip4Address ipAddress; |
| 250 | |
| 251 | ipAddress = Ip4Address.valueOf("1.2.3.4"); |
| 252 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 253 | |
| 254 | ipAddress = Ip4Address.valueOf("0.0.0.0"); |
| 255 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 256 | |
| 257 | ipAddress = Ip4Address.valueOf("255.255.255.255"); |
| 258 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Tests invalid valueOf() converter for a null string. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 263 | */ |
| 264 | @Test(expected = NullPointerException.class) |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 265 | public void testInvalidValueOfNullString() { |
| 266 | Ip4Address ipAddress; |
| 267 | |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 268 | String fromString = null; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 269 | ipAddress = Ip4Address.valueOf(fromString); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 273 | * Tests invalid valueOf() converter for an empty string. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 274 | */ |
| 275 | @Test(expected = IllegalArgumentException.class) |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 276 | public void testInvalidValueOfEmptyString() { |
| 277 | Ip4Address ipAddress; |
| 278 | |
| 279 | String fromString = ""; |
| 280 | ipAddress = Ip4Address.valueOf(fromString); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 284 | * Tests invalid valueOf() converter for an incorrect string. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 285 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 286 | @Test(expected = IllegalArgumentException.class) |
| 287 | public void testInvalidValueOfIncorrectString() { |
| 288 | Ip4Address ipAddress; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 289 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 290 | String fromString = "NoSuchIpAddress"; |
| 291 | ipAddress = Ip4Address.valueOf(fromString); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 295 | * Tests making a mask prefix for a given prefix length for IPv4. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 296 | */ |
| 297 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 298 | public void testMakeMaskPrefixIPv4() { |
| 299 | Ip4Address ipAddress; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 300 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 301 | ipAddress = Ip4Address.makeMaskPrefix(25); |
| 302 | assertThat(ipAddress.toString(), is("255.255.255.128")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 303 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 304 | ipAddress = Ip4Address.makeMaskPrefix(0); |
| 305 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 306 | |
| 307 | ipAddress = Ip4Address.makeMaskPrefix(32); |
| 308 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 312 | * Tests making a mask prefix for an invalid prefix length for IPv4: |
| 313 | * negative prefix length. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 314 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 315 | @Test(expected = IllegalArgumentException.class) |
| 316 | public void testInvalidMakeNegativeMaskPrefixIPv4() { |
| 317 | Ip4Address ipAddress; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 318 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 319 | ipAddress = Ip4Address.makeMaskPrefix(-1); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 323 | * Tests making a mask prefix for an invalid prefix length for IPv4: |
| 324 | * too long prefix length. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 325 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 326 | @Test(expected = IllegalArgumentException.class) |
| 327 | public void testInvalidMakeTooLongMaskPrefixIPv4() { |
| 328 | Ip4Address ipAddress; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 329 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 330 | ipAddress = Ip4Address.makeMaskPrefix(33); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 334 | * Tests making of a masked address for IPv4. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 335 | */ |
| 336 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 337 | public void testMakeMaskedAddressIPv4() { |
| 338 | Ip4Address ipAddress = Ip4Address.valueOf("1.2.3.5"); |
| 339 | Ip4Address ipAddressMasked; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 340 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 341 | ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 24); |
| 342 | assertThat(ipAddressMasked.toString(), is("1.2.3.0")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 343 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 344 | ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 0); |
| 345 | assertThat(ipAddressMasked.toString(), is("0.0.0.0")); |
| 346 | |
| 347 | ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 32); |
| 348 | assertThat(ipAddressMasked.toString(), is("1.2.3.5")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 352 | * Tests making of a masked address for invalid prefix length for IPv4: |
| 353 | * negative prefix length. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 354 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 355 | @Test(expected = IllegalArgumentException.class) |
| 356 | public void testInvalidMakeNegativeMaskedAddressIPv4() { |
| 357 | Ip4Address ipAddress = Ip4Address.valueOf("1.2.3.5"); |
| 358 | Ip4Address ipAddressMasked; |
| 359 | |
| 360 | ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, -1); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 364 | * Tests making of a masked address for an invalid prefix length for IPv4: |
| 365 | * too long prefix length. |
| 366 | */ |
| 367 | @Test(expected = IllegalArgumentException.class) |
| 368 | public void testInvalidMakeTooLongMaskedAddressIPv4() { |
| 369 | Ip4Address ipAddress = Ip4Address.valueOf("1.2.3.5"); |
| 370 | Ip4Address ipAddressMasked; |
| 371 | |
| 372 | ipAddressMasked = Ip4Address.makeMaskedAddress(ipAddress, 33); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Tests comparison of {@link Ip4Address} for IPv4. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 377 | */ |
| 378 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 379 | public void testComparisonIPv4() { |
| 380 | Ip4Address addr1, addr2, addr3, addr4; |
| 381 | |
| 382 | addr1 = Ip4Address.valueOf("1.2.3.4"); |
| 383 | addr2 = Ip4Address.valueOf("1.2.3.4"); |
| 384 | addr3 = Ip4Address.valueOf("1.2.3.3"); |
| 385 | addr4 = Ip4Address.valueOf("1.2.3.5"); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 386 | assertTrue(addr1.compareTo(addr2) == 0); |
| 387 | assertTrue(addr1.compareTo(addr3) > 0); |
| 388 | assertTrue(addr1.compareTo(addr4) < 0); |
| 389 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 390 | addr1 = Ip4Address.valueOf("255.2.3.4"); |
| 391 | addr2 = Ip4Address.valueOf("255.2.3.4"); |
| 392 | addr3 = Ip4Address.valueOf("255.2.3.3"); |
| 393 | addr4 = Ip4Address.valueOf("255.2.3.5"); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 394 | assertTrue(addr1.compareTo(addr2) == 0); |
| 395 | assertTrue(addr1.compareTo(addr3) > 0); |
| 396 | assertTrue(addr1.compareTo(addr4) < 0); |
| 397 | } |
| 398 | |
| 399 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 400 | * Tests equality of {@link Ip4Address} for IPv4. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 401 | */ |
| 402 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 403 | public void testEqualityIPv4() { |
| 404 | new EqualsTester() |
| 405 | .addEqualityGroup(Ip4Address.valueOf("1.2.3.4"), |
| 406 | Ip4Address.valueOf("1.2.3.4")) |
| 407 | .addEqualityGroup(Ip4Address.valueOf("1.2.3.5"), |
| 408 | Ip4Address.valueOf("1.2.3.5")) |
| 409 | .addEqualityGroup(Ip4Address.valueOf("0.0.0.0"), |
| 410 | Ip4Address.valueOf("0.0.0.0")) |
| 411 | .addEqualityGroup(Ip4Address.valueOf("255.255.255.255"), |
| 412 | Ip4Address.valueOf("255.255.255.255")) |
| 413 | .testEquals(); |
| 414 | } |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 415 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 416 | /** |
| 417 | * Tests object string representation for IPv4. |
| 418 | */ |
| 419 | @Test |
| 420 | public void testToStringIPv4() { |
| 421 | Ip4Address ipAddress; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 422 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 423 | ipAddress = Ip4Address.valueOf("1.2.3.4"); |
| 424 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 425 | |
| 426 | ipAddress = Ip4Address.valueOf("0.0.0.0"); |
| 427 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 428 | |
| 429 | ipAddress = Ip4Address.valueOf("255.255.255.255"); |
| 430 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 431 | } |
| 432 | } |