Merge "Import a few android.net unit tests into frameworks-net"
am: f851fef87f
Change-Id: I41655938d6d1df19130847210437f047c8b65cdf
This commit is contained in:
@@ -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.Collections;
|
||||
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 @@ public class LinkPropertiesTest extends TestCase {
|
||||
assertEquals(source.hashCode(), target.hashCode());
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testEqualsNull() {
|
||||
LinkProperties source = new LinkProperties();
|
||||
LinkProperties target = new LinkProperties();
|
||||
@@ -101,9 +108,8 @@ public class LinkPropertiesTest extends TestCase {
|
||||
assertLinkPropertiesEqual(source, target);
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
public void testEqualsSameOrder() {
|
||||
try {
|
||||
@Test
|
||||
public void testEqualsSameOrder() throws Exception {
|
||||
LinkProperties source = new LinkProperties();
|
||||
source.setInterfaceName(NAME);
|
||||
// set 2 link addresses
|
||||
@@ -191,16 +197,10 @@ public class LinkPropertiesTest extends TestCase {
|
||||
// change mtu
|
||||
target.setMtu(1440);
|
||||
assertFalse(source.equals(target));
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e.toString());
|
||||
//fail();
|
||||
}
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
public void testEqualsDifferentOrder() {
|
||||
try {
|
||||
@Test
|
||||
public void testEqualsDifferentOrder() throws Exception {
|
||||
LinkProperties source = new LinkProperties();
|
||||
source.setInterfaceName(NAME);
|
||||
// set 2 link addresses
|
||||
@@ -226,14 +226,10 @@ public class LinkPropertiesTest extends TestCase {
|
||||
target.setMtu(MTU);
|
||||
|
||||
assertLinkPropertiesEqual(source, target);
|
||||
} catch (Exception e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
public void testEqualsDuplicated() {
|
||||
try {
|
||||
@Test
|
||||
public void testEqualsDuplicated() throws Exception {
|
||||
LinkProperties source = new LinkProperties();
|
||||
// set 3 link addresses, eg, [A, A, B]
|
||||
source.addLinkAddress(LINKADDRV4);
|
||||
@@ -247,9 +243,6 @@ public class LinkPropertiesTest extends TestCase {
|
||||
target.addLinkAddress(LINKADDRV6);
|
||||
|
||||
assertLinkPropertiesEqual(source, target);
|
||||
} catch (Exception e) {
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
private void assertAllRoutesHaveInterface(String iface, LinkProperties lp) {
|
||||
@@ -258,7 +251,7 @@ public class LinkPropertiesTest extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testRouteInterfaces() {
|
||||
LinkAddress prefix = new LinkAddress(
|
||||
NetworkUtils.numericToInetAddress("2001:db8::"), 32);
|
||||
@@ -317,7 +310,7 @@ public class LinkPropertiesTest extends TestCase {
|
||||
assertEquals(3, lp.compareAllRoutes(lp2).removed.size());
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testStackedInterfaces() {
|
||||
LinkProperties rmnet0 = new LinkProperties();
|
||||
rmnet0.setInterfaceName("rmnet0");
|
||||
@@ -373,7 +366,7 @@ public class LinkPropertiesTest extends TestCase {
|
||||
return lp.getLinkAddresses().iterator().next();
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testAddressMethods() {
|
||||
LinkProperties lp = new LinkProperties();
|
||||
|
||||
@@ -460,7 +453,7 @@ public class LinkPropertiesTest extends TestCase {
|
||||
assertEquals(0, lp.getLinkAddresses().size());
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testSetLinkAddresses() {
|
||||
LinkProperties lp = new LinkProperties();
|
||||
lp.addLinkAddress(LINKADDRV4);
|
||||
@@ -475,7 +468,7 @@ public class LinkPropertiesTest extends TestCase {
|
||||
assertTrue(lp.equals(lp));
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testIsProvisioned() {
|
||||
LinkProperties lp4 = new LinkProperties();
|
||||
assertFalse("v4only:empty", lp4.isProvisioned());
|
||||
@@ -529,7 +522,7 @@ public class LinkPropertiesTest extends TestCase {
|
||||
assertFalse("mixed:addr6+route6+dns4", mixed.isProvisioned());
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testCompareProvisioning() {
|
||||
LinkProperties v4lp = new LinkProperties();
|
||||
v4lp.addLinkAddress(LINKADDRV4);
|
||||
@@ -589,8 +582,7 @@ public class LinkPropertiesTest extends TestCase {
|
||||
LinkProperties.compareProvisioning(v6lp, v6lp2));
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Suppress // Failing.
|
||||
@Test
|
||||
public void testIsReachable() {
|
||||
final LinkProperties v4lp = new LinkProperties();
|
||||
assertFalse(v4lp.isReachable(DNS1));
|
||||
@@ -687,7 +679,7 @@ public class LinkPropertiesTest extends TestCase {
|
||||
assertTrue(v6lp.isReachable(DNS1));
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testLinkPropertiesEnsureDirectlyConnectedRoutes() {
|
||||
// IPv4 case: no route added initially
|
||||
LinkProperties rmnet0 = new LinkProperties();
|
||||
@@ -750,25 +742,25 @@ public class LinkPropertiesTest extends TestCase {
|
||||
|
||||
}
|
||||
|
||||
@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 @@ public class LinkPropertiesTest extends TestCase {
|
||||
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));
|
||||
@@ -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.DatagramSocket;
|
||||
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 @@ public class NetworkTest extends TestCase {
|
||||
} catch (SocketException expected) {}
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testBindSocketOfNonSocketFdThrows() throws Exception {
|
||||
final File devNull = new File("/dev/null");
|
||||
assertTrue(devNull.canRead());
|
||||
@@ -65,7 +73,7 @@ public class NetworkTest extends TestCase {
|
||||
} 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 @@ public class NetworkTest extends TestCase {
|
||||
} catch (SocketException expected) {}
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testBindSocketOfLocalSocketThrows() throws Exception {
|
||||
final LocalSocket mLocalClient = new LocalSocket();
|
||||
mLocalClient.bind(new LocalSocketAddress("testClient"));
|
||||
@@ -98,7 +106,7 @@ public class NetworkTest extends TestCase {
|
||||
} catch (SocketException expected) {}
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testZeroIsObviousForDebugging() {
|
||||
Network zero = new Network(0);
|
||||
assertEquals(0, zero.hashCode());
|
||||
@@ -106,7 +114,7 @@ public class NetworkTest extends TestCase {
|
||||
assertEquals("0", zero.toString());
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testGetNetworkHandle() {
|
||||
Network one = new Network(1);
|
||||
Network two = new Network(2);
|
||||
@@ -143,4 +151,8 @@ public class NetworkTest extends TestCase {
|
||||
assertEquals(8606370526L, two.getNetworkHandle());
|
||||
assertEquals(12901337822L, three.getNetworkHandle());
|
||||
}
|
||||
|
||||
private static <T> void assertNotEqual(T t1, T t2) {
|
||||
assertFalse(Objects.equals(t1, t2));
|
||||
}
|
||||
}
|
||||
@@ -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 @@ public class StaticIpConfigurationTest extends TestCase {
|
||||
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 @@ public class StaticIpConfigurationTest extends TestCase {
|
||||
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 @@ public class StaticIpConfigurationTest extends TestCase {
|
||||
assertEquals(empty, s2);
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testHashCodeAndEquals() {
|
||||
HashSet<Integer> hashCodes = new HashSet();
|
||||
hashCodes.add(0);
|
||||
@@ -143,7 +139,7 @@ public class StaticIpConfigurationTest extends TestCase {
|
||||
assertNotEquals(s, s2);
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testToLinkProperties() {
|
||||
LinkProperties expected = new LinkProperties();
|
||||
expected.setInterfaceName(IFACE);
|
||||
@@ -215,11 +211,10 @@ public class StaticIpConfigurationTest extends TestCase {
|
||||
return s2;
|
||||
}
|
||||
|
||||
@SmallTest
|
||||
@Test
|
||||
public void testParceling() {
|
||||
StaticIpConfiguration s = makeTestObject();
|
||||
StaticIpConfiguration s2 = passThroughParcel(s);
|
||||
assertEquals(s, s2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user