Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Alex Light | e40dd38 | 2017-01-25 17:44:53 -0800 | [diff] [blame] | 17 | import java.lang.reflect.*; |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 18 | import java.util.Base64; |
| 19 | |
Alex Light | e40dd38 | 2017-01-25 17:44:53 -0800 | [diff] [blame] | 20 | class Main { |
| 21 | public static String TEST_NAME = "935-non-retransformable"; |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * base64 encoded class/dex file for |
| 25 | * class Transform { |
| 26 | * public void sayHi() { |
| 27 | * System.out.println("Hello"); |
| 28 | * } |
| 29 | * public void sayGoodbye() { |
| 30 | * System.out.println("Goodbye"); |
| 31 | * } |
| 32 | * } |
| 33 | */ |
| 34 | private static final byte[] CLASS_BYTES = Base64.getDecoder().decode( |
| 35 | "yv66vgAAADQAHwoABwAQCQARABIIABMKABQAFQgAFgcAFwcAGAEABjxpbml0PgEAAygpVgEABENv" + |
| 36 | "ZGUBAA9MaW5lTnVtYmVyVGFibGUBAAVzYXlIaQEACnNheUdvb2RieWUBAApTb3VyY2VGaWxlAQAO" + |
| 37 | "VHJhbnNmb3JtLmphdmEMAAgACQcAGQwAGgAbAQAFSGVsbG8HABwMAB0AHgEAB0dvb2RieWUBAAlU" + |
| 38 | "cmFuc2Zvcm0BABBqYXZhL2xhbmcvT2JqZWN0AQAQamF2YS9sYW5nL1N5c3RlbQEAA291dAEAFUxq" + |
| 39 | "YXZhL2lvL1ByaW50U3RyZWFtOwEAE2phdmEvaW8vUHJpbnRTdHJlYW0BAAdwcmludGxuAQAVKExq" + |
| 40 | "YXZhL2xhbmcvU3RyaW5nOylWACAABgAHAAAAAAADAAAACAAJAAEACgAAAB0AAQABAAAABSq3AAGx" + |
| 41 | "AAAAAQALAAAABgABAAAAAQABAAwACQABAAoAAAAlAAIAAQAAAAmyAAISA7YABLEAAAABAAsAAAAK" + |
| 42 | "AAIAAAADAAgABAABAA0ACQABAAoAAAAlAAIAAQAAAAmyAAISBbYABLEAAAABAAsAAAAKAAIAAAAG" + |
| 43 | "AAgABwABAA4AAAACAA8="); |
| 44 | private static final byte[] DEX_BYTES = Base64.getDecoder().decode( |
| 45 | "ZGV4CjAzNQDpaN+7jX/ZLl9Jr0HAEV7nqL1YDuakKakgAwAAcAAAAHhWNBIAAAAAAAAAAIACAAAQ" + |
| 46 | "AAAAcAAAAAYAAACwAAAAAgAAAMgAAAABAAAA4AAAAAUAAADoAAAAAQAAABABAADwAQAAMAEAAJYB" + |
| 47 | "AACeAQAApwEAAK4BAAC7AQAA0gEAAOYBAAD6AQAADgIAAB4CAAAhAgAAJQIAADkCAAA+AgAARwIA" + |
| 48 | "AFMCAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABQAAAAAAAAAKAAAABQAAAJABAAAEAAEA" + |
| 49 | "DAAAAAAAAAAAAAAAAAAAAA4AAAAAAAAADwAAAAEAAQANAAAAAgAAAAAAAAAAAAAAAAAAAAIAAAAA" + |
| 50 | "AAAACAAAAAAAAABrAgAAAAAAAAEAAQABAAAAWgIAAAQAAABwEAQAAAAOAAMAAQACAAAAXwIAAAkA" + |
| 51 | "AABiAAAAGwEBAAAAbiADABAADgAAAAMAAQACAAAAZQIAAAkAAABiAAAAGwECAAAAbiADABAADgAA" + |
| 52 | "AAEAAAADAAY8aW5pdD4AB0dvb2RieWUABUhlbGxvAAtMVHJhbnNmb3JtOwAVTGphdmEvaW8vUHJp" + |
| 53 | "bnRTdHJlYW07ABJMamF2YS9sYW5nL09iamVjdDsAEkxqYXZhL2xhbmcvU3RyaW5nOwASTGphdmEv" + |
| 54 | "bGFuZy9TeXN0ZW07AA5UcmFuc2Zvcm0uamF2YQABVgACVkwAEmVtaXR0ZXI6IGphY2stNC4yMgAD" + |
| 55 | "b3V0AAdwcmludGxuAApzYXlHb29kYnllAAVzYXlIaQABAAcOAAYABw6HAAMABw6HAAAAAQIAgIAE" + |
| 56 | "sAIBAcgCAQHsAgAAAA0AAAAAAAAAAQAAAAAAAAABAAAAEAAAAHAAAAACAAAABgAAALAAAAADAAAA" + |
| 57 | "AgAAAMgAAAAEAAAAAQAAAOAAAAAFAAAABQAAAOgAAAAGAAAAAQAAABABAAABIAAAAwAAADABAAAB" + |
| 58 | "EAAAAQAAAJABAAACIAAAEAAAAJYBAAADIAAAAwAAAFoCAAAAIAAAAQAAAGsCAAAAEAAAAQAAAIAC" + |
| 59 | "AAA="); |
| 60 | |
Alex Light | e40dd38 | 2017-01-25 17:44:53 -0800 | [diff] [blame] | 61 | |
| 62 | public static ClassLoader getClassLoaderFor(String location) throws Exception { |
| 63 | try { |
| 64 | Class<?> class_loader_class = Class.forName("dalvik.system.PathClassLoader"); |
| 65 | Constructor<?> ctor = class_loader_class.getConstructor(String.class, ClassLoader.class); |
| 66 | /* on Dalvik, this is a DexFile; otherwise, it's null */ |
| 67 | return (ClassLoader)ctor.newInstance(location + "/" + TEST_NAME + "-ex.jar", |
| 68 | Main.class.getClassLoader()); |
| 69 | } catch (ClassNotFoundException e) { |
| 70 | // Running on RI. Use URLClassLoader. |
| 71 | return new java.net.URLClassLoader( |
| 72 | new java.net.URL[] { new java.net.URL("file://" + location + "/classes-ex/") }); |
| 73 | } |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Alex Light | e40dd38 | 2017-01-25 17:44:53 -0800 | [diff] [blame] | 76 | public static void main(String[] args) { |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 77 | addCommonTransformationResult("Transform", CLASS_BYTES, DEX_BYTES); |
| 78 | enableCommonRetransformation(true); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 79 | try { |
Alex Light | e40dd38 | 2017-01-25 17:44:53 -0800 | [diff] [blame] | 80 | /* this is the "alternate" DEX/Jar file */ |
| 81 | ClassLoader new_loader = getClassLoaderFor(System.getenv("DEX_LOCATION")); |
| 82 | Class<?> klass = (Class<?>)new_loader.loadClass("TestMain"); |
| 83 | if (klass == null) { |
| 84 | throw new AssertionError("loadClass failed"); |
| 85 | } |
| 86 | Method run_test = klass.getMethod("runTest"); |
| 87 | run_test.invoke(null); |
| 88 | |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 89 | // Make sure we don't get called for transformation again. |
| 90 | addCommonTransformationResult("Transform", new byte[0], new byte[0]); |
Alex Light | e40dd38 | 2017-01-25 17:44:53 -0800 | [diff] [blame] | 91 | doCommonClassRetransformation(new_loader.loadClass("Transform")); |
| 92 | run_test.invoke(null); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 93 | } catch (Exception e) { |
Alex Light | e40dd38 | 2017-01-25 17:44:53 -0800 | [diff] [blame] | 94 | System.out.println(e.toString()); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 95 | e.printStackTrace(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Transforms the class |
Alex Light | e40dd38 | 2017-01-25 17:44:53 -0800 | [diff] [blame] | 100 | private static native void doCommonClassRetransformation(Class<?>... classes); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 101 | private static native void enableCommonRetransformation(boolean enable); |
| 102 | private static native void addCommonTransformationResult(String target_name, |
| 103 | byte[] class_bytes, |
| 104 | byte[] dex_bytes); |
| 105 | } |