diff options
author | Sudheer Shanka <sudheersai@google.com> | 2021-07-20 12:28:08 +0000 |
---|---|---|
committer | Sudheer Shanka <sudheersai@google.com> | 2021-08-11 17:50:14 +0000 |
commit | e2b820eec968f4cfaa334959d00c9a47abfa526e (patch) | |
tree | 2c33ab845875f6bf1cccf7753fc58b96b0ee579d | |
parent | 484fc6a81df7e3fc110dbdb94dc698dc6e494f23 (diff) | |
download | cts-e2b820eec968f4cfaa334959d00c9a47abfa526e.tar.gz |
Verify apps cannot receive ACTION_SNOOZE_WARNING broadcast.
Bug: 177931370
Test: atest tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java
Ignore-AOSP-First: Platform changes related to this are not in AOSP yet.
Change-Id: I6d525c4d88ba01838d51a873251593281b628898
Merged-In: I6d525c4d88ba01838d51a873251593281b628898
Merged-In: I19b6b09febeb62867cd3d26246b2b3b46b9422c6
Merged-In: I89d4791fddb419342e9bd026d96fa37c4273c62e
Merged-In: Iacc33d5eb7b70b4f2984ff073c3f7003951af6e6
Merged-In: If8c54ca1e51eb6b587c70e8d218f9b90c7ad4c8e
11 files changed, 161 insertions, 0 deletions
diff --git a/hostsidetests/net/Android.bp b/hostsidetests/net/Android.bp index 741c961e5f3..b8d1c69b443 100644 --- a/hostsidetests/net/Android.bp +++ b/hostsidetests/net/Android.bp @@ -26,5 +26,6 @@ java_test_host { "cts", "vts10", "general-tests", + "sts" ], } diff --git a/hostsidetests/net/app/Android.bp b/hostsidetests/net/app/Android.bp index 7a1145609f6..dabc3cd0fe8 100644 --- a/hostsidetests/net/app/Android.bp +++ b/hostsidetests/net/app/Android.bp @@ -22,6 +22,7 @@ android_test_helper_app { static_libs: [ "androidx.test.rules", "androidx.test.ext.junit", + "androidx.test.uiautomator_uiautomator", "compatibility-device-util-axt", "ctstestrunner-axt", "ub-uiautomator", diff --git a/hostsidetests/net/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java b/hostsidetests/net/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java index e5fd149aec0..87e89b9d3dd 100644 --- a/hostsidetests/net/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java +++ b/hostsidetests/net/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java @@ -82,6 +82,10 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { private static final String MANIFEST_RECEIVER = "ManifestReceiver"; private static final String DYNAMIC_RECEIVER = "DynamicReceiver"; + // Copied from com.android.server.net.NetworkPolicyManagerService class + private static final String ACTION_SNOOZE_WARNING = + "com.android.server.net.action.SNOOZE_WARNING"; + private static final String ACTION_RECEIVER_READY = "com.android.cts.net.hostside.app2.action.RECEIVER_READY"; static final String ACTION_SHOW_TOAST = @@ -124,6 +128,8 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { protected static final long TEMP_POWERSAVE_WHITELIST_DURATION_MS = 5_000; // 5 sec + private static final long BROADCAST_TIMEOUT_MS = 15_000; + protected Context mContext; protected Instrumentation mInstrumentation; protected ConnectivityManager mCm; @@ -192,6 +198,12 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { + maxAttempts * SLEEP_TIME_SEC + " seconds", expectedCount, count); } + protected void assertSnoozeWarningNotReceived() throws Exception { + // Wait for a while to take broadcast queue delays into account + SystemClock.sleep(BROADCAST_TIMEOUT_MS); + assertEquals(0, getNumberBroadcastsReceived(DYNAMIC_RECEIVER, ACTION_SNOOZE_WARNING)); + } + protected String sendOrderedBroadcast(Intent intent) throws Exception { return sendOrderedBroadcast(intent, ORDERED_BROADCAST_TIMEOUT_MS); } diff --git a/hostsidetests/net/app/src/com/android/cts/net/hostside/DataWarningReceiverTest.java b/hostsidetests/net/app/src/com/android/cts/net/hostside/DataWarningReceiverTest.java new file mode 100644 index 00000000000..b2e81ff7cd2 --- /dev/null +++ b/hostsidetests/net/app/src/com/android/cts/net/hostside/DataWarningReceiverTest.java @@ -0,0 +1,108 @@ +/* + * 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 static com.android.cts.net.hostside.NetworkPolicyTestUtils.clearSnoozeTimestamps; + +import android.content.pm.PackageManager; +import android.support.test.uiautomator.By; +import android.support.test.uiautomator.Direction; +import android.support.test.uiautomator.UiObject2; +import android.support.test.uiautomator.Until; +import android.telephony.SubscriptionManager; +import android.telephony.SubscriptionPlan; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.uiautomator.UiDevice; + +import com.android.compatibility.common.util.SystemUtil; +import com.android.compatibility.common.util.UiAutomatorUtils; + +import org.junit.After; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; + +import java.time.Period; +import java.time.ZonedDateTime; +import java.util.Arrays; +import java.util.List; + +public class DataWarningReceiverTest extends AbstractRestrictBackgroundNetworkTestCase { + + @Before + public void setUp() throws Exception { + super.setUp(); + + clearSnoozeTimestamps(); + registerBroadcastReceiver(); + turnScreenOn(); + } + + @After + public void tearDown() throws Exception { + super.tearDown(); + } + + @Test + public void testSnoozeWarningNotReceived() throws Exception { + Assume.assumeTrue("Feature not supported: " + PackageManager.FEATURE_TELEPHONY, + mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)); + final SubscriptionManager sm = mContext.getSystemService(SubscriptionManager.class); + final int subId = SubscriptionManager.getDefaultDataSubscriptionId(); + Assume.assumeTrue("Valid subId not found", + subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID); + + setSubPlanOwner(subId, TEST_PKG); + final List<SubscriptionPlan> originalPlans = sm.getSubscriptionPlans(subId); + try { + // In NetworkPolicyManagerService class, we set the data warning bytes to 90% of + // data limit bytes. So, create the subscription plan in such a way this data warning + // threshold is already reached. + final SubscriptionPlan plan = SubscriptionPlan.Builder + .createRecurring(ZonedDateTime.parse("2007-03-14T00:00:00.000Z"), + Period.ofMonths(1)) + .setTitle("CTS") + .setDataLimit(1_000_000_000, SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED) + .setDataUsage(999_000_000, System.currentTimeMillis()) + .build(); + sm.setSubscriptionPlans(subId, Arrays.asList(plan)); + final UiDevice uiDevice = UiDevice.getInstance(mInstrumentation); + uiDevice.openNotification(); + try { + final UiObject2 uiObject = UiAutomatorUtils.waitFindObject( + By.text("Data warning")); + Assume.assumeNotNull(uiObject); + uiObject.wait(Until.clickable(true), 10_000L); + uiObject.getParent().swipe(Direction.RIGHT, 1.0f); + } catch (Throwable t) { + Assume.assumeNoException( + "Error occurred while finding and swiping the notification", t); + } + assertSnoozeWarningNotReceived(); + uiDevice.pressHome(); + } finally { + sm.setSubscriptionPlans(subId, originalPlans); + setSubPlanOwner(subId, ""); + } + } + + private static void setSubPlanOwner(int subId, String packageName) throws Exception { + SystemUtil.runShellCommand(InstrumentationRegistry.getInstrumentation(), + "cmd netpolicy set sub-plan-owner " + subId + " " + packageName); + } +} diff --git a/hostsidetests/net/app/src/com/android/cts/net/hostside/DumpOnFailureRule.java b/hostsidetests/net/app/src/com/android/cts/net/hostside/DumpOnFailureRule.java index 5ecb399da0f..bc93fdb6c7b 100644 --- a/hostsidetests/net/app/src/com/android/cts/net/hostside/DumpOnFailureRule.java +++ b/hostsidetests/net/app/src/com/android/cts/net/hostside/DumpOnFailureRule.java @@ -38,6 +38,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.uiautomator.UiDevice; public class DumpOnFailureRule extends OnFailureRule { private File mDumpDir = new File(Environment.getExternalStorageDirectory(), @@ -69,6 +70,16 @@ public class DumpOnFailureRule extends OnFailureRule { } catch (IOException e) { Log.e(TAG, "Error closing file: " + dumpFile, e); } + final UiDevice uiDevice = UiDevice.getInstance( + InstrumentationRegistry.getInstrumentation()); + final File screenshotFile = new File(mDumpDir, "sc-" + testName + ".png"); + uiDevice.takeScreenshot(screenshotFile); + final File windowHierarchyFile = new File(mDumpDir, "wh-" + testName + ".xml"); + try { + uiDevice.dumpWindowHierarchy(windowHierarchyFile); + } catch (IOException e) { + Log.e(TAG, "Error dumping window hierarchy", e); + } } void dumpCommandOutput(FileOutputStream out, String cmd) { diff --git a/hostsidetests/net/app/src/com/android/cts/net/hostside/NetworkPolicyTestUtils.java b/hostsidetests/net/app/src/com/android/cts/net/hostside/NetworkPolicyTestUtils.java index 3807d79c350..8462a804590 100644 --- a/hostsidetests/net/app/src/com/android/cts/net/hostside/NetworkPolicyTestUtils.java +++ b/hostsidetests/net/app/src/com/android/cts/net/hostside/NetworkPolicyTestUtils.java @@ -248,6 +248,10 @@ public class NetworkPolicyTestUtils { } } + public static void clearSnoozeTimestamps() { + executeShellCommand("dumpsys netpolicy --unsnooze"); + } + public static String executeShellCommand(String command) { final String result = runShellCommand(command).trim(); Log.d(TAG, "Output of '" + command + "': '" + result + "'"); diff --git a/hostsidetests/net/app2/AndroidManifest.xml b/hostsidetests/net/app2/AndroidManifest.xml index ad270b3170f..13e0bddcb04 100644 --- a/hostsidetests/net/app2/AndroidManifest.xml +++ b/hostsidetests/net/app2/AndroidManifest.xml @@ -52,4 +52,11 @@ </receiver> </application> + <!-- + Adding this to make sure that receiving the broadcast is not restricted by + package visibility restrictions. + --> + <queries> + <package android:name="android" /> + </queries> </manifest> diff --git a/hostsidetests/net/app2/src/com/android/cts/net/hostside/app2/Common.java b/hostsidetests/net/app2/src/com/android/cts/net/hostside/app2/Common.java index 351733edc5a..d34ff174ca2 100644 --- a/hostsidetests/net/app2/src/com/android/cts/net/hostside/app2/Common.java +++ b/hostsidetests/net/app2/src/com/android/cts/net/hostside/app2/Common.java @@ -40,6 +40,9 @@ public final class Common { "com.android.cts.net.hostside.app2.action.FINISH_ACTIVITY"; static final String ACTION_SHOW_TOAST = "com.android.cts.net.hostside.app2.action.SHOW_TOAST"; + // Copied from com.android.server.net.NetworkPolicyManagerService class + static final String ACTION_SNOOZE_WARNING = + "com.android.server.net.action.SNOOZE_WARNING"; static final String NOTIFICATION_TYPE_CONTENT = "CONTENT"; static final String NOTIFICATION_TYPE_DELETE = "DELETE"; diff --git a/hostsidetests/net/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java b/hostsidetests/net/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java index aa540757837..a8eb5676b1f 100644 --- a/hostsidetests/net/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java +++ b/hostsidetests/net/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java @@ -20,6 +20,7 @@ import static android.net.ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED import static com.android.cts.net.hostside.app2.Common.ACTION_RECEIVER_READY; import static com.android.cts.net.hostside.app2.Common.ACTION_SHOW_TOAST; +import static com.android.cts.net.hostside.app2.Common.ACTION_SNOOZE_WARNING; import static com.android.cts.net.hostside.app2.Common.MANIFEST_RECEIVER; import static com.android.cts.net.hostside.app2.Common.NOTIFICATION_TYPE_ACTION; import static com.android.cts.net.hostside.app2.Common.NOTIFICATION_TYPE_ACTION_BUNDLE; @@ -76,6 +77,9 @@ public class MyBroadcastReceiver extends BroadcastReceiver { Log.d(TAG, "onReceive() for " + mName + ": " + intent); final String action = intent.getAction(); switch (action) { + case ACTION_SNOOZE_WARNING: + increaseCounter(context, action); + break; case ACTION_RESTRICT_BACKGROUND_CHANGED: increaseCounter(context, action); break; diff --git a/hostsidetests/net/app2/src/com/android/cts/net/hostside/app2/MyService.java b/hostsidetests/net/app2/src/com/android/cts/net/hostside/app2/MyService.java index 590e17e5e5d..7276af2e450 100644 --- a/hostsidetests/net/app2/src/com/android/cts/net/hostside/app2/MyService.java +++ b/hostsidetests/net/app2/src/com/android/cts/net/hostside/app2/MyService.java @@ -18,6 +18,7 @@ package com.android.cts.net.hostside.app2; import static android.net.ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED; import static com.android.cts.net.hostside.app2.Common.ACTION_RECEIVER_READY; +import static com.android.cts.net.hostside.app2.Common.ACTION_SNOOZE_WARNING; import static com.android.cts.net.hostside.app2.Common.DYNAMIC_RECEIVER; import static com.android.cts.net.hostside.app2.Common.TAG; @@ -65,6 +66,7 @@ public class MyService extends Service { context.registerReceiver(mReceiver, new IntentFilter(ACTION_RECEIVER_READY)); context.registerReceiver(mReceiver, new IntentFilter(ACTION_RESTRICT_BACKGROUND_CHANGED)); + context.registerReceiver(mReceiver, new IntentFilter(ACTION_SNOOZE_WARNING)); Log.d(TAG, "receiver registered"); } diff --git a/hostsidetests/net/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java b/hostsidetests/net/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java index 4598c3936b9..b914143b340 100644 --- a/hostsidetests/net/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java +++ b/hostsidetests/net/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java @@ -16,6 +16,8 @@ package com.android.cts.net; +import android.platform.test.annotations.SecurityTest; + import com.android.ddmlib.Log; import com.android.tradefed.device.DeviceNotAvailableException; @@ -36,6 +38,12 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC uninstallPackage(TEST_APP2_PKG, true); } + @SecurityTest + public void testDataWarningReceiver() throws Exception { + runDeviceTests(TEST_PKG, TEST_PKG + ".DataWarningReceiverTest", + "testSnoozeWarningNotReceived"); + } + /************************** * Data Saver Mode tests. * **************************/ |