1/*
2 * Copyright (C) 2011 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
17package com.android.dx.merge;
18
19import com.android.dex.Dex;
20import java.io.File;
21import java.io.FileInputStream;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.io.InputStream;
25import java.io.OutputStream;
26import java.lang.annotation.Annotation;
27import java.lang.reflect.Field;
28import java.lang.reflect.Method;
29import java.util.Arrays;
30import java.util.jar.JarEntry;
31import java.util.jar.JarOutputStream;
32import junit.framework.TestCase;
33
34/**
35 * Test that DexMerge works by merging dex files, and then loading them into
36 * the current VM.
37 */
38public final class DexMergeTest extends TestCase {
39
40    public void testFillArrayData() throws Exception {
41        ClassLoader loader = mergeAndLoad(
42                "/testdata/Basic.dex",
43                "/testdata/FillArrayData.dex");
44
45        Class<?> basic = loader.loadClass("testdata.Basic");
46        assertEquals(1, basic.getDeclaredMethods().length);
47
48        Class<?> fillArrayData = loader.loadClass("testdata.FillArrayData");
49        assertTrue(Arrays.equals(
50                new byte[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, -112, -23, 121 },
51                (byte[]) fillArrayData.getMethod("newByteArray").invoke(null)));
52        assertTrue(Arrays.equals(
53                new char[] { 0xFFFF, 0x4321, 0xABCD, 0, 'a', 'b', 'c' },
54                (char[]) fillArrayData.getMethod("newCharArray").invoke(null)));
55        assertTrue(Arrays.equals(
56                new long[] { 4660046610375530309L, 7540113804746346429L, -6246583658587674878L },
57                (long[]) fillArrayData.getMethod("newLongArray").invoke(null)));
58    }
59
60    public void testTryCatchFinally() throws Exception {
61        ClassLoader loader = mergeAndLoad(
62                "/testdata/Basic.dex",
63                "/testdata/TryCatchFinally.dex");
64
65        Class<?> basic = loader.loadClass("testdata.Basic");
66        assertEquals(1, basic.getDeclaredMethods().length);
67
68        Class<?> tryCatchFinally = loader.loadClass("testdata.TryCatchFinally");
69        tryCatchFinally.getDeclaredMethod("method").invoke(null);
70    }
71
72    public void testStaticValues() throws Exception {
73        ClassLoader loader = mergeAndLoad(
74                "/testdata/Basic.dex",
75                "/testdata/StaticValues.dex");
76
77        Class<?> basic = loader.loadClass("testdata.Basic");
78        assertEquals(1, basic.getDeclaredMethods().length);
79
80        Class<?> staticValues = loader.loadClass("testdata.StaticValues");
81        assertEquals((byte) 1, staticValues.getField("a").get(null));
82        assertEquals((short) 2, staticValues.getField("b").get(null));
83        assertEquals('C', staticValues.getField("c").get(null));
84        assertEquals(0xabcd1234, staticValues.getField("d").get(null));
85        assertEquals(4660046610375530309L,staticValues.getField("e").get(null));
86        assertEquals(0.5f, staticValues.getField("f").get(null));
87        assertEquals(-0.25, staticValues.getField("g").get(null));
88        assertEquals("this is a String", staticValues.getField("h").get(null));
89        assertEquals(String.class, staticValues.getField("i").get(null));
90        assertEquals("[0, 1]", Arrays.toString((int[]) staticValues.getField("j").get(null)));
91        assertEquals(null, staticValues.getField("k").get(null));
92        assertEquals(true, staticValues.getField("l").get(null));
93        assertEquals(false, staticValues.getField("m").get(null));
94    }
95
96    public void testAnnotations() throws Exception {
97        ClassLoader loader = mergeAndLoad(
98                "/testdata/Basic.dex",
99                "/testdata/Annotated.dex");
100
101        Class<?> basic = loader.loadClass("testdata.Basic");
102        assertEquals(1, basic.getDeclaredMethods().length);
103
104        Class<?> annotated = loader.loadClass("testdata.Annotated");
105        Method method = annotated.getMethod("method", String.class, String.class);
106        Field field = annotated.getField("field");
107
108        @SuppressWarnings("unchecked")
109        Class<? extends Annotation> marker
110                = (Class<? extends Annotation>) loader.loadClass("testdata.Annotated$Marker");
111
112        assertEquals("@testdata.Annotated$Marker(a=on class, b=[A, B, C], "
113                + "c=@testdata.Annotated$Nested(e=E1, f=1695938256, g=7264081114510713000), "
114                + "d=[@testdata.Annotated$Nested(e=E2, f=1695938256, g=7264081114510713000)])",
115                annotated.getAnnotation(marker).toString());
116        assertEquals("@testdata.Annotated$Marker(a=on method, b=[], "
117                + "c=@testdata.Annotated$Nested(e=, f=0, g=0), d=[])",
118                method.getAnnotation(marker).toString());
119        assertEquals("@testdata.Annotated$Marker(a=on field, b=[], "
120                + "c=@testdata.Annotated$Nested(e=, f=0, g=0), d=[])",
121                field.getAnnotation(marker).toString());
122        assertEquals("@testdata.Annotated$Marker(a=on parameter, b=[], "
123                + "c=@testdata.Annotated$Nested(e=, f=0, g=0), d=[])",
124                method.getParameterAnnotations()[1][0].toString());
125    }
126
127    /**
128     * Merging dex files uses pessimistic sizes that naturally leave gaps in the
129     * output files. If those gaps grow too large, the merger is supposed to
130     * compact the result. This exercises that by repeatedly merging a dex with
131     * itself.
132     */
133    public void testMergedOutputSizeIsBounded() throws Exception {
134        /*
135         * At the time this test was written, the output would grow ~25% with
136         * each merge. Setting a low 1KiB ceiling on the maximum size caused
137         * the file to be compacted every four merges.
138         */
139        int steps = 100;
140        int compactWasteThreshold = 1024;
141
142        Dex dexA = resourceToDexBuffer("/testdata/Basic.dex");
143        Dex dexB = resourceToDexBuffer("/testdata/TryCatchFinally.dex");
144        Dex merged = new DexMerger(dexA, dexB, CollisionPolicy.KEEP_FIRST).merge();
145
146        int maxLength = 0;
147        for (int i = 0; i < steps; i++) {
148            DexMerger dexMerger = new DexMerger(dexA, merged, CollisionPolicy.KEEP_FIRST);
149            dexMerger.setCompactWasteThreshold(compactWasteThreshold);
150            merged = dexMerger.merge();
151            maxLength = Math.max(maxLength, merged.getLength());
152        }
153
154        int maxExpectedLength = dexA.getLength() + dexB.getLength() + compactWasteThreshold;
155        assertTrue(maxLength + " < " + maxExpectedLength, maxLength < maxExpectedLength);
156    }
157
158    public ClassLoader mergeAndLoad(String dexAResource, String dexBResource) throws Exception {
159        Dex dexA = resourceToDexBuffer(dexAResource);
160        Dex dexB = resourceToDexBuffer(dexBResource);
161        Dex merged = new DexMerger(dexA, dexB, CollisionPolicy.KEEP_FIRST).merge();
162        File mergedDex = File.createTempFile("DexMergeTest", ".classes.dex");
163        merged.writeTo(mergedDex);
164        File mergedJar = dexToJar(mergedDex);
165        // simplify the javac classpath by not depending directly on 'dalvik.system' classes
166        return (ClassLoader) Class.forName("dalvik.system.PathClassLoader")
167                .getConstructor(String.class, ClassLoader.class)
168                .newInstance(mergedJar.getPath(), getClass().getClassLoader());
169    }
170
171    private Dex resourceToDexBuffer(String resource) throws IOException {
172        return new Dex(getClass().getResourceAsStream(resource));
173    }
174
175    private File dexToJar(File dex) throws IOException {
176        File result = File.createTempFile("DexMergeTest", ".jar");
177        result.deleteOnExit();
178        JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(result));
179        jarOut.putNextEntry(new JarEntry("classes.dex"));
180        copy(new FileInputStream(dex), jarOut);
181        jarOut.closeEntry();
182        jarOut.close();
183        return result;
184    }
185
186    private void copy(InputStream in, OutputStream out) throws IOException {
187        byte[] buffer = new byte[1024];
188        int count;
189        while ((count = in.read(buffer)) != -1) {
190            out.write(buffer, 0, count);
191        }
192        in.close();
193    }
194}
195