| 1 |
/* |
| 2 |
* Copyright (C) 2010 Ryszard Wiśniewski <[email protected]> |
| 3 |
* Copyright (C) 2010 Connor Tumbleson <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* https://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package brut.androlib; |
| 18 |
|
| 19 |
import brut.androlib.options.BuildOptions; |
| 20 |
import brut.androlib.res.AndrolibResources; |
| 21 |
import brut.common.BrutException; |
| 22 |
import brut.directory.DirUtil; |
| 23 |
import brut.directory.Directory; |
| 24 |
import brut.directory.FileDirectory; |
| 25 |
import brut.util.OS; |
| 26 |
import org.custommonkey.xmlunit.ElementQualifier; |
| 27 |
import org.w3c.dom.Element; |
| 28 |
import org.xmlpull.v1.XmlPullParser; |
| 29 |
import org.xmlpull.v1.XmlPullParserException; |
| 30 |
import org.xmlpull.v1.XmlPullParserFactory; |
| 31 |
|
| 32 |
import java.io.*; |
| 33 |
import java.net.URL; |
| 34 |
import java.net.URLDecoder; |
| 35 |
import java.nio.file.Files; |
| 36 |
import java.util.HashMap; |
| 37 |
import java.util.Map; |
| 38 |
|
| 39 |
public abstract class TestUtils { |
| 40 |
|
| 41 |
public static Map<String, String> parseStringsXml(File file) |
| 42 |
throws BrutException { |
| 43 |
try { |
| 44 |
XmlPullParser xpp = XmlPullParserFactory.newInstance().newPullParser(); |
| 45 |
xpp.setInput(new FileReader(file)); |
| 46 |
|
| 47 |
int eventType; |
| 48 |
String key = null; |
| 49 |
Map<String, String> map = new HashMap<>(); |
| 50 |
while ((eventType = xpp.next()) != XmlPullParser.END_DOCUMENT) { |
| 51 |
switch (eventType) { |
| 52 |
case XmlPullParser.START_TAG: |
| 53 |
if ("string".equals(xpp.getName())) { |
| 54 |
int attrCount = xpp.getAttributeCount(); |
| 55 |
for (int i = 0; i < attrCount; i++) { |
| 56 |
if ("name".equals(xpp.getAttributeName(i))) { |
| 57 |
key = xpp.getAttributeValue(i); |
| 58 |
break; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
break; |
| 63 |
case XmlPullParser.END_TAG: |
| 64 |
if ("string".equals(xpp.getName())) { |
| 65 |
key = null; |
| 66 |
} |
| 67 |
break; |
| 68 |
case XmlPullParser.TEXT: |
| 69 |
if (key != null) { |
| 70 |
map.put(key, xpp.getText()); |
| 71 |
} |
| 72 |
break; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
return map; |
| 77 |
} catch (IOException | XmlPullParserException ex) { |
| 78 |
throw new BrutException(ex); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public static void copyResourceDir(Class<?> class_, String dirPath, File out) throws BrutException { |
| 83 |
if (!out.exists()) { |
| 84 |
out.mkdirs(); |
| 85 |
} |
| 86 |
copyResourceDir(class_, dirPath, new FileDirectory(out)); |
| 87 |
} |
| 88 |
|
| 89 |
public static void copyResourceDir(Class<?> class_, String dirPath, Directory out) throws BrutException { |
| 90 |
if (class_ == null) { |
| 91 |
class_ = Class.class; |
| 92 |
} |
| 93 |
|
| 94 |
URL dirURL = class_.getClassLoader().getResource(dirPath); |
| 95 |
if (dirURL != null && dirURL.getProtocol().equals("file")) { |
| 96 |
try { |
| 97 |
DirUtil.copyToDir(new FileDirectory(dirURL.getFile()), out); |
| 98 |
} catch (UnsupportedEncodingException ex) { |
| 99 |
throw new BrutException(ex); |
| 100 |
} |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
if (dirURL == null) { |
| 105 |
String className = class_.getName().replace(".", "/") + ".class"; |
| 106 |
dirURL = class_.getClassLoader().getResource(className); |
| 107 |
} |
| 108 |
|
| 109 |
if (dirURL.getProtocol().equals("jar")) { |
| 110 |
String jarPath; |
| 111 |
try { |
| 112 |
jarPath = URLDecoder.decode(dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")), "UTF-8"); |
| 113 |
DirUtil.copyToDir(new FileDirectory(jarPath), out); |
| 114 |
} catch (UnsupportedEncodingException ex) { |
| 115 |
throw new BrutException(ex); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
public static void cleanFrameworkFile() throws BrutException { |
| 121 |
File framework = new File(getFrameworkDir(), "1.apk"); |
| 122 |
|
| 123 |
if (Files.exists(framework.toPath())) { |
| 124 |
OS.rmfile(framework.getAbsolutePath()); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
public static byte[] readHeaderOfFile(File file, int size) throws IOException { |
| 129 |
byte[] buffer = new byte[size]; |
| 130 |
InputStream inputStream = new FileInputStream(file); |
| 131 |
if (inputStream.read(buffer) != buffer.length) { |
| 132 |
throw new IOException("File size too small for buffer length: " + size); |
| 133 |
} |
| 134 |
inputStream.close(); |
| 135 |
|
| 136 |
return buffer; |
| 137 |
} |
| 138 |
|
| 139 |
static File getFrameworkDir() throws AndrolibException { |
| 140 |
AndrolibResources androlibResources = new AndrolibResources(); |
| 141 |
androlibResources.buildOptions = new BuildOptions(); |
| 142 |
return androlibResources.getFrameworkDir(); |
| 143 |
} |
| 144 |
|
| 145 |
public static class ResValueElementQualifier implements ElementQualifier { |
| 146 |
|
| 147 |
@Override |
| 148 |
public boolean qualifyForComparison(Element control, Element test) { |
| 149 |
String controlType = control.getTagName(); |
| 150 |
if ("item".equals(controlType)) { |
| 151 |
controlType = control.getAttribute("type"); |
| 152 |
} |
| 153 |
|
| 154 |
String testType = test.getTagName(); |
| 155 |
if ("item".equals(testType)) { |
| 156 |
testType = test.getAttribute("type"); |
| 157 |
} |
| 158 |
|
| 159 |
return controlType.equals(testType) && control.getAttribute("name").equals(test.getAttribute("name")); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
public static String replaceNewlines(String value) { |
| 164 |
return value.replace("\n", "").replace("\r", ""); |
| 165 |
} |
| 166 |
} |