1 /*
2  * Copyright (C) 2022 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.test.lib;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static com.google.common.truth.Truth.assertWithMessage;
21 
22 import static org.junit.Assert.assertThrows;
23 
24 import android.os.SystemProperties;
25 
26 import androidx.test.platform.app.InstrumentationRegistry;
27 
28 import com.android.modules.utils.build.SdkLevel;
29 
30 import org.junit.function.ThrowingRunnable;
31 
32 import java.io.File;
33 
34 public final class TestUtils {
assertLibraryInaccessible(ThrowingRunnable loadLibrary)35     public static void assertLibraryInaccessible(ThrowingRunnable loadLibrary) {
36         Throwable t = assertThrows(UnsatisfiedLinkError.class, loadLibrary);
37         assertThat(t.getMessage())
38                 .containsMatch("dlopen failed: .* (not found|not accessible for the namespace)");
39     }
40 
libPath(String dir, String libName)41     public static String libPath(String dir, String libName) {
42         String libDirName = InstrumentationRegistry.getArguments().getString("libDirName");
43         return dir + "/" + libDirName + "/lib" + libName + ".so";
44     }
45 
46     // True if we have to skip testing public libraries in the product
47     // partition, which got supported in T.
skipPublicProductLibTests()48     public static boolean skipPublicProductLibTests() {
49         return !SdkLevel.isAtLeastT();
50     }
51 
52     // True if apps in product partitions get shared library namespaces, so we
53     // cannot test that libs in system and system_ext get blocked.
productAppsAreShared()54     public static boolean productAppsAreShared() {
55         return !SdkLevel.isAtLeastU() && SystemProperties.get("ro.product.vndk.version").isEmpty();
56     }
57 
58     // True if apps and shared Java libs in system/product/vendor partitions are
59     // able to load private native libs in the same partition.
canLoadPrivateLibsFromSamePartition()60     public static boolean canLoadPrivateLibsFromSamePartition() {
61         return SdkLevel.isAtLeastV();
62     }
63 
64     // Test that private libs are present, as a safeguard so that the dlopen
65     // failures we expect in other tests aren't due to them not being there.
testPrivateLibsExist(String libDir, String libStem)66     public static void testPrivateLibsExist(String libDir, String libStem) {
67         // Remember to update pushPrivateLibs in LibnativeloaderTest.java when
68         // the number of libraries changes.
69         for (int i = 1; i <= 10; ++i) {
70             String libPath = libPath(libDir, libStem + i);
71             assertWithMessage(libPath + " does not exist")
72                     .that(new File(libPath).exists())
73                     .isTrue();
74         }
75     }
76 }
77