Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 com.android.server.pm.dex; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 20 | import static org.junit.Assert.assertNotNull; |
| 21 | import static org.junit.Assert.assertNull; |
| 22 | import static org.junit.Assert.assertTrue; |
Shubham Ajmera | 557c054 | 2017-09-05 10:20:41 -0700 | [diff] [blame] | 23 | import static org.junit.Assert.fail; |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 24 | |
| 25 | import android.content.pm.ApplicationInfo; |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 26 | import android.content.pm.SharedLibraryInfo; |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 27 | import android.util.SparseArray; |
| 28 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 29 | import androidx.test.filters.SmallTest; |
| 30 | import androidx.test.runner.AndroidJUnit4; |
| 31 | |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 32 | import dalvik.system.DelegateLastClassLoader; |
Calin Juravle | f8c14e9 | 2017-07-24 17:56:51 -0700 | [diff] [blame] | 33 | import dalvik.system.DexClassLoader; |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 34 | import dalvik.system.PathClassLoader; |
| 35 | |
Nicolas Geoffray | 03dba3b | 2018-12-20 09:47:00 +0000 | [diff] [blame] | 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 39 | import java.io.File; |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 40 | import java.util.ArrayList; |
Nicolas Geoffray | 03dba3b | 2018-12-20 09:47:00 +0000 | [diff] [blame] | 41 | import java.util.Arrays; |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 42 | import java.util.Collections; |
| 43 | import java.util.List; |
| 44 | |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 45 | @RunWith(AndroidJUnit4.class) |
| 46 | @SmallTest |
| 47 | public class DexoptUtilsTest { |
Calin Juravle | f8c14e9 | 2017-07-24 17:56:51 -0700 | [diff] [blame] | 48 | private static final String DEX_CLASS_LOADER_NAME = DexClassLoader.class.getName(); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 49 | private static final String PATH_CLASS_LOADER_NAME = PathClassLoader.class.getName(); |
| 50 | private static final String DELEGATE_LAST_CLASS_LOADER_NAME = |
| 51 | DelegateLastClassLoader.class.getName(); |
| 52 | |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 53 | private static class TestData { |
| 54 | ApplicationInfo info; |
| 55 | boolean[] pathsWithCode; |
| 56 | } |
| 57 | |
| 58 | private TestData createMockApplicationInfo(String baseClassLoader, boolean addSplits, |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 59 | boolean addSplitDependencies) { |
| 60 | ApplicationInfo ai = new ApplicationInfo(); |
| 61 | String codeDir = "/data/app/mock.android.com"; |
| 62 | ai.setBaseCodePath(codeDir + "/base.dex"); |
| 63 | ai.classLoaderName = baseClassLoader; |
Calin Juravle | 305aeea | 2017-07-14 16:40:07 -0700 | [diff] [blame] | 64 | ai.privateFlags = ai.privateFlags | ApplicationInfo.PRIVATE_FLAG_ISOLATED_SPLIT_LOADING; |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 65 | boolean[] pathsWithCode; |
| 66 | if (!addSplits) { |
| 67 | pathsWithCode = new boolean[] {true}; |
| 68 | } else { |
| 69 | pathsWithCode = new boolean[9]; |
| 70 | Arrays.fill(pathsWithCode, true); |
| 71 | pathsWithCode[7] = false; // config split |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 72 | |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 73 | ai.setSplitCodePaths(new String[]{ |
| 74 | codeDir + "/base-1.dex", |
| 75 | codeDir + "/base-2.dex", |
| 76 | codeDir + "/base-3.dex", |
| 77 | codeDir + "/base-4.dex", |
| 78 | codeDir + "/base-5.dex", |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 79 | codeDir + "/base-6.dex", |
| 80 | codeDir + "/config-split-7.dex", |
| 81 | codeDir + "/feature-no-deps.dex"}); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 82 | |
| 83 | ai.splitClassLoaderNames = new String[]{ |
| 84 | DELEGATE_LAST_CLASS_LOADER_NAME, |
| 85 | DELEGATE_LAST_CLASS_LOADER_NAME, |
| 86 | PATH_CLASS_LOADER_NAME, |
Calin Juravle | f8c14e9 | 2017-07-24 17:56:51 -0700 | [diff] [blame] | 87 | DEX_CLASS_LOADER_NAME, |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 88 | PATH_CLASS_LOADER_NAME, |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 89 | null, // A null class loader name should default to PathClassLoader. |
| 90 | null, // The config split gets a null class loader. |
| 91 | null}; // The feature split with no dependency and no specified class loader. |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 92 | if (addSplitDependencies) { |
| 93 | ai.splitDependencies = new SparseArray<>(ai.splitClassLoaderNames.length + 1); |
| 94 | ai.splitDependencies.put(0, new int[] {-1}); // base has no dependency |
| 95 | ai.splitDependencies.put(1, new int[] {2}); // split 1 depends on 2 |
| 96 | ai.splitDependencies.put(2, new int[] {4}); // split 2 depends on 4 |
| 97 | ai.splitDependencies.put(3, new int[] {4}); // split 3 depends on 4 |
| 98 | ai.splitDependencies.put(4, new int[] {0}); // split 4 depends on base |
| 99 | ai.splitDependencies.put(5, new int[] {0}); // split 5 depends on base |
| 100 | ai.splitDependencies.put(6, new int[] {5}); // split 6 depends on 5 |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 101 | // Do not add the config split to the dependency list. |
| 102 | // Do not add the feature split with no dependency to the dependency list. |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 105 | TestData data = new TestData(); |
| 106 | data.info = ai; |
| 107 | data.pathsWithCode = pathsWithCode; |
| 108 | return data; |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 111 | private List<SharedLibraryInfo> createMockSharedLibrary(String [] sharedLibrary) { |
| 112 | SharedLibraryInfo info = new SharedLibraryInfo(null, null, Arrays.asList(sharedLibrary), |
| 113 | null, 0L, SharedLibraryInfo.TYPE_STATIC, null, null, null); |
| 114 | ArrayList<SharedLibraryInfo> libraries = new ArrayList<>(); |
| 115 | libraries.add(info); |
| 116 | return libraries; |
| 117 | } |
| 118 | |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 119 | @Test |
| 120 | public void testSplitChain() { |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 121 | TestData data = createMockApplicationInfo(PATH_CLASS_LOADER_NAME, true, true); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 122 | List<SharedLibraryInfo> sharedLibrary = |
| 123 | createMockSharedLibrary(new String[] {"a.dex", "b.dex"}); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 124 | String[] contexts = DexoptUtils.getClassLoaderContexts( |
| 125 | data.info, sharedLibrary, data.pathsWithCode); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 126 | |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 127 | assertEquals(9, contexts.length); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 128 | assertEquals("PCL[]{PCL[a.dex:b.dex]}", contexts[0]); |
| 129 | assertEquals("DLC[];DLC[base-2.dex];PCL[base-4.dex];PCL[base.dex]{PCL[a.dex:b.dex]}", |
Calin Juravle | 305aeea | 2017-07-14 16:40:07 -0700 | [diff] [blame] | 130 | contexts[1]); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 131 | assertEquals("DLC[];PCL[base-4.dex];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[2]); |
| 132 | assertEquals("PCL[];PCL[base-4.dex];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[3]); |
| 133 | assertEquals("PCL[];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[4]); |
| 134 | assertEquals("PCL[];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[5]); |
| 135 | assertEquals("PCL[];PCL[base-5.dex];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[6]); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 136 | assertEquals(null, contexts[7]); // config split |
| 137 | assertEquals("PCL[]", contexts[8]); // feature split with no dependency |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void testSplitChainNoSplitDependencies() { |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 142 | TestData data = createMockApplicationInfo(PATH_CLASS_LOADER_NAME, true, false); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 143 | List<SharedLibraryInfo> sharedLibrary = |
| 144 | createMockSharedLibrary(new String[] {"a.dex", "b.dex"}); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 145 | String[] contexts = DexoptUtils.getClassLoaderContexts( |
| 146 | data.info, sharedLibrary, data.pathsWithCode); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 147 | |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 148 | assertEquals(9, contexts.length); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 149 | assertEquals("PCL[]{PCL[a.dex:b.dex]}", contexts[0]); |
| 150 | assertEquals("PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[1]); |
| 151 | assertEquals("PCL[base.dex:base-1.dex]{PCL[a.dex:b.dex]}", contexts[2]); |
| 152 | assertEquals("PCL[base.dex:base-1.dex:base-2.dex]{PCL[a.dex:b.dex]}", contexts[3]); |
Calin Juravle | 305aeea | 2017-07-14 16:40:07 -0700 | [diff] [blame] | 153 | assertEquals( |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 154 | "PCL[base.dex:base-1.dex:base-2.dex:base-3.dex]{PCL[a.dex:b.dex]}", |
| 155 | contexts[4]); |
| 156 | assertEquals( |
| 157 | "PCL[base.dex:base-1.dex:base-2.dex:base-3.dex:base-4.dex]{PCL[a.dex:b.dex]}", |
Calin Juravle | 305aeea | 2017-07-14 16:40:07 -0700 | [diff] [blame] | 158 | contexts[5]); |
| 159 | assertEquals( |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 160 | "PCL[base.dex:base-1.dex:base-2.dex:base-3.dex:base-4.dex:base-5.dex]{PCL[a.dex:b.dex]}", |
Calin Juravle | 305aeea | 2017-07-14 16:40:07 -0700 | [diff] [blame] | 161 | contexts[6]); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 162 | assertEquals(null, contexts[7]); // config split |
| 163 | assertEquals( |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 164 | "PCL[base.dex:base-1.dex:base-2.dex:base-3.dex:base-4.dex:base-5.dex:base-6.dex:config-split-7.dex]{PCL[a.dex:b.dex]}", |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 165 | contexts[8]); // feature split with no dependency |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Test |
Calin Juravle | 305aeea | 2017-07-14 16:40:07 -0700 | [diff] [blame] | 169 | public void testSplitChainNoIsolationNoSharedLibrary() { |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 170 | TestData data = createMockApplicationInfo(PATH_CLASS_LOADER_NAME, true, true); |
| 171 | data.info.privateFlags = data.info.privateFlags |
| 172 | & (~ApplicationInfo.PRIVATE_FLAG_ISOLATED_SPLIT_LOADING); |
| 173 | String[] contexts = DexoptUtils.getClassLoaderContexts( |
| 174 | data.info, null, data.pathsWithCode); |
Calin Juravle | 305aeea | 2017-07-14 16:40:07 -0700 | [diff] [blame] | 175 | |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 176 | assertEquals(9, contexts.length); |
Calin Juravle | 305aeea | 2017-07-14 16:40:07 -0700 | [diff] [blame] | 177 | assertEquals("PCL[]", contexts[0]); |
| 178 | assertEquals("PCL[base.dex]", contexts[1]); |
| 179 | assertEquals("PCL[base.dex:base-1.dex]", contexts[2]); |
| 180 | assertEquals("PCL[base.dex:base-1.dex:base-2.dex]", contexts[3]); |
| 181 | assertEquals("PCL[base.dex:base-1.dex:base-2.dex:base-3.dex]", contexts[4]); |
| 182 | assertEquals("PCL[base.dex:base-1.dex:base-2.dex:base-3.dex:base-4.dex]", contexts[5]); |
| 183 | assertEquals( |
| 184 | "PCL[base.dex:base-1.dex:base-2.dex:base-3.dex:base-4.dex:base-5.dex]", |
| 185 | contexts[6]); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 186 | assertEquals(null, contexts[7]); // config split |
| 187 | assertEquals( |
| 188 | "PCL[base.dex:base-1.dex:base-2.dex:base-3.dex:base-4.dex:base-5.dex:base-6.dex:config-split-7.dex]", |
| 189 | contexts[8]); // feature split with no dependency |
Calin Juravle | 305aeea | 2017-07-14 16:40:07 -0700 | [diff] [blame] | 190 | } |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 191 | |
Calin Juravle | 305aeea | 2017-07-14 16:40:07 -0700 | [diff] [blame] | 192 | @Test |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 193 | public void testSplitChainNoSharedLibraries() { |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 194 | TestData data = createMockApplicationInfo( |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 195 | DELEGATE_LAST_CLASS_LOADER_NAME, true, true); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 196 | String[] contexts = DexoptUtils.getClassLoaderContexts( |
| 197 | data.info, null, data.pathsWithCode); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 198 | |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 199 | assertEquals(9, contexts.length); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 200 | assertEquals("DLC[]", contexts[0]); |
| 201 | assertEquals("DLC[];DLC[base-2.dex];PCL[base-4.dex];DLC[base.dex]", contexts[1]); |
| 202 | assertEquals("DLC[];PCL[base-4.dex];DLC[base.dex]", contexts[2]); |
| 203 | assertEquals("PCL[];PCL[base-4.dex];DLC[base.dex]", contexts[3]); |
| 204 | assertEquals("PCL[];DLC[base.dex]", contexts[4]); |
| 205 | assertEquals("PCL[];DLC[base.dex]", contexts[5]); |
| 206 | assertEquals("PCL[];PCL[base-5.dex];DLC[base.dex]", contexts[6]); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 207 | assertEquals(null, contexts[7]); // config split |
| 208 | assertEquals("PCL[]", contexts[8]); // feature split with no dependency |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Test |
| 212 | public void testSplitChainWithNullPrimaryClassLoader() { |
| 213 | // A null classLoaderName should mean PathClassLoader. |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 214 | TestData data = createMockApplicationInfo(null, true, true); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 215 | List<SharedLibraryInfo> sharedLibrary = |
| 216 | createMockSharedLibrary(new String[] {"a.dex", "b.dex"}); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 217 | String[] contexts = DexoptUtils.getClassLoaderContexts( |
| 218 | data.info, sharedLibrary, data.pathsWithCode); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 219 | |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 220 | assertEquals(9, contexts.length); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 221 | assertEquals("PCL[]{PCL[a.dex:b.dex]}", contexts[0]); |
| 222 | assertEquals( |
| 223 | "DLC[];DLC[base-2.dex];PCL[base-4.dex];PCL[base.dex]{PCL[a.dex:b.dex]}", |
| 224 | contexts[1]); |
| 225 | assertEquals("DLC[];PCL[base-4.dex];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[2]); |
| 226 | assertEquals("PCL[];PCL[base-4.dex];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[3]); |
| 227 | assertEquals("PCL[];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[4]); |
| 228 | assertEquals("PCL[];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[5]); |
| 229 | assertEquals("PCL[];PCL[base-5.dex];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[6]); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 230 | assertEquals(null, contexts[7]); // config split |
| 231 | assertEquals("PCL[]", contexts[8]); // feature split with no dependency |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Test |
| 235 | public void tesNoSplits() { |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 236 | TestData data = createMockApplicationInfo(PATH_CLASS_LOADER_NAME, false, false); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 237 | List<SharedLibraryInfo> sharedLibrary = |
| 238 | createMockSharedLibrary(new String[] {"a.dex", "b.dex"}); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 239 | String[] contexts = DexoptUtils.getClassLoaderContexts( |
| 240 | data.info, sharedLibrary, data.pathsWithCode); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 241 | |
| 242 | assertEquals(1, contexts.length); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 243 | assertEquals("PCL[]{PCL[a.dex:b.dex]}", contexts[0]); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | @Test |
| 247 | public void tesNoSplitsNullClassLoaderName() { |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 248 | TestData data = createMockApplicationInfo(null, false, false); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 249 | List<SharedLibraryInfo> sharedLibrary = |
| 250 | createMockSharedLibrary(new String[] {"a.dex", "b.dex"}); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 251 | String[] contexts = DexoptUtils.getClassLoaderContexts( |
| 252 | data.info, sharedLibrary, data.pathsWithCode); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 253 | |
| 254 | assertEquals(1, contexts.length); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 255 | assertEquals("PCL[]{PCL[a.dex:b.dex]}", contexts[0]); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Test |
| 259 | public void tesNoSplitDelegateLast() { |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 260 | TestData data = createMockApplicationInfo( |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 261 | DELEGATE_LAST_CLASS_LOADER_NAME, false, false); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 262 | List<SharedLibraryInfo> sharedLibrary = |
| 263 | createMockSharedLibrary(new String[] {"a.dex", "b.dex"}); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 264 | String[] contexts = DexoptUtils.getClassLoaderContexts( |
| 265 | data.info, sharedLibrary, data.pathsWithCode); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 266 | |
| 267 | assertEquals(1, contexts.length); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 268 | assertEquals("DLC[]{PCL[a.dex:b.dex]}", contexts[0]); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Test |
| 272 | public void tesNoSplitsNoSharedLibraries() { |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 273 | TestData data = createMockApplicationInfo(PATH_CLASS_LOADER_NAME, false, false); |
| 274 | String[] contexts = DexoptUtils.getClassLoaderContexts( |
| 275 | data.info, null, data.pathsWithCode); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 276 | |
| 277 | assertEquals(1, contexts.length); |
| 278 | assertEquals("PCL[]", contexts[0]); |
| 279 | } |
| 280 | |
| 281 | @Test |
| 282 | public void tesNoSplitDelegateLastNoSharedLibraries() { |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 283 | TestData data = createMockApplicationInfo( |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 284 | DELEGATE_LAST_CLASS_LOADER_NAME, false, false); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 285 | String[] contexts = DexoptUtils.getClassLoaderContexts( |
| 286 | data.info, null, data.pathsWithCode); |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 287 | |
| 288 | assertEquals(1, contexts.length); |
| 289 | assertEquals("DLC[]", contexts[0]); |
| 290 | } |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 291 | |
| 292 | @Test |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 293 | public void testContextWithNoCode() { |
| 294 | TestData data = createMockApplicationInfo(null, true, false); |
| 295 | Arrays.fill(data.pathsWithCode, false); |
| 296 | |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 297 | List<SharedLibraryInfo> sharedLibrary = |
| 298 | createMockSharedLibrary(new String[] {"a.dex", "b.dex"}); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 299 | String[] contexts = DexoptUtils.getClassLoaderContexts( |
| 300 | data.info, sharedLibrary, data.pathsWithCode); |
| 301 | |
| 302 | assertEquals(9, contexts.length); |
| 303 | assertEquals(null, contexts[0]); |
| 304 | assertEquals(null, contexts[1]); |
| 305 | assertEquals(null, contexts[2]); |
| 306 | assertEquals(null, contexts[3]); |
| 307 | assertEquals(null, contexts[4]); |
| 308 | assertEquals(null, contexts[5]); |
| 309 | assertEquals(null, contexts[6]); |
| 310 | assertEquals(null, contexts[7]); |
| 311 | } |
| 312 | |
| 313 | @Test |
| 314 | public void testContextBaseNoCode() { |
| 315 | TestData data = createMockApplicationInfo(null, true, true); |
| 316 | data.pathsWithCode[0] = false; |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 317 | List<SharedLibraryInfo> sharedLibrary = |
| 318 | createMockSharedLibrary(new String[] {"a.dex", "b.dex"}); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 319 | String[] contexts = DexoptUtils.getClassLoaderContexts( |
| 320 | data.info, sharedLibrary, data.pathsWithCode); |
| 321 | |
| 322 | assertEquals(9, contexts.length); |
| 323 | assertEquals(null, contexts[0]); |
Nicolas Geoffray | 8d144eb | 2018-11-15 12:59:52 +0000 | [diff] [blame] | 324 | assertEquals( |
| 325 | "DLC[];DLC[base-2.dex];PCL[base-4.dex];PCL[base.dex]{PCL[a.dex:b.dex]}", |
| 326 | contexts[1]); |
| 327 | assertEquals("DLC[];PCL[base-4.dex];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[2]); |
| 328 | assertEquals("PCL[];PCL[base-4.dex];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[3]); |
| 329 | assertEquals("PCL[];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[4]); |
| 330 | assertEquals("PCL[];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[5]); |
| 331 | assertEquals("PCL[];PCL[base-5.dex];PCL[base.dex]{PCL[a.dex:b.dex]}", contexts[6]); |
Calin Juravle | da09815 | 2017-09-01 17:30:01 -0700 | [diff] [blame] | 332 | assertEquals(null, contexts[7]); |
| 333 | } |
| 334 | |
| 335 | @Test |
Nicolas Geoffray | 653356f | 2019-02-26 23:24:34 +0000 | [diff] [blame] | 336 | public void testSharedLibraryContext() { |
| 337 | SharedLibraryInfo sharedLibrary = |
| 338 | createMockSharedLibrary(new String[] {"a.dex", "b.dex"}).get(0); |
| 339 | String context = DexoptUtils.getClassLoaderContext(sharedLibrary); |
| 340 | assertEquals("PCL[]", context); |
| 341 | |
| 342 | SharedLibraryInfo otherSharedLibrary = |
| 343 | createMockSharedLibrary(new String[] {"c.dex"}).get(0); |
| 344 | otherSharedLibrary.addDependency(sharedLibrary); |
| 345 | context = DexoptUtils.getClassLoaderContext(otherSharedLibrary); |
| 346 | assertEquals("PCL[]{PCL[a.dex:b.dex]}", context); |
| 347 | } |
| 348 | |
| 349 | @Test |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 350 | public void testProcessContextForDexLoad() { |
| 351 | List<String> classLoaders = Arrays.asList( |
| 352 | DELEGATE_LAST_CLASS_LOADER_NAME, |
| 353 | PATH_CLASS_LOADER_NAME, |
| 354 | PATH_CLASS_LOADER_NAME); |
| 355 | List<String> classPaths = Arrays.asList( |
| 356 | String.join(File.pathSeparator, "foo.dex", "bar.dex"), |
| 357 | String.join(File.pathSeparator, "parent1.dex"), |
| 358 | String.join(File.pathSeparator, "parent2.dex", "parent3.dex")); |
| 359 | String[] context = DexoptUtils.processContextForDexLoad(classLoaders, classPaths); |
| 360 | assertNotNull(context); |
| 361 | assertEquals(2, context.length); |
| 362 | assertEquals("DLC[];PCL[parent1.dex];PCL[parent2.dex:parent3.dex]", context[0]); |
| 363 | assertEquals("DLC[foo.dex];PCL[parent1.dex];PCL[parent2.dex:parent3.dex]", context[1]); |
| 364 | } |
| 365 | |
| 366 | @Test |
| 367 | public void testProcessContextForDexLoadSingleElement() { |
| 368 | List<String> classLoaders = Arrays.asList(PATH_CLASS_LOADER_NAME); |
| 369 | List<String> classPaths = Arrays.asList( |
| 370 | String.join(File.pathSeparator, "foo.dex", "bar.dex", "zoo.dex")); |
| 371 | String[] context = DexoptUtils.processContextForDexLoad(classLoaders, classPaths); |
| 372 | assertNotNull(context); |
| 373 | assertEquals(3, context.length); |
| 374 | assertEquals("PCL[]", context[0]); |
| 375 | assertEquals("PCL[foo.dex]", context[1]); |
| 376 | assertEquals("PCL[foo.dex:bar.dex]", context[2]); |
| 377 | } |
| 378 | |
| 379 | @Test |
| 380 | public void testProcessContextForDexLoadUnsupported() { |
| 381 | List<String> classLoaders = Arrays.asList( |
| 382 | DELEGATE_LAST_CLASS_LOADER_NAME, |
| 383 | "unsupported.class.loader"); |
| 384 | List<String> classPaths = Arrays.asList( |
| 385 | String.join(File.pathSeparator, "foo.dex", "bar.dex"), |
| 386 | String.join(File.pathSeparator, "parent1.dex")); |
| 387 | String[] context = DexoptUtils.processContextForDexLoad(classLoaders, classPaths); |
| 388 | assertNull(context); |
| 389 | } |
| 390 | |
| 391 | @Test |
Alan Stokes | b6c3a60 | 2018-11-02 12:10:42 +0000 | [diff] [blame] | 392 | public void testProcessContextForDexLoadNoClassPath() { |
| 393 | List<String> classLoaders = Arrays.asList( |
| 394 | DELEGATE_LAST_CLASS_LOADER_NAME, |
| 395 | PATH_CLASS_LOADER_NAME); |
| 396 | List<String> classPaths = Arrays.asList( |
| 397 | String.join(File.pathSeparator, "foo.dex", "bar.dex"), |
| 398 | null); |
| 399 | String[] context = DexoptUtils.processContextForDexLoad(classLoaders, classPaths); |
| 400 | assertNull(context); |
| 401 | } |
| 402 | |
| 403 | @Test |
Calin Juravle | f1ff36f | 2017-07-22 12:33:41 -0700 | [diff] [blame] | 404 | public void testProcessContextForDexLoadIllegalCallEmptyList() { |
| 405 | boolean gotException = false; |
| 406 | try { |
| 407 | DexoptUtils.processContextForDexLoad(Collections.emptyList(), Collections.emptyList()); |
| 408 | } catch (IllegalArgumentException ignore) { |
| 409 | gotException = true; |
| 410 | } |
| 411 | assertTrue(gotException); |
| 412 | } |
| 413 | |
| 414 | @Test |
| 415 | public void testProcessContextForDexLoadIllegalCallDifferentSize() { |
| 416 | boolean gotException = false; |
| 417 | try { |
| 418 | DexoptUtils.processContextForDexLoad(Collections.emptyList(), Arrays.asList("a")); |
| 419 | } catch (IllegalArgumentException ignore) { |
| 420 | gotException = true; |
| 421 | } |
| 422 | assertTrue(gotException); |
| 423 | } |
Shubham Ajmera | 557c054 | 2017-09-05 10:20:41 -0700 | [diff] [blame] | 424 | |
| 425 | @Test |
| 426 | public void testEncodeClassLoader() { |
Shubham Ajmera | 557c054 | 2017-09-05 10:20:41 -0700 | [diff] [blame] | 427 | assertEquals("PCL[xyz]", DexoptUtils.encodeClassLoader("xyz", |
| 428 | "dalvik.system.PathClassLoader")); |
| 429 | assertEquals("PCL[xyz]", DexoptUtils.encodeClassLoader("xyz", |
| 430 | "dalvik.system.DexClassLoader")); |
| 431 | assertEquals("DLC[xyz]", DexoptUtils.encodeClassLoader("xyz", |
| 432 | "dalvik.system.DelegateLastClassLoader")); |
| 433 | assertEquals("PCL[xyz]", DexoptUtils.encodeClassLoader("xyz", null)); |
| 434 | assertEquals("abc[xyz]", DexoptUtils.encodeClassLoader("xyz", "abc")); |
| 435 | |
| 436 | try { |
| 437 | DexoptUtils.encodeClassLoader(null, "abc"); |
| 438 | fail(); // Exception should be caught. |
| 439 | } catch (NullPointerException expected) {} |
| 440 | } |
| 441 | |
| 442 | @Test |
| 443 | public void testEncodeClassLoaderChain() { |
Shubham Ajmera | 557c054 | 2017-09-05 10:20:41 -0700 | [diff] [blame] | 444 | assertEquals("PCL[a];DLC[b]", DexoptUtils.encodeClassLoaderChain("PCL[a]", |
| 445 | "DLC[b]")); |
Shubham Ajmera | 557c054 | 2017-09-05 10:20:41 -0700 | [diff] [blame] | 446 | try { |
| 447 | DexoptUtils.encodeClassLoaderChain("a", null); |
| 448 | fail(); // exception is expected |
| 449 | } catch (NullPointerException expected) {} |
| 450 | |
| 451 | try { |
| 452 | DexoptUtils.encodeClassLoaderChain(null, "b"); |
| 453 | fail(); // exception is expected |
| 454 | } catch (NullPointerException expected) {} |
| 455 | } |
Calin Juravle | 19da1cf | 2017-07-12 18:52:49 -0700 | [diff] [blame] | 456 | } |