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
This commit is contained in:
paulhu
2022-04-06 10:15:54 +08:00
parent 09475308b7
commit fd4deb3e9e
10 changed files with 390 additions and 0 deletions

View File

@@ -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",
],
}

View File

@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
* 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.networkpermission.internetpermission.cts">
<application>
<uses-library android:name="android.test.runner"/>
<activity android:name="android.networkpermission.internetpermission.cts.InternetPermissionTest"
android:label="InternetPermissionTest"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST"/>
</intent-filter>
</activity>
</application>
<!--
The CTS stubs package cannot be used as the target application here,
since that requires many permissions to be set. Instead, specify this
package itself as the target and include any stub activities needed.
This test package uses the default InstrumentationTestRunner, because
the InstrumentationCtsTestRunner is only available in the stubs
package. That runner cannot be added to this package either, since it
relies on hidden APIs.
-->
<instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="android.networkpermission.internetpermission.cts"
android:label="CTS tests for INTERNET permissions">
<meta-data android:name="listener"
android:value="com.android.cts.runner.CtsTestRunListener"/>
</instrumentation>
</manifest>

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<configuration description="Config for CTS internet permission test cases">
<option name="test-suite-tag" value="cts" />
<option name="config-descriptor:metadata" key="component" value="networking" />
<option name="config-descriptor:metadata" key="parameter" value="instant_app" />
<option name="config-descriptor:metadata" key="parameter" value="not_multi_abi" />
<option name="config-descriptor:metadata" key="parameter" value="secondary_user" />
<option name="not-shardable" value="true" />
<target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
<option name="cleanup-apks" value="true" />
<option name="test-file-name" value="CtsNetTestCasesInternetPermission.apk" />
</target_preparer>
<test class="com.android.tradefed.testtype.AndroidJUnitTest" >
<option name="package" value="android.networkpermission.internetpermission.cts" />
<option name="runtime-hint" value="10s" />
</test>
</configuration>

View File

@@ -0,0 +1,7 @@
{
"presubmit": [
{
"name": "CtsNetTestCasesInternetPermission"
}
]
}

View File

@@ -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.
* <p>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
}
}
}

View File

@@ -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",
],
}

View File

@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
* 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.networkpermission.updatestatspermission.cts">
<!--
This CTS test is designed to test that an unprivileged app cannot get the
UPDATE_DEVICE_STATS permission even if it specified it in the manifest. the
UPDATE_DEVICE_STATS permission is a signature|privileged permission that CTS
test cannot have.
-->
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application>
<uses-library android:name="android.test.runner"/>
<activity android:name="android.networkpermission.updatestatspermission.cts.UpdateStatsPermissionTest"
android:label="UpdateStatsPermissionTest"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST"/>
</intent-filter>
</activity>
</application>
<!--
The CTS stubs package cannot be used as the target application here,
since that requires many permissions to be set. Instead, specify this
package itself as the target and include any stub activities needed.
This test package uses the default InstrumentationTestRunner, because
the InstrumentationCtsTestRunner is only available in the stubs
package. That runner cannot be added to this package either, since it
relies on hidden APIs.
-->
<instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="android.networkpermission.updatestatspermission.cts"
android:label="CTS tests for UPDATE_DEVICE_STATS permissions">
<meta-data android:name="listener"
android:value="com.android.cts.runner.CtsTestRunListener"/>
</instrumentation>
</manifest>

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<configuration description="Config for CTS update stats permission test cases">
<option name="test-suite-tag" value="cts" />
<option name="config-descriptor:metadata" key="component" value="networking" />
<option name="config-descriptor:metadata" key="parameter" value="instant_app" />
<option name="config-descriptor:metadata" key="parameter" value="not_multi_abi" />
<option name="config-descriptor:metadata" key="parameter" value="secondary_user" />
<option name="not-shardable" value="true" />
<target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
<option name="cleanup-apks" value="true" />
<option name="test-file-name" value="CtsNetTestCasesUpdateStatsPermission.apk" />
</target_preparer>
<test class="com.android.tradefed.testtype.AndroidJUnitTest" >
<option name="package" value="android.networkpermission.updatestatspermission.cts" />
<option name="runtime-hint" value="10s" />
</test>
</configuration>

View File

@@ -0,0 +1,7 @@
{
"presubmit": [
{
"name": "CtsNetTestCasesUpdateStatsPermission"
}
]
}

View File

@@ -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.
* <p>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.
* <p>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));
}
}