Fix storage cts due to app data storage isolation change

Now each apps storage data and obb are in its own process namespace,
apps will see it as another new mount point, which it always has write
access and not a normal mount point, so we need to fix the existing
tests to apply the new changes.

Bug: 183108201
Test: After turning on app data isolation change flag
atest android.appsecurity.cts.ExternalStorageHostTest#testExternalStorageNone29
atest android.appsecurity.cts.ExternalStorageHostTest#testExternalStorageRead29
atest android.appsecurity.cts.ExternalStorageHostTest#testExternalStorageWrite

Change-Id: I48a30bc554bb5d2966ed586bf02850820e888a46
diff --git a/hostsidetests/appsecurity/test-apps/ExternalStorageApp/src/com/android/cts/externalstorageapp/ExternalStorageTest.java b/hostsidetests/appsecurity/test-apps/ExternalStorageApp/src/com/android/cts/externalstorageapp/ExternalStorageTest.java
index 9b5424b..51395bd 100644
--- a/hostsidetests/appsecurity/test-apps/ExternalStorageApp/src/com/android/cts/externalstorageapp/ExternalStorageTest.java
+++ b/hostsidetests/appsecurity/test-apps/ExternalStorageApp/src/com/android/cts/externalstorageapp/ExternalStorageTest.java
@@ -81,9 +81,17 @@
     public void testMountPointsNotReadable() throws Exception {
         final String userId = Integer.toString(android.os.Process.myUid() / 100000);
         final List<File> mountPaths = getMountPaths();
+        final String packageName = getContext().getPackageName();
         for (File path : mountPaths) {
             if (path.getAbsolutePath().startsWith("/mnt/")
                     || path.getAbsolutePath().startsWith("/storage/")) {
+                if (path.getAbsolutePath().endsWith("obb/" + packageName) ||
+                        path.getAbsolutePath().endsWith("data/" + packageName)) {
+                    // It happens when app data isolation is enabled, obb and data dir will
+                    // be mounted in app's mount namespace.
+                    // It's package's own obb / data dir, we allow it.
+                    continue;
+                }
                 // Mount points could be multi-user aware, so try probing both
                 // top level and user-specific directory.
                 final File userPath = new File(path, userId);
diff --git a/hostsidetests/appsecurity/test-apps/ReadExternalStorageApp/src/com/android/cts/readexternalstorageapp/ReadExternalStorageTest.java b/hostsidetests/appsecurity/test-apps/ReadExternalStorageApp/src/com/android/cts/readexternalstorageapp/ReadExternalStorageTest.java
index a43bbe7..73f56dd 100644
--- a/hostsidetests/appsecurity/test-apps/ReadExternalStorageApp/src/com/android/cts/readexternalstorageapp/ReadExternalStorageTest.java
+++ b/hostsidetests/appsecurity/test-apps/ReadExternalStorageApp/src/com/android/cts/readexternalstorageapp/ReadExternalStorageTest.java
@@ -85,9 +85,14 @@
     public void testMountPointsNotWritable() throws Exception {
         final String userId = Integer.toString(android.os.Process.myUid() / 100000);
         final List<File> mountPaths = getMountPaths();
+        final String packageName = getContext().getPackageName();
         for (File path : mountPaths) {
             if (path.getAbsolutePath().startsWith("/mnt/")
                     || path.getAbsolutePath().startsWith("/storage/")) {
+                if (path.getAbsolutePath().endsWith(packageName)) {
+                    // It's package's own obb / data dir, we allow it.
+                    continue;
+                }
                 // Mount points could be multi-user aware, so try probing both
                 // top level and user-specific directory.
                 final File userPath = new File(path, userId);
diff --git a/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/Android.bp b/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/Android.bp
index fa03865..2744c6f 100644
--- a/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/Android.bp
+++ b/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/Android.bp
@@ -22,6 +22,7 @@
     sdk_version: "test_current",
     static_libs: [
         "CtsWriteExternalStorageWriteGiftLib",
+        "androidx.appcompat_appcompat",
         "androidx.test.rules",
         "compatibility-device-util-axt",
     ],
diff --git a/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/src/com/android/cts/writeexternalstorageapp/WriteExternalStorageTest.java b/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/src/com/android/cts/writeexternalstorageapp/WriteExternalStorageTest.java
index 827b440..8720537 100644
--- a/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/src/com/android/cts/writeexternalstorageapp/WriteExternalStorageTest.java
+++ b/hostsidetests/appsecurity/test-apps/WriteExternalStorageApp/src/com/android/cts/writeexternalstorageapp/WriteExternalStorageTest.java
@@ -36,6 +36,7 @@
 import android.test.AndroidTestCase;
 import android.util.Log;
 
+import androidx.core.os.BuildCompat;
 import com.android.cts.externalstorageapp.CommonExternalStorageTest;
 
 import java.io.File;
@@ -263,6 +264,12 @@
      * Verify that .nomedia is created correctly.
      */
     public void testVerifyNoMediaCreated() throws Exception {
+        boolean expectNoMediaFileExists = true;
+        if (BuildCompat.isAtLeastS()) {
+            // All package specific paths will be in app's mount namespace, and it cannot
+            // access its parent to check .nomedia file.
+            expectNoMediaFileExists = false;
+        }
         for (File file : getAllPackageSpecificPathsExceptMedia(getContext())) {
             deleteContents(file);
         }
@@ -290,9 +297,12 @@
                 path = path.getParentFile();
             }
 
-            if (!found) {
+            if (expectNoMediaFileExists && !found) {
                 fail("Missing .nomedia file above package-specific directory " + start
                         + "; gave up at " + path);
+            } else if (!expectNoMediaFileExists && found) {
+                fail(".nomedia file should not be exists due to app data isolation, but found " +
+                    " in package-specific directory " + start + "; gave up at " + path);
             }
         }
     }
@@ -309,6 +319,7 @@
 
         final String userId = Integer.toString(android.os.Process.myUid() / 100000);
         final List<File> mountPaths = getMountPaths();
+        final String packageName = getContext().getPackageName();
         for (File path : mountPaths) {
             // Mount points could be multi-user aware, so try probing both top
             // level and user-specific directory.
@@ -326,6 +337,11 @@
                             || path.getAbsolutePath().endsWith("/Android/obb")) {
                         assertDirNoWriteAccess(path);
                     } else {
+                        if (path.getAbsolutePath().endsWith(packageName)) {
+                            // It's package's own obb / data dir, it's not a normal mount point
+                            // and we don't need to check the access.
+                            continue;
+                        }
                         assertDirReadWriteAccess(path);
                         assertDirReadWriteAccess(buildCommonChildDirs(path));
                     }