Merge "Check network slicing declaration for network request"

This commit is contained in:
Yuyang Huang
2023-03-01 02:13:31 +00:00
committed by Gerrit Code Review
19 changed files with 1078 additions and 0 deletions

View File

@@ -183,6 +183,8 @@ import static com.android.testutils.RecorderCallback.CallbackEntry.SUSPENDED;
import static com.android.testutils.RecorderCallback.CallbackEntry.UNAVAILABLE;
import static com.android.testutils.TestPermissionUtil.runAsShell;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
@@ -228,6 +230,7 @@ import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.admin.DevicePolicyManager;
import android.app.usage.NetworkStatsManager;
import android.compat.testing.PlatformCompatChangeRule;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentProvider;
@@ -312,6 +315,7 @@ import android.net.UnderlyingNetworkInfo;
import android.net.Uri;
import android.net.VpnManager;
import android.net.VpnTransportInfo;
import android.net.connectivity.ConnectivityCompatChanges;
import android.net.metrics.IpConnectivityLog;
import android.net.netd.aidl.NativeUidRangeConfig;
import android.net.networkstack.NetworkStackClientBase;
@@ -379,6 +383,7 @@ import com.android.networkstack.apishim.common.UnsupportedApiLevelException;
import com.android.server.ConnectivityService.ConnectivityDiagnosticsCallbackInfo;
import com.android.server.ConnectivityService.NetworkRequestInfo;
import com.android.server.ConnectivityServiceTest.ConnectivityServiceDependencies.ReportedInterfaces;
import com.android.server.connectivity.ApplicationSelfCertifiedNetworkCapabilities;
import com.android.server.connectivity.AutomaticOnOffKeepaliveTracker;
import com.android.server.connectivity.CarrierPrivilegeAuthenticator;
import com.android.server.connectivity.ClatCoordinator;
@@ -406,6 +411,9 @@ import com.android.testutils.RecorderCallback.CallbackEntry;
import com.android.testutils.TestableNetworkCallback;
import com.android.testutils.TestableNetworkOfferCallback;
import libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges;
import libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@@ -475,6 +483,9 @@ public class ConnectivityServiceTest {
@Rule
public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule();
@Rule
public final PlatformCompatChangeRule compatChangeRule = new PlatformCompatChangeRule();
private static final int TIMEOUT_MS = 2_000;
// Broadcasts can take a long time to be delivered. The test will not wait for that long unless
// there is a failure, so use a long timeout.
@@ -2105,6 +2116,37 @@ public class ConnectivityServiceTest {
reset(mBroadcastOptionsShim);
return mBroadcastOptionsShim;
}
@GuardedBy("this")
private boolean mForceDisableCompatChangeCheck = true;
/**
* By default, the {@link #isChangeEnabled(long, String, UserHandle)} will always return
* true as the mForceDisableCompatChangeCheck is true and compat change check logic is
* never executed. The compat change check logic can be turned on by calling this method.
* If this method is called, the
* {@link libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges} or
* {@link libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges} must be
* used to turn on/off the compat change flag.
*/
private void enableCompatChangeCheck() {
synchronized (this) {
mForceDisableCompatChangeCheck = false;
}
}
@Override
public boolean isChangeEnabled(long changeId,
@NonNull final String packageName,
@NonNull final UserHandle user) {
synchronized (this) {
if (mForceDisableCompatChangeCheck) {
return false;
} else {
return super.isChangeEnabled(changeId, packageName, user);
}
}
}
}
private class AutomaticOnOffKeepaliveTrackerDependencies
@@ -6310,6 +6352,142 @@ public class ConnectivityServiceTest {
}
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
@DisableCompatChanges(ConnectivityCompatChanges.ENABLE_SELF_CERTIFIED_CAPABILITIES_DECLARATION)
public void testSelfCertifiedCapabilitiesDisabled()
throws Exception {
mDeps.enableCompatChangeCheck();
final NetworkRequest networkRequest = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY)
.addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_BANDWIDTH)
.build();
final TestNetworkCallback cb = new TestNetworkCallback();
mCm.requestNetwork(networkRequest, cb);
mCm.unregisterNetworkCallback(cb);
}
/** Set the networkSliceResourceId to 0 will result in NameNotFoundException be thrown. */
private void setupMockForNetworkCapabilitiesResources(int networkSliceResourceId)
throws PackageManager.NameNotFoundException {
if (networkSliceResourceId == 0) {
doThrow(new PackageManager.NameNotFoundException()).when(mPackageManager).getProperty(
ConstantsShim.PROPERTY_SELF_CERTIFIED_NETWORK_CAPABILITIES,
mContext.getPackageName());
} else {
final PackageManager.Property property = new PackageManager.Property(
ConstantsShim.PROPERTY_SELF_CERTIFIED_NETWORK_CAPABILITIES,
networkSliceResourceId,
true /* isResource */,
mContext.getPackageName(),
"dummyClass"
);
doReturn(property).when(mPackageManager).getProperty(
ConstantsShim.PROPERTY_SELF_CERTIFIED_NETWORK_CAPABILITIES,
mContext.getPackageName());
doReturn(mContext.getResources()).when(mPackageManager).getResourcesForApplication(
mContext.getPackageName());
}
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
@EnableCompatChanges(ConnectivityCompatChanges.ENABLE_SELF_CERTIFIED_CAPABILITIES_DECLARATION)
public void requestNetwork_withoutPrioritizeBandwidthDeclaration_shouldThrowException()
throws Exception {
mDeps.enableCompatChangeCheck();
setupMockForNetworkCapabilitiesResources(
com.android.frameworks.tests.net.R.xml.self_certified_capabilities_latency);
final NetworkRequest networkRequest = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_BANDWIDTH)
.build();
final TestNetworkCallback cb = new TestNetworkCallback();
final Exception e = assertThrows(SecurityException.class,
() -> mCm.requestNetwork(networkRequest, cb));
assertThat(e.getMessage(),
containsString(ApplicationSelfCertifiedNetworkCapabilities.PRIORITIZE_BANDWIDTH));
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
@EnableCompatChanges(ConnectivityCompatChanges.ENABLE_SELF_CERTIFIED_CAPABILITIES_DECLARATION)
public void requestNetwork_withoutPrioritizeLatencyDeclaration_shouldThrowException()
throws Exception {
mDeps.enableCompatChangeCheck();
setupMockForNetworkCapabilitiesResources(
com.android.frameworks.tests.net.R.xml.self_certified_capabilities_bandwidth);
final NetworkRequest networkRequest = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY)
.build();
final TestNetworkCallback cb = new TestNetworkCallback();
final Exception e = assertThrows(SecurityException.class,
() -> mCm.requestNetwork(networkRequest, cb));
assertThat(e.getMessage(),
containsString(ApplicationSelfCertifiedNetworkCapabilities.PRIORITIZE_LATENCY));
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
@EnableCompatChanges(ConnectivityCompatChanges.ENABLE_SELF_CERTIFIED_CAPABILITIES_DECLARATION)
public void requestNetwork_withoutNetworkSliceProperty_shouldThrowException() throws Exception {
mDeps.enableCompatChangeCheck();
setupMockForNetworkCapabilitiesResources(0 /* networkSliceResourceId */);
final NetworkRequest networkRequest = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY)
.addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_BANDWIDTH)
.build();
final TestNetworkCallback cb = new TestNetworkCallback();
final Exception e = assertThrows(SecurityException.class,
() -> mCm.requestNetwork(networkRequest, cb));
assertThat(e.getMessage(),
containsString(ConstantsShim.PROPERTY_SELF_CERTIFIED_NETWORK_CAPABILITIES));
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
@EnableCompatChanges(ConnectivityCompatChanges.ENABLE_SELF_CERTIFIED_CAPABILITIES_DECLARATION)
public void requestNetwork_withNetworkSliceDeclaration_shouldSucceed() throws Exception {
mDeps.enableCompatChangeCheck();
setupMockForNetworkCapabilitiesResources(
com.android.frameworks.tests.net.R.xml.self_certified_capabilities_both);
final NetworkRequest networkRequest = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY)
.addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_BANDWIDTH)
.build();
final TestNetworkCallback cb = new TestNetworkCallback();
mCm.requestNetwork(networkRequest, cb);
mCm.unregisterNetworkCallback(cb);
}
@Test
@IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
@EnableCompatChanges(ConnectivityCompatChanges.ENABLE_SELF_CERTIFIED_CAPABILITIES_DECLARATION)
public void requestNetwork_withNetworkSliceDeclaration_shouldUseCache() throws Exception {
mDeps.enableCompatChangeCheck();
setupMockForNetworkCapabilitiesResources(
com.android.frameworks.tests.net.R.xml.self_certified_capabilities_both);
final NetworkRequest networkRequest = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY)
.addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_BANDWIDTH)
.build();
final TestNetworkCallback cb = new TestNetworkCallback();
mCm.requestNetwork(networkRequest, cb);
mCm.unregisterNetworkCallback(cb);
// Second call should use caches
mCm.requestNetwork(networkRequest, cb);
mCm.unregisterNetworkCallback(cb);
// PackageManager's API only called once because the second call is using cache.
verify(mPackageManager, times(1)).getProperty(
ConstantsShim.PROPERTY_SELF_CERTIFIED_NETWORK_CAPABILITIES,
mContext.getPackageName());
verify(mPackageManager, times(1)).getResourcesForApplication(
mContext.getPackageName());
}
/**
* Validate the service throws if request with CBS but without carrier privilege.
*/

View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2023 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.net.NetworkCapabilities
import android.os.Build
import androidx.test.InstrumentationRegistry
import androidx.test.filters.SmallTest
import com.android.frameworks.tests.net.R
import com.android.testutils.DevSdkIgnoreRule
import com.android.testutils.DevSdkIgnoreRunner
import com.google.common.truth.Truth.assertThat
import kotlin.test.assertFailsWith
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(DevSdkIgnoreRunner::class)
@SmallTest
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
class ApplicationSelfCertifiedNetworkCapabilitiesTest {
private val mResource = InstrumentationRegistry.getContext().getResources()
private val bandwidthCapability = NetworkCapabilities.Builder().apply {
addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_BANDWIDTH)
}.build()
private val latencyCapability = NetworkCapabilities.Builder().apply {
addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY)
}.build()
private val emptyCapability = NetworkCapabilities.Builder().build()
private val bothCapabilities = NetworkCapabilities.Builder().apply {
addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_BANDWIDTH)
addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY)
}.build()
@Test
fun parseXmlWithWrongTag_shouldIgnoreWrongTag() {
val parser = mResource.getXml(
R.xml.self_certified_capabilities_wrong_tag
)
val selfDeclaredCaps = ApplicationSelfCertifiedNetworkCapabilities.createFromXml(parser)
selfDeclaredCaps.enforceSelfCertifiedNetworkCapabilitiesDeclared(latencyCapability)
selfDeclaredCaps.enforceSelfCertifiedNetworkCapabilitiesDeclared(bandwidthCapability)
}
@Test
fun parseXmlWithWrongDeclaration_shouldThrowException() {
val parser = mResource.getXml(
R.xml.self_certified_capabilities_wrong_declaration
)
val exception = assertFailsWith<InvalidTagException> {
ApplicationSelfCertifiedNetworkCapabilities.createFromXml(parser)
}
assertThat(exception.message).contains("network-capabilities-declaration1")
}
@Test
fun checkIfSelfCertifiedNetworkCapabilitiesDeclared_shouldThrowExceptionWhenNoDeclaration() {
val parser = mResource.getXml(R.xml.self_certified_capabilities_other)
val selfDeclaredCaps = ApplicationSelfCertifiedNetworkCapabilities.createFromXml(parser)
val exception1 = assertFailsWith<SecurityException> {
selfDeclaredCaps.enforceSelfCertifiedNetworkCapabilitiesDeclared(latencyCapability)
}
assertThat(exception1.message).contains(
ApplicationSelfCertifiedNetworkCapabilities.PRIORITIZE_LATENCY
)
val exception2 = assertFailsWith<SecurityException> {
selfDeclaredCaps.enforceSelfCertifiedNetworkCapabilitiesDeclared(bandwidthCapability)
}
assertThat(exception2.message).contains(
ApplicationSelfCertifiedNetworkCapabilities.PRIORITIZE_BANDWIDTH
)
}
@Test
fun checkIfSelfCertifiedNetworkCapabilitiesDeclared_shouldPassIfDeclarationExist() {
val parser = mResource.getXml(R.xml.self_certified_capabilities_both)
val selfDeclaredCaps = ApplicationSelfCertifiedNetworkCapabilities.createFromXml(parser)
selfDeclaredCaps.enforceSelfCertifiedNetworkCapabilitiesDeclared(latencyCapability)
selfDeclaredCaps.enforceSelfCertifiedNetworkCapabilitiesDeclared(bandwidthCapability)
selfDeclaredCaps.enforceSelfCertifiedNetworkCapabilitiesDeclared(bothCapabilities)
selfDeclaredCaps.enforceSelfCertifiedNetworkCapabilitiesDeclared(emptyCapability)
}
}