• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013, Google Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * 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 
32 package org.jf.baksmali;
33 
34 import com.google.common.base.Charsets;
35 import com.google.common.io.Resources;
36 import junit.framework.Assert;
37 import org.jf.baksmali.Adaptors.ClassDefinition;
38 import org.jf.dexlib2.DexFileFactory;
39 import org.jf.dexlib2.Opcodes;
40 import org.jf.dexlib2.analysis.ClassPath;
41 import org.jf.dexlib2.analysis.ClassProvider;
42 import org.jf.dexlib2.iface.ClassDef;
43 import org.jf.dexlib2.iface.DexFile;
44 import org.jf.util.IndentingWriter;
45 import org.junit.Test;
46 
47 import javax.annotation.Nonnull;
48 import java.io.File;
49 import java.io.IOException;
50 import java.io.StringWriter;
51 import java.net.URISyntaxException;
52 import java.net.URL;
53 import java.util.ArrayList;
54 
55 public class AnalysisTest {
56 
57     @Test
ConstructorTest()58     public void ConstructorTest() throws IOException, URISyntaxException {
59         runTest("ConstructorTest", true);
60     }
61 
62     @Test
RegisterEqualityOnMergeTest()63     public void RegisterEqualityOnMergeTest() throws IOException, URISyntaxException {
64         runTest("RegisterEqualityOnMergeTest", true);
65     }
66 
67     @Test
UninitRefIdentityTest()68     public void UninitRefIdentityTest() throws IOException, URISyntaxException {
69         runTest("UninitRefIdentityTest", true);
70     }
71 
72     @Test
InstanceOfTest()73     public void InstanceOfTest() throws IOException, URISyntaxException {
74         runTest("InstanceOfTest", true, true);
75     }
76 
77     @Test
MultipleStartInstructionsTest()78     public void MultipleStartInstructionsTest() throws IOException, URISyntaxException {
79         runTest("MultipleStartInstructionsTest", true);
80     }
81 
82     @Test
DuplicateTest()83     public void DuplicateTest() throws IOException, URISyntaxException {
84         runTest("DuplicateTest", false);
85     }
86 
87     @Test
LocalTest()88     public void LocalTest() throws IOException, URISyntaxException {
89         runTest("LocalTest", false);
90     }
91 
runTest(String test, boolean registerInfo)92     public void runTest(String test, boolean registerInfo) throws IOException, URISyntaxException {
93         runTest(test, registerInfo, false);
94     }
95 
runTest(String test, boolean registerInfo, boolean isArt)96     public void runTest(String test, boolean registerInfo, boolean isArt) throws IOException, URISyntaxException {
97         String dexFilePath = String.format("%s%sclasses.dex", test, File.separatorChar);
98 
99         DexFile dexFile = DexFileFactory.loadDexFile(findResource(dexFilePath), Opcodes.getDefault());
100 
101         BaksmaliOptions options = new BaksmaliOptions();
102         if (registerInfo) {
103             options.registerInfo = BaksmaliOptions.ALL | BaksmaliOptions.FULLMERGE;
104             if (isArt) {
105                 options.classPath = new ClassPath(new ArrayList<ClassProvider>(), true, 56);
106             } else {
107                 options.classPath = new ClassPath();
108             }
109         }
110         options.implicitReferences = false;
111 
112         for (ClassDef classDef: dexFile.getClasses()) {
113             StringWriter stringWriter = new StringWriter();
114             IndentingWriter writer = new IndentingWriter(stringWriter);
115             ClassDefinition classDefinition = new ClassDefinition(options, classDef);
116             classDefinition.writeTo(writer);
117             writer.close();
118 
119             String className = classDef.getType();
120             String smaliPath = String.format("%s%s%s.smali", test, File.separatorChar,
121                     className.substring(1, className.length() - 1));
122             String smaliContents = readResource(smaliPath);
123 
124             Assert.assertEquals(BaksmaliTestUtils.normalizeWhitespace(smaliContents),
125                     BaksmaliTestUtils.normalizeWhitespace((stringWriter.toString())));
126         }
127     }
128 
129     @Nonnull
findResource(String resource)130     private File findResource(String resource) throws URISyntaxException {
131         URL resUrl = Resources.getResource(resource);
132         return new File(resUrl.toURI());
133     }
134 
135     @Nonnull
readResource(String resource)136     private String readResource(String resource) throws URISyntaxException, IOException {
137         URL url = Resources.getResource(resource);
138         return Resources.toString(url, Charsets.UTF_8);
139     }
140 }
141