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");
}
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");
}
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;
}
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));
}
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.");
}
Aggregations