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
This commit is contained in:
junyulai 2021-04-14 11:05:59 +08:00
commit 3a7d92513f
2 changed files with 7 additions and 3 deletions

View file

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

View file

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