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.jf.util.TextUtils; 46 import org.junit.Test; 47 48 import javax.annotation.Nonnull; 49 import java.io.File; 50 import java.io.IOException; 51 import java.io.StringWriter; 52 import java.net.URISyntaxException; 53 import java.net.URL; 54 import java.util.ArrayList; 55 56 public class AnalysisTest { 57 58 @Test ConstructorTest()59 public void ConstructorTest() throws IOException, URISyntaxException { 60 runTest("ConstructorTest", true); 61 } 62 63 @Test RegisterEqualityOnMergeTest()64 public void RegisterEqualityOnMergeTest() throws IOException, URISyntaxException { 65 runTest("RegisterEqualityOnMergeTest", true); 66 } 67 68 @Test UninitRefIdentityTest()69 public void UninitRefIdentityTest() throws IOException, URISyntaxException { 70 runTest("UninitRefIdentityTest", true); 71 } 72 73 @Test InstanceOfTest()74 public void InstanceOfTest() throws IOException, URISyntaxException { 75 runTest("InstanceOfTest", true, true); 76 } 77 78 @Test MultipleStartInstructionsTest()79 public void MultipleStartInstructionsTest() throws IOException, URISyntaxException { 80 runTest("MultipleStartInstructionsTest", true); 81 } 82 83 @Test DuplicateTest()84 public void DuplicateTest() throws IOException, URISyntaxException { 85 runTest("DuplicateTest", false); 86 } 87 88 @Test LocalTest()89 public void LocalTest() throws IOException, URISyntaxException { 90 runTest("LocalTest", false); 91 } 92 runTest(String test, boolean registerInfo)93 public void runTest(String test, boolean registerInfo) throws IOException, URISyntaxException { 94 runTest(test, registerInfo, false); 95 } 96 runTest(String test, boolean registerInfo, boolean isArt)97 public void runTest(String test, boolean registerInfo, boolean isArt) throws IOException, URISyntaxException { 98 String dexFilePath = String.format("%s%sclasses.dex", test, File.separatorChar); 99 100 DexFile dexFile = DexFileFactory.loadDexFile(findResource(dexFilePath), Opcodes.getDefault()); 101 102 BaksmaliOptions options = new BaksmaliOptions(); 103 if (registerInfo) { 104 options.registerInfo = BaksmaliOptions.ALL | BaksmaliOptions.FULLMERGE; 105 if (isArt) { 106 options.classPath = new ClassPath(new ArrayList<ClassProvider>(), true, 56); 107 } else { 108 options.classPath = new ClassPath(); 109 } 110 } 111 options.implicitReferences = false; 112 113 for (ClassDef classDef: dexFile.getClasses()) { 114 StringWriter stringWriter = new StringWriter(); 115 IndentingWriter writer = new IndentingWriter(stringWriter); 116 ClassDefinition classDefinition = new ClassDefinition(options, classDef); 117 classDefinition.writeTo(writer); 118 writer.close(); 119 120 String className = classDef.getType(); 121 String smaliPath = String.format("%s%s%s.smali", test, File.separatorChar, 122 className.substring(1, className.length() - 1)); 123 String smaliContents = readResource(smaliPath); 124 125 Assert.assertEquals(TextUtils.normalizeWhitespace(smaliContents), 126 TextUtils.normalizeWhitespace((stringWriter.toString()))); 127 } 128 } 129 130 @Nonnull findResource(String resource)131 private File findResource(String resource) throws URISyntaxException { 132 URL resUrl = Resources.getResource(resource); 133 return new File(resUrl.toURI()); 134 } 135 136 @Nonnull readResource(String resource)137 private String readResource(String resource) throws URISyntaxException, IOException { 138 URL url = Resources.getResource(resource); 139 return Resources.toString(url, Charsets.UTF_8); 140 } 141 } 142