Factorize custom asserts.

Also a few utilities that were in the way, and some opportunistic
cleanups.

Test: FrameworksNetTest NetworkStackTest
Change-Id: I385070e2044fd967cb18f1ffea9a86a4627b742e
This commit is contained in:
Chalard Jean
2019-05-30 17:11:14 +09:00
parent c961e03526
commit af718367c2
24 changed files with 181 additions and 395 deletions

View File

@@ -6,7 +6,6 @@ java_defaults {
static_libs: [
"FrameworksNetCommonTests",
"frameworks-base-testutils",
"frameworks-net-testutils",
"framework-protos",
"androidx.test.rules",
"mockito-target-minus-junit4",

View File

@@ -21,9 +21,9 @@ java_library {
srcs: ["java/**/*.java", "java/**/*.kt"],
static_libs: [
"androidx.test.rules",
"frameworks-net-testutils",
"junit",
"mockito-target-minus-junit4",
"net-tests-utils",
"platform-test-annotations",
],
libs: [

View File

@@ -16,16 +16,18 @@
package android.net;
import static com.android.testutils.MiscAssertsKt.assertEqualBothWays;
import static com.android.testutils.MiscAssertsKt.assertFieldCountEquals;
import static com.android.testutils.MiscAssertsKt.assertNotEqualEitherWay;
import static com.android.testutils.ParcelUtilsKt.assertParcelingIsLossless;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import android.os.Parcel;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
@@ -171,56 +173,46 @@ public class IpPrefixTest {
}
private void assertAreEqual(Object o1, Object o2) {
assertTrue(o1.equals(o2));
assertTrue(o2.equals(o1));
}
private void assertAreNotEqual(Object o1, Object o2) {
assertFalse(o1.equals(o2));
assertFalse(o2.equals(o1));
}
@Test
public void testEquals() {
IpPrefix p1, p2;
p1 = new IpPrefix("192.0.2.251/23");
p2 = new IpPrefix(new byte[]{(byte) 192, (byte) 0, (byte) 2, (byte) 251}, 23);
assertAreEqual(p1, p2);
assertEqualBothWays(p1, p2);
p1 = new IpPrefix("192.0.2.5/23");
assertAreEqual(p1, p2);
assertEqualBothWays(p1, p2);
p1 = new IpPrefix("192.0.2.5/24");
assertAreNotEqual(p1, p2);
assertNotEqualEitherWay(p1, p2);
p1 = new IpPrefix("192.0.4.5/23");
assertAreNotEqual(p1, p2);
assertNotEqualEitherWay(p1, p2);
p1 = new IpPrefix("2001:db8:dead:beef:f00::80/122");
p2 = new IpPrefix(IPV6_BYTES, 122);
assertEquals("2001:db8:dead:beef:f00::80/122", p2.toString());
assertAreEqual(p1, p2);
assertEqualBothWays(p1, p2);
p1 = new IpPrefix("2001:db8:dead:beef:f00::bf/122");
assertAreEqual(p1, p2);
assertEqualBothWays(p1, p2);
p1 = new IpPrefix("2001:db8:dead:beef:f00::8:0/123");
assertAreNotEqual(p1, p2);
assertNotEqualEitherWay(p1, p2);
p1 = new IpPrefix("2001:db8:dead:beef::/122");
assertAreNotEqual(p1, p2);
assertNotEqualEitherWay(p1, p2);
// 192.0.2.4/32 != c000:0204::/32.
byte[] ipv6bytes = new byte[16];
System.arraycopy(IPV4_BYTES, 0, ipv6bytes, 0, IPV4_BYTES.length);
p1 = new IpPrefix(ipv6bytes, 32);
assertAreEqual(p1, new IpPrefix("c000:0204::/32"));
assertEqualBothWays(p1, new IpPrefix("c000:0204::/32"));
p2 = new IpPrefix(IPV4_BYTES, 32);
assertAreNotEqual(p1, p2);
assertNotEqualEitherWay(p1, p2);
}
@Test
@@ -356,25 +348,6 @@ public class IpPrefixTest {
assertEquals(InetAddress.parseNumericAddress("192.0.2.0"), p.getAddress());
}
public IpPrefix passThroughParcel(IpPrefix p) {
Parcel parcel = Parcel.obtain();
IpPrefix p2 = null;
try {
p.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
p2 = IpPrefix.CREATOR.createFromParcel(parcel);
} finally {
parcel.recycle();
}
assertNotNull(p2);
return p2;
}
public void assertParcelingIsLossless(IpPrefix p) {
IpPrefix p2 = passThroughParcel(p);
assertEquals(p, p2);
}
@Test
public void testParceling() {
IpPrefix p;
@@ -386,5 +359,7 @@ public class IpPrefixTest {
p = new IpPrefix("192.0.2.0/25");
assertParcelingIsLossless(p);
assertTrue(p.isIPv4());
assertFieldCountEquals(2, IpPrefix.class);
}
}

View File

@@ -27,15 +27,17 @@ 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 com.android.testutils.MiscAssertsKt.assertEqualBothWays;
import static com.android.testutils.MiscAssertsKt.assertNotEqualEitherWay;
import static com.android.testutils.ParcelUtilsKt.assertParcelSane;
import static com.android.testutils.ParcelUtilsKt.assertParcelingIsLossless;
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;
@@ -217,67 +219,56 @@ public class LinkAddressTest {
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);
assertEqualBothWays(l1, l2);
assertIsSameAddressAs(l1, l2);
l2 = new LinkAddress("2001:db8::1/65");
assertLinkAddressesNotEqual(l1, l2);
assertNotEqualEitherWay(l1, l2);
assertIsNotSameAddressAs(l1, l2);
l2 = new LinkAddress("2001:db8::2/64");
assertLinkAddressesNotEqual(l1, l2);
assertNotEqualEitherWay(l1, l2);
assertIsNotSameAddressAs(l1, l2);
l1 = new LinkAddress("192.0.2.1/24");
l2 = new LinkAddress("192.0.2.1/24");
assertLinkAddressesEqual(l1, l2);
assertEqualBothWays(l1, l2);
assertIsSameAddressAs(l1, l2);
l2 = new LinkAddress("192.0.2.1/23");
assertLinkAddressesNotEqual(l1, l2);
assertNotEqualEitherWay(l1, l2);
assertIsNotSameAddressAs(l1, l2);
l2 = new LinkAddress("192.0.2.2/24");
assertLinkAddressesNotEqual(l1, l2);
assertNotEqualEitherWay(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);
assertEqualBothWays(l1, l2);
assertIsSameAddressAs(l1, l2);
l2 = new LinkAddress(V6_ADDRESS, 64, IFA_F_DEPRECATED, RT_SCOPE_UNIVERSE);
assertLinkAddressesNotEqual(l1, l2);
assertNotEqualEitherWay(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);
assertEqualBothWays(l1, l2);
assertIsSameAddressAs(l1, l2);
l2 = new LinkAddress(V4_ADDRESS, 24, 0, RT_SCOPE_HOST);
assertLinkAddressesNotEqual(l1, l2);
assertNotEqualEitherWay(l1, l2);
assertIsSameAddressAs(l1, l2);
// Addresses with the same start or end bytes aren't equal between families.
@@ -291,10 +282,10 @@ public class LinkAddressTest {
assertTrue(Arrays.equals(ipv4Bytes, l2FirstIPv6Bytes));
assertTrue(Arrays.equals(ipv4Bytes, l3LastIPv6Bytes));
assertLinkAddressesNotEqual(l1, l2);
assertNotEqualEitherWay(l1, l2);
assertIsNotSameAddressAs(l1, l2);
assertLinkAddressesNotEqual(l1, l3);
assertNotEqualEitherWay(l1, l3);
assertIsNotSameAddressAs(l1, l3);
// Because we use InetAddress, an IPv4 address is equal to its IPv4-mapped address.
@@ -302,7 +293,7 @@ public class LinkAddressTest {
String addressString = V4 + "/24";
l1 = new LinkAddress(addressString);
l2 = new LinkAddress("::ffff:" + addressString);
assertLinkAddressesEqual(l1, l2);
assertEqualBothWays(l1, l2);
assertIsSameAddressAs(l1, l2);
}
@@ -319,25 +310,6 @@ public class LinkAddressTest {
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;
@@ -346,7 +318,7 @@ public class LinkAddressTest {
assertParcelingIsLossless(l);
l = new LinkAddress(V4 + "/28", IFA_F_PERMANENT, RT_SCOPE_LINK);
assertParcelingIsLossless(l);
assertParcelSane(l, 4);
}
private void assertGlobalPreferred(LinkAddress l, String msg) {

View File

@@ -16,6 +16,8 @@
package android.net;
import static com.android.testutils.ParcelUtilsKt.assertParcelingIsLossless;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
@@ -31,8 +33,6 @@ 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;
@@ -942,13 +942,13 @@ public class LinkPropertiesTest {
source.setNat64Prefix(new IpPrefix("2001:db8:1:2:64:64::/96"));
TestUtils.assertParcelingIsLossless(source);
assertParcelingIsLossless(source);
}
@Test
public void testParcelUninitialized() throws Exception {
LinkProperties empty = new LinkProperties();
TestUtils.assertParcelingIsLossless(empty);
assertParcelingIsLossless(empty);
}
@Test

View File

@@ -38,6 +38,9 @@ import static android.net.NetworkCapabilities.TRANSPORT_VPN;
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
import static android.net.NetworkCapabilities.UNRESTRICTED_CAPABILITIES;
import static com.android.testutils.ParcelUtilsKt.assertParcelSane;
import static com.android.testutils.ParcelUtilsKt.assertParcelingIsLossless;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -45,7 +48,6 @@ 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;
@@ -267,9 +269,9 @@ public class NetworkCapabilitiesTest {
.setUids(uids)
.addCapability(NET_CAPABILITY_EIMS)
.addCapability(NET_CAPABILITY_NOT_METERED);
assertEqualsThroughMarshalling(netCap);
assertParcelingIsLossless(netCap);
netCap.setSSID(TEST_SSID);
assertEqualsThroughMarshalling(netCap);
assertParcelSane(netCap, 11);
}
@Test
@@ -542,18 +544,6 @@ public class NetworkCapabilitiesTest {
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();

View File

@@ -18,13 +18,10 @@ package android.net;
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.net.LocalServerSocket;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.net.Network;
import android.platform.test.annotations.AppModeFull;
import androidx.test.filters.SmallTest;
@@ -40,7 +37,6 @@ import java.net.DatagramSocket;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Objects;
@RunWith(AndroidJUnit4.class)
@SmallTest
@@ -123,29 +119,29 @@ public class NetworkTest {
Network three = new Network(3);
// None of the hashcodes are zero.
assertNotEqual(0, one.hashCode());
assertNotEqual(0, two.hashCode());
assertNotEqual(0, three.hashCode());
assertNotEquals(0, one.hashCode());
assertNotEquals(0, two.hashCode());
assertNotEquals(0, three.hashCode());
// All the hashcodes are distinct.
assertNotEqual(one.hashCode(), two.hashCode());
assertNotEqual(one.hashCode(), three.hashCode());
assertNotEqual(two.hashCode(), three.hashCode());
assertNotEquals(one.hashCode(), two.hashCode());
assertNotEquals(one.hashCode(), three.hashCode());
assertNotEquals(two.hashCode(), three.hashCode());
// None of the handles are zero.
assertNotEqual(0, one.getNetworkHandle());
assertNotEqual(0, two.getNetworkHandle());
assertNotEqual(0, three.getNetworkHandle());
assertNotEquals(0, one.getNetworkHandle());
assertNotEquals(0, two.getNetworkHandle());
assertNotEquals(0, three.getNetworkHandle());
// All the handles are distinct.
assertNotEqual(one.getNetworkHandle(), two.getNetworkHandle());
assertNotEqual(one.getNetworkHandle(), three.getNetworkHandle());
assertNotEqual(two.getNetworkHandle(), three.getNetworkHandle());
assertNotEquals(one.getNetworkHandle(), two.getNetworkHandle());
assertNotEquals(one.getNetworkHandle(), three.getNetworkHandle());
assertNotEquals(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());
assertNotEquals(one.hashCode(), one.getNetworkHandle());
assertNotEquals(two.hashCode(), two.getNetworkHandle());
assertNotEquals(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.
@@ -154,15 +150,11 @@ public class NetworkTest {
assertEquals(16290598925L, three.getNetworkHandle());
}
private static <T> void assertNotEqual(T t1, T t2) {
assertFalse(Objects.equals(t1, t2));
}
@Test
public void testGetPrivateDnsBypassingCopy() {
final Network copy = mNetwork.getPrivateDnsBypassingCopy();
assertEquals(mNetwork.netId, copy.netId);
assertNotEqual(copy.netId, copy.getNetIdForResolv());
assertNotEqual(mNetwork.getNetIdForResolv(), copy.getNetIdForResolv());
assertNotEquals(copy.netId, copy.getNetIdForResolv());
assertNotEquals(mNetwork.getNetIdForResolv(), copy.getNetIdForResolv());
}
}

View File

@@ -18,7 +18,11 @@ package android.net;
import static android.net.RouteInfo.RTN_UNREACHABLE;
import android.os.Parcel;
import static com.android.testutils.MiscAssertsKt.assertEqualBothWays;
import static com.android.testutils.MiscAssertsKt.assertNotEqualEitherWay;
import static com.android.testutils.ParcelUtilsKt.assertParcelSane;
import static com.android.testutils.ParcelUtilsKt.assertParcelingIsLossless;
import android.test.suitebuilder.annotation.SmallTest;
import junit.framework.TestCase;
@@ -109,47 +113,37 @@ public class RouteInfoTest extends TestCase {
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);
assertEqualBothWays(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);
assertNotEqualEitherWay(r1, r3);
assertNotEqualEitherWay(r1, r4);
assertNotEqualEitherWay(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);
assertEqualBothWays(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);
assertNotEqualEitherWay(r1, r3);
assertNotEqualEitherWay(r1, r4);
assertNotEqualEitherWay(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);
assertEqualBothWays(r1, r2);
assertNotEqualEitherWay(r1, r3);
}
public void testHostAndDefaultRoutes() {
@@ -257,25 +251,6 @@ public class RouteInfoTest extends TestCase {
// 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;
@@ -283,6 +258,6 @@ public class RouteInfoTest extends TestCase {
assertParcelingIsLossless(r);
r = new RouteInfo(Prefix("192.0.2.0/24"), null, "wlan0");
assertParcelingIsLossless(r);
assertParcelSane(r, 6);
}
}

View File

@@ -18,6 +18,7 @@ package android.net;
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.assertNull;
import static org.junit.Assert.assertTrue;
@@ -34,7 +35,6 @@ import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
@RunWith(AndroidJUnit4.class)
@SmallTest
@@ -61,10 +61,6 @@ public class StaticIpConfigurationTest {
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;

View File

@@ -16,6 +16,8 @@
package android.net.apf;
import static com.android.testutils.ParcelUtilsKt.assertParcelSane;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
@@ -24,9 +26,6 @@ import static org.junit.Assert.assertTrue;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.internal.util.ParcelableTestUtil;
import com.android.internal.util.TestUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -40,9 +39,7 @@ public class ApfCapabilitiesTest {
assertEquals(456, caps.maximumApfProgramSize);
assertEquals(789, caps.apfPacketFormat);
ParcelableTestUtil.assertFieldCountEquals(3, ApfCapabilities.class);
TestUtils.assertParcelingIsLossless(caps);
assertParcelSane(caps, 3);
}
@Test

View File

@@ -16,11 +16,9 @@
package android.net.metrics;
import android.os.Parcelable
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import com.android.internal.util.ParcelableTestUtil
import com.android.internal.util.TestUtils
import com.android.testutils.assertParcelSane
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
@@ -30,11 +28,6 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
@SmallTest
class ApfProgramEventTest {
private fun <T: Parcelable> testParcel(obj: T, fieldCount: Int) {
ParcelableTestUtil.assertFieldCountEquals(fieldCount, obj::class.java)
TestUtils.assertParcelingIsLossless(obj)
}
private infix fun Int.hasFlag(flag: Int) = (this and (1 shl flag)) != 0
@Test
@@ -55,7 +48,7 @@ class ApfProgramEventTest {
assertEquals(5, apfProgramEvent.programLength)
assertEquals(ApfProgramEvent.flagsFor(true, true), apfProgramEvent.flags)
testParcel(apfProgramEvent, 6)
assertParcelSane(apfProgramEvent, 6)
}
@Test

View File

@@ -16,11 +16,9 @@
package android.net.metrics
import android.os.Parcelable
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import com.android.internal.util.ParcelableTestUtil
import com.android.internal.util.TestUtils
import com.android.testutils.assertParcelSane
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
@@ -28,11 +26,6 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
@SmallTest
class ApfStatsTest {
private fun <T: Parcelable> testParcel(obj: T, fieldCount: Int) {
ParcelableTestUtil.assertFieldCountEquals(fieldCount, obj::class.java)
TestUtils.assertParcelingIsLossless(obj)
}
@Test
fun testBuilderAndParcel() {
val apfStats = ApfStats.Builder()
@@ -59,6 +52,6 @@ class ApfStatsTest {
assertEquals(8, apfStats.programUpdatesAllowingMulticast)
assertEquals(9, apfStats.maxProgramSize)
testParcel(apfStats, 10)
assertParcelSane(apfStats, 10)
}
}

View File

@@ -1,10 +1,10 @@
package android.net.metrics
import android.net.metrics.DhcpErrorEvent.errorCodeWithOption
import android.net.metrics.DhcpErrorEvent.DHCP_INVALID_OPTION_LENGTH
import android.net.metrics.DhcpErrorEvent.errorCodeWithOption
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import com.android.internal.util.TestUtils.parcelingRoundTrip
import com.android.testutils.parcelingRoundTrip
import java.lang.reflect.Modifier
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull

View File

@@ -16,11 +16,9 @@
package android.net.metrics
import android.os.Parcelable
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import com.android.internal.util.ParcelableTestUtil
import com.android.internal.util.TestUtils
import com.android.testutils.assertParcelSane
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
@@ -28,18 +26,13 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
@SmallTest
class IpReachabilityEventTest {
private fun <T: Parcelable> testParcel(obj: T, fieldCount: Int) {
ParcelableTestUtil.assertFieldCountEquals(fieldCount, obj::class.java)
TestUtils.assertParcelingIsLossless(obj)
}
@Test
fun testConstructorAndParcel() {
(IpReachabilityEvent.PROBE..IpReachabilityEvent.PROVISIONING_LOST_ORGANIC).forEach {
val ipReachabilityEvent = IpReachabilityEvent(it)
assertEquals(it, ipReachabilityEvent.eventType)
testParcel(ipReachabilityEvent, 1)
assertParcelSane(ipReachabilityEvent, 1)
}
}
}

View File

@@ -16,11 +16,9 @@
package android.net.metrics
import android.os.Parcelable
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import com.android.internal.util.ParcelableTestUtil
import com.android.internal.util.TestUtils
import com.android.testutils.assertParcelSane
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
@@ -30,11 +28,6 @@ private const val NO_LIFETIME: Long = -1L
@RunWith(AndroidJUnit4::class)
@SmallTest
class RaEventTest {
private fun <T: Parcelable> testParcel(obj: T, fieldCount: Int) {
ParcelableTestUtil.assertFieldCountEquals(fieldCount, obj::class.java)
TestUtils.assertParcelingIsLossless(obj)
}
@Test
fun testConstructorAndParcel() {
var raEvent = RaEvent.Builder().build()
@@ -74,6 +67,6 @@ class RaEventTest {
assertEquals(5, raEvent.rdnssLifetime)
assertEquals(6, raEvent.dnsslLifetime)
testParcel(raEvent, 6)
assertParcelSane(raEvent, 6)
}
}

View File

@@ -16,11 +16,9 @@
package android.net.metrics
import android.os.Parcelable
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import com.android.internal.util.ParcelableTestUtil
import com.android.internal.util.TestUtils
import com.android.testutils.assertParcelSane
import java.lang.reflect.Modifier
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
@@ -33,11 +31,6 @@ private const val REVALIDATION: Int = 2 shl 8
@RunWith(AndroidJUnit4::class)
@SmallTest
class ValidationProbeEventTest {
private fun <T: Parcelable> testParcel(obj: T, fieldCount: Int) {
ParcelableTestUtil.assertFieldCountEquals(fieldCount, obj::class.java)
TestUtils.assertParcelingIsLossless(obj)
}
private infix fun Int.hasType(type: Int) = (type and this) == type
@Test
@@ -58,7 +51,7 @@ class ValidationProbeEventTest {
assertTrue(validationProbeEvent.probeType hasType FIRST_VALIDATION)
assertEquals(ValidationProbeEvent.DNS_SUCCESS, validationProbeEvent.returnCode)
testParcel(validationProbeEvent, 3)
assertParcelSane(validationProbeEvent, 3)
}
@Test

View File

@@ -202,8 +202,7 @@ public class NetworkStatsManagerTest {
assertFalse(stats.hasNextBucket());
}
private void assertBucketMatches(Entry expected,
NetworkStats.Bucket actual) {
private void assertBucketMatches(Entry expected, NetworkStats.Bucket actual) {
assertEquals(expected.uid, actual.getUid());
assertEquals(expected.rxBytes, actual.getRxBytes());
assertEquals(expected.rxPackets, actual.getRxPackets());

View File

@@ -16,12 +16,12 @@
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 com.android.testutils.ParcelUtilsKt.assertParcelSane;
import static com.android.testutils.ParcelUtilsKt.assertParcelingIsLossless;
import android.os.Parcel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import androidx.test.filters.SmallTest;
@@ -89,23 +89,15 @@ public class IpSecConfigTest {
IpSecConfig original = getSampleConfig();
IpSecConfig copy = new IpSecConfig(original);
assertTrue(IpSecConfig.equals(original, copy));
assertFalse(original == copy);
assertEquals(original, copy);
assertNotSame(original, copy);
}
@Test
public void testParcelUnparcel() throws Exception {
public void testParcelUnparcel() {
assertParcelingIsLossless(new IpSecConfig());
IpSecConfig c = getSampleConfig();
assertParcelingIsLossless(c);
}
private void assertParcelingIsLossless(IpSecConfig ci) throws Exception {
Parcel p = Parcel.obtain();
ci.writeToParcel(p, 0);
p.setDataPosition(0);
IpSecConfig co = IpSecConfig.CREATOR.createFromParcel(p);
assertTrue(IpSecConfig.equals(co, ci));
assertParcelSane(c, 15);
}
}

View File

@@ -16,8 +16,8 @@
package android.net;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import androidx.test.filters.SmallTest;
@@ -43,7 +43,7 @@ public class IpSecTransformTest {
config.setSpiResourceId(1985);
IpSecTransform postModification = new IpSecTransform(null, config);
assertFalse(IpSecTransform.equals(preModification, postModification));
assertNotEquals(preModification, postModification);
}
@Test
@@ -57,6 +57,6 @@ public class IpSecTransformTest {
IpSecTransform config1 = new IpSecTransform(null, config);
IpSecTransform config2 = new IpSecTransform(null, config);
assertTrue(IpSecTransform.equals(config1, config2));
assertEquals(config1, config2);
}
}

View File

@@ -16,14 +16,14 @@
package android.net;
import static com.android.testutils.ParcelUtilsKt.assertParcelingIsLossless;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import android.net.SocketKeepalive.InvalidPacketException;
import com.android.internal.util.TestUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -79,7 +79,7 @@ public final class TcpKeepalivePacketDataTest {
assertEquals(testInfo.tos, resultData.ipTos);
assertEquals(testInfo.ttl, resultData.ipTtl);
TestUtils.assertParcelingIsLossless(resultData);
assertParcelingIsLossless(resultData);
final byte[] packet = resultData.getPacket();
// IP version and IHL

View File

@@ -16,6 +16,7 @@
package com.android.internal.util;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
@@ -25,9 +26,6 @@ import androidx.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Arrays;
import java.util.Objects;
@SmallTest
@RunWith(AndroidJUnit4.class)
public class RingBufferTest {
@@ -36,7 +34,7 @@ public class RingBufferTest {
public void testEmptyRingBuffer() {
RingBuffer<String> buffer = new RingBuffer<>(String.class, 100);
assertArraysEqual(new String[0], buffer.toArray());
assertArrayEquals(new String[0], buffer.toArray());
}
@Test
@@ -65,7 +63,7 @@ public class RingBufferTest {
buffer.append("e");
String[] expected = {"a", "b", "c", "d", "e"};
assertArraysEqual(expected, buffer.toArray());
assertArrayEquals(expected, buffer.toArray());
}
@Test
@@ -73,19 +71,19 @@ public class RingBufferTest {
RingBuffer<String> buffer = new RingBuffer<>(String.class, 1);
buffer.append("a");
assertArraysEqual(new String[]{"a"}, buffer.toArray());
assertArrayEquals(new String[]{"a"}, buffer.toArray());
buffer.append("b");
assertArraysEqual(new String[]{"b"}, buffer.toArray());
assertArrayEquals(new String[]{"b"}, buffer.toArray());
buffer.append("c");
assertArraysEqual(new String[]{"c"}, buffer.toArray());
assertArrayEquals(new String[]{"c"}, buffer.toArray());
buffer.append("d");
assertArraysEqual(new String[]{"d"}, buffer.toArray());
assertArrayEquals(new String[]{"d"}, buffer.toArray());
buffer.append("e");
assertArraysEqual(new String[]{"e"}, buffer.toArray());
assertArrayEquals(new String[]{"e"}, buffer.toArray());
}
@Test
@@ -100,7 +98,7 @@ public class RingBufferTest {
buffer.append("e");
String[] expected1 = {"a", "b", "c", "d", "e"};
assertArraysEqual(expected1, buffer.toArray());
assertArrayEquals(expected1, buffer.toArray());
String[] expected2 = new String[capacity];
int firstIndex = 0;
@@ -111,22 +109,22 @@ public class RingBufferTest {
buffer.append("x");
expected2[i] = "x";
}
assertArraysEqual(expected2, buffer.toArray());
assertArrayEquals(expected2, buffer.toArray());
buffer.append("x");
expected2[firstIndex] = "x";
assertArraysEqual(expected2, buffer.toArray());
assertArrayEquals(expected2, buffer.toArray());
for (int i = 0; i < 10; i++) {
for (String s : expected2) {
buffer.append(s);
}
}
assertArraysEqual(expected2, buffer.toArray());
assertArrayEquals(expected2, buffer.toArray());
buffer.append("a");
expected2[lastIndex] = "a";
assertArraysEqual(expected2, buffer.toArray());
assertArrayEquals(expected2, buffer.toArray());
}
@Test
@@ -143,7 +141,7 @@ public class RingBufferTest {
expected[i] = new DummyClass1();
expected[i].x = capacity * i;
}
assertArraysEqual(expected, buffer.toArray());
assertArrayEquals(expected, buffer.toArray());
for (int i = 0; i < capacity; ++i) {
if (actual[i] != buffer.getNextSlot()) {
@@ -177,18 +175,4 @@ public class RingBufferTest {
}
private static final class DummyClass3 {}
static <T> void assertArraysEqual(T[] expected, T[] got) {
if (expected.length != got.length) {
fail(Arrays.toString(expected) + " and " + Arrays.toString(got)
+ " did not have the same length");
}
for (int i = 0; i < expected.length; i++) {
if (!Objects.equals(expected[i], got[i])) {
fail(Arrays.toString(expected) + " and " + Arrays.toString(got)
+ " were not equal");
}
}
}
}

View File

@@ -68,7 +68,15 @@ import static android.net.NetworkPolicyManager.RULE_REJECT_ALL;
import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
import static android.net.RouteInfo.RTN_UNREACHABLE;
import static org.junit.Assert.assertArrayEquals;
import static com.android.testutils.ConcurrentUtilsKt.await;
import static com.android.testutils.ConcurrentUtilsKt.durationOf;
import static com.android.testutils.HandlerUtilsKt.waitForIdleSerialExecutor;
import static com.android.testutils.MiscAssertsKt.assertContainsExactly;
import static com.android.testutils.MiscAssertsKt.assertEmpty;
import static com.android.testutils.MiscAssertsKt.assertLength;
import static com.android.testutils.MiscAssertsKt.assertRunsInAtMost;
import static com.android.testutils.MiscAssertsKt.assertThrows;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
@@ -194,6 +202,7 @@ import com.android.server.net.NetworkPinner;
import com.android.server.net.NetworkPolicyManagerInternal;
import com.android.server.net.NetworkStatsFactory;
import com.android.testutils.HandlerUtilsKt;
import com.android.testutils.ThrowingConsumer;
import org.junit.After;
import org.junit.Before;
@@ -2567,10 +2576,10 @@ public class ConnectivityServiceTest {
.build();
Class<IllegalArgumentException> expected = IllegalArgumentException.class;
assertException(() -> { mCm.requestNetwork(request1, new NetworkCallback()); }, expected);
assertException(() -> { mCm.requestNetwork(request1, pendingIntent); }, expected);
assertException(() -> { mCm.requestNetwork(request2, new NetworkCallback()); }, expected);
assertException(() -> { mCm.requestNetwork(request2, pendingIntent); }, expected);
assertThrows(expected, () -> mCm.requestNetwork(request1, new NetworkCallback()));
assertThrows(expected, () -> mCm.requestNetwork(request1, pendingIntent));
assertThrows(expected, () -> mCm.requestNetwork(request2, new NetworkCallback()));
assertThrows(expected, () -> mCm.requestNetwork(request2, pendingIntent));
}
@Test
@@ -3482,7 +3491,7 @@ public class ConnectivityServiceTest {
};
}
assertTimeLimit("Registering callbacks", REGISTER_TIME_LIMIT_MS, () -> {
assertRunsInAtMost("Registering callbacks", REGISTER_TIME_LIMIT_MS, () -> {
for (NetworkCallback cb : callbacks) {
mCm.registerNetworkCallback(request, cb);
}
@@ -3495,7 +3504,7 @@ public class ConnectivityServiceTest {
mCellNetworkAgent.connect(false);
long onAvailableDispatchingDuration = durationOf(() -> {
awaitLatch(availableLatch, 10 * CONNECT_TIME_LIMIT_MS);
await(availableLatch, 10 * CONNECT_TIME_LIMIT_MS);
});
Log.d(TAG, String.format("Dispatched %d of %d onAvailable callbacks in %dms",
NUM_REQUESTS - availableLatch.getCount(), NUM_REQUESTS,
@@ -3510,7 +3519,7 @@ public class ConnectivityServiceTest {
mWiFiNetworkAgent.connect(false);
long onLostDispatchingDuration = durationOf(() -> {
awaitLatch(losingLatch, 10 * SWITCH_TIME_LIMIT_MS);
await(losingLatch, 10 * SWITCH_TIME_LIMIT_MS);
});
Log.d(TAG, String.format("Dispatched %d of %d onLosing callbacks in %dms",
NUM_REQUESTS - losingLatch.getCount(), NUM_REQUESTS, onLostDispatchingDuration));
@@ -3518,33 +3527,13 @@ public class ConnectivityServiceTest {
NUM_REQUESTS, onLostDispatchingDuration, SWITCH_TIME_LIMIT_MS),
onLostDispatchingDuration <= SWITCH_TIME_LIMIT_MS);
assertTimeLimit("Unregistering callbacks", UNREGISTER_TIME_LIMIT_MS, () -> {
assertRunsInAtMost("Unregistering callbacks", UNREGISTER_TIME_LIMIT_MS, () -> {
for (NetworkCallback cb : callbacks) {
mCm.unregisterNetworkCallback(cb);
}
});
}
private long durationOf(Runnable fn) {
long startTime = SystemClock.elapsedRealtime();
fn.run();
return SystemClock.elapsedRealtime() - startTime;
}
private void assertTimeLimit(String descr, long timeLimit, Runnable fn) {
long timeTaken = durationOf(fn);
String msg = String.format("%s: took %dms, limit was %dms", descr, timeTaken, timeLimit);
Log.d(TAG, msg);
assertTrue(msg, timeTaken <= timeLimit);
}
private boolean awaitLatch(CountDownLatch l, long timeoutMs) {
try {
return l.await(timeoutMs, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {}
return false;
}
@Test
public void testMobileDataAlwaysOn() throws Exception {
final TestNetworkCallback cellNetworkCallback = new TestNetworkCallback();
@@ -4105,7 +4094,7 @@ public class ConnectivityServiceTest {
}
public void assertNoCallback() {
HandlerUtilsKt.waitForIdleSerialExecutor(mExecutor, TIMEOUT_MS);
waitForIdleSerialExecutor(mExecutor, TIMEOUT_MS);
CallbackValue cv = mCallbacks.peek();
assertNull("Unexpected callback: " + cv, cv);
}
@@ -4244,11 +4233,6 @@ public class ConnectivityServiceTest {
callback3.expectStopped();
}
@FunctionalInterface
private interface ThrowingConsumer<T> {
void accept(T t) throws Exception;
}
// Helper method to prepare the executor and run test
private void runTestWithSerialExecutors(ThrowingConsumer<Executor> functor) throws Exception {
final ExecutorService executorSingleThread = Executors.newSingleThreadExecutor();
@@ -4823,17 +4807,17 @@ public class ConnectivityServiceTest {
assertNull(mCm.getLinkProperties(TYPE_NONE));
assertFalse(mCm.isNetworkSupported(TYPE_NONE));
assertException(() -> { mCm.networkCapabilitiesForType(TYPE_NONE); },
IllegalArgumentException.class);
assertThrows(IllegalArgumentException.class,
() -> { mCm.networkCapabilitiesForType(TYPE_NONE); });
Class<UnsupportedOperationException> unsupported = UnsupportedOperationException.class;
assertException(() -> { mCm.startUsingNetworkFeature(TYPE_WIFI, ""); }, unsupported);
assertException(() -> { mCm.stopUsingNetworkFeature(TYPE_WIFI, ""); }, unsupported);
assertThrows(unsupported, () -> { mCm.startUsingNetworkFeature(TYPE_WIFI, ""); });
assertThrows(unsupported, () -> { mCm.stopUsingNetworkFeature(TYPE_WIFI, ""); });
// TODO: let test context have configuration application target sdk version
// and test that pre-M requesting for TYPE_NONE sends back APN_REQUEST_FAILED
assertException(() -> { mCm.startUsingNetworkFeature(TYPE_NONE, ""); }, unsupported);
assertException(() -> { mCm.stopUsingNetworkFeature(TYPE_NONE, ""); }, unsupported);
assertException(() -> { mCm.requestRouteToHostAddress(TYPE_NONE, null); }, unsupported);
assertThrows(unsupported, () -> { mCm.startUsingNetworkFeature(TYPE_NONE, ""); });
assertThrows(unsupported, () -> { mCm.stopUsingNetworkFeature(TYPE_NONE, ""); });
assertThrows(unsupported, () -> { mCm.requestRouteToHostAddress(TYPE_NONE, null); });
}
@Test
@@ -5095,11 +5079,11 @@ public class ConnectivityServiceTest {
ResolverParamsParcel resolvrParams = mResolverParamsParcelCaptor.getValue();
assertEquals(2, resolvrParams.tlsServers.length);
assertTrue(ArrayUtils.containsAll(resolvrParams.tlsServers,
new String[]{"2001:db8::1", "192.0.2.1"}));
new String[] { "2001:db8::1", "192.0.2.1" }));
// Opportunistic mode.
assertEquals(2, resolvrParams.tlsServers.length);
assertTrue(ArrayUtils.containsAll(resolvrParams.tlsServers,
new String[]{"2001:db8::1", "192.0.2.1"}));
new String[] { "2001:db8::1", "192.0.2.1" }));
reset(mMockDnsResolver);
cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
cellNetworkCallback.expectCallback(CallbackState.NETWORK_CAPABILITIES,
@@ -5117,7 +5101,7 @@ public class ConnectivityServiceTest {
resolvrParams = mResolverParamsParcelCaptor.getValue();
assertEquals(2, resolvrParams.servers.length);
assertTrue(ArrayUtils.containsAll(resolvrParams.servers,
new String[]{"2001:db8::1", "192.0.2.1"}));
new String[] { "2001:db8::1", "192.0.2.1" }));
reset(mMockDnsResolver);
cellNetworkCallback.assertNoCallback();
@@ -5127,10 +5111,10 @@ public class ConnectivityServiceTest {
resolvrParams = mResolverParamsParcelCaptor.getValue();
assertEquals(2, resolvrParams.servers.length);
assertTrue(ArrayUtils.containsAll(resolvrParams.servers,
new String[]{"2001:db8::1", "192.0.2.1"}));
new String[] { "2001:db8::1", "192.0.2.1" }));
assertEquals(2, resolvrParams.tlsServers.length);
assertTrue(ArrayUtils.containsAll(resolvrParams.tlsServers,
new String[]{"2001:db8::1", "192.0.2.1"}));
new String[] { "2001:db8::1", "192.0.2.1" }));
reset(mMockDnsResolver);
cellNetworkCallback.assertNoCallback();
@@ -5265,29 +5249,6 @@ public class ConnectivityServiceTest {
assertTrue(lp.getDnsServers().containsAll(dnsServers));
}
private static <T> void assertEmpty(T[] ts) {
int length = ts.length;
assertEquals("expected empty array, but length was " + length, 0, length);
}
private static <T> void assertLength(int expected, T[] got) {
int length = got.length;
assertEquals(String.format("expected array of length %s, but length was %s for %s",
expected, length, Arrays.toString(got)), expected, length);
}
private static <T> void assertException(Runnable block, Class<T> expected) {
try {
block.run();
fail("Expected exception of type " + expected);
} catch (Exception got) {
if (!got.getClass().equals(expected)) {
fail("Expected exception of type " + expected + " but got " + got);
}
return;
}
}
@Test
public void testVpnNetworkActive() {
final int uid = Process.myUid();
@@ -6484,14 +6445,6 @@ public class ConnectivityServiceTest {
return vpnNetworkAgent;
}
private void assertContainsExactly(int[] actual, int... expected) {
int[] sortedActual = Arrays.copyOf(actual, actual.length);
int[] sortedExpected = Arrays.copyOf(expected, expected.length);
Arrays.sort(sortedActual);
Arrays.sort(sortedExpected);
assertArrayEquals(sortedExpected, sortedActual);
}
private static PackageInfo buildPackageInfo(boolean hasSystemPermission, int uid) {
final PackageInfo packageInfo = new PackageInfo();
packageInfo.requestedPermissions = new String[0];

View File

@@ -19,8 +19,9 @@ package com.android.server.connectivity;
import static android.net.metrics.INetdEventListener.EVENT_GETADDRINFO;
import static android.net.metrics.INetdEventListener.EVENT_GETHOSTBYNAME;
import static com.android.testutils.MiscAssertsKt.assertStringContains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -111,15 +112,15 @@ public class NetdEventListenerServiceTest {
String[] events2 = remove(listNetdEvent(), baseline);
int expectedLength2 = uids.length + 1; // +1 for the WakeupStats line
assertEquals(expectedLength2, events2.length);
assertContains(events2[0], "WakeupStats");
assertContains(events2[0], "wlan0");
assertContains(events2[0], "0x800");
assertContains(events2[0], "0x86dd");
assertStringContains(events2[0], "WakeupStats");
assertStringContains(events2[0], "wlan0");
assertStringContains(events2[0], "0x800");
assertStringContains(events2[0], "0x86dd");
for (int i = 0; i < uids.length; i++) {
String got = events2[i+1];
assertContains(got, "WakeupEvent");
assertContains(got, "wlan0");
assertContains(got, "uid: " + uids[i]);
assertStringContains(got, "WakeupEvent");
assertStringContains(got, "wlan0");
assertStringContains(got, "uid: " + uids[i]);
}
int uid = 20000;
@@ -131,13 +132,13 @@ public class NetdEventListenerServiceTest {
String[] events3 = remove(listNetdEvent(), baseline);
int expectedLength3 = BUFFER_LENGTH + 1; // +1 for the WakeupStats line
assertEquals(expectedLength3, events3.length);
assertContains(events2[0], "WakeupStats");
assertContains(events2[0], "wlan0");
assertStringContains(events2[0], "WakeupStats");
assertStringContains(events2[0], "wlan0");
for (int i = 1; i < expectedLength3; i++) {
String got = events3[i];
assertContains(got, "WakeupEvent");
assertContains(got, "wlan0");
assertContains(got, "uid: " + uid);
assertStringContains(got, "WakeupEvent");
assertStringContains(got, "wlan0");
assertStringContains(got, "uid: " + uid);
}
uid = 45678;
@@ -145,9 +146,9 @@ public class NetdEventListenerServiceTest {
String[] events4 = remove(listNetdEvent(), baseline);
String lastEvent = events4[events4.length - 1];
assertContains(lastEvent, "WakeupEvent");
assertContains(lastEvent, "wlan0");
assertContains(lastEvent, "uid: " + uid);
assertStringContains(lastEvent, "WakeupEvent");
assertStringContains(lastEvent, "wlan0");
assertStringContains(lastEvent, "uid: " + uid);
}
@Test
@@ -529,10 +530,6 @@ public class NetdEventListenerServiceTest {
return buffer.toString().split("\\n");
}
static void assertContains(String got, String want) {
assertTrue(got + " did not contain \"" + want + "\"", got.contains(want));
}
static <T> T[] remove(T[] array, T[] filtered) {
List<T> c = Arrays.asList(filtered);
int next = 0;

View File

@@ -29,6 +29,7 @@ import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
import static com.android.server.net.NetworkStatsCollection.multiplySafe;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
@@ -43,7 +44,6 @@ import android.os.Process;
import android.os.UserHandle;
import android.telephony.SubscriptionPlan;
import android.telephony.TelephonyManager;
import android.test.MoreAsserts;
import android.text.format.DateUtils;
import android.util.RecurrenceRule;
@@ -240,11 +240,11 @@ public class NetworkStatsCollectionTest {
60 * MINUTE_IN_MILLIS, entry);
// Verify the set of relevant UIDs for each access level.
MoreAsserts.assertEquals(new int[] { myUid },
assertArrayEquals(new int[] { myUid },
collection.getRelevantUids(NetworkStatsAccess.Level.DEFAULT));
MoreAsserts.assertEquals(new int[] { Process.SYSTEM_UID, myUid, otherUidInSameUser },
assertArrayEquals(new int[] { Process.SYSTEM_UID, myUid, otherUidInSameUser },
collection.getRelevantUids(NetworkStatsAccess.Level.USER));
MoreAsserts.assertEquals(
assertArrayEquals(
new int[] { Process.SYSTEM_UID, myUid, otherUidInSameUser, uidInDifferentUser },
collection.getRelevantUids(NetworkStatsAccess.Level.DEVICE));