Fix test failures on sargo. am: 16b9c0b1ab

Original change: https://android-review.googlesource.com/c/platform/art/+/2968599

Change-Id: I1974fa616d504e8facdd8e00dd659357e221bde6
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/libnativeloader/test/Android.bp b/libnativeloader/test/Android.bp
index 268e93a..6166725 100644
--- a/libnativeloader/test/Android.bp
+++ b/libnativeloader/test/Android.bp
@@ -102,11 +102,12 @@
 
 java_library {
     name: "loadlibrarytest_testlib",
-    sdk_version: "31",
+    sdk_version: "system_31",
     static_libs: [
         "androidx.test.ext.junit",
         "androidx.test.ext.truth",
         "androidx.test.rules",
+        "modules-utils-build_system",
     ],
     libs: [
         "libnativeloader_system_shared_lib",
diff --git a/libnativeloader/test/src/android/test/app/ProductAppTest.java b/libnativeloader/test/src/android/test/app/ProductAppTest.java
index bfa648b..4cf379c 100644
--- a/libnativeloader/test/src/android/test/app/ProductAppTest.java
+++ b/libnativeloader/test/src/android/test/app/ProductAppTest.java
@@ -16,8 +16,6 @@
 
 package android.test.app;
 
-import android.os.Build;
-import android.os.SystemProperties;
 import android.test.lib.AppTestCommon;
 import android.test.lib.TestUtils;
 import android.test.productsharedlib.ProductSharedLib;
@@ -36,13 +34,6 @@
         return AppLocation.PRODUCT;
     }
 
-    // True if apps in product partitions get shared library namespaces, so we
-    // cannot test that libs in system and system_ext get blocked.
-    private static boolean productAppsAreShared() {
-        return Build.VERSION.SDK_INT <= 34 && // UPSIDE_DOWN_CAKE
-                SystemProperties.get("ro.product.vndk.version").isEmpty();
-    }
-
     @Test
     public void testPrivateLibsExist() {
         TestUtils.testPrivateLibsExist("/product", "product_private");
@@ -53,7 +44,7 @@
         System.loadLibrary("system_extpub.oem1");
         System.loadLibrary("system_extpub.oem2");
         System.loadLibrary("system_extpub1.oem1");
-        if (!productAppsAreShared()) {
+        if (!TestUtils.productAppsAreShared()) {
             TestUtils.assertLibraryInaccessible( // Missing <uses-native-library>.
                     () -> System.loadLibrary("system_extpub_nouses.oem2"));
         }
@@ -63,7 +54,7 @@
 
     @Test
     public void testLoadPrivateLibraries() {
-        if (!productAppsAreShared()) {
+        if (!TestUtils.productAppsAreShared()) {
             TestUtils.assertLibraryInaccessible(() -> System.loadLibrary("system_private1"));
             TestUtils.assertLibraryInaccessible(() -> System.loadLibrary("systemext_private1"));
         }
@@ -81,13 +72,14 @@
 
     @Test
     public void testLoadPrivateLibrariesViaSystemSharedLib() {
-        // TODO(b/237577392): Loading a private native system library via a shared system library
-        // ought to work.
-        TestUtils.assertLibraryInaccessible(() -> SystemSharedLib.loadLibrary("system_private2"));
-        TestUtils.assertLibraryInaccessible(
-                () -> SystemSharedLib.loadLibrary("systemext_private2"));
+        if (!TestUtils.productAppsAreShared()) {
+            // TODO(b/237577392): Loading a private native system library via a shared system
+            // library ought to work.
+            TestUtils.assertLibraryInaccessible(
+                    () -> SystemSharedLib.loadLibrary("system_private2"));
+            TestUtils.assertLibraryInaccessible(
+                    () -> SystemSharedLib.loadLibrary("systemext_private2"));
 
-        if (!productAppsAreShared()) {
             TestUtils.assertLibraryInaccessible(
                     () -> SystemSharedLib.loadLibrary("product_private2"));
         }
@@ -97,14 +89,14 @@
 
     @Test
     public void testLoadPrivateLibrariesViaSystemExtSharedLib() {
-        // TODO(b/237577392): Loading a private native system library via a shared system library
-        // ought to work.
-        TestUtils.assertLibraryInaccessible(
-                () -> SystemExtSharedLib.loadLibrary("system_private3"));
-        TestUtils.assertLibraryInaccessible(
-                () -> SystemExtSharedLib.loadLibrary("systemext_private3"));
+        if (!TestUtils.productAppsAreShared()) {
+            // TODO(b/237577392): Loading a private native system library via a shared system
+            // library ought to work.
+            TestUtils.assertLibraryInaccessible(
+                    () -> SystemExtSharedLib.loadLibrary("system_private3"));
+            TestUtils.assertLibraryInaccessible(
+                    () -> SystemExtSharedLib.loadLibrary("systemext_private3"));
 
-        if (!productAppsAreShared()) {
             TestUtils.assertLibraryInaccessible(
                     () -> SystemExtSharedLib.loadLibrary("product_private3"));
         }
@@ -115,7 +107,7 @@
 
     @Test
     public void testLoadPrivateLibrariesViaProductSharedLib() {
-        if (!productAppsAreShared()) {
+        if (!TestUtils.productAppsAreShared()) {
             TestUtils.assertLibraryInaccessible(
                     () -> ProductSharedLib.loadLibrary("system_private4"));
             TestUtils.assertLibraryInaccessible(
@@ -130,7 +122,7 @@
 
     @Test
     public void testLoadPrivateLibrariesViaVendorSharedLib() {
-        if (!productAppsAreShared()) {
+        if (!TestUtils.productAppsAreShared()) {
             TestUtils.assertLibraryInaccessible(
                     () -> VendorSharedLib.loadLibrary("system_private5"));
             TestUtils.assertLibraryInaccessible(
@@ -155,7 +147,7 @@
 
     @Test
     public void testLoadPrivateLibrariesWithAbsolutePaths() {
-        if (!productAppsAreShared()) {
+        if (!TestUtils.productAppsAreShared()) {
             TestUtils.assertLibraryInaccessible(
                     () -> System.load(TestUtils.libPath("/system", "system_private6")));
             TestUtils.assertLibraryInaccessible(
diff --git a/libnativeloader/test/src/android/test/lib/AppTestCommon.java b/libnativeloader/test/src/android/test/lib/AppTestCommon.java
index 36ef3dc..51f4655 100644
--- a/libnativeloader/test/src/android/test/lib/AppTestCommon.java
+++ b/libnativeloader/test/src/android/test/lib/AppTestCommon.java
@@ -32,31 +32,50 @@
 
     public abstract AppLocation getAppLocation();
 
-    // Loading private libs using absolute paths through shared libs should only
-    // depend on the location of the shared lib, so these tests are shared for
-    // all apps, regardless of location.
+    // Loading private libs using absolute paths through shared libs should
+    // normally only depend on the location of the shared lib, so these tests
+    // are shared for all apps, regardless of location.
 
-    // There's an exception for system apps. For them LibraryNamespaces::Create
-    // gets called with is_shared=true. That means they don't set up separate
-    // "unbundled" namespaces for the shared libs in product and vendor, so
-    // ProductSharedLib and VendorSharedLib can still load private system libs
-    // through their classloader namespaces, but not the private libs in the
-    // same partition as themselves.
-    private boolean isSharedSystemApp() {
-        return getAppLocation() == AppLocation.SYSTEM;
+    // Returns true when system private native libs are accessible directly from
+    // the app classloader namespace.
+    private boolean systemPrivateLibsAccessibleFromAppNamespace() {
+        // Currently it only works from system apps. It also works from product
+        // apps on old versions where they were treated like system apps.
+        // TODO(b/237577392): Fix this to work from system shared libs.
+        return getAppLocation() == AppLocation.SYSTEM
+                || (getAppLocation() == AppLocation.PRODUCT && TestUtils.productAppsAreShared());
+    }
+
+    // Detect exception when product private libs are accessible directly from
+    // the app classloader namespace even when they shouldn't be.
+    private boolean productPrivateLibsAccessibleFromAppNamespace() {
+        // In old versions where product apps were treated like system apps, the
+        // product private libs were included in the system namespace, so
+        // they're accessible both from system and product apps.
+        // TODO(b/237577392): Fix this to work from product shared libs.
+        return (getAppLocation() == AppLocation.SYSTEM || getAppLocation() == AppLocation.PRODUCT)
+                && TestUtils.productAppsAreShared();
+    }
+
+    // Detect exception where we don't switch from a shared system namespace to
+    // a product or vendor "unbundled" namespace when calling into
+    // ProductSharedLib and VendorSharedLib. That means they still can load
+    // private system libs but not private libs in their own partition.
+    // TODO(mast): Stop propagating the shared property (isBundledApp in
+    // LoadedApk.java) down to public and vendor shared java libs?
+    private boolean noSwitchToVendorOrProductNamespace() {
+        // System apps get shared namespaces, and also product apps on old
+        // versions where they were treated like system apps.
+        return getAppLocation() == AppLocation.SYSTEM
+                || (getAppLocation() == AppLocation.PRODUCT && TestUtils.productAppsAreShared());
     }
 
     @Test
     public void testLoadPrivateLibrariesViaSystemSharedLibWithAbsolutePaths() {
-        if (getAppLocation() == AppLocation.SYSTEM) {
+        if (systemPrivateLibsAccessibleFromAppNamespace()) {
             SystemSharedLib.load(TestUtils.libPath("/system", "system_private7"));
             SystemSharedLib.load(TestUtils.libPath("/system_ext", "systemext_private7"));
         } else {
-            // Cannot load private system libs because there is no provision in
-            // LibraryNamespaces::Create to create an "unbundled system apk" for
-            // shared system libs based on their location. Hence SystemSharedLib
-            // gets a classloader namespace as an "other apk", with the same
-            // library_path as the app.
             TestUtils.assertLibraryInaccessible(() -> {
                 SystemSharedLib.load(TestUtils.libPath("/system", "system_private7"));
             });
@@ -65,8 +84,13 @@
             });
         }
 
-        TestUtils.assertLibraryInaccessible(
-                () -> { SystemSharedLib.load(TestUtils.libPath("/product", "product_private7")); });
+        if (productPrivateLibsAccessibleFromAppNamespace()) {
+            SystemSharedLib.load(TestUtils.libPath("/product", "product_private7"));
+        } else {
+            TestUtils.assertLibraryInaccessible(() -> {
+                SystemSharedLib.load(TestUtils.libPath("/product", "product_private7"));
+            });
+        }
 
         TestUtils.assertLibraryInaccessible(
                 () -> { SystemSharedLib.load(TestUtils.libPath("/vendor", "vendor_private7")); });
@@ -74,11 +98,10 @@
 
     @Test
     public void testLoadPrivateLibrariesViaSystemExtSharedLibWithAbsolutePaths() {
-        if (getAppLocation() == AppLocation.SYSTEM) {
+        if (systemPrivateLibsAccessibleFromAppNamespace()) {
             SystemExtSharedLib.load(TestUtils.libPath("/system", "system_private8"));
             SystemExtSharedLib.load(TestUtils.libPath("/system_ext", "systemext_private8"));
         } else {
-            // See comment in the corresponding test for SystemSharedLib above.
             TestUtils.assertLibraryInaccessible(() -> {
                 SystemExtSharedLib.load(TestUtils.libPath("/system", "system_private8"));
             });
@@ -87,9 +110,13 @@
             });
         }
 
-        TestUtils.assertLibraryInaccessible(() -> {
+        if (productPrivateLibsAccessibleFromAppNamespace()) {
             SystemExtSharedLib.load(TestUtils.libPath("/product", "product_private8"));
-        });
+        } else {
+            TestUtils.assertLibraryInaccessible(() -> {
+                SystemExtSharedLib.load(TestUtils.libPath("/product", "product_private8"));
+            });
+        }
 
         TestUtils.assertLibraryInaccessible(() -> {
             SystemExtSharedLib.load(TestUtils.libPath("/vendor", "vendor_private8"));
@@ -98,7 +125,7 @@
 
     @Test
     public void testLoadPrivateLibrariesViaProductSharedLibWithAbsolutePaths() {
-        if (isSharedSystemApp()) {
+        if (systemPrivateLibsAccessibleFromAppNamespace() || noSwitchToVendorOrProductNamespace()) {
             ProductSharedLib.load(TestUtils.libPath("/system", "system_private9"));
             ProductSharedLib.load(TestUtils.libPath("/system_ext", "systemext_private9"));
         } else {
@@ -110,7 +137,17 @@
             });
         }
 
-        if (!isSharedSystemApp()) {
+        boolean loadPrivateProductLib;
+        if (TestUtils.productAppsAreShared()) {
+            // The library is accessible if the app is in either system or
+            // product, because both are loaded as system apps and private product
+            // libs are available for both.
+            loadPrivateProductLib = getAppLocation() == AppLocation.SYSTEM
+                    || getAppLocation() == AppLocation.PRODUCT;
+        } else {
+            loadPrivateProductLib = !noSwitchToVendorOrProductNamespace();
+        }
+        if (loadPrivateProductLib) {
             ProductSharedLib.load(TestUtils.libPath("/product", "product_private9"));
         } else {
             TestUtils.assertLibraryInaccessible(() -> {
@@ -124,7 +161,7 @@
 
     @Test
     public void testLoadPrivateLibrariesViaVendorSharedLibWithAbsolutePaths() {
-        if (isSharedSystemApp()) {
+        if (systemPrivateLibsAccessibleFromAppNamespace() || noSwitchToVendorOrProductNamespace()) {
             VendorSharedLib.load(TestUtils.libPath("/system", "system_private10"));
             VendorSharedLib.load(TestUtils.libPath("/system_ext", "systemext_private10"));
         } else {
@@ -136,11 +173,15 @@
             });
         }
 
-        TestUtils.assertLibraryInaccessible(() -> {
+        if (productPrivateLibsAccessibleFromAppNamespace()) {
             VendorSharedLib.load(TestUtils.libPath("/product", "product_private10"));
-        });
+        } else {
+            TestUtils.assertLibraryInaccessible(() -> {
+                VendorSharedLib.load(TestUtils.libPath("/product", "product_private10"));
+            });
+        }
 
-        if (!isSharedSystemApp()) {
+        if (!noSwitchToVendorOrProductNamespace()) {
             VendorSharedLib.load(TestUtils.libPath("/vendor", "vendor_private10"));
         } else {
             TestUtils.assertLibraryInaccessible(() -> {
diff --git a/libnativeloader/test/src/android/test/lib/TestUtils.java b/libnativeloader/test/src/android/test/lib/TestUtils.java
index 6d2a9db..5f5cd91 100644
--- a/libnativeloader/test/src/android/test/lib/TestUtils.java
+++ b/libnativeloader/test/src/android/test/lib/TestUtils.java
@@ -21,10 +21,12 @@
 
 import static org.junit.Assert.assertThrows;
 
-import android.os.Build;
+import android.os.SystemProperties;
 
 import androidx.test.platform.app.InstrumentationRegistry;
 
+import com.android.modules.utils.build.SdkLevel;
+
 import org.junit.function.ThrowingRunnable;
 
 import java.io.File;
@@ -44,7 +46,13 @@
     // True if we have to skip testing public libraries in the product
     // partition, which got supported in T.
     public static boolean skipPublicProductLibTests() {
-        return Build.VERSION.SDK_INT < 33; // TIRAMISU
+        return !SdkLevel.isAtLeastT();
+    }
+
+    // True if apps in product partitions get shared library namespaces, so we
+    // cannot test that libs in system and system_ext get blocked.
+    public static boolean productAppsAreShared() {
+        return !SdkLevel.isAtLeastU() && SystemProperties.get("ro.product.vndk.version").isEmpty();
     }
 
     // Test that private libs are present, as a safeguard so that the dlopen