Add test for NetworkRequest.Builder(NetworkRequest)
Build NetworkRequest using builder from an existing
NetworkRequest is exposed to API surface, so add a test to
verify its behavior works as expectation.
Also, create a TestUtils in the cts to put the multiple
reference methods(shouldTestSApis) to prevent duplication.
Bug: 186061922
Test: atest CtsNetTestCases:android.net.cts.NetworkRequestTest
in current sdk
Test: atest CtsNetTestCasesLatestSdk:android.net.cts.NetworkRequestTest
in R and current sdk
Test: atest android.net.cts.ConnectivityManagerTest#\
testRegisterNetworkCallback
Change-Id: I46318e212d51e4e13efa86e8b9c3ca7326d62b77
This commit is contained in:
@@ -131,9 +131,7 @@ import androidx.test.InstrumentationRegistry;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import com.android.internal.util.ArrayUtils;
|
||||
import com.android.modules.utils.build.SdkLevel;
|
||||
import com.android.networkstack.apishim.ConnectivityManagerShimImpl;
|
||||
import com.android.networkstack.apishim.ConstantsShim;
|
||||
import com.android.networkstack.apishim.common.ConnectivityManagerShim;
|
||||
import com.android.testutils.CompatUtil;
|
||||
import com.android.testutils.DevSdkIgnoreRule;
|
||||
@@ -312,7 +310,7 @@ public class ConnectivityManagerTest {
|
||||
mCtsNetUtils.disconnectFromCell();
|
||||
}
|
||||
|
||||
if (shouldTestSApis()) {
|
||||
if (TestUtils.shouldTestSApis()) {
|
||||
runWithShellPermissionIdentity(
|
||||
() -> mCmShim.setRequireVpnForUids(false, mVpnRequiredUidRanges),
|
||||
NETWORK_SETTINGS);
|
||||
@@ -605,7 +603,7 @@ public class ConnectivityManagerTest {
|
||||
final TestNetworkCallback systemDefaultCallback = new TestNetworkCallback();
|
||||
final TestNetworkCallback perUidCallback = new TestNetworkCallback();
|
||||
final Handler h = new Handler(Looper.getMainLooper());
|
||||
if (shouldTestSApis()) {
|
||||
if (TestUtils.shouldTestSApis()) {
|
||||
runWithShellPermissionIdentity(() -> {
|
||||
mCmShim.registerSystemDefaultNetworkCallback(systemDefaultCallback, h);
|
||||
mCmShim.registerDefaultNetworkCallbackAsUid(Process.myUid(), perUidCallback, h);
|
||||
@@ -628,7 +626,7 @@ public class ConnectivityManagerTest {
|
||||
assertNotNull("Did not receive onAvailable on default network callback",
|
||||
defaultNetwork);
|
||||
|
||||
if (shouldTestSApis()) {
|
||||
if (TestUtils.shouldTestSApis()) {
|
||||
assertNotNull("Did not receive onAvailable on system default network callback",
|
||||
systemDefaultCallback.waitForAvailable());
|
||||
final Network perUidNetwork = perUidCallback.waitForAvailable();
|
||||
@@ -642,7 +640,7 @@ public class ConnectivityManagerTest {
|
||||
} finally {
|
||||
mCm.unregisterNetworkCallback(callback);
|
||||
mCm.unregisterNetworkCallback(defaultTrackingCallback);
|
||||
if (shouldTestSApis()) {
|
||||
if (TestUtils.shouldTestSApis()) {
|
||||
mCm.unregisterNetworkCallback(systemDefaultCallback);
|
||||
mCm.unregisterNetworkCallback(perUidCallback);
|
||||
}
|
||||
@@ -1670,7 +1668,7 @@ public class ConnectivityManagerTest {
|
||||
|
||||
final Network network = mCtsNetUtils.ensureWifiConnected();
|
||||
final String ssid = unquoteSSID(mWifiManager.getConnectionInfo().getSSID());
|
||||
assertNotNull("Ssid getting from WiifManager is null", ssid);
|
||||
assertNotNull("Ssid getting from WifiManager is null", ssid);
|
||||
// This package should have no NETWORK_SETTINGS permission. Verify that no ssid is contained
|
||||
// in the NetworkCapabilities.
|
||||
verifySsidFromQueriedNetworkCapabilities(network, ssid, false /* hasSsid */);
|
||||
@@ -1721,7 +1719,7 @@ public class ConnectivityManagerTest {
|
||||
public void testRequestBackgroundNetwork() {
|
||||
// Cannot use @IgnoreUpTo(Build.VERSION_CODES.R) because this test also requires API 31
|
||||
// shims, and @IgnoreUpTo does not check that.
|
||||
assumeTrue(shouldTestSApis());
|
||||
assumeTrue(TestUtils.shouldTestSApis());
|
||||
|
||||
// Create a tun interface. Use the returned interface name as the specifier to create
|
||||
// a test network request.
|
||||
@@ -1866,7 +1864,7 @@ public class ConnectivityManagerTest {
|
||||
public void testBlockedStatusCallback() {
|
||||
// Cannot use @IgnoreUpTo(Build.VERSION_CODES.R) because this test also requires API 31
|
||||
// shims, and @IgnoreUpTo does not check that.
|
||||
assumeTrue(shouldTestSApis());
|
||||
assumeTrue(TestUtils.shouldTestSApis());
|
||||
runWithShellPermissionIdentity(() -> doTestBlockedStatusCallback(), NETWORK_SETTINGS);
|
||||
}
|
||||
|
||||
@@ -1900,15 +1898,7 @@ public class ConnectivityManagerTest {
|
||||
public void testLegacyLockdownEnabled() {
|
||||
// Cannot use @IgnoreUpTo(Build.VERSION_CODES.R) because this test also requires API 31
|
||||
// shims, and @IgnoreUpTo does not check that.
|
||||
assumeTrue(shouldTestSApis());
|
||||
assumeTrue(TestUtils.shouldTestSApis());
|
||||
runWithShellPermissionIdentity(() -> doTestLegacyLockdownEnabled(), NETWORK_SETTINGS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to test S+ APIs. This requires a) that the test be running on an S+ device, and
|
||||
* b) that the code be compiled against shims new enough to access these APIs.
|
||||
*/
|
||||
private boolean shouldTestSApis() {
|
||||
return SdkLevel.isAtLeastS() && ConstantsShim.VERSION > Build.VERSION_CODES.R;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,10 +28,13 @@ import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
|
||||
import static android.net.NetworkCapabilities.TRANSPORT_VPN;
|
||||
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
|
||||
|
||||
import static junit.framework.Assert.fail;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.net.MacAddress;
|
||||
@@ -396,4 +399,42 @@ public class NetworkRequestTest {
|
||||
.addCapability(NET_CAPABILITY_DUN).build();
|
||||
assertTrue(dunRequest.hasCapability(ConstantsShim.NET_CAPABILITY_NOT_VCN_MANAGED));
|
||||
}
|
||||
|
||||
private void verifyEqualRequestBuilt(NetworkRequest orig) {
|
||||
try {
|
||||
final NetworkRequestShim shim = NetworkRequestShimImpl.newInstance();
|
||||
final NetworkRequest copy = shim.newBuilder(orig).build();
|
||||
assertEquals(orig, copy);
|
||||
} catch (UnsupportedApiLevelException e) {
|
||||
fail("NetworkRequestShim.newBuilder should be supported in this SDK version");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildRequestFromExistingRequestWithBuilder() {
|
||||
assumeTrue(TestUtils.shouldTestSApis());
|
||||
final NetworkRequest.Builder builder = new NetworkRequest.Builder();
|
||||
|
||||
final NetworkRequest baseRequest = builder.build();
|
||||
verifyEqualRequestBuilt(baseRequest);
|
||||
|
||||
final NetworkRequest requestCellMms = builder
|
||||
.addTransportType(TRANSPORT_CELLULAR)
|
||||
.addCapability(NET_CAPABILITY_MMS)
|
||||
.setSignalStrength(-99).build();
|
||||
verifyEqualRequestBuilt(requestCellMms);
|
||||
|
||||
final WifiNetworkSpecifier specifier = new WifiNetworkSpecifier.Builder()
|
||||
.setSsidPattern(new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_LITERAL))
|
||||
.setBssidPattern(ARBITRARY_ADDRESS, ARBITRARY_ADDRESS)
|
||||
.build();
|
||||
final NetworkRequest requestWifi = builder
|
||||
.addTransportType(TRANSPORT_WIFI)
|
||||
.removeTransportType(TRANSPORT_CELLULAR)
|
||||
.addCapability(NET_CAPABILITY_INTERNET)
|
||||
.removeCapability(NET_CAPABILITY_MMS)
|
||||
.setNetworkSpecifier(specifier)
|
||||
.setSignalStrength(-33).build();
|
||||
verifyEqualRequestBuilt(requestWifi);
|
||||
}
|
||||
}
|
||||
|
||||
36
tests/cts/net/src/android/net/cts/TestUtils.java
Normal file
36
tests/cts/net/src/android/net/cts/TestUtils.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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.cts;
|
||||
|
||||
import android.os.Build;
|
||||
|
||||
import com.android.modules.utils.build.SdkLevel;
|
||||
import com.android.networkstack.apishim.ConstantsShim;
|
||||
|
||||
/**
|
||||
* Utils class to provide common shared test helper methods or constants that behave different
|
||||
* against compiled sdk.
|
||||
*/
|
||||
public class TestUtils {
|
||||
/**
|
||||
* Whether to test S+ APIs. This requires a) that the test be running on an S+ device, and
|
||||
* b) that the code be compiled against shims new enough to access these APIs.
|
||||
*/
|
||||
public static boolean shouldTestSApis() {
|
||||
return SdkLevel.isAtLeastS() && ConstantsShim.VERSION > Build.VERSION_CODES.R;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user