Merge "Add convenience methods to IpPrefix and LinkAddress" into oc-mr1-dev
This commit is contained in:
343
tests/net/java/android/net/IpPrefixTest.java
Normal file
343
tests/net/java/android/net/IpPrefixTest.java
Normal file
@@ -0,0 +1,343 @@
|
||||
/*
|
||||
* Copyright (C) 2014 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.net;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.support.test.filters.SmallTest;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.Test;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
public class IpPrefixTest {
|
||||
|
||||
private static InetAddress Address(String addr) {
|
||||
return InetAddress.parseNumericAddress(addr);
|
||||
}
|
||||
|
||||
// Explicitly cast everything to byte because "error: possible loss of precision".
|
||||
private static final byte[] IPV4_BYTES = { (byte) 192, (byte) 0, (byte) 2, (byte) 4};
|
||||
private static final byte[] IPV6_BYTES = {
|
||||
(byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8,
|
||||
(byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef,
|
||||
(byte) 0x0f, (byte) 0x00, (byte) 0x00, (byte) 0x00,
|
||||
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xa0
|
||||
};
|
||||
|
||||
@Test
|
||||
public void testConstructor() {
|
||||
IpPrefix p;
|
||||
try {
|
||||
p = new IpPrefix((byte[]) null, 9);
|
||||
fail("Expected NullPointerException: null byte array");
|
||||
} catch(RuntimeException expected) {}
|
||||
|
||||
try {
|
||||
p = new IpPrefix((InetAddress) null, 10);
|
||||
fail("Expected NullPointerException: null InetAddress");
|
||||
} catch(RuntimeException expected) {}
|
||||
|
||||
try {
|
||||
p = new IpPrefix((String) null);
|
||||
fail("Expected NullPointerException: null String");
|
||||
} catch(RuntimeException expected) {}
|
||||
|
||||
|
||||
try {
|
||||
byte[] b2 = {1, 2, 3, 4, 5};
|
||||
p = new IpPrefix(b2, 29);
|
||||
fail("Expected IllegalArgumentException: invalid array length");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
p = new IpPrefix("1.2.3.4");
|
||||
fail("Expected IllegalArgumentException: no prefix length");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
p = new IpPrefix("1.2.3.4/");
|
||||
fail("Expected IllegalArgumentException: empty prefix length");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
p = new IpPrefix("foo/32");
|
||||
fail("Expected IllegalArgumentException: invalid address");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
p = new IpPrefix("1/32");
|
||||
fail("Expected IllegalArgumentException: deprecated IPv4 format");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
p = new IpPrefix("1.2.3.256/32");
|
||||
fail("Expected IllegalArgumentException: invalid IPv4 address");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
p = new IpPrefix("foo/32");
|
||||
fail("Expected IllegalArgumentException: non-address");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
p = new IpPrefix("f00:::/32");
|
||||
fail("Expected IllegalArgumentException: invalid IPv6 address");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTruncation() {
|
||||
IpPrefix p;
|
||||
|
||||
p = new IpPrefix(IPV4_BYTES, 32);
|
||||
assertEquals("192.0.2.4/32", p.toString());
|
||||
|
||||
p = new IpPrefix(IPV4_BYTES, 29);
|
||||
assertEquals("192.0.2.0/29", p.toString());
|
||||
|
||||
p = new IpPrefix(IPV4_BYTES, 8);
|
||||
assertEquals("192.0.0.0/8", p.toString());
|
||||
|
||||
p = new IpPrefix(IPV4_BYTES, 0);
|
||||
assertEquals("0.0.0.0/0", p.toString());
|
||||
|
||||
try {
|
||||
p = new IpPrefix(IPV4_BYTES, 33);
|
||||
fail("Expected IllegalArgumentException: invalid prefix length");
|
||||
} catch(RuntimeException expected) {}
|
||||
|
||||
try {
|
||||
p = new IpPrefix(IPV4_BYTES, 128);
|
||||
fail("Expected IllegalArgumentException: invalid prefix length");
|
||||
} catch(RuntimeException expected) {}
|
||||
|
||||
try {
|
||||
p = new IpPrefix(IPV4_BYTES, -1);
|
||||
fail("Expected IllegalArgumentException: negative prefix length");
|
||||
} catch(RuntimeException expected) {}
|
||||
|
||||
p = new IpPrefix(IPV6_BYTES, 128);
|
||||
assertEquals("2001:db8:dead:beef:f00::a0/128", p.toString());
|
||||
|
||||
p = new IpPrefix(IPV6_BYTES, 122);
|
||||
assertEquals("2001:db8:dead:beef:f00::80/122", p.toString());
|
||||
|
||||
p = new IpPrefix(IPV6_BYTES, 64);
|
||||
assertEquals("2001:db8:dead:beef::/64", p.toString());
|
||||
|
||||
p = new IpPrefix(IPV6_BYTES, 3);
|
||||
assertEquals("2000::/3", p.toString());
|
||||
|
||||
p = new IpPrefix(IPV6_BYTES, 0);
|
||||
assertEquals("::/0", p.toString());
|
||||
|
||||
try {
|
||||
p = new IpPrefix(IPV6_BYTES, -1);
|
||||
fail("Expected IllegalArgumentException: negative prefix length");
|
||||
} catch(RuntimeException expected) {}
|
||||
|
||||
try {
|
||||
p = new IpPrefix(IPV6_BYTES, 129);
|
||||
fail("Expected IllegalArgumentException: negative prefix length");
|
||||
} catch(RuntimeException expected) {}
|
||||
|
||||
}
|
||||
|
||||
private void assertAreEqual(Object o1, Object o2) {
|
||||
assertTrue(o1.equals(o2));
|
||||
assertTrue(o2.equals(o1));
|
||||
}
|
||||
|
||||
private void assertAreNotEqual(Object o1, Object o2) {
|
||||
assertFalse(o1.equals(o2));
|
||||
assertFalse(o2.equals(o1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
IpPrefix p1, p2;
|
||||
|
||||
p1 = new IpPrefix("192.0.2.251/23");
|
||||
p2 = new IpPrefix(new byte[]{(byte) 192, (byte) 0, (byte) 2, (byte) 251}, 23);
|
||||
assertAreEqual(p1, p2);
|
||||
|
||||
p1 = new IpPrefix("192.0.2.5/23");
|
||||
assertAreEqual(p1, p2);
|
||||
|
||||
p1 = new IpPrefix("192.0.2.5/24");
|
||||
assertAreNotEqual(p1, p2);
|
||||
|
||||
p1 = new IpPrefix("192.0.4.5/23");
|
||||
assertAreNotEqual(p1, p2);
|
||||
|
||||
|
||||
p1 = new IpPrefix("2001:db8:dead:beef:f00::80/122");
|
||||
p2 = new IpPrefix(IPV6_BYTES, 122);
|
||||
assertEquals("2001:db8:dead:beef:f00::80/122", p2.toString());
|
||||
assertAreEqual(p1, p2);
|
||||
|
||||
p1 = new IpPrefix("2001:db8:dead:beef:f00::bf/122");
|
||||
assertAreEqual(p1, p2);
|
||||
|
||||
p1 = new IpPrefix("2001:db8:dead:beef:f00::8:0/123");
|
||||
assertAreNotEqual(p1, p2);
|
||||
|
||||
p1 = new IpPrefix("2001:db8:dead:beef::/122");
|
||||
assertAreNotEqual(p1, p2);
|
||||
|
||||
// 192.0.2.4/32 != c000:0204::/32.
|
||||
byte[] ipv6bytes = new byte[16];
|
||||
System.arraycopy(IPV4_BYTES, 0, ipv6bytes, 0, IPV4_BYTES.length);
|
||||
p1 = new IpPrefix(ipv6bytes, 32);
|
||||
assertAreEqual(p1, new IpPrefix("c000:0204::/32"));
|
||||
|
||||
p2 = new IpPrefix(IPV4_BYTES, 32);
|
||||
assertAreNotEqual(p1, p2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContains() {
|
||||
IpPrefix p = new IpPrefix("2001:db8:f00::ace:d00d/127");
|
||||
assertTrue(p.contains(Address("2001:db8:f00::ace:d00c")));
|
||||
assertTrue(p.contains(Address("2001:db8:f00::ace:d00d")));
|
||||
assertFalse(p.contains(Address("2001:db8:f00::ace:d00e")));
|
||||
assertFalse(p.contains(Address("2001:db8:f00::bad:d00d")));
|
||||
assertFalse(p.contains(Address("2001:4868:4860::8888")));
|
||||
assertFalse(p.contains(null));
|
||||
assertFalse(p.contains(Address("8.8.8.8")));
|
||||
|
||||
p = new IpPrefix("192.0.2.0/23");
|
||||
assertTrue(p.contains(Address("192.0.2.43")));
|
||||
assertTrue(p.contains(Address("192.0.3.21")));
|
||||
assertFalse(p.contains(Address("192.0.0.21")));
|
||||
assertFalse(p.contains(Address("8.8.8.8")));
|
||||
assertFalse(p.contains(Address("2001:4868:4860::8888")));
|
||||
|
||||
IpPrefix ipv6Default = new IpPrefix("::/0");
|
||||
assertTrue(ipv6Default.contains(Address("2001:db8::f00")));
|
||||
assertFalse(ipv6Default.contains(Address("192.0.2.1")));
|
||||
|
||||
IpPrefix ipv4Default = new IpPrefix("0.0.0.0/0");
|
||||
assertTrue(ipv4Default.contains(Address("255.255.255.255")));
|
||||
assertTrue(ipv4Default.contains(Address("192.0.2.1")));
|
||||
assertFalse(ipv4Default.contains(Address("2001:db8::f00")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashCode() {
|
||||
IpPrefix p = new IpPrefix(new byte[4], 0);
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
final IpPrefix oldP = p;
|
||||
if (random.nextBoolean()) {
|
||||
// IPv4.
|
||||
byte[] b = new byte[4];
|
||||
random.nextBytes(b);
|
||||
p = new IpPrefix(b, random.nextInt(33));
|
||||
} else {
|
||||
// IPv6.
|
||||
byte[] b = new byte[16];
|
||||
random.nextBytes(b);
|
||||
p = new IpPrefix(b, random.nextInt(129));
|
||||
}
|
||||
if (p.equals(oldP)) {
|
||||
assertEquals(p.hashCode(), oldP.hashCode());
|
||||
}
|
||||
if (p.hashCode() != oldP.hashCode()) {
|
||||
assertNotEquals(p, oldP);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashCodeIsNotConstant() {
|
||||
IpPrefix[] prefixes = {
|
||||
new IpPrefix("2001:db8:f00::ace:d00d/127"),
|
||||
new IpPrefix("192.0.2.0/23"),
|
||||
new IpPrefix("::/0"),
|
||||
new IpPrefix("0.0.0.0/0"),
|
||||
};
|
||||
for (int i = 0; i < prefixes.length; i++) {
|
||||
for (int j = i + 1; j < prefixes.length; j++) {
|
||||
assertNotEquals(prefixes[i].hashCode(), prefixes[j].hashCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMappedAddressesAreBroken() {
|
||||
// 192.0.2.0/24 != ::ffff:c000:0204/120, but because we use InetAddress,
|
||||
// we are unable to comprehend that.
|
||||
byte[] ipv6bytes = {
|
||||
(byte) 0, (byte) 0, (byte) 0, (byte) 0,
|
||||
(byte) 0, (byte) 0, (byte) 0, (byte) 0,
|
||||
(byte) 0, (byte) 0, (byte) 0xff, (byte) 0xff,
|
||||
(byte) 192, (byte) 0, (byte) 2, (byte) 0};
|
||||
IpPrefix p = new IpPrefix(ipv6bytes, 120);
|
||||
assertEquals(16, p.getRawAddress().length); // Fine.
|
||||
assertArrayEquals(ipv6bytes, p.getRawAddress()); // Fine.
|
||||
|
||||
// Broken.
|
||||
assertEquals("192.0.2.0/120", p.toString());
|
||||
assertEquals(InetAddress.parseNumericAddress("192.0.2.0"), p.getAddress());
|
||||
}
|
||||
|
||||
public IpPrefix passThroughParcel(IpPrefix p) {
|
||||
Parcel parcel = Parcel.obtain();
|
||||
IpPrefix p2 = null;
|
||||
try {
|
||||
p.writeToParcel(parcel, 0);
|
||||
parcel.setDataPosition(0);
|
||||
p2 = IpPrefix.CREATOR.createFromParcel(parcel);
|
||||
} finally {
|
||||
parcel.recycle();
|
||||
}
|
||||
assertNotNull(p2);
|
||||
return p2;
|
||||
}
|
||||
|
||||
public void assertParcelingIsLossless(IpPrefix p) {
|
||||
IpPrefix p2 = passThroughParcel(p);
|
||||
assertEquals(p, p2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParceling() {
|
||||
IpPrefix p;
|
||||
|
||||
p = new IpPrefix("2001:4860:db8::/64");
|
||||
assertParcelingIsLossless(p);
|
||||
assertTrue(p.isIPv6());
|
||||
|
||||
p = new IpPrefix("192.0.2.0/25");
|
||||
assertParcelingIsLossless(p);
|
||||
assertTrue(p.isIPv4());
|
||||
}
|
||||
}
|
||||
422
tests/net/java/android/net/LinkAddressTest.java
Normal file
422
tests/net/java/android/net/LinkAddressTest.java
Normal file
@@ -0,0 +1,422 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package android.net;
|
||||
|
||||
import static android.system.OsConstants.IFA_F_DADFAILED;
|
||||
import static android.system.OsConstants.IFA_F_DEPRECATED;
|
||||
import static android.system.OsConstants.IFA_F_OPTIMISTIC;
|
||||
import static android.system.OsConstants.IFA_F_PERMANENT;
|
||||
import static android.system.OsConstants.IFA_F_TEMPORARY;
|
||||
import static android.system.OsConstants.IFA_F_TENTATIVE;
|
||||
import static android.system.OsConstants.RT_SCOPE_HOST;
|
||||
import static android.system.OsConstants.RT_SCOPE_LINK;
|
||||
import static android.system.OsConstants.RT_SCOPE_SITE;
|
||||
import static android.system.OsConstants.RT_SCOPE_UNIVERSE;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InterfaceAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.support.test.filters.SmallTest;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.Test;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
public class LinkAddressTest {
|
||||
|
||||
private static final String V4 = "192.0.2.1";
|
||||
private static final String V6 = "2001:db8::1";
|
||||
private static final InetAddress V4_ADDRESS = NetworkUtils.numericToInetAddress(V4);
|
||||
private static final InetAddress V6_ADDRESS = NetworkUtils.numericToInetAddress(V6);
|
||||
|
||||
@Test
|
||||
public void testConstants() {
|
||||
// RT_SCOPE_UNIVERSE = 0, but all the other constants should be nonzero.
|
||||
assertNotEquals(0, RT_SCOPE_HOST);
|
||||
assertNotEquals(0, RT_SCOPE_LINK);
|
||||
assertNotEquals(0, RT_SCOPE_SITE);
|
||||
|
||||
assertNotEquals(0, IFA_F_DEPRECATED);
|
||||
assertNotEquals(0, IFA_F_PERMANENT);
|
||||
assertNotEquals(0, IFA_F_TENTATIVE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructors() throws SocketException {
|
||||
LinkAddress address;
|
||||
|
||||
// Valid addresses work as expected.
|
||||
address = new LinkAddress(V4_ADDRESS, 25);
|
||||
assertEquals(V4_ADDRESS, address.getAddress());
|
||||
assertEquals(25, address.getPrefixLength());
|
||||
assertEquals(0, address.getFlags());
|
||||
assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
|
||||
assertTrue(address.isIPv4());
|
||||
|
||||
address = new LinkAddress(V6_ADDRESS, 127);
|
||||
assertEquals(V6_ADDRESS, address.getAddress());
|
||||
assertEquals(127, address.getPrefixLength());
|
||||
assertEquals(0, address.getFlags());
|
||||
assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
|
||||
assertTrue(address.isIPv6());
|
||||
|
||||
// Nonsensical flags/scopes or combinations thereof are acceptable.
|
||||
address = new LinkAddress(V6 + "/64", IFA_F_DEPRECATED | IFA_F_PERMANENT, RT_SCOPE_LINK);
|
||||
assertEquals(V6_ADDRESS, address.getAddress());
|
||||
assertEquals(64, address.getPrefixLength());
|
||||
assertEquals(IFA_F_DEPRECATED | IFA_F_PERMANENT, address.getFlags());
|
||||
assertEquals(RT_SCOPE_LINK, address.getScope());
|
||||
assertTrue(address.isIPv6());
|
||||
|
||||
address = new LinkAddress(V4 + "/23", 123, 456);
|
||||
assertEquals(V4_ADDRESS, address.getAddress());
|
||||
assertEquals(23, address.getPrefixLength());
|
||||
assertEquals(123, address.getFlags());
|
||||
assertEquals(456, address.getScope());
|
||||
assertTrue(address.isIPv4());
|
||||
|
||||
// InterfaceAddress doesn't have a constructor. Fetch some from an interface.
|
||||
List<InterfaceAddress> addrs = NetworkInterface.getByName("lo").getInterfaceAddresses();
|
||||
|
||||
// We expect to find 127.0.0.1/8 and ::1/128, in any order.
|
||||
LinkAddress ipv4Loopback, ipv6Loopback;
|
||||
assertEquals(2, addrs.size());
|
||||
if (addrs.get(0).getAddress() instanceof Inet4Address) {
|
||||
ipv4Loopback = new LinkAddress(addrs.get(0));
|
||||
ipv6Loopback = new LinkAddress(addrs.get(1));
|
||||
} else {
|
||||
ipv4Loopback = new LinkAddress(addrs.get(1));
|
||||
ipv6Loopback = new LinkAddress(addrs.get(0));
|
||||
}
|
||||
|
||||
assertEquals(NetworkUtils.numericToInetAddress("127.0.0.1"), ipv4Loopback.getAddress());
|
||||
assertEquals(8, ipv4Loopback.getPrefixLength());
|
||||
|
||||
assertEquals(NetworkUtils.numericToInetAddress("::1"), ipv6Loopback.getAddress());
|
||||
assertEquals(128, ipv6Loopback.getPrefixLength());
|
||||
|
||||
// Null addresses are rejected.
|
||||
try {
|
||||
address = new LinkAddress(null, 24);
|
||||
fail("Null InetAddress should cause IllegalArgumentException");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
address = new LinkAddress((String) null, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
|
||||
fail("Null string should cause IllegalArgumentException");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
address = new LinkAddress((InterfaceAddress) null);
|
||||
fail("Null string should cause NullPointerException");
|
||||
} catch(NullPointerException expected) {}
|
||||
|
||||
// Invalid prefix lengths are rejected.
|
||||
try {
|
||||
address = new LinkAddress(V4_ADDRESS, -1);
|
||||
fail("Negative IPv4 prefix length should cause IllegalArgumentException");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
address = new LinkAddress(V6_ADDRESS, -1);
|
||||
fail("Negative IPv6 prefix length should cause IllegalArgumentException");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
address = new LinkAddress(V4_ADDRESS, 33);
|
||||
fail("/33 IPv4 prefix length should cause IllegalArgumentException");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
address = new LinkAddress(V4 + "/33", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
|
||||
fail("/33 IPv4 prefix length should cause IllegalArgumentException");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
|
||||
try {
|
||||
address = new LinkAddress(V6_ADDRESS, 129, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
|
||||
fail("/129 IPv6 prefix length should cause IllegalArgumentException");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
address = new LinkAddress(V6 + "/129", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
|
||||
fail("/129 IPv6 prefix length should cause IllegalArgumentException");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
// Multicast addresses are rejected.
|
||||
try {
|
||||
address = new LinkAddress("224.0.0.2/32");
|
||||
fail("IPv4 multicast address should cause IllegalArgumentException");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
|
||||
try {
|
||||
address = new LinkAddress("ff02::1/128");
|
||||
fail("IPv6 multicast address should cause IllegalArgumentException");
|
||||
} catch(IllegalArgumentException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddressScopes() {
|
||||
assertEquals(RT_SCOPE_HOST, new LinkAddress("::/128").getScope());
|
||||
assertEquals(RT_SCOPE_HOST, new LinkAddress("0.0.0.0/32").getScope());
|
||||
|
||||
assertEquals(RT_SCOPE_LINK, new LinkAddress("::1/128").getScope());
|
||||
assertEquals(RT_SCOPE_LINK, new LinkAddress("127.0.0.5/8").getScope());
|
||||
assertEquals(RT_SCOPE_LINK, new LinkAddress("fe80::ace:d00d/64").getScope());
|
||||
assertEquals(RT_SCOPE_LINK, new LinkAddress("169.254.5.12/16").getScope());
|
||||
|
||||
assertEquals(RT_SCOPE_SITE, new LinkAddress("fec0::dead/64").getScope());
|
||||
|
||||
assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("10.1.2.3/21").getScope());
|
||||
assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("192.0.2.1/25").getScope());
|
||||
assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("2001:db8::/64").getScope());
|
||||
assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("5000::/127").getScope());
|
||||
}
|
||||
|
||||
private void assertIsSameAddressAs(LinkAddress l1, LinkAddress l2) {
|
||||
assertTrue(l1 + " unexpectedly does not have same address as " + l2,
|
||||
l1.isSameAddressAs(l2));
|
||||
assertTrue(l2 + " unexpectedly does not have same address as " + l1,
|
||||
l2.isSameAddressAs(l1));
|
||||
}
|
||||
|
||||
private void assertIsNotSameAddressAs(LinkAddress l1, LinkAddress l2) {
|
||||
assertFalse(l1 + " unexpectedly has same address as " + l2,
|
||||
l1.isSameAddressAs(l2));
|
||||
assertFalse(l2 + " unexpectedly has same address as " + l1,
|
||||
l1.isSameAddressAs(l2));
|
||||
}
|
||||
|
||||
private void assertLinkAddressesEqual(LinkAddress l1, LinkAddress l2) {
|
||||
assertTrue(l1 + " unexpectedly not equal to " + l2, l1.equals(l2));
|
||||
assertTrue(l2 + " unexpectedly not equal to " + l1, l2.equals(l1));
|
||||
assertEquals(l1.hashCode(), l2.hashCode());
|
||||
}
|
||||
|
||||
private void assertLinkAddressesNotEqual(LinkAddress l1, LinkAddress l2) {
|
||||
assertFalse(l1 + " unexpectedly equal to " + l2, l1.equals(l2));
|
||||
assertFalse(l2 + " unexpectedly equal to " + l1, l2.equals(l1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsAndSameAddressAs() {
|
||||
LinkAddress l1, l2, l3;
|
||||
|
||||
l1 = new LinkAddress("2001:db8::1/64");
|
||||
l2 = new LinkAddress("2001:db8::1/64");
|
||||
assertLinkAddressesEqual(l1, l2);
|
||||
assertIsSameAddressAs(l1, l2);
|
||||
|
||||
l2 = new LinkAddress("2001:db8::1/65");
|
||||
assertLinkAddressesNotEqual(l1, l2);
|
||||
assertIsNotSameAddressAs(l1, l2);
|
||||
|
||||
l2 = new LinkAddress("2001:db8::2/64");
|
||||
assertLinkAddressesNotEqual(l1, l2);
|
||||
assertIsNotSameAddressAs(l1, l2);
|
||||
|
||||
|
||||
l1 = new LinkAddress("192.0.2.1/24");
|
||||
l2 = new LinkAddress("192.0.2.1/24");
|
||||
assertLinkAddressesEqual(l1, l2);
|
||||
assertIsSameAddressAs(l1, l2);
|
||||
|
||||
l2 = new LinkAddress("192.0.2.1/23");
|
||||
assertLinkAddressesNotEqual(l1, l2);
|
||||
assertIsNotSameAddressAs(l1, l2);
|
||||
|
||||
l2 = new LinkAddress("192.0.2.2/24");
|
||||
assertLinkAddressesNotEqual(l1, l2);
|
||||
assertIsNotSameAddressAs(l1, l2);
|
||||
|
||||
|
||||
// Check equals() and isSameAddressAs() on identical addresses with different flags.
|
||||
l1 = new LinkAddress(V6_ADDRESS, 64);
|
||||
l2 = new LinkAddress(V6_ADDRESS, 64, 0, RT_SCOPE_UNIVERSE);
|
||||
assertLinkAddressesEqual(l1, l2);
|
||||
assertIsSameAddressAs(l1, l2);
|
||||
|
||||
l2 = new LinkAddress(V6_ADDRESS, 64, IFA_F_DEPRECATED, RT_SCOPE_UNIVERSE);
|
||||
assertLinkAddressesNotEqual(l1, l2);
|
||||
assertIsSameAddressAs(l1, l2);
|
||||
|
||||
// Check equals() and isSameAddressAs() on identical addresses with different scope.
|
||||
l1 = new LinkAddress(V4_ADDRESS, 24);
|
||||
l2 = new LinkAddress(V4_ADDRESS, 24, 0, RT_SCOPE_UNIVERSE);
|
||||
assertLinkAddressesEqual(l1, l2);
|
||||
assertIsSameAddressAs(l1, l2);
|
||||
|
||||
l2 = new LinkAddress(V4_ADDRESS, 24, 0, RT_SCOPE_HOST);
|
||||
assertLinkAddressesNotEqual(l1, l2);
|
||||
assertIsSameAddressAs(l1, l2);
|
||||
|
||||
// Addresses with the same start or end bytes aren't equal between families.
|
||||
l1 = new LinkAddress("32.1.13.184/24");
|
||||
l2 = new LinkAddress("2001:db8::1/24");
|
||||
l3 = new LinkAddress("::2001:db8/24");
|
||||
|
||||
byte[] ipv4Bytes = l1.getAddress().getAddress();
|
||||
byte[] l2FirstIPv6Bytes = Arrays.copyOf(l2.getAddress().getAddress(), 4);
|
||||
byte[] l3LastIPv6Bytes = Arrays.copyOfRange(l3.getAddress().getAddress(), 12, 16);
|
||||
assertTrue(Arrays.equals(ipv4Bytes, l2FirstIPv6Bytes));
|
||||
assertTrue(Arrays.equals(ipv4Bytes, l3LastIPv6Bytes));
|
||||
|
||||
assertLinkAddressesNotEqual(l1, l2);
|
||||
assertIsNotSameAddressAs(l1, l2);
|
||||
|
||||
assertLinkAddressesNotEqual(l1, l3);
|
||||
assertIsNotSameAddressAs(l1, l3);
|
||||
|
||||
// Because we use InetAddress, an IPv4 address is equal to its IPv4-mapped address.
|
||||
// TODO: Investigate fixing this.
|
||||
String addressString = V4 + "/24";
|
||||
l1 = new LinkAddress(addressString);
|
||||
l2 = new LinkAddress("::ffff:" + addressString);
|
||||
assertLinkAddressesEqual(l1, l2);
|
||||
assertIsSameAddressAs(l1, l2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashCode() {
|
||||
LinkAddress l1, l2;
|
||||
|
||||
l1 = new LinkAddress(V4_ADDRESS, 23);
|
||||
l2 = new LinkAddress(V4_ADDRESS, 23, 0, RT_SCOPE_HOST);
|
||||
assertNotEquals(l1.hashCode(), l2.hashCode());
|
||||
|
||||
l1 = new LinkAddress(V6_ADDRESS, 128);
|
||||
l2 = new LinkAddress(V6_ADDRESS, 128, IFA_F_TENTATIVE, RT_SCOPE_UNIVERSE);
|
||||
assertNotEquals(l1.hashCode(), l2.hashCode());
|
||||
}
|
||||
|
||||
private LinkAddress passThroughParcel(LinkAddress l) {
|
||||
Parcel p = Parcel.obtain();
|
||||
LinkAddress l2 = null;
|
||||
try {
|
||||
l.writeToParcel(p, 0);
|
||||
p.setDataPosition(0);
|
||||
l2 = LinkAddress.CREATOR.createFromParcel(p);
|
||||
} finally {
|
||||
p.recycle();
|
||||
}
|
||||
assertNotNull(l2);
|
||||
return l2;
|
||||
}
|
||||
|
||||
private void assertParcelingIsLossless(LinkAddress l) {
|
||||
LinkAddress l2 = passThroughParcel(l);
|
||||
assertEquals(l, l2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParceling() {
|
||||
LinkAddress l;
|
||||
|
||||
l = new LinkAddress(V6_ADDRESS, 64, 123, 456);
|
||||
assertParcelingIsLossless(l);
|
||||
|
||||
l = new LinkAddress(V4 + "/28", IFA_F_PERMANENT, RT_SCOPE_LINK);
|
||||
assertParcelingIsLossless(l);
|
||||
}
|
||||
|
||||
private void assertGlobalPreferred(LinkAddress l, String msg) {
|
||||
assertTrue(msg, l.isGlobalPreferred());
|
||||
}
|
||||
|
||||
private void assertNotGlobalPreferred(LinkAddress l, String msg) {
|
||||
assertFalse(msg, l.isGlobalPreferred());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsGlobalPreferred() {
|
||||
LinkAddress l;
|
||||
|
||||
l = new LinkAddress(V4_ADDRESS, 32, 0, RT_SCOPE_UNIVERSE);
|
||||
assertGlobalPreferred(l, "v4,global,noflags");
|
||||
|
||||
l = new LinkAddress("10.10.1.7/23", 0, RT_SCOPE_UNIVERSE);
|
||||
assertGlobalPreferred(l, "v4-rfc1918,global,noflags");
|
||||
|
||||
l = new LinkAddress("10.10.1.7/23", 0, RT_SCOPE_SITE);
|
||||
assertNotGlobalPreferred(l, "v4-rfc1918,site-local,noflags");
|
||||
|
||||
l = new LinkAddress("127.0.0.7/8", 0, RT_SCOPE_HOST);
|
||||
assertNotGlobalPreferred(l, "v4-localhost,node-local,noflags");
|
||||
|
||||
l = new LinkAddress(V6_ADDRESS, 64, 0, RT_SCOPE_UNIVERSE);
|
||||
assertGlobalPreferred(l, "v6,global,noflags");
|
||||
|
||||
l = new LinkAddress(V6_ADDRESS, 64, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
|
||||
assertGlobalPreferred(l, "v6,global,permanent");
|
||||
|
||||
// IPv6 ULAs are not acceptable "global preferred" addresses.
|
||||
l = new LinkAddress("fc12::1/64", 0, RT_SCOPE_UNIVERSE);
|
||||
assertNotGlobalPreferred(l, "v6,ula1,noflags");
|
||||
|
||||
l = new LinkAddress("fd34::1/64", 0, RT_SCOPE_UNIVERSE);
|
||||
assertNotGlobalPreferred(l, "v6,ula2,noflags");
|
||||
|
||||
l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_UNIVERSE);
|
||||
assertGlobalPreferred(l, "v6,global,tempaddr");
|
||||
|
||||
l = new LinkAddress(V6_ADDRESS, 64, (IFA_F_TEMPORARY|IFA_F_DADFAILED),
|
||||
RT_SCOPE_UNIVERSE);
|
||||
assertNotGlobalPreferred(l, "v6,global,tempaddr+dadfailed");
|
||||
|
||||
l = new LinkAddress(V6_ADDRESS, 64, (IFA_F_TEMPORARY|IFA_F_DEPRECATED),
|
||||
RT_SCOPE_UNIVERSE);
|
||||
assertNotGlobalPreferred(l, "v6,global,tempaddr+deprecated");
|
||||
|
||||
l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_SITE);
|
||||
assertNotGlobalPreferred(l, "v6,site-local,tempaddr");
|
||||
|
||||
l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_LINK);
|
||||
assertNotGlobalPreferred(l, "v6,link-local,tempaddr");
|
||||
|
||||
l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_HOST);
|
||||
assertNotGlobalPreferred(l, "v6,node-local,tempaddr");
|
||||
|
||||
l = new LinkAddress("::1/128", IFA_F_PERMANENT, RT_SCOPE_HOST);
|
||||
assertNotGlobalPreferred(l, "v6-localhost,node-local,permanent");
|
||||
|
||||
l = new LinkAddress(V6_ADDRESS, 64, (IFA_F_TEMPORARY|IFA_F_TENTATIVE),
|
||||
RT_SCOPE_UNIVERSE);
|
||||
assertNotGlobalPreferred(l, "v6,global,tempaddr+tentative");
|
||||
|
||||
l = new LinkAddress(V6_ADDRESS, 64,
|
||||
(IFA_F_TEMPORARY|IFA_F_TENTATIVE|IFA_F_OPTIMISTIC),
|
||||
RT_SCOPE_UNIVERSE);
|
||||
assertGlobalPreferred(l, "v6,global,tempaddr+optimistic");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user