Debian Codegraph @

brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt1/BuildAndDecodeTest.java

raw
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.aapt1;
18
19 import brut.androlib.Androlib;
20 import brut.androlib.ApkDecoder;
21 import brut.androlib.BaseTest;
22 import brut.androlib.TestUtils;
23 import brut.androlib.meta.MetaInfo;
24 import brut.common.BrutException;
25 import brut.directory.ExtFile;
26 import brut.util.OS;
27 import brut.util.OSDetection;
28 import org.junit.AfterClass;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31
32 import javax.imageio.ImageIO;
33 import java.awt.image.BufferedImage;
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.Map;
37
38 import static org.junit.Assert.*;
39 import static org.junit.Assume.assumeTrue;
40
41 public class BuildAndDecodeTest extends BaseTest {
42
43 @BeforeClass
44 public static void beforeClass() throws Exception {
45 TestUtils.cleanFrameworkFile();
46
47 sTmpDir = new ExtFile(OS.createTempDirectory());
48 sTestOrigDir = new ExtFile(sTmpDir, "testapp-orig");
49 sTestNewDir = new ExtFile(sTmpDir, "testapp-new");
50 LOGGER.info("Unpacking testapp...");
51 TestUtils.copyResourceDir(BuildAndDecodeTest.class, "aapt1/testapp/", sTestOrigDir);
52
53 LOGGER.info("Building testapp.apk...");
54 File testApk = new File(sTmpDir, "testapp.apk");
55 new Androlib().build(sTestOrigDir, testApk);
56
57 LOGGER.info("Decoding testapp.apk...");
58 ApkDecoder apkDecoder = new ApkDecoder(testApk);
59 apkDecoder.setOutDir(sTestNewDir);
60 apkDecoder.decode();
61 }
62
63 @AfterClass
64 public static void afterClass() throws BrutException {
65 OS.rmdir(sTmpDir);
66 }
67
68 @Test
69 public void buildAndDecodeTest() {
70 assertTrue(sTestNewDir.isDirectory());
71 }
72
73 @Test
74 public void manifestTaggingNotSupressed() throws BrutException {
75 compareXmlFiles("AndroidManifest.xml");
76 }
77
78 @Test
79 public void valuesAnimsTest() throws BrutException {
80 compareValuesFiles("values-mcc001/anims.xml");
81 }
82
83 @Test
84 public void valuesArraysTest() throws BrutException {
85 compareValuesFiles("values-mcc001/arrays.xml");
86 }
87
88 @Test
89 public void valuesArraysCastingTest() throws BrutException {
90 compareValuesFiles("values-mcc002/arrays.xml");
91 compareValuesFiles("values-mcc003/arrays.xml");
92 }
93
94 @Test
95 public void valuesAttrsTest() throws BrutException {
96 compareValuesFiles("values/attrs.xml");
97 }
98
99 @Test
100 public void valuesBoolsTest() throws BrutException {
101 compareValuesFiles("values-mcc001/bools.xml");
102 }
103
104 @Test
105 public void valuesColorsTest() throws BrutException {
106 compareValuesFiles("values-mcc001/colors.xml");
107 }
108
109 @Test
110 public void bug702Test() throws BrutException {
111 compareValuesFiles("values-mcc001-mnc00/strings.xml");
112 }
113
114 @Test
115 public void valuesDimensTest() throws BrutException {
116 compareValuesFiles("values-mcc001/dimens.xml");
117 }
118
119 @Test
120 public void valuesDrawablesTest() throws BrutException {
121 compareValuesFiles("values-mcc001/drawables.xml");
122 }
123
124 @Test
125 public void valuesIdsTest() throws BrutException {
126 compareValuesFiles("values-mcc001/ids.xml");
127 }
128
129 @Test
130 public void valuesIntegersTest() throws BrutException {
131 compareValuesFiles("values-mcc001/integers.xml");
132 }
133
134 @Test
135 public void valuesLayoutsTest() throws BrutException {
136 compareValuesFiles("values-mcc001/layouts.xml");
137 }
138
139 @Test
140 public void xmlPluralsTest() throws BrutException {
141 compareValuesFiles("values-mcc001/plurals.xml");
142 }
143
144 @Test
145 public void valuesStringsTest() throws BrutException {
146 compareValuesFiles("values-mcc001/strings.xml");
147 }
148
149 @Test
150 public void valuesStylesTest() throws BrutException {
151 compareValuesFiles("values-mcc001/styles.xml");
152 }
153
154 @Test
155 public void valuesReferencesTest() throws BrutException {
156 compareValuesFiles("values-mcc002/strings.xml");
157 }
158
159 @Test
160 public void valuesExtraLongTest() throws BrutException {
161 compareValuesFiles("values-en/strings.xml");
162 }
163
164 @Test
165 public void valuesExtraLongExactLengthTest() throws BrutException {
166 Map<String, String> strs = TestUtils.parseStringsXml(new File(sTestNewDir, "res/values-en/strings.xml"));
167
168 // long_string6 should be exactly 0x8888 chars of "a"
169 // the valuesExtraLongTest() should handle this
170 // but such an edge case, want a specific test
171 String aaaa = strs.get("long_string6");
172 assertEquals(0x8888, aaaa.length());
173 }
174
175 @Test
176 public void storedMp3FilesAreNotCompressedTest() throws BrutException {
177 ExtFile extFile = new ExtFile(sTmpDir, "testapp.apk");
178 Integer built = extFile.getDirectory().getCompressionLevel("res/raw/rain.mp3");
179 assertEquals(Integer.valueOf(0), built);
180 }
181
182 @Test
183 public void crossTypeTest() throws BrutException {
184 compareValuesFiles("values-mcc003/strings.xml");
185 compareValuesFiles("values-mcc003/integers.xml");
186 compareValuesFiles("values-mcc003/bools.xml");
187 }
188
189 @Test
190 public void xmlLiteralsTest() throws BrutException {
191 compareXmlFiles("res/xml/literals.xml");
192 }
193
194 @Test
195 public void xmlReferencesTest() throws BrutException {
196 compareXmlFiles("res/xml/references.xml");
197 }
198
199 @Test
200 public void xmlXsdFileTest() throws BrutException {
201 compareXmlFiles("res/xml/ww_box_styles_schema.xsd");
202 }
203
204 @Test
205 public void xmlIdsEmptyTest() throws BrutException {
206 compareXmlFiles("res/values/ids.xml");
207 }
208
209 @Test
210 public void xmlReferenceAttributeTest() throws BrutException {
211 compareXmlFiles("res/layout/issue1040.xml");
212 }
213
214 @Test
215 public void xmlCustomAttributeTest() throws BrutException {
216 compareXmlFiles("res/layout/issue1063.xml");
217 }
218
219 @Test
220 public void xmlSmallNumbersDontEscapeTest() throws BrutException {
221 compareXmlFiles("res/layout/issue1130.xml");
222 }
223
224 @Test
225 public void xmlUniformAutoTextTest() throws BrutException {
226 compareXmlFiles("res/layout/issue1674.xml");
227 }
228
229 @Test(expected = AssertionError.class)
230 public void xmlFillParentBecomesMatchTest() throws BrutException {
231 compareXmlFiles("res/layout/issue1274.xml");
232 }
233
234 @Test
235 public void xmlCustomAttrsNotAndroidTest() throws BrutException {
236 compareXmlFiles("res/layout/issue1157.xml");
237 }
238
239 @Test
240 public void qualifiersTest() throws BrutException {
241 compareValuesFiles("values-mcc004-mnc4-en-rUS-ldrtl-sw100dp-w200dp-h300dp"
242 + "-long-round-highdr-land-desk-night-xhdpi-finger-keyssoft-12key"
243 + "-navhidden-dpad-v26/strings.xml");
244 }
245
246 @Test
247 public void shortendedMncTest() throws BrutException {
248 compareValuesFiles("values-mcc001-mnc1/strings.xml");
249 }
250
251 @Test
252 public void shortMncHtcTest() throws BrutException {
253 compareValuesFiles("values-mnc1/strings.xml");
254 }
255
256 @Test
257 public void shortMncv2Test() throws BrutException {
258 compareValuesFiles("values-mcc238-mnc6/strings.xml");
259 }
260
261 @Test
262 public void longMncTest() throws BrutException {
263 compareValuesFiles("values-mcc238-mnc870/strings.xml");
264 }
265
266 @Test
267 public void anyDpiTest() throws BrutException {
268 compareValuesFiles("values-watch/strings.xml");
269 }
270
271 @Test
272 public void packed3CharsTest() throws BrutException {
273 compareValuesFiles("values-ast-rES/strings.xml");
274 }
275
276 @Test
277 public void rightToLeftTest() throws BrutException {
278 compareValuesFiles("values-ldrtl/strings.xml");
279 }
280
281 @Test
282 public void scriptBcp47Test() throws BrutException {
283 compareValuesFiles("values-b+en+Latn+US/strings.xml");
284 }
285
286 @Test
287 public void threeLetterLangBcp47Test() throws BrutException {
288 compareValuesFiles("values-ast/strings.xml");
289 }
290
291 @Test
292 public void androidOStringTest() throws BrutException {
293 compareValuesFiles("values-ast/strings.xml");
294 }
295
296 @Test
297 public void twoLetterNotHandledAsBcpTest() {
298 checkFolderExists("res/values-fr");
299 }
300
301 @Test
302 public void twoLetterLangBcp47Test() throws BrutException {
303 compareValuesFiles("values-en-rUS/strings.xml");
304 }
305
306 @Test
307 public void variantBcp47Test() throws BrutException {
308 compareValuesFiles("values-b+en+US+POSIX/strings.xml");
309 }
310
311 @Test
312 public void fourpartBcp47Test() throws BrutException {
313 compareValuesFiles("values-b+ast+Latn+IT+AREVELA/strings.xml");
314 }
315
316 @Test
317 public void RegionLocaleBcp47Test() throws BrutException {
318 compareValuesFiles("values-b+en+Latn+419/strings.xml");
319 }
320
321 @Test
322 public void numericalRegionBcp47Test() throws BrutException {
323 compareValuesFiles("values-b+eng+419/strings.xml");
324 }
325
326 @Test
327 public void api23ConfigurationsTest() throws BrutException {
328 compareValuesFiles("values-round/strings.xml");
329 compareValuesFiles("values-notround/strings.xml");
330 }
331
332 @Test
333 public void api26ConfigurationsTest() throws BrutException {
334 compareValuesFiles("values-widecg-v26/strings.xml");
335 compareValuesFiles("values-lowdr-v26/strings.xml");
336 compareValuesFiles("values-nowidecg-v26/strings.xml");
337 compareValuesFiles("values-vrheadset-v26/strings.xml");
338 }
339
340 @Test
341 public void fontTest() throws BrutException {
342 File fontXml = new File((sTestNewDir + "/res/font"), "lobster.xml");
343 File fontFile = new File((sTestNewDir + "/res/font"), "lobster_regular.otf");
344
345 // Per #1662, ensure font file is not encoded.
346 assertTrue(fontXml.isFile());
347 compareXmlFiles("/res/font/lobster.xml");
348
349 // If we properly skipped decoding the font (otf) file, this file should not exist
350 assertFalse((new File((sTestNewDir + "/res/values"), "fonts.xml")).isFile());
351 assertTrue(fontFile.isFile());
352 }
353
354 @Test
355 public void drawableNoDpiTest() throws BrutException, IOException {
356 compareResFolder("drawable-nodpi");
357 }
358
359 @Test
360 public void drawableAnyDpiTest() throws BrutException, IOException {
361 compareResFolder("drawable-anydpi");
362 }
363
364 @Test
365 public void drawableNumberedDpiTest() throws BrutException, IOException {
366 compareResFolder("drawable-534dpi");
367 }
368
369 @Test
370 public void drawableLdpiTest() throws BrutException, IOException {
371 compareResFolder("drawable-ldpi");
372 }
373
374 @Test
375 public void drawableMdpiTest() throws BrutException, IOException {
376 compareResFolder("drawable-mdpi");
377 }
378
379 @Test
380 public void drawableTvdpiTest() throws BrutException, IOException {
381 compareResFolder("drawable-tvdpi");
382 }
383
384 @Test
385 public void drawableXhdpiTest() throws BrutException, IOException {
386 compareResFolder("drawable-xhdpi");
387 }
388
389 @Test
390 public void ninePatchImageColorTest() throws IOException {
391 char slash = File.separatorChar;
392 String location = slash + "res" + slash + "drawable-xhdpi" + slash;
393
394 File control = new File((sTestOrigDir + location), "9patch.9.png");
395 File test = new File((sTestNewDir + location), "9patch.9.png");
396
397 BufferedImage controlImage = ImageIO.read(control);
398 BufferedImage testImage = ImageIO.read(test);
399
400 // lets start with 0,0 - empty
401 assertEquals(controlImage.getRGB(0, 0), testImage.getRGB(0, 0));
402
403 // then with 30, 0 - black
404 assertEquals(controlImage.getRGB(30, 0), testImage.getRGB(30, 0));
405
406 // then 30, 30 - blue
407 assertEquals(controlImage.getRGB(30, 30), testImage.getRGB(30, 30));
408 }
409
410 @Test
411 public void issue1508Test() throws IOException {
412 char slash = File.separatorChar;
413 String location = slash + "res" + slash + "drawable-xhdpi" + slash;
414
415 File control = new File((sTestOrigDir + location), "btn_zoom_up_normal.9.png");
416 File test = new File((sTestNewDir + location), "btn_zoom_up_normal.9.png");
417
418 BufferedImage controlImage = ImageIO.read(control);
419 BufferedImage testImage = ImageIO.read(test);
420
421 // 0, 0 = clear
422 assertEquals(controlImage.getRGB(0, 0), testImage.getRGB(0, 0));
423
424 // 30, 0 = black line
425 assertEquals(controlImage.getRGB(0, 30), testImage.getRGB(0, 30));
426
427 // 30, 30 = greyish button
428 assertEquals(controlImage.getRGB(30, 30), testImage.getRGB(30, 30));
429 }
430
431 @Test
432 public void issue1511Test() throws IOException {
433 char slash = File.separatorChar;
434 String location = slash + "res" + slash + "drawable-xxhdpi" + slash;
435
436 File control = new File((sTestOrigDir + location), "textfield_activated_holo_dark.9.png");
437 File test = new File((sTestNewDir + location), "textfield_activated_holo_dark.9.png");
438
439 BufferedImage controlImage = ImageIO.read(control);
440 BufferedImage testImage = ImageIO.read(test);
441
442 // Check entire image as we cannot mess this up
443 final int w = controlImage.getWidth(),
444 h = controlImage.getHeight();
445
446 final int[] controlImageGrid = controlImage.getRGB(0, 0, w, h, null, 0, w);
447 final int[] testImageGrid = testImage.getRGB(0, 0, w, h, null, 0, w);
448
449 for (int i = 0; i < controlImageGrid.length; i++) {
450 assertEquals("Image lost Optical Bounds at i = " + i, controlImageGrid[i], testImageGrid[i]);
451 }
452 }
453
454 @Test
455 public void robust9patchTest() throws IOException {
456 String[] ninePatches = {"ic_notification_overlay.9.png", "status_background.9.png",
457 "search_bg_transparent.9.png", "screenshot_panel.9.png", "recents_lower_gradient.9.png"};
458
459 char slash = File.separatorChar;
460 String location = slash + "res" + slash + "drawable-xxhdpi" + slash;
461
462 for (String ninePatch : ninePatches) {
463 File control = new File((sTestOrigDir + location), ninePatch);
464 File test = new File((sTestNewDir + location), ninePatch);
465
466 BufferedImage controlImage = ImageIO.read(control);
467 BufferedImage testImage = ImageIO.read(test);
468
469 int w = controlImage.getWidth(), h = controlImage.getHeight();
470
471 // Check the entire horizontal line
472 for (int i = 1; i < w; i++) {
473 if (isTransparent(controlImage.getRGB(i, 0))) {
474 assertTrue(isTransparent(testImage.getRGB(i, 0)));
475 } else {
476 assertEquals("Image lost npTc chunk on image " + ninePatch + " at (x, y) (" + i + "," + 0 + ")",
477 controlImage.getRGB(i, 0), testImage.getRGB(i, 0));
478 }
479 }
480
481 // Check the entire vertical line
482 for (int i = 1; i < h; i++) {
483 if (isTransparent(controlImage.getRGB(0, i))) {
484 assertTrue(isTransparent(testImage.getRGB(0, i)));
485 } else {
486 assertEquals("Image lost npTc chunk on image " + ninePatch + " at (x, y) (" + 0 + "," + i + ")",
487 controlImage.getRGB(0, i), testImage.getRGB(0, i));
488 }
489 }
490 }
491 }
492
493 @Test
494 public void confirmZeroByteFileExtensionIsNotStored() throws BrutException {
495 MetaInfo metaInfo = new Androlib().readMetaFile(sTestNewDir);
496
497 for (String item : metaInfo.doNotCompress) {
498 assertNotEquals("jpg", item);
499 }
500 }
501
502 @Test
503 public void confirmZeroByteFileIsStored() throws BrutException {
504 MetaInfo metaInfo = new Androlib().readMetaFile(sTestNewDir);
505 assertTrue(metaInfo.doNotCompress.contains("assets/0byte_file.jpg"));
506 }
507
508 @Test
509 public void drawableXxhdpiTest() throws BrutException, IOException {
510 compareResFolder("drawable-xxhdpi");
511 }
512
513 @Test
514 public void drawableQualifierXxhdpiTest() throws BrutException, IOException {
515 compareResFolder("drawable-xxhdpi-v4");
516 }
517
518 @Test
519 public void drawableXxxhdpiTest() throws BrutException, IOException {
520 compareResFolder("drawable-xxxhdpi");
521 }
522
523 @Test
524 public void resRawTest() throws BrutException, IOException {
525 compareResFolder("raw");
526 }
527
528 @Test
529 public void libsTest() throws BrutException, IOException {
530 compareLibsFolder("libs");
531 compareLibsFolder("lib");
532 }
533
534 @Test
535 public void unknownFolderTest() throws BrutException {
536 compareUnknownFiles();
537 }
538
539 @Test
540 public void fileAssetTest() throws BrutException, IOException {
541 compareAssetsFolder("txt");
542 }
543
544 @Test
545 public void unicodeAssetTest() throws BrutException, IOException {
546 assumeTrue(! OSDetection.isWindows());
547 compareAssetsFolder("unicode-txt");
548 }
549
550 @Test
551 public void multipleDexTest() throws BrutException, IOException {
552 compareBinaryFolder("/smali_classes2", false);
553 compareBinaryFolder("/smali_classes3", false);
554
555 File classes2Dex = new File(sTestOrigDir, "build/apk/classes2.dex");
556 File classes3Dex = new File(sTestOrigDir, "build/apk/classes3.dex");
557
558 assertTrue(classes2Dex.isFile());
559 assertTrue(classes3Dex.isFile());
560 }
561
562 @Test
563 public void singleDexTest() throws BrutException, IOException {
564 compareBinaryFolder("/smali", false);
565
566 File classesDex = new File(sTestOrigDir, "build/apk/classes.dex");
567 assertTrue(classesDex.isFile());
568 }
569
570 @Test
571 public void confirmKotlinFolderPersistsTest() {
572 checkFolderExists("/kotlin");
573 }
574 }