Search in sources :

Example 11 with ZipBuilder

use of com.android.tools.build.bundletool.io.ZipBuilder in project bundletool by google.

the class BuildBundleCommandTest method runsBundleFilesValidator_rogueFileInModuleRoot_throws.

// Tests bellow validate that specific validators are run for the input modules. For the lack
// of better options, for each validator there is a single test that is most representative of
// the validator.
@Test
public void runsBundleFilesValidator_rogueFileInModuleRoot_throws() throws Exception {
    Path module = new ZipBuilder().addFileWithContent(ZipPath.create("rogue.txt"), "".getBytes(UTF_8)).addFileWithProtoContent(ZipPath.create("manifest/AndroidManifest.xml"), androidManifest(PKG_NAME)).writeTo(tmpDir.resolve("base.zip"));
    BuildBundleCommand command = BuildBundleCommand.builder().setOutputPath(bundlePath).setModulesPaths(ImmutableList.of(module)).build();
    InvalidBundleException exception = assertThrows(InvalidBundleException.class, command::execute);
    assertThat(exception).hasMessageThat().contains("Module files can be only in pre-defined directories");
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) Test(org.junit.Test)

Example 12 with ZipBuilder

use of com.android.tools.build.bundletool.io.ZipBuilder in project bundletool by google.

the class BuildSdkApksCommandTest method overwriteNotSetOutputFileAlreadyExists_throws.

@Test
public void overwriteNotSetOutputFileAlreadyExists_throws() throws Exception {
    createBasicZipBuilderWithManifest().writeTo(sdkBundlePath);
    new ZipBuilder().addFileWithContent(ZipPath.create("BundleConfig.pb"), BUNDLE_CONFIG.toByteArray()).writeTo(outputFilePath);
    BuildSdkApksCommand command = BuildSdkApksCommand.fromFlags(getDefaultFlagsWithAdditionalFlags());
    Exception e = assertThrows(IllegalArgumentException.class, command::execute);
    assertThat(e).hasMessageThat().contains("already exists");
}
Also used : ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) RequiredFlagNotSetException(com.android.tools.build.bundletool.flags.Flag.RequiredFlagNotSetException) FlagParseException(com.android.tools.build.bundletool.flags.FlagParser.FlagParseException) TestUtils.expectMissingRequiredBuilderPropertyException(com.android.tools.build.bundletool.testing.TestUtils.expectMissingRequiredBuilderPropertyException) UnknownFlagsException(com.android.tools.build.bundletool.flags.ParsedFlags.UnknownFlagsException) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) TestUtils.expectMissingRequiredFlagException(com.android.tools.build.bundletool.testing.TestUtils.expectMissingRequiredFlagException) InvalidCommandException(com.android.tools.build.bundletool.model.exceptions.InvalidCommandException) Test(org.junit.Test)

Example 13 with ZipBuilder

use of com.android.tools.build.bundletool.io.ZipBuilder in project bundletool by google.

the class SdkBundleTest method createBasicZipBuilderWithManifest.

private ZipBuilder createBasicZipBuilderWithManifest() {
    ZipBuilder zipBuilder = new ZipBuilder();
    zipBuilder.addFileWithContent(ZipPath.create("BundleConfig.pb"), BUNDLE_CONFIG.toByteArray()).addFileWithProtoContent(ZipPath.create("base/manifest/AndroidManifest.xml"), MANIFEST).addFileWithContent(ZipPath.create("base/dex/classes.dex"), DUMMY_CONTENT).addFileWithContent(ZipPath.create("BUNDLE-METADATA/some.namespace/metadata1"), new byte[] { 0x01 });
    return zipBuilder;
}
Also used : ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder)

Example 14 with ZipBuilder

use of com.android.tools.build.bundletool.io.ZipBuilder in project bundletool by google.

the class BundleZipValidatorTest method validateBundleZipEntry_directory_throws.

@Test
public void validateBundleZipEntry_directory_throws() throws Exception {
    Path bundlePath = new ZipBuilder().addDirectory(ZipPath.create("directory")).writeTo(tempFolder.resolve("bundle.aab"));
    try (ZipFile bundleZip = new ZipFile(bundlePath.toFile())) {
        ArrayList<? extends ZipEntry> entries = Collections.list(bundleZip.entries());
        // Sanity check.
        assertThat(entries).hasSize(1);
        InvalidBundleException exception = assertThrows(InvalidBundleException.class, () -> new BundleZipValidator().validateBundleZipEntry(bundleZip, entries.get(0)));
        assertThat(exception).hasMessageThat().contains("zip file contains directory zip entry");
    }
}
Also used : ZipPath(com.android.tools.build.bundletool.model.ZipPath) Path(java.nio.file.Path) ZipFile(java.util.zip.ZipFile) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) Test(org.junit.Test)

Example 15 with ZipBuilder

use of com.android.tools.build.bundletool.io.ZipBuilder in project bundletool by google.

the class ValidatorRunnerTest method validateSdkBundleZipFile_invokesRightSubValidatorMethods.

@Test
public void validateSdkBundleZipFile_invokesRightSubValidatorMethods() throws Exception {
    Path bundlePath = new ZipBuilder().addDirectory(ZipPath.create("directory")).addFileWithContent(ZipPath.create("file.txt"), DUMMY_CONTENT).writeTo(tempFolder.resolve("bundle.asb"));
    try (ZipFile bundleZip = new ZipFile(bundlePath.toFile())) {
        new ValidatorRunner(ImmutableList.of(validator)).validateBundleZipFile(bundleZip);
        ArgumentCaptor<ZipEntry> zipEntryArgs = ArgumentCaptor.forClass(ZipEntry.class);
        verify(validator).validateBundleZipFile(eq(bundleZip));
        verify(validator, atLeastOnce()).validateBundleZipEntry(eq(bundleZip), zipEntryArgs.capture());
        verifyNoMoreInteractions(validator);
        assertThat(zipEntryArgs.getAllValues().stream().map(ZipEntry::getName)).containsExactly("directory/", "file.txt");
    }
}
Also used : ZipPath(com.android.tools.build.bundletool.model.ZipPath) Path(java.nio.file.Path) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) Test(org.junit.Test)

Aggregations

ZipBuilder (com.android.tools.build.bundletool.io.ZipBuilder)44 Test (org.junit.Test)39 ZipPath (com.android.tools.build.bundletool.model.ZipPath)37 Path (java.nio.file.Path)37 ZipFile (java.util.zip.ZipFile)19 InvalidBundleException (com.android.tools.build.bundletool.model.exceptions.InvalidBundleException)11 BuildApksResult (com.android.bundle.Commands.BuildApksResult)5 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)5 ByteString (com.google.protobuf.ByteString)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 PrintStream (java.io.PrintStream)5 ZipEntry (java.util.zip.ZipEntry)5 XmlNode (com.android.aapt.Resources.XmlNode)4 Variant (com.android.bundle.Commands.Variant)4 ApksArchiveHelpers.createVariant (com.android.tools.build.bundletool.testing.ApksArchiveHelpers.createVariant)4 FlagParseException (com.android.tools.build.bundletool.flags.FlagParser.FlagParseException)3 ConfigurationSizes (com.android.tools.build.bundletool.model.ConfigurationSizes)3 InvalidCommandException (com.android.tools.build.bundletool.model.exceptions.InvalidCommandException)3 ApksArchiveHelpers.standaloneVariant (com.android.tools.build.bundletool.testing.ApksArchiveHelpers.standaloneVariant)3 ResourceTableBuilder (com.android.tools.build.bundletool.testing.ResourceTableBuilder)3