184a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe/* 284a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * Copyright (C) 2015 The Android Open Source Project 384a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * 484a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * Licensed under the Apache License, Version 2.0 (the "License"); 584a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * you may not use this file except in compliance with the License. 684a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * You may obtain a copy of the License at 784a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * 884a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * http://www.apache.org/licenses/LICENSE-2.0 984a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * 1084a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * Unless required by applicable law or agreed to in writing, software 1184a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * distributed under the License is distributed on an "AS IS" BASIS, 1284a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1384a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * See the License for the specific language governing permissions and 1484a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * limitations under the License. 1584a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe */ 1684a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe 1784a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampeimport java.lang.reflect.Constructor; 1884a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampeimport java.lang.reflect.Field; 1984a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe 2084a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe/** 2184a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * PathClassLoader test. 2284a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe */ 2384a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampepublic class Main { 2484a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe 2584a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe private static ClassLoader createClassLoader(String dexPath, ClassLoader parent) { 2684a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe try { 2784a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe Class<?> myClassLoaderClass = Class.forName("MyPathClassLoader"); 2884a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe Constructor constructor = myClassLoaderClass.getConstructor(String.class, 2984a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe ClassLoader.class); 3084a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe return (ClassLoader)constructor.newInstance(dexPath, parent); 3184a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe } catch (Exception e) { 3284a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe // Ups, not available?!?! 3384a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe throw new RuntimeException(e); 3484a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe } 3584a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe } 3684a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe 3784a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe /** 3884a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe * Main entry point. 3984a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe */ 4084a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe public static void main(String[] args) throws Exception { 4184a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe // Check the class-path for the second file. We'll use that one as the source of the 4284a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe // new classloader. 4384a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe String cp = System.getProperty("java.class.path"); 4484a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe if (cp.split(System.getProperty("path.separator")).length != 1) { 4584a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe throw new IllegalStateException("Didn't find exactly one classpath element in " + cp); 4684a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe } 4784a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe if (!cp.endsWith("classloader2.jar")) { 4884a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe throw new IllegalStateException("Don't understand classpath " + cp); 4984a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe } 5084a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe cp = cp.replace("classloader2.jar", "classloader2-ex.jar"); 5184a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe 5284a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe ClassLoader myClassLoader = createClassLoader( 5384a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe cp, ClassLoader.getSystemClassLoader().getParent()); 5484a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe 5584a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe // Now load our test class. 5684a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe Class<?> srcClass = A.class; 5784a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe Class<?> exClass = myClassLoader.loadClass("A"); 5884a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe 5984a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe // First check: classes should be different. 6084a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe if (srcClass == exClass) { 6184a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe throw new IllegalStateException("Loaded class instances are the same"); 6284a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe } 6384a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe 6484a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe // Secondary checks: get the static field values and make sure they aren't the same. 6584a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe String srcValue = (String)srcClass.getDeclaredField("value").get(null); 6684a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe if (!"Src-A".equals(srcValue)) { 6784a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe throw new IllegalStateException("Expected Src-A, found " + srcValue); 6884a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe } 6984a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe String exValue = (String)exClass.getDeclaredField("value").get(null); 7084a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe if (!"Ex-A".equals(exValue)) { 7184a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe throw new IllegalStateException("Expected Ex-A, found " + exValue); 7284a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe } 7384a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe 747c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao // Try to load a dex file with bad dex code. Use new instance to force verification. 757c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao try { 767c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao Class<?> badClass = Main.class.getClassLoader().loadClass("B"); 777c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao badClass.newInstance(); 787c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao System.out.println("Should not be able to load class from bad dex file."); 797c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao } catch (VerifyError e) { 807c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao } 817c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao 827c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao // Make sure the same error is rethrown when reloading the bad class. 837c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao try { 847c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao Class<?> badClass = Main.class.getClassLoader().loadClass("B"); 857c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao System.out.println("Should not be able to load class from bad dex file."); 867c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao } catch (VerifyError e) { 877c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao } 887c8aa8357196781c811a73d2eb66aaaa1681ce36Jeff Hao 8984a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe System.out.println("Everything OK."); 9084a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe } 9184a790358b95e0f79be3a6e69ae9a43dc113f921Andreas Gampe} 92