diff options
Diffstat (limited to 'hostsidetests/scopedstorage/src/android/scopedstorage/cts/AppCloningDeviceTest.java')
-rw-r--r-- | hostsidetests/scopedstorage/src/android/scopedstorage/cts/AppCloningDeviceTest.java | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/hostsidetests/scopedstorage/src/android/scopedstorage/cts/AppCloningDeviceTest.java b/hostsidetests/scopedstorage/src/android/scopedstorage/cts/AppCloningDeviceTest.java new file mode 100644 index 00000000000..6df4c8fb68b --- /dev/null +++ b/hostsidetests/scopedstorage/src/android/scopedstorage/cts/AppCloningDeviceTest.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2022 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.scopedstorage.cts; + +import static android.scopedstorage.cts.lib.TestUtils.canOpenFileAs; +import static android.scopedstorage.cts.lib.TestUtils.createFileAs; +import static android.scopedstorage.cts.lib.TestUtils.listAs; + +import static com.google.common.truth.Truth.assertThat; + +import android.os.Bundle; + +import androidx.test.InstrumentationRegistry; +import androidx.test.runner.AndroidJUnit4; + +import com.android.cts.install.lib.TestApp; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.File; + +@RunWith(AndroidJUnit4.class) +public class AppCloningDeviceTest { + + private static final String EMPTY_STRING = ""; + private static final String EXTERNAL_STORAGE_DCIM_PATH = "/storage/emulated/%d/DCIM"; + + // An app with no permissions + private static final TestApp APP_B_NO_PERMS = new TestApp("TestAppB", + "android.scopedstorage.cts.testapp.B.noperms", 1, false, + "CtsScopedStorageTestAppB.apk"); + + @Test + public void testInsertFilesInDirectoryViaMediaProvider() throws Exception { + String dirPath = String.format(EXTERNAL_STORAGE_DCIM_PATH, + Integer.parseInt(getCurrentUserId())); + final File dir = new File(dirPath); + assertThat(dir.exists()).isTrue(); + final File file = new File(dir, getFileToBeCreatedName()); + assertThat(createFileAs(APP_B_NO_PERMS, file.getPath())).isTrue(); + assertThat(canOpenFileAs(APP_B_NO_PERMS, file, true)).isTrue(); + assertThat(listAs(APP_B_NO_PERMS, dir.getPath())).contains(file.getName()); + } + + @Test + public void testGetFilesInDirectoryViaMediaProviderRespectsUserId() throws Exception { + String dirPath = String.format(EXTERNAL_STORAGE_DCIM_PATH, + Integer.parseInt(getCurrentUserId())); + final File dir = new File(dirPath); + assertThat(dir.exists()).isTrue(); + final File expectedFile = new File(dir, getFileToBeExpectedName()); + assertThat(listAs(APP_B_NO_PERMS, dir.getPath())).contains(expectedFile.getName()); + final File notExpectedFile = new File(dir, getFileNotToBeExpectedName()); + assertThat(listAs(APP_B_NO_PERMS, dir.getPath())).doesNotContain(notExpectedFile.getName()); + } + + private String getTestArgumentValueForGivenKey(String testArgumentKey) { + final Bundle testArguments = InstrumentationRegistry.getArguments(); + String testArgumentValue = testArguments.getString(testArgumentKey, EMPTY_STRING); + return testArgumentValue; + } + + private String getCurrentUserId() { + return getTestArgumentValueForGivenKey("currentUserId"); + } + + private String getFileToBeCreatedName() { + return getTestArgumentValueForGivenKey("fileToBeCreated"); + } + + private String getFileToBeExpectedName() { + return getTestArgumentValueForGivenKey("fileExpectedToBePresent"); + } + + private String getFileNotToBeExpectedName() { + return getTestArgumentValueForGivenKey("fileNotExpectedToBePresent"); + } +} |