Apktool/brut.apktool/apktool-lib/src/test/java/brut/androlib/TestUtils.java

159 lines
5.6 KiB
Java
Raw Normal View History

2019-10-16 07:49:58 +02:00
/*
* Copyright (C) 2010 Ryszard Wiśniewski <brut.alll@gmail.com>
* Copyright (C) 2010 Connor Tumbleson <connor.tumbleson@gmail.com>
2012-09-20 03:27:35 +02:00
*
* 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
*
2021-08-24 15:31:41 +02:00
* https://www.apache.org/licenses/LICENSE-2.0
2012-09-20 03:27:35 +02:00
*
* 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.exceptions.AndrolibException;
import brut.androlib.res.Framework;
import brut.androlib.res.xml.ResXmlPatcher;
2012-09-20 03:27:35 +02:00
import brut.common.BrutException;
import brut.directory.DirUtil;
import brut.directory.Directory;
import brut.directory.FileDirectory;
import brut.util.OS;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import javax.xml.parsers.ParserConfigurationException;
2012-09-20 03:27:35 +02:00
import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.Files;
2012-09-20 03:27:35 +02:00
import java.util.HashMap;
import java.util.Map;
2012-09-20 03:27:35 +02:00
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;
2021-08-26 19:00:37 +02:00
Map<String, String> map = new HashMap<>();
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 | XmlPullParserException ex) {
throw new BrutException(ex);
}
}
public static Document getDocumentFromFile(File file) throws BrutException {
try {
return ResXmlPatcher.loadDocument(file);
} catch (ParserConfigurationException | SAXException | IOException ex) {
throw new BrutException(ex);
}
}
2021-08-26 19:00:37 +02:00
public static void copyResourceDir(Class<?> class_, String dirPath, File out) throws BrutException {
if (!out.exists()) {
out.mkdirs();
}
copyResourceDir(class_, dirPath, new FileDirectory(out));
}
2021-08-26 19:00:37 +02:00
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")) {
2016-12-25 14:47:20 +01:00
try {
DirUtil.copyToDir(new FileDirectory(dirURL.getFile()), out);
} catch (UnsupportedEncodingException ex) {
throw new BrutException(ex);
}
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");
2016-12-25 14:47:20 +01:00
DirUtil.copyToDir(new FileDirectory(jarPath), out);
} catch (UnsupportedEncodingException ex) {
throw new BrutException(ex);
}
}
}
public static void cleanFrameworkFile() throws BrutException {
File framework = new File(getFrameworkDirectory(), "1.apk");
if (Files.exists(framework.toPath())) {
OS.rmfile(framework.getAbsolutePath());
}
}
2017-12-07 22:38:38 +01:00
public static byte[] readHeaderOfFile(File file, int size) throws IOException {
byte[] buffer = new byte[size];
InputStream inputStream = Files.newInputStream(file.toPath());
2017-12-07 22:38:38 +01:00
if (inputStream.read(buffer) != buffer.length) {
throw new IOException("File size too small for buffer length: " + size);
}
inputStream.close();
return buffer;
}
static File getFrameworkDirectory() throws AndrolibException {
Config config = Config.getDefaultConfig();
Framework framework = new Framework(config);
return framework.getFrameworkDirectory();
}
public static String replaceNewlines(String value) {
return value.replace("\n", "").replace("\r", "");
}
2012-09-20 03:27:35 +02:00
}