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