1 /* 2 * Copyright (C) 2019 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.app; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotEquals; 21 22 import android.content.pm.SharedLibraryInfo; 23 24 import androidx.test.filters.SmallTest; 25 import androidx.test.runner.AndroidJUnit4; 26 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 import java.util.ArrayList; 31 32 @RunWith(AndroidJUnit4.class) 33 @SmallTest 34 public class ApplicationLoadersTest { 35 36 // a library installed onto the device with no dependencies 37 private static final String LIB_A = "/system/framework/android.hidl.base-V1.0-java.jar"; 38 // a library installed onto the device which only depends on A 39 private static final String LIB_DEP_A = "/system/framework/android.hidl.manager-V1.0-java.jar"; 40 createLib(String zip)41 private static SharedLibraryInfo createLib(String zip) { 42 return new SharedLibraryInfo( 43 zip, null /*packageName*/, null /*codePaths*/, null /*name*/, 0 /*version*/, 44 SharedLibraryInfo.TYPE_BUILTIN, null /*declaringPackage*/, 45 null /*dependentPackages*/, null /*dependencies*/, false /*isNative*/); 46 } 47 48 @Test testGetNonExistantLib()49 public void testGetNonExistantLib() { 50 ApplicationLoaders loaders = new ApplicationLoaders(); 51 assertEquals(null, loaders.getCachedNonBootclasspathSystemLib( 52 "/system/framework/nonexistantlib.jar", null, null, null)); 53 } 54 55 @Test testCacheExistantLib()56 public void testCacheExistantLib() { 57 ApplicationLoaders loaders = new ApplicationLoaders(); 58 SharedLibraryInfo libA = createLib(LIB_A); 59 60 loaders.createAndCacheNonBootclasspathSystemClassLoaders(new SharedLibraryInfo[]{libA}); 61 62 assertNotEquals(null, loaders.getCachedNonBootclasspathSystemLib( 63 LIB_A, null, null, null)); 64 } 65 66 @Test testNonNullParent()67 public void testNonNullParent() { 68 ApplicationLoaders loaders = new ApplicationLoaders(); 69 SharedLibraryInfo libA = createLib(LIB_A); 70 71 ClassLoader parent = ClassLoader.getSystemClassLoader(); 72 assertNotEquals(null, parent); 73 74 loaders.createAndCacheNonBootclasspathSystemClassLoaders(new SharedLibraryInfo[]{libA}); 75 76 assertEquals(null, loaders.getCachedNonBootclasspathSystemLib( 77 LIB_A, parent, null, null)); 78 } 79 80 @Test testNonNullClassLoaderNamespace()81 public void testNonNullClassLoaderNamespace() { 82 ApplicationLoaders loaders = new ApplicationLoaders(); 83 SharedLibraryInfo libA = createLib(LIB_A); 84 85 loaders.createAndCacheNonBootclasspathSystemClassLoaders(new SharedLibraryInfo[]{libA}); 86 87 assertEquals(null, loaders.getCachedNonBootclasspathSystemLib( 88 LIB_A, null, "other classloader", null)); 89 } 90 91 @Test testDifferentSharedLibraries()92 public void testDifferentSharedLibraries() { 93 ApplicationLoaders loaders = new ApplicationLoaders(); 94 SharedLibraryInfo libA = createLib(LIB_A); 95 96 // any other existant lib 97 ClassLoader dep = ClassLoader.getSystemClassLoader(); 98 ArrayList<ClassLoader> sharedLibraries = new ArrayList<>(); 99 sharedLibraries.add(dep); 100 101 loaders.createAndCacheNonBootclasspathSystemClassLoaders(new SharedLibraryInfo[]{libA}); 102 103 assertEquals(null, loaders.getCachedNonBootclasspathSystemLib( 104 LIB_A, null, null, sharedLibraries)); 105 } 106 107 @Test testDependentLibs()108 public void testDependentLibs() { 109 ApplicationLoaders loaders = new ApplicationLoaders(); 110 SharedLibraryInfo libA = createLib(LIB_A); 111 SharedLibraryInfo libB = createLib(LIB_DEP_A); 112 libB.addDependency(libA); 113 114 loaders.createAndCacheNonBootclasspathSystemClassLoaders( 115 new SharedLibraryInfo[]{libA, libB}); 116 117 ClassLoader loadA = loaders.getCachedNonBootclasspathSystemLib( 118 LIB_A, null, null, null); 119 assertNotEquals(null, loadA); 120 121 ArrayList<ClassLoader> sharedLibraries = new ArrayList<>(); 122 sharedLibraries.add(loadA); 123 124 assertNotEquals(null, loaders.getCachedNonBootclasspathSystemLib( 125 LIB_DEP_A, null, null, sharedLibraries)); 126 } 127 128 @Test(expected = IllegalStateException.class) testDependentLibsWrongOrder()129 public void testDependentLibsWrongOrder() { 130 ApplicationLoaders loaders = new ApplicationLoaders(); 131 SharedLibraryInfo libA = createLib(LIB_A); 132 SharedLibraryInfo libB = createLib(LIB_DEP_A); 133 libB.addDependency(libA); 134 135 loaders.createAndCacheNonBootclasspathSystemClassLoaders( 136 new SharedLibraryInfo[]{libB, libA}); 137 } 138 } 139