apktool-lib: added basic tests for resources building/decoding.

This commit is contained in:
Ryszard Wiśniewski 2011-05-02 02:05:46 +02:00
parent e1f3ab852a
commit d86909707b
9 changed files with 306 additions and 0 deletions

View File

@ -34,6 +34,7 @@
<excludes>
<exclude>src/main/java/com/mindprod/**</exclude>
<exclude>src/main/java/android/**</exclude>
<exclude>src/test/resources/brut/apktool/testapp/**</exclude>
</excludes>
</configuration>
</plugin>
@ -81,5 +82,15 @@
<artifactId>xpp3</artifactId>
<version>1.1.4c</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
<dependency>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,113 @@
/**
* 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.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...");
new Androlib().build(sTestOrigDir, testApk, false, false);
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 literalStringsTest() throws BrutException {
compareValuesFiles("values-mcc001/strings.xml");
}
@Test
public void referenceStringsTest() throws BrutException {
compareValuesFiles("values-mcc002/strings.xml");
}
@Test
public void layout1Test() throws BrutException {
compareXmlFiles("res/layout/layout1.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 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.3.2
apkFileName: testapp.apk
isFrameworkApk: false
usesFramework:
ids:
- 1

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/test1"
test1="@string/test1"
style="?android:textStyle"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@android:string/ok"
/>
</LinearLayout>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string name="test_literal1"></string>
<string name="test_literal2">Lorem ipsum...</string>
<string name="test_literal3">\@</string>
<string name="test_literal4">\?</string>
<string name="test_literal5">&amp;</string>
</resources>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string name="test_reference1">@android:string/ok</string>
</resources>

View File

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