Debian Codegraph @

brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt1/ProviderAttributeTest.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.directory.ExtFile;
24 import brut.common.BrutException;
25 import brut.util.OS;
26 import org.custommonkey.xmlunit.XMLUnit;
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.xml.sax.SAXException;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36
37 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
38 import static org.junit.Assert.assertTrue;
39
40 public class ProviderAttributeTest extends BaseTest {
41
42 @BeforeClass
43 public static void beforeClass() throws BrutException {
44 TestUtils.cleanFrameworkFile();
45 sTmpDir = new ExtFile(OS.createTempDirectory());
46 TestUtils.copyResourceDir(ProviderAttributeTest.class, "aapt1/issue636/", sTmpDir);
47 }
48
49 @AfterClass
50 public static void afterClass() throws BrutException {
51 OS.rmdir(sTmpDir);
52 }
53
54 @Test
55 public void isProviderStringReplacementWorking() throws BrutException, IOException, SAXException {
56 String apk = "issue636.apk";
57
58 // decode issue636.apk
59 ApkDecoder apkDecoder = new ApkDecoder(new File(sTmpDir + File.separator + apk));
60 apkDecoder.setOutDir(new File(sTmpDir + File.separator + apk + ".out"));
61 apkDecoder.decode();
62
63 // build issue636
64 ExtFile testApk = new ExtFile(sTmpDir, apk + ".out");
65 new Androlib().build(testApk, null);
66 String newApk = apk + ".out" + File.separator + "dist" + File.separator + apk;
67 assertTrue(fileExists(newApk));
68
69 // decode issues636 again
70 apkDecoder = new ApkDecoder(new File(sTmpDir + File.separator + newApk));
71 apkDecoder.setOutDir(new File(sTmpDir + File.separator + apk + ".out.two"));
72 apkDecoder.decode();
73
74 String expected = TestUtils.replaceNewlines("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n" +
75 "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\" android:compileSdkVersion=\"23\" android:compileSdkVersionCodename=\"6.0-2438415\" package=\"com.ibotpeaches.issue636\" platformBuildVersionCode=\"22\" platformBuildVersionName=\"5.1-1756733\">\n" +
76 " <application android:allowBackup=\"true\" android:debuggable=\"true\" android:icon=\"@mipmap/ic_launcher\" android:label=\"@string/app_name\" android:theme=\"@style/AppTheme\">\n" +
77 " <provider android:authorities=\"com.ibotpeaches.issue636.Provider\" android:exported=\"false\" android:grantUriPermissions=\"true\" android:label=\"@string/app_name\" android:multiprocess=\"false\" android:name=\"com.ibotpeaches.issue636.Provider\"/>\n" +
78 " <provider android:authorities=\"com.ibotpeaches.issue636.ProviderTwo\" android:exported=\"false\" android:grantUriPermissions=\"true\" android:label=\"@string/app_name\" android:multiprocess=\"false\" android:name=\"com.ibotpeaches.issue636.ProviderTwo\"/>\n" +
79 " </application>\n" +
80 "</manifest>");
81
82
83 byte[] encoded = Files.readAllBytes(Paths.get(sTmpDir + File.separator + apk + ".out.two" + File.separator + "AndroidManifest.xml"));
84 String obtained = TestUtils.replaceNewlines(new String(encoded));
85
86 XMLUnit.setIgnoreWhitespace(true);
87 XMLUnit.setIgnoreAttributeOrder(true);
88 XMLUnit.setCompareUnmatched(false);
89
90 assertXMLEqual(expected, obtained);
91 }
92
93 private boolean fileExists(String filepath) {
94 return Files.exists(Paths.get(sTmpDir.getAbsolutePath() + File.separator + filepath));
95 }
96 }