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 
17 import static art.Redefinition.*;
18 import java.lang.reflect.*;
19 import java.util.Base64;
20 
21 class Main {
22   public static String TEST_NAME = "938-load-transform-bcp";
23 
24   /**
25    * base64 encoded class/dex file for
26    *
27    * // Yes this version of OptionalLong is not compatible with the real one but since it isn't used
28    * // for anything in the runtime initialization it should be fine.
29    *
30    * package java.util;
31    * public final class OptionalLong {
32    *   private long val;
33    *
34    *   private OptionalLong(long abc) {
35    *     this.val = abc;
36    *   }
37    *
38    *   public static OptionalLong of(long abc) {
39    *     return new OptionalLong(abc);
40    *   }
41    *
42    *   public String foo() {
43    *     return "This is foo for val=" + val;
44    *   }
45    *
46    *   public String toString() {
47    *     return "This is toString() for val=" + val;
48    *   }
49    * }
50    */
51   private static final byte[] CLASS_BYTES = Base64.getDecoder().decode(
52     "yv66vgAAADQAKQoADAAaCQADABsHABwKAAMAHQcAHgoABQAaCAAfCgAFACAKAAUAIQoABQAiCAAj" +
53     "BwAkAQADdmFsAQABSgEABjxpbml0PgEABChKKVYBAARDb2RlAQAPTGluZU51bWJlclRhYmxlAQAC" +
54     "b2YBABsoSilMamF2YS91dGlsL09wdGlvbmFsTG9uZzsBAANmb28BABQoKUxqYXZhL2xhbmcvU3Ry" +
55     "aW5nOwEACHRvU3RyaW5nAQAKU291cmNlRmlsZQEAEU9wdGlvbmFsTG9uZy5qYXZhDAAPACUMAA0A" +
56     "DgEAFmphdmEvdXRpbC9PcHRpb25hbExvbmcMAA8AEAEAF2phdmEvbGFuZy9TdHJpbmdCdWlsZGVy" +
57     "AQAUVGhpcyBpcyBmb28gZm9yIHZhbD0MACYAJwwAJgAoDAAXABYBABtUaGlzIGlzIHRvU3RyaW5n" +
58     "KCkgZm9yIHZhbD0BABBqYXZhL2xhbmcvT2JqZWN0AQADKClWAQAGYXBwZW5kAQAtKExqYXZhL2xh" +
59     "bmcvU3RyaW5nOylMamF2YS9sYW5nL1N0cmluZ0J1aWxkZXI7AQAcKEopTGphdmEvbGFuZy9TdHJp" +
60     "bmdCdWlsZGVyOwAxAAMADAAAAAEAAgANAA4AAAAEAAIADwAQAAEAEQAAACoAAwADAAAACiq3AAEq" +
61     "H7UAArEAAAABABIAAAAOAAMAAAAFAAQABgAJAAcACQATABQAAQARAAAAIQAEAAIAAAAJuwADWR63" +
62     "AASwAAAAAQASAAAABgABAAAACgABABUAFgABABEAAAAvAAMAAQAAABe7AAVZtwAGEge2AAgqtAAC" +
63     "tgAJtgAKsAAAAAEAEgAAAAYAAQAAAA4AAQAXABYAAQARAAAALwADAAEAAAAXuwAFWbcABhILtgAI" +
64     "KrQAArYACbYACrAAAAABABIAAAAGAAEAAAASAAEAGAAAAAIAGQ==");
65   private static final byte[] DEX_BYTES = Base64.getDecoder().decode(
66     "ZGV4CjAzNQAOe/TYJCvVthTToFA3tveMDhwTo7uDf0IcBAAAcAAAAHhWNBIAAAAAAAAAAHwDAAAU" +
67     "AAAAcAAAAAYAAADAAAAABgAAANgAAAABAAAAIAEAAAkAAAAoAQAAAQAAAHABAACMAgAAkAEAAFYC" +
68     "AABeAgAAYQIAAGQCAABoAgAAbAIAAIACAACUAgAArwIAAMkCAADcAgAA8gIAAA8DAAASAwAAFgMA" +
69     "AB4DAAAyAwAANwMAADsDAABFAwAAAQAAAAUAAAAGAAAABwAAAAgAAAAMAAAAAgAAAAIAAAAAAAAA" +
70     "AwAAAAMAAABIAgAABAAAAAMAAABQAgAAAwAAAAQAAABIAgAADAAAAAUAAAAAAAAADQAAAAUAAABI" +
71     "AgAABAAAABMAAAABAAQAAAAAAAMABAAAAAAAAwABAA4AAAADAAIADgAAAAMAAAASAAAABAAFAAAA" +
72     "AAAEAAAAEAAAAAQAAwARAAAABAAAABIAAAAEAAAAEQAAAAEAAAAAAAAACQAAAAAAAABiAwAAAAAA" +
73     "AAQAAwABAAAASgMAAAYAAABwEAAAAQBaEgAADgAEAAIAAwAAAFIDAAAGAAAAIgAEAHAwBQAgAxEA" +
74     "BQABAAMAAABYAwAAFwAAACIAAwBwEAEAAAAbAQoAAABuIAMAEAAMAFNCAABuMAIAIAMMAG4QBAAA" +
75     "AAwAEQAAAAUAAQADAAAAXQMAABcAAAAiAAMAcBABAAAAGwELAAAAbiADABAADABTQgAAbjACACAD" +
76     "DABuEAQAAAAMABEAAAABAAAAAAAAAAEAAAACAAY8aW5pdD4AAUoAAUwAAkxKAAJMTAASTGphdmEv" +
77     "bGFuZy9PYmplY3Q7ABJMamF2YS9sYW5nL1N0cmluZzsAGUxqYXZhL2xhbmcvU3RyaW5nQnVpbGRl" +
78     "cjsAGExqYXZhL3V0aWwvT3B0aW9uYWxMb25nOwART3B0aW9uYWxMb25nLmphdmEAFFRoaXMgaXMg" +
79     "Zm9vIGZvciB2YWw9ABtUaGlzIGlzIHRvU3RyaW5nKCkgZm9yIHZhbD0AAVYAAlZKAAZhcHBlbmQA" +
80     "EmVtaXR0ZXI6IGphY2stNC4yMgADZm9vAAJvZgAIdG9TdHJpbmcAA3ZhbAAFAQAHDjwtAAoBAAcO" +
81     "AA4ABw4AEgAHDgAAAQICAAIFgoAEkAMCCawDBgHIAwIBiAQAAA0AAAAAAAAAAQAAAAAAAAABAAAA" +
82     "FAAAAHAAAAACAAAABgAAAMAAAAADAAAABgAAANgAAAAEAAAAAQAAACABAAAFAAAACQAAACgBAAAG" +
83     "AAAAAQAAAHABAAABIAAABAAAAJABAAABEAAAAgAAAEgCAAACIAAAFAAAAFYCAAADIAAABAAAAEoD" +
84     "AAAAIAAAAQAAAGIDAAAAEAAAAQAAAHwDAAA=");
85 
getClassLoaderFor(String location)86   public static ClassLoader getClassLoaderFor(String location) throws Exception {
87     try {
88       Class<?> class_loader_class = Class.forName("dalvik.system.PathClassLoader");
89       Constructor<?> ctor = class_loader_class.getConstructor(String.class, ClassLoader.class);
90       return (ClassLoader)ctor.newInstance(location + "/" + TEST_NAME + "-ex.jar",
91                                            Main.class.getClassLoader());
92     } catch (ClassNotFoundException e) {
93       // Running on RI. Use URLClassLoader.
94       return new java.net.URLClassLoader(
95           new java.net.URL[] { new java.net.URL("file://" + location + "/classes-ex/") });
96     }
97   }
98 
main(String[] args)99   public static void main(String[] args) {
100     setPopRetransformations(false);
101     addCommonTransformationResult("java/util/OptionalLong", CLASS_BYTES, DEX_BYTES);
102     enableCommonRetransformation(true);
103     try {
104       /* this is the "alternate" DEX/Jar file */
105       ClassLoader new_loader = getClassLoaderFor(System.getenv("DEX_LOCATION"));
106       Class<?> klass = (Class<?>)new_loader.loadClass("TestMain");
107       if (klass == null) {
108         throw new AssertionError("loadClass failed");
109       }
110       Method run_test = klass.getMethod("runTest");
111       run_test.invoke(null);
112     } catch (Exception e) {
113       System.out.println(e.toString());
114       e.printStackTrace(System.out);
115     }
116   }
117 }
118