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.testing.EqualsTester; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 19 | import org.junit.Test; |
| 20 | |
| 21 | import static org.hamcrest.Matchers.equalTo; |
| 22 | import static org.hamcrest.Matchers.is; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 23 | import static org.junit.Assert.assertThat; |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 24 | import static org.junit.Assert.assertFalse; |
| 25 | import static org.junit.Assert.assertTrue; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 26 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 27 | |
| 28 | /** |
| 29 | * Tests for class {@link Ip4Prefix}. |
| 30 | */ |
| 31 | public class Ip4PrefixTest { |
| 32 | /** |
| 33 | * Tests the immutability of {@link Ip4Prefix}. |
| 34 | */ |
| 35 | @Test |
| 36 | public void testImmutable() { |
| 37 | assertThatClassIsImmutable(Ip4Prefix.class); |
| 38 | } |
| 39 | |
| 40 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 41 | * Tests the IPv4 prefix address version constant. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 42 | */ |
| 43 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 44 | public void testAddressVersion() { |
| 45 | assertThat(Ip4Prefix.VERSION, is(IpAddress.Version.INET)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 49 | * Tests the maximum mask length. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 50 | */ |
| 51 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 52 | public void testMaxMaskLength() { |
| 53 | assertThat(Ip4Prefix.MAX_MASK_LENGTH, is(32)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 57 | * Tests returning the IP version of the prefix. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 58 | */ |
| 59 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 60 | public void testVersion() { |
| 61 | Ip4Prefix ipPrefix; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 62 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 63 | // IPv4 |
| 64 | ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0"); |
| 65 | assertThat(ipPrefix.version(), is(IpAddress.Version.INET)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 69 | * Tests returning the IP address value and IP address prefix length of |
| 70 | * an IPv4 prefix. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 71 | */ |
| 72 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 73 | public void testAddressAndPrefixLengthIPv4() { |
| 74 | Ip4Prefix ipPrefix; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 75 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 76 | ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 77 | assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.0"))); |
| 78 | assertThat(ipPrefix.prefixLength(), is(24)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 79 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 80 | ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24"); |
| 81 | assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.0"))); |
| 82 | assertThat(ipPrefix.prefixLength(), is(24)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 83 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 84 | ipPrefix = Ip4Prefix.valueOf("1.2.3.4/32"); |
| 85 | assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.4"))); |
| 86 | assertThat(ipPrefix.prefixLength(), is(32)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 87 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 88 | ipPrefix = Ip4Prefix.valueOf("1.2.3.5/32"); |
| 89 | assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("1.2.3.5"))); |
| 90 | assertThat(ipPrefix.prefixLength(), is(32)); |
| 91 | |
| 92 | ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0"); |
| 93 | assertThat(ipPrefix.address(), equalTo(Ip4Address.valueOf("0.0.0.0"))); |
| 94 | assertThat(ipPrefix.prefixLength(), is(0)); |
| 95 | |
| 96 | ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32"); |
| 97 | assertThat(ipPrefix.address(), |
| 98 | equalTo(Ip4Address.valueOf("255.255.255.255"))); |
| 99 | assertThat(ipPrefix.prefixLength(), is(32)); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 103 | * Tests valueOf() converter for IPv4 integer value. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 104 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 105 | @Test |
| 106 | public void testValueOfForIntegerIPv4() { |
| 107 | Ip4Prefix ipPrefix; |
| 108 | |
| 109 | ipPrefix = Ip4Prefix.valueOf(0x01020304, 24); |
| 110 | assertThat(ipPrefix.toString(), is("1.2.3.0/24")); |
| 111 | |
| 112 | ipPrefix = Ip4Prefix.valueOf(0x01020304, 32); |
| 113 | assertThat(ipPrefix.toString(), is("1.2.3.4/32")); |
| 114 | |
| 115 | ipPrefix = Ip4Prefix.valueOf(0x01020305, 32); |
| 116 | assertThat(ipPrefix.toString(), is("1.2.3.5/32")); |
| 117 | |
| 118 | ipPrefix = Ip4Prefix.valueOf(0, 0); |
| 119 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 120 | |
| 121 | ipPrefix = Ip4Prefix.valueOf(0, 32); |
| 122 | assertThat(ipPrefix.toString(), is("0.0.0.0/32")); |
| 123 | |
| 124 | ipPrefix = Ip4Prefix.valueOf(0xffffffff, 0); |
| 125 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 126 | |
| 127 | ipPrefix = Ip4Prefix.valueOf(0xffffffff, 16); |
| 128 | assertThat(ipPrefix.toString(), is("255.255.0.0/16")); |
| 129 | |
| 130 | ipPrefix = Ip4Prefix.valueOf(0xffffffff, 32); |
| 131 | assertThat(ipPrefix.toString(), is("255.255.255.255/32")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 135 | * Tests invalid valueOf() converter for IPv4 integer value and |
| 136 | * negative prefix length. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 137 | */ |
| 138 | @Test(expected = IllegalArgumentException.class) |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 139 | public void testInvalidValueOfIntegerNegativePrefixLengthIPv4() { |
| 140 | Ip4Prefix ipPrefix; |
| 141 | |
| 142 | ipPrefix = Ip4Prefix.valueOf(0x01020304, -1); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 146 | * Tests invalid valueOf() converter for IPv4 integer value and |
| 147 | * too long prefix length. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 148 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 149 | @Test(expected = IllegalArgumentException.class) |
| 150 | public void testInvalidValueOfIntegerTooLongPrefixLengthIPv4() { |
| 151 | Ip4Prefix ipPrefix; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 152 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 153 | ipPrefix = Ip4Prefix.valueOf(0x01020304, 33); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 157 | * Tests valueOf() converter for IPv4 byte array. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 158 | */ |
| 159 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 160 | public void testValueOfByteArrayIPv4() { |
| 161 | Ip4Prefix ipPrefix; |
| 162 | byte[] value; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 163 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 164 | value = new byte[] {1, 2, 3, 4}; |
| 165 | ipPrefix = Ip4Prefix.valueOf(value, 24); |
| 166 | assertThat(ipPrefix.toString(), is("1.2.3.0/24")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 167 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 168 | ipPrefix = Ip4Prefix.valueOf(value, 32); |
| 169 | assertThat(ipPrefix.toString(), is("1.2.3.4/32")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 170 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 171 | value = new byte[] {1, 2, 3, 5}; |
| 172 | ipPrefix = Ip4Prefix.valueOf(value, 32); |
| 173 | assertThat(ipPrefix.toString(), is("1.2.3.5/32")); |
| 174 | |
| 175 | value = new byte[] {0, 0, 0, 0}; |
| 176 | ipPrefix = Ip4Prefix.valueOf(value, 0); |
| 177 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 178 | |
| 179 | ipPrefix = Ip4Prefix.valueOf(value, 32); |
| 180 | assertThat(ipPrefix.toString(), is("0.0.0.0/32")); |
| 181 | |
| 182 | value = new byte[] {(byte) 0xff, (byte) 0xff, |
| 183 | (byte) 0xff, (byte) 0xff}; |
| 184 | ipPrefix = Ip4Prefix.valueOf(value, 0); |
| 185 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 186 | |
| 187 | ipPrefix = Ip4Prefix.valueOf(value, 16); |
| 188 | assertThat(ipPrefix.toString(), is("255.255.0.0/16")); |
| 189 | |
| 190 | ipPrefix = Ip4Prefix.valueOf(value, 32); |
| 191 | assertThat(ipPrefix.toString(), is("255.255.255.255/32")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 195 | * Tests invalid valueOf() converter for a null array for IPv4. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 196 | */ |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 197 | @Test(expected = NullPointerException.class) |
| 198 | public void testInvalidValueOfNullArrayIPv4() { |
| 199 | Ip4Prefix ipPrefix; |
| 200 | byte[] value; |
| 201 | |
| 202 | value = null; |
| 203 | ipPrefix = Ip4Prefix.valueOf(value, 24); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 207 | * Tests invalid valueOf() converter for a short array for IPv4. |
| 208 | */ |
| 209 | @Test(expected = IllegalArgumentException.class) |
| 210 | public void testInvalidValueOfShortArrayIPv4() { |
| 211 | Ip4Prefix ipPrefix; |
| 212 | byte[] value; |
| 213 | |
| 214 | value = new byte[] {1, 2, 3}; |
| 215 | ipPrefix = Ip4Prefix.valueOf(value, 24); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Tests invalid valueOf() converter for IPv4 byte array and |
| 220 | * negative prefix length. |
| 221 | */ |
| 222 | @Test(expected = IllegalArgumentException.class) |
| 223 | public void testInvalidValueOfByteArrayNegativePrefixLengthIPv4() { |
| 224 | Ip4Prefix ipPrefix; |
| 225 | byte[] value; |
| 226 | |
| 227 | value = new byte[] {1, 2, 3, 4}; |
| 228 | ipPrefix = Ip4Prefix.valueOf(value, -1); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Tests invalid valueOf() converter for IPv4 byte array and |
| 233 | * too long prefix length. |
| 234 | */ |
| 235 | @Test(expected = IllegalArgumentException.class) |
| 236 | public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv4() { |
| 237 | Ip4Prefix ipPrefix; |
| 238 | byte[] value; |
| 239 | |
| 240 | value = new byte[] {1, 2, 3, 4}; |
| 241 | ipPrefix = Ip4Prefix.valueOf(value, 33); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Tests valueOf() converter for IPv4 address. |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 246 | */ |
| 247 | @Test |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 248 | public void testValueOfAddressIPv4() { |
| 249 | Ip4Address ipAddress; |
| 250 | Ip4Prefix ipPrefix; |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 251 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 252 | ipAddress = Ip4Address.valueOf("1.2.3.4"); |
| 253 | ipPrefix = Ip4Prefix.valueOf(ipAddress, 24); |
| 254 | assertThat(ipPrefix.toString(), is("1.2.3.0/24")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 255 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 256 | ipPrefix = Ip4Prefix.valueOf(ipAddress, 32); |
| 257 | assertThat(ipPrefix.toString(), is("1.2.3.4/32")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 258 | |
Pavlin Radoslavov | f182f01 | 2014-11-04 15:03:18 -0800 | [diff] [blame] | 259 | ipAddress = Ip4Address.valueOf("1.2.3.5"); |
| 260 | ipPrefix = Ip4Prefix.valueOf(ipAddress, 32); |
| 261 | assertThat(ipPrefix.toString(), is("1.2.3.5/32")); |
| 262 | |
| 263 | ipAddress = Ip4Address.valueOf("0.0.0.0"); |
| 264 | ipPrefix = Ip4Prefix.valueOf(ipAddress, 0); |
| 265 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 266 | |
| 267 | ipPrefix = Ip4Prefix.valueOf(ipAddress, 32); |
| 268 | assertThat(ipPrefix.toString(), is("0.0.0.0/32")); |
| 269 | |
| 270 | ipAddress = Ip4Address.valueOf("255.255.255.255"); |
| 271 | ipPrefix = Ip4Prefix.valueOf(ipAddress, 0); |
| 272 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 273 | |
| 274 | ipPrefix = Ip4Prefix.valueOf(ipAddress, 16); |
| 275 | assertThat(ipPrefix.toString(), is("255.255.0.0/16")); |
| 276 | |
| 277 | ipPrefix = Ip4Prefix.valueOf(ipAddress, 32); |
| 278 | assertThat(ipPrefix.toString(), is("255.255.255.255/32")); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Tests invalid valueOf() converter for a null IP address. |
| 283 | */ |
| 284 | @Test(expected = NullPointerException.class) |
| 285 | public void testInvalidValueOfNullAddress() { |
| 286 | Ip4Address ipAddress; |
| 287 | Ip4Prefix ipPrefix; |
| 288 | |
| 289 | ipAddress = null; |
| 290 | ipPrefix = Ip4Prefix.valueOf(ipAddress, 24); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Tests invalid valueOf() converter for IPv4 address and |
| 295 | * negative prefix length. |
| 296 | */ |
| 297 | @Test(expected = IllegalArgumentException.class) |
| 298 | public void testInvalidValueOfAddressNegativePrefixLengthIPv4() { |
| 299 | Ip4Address ipAddress; |
| 300 | Ip4Prefix ipPrefix; |
| 301 | |
| 302 | ipAddress = Ip4Address.valueOf("1.2.3.4"); |
| 303 | ipPrefix = Ip4Prefix.valueOf(ipAddress, -1); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Tests invalid valueOf() converter for IPv4 address and |
| 308 | * too long prefix length. |
| 309 | */ |
| 310 | @Test(expected = IllegalArgumentException.class) |
| 311 | public void testInvalidValueOfAddressTooLongPrefixLengthIPv4() { |
| 312 | Ip4Address ipAddress; |
| 313 | Ip4Prefix ipPrefix; |
| 314 | |
| 315 | ipAddress = Ip4Address.valueOf("1.2.3.4"); |
| 316 | ipPrefix = Ip4Prefix.valueOf(ipAddress, 33); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Tests valueOf() converter for IPv4 string. |
| 321 | */ |
| 322 | @Test |
| 323 | public void testValueOfStringIPv4() { |
| 324 | Ip4Prefix ipPrefix; |
| 325 | |
| 326 | ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24"); |
| 327 | assertThat(ipPrefix.toString(), is("1.2.3.0/24")); |
| 328 | |
| 329 | ipPrefix = Ip4Prefix.valueOf("1.2.3.4/32"); |
| 330 | assertThat(ipPrefix.toString(), is("1.2.3.4/32")); |
| 331 | |
| 332 | ipPrefix = Ip4Prefix.valueOf("1.2.3.5/32"); |
| 333 | assertThat(ipPrefix.toString(), is("1.2.3.5/32")); |
| 334 | |
| 335 | ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0"); |
| 336 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 337 | |
| 338 | ipPrefix = Ip4Prefix.valueOf("0.0.0.0/32"); |
| 339 | assertThat(ipPrefix.toString(), is("0.0.0.0/32")); |
| 340 | |
| 341 | ipPrefix = Ip4Prefix.valueOf("255.255.255.255/0"); |
| 342 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 343 | |
| 344 | ipPrefix = Ip4Prefix.valueOf("255.255.255.255/16"); |
| 345 | assertThat(ipPrefix.toString(), is("255.255.0.0/16")); |
| 346 | |
| 347 | ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32"); |
| 348 | assertThat(ipPrefix.toString(), is("255.255.255.255/32")); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Tests invalid valueOf() converter for a null string. |
| 353 | */ |
| 354 | @Test(expected = NullPointerException.class) |
| 355 | public void testInvalidValueOfNullString() { |
| 356 | Ip4Prefix ipPrefix; |
| 357 | String fromString; |
| 358 | |
| 359 | fromString = null; |
| 360 | ipPrefix = Ip4Prefix.valueOf(fromString); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Tests invalid valueOf() converter for an empty string. |
| 365 | */ |
| 366 | @Test(expected = IllegalArgumentException.class) |
| 367 | public void testInvalidValueOfEmptyString() { |
| 368 | Ip4Prefix ipPrefix; |
| 369 | String fromString; |
| 370 | |
| 371 | fromString = ""; |
| 372 | ipPrefix = Ip4Prefix.valueOf(fromString); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Tests invalid valueOf() converter for an incorrect string. |
| 377 | */ |
| 378 | @Test(expected = IllegalArgumentException.class) |
| 379 | public void testInvalidValueOfIncorrectString() { |
| 380 | Ip4Prefix ipPrefix; |
| 381 | String fromString; |
| 382 | |
| 383 | fromString = "NoSuchIpPrefix"; |
| 384 | ipPrefix = Ip4Prefix.valueOf(fromString); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Tests invalid valueOf() converter for IPv4 string and |
| 389 | * negative prefix length. |
| 390 | */ |
| 391 | @Test(expected = IllegalArgumentException.class) |
| 392 | public void testInvalidValueOfStringNegativePrefixLengthIPv4() { |
| 393 | Ip4Prefix ipPrefix; |
| 394 | |
| 395 | ipPrefix = Ip4Prefix.valueOf("1.2.3.4/-1"); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Tests invalid valueOf() converter for IPv4 string and |
| 400 | * too long prefix length. |
| 401 | */ |
| 402 | @Test(expected = IllegalArgumentException.class) |
| 403 | public void testInvalidValueOfStringTooLongPrefixLengthIPv4() { |
| 404 | Ip4Prefix ipPrefix; |
| 405 | |
| 406 | ipPrefix = Ip4Prefix.valueOf("1.2.3.4/33"); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Tests IP prefix contains another IP prefix for IPv4. |
| 411 | */ |
| 412 | @Test |
| 413 | public void testContainsIpPrefixIPv4() { |
| 414 | Ip4Prefix ipPrefix; |
| 415 | |
| 416 | ipPrefix = Ip4Prefix.valueOf("1.2.0.0/24"); |
| 417 | assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24"))); |
| 418 | assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32"))); |
| 419 | assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32"))); |
| 420 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16"))); |
| 421 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24"))); |
| 422 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16"))); |
| 423 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0"))); |
| 424 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32"))); |
| 425 | |
| 426 | ipPrefix = Ip4Prefix.valueOf("1.2.0.0/32"); |
| 427 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24"))); |
| 428 | assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32"))); |
| 429 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32"))); |
| 430 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16"))); |
| 431 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24"))); |
| 432 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16"))); |
| 433 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0"))); |
| 434 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32"))); |
| 435 | |
| 436 | ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0"); |
| 437 | assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24"))); |
| 438 | assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32"))); |
| 439 | assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32"))); |
| 440 | assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16"))); |
| 441 | assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24"))); |
| 442 | assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16"))); |
| 443 | assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0"))); |
| 444 | assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32"))); |
| 445 | |
| 446 | ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32"); |
| 447 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/24"))); |
| 448 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/32"))); |
| 449 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.4/32"))); |
| 450 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.2.0.0/16"))); |
| 451 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("1.3.0.0/24"))); |
| 452 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/16"))); |
| 453 | assertFalse(ipPrefix.contains(Ip4Prefix.valueOf("0.0.0.0/0"))); |
| 454 | assertTrue(ipPrefix.contains(Ip4Prefix.valueOf("255.255.255.255/32"))); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Tests IP prefix contains IP address for IPv4. |
| 459 | */ |
| 460 | @Test |
| 461 | public void testContainsIpAddressIPv4() { |
| 462 | Ip4Prefix ipPrefix; |
| 463 | |
| 464 | ipPrefix = Ip4Prefix.valueOf("1.2.0.0/24"); |
| 465 | assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0"))); |
| 466 | assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4"))); |
| 467 | assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0"))); |
| 468 | assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0"))); |
| 469 | assertFalse(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255"))); |
| 470 | |
| 471 | ipPrefix = Ip4Prefix.valueOf("1.2.0.0/32"); |
| 472 | assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0"))); |
| 473 | assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4"))); |
| 474 | assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0"))); |
| 475 | assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0"))); |
| 476 | assertFalse(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255"))); |
| 477 | |
| 478 | ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0"); |
| 479 | assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0"))); |
| 480 | assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4"))); |
| 481 | assertTrue(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0"))); |
| 482 | assertTrue(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0"))); |
| 483 | assertTrue(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255"))); |
| 484 | |
| 485 | ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32"); |
| 486 | assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.0"))); |
| 487 | assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.2.0.4"))); |
| 488 | assertFalse(ipPrefix.contains(Ip4Address.valueOf("1.3.0.0"))); |
| 489 | assertFalse(ipPrefix.contains(Ip4Address.valueOf("0.0.0.0"))); |
| 490 | assertTrue(ipPrefix.contains(Ip4Address.valueOf("255.255.255.255"))); |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Tests equality of {@link Ip4Prefix} for IPv4. |
| 495 | */ |
| 496 | @Test |
| 497 | public void testEqualityIPv4() { |
| 498 | new EqualsTester() |
| 499 | .addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/24"), |
| 500 | Ip4Prefix.valueOf("1.2.0.0/24"), |
| 501 | Ip4Prefix.valueOf("1.2.0.4/24")) |
| 502 | .addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/16"), |
| 503 | Ip4Prefix.valueOf("1.2.0.0/16")) |
| 504 | .addEqualityGroup(Ip4Prefix.valueOf("1.2.0.0/32"), |
| 505 | Ip4Prefix.valueOf("1.2.0.0/32")) |
| 506 | .addEqualityGroup(Ip4Prefix.valueOf("1.3.0.0/24"), |
| 507 | Ip4Prefix.valueOf("1.3.0.0/24")) |
| 508 | .addEqualityGroup(Ip4Prefix.valueOf("0.0.0.0/0"), |
| 509 | Ip4Prefix.valueOf("0.0.0.0/0")) |
| 510 | .addEqualityGroup(Ip4Prefix.valueOf("255.255.255.255/32"), |
| 511 | Ip4Prefix.valueOf("255.255.255.255/32")) |
| 512 | .testEquals(); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Tests object string representation for IPv4. |
| 517 | */ |
| 518 | @Test |
| 519 | public void testToStringIPv4() { |
| 520 | Ip4Prefix ipPrefix; |
| 521 | |
| 522 | ipPrefix = Ip4Prefix.valueOf("1.2.3.0/24"); |
| 523 | assertThat(ipPrefix.toString(), is("1.2.3.0/24")); |
| 524 | |
| 525 | ipPrefix = Ip4Prefix.valueOf("1.2.3.4/24"); |
| 526 | assertThat(ipPrefix.toString(), is("1.2.3.0/24")); |
| 527 | |
| 528 | ipPrefix = Ip4Prefix.valueOf("0.0.0.0/0"); |
| 529 | assertThat(ipPrefix.toString(), is("0.0.0.0/0")); |
| 530 | |
| 531 | ipPrefix = Ip4Prefix.valueOf("255.255.255.255/32"); |
| 532 | assertThat(ipPrefix.toString(), is("255.255.255.255/32")); |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 533 | } |
| 534 | } |