Add a test framework for baksmali

This also adds a test for register info comments in constructors
This commit is contained in:
Ben Gruver 2013-04-08 00:00:17 -07:00
parent 059e1249b4
commit d87770e69b
7 changed files with 169 additions and 0 deletions

View File

@ -39,6 +39,8 @@ dependencies {
compile depends.commons_cli compile depends.commons_cli
compile depends.guava compile depends.guava
testCompile depends.junit
proguard depends.proguard proguard depends.proguard
} }

View File

@ -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);
}
}

View File

@ -0,0 +1,16 @@
.class public LConstructorTest;
.super Ljava/lang/Object;
# direct methods
.method public constructor <init>()V
.registers 4
#v0=(Uninit);v1=(Uninit);v2=(Uninit);p0=(UninitThis,LConstructorTest;);
invoke-direct {p0}, Ljava/lang/Object;-><init>()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

View File

@ -0,0 +1,25 @@
.class public LConstructorTest2;
.super Ljava/lang/Object;
# direct methods
.method public constructor <init>()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;-><init>()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

View File

@ -0,0 +1,12 @@
.class public LConstructorTest;
.super Ljava/lang/Object;
# direct methods
.method public constructor <init>()V
.registers 4
invoke-direct {p0}, Ljava/lang/Object;-><init>()V
return-void
.end method

View File

@ -0,0 +1,17 @@
.class public LConstructorTest2;
.super Ljava/lang/Object;
# direct methods
.method public constructor <init>()V
.registers 4
if-eqz p0, :cond_3
nop
:cond_3
invoke-direct {p0}, Ljava/lang/Object;-><init>()V
return-void
.end method