From 3a7d92513fa4fce0c9ecaba53f3e6ae8f8662782 Mon Sep 17 00:00:00 2001 From: junyulai Date: Wed, 14 Apr 2021 11:05:59 +0800 Subject: [PATCH] Fix CTS fail to register network callback since lack of permission Currently, NetworkCallbackTest gets the capabilities of current active network to track the network used for the tests. And construct the request with the capabilities gets from the network. This causes test fails if the capabilities contain signal strength, since it requires NETWORK_SIGNAL_STRENGTH_WAKEUP to be declared. Thus, this patch null out several fields from capabilities of the current active network to construct the new request in order to prevent over-specifying the requirement. Test: atest CtsHostsideNetworkTests:com.android.cts.net.HostsideNetworkCallbackTests Bug: 182516128 Change-Id: Ib79b38e63ad2083c29fd71a24b8686b3f6f4e12f --- .../com/android/cts/net/hostside/NetworkCallbackTest.java | 8 ++++++-- .../src/com/android/cts/net/hostside/app2/MyService.java | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/NetworkCallbackTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/NetworkCallbackTest.java index 36e2ffea08..0715e32bd1 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/NetworkCallbackTest.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/NetworkCallbackTest.java @@ -17,6 +17,7 @@ package com.android.cts.net.hostside; import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; +import static android.net.NetworkCapabilities.SIGNAL_STRENGTH_UNSPECIFIED; import static com.android.cts.net.hostside.NetworkPolicyTestUtils.canChangeActiveNetworkMeteredness; import static com.android.cts.net.hostside.NetworkPolicyTestUtils.getActiveNetworkCapabilities; @@ -204,9 +205,12 @@ public class NetworkCallbackTest extends AbstractRestrictBackgroundNetworkTestCa // Mark network as metered. mMeterednessConfiguration.configureNetworkMeteredness(true); - // Register callback + // Register callback, copy the capabilities from the active network to expect the "original" + // network before disconnecting, but null out some fields to prevent over-specified. registerNetworkCallback(new NetworkRequest.Builder() - .setCapabilities(networkCapabilities).build(), mTestNetworkCallback); + .setCapabilities(networkCapabilities.setTransportInfo(null)) + .removeCapability(NET_CAPABILITY_NOT_METERED) + .setSignalStrength(SIGNAL_STRENGTH_UNSPECIFIED).build(), mTestNetworkCallback); // Wait for onAvailable() callback to ensure network is available before the test // and store the default network. mNetwork = mTestNetworkCallback.expectAvailableCallbackAndGetNetwork(); diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java index 8a5e00fdaf..717ccb107b 100644 --- a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java +++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java @@ -95,7 +95,7 @@ public class MyService extends Service { Log.d(TAG, "unregister previous network callback: " + mNetworkCallback); unregisterNetworkCallback(); } - Log.d(TAG, "registering network callback"); + Log.d(TAG, "registering network callback for " + request); mNetworkCallback = new ConnectivityManager.NetworkCallback() { @Override