use of com.android.tools.build.bundletool.model.AppBundle in project bundletool by google.
the class BuildApksDeviceSpecTest method deviceSpec_universalApk_throws.
@Test
public void deviceSpec_universalApk_throws() throws Exception {
DeviceSpec deviceSpec = deviceWithSdk(21);
AppBundle appBundle = new AppBundleBuilder().addModule("base", module -> module.setManifest(androidManifest("com.app"))).build();
bundleSerializer.writeToDisk(appBundle, bundlePath);
BuildApksCommand.Builder command = BuildApksCommand.builder().setBundlePath(bundlePath).setOutputFile(outputFilePath).setDeviceSpec(deviceSpec).setApkBuildMode(UNIVERSAL);
Throwable exception = assertThrows(InvalidCommandException.class, command::build);
assertThat(exception).hasMessageThat().contains("Optimizing for device spec is not possible when running with 'universal' mode flag.");
}
use of com.android.tools.build.bundletool.model.AppBundle in project bundletool by google.
the class BuildApksDeviceSpecTest method deviceSpec_systemApkMode_partialDeviceSpecMissingDensity_throws.
@Test
@Theory
public void deviceSpec_systemApkMode_partialDeviceSpecMissingDensity_throws() throws Exception {
DeviceSpec deviceSpec = mergeSpecs(abis("arm64-v8a"));
AppBundle appBundle = new AppBundleBuilder().addModule("base", module -> module.setManifest(androidManifest("com.app"))).build();
bundleSerializer.writeToDisk(appBundle, bundlePath);
BuildApksCommand.Builder command = BuildApksCommand.builder().setBundlePath(bundlePath).setOutputFile(outputFilePath).setDeviceSpec(deviceSpec).setApkBuildMode(SYSTEM);
Throwable exception = assertThrows(InvalidCommandException.class, command::build);
assertThat(exception).hasMessageThat().contains("Device spec must have screen density and ABIs set when running with 'system' mode" + " flag.");
}
use of com.android.tools.build.bundletool.model.AppBundle in project bundletool by google.
the class BuildApksDeviceSpecTest method deviceSpec_systemApkMode_partialDeviceSpecMissingAbi_throws.
@Test
@Theory
public void deviceSpec_systemApkMode_partialDeviceSpecMissingAbi_throws() throws Exception {
DeviceSpec deviceSpec = mergeSpecs(density(DensityAlias.MDPI));
AppBundle appBundle = new AppBundleBuilder().addModule("base", module -> module.setManifest(androidManifest("com.app"))).build();
bundleSerializer.writeToDisk(appBundle, bundlePath);
BuildApksCommand.Builder command = BuildApksCommand.builder().setBundlePath(bundlePath).setOutputFile(outputFilePath).setDeviceSpec(deviceSpec).setApkBuildMode(SYSTEM);
Throwable exception = assertThrows(InvalidCommandException.class, command::build);
assertThat(exception).hasMessageThat().contains("Device spec must have screen density and ABIs set when running with 'system' mode" + " flag.");
}
use of com.android.tools.build.bundletool.model.AppBundle in project bundletool by google.
the class AddTransparencyCommandTest method execute_defaultMode_success.
@Test
public void execute_defaultMode_success() throws Exception {
createBundle(bundlePath);
AddTransparencyCommand addTransparencyCommand = AddTransparencyCommand.builder().setMode(Mode.DEFAULT).setBundlePath(bundlePath).setOutputPath(outputBundlePath).setSignerConfig(signerConfig).build();
addTransparencyCommand.execute();
AppBundle outputBundle = AppBundle.buildFromZip(new ZipFile(outputBundlePath.toFile()));
Optional<ByteSource> signedTransparencyFile = outputBundle.getBundleMetadata().getFileAsByteSource(BUNDLETOOL_NAMESPACE, BundleMetadata.TRANSPARENCY_SIGNED_FILE_NAME);
assertThat(signedTransparencyFile).isPresent();
JsonWebSignature jws = (JsonWebSignature) JsonWebSignature.fromCompactSerialization(signedTransparencyFile.get().asCharSource(Charset.defaultCharset()).read());
assertThat(jws.getAlgorithmHeaderValue()).isEqualTo(RSA_USING_SHA256);
assertThat(jws.getCertificateChainHeaderValue()).isEqualTo(signerConfig.getCertificates());
// jws.getPayload method will do signature verification using the public key set below.
jws.setKey(signerConfig.getCertificates().get(0).getPublicKey());
CodeTransparency transparencyProto = getTransparencyProto(jws.getPayload());
assertThat(transparencyProto).isEqualTo(expectedTransparencyProto());
}
use of com.android.tools.build.bundletool.model.AppBundle in project bundletool by google.
the class AddTransparencyCommandTest method execute_injectSignature.
@Test
public void execute_injectSignature() throws Exception {
// create bundle.
createBundle(bundlePath);
// add transparency file in default mode.
Path tmpOutputBundlePath = tmpDir.resolve("tmp_output_bundle.aab");
AddTransparencyCommand.builder().setMode(Mode.DEFAULT).setBundlePath(bundlePath).setOutputPath(tmpOutputBundlePath).setSignerConfig(signerConfig).build().execute();
// get the correct transparency signature bytes.
AppBundle tmpOutputBundle = AppBundle.buildFromZip(new ZipFile(tmpOutputBundlePath.toFile()));
ByteSource signedTransparencyFile = tmpOutputBundle.getBundleMetadata().getFileAsByteSource(BUNDLETOOL_NAMESPACE, BundleMetadata.TRANSPARENCY_SIGNED_FILE_NAME).get();
String jws = signedTransparencyFile.asCharSource(Charset.defaultCharset()).read();
String signature = ImmutableList.copyOf(Splitter.on(".").split(jws)).get(2);
byte[] signatureBytes = BaseEncoding.base64Url().decode(signature);
Files.write(transparencySignatureFilePath, signatureBytes);
// inject signature into the original bundle
AddTransparencyCommand.builder().setMode(Mode.INJECT_SIGNATURE).setBundlePath(bundlePath).setOutputPath(outputBundlePath).setTransparencyKeyCertificate(signerConfig.getCertificates().get(0)).setTransparencySignaturePath(transparencySignatureFilePath).build().execute();
// verify that the output bundle contains signed code transparency metadata.
AppBundle outputBundle = AppBundle.buildFromZip(new ZipFile(outputBundlePath.toFile()));
Optional<ByteSource> finalSignedTransparencyFile = outputBundle.getBundleMetadata().getFileAsByteSource(BUNDLETOOL_NAMESPACE, BundleMetadata.TRANSPARENCY_SIGNED_FILE_NAME);
assertThat(finalSignedTransparencyFile).isPresent();
JsonWebSignature finalJws = (JsonWebSignature) JsonWebSignature.fromCompactSerialization(finalSignedTransparencyFile.get().asCharSource(Charset.defaultCharset()).read());
assertThat(finalJws.getAlgorithmHeaderValue()).isEqualTo(RSA_USING_SHA256);
assertThat(finalJws.getCertificateChainHeaderValue()).isEqualTo(signerConfig.getCertificates());
// jws.getPayload method will do signature verification using the public key set below.
finalJws.setKey(signerConfig.getCertificates().get(0).getPublicKey());
CodeTransparency transparencyProto = getTransparencyProto(finalJws.getPayload());
assertThat(transparencyProto).isEqualTo(expectedTransparencyProto());
}
Aggregations