Merge "Add CTS API coverage for APIs which are used by NetworkStack"
This commit is contained in:
@@ -23,6 +23,7 @@ java_library {
|
||||
"androidx.test.rules",
|
||||
"frameworks-net-testutils",
|
||||
"junit",
|
||||
"mockito-target-minus-junit4",
|
||||
],
|
||||
libs: [
|
||||
"android.test.base.stubs",
|
||||
|
||||
90
tests/net/common/java/android/net/CaptivePortalTest.java
Normal file
90
tests/net/common/java/android/net/CaptivePortalTest.java
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import android.os.RemoteException;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
public class CaptivePortalTest {
|
||||
private static final int DEFAULT_TIMEOUT_MS = 5000;
|
||||
private static final String TEST_PACKAGE_NAME = "com.google.android.test";
|
||||
|
||||
private final class MyCaptivePortalImpl extends ICaptivePortal.Stub {
|
||||
int mCode = -1;
|
||||
String mPackageName = null;
|
||||
|
||||
@Override
|
||||
public void appResponse(final int response) throws RemoteException {
|
||||
mCode = response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logEvent(int eventId, String packageName) throws RemoteException {
|
||||
mCode = eventId;
|
||||
mPackageName = packageName;
|
||||
}
|
||||
}
|
||||
|
||||
private interface TestFunctor {
|
||||
void useCaptivePortal(CaptivePortal o);
|
||||
}
|
||||
|
||||
private MyCaptivePortalImpl runCaptivePortalTest(TestFunctor f) {
|
||||
final MyCaptivePortalImpl cp = new MyCaptivePortalImpl();
|
||||
f.useCaptivePortal(new CaptivePortal(cp.asBinder()));
|
||||
return cp;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReportCaptivePortalDismissed() {
|
||||
final MyCaptivePortalImpl result =
|
||||
runCaptivePortalTest(c -> c.reportCaptivePortalDismissed());
|
||||
assertEquals(result.mCode, CaptivePortal.APP_RETURN_DISMISSED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreNetwork() {
|
||||
final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.ignoreNetwork());
|
||||
assertEquals(result.mCode, CaptivePortal.APP_RETURN_UNWANTED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUseNetwork() {
|
||||
final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.useNetwork());
|
||||
assertEquals(result.mCode, CaptivePortal.APP_RETURN_WANTED_AS_IS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLogEvent() {
|
||||
final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.logEvent(
|
||||
MetricsEvent.ACTION_CAPTIVE_PORTAL_LOGIN_ACTIVITY,
|
||||
TEST_PACKAGE_NAME));
|
||||
assertEquals(result.mCode, MetricsEvent.ACTION_CAPTIVE_PORTAL_LOGIN_ACTIVITY);
|
||||
assertEquals(result.mPackageName, TEST_PACKAGE_NAME);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.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 org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
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
|
||||
fun testBuilderAndParcel() {
|
||||
val apfProgramEvent = ApfProgramEvent.Builder()
|
||||
.setLifetime(1)
|
||||
.setActualLifetime(2)
|
||||
.setFilteredRas(3)
|
||||
.setCurrentRas(4)
|
||||
.setProgramLength(5)
|
||||
.setFlags(true, true)
|
||||
.build()
|
||||
|
||||
assertEquals(1, apfProgramEvent.lifetime)
|
||||
assertEquals(2, apfProgramEvent.actualLifetime)
|
||||
assertEquals(3, apfProgramEvent.filteredRas)
|
||||
assertEquals(4, apfProgramEvent.currentRas)
|
||||
assertEquals(5, apfProgramEvent.programLength)
|
||||
assertEquals(ApfProgramEvent.flagsFor(true, true), apfProgramEvent.flags)
|
||||
|
||||
testParcel(apfProgramEvent, 6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFlagsFor() {
|
||||
var flags = ApfProgramEvent.flagsFor(false, false)
|
||||
assertFalse(flags hasFlag ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)
|
||||
assertFalse(flags hasFlag ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)
|
||||
|
||||
flags = ApfProgramEvent.flagsFor(true, false)
|
||||
assertTrue(flags hasFlag ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)
|
||||
assertFalse(flags hasFlag ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)
|
||||
|
||||
flags = ApfProgramEvent.flagsFor(false, true)
|
||||
assertFalse(flags hasFlag ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)
|
||||
assertTrue(flags hasFlag ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)
|
||||
|
||||
flags = ApfProgramEvent.flagsFor(true, true)
|
||||
assertTrue(flags hasFlag ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)
|
||||
assertTrue(flags hasFlag ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)
|
||||
}
|
||||
}
|
||||
64
tests/net/common/java/android/net/metrics/ApfStatsTest.kt
Normal file
64
tests/net/common/java/android/net/metrics/ApfStatsTest.kt
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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.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 org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
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()
|
||||
.setDurationMs(Long.MAX_VALUE)
|
||||
.setReceivedRas(1)
|
||||
.setMatchingRas(2)
|
||||
.setDroppedRas(3)
|
||||
.setZeroLifetimeRas(4)
|
||||
.setParseErrors(5)
|
||||
.setProgramUpdates(6)
|
||||
.setProgramUpdatesAll(7)
|
||||
.setProgramUpdatesAllowingMulticast(8)
|
||||
.setMaxProgramSize(9)
|
||||
.build()
|
||||
|
||||
assertEquals(Long.MAX_VALUE, apfStats.durationMs)
|
||||
assertEquals(1, apfStats.receivedRas)
|
||||
assertEquals(2, apfStats.matchingRas)
|
||||
assertEquals(3, apfStats.droppedRas)
|
||||
assertEquals(4, apfStats.zeroLifetimeRas)
|
||||
assertEquals(5, apfStats.parseErrors)
|
||||
assertEquals(6, apfStats.programUpdates)
|
||||
assertEquals(7, apfStats.programUpdatesAll)
|
||||
assertEquals(8, apfStats.programUpdatesAllowingMulticast)
|
||||
assertEquals(9, apfStats.maxProgramSize)
|
||||
|
||||
testParcel(apfStats, 10)
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,7 @@ import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
private const val TEST_ERROR_CODE = 12345
|
||||
/**
|
||||
* DHCP Optional Type: DHCP Subnet Mask (Copy from DhcpPacket.java)
|
||||
*/
|
||||
//DHCP Optional Type: DHCP Subnet Mask (Copy from DhcpPacket.java due to it's protected)
|
||||
private const val DHCP_SUBNET_MASK = 1
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* 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.metrics;
|
||||
|
||||
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
|
||||
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.net.ConnectivityMetricsEvent;
|
||||
import android.net.IIpConnectivityMetrics;
|
||||
import android.net.Network;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import com.android.internal.util.BitUtils;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
public class IpConnectivityLogTest {
|
||||
private static final int FAKE_NET_ID = 100;
|
||||
private static final int[] FAKE_TRANSPORT_TYPES = BitUtils.unpackBits(TRANSPORT_WIFI);
|
||||
private static final long FAKE_TIME_STAMP = System.currentTimeMillis();
|
||||
private static final String FAKE_INTERFACE_NAME = "test";
|
||||
private static final IpReachabilityEvent FAKE_EV =
|
||||
new IpReachabilityEvent(IpReachabilityEvent.NUD_FAILED);
|
||||
|
||||
@Mock IIpConnectivityMetrics mMockService;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoggingEvents() throws Exception {
|
||||
IpConnectivityLog logger = new IpConnectivityLog(mMockService);
|
||||
|
||||
assertTrue(logger.log(FAKE_EV));
|
||||
assertTrue(logger.log(FAKE_TIME_STAMP, FAKE_EV));
|
||||
assertTrue(logger.log(FAKE_NET_ID, FAKE_TRANSPORT_TYPES, FAKE_EV));
|
||||
assertTrue(logger.log(new Network(FAKE_NET_ID), FAKE_TRANSPORT_TYPES, FAKE_EV));
|
||||
assertTrue(logger.log(FAKE_INTERFACE_NAME, FAKE_EV));
|
||||
assertTrue(logger.log(makeExpectedEvent(FAKE_TIME_STAMP, FAKE_NET_ID, TRANSPORT_WIFI,
|
||||
FAKE_INTERFACE_NAME)));
|
||||
|
||||
List<ConnectivityMetricsEvent> got = verifyEvents(6);
|
||||
assertEventsEqual(makeExpectedEvent(got.get(0).timestamp, 0, 0, null), got.get(0));
|
||||
assertEventsEqual(makeExpectedEvent(FAKE_TIME_STAMP, 0, 0, null), got.get(1));
|
||||
assertEventsEqual(makeExpectedEvent(got.get(2).timestamp, FAKE_NET_ID,
|
||||
TRANSPORT_WIFI, null), got.get(2));
|
||||
assertEventsEqual(makeExpectedEvent(got.get(3).timestamp, FAKE_NET_ID,
|
||||
TRANSPORT_WIFI, null), got.get(3));
|
||||
assertEventsEqual(makeExpectedEvent(got.get(4).timestamp, 0, 0, FAKE_INTERFACE_NAME),
|
||||
got.get(4));
|
||||
assertEventsEqual(makeExpectedEvent(FAKE_TIME_STAMP, FAKE_NET_ID,
|
||||
TRANSPORT_WIFI, FAKE_INTERFACE_NAME), got.get(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoggingEventsWithMultipleCallers() throws Exception {
|
||||
IpConnectivityLog logger = new IpConnectivityLog(mMockService);
|
||||
|
||||
final int nCallers = 10;
|
||||
final int nEvents = 10;
|
||||
for (int n = 0; n < nCallers; n++) {
|
||||
final int i = n;
|
||||
new Thread() {
|
||||
public void run() {
|
||||
for (int j = 0; j < nEvents; j++) {
|
||||
assertTrue(logger.log(makeExpectedEvent(
|
||||
FAKE_TIME_STAMP + i * 100 + j,
|
||||
FAKE_NET_ID + i * 100 + j,
|
||||
((i + j) % 2 == 0) ? TRANSPORT_WIFI : TRANSPORT_CELLULAR,
|
||||
FAKE_INTERFACE_NAME)));
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
List<ConnectivityMetricsEvent> got = verifyEvents(nCallers * nEvents, 200);
|
||||
Collections.sort(got, EVENT_COMPARATOR);
|
||||
Iterator<ConnectivityMetricsEvent> iter = got.iterator();
|
||||
for (int i = 0; i < nCallers; i++) {
|
||||
for (int j = 0; j < nEvents; j++) {
|
||||
final long expectedTimestamp = FAKE_TIME_STAMP + i * 100 + j;
|
||||
final int expectedNetId = FAKE_NET_ID + i * 100 + j;
|
||||
final long expectedTransports =
|
||||
((i + j) % 2 == 0) ? TRANSPORT_WIFI : TRANSPORT_CELLULAR;
|
||||
assertEventsEqual(makeExpectedEvent(expectedTimestamp, expectedNetId,
|
||||
expectedTransports, FAKE_INTERFACE_NAME), iter.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<ConnectivityMetricsEvent> verifyEvents(int n, int timeoutMs) throws Exception {
|
||||
ArgumentCaptor<ConnectivityMetricsEvent> captor =
|
||||
ArgumentCaptor.forClass(ConnectivityMetricsEvent.class);
|
||||
verify(mMockService, timeout(timeoutMs).times(n)).logEvent(captor.capture());
|
||||
return captor.getAllValues();
|
||||
}
|
||||
|
||||
private List<ConnectivityMetricsEvent> verifyEvents(int n) throws Exception {
|
||||
return verifyEvents(n, 10);
|
||||
}
|
||||
|
||||
|
||||
private ConnectivityMetricsEvent makeExpectedEvent(long timestamp, int netId, long transports,
|
||||
String ifname) {
|
||||
ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
|
||||
ev.timestamp = timestamp;
|
||||
ev.data = FAKE_EV;
|
||||
ev.netId = netId;
|
||||
ev.transports = transports;
|
||||
ev.ifname = ifname;
|
||||
return ev;
|
||||
}
|
||||
|
||||
/** Outer equality for ConnectivityMetricsEvent to avoid overriding equals() and hashCode(). */
|
||||
private void assertEventsEqual(ConnectivityMetricsEvent expected,
|
||||
ConnectivityMetricsEvent got) {
|
||||
assertEquals(expected.data, got.data);
|
||||
assertEquals(expected.timestamp, got.timestamp);
|
||||
assertEquals(expected.netId, got.netId);
|
||||
assertEquals(expected.transports, got.transports);
|
||||
assertEquals(expected.ifname, got.ifname);
|
||||
}
|
||||
|
||||
static final Comparator<ConnectivityMetricsEvent> EVENT_COMPARATOR =
|
||||
Comparator.comparingLong((ev) -> ev.timestamp);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.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 org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
79
tests/net/common/java/android/net/metrics/RaEventTest.kt
Normal file
79
tests/net/common/java/android/net/metrics/RaEventTest.kt
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.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 org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
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()
|
||||
assertEquals(NO_LIFETIME, raEvent.routerLifetime)
|
||||
assertEquals(NO_LIFETIME, raEvent.prefixValidLifetime)
|
||||
assertEquals(NO_LIFETIME, raEvent.prefixPreferredLifetime)
|
||||
assertEquals(NO_LIFETIME, raEvent.routeInfoLifetime)
|
||||
assertEquals(NO_LIFETIME, raEvent.rdnssLifetime)
|
||||
assertEquals(NO_LIFETIME, raEvent.dnsslLifetime)
|
||||
|
||||
raEvent = RaEvent.Builder()
|
||||
.updateRouterLifetime(1)
|
||||
.updatePrefixValidLifetime(2)
|
||||
.updatePrefixPreferredLifetime(3)
|
||||
.updateRouteInfoLifetime(4)
|
||||
.updateRdnssLifetime(5)
|
||||
.updateDnsslLifetime(6)
|
||||
.build()
|
||||
assertEquals(1, raEvent.routerLifetime)
|
||||
assertEquals(2, raEvent.prefixValidLifetime)
|
||||
assertEquals(3, raEvent.prefixPreferredLifetime)
|
||||
assertEquals(4, raEvent.routeInfoLifetime)
|
||||
assertEquals(5, raEvent.rdnssLifetime)
|
||||
assertEquals(6, raEvent.dnsslLifetime)
|
||||
|
||||
raEvent = RaEvent.Builder()
|
||||
.updateRouterLifetime(Long.MIN_VALUE)
|
||||
.updateRouterLifetime(Long.MAX_VALUE)
|
||||
.build()
|
||||
assertEquals(Long.MIN_VALUE, raEvent.routerLifetime)
|
||||
|
||||
raEvent = RaEvent(1, 2, 3, 4, 5, 6)
|
||||
assertEquals(1, raEvent.routerLifetime)
|
||||
assertEquals(2, raEvent.prefixValidLifetime)
|
||||
assertEquals(3, raEvent.prefixPreferredLifetime)
|
||||
assertEquals(4, raEvent.routeInfoLifetime)
|
||||
assertEquals(5, raEvent.rdnssLifetime)
|
||||
assertEquals(6, raEvent.dnsslLifetime)
|
||||
|
||||
testParcel(raEvent, 6)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.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 java.lang.reflect.Modifier
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
private const val FIRST_VALIDATION: Int = 1 shl 8
|
||||
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
|
||||
fun testBuilderAndParcel() {
|
||||
var validationProbeEvent = ValidationProbeEvent.Builder()
|
||||
.setProbeType(ValidationProbeEvent.PROBE_DNS, false).build()
|
||||
|
||||
assertTrue(validationProbeEvent.probeType hasType REVALIDATION)
|
||||
|
||||
validationProbeEvent = ValidationProbeEvent.Builder()
|
||||
.setDurationMs(Long.MAX_VALUE)
|
||||
.setProbeType(ValidationProbeEvent.PROBE_DNS, true)
|
||||
.setReturnCode(ValidationProbeEvent.DNS_SUCCESS)
|
||||
.build()
|
||||
|
||||
assertEquals(Long.MAX_VALUE, validationProbeEvent.durationMs)
|
||||
assertTrue(validationProbeEvent.probeType hasType ValidationProbeEvent.PROBE_DNS)
|
||||
assertTrue(validationProbeEvent.probeType hasType FIRST_VALIDATION)
|
||||
assertEquals(ValidationProbeEvent.DNS_SUCCESS, validationProbeEvent.returnCode)
|
||||
|
||||
testParcel(validationProbeEvent, 3)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetProbeName() {
|
||||
val probeFields = ValidationProbeEvent::class.java.declaredFields.filter {
|
||||
it.type == Int::class.javaPrimitiveType
|
||||
&& Modifier.isPublic(it.modifiers) && Modifier.isStatic(it.modifiers)
|
||||
&& it.name.contains("PROBE")
|
||||
}
|
||||
|
||||
probeFields.forEach {
|
||||
val intValue = it.getInt(null)
|
||||
val stringValue = ValidationProbeEvent.getProbeName(intValue)
|
||||
assertEquals(it.name, stringValue)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -21,11 +21,8 @@ import static android.net.metrics.INetdEventListener.EVENT_GETHOSTBYNAME;
|
||||
|
||||
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 static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
@@ -59,16 +56,11 @@ import com.android.server.connectivity.metrics.nano.IpConnectivityLogClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
@@ -97,48 +89,6 @@ public class IpConnectivityMetricsTest {
|
||||
mService.mNetdListener = mNetdListener;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoggingEvents() throws Exception {
|
||||
IpConnectivityLog logger = new IpConnectivityLog(mMockService);
|
||||
|
||||
assertTrue(logger.log(1, FAKE_EV));
|
||||
assertTrue(logger.log(2, FAKE_EV));
|
||||
assertTrue(logger.log(3, FAKE_EV));
|
||||
|
||||
List<ConnectivityMetricsEvent> got = verifyEvents(3);
|
||||
assertEventsEqual(expectedEvent(1), got.get(0));
|
||||
assertEventsEqual(expectedEvent(2), got.get(1));
|
||||
assertEventsEqual(expectedEvent(3), got.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoggingEventsWithMultipleCallers() throws Exception {
|
||||
IpConnectivityLog logger = new IpConnectivityLog(mMockService);
|
||||
|
||||
final int nCallers = 10;
|
||||
final int nEvents = 10;
|
||||
for (int n = 0; n < nCallers; n++) {
|
||||
final int i = n;
|
||||
new Thread() {
|
||||
public void run() {
|
||||
for (int j = 0; j < nEvents; j++) {
|
||||
assertTrue(logger.log(1 + i * 100 + j, FAKE_EV));
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
List<ConnectivityMetricsEvent> got = verifyEvents(nCallers * nEvents, 200);
|
||||
Collections.sort(got, EVENT_COMPARATOR);
|
||||
Iterator<ConnectivityMetricsEvent> iter = got.iterator();
|
||||
for (int i = 0; i < nCallers; i++) {
|
||||
for (int j = 0; j < nEvents; j++) {
|
||||
int expectedTimestamp = 1 + i * 100 + j;
|
||||
assertEventsEqual(expectedEvent(expectedTimestamp), iter.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBufferFlushing() {
|
||||
String output1 = getdump("flush");
|
||||
@@ -653,16 +603,7 @@ public class IpConnectivityMetricsTest {
|
||||
return nai;
|
||||
}
|
||||
|
||||
List<ConnectivityMetricsEvent> verifyEvents(int n, int timeoutMs) throws Exception {
|
||||
ArgumentCaptor<ConnectivityMetricsEvent> captor =
|
||||
ArgumentCaptor.forClass(ConnectivityMetricsEvent.class);
|
||||
verify(mMockService, timeout(timeoutMs).times(n)).logEvent(captor.capture());
|
||||
return captor.getAllValues();
|
||||
}
|
||||
|
||||
List<ConnectivityMetricsEvent> verifyEvents(int n) throws Exception {
|
||||
return verifyEvents(n, 10);
|
||||
}
|
||||
|
||||
static void verifySerialization(String want, String output) {
|
||||
try {
|
||||
@@ -674,28 +615,4 @@ public class IpConnectivityMetricsTest {
|
||||
fail(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
static String joinLines(String ... elems) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
for (String s : elems) {
|
||||
b.append(s).append("\n");
|
||||
}
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
static ConnectivityMetricsEvent expectedEvent(int timestamp) {
|
||||
ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
|
||||
ev.timestamp = timestamp;
|
||||
ev.data = FAKE_EV;
|
||||
return ev;
|
||||
}
|
||||
|
||||
/** Outer equality for ConnectivityMetricsEvent to avoid overriding equals() and hashCode(). */
|
||||
static void assertEventsEqual(ConnectivityMetricsEvent expected, ConnectivityMetricsEvent got) {
|
||||
assertEquals(expected.timestamp, got.timestamp);
|
||||
assertEquals(expected.data, got.data);
|
||||
}
|
||||
|
||||
static final Comparator<ConnectivityMetricsEvent> EVENT_COMPARATOR =
|
||||
Comparator.comparingLong((ev) -> ev.timestamp);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user