ConnectivityServiceTest: remove remaining poll loops. am: aebc0598b9 am: 5f807b3b74
am: 87eab8e1c4
Change-Id: I31681e1248721704a9b792962a2ef5955e375e93
diff --git a/core/tests/coretests/src/android/net/IpPrefixTest.java b/core/tests/coretests/src/android/net/IpPrefixTest.java
index 4f2387d..fcc6389 100644
--- a/core/tests/coretests/src/android/net/IpPrefixTest.java
+++ b/core/tests/coretests/src/android/net/IpPrefixTest.java
@@ -18,14 +18,14 @@
import android.net.IpPrefix;
import android.os.Parcel;
+import static android.test.MoreAsserts.assertNotEqual;
import android.test.suitebuilder.annotation.SmallTest;
+
+import static org.junit.Assert.assertArrayEquals;
import java.net.InetAddress;
import java.util.Random;
import junit.framework.TestCase;
-import static android.test.MoreAsserts.assertNotEqual;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
public class IpPrefixTest extends TestCase {
@@ -242,42 +242,25 @@
@SmallTest
public void testHashCode() {
- IpPrefix p = new IpPrefix(new byte[4], 0);
+ IpPrefix p;
+ int oldCode = -1;
Random random = new Random();
for (int i = 0; i < 100; i++) {
- final IpPrefix oldP = p;
if (random.nextBoolean()) {
// IPv4.
byte[] b = new byte[4];
random.nextBytes(b);
p = new IpPrefix(b, random.nextInt(33));
+ assertNotEqual(oldCode, p.hashCode());
+ oldCode = p.hashCode();
} else {
// IPv6.
byte[] b = new byte[16];
random.nextBytes(b);
p = new IpPrefix(b, random.nextInt(129));
+ assertNotEqual(oldCode, p.hashCode());
+ oldCode = p.hashCode();
}
- if (p.equals(oldP)) {
- assertEquals(p.hashCode(), oldP.hashCode());
- }
- if (p.hashCode() != oldP.hashCode()) {
- assertNotEqual(p, oldP);
- }
- }
- }
-
- @SmallTest
- public void testHashCodeIsNotConstant() {
- IpPrefix[] prefixes = {
- new IpPrefix("2001:db8:f00::ace:d00d/127"),
- new IpPrefix("192.0.2.0/23"),
- new IpPrefix("::/0"),
- new IpPrefix("0.0.0.0/0"),
- };
- for (int i = 0; i < prefixes.length; i++) {
- for (int j = i + 1; j < prefixes.length; j++) {
- assertNotEqual(prefixes[i].hashCode(), prefixes[j].hashCode());
- }
}
}
diff --git a/services/core/java/com/android/server/connectivity/NetworkNotificationManager.java b/services/core/java/com/android/server/connectivity/NetworkNotificationManager.java
index c6bf4c5..4ff6657 100644
--- a/services/core/java/com/android/server/connectivity/NetworkNotificationManager.java
+++ b/services/core/java/com/android/server/connectivity/NetworkNotificationManager.java
@@ -19,7 +19,6 @@
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
-import android.widget.Toast;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
@@ -27,17 +26,40 @@
import android.os.UserHandle;
import android.telephony.TelephonyManager;
import android.util.Slog;
-
+import android.util.SparseArray;
+import android.util.SparseIntArray;
+import android.widget.Toast;
import com.android.internal.R;
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.logging.MetricsProto.MetricsEvent;
-import static android.net.NetworkCapabilities.*;
-
+import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
+import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
+import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
public class NetworkNotificationManager {
- public static enum NotificationType { SIGN_IN, NO_INTERNET, LOST_INTERNET, NETWORK_SWITCH };
+ public static enum NotificationType {
+ LOST_INTERNET(MetricsEvent.NOTIFICATION_NETWORK_LOST_INTERNET),
+ NETWORK_SWITCH(MetricsEvent.NOTIFICATION_NETWORK_SWITCH),
+ NO_INTERNET(MetricsEvent.NOTIFICATION_NETWORK_NO_INTERNET),
+ SIGN_IN(MetricsEvent.NOTIFICATION_NETWORK_SIGN_IN);
- private static final String NOTIFICATION_ID = "Connectivity.Notification";
+ public final int eventId;
+
+ NotificationType(int eventId) {
+ this.eventId = eventId;
+ Holder.sIdToTypeMap.put(eventId, this);
+ }
+
+ private static class Holder {
+ private static SparseArray<NotificationType> sIdToTypeMap = new SparseArray<>();
+ }
+
+ public static NotificationType getFromId(int id) {
+ return Holder.sIdToTypeMap.get(id);
+ }
+ };
private static final String TAG = NetworkNotificationManager.class.getSimpleName();
private static final boolean DBG = true;
@@ -46,11 +68,14 @@
private final Context mContext;
private final TelephonyManager mTelephonyManager;
private final NotificationManager mNotificationManager;
+ // Tracks the types of notifications managed by this instance, from creation to cancellation.
+ private final SparseIntArray mNotificationTypeMap;
public NetworkNotificationManager(Context c, TelephonyManager t, NotificationManager n) {
mContext = c;
mTelephonyManager = t;
mNotificationManager = n;
+ mNotificationTypeMap = new SparseIntArray();
}
// TODO: deal more gracefully with multi-transport networks.
@@ -100,8 +125,10 @@
*/
public void showNotification(int id, NotificationType notifyType, NetworkAgentInfo nai,
NetworkAgentInfo switchToNai, PendingIntent intent, boolean highPriority) {
- int transportType;
- String extraInfo;
+ final String tag = tagFor(id);
+ final int eventId = notifyType.eventId;
+ final int transportType;
+ final String extraInfo;
if (nai != null) {
transportType = getFirstTransportType(nai);
extraInfo = nai.networkInfo.getExtraInfo();
@@ -114,9 +141,10 @@
}
if (DBG) {
- Slog.d(TAG, "showNotification id=" + id + " " + notifyType
- + " transportType=" + getTransportName(transportType)
- + " extraInfo=" + extraInfo + " highPriority=" + highPriority);
+ Slog.d(TAG, String.format(
+ "showNotification tag=%s event=%s transport=%s extraInfo=%s highPrioriy=%s",
+ tag, nameOf(eventId), getTransportName(transportType), extraInfo,
+ highPriority));
}
Resources r = Resources.getSystem();
@@ -154,7 +182,7 @@
details = r.getString(R.string.network_switch_metered_detail, toTransport,
fromTransport);
} else {
- Slog.wtf(TAG, "Unknown notification type " + notifyType + "on network transport "
+ Slog.wtf(TAG, "Unknown notification type " + notifyType + " on network transport "
+ getTransportName(transportType));
return;
}
@@ -184,22 +212,31 @@
Notification notification = builder.build();
+ mNotificationTypeMap.put(id, eventId);
try {
- mNotificationManager.notifyAsUser(NOTIFICATION_ID, id, notification, UserHandle.ALL);
+ mNotificationManager.notifyAsUser(tag, eventId, notification, UserHandle.ALL);
} catch (NullPointerException npe) {
Slog.d(TAG, "setNotificationVisible: visible notificationManager error", npe);
}
}
public void clearNotification(int id) {
+ if (mNotificationTypeMap.indexOfKey(id) < 0) {
+ return;
+ }
+ final String tag = tagFor(id);
+ final int eventId = mNotificationTypeMap.get(id);
if (DBG) {
- Slog.d(TAG, "clearNotification id=" + id);
+ Slog.d(TAG, String.format("clearing notification tag=%s event=%s", tag,
+ nameOf(eventId)));
}
try {
- mNotificationManager.cancelAsUser(NOTIFICATION_ID, id, UserHandle.ALL);
+ mNotificationManager.cancelAsUser(tag, eventId, UserHandle.ALL);
} catch (NullPointerException npe) {
- Slog.d(TAG, "setNotificationVisible: cancel notificationManager error", npe);
+ Slog.d(TAG, String.format(
+ "failed to clear notification tag=%s event=%s", tag, nameOf(eventId)), npe);
}
+ mNotificationTypeMap.delete(id);
}
/**
@@ -222,4 +259,15 @@
R.string.network_switch_metered_toast, fromTransport, toTransport);
Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
}
+
+ @VisibleForTesting
+ static String tagFor(int id) {
+ return String.format("ConnectivityNotification:%d", id);
+ }
+
+ @VisibleForTesting
+ static String nameOf(int eventId) {
+ NotificationType t = NotificationType.getFromId(eventId);
+ return (t != null) ? t.name() : "UNKNOWN";
+ }
}
diff --git a/services/tests/servicestests/src/com/android/server/connectivity/NetworkNotificationManagerTest.java b/services/tests/servicestests/src/com/android/server/connectivity/NetworkNotificationManagerTest.java
new file mode 100644
index 0000000..21c2de7
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/connectivity/NetworkNotificationManagerTest.java
@@ -0,0 +1,142 @@
+/*
+ * Copyright (C) 2016 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 com.android.server.connectivity;
+
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.content.res.Resources;
+import android.net.NetworkCapabilities;
+import android.net.NetworkInfo;
+import android.telephony.TelephonyManager;
+import android.test.suitebuilder.annotation.SmallTest;
+import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import junit.framework.TestCase;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+import static com.android.server.connectivity.NetworkNotificationManager.NotificationType.*;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class NetworkNotificationManagerTest extends TestCase {
+
+ static final NetworkCapabilities CELL_CAPABILITIES = new NetworkCapabilities();
+ static final NetworkCapabilities WIFI_CAPABILITIES = new NetworkCapabilities();
+ static {
+ CELL_CAPABILITIES.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
+ CELL_CAPABILITIES.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
+
+ WIFI_CAPABILITIES.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
+ WIFI_CAPABILITIES.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
+ }
+
+ @Mock Context mCtx;
+ @Mock Resources mResources;
+ @Mock PackageManager mPm;
+ @Mock TelephonyManager mTelephonyManager;
+ @Mock NotificationManager mNotificationManager;
+ @Mock NetworkAgentInfo mWifiNai;
+ @Mock NetworkAgentInfo mCellNai;
+ @Mock NetworkInfo mNetworkInfo;
+ ArgumentCaptor<Notification> mCaptor;
+
+ NetworkNotificationManager mManager;
+
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mCaptor = ArgumentCaptor.forClass(Notification.class);
+ mWifiNai.networkCapabilities = WIFI_CAPABILITIES;
+ mWifiNai.networkInfo = mNetworkInfo;
+ mCellNai.networkCapabilities = CELL_CAPABILITIES;
+ mCellNai.networkInfo = mNetworkInfo;
+ when(mCtx.getResources()).thenReturn(mResources);
+ when(mCtx.getPackageManager()).thenReturn(mPm);
+ when(mCtx.getApplicationInfo()).thenReturn(new ApplicationInfo());
+ when(mNetworkInfo.getExtraInfo()).thenReturn("extra");
+ when(mResources.getColor(anyInt(), any())).thenReturn(0xFF607D8B);
+
+ mManager = new NetworkNotificationManager(mCtx, mTelephonyManager, mNotificationManager);
+ }
+
+ @SmallTest
+ public void testNotificationsShownAndCleared() {
+ final int NETWORK_ID_BASE = 100;
+ List<NotificationType> types = Arrays.asList(NotificationType.values());
+ List<Integer> ids = new ArrayList<>(types.size());
+ for (int i = 0; i < ids.size(); i++) {
+ ids.add(NETWORK_ID_BASE + i);
+ }
+ Collections.shuffle(ids);
+ Collections.shuffle(types);
+
+ for (int i = 0; i < ids.size(); i++) {
+ mManager.showNotification(ids.get(i), types.get(i), mWifiNai, mCellNai, null, false);
+ }
+
+ Collections.shuffle(ids);
+ for (int i = 0; i < ids.size(); i++) {
+ mManager.clearNotification(ids.get(i));
+ }
+
+ for (int i = 0; i < ids.size(); i++) {
+ final int id = ids.get(i);
+ final int eventId = types.get(i).eventId;
+ final String tag = NetworkNotificationManager.tagFor(id);
+ verify(mNotificationManager, times(1)).notifyAsUser(eq(tag), eq(eventId), any(), any());
+ verify(mNotificationManager, times(1)).cancelAsUser(eq(tag), eq(eventId), any());
+ }
+ }
+
+ @SmallTest
+ public void testNoInternetNotificationsNotShownForCellular() {
+ mManager.showNotification(100, NO_INTERNET, mCellNai, mWifiNai, null, false);
+ mManager.showNotification(101, LOST_INTERNET, mCellNai, mWifiNai, null, false);
+
+ verify(mNotificationManager, never()).notifyAsUser(any(), anyInt(), any(), any());
+
+ mManager.showNotification(102, NO_INTERNET, mWifiNai, mCellNai, null, false);
+
+ final int eventId = NO_INTERNET.eventId;
+ final String tag = NetworkNotificationManager.tagFor(102);
+ verify(mNotificationManager, times(1)).notifyAsUser(eq(tag), eq(eventId), any(), any());
+ }
+
+ @SmallTest
+ public void testNotificationsNotShownIfNoInternetCapability() {
+ mWifiNai.networkCapabilities = new NetworkCapabilities();
+ mWifiNai.networkCapabilities .addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
+ mManager.showNotification(102, NO_INTERNET, mWifiNai, mCellNai, null, false);
+ mManager.showNotification(103, LOST_INTERNET, mWifiNai, mCellNai, null, false);
+ mManager.showNotification(104, NETWORK_SWITCH, mWifiNai, mCellNai, null, false);
+
+ verify(mNotificationManager, never()).notifyAsUser(any(), anyInt(), any(), any());
+ }
+}
diff --git a/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java b/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java
index 84f0f90..011e505 100644
--- a/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java
+++ b/tests/net/java/com/android/server/connectivity/IpConnectivityEventBuilderTest.java
@@ -16,6 +16,17 @@
package com.android.server.connectivity;
+import static com.android.server.connectivity.MetricsTestUtil.aBool;
+import static com.android.server.connectivity.MetricsTestUtil.aByteArray;
+import static com.android.server.connectivity.MetricsTestUtil.aLong;
+import static com.android.server.connectivity.MetricsTestUtil.aString;
+import static com.android.server.connectivity.MetricsTestUtil.aType;
+import static com.android.server.connectivity.MetricsTestUtil.anInt;
+import static com.android.server.connectivity.MetricsTestUtil.anIntArray;
+import static com.android.server.connectivity.MetricsTestUtil.b;
+import static com.android.server.connectivity.MetricsTestUtil.describeIpEvent;
+import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityLog;
+
import android.net.ConnectivityMetricsEvent;
import android.net.metrics.ApfProgramEvent;
import android.net.metrics.ApfStats;
@@ -28,24 +39,13 @@
import android.net.metrics.NetworkEvent;
import android.net.metrics.RaEvent;
import android.net.metrics.ValidationProbeEvent;
-import com.google.protobuf.nano.MessageNano;
+import android.test.suitebuilder.annotation.SmallTest;
import java.util.Arrays;
import junit.framework.TestCase;
-import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityLog;
-import static com.android.server.connectivity.MetricsTestUtil.aBool;
-import static com.android.server.connectivity.MetricsTestUtil.aByteArray;
-import static com.android.server.connectivity.MetricsTestUtil.aLong;
-import static com.android.server.connectivity.MetricsTestUtil.aString;
-import static com.android.server.connectivity.MetricsTestUtil.aType;
-import static com.android.server.connectivity.MetricsTestUtil.anInt;
-import static com.android.server.connectivity.MetricsTestUtil.anIntArray;
-import static com.android.server.connectivity.MetricsTestUtil.b;
-import static com.android.server.connectivity.MetricsTestUtil.describeIpEvent;
-import static com.android.server.connectivity.MetricsTestUtil.ipEv;
-
public class IpConnectivityEventBuilderTest extends TestCase {
+ @SmallTest
public void testDefaultNetworkEventSerialization() {
ConnectivityMetricsEvent ev = describeIpEvent(
aType(DefaultNetworkEvent.class),
@@ -58,6 +58,8 @@
String want = joinLines(
"dropped_events: 0",
"events <",
+ " time_ms: 1",
+ " transport: 0",
" default_network_event <",
" network_id <",
" network_id: 102",
@@ -70,13 +72,13 @@
" transport_types: 2",
" transport_types: 3",
" >",
- " time_ms: 1",
">",
"version: 2");
verifySerialization(want, ev);
}
+ @SmallTest
public void testDhcpClientEventSerialization() {
ConnectivityMetricsEvent ev = describeIpEvent(
aType(DhcpClientEvent.class),
@@ -87,19 +89,20 @@
String want = joinLines(
"dropped_events: 0",
"events <",
+ " time_ms: 1",
+ " transport: 0",
" dhcp_event <",
" duration_ms: 192",
- " error_code: 0",
" if_name: \"wlan0\"",
" state_transition: \"SomeState\"",
" >",
- " time_ms: 1",
">",
"version: 2");
verifySerialization(want, ev);
}
+ @SmallTest
public void testDhcpErrorEventSerialization() {
ConnectivityMetricsEvent ev = describeIpEvent(
aType(DhcpErrorEvent.class),
@@ -109,19 +112,20 @@
String want = joinLines(
"dropped_events: 0",
"events <",
+ " time_ms: 1",
+ " transport: 0",
" dhcp_event <",
" duration_ms: 0",
- " error_code: 50397184",
" if_name: \"wlan0\"",
- " state_transition: \"\"",
+ " error_code: 50397184",
" >",
- " time_ms: 1",
">",
"version: 2");
verifySerialization(want, ev);
}
+ @SmallTest
public void testDnsEventSerialization() {
ConnectivityMetricsEvent ev = describeIpEvent(
aType(DnsEvent.class),
@@ -133,6 +137,8 @@
String want = joinLines(
"dropped_events: 0",
"events <",
+ " time_ms: 1",
+ " transport: 0",
" dns_lookup_batch <",
" event_types: 1",
" event_types: 1",
@@ -162,13 +168,13 @@
" return_codes: 200",
" return_codes: 178",
" >",
- " time_ms: 1",
">",
"version: 2");
verifySerialization(want, ev);
}
+ @SmallTest
public void testIpManagerEventSerialization() {
ConnectivityMetricsEvent ev = describeIpEvent(
aType(IpManagerEvent.class),
@@ -179,18 +185,20 @@
String want = joinLines(
"dropped_events: 0",
"events <",
+ " time_ms: 1",
+ " transport: 0",
" ip_provisioning_event <",
" event_type: 1",
" if_name: \"wlan0\"",
" latency_ms: 5678",
" >",
- " time_ms: 1",
">",
"version: 2");
verifySerialization(want, ev);
}
+ @SmallTest
public void testIpReachabilityEventSerialization() {
ConnectivityMetricsEvent ev = describeIpEvent(
aType(IpReachabilityEvent.class),
@@ -200,17 +208,19 @@
String want = joinLines(
"dropped_events: 0",
"events <",
+ " time_ms: 1",
+ " transport: 0",
" ip_reachability_event <",
" event_type: 512",
" if_name: \"wlan0\"",
" >",
- " time_ms: 1",
">",
"version: 2");
verifySerialization(want, ev);
}
+ @SmallTest
public void testNetworkEventSerialization() {
ConnectivityMetricsEvent ev = describeIpEvent(
aType(NetworkEvent.class),
@@ -221,6 +231,8 @@
String want = joinLines(
"dropped_events: 0",
"events <",
+ " time_ms: 1",
+ " transport: 0",
" network_event <",
" event_type: 5",
" latency_ms: 20410",
@@ -228,13 +240,13 @@
" network_id: 100",
" >",
" >",
- " time_ms: 1",
">",
"version: 2");
verifySerialization(want, ev);
}
+ @SmallTest
public void testValidationProbeEventSerialization() {
ConnectivityMetricsEvent ev = describeIpEvent(
aType(ValidationProbeEvent.class),
@@ -247,6 +259,7 @@
"dropped_events: 0",
"events <",
" time_ms: 1",
+ " transport: 0",
" validation_probe_event <",
" latency_ms: 40730",
" network_id <",
@@ -261,6 +274,7 @@
verifySerialization(want, ev);
}
+ @SmallTest
public void testApfProgramEventSerialization() {
ConnectivityMetricsEvent ev = describeIpEvent(
aType(ApfProgramEvent.class),
@@ -273,6 +287,8 @@
String want = joinLines(
"dropped_events: 0",
"events <",
+ " time_ms: 1",
+ " transport: 0",
" apf_program_event <",
" current_ras: 9",
" drop_multicast: true",
@@ -281,13 +297,13 @@
" lifetime: 200",
" program_length: 2048",
" >",
- " time_ms: 1",
">",
"version: 2");
verifySerialization(want, ev);
}
+ @SmallTest
public void testApfStatsSerialization() {
ConnectivityMetricsEvent ev = describeIpEvent(
aType(ApfStats.class),
@@ -303,6 +319,8 @@
String want = joinLines(
"dropped_events: 0",
"events <",
+ " time_ms: 1",
+ " transport: 0",
" apf_statistics <",
" dropped_ras: 2",
" duration_ms: 45000",
@@ -313,13 +331,13 @@
" received_ras: 10",
" zero_lifetime_ras: 1",
" >",
- " time_ms: 1",
">",
"version: 2");
verifySerialization(want, ev);
}
+ @SmallTest
public void testRaEventSerialization() {
ConnectivityMetricsEvent ev = describeIpEvent(
aType(RaEvent.class),
@@ -333,6 +351,8 @@
String want = joinLines(
"dropped_events: 0",
"events <",
+ " time_ms: 1",
+ " transport: 0",
" ra_event <",
" dnssl_lifetime: -1",
" prefix_preferred_lifetime: 300",
@@ -341,7 +361,6 @@
" route_info_lifetime: -1",
" router_lifetime: 2000",
" >",
- " time_ms: 1",
">",
"version: 2");
@@ -350,9 +369,9 @@
static void verifySerialization(String want, ConnectivityMetricsEvent... input) {
try {
- byte[] got = IpConnectivityEventBuilder.serialize(0, Arrays.asList(input));
- IpConnectivityLog log = new IpConnectivityLog();
- MessageNano.mergeFrom(log, got);
+ byte[] got = IpConnectivityEventBuilder.serialize(0,
+ IpConnectivityEventBuilder.toProto(Arrays.asList(input)));
+ IpConnectivityLog log = IpConnectivityLog.parseFrom(got);
assertEquals(want, log.toString());
} catch (Exception e) {
fail(e.toString());
diff --git a/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java b/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java
index 14b5cbe..450653c 100644
--- a/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java
+++ b/tests/net/java/com/android/server/connectivity/IpConnectivityMetricsTest.java
@@ -16,9 +16,13 @@
package com.android.server.connectivity;
+import static org.mockito.Mockito.timeout;
+import static org.mockito.Mockito.verify;
+
import android.content.Context;
import android.net.ConnectivityMetricsEvent;
import android.net.IIpConnectivityMetrics;
+import android.net.metrics.ApfProgramEvent;
import android.net.metrics.ApfStats;
import android.net.metrics.DefaultNetworkEvent;
import android.net.metrics.DhcpClientEvent;
@@ -28,9 +32,9 @@
import android.net.metrics.RaEvent;
import android.net.metrics.ValidationProbeEvent;
import android.os.Parcelable;
+import android.test.suitebuilder.annotation.SmallTest;
import android.util.Base64;
import com.android.server.connectivity.metrics.IpConnectivityLogClass;
-import com.google.protobuf.nano.MessageNano;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collections;
@@ -42,10 +46,6 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
-import static org.mockito.Mockito.timeout;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-
public class IpConnectivityMetricsTest extends TestCase {
static final IpReachabilityEvent FAKE_EV =
new IpReachabilityEvent("wlan0", IpReachabilityEvent.NUD_FAILED);
@@ -60,6 +60,7 @@
mService = new IpConnectivityMetrics(mCtx, (ctx) -> 2000);
}
+ @SmallTest
public void testLoggingEvents() throws Exception {
IpConnectivityLog logger = new IpConnectivityLog(mMockService);
@@ -73,6 +74,7 @@
assertEventsEqual(expectedEvent(3), got.get(2));
}
+ @SmallTest
public void testLoggingEventsWithMultipleCallers() throws Exception {
IpConnectivityLog logger = new IpConnectivityLog(mMockService);
@@ -100,6 +102,7 @@
}
}
+ @SmallTest
public void testBufferFlushing() {
String output1 = getdump("flush");
assertEquals("", output1);
@@ -112,6 +115,29 @@
assertEquals("", output3);
}
+ @SmallTest
+ public void testRateLimiting() {
+ final IpConnectivityLog logger = new IpConnectivityLog(mService.impl);
+ final ApfProgramEvent ev = new ApfProgramEvent(0, 0, 0, 0, 0);
+ final long fakeTimestamp = 1;
+
+ int attempt = 100; // More than burst quota, but less than buffer size.
+ for (int i = 0; i < attempt; i++) {
+ logger.log(ev);
+ }
+
+ String output1 = getdump("flush");
+ assertFalse("".equals(output1));
+
+ for (int i = 0; i < attempt; i++) {
+ assertFalse("expected event to be dropped", logger.log(fakeTimestamp, ev));
+ }
+
+ String output2 = getdump("flush");
+ assertEquals("", output2);
+ }
+
+ @SmallTest
public void testEndToEndLogging() {
IpConnectivityLog logger = new IpConnectivityLog(mService.impl);
@@ -132,22 +158,25 @@
String want = joinLines(
"dropped_events: 0",
"events <",
+ " time_ms: 100",
+ " transport: 0",
" ip_reachability_event <",
" event_type: 512",
" if_name: \"wlan0\"",
" >",
- " time_ms: 100",
">",
"events <",
+ " time_ms: 200",
+ " transport: 0",
" dhcp_event <",
" duration_ms: 192",
- " error_code: 0",
" if_name: \"wlan0\"",
" state_transition: \"SomeState\"",
" >",
- " time_ms: 200",
">",
"events <",
+ " time_ms: 300",
+ " transport: 0",
" default_network_event <",
" network_id <",
" network_id: 102",
@@ -160,18 +189,19 @@
" transport_types: 2",
" transport_types: 3",
" >",
- " time_ms: 300",
">",
"events <",
+ " time_ms: 400",
+ " transport: 0",
" ip_provisioning_event <",
" event_type: 1",
" if_name: \"wlan0\"",
" latency_ms: 5678",
" >",
- " time_ms: 400",
">",
"events <",
" time_ms: 500",
+ " transport: 0",
" validation_probe_event <",
" latency_ms: 40730",
" network_id <",
@@ -182,6 +212,8 @@
" >",
">",
"events <",
+ " time_ms: 600",
+ " transport: 0",
" apf_statistics <",
" dropped_ras: 2",
" duration_ms: 45000",
@@ -192,9 +224,10 @@
" received_ras: 10",
" zero_lifetime_ras: 1",
" >",
- " time_ms: 600",
">",
"events <",
+ " time_ms: 700",
+ " transport: 0",
" ra_event <",
" dnssl_lifetime: -1",
" prefix_preferred_lifetime: 300",
@@ -203,7 +236,6 @@
" route_info_lifetime: -1",
" router_lifetime: 2000",
" >",
- " time_ms: 700",
">",
"version: 2");
@@ -232,8 +264,7 @@
try {
byte[] got = Base64.decode(output, Base64.DEFAULT);
IpConnectivityLogClass.IpConnectivityLog log =
- new IpConnectivityLogClass.IpConnectivityLog();
- MessageNano.mergeFrom(log, got);
+ IpConnectivityLogClass.IpConnectivityLog.parseFrom(got);
assertEquals(want, log.toString());
} catch (Exception e) {
fail(e.toString());
@@ -261,10 +292,5 @@
}
static final Comparator<ConnectivityMetricsEvent> EVENT_COMPARATOR =
- new Comparator<ConnectivityMetricsEvent>() {
- @Override
- public int compare(ConnectivityMetricsEvent ev1, ConnectivityMetricsEvent ev2) {
- return (int) (ev1.timestamp - ev2.timestamp);
- }
- };
+ Comparator.comparingLong((ev) -> ev.timestamp);
}
diff --git a/tests/net/java/com/android/server/connectivity/NetdEventListenerServiceTest.java b/tests/net/java/com/android/server/connectivity/NetdEventListenerServiceTest.java
index 2bb62bb..97afa60 100644
--- a/tests/net/java/com/android/server/connectivity/NetdEventListenerServiceTest.java
+++ b/tests/net/java/com/android/server/connectivity/NetdEventListenerServiceTest.java
@@ -16,25 +16,34 @@
package com.android.server.connectivity;
-import android.net.ConnectivityManager.NetworkCallback;
import android.net.ConnectivityManager;
+import android.net.ConnectivityManager.NetworkCallback;
import android.net.Network;
import android.net.metrics.DnsEvent;
import android.net.metrics.INetdEventListener;
import android.net.metrics.IpConnectivityLog;
import android.os.RemoteException;
+import android.system.OsConstants;
import android.test.suitebuilder.annotation.SmallTest;
-
+import com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityEvent;
+import java.io.FileOutputStream;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.stream.IntStream;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertTrue;
-
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.eq;
@@ -42,13 +51,6 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
-import java.io.FileOutputStream;
-import java.io.PrintWriter;
-import java.util.Arrays;
-import java.util.List;
-import java.util.OptionalInt;
-import java.util.stream.IntStream;
-
public class NetdEventListenerServiceTest extends TestCase {
// TODO: read from NetdEventListenerService after this constant is read from system property
@@ -68,45 +70,48 @@
}
}
+ private static final String EXAMPLE_IPV4 = "192.0.2.1";
+ private static final String EXAMPLE_IPV6 = "2001:db8:1200::2:1";
+
NetdEventListenerService mNetdEventListenerService;
@Mock ConnectivityManager mCm;
@Mock IpConnectivityLog mLog;
ArgumentCaptor<NetworkCallback> mCallbackCaptor;
- ArgumentCaptor<DnsEvent> mEvCaptor;
+ ArgumentCaptor<DnsEvent> mDnsEvCaptor;
public void setUp() {
MockitoAnnotations.initMocks(this);
mCallbackCaptor = ArgumentCaptor.forClass(NetworkCallback.class);
- mEvCaptor = ArgumentCaptor.forClass(DnsEvent.class);
+ mDnsEvCaptor = ArgumentCaptor.forClass(DnsEvent.class);
mNetdEventListenerService = new NetdEventListenerService(mCm, mLog);
verify(mCm, times(1)).registerNetworkCallback(any(), mCallbackCaptor.capture());
}
@SmallTest
- public void testOneBatch() throws Exception {
+ public void testOneDnsBatch() throws Exception {
log(105, LATENCIES);
log(106, Arrays.copyOf(LATENCIES, BATCH_SIZE - 1)); // one lookup short of a batch event
- verifyLoggedEvents(new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES));
+ verifyLoggedDnsEvents(new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES));
log(106, Arrays.copyOfRange(LATENCIES, BATCH_SIZE - 1, BATCH_SIZE));
- mEvCaptor = ArgumentCaptor.forClass(DnsEvent.class); // reset argument captor
- verifyLoggedEvents(
+ mDnsEvCaptor = ArgumentCaptor.forClass(DnsEvent.class); // reset argument captor
+ verifyLoggedDnsEvents(
new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
new DnsEvent(106, EVENT_TYPES, RETURN_CODES, LATENCIES));
}
@SmallTest
- public void testSeveralBatches() throws Exception {
+ public void testSeveralDmsBatches() throws Exception {
log(105, LATENCIES);
log(106, LATENCIES);
log(105, LATENCIES);
log(107, LATENCIES);
- verifyLoggedEvents(
+ verifyLoggedDnsEvents(
new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
new DnsEvent(106, EVENT_TYPES, RETURN_CODES, LATENCIES),
new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
@@ -114,7 +119,7 @@
}
@SmallTest
- public void testBatchAndNetworkLost() throws Exception {
+ public void testDnsBatchAndNetworkLost() throws Exception {
byte[] eventTypes = Arrays.copyOf(EVENT_TYPES, 20);
byte[] returnCodes = Arrays.copyOf(RETURN_CODES, 20);
int[] latencies = Arrays.copyOf(LATENCIES, 20);
@@ -124,14 +129,14 @@
mCallbackCaptor.getValue().onLost(new Network(105));
log(105, LATENCIES);
- verifyLoggedEvents(
+ verifyLoggedDnsEvents(
new DnsEvent(105, eventTypes, returnCodes, latencies),
new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES));
}
@SmallTest
- public void testConcurrentBatchesAndDumps() throws Exception {
+ public void testConcurrentDnsBatchesAndDumps() throws Exception {
final long stop = System.currentTimeMillis() + 100;
final PrintWriter pw = new PrintWriter(new FileOutputStream("/dev/null"));
new Thread() {
@@ -142,27 +147,120 @@
}
}.start();
- logAsync(105, LATENCIES);
- logAsync(106, LATENCIES);
- logAsync(107, LATENCIES);
+ logDnsAsync(105, LATENCIES);
+ logDnsAsync(106, LATENCIES);
+ logDnsAsync(107, LATENCIES);
- verifyLoggedEvents(500,
+ verifyLoggedDnsEvents(500,
new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
new DnsEvent(106, EVENT_TYPES, RETURN_CODES, LATENCIES),
new DnsEvent(107, EVENT_TYPES, RETURN_CODES, LATENCIES));
}
@SmallTest
- public void testConcurrentBatchesAndNetworkLoss() throws Exception {
- logAsync(105, LATENCIES);
+ public void testConcurrentDnsBatchesAndNetworkLoss() throws Exception {
+ logDnsAsync(105, LATENCIES);
Thread.sleep(10L);
- // call onLost() asynchronously to logAsync's onDnsEvent() calls.
+ // call onLost() asynchronously to logDnsAsync's onDnsEvent() calls.
mCallbackCaptor.getValue().onLost(new Network(105));
// do not verify unpredictable batch
verify(mLog, timeout(500).times(1)).log(any());
}
+ @SmallTest
+ public void testConnectLogging() throws Exception {
+ final int OK = 0;
+ Thread[] logActions = {
+ // ignored
+ connectEventAction(OsConstants.EALREADY, 0, EXAMPLE_IPV4),
+ connectEventAction(OsConstants.EALREADY, 0, EXAMPLE_IPV6),
+ connectEventAction(OsConstants.EINPROGRESS, 0, EXAMPLE_IPV4),
+ connectEventAction(OsConstants.EINPROGRESS, 0, EXAMPLE_IPV6),
+ connectEventAction(OsConstants.EINPROGRESS, 0, EXAMPLE_IPV6),
+ // valid latencies
+ connectEventAction(OK, 110, EXAMPLE_IPV4),
+ connectEventAction(OK, 23, EXAMPLE_IPV4),
+ connectEventAction(OK, 45, EXAMPLE_IPV4),
+ connectEventAction(OK, 56, EXAMPLE_IPV4),
+ connectEventAction(OK, 523, EXAMPLE_IPV6),
+ connectEventAction(OK, 214, EXAMPLE_IPV6),
+ connectEventAction(OK, 67, EXAMPLE_IPV6),
+ // errors
+ connectEventAction(OsConstants.EPERM, 0, EXAMPLE_IPV4),
+ connectEventAction(OsConstants.EPERM, 0, EXAMPLE_IPV4),
+ connectEventAction(OsConstants.EAGAIN, 0, EXAMPLE_IPV4),
+ connectEventAction(OsConstants.EACCES, 0, EXAMPLE_IPV4),
+ connectEventAction(OsConstants.EACCES, 0, EXAMPLE_IPV4),
+ connectEventAction(OsConstants.EACCES, 0, EXAMPLE_IPV6),
+ connectEventAction(OsConstants.EADDRINUSE, 0, EXAMPLE_IPV4),
+ connectEventAction(OsConstants.ETIMEDOUT, 0, EXAMPLE_IPV4),
+ connectEventAction(OsConstants.ETIMEDOUT, 0, EXAMPLE_IPV6),
+ connectEventAction(OsConstants.ETIMEDOUT, 0, EXAMPLE_IPV6),
+ connectEventAction(OsConstants.ECONNREFUSED, 0, EXAMPLE_IPV4),
+ };
+
+ for (Thread t : logActions) {
+ t.start();
+ }
+ for (Thread t : logActions) {
+ t.join();
+ }
+
+ List<IpConnectivityEvent> events = new ArrayList<>();
+ mNetdEventListenerService.flushStatistics(events);
+
+ IpConnectivityEvent got = events.get(0);
+ String want = joinLines(
+ "time_ms: 0",
+ "transport: 0",
+ "connect_statistics <",
+ " connect_count: 12",
+ " errnos_counters <",
+ " key: 1",
+ " value: 2",
+ " >",
+ " errnos_counters <",
+ " key: 11",
+ " value: 1",
+ " >",
+ " errnos_counters <",
+ " key: 13",
+ " value: 3",
+ " >",
+ " errnos_counters <",
+ " key: 98",
+ " value: 1",
+ " >",
+ " errnos_counters <",
+ " key: 110",
+ " value: 3",
+ " >",
+ " errnos_counters <",
+ " key: 111",
+ " value: 1",
+ " >",
+ " ipv6_addr_count: 6",
+ " latencies_ms: 23",
+ " latencies_ms: 45",
+ " latencies_ms: 56",
+ " latencies_ms: 67",
+ " latencies_ms: 110",
+ " latencies_ms: 214",
+ " latencies_ms: 523");
+ verifyConnectEvent(want, got);
+ }
+
+ Thread connectEventAction(int error, int latencyMs, String ipAddr) {
+ return new Thread(() -> {
+ try {
+ mNetdEventListenerService.onConnectEvent(100, error, latencyMs, ipAddr, 80, 1);
+ } catch (Exception e) {
+ fail(e.toString());
+ }
+ });
+ }
+
void log(int netId, int[] latencies) {
try {
for (int l : latencies) {
@@ -174,7 +272,7 @@
}
}
- void logAsync(int netId, int[] latencies) {
+ void logDnsAsync(int netId, int[] latencies) {
new Thread() {
public void run() {
log(netId, latencies);
@@ -182,15 +280,15 @@
}.start();
}
- void verifyLoggedEvents(DnsEvent... expected) {
- verifyLoggedEvents(0, expected);
+ void verifyLoggedDnsEvents(DnsEvent... expected) {
+ verifyLoggedDnsEvents(0, expected);
}
- void verifyLoggedEvents(int wait, DnsEvent... expectedEvents) {
- verify(mLog, timeout(wait).times(expectedEvents.length)).log(mEvCaptor.capture());
- for (DnsEvent got : mEvCaptor.getAllValues()) {
+ void verifyLoggedDnsEvents(int wait, DnsEvent... expectedEvents) {
+ verify(mLog, timeout(wait).times(expectedEvents.length)).log(mDnsEvCaptor.capture());
+ for (DnsEvent got : mDnsEvCaptor.getAllValues()) {
OptionalInt index = IntStream.range(0, expectedEvents.length)
- .filter(i -> eventsEqual(expectedEvents[i], got))
+ .filter(i -> dnsEventsEqual(expectedEvents[i], got))
.findFirst();
// Don't match same expected event more than once.
index.ifPresent(i -> expectedEvents[i] = null);
@@ -199,11 +297,30 @@
}
/** equality function for DnsEvent to avoid overriding equals() and hashCode(). */
- static boolean eventsEqual(DnsEvent expected, DnsEvent got) {
+ static boolean dnsEventsEqual(DnsEvent expected, DnsEvent got) {
return (expected == got) || ((expected != null) && (got != null)
&& (expected.netId == got.netId)
&& Arrays.equals(expected.eventTypes, got.eventTypes)
&& Arrays.equals(expected.returnCodes, got.returnCodes)
&& Arrays.equals(expected.latenciesMs, got.latenciesMs));
}
+
+ static String joinLines(String ... elems) {
+ StringBuilder b = new StringBuilder();
+ for (String s : elems) {
+ b.append(s).append("\n");
+ }
+ return b.toString();
+ }
+
+ static void verifyConnectEvent(String expected, IpConnectivityEvent got) {
+ try {
+ Arrays.sort(got.connectStatistics.latenciesMs);
+ Arrays.sort(got.connectStatistics.errnosCounters,
+ Comparator.comparingInt((p) -> p.key));
+ assertEquals(expected, got.toString());
+ } catch (Exception e) {
+ fail(e.toString());
+ }
+ }
}