1 /*
2  * Copyright (C) 2021 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 import dalvik.system.PathClassLoader;
18 import java.io.File;
19 import java.lang.reflect.Method;
20 import java.nio.file.Files;
21 import java.util.Arrays;
22 
23 public class Main {
24 
main(String[] args)25   public static void main(String[] args) throws Exception {
26     System.loadLibrary(args[0]);
27 
28     // Enable hidden API checks in case they are disabled by default.
29     init();
30 
31     // Put the classes with hiddenapi bits in the boot classpath.
32     appendToBootClassLoader(DEX_PARENT_BOOT, /* isCorePlatform */ false);
33 
34     // Create a new class loader so the TestCase class sees the InheritAbstract classes in the boot
35     // classpath.
36     ClassLoader childLoader = new PathClassLoader(DEX_CHILD, Object.class.getClassLoader());
37     Class<?> cls = Class.forName("TestCase", true, childLoader);
38     Method m = cls.getDeclaredMethod("test");
39     m.invoke(null);
40 
41     // Create a new native library which 'childLoader' can load.
42     String absoluteLibraryPath = getNativeLibFileName(args[0]);
43 
44     // Do the test for JNI code.
45     m = cls.getDeclaredMethod("testNative", String.class);
46     m.invoke(null, createNativeLibCopy(absoluteLibraryPath));
47   }
48 
49   // Tries to find the absolute path of the native library whose basename is 'arg'.
getNativeLibFileName(String arg)50   private static String getNativeLibFileName(String arg) throws Exception {
51     String libName = System.mapLibraryName(arg);
52     Method libPathsMethod = Runtime.class.getDeclaredMethod("getLibPaths");
53     libPathsMethod.setAccessible(true);
54     String[] libPaths = (String[]) libPathsMethod.invoke(Runtime.getRuntime());
55     String nativeLibFileName = null;
56     for (String p : libPaths) {
57       String candidate = p + libName;
58       if (new File(candidate).exists()) {
59         nativeLibFileName = candidate;
60         break;
61       }
62     }
63     if (nativeLibFileName == null) {
64       throw new IllegalStateException("Didn't find " + libName + " in " +
65           Arrays.toString(libPaths));
66     }
67     return nativeLibFileName;
68   }
69 
70   // Copy native library to a new file with a unique name so it does not
71   // conflict with other loaded instance of the same binary file.
createNativeLibCopy(String nativeLibFileName)72   private static String createNativeLibCopy(String nativeLibFileName) throws Exception {
73     String tempFileName = System.mapLibraryName("hiddenapitest");
74     File tempFile = new File(System.getenv("DEX_LOCATION"), tempFileName);
75     Files.copy(new File(nativeLibFileName).toPath(), tempFile.toPath());
76     return tempFile.getAbsolutePath();
77   }
78 
79   private static final String DEX_PARENT_BOOT =
80       new File(new File(System.getenv("DEX_LOCATION"), "res"), "boot.jar").getAbsolutePath();
81   private static final String DEX_CHILD =
82       new File(System.getenv("DEX_LOCATION"), "817-hiddenapi-ex.jar").getAbsolutePath();
83 
appendToBootClassLoader(String dexPath, boolean isCorePlatform)84   private static native int appendToBootClassLoader(String dexPath, boolean isCorePlatform);
init()85   private static native void init();
86 }
87