Pavlin Radoslavov

Completed the IPv6 implementation for IpPrefix, and added

a new set of IpPrefix unit tests.

Also, fix few nits in IpAddress and IpAddressTest
...@@ -30,7 +30,6 @@ import static com.google.common.base.Preconditions.checkState; ...@@ -30,7 +30,6 @@ import static com.google.common.base.Preconditions.checkState;
30 30
31 /** 31 /**
32 * A class representing an IP address. 32 * A class representing an IP address.
33 - * TODO: Add support for IPv6 as well.
34 */ 33 */
35 public final class IpAddress implements Comparable<IpAddress> { 34 public final class IpAddress implements Comparable<IpAddress> {
36 // IP Versions 35 // IP Versions
......
...@@ -17,8 +17,6 @@ package org.onlab.packet; ...@@ -17,8 +17,6 @@ package org.onlab.packet;
17 17
18 import java.util.Objects; 18 import java.util.Objects;
19 19
20 -// TODO: Add support for IPv6 as well.
21 -
22 /** 20 /**
23 * A class representing an IP prefix. A prefix consists of an IP address and 21 * A class representing an IP prefix. A prefix consists of an IP address and
24 * a subnet mask. 22 * a subnet mask.
...@@ -40,26 +38,39 @@ public final class IpPrefix { ...@@ -40,26 +38,39 @@ public final class IpPrefix {
40 * 38 *
41 * @param address the IP address 39 * @param address the IP address
42 * @param prefixLength the prefix length 40 * @param prefixLength the prefix length
41 + * @throws IllegalArgumentException if the prefix length value is invalid
43 */ 42 */
44 private IpPrefix(IpAddress address, int prefixLength) { 43 private IpPrefix(IpAddress address, int prefixLength) {
45 - checkPrefixLength(prefixLength); 44 + checkPrefixLength(address.version(), prefixLength);
46 this.address = IpAddress.makeMaskedAddress(address, prefixLength); 45 this.address = IpAddress.makeMaskedAddress(address, prefixLength);
47 this.prefixLength = (short) prefixLength; 46 this.prefixLength = (short) prefixLength;
48 } 47 }
49 48
50 /** 49 /**
51 - * Checks whether the prefix length is valid. 50 + * Returns the IP version of the prefix.
52 * 51 *
53 - * @param prefixLength the prefix length value to check 52 + * @return the IP version of the prefix
54 - * @throws IllegalArgumentException if the prefix length value is invalid
55 */ 53 */
56 - private static void checkPrefixLength(int prefixLength) { 54 + public IpAddress.Version version() {
57 - if ((prefixLength < 0) || (prefixLength > MAX_INET_MASK_LENGTH)) { 55 + return address.version();
58 - String msg = "Invalid prefix length " + prefixLength + ". " + 56 + }
59 - "The value must be in the interval [0, " + 57 +
60 - MAX_INET_MASK_LENGTH + "]"; 58 + /**
61 - throw new IllegalArgumentException(msg); 59 + * Returns the IP address value of the prefix.
60 + *
61 + * @return the IP address value of the prefix
62 + */
63 + public IpAddress address() {
64 + return address;
62 } 65 }
66 +
67 + /**
68 + * Returns the IP address prefix length.
69 + *
70 + * @return the IP address prefix length
71 + */
72 + public int prefixLength() {
73 + return prefixLength;
63 } 74 }
64 75
65 /** 76 /**
...@@ -68,6 +79,7 @@ public final class IpPrefix { ...@@ -68,6 +79,7 @@ public final class IpPrefix {
68 * @param address an integer representing the IPv4 address 79 * @param address an integer representing the IPv4 address
69 * @param prefixLength the prefix length 80 * @param prefixLength the prefix length
70 * @return an IP prefix 81 * @return an IP prefix
82 + * @throws IllegalArgumentException if the prefix length value is invalid
71 */ 83 */
72 public static IpPrefix valueOf(int address, int prefixLength) { 84 public static IpPrefix valueOf(int address, int prefixLength) {
73 return new IpPrefix(IpAddress.valueOf(address), prefixLength); 85 return new IpPrefix(IpAddress.valueOf(address), prefixLength);
...@@ -80,11 +92,11 @@ public final class IpPrefix { ...@@ -80,11 +92,11 @@ public final class IpPrefix {
80 * @param address the IP address value stored in network byte order 92 * @param address the IP address value stored in network byte order
81 * @param prefixLength the prefix length 93 * @param prefixLength the prefix length
82 * @return an IP prefix 94 * @return an IP prefix
95 + * @throws IllegalArgumentException if the prefix length value is invalid
83 */ 96 */
84 public static IpPrefix valueOf(IpAddress.Version version, byte[] address, 97 public static IpPrefix valueOf(IpAddress.Version version, byte[] address,
85 int prefixLength) { 98 int prefixLength) {
86 - return new IpPrefix(IpAddress.valueOf(version, address), 99 + return new IpPrefix(IpAddress.valueOf(version, address), prefixLength);
87 - prefixLength);
88 } 100 }
89 101
90 /** 102 /**
...@@ -93,6 +105,7 @@ public final class IpPrefix { ...@@ -93,6 +105,7 @@ public final class IpPrefix {
93 * @param address the IP address 105 * @param address the IP address
94 * @param prefixLength the prefix length 106 * @param prefixLength the prefix length
95 * @return an IP prefix 107 * @return an IP prefix
108 + * @throws IllegalArgumentException if the prefix length value is invalid
96 */ 109 */
97 public static IpPrefix valueOf(IpAddress address, int prefixLength) { 110 public static IpPrefix valueOf(IpAddress address, int prefixLength) {
98 return new IpPrefix(address, prefixLength); 111 return new IpPrefix(address, prefixLength);
...@@ -104,6 +117,7 @@ public final class IpPrefix { ...@@ -104,6 +117,7 @@ public final class IpPrefix {
104 * 117 *
105 * @param address an IP prefix in string form, e.g. "10.1.0.0/16" 118 * @param address an IP prefix in string form, e.g. "10.1.0.0/16"
106 * @return an IP prefix 119 * @return an IP prefix
120 + * @throws IllegalArgumentException if the arguments are invalid
107 */ 121 */
108 public static IpPrefix valueOf(String address) { 122 public static IpPrefix valueOf(String address) {
109 final String[] parts = address.split("/"); 123 final String[] parts = address.split("/");
...@@ -119,33 +133,6 @@ public final class IpPrefix { ...@@ -119,33 +133,6 @@ public final class IpPrefix {
119 } 133 }
120 134
121 /** 135 /**
122 - * Returns the IP version of the prefix.
123 - *
124 - * @return the IP version of the prefix
125 - */
126 - public IpAddress.Version version() {
127 - return address.version();
128 - }
129 -
130 - /**
131 - * Returns the IP address value of the prefix.
132 - *
133 - * @return the IP address value of the prefix
134 - */
135 - public IpAddress address() {
136 - return address;
137 - }
138 -
139 - /**
140 - * Returns the IP address prefix length.
141 - *
142 - * @return the IP address prefix length
143 - */
144 - public int prefixLength() {
145 - return prefixLength;
146 - }
147 -
148 - /**
149 * Determines whether a given IP prefix is contained within this prefix. 136 * Determines whether a given IP prefix is contained within this prefix.
150 * 137 *
151 * @param other the IP prefix to test 138 * @param other the IP prefix to test
...@@ -217,4 +204,35 @@ public final class IpPrefix { ...@@ -217,4 +204,35 @@ public final class IpPrefix {
217 builder.append(String.format("%d", prefixLength)); 204 builder.append(String.format("%d", prefixLength));
218 return builder.toString(); 205 return builder.toString();
219 } 206 }
207 +
208 + /**
209 + * Checks whether the prefix length is valid.
210 + *
211 + * @param version the IP address version
212 + * @param prefixLength the prefix length value to check
213 + * @throws IllegalArgumentException if the prefix length value is invalid
214 + */
215 + private static void checkPrefixLength(IpAddress.Version version,
216 + int prefixLength) {
217 + int maxPrefixLen = 0;
218 +
219 + switch (version) {
220 + case INET:
221 + maxPrefixLen = MAX_INET_MASK_LENGTH;
222 + break;
223 + case INET6:
224 + maxPrefixLen = MAX_INET6_MASK_LENGTH;
225 + break;
226 + default:
227 + String msg = "Invalid IP version " + version;
228 + throw new IllegalArgumentException(msg);
229 + }
230 +
231 + if ((prefixLength < 0) || (prefixLength > maxPrefixLen)) {
232 + String msg = "Invalid prefix length " + prefixLength + ". " +
233 + "The value must be in the interval [0, " +
234 + maxPrefixLen + "]";
235 + throw new IllegalArgumentException(msg);
236 + }
237 + }
220 } 238 }
......
...@@ -135,7 +135,7 @@ public class IpAddressTest { ...@@ -135,7 +135,7 @@ public class IpAddressTest {
135 * Tests returning an IPv4 address asn an integer. 135 * Tests returning an IPv4 address asn an integer.
136 */ 136 */
137 @Test 137 @Test
138 - public void testToint() { 138 + public void testToInt() {
139 IpAddress ipAddress; 139 IpAddress ipAddress;
140 140
141 ipAddress = IpAddress.valueOf("1.2.3.4"); 141 ipAddress = IpAddress.valueOf("1.2.3.4");
...@@ -149,10 +149,10 @@ public class IpAddressTest { ...@@ -149,10 +149,10 @@ public class IpAddressTest {
149 } 149 }
150 150
151 /** 151 /**
152 - * Tests valueOf() converter for an integer value. 152 + * Tests valueOf() converter for IPv4 integer value.
153 */ 153 */
154 @Test 154 @Test
155 - public void testValueOfForInteger() { 155 + public void testValueOfForIntegerIPv4() {
156 IpAddress ipAddress; 156 IpAddress ipAddress;
157 157
158 ipAddress = IpAddress.valueOf(0x01020304); 158 ipAddress = IpAddress.valueOf(0x01020304);
......
...@@ -15,123 +15,970 @@ ...@@ -15,123 +15,970 @@
15 */ 15 */
16 package org.onlab.packet; 16 package org.onlab.packet;
17 17
18 -import static org.junit.Assert.assertEquals; 18 +import com.google.common.testing.EqualsTester;
19 +import org.junit.Test;
20 +
21 +import static org.hamcrest.Matchers.equalTo;
22 +import static org.hamcrest.Matchers.is;
23 +import static org.junit.Assert.assertThat;
19 import static org.junit.Assert.assertFalse; 24 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue; 25 import static org.junit.Assert.assertTrue;
26 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
21 27
22 -import java.util.Arrays; 28 +/**
29 + * Tests for class {@link IpPrefix}.
30 + */
31 +public class IpPrefixTest {
32 + /**
33 + * Tests the immutability of {@link IpPrefix}.
34 + */
35 + @Test
36 + public void testImmutable() {
37 + assertThatClassIsImmutable(IpPrefix.class);
38 + }
23 39
24 -import org.junit.Test; 40 + /**
25 -import org.onlab.packet.IpAddress.Version; 41 + * Tests the maximum mask length.
42 + */
43 + @Test
44 + public void testMaxMaskLength() {
45 + assertThat(IpPrefix.MAX_INET_MASK_LENGTH, is(32));
46 + assertThat(IpPrefix.MAX_INET6_MASK_LENGTH, is(128));
47 + }
26 48
27 -import com.google.common.testing.EqualsTester; 49 + /**
50 + * Tests returning the IP version of the prefix.
51 + */
52 + @Test
53 + public void testVersion() {
54 + IpPrefix ipPrefix;
28 55
29 -public class IpPrefixTest { 56 + // IPv4
57 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
58 + assertThat(ipPrefix.version(), is(IpAddress.Version.INET));
30 59
31 - private static final byte [] BYTES1 = new byte [] {0xa, 0x0, 0x0, 0xa}; 60 + // IPv6
32 - private static final byte [] BYTES2 = new byte [] {0xa, 0x0, 0x0, 0xb}; 61 + ipPrefix = IpPrefix.valueOf("::/0");
33 - private static final int INTVAL0 = 0x0a000000; 62 + assertThat(ipPrefix.version(), is(IpAddress.Version.INET6));
34 - private static final int INTVAL1 = 0x0a00000a; 63 + }
35 - private static final int INTVAL2 = 0x0a00000b; 64 +
36 - private static final String STRVAL = "10.0.0.12/16"; 65 + /**
37 - private static final int MASK_LENGTH = 16; 66 + * Tests returning the IP address value and IP address prefix length of
38 - 67 + * an IPv4 prefix.
39 - @Test 68 + */
40 - public void testEquality() { 69 + @Test
41 - IpPrefix ip1 = IpPrefix.valueOf(IpAddress.Version.INET, 70 + public void testAddressAndPrefixLengthIPv4() {
42 - BYTES1, IpPrefix.MAX_INET_MASK_LENGTH); 71 + IpPrefix ipPrefix;
43 - IpPrefix ip2 = IpPrefix.valueOf(INTVAL1, IpPrefix.MAX_INET_MASK_LENGTH); 72 +
44 - IpPrefix ip3 = IpPrefix.valueOf(IpAddress.Version.INET, 73 + ipPrefix = IpPrefix.valueOf("1.2.3.0/24");
45 - BYTES2, IpPrefix.MAX_INET_MASK_LENGTH); 74 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.0")));
46 - IpPrefix ip4 = IpPrefix.valueOf(INTVAL2, IpPrefix.MAX_INET_MASK_LENGTH); 75 + assertThat(ipPrefix.prefixLength(), is(24));
47 - IpPrefix ip5 = IpPrefix.valueOf(STRVAL); 76 +
48 - 77 + ipPrefix = IpPrefix.valueOf("1.2.3.4/24");
49 - new EqualsTester().addEqualityGroup(ip1, ip2) 78 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.0")));
50 - .addEqualityGroup(ip3, ip4) 79 + assertThat(ipPrefix.prefixLength(), is(24));
51 - .addEqualityGroup(ip5) 80 +
52 - .testEquals(); 81 + ipPrefix = IpPrefix.valueOf("1.2.3.4/32");
82 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.4")));
83 + assertThat(ipPrefix.prefixLength(), is(32));
84 +
85 + ipPrefix = IpPrefix.valueOf("1.2.3.5/32");
86 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.5")));
87 + assertThat(ipPrefix.prefixLength(), is(32));
88 +
89 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
90 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("0.0.0.0")));
91 + assertThat(ipPrefix.prefixLength(), is(0));
92 +
93 + ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
94 + assertThat(ipPrefix.address(),
95 + equalTo(IpAddress.valueOf("255.255.255.255")));
96 + assertThat(ipPrefix.prefixLength(), is(32));
97 + }
98 +
99 + /**
100 + * Tests returning the IP address value and IP address prefix length of
101 + * an IPv6 prefix.
102 + */
103 + @Test
104 + public void testAddressAndPrefixLengthIPv6() {
105 + IpPrefix ipPrefix;
106 +
107 + ipPrefix = IpPrefix.valueOf("1100::/8");
108 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1100::")));
109 + assertThat(ipPrefix.prefixLength(), is(8));
110 +
111 + ipPrefix =
112 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8");
113 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1100::")));
114 + assertThat(ipPrefix.prefixLength(), is(8));
115 +
116 + ipPrefix =
117 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8800/120");
118 + assertThat(ipPrefix.address(),
119 + equalTo(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8800")));
120 + assertThat(ipPrefix.prefixLength(), is(120));
121 +
122 + ipPrefix =
123 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/128");
124 + assertThat(ipPrefix.address(),
125 + equalTo(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885")));
126 + assertThat(ipPrefix.prefixLength(), is(128));
127 +
128 + ipPrefix = IpPrefix.valueOf("::/0");
129 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("::")));
130 + assertThat(ipPrefix.prefixLength(), is(0));
131 +
132 + ipPrefix =
133 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
134 + assertThat(ipPrefix.address(),
135 + equalTo(IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
136 + assertThat(ipPrefix.prefixLength(), is(128));
137 +
138 + ipPrefix =
139 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/64");
140 + assertThat(ipPrefix.address(),
141 + equalTo(IpAddress.valueOf("1111:2222:3333:4444::")));
142 + assertThat(ipPrefix.prefixLength(), is(64));
143 + }
144 +
145 + /**
146 + * Tests valueOf() converter for IPv4 integer value.
147 + */
148 + @Test
149 + public void testValueOfForIntegerIPv4() {
150 + IpPrefix ipPrefix;
151 +
152 + ipPrefix = IpPrefix.valueOf(0x01020304, 24);
153 + assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
154 +
155 + ipPrefix = IpPrefix.valueOf(0x01020304, 32);
156 + assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
157 +
158 + ipPrefix = IpPrefix.valueOf(0x01020305, 32);
159 + assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
160 +
161 + ipPrefix = IpPrefix.valueOf(0, 0);
162 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
163 +
164 + ipPrefix = IpPrefix.valueOf(0, 32);
165 + assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
166 +
167 + ipPrefix = IpPrefix.valueOf(0xffffffff, 0);
168 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
169 +
170 + ipPrefix = IpPrefix.valueOf(0xffffffff, 16);
171 + assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
172 +
173 + ipPrefix = IpPrefix.valueOf(0xffffffff, 32);
174 + assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
175 + }
176 +
177 + /**
178 + * Tests invalid valueOf() converter for IPv4 integer value and
179 + * negative prefix length.
180 + */
181 + @Test(expected = IllegalArgumentException.class)
182 + public void testInvalidValueOfIntegerNegativePrefixLengthIPv4() {
183 + IpPrefix ipPrefix;
184 +
185 + ipPrefix = IpPrefix.valueOf(0x01020304, -1);
186 + }
187 +
188 + /**
189 + * Tests invalid valueOf() converter for IPv4 integer value and
190 + * too long prefix length.
191 + */
192 + @Test(expected = IllegalArgumentException.class)
193 + public void testInvalidValueOfIntegerTooLongPrefixLengthIPv4() {
194 + IpPrefix ipPrefix;
195 +
196 + ipPrefix = IpPrefix.valueOf(0x01020304, 33);
197 + }
198 +
199 + /**
200 + * Tests valueOf() converter for IPv4 byte array.
201 + */
202 + @Test
203 + public void testValueOfByteArrayIPv4() {
204 + IpPrefix ipPrefix;
205 + byte[] value;
206 +
207 + value = new byte[] {1, 2, 3, 4};
208 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24);
209 + assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
210 +
211 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32);
212 + assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
213 +
214 + value = new byte[] {1, 2, 3, 5};
215 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32);
216 + assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
217 +
218 + value = new byte[] {0, 0, 0, 0};
219 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 0);
220 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
221 +
222 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32);
223 + assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
224 +
225 + value = new byte[] {(byte) 0xff, (byte) 0xff,
226 + (byte) 0xff, (byte) 0xff};
227 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 0);
228 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
229 +
230 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 16);
231 + assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
232 +
233 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32);
234 + assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
235 + }
236 +
237 + /**
238 + * Tests valueOf() converter for IPv6 byte array.
239 + */
240 + @Test
241 + public void testValueOfByteArrayIPv6() {
242 + IpPrefix ipPrefix;
243 + byte[] value;
244 +
245 + value = new byte[] {0x11, 0x11, 0x22, 0x22,
246 + 0x33, 0x33, 0x44, 0x44,
247 + 0x55, 0x55, 0x66, 0x66,
248 + 0x77, 0x77, (byte) 0x88, (byte) 0x88};
249 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120);
250 + assertThat(ipPrefix.toString(),
251 + is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
252 +
253 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128);
254 + assertThat(ipPrefix.toString(),
255 + is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
256 +
257 + value = new byte[] {0x00, 0x00, 0x00, 0x00,
258 + 0x00, 0x00, 0x00, 0x00,
259 + 0x00, 0x00, 0x00, 0x00,
260 + 0x00, 0x00, 0x00, 0x00};
261 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 0);
262 + assertThat(ipPrefix.toString(), is("::/0"));
263 +
264 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128);
265 + assertThat(ipPrefix.toString(), is("::/128"));
266 +
267 + value = new byte[] {(byte) 0xff, (byte) 0xff,
268 + (byte) 0xff, (byte) 0xff,
269 + (byte) 0xff, (byte) 0xff,
270 + (byte) 0xff, (byte) 0xff,
271 + (byte) 0xff, (byte) 0xff,
272 + (byte) 0xff, (byte) 0xff,
273 + (byte) 0xff, (byte) 0xff,
274 + (byte) 0xff, (byte) 0xff};
275 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 0);
276 + assertThat(ipPrefix.toString(), is("::/0"));
277 +
278 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 64);
279 + assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
280 +
281 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128);
282 + assertThat(ipPrefix.toString(),
283 + is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
284 + }
285 +
286 + /**
287 + * Tests invalid valueOf() converter for a null array for IPv4.
288 + */
289 + @Test(expected = NullPointerException.class)
290 + public void testInvalidValueOfNullArrayIPv4() {
291 + IpPrefix ipPrefix;
292 + byte[] value;
293 +
294 + value = null;
295 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24);
296 + }
297 +
298 + /**
299 + * Tests invalid valueOf() converter for a null array for IPv6.
300 + */
301 + @Test(expected = NullPointerException.class)
302 + public void testInvalidValueOfNullArrayIPv6() {
303 + IpPrefix ipPrefix;
304 + byte[] value;
305 +
306 + value = null;
307 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120);
308 + }
309 +
310 + /**
311 + * Tests invalid valueOf() converter for a short array for IPv4.
312 + */
313 + @Test(expected = IllegalArgumentException.class)
314 + public void testInvalidValueOfShortArrayIPv4() {
315 + IpPrefix ipPrefix;
316 + byte[] value;
317 +
318 + value = new byte[] {1, 2, 3};
319 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24);
320 + }
321 +
322 + /**
323 + * Tests invalid valueOf() converter for a short array for IPv6.
324 + */
325 + @Test(expected = IllegalArgumentException.class)
326 + public void testInvalidValueOfShortArrayIPv6() {
327 + IpPrefix ipPrefix;
328 + byte[] value;
329 +
330 + value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
331 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120);
332 + }
333 +
334 + /**
335 + * Tests invalid valueOf() converter for IPv4 byte array and
336 + * negative prefix length.
337 + */
338 + @Test(expected = IllegalArgumentException.class)
339 + public void testInvalidValueOfByteArrayNegativePrefixLengthIPv4() {
340 + IpPrefix ipPrefix;
341 + byte[] value;
342 +
343 + value = new byte[] {1, 2, 3, 4};
344 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, -1);
345 + }
346 +
347 + /**
348 + * Tests invalid valueOf() converter for IPv6 byte array and
349 + * negative prefix length.
350 + */
351 + @Test(expected = IllegalArgumentException.class)
352 + public void testInvalidValueOfByteArrayNegativePrefixLengthIPv6() {
353 + IpPrefix ipPrefix;
354 + byte[] value;
355 +
356 + value = new byte[] {0x11, 0x11, 0x22, 0x22,
357 + 0x33, 0x33, 0x44, 0x44,
358 + 0x55, 0x55, 0x66, 0x66,
359 + 0x77, 0x77, (byte) 0x88, (byte) 0x88};
360 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, -1);
361 + }
362 +
363 + /**
364 + * Tests invalid valueOf() converter for IPv4 byte array and
365 + * too long prefix length.
366 + */
367 + @Test(expected = IllegalArgumentException.class)
368 + public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv4() {
369 + IpPrefix ipPrefix;
370 + byte[] value;
371 +
372 + value = new byte[] {1, 2, 3, 4};
373 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 33);
374 + }
375 +
376 + /**
377 + * Tests invalid valueOf() converter for IPv6 byte array and
378 + * too long prefix length.
379 + */
380 + @Test(expected = IllegalArgumentException.class)
381 + public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv6() {
382 + IpPrefix ipPrefix;
383 + byte[] value;
384 +
385 + value = new byte[] {0x11, 0x11, 0x22, 0x22,
386 + 0x33, 0x33, 0x44, 0x44,
387 + 0x55, 0x55, 0x66, 0x66,
388 + 0x77, 0x77, (byte) 0x88, (byte) 0x88};
389 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 129);
390 + }
391 +
392 + /**
393 + * Tests valueOf() converter for IPv4 address.
394 + */
395 + @Test
396 + public void testValueOfAddressIPv4() {
397 + IpAddress ipAddress;
398 + IpPrefix ipPrefix;
399 +
400 + ipAddress = IpAddress.valueOf("1.2.3.4");
401 + ipPrefix = IpPrefix.valueOf(ipAddress, 24);
402 + assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
403 +
404 + ipPrefix = IpPrefix.valueOf(ipAddress, 32);
405 + assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
406 +
407 + ipAddress = IpAddress.valueOf("1.2.3.5");
408 + ipPrefix = IpPrefix.valueOf(ipAddress, 32);
409 + assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
410 +
411 + ipAddress = IpAddress.valueOf("0.0.0.0");
412 + ipPrefix = IpPrefix.valueOf(ipAddress, 0);
413 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
53 414
54 - // string conversions 415 + ipPrefix = IpPrefix.valueOf(ipAddress, 32);
55 - IpPrefix ip6 = IpPrefix.valueOf(IpAddress.Version.INET, 416 + assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
56 - BYTES1, MASK_LENGTH); 417 +
57 - IpPrefix ip7 = IpPrefix.valueOf("10.0.0.10/16"); 418 + ipAddress = IpAddress.valueOf("255.255.255.255");
58 - IpPrefix ip8 = IpPrefix.valueOf(IpAddress.Version.INET, 419 + ipPrefix = IpPrefix.valueOf(ipAddress, 0);
59 - new byte [] {0xa, 0x0, 0x0, 0xc}, 16); 420 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
60 - assertEquals("incorrect address conversion", ip6, ip7); 421 +
61 - assertEquals("incorrect address conversion", ip5, ip8); 422 + ipPrefix = IpPrefix.valueOf(ipAddress, 16);
423 + assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
424 +
425 + ipPrefix = IpPrefix.valueOf(ipAddress, 32);
426 + assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
62 } 427 }
63 428
429 + /**
430 + * Tests valueOf() converter for IPv6 address.
431 + */
64 @Test 432 @Test
65 - public void basics() { 433 + public void testValueOfAddressIPv6() {
66 - IpPrefix ip1 = IpPrefix.valueOf(IpAddress.Version.INET, 434 + IpAddress ipAddress;
67 - BYTES1, MASK_LENGTH); 435 + IpPrefix ipPrefix;
68 - final byte [] bytes = new byte [] {0xa, 0x0, 0x0, 0x0}; 436 +
437 + ipAddress =
438 + IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
439 + ipPrefix = IpPrefix.valueOf(ipAddress, 120);
440 + assertThat(ipPrefix.toString(),
441 + is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
442 +
443 + ipPrefix = IpPrefix.valueOf(ipAddress, 128);
444 + assertThat(ipPrefix.toString(),
445 + is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
446 +
447 + ipAddress = IpAddress.valueOf("::");
448 + ipPrefix = IpPrefix.valueOf(ipAddress, 0);
449 + assertThat(ipPrefix.toString(), is("::/0"));
450 +
451 + ipPrefix = IpPrefix.valueOf(ipAddress, 128);
452 + assertThat(ipPrefix.toString(), is("::/128"));
69 453
70 - // check fields 454 + ipAddress =
71 - assertEquals("incorrect IP Version", Version.INET, ip1.version()); 455 + IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
72 - assertEquals("incorrect netmask", 16, ip1.prefixLength()); 456 + ipPrefix = IpPrefix.valueOf(ipAddress, 0);
73 - assertTrue("faulty toOctets()", 457 + assertThat(ipPrefix.toString(), is("::/0"));
74 - Arrays.equals(bytes, ip1.address().toOctets())); 458 +
75 - assertEquals("faulty toInt()", INTVAL0, ip1.address().toInt()); 459 + ipPrefix = IpPrefix.valueOf(ipAddress, 64);
76 - assertEquals("faulty toString()", "10.0.0.0/16", ip1.toString()); 460 + assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
461 +
462 + ipPrefix = IpPrefix.valueOf(ipAddress, 128);
463 + assertThat(ipPrefix.toString(),
464 + is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
465 + }
466 +
467 + /**
468 + * Tests invalid valueOf() converter for a null IP address.
469 + */
470 + @Test(expected = NullPointerException.class)
471 + public void testInvalidValueOfNullAddress() {
472 + IpAddress ipAddress;
473 + IpPrefix ipPrefix;
474 +
475 + ipAddress = null;
476 + ipPrefix = IpPrefix.valueOf(ipAddress, 24);
77 } 477 }
78 478
479 + /**
480 + * Tests invalid valueOf() converter for IPv4 address and
481 + * negative prefix length.
482 + */
483 + @Test(expected = IllegalArgumentException.class)
484 + public void testInvalidValueOfAddressNegativePrefixLengthIPv4() {
485 + IpAddress ipAddress;
486 + IpPrefix ipPrefix;
487 +
488 + ipAddress = IpAddress.valueOf("1.2.3.4");
489 + ipPrefix = IpPrefix.valueOf(ipAddress, -1);
490 + }
491 +
492 + /**
493 + * Tests invalid valueOf() converter for IPv6 address and
494 + * negative prefix length.
495 + */
496 + @Test(expected = IllegalArgumentException.class)
497 + public void testInvalidValueOfAddressNegativePrefixLengthIPv6() {
498 + IpAddress ipAddress;
499 + IpPrefix ipPrefix;
500 +
501 + ipAddress =
502 + IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
503 + ipPrefix = IpPrefix.valueOf(ipAddress, -1);
504 + }
505 +
506 + /**
507 + * Tests invalid valueOf() converter for IPv4 address and
508 + * too long prefix length.
509 + */
510 + @Test(expected = IllegalArgumentException.class)
511 + public void testInvalidValueOfAddressTooLongPrefixLengthIPv4() {
512 + IpAddress ipAddress;
513 + IpPrefix ipPrefix;
514 +
515 + ipAddress = IpAddress.valueOf("1.2.3.4");
516 + ipPrefix = IpPrefix.valueOf(ipAddress, 33);
517 + }
518 +
519 + /**
520 + * Tests invalid valueOf() converter for IPv6 address and
521 + * too long prefix length.
522 + */
523 + @Test(expected = IllegalArgumentException.class)
524 + public void testInvalidValueOfAddressTooLongPrefixLengthIPv6() {
525 + IpAddress ipAddress;
526 + IpPrefix ipPrefix;
527 +
528 + ipAddress =
529 + IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
530 + ipPrefix = IpPrefix.valueOf(ipAddress, 129);
531 + }
532 +
533 + /**
534 + * Tests valueOf() converter for IPv4 string.
535 + */
79 @Test 536 @Test
80 - public void netmasks() { 537 + public void testValueOfStringIPv4() {
81 - // masked 538 + IpPrefix ipPrefix;
82 - IpPrefix ip1 = IpPrefix.valueOf(IpAddress.Version.INET, 539 +
83 - BYTES1, MASK_LENGTH); 540 + ipPrefix = IpPrefix.valueOf("1.2.3.4/24");
84 - IpPrefix ip2 = IpPrefix.valueOf("10.0.0.10/16"); 541 + assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
85 - IpPrefix ip3 = IpPrefix.valueOf("10.0.0.0/16"); 542 +
86 - assertEquals("incorrect binary masked address", 543 + ipPrefix = IpPrefix.valueOf("1.2.3.4/32");
87 - ip1.toString(), "10.0.0.0/16"); 544 + assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
88 - assertEquals("incorrect string masked address", 545 +
89 - ip2.toString(), "10.0.0.0/16"); 546 + ipPrefix = IpPrefix.valueOf("1.2.3.5/32");
90 - assertEquals("incorrect network address", 547 + assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
91 - ip2.toString(), "10.0.0.0/16"); 548 +
549 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
550 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
551 +
552 + ipPrefix = IpPrefix.valueOf("0.0.0.0/32");
553 + assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
554 +
555 + ipPrefix = IpPrefix.valueOf("255.255.255.255/0");
556 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
557 +
558 + ipPrefix = IpPrefix.valueOf("255.255.255.255/16");
559 + assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
560 +
561 + ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
562 + assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
92 } 563 }
93 564
565 + /**
566 + * Tests valueOf() converter for IPv6 string.
567 + */
94 @Test 568 @Test
95 - public void testContainsIpPrefix() { 569 + public void testValueOfStringIPv6() {
96 - IpPrefix slash31 = IpPrefix.valueOf(IpAddress.Version.INET, 570 + IpPrefix ipPrefix;
97 - BYTES1, 31); 571 +
98 - IpPrefix slash32 = IpPrefix.valueOf(IpAddress.Version.INET, 572 + ipPrefix =
99 - BYTES1, 32); 573 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/120");
100 - IpPrefix differentSlash32 = IpPrefix.valueOf(IpAddress.Version.INET, 574 + assertThat(ipPrefix.toString(),
101 - BYTES2, 32); 575 + is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
576 +
577 + ipPrefix =
578 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/128");
579 + assertThat(ipPrefix.toString(),
580 + is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
581 +
582 + ipPrefix = IpPrefix.valueOf("::/0");
583 + assertThat(ipPrefix.toString(), is("::/0"));
584 +
585 + ipPrefix = IpPrefix.valueOf("::/128");
586 + assertThat(ipPrefix.toString(), is("::/128"));
587 +
588 + ipPrefix =
589 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0");
590 + assertThat(ipPrefix.toString(), is("::/0"));
591 +
592 + ipPrefix =
593 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64");
594 + assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
102 595
103 - assertTrue(slash31.contains(differentSlash32)); 596 + ipPrefix =
104 - assertFalse(differentSlash32.contains(slash31)); 597 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
598 + assertThat(ipPrefix.toString(),
599 + is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
600 + }
601 +
602 + /**
603 + * Tests invalid valueOf() converter for a null string.
604 + */
605 + @Test(expected = NullPointerException.class)
606 + public void testInvalidValueOfNullString() {
607 + IpPrefix ipPrefix;
608 + String fromString;
105 609
106 - assertTrue(slash31.contains(slash32)); 610 + fromString = null;
107 - assertFalse(slash32.contains(differentSlash32)); 611 + ipPrefix = IpPrefix.valueOf(fromString);
108 - assertFalse(differentSlash32.contains(slash32)); 612 + }
109 613
110 - IpPrefix zero = IpPrefix.valueOf("0.0.0.0/0"); 614 + /**
111 - assertTrue(zero.contains(differentSlash32)); 615 + * Tests invalid valueOf() converter for an empty string.
112 - assertFalse(differentSlash32.contains(zero)); 616 + */
617 + @Test(expected = IllegalArgumentException.class)
618 + public void testInvalidValueOfEmptyString() {
619 + IpPrefix ipPrefix;
620 + String fromString;
113 621
114 - IpPrefix slash8 = IpPrefix.valueOf("10.0.0.0/8"); 622 + fromString = "";
115 - assertTrue(slash8.contains(slash31)); 623 + ipPrefix = IpPrefix.valueOf(fromString);
116 - assertFalse(slash31.contains(slash8));
117 } 624 }
118 625
626 + /**
627 + * Tests invalid valueOf() converter for an incorrect string.
628 + */
629 + @Test(expected = IllegalArgumentException.class)
630 + public void testInvalidValueOfIncorrectString() {
631 + IpPrefix ipPrefix;
632 + String fromString;
633 +
634 + fromString = "NoSuchIpPrefix";
635 + ipPrefix = IpPrefix.valueOf(fromString);
636 + }
637 +
638 + /**
639 + * Tests invalid valueOf() converter for IPv4 string and
640 + * negative prefix length.
641 + */
642 + @Test(expected = IllegalArgumentException.class)
643 + public void testInvalidValueOfStringNegativePrefixLengthIPv4() {
644 + IpPrefix ipPrefix;
645 +
646 + ipPrefix = IpPrefix.valueOf("1.2.3.4/-1");
647 + }
648 +
649 + /**
650 + * Tests invalid valueOf() converter for IPv6 string and
651 + * negative prefix length.
652 + */
653 + @Test(expected = IllegalArgumentException.class)
654 + public void testInvalidValueOfStringNegativePrefixLengthIPv6() {
655 + IpPrefix ipPrefix;
656 +
657 + ipPrefix =
658 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/-1");
659 + }
660 +
661 + /**
662 + * Tests invalid valueOf() converter for IPv4 string and
663 + * too long prefix length.
664 + */
665 + @Test(expected = IllegalArgumentException.class)
666 + public void testInvalidValueOfStringTooLongPrefixLengthIPv4() {
667 + IpPrefix ipPrefix;
668 +
669 + ipPrefix = IpPrefix.valueOf("1.2.3.4/33");
670 + }
671 +
672 + /**
673 + * Tests invalid valueOf() converter for IPv6 string and
674 + * too long prefix length.
675 + */
676 + @Test(expected = IllegalArgumentException.class)
677 + public void testInvalidValueOfStringTooLongPrefixLengthIPv6() {
678 + IpPrefix ipPrefix;
679 +
680 + ipPrefix =
681 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/129");
682 + }
683 +
684 + /**
685 + * Tests IP prefix contains another IP prefix for IPv4.
686 + */
687 + @Test
688 + public void testContainsIpPrefixIPv4() {
689 + IpPrefix ipPrefix;
690 +
691 + ipPrefix = IpPrefix.valueOf("1.2.0.0/24");
692 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
693 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32")));
694 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32")));
695 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16")));
696 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24")));
697 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16")));
698 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0")));
699 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
700 +
701 + ipPrefix = IpPrefix.valueOf("1.2.0.0/32");
702 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
703 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32")));
704 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32")));
705 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16")));
706 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24")));
707 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16")));
708 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0")));
709 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
710 +
711 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
712 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
713 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32")));
714 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32")));
715 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16")));
716 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24")));
717 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16")));
718 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0")));
719 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
720 +
721 + ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
722 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
723 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32")));
724 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32")));
725 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16")));
726 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24")));
727 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16")));
728 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0")));
729 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
730 + }
731 +
732 + /**
733 + * Tests IP prefix contains another IP prefix for IPv6.
734 + */
735 + @Test
736 + public void testContainsIpPrefixIPv6() {
737 + IpPrefix ipPrefix;
738 +
739 + ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/120");
740 + assertTrue(ipPrefix.contains(
741 + IpPrefix.valueOf("1111:2222:3333:4444::/120")));
742 + assertTrue(ipPrefix.contains(
743 + IpPrefix.valueOf("1111:2222:3333:4444::/128")));
744 + assertTrue(ipPrefix.contains(
745 + IpPrefix.valueOf("1111:2222:3333:4444::1/128")));
746 + assertFalse(ipPrefix.contains(
747 + IpPrefix.valueOf("1111:2222:3333:4444::/64")));
748 + assertFalse(ipPrefix.contains(
749 + IpPrefix.valueOf("1111:2222:3333:4445::/120")));
750 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64")));
751 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0")));
752 + assertFalse(ipPrefix.contains(
753 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
754 +
755 + ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/128");
756 + assertFalse(ipPrefix.contains(
757 + IpPrefix.valueOf("1111:2222:3333:4444::/120")));
758 + assertTrue(ipPrefix.contains(
759 + IpPrefix.valueOf("1111:2222:3333:4444::/128")));
760 + assertFalse(ipPrefix.contains(
761 + IpPrefix.valueOf("1111:2222:3333:4444::1/128")));
762 + assertFalse(ipPrefix.contains(
763 + IpPrefix.valueOf("1111:2222:3333:4444::/64")));
764 + assertFalse(ipPrefix.contains(
765 + IpPrefix.valueOf("1111:2222:3333:4445::/120")));
766 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64")));
767 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0")));
768 + assertFalse(ipPrefix.contains(
769 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
770 +
771 + ipPrefix = IpPrefix.valueOf("::/0");
772 + assertTrue(ipPrefix.contains(
773 + IpPrefix.valueOf("1111:2222:3333:4444::/120")));
774 + assertTrue(ipPrefix.contains(
775 + IpPrefix.valueOf("1111:2222:3333:4444::/128")));
776 + assertTrue(ipPrefix.contains(
777 + IpPrefix.valueOf("1111:2222:3333:4444::1/128")));
778 + assertTrue(ipPrefix.contains(
779 + IpPrefix.valueOf("1111:2222:3333:4444::/64")));
780 + assertTrue(ipPrefix.contains(
781 + IpPrefix.valueOf("1111:2222:3333:4445::/120")));
782 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("::/64")));
783 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("::/0")));
784 + assertTrue(ipPrefix.contains(
785 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
786 +
787 + ipPrefix =
788 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
789 + assertFalse(ipPrefix.contains(
790 + IpPrefix.valueOf("1111:2222:3333:4444::/120")));
791 + assertFalse(ipPrefix.contains(
792 + IpPrefix.valueOf("1111:2222:3333:4444::/128")));
793 + assertFalse(ipPrefix.contains(
794 + IpPrefix.valueOf("1111:2222:3333:4444::1/128")));
795 + assertFalse(ipPrefix.contains(
796 + IpPrefix.valueOf("1111:2222:3333:4444::/64")));
797 + assertFalse(ipPrefix.contains(
798 + IpPrefix.valueOf("1111:2222:3333:4445::/120")));
799 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64")));
800 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0")));
801 + assertTrue(ipPrefix.contains(
802 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
803 + }
804 +
805 + /**
806 + * Tests IP prefix contains IP address for IPv4.
807 + */
808 + @Test
809 + public void testContainsIpAddressIPv4() {
810 + IpPrefix ipPrefix;
811 +
812 + ipPrefix = IpPrefix.valueOf("1.2.0.0/24");
813 + assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
814 + assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.4")));
815 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0")));
816 + assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0")));
817 + assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
818 +
819 + ipPrefix = IpPrefix.valueOf("1.2.0.0/32");
820 + assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
821 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.4")));
822 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0")));
823 + assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0")));
824 + assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
825 +
826 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
827 + assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
828 + assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.4")));
829 + assertTrue(ipPrefix.contains(IpAddress.valueOf("1.3.0.0")));
830 + assertTrue(ipPrefix.contains(IpAddress.valueOf("0.0.0.0")));
831 + assertTrue(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
832 +
833 + ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
834 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
835 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.4")));
836 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0")));
837 + assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0")));
838 + assertTrue(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
839 + }
840 +
841 + /**
842 + * Tests IP prefix contains IP address for IPv6.
843 + */
844 + @Test
845 + public void testContainsIpAddressIPv6() {
846 + IpPrefix ipPrefix;
847 +
848 + ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/120");
849 + assertTrue(ipPrefix.contains(
850 + IpAddress.valueOf("1111:2222:3333:4444::")));
851 + assertTrue(ipPrefix.contains(
852 + IpAddress.valueOf("1111:2222:3333:4444::1")));
853 + assertFalse(ipPrefix.contains(
854 + IpAddress.valueOf("1111:2222:3333:4445::")));
855 + assertFalse(ipPrefix.contains(IpAddress.valueOf("::")));
856 + assertFalse(ipPrefix.contains(
857 + IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
858 +
859 + ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/128");
860 + assertTrue(ipPrefix.contains(
861 + IpAddress.valueOf("1111:2222:3333:4444::")));
862 + assertFalse(ipPrefix.contains(
863 + IpAddress.valueOf("1111:2222:3333:4444::1")));
864 + assertFalse(ipPrefix.contains(
865 + IpAddress.valueOf("1111:2222:3333:4445::")));
866 + assertFalse(ipPrefix.contains(IpAddress.valueOf("::")));
867 + assertFalse(ipPrefix.contains(
868 + IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
869 +
870 + ipPrefix = IpPrefix.valueOf("::/0");
871 + assertTrue(ipPrefix.contains(
872 + IpAddress.valueOf("1111:2222:3333:4444::")));
873 + assertTrue(ipPrefix.contains(
874 + IpAddress.valueOf("1111:2222:3333:4444::1")));
875 + assertTrue(ipPrefix.contains(
876 + IpAddress.valueOf("1111:2222:3333:4445::")));
877 + assertTrue(ipPrefix.contains(IpAddress.valueOf("::")));
878 + assertTrue(ipPrefix.contains(
879 + IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
880 +
881 + ipPrefix =
882 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
883 + assertFalse(ipPrefix.contains(
884 + IpAddress.valueOf("1111:2222:3333:4444::")));
885 + assertFalse(ipPrefix.contains(
886 + IpAddress.valueOf("1111:2222:3333:4444::1")));
887 + assertFalse(ipPrefix.contains(
888 + IpAddress.valueOf("1111:2222:3333:4445::")));
889 + assertFalse(ipPrefix.contains(IpAddress.valueOf("::")));
890 + assertTrue(ipPrefix.contains(
891 + IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
892 + }
893 +
894 + /**
895 + * Tests equality of {@link IpPrefix} for IPv4.
896 + */
897 + @Test
898 + public void testEqualityIPv4() {
899 + new EqualsTester()
900 + .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/24"),
901 + IpPrefix.valueOf("1.2.0.0/24"),
902 + IpPrefix.valueOf("1.2.0.4/24"))
903 + .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/16"),
904 + IpPrefix.valueOf("1.2.0.0/16"))
905 + .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/32"),
906 + IpPrefix.valueOf("1.2.0.0/32"))
907 + .addEqualityGroup(IpPrefix.valueOf("1.3.0.0/24"),
908 + IpPrefix.valueOf("1.3.0.0/24"))
909 + .addEqualityGroup(IpPrefix.valueOf("0.0.0.0/0"),
910 + IpPrefix.valueOf("0.0.0.0/0"))
911 + .addEqualityGroup(IpPrefix.valueOf("255.255.255.255/32"),
912 + IpPrefix.valueOf("255.255.255.255/32"))
913 + .testEquals();
914 + }
915 +
916 + /**
917 + * Tests equality of {@link IpPrefix} for IPv6.
918 + */
919 + @Test
920 + public void testEqualityIPv6() {
921 + new EqualsTester()
922 + .addEqualityGroup(
923 + IpPrefix.valueOf("1111:2222:3333:4444::/120"),
924 + IpPrefix.valueOf("1111:2222:3333:4444::1/120"),
925 + IpPrefix.valueOf("1111:2222:3333:4444::/120"))
926 + .addEqualityGroup(
927 + IpPrefix.valueOf("1111:2222:3333:4444::/64"),
928 + IpPrefix.valueOf("1111:2222:3333:4444::/64"))
929 + .addEqualityGroup(
930 + IpPrefix.valueOf("1111:2222:3333:4444::/128"),
931 + IpPrefix.valueOf("1111:2222:3333:4444::/128"))
932 + .addEqualityGroup(
933 + IpPrefix.valueOf("1111:2222:3333:4445::/64"),
934 + IpPrefix.valueOf("1111:2222:3333:4445::/64"))
935 + .addEqualityGroup(
936 + IpPrefix.valueOf("::/0"),
937 + IpPrefix.valueOf("::/0"))
938 + .addEqualityGroup(
939 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"),
940 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))
941 + .testEquals();
942 + }
943 +
944 + /**
945 + * Tests object string representation for IPv4.
946 + */
119 @Test 947 @Test
120 - public void testContainsIpAddress() { 948 + public void testToStringIPv4() {
121 - IpPrefix slash31 = IpPrefix.valueOf(IpAddress.Version.INET, 949 + IpPrefix ipPrefix;
122 - BYTES1, 31);
123 - IpAddress addr32 = IpAddress.valueOf(IpAddress.Version.INET, BYTES1);
124 950
125 - assertTrue(slash31.contains(addr32)); 951 + ipPrefix = IpPrefix.valueOf("1.2.3.0/24");
952 + assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
953 +
954 + ipPrefix = IpPrefix.valueOf("1.2.3.4/24");
955 + assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
956 +
957 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
958 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
959 +
960 + ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
961 + assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
962 + }
963 +
964 + /**
965 + * Tests object string representation for IPv6.
966 + */
967 + @Test
968 + public void testToStringIPv6() {
969 + IpPrefix ipPrefix;
126 970
127 - IpPrefix intf = IpPrefix.valueOf("192.168.10.101/24"); 971 + ipPrefix = IpPrefix.valueOf("1100::/8");
128 - IpAddress addr = IpAddress.valueOf("192.168.10.1"); 972 + assertThat(ipPrefix.toString(), is("1100::/8"));
129 973
130 - assertTrue(intf.contains(addr)); 974 + ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8");
975 + assertThat(ipPrefix.toString(), is("1100::/8"));
131 976
132 - IpPrefix intf1 = IpPrefix.valueOf("10.0.0.101/24"); 977 + ipPrefix = IpPrefix.valueOf("::/0");
133 - IpAddress addr1 = IpAddress.valueOf("10.0.0.4"); 978 + assertThat(ipPrefix.toString(), is("::/0"));
134 979
135 - assertTrue(intf1.contains(addr1)); 980 + ipPrefix = IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
981 + assertThat(ipPrefix.toString(),
982 + is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
136 } 983 }
137 } 984 }
......