Debian Codegraph @

brut.apktool/apktool-lib/src/test/java/brut/androlib/aapt2/NetworkConfigTest.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.aapt2;
18
19 import brut.androlib.*;
20 import brut.androlib.options.BuildOptions;
21 import brut.common.BrutException;
22 import brut.directory.ExtFile;
23 import brut.util.OS;
24 import org.custommonkey.xmlunit.XMLUnit;
25 import org.junit.AfterClass;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.NamedNodeMap;
30 import org.w3c.dom.Node;
31 import org.xml.sax.SAXException;
32
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.ParserConfigurationException;
36 import java.io.*;
37 import java.nio.file.Files;
38 import java.nio.file.Paths;
39
40 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
41 import static org.junit.Assert.*;
42
43 public class NetworkConfigTest extends BaseTest {
44
45 @BeforeClass
46 public static void beforeClass() throws Exception {
47 TestUtils.cleanFrameworkFile();
48
49 sTmpDir = new ExtFile(OS.createTempDirectory());
50 sTestOrigDir = new ExtFile(sTmpDir, "testapp-orig");
51 sTestNewDir = new ExtFile(sTmpDir, "testapp-new");
52 LOGGER.info("Unpacking testapp...");
53 TestUtils.copyResourceDir(NetworkConfigTest.class, "aapt2/network_config/", sTestOrigDir);
54
55 LOGGER.info("Building testapp.apk...");
56 BuildOptions buildOptions = new BuildOptions();
57 buildOptions.netSecConf = true;
58 buildOptions.useAapt2 = true;
59 File testApk = new File(sTmpDir, "testapp.apk");
60 new Androlib(buildOptions).build(sTestOrigDir, testApk);
61
62 LOGGER.info("Decoding testapp.apk...");
63 ApkDecoder apkDecoder = new ApkDecoder(testApk);
64 apkDecoder.setOutDir(sTestNewDir);
65 apkDecoder.decode();
66 }
67
68 @AfterClass
69 public static void afterClass() throws BrutException {
70 OS.rmdir(sTmpDir);
71 }
72
73 @Test
74 public void buildAndDecodeTest() {
75 assertTrue(sTestNewDir.isDirectory());
76 }
77
78 @Test
79 public void netSecConfGeneric() throws IOException, SAXException {
80 LOGGER.info("Comparing network security configuration file...");
81 String expected = TestUtils.replaceNewlines("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
82 "<network-security-config><base-config><trust-anchors><certificates src=\"system\"/><certificates src=\"us" +
83 "er\"/></trust-anchors></base-config></network-security-config>");
84
85 byte[] encoded = Files.readAllBytes(Paths.get(String.valueOf(sTestNewDir), "res/xml/network_security_config.xml"));
86 String obtained = TestUtils.replaceNewlines(new String(encoded));
87
88 XMLUnit.setIgnoreWhitespace(true);
89 XMLUnit.setIgnoreAttributeOrder(true);
90 XMLUnit.setCompareUnmatched(false);
91
92 assertXMLEqual(expected, obtained);
93 }
94
95 @Test
96 public void netSecConfInManifest() throws IOException, ParserConfigurationException, SAXException {
97 LOGGER.info("Validating network security config in Manifest...");
98 Document doc = loadDocument(new File(sTestNewDir + "/AndroidManifest.xml"));
99 Node application = doc.getElementsByTagName("application").item(0);
100 NamedNodeMap attr = application.getAttributes();
101 Node debugAttr = attr.getNamedItem("android:networkSecurityConfig");
102 assertEquals("@xml/network_security_config", debugAttr.getNodeValue());
103 }
104
105 private static Document loadDocument(File file)
106 throws IOException, SAXException, ParserConfigurationException {
107
108 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
109 docFactory.setFeature(FEATURE_DISABLE_DOCTYPE_DECL, true);
110 docFactory.setFeature(FEATURE_LOAD_DTD, false);
111
112 try {
113 docFactory.setAttribute(ACCESS_EXTERNAL_DTD, " ");
114 docFactory.setAttribute(ACCESS_EXTERNAL_SCHEMA, " ");
115 } catch (IllegalArgumentException ex) {
116 LOGGER.warning("JAXP 1.5 Support is required to validate XML");
117 }
118
119 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
120 try (FileInputStream inputStream = new FileInputStream(file)) {
121 return docBuilder.parse(inputStream);
122 }
123 }
124
125 private static final String ACCESS_EXTERNAL_DTD = "http://javax.xml.XMLConstants/property/accessExternalDTD";
126 private static final String ACCESS_EXTERNAL_SCHEMA = "http://javax.xml.XMLConstants/property/accessExternalSchema";
127 private static final String FEATURE_LOAD_DTD = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
128 private static final String FEATURE_DISABLE_DOCTYPE_DECL = "http://apache.org/xml/features/disallow-doctype-decl";
129 }