blob: a0c77645a3017802a7ae0fdd822692aafd8804bd [file] [log] [blame]
Andreas Gampe84a79032015-09-21 11:25:10 -07001/*
2 * Copyright (C) 2015 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
17import java.lang.reflect.Constructor;
18import java.lang.reflect.Field;
19
20/**
21 * PathClassLoader test.
22 */
23public class Main {
24
25 private static ClassLoader createClassLoader(String dexPath, ClassLoader parent) {
26 try {
27 Class<?> myClassLoaderClass = Class.forName("MyPathClassLoader");
Andreas Gampe166aaee2016-07-18 08:27:23 -070028 Constructor<?> constructor = myClassLoaderClass.getConstructor(String.class,
29 ClassLoader.class);
Andreas Gampe84a79032015-09-21 11:25:10 -070030 return (ClassLoader)constructor.newInstance(dexPath, parent);
31 } catch (Exception e) {
32 // Ups, not available?!?!
33 throw new RuntimeException(e);
34 }
35 }
36
37 /**
38 * Main entry point.
39 */
40 public static void main(String[] args) throws Exception {
41 // Check the class-path for the second file. We'll use that one as the source of the
42 // new classloader.
43 String cp = System.getProperty("java.class.path");
44 if (cp.split(System.getProperty("path.separator")).length != 1) {
45 throw new IllegalStateException("Didn't find exactly one classpath element in " + cp);
46 }
47 if (!cp.endsWith("classloader2.jar")) {
48 throw new IllegalStateException("Don't understand classpath " + cp);
49 }
50 cp = cp.replace("classloader2.jar", "classloader2-ex.jar");
51
52 ClassLoader myClassLoader = createClassLoader(
53 cp, ClassLoader.getSystemClassLoader().getParent());
54
55 // Now load our test class.
56 Class<?> srcClass = A.class;
57 Class<?> exClass = myClassLoader.loadClass("A");
58
59 // First check: classes should be different.
60 if (srcClass == exClass) {
61 throw new IllegalStateException("Loaded class instances are the same");
62 }
63
64 // Secondary checks: get the static field values and make sure they aren't the same.
65 String srcValue = (String)srcClass.getDeclaredField("value").get(null);
66 if (!"Src-A".equals(srcValue)) {
67 throw new IllegalStateException("Expected Src-A, found " + srcValue);
68 }
69 String exValue = (String)exClass.getDeclaredField("value").get(null);
70 if (!"Ex-A".equals(exValue)) {
71 throw new IllegalStateException("Expected Ex-A, found " + exValue);
72 }
73
Jeff Haoc92a7a12016-06-06 11:09:20 -070074 // Try to load a dex file with bad dex code. Use new instance to force verification.
75 try {
76 Class<?> badClass = Main.class.getClassLoader().loadClass("B");
Vladimir Marko72ab6842017-01-20 19:32:50 +000077 System.out.println("Loaded class B.");
Jeff Haoc92a7a12016-06-06 11:09:20 -070078 badClass.newInstance();
Vladimir Marko72ab6842017-01-20 19:32:50 +000079 System.out.println("Should not be able to instantiate B with bad dex bytecode.");
Jeff Haoc92a7a12016-06-06 11:09:20 -070080 } catch (VerifyError e) {
Vladimir Marko72ab6842017-01-20 19:32:50 +000081 System.out.println("Caught VerifyError.");
Jeff Haoc92a7a12016-06-06 11:09:20 -070082 }
83
84 // Make sure the same error is rethrown when reloading the bad class.
85 try {
86 Class<?> badClass = Main.class.getClassLoader().loadClass("B");
Vladimir Marko72ab6842017-01-20 19:32:50 +000087 System.out.println("Loaded class B.");
88 badClass.newInstance();
89 System.out.println("Should not be able to instantiate B with bad dex bytecode.");
90 } catch (NoClassDefFoundError e) {
91 if (e.getCause() instanceof VerifyError) {
92 System.out.println("Caught wrapped VerifyError.");
93 } else {
94 e.printStackTrace();
95 }
Jeff Haoc92a7a12016-06-06 11:09:20 -070096 }
97
Andreas Gampe84a79032015-09-21 11:25:10 -070098 System.out.println("Everything OK.");
99 }
100}