Debian Codegraph @

brut.apktool/apktool-lib/src/test/java/brut/androlib/decode/ForceManifestDecodeNoResourcesTest.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.decode;
18
19 import brut.androlib.ApkDecoder;
20 import brut.androlib.BaseTest;
21 import brut.androlib.TestUtils;
22 import brut.common.BrutException;
23 import brut.directory.ExtFile;
24 import brut.util.OS;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.Arrays;
32
33 import static org.junit.Assert.*;
34
35 public class ForceManifestDecodeNoResourcesTest extends BaseTest {
36
37 private final byte[] xmlHeader = new byte[] {
38 0x3C, // <
39 0x3F, // ?
40 0x78, // x
41 0x6D, // m
42 0x6C, // l
43 0x20, // (empty)
44 };
45
46 @BeforeClass
47 public static void beforeClass() throws Exception {
48 TestUtils.cleanFrameworkFile();
49 sTmpDir = new ExtFile(OS.createTempDirectory());
50 TestUtils.copyResourceDir(ForceManifestDecodeNoResourcesTest.class, "decode/issue1680/", sTmpDir);
51 }
52
53 @AfterClass
54 public static void afterClass() throws BrutException {
55 OS.rmdir(sTmpDir);
56 }
57
58 @Test
59 public void checkIfForceManifestWithNoResourcesWorks() throws BrutException, IOException {
60 String apk = "issue1680.apk";
61 String output = sTmpDir + File.separator + apk + ".out";
62
63 // decode issue1680.apk
64 decodeFile(sTmpDir + File.separator + apk, ApkDecoder.DECODE_RESOURCES_NONE,
65 ApkDecoder.FORCE_DECODE_MANIFEST_FULL, output);
66
67 // lets probe filetype of manifest, we should detect XML
68 File manifestFile = new File(output + File.separator + "AndroidManifest.xml");
69 byte[] magic = TestUtils.readHeaderOfFile(manifestFile, 6);
70 assertArrayEquals(this.xmlHeader, magic);
71
72 // confirm resources.arsc still exists, as its raw
73 File resourcesArsc = new File(output + File.separator + "resources.arsc");
74 assertTrue(resourcesArsc.isFile());
75 }
76
77 @Test
78 public void checkIfForceManifestWorksWithNoChangeToResources() throws BrutException, IOException {
79 String apk = "issue1680.apk";
80 String output = sTmpDir + File.separator + apk + ".out";
81
82 // decode issue1680.apk
83 decodeFile(sTmpDir + File.separator + apk, ApkDecoder.DECODE_RESOURCES_FULL,
84 ApkDecoder.FORCE_DECODE_MANIFEST_FULL, output);
85
86 // lets probe filetype of manifest, we should detect XML
87 File manifestFile = new File(output + File.separator + "AndroidManifest.xml");
88 byte[] magic = TestUtils.readHeaderOfFile(manifestFile, 6);
89 assertArrayEquals(this.xmlHeader, magic);
90
91 // confirm resources.arsc does not exist
92 File resourcesArsc = new File(output + File.separator + "resources.arsc");
93 assertFalse(resourcesArsc.isFile());
94 }
95
96 @Test
97 public void checkForceManifestToFalseWithResourcesEnabledIsIgnored() throws BrutException, IOException {
98 String apk = "issue1680.apk";
99 String output = sTmpDir + File.separator + apk + ".out";
100
101 // decode issue1680.apk
102 decodeFile(sTmpDir + File.separator + apk, ApkDecoder.DECODE_RESOURCES_FULL,
103 ApkDecoder.FORCE_DECODE_MANIFEST_NONE, output);
104
105 // lets probe filetype of manifest, we should detect XML
106 File manifestFile = new File(output + File.separator + "AndroidManifest.xml");
107 byte[] magic = TestUtils.readHeaderOfFile(manifestFile, 6);
108 assertArrayEquals(this.xmlHeader, magic);
109
110 // confirm resources.arsc does not exist
111 File resourcesArsc = new File(output + File.separator + "resources.arsc");
112 assertFalse(resourcesArsc.isFile());
113 }
114
115 @Test
116 public void checkBothManifestAndResourcesSetToNone() throws BrutException, IOException {
117 String apk = "issue1680.apk";
118 String output = sTmpDir + File.separator + apk + ".out";
119
120 // decode issue1680.apk
121 decodeFile(sTmpDir + File.separator + apk, ApkDecoder.DECODE_RESOURCES_NONE,
122 ApkDecoder.FORCE_DECODE_MANIFEST_NONE, output);
123
124 // lets probe filetype of manifest, we should not detect XML
125 File manifestFile = new File(output + File.separator + "AndroidManifest.xml");
126 byte[] magic = TestUtils.readHeaderOfFile(manifestFile, 6);
127 assertFalse(Arrays.equals(this.xmlHeader, magic));
128
129 // confirm resources.arsc exists
130 File resourcesArsc = new File(output + File.separator + "resources.arsc");
131 assertTrue(resourcesArsc.isFile());
132 }
133
134 private void decodeFile(String apk, short decodeResources, short decodeManifest, String output)
135 throws BrutException, IOException {
136 ApkDecoder apkDecoder = new ApkDecoder(new File(apk));
137 apkDecoder.setDecodeResources(decodeResources);
138 apkDecoder.setForceDecodeManifest(decodeManifest);
139 apkDecoder.setForceDelete(true); // delete directory due to multiple tests.
140
141 apkDecoder.setOutDir(new File(output));
142 apkDecoder.decode();
143 }
144 }