1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.scopedstorage.cts.host; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.assertNull; 22 import static org.junit.Assume.assumeTrue; 23 24 import android.platform.test.annotations.AppModeFull; 25 26 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; 27 import com.android.compatibility.common.util.CtsDownstreamingTest; 28 import com.android.modules.utils.build.testing.DeviceSdkLevel; 29 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 30 31 import org.junit.After; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 /** 36 * Runs the legacy file path access tests. 37 */ 38 @CtsDownstreamingTest 39 @RunWith(DeviceJUnit4ClassRunner.class) 40 @AppModeFull 41 public class PreserveLegacyStorageHostTest extends BaseHostTestCase { 42 private static final String LEGACY_29_APK = "CtsLegacyStorageTestAppRequestLegacy.apk"; 43 private static final String PRESERVE_30_APK = "CtsLegacyStorageTestAppPreserveLegacy.apk"; 44 private static final String PACKAGE_NAME = "android.scopedstorage.cts.legacy.preserve"; 45 installApp(String appFileName)46 protected void installApp(String appFileName) throws Exception { 47 CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(getBuild()); 48 int userId = getCurrentUserId(); 49 String result = getDevice().installPackageForUser( 50 buildHelper.getTestFile(appFileName), true, true, userId, "-t"); 51 assertNull("Failed to install " + appFileName + " for user " + userId + ": " + result, 52 result); 53 } 54 55 @After tearDown()56 public void tearDown() throws Exception { 57 uninstallPackage(PACKAGE_NAME); 58 } 59 60 @Test testPreserveLegacy()61 public void testPreserveLegacy() throws Exception { 62 // This was broken on R, so only run the test on S+ devices 63 DeviceSdkLevel deviceSdkLevel = new DeviceSdkLevel(getDevice()); 64 assumeTrue(deviceSdkLevel.isDeviceAtLeastS()); 65 66 // Most of these tests are done device-side; see RestrictedStoragePermissionTest.java 67 // This test is done on the host, because we want to verify preserveLegacyExternalStorage 68 // is sticky across a reboot. 69 installApp(LEGACY_29_APK); 70 String result = getDevice().executeShellCommand( 71 "appops get " + PACKAGE_NAME + " LEGACY_STORAGE"); 72 assertThat(result).contains(": allow"); 73 74 // Upgrade to targetSdk 30 with preserveLegacyExternalStorage 75 installApp(PRESERVE_30_APK); 76 result = getDevice().executeShellCommand( 77 "appops get " + PACKAGE_NAME + " LEGACY_STORAGE"); 78 79 // And make sure we still have legacy 80 assertThat(result).contains(": allow"); 81 82 // Reboot, and again make sure we have legacy 83 getDevice().reboot(); 84 result = getDevice().executeShellCommand( 85 "appops get " + PACKAGE_NAME + " LEGACY_STORAGE"); 86 assertThat(result).contains(": allow"); 87 } 88 } 89