AnalysisTest.java revision 83f77f51aa888998486c0c9ad693047480b060b0
1e2fab734ade8a2e3e8679c11cac7236b920d567bJohn Criswell/*
2e2fab734ade8a2e3e8679c11cac7236b920d567bJohn Criswell * Copyright 2013, Google Inc.
3e2fab734ade8a2e3e8679c11cac7236b920d567bJohn Criswell * All rights reserved.
4e2fab734ade8a2e3e8679c11cac7236b920d567bJohn Criswell *
551167848265a0fa006c32557caa4aeb3f482f45eChris Lattner * Redistribution and use in source and binary forms, with or without
651167848265a0fa006c32557caa4aeb3f482f45eChris Lattner * modification, are permitted provided that the following conditions are
7e2fab734ade8a2e3e8679c11cac7236b920d567bJohn Criswell * met:
8e2fab734ade8a2e3e8679c11cac7236b920d567bJohn Criswell *
99bf8ea26a9b9571cab76819cc69f3bfa857610a8Chris Lattner *     * Redistributions of source code must retain the above copyright
108c39c9647da4f375e4f89bd417d86f5c3ff6dfa5Daniel Dunbar * notice, this list of conditions and the following disclaimer.
118c39c9647da4f375e4f89bd417d86f5c3ff6dfa5Daniel Dunbar *     * Redistributions in binary form must reproduce the above
1244dadffe4bd58ab32961ca5fe537e8ba69c09243Chris Lattner * copyright notice, this list of conditions and the following disclaimer
13009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner * in the documentation and/or other materials provided with the
146b37dcdedffe4c91601da8cfaf793053a2ce4b0fChris Lattner * distribution.
158c39c9647da4f375e4f89bd417d86f5c3ff6dfa5Daniel Dunbar *     * Neither the name of Google Inc. nor the names of its
166b37dcdedffe4c91601da8cfaf793053a2ce4b0fChris Lattner * contributors may be used to endorse or promote products derived from
17c9744e7f269ec8003b64c39c6c9e62f0b6c33a5aChris Lattner * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.jf.baksmali;
33
34import com.google.common.base.Charsets;
35import com.google.common.io.Resources;
36import junit.framework.Assert;
37import org.jf.baksmali.Adaptors.ClassDefinition;
38import org.jf.dexlib2.DexFileFactory;
39import org.jf.dexlib2.analysis.ClassPath;
40import org.jf.dexlib2.iface.ClassDef;
41import org.jf.dexlib2.iface.DexFile;
42import org.jf.util.IndentingWriter;
43import org.junit.Test;
44
45import javax.annotation.Nonnull;
46import java.io.File;
47import java.io.IOException;
48import java.io.StringWriter;
49import java.net.URISyntaxException;
50import java.net.URL;
51
52public class AnalysisTest {
53
54    @Test
55    public void ConstructorTest() throws IOException, URISyntaxException {
56        runTest("ConstructorTest", true);
57    }
58
59    @Test
60    public void RegisterEqualityOnMergeTest() throws IOException, URISyntaxException {
61        runTest("RegisterEqualityOnMergeTest", true);
62    }
63
64    @Test
65    public void UninitRefIdentityTest() throws IOException, URISyntaxException {
66        runTest("UninitRefIdentityTest", true);
67    }
68
69    @Test
70    public void MultipleStartInstructionsTest() throws IOException, URISyntaxException {
71        runTest("MultipleStartInstructionsTest", true);
72    }
73
74    @Test
75    public void DuplicateTest() throws IOException, URISyntaxException {
76        runTest("DuplicateTest", false);
77    }
78
79    public void runTest(String test, boolean registerInfo) throws IOException, URISyntaxException {
80        String dexFilePath = String.format("%s%sclasses.dex", test, File.separatorChar);
81
82        DexFile dexFile = DexFileFactory.loadDexFile(findResource(dexFilePath), 15);
83
84        baksmaliOptions options = new baksmaliOptions();
85        if (registerInfo) {
86            options.registerInfo = baksmaliOptions.ALL | baksmaliOptions.FULLMERGE;
87            options.classPath = new ClassPath();
88        }
89
90        for (ClassDef classDef: dexFile.getClasses()) {
91            StringWriter stringWriter = new StringWriter();
92            IndentingWriter writer = new IndentingWriter(stringWriter);
93            ClassDefinition classDefinition = new ClassDefinition(options, classDef);
94            classDefinition.writeTo(writer);
95            writer.close();
96
97            String className = classDef.getType();
98            String smaliPath = String.format("%s%s%s.smali", test, File.separatorChar,
99                    className.substring(1, className.length() - 1));
100            String smaliContents = readResource(smaliPath);
101
102            Assert.assertEquals(smaliContents, stringWriter.toString());
103        }
104    }
105
106    @Nonnull
107    private File findResource(String resource) throws URISyntaxException {
108        URL resUrl = Resources.getResource(resource);
109        return new File(resUrl.toURI());
110    }
111
112    @Nonnull
113    private String readResource(String resource) throws URISyntaxException, IOException {
114        URL url = Resources.getResource(resource);
115        return Resources.toString(url, Charsets.UTF_8);
116    }
117}
118