AnalysisTest.java revision fce4cc0c425acfd86a2a89cc70b5199bce9ff51c
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 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 @Test 80 public void LocalTest() throws IOException, URISyntaxException { 81 runTest("LocalTest", false); 82 } 83 84 public void runTest(String test, boolean registerInfo) throws IOException, URISyntaxException { 85 String dexFilePath = String.format("%s%sclasses.dex", test, File.separatorChar); 86 87 DexFile dexFile = DexFileFactory.loadDexFile(findResource(dexFilePath), 15); 88 89 baksmaliOptions options = new baksmaliOptions(); 90 if (registerInfo) { 91 options.registerInfo = baksmaliOptions.ALL | baksmaliOptions.FULLMERGE; 92 options.classPath = new ClassPath(); 93 } 94 95 for (ClassDef classDef: dexFile.getClasses()) { 96 StringWriter stringWriter = new StringWriter(); 97 IndentingWriter writer = new IndentingWriter(stringWriter); 98 ClassDefinition classDefinition = new ClassDefinition(options, classDef); 99 classDefinition.writeTo(writer); 100 writer.close(); 101 102 String className = classDef.getType(); 103 String smaliPath = String.format("%s%s%s.smali", test, File.separatorChar, 104 className.substring(1, className.length() - 1)); 105 String smaliContents = readResource(smaliPath); 106 107 String newline = System.getProperty("line.separator"); 108 Assert.assertEquals(smaliContents.replace("\r", "").replace("\n", newline), 109 stringWriter.toString().replace("\r", "").replace("\n", newline)); 110 } 111 } 112 113 @Nonnull 114 private File findResource(String resource) throws URISyntaxException { 115 URL resUrl = Resources.getResource(resource); 116 return new File(resUrl.toURI()); 117 } 118 119 @Nonnull 120 private String readResource(String resource) throws URISyntaxException, IOException { 121 URL url = Resources.getResource(resource); 122 return Resources.toString(url, Charsets.UTF_8); 123 } 124} 125