Skip to content

Commit

Permalink
refactor: moved network tests to aapt2
Browse files Browse the repository at this point in the history
  • Loading branch information
iBotPeaches committed Apr 6, 2022
1 parent 0426623 commit 007c97a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package brut.androlib.aapt1;
package brut.androlib.aapt2;

import brut.androlib.*;
import brut.androlib.meta.MetaInfo;
import brut.androlib.options.BuildOptions;
import brut.common.BrutException;
import brut.directory.ExtFile;
import brut.util.OS;
import brut.util.OSDetection;
import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -33,20 +30,15 @@
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import javax.imageio.ImageIO;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;

public class NetworkConfigTest extends BaseTest {

Expand All @@ -58,11 +50,12 @@ public static void beforeClass() throws Exception {
sTestOrigDir = new ExtFile(sTmpDir, "testapp-orig");
sTestNewDir = new ExtFile(sTmpDir, "testapp-new");
LOGGER.info("Unpacking testapp...");
TestUtils.copyResourceDir(NetworkConfigTest.class, "aapt1/testapp_network_config/", sTestOrigDir);
TestUtils.copyResourceDir(NetworkConfigTest.class, "aapt2/network_config/", sTestOrigDir);

LOGGER.info("Building testapp.apk...");
BuildOptions buildOptions = new BuildOptions();
buildOptions.netSecConf = true;
buildOptions.useAapt2 = true;
File testApk = new File(sTmpDir, "testapp.apk");
new Androlib(buildOptions).build(sTestOrigDir, testApk);

Expand All @@ -83,8 +76,8 @@ public void buildAndDecodeTest() {
}

@Test
public void netSecConfGeneric() throws BrutException, IOException, SAXException {
LOGGER.info("Compraring network security configuration file...");
public void netSecConfGeneric() throws IOException, SAXException {
LOGGER.info("Comparing network security configuration file...");
String expected = TestUtils.replaceNewlines("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
"<network-security-config><base-config><trust-anchors><certificates src=\"system\"/><certificates src=\"us" +
"er\"/></trust-anchors></base-config></network-security-config>");
Expand All @@ -100,8 +93,7 @@ public void netSecConfGeneric() throws BrutException, IOException, SAXException
}

@Test
public void netSecConfInManifest() throws AndrolibException, IOException, ParserConfigurationException, SAXException {

public void netSecConfInManifest() throws IOException, ParserConfigurationException, SAXException {
LOGGER.info("Validating network security config in Manifest...");
Document doc = loadDocument(new File(sTestNewDir + "/AndroidManifest.xml"));
Node application = doc.getElementsByTagName("application").item(0);
Expand All @@ -125,8 +117,6 @@ private static Document loadDocument(File file)
}

DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// Not using the parse(File) method on purpose, so that we can control when
// to close it. Somehow parse(File) does not seem to close the file in all cases.
try (FileInputStream inputStream = new FileInputStream(file)) {
return docBuilder.parse(inputStream);
}
Expand All @@ -136,5 +126,4 @@ private static Document loadDocument(File file)
private static final String ACCESS_EXTERNAL_SCHEMA = "http://javax.xml.XMLConstants/property/accessExternalSchema";
private static final String FEATURE_LOAD_DTD = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
private static final String FEATURE_DISABLE_DOCTYPE_DECL = "http://apache.org/xml/features/disallow-doctype-decl";

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package brut.androlib.aapt1;
package brut.androlib.aapt2;

import brut.androlib.*;
import brut.androlib.options.BuildOptions;
Expand Down Expand Up @@ -53,7 +53,7 @@ public static void beforeClass() throws Exception {
sTestOrigDir = new ExtFile(sTmpDir, "testapp-orig");
sTestNewDir = new ExtFile(sTmpDir, "testapp-new");
LOGGER.info("Unpacking testapp...");
TestUtils.copyResourceDir(NoNetworkConfigTest.class, "aapt1/testapp/", sTestOrigDir);
TestUtils.copyResourceDir(NoNetworkConfigTest.class, "aapt2/testapp/", sTestOrigDir);

LOGGER.info("Building testapp.apk...");
BuildOptions buildOptions = new BuildOptions();
Expand All @@ -78,8 +78,8 @@ public void buildAndDecodeTest() {
}

@Test
public void netSecConfGeneric() throws BrutException, IOException, SAXException {
LOGGER.info("Compraring network security configuration file...");
public void netSecConfGeneric() throws IOException, SAXException {
LOGGER.info("Comparing network security configuration file...");
String expected = TestUtils.replaceNewlines("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
"<network-security-config><base-config><trust-anchors><certificates src=\"system\"/><certificates src=\"us" +
"er\"/></trust-anchors></base-config></network-security-config>");
Expand All @@ -95,8 +95,7 @@ public void netSecConfGeneric() throws BrutException, IOException, SAXException
}

@Test
public void netSecConfInManifest() throws AndrolibException, IOException, ParserConfigurationException, SAXException {

public void netSecConfInManifest() throws IOException, ParserConfigurationException, SAXException {
LOGGER.info("Validating network security config in Manifest...");
Document doc = loadDocument(new File(sTestNewDir + "/AndroidManifest.xml"));
Node application = doc.getElementsByTagName("application").item(0);
Expand Down Expand Up @@ -131,5 +130,4 @@ private static Document loadDocument(File file)
private static final String ACCESS_EXTERNAL_SCHEMA = "http://javax.xml.XMLConstants/property/accessExternalSchema";
private static final String FEATURE_LOAD_DTD = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
private static final String FEATURE_DISABLE_DOCTYPE_DECL = "http://apache.org/xml/features/disallow-doctype-decl";

}

0 comments on commit 007c97a

Please sign in to comment.