blob: 89dadcee688da970b7abf6adbcf872c2fe3d258c [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");
28 Constructor constructor = myClassLoaderClass.getConstructor(String.class,
29 ClassLoader.class);
30 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");
77 badClass.newInstance();
78 System.out.println("Should not be able to load class from bad dex file.");
79 } catch (VerifyError e) {
80 }
81
82 // Make sure the same error is rethrown when reloading the bad class.
83 try {
84 Class<?> badClass = Main.class.getClassLoader().loadClass("B");
85 System.out.println("Should not be able to load class from bad dex file.");
86 } catch (VerifyError e) {
87 }
88
Andreas Gampe84a79032015-09-21 11:25:10 -070089 System.out.println("Everything OK.");
90 }
91}