From d87770e69bd46adab536dd7f1b4eee0528741633 Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Mon, 8 Apr 2013 00:00:17 -0700 Subject: [PATCH] Add a test framework for baksmali This also adds a test for register info comments in constructors --- baksmali/build.gradle | 2 + .../java/org/jf/baksmali/AnalysisTest.java | 97 ++++++++++++++++++ .../ConstructorTest/ConstructorTest.smali | 16 +++ .../ConstructorTest/ConstructorTest2.smali | 25 +++++ .../resources/ConstructorTest/classes.dex | Bin 0 -> 520 bytes .../ConstructorTest/out/ConstructorTest.smali | 12 +++ .../out/ConstructorTest2.smali | 17 +++ 7 files changed, 169 insertions(+) create mode 100644 baksmali/src/test/java/org/jf/baksmali/AnalysisTest.java create mode 100644 baksmali/src/test/resources/ConstructorTest/ConstructorTest.smali create mode 100644 baksmali/src/test/resources/ConstructorTest/ConstructorTest2.smali create mode 100644 baksmali/src/test/resources/ConstructorTest/classes.dex create mode 100644 baksmali/src/test/resources/ConstructorTest/out/ConstructorTest.smali create mode 100644 baksmali/src/test/resources/ConstructorTest/out/ConstructorTest2.smali diff --git a/baksmali/build.gradle b/baksmali/build.gradle index 3fc7b069..52f902ed 100644 --- a/baksmali/build.gradle +++ b/baksmali/build.gradle @@ -39,6 +39,8 @@ dependencies { compile depends.commons_cli compile depends.guava + testCompile depends.junit + proguard depends.proguard } diff --git a/baksmali/src/test/java/org/jf/baksmali/AnalysisTest.java b/baksmali/src/test/java/org/jf/baksmali/AnalysisTest.java new file mode 100644 index 00000000..06d9cc1f --- /dev/null +++ b/baksmali/src/test/java/org/jf/baksmali/AnalysisTest.java @@ -0,0 +1,97 @@ +/* + * Copyright 2013, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.baksmali; + +import com.google.common.base.Charsets; +import com.google.common.io.Resources; +import junit.framework.Assert; +import org.jf.baksmali.Adaptors.ClassDefinition; +import org.jf.dexlib2.DexFileFactory; +import org.jf.dexlib2.analysis.ClassPath; +import org.jf.dexlib2.iface.ClassDef; +import org.jf.dexlib2.iface.DexFile; +import org.jf.util.IndentingWriter; +import org.junit.Test; + +import javax.annotation.Nonnull; +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; +import java.net.URISyntaxException; +import java.net.URL; + +public class AnalysisTest { + + @Test + public void ConstructorTest() throws IOException, URISyntaxException { + runTest("ConstructorTest"); + } + + public void runTest(String test) throws IOException, URISyntaxException { + String dexFilePath = String.format("%s%sclasses.dex", test, File.separatorChar); + + DexFile dexFile = DexFileFactory.loadDexFile(findResource(dexFilePath)); + + baksmaliOptions options = new baksmaliOptions(); + options.registerInfo = baksmaliOptions.ALL | baksmaliOptions.FULLMERGE; + options.classPath = new ClassPath(); + + for (ClassDef classDef: dexFile.getClasses()) { + StringWriter stringWriter = new StringWriter(); + IndentingWriter writer = new IndentingWriter(stringWriter); + ClassDefinition classDefinition = new ClassDefinition(options, classDef); + classDefinition.writeTo(writer); + writer.close(); + + String className = classDef.getType(); + String smaliPath = String.format("%s%s%s.smali", test, File.separatorChar, + className.substring(1, className.length() - 1)); + String smaliContents = readResource(smaliPath); + + Assert.assertEquals(smaliContents, stringWriter.toString()); + + System.out.println(String.format("%d, %d", smaliContents.length(), stringWriter.toString().length())); + } + } + + @Nonnull + private File findResource(String resource) throws URISyntaxException { + URL resUrl = Resources.getResource(resource); + return new File(resUrl.toURI()); + } + + @Nonnull + private String readResource(String resource) throws URISyntaxException, IOException { + URL url = Resources.getResource(resource); + return Resources.toString(url, Charsets.UTF_8); + } +} diff --git a/baksmali/src/test/resources/ConstructorTest/ConstructorTest.smali b/baksmali/src/test/resources/ConstructorTest/ConstructorTest.smali new file mode 100644 index 00000000..88e2eb86 --- /dev/null +++ b/baksmali/src/test/resources/ConstructorTest/ConstructorTest.smali @@ -0,0 +1,16 @@ +.class public LConstructorTest; +.super Ljava/lang/Object; + + +# direct methods +.method public constructor ()V + .registers 4 + + #v0=(Uninit);v1=(Uninit);v2=(Uninit);p0=(UninitThis,LConstructorTest;); + invoke-direct {p0}, Ljava/lang/Object;->()V + #v0=(Uninit);v1=(Uninit);v2=(Uninit);p0=(Reference,LConstructorTest;); + + #v0=(Uninit);v1=(Uninit);v2=(Uninit);p0=(Reference,LConstructorTest;); + return-void + #v0=(Uninit);v1=(Uninit);v2=(Uninit);p0=(Reference,LConstructorTest;); +.end method diff --git a/baksmali/src/test/resources/ConstructorTest/ConstructorTest2.smali b/baksmali/src/test/resources/ConstructorTest/ConstructorTest2.smali new file mode 100644 index 00000000..a376b25b --- /dev/null +++ b/baksmali/src/test/resources/ConstructorTest/ConstructorTest2.smali @@ -0,0 +1,25 @@ +.class public LConstructorTest2; +.super Ljava/lang/Object; + + +# direct methods +.method public constructor ()V + .registers 4 + + #v0=(Uninit);v1=(Uninit);v2=(Uninit);p0=(UninitThis,LConstructorTest2;); + if-eqz p0, :cond_3 + #v0=(Uninit);v1=(Uninit);v2=(Uninit);p0=(UninitThis,LConstructorTest2;); + + #v0=(Uninit);v1=(Uninit);v2=(Uninit);p0=(UninitThis,LConstructorTest2;); + nop + #v0=(Uninit);v1=(Uninit);v2=(Uninit);p0=(UninitThis,LConstructorTest2;); + + :cond_3 + #v0=(Uninit);v1=(Uninit);v2=(Uninit);p0=(UninitThis,LConstructorTest2;); + invoke-direct {p0}, Ljava/lang/Object;->()V + #v0=(Uninit);v1=(Uninit);v2=(Uninit);p0=(Reference,LConstructorTest2;); + + #v0=(Uninit);v1=(Uninit);v2=(Uninit);p0=(Reference,LConstructorTest2;); + return-void + #v0=(Uninit);v1=(Uninit);v2=(Uninit);p0=(Reference,LConstructorTest2;); +.end method diff --git a/baksmali/src/test/resources/ConstructorTest/classes.dex b/baksmali/src/test/resources/ConstructorTest/classes.dex new file mode 100644 index 0000000000000000000000000000000000000000..ef6e6d938bd3454809bf04996169f15263897081 GIT binary patch literal 520 zcmZuu%}N4M82x6POkBt~PtXGtiX{kBZK|DM3mJk|<1~mSqv=hAHU>gMEe%?Uj}g5< zuTYyFp=G$sbH=$K(Z?Llx%d0M-&}>GY;nIVXGilJ_igX_;re{~eYgAg^Oln@HJ4Y$e5xJ=;X*9MqYq1I@JfpW$0Bz@S5TIyKxe^Xr4L+|^swFF@fs24mei+h@$i>&v| pcXN&jz)9)jzUk+A0i*%FZ)0bzxpNbmaooE=GbY}Xy{^OmxdLJDK)C<_ literal 0 HcmV?d00001 diff --git a/baksmali/src/test/resources/ConstructorTest/out/ConstructorTest.smali b/baksmali/src/test/resources/ConstructorTest/out/ConstructorTest.smali new file mode 100644 index 00000000..925ef7cc --- /dev/null +++ b/baksmali/src/test/resources/ConstructorTest/out/ConstructorTest.smali @@ -0,0 +1,12 @@ +.class public LConstructorTest; +.super Ljava/lang/Object; + + +# direct methods +.method public constructor ()V + .registers 4 + + invoke-direct {p0}, Ljava/lang/Object;->()V + + return-void +.end method diff --git a/baksmali/src/test/resources/ConstructorTest/out/ConstructorTest2.smali b/baksmali/src/test/resources/ConstructorTest/out/ConstructorTest2.smali new file mode 100644 index 00000000..46ffebf9 --- /dev/null +++ b/baksmali/src/test/resources/ConstructorTest/out/ConstructorTest2.smali @@ -0,0 +1,17 @@ +.class public LConstructorTest2; +.super Ljava/lang/Object; + + +# direct methods +.method public constructor ()V + .registers 4 + + if-eqz p0, :cond_3 + + nop + + :cond_3 + invoke-direct {p0}, Ljava/lang/Object;->()V + + return-void +.end method