Merge "Import a few android.net unit tests into frameworks-net" am: 24660e5bb7 am: 1ce75f643c am: 3d4d464f0a
am: 86eb8a58af
Change-Id: I4922d8505e641652cea028c6d60a0f9996b130e6
diff --git a/core/tests/coretests/src/android/net/LinkPropertiesTest.java b/tests/net/java/android/net/LinkPropertiesTest.java
similarity index 80%
rename from core/tests/coretests/src/android/net/LinkPropertiesTest.java
rename to tests/net/java/android/net/LinkPropertiesTest.java
index 1cb0ecd..52da79a 100644
--- a/core/tests/coretests/src/android/net/LinkPropertiesTest.java
+++ b/tests/net/java/android/net/LinkPropertiesTest.java
@@ -16,19 +16,22 @@
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.IpPrefix;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.LinkProperties.CompareResult;
import android.net.LinkProperties.ProvisioningChange;
import android.net.RouteInfo;
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
import android.system.OsConstants;
-import android.test.suitebuilder.annotation.SmallTest;
-import android.test.suitebuilder.annotation.Suppress;
import android.util.ArraySet;
-import junit.framework.TestCase;
-
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
@@ -37,8 +40,12 @@
import java.util.List;
import java.util.Set;
+import org.junit.Test;
+import org.junit.runner.RunWith;
-public class LinkPropertiesTest extends TestCase {
+@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");
@@ -92,7 +99,7 @@
assertEquals(source.hashCode(), target.hashCode());
}
- @SmallTest
+ @Test
public void testEqualsNull() {
LinkProperties source = new LinkProperties();
LinkProperties target = new LinkProperties();
@@ -101,155 +108,141 @@
assertLinkPropertiesEqual(source, target);
}
- @SmallTest
- public void testEqualsSameOrder() {
- try {
- 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);
+ @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 2 gateways
+ source.addRoute(new RouteInfo(GATEWAY1));
+ source.addRoute(new RouteInfo(GATEWAY2));
+ source.setMtu(MTU);
- LinkProperties target = new LinkProperties();
+ LinkProperties target = new LinkProperties();
- // All fields are same
- 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));
- target.setMtu(MTU);
+ // All fields are same
+ 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));
+ target.setMtu(MTU);
- assertLinkPropertiesEqual(source, target);
+ assertLinkPropertiesEqual(source, target);
- target.clear();
- // change Interface Name
- target.setInterfaceName("qmi1");
- target.addLinkAddress(LINKADDRV4);
- target.addLinkAddress(LINKADDRV6);
- target.addDnsServer(DNS1);
- target.addDnsServer(DNS2);
- target.addRoute(new RouteInfo(GATEWAY1));
- target.addRoute(new RouteInfo(GATEWAY2));
- target.setMtu(MTU);
- assertFalse(source.equals(target));
+ target.clear();
+ // change Interface Name
+ target.setInterfaceName("qmi1");
+ target.addLinkAddress(LINKADDRV4);
+ target.addLinkAddress(LINKADDRV6);
+ target.addDnsServer(DNS1);
+ target.addDnsServer(DNS2);
+ 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.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.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.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.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);
+ // 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));
-
- } catch (Exception e) {
- throw new RuntimeException(e.toString());
- //fail();
- }
+ 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));
}
- @SmallTest
- public void testEqualsDifferentOrder() {
- try {
- 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);
+ @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);
+ 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);
- } catch (Exception e) {
- fail();
- }
+ assertLinkPropertiesEqual(source, target);
}
- @SmallTest
- public void testEqualsDuplicated() {
- try {
- LinkProperties source = new LinkProperties();
- // set 3 link addresses, eg, [A, A, B]
- source.addLinkAddress(LINKADDRV4);
- source.addLinkAddress(LINKADDRV4);
- source.addLinkAddress(LINKADDRV6);
+ @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);
+ LinkProperties target = new LinkProperties();
+ // set 3 link addresses, eg, [A, B, B]
+ target.addLinkAddress(LINKADDRV4);
+ target.addLinkAddress(LINKADDRV6);
+ target.addLinkAddress(LINKADDRV6);
- assertLinkPropertiesEqual(source, target);
- } catch (Exception e) {
- fail();
- }
+ assertLinkPropertiesEqual(source, target);
}
private void assertAllRoutesHaveInterface(String iface, LinkProperties lp) {
@@ -258,7 +251,7 @@
}
}
- @SmallTest
+ @Test
public void testRouteInterfaces() {
LinkAddress prefix = new LinkAddress(
NetworkUtils.numericToInetAddress("2001:db8::"), 32);
@@ -317,7 +310,7 @@
assertEquals(3, lp.compareAllRoutes(lp2).removed.size());
}
- @SmallTest
+ @Test
public void testStackedInterfaces() {
LinkProperties rmnet0 = new LinkProperties();
rmnet0.setInterfaceName("rmnet0");
@@ -373,7 +366,7 @@
return lp.getLinkAddresses().iterator().next();
}
- @SmallTest
+ @Test
public void testAddressMethods() {
LinkProperties lp = new LinkProperties();
@@ -460,7 +453,7 @@
assertEquals(0, lp.getLinkAddresses().size());
}
- @SmallTest
+ @Test
public void testSetLinkAddresses() {
LinkProperties lp = new LinkProperties();
lp.addLinkAddress(LINKADDRV4);
@@ -475,7 +468,7 @@
assertTrue(lp.equals(lp));
}
- @SmallTest
+ @Test
public void testIsProvisioned() {
LinkProperties lp4 = new LinkProperties();
assertFalse("v4only:empty", lp4.isProvisioned());
@@ -529,7 +522,7 @@
assertFalse("mixed:addr6+route6+dns4", mixed.isProvisioned());
}
- @SmallTest
+ @Test
public void testCompareProvisioning() {
LinkProperties v4lp = new LinkProperties();
v4lp.addLinkAddress(LINKADDRV4);
@@ -589,8 +582,7 @@
LinkProperties.compareProvisioning(v6lp, v6lp2));
}
- @SmallTest
- @Suppress // Failing.
+ @Test
public void testIsReachable() {
final LinkProperties v4lp = new LinkProperties();
assertFalse(v4lp.isReachable(DNS1));
@@ -687,7 +679,7 @@
assertTrue(v6lp.isReachable(DNS1));
}
- @SmallTest
+ @Test
public void testLinkPropertiesEnsureDirectlyConnectedRoutes() {
// IPv4 case: no route added initially
LinkProperties rmnet0 = new LinkProperties();
@@ -750,25 +742,25 @@
}
- @SmallTest
+ @Test
public void testCompareResult() {
// Either adding or removing items
- testCompareResult(Arrays.asList(1, 2, 3, 4), Arrays.asList(1),
+ compareResult(Arrays.asList(1, 2, 3, 4), Arrays.asList(1),
Arrays.asList(2, 3, 4), new ArrayList<>());
- testCompareResult(Arrays.asList(1, 2), Arrays.asList(3, 2, 1, 4),
+ 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
- testCompareResult(Arrays.asList(1, 2, 3, 4), Arrays.asList(2, 3, 4, 5),
+ compareResult(Arrays.asList(1, 2, 3, 4), Arrays.asList(2, 3, 4, 5),
Arrays.asList(1), Arrays.asList(5));
- testCompareResult(Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6),
+ compareResult(Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6),
Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6));
// null cases
- testCompareResult(Arrays.asList(1, 2, 3), null, Arrays.asList(1, 2, 3), new ArrayList<>());
- testCompareResult(null, Arrays.asList(3, 2, 1), new ArrayList<>(), Arrays.asList(1, 2, 3));
- testCompareResult(null, null, new ArrayList<>(), new ArrayList<>());
+ 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) {
@@ -780,7 +772,7 @@
assertEquals(expectedSet, actualSet);
}
- private <T> void testCompareResult(List<T> oldItems, List<T> newItems, List<T> expectRemoved,
+ 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));
diff --git a/core/tests/coretests/src/android/net/NetworkTest.java b/tests/net/java/android/net/NetworkTest.java
similarity index 89%
rename from core/tests/coretests/src/android/net/NetworkTest.java
rename to tests/net/java/android/net/NetworkTest.java
index 74b6d98..bacf986 100644
--- a/core/tests/coretests/src/android/net/NetworkTest.java
+++ b/tests/net/java/android/net/NetworkTest.java
@@ -16,13 +16,17 @@
package android.net;
-import static android.test.MoreAsserts.assertNotEqual;
+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 android.test.suitebuilder.annotation.SmallTest;
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
import java.io.File;
import java.io.FileDescriptor;
@@ -32,13 +36,17 @@
import java.net.InetAddress;
import java.net.Inet6Address;
import java.net.SocketException;
+import java.util.Objects;
-import junit.framework.TestCase;
+import org.junit.Test;
+import org.junit.runner.RunWith;
-public class NetworkTest extends TestCase {
+@RunWith(AndroidJUnit4.class)
+@SmallTest
+public class NetworkTest {
final Network mNetwork = new Network(99);
- @SmallTest
+ @Test
public void testBindSocketOfInvalidFdThrows() throws Exception {
final FileDescriptor fd = new FileDescriptor();
@@ -50,7 +58,7 @@
} catch (SocketException expected) {}
}
- @SmallTest
+ @Test
public void testBindSocketOfNonSocketFdThrows() throws Exception {
final File devNull = new File("/dev/null");
assertTrue(devNull.canRead());
@@ -65,7 +73,7 @@
} catch (SocketException expected) {}
}
- @SmallTest
+ @Test
public void testBindSocketOfConnectedDatagramSocketThrows() throws Exception {
final DatagramSocket mDgramSocket = new DatagramSocket(0, (InetAddress) Inet6Address.ANY);
mDgramSocket.connect((InetAddress) Inet6Address.LOOPBACK, 53);
@@ -77,7 +85,7 @@
} catch (SocketException expected) {}
}
- @SmallTest
+ @Test
public void testBindSocketOfLocalSocketThrows() throws Exception {
final LocalSocket mLocalClient = new LocalSocket();
mLocalClient.bind(new LocalSocketAddress("testClient"));
@@ -98,7 +106,7 @@
} catch (SocketException expected) {}
}
- @SmallTest
+ @Test
public void testZeroIsObviousForDebugging() {
Network zero = new Network(0);
assertEquals(0, zero.hashCode());
@@ -106,7 +114,7 @@
assertEquals("0", zero.toString());
}
- @SmallTest
+ @Test
public void testGetNetworkHandle() {
Network one = new Network(1);
Network two = new Network(2);
@@ -143,4 +151,8 @@
assertEquals(8606370526L, two.getNetworkHandle());
assertEquals(12901337822L, three.getNetworkHandle());
}
+
+ private static <T> void assertNotEqual(T t1, T t2) {
+ assertFalse(Objects.equals(t1, t2));
+ }
}
diff --git a/core/tests/coretests/src/android/net/StaticIpConfigurationTest.java b/tests/net/java/android/net/StaticIpConfigurationTest.java
similarity index 89%
rename from core/tests/coretests/src/android/net/StaticIpConfigurationTest.java
rename to tests/net/java/android/net/StaticIpConfigurationTest.java
index 59f780f..5bb5734 100644
--- a/core/tests/coretests/src/android/net/StaticIpConfigurationTest.java
+++ b/tests/net/java/android/net/StaticIpConfigurationTest.java
@@ -16,22 +16,26 @@
package android.net;
-import android.net.IpPrefix;
-import android.net.LinkAddress;
-import android.net.RouteInfo;
-import android.net.StaticIpConfiguration;
+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 android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
import java.net.InetAddress;
import java.util.HashSet;
+import java.util.Objects;
-import junit.framework.TestCase;
-import android.test.suitebuilder.annotation.SmallTest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
-import static org.junit.Assert.*;
-
-
-public class StaticIpConfigurationTest extends TestCase {
+@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);
@@ -53,16 +57,8 @@
assertEquals(0, s.dnsServers.size());
}
- private boolean isEqual(StaticIpConfiguration s1, StaticIpConfiguration s2) {
- return s1.equals(s2);
- }
-
- private void assertEquals(StaticIpConfiguration s1, StaticIpConfiguration s2) {
- assertTrue(isEqual(s1, s2));
- }
-
- private void assertNotEquals(StaticIpConfiguration s1, StaticIpConfiguration s2) {
- assertFalse(isEqual(s1, s2));
+ private static <T> void assertNotEquals(T t1, T t2) {
+ assertFalse(Objects.equals(t1, t2));
}
private StaticIpConfiguration makeTestObject() {
@@ -76,13 +72,13 @@
return s;
}
- @SmallTest
+ @Test
public void testConstructor() {
StaticIpConfiguration s = new StaticIpConfiguration();
checkEmpty(s);
}
- @SmallTest
+ @Test
public void testCopyAndClear() {
StaticIpConfiguration empty = new StaticIpConfiguration((StaticIpConfiguration) null);
checkEmpty(empty);
@@ -94,7 +90,7 @@
assertEquals(empty, s2);
}
- @SmallTest
+ @Test
public void testHashCodeAndEquals() {
HashSet<Integer> hashCodes = new HashSet();
hashCodes.add(0);
@@ -143,7 +139,7 @@
assertNotEquals(s, s2);
}
- @SmallTest
+ @Test
public void testToLinkProperties() {
LinkProperties expected = new LinkProperties();
expected.setInterfaceName(IFACE);
@@ -215,11 +211,10 @@
return s2;
}
- @SmallTest
+ @Test
public void testParceling() {
StaticIpConfiguration s = makeTestObject();
StaticIpConfiguration s2 = passThroughParcel(s);
assertEquals(s, s2);
}
}
-