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