mirror of
https://github.com/revanced/Apktool.git
synced 2025-06-13 13:27:37 +02:00
Code cleanup of 2013
Signed-off-by: Connor Tumbleson <connor.tumbleson@gmail.com>
This commit is contained in:
@ -26,175 +26,173 @@ import org.junit.*;
|
||||
import static org.junit.Assert.*;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
|
||||
/**
|
||||
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
|
||||
*/
|
||||
public class BuildAndDecodeTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception, BrutException {
|
||||
sTmpDir = new ExtFile(OS.createTempDirectory());
|
||||
sTestOrigDir = new ExtFile(sTmpDir, "testapp-orig");
|
||||
sTestNewDir = new ExtFile(sTmpDir, "testapp-new");
|
||||
LOGGER.info("Unpacking testapp...");
|
||||
TestUtils.copyResourceDir(BuildAndDecodeTest.class,
|
||||
"brut/apktool/testapp/", sTestOrigDir);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws BrutException {
|
||||
OS.rmdir(sTmpDir);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAaptInstalledTest() throws Exception {
|
||||
assertEquals(true, isAaptPresent());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void encodeAndDecodeTest() throws BrutException, IOException {
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception, BrutException {
|
||||
sTmpDir = new ExtFile(OS.createTempDirectory());
|
||||
sTestOrigDir = new ExtFile(sTmpDir, "testapp-orig");
|
||||
sTestNewDir = new ExtFile(sTmpDir, "testapp-new");
|
||||
LOGGER.info("Unpacking testapp...");
|
||||
TestUtils.copyResourceDir(BuildAndDecodeTest.class,
|
||||
"brut/apktool/testapp/", sTestOrigDir);
|
||||
}
|
||||
|
||||
LOGGER.info("Building testapp.apk...");
|
||||
File testApk = new File(sTmpDir, "testapp.apk");
|
||||
ExtFile blank = null;
|
||||
new Androlib().build(sTestOrigDir, testApk, BuildAndDecodeTest.returnStock(),blank,"");
|
||||
@AfterClass
|
||||
public static void afterClass() throws BrutException {
|
||||
OS.rmdir(sTmpDir);
|
||||
}
|
||||
|
||||
LOGGER.info("Decoding testapp.apk...");
|
||||
ApkDecoder apkDecoder = new ApkDecoder(testApk);
|
||||
apkDecoder.setOutDir(sTestNewDir);
|
||||
apkDecoder.decode();
|
||||
}
|
||||
@Test
|
||||
public void isAaptInstalledTest() throws Exception {
|
||||
assertEquals(true, isAaptPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeAndDecodeTest() throws BrutException, IOException {
|
||||
|
||||
@Test
|
||||
public void valuesArraysTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/arrays.xml");
|
||||
compareValuesFiles("values-mcc002/arrays.xml");
|
||||
}
|
||||
LOGGER.info("Building testapp.apk...");
|
||||
File testApk = new File(sTmpDir, "testapp.apk");
|
||||
ExtFile blank = null;
|
||||
new Androlib().build(sTestOrigDir, testApk,
|
||||
BuildAndDecodeTest.returnStock(), blank, "");
|
||||
|
||||
@Test
|
||||
public void valuesBoolsTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/bools.xml");
|
||||
}
|
||||
LOGGER.info("Decoding testapp.apk...");
|
||||
ApkDecoder apkDecoder = new ApkDecoder(testApk);
|
||||
apkDecoder.setOutDir(sTestNewDir);
|
||||
apkDecoder.decode();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void valuesColorsTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/colors.xml");
|
||||
}
|
||||
@Test
|
||||
public void valuesArraysTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/arrays.xml");
|
||||
compareValuesFiles("values-mcc002/arrays.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void valuesDimensTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/dimens.xml");
|
||||
}
|
||||
@Test
|
||||
public void valuesBoolsTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/bools.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void valuesIdsTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/ids.xml");
|
||||
}
|
||||
@Test
|
||||
public void valuesColorsTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/colors.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void valuesIntegersTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/integers.xml");
|
||||
}
|
||||
@Test
|
||||
public void valuesDimensTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/dimens.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void valuesStringsTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/strings.xml");
|
||||
}
|
||||
@Test
|
||||
public void valuesIdsTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/ids.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void valuesReferencesTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc002/strings.xml");
|
||||
}
|
||||
@Test
|
||||
public void valuesIntegersTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/integers.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void crossTypeTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc003/strings.xml");
|
||||
compareValuesFiles("values-mcc003/integers.xml");
|
||||
compareValuesFiles("values-mcc003/bools.xml");
|
||||
}
|
||||
@Test
|
||||
public void valuesStringsTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc001/strings.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xmlLiteralsTest() throws BrutException {
|
||||
compareXmlFiles("res/xml/literals.xml");
|
||||
}
|
||||
@Test
|
||||
public void valuesReferencesTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc002/strings.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xmlReferencesTest() throws BrutException {
|
||||
compareXmlFiles("res/xml/references.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void qualifiersTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc004-mnc4-en-rUS-ldrtl-sw100dp-w200dp-h300dp" +
|
||||
"-xlarge-long-land-desk-night-xhdpi-finger-keyssoft-12key" +
|
||||
"-navhidden-dpad/strings.xml");
|
||||
}
|
||||
|
||||
private static boolean isAaptPresent() throws Exception {
|
||||
boolean result = true;
|
||||
try
|
||||
{
|
||||
Process proc = Runtime.getRuntime().exec("aapt");
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
|
||||
String line = null;
|
||||
while ( (line = br.readLine()) != null){}
|
||||
} catch (Exception ex){
|
||||
result = false;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@Test
|
||||
public void crossTypeTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc003/strings.xml");
|
||||
compareValuesFiles("values-mcc003/integers.xml");
|
||||
compareValuesFiles("values-mcc003/bools.xml");
|
||||
}
|
||||
|
||||
private void compareValuesFiles(String path) throws BrutException {
|
||||
compareXmlFiles("res/" + path,
|
||||
new ElementNameAndAttributeQualifier("name"));
|
||||
}
|
||||
@Test
|
||||
public void xmlLiteralsTest() throws BrutException {
|
||||
compareXmlFiles("res/xml/literals.xml");
|
||||
}
|
||||
|
||||
private void compareXmlFiles(String path) throws BrutException {
|
||||
compareXmlFiles(path, null);
|
||||
}
|
||||
@Test
|
||||
public void xmlReferencesTest() throws BrutException {
|
||||
compareXmlFiles("res/xml/references.xml");
|
||||
}
|
||||
|
||||
private void compareXmlFiles(String path,
|
||||
ElementQualifier qualifier) throws BrutException {
|
||||
DetailedDiff diff;
|
||||
try {
|
||||
Reader control = new FileReader(
|
||||
new File(sTestOrigDir, path));
|
||||
Reader test = new FileReader(new File(sTestNewDir, path));
|
||||
@Test
|
||||
public void qualifiersTest() throws BrutException {
|
||||
compareValuesFiles("values-mcc004-mnc4-en-rUS-ldrtl-sw100dp-w200dp-h300dp"
|
||||
+ "-xlarge-long-land-desk-night-xhdpi-finger-keyssoft-12key"
|
||||
+ "-navhidden-dpad/strings.xml");
|
||||
}
|
||||
|
||||
diff = new DetailedDiff(new Diff(control, test));
|
||||
} catch (SAXException ex) {
|
||||
throw new BrutException(ex);
|
||||
} catch (IOException ex) {
|
||||
throw new BrutException(ex);
|
||||
}
|
||||
private static boolean isAaptPresent() throws Exception {
|
||||
boolean result = true;
|
||||
try {
|
||||
Process proc = Runtime.getRuntime().exec("aapt");
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(
|
||||
proc.getErrorStream()));
|
||||
String line = null;
|
||||
while ((line = br.readLine()) != null) {
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
result = false;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
if (qualifier != null) {
|
||||
diff.overrideElementQualifier(qualifier);
|
||||
}
|
||||
private void compareValuesFiles(String path) throws BrutException {
|
||||
compareXmlFiles("res/" + path, new ElementNameAndAttributeQualifier(
|
||||
"name"));
|
||||
}
|
||||
|
||||
assertTrue(path + ": " +
|
||||
diff.getAllDifferences().toString(), diff.similar());
|
||||
}
|
||||
|
||||
private static HashMap<String, Boolean> returnStock() throws BrutException {
|
||||
HashMap<String, Boolean> tmp = new HashMap<String, Boolean>();
|
||||
tmp.put("forceBuildAll", false);
|
||||
tmp.put("debug", false);
|
||||
tmp.put("verbose", false);
|
||||
tmp.put("injectOriginal", false);
|
||||
tmp.put("framework", false);
|
||||
tmp.put("update", false);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
private void compareXmlFiles(String path) throws BrutException {
|
||||
compareXmlFiles(path, null);
|
||||
}
|
||||
|
||||
private static ExtFile sTmpDir;
|
||||
private static ExtFile sTestOrigDir;
|
||||
private static ExtFile sTestNewDir;
|
||||
|
||||
private final static Logger LOGGER =
|
||||
Logger.getLogger(BuildAndDecodeTest.class.getName());
|
||||
private void compareXmlFiles(String path, ElementQualifier qualifier)
|
||||
throws BrutException {
|
||||
DetailedDiff diff;
|
||||
try {
|
||||
Reader control = new FileReader(new File(sTestOrigDir, path));
|
||||
Reader test = new FileReader(new File(sTestNewDir, path));
|
||||
|
||||
diff = new DetailedDiff(new Diff(control, test));
|
||||
} catch (SAXException ex) {
|
||||
throw new BrutException(ex);
|
||||
} catch (IOException ex) {
|
||||
throw new BrutException(ex);
|
||||
}
|
||||
|
||||
if (qualifier != null) {
|
||||
diff.overrideElementQualifier(qualifier);
|
||||
}
|
||||
|
||||
assertTrue(path + ": " + diff.getAllDifferences().toString(),
|
||||
diff.similar());
|
||||
}
|
||||
|
||||
private static HashMap<String, Boolean> returnStock() throws BrutException {
|
||||
HashMap<String, Boolean> tmp = new HashMap<String, Boolean>();
|
||||
tmp.put("forceBuildAll", false);
|
||||
tmp.put("debug", false);
|
||||
tmp.put("verbose", false);
|
||||
tmp.put("injectOriginal", false);
|
||||
tmp.put("framework", false);
|
||||
tmp.put("update", false);
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
private static ExtFile sTmpDir;
|
||||
private static ExtFile sTestOrigDir;
|
||||
private static ExtFile sTestNewDir;
|
||||
|
||||
private final static Logger LOGGER = Logger
|
||||
.getLogger(BuildAndDecodeTest.class.getName());
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
/**
|
||||
/**
|
||||
* Copyright 2011 Ryszard Wiśniewski <brut.alll@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@ -32,107 +32,110 @@ import org.xmlpull.v1.*;
|
||||
*/
|
||||
public abstract class TestUtils {
|
||||
|
||||
public static Map<String, String> parseStringsXml(File file)
|
||||
throws BrutException {
|
||||
try {
|
||||
XmlPullParser xpp = XmlPullParserFactory.newInstance().newPullParser();
|
||||
xpp.setInput(new FileReader(file));
|
||||
public static Map<String, String> parseStringsXml(File file)
|
||||
throws BrutException {
|
||||
try {
|
||||
XmlPullParser xpp = XmlPullParserFactory.newInstance()
|
||||
.newPullParser();
|
||||
xpp.setInput(new FileReader(file));
|
||||
|
||||
int eventType;
|
||||
String key = null;
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
while ((eventType = xpp.next()) != XmlPullParser.END_DOCUMENT) {
|
||||
switch (eventType) {
|
||||
case XmlPullParser.START_TAG:
|
||||
if ("string".equals(xpp.getName())) {
|
||||
int attrCount = xpp.getAttributeCount();
|
||||
for (int i = 0; i < attrCount; i++) {
|
||||
if ("name".equals(xpp.getAttributeName(i))) {
|
||||
key = xpp.getAttributeValue(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case XmlPullParser.END_TAG:
|
||||
if ("string".equals(xpp.getName())) {
|
||||
key = null;
|
||||
}
|
||||
break;
|
||||
case XmlPullParser.TEXT:
|
||||
if (key != null) {
|
||||
map.put(key, xpp.getText());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
int eventType;
|
||||
String key = null;
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
while ((eventType = xpp.next()) != XmlPullParser.END_DOCUMENT) {
|
||||
switch (eventType) {
|
||||
case XmlPullParser.START_TAG:
|
||||
if ("string".equals(xpp.getName())) {
|
||||
int attrCount = xpp.getAttributeCount();
|
||||
for (int i = 0; i < attrCount; i++) {
|
||||
if ("name".equals(xpp.getAttributeName(i))) {
|
||||
key = xpp.getAttributeValue(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case XmlPullParser.END_TAG:
|
||||
if ("string".equals(xpp.getName())) {
|
||||
key = null;
|
||||
}
|
||||
break;
|
||||
case XmlPullParser.TEXT:
|
||||
if (key != null) {
|
||||
map.put(key, xpp.getText());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
} catch (IOException ex) {
|
||||
throw new BrutException(ex);
|
||||
} catch (XmlPullParserException ex) {
|
||||
throw new BrutException(ex);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
} catch (IOException ex) {
|
||||
throw new BrutException(ex);
|
||||
} catch (XmlPullParserException ex) {
|
||||
throw new BrutException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/* TODO: move to brut.util.Jar - it's not possible for now, because below
|
||||
* implementation uses brut.dir. I think I should merge all my projects to
|
||||
* single brut.common .
|
||||
*/
|
||||
public static void copyResourceDir(Class class_, String dirPath, File out)
|
||||
throws BrutException {
|
||||
if (! out.exists()) {
|
||||
out.mkdirs();
|
||||
}
|
||||
copyResourceDir(class_, dirPath, new FileDirectory(out));
|
||||
}
|
||||
/*
|
||||
* TODO: move to brut.util.Jar - it's not possible for now, because below
|
||||
* implementation uses brut.dir. I think I should merge all my projects to
|
||||
* single brut.common .
|
||||
*/
|
||||
public static void copyResourceDir(Class class_, String dirPath, File out)
|
||||
throws BrutException {
|
||||
if (!out.exists()) {
|
||||
out.mkdirs();
|
||||
}
|
||||
copyResourceDir(class_, dirPath, new FileDirectory(out));
|
||||
}
|
||||
|
||||
public static void copyResourceDir(Class class_, String dirPath,
|
||||
Directory out) throws BrutException {
|
||||
if (class_ == null) {
|
||||
class_ = Class.class;
|
||||
}
|
||||
public static void copyResourceDir(Class class_, String dirPath,
|
||||
Directory out) throws BrutException {
|
||||
if (class_ == null) {
|
||||
class_ = Class.class;
|
||||
}
|
||||
|
||||
URL dirURL = class_.getClassLoader().getResource(dirPath);
|
||||
if (dirURL != null && dirURL.getProtocol().equals("file")) {
|
||||
DirUtil.copyToDir(new FileDirectory(dirURL.getFile()), out);
|
||||
return;
|
||||
}
|
||||
URL dirURL = class_.getClassLoader().getResource(dirPath);
|
||||
if (dirURL != null && dirURL.getProtocol().equals("file")) {
|
||||
DirUtil.copyToDir(new FileDirectory(dirURL.getFile()), out);
|
||||
return;
|
||||
}
|
||||
|
||||
if (dirURL == null) {
|
||||
String className = class_.getName().replace(".", "/") + ".class";
|
||||
dirURL = class_.getClassLoader().getResource(className);
|
||||
}
|
||||
if (dirURL == null) {
|
||||
String className = class_.getName().replace(".", "/") + ".class";
|
||||
dirURL = class_.getClassLoader().getResource(className);
|
||||
}
|
||||
|
||||
if (dirURL.getProtocol().equals("jar")) {
|
||||
String jarPath;
|
||||
try {
|
||||
jarPath = URLDecoder.decode(
|
||||
dirURL.getPath().substring(5,
|
||||
dirURL.getPath().indexOf("!")), "UTF-8");
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
throw new BrutException(ex);
|
||||
}
|
||||
DirUtil.copyToDir(new FileDirectory(jarPath), out);
|
||||
}
|
||||
}
|
||||
|
||||
if (dirURL.getProtocol().equals("jar")) {
|
||||
String jarPath;
|
||||
try {
|
||||
jarPath = URLDecoder.decode(dirURL.getPath().substring(
|
||||
5, dirURL.getPath().indexOf("!")), "UTF-8");
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
throw new BrutException(ex);
|
||||
}
|
||||
DirUtil.copyToDir(new FileDirectory(jarPath), out);
|
||||
}
|
||||
}
|
||||
public static class ResValueElementQualifier implements ElementQualifier {
|
||||
|
||||
@Override
|
||||
public boolean qualifyForComparison(Element control, Element test) {
|
||||
String controlType = control.getTagName();
|
||||
if ("item".equals(controlType)) {
|
||||
controlType = control.getAttribute("type");
|
||||
}
|
||||
|
||||
public static class ResValueElementQualifier implements ElementQualifier {
|
||||
String testType = test.getTagName();
|
||||
if ("item".equals(testType)) {
|
||||
testType = test.getAttribute("type");
|
||||
}
|
||||
|
||||
public boolean qualifyForComparison(Element control, Element test) {
|
||||
String controlType = control.getTagName();
|
||||
if ("item".equals(controlType)) {
|
||||
controlType = control.getAttribute("type");
|
||||
}
|
||||
|
||||
String testType = test.getTagName();
|
||||
if ("item".equals(testType)) {
|
||||
testType = test.getAttribute("type");
|
||||
}
|
||||
|
||||
return controlType.equals(testType) && control.getAttribute("name")
|
||||
.equals(test.getAttribute("name"));
|
||||
}
|
||||
}
|
||||
return controlType.equals(testType)
|
||||
&& control.getAttribute("name").equals(
|
||||
test.getAttribute("name"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user