Search in sources :

Example 31 with ZipBuilder

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

the class BuildBundleCommandTest method runsResourceTableValidator_resourceTableReferencingNonExistingFile_throws.

@Test
public void runsResourceTableValidator_resourceTableReferencingNonExistingFile_throws() throws Exception {
    ResourceTable resourceTable = new ResourceTableBuilder().addPackage(PKG_NAME).addDrawableResource("icon", "res/drawable/icon.png").build();
    Path module = new ZipBuilder().addFileWithProtoContent(ZipPath.create("resources.pb"), resourceTable).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("contains references to non-existing files");
}
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) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) ResourceTable(com.android.aapt.Resources.ResourceTable) Test(org.junit.Test)

Example 32 with ZipBuilder

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

the class BuildSdkApksCommandTest method bundleMissingFiles_throws.

@Test
public void bundleMissingFiles_throws() throws Exception {
    ZipBuilder zipBuilder = new ZipBuilder();
    zipBuilder.writeTo(sdkBundlePath);
    BuildSdkApksCommand command = BuildSdkApksCommand.fromFlags(getDefaultFlagsWithAdditionalFlags());
    Exception e = assertThrows(InvalidBundleException.class, command::execute);
    assertThat(e).hasMessageThat().contains("missing required file");
}
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 33 with ZipBuilder

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

the class BuildSdkApksCommandTest 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("base/assets.pb"), ASSETS_CONFIG.toByteArray()).addFileWithContent(ZipPath.create("base/native.pb"), NATIVE_CONFIG.toByteArray()).addFileWithContent(ZipPath.create("base/resources.pb"), RESOURCE_TABLE.toByteArray()).addFileWithContent(ZipPath.create("base/lib/x86/libfoo.so"), 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 34 with ZipBuilder

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

the class CheckTransparencyCommandTest method apkMode_verificationFailed_apkSigningKeyCertificateMismatch.

@Test
public void apkMode_verificationFailed_apkSigningKeyCertificateMismatch() throws Exception {
    Path apkPath = tmpDir.resolve("universal.apk");
    Path zipOfApksPath = tmpDir.resolve("apks.zip");
    String dexFileName = "classes.dex";
    byte[] dexFileContents = new byte[] { 1, 2, 3 };
    String serializedJws = createJwsToken(CodeTransparency.newBuilder().setVersion(CodeTransparencyVersion.getCurrentVersion()).addCodeRelatedFile(CodeRelatedFile.newBuilder().setType(CodeRelatedFile.Type.DEX).setPath("base/dex/" + dexFileName).setSha256(ByteSource.wrap(dexFileContents).hash(Hashing.sha256()).toString()).build()).build(), transparencyKeyCertificate, transparencyPrivateKey);
    ModuleSplit baseModuleSplit = ModuleSplit.builder().setModuleName(BundleModuleName.create("base")).setAndroidManifest(AndroidManifest.create(androidManifest("com.app"))).setApkTargeting(ApkTargeting.getDefaultInstance()).setVariantTargeting(VariantTargeting.getDefaultInstance()).setMasterSplit(true).addEntry(ModuleEntry.builder().setPath(ZipPath.create("").resolve(dexFileName)).setContent(ByteSource.wrap(dexFileContents)).build()).addEntry(ModuleEntry.builder().setPath(ZipPath.create("META-INF").resolve(BundleMetadata.TRANSPARENCY_SIGNED_FILE_NAME)).setContent(CharSource.wrap(serializedJws).asByteSource(Charset.defaultCharset())).build()).build();
    apkSerializer.serialize(apkPath, baseModuleSplit);
    ZipBuilder zipBuilder = new ZipBuilder().addFileWithContent(ZipPath.create("universal.apk"), ByteString.readFrom(Files.newInputStream(apkPath)).toByteArray());
    zipBuilder.writeTo(zipOfApksPath);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    X509Certificate providedApkSigningKeyCert = CertificateFactory.buildSelfSignedCertificate(kpg.genKeyPair(), "CN=BadApkSigningCert");
    CheckTransparencyCommand.builder().setMode(Mode.APK).setApkZipPath(zipOfApksPath).setApkSigningKeyCertificate(providedApkSigningKeyCert).build().checkTransparency(new PrintStream(outputStream));
    String output = new String(outputStream.toByteArray(), UTF_8);
    assertThat(output).contains("APK signature verification failed because the provided public key certificate does" + " not match the APK signature." + "\nSHA-256 fingerprint of the certificate that was used to sign the APKs: " + CodeTransparencyCryptoUtils.getCertificateFingerprint(apkSigningKeyCertificate) + "\nSHA-256 fingerprint of the certificate that was provided: " + CodeTransparencyCryptoUtils.getCertificateFingerprint(providedApkSigningKeyCert));
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) PrintStream(java.io.PrintStream) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) ByteString(com.google.protobuf.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Example 35 with ZipBuilder

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

the class CheckTransparencyCommandTest method apkMode_transparencyVerified_apkSigningKeyCertificateProvidedByUser.

@Test
public void apkMode_transparencyVerified_apkSigningKeyCertificateProvidedByUser() throws Exception {
    Path apkPath = tmpDir.resolve("universal.apk");
    Path zipOfApksPath = tmpDir.resolve("apks.zip");
    String dexFileName = "classes.dex";
    byte[] dexFileContents = new byte[] { 1, 2, 3 };
    String serializedJws = createJwsToken(CodeTransparency.newBuilder().setVersion(CodeTransparencyVersion.getCurrentVersion()).addCodeRelatedFile(CodeRelatedFile.newBuilder().setType(CodeRelatedFile.Type.DEX).setPath("base/dex/" + dexFileName).setSha256(ByteSource.wrap(dexFileContents).hash(Hashing.sha256()).toString()).build()).build(), transparencyKeyCertificate, transparencyPrivateKey);
    ModuleSplit baseModuleSplit = ModuleSplit.builder().setModuleName(BundleModuleName.create("base")).setAndroidManifest(AndroidManifest.create(androidManifest("com.app"))).setApkTargeting(ApkTargeting.getDefaultInstance()).setVariantTargeting(VariantTargeting.getDefaultInstance()).setMasterSplit(true).addEntry(ModuleEntry.builder().setPath(ZipPath.create("").resolve(dexFileName)).setContent(ByteSource.wrap(dexFileContents)).build()).addEntry(ModuleEntry.builder().setPath(ZipPath.create("META-INF").resolve(BundleMetadata.TRANSPARENCY_SIGNED_FILE_NAME)).setContent(CharSource.wrap(serializedJws).asByteSource(Charset.defaultCharset())).build()).build();
    apkSerializer.serialize(apkPath, baseModuleSplit);
    ZipBuilder zipBuilder = new ZipBuilder().addFileWithContent(ZipPath.create("universal.apk"), ByteString.readFrom(Files.newInputStream(apkPath)).toByteArray());
    zipBuilder.writeTo(zipOfApksPath);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CheckTransparencyCommand.builder().setMode(Mode.APK).setApkZipPath(zipOfApksPath).setApkSigningKeyCertificate(apkSigningKeyCertificate).build().checkTransparency(new PrintStream(outputStream));
    String output = new String(outputStream.toByteArray(), UTF_8);
    assertThat(output).contains("APK signature verified for the provided apk signing key certificate.");
    assertThat(output).contains("Code transparency signature is valid. SHA-256 fingerprint of the code transparency key" + " certificate (must be compared with the developer's public key manually): " + CodeTransparencyCryptoUtils.getCertificateFingerprint((JsonWebSignature) JsonWebSignature.fromCompactSerialization(serializedJws)));
    assertThat(output).contains("Code transparency verified: code related file contents match the code transparency" + " file.");
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) PrintStream(java.io.PrintStream) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) ByteString(com.google.protobuf.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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