From 4df8a7aef18e44089838f94fa51afe2a74af3b5e Mon Sep 17 00:00:00 2001 From: Patrick Rohr Date: Mon, 11 Jan 2021 09:47:04 +0100 Subject: [PATCH] Add CTS test for Restricted Networking Mode Test: atest com.android.cts.net.HostsideRestrictBackgroundNetworkTests Bug: 175281879 Bug: 170323671 Change-Id: Ic69e7e029debeea1f131242fb2baad2796d4768c --- ...ractRestrictBackgroundNetworkTestCase.java | 6 +- .../cts/net/hostside/RestrictedModeTest.java | 68 +++++++++++++++++++ ...ostsideRestrictBackgroundNetworkTests.java | 8 +++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 tests/cts/hostside/app/src/com/android/cts/net/hostside/RestrictedModeTest.java diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java index 71f6f2f5f0..f423503c56 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java @@ -234,12 +234,16 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { } protected void assertForegroundNetworkAccess() throws Exception { + assertForegroundNetworkAccess(true); + } + + protected void assertForegroundNetworkAccess(boolean expectAllowed) throws Exception { assertForegroundState(); // We verified that app is in foreground state but if the screen turns-off while // verifying for network access, the app will go into background state (in case app's // foreground status was due to top activity). So, turn the screen on when verifying // network connectivity. - assertNetworkAccess(true /* expectAvailable */, true /* needScreenOn */); + assertNetworkAccess(expectAllowed /* expectAvailable */, true /* needScreenOn */); } protected void assertForegroundServiceNetworkAccess() throws Exception { diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/RestrictedModeTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/RestrictedModeTest.java new file mode 100644 index 0000000000..29d3c6e1ba --- /dev/null +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/RestrictedModeTest.java @@ -0,0 +1,68 @@ +/* + * 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 com.android.cts.net.hostside; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public final class RestrictedModeTest extends AbstractRestrictBackgroundNetworkTestCase { + @Before + public void setUp() throws Exception { + super.setUp(); + } + + @After + public void tearDown() throws Exception { + setRestrictedMode(false); + super.tearDown(); + } + + private void setRestrictedMode(boolean enabled) throws Exception { + executeSilentShellCommand( + "settings put global restricted_networking_mode " + (enabled ? 1 : 0)); + assertRestrictedModeState(enabled); + } + + private void assertRestrictedModeState(boolean enabled) throws Exception { + assertDelayedShellCommand("cmd netpolicy get restricted-mode", + "Restricted mode status: " + (enabled ? "enabled" : "disabled")); + } + + @Test + public void testNetworkAccess() throws Exception { + setRestrictedMode(false); + + // go to foreground state and enable restricted mode + launchComponentAndAssertNetworkAccess(TYPE_COMPONENT_ACTIVTIY); + setRestrictedMode(true); + assertForegroundNetworkAccess(false); + + // go to background state + finishActivity(); + assertBackgroundNetworkAccess(false); + + // disable restricted mode and assert network access in foreground and background states + setRestrictedMode(false); + launchComponentAndAssertNetworkAccess(TYPE_COMPONENT_ACTIVTIY); + assertForegroundNetworkAccess(true); + + // go to background state + finishActivity(); + assertBackgroundNetworkAccess(true); + } +} diff --git a/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java b/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java index ac28c7ab63..a629ccf65e 100644 --- a/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java +++ b/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java @@ -311,6 +311,14 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC "testAppIdleAndBatterySaver_tempPowerSaveAndAppIdleWhitelists"); } + /************************** + * Restricted mode tests. * + **************************/ + public void testRestrictedMode_networkAccess() throws Exception { + runDeviceTests(TEST_PKG, TEST_PKG + ".RestrictedModeTest", + "testNetworkAccess"); + } + /******************* * Helper methods. * *******************/