Merge "Move more network unit tests to common tests"
This commit is contained in:
@@ -1,421 +0,0 @@
|
||||
/*
|
||||
* 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 android.os.Parcel;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InterfaceAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@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");
|
||||
}
|
||||
}
|
||||
@@ -1,879 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2010 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.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import android.net.LinkProperties.CompareResult;
|
||||
import android.net.LinkProperties.ProvisioningChange;
|
||||
import android.system.OsConstants;
|
||||
import android.util.ArraySet;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import com.android.internal.util.TestUtils;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
public class LinkPropertiesTest {
|
||||
private static InetAddress ADDRV4 = NetworkUtils.numericToInetAddress("75.208.6.1");
|
||||
private static InetAddress ADDRV6 = NetworkUtils.numericToInetAddress(
|
||||
"2001:0db8:85a3:0000:0000:8a2e:0370:7334");
|
||||
private static InetAddress DNS1 = NetworkUtils.numericToInetAddress("75.208.7.1");
|
||||
private static InetAddress DNS2 = NetworkUtils.numericToInetAddress("69.78.7.1");
|
||||
private static InetAddress DNS6 = NetworkUtils.numericToInetAddress("2001:4860:4860::8888");
|
||||
private static InetAddress PCSCFV6 = NetworkUtils.numericToInetAddress(
|
||||
"2001:0db8:85a3:0000:0000:8a2e:0370:1");
|
||||
private static InetAddress GATEWAY1 = NetworkUtils.numericToInetAddress("75.208.8.1");
|
||||
private static InetAddress GATEWAY2 = NetworkUtils.numericToInetAddress("69.78.8.1");
|
||||
private static InetAddress GATEWAY61 = NetworkUtils.numericToInetAddress("fe80::6:0000:613");
|
||||
private static InetAddress GATEWAY62 = NetworkUtils.numericToInetAddress("fe80::6:2222");
|
||||
private static String NAME = "qmi0";
|
||||
private static int MTU = 1500;
|
||||
|
||||
private static LinkAddress LINKADDRV4 = new LinkAddress(ADDRV4, 32);
|
||||
private static LinkAddress LINKADDRV6 = new LinkAddress(ADDRV6, 128);
|
||||
private static LinkAddress LINKADDRV6LINKLOCAL = new LinkAddress("fe80::1/64");
|
||||
|
||||
// TODO: replace all calls to NetworkUtils.numericToInetAddress with calls to this method.
|
||||
private InetAddress Address(String addrString) {
|
||||
return NetworkUtils.numericToInetAddress(addrString);
|
||||
}
|
||||
|
||||
public void assertLinkPropertiesEqual(LinkProperties source, LinkProperties target) {
|
||||
// Check implementation of equals(), element by element.
|
||||
assertTrue(source.isIdenticalInterfaceName(target));
|
||||
assertTrue(target.isIdenticalInterfaceName(source));
|
||||
|
||||
assertTrue(source.isIdenticalAddresses(target));
|
||||
assertTrue(target.isIdenticalAddresses(source));
|
||||
|
||||
assertTrue(source.isIdenticalDnses(target));
|
||||
assertTrue(target.isIdenticalDnses(source));
|
||||
|
||||
assertTrue(source.isIdenticalPrivateDns(target));
|
||||
assertTrue(target.isIdenticalPrivateDns(source));
|
||||
|
||||
assertTrue(source.isIdenticalValidatedPrivateDnses(target));
|
||||
assertTrue(target.isIdenticalValidatedPrivateDnses(source));
|
||||
|
||||
assertTrue(source.isIdenticalPcscfs(target));
|
||||
assertTrue(target.isIdenticalPcscfs(source));
|
||||
|
||||
assertTrue(source.isIdenticalRoutes(target));
|
||||
assertTrue(target.isIdenticalRoutes(source));
|
||||
|
||||
assertTrue(source.isIdenticalHttpProxy(target));
|
||||
assertTrue(target.isIdenticalHttpProxy(source));
|
||||
|
||||
assertTrue(source.isIdenticalStackedLinks(target));
|
||||
assertTrue(target.isIdenticalStackedLinks(source));
|
||||
|
||||
assertTrue(source.isIdenticalMtu(target));
|
||||
assertTrue(target.isIdenticalMtu(source));
|
||||
|
||||
assertTrue(source.isIdenticalTcpBufferSizes(target));
|
||||
assertTrue(target.isIdenticalTcpBufferSizes(source));
|
||||
|
||||
// Check result of equals().
|
||||
assertTrue(source.equals(target));
|
||||
assertTrue(target.equals(source));
|
||||
|
||||
// Check hashCode.
|
||||
assertEquals(source.hashCode(), target.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsNull() {
|
||||
LinkProperties source = new LinkProperties();
|
||||
LinkProperties target = new LinkProperties();
|
||||
|
||||
assertFalse(source == target);
|
||||
assertLinkPropertiesEqual(source, target);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsSameOrder() throws Exception {
|
||||
LinkProperties source = new LinkProperties();
|
||||
source.setInterfaceName(NAME);
|
||||
// set 2 link addresses
|
||||
source.addLinkAddress(LINKADDRV4);
|
||||
source.addLinkAddress(LINKADDRV6);
|
||||
// set 2 dnses
|
||||
source.addDnsServer(DNS1);
|
||||
source.addDnsServer(DNS2);
|
||||
// set 1 pcscf
|
||||
source.addPcscfServer(PCSCFV6);
|
||||
// set 2 gateways
|
||||
source.addRoute(new RouteInfo(GATEWAY1));
|
||||
source.addRoute(new RouteInfo(GATEWAY2));
|
||||
source.setMtu(MTU);
|
||||
|
||||
LinkProperties target = new LinkProperties();
|
||||
|
||||
// All fields are same
|
||||
target.setInterfaceName(NAME);
|
||||
target.addLinkAddress(LINKADDRV4);
|
||||
target.addLinkAddress(LINKADDRV6);
|
||||
target.addDnsServer(DNS1);
|
||||
target.addDnsServer(DNS2);
|
||||
target.addPcscfServer(PCSCFV6);
|
||||
target.addRoute(new RouteInfo(GATEWAY1));
|
||||
target.addRoute(new RouteInfo(GATEWAY2));
|
||||
target.setMtu(MTU);
|
||||
|
||||
assertLinkPropertiesEqual(source, target);
|
||||
|
||||
target.clear();
|
||||
// change Interface Name
|
||||
target.setInterfaceName("qmi1");
|
||||
target.addLinkAddress(LINKADDRV4);
|
||||
target.addLinkAddress(LINKADDRV6);
|
||||
target.addDnsServer(DNS1);
|
||||
target.addDnsServer(DNS2);
|
||||
target.addPcscfServer(PCSCFV6);
|
||||
target.addRoute(new RouteInfo(GATEWAY1));
|
||||
target.addRoute(new RouteInfo(GATEWAY2));
|
||||
target.setMtu(MTU);
|
||||
assertFalse(source.equals(target));
|
||||
|
||||
target.clear();
|
||||
target.setInterfaceName(NAME);
|
||||
// change link addresses
|
||||
target.addLinkAddress(new LinkAddress(
|
||||
NetworkUtils.numericToInetAddress("75.208.6.2"), 32));
|
||||
target.addLinkAddress(LINKADDRV6);
|
||||
target.addDnsServer(DNS1);
|
||||
target.addDnsServer(DNS2);
|
||||
target.addPcscfServer(PCSCFV6);
|
||||
target.addRoute(new RouteInfo(GATEWAY1));
|
||||
target.addRoute(new RouteInfo(GATEWAY2));
|
||||
target.setMtu(MTU);
|
||||
assertFalse(source.equals(target));
|
||||
|
||||
target.clear();
|
||||
target.setInterfaceName(NAME);
|
||||
target.addLinkAddress(LINKADDRV4);
|
||||
target.addLinkAddress(LINKADDRV6);
|
||||
// change dnses
|
||||
target.addDnsServer(NetworkUtils.numericToInetAddress("75.208.7.2"));
|
||||
target.addDnsServer(DNS2);
|
||||
target.addPcscfServer(PCSCFV6);
|
||||
target.addRoute(new RouteInfo(GATEWAY1));
|
||||
target.addRoute(new RouteInfo(GATEWAY2));
|
||||
target.setMtu(MTU);
|
||||
assertFalse(source.equals(target));
|
||||
|
||||
target.clear();
|
||||
target.setInterfaceName(NAME);
|
||||
target.addLinkAddress(LINKADDRV4);
|
||||
target.addLinkAddress(LINKADDRV6);
|
||||
target.addDnsServer(NetworkUtils.numericToInetAddress("75.208.7.2"));
|
||||
target.addDnsServer(DNS2);
|
||||
// change pcscf
|
||||
target.addPcscfServer(NetworkUtils.numericToInetAddress(
|
||||
"2001::1"));
|
||||
target.addRoute(new RouteInfo(GATEWAY1));
|
||||
target.addRoute(new RouteInfo(GATEWAY2));
|
||||
target.setMtu(MTU);
|
||||
assertFalse(source.equals(target));
|
||||
|
||||
target.clear();
|
||||
target.setInterfaceName(NAME);
|
||||
target.addLinkAddress(LINKADDRV4);
|
||||
target.addLinkAddress(LINKADDRV6);
|
||||
target.addDnsServer(DNS1);
|
||||
target.addDnsServer(DNS2);
|
||||
// change gateway
|
||||
target.addRoute(new RouteInfo(NetworkUtils.numericToInetAddress("75.208.8.2")));
|
||||
target.addRoute(new RouteInfo(GATEWAY2));
|
||||
target.setMtu(MTU);
|
||||
assertFalse(source.equals(target));
|
||||
|
||||
target.clear();
|
||||
target.setInterfaceName(NAME);
|
||||
target.addLinkAddress(LINKADDRV4);
|
||||
target.addLinkAddress(LINKADDRV6);
|
||||
target.addDnsServer(DNS1);
|
||||
target.addDnsServer(DNS2);
|
||||
target.addRoute(new RouteInfo(GATEWAY1));
|
||||
target.addRoute(new RouteInfo(GATEWAY2));
|
||||
// change mtu
|
||||
target.setMtu(1440);
|
||||
assertFalse(source.equals(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsDifferentOrder() throws Exception {
|
||||
LinkProperties source = new LinkProperties();
|
||||
source.setInterfaceName(NAME);
|
||||
// set 2 link addresses
|
||||
source.addLinkAddress(LINKADDRV4);
|
||||
source.addLinkAddress(LINKADDRV6);
|
||||
// set 2 dnses
|
||||
source.addDnsServer(DNS1);
|
||||
source.addDnsServer(DNS2);
|
||||
// set 2 gateways
|
||||
source.addRoute(new RouteInfo(GATEWAY1));
|
||||
source.addRoute(new RouteInfo(GATEWAY2));
|
||||
source.setMtu(MTU);
|
||||
|
||||
LinkProperties target = new LinkProperties();
|
||||
// Exchange order
|
||||
target.setInterfaceName(NAME);
|
||||
target.addLinkAddress(LINKADDRV6);
|
||||
target.addLinkAddress(LINKADDRV4);
|
||||
target.addDnsServer(DNS2);
|
||||
target.addDnsServer(DNS1);
|
||||
target.addRoute(new RouteInfo(GATEWAY2));
|
||||
target.addRoute(new RouteInfo(GATEWAY1));
|
||||
target.setMtu(MTU);
|
||||
|
||||
assertLinkPropertiesEqual(source, target);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsDuplicated() throws Exception {
|
||||
LinkProperties source = new LinkProperties();
|
||||
// set 3 link addresses, eg, [A, A, B]
|
||||
source.addLinkAddress(LINKADDRV4);
|
||||
source.addLinkAddress(LINKADDRV4);
|
||||
source.addLinkAddress(LINKADDRV6);
|
||||
|
||||
LinkProperties target = new LinkProperties();
|
||||
// set 3 link addresses, eg, [A, B, B]
|
||||
target.addLinkAddress(LINKADDRV4);
|
||||
target.addLinkAddress(LINKADDRV6);
|
||||
target.addLinkAddress(LINKADDRV6);
|
||||
|
||||
assertLinkPropertiesEqual(source, target);
|
||||
}
|
||||
|
||||
private void assertAllRoutesHaveInterface(String iface, LinkProperties lp) {
|
||||
for (RouteInfo r : lp.getRoutes()) {
|
||||
assertEquals(iface, r.getInterface());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRouteInterfaces() {
|
||||
LinkAddress prefix = new LinkAddress(
|
||||
NetworkUtils.numericToInetAddress("2001:db8::"), 32);
|
||||
InetAddress address = ADDRV6;
|
||||
|
||||
// Add a route with no interface to a LinkProperties with no interface. No errors.
|
||||
LinkProperties lp = new LinkProperties();
|
||||
RouteInfo r = new RouteInfo(prefix, address, null);
|
||||
assertTrue(lp.addRoute(r));
|
||||
assertEquals(1, lp.getRoutes().size());
|
||||
assertAllRoutesHaveInterface(null, lp);
|
||||
|
||||
// Adding the same route twice has no effect.
|
||||
assertFalse(lp.addRoute(r));
|
||||
assertEquals(1, lp.getRoutes().size());
|
||||
|
||||
// Add a route with an interface. Expect an exception.
|
||||
r = new RouteInfo(prefix, address, "wlan0");
|
||||
try {
|
||||
lp.addRoute(r);
|
||||
fail("Adding wlan0 route to LP with no interface, expect exception");
|
||||
} catch (IllegalArgumentException expected) {}
|
||||
|
||||
// Change the interface name. All the routes should change their interface name too.
|
||||
lp.setInterfaceName("rmnet0");
|
||||
assertAllRoutesHaveInterface("rmnet0", lp);
|
||||
|
||||
// Now add a route with the wrong interface. This causes an exception too.
|
||||
try {
|
||||
lp.addRoute(r);
|
||||
fail("Adding wlan0 route to rmnet0 LP, expect exception");
|
||||
} catch (IllegalArgumentException expected) {}
|
||||
|
||||
// If the interface name matches, the route is added.
|
||||
r = new RouteInfo(prefix, null, "wlan0");
|
||||
lp.setInterfaceName("wlan0");
|
||||
lp.addRoute(r);
|
||||
assertEquals(2, lp.getRoutes().size());
|
||||
assertAllRoutesHaveInterface("wlan0", lp);
|
||||
|
||||
// Routes with null interfaces are converted to wlan0.
|
||||
r = RouteInfo.makeHostRoute(ADDRV6, null);
|
||||
lp.addRoute(r);
|
||||
assertEquals(3, lp.getRoutes().size());
|
||||
assertAllRoutesHaveInterface("wlan0", lp);
|
||||
|
||||
// Check comparisons work.
|
||||
LinkProperties lp2 = new LinkProperties(lp);
|
||||
assertAllRoutesHaveInterface("wlan0", lp);
|
||||
assertEquals(0, lp.compareAllRoutes(lp2).added.size());
|
||||
assertEquals(0, lp.compareAllRoutes(lp2).removed.size());
|
||||
|
||||
lp2.setInterfaceName("p2p0");
|
||||
assertAllRoutesHaveInterface("p2p0", lp2);
|
||||
assertEquals(3, lp.compareAllRoutes(lp2).added.size());
|
||||
assertEquals(3, lp.compareAllRoutes(lp2).removed.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStackedInterfaces() {
|
||||
LinkProperties rmnet0 = new LinkProperties();
|
||||
rmnet0.setInterfaceName("rmnet0");
|
||||
rmnet0.addLinkAddress(LINKADDRV6);
|
||||
|
||||
LinkProperties clat4 = new LinkProperties();
|
||||
clat4.setInterfaceName("clat4");
|
||||
clat4.addLinkAddress(LINKADDRV4);
|
||||
|
||||
assertEquals(0, rmnet0.getStackedLinks().size());
|
||||
assertEquals(1, rmnet0.getAddresses().size());
|
||||
assertEquals(1, rmnet0.getLinkAddresses().size());
|
||||
assertEquals(1, rmnet0.getAllAddresses().size());
|
||||
assertEquals(1, rmnet0.getAllLinkAddresses().size());
|
||||
|
||||
rmnet0.addStackedLink(clat4);
|
||||
assertEquals(1, rmnet0.getStackedLinks().size());
|
||||
assertEquals(1, rmnet0.getAddresses().size());
|
||||
assertEquals(1, rmnet0.getLinkAddresses().size());
|
||||
assertEquals(2, rmnet0.getAllAddresses().size());
|
||||
assertEquals(2, rmnet0.getAllLinkAddresses().size());
|
||||
|
||||
rmnet0.addStackedLink(clat4);
|
||||
assertEquals(1, rmnet0.getStackedLinks().size());
|
||||
assertEquals(1, rmnet0.getAddresses().size());
|
||||
assertEquals(1, rmnet0.getLinkAddresses().size());
|
||||
assertEquals(2, rmnet0.getAllAddresses().size());
|
||||
assertEquals(2, rmnet0.getAllLinkAddresses().size());
|
||||
|
||||
assertEquals(0, clat4.getStackedLinks().size());
|
||||
|
||||
// Modify an item in the returned collection to see what happens.
|
||||
for (LinkProperties link : rmnet0.getStackedLinks()) {
|
||||
if (link.getInterfaceName().equals("clat4")) {
|
||||
link.setInterfaceName("newname");
|
||||
}
|
||||
}
|
||||
for (LinkProperties link : rmnet0.getStackedLinks()) {
|
||||
assertFalse("newname".equals(link.getInterfaceName()));
|
||||
}
|
||||
|
||||
assertTrue(rmnet0.removeStackedLink("clat4"));
|
||||
assertEquals(0, rmnet0.getStackedLinks().size());
|
||||
assertEquals(1, rmnet0.getAddresses().size());
|
||||
assertEquals(1, rmnet0.getLinkAddresses().size());
|
||||
assertEquals(1, rmnet0.getAllAddresses().size());
|
||||
assertEquals(1, rmnet0.getAllLinkAddresses().size());
|
||||
|
||||
assertFalse(rmnet0.removeStackedLink("clat4"));
|
||||
}
|
||||
|
||||
private LinkAddress getFirstLinkAddress(LinkProperties lp) {
|
||||
return lp.getLinkAddresses().iterator().next();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddressMethods() {
|
||||
LinkProperties lp = new LinkProperties();
|
||||
|
||||
// No addresses.
|
||||
assertFalse(lp.hasIpv4Address());
|
||||
assertFalse(lp.hasGlobalIpv6Address());
|
||||
|
||||
// Addresses on stacked links don't count.
|
||||
LinkProperties stacked = new LinkProperties();
|
||||
stacked.setInterfaceName("stacked");
|
||||
lp.addStackedLink(stacked);
|
||||
stacked.addLinkAddress(LINKADDRV4);
|
||||
stacked.addLinkAddress(LINKADDRV6);
|
||||
assertTrue(stacked.hasIpv4Address());
|
||||
assertTrue(stacked.hasGlobalIpv6Address());
|
||||
assertFalse(lp.hasIpv4Address());
|
||||
assertFalse(lp.hasGlobalIpv6Address());
|
||||
lp.removeStackedLink("stacked");
|
||||
assertFalse(lp.hasIpv4Address());
|
||||
assertFalse(lp.hasGlobalIpv6Address());
|
||||
|
||||
// Addresses on the base link.
|
||||
// Check the return values of hasIpvXAddress and ensure the add/remove methods return true
|
||||
// iff something changes.
|
||||
assertEquals(0, lp.getLinkAddresses().size());
|
||||
assertTrue(lp.addLinkAddress(LINKADDRV6));
|
||||
assertEquals(1, lp.getLinkAddresses().size());
|
||||
assertFalse(lp.hasIpv4Address());
|
||||
assertTrue(lp.hasGlobalIpv6Address());
|
||||
|
||||
assertTrue(lp.removeLinkAddress(LINKADDRV6));
|
||||
assertEquals(0, lp.getLinkAddresses().size());
|
||||
|
||||
assertTrue(lp.addLinkAddress(LINKADDRV6LINKLOCAL));
|
||||
assertEquals(1, lp.getLinkAddresses().size());
|
||||
assertFalse(lp.hasGlobalIpv6Address());
|
||||
|
||||
assertTrue(lp.addLinkAddress(LINKADDRV4));
|
||||
assertEquals(2, lp.getLinkAddresses().size());
|
||||
assertTrue(lp.hasIpv4Address());
|
||||
assertFalse(lp.hasGlobalIpv6Address());
|
||||
|
||||
assertTrue(lp.addLinkAddress(LINKADDRV6));
|
||||
assertEquals(3, lp.getLinkAddresses().size());
|
||||
assertTrue(lp.hasIpv4Address());
|
||||
assertTrue(lp.hasGlobalIpv6Address());
|
||||
|
||||
assertTrue(lp.removeLinkAddress(LINKADDRV6LINKLOCAL));
|
||||
assertEquals(2, lp.getLinkAddresses().size());
|
||||
assertTrue(lp.hasIpv4Address());
|
||||
assertTrue(lp.hasGlobalIpv6Address());
|
||||
|
||||
// Adding an address twice has no effect.
|
||||
// Removing an address that's not present has no effect.
|
||||
assertFalse(lp.addLinkAddress(LINKADDRV4));
|
||||
assertEquals(2, lp.getLinkAddresses().size());
|
||||
assertTrue(lp.hasIpv4Address());
|
||||
assertTrue(lp.removeLinkAddress(LINKADDRV4));
|
||||
assertEquals(1, lp.getLinkAddresses().size());
|
||||
assertFalse(lp.hasIpv4Address());
|
||||
assertFalse(lp.removeLinkAddress(LINKADDRV4));
|
||||
assertEquals(1, lp.getLinkAddresses().size());
|
||||
|
||||
// Adding an address that's already present but with different properties causes the
|
||||
// existing address to be updated and returns true.
|
||||
// Start with only LINKADDRV6.
|
||||
assertEquals(1, lp.getLinkAddresses().size());
|
||||
assertEquals(LINKADDRV6, getFirstLinkAddress(lp));
|
||||
|
||||
// Create a LinkAddress object for the same address, but with different flags.
|
||||
LinkAddress deprecated = new LinkAddress(ADDRV6, 128,
|
||||
OsConstants.IFA_F_DEPRECATED, OsConstants.RT_SCOPE_UNIVERSE);
|
||||
assertTrue(deprecated.isSameAddressAs(LINKADDRV6));
|
||||
assertFalse(deprecated.equals(LINKADDRV6));
|
||||
|
||||
// Check that adding it updates the existing address instead of adding a new one.
|
||||
assertTrue(lp.addLinkAddress(deprecated));
|
||||
assertEquals(1, lp.getLinkAddresses().size());
|
||||
assertEquals(deprecated, getFirstLinkAddress(lp));
|
||||
assertFalse(LINKADDRV6.equals(getFirstLinkAddress(lp)));
|
||||
|
||||
// Removing LINKADDRV6 removes deprecated, because removing addresses ignores properties.
|
||||
assertTrue(lp.removeLinkAddress(LINKADDRV6));
|
||||
assertEquals(0, lp.getLinkAddresses().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetLinkAddresses() {
|
||||
LinkProperties lp = new LinkProperties();
|
||||
lp.addLinkAddress(LINKADDRV4);
|
||||
lp.addLinkAddress(LINKADDRV6);
|
||||
|
||||
LinkProperties lp2 = new LinkProperties();
|
||||
lp2.addLinkAddress(LINKADDRV6);
|
||||
|
||||
assertFalse(lp.equals(lp2));
|
||||
|
||||
lp2.setLinkAddresses(lp.getLinkAddresses());
|
||||
assertTrue(lp.equals(lp));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNat64Prefix() throws Exception {
|
||||
LinkProperties lp = new LinkProperties();
|
||||
lp.addLinkAddress(LINKADDRV4);
|
||||
lp.addLinkAddress(LINKADDRV6);
|
||||
|
||||
assertNull(lp.getNat64Prefix());
|
||||
|
||||
IpPrefix p = new IpPrefix("64:ff9b::/96");
|
||||
lp.setNat64Prefix(p);
|
||||
assertEquals(p, lp.getNat64Prefix());
|
||||
|
||||
p = new IpPrefix("2001:db8:a:b:1:2:3::/96");
|
||||
lp.setNat64Prefix(p);
|
||||
assertEquals(p, lp.getNat64Prefix());
|
||||
|
||||
p = new IpPrefix("2001:db8:a:b:1:2::/80");
|
||||
try {
|
||||
lp.setNat64Prefix(p);
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
|
||||
p = new IpPrefix("64:ff9b::/64");
|
||||
try {
|
||||
lp.setNat64Prefix(p);
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
|
||||
assertEquals(new IpPrefix("2001:db8:a:b:1:2:3::/96"), lp.getNat64Prefix());
|
||||
|
||||
lp.setNat64Prefix(null);
|
||||
assertNull(lp.getNat64Prefix());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsProvisioned() {
|
||||
LinkProperties lp4 = new LinkProperties();
|
||||
assertFalse("v4only:empty", lp4.isProvisioned());
|
||||
lp4.addLinkAddress(LINKADDRV4);
|
||||
assertFalse("v4only:addr-only", lp4.isProvisioned());
|
||||
lp4.addDnsServer(DNS1);
|
||||
assertFalse("v4only:addr+dns", lp4.isProvisioned());
|
||||
lp4.addRoute(new RouteInfo(GATEWAY1));
|
||||
assertTrue("v4only:addr+dns+route", lp4.isProvisioned());
|
||||
assertTrue("v4only:addr+dns+route", lp4.isIpv4Provisioned());
|
||||
assertFalse("v4only:addr+dns+route", lp4.isIpv6Provisioned());
|
||||
|
||||
LinkProperties lp6 = new LinkProperties();
|
||||
assertFalse("v6only:empty", lp6.isProvisioned());
|
||||
lp6.addLinkAddress(LINKADDRV6LINKLOCAL);
|
||||
assertFalse("v6only:fe80-only", lp6.isProvisioned());
|
||||
lp6.addDnsServer(DNS6);
|
||||
assertFalse("v6only:fe80+dns", lp6.isProvisioned());
|
||||
lp6.addRoute(new RouteInfo(GATEWAY61));
|
||||
assertFalse("v6only:fe80+dns+route", lp6.isProvisioned());
|
||||
lp6.addLinkAddress(LINKADDRV6);
|
||||
assertTrue("v6only:fe80+global+dns+route", lp6.isIpv6Provisioned());
|
||||
assertTrue("v6only:fe80+global+dns+route", lp6.isProvisioned());
|
||||
lp6.removeLinkAddress(LINKADDRV6LINKLOCAL);
|
||||
assertFalse("v6only:global+dns+route", lp6.isIpv4Provisioned());
|
||||
assertTrue("v6only:global+dns+route", lp6.isIpv6Provisioned());
|
||||
assertTrue("v6only:global+dns+route", lp6.isProvisioned());
|
||||
|
||||
LinkProperties lp46 = new LinkProperties();
|
||||
lp46.addLinkAddress(LINKADDRV4);
|
||||
lp46.addLinkAddress(LINKADDRV6);
|
||||
lp46.addDnsServer(DNS1);
|
||||
lp46.addDnsServer(DNS6);
|
||||
assertFalse("dualstack:missing-routes", lp46.isProvisioned());
|
||||
lp46.addRoute(new RouteInfo(GATEWAY1));
|
||||
assertTrue("dualstack:v4-provisioned", lp46.isIpv4Provisioned());
|
||||
assertFalse("dualstack:v4-provisioned", lp46.isIpv6Provisioned());
|
||||
assertTrue("dualstack:v4-provisioned", lp46.isProvisioned());
|
||||
lp46.addRoute(new RouteInfo(GATEWAY61));
|
||||
assertTrue("dualstack:both-provisioned", lp46.isIpv4Provisioned());
|
||||
assertTrue("dualstack:both-provisioned", lp46.isIpv6Provisioned());
|
||||
assertTrue("dualstack:both-provisioned", lp46.isProvisioned());
|
||||
|
||||
// A link with an IPv6 address and default route, but IPv4 DNS server.
|
||||
LinkProperties mixed = new LinkProperties();
|
||||
mixed.addLinkAddress(LINKADDRV6);
|
||||
mixed.addDnsServer(DNS1);
|
||||
mixed.addRoute(new RouteInfo(GATEWAY61));
|
||||
assertFalse("mixed:addr6+route6+dns4", mixed.isIpv4Provisioned());
|
||||
assertFalse("mixed:addr6+route6+dns4", mixed.isIpv6Provisioned());
|
||||
assertFalse("mixed:addr6+route6+dns4", mixed.isProvisioned());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompareProvisioning() {
|
||||
LinkProperties v4lp = new LinkProperties();
|
||||
v4lp.addLinkAddress(LINKADDRV4);
|
||||
v4lp.addRoute(new RouteInfo(GATEWAY1));
|
||||
v4lp.addDnsServer(DNS1);
|
||||
assertTrue(v4lp.isProvisioned());
|
||||
|
||||
LinkProperties v4r = new LinkProperties(v4lp);
|
||||
v4r.removeDnsServer(DNS1);
|
||||
assertFalse(v4r.isProvisioned());
|
||||
|
||||
assertEquals(ProvisioningChange.STILL_NOT_PROVISIONED,
|
||||
LinkProperties.compareProvisioning(v4r, v4r));
|
||||
assertEquals(ProvisioningChange.LOST_PROVISIONING,
|
||||
LinkProperties.compareProvisioning(v4lp, v4r));
|
||||
assertEquals(ProvisioningChange.GAINED_PROVISIONING,
|
||||
LinkProperties.compareProvisioning(v4r, v4lp));
|
||||
assertEquals(ProvisioningChange.STILL_PROVISIONED,
|
||||
LinkProperties.compareProvisioning(v4lp, v4lp));
|
||||
|
||||
// Check that losing IPv4 provisioning on a dualstack network is
|
||||
// seen as a total loss of provisioning.
|
||||
LinkProperties v6lp = new LinkProperties();
|
||||
v6lp.addLinkAddress(LINKADDRV6);
|
||||
v6lp.addRoute(new RouteInfo(GATEWAY61));
|
||||
v6lp.addDnsServer(DNS6);
|
||||
assertFalse(v6lp.isIpv4Provisioned());
|
||||
assertTrue(v6lp.isIpv6Provisioned());
|
||||
assertTrue(v6lp.isProvisioned());
|
||||
|
||||
LinkProperties v46lp = new LinkProperties(v6lp);
|
||||
v46lp.addLinkAddress(LINKADDRV4);
|
||||
v46lp.addRoute(new RouteInfo(GATEWAY1));
|
||||
v46lp.addDnsServer(DNS1);
|
||||
assertTrue(v46lp.isIpv4Provisioned());
|
||||
assertTrue(v46lp.isIpv6Provisioned());
|
||||
assertTrue(v46lp.isProvisioned());
|
||||
|
||||
assertEquals(ProvisioningChange.STILL_PROVISIONED,
|
||||
LinkProperties.compareProvisioning(v4lp, v46lp));
|
||||
assertEquals(ProvisioningChange.STILL_PROVISIONED,
|
||||
LinkProperties.compareProvisioning(v6lp, v46lp));
|
||||
assertEquals(ProvisioningChange.LOST_PROVISIONING,
|
||||
LinkProperties.compareProvisioning(v46lp, v6lp));
|
||||
assertEquals(ProvisioningChange.LOST_PROVISIONING,
|
||||
LinkProperties.compareProvisioning(v46lp, v4lp));
|
||||
|
||||
// Check that losing and gaining a secondary router does not change
|
||||
// the provisioning status.
|
||||
LinkProperties v6lp2 = new LinkProperties(v6lp);
|
||||
v6lp2.addRoute(new RouteInfo(GATEWAY62));
|
||||
assertTrue(v6lp2.isProvisioned());
|
||||
|
||||
assertEquals(ProvisioningChange.STILL_PROVISIONED,
|
||||
LinkProperties.compareProvisioning(v6lp2, v6lp));
|
||||
assertEquals(ProvisioningChange.STILL_PROVISIONED,
|
||||
LinkProperties.compareProvisioning(v6lp, v6lp2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsReachable() {
|
||||
final LinkProperties v4lp = new LinkProperties();
|
||||
assertFalse(v4lp.isReachable(DNS1));
|
||||
assertFalse(v4lp.isReachable(DNS2));
|
||||
|
||||
// Add an on-link route, making the on-link DNS server reachable,
|
||||
// but there is still no IPv4 address.
|
||||
assertTrue(v4lp.addRoute(new RouteInfo(
|
||||
new IpPrefix(NetworkUtils.numericToInetAddress("75.208.0.0"), 16))));
|
||||
assertFalse(v4lp.isReachable(DNS1));
|
||||
assertFalse(v4lp.isReachable(DNS2));
|
||||
|
||||
// Adding an IPv4 address (right now, any IPv4 address) means we use
|
||||
// the routes to compute likely reachability.
|
||||
assertTrue(v4lp.addLinkAddress(new LinkAddress(ADDRV4, 16)));
|
||||
assertTrue(v4lp.isReachable(DNS1));
|
||||
assertFalse(v4lp.isReachable(DNS2));
|
||||
|
||||
// Adding a default route makes the off-link DNS server reachable.
|
||||
assertTrue(v4lp.addRoute(new RouteInfo(GATEWAY1)));
|
||||
assertTrue(v4lp.isReachable(DNS1));
|
||||
assertTrue(v4lp.isReachable(DNS2));
|
||||
|
||||
final LinkProperties v6lp = new LinkProperties();
|
||||
final InetAddress kLinkLocalDns = NetworkUtils.numericToInetAddress("fe80::6:1");
|
||||
final InetAddress kLinkLocalDnsWithScope = NetworkUtils.numericToInetAddress("fe80::6:2%43");
|
||||
final InetAddress kOnLinkDns = NetworkUtils.numericToInetAddress("2001:db8:85a3::53");
|
||||
assertFalse(v6lp.isReachable(kLinkLocalDns));
|
||||
assertFalse(v6lp.isReachable(kLinkLocalDnsWithScope));
|
||||
assertFalse(v6lp.isReachable(kOnLinkDns));
|
||||
assertFalse(v6lp.isReachable(DNS6));
|
||||
|
||||
// Add a link-local route, making the link-local DNS servers reachable. Because
|
||||
// we assume the presence of an IPv6 link-local address, link-local DNS servers
|
||||
// are considered reachable, but only those with a non-zero scope identifier.
|
||||
assertTrue(v6lp.addRoute(new RouteInfo(
|
||||
new IpPrefix(NetworkUtils.numericToInetAddress("fe80::"), 64))));
|
||||
assertFalse(v6lp.isReachable(kLinkLocalDns));
|
||||
assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
|
||||
assertFalse(v6lp.isReachable(kOnLinkDns));
|
||||
assertFalse(v6lp.isReachable(DNS6));
|
||||
|
||||
// Add a link-local address--nothing changes.
|
||||
assertTrue(v6lp.addLinkAddress(LINKADDRV6LINKLOCAL));
|
||||
assertFalse(v6lp.isReachable(kLinkLocalDns));
|
||||
assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
|
||||
assertFalse(v6lp.isReachable(kOnLinkDns));
|
||||
assertFalse(v6lp.isReachable(DNS6));
|
||||
|
||||
// Add a global route on link, but no global address yet. DNS servers reachable
|
||||
// via a route that doesn't require a gateway: give them the benefit of the
|
||||
// doubt and hope the link-local source address suffices for communication.
|
||||
assertTrue(v6lp.addRoute(new RouteInfo(
|
||||
new IpPrefix(NetworkUtils.numericToInetAddress("2001:db8:85a3::"), 64))));
|
||||
assertFalse(v6lp.isReachable(kLinkLocalDns));
|
||||
assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
|
||||
assertTrue(v6lp.isReachable(kOnLinkDns));
|
||||
assertFalse(v6lp.isReachable(DNS6));
|
||||
|
||||
// Add a global address; the on-link global address DNS server is (still)
|
||||
// presumed reachable.
|
||||
assertTrue(v6lp.addLinkAddress(new LinkAddress(ADDRV6, 64)));
|
||||
assertFalse(v6lp.isReachable(kLinkLocalDns));
|
||||
assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
|
||||
assertTrue(v6lp.isReachable(kOnLinkDns));
|
||||
assertFalse(v6lp.isReachable(DNS6));
|
||||
|
||||
// Adding a default route makes the off-link DNS server reachable.
|
||||
assertTrue(v6lp.addRoute(new RouteInfo(GATEWAY62)));
|
||||
assertFalse(v6lp.isReachable(kLinkLocalDns));
|
||||
assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
|
||||
assertTrue(v6lp.isReachable(kOnLinkDns));
|
||||
assertTrue(v6lp.isReachable(DNS6));
|
||||
|
||||
// Check isReachable on stacked links. This requires that the source IP address be assigned
|
||||
// on the interface returned by the route lookup.
|
||||
LinkProperties stacked = new LinkProperties();
|
||||
|
||||
// Can't add a stacked link without an interface name.
|
||||
stacked.setInterfaceName("v4-test0");
|
||||
v6lp.addStackedLink(stacked);
|
||||
|
||||
InetAddress stackedAddress = Address("192.0.0.4");
|
||||
LinkAddress stackedLinkAddress = new LinkAddress(stackedAddress, 32);
|
||||
assertFalse(v6lp.isReachable(stackedAddress));
|
||||
stacked.addLinkAddress(stackedLinkAddress);
|
||||
assertFalse(v6lp.isReachable(stackedAddress));
|
||||
stacked.addRoute(new RouteInfo(stackedLinkAddress));
|
||||
assertTrue(stacked.isReachable(stackedAddress));
|
||||
assertTrue(v6lp.isReachable(stackedAddress));
|
||||
|
||||
assertFalse(v6lp.isReachable(DNS1));
|
||||
stacked.addRoute(new RouteInfo((IpPrefix) null, stackedAddress));
|
||||
assertTrue(v6lp.isReachable(DNS1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLinkPropertiesEnsureDirectlyConnectedRoutes() {
|
||||
// IPv4 case: no route added initially
|
||||
LinkProperties rmnet0 = new LinkProperties();
|
||||
rmnet0.setInterfaceName("rmnet0");
|
||||
rmnet0.addLinkAddress(new LinkAddress("10.0.0.2/8"));
|
||||
RouteInfo directRoute0 = new RouteInfo(new IpPrefix("10.0.0.0/8"), null,
|
||||
rmnet0.getInterfaceName());
|
||||
|
||||
// Since no routes is added explicitly, getAllRoutes() should return empty.
|
||||
assertTrue(rmnet0.getAllRoutes().isEmpty());
|
||||
rmnet0.ensureDirectlyConnectedRoutes();
|
||||
// ensureDirectlyConnectedRoutes() should have added the missing local route.
|
||||
assertEqualRoutes(Collections.singletonList(directRoute0), rmnet0.getAllRoutes());
|
||||
|
||||
// IPv4 case: both direct and default routes added initially
|
||||
LinkProperties rmnet1 = new LinkProperties();
|
||||
rmnet1.setInterfaceName("rmnet1");
|
||||
rmnet1.addLinkAddress(new LinkAddress("10.0.0.3/8"));
|
||||
RouteInfo defaultRoute1 = new RouteInfo((IpPrefix) null,
|
||||
NetworkUtils.numericToInetAddress("10.0.0.1"), rmnet1.getInterfaceName());
|
||||
RouteInfo directRoute1 = new RouteInfo(new IpPrefix("10.0.0.0/8"), null,
|
||||
rmnet1.getInterfaceName());
|
||||
rmnet1.addRoute(defaultRoute1);
|
||||
rmnet1.addRoute(directRoute1);
|
||||
|
||||
// Check added routes
|
||||
assertEqualRoutes(Arrays.asList(defaultRoute1, directRoute1), rmnet1.getAllRoutes());
|
||||
// ensureDirectlyConnectedRoutes() shouldn't change the routes since direct connected
|
||||
// route is already part of the configuration.
|
||||
rmnet1.ensureDirectlyConnectedRoutes();
|
||||
assertEqualRoutes(Arrays.asList(defaultRoute1, directRoute1), rmnet1.getAllRoutes());
|
||||
|
||||
// IPv6 case: only default routes added initially
|
||||
LinkProperties rmnet2 = new LinkProperties();
|
||||
rmnet2.setInterfaceName("rmnet2");
|
||||
rmnet2.addLinkAddress(new LinkAddress("fe80::cafe/64"));
|
||||
rmnet2.addLinkAddress(new LinkAddress("2001:db8::2/64"));
|
||||
RouteInfo defaultRoute2 = new RouteInfo((IpPrefix) null,
|
||||
NetworkUtils.numericToInetAddress("2001:db8::1"), rmnet2.getInterfaceName());
|
||||
RouteInfo directRoute2 = new RouteInfo(new IpPrefix("2001:db8::/64"), null,
|
||||
rmnet2.getInterfaceName());
|
||||
RouteInfo linkLocalRoute2 = new RouteInfo(new IpPrefix("fe80::/64"), null,
|
||||
rmnet2.getInterfaceName());
|
||||
rmnet2.addRoute(defaultRoute2);
|
||||
|
||||
assertEqualRoutes(Arrays.asList(defaultRoute2), rmnet2.getAllRoutes());
|
||||
rmnet2.ensureDirectlyConnectedRoutes();
|
||||
assertEqualRoutes(Arrays.asList(defaultRoute2, directRoute2, linkLocalRoute2),
|
||||
rmnet2.getAllRoutes());
|
||||
|
||||
// Corner case: no interface name
|
||||
LinkProperties rmnet3 = new LinkProperties();
|
||||
rmnet3.addLinkAddress(new LinkAddress("192.168.0.2/24"));
|
||||
RouteInfo directRoute3 = new RouteInfo(new IpPrefix("192.168.0.0/24"), null,
|
||||
rmnet3.getInterfaceName());
|
||||
|
||||
assertTrue(rmnet3.getAllRoutes().isEmpty());
|
||||
rmnet3.ensureDirectlyConnectedRoutes();
|
||||
assertEqualRoutes(Collections.singletonList(directRoute3), rmnet3.getAllRoutes());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompareResult() {
|
||||
// Either adding or removing items
|
||||
compareResult(Arrays.asList(1, 2, 3, 4), Arrays.asList(1),
|
||||
Arrays.asList(2, 3, 4), new ArrayList<>());
|
||||
compareResult(Arrays.asList(1, 2), Arrays.asList(3, 2, 1, 4),
|
||||
new ArrayList<>(), Arrays.asList(3, 4));
|
||||
|
||||
|
||||
// adding and removing items at the same time
|
||||
compareResult(Arrays.asList(1, 2, 3, 4), Arrays.asList(2, 3, 4, 5),
|
||||
Arrays.asList(1), Arrays.asList(5));
|
||||
compareResult(Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6),
|
||||
Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6));
|
||||
|
||||
// null cases
|
||||
compareResult(Arrays.asList(1, 2, 3), null, Arrays.asList(1, 2, 3), new ArrayList<>());
|
||||
compareResult(null, Arrays.asList(3, 2, 1), new ArrayList<>(), Arrays.asList(1, 2, 3));
|
||||
compareResult(null, null, new ArrayList<>(), new ArrayList<>());
|
||||
}
|
||||
|
||||
private void assertEqualRoutes(Collection<RouteInfo> expected, Collection<RouteInfo> actual) {
|
||||
Set<RouteInfo> expectedSet = new ArraySet<>(expected);
|
||||
Set<RouteInfo> actualSet = new ArraySet<>(actual);
|
||||
// Duplicated entries in actual routes are considered failures
|
||||
assertEquals(actual.size(), actualSet.size());
|
||||
|
||||
assertEquals(expectedSet, actualSet);
|
||||
}
|
||||
|
||||
private <T> void compareResult(List<T> oldItems, List<T> newItems, List<T> expectRemoved,
|
||||
List<T> expectAdded) {
|
||||
CompareResult<T> result = new CompareResult<>(oldItems, newItems);
|
||||
assertEquals(new ArraySet<>(expectAdded), new ArraySet<>(result.added));
|
||||
assertEquals(new ArraySet<>(expectRemoved), (new ArraySet<>(result.removed)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLinkPropertiesParcelable() throws Exception {
|
||||
LinkProperties source = new LinkProperties();
|
||||
source.setInterfaceName(NAME);
|
||||
// set 2 link addresses
|
||||
source.addLinkAddress(LINKADDRV4);
|
||||
source.addLinkAddress(LINKADDRV6);
|
||||
// set 2 dnses
|
||||
source.addDnsServer(DNS1);
|
||||
source.addDnsServer(DNS2);
|
||||
// set 2 gateways
|
||||
source.addRoute(new RouteInfo(GATEWAY1));
|
||||
source.addRoute(new RouteInfo(GATEWAY2));
|
||||
// set 2 validated private dnses
|
||||
source.addValidatedPrivateDnsServer(DNS6);
|
||||
source.addValidatedPrivateDnsServer(GATEWAY61);
|
||||
|
||||
source.setMtu(MTU);
|
||||
|
||||
source.setNat64Prefix(new IpPrefix("2001:db8:1:2:64:64::/96"));
|
||||
|
||||
TestUtils.assertParcelingIsLossless(source, LinkProperties.CREATOR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelUninitialized() throws Exception {
|
||||
LinkProperties empty = new LinkProperties();
|
||||
TestUtils.assertParcelingIsLossless(empty, LinkProperties.CREATOR);
|
||||
}
|
||||
}
|
||||
@@ -1,588 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.net.NetworkCapabilities.LINK_BANDWIDTH_UNSPECIFIED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_CBS;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_EIMS;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_FOREGROUND;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_MMS;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_OEM_PAID;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_WIFI_P2P;
|
||||
import static android.net.NetworkCapabilities.RESTRICTED_CAPABILITIES;
|
||||
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
|
||||
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
|
||||
import static android.net.NetworkCapabilities.UNRESTRICTED_CAPABILITIES;
|
||||
|
||||
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.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.test.suitebuilder.annotation.SmallTest;
|
||||
import android.util.ArraySet;
|
||||
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
public class NetworkCapabilitiesTest {
|
||||
private static final String TEST_SSID = "TEST_SSID";
|
||||
private static final String DIFFERENT_TEST_SSID = "DIFFERENT_TEST_SSID";
|
||||
|
||||
@Test
|
||||
public void testMaybeMarkCapabilitiesRestricted() {
|
||||
// verify EIMS is restricted
|
||||
assertEquals((1 << NET_CAPABILITY_EIMS) & RESTRICTED_CAPABILITIES,
|
||||
(1 << NET_CAPABILITY_EIMS));
|
||||
|
||||
// verify CBS is also restricted
|
||||
assertEquals((1 << NET_CAPABILITY_CBS) & RESTRICTED_CAPABILITIES,
|
||||
(1 << NET_CAPABILITY_CBS));
|
||||
|
||||
// verify default is not restricted
|
||||
assertEquals((1 << NET_CAPABILITY_INTERNET) & RESTRICTED_CAPABILITIES, 0);
|
||||
|
||||
// just to see
|
||||
assertEquals(RESTRICTED_CAPABILITIES & UNRESTRICTED_CAPABILITIES, 0);
|
||||
|
||||
// check that internet does not get restricted
|
||||
NetworkCapabilities netCap = new NetworkCapabilities();
|
||||
netCap.addCapability(NET_CAPABILITY_INTERNET);
|
||||
netCap.maybeMarkCapabilitiesRestricted();
|
||||
assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
|
||||
// metered-ness shouldn't matter
|
||||
netCap = new NetworkCapabilities();
|
||||
netCap.addCapability(NET_CAPABILITY_INTERNET);
|
||||
netCap.addCapability(NET_CAPABILITY_NOT_METERED);
|
||||
netCap.maybeMarkCapabilitiesRestricted();
|
||||
assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
netCap = new NetworkCapabilities();
|
||||
netCap.addCapability(NET_CAPABILITY_INTERNET);
|
||||
netCap.removeCapability(NET_CAPABILITY_NOT_METERED);
|
||||
netCap.maybeMarkCapabilitiesRestricted();
|
||||
assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
|
||||
// add EIMS - bundled with unrestricted means it's unrestricted
|
||||
netCap = new NetworkCapabilities();
|
||||
netCap.addCapability(NET_CAPABILITY_INTERNET);
|
||||
netCap.addCapability(NET_CAPABILITY_EIMS);
|
||||
netCap.addCapability(NET_CAPABILITY_NOT_METERED);
|
||||
netCap.maybeMarkCapabilitiesRestricted();
|
||||
assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
netCap = new NetworkCapabilities();
|
||||
netCap.addCapability(NET_CAPABILITY_INTERNET);
|
||||
netCap.addCapability(NET_CAPABILITY_EIMS);
|
||||
netCap.removeCapability(NET_CAPABILITY_NOT_METERED);
|
||||
netCap.maybeMarkCapabilitiesRestricted();
|
||||
assertTrue(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
|
||||
// just a restricted cap should be restricted regardless of meteredness
|
||||
netCap = new NetworkCapabilities();
|
||||
netCap.addCapability(NET_CAPABILITY_EIMS);
|
||||
netCap.addCapability(NET_CAPABILITY_NOT_METERED);
|
||||
netCap.maybeMarkCapabilitiesRestricted();
|
||||
assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
netCap = new NetworkCapabilities();
|
||||
netCap.addCapability(NET_CAPABILITY_EIMS);
|
||||
netCap.removeCapability(NET_CAPABILITY_NOT_METERED);
|
||||
netCap.maybeMarkCapabilitiesRestricted();
|
||||
assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
|
||||
// try 2 restricted caps
|
||||
netCap = new NetworkCapabilities();
|
||||
netCap.addCapability(NET_CAPABILITY_CBS);
|
||||
netCap.addCapability(NET_CAPABILITY_EIMS);
|
||||
netCap.addCapability(NET_CAPABILITY_NOT_METERED);
|
||||
netCap.maybeMarkCapabilitiesRestricted();
|
||||
assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
netCap = new NetworkCapabilities();
|
||||
netCap.addCapability(NET_CAPABILITY_CBS);
|
||||
netCap.addCapability(NET_CAPABILITY_EIMS);
|
||||
netCap.removeCapability(NET_CAPABILITY_NOT_METERED);
|
||||
netCap.maybeMarkCapabilitiesRestricted();
|
||||
assertFalse(netCap.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescribeImmutableDifferences() {
|
||||
NetworkCapabilities nc1;
|
||||
NetworkCapabilities nc2;
|
||||
|
||||
// Transports changing
|
||||
nc1 = new NetworkCapabilities().addTransportType(TRANSPORT_CELLULAR);
|
||||
nc2 = new NetworkCapabilities().addTransportType(TRANSPORT_WIFI);
|
||||
assertNotEquals("", nc1.describeImmutableDifferences(nc2));
|
||||
assertEquals("", nc1.describeImmutableDifferences(nc1));
|
||||
|
||||
// Mutable capability changing
|
||||
nc1 = new NetworkCapabilities().addCapability(NET_CAPABILITY_VALIDATED);
|
||||
nc2 = new NetworkCapabilities();
|
||||
assertEquals("", nc1.describeImmutableDifferences(nc2));
|
||||
assertEquals("", nc1.describeImmutableDifferences(nc1));
|
||||
|
||||
// NOT_METERED changing (http://b/63326103)
|
||||
nc1 = new NetworkCapabilities()
|
||||
.addCapability(NET_CAPABILITY_NOT_METERED)
|
||||
.addCapability(NET_CAPABILITY_INTERNET);
|
||||
nc2 = new NetworkCapabilities().addCapability(NET_CAPABILITY_INTERNET);
|
||||
assertEquals("", nc1.describeImmutableDifferences(nc2));
|
||||
assertEquals("", nc1.describeImmutableDifferences(nc1));
|
||||
|
||||
// Immutable capability changing
|
||||
nc1 = new NetworkCapabilities()
|
||||
.addCapability(NET_CAPABILITY_INTERNET)
|
||||
.removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
|
||||
nc2 = new NetworkCapabilities().addCapability(NET_CAPABILITY_INTERNET);
|
||||
assertNotEquals("", nc1.describeImmutableDifferences(nc2));
|
||||
assertEquals("", nc1.describeImmutableDifferences(nc1));
|
||||
|
||||
// Specifier changing
|
||||
nc1 = new NetworkCapabilities().addTransportType(TRANSPORT_WIFI);
|
||||
nc2 = new NetworkCapabilities()
|
||||
.addTransportType(TRANSPORT_WIFI)
|
||||
.setNetworkSpecifier(new StringNetworkSpecifier("specs"));
|
||||
assertNotEquals("", nc1.describeImmutableDifferences(nc2));
|
||||
assertEquals("", nc1.describeImmutableDifferences(nc1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLinkBandwidthUtils() {
|
||||
assertEquals(LINK_BANDWIDTH_UNSPECIFIED, NetworkCapabilities
|
||||
.minBandwidth(LINK_BANDWIDTH_UNSPECIFIED, LINK_BANDWIDTH_UNSPECIFIED));
|
||||
assertEquals(10, NetworkCapabilities
|
||||
.minBandwidth(LINK_BANDWIDTH_UNSPECIFIED, 10));
|
||||
assertEquals(10, NetworkCapabilities
|
||||
.minBandwidth(10, LINK_BANDWIDTH_UNSPECIFIED));
|
||||
assertEquals(10, NetworkCapabilities
|
||||
.minBandwidth(10, 20));
|
||||
|
||||
assertEquals(LINK_BANDWIDTH_UNSPECIFIED, NetworkCapabilities
|
||||
.maxBandwidth(LINK_BANDWIDTH_UNSPECIFIED, LINK_BANDWIDTH_UNSPECIFIED));
|
||||
assertEquals(10, NetworkCapabilities
|
||||
.maxBandwidth(LINK_BANDWIDTH_UNSPECIFIED, 10));
|
||||
assertEquals(10, NetworkCapabilities
|
||||
.maxBandwidth(10, LINK_BANDWIDTH_UNSPECIFIED));
|
||||
assertEquals(20, NetworkCapabilities
|
||||
.maxBandwidth(10, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetUids() {
|
||||
final NetworkCapabilities netCap = new NetworkCapabilities();
|
||||
final Set<UidRange> uids = new ArraySet<>();
|
||||
uids.add(new UidRange(50, 100));
|
||||
uids.add(new UidRange(3000, 4000));
|
||||
netCap.setUids(uids);
|
||||
assertTrue(netCap.appliesToUid(50));
|
||||
assertTrue(netCap.appliesToUid(80));
|
||||
assertTrue(netCap.appliesToUid(100));
|
||||
assertTrue(netCap.appliesToUid(3000));
|
||||
assertTrue(netCap.appliesToUid(3001));
|
||||
assertFalse(netCap.appliesToUid(10));
|
||||
assertFalse(netCap.appliesToUid(25));
|
||||
assertFalse(netCap.appliesToUid(49));
|
||||
assertFalse(netCap.appliesToUid(101));
|
||||
assertFalse(netCap.appliesToUid(2000));
|
||||
assertFalse(netCap.appliesToUid(100000));
|
||||
|
||||
assertTrue(netCap.appliesToUidRange(new UidRange(50, 100)));
|
||||
assertTrue(netCap.appliesToUidRange(new UidRange(70, 72)));
|
||||
assertTrue(netCap.appliesToUidRange(new UidRange(3500, 3912)));
|
||||
assertFalse(netCap.appliesToUidRange(new UidRange(1, 100)));
|
||||
assertFalse(netCap.appliesToUidRange(new UidRange(49, 100)));
|
||||
assertFalse(netCap.appliesToUidRange(new UidRange(1, 10)));
|
||||
assertFalse(netCap.appliesToUidRange(new UidRange(60, 101)));
|
||||
assertFalse(netCap.appliesToUidRange(new UidRange(60, 3400)));
|
||||
|
||||
NetworkCapabilities netCap2 = new NetworkCapabilities();
|
||||
// A new netcap object has null UIDs, so anything will satisfy it.
|
||||
assertTrue(netCap2.satisfiedByUids(netCap));
|
||||
// Still not equal though.
|
||||
assertFalse(netCap2.equalsUids(netCap));
|
||||
netCap2.setUids(uids);
|
||||
assertTrue(netCap2.satisfiedByUids(netCap));
|
||||
assertTrue(netCap.equalsUids(netCap2));
|
||||
assertTrue(netCap2.equalsUids(netCap));
|
||||
|
||||
uids.add(new UidRange(600, 700));
|
||||
netCap2.setUids(uids);
|
||||
assertFalse(netCap2.satisfiedByUids(netCap));
|
||||
assertFalse(netCap.appliesToUid(650));
|
||||
assertTrue(netCap2.appliesToUid(650));
|
||||
netCap.combineCapabilities(netCap2);
|
||||
assertTrue(netCap2.satisfiedByUids(netCap));
|
||||
assertTrue(netCap.appliesToUid(650));
|
||||
assertFalse(netCap.appliesToUid(500));
|
||||
|
||||
assertTrue(new NetworkCapabilities().satisfiedByUids(netCap));
|
||||
netCap.combineCapabilities(new NetworkCapabilities());
|
||||
assertTrue(netCap.appliesToUid(500));
|
||||
assertTrue(netCap.appliesToUidRange(new UidRange(1, 100000)));
|
||||
assertFalse(netCap2.appliesToUid(500));
|
||||
assertFalse(netCap2.appliesToUidRange(new UidRange(1, 100000)));
|
||||
assertTrue(new NetworkCapabilities().satisfiedByUids(netCap));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelNetworkCapabilities() {
|
||||
final Set<UidRange> uids = new ArraySet<>();
|
||||
uids.add(new UidRange(50, 100));
|
||||
uids.add(new UidRange(3000, 4000));
|
||||
final NetworkCapabilities netCap = new NetworkCapabilities()
|
||||
.addCapability(NET_CAPABILITY_INTERNET)
|
||||
.setUids(uids)
|
||||
.addCapability(NET_CAPABILITY_EIMS)
|
||||
.addCapability(NET_CAPABILITY_NOT_METERED);
|
||||
assertEqualsThroughMarshalling(netCap);
|
||||
netCap.setSSID(TEST_SSID);
|
||||
assertEqualsThroughMarshalling(netCap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOemPaid() {
|
||||
NetworkCapabilities nc = new NetworkCapabilities();
|
||||
// By default OEM_PAID is neither in the unwanted or required lists and the network is not
|
||||
// restricted.
|
||||
assertFalse(nc.hasUnwantedCapability(NET_CAPABILITY_OEM_PAID));
|
||||
assertFalse(nc.hasCapability(NET_CAPABILITY_OEM_PAID));
|
||||
nc.maybeMarkCapabilitiesRestricted();
|
||||
assertTrue(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
|
||||
// Adding OEM_PAID to capability list should make network restricted.
|
||||
nc.addCapability(NET_CAPABILITY_OEM_PAID);
|
||||
nc.addCapability(NET_CAPABILITY_INTERNET); // Combine with unrestricted capability.
|
||||
nc.maybeMarkCapabilitiesRestricted();
|
||||
assertTrue(nc.hasCapability(NET_CAPABILITY_OEM_PAID));
|
||||
assertFalse(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
|
||||
// Now let's make request for OEM_PAID network.
|
||||
NetworkCapabilities nr = new NetworkCapabilities();
|
||||
nr.addCapability(NET_CAPABILITY_OEM_PAID);
|
||||
nr.maybeMarkCapabilitiesRestricted();
|
||||
assertTrue(nr.satisfiedByNetworkCapabilities(nc));
|
||||
|
||||
// Request fails for network with the default capabilities.
|
||||
assertFalse(nr.satisfiedByNetworkCapabilities(new NetworkCapabilities()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnwantedCapabilities() {
|
||||
NetworkCapabilities network = new NetworkCapabilities();
|
||||
|
||||
NetworkCapabilities request = new NetworkCapabilities();
|
||||
assertTrue("Request: " + request + ", Network:" + network,
|
||||
request.satisfiedByNetworkCapabilities(network));
|
||||
|
||||
// Requesting absence of capabilities that network doesn't have. Request should satisfy.
|
||||
request.addUnwantedCapability(NET_CAPABILITY_WIFI_P2P);
|
||||
request.addUnwantedCapability(NET_CAPABILITY_NOT_METERED);
|
||||
assertTrue(request.satisfiedByNetworkCapabilities(network));
|
||||
assertArrayEquals(new int[] {NET_CAPABILITY_WIFI_P2P,
|
||||
NET_CAPABILITY_NOT_METERED},
|
||||
request.getUnwantedCapabilities());
|
||||
|
||||
// This is a default capability, just want to make sure its there because we use it below.
|
||||
assertTrue(network.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
|
||||
// Verify that adding unwanted capability will effectively remove it from capability list.
|
||||
request.addUnwantedCapability(NET_CAPABILITY_NOT_RESTRICTED);
|
||||
assertTrue(request.hasUnwantedCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
assertFalse(request.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
|
||||
// Now this request won't be satisfied because network contains NOT_RESTRICTED.
|
||||
assertFalse(request.satisfiedByNetworkCapabilities(network));
|
||||
network.removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
|
||||
assertTrue(request.satisfiedByNetworkCapabilities(network));
|
||||
|
||||
// Verify that adding capability will effectively remove it from unwanted list
|
||||
request.addCapability(NET_CAPABILITY_NOT_RESTRICTED);
|
||||
assertTrue(request.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
assertFalse(request.hasUnwantedCapability(NET_CAPABILITY_NOT_RESTRICTED));
|
||||
|
||||
assertFalse(request.satisfiedByNetworkCapabilities(network));
|
||||
network.addCapability(NET_CAPABILITY_NOT_RESTRICTED);
|
||||
assertTrue(request.satisfiedByNetworkCapabilities(network));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectivityManagedCapabilities() {
|
||||
NetworkCapabilities nc = new NetworkCapabilities();
|
||||
assertFalse(nc.hasConnectivityManagedCapability());
|
||||
// Check every single system managed capability.
|
||||
nc.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
|
||||
assertTrue(nc.hasConnectivityManagedCapability());
|
||||
nc.removeCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
|
||||
nc.addCapability(NET_CAPABILITY_FOREGROUND);
|
||||
assertTrue(nc.hasConnectivityManagedCapability());
|
||||
nc.removeCapability(NET_CAPABILITY_FOREGROUND);
|
||||
nc.addCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY);
|
||||
assertTrue(nc.hasConnectivityManagedCapability());
|
||||
nc.removeCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY);
|
||||
nc.addCapability(NET_CAPABILITY_VALIDATED);
|
||||
assertTrue(nc.hasConnectivityManagedCapability());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsNetCapabilities() {
|
||||
NetworkCapabilities nc1 = new NetworkCapabilities();
|
||||
NetworkCapabilities nc2 = new NetworkCapabilities();
|
||||
assertTrue(nc1.equalsNetCapabilities(nc2));
|
||||
assertEquals(nc1, nc2);
|
||||
|
||||
nc1.addCapability(NET_CAPABILITY_MMS);
|
||||
assertFalse(nc1.equalsNetCapabilities(nc2));
|
||||
assertNotEquals(nc1, nc2);
|
||||
nc2.addCapability(NET_CAPABILITY_MMS);
|
||||
assertTrue(nc1.equalsNetCapabilities(nc2));
|
||||
assertEquals(nc1, nc2);
|
||||
|
||||
nc1.addUnwantedCapability(NET_CAPABILITY_INTERNET);
|
||||
assertFalse(nc1.equalsNetCapabilities(nc2));
|
||||
nc2.addUnwantedCapability(NET_CAPABILITY_INTERNET);
|
||||
assertTrue(nc1.equalsNetCapabilities(nc2));
|
||||
|
||||
nc1.removeCapability(NET_CAPABILITY_INTERNET);
|
||||
assertFalse(nc1.equalsNetCapabilities(nc2));
|
||||
nc2.removeCapability(NET_CAPABILITY_INTERNET);
|
||||
assertTrue(nc1.equalsNetCapabilities(nc2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSSID() {
|
||||
NetworkCapabilities nc1 = new NetworkCapabilities();
|
||||
NetworkCapabilities nc2 = new NetworkCapabilities();
|
||||
assertTrue(nc2.satisfiedBySSID(nc1));
|
||||
|
||||
nc1.setSSID(TEST_SSID);
|
||||
assertTrue(nc2.satisfiedBySSID(nc1));
|
||||
nc2.setSSID("different " + TEST_SSID);
|
||||
assertFalse(nc2.satisfiedBySSID(nc1));
|
||||
|
||||
assertTrue(nc1.satisfiedByImmutableNetworkCapabilities(nc2));
|
||||
assertFalse(nc1.satisfiedByNetworkCapabilities(nc2));
|
||||
}
|
||||
|
||||
private ArraySet<UidRange> uidRange(int from, int to) {
|
||||
final ArraySet<UidRange> range = new ArraySet<>(1);
|
||||
range.add(new UidRange(from, to));
|
||||
return range;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCombineCapabilities() {
|
||||
NetworkCapabilities nc1 = new NetworkCapabilities();
|
||||
NetworkCapabilities nc2 = new NetworkCapabilities();
|
||||
|
||||
nc1.addUnwantedCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
|
||||
nc1.addCapability(NET_CAPABILITY_NOT_ROAMING);
|
||||
assertNotEquals(nc1, nc2);
|
||||
nc2.combineCapabilities(nc1);
|
||||
assertEquals(nc1, nc2);
|
||||
assertTrue(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(nc2.hasUnwantedCapability(NET_CAPABILITY_CAPTIVE_PORTAL));
|
||||
|
||||
// This will effectively move NOT_ROAMING capability from required to unwanted for nc1.
|
||||
nc1.addUnwantedCapability(NET_CAPABILITY_NOT_ROAMING);
|
||||
|
||||
nc2.combineCapabilities(nc1);
|
||||
// We will get this capability in both requested and unwanted lists thus this request
|
||||
// will never be satisfied.
|
||||
assertTrue(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(nc2.hasUnwantedCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
|
||||
nc1.setSSID(TEST_SSID);
|
||||
nc2.combineCapabilities(nc1);
|
||||
assertTrue(TEST_SSID.equals(nc2.getSSID()));
|
||||
|
||||
// Because they now have the same SSID, the following call should not throw
|
||||
nc2.combineCapabilities(nc1);
|
||||
|
||||
nc1.setSSID(DIFFERENT_TEST_SSID);
|
||||
try {
|
||||
nc2.combineCapabilities(nc1);
|
||||
fail("Expected IllegalStateException: can't combine different SSIDs");
|
||||
} catch (IllegalStateException expected) {}
|
||||
nc1.setSSID(TEST_SSID);
|
||||
|
||||
nc1.setUids(uidRange(10, 13));
|
||||
assertNotEquals(nc1, nc2);
|
||||
nc2.combineCapabilities(nc1); // Everything + 10~13 is still everything.
|
||||
assertNotEquals(nc1, nc2);
|
||||
nc1.combineCapabilities(nc2); // 10~13 + everything is everything.
|
||||
assertEquals(nc1, nc2);
|
||||
nc1.setUids(uidRange(10, 13));
|
||||
nc2.setUids(uidRange(20, 23));
|
||||
assertNotEquals(nc1, nc2);
|
||||
nc1.combineCapabilities(nc2);
|
||||
assertTrue(nc1.appliesToUid(12));
|
||||
assertFalse(nc2.appliesToUid(12));
|
||||
assertTrue(nc1.appliesToUid(22));
|
||||
assertTrue(nc2.appliesToUid(22));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetCapabilities() {
|
||||
final int[] REQUIRED_CAPABILITIES = new int[] {
|
||||
NET_CAPABILITY_INTERNET, NET_CAPABILITY_NOT_VPN };
|
||||
final int[] UNWANTED_CAPABILITIES = new int[] {
|
||||
NET_CAPABILITY_NOT_RESTRICTED, NET_CAPABILITY_NOT_METERED
|
||||
};
|
||||
|
||||
NetworkCapabilities nc1 = new NetworkCapabilities();
|
||||
NetworkCapabilities nc2 = new NetworkCapabilities();
|
||||
|
||||
nc1.setCapabilities(REQUIRED_CAPABILITIES, UNWANTED_CAPABILITIES);
|
||||
assertArrayEquals(REQUIRED_CAPABILITIES, nc1.getCapabilities());
|
||||
|
||||
// Verify that setting and adding capabilities leads to the same object state.
|
||||
nc2.clearAll();
|
||||
for (int cap : REQUIRED_CAPABILITIES) {
|
||||
nc2.addCapability(cap);
|
||||
}
|
||||
for (int cap : UNWANTED_CAPABILITIES) {
|
||||
nc2.addUnwantedCapability(cap);
|
||||
}
|
||||
assertEquals(nc1, nc2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetNetworkSpecifierOnMultiTransportNc() {
|
||||
// Sequence 1: Transport + Transport + NetworkSpecifier
|
||||
NetworkCapabilities nc1 = new NetworkCapabilities();
|
||||
nc1.addTransportType(TRANSPORT_CELLULAR).addTransportType(TRANSPORT_WIFI);
|
||||
try {
|
||||
nc1.setNetworkSpecifier(new StringNetworkSpecifier("specs"));
|
||||
fail("Cannot set NetworkSpecifier on a NetworkCapability with multiple transports!");
|
||||
} catch (IllegalStateException expected) {
|
||||
// empty
|
||||
}
|
||||
|
||||
// Sequence 2: Transport + NetworkSpecifier + Transport
|
||||
NetworkCapabilities nc2 = new NetworkCapabilities();
|
||||
nc2.addTransportType(TRANSPORT_CELLULAR).setNetworkSpecifier(
|
||||
new StringNetworkSpecifier("specs"));
|
||||
try {
|
||||
nc2.addTransportType(TRANSPORT_WIFI);
|
||||
fail("Cannot set a second TransportType of a network which has a NetworkSpecifier!");
|
||||
} catch (IllegalStateException expected) {
|
||||
// empty
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetTransportInfoOnMultiTransportNc() {
|
||||
// Sequence 1: Transport + Transport + TransportInfo
|
||||
NetworkCapabilities nc1 = new NetworkCapabilities();
|
||||
nc1.addTransportType(TRANSPORT_CELLULAR).addTransportType(TRANSPORT_WIFI)
|
||||
.setTransportInfo(new TransportInfo() {});
|
||||
|
||||
// Sequence 2: Transport + NetworkSpecifier + Transport
|
||||
NetworkCapabilities nc2 = new NetworkCapabilities();
|
||||
nc2.addTransportType(TRANSPORT_CELLULAR).setTransportInfo(new TransportInfo() {})
|
||||
.addTransportType(TRANSPORT_WIFI);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCombineTransportInfo() {
|
||||
NetworkCapabilities nc1 = new NetworkCapabilities();
|
||||
nc1.setTransportInfo(new TransportInfo() {
|
||||
// empty
|
||||
});
|
||||
NetworkCapabilities nc2 = new NetworkCapabilities();
|
||||
// new TransportInfo so that object is not #equals to nc1's TransportInfo (that's where
|
||||
// combine fails)
|
||||
nc2.setTransportInfo(new TransportInfo() {
|
||||
// empty
|
||||
});
|
||||
|
||||
try {
|
||||
nc1.combineCapabilities(nc2);
|
||||
fail("Should not be able to combine NetworkCabilities which contain TransportInfos");
|
||||
} catch (IllegalStateException expected) {
|
||||
// empty
|
||||
}
|
||||
|
||||
// verify that can combine with identical TransportInfo objects
|
||||
NetworkCapabilities nc3 = new NetworkCapabilities();
|
||||
nc3.setTransportInfo(nc1.getTransportInfo());
|
||||
nc1.combineCapabilities(nc3);
|
||||
}
|
||||
|
||||
private void assertEqualsThroughMarshalling(NetworkCapabilities netCap) {
|
||||
Parcel p = Parcel.obtain();
|
||||
netCap.writeToParcel(p, /* flags */ 0);
|
||||
p.setDataPosition(0);
|
||||
byte[] marshalledData = p.marshall();
|
||||
|
||||
p = Parcel.obtain();
|
||||
p.unmarshall(marshalledData, 0, marshalledData.length);
|
||||
p.setDataPosition(0);
|
||||
assertEquals(NetworkCapabilities.CREATOR.createFromParcel(p), netCap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSet() {
|
||||
NetworkCapabilities nc1 = new NetworkCapabilities();
|
||||
NetworkCapabilities nc2 = new NetworkCapabilities();
|
||||
|
||||
nc1.addUnwantedCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
|
||||
nc1.addCapability(NET_CAPABILITY_NOT_ROAMING);
|
||||
assertNotEquals(nc1, nc2);
|
||||
nc2.set(nc1);
|
||||
assertEquals(nc1, nc2);
|
||||
assertTrue(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(nc2.hasUnwantedCapability(NET_CAPABILITY_CAPTIVE_PORTAL));
|
||||
|
||||
// This will effectively move NOT_ROAMING capability from required to unwanted for nc1.
|
||||
nc1.addUnwantedCapability(NET_CAPABILITY_NOT_ROAMING);
|
||||
nc1.setSSID(TEST_SSID);
|
||||
nc2.set(nc1);
|
||||
assertEquals(nc1, nc2);
|
||||
// Contrary to combineCapabilities, set() will have removed the NOT_ROAMING capability
|
||||
// from nc2.
|
||||
assertFalse(nc2.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(nc2.hasUnwantedCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(TEST_SSID.equals(nc2.getSSID()));
|
||||
|
||||
nc1.setSSID(DIFFERENT_TEST_SSID);
|
||||
nc2.set(nc1);
|
||||
assertEquals(nc1, nc2);
|
||||
assertTrue(DIFFERENT_TEST_SSID.equals(nc2.getSSID()));
|
||||
|
||||
nc1.setUids(uidRange(10, 13));
|
||||
nc2.set(nc1); // Overwrites, as opposed to combineCapabilities
|
||||
assertEquals(nc1, nc2);
|
||||
}
|
||||
}
|
||||
@@ -1,158 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2015 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.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import android.net.LocalServerSocket;
|
||||
import android.net.LocalSocket;
|
||||
import android.net.LocalSocketAddress;
|
||||
import android.net.Network;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileDescriptor;
|
||||
import java.io.FileInputStream;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.util.Objects;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
public class NetworkTest {
|
||||
final Network mNetwork = new Network(99);
|
||||
|
||||
@Test
|
||||
public void testBindSocketOfInvalidFdThrows() throws Exception {
|
||||
|
||||
final FileDescriptor fd = new FileDescriptor();
|
||||
assertFalse(fd.valid());
|
||||
|
||||
try {
|
||||
mNetwork.bindSocket(fd);
|
||||
fail("SocketException not thrown");
|
||||
} catch (SocketException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindSocketOfNonSocketFdThrows() throws Exception {
|
||||
final File devNull = new File("/dev/null");
|
||||
assertTrue(devNull.canRead());
|
||||
|
||||
final FileInputStream fis = new FileInputStream(devNull);
|
||||
assertTrue(null != fis.getFD());
|
||||
assertTrue(fis.getFD().valid());
|
||||
|
||||
try {
|
||||
mNetwork.bindSocket(fis.getFD());
|
||||
fail("SocketException not thrown");
|
||||
} catch (SocketException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindSocketOfConnectedDatagramSocketThrows() throws Exception {
|
||||
final DatagramSocket mDgramSocket = new DatagramSocket(0, (InetAddress) Inet6Address.ANY);
|
||||
mDgramSocket.connect((InetAddress) Inet6Address.LOOPBACK, 53);
|
||||
assertTrue(mDgramSocket.isConnected());
|
||||
|
||||
try {
|
||||
mNetwork.bindSocket(mDgramSocket);
|
||||
fail("SocketException not thrown");
|
||||
} catch (SocketException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindSocketOfLocalSocketThrows() throws Exception {
|
||||
final LocalSocket mLocalClient = new LocalSocket();
|
||||
mLocalClient.bind(new LocalSocketAddress("testClient"));
|
||||
assertTrue(mLocalClient.getFileDescriptor().valid());
|
||||
|
||||
try {
|
||||
mNetwork.bindSocket(mLocalClient.getFileDescriptor());
|
||||
fail("SocketException not thrown");
|
||||
} catch (SocketException expected) {}
|
||||
|
||||
final LocalServerSocket mLocalServer = new LocalServerSocket("testServer");
|
||||
mLocalClient.connect(mLocalServer.getLocalSocketAddress());
|
||||
assertTrue(mLocalClient.isConnected());
|
||||
|
||||
try {
|
||||
mNetwork.bindSocket(mLocalClient.getFileDescriptor());
|
||||
fail("SocketException not thrown");
|
||||
} catch (SocketException expected) {}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZeroIsObviousForDebugging() {
|
||||
Network zero = new Network(0);
|
||||
assertEquals(0, zero.hashCode());
|
||||
assertEquals(0, zero.getNetworkHandle());
|
||||
assertEquals("0", zero.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNetworkHandle() {
|
||||
Network one = new Network(1);
|
||||
Network two = new Network(2);
|
||||
Network three = new Network(3);
|
||||
|
||||
// None of the hashcodes are zero.
|
||||
assertNotEqual(0, one.hashCode());
|
||||
assertNotEqual(0, two.hashCode());
|
||||
assertNotEqual(0, three.hashCode());
|
||||
|
||||
// All the hashcodes are distinct.
|
||||
assertNotEqual(one.hashCode(), two.hashCode());
|
||||
assertNotEqual(one.hashCode(), three.hashCode());
|
||||
assertNotEqual(two.hashCode(), three.hashCode());
|
||||
|
||||
// None of the handles are zero.
|
||||
assertNotEqual(0, one.getNetworkHandle());
|
||||
assertNotEqual(0, two.getNetworkHandle());
|
||||
assertNotEqual(0, three.getNetworkHandle());
|
||||
|
||||
// All the handles are distinct.
|
||||
assertNotEqual(one.getNetworkHandle(), two.getNetworkHandle());
|
||||
assertNotEqual(one.getNetworkHandle(), three.getNetworkHandle());
|
||||
assertNotEqual(two.getNetworkHandle(), three.getNetworkHandle());
|
||||
|
||||
// The handles are not equal to the hashcodes.
|
||||
assertNotEqual(one.hashCode(), one.getNetworkHandle());
|
||||
assertNotEqual(two.hashCode(), two.getNetworkHandle());
|
||||
assertNotEqual(three.hashCode(), three.getNetworkHandle());
|
||||
|
||||
// Adjust as necessary to test an implementation's specific constants.
|
||||
// When running with runtest, "adb logcat -s TestRunner" can be useful.
|
||||
assertEquals(7700664333L, one.getNetworkHandle());
|
||||
assertEquals(11995631629L, two.getNetworkHandle());
|
||||
assertEquals(16290598925L, three.getNetworkHandle());
|
||||
}
|
||||
|
||||
private static <T> void assertNotEqual(T t1, T t2) {
|
||||
assertFalse(Objects.equals(t1, t2));
|
||||
}
|
||||
}
|
||||
@@ -1,288 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2010 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.net.RouteInfo.RTN_UNREACHABLE;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.test.suitebuilder.annotation.SmallTest;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
|
||||
public class RouteInfoTest extends TestCase {
|
||||
|
||||
private InetAddress Address(String addr) {
|
||||
return InetAddress.parseNumericAddress(addr);
|
||||
}
|
||||
|
||||
private IpPrefix Prefix(String prefix) {
|
||||
return new IpPrefix(prefix);
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
public void testConstructor() {
|
||||
RouteInfo r;
|
||||
|
||||
// Invalid input.
|
||||
try {
|
||||
r = new RouteInfo((IpPrefix) null, null, "rmnet0");
|
||||
fail("Expected RuntimeException: destination and gateway null");
|
||||
} catch(RuntimeException e) {}
|
||||
|
||||
// Null destination is default route.
|
||||
r = new RouteInfo((IpPrefix) null, Address("2001:db8::1"), null);
|
||||
assertEquals(Prefix("::/0"), r.getDestination());
|
||||
assertEquals(Address("2001:db8::1"), r.getGateway());
|
||||
assertNull(r.getInterface());
|
||||
|
||||
r = new RouteInfo((IpPrefix) null, Address("192.0.2.1"), "wlan0");
|
||||
assertEquals(Prefix("0.0.0.0/0"), r.getDestination());
|
||||
assertEquals(Address("192.0.2.1"), r.getGateway());
|
||||
assertEquals("wlan0", r.getInterface());
|
||||
|
||||
// Null gateway sets gateway to unspecified address (why?).
|
||||
r = new RouteInfo(Prefix("2001:db8:beef:cafe::/48"), null, "lo");
|
||||
assertEquals(Prefix("2001:db8:beef::/48"), r.getDestination());
|
||||
assertEquals(Address("::"), r.getGateway());
|
||||
assertEquals("lo", r.getInterface());
|
||||
|
||||
r = new RouteInfo(Prefix("192.0.2.5/24"), null);
|
||||
assertEquals(Prefix("192.0.2.0/24"), r.getDestination());
|
||||
assertEquals(Address("0.0.0.0"), r.getGateway());
|
||||
assertNull(r.getInterface());
|
||||
}
|
||||
|
||||
public void testMatches() {
|
||||
class PatchedRouteInfo {
|
||||
private final RouteInfo mRouteInfo;
|
||||
|
||||
public PatchedRouteInfo(IpPrefix destination, InetAddress gateway, String iface) {
|
||||
mRouteInfo = new RouteInfo(destination, gateway, iface);
|
||||
}
|
||||
|
||||
public boolean matches(InetAddress destination) {
|
||||
return mRouteInfo.matches(destination);
|
||||
}
|
||||
}
|
||||
|
||||
PatchedRouteInfo r;
|
||||
|
||||
r = new PatchedRouteInfo(Prefix("2001:db8:f00::ace:d00d/127"), null, "rmnet0");
|
||||
assertTrue(r.matches(Address("2001:db8:f00::ace:d00c")));
|
||||
assertTrue(r.matches(Address("2001:db8:f00::ace:d00d")));
|
||||
assertFalse(r.matches(Address("2001:db8:f00::ace:d00e")));
|
||||
assertFalse(r.matches(Address("2001:db8:f00::bad:d00d")));
|
||||
assertFalse(r.matches(Address("2001:4868:4860::8888")));
|
||||
assertFalse(r.matches(Address("8.8.8.8")));
|
||||
|
||||
r = new PatchedRouteInfo(Prefix("192.0.2.0/23"), null, "wlan0");
|
||||
assertTrue(r.matches(Address("192.0.2.43")));
|
||||
assertTrue(r.matches(Address("192.0.3.21")));
|
||||
assertFalse(r.matches(Address("192.0.0.21")));
|
||||
assertFalse(r.matches(Address("8.8.8.8")));
|
||||
|
||||
PatchedRouteInfo ipv6Default = new PatchedRouteInfo(Prefix("::/0"), null, "rmnet0");
|
||||
assertTrue(ipv6Default.matches(Address("2001:db8::f00")));
|
||||
assertFalse(ipv6Default.matches(Address("192.0.2.1")));
|
||||
|
||||
PatchedRouteInfo ipv4Default = new PatchedRouteInfo(Prefix("0.0.0.0/0"), null, "rmnet0");
|
||||
assertTrue(ipv4Default.matches(Address("255.255.255.255")));
|
||||
assertTrue(ipv4Default.matches(Address("192.0.2.1")));
|
||||
assertFalse(ipv4Default.matches(Address("2001:db8::f00")));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
public void testEquals() {
|
||||
// IPv4
|
||||
RouteInfo r1 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::1"), "wlan0");
|
||||
RouteInfo r2 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::1"), "wlan0");
|
||||
assertAreEqual(r1, r2);
|
||||
|
||||
RouteInfo r3 = new RouteInfo(Prefix("2001:db8:ace::/49"), Address("2001:db8::1"), "wlan0");
|
||||
RouteInfo r4 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::2"), "wlan0");
|
||||
RouteInfo r5 = new RouteInfo(Prefix("2001:db8:ace::/48"), Address("2001:db8::1"), "rmnet0");
|
||||
assertAreNotEqual(r1, r3);
|
||||
assertAreNotEqual(r1, r4);
|
||||
assertAreNotEqual(r1, r5);
|
||||
|
||||
// IPv6
|
||||
r1 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), "wlan0");
|
||||
r2 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), "wlan0");
|
||||
assertAreEqual(r1, r2);
|
||||
|
||||
r3 = new RouteInfo(Prefix("192.0.2.0/24"), Address("192.0.2.1"), "wlan0");
|
||||
r4 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.2"), "wlan0");
|
||||
r5 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), "rmnet0");
|
||||
assertAreNotEqual(r1, r3);
|
||||
assertAreNotEqual(r1, r4);
|
||||
assertAreNotEqual(r1, r5);
|
||||
|
||||
// Interfaces (but not destinations or gateways) can be null.
|
||||
r1 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), null);
|
||||
r2 = new RouteInfo(Prefix("192.0.2.0/25"), Address("192.0.2.1"), null);
|
||||
r3 = new RouteInfo(Prefix("192.0.2.0/24"), Address("192.0.2.1"), "wlan0");
|
||||
assertAreEqual(r1, r2);
|
||||
assertAreNotEqual(r1, r3);
|
||||
}
|
||||
|
||||
public void testHostAndDefaultRoutes() {
|
||||
RouteInfo r;
|
||||
|
||||
r = new RouteInfo(Prefix("0.0.0.0/0"), Address("0.0.0.0"), "wlan0");
|
||||
assertFalse(r.isHostRoute());
|
||||
assertTrue(r.isDefaultRoute());
|
||||
assertTrue(r.isIPv4Default());
|
||||
assertFalse(r.isIPv6Default());
|
||||
|
||||
r = new RouteInfo(Prefix("::/0"), Address("::"), "wlan0");
|
||||
assertFalse(r.isHostRoute());
|
||||
assertTrue(r.isDefaultRoute());
|
||||
assertFalse(r.isIPv4Default());
|
||||
assertTrue(r.isIPv6Default());
|
||||
|
||||
r = new RouteInfo(Prefix("192.0.2.0/24"), null, "wlan0");
|
||||
assertFalse(r.isHostRoute());
|
||||
assertFalse(r.isDefaultRoute());
|
||||
assertFalse(r.isIPv4Default());
|
||||
assertFalse(r.isIPv6Default());
|
||||
|
||||
r = new RouteInfo(Prefix("2001:db8::/48"), null, "wlan0");
|
||||
assertFalse(r.isHostRoute());
|
||||
assertFalse(r.isDefaultRoute());
|
||||
assertFalse(r.isIPv4Default());
|
||||
assertFalse(r.isIPv6Default());
|
||||
|
||||
r = new RouteInfo(Prefix("192.0.2.0/32"), Address("0.0.0.0"), "wlan0");
|
||||
assertTrue(r.isHostRoute());
|
||||
assertFalse(r.isDefaultRoute());
|
||||
assertFalse(r.isIPv4Default());
|
||||
assertFalse(r.isIPv6Default());
|
||||
|
||||
r = new RouteInfo(Prefix("2001:db8::/128"), Address("::"), "wlan0");
|
||||
assertTrue(r.isHostRoute());
|
||||
assertFalse(r.isDefaultRoute());
|
||||
assertFalse(r.isIPv4Default());
|
||||
assertFalse(r.isIPv6Default());
|
||||
|
||||
r = new RouteInfo(Prefix("192.0.2.0/32"), null, "wlan0");
|
||||
assertTrue(r.isHostRoute());
|
||||
assertFalse(r.isDefaultRoute());
|
||||
assertFalse(r.isIPv4Default());
|
||||
assertFalse(r.isIPv6Default());
|
||||
|
||||
r = new RouteInfo(Prefix("2001:db8::/128"), null, "wlan0");
|
||||
assertTrue(r.isHostRoute());
|
||||
assertFalse(r.isDefaultRoute());
|
||||
assertFalse(r.isIPv4Default());
|
||||
assertFalse(r.isIPv6Default());
|
||||
|
||||
r = new RouteInfo(Prefix("::/128"), Address("fe80::"), "wlan0");
|
||||
assertTrue(r.isHostRoute());
|
||||
assertFalse(r.isDefaultRoute());
|
||||
assertFalse(r.isIPv4Default());
|
||||
assertFalse(r.isIPv6Default());
|
||||
|
||||
r = new RouteInfo(Prefix("0.0.0.0/32"), Address("192.0.2.1"), "wlan0");
|
||||
assertTrue(r.isHostRoute());
|
||||
assertFalse(r.isDefaultRoute());
|
||||
assertFalse(r.isIPv4Default());
|
||||
assertFalse(r.isIPv6Default());
|
||||
|
||||
r = new RouteInfo(Prefix("0.0.0.0/32"), Address("192.0.2.1"), "wlan0");
|
||||
assertTrue(r.isHostRoute());
|
||||
assertFalse(r.isDefaultRoute());
|
||||
assertFalse(r.isIPv4Default());
|
||||
assertFalse(r.isIPv6Default());
|
||||
|
||||
r = new RouteInfo(new IpPrefix(Inet4Address.ANY, 0), RTN_UNREACHABLE);
|
||||
assertFalse(r.isHostRoute());
|
||||
assertFalse(r.isDefaultRoute());
|
||||
assertFalse(r.isIPv4Default());
|
||||
assertFalse(r.isIPv6Default());
|
||||
|
||||
r = new RouteInfo(new IpPrefix(Inet6Address.ANY, 0), RTN_UNREACHABLE);
|
||||
assertFalse(r.isHostRoute());
|
||||
assertFalse(r.isDefaultRoute());
|
||||
assertFalse(r.isIPv4Default());
|
||||
assertFalse(r.isIPv6Default());
|
||||
}
|
||||
|
||||
public void testTruncation() {
|
||||
LinkAddress l;
|
||||
RouteInfo r;
|
||||
|
||||
l = new LinkAddress("192.0.2.5/30");
|
||||
r = new RouteInfo(l, Address("192.0.2.1"), "wlan0");
|
||||
assertEquals("192.0.2.4", r.getDestination().getAddress().getHostAddress());
|
||||
|
||||
l = new LinkAddress("2001:db8:1:f::5/63");
|
||||
r = new RouteInfo(l, Address("2001:db8:5::1"), "wlan0");
|
||||
assertEquals("2001:db8:1:e::", r.getDestination().getAddress().getHostAddress());
|
||||
}
|
||||
|
||||
// Make sure that creating routes to multicast addresses doesn't throw an exception. Even though
|
||||
// there's nothing we can do with them, we don't want to crash if, e.g., someone calls
|
||||
// requestRouteToHostAddress("230.0.0.0", MOBILE_HIPRI);
|
||||
public void testMulticastRoute() {
|
||||
RouteInfo r;
|
||||
r = new RouteInfo(Prefix("230.0.0.0/32"), Address("192.0.2.1"), "wlan0");
|
||||
r = new RouteInfo(Prefix("ff02::1/128"), Address("2001:db8::1"), "wlan0");
|
||||
// No exceptions? Good.
|
||||
}
|
||||
|
||||
public RouteInfo passThroughParcel(RouteInfo r) {
|
||||
Parcel p = Parcel.obtain();
|
||||
RouteInfo r2 = null;
|
||||
try {
|
||||
r.writeToParcel(p, 0);
|
||||
p.setDataPosition(0);
|
||||
r2 = RouteInfo.CREATOR.createFromParcel(p);
|
||||
} finally {
|
||||
p.recycle();
|
||||
}
|
||||
assertNotNull(r2);
|
||||
return r2;
|
||||
}
|
||||
|
||||
public void assertParcelingIsLossless(RouteInfo r) {
|
||||
RouteInfo r2 = passThroughParcel(r);
|
||||
assertEquals(r, r2);
|
||||
}
|
||||
|
||||
public void testParceling() {
|
||||
RouteInfo r;
|
||||
|
||||
r = new RouteInfo(Prefix("::/0"), Address("2001:db8::"), null);
|
||||
assertParcelingIsLossless(r);
|
||||
|
||||
r = new RouteInfo(Prefix("192.0.2.0/24"), null, "wlan0");
|
||||
assertParcelingIsLossless(r);
|
||||
}
|
||||
}
|
||||
@@ -1,221 +0,0 @@
|
||||
/*
|
||||
* 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.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import android.os.Parcel;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
public class StaticIpConfigurationTest {
|
||||
|
||||
private static final String ADDRSTR = "192.0.2.2/25";
|
||||
private static final LinkAddress ADDR = new LinkAddress(ADDRSTR);
|
||||
private static final InetAddress GATEWAY = IpAddress("192.0.2.1");
|
||||
private static final InetAddress OFFLINKGATEWAY = IpAddress("192.0.2.129");
|
||||
private static final InetAddress DNS1 = IpAddress("8.8.8.8");
|
||||
private static final InetAddress DNS2 = IpAddress("8.8.4.4");
|
||||
private static final InetAddress DNS3 = IpAddress("4.2.2.2");
|
||||
private static final String IFACE = "eth0";
|
||||
|
||||
private static InetAddress IpAddress(String addr) {
|
||||
return InetAddress.parseNumericAddress(addr);
|
||||
}
|
||||
|
||||
private void checkEmpty(StaticIpConfiguration s) {
|
||||
assertNull(s.ipAddress);
|
||||
assertNull(s.gateway);
|
||||
assertNull(s.domains);
|
||||
assertEquals(0, s.dnsServers.size());
|
||||
}
|
||||
|
||||
private static <T> void assertNotEquals(T t1, T t2) {
|
||||
assertFalse(Objects.equals(t1, t2));
|
||||
}
|
||||
|
||||
private StaticIpConfiguration makeTestObject() {
|
||||
StaticIpConfiguration s = new StaticIpConfiguration();
|
||||
s.ipAddress = ADDR;
|
||||
s.gateway = GATEWAY;
|
||||
s.dnsServers.add(DNS1);
|
||||
s.dnsServers.add(DNS2);
|
||||
s.dnsServers.add(DNS3);
|
||||
s.domains = "google.com";
|
||||
return s;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor() {
|
||||
StaticIpConfiguration s = new StaticIpConfiguration();
|
||||
checkEmpty(s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyAndClear() {
|
||||
StaticIpConfiguration empty = new StaticIpConfiguration((StaticIpConfiguration) null);
|
||||
checkEmpty(empty);
|
||||
|
||||
StaticIpConfiguration s1 = makeTestObject();
|
||||
StaticIpConfiguration s2 = new StaticIpConfiguration(s1);
|
||||
assertEquals(s1, s2);
|
||||
s2.clear();
|
||||
assertEquals(empty, s2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashCodeAndEquals() {
|
||||
HashSet<Integer> hashCodes = new HashSet();
|
||||
hashCodes.add(0);
|
||||
|
||||
StaticIpConfiguration s = new StaticIpConfiguration();
|
||||
// Check that this hash code is nonzero and different from all the ones seen so far.
|
||||
assertTrue(hashCodes.add(s.hashCode()));
|
||||
|
||||
s.ipAddress = ADDR;
|
||||
assertTrue(hashCodes.add(s.hashCode()));
|
||||
|
||||
s.gateway = GATEWAY;
|
||||
assertTrue(hashCodes.add(s.hashCode()));
|
||||
|
||||
s.dnsServers.add(DNS1);
|
||||
assertTrue(hashCodes.add(s.hashCode()));
|
||||
|
||||
s.dnsServers.add(DNS2);
|
||||
assertTrue(hashCodes.add(s.hashCode()));
|
||||
|
||||
s.dnsServers.add(DNS3);
|
||||
assertTrue(hashCodes.add(s.hashCode()));
|
||||
|
||||
s.domains = "example.com";
|
||||
assertTrue(hashCodes.add(s.hashCode()));
|
||||
|
||||
assertFalse(s.equals(null));
|
||||
assertEquals(s, s);
|
||||
|
||||
StaticIpConfiguration s2 = new StaticIpConfiguration(s);
|
||||
assertEquals(s, s2);
|
||||
|
||||
s.ipAddress = new LinkAddress(DNS1, 32);
|
||||
assertNotEquals(s, s2);
|
||||
|
||||
s2 = new StaticIpConfiguration(s);
|
||||
s.domains = "foo";
|
||||
assertNotEquals(s, s2);
|
||||
|
||||
s2 = new StaticIpConfiguration(s);
|
||||
s.gateway = DNS2;
|
||||
assertNotEquals(s, s2);
|
||||
|
||||
s2 = new StaticIpConfiguration(s);
|
||||
s.dnsServers.add(DNS3);
|
||||
assertNotEquals(s, s2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToLinkProperties() {
|
||||
LinkProperties expected = new LinkProperties();
|
||||
expected.setInterfaceName(IFACE);
|
||||
|
||||
StaticIpConfiguration s = new StaticIpConfiguration();
|
||||
assertEquals(expected, s.toLinkProperties(IFACE));
|
||||
|
||||
final RouteInfo connectedRoute = new RouteInfo(new IpPrefix(ADDRSTR), null, IFACE);
|
||||
s.ipAddress = ADDR;
|
||||
expected.addLinkAddress(ADDR);
|
||||
expected.addRoute(connectedRoute);
|
||||
assertEquals(expected, s.toLinkProperties(IFACE));
|
||||
|
||||
s.gateway = GATEWAY;
|
||||
RouteInfo defaultRoute = new RouteInfo(new IpPrefix("0.0.0.0/0"), GATEWAY, IFACE);
|
||||
expected.addRoute(defaultRoute);
|
||||
assertEquals(expected, s.toLinkProperties(IFACE));
|
||||
|
||||
s.gateway = OFFLINKGATEWAY;
|
||||
expected.removeRoute(defaultRoute);
|
||||
defaultRoute = new RouteInfo(new IpPrefix("0.0.0.0/0"), OFFLINKGATEWAY, IFACE);
|
||||
expected.addRoute(defaultRoute);
|
||||
|
||||
RouteInfo gatewayRoute = new RouteInfo(new IpPrefix("192.0.2.129/32"), null, IFACE);
|
||||
expected.addRoute(gatewayRoute);
|
||||
assertEquals(expected, s.toLinkProperties(IFACE));
|
||||
|
||||
s.dnsServers.add(DNS1);
|
||||
expected.addDnsServer(DNS1);
|
||||
assertEquals(expected, s.toLinkProperties(IFACE));
|
||||
|
||||
s.dnsServers.add(DNS2);
|
||||
s.dnsServers.add(DNS3);
|
||||
expected.addDnsServer(DNS2);
|
||||
expected.addDnsServer(DNS3);
|
||||
assertEquals(expected, s.toLinkProperties(IFACE));
|
||||
|
||||
s.domains = "google.com";
|
||||
expected.setDomains("google.com");
|
||||
assertEquals(expected, s.toLinkProperties(IFACE));
|
||||
|
||||
s.gateway = null;
|
||||
expected.removeRoute(defaultRoute);
|
||||
expected.removeRoute(gatewayRoute);
|
||||
assertEquals(expected, s.toLinkProperties(IFACE));
|
||||
|
||||
// Without knowing the IP address, we don't have a directly-connected route, so we can't
|
||||
// tell if the gateway is off-link or not and we don't add a host route. This isn't a real
|
||||
// configuration, but we should at least not crash.
|
||||
s.gateway = OFFLINKGATEWAY;
|
||||
s.ipAddress = null;
|
||||
expected.removeLinkAddress(ADDR);
|
||||
expected.removeRoute(connectedRoute);
|
||||
expected.addRoute(defaultRoute);
|
||||
assertEquals(expected, s.toLinkProperties(IFACE));
|
||||
}
|
||||
|
||||
private StaticIpConfiguration passThroughParcel(StaticIpConfiguration s) {
|
||||
Parcel p = Parcel.obtain();
|
||||
StaticIpConfiguration s2 = null;
|
||||
try {
|
||||
s.writeToParcel(p, 0);
|
||||
p.setDataPosition(0);
|
||||
s2 = StaticIpConfiguration.readFromParcel(p);
|
||||
} finally {
|
||||
p.recycle();
|
||||
}
|
||||
assertNotNull(s2);
|
||||
return s2;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParceling() {
|
||||
StaticIpConfiguration s = makeTestObject();
|
||||
StaticIpConfiguration s2 = passThroughParcel(s);
|
||||
assertEquals(s, s2);
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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.apf;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import android.net.shared.ParcelableTestUtil;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import com.android.internal.util.TestUtils;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
public class ApfCapabilitiesTest {
|
||||
@Test
|
||||
public void testParcelUnparcel() {
|
||||
final ApfCapabilities caps = new ApfCapabilities(123, 456, 789);
|
||||
ParcelableTestUtil.assertFieldCountEquals(3, ApfCapabilities.class);
|
||||
|
||||
TestUtils.assertParcelingIsLossless(caps, ApfCapabilities.CREATOR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
assertEquals(new ApfCapabilities(1, 2, 3), new ApfCapabilities(1, 2, 3));
|
||||
assertNotEquals(new ApfCapabilities(2, 2, 3), new ApfCapabilities(1, 2, 3));
|
||||
assertNotEquals(new ApfCapabilities(1, 3, 3), new ApfCapabilities(1, 2, 3));
|
||||
assertNotEquals(new ApfCapabilities(1, 2, 4), new ApfCapabilities(1, 2, 3));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user