From fd4deb3e9ee68824e1b3be3d6709d27d84b36e5b Mon Sep 17 00:00:00 2001 From: paulhu Date: Wed, 6 Apr 2022 10:15:54 +0800 Subject: [PATCH] Move network permission cts tests to module CtsNetTestCasesUpdateStatsPermission and CtsNetTestCasesInternetPermission are used to verify PermissionMonitor relevant permissions. CtsNetTestCasesInternetPermission: One app does not have INTERNET permission so it should not be able to create network socket. CtsNetTestCasesUpdateStatsPermission: The other one specify the UPDATE_DEVICE_STATS in the AndroidManifest file but it is not a privileged app so it should not have UPDATE_DEVICE_STATS permission granted. Bug: 228253498 Test: atest CtsNetTestCasesUpdateStatsPermission \ CtsNetTestCasesInternetPermission Original-Change: https://android-review.googlesource.com/926255 Merged-In: I380307fe0a5bc6aac6d9f836f03f0d043b99ccf5 Change-Id: I380307fe0a5bc6aac6d9f836f03f0d043b99ccf5 --- .../internetpermission/Android.bp | 33 +++++++ .../internetpermission/AndroidManifest.xml | 50 +++++++++++ .../internetpermission/AndroidTest.xml | 31 +++++++ .../internetpermission/TEST_MAPPING | 7 ++ .../permission/InternetPermissionTest.java | 50 +++++++++++ .../updatestatspermission/Android.bp | 33 +++++++ .../updatestatspermission/AndroidManifest.xml | 58 ++++++++++++ .../updatestatspermission/AndroidTest.xml | 31 +++++++ .../updatestatspermission/TEST_MAPPING | 7 ++ .../permission/UpdateStatsPermissionTest.java | 90 +++++++++++++++++++ 10 files changed, 390 insertions(+) create mode 100644 tests/cts/netpermission/internetpermission/Android.bp create mode 100644 tests/cts/netpermission/internetpermission/AndroidManifest.xml create mode 100644 tests/cts/netpermission/internetpermission/AndroidTest.xml create mode 100644 tests/cts/netpermission/internetpermission/TEST_MAPPING create mode 100644 tests/cts/netpermission/internetpermission/src/android/net/cts/network/permission/InternetPermissionTest.java create mode 100644 tests/cts/netpermission/updatestatspermission/Android.bp create mode 100644 tests/cts/netpermission/updatestatspermission/AndroidManifest.xml create mode 100644 tests/cts/netpermission/updatestatspermission/AndroidTest.xml create mode 100644 tests/cts/netpermission/updatestatspermission/TEST_MAPPING create mode 100644 tests/cts/netpermission/updatestatspermission/src/android/net/cts/network/permission/UpdateStatsPermissionTest.java diff --git a/tests/cts/netpermission/internetpermission/Android.bp b/tests/cts/netpermission/internetpermission/Android.bp new file mode 100644 index 0000000000..37ad7cb2ac --- /dev/null +++ b/tests/cts/netpermission/internetpermission/Android.bp @@ -0,0 +1,33 @@ +// Copyright (C) 2019 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 { + default_applicable_licenses: ["Android-Apache-2.0"], +} + +android_test { + name: "CtsNetTestCasesInternetPermission", + defaults: ["cts_defaults"], + + srcs: ["src/**/*.java"], + + static_libs: ["ctstestrunner-axt"], + + // Tag this module as a cts test artifact + test_suites: [ + "cts", + "general-tests", + ], + +} diff --git a/tests/cts/netpermission/internetpermission/AndroidManifest.xml b/tests/cts/netpermission/internetpermission/AndroidManifest.xml new file mode 100644 index 0000000000..45ef5bdd5f --- /dev/null +++ b/tests/cts/netpermission/internetpermission/AndroidManifest.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/cts/netpermission/internetpermission/AndroidTest.xml b/tests/cts/netpermission/internetpermission/AndroidTest.xml new file mode 100644 index 0000000000..3b23e72145 --- /dev/null +++ b/tests/cts/netpermission/internetpermission/AndroidTest.xml @@ -0,0 +1,31 @@ + + + + diff --git a/tests/cts/netpermission/internetpermission/TEST_MAPPING b/tests/cts/netpermission/internetpermission/TEST_MAPPING new file mode 100644 index 0000000000..60877f4a32 --- /dev/null +++ b/tests/cts/netpermission/internetpermission/TEST_MAPPING @@ -0,0 +1,7 @@ +{ + "presubmit": [ + { + "name": "CtsNetTestCasesInternetPermission" + } + ] +} diff --git a/tests/cts/netpermission/internetpermission/src/android/net/cts/network/permission/InternetPermissionTest.java b/tests/cts/netpermission/internetpermission/src/android/net/cts/network/permission/InternetPermissionTest.java new file mode 100644 index 0000000000..2b7c8b5f35 --- /dev/null +++ b/tests/cts/netpermission/internetpermission/src/android/net/cts/network/permission/InternetPermissionTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2019 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.networkpermission.internetpermission; + +import static org.junit.Assert.fail; + +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.net.Socket; +/** +* Test that protected android.net.ConnectivityManager methods cannot be called without +* permissions +*/ +@RunWith(AndroidJUnit4.class) +public class InternetPermissionTest { + + /** + * Verify that create inet socket failed because of the permission is missing. + *

Tests Permission: + * {@link android.Manifest.permission#INTERNET}. + */ + @SmallTest + @Test + public void testCreateSocket() throws Exception { + try { + Socket socket = new Socket("example.com", 80); + fail("Ceate inet socket did not throw SecurityException as expected"); + } catch (SecurityException e) { + // expected + } + } +} diff --git a/tests/cts/netpermission/updatestatspermission/Android.bp b/tests/cts/netpermission/updatestatspermission/Android.bp new file mode 100644 index 0000000000..7a24886e69 --- /dev/null +++ b/tests/cts/netpermission/updatestatspermission/Android.bp @@ -0,0 +1,33 @@ +// Copyright (C) 2019 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 { + default_applicable_licenses: ["Android-Apache-2.0"], +} + +android_test { + name: "CtsNetTestCasesUpdateStatsPermission", + defaults: ["cts_defaults"], + + srcs: ["src/**/*.java"], + + static_libs: ["ctstestrunner-axt"], + + // Tag this module as a cts test artifact + test_suites: [ + "cts", + "general-tests", + ], + +} diff --git a/tests/cts/netpermission/updatestatspermission/AndroidManifest.xml b/tests/cts/netpermission/updatestatspermission/AndroidManifest.xml new file mode 100644 index 0000000000..6babe8fd55 --- /dev/null +++ b/tests/cts/netpermission/updatestatspermission/AndroidManifest.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/cts/netpermission/updatestatspermission/AndroidTest.xml b/tests/cts/netpermission/updatestatspermission/AndroidTest.xml new file mode 100644 index 0000000000..c47cad9711 --- /dev/null +++ b/tests/cts/netpermission/updatestatspermission/AndroidTest.xml @@ -0,0 +1,31 @@ + + + + diff --git a/tests/cts/netpermission/updatestatspermission/TEST_MAPPING b/tests/cts/netpermission/updatestatspermission/TEST_MAPPING new file mode 100644 index 0000000000..6d6dfe042b --- /dev/null +++ b/tests/cts/netpermission/updatestatspermission/TEST_MAPPING @@ -0,0 +1,7 @@ +{ + "presubmit": [ + { + "name": "CtsNetTestCasesUpdateStatsPermission" + } + ] +} diff --git a/tests/cts/netpermission/updatestatspermission/src/android/net/cts/network/permission/UpdateStatsPermissionTest.java b/tests/cts/netpermission/updatestatspermission/src/android/net/cts/network/permission/UpdateStatsPermissionTest.java new file mode 100644 index 0000000000..bea843c706 --- /dev/null +++ b/tests/cts/netpermission/updatestatspermission/src/android/net/cts/network/permission/UpdateStatsPermissionTest.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2019 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.networkpermission.updatestatspermission; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import android.net.TrafficStats; +import android.os.Process; + +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.OutputStream; +import java.net.Socket; + +/** +* Test that protected android.net.ConnectivityManager methods cannot be called without +* permissions +*/ +@RunWith(AndroidJUnit4.class) +public class UpdateStatsPermissionTest { + + /** + * Verify that setCounterSet for a different uid failed because of the permission cannot be + * granted to a third-party app. + *

Tests Permission: + * {@link android.Manifest.permission#UPDATE_DEVICE_STATS}. + */ + @SmallTest + @Test + public void testUpdateDeviceStatsPermission() throws Exception { + + // Set the current thread uid to a another uid. It should silently fail when tagging the + // socket since the current process doesn't have UPDATE_DEVICE_STATS permission. + TrafficStats.setThreadStatsTag(0); + TrafficStats.setThreadStatsUid(/*root uid*/ 0); + Socket socket = new Socket("example.com", 80); + TrafficStats.tagSocket(socket); + + // Transfer 1K of data to a remote host and verify the stats is still billed to the current + // uid. + final int byteCount = 1024; + + socket.setTcpNoDelay(true); + socket.setSoLinger(true, 0); + OutputStream out = socket.getOutputStream(); + byte[] buf = new byte[byteCount]; + final long uidTxBytesBefore = TrafficStats.getUidTxBytes(Process.myUid()); + out.write(buf); + out.close(); + socket.close(); + long uidTxBytesAfter = TrafficStats.getUidTxBytes(Process.myUid()); + long uidTxDeltaBytes = uidTxBytesAfter - uidTxBytesBefore; + assertTrue("uidtxb: " + uidTxBytesBefore + " -> " + uidTxBytesAfter + " delta=" + + uidTxDeltaBytes + " >= " + byteCount, uidTxDeltaBytes >= byteCount); + } + + static final int UNSUPPORTED = -1; + + /** + * Verify that get TrafficStats of a different uid failed because of the permission is not + * granted to a third-party app. + *

Tests Permission: + * {@link android.Manifest.permission#UPDATE_DEVICE_STATS}. + */ + @SmallTest + @Test + public void testGetStatsOfOtherUid() throws Exception { + // Test get stats of another uid failed since the current process does not have permission + assertEquals(UNSUPPORTED, TrafficStats.getUidRxBytes(/*root uid*/ 0)); + } +}