Merge "Add CTS API coverage for APIs which are used by NetworkStack"

This commit is contained in:
Paul Hu
2019-05-02 01:37:26 +00:00
committed by Gerrit Code Review
21 changed files with 827 additions and 87 deletions

View File

@@ -176,7 +176,7 @@ public final class ApfProgramEvent implements IpConnectivityLog.Event {
out.writeInt(filteredRas);
out.writeInt(currentRas);
out.writeInt(programLength);
out.writeInt(flags);
out.writeInt(this.flags);
}
/** @hide */
@@ -192,6 +192,18 @@ public final class ApfProgramEvent implements IpConnectivityLog.Event {
programLength, actualLifetime, lifetimeString, namesOf(flags));
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj.getClass().equals(ApfProgramEvent.class))) return false;
final ApfProgramEvent other = (ApfProgramEvent) obj;
return lifetime == other.lifetime
&& actualLifetime == other.actualLifetime
&& filteredRas == other.filteredRas
&& currentRas == other.currentRas
&& programLength == other.programLength
&& flags == other.flags;
}
/** @hide */
public static final Parcelable.Creator<ApfProgramEvent> CREATOR
= new Parcelable.Creator<ApfProgramEvent>() {

View File

@@ -275,6 +275,22 @@ public final class ApfStats implements IpConnectivityLog.Event {
.toString();
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj.getClass().equals(ApfStats.class))) return false;
final ApfStats other = (ApfStats) obj;
return durationMs == other.durationMs
&& receivedRas == other.receivedRas
&& matchingRas == other.matchingRas
&& droppedRas == other.droppedRas
&& zeroLifetimeRas == other.zeroLifetimeRas
&& parseErrors == other.parseErrors
&& programUpdates == other.programUpdates
&& programUpdatesAll == other.programUpdatesAll
&& programUpdatesAllowingMulticast == other.programUpdatesAllowingMulticast
&& maxProgramSize == other.maxProgramSize;
}
/** @hide */
public static final Parcelable.Creator<ApfStats> CREATOR = new Parcelable.Creator<ApfStats>() {
public ApfStats createFromParcel(Parcel in) {

View File

@@ -22,6 +22,7 @@ import android.annotation.TestApi;
import android.annotation.UnsupportedAppUsage;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
/**
* An event recorded when a DhcpClient state machine transitions to a new state.
@@ -101,6 +102,14 @@ public final class DhcpClientEvent implements IpConnectivityLog.Event {
return String.format("DhcpClientEvent(%s, %dms)", msg, durationMs);
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj.getClass().equals(DhcpClientEvent.class))) return false;
final DhcpClientEvent other = (DhcpClientEvent) obj;
return TextUtils.equals(msg, other.msg)
&& durationMs == other.durationMs;
}
/** @hide */
public static final Parcelable.Creator<DhcpClientEvent> CREATOR
= new Parcelable.Creator<DhcpClientEvent>() {

View File

@@ -101,6 +101,14 @@ public final class IpManagerEvent implements IpConnectivityLog.Event {
Decoder.constants.get(eventType), durationMs);
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj.getClass().equals(IpManagerEvent.class))) return false;
final IpManagerEvent other = (IpManagerEvent) obj;
return eventType == other.eventType
&& durationMs == other.durationMs;
}
final static class Decoder {
static final SparseArray<String> constants = MessageUtils.findMessageNames(
new Class[]{IpManagerEvent.class},

View File

@@ -93,6 +93,13 @@ public final class IpReachabilityEvent implements IpConnectivityLog.Event {
return String.format("IpReachabilityEvent(%s:%02x)", eventName, lo);
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj.getClass().equals(IpReachabilityEvent.class))) return false;
final IpReachabilityEvent other = (IpReachabilityEvent) obj;
return eventType == other.eventType;
}
final static class Decoder {
static final SparseArray<String> constants =
MessageUtils.findMessageNames(new Class[]{IpReachabilityEvent.class},

View File

@@ -121,6 +121,14 @@ public final class NetworkEvent implements IpConnectivityLog.Event {
Decoder.constants.get(eventType), durationMs);
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj.getClass().equals(NetworkEvent.class))) return false;
final NetworkEvent other = (NetworkEvent) obj;
return eventType == other.eventType
&& durationMs == other.durationMs;
}
final static class Decoder {
static final SparseArray<String> constants = MessageUtils.findMessageNames(
new Class[]{NetworkEvent.class}, new String[]{"NETWORK_"});

View File

@@ -97,6 +97,18 @@ public final class RaEvent implements IpConnectivityLog.Event {
.toString();
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj.getClass().equals(RaEvent.class))) return false;
final RaEvent other = (RaEvent) obj;
return routerLifetime == other.routerLifetime
&& prefixValidLifetime == other.prefixValidLifetime
&& prefixPreferredLifetime == other.prefixPreferredLifetime
&& routeInfoLifetime == other.routeInfoLifetime
&& rdnssLifetime == other.rdnssLifetime
&& dnsslLifetime == other.dnsslLifetime;
}
/** @hide */
public static final Parcelable.Creator<RaEvent> CREATOR = new Parcelable.Creator<RaEvent>() {
public RaEvent createFromParcel(Parcel in) {

View File

@@ -170,6 +170,15 @@ public final class ValidationProbeEvent implements IpConnectivityLog.Event {
getProbeName(probeType), returnCode, getValidationStage(probeType), durationMs);
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj.getClass().equals(ValidationProbeEvent.class))) return false;
final ValidationProbeEvent other = (ValidationProbeEvent) obj;
return durationMs == other.durationMs
&& probeType == other.probeType
&& returnCode == other.returnCode;
}
final static class Decoder {
static final SparseArray<String> constants = MessageUtils.findMessageNames(
new Class[]{ValidationProbeEvent.class},

View File

@@ -23,6 +23,7 @@ java_library {
"androidx.test.rules",
"frameworks-net-testutils",
"junit",
"mockito-target-minus-junit4",
],
libs: [
"android.test.base.stubs",

View 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);
}
}

View 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.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)
}
}

View 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)
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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 FAKE_MESSAGE = "test"
@RunWith(AndroidJUnit4::class)
@SmallTest
class DhcpClientEventTest {
private fun <T: Parcelable> testParcel(obj: T, fieldCount: Int) {
ParcelableTestUtil.assertFieldCountEquals(fieldCount, obj::class.java)
TestUtils.assertParcelingIsLossless(obj)
}
@Test
fun testBuilderAndParcel() {
val dhcpClientEvent = DhcpClientEvent.Builder()
.setMsg(FAKE_MESSAGE)
.setDurationMs(Integer.MAX_VALUE)
.build()
assertEquals(FAKE_MESSAGE, dhcpClientEvent.msg)
assertEquals(Integer.MAX_VALUE, dhcpClientEvent.durationMs)
testParcel(dhcpClientEvent, 2)
}
}

View File

@@ -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)

View File

@@ -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);
}

View File

@@ -0,0 +1,46 @@
/*
* 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 IpManagerEventTest {
private fun <T: Parcelable> testParcel(obj: T, fieldCount: Int) {
ParcelableTestUtil.assertFieldCountEquals(fieldCount, obj::class.java)
TestUtils.assertParcelingIsLossless(obj)
}
@Test
fun testConstructorAndParcel() {
(IpManagerEvent.PROVISIONING_OK..IpManagerEvent.ERROR_INTERFACE_NOT_FOUND).forEach {
val ipManagerEvent = IpManagerEvent(it, Long.MAX_VALUE)
assertEquals(it, ipManagerEvent.eventType)
assertEquals(Long.MAX_VALUE, ipManagerEvent.durationMs)
testParcel(ipManagerEvent, 2)
}
}
}

View File

@@ -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)
}
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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 NetworkEventTest {
private fun <T: Parcelable> testParcel(obj: T, fieldCount: Int) {
ParcelableTestUtil.assertFieldCountEquals(fieldCount, obj::class.java)
TestUtils.assertParcelingIsLossless(obj)
}
@Test
fun testConstructorAndParcel() {
(NetworkEvent.NETWORK_CONNECTED..NetworkEvent.NETWORK_PARTIAL_CONNECTIVITY).forEach {
var networkEvent = NetworkEvent(it)
assertEquals(it, networkEvent.eventType)
assertEquals(0, networkEvent.durationMs)
networkEvent = NetworkEvent(it, Long.MAX_VALUE)
assertEquals(it, networkEvent.eventType)
assertEquals(Long.MAX_VALUE, networkEvent.durationMs)
testParcel(networkEvent, 2)
}
}
}

View 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)
}
}

View 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 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)
}
}
}

View File

@@ -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);
}