Adding brut.apktool :/

This commit is contained in:
Connor Tumbleson
2012-09-19 20:27:35 -05:00
parent 60c806f2e2
commit 4db49ab347
96 changed files with 11218 additions and 1 deletions

View File

@ -0,0 +1,177 @@
/**
* Copyright 2011 Ryszard Wiśniewski <brut.alll@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package brut.androlib;
import brut.androlib.res.util.ExtFile;
import brut.common.BrutException;
import brut.util.OS;
import java.io.*;
import java.util.HashMap;
import java.util.logging.Logger;
import org.custommonkey.xmlunit.*;
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 BrutException {
sTmpDir = new ExtFile(OS.createTempDirectory());
sTestOrigDir = new ExtFile(sTmpDir, "testapp-orig");
sTestNewDir = new ExtFile(sTmpDir, "testapp-new");
File testApk = new File(sTmpDir, "testapp.apk");
LOGGER.info("Unpacking testapp...");
TestUtils.copyResourceDir(BuildAndDecodeTest.class,
"brut/apktool/testapp/", sTestOrigDir);
LOGGER.info("Building testapp.apk...");
ExtFile blank = null;
new Androlib().build(sTestOrigDir, testApk, BuildAndDecodeTest.returnStock(),blank);
LOGGER.info("Decoding testapp.apk...");
ApkDecoder apkDecoder = new ApkDecoder(testApk);
apkDecoder.setOutDir(sTestNewDir);
apkDecoder.decode();
}
@AfterClass
public static void afterClass() throws BrutException {
OS.rmdir(sTmpDir);
}
@Test
public void valuesArraysTest() throws BrutException {
compareValuesFiles("values-mcc001/arrays.xml");
compareValuesFiles("values-mcc002/arrays.xml");
}
@Test
public void valuesBoolsTest() throws BrutException {
compareValuesFiles("values-mcc001/bools.xml");
}
@Test
public void valuesColorsTest() throws BrutException {
compareValuesFiles("values-mcc001/colors.xml");
}
@Test
public void valuesDimensTest() throws BrutException {
compareValuesFiles("values-mcc001/dimens.xml");
}
@Test
public void valuesIdsTest() throws BrutException {
compareValuesFiles("values-mcc001/ids.xml");
}
@Test
public void valuesIntegersTest() throws BrutException {
compareValuesFiles("values-mcc001/integers.xml");
}
@Test
public void valuesStringsTest() throws BrutException {
compareValuesFiles("values-mcc001/strings.xml");
}
@Test
public void valuesReferencesTest() throws BrutException {
compareValuesFiles("values-mcc002/strings.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 xmlLiteralsTest() throws BrutException {
compareXmlFiles("res/xml/literals.xml");
}
@Test
public void xmlReferencesTest() throws BrutException {
compareXmlFiles("res/xml/references.xml");
}
@Test
public void qualifiersTest() throws BrutException {
compareValuesFiles("values-mcc004-mnc4-en-rUS-sw100dp-w200dp-h300dp" +
"-xlarge-long-land-desk-night-xhdpi-finger-keyssoft-12key" +
"-navhidden-dpad/strings.xml");
}
private void compareValuesFiles(String path) throws BrutException {
compareXmlFiles("res/" + path,
new ElementNameAndAttributeQualifier("name"));
}
private void compareXmlFiles(String path) throws BrutException {
compareXmlFiles(path, null);
}
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());
}

View File

@ -0,0 +1,138 @@
/**
* Copyright 2011 Ryszard Wiśniewski <brut.alll@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package brut.androlib;
import brut.common.BrutException;
import brut.directory.*;
import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import org.custommonkey.xmlunit.ElementQualifier;
import org.w3c.dom.Element;
import org.xmlpull.v1.*;
/**
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
*/
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));
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);
}
}
/* 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;
}
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.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 {
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"));
}
}
}

View File

@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest android:versionCode="1" android:versionName="1.0" package="brut.apktool.testapp"
xmlns:android="http://schemas.android.com/apk/res/android" />

View File

@ -0,0 +1,6 @@
version: 1.5.0
apkFileName: testapp.apk
isFrameworkApk: false
usesFramework:
ids:
- 1

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="test_array1">
<item>TEST1</item>
<item>TEST2</item>
<item>TEST3</item>
<item>%2$s foo %1$d</item>
</string-array>
<integer-array name="test_array2">
<item>-1</item>
<item>0</item>
<item>1</item>
</integer-array>
<array name="test_array3">
<item></item>
<item>true</item>
<item>TEST</item>
<item>5</item>
<item>5.5</item>
<item>10.0sp</item>
<item>#ff123456</item>
</array>
</resources>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="test_bool1">false</bool>
<bool name="test_bool2">true</bool>
</resources>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="test_color1">#ff123456</color>
</resources>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="test_dimen1">10.0dip</dimen>
<dimen name="test_dimen2">10.0sp</dimen>
<dimen name="test_dimen3">10.0pt</dimen>
<dimen name="test_dimen4">10.0px</dimen>
<dimen name="test_dimen5">10.0mm</dimen>
<dimen name="test_dimen6">10.0in</dimen>
</resources>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="test_id1" />
<item type="id" name="test_id2" />
</resources>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="test_integer1">-1</integer>
<integer name="test_integer2">0</integer>
<integer name="test_integer3">1</integer>
</resources>

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="test_string1"></string>
<string name="test_string2">Lorem ipsum...</string>
<string name="test_string3">\@</string>
<string name="test_string4">\?</string>
<string name="test_string5">\#ff123456</string>
<string name="test_string7">&amp;</string>
<string name="test_string8">"'"</string>
<string name="test_string9">\"</string>
<string name="test_string10">\u0005</string>
<string name="test_string11">" foo bar "</string>
<string name="test_string12">"foo
bar"</string>
<string name="test_string13">" foo"<b>bar <i> baz </i></b> <u>foo</u></string>
<string name="test_string14">foo<sometag someattr1="someval1" someattr2="someval2">bar</sometag>baz</string>
<string name="test_string16">foo<b>bar<i>"b
az"</i></b>foo</string>
<string name="test_string17" formatted="false">%d of %d</string>
<string name="test_string18">foo %d bar %</string>
<string name="test_string19">%2$s foo %1$d</string>
<string name="test_string20" formatted="false">%-e foo %,d</string>
<string name="test_string21">%2$-e foo %1$,d</string>
<string name="test_string22" formatted="false">%02d foo %01d</string>
<string name="test_string23" formatted="false">%d foo %1</string>
<string name="test_string24" formatted="false">%1% foo %2%</string>
</resources>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="test_array4">
<item>@string/test_string1</item>
<item>@string/test_string2</item>
</string-array>
</resources>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="test_reference1">@string/test1</string>
<string name="test_reference2">@*android:string/ok</string>
<string name="test_reference3">?android:textStyle</string>
</resources>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="bool" name="test_crossType_str2bool">TEST</item>
</resources>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="integer" name="test_crossType_str2int">TEST</item>
</resources>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="string" name="test_crossType_bool2str">true</item>
<item type="string" name="test_crossType_int2str">5</item>
</resources>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="test1" />
</resources>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
test1=""
test2="Lorem ipsum"
test3="\@"
test4="\?"
test5="#ff123456"
test6="\#ff123456"
test7="&amp;"
test8="'"
test9="&quot;"
test10="\u0005"
test11=" foo bar "
test12="foo \n bar"
test15="005"
/>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
test1="@string/test1"
test2="@*android:string/ok"
test3="?android:textStyle"
/>