Search in sources :

Example 96 with AppBundleBuilder

use of com.android.tools.build.bundletool.testing.AppBundleBuilder in project bundletool by google.

the class BuildApksValidationTest method splits_textureDefaultSuffixNotPresentInAssetPacks.

@Test
public void splits_textureDefaultSuffixNotPresentInAssetPacks() throws Exception {
    AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.setManifest(androidManifest("com.test.app")).setResourceTable(resourceTableWithTestLabel("Test feature"))).addModule("feature_tcf_assets", builder -> builder.addFile("assets/textures/untargeted_texture.dat").addFile("assets/textures#tcf_etc1/etc1_texture.dat").setAssetsConfig(assets(targetedAssetsDirectory("assets/textures", assetsDirectoryTargeting(alternativeTextureCompressionTargeting(ETC1_RGB8))), targetedAssetsDirectory("assets/textures#tcf_etc1", assetsDirectoryTargeting(textureCompressionTargeting(ETC1_RGB8))))).setManifest(androidManifestForFeature("com.test.app", withTitle("@string/test_label", TEST_LABEL_RESOURCE_ID)))).setBundleConfig(BundleConfigBuilder.create().addSplitDimension(Value.TEXTURE_COMPRESSION_FORMAT, /* negate= */
    false, /* stripSuffix= */
    false, /* defaultSuffix= */
    "astc").build()).build();
    new AppBundleSerializer().writeToDisk(appBundle, bundlePath);
    BuildApksCommand command = BuildApksCommand.builder().setBundlePath(bundlePath).setOutputFile(outputFilePath).build();
    // ASTC format is not present in one module with TCF targeting, so we'll
    // consider the bundle invalid as the configuration specified that this is the default
    // format to use for generating standalone or universal APKs.
    InvalidBundleException exception = assertThrows(InvalidBundleException.class, command::execute);
    assertThat(exception).hasMessageThat().contains("the texture folders for format 'ASTC' will be used, but module 'feature_tcf_assets'" + " has no such folders");
}
Also used : ManifestProtoUtils.withMaxSdkVersion(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withMaxSdkVersion) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) TEST_LABEL_RESOURCE_ID(com.android.tools.build.bundletool.testing.ResourcesTableFactory.TEST_LABEL_RESOURCE_ID) Value(com.android.bundle.Config.SplitDimension.Value) TargetingUtils.alternativeTextureCompressionTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.alternativeTextureCompressionTargeting) ManifestProtoUtils.androidManifest(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest) TargetingUtils.textureCompressionTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.textureCompressionTargeting) ATC(com.android.bundle.Targeting.TextureCompressionFormat.TextureCompressionFormatAlias.ATC) BundleConfigBuilder(com.android.tools.build.bundletool.testing.BundleConfigBuilder) RunWith(org.junit.runner.RunWith) ImmutableList(com.google.common.collect.ImmutableList) TargetingUtils.targetedAssetsDirectory(com.android.tools.build.bundletool.testing.TargetingUtils.targetedAssetsDirectory) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) ResourcesTableFactory.resourceTableWithTestLabel(com.android.tools.build.bundletool.testing.ResourcesTableFactory.resourceTableWithTestLabel) Path(java.nio.file.Path) Before(org.junit.Before) ManifestProtoUtils.withInstant(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withInstant) ManifestProtoUtils.withTitle(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withTitle) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) ETC1_RGB8(com.android.bundle.Targeting.TextureCompressionFormat.TextureCompressionFormatAlias.ETC1_RGB8) TargetingUtils.assets(com.android.tools.build.bundletool.testing.TargetingUtils.assets) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) Rule(org.junit.Rule) SubValidator(com.android.tools.build.bundletool.validation.SubValidator) ManifestProtoUtils.androidManifestForFeature(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifestForFeature) TargetingUtils.assetsDirectoryTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.assetsDirectoryTargeting) AppBundle(com.android.tools.build.bundletool.model.AppBundle) TemporaryFolder(org.junit.rules.TemporaryFolder) AppBundle(com.android.tools.build.bundletool.model.AppBundle) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) Test(org.junit.Test)

Example 97 with AppBundleBuilder

use of com.android.tools.build.bundletool.testing.AppBundleBuilder in project bundletool by google.

the class BuildApksValidationTest method buildApksCommand_appTargetingPreL_failsGeneratingInstant.

@Test
public void buildApksCommand_appTargetingPreL_failsGeneratingInstant() throws Exception {
    AppBundle appBundle = new AppBundleBuilder().addModule("base", builder -> builder.setManifest(androidManifest("com.app", withInstant(true), withMaxSdkVersion(20)))).build();
    new AppBundleSerializer().writeToDisk(appBundle, bundlePath);
    BuildApksCommand command = BuildApksCommand.builder().setBundlePath(bundlePath).setOutputFile(outputFilePath).build();
    Throwable exception = assertThrows(InvalidBundleException.class, command::execute);
    assertThat(exception).hasMessageThat().contains("maxSdkVersion (20) is less than minimum sdk allowed for instant apps (21).");
}
Also used : ManifestProtoUtils.withMaxSdkVersion(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withMaxSdkVersion) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) TEST_LABEL_RESOURCE_ID(com.android.tools.build.bundletool.testing.ResourcesTableFactory.TEST_LABEL_RESOURCE_ID) Value(com.android.bundle.Config.SplitDimension.Value) TargetingUtils.alternativeTextureCompressionTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.alternativeTextureCompressionTargeting) ManifestProtoUtils.androidManifest(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest) TargetingUtils.textureCompressionTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.textureCompressionTargeting) ATC(com.android.bundle.Targeting.TextureCompressionFormat.TextureCompressionFormatAlias.ATC) BundleConfigBuilder(com.android.tools.build.bundletool.testing.BundleConfigBuilder) RunWith(org.junit.runner.RunWith) ImmutableList(com.google.common.collect.ImmutableList) TargetingUtils.targetedAssetsDirectory(com.android.tools.build.bundletool.testing.TargetingUtils.targetedAssetsDirectory) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) ResourcesTableFactory.resourceTableWithTestLabel(com.android.tools.build.bundletool.testing.ResourcesTableFactory.resourceTableWithTestLabel) Path(java.nio.file.Path) Before(org.junit.Before) ManifestProtoUtils.withInstant(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withInstant) ManifestProtoUtils.withTitle(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withTitle) InvalidBundleException(com.android.tools.build.bundletool.model.exceptions.InvalidBundleException) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) ETC1_RGB8(com.android.bundle.Targeting.TextureCompressionFormat.TextureCompressionFormatAlias.ETC1_RGB8) TargetingUtils.assets(com.android.tools.build.bundletool.testing.TargetingUtils.assets) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) Rule(org.junit.Rule) SubValidator(com.android.tools.build.bundletool.validation.SubValidator) ManifestProtoUtils.androidManifestForFeature(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifestForFeature) TargetingUtils.assetsDirectoryTargeting(com.android.tools.build.bundletool.testing.TargetingUtils.assetsDirectoryTargeting) AppBundle(com.android.tools.build.bundletool.model.AppBundle) TemporaryFolder(org.junit.rules.TemporaryFolder) AppBundle(com.android.tools.build.bundletool.model.AppBundle) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) Test(org.junit.Test)

Example 98 with AppBundleBuilder

use of com.android.tools.build.bundletool.testing.AppBundleBuilder in project bundletool by google.

the class CheckTransparencyCommandTest method bundleMode_verificationFailed_transparencyKeyCertificateNotProvidedByUser.

@Test
public void bundleMode_verificationFailed_transparencyKeyCertificateNotProvidedByUser() throws Exception {
    // The public key transparencyKeyCertificate used to create JWS does not match the private key,
    // and will result
    // in signature verification failure later.
    String serializedJws = createJwsToken(CodeTransparency.getDefaultInstance(), CertificateFactory.buildSelfSignedCertificate(kpg.generateKeyPair(), "CN=CheckTransparencyCommandTest_BadCertificate"), transparencyPrivateKey);
    AppBundleBuilder appBundle = new AppBundleBuilder().addModule("base", module -> module.setManifest(androidManifest("com.test.app"))).addMetadataFile(BundleMetadata.BUNDLETOOL_NAMESPACE, BundleMetadata.TRANSPARENCY_SIGNED_FILE_NAME, CharSource.wrap(serializedJws).asByteSource(Charset.defaultCharset()));
    new AppBundleSerializer().writeToDisk(appBundle.build(), bundlePath);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CheckTransparencyCommand.builder().setMode(Mode.BUNDLE).setBundlePath(bundlePath).build().checkTransparency(new PrintStream(outputStream));
    String output = new String(outputStream.toByteArray(), UTF_8);
    assertThat(output).contains("No APK present. APK signature was not checked.");
    assertThat(output).contains("Verification failed because code transparency signature is invalid.");
}
Also used : X509Certificate(java.security.cert.X509Certificate) KeyPair(java.security.KeyPair) ApkTargeting(com.android.bundle.Targeting.ApkTargeting) ManifestProtoUtils.androidManifest(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest) AndroidManifest(com.android.tools.build.bundletool.model.AndroidManifest) ZipBuilder(com.android.tools.build.bundletool.io.ZipBuilder) Mode(com.android.tools.build.bundletool.commands.CheckTransparencyCommand.Mode) Path(java.nio.file.Path) FakeSystemEnvironmentProvider(com.android.tools.build.bundletool.testing.FakeSystemEnvironmentProvider) KeyPairGenerator(java.security.KeyPairGenerator) JsonWebSignature(org.jose4j.jws.JsonWebSignature) ImmutableMap(com.google.common.collect.ImmutableMap) CommandExecutionException(com.android.tools.build.bundletool.model.exceptions.CommandExecutionException) RequiredFlagNotSetException(com.android.tools.build.bundletool.flags.Flag.RequiredFlagNotSetException) ApkSerializer(com.android.tools.build.bundletool.io.ApkSerializer) FlagParser(com.android.tools.build.bundletool.flags.FlagParser) ByteString(com.google.protobuf.ByteString) SystemEnvironmentProvider(com.android.tools.build.bundletool.model.utils.SystemEnvironmentProvider) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) PrivateKey(java.security.PrivateKey) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) SignerConfig(com.android.tools.build.bundletool.model.SignerConfig) CodeTransparencyCryptoUtils(com.android.tools.build.bundletool.transparency.CodeTransparencyCryptoUtils) CodeTransparencyVersion(com.android.tools.build.bundletool.transparency.CodeTransparencyVersion) CodeTransparency(com.android.bundle.CodeTransparencyOuterClass.CodeTransparency) Mockito.mock(org.mockito.Mockito.mock) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) TestModule(com.android.tools.build.bundletool.testing.TestModule) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BundleModuleName(com.android.tools.build.bundletool.model.BundleModuleName) ZipPath(com.android.tools.build.bundletool.model.ZipPath) RunWith(org.junit.runner.RunWith) Hashing(com.google.common.hash.Hashing) Component(dagger.Component) RSA_USING_SHA384(org.jose4j.jws.AlgorithmIdentifiers.RSA_USING_SHA384) SigningConfiguration(com.android.tools.build.bundletool.model.SigningConfiguration) Inject(javax.inject.Inject) UnknownFlagsException(com.android.tools.build.bundletool.flags.ParsedFlags.UnknownFlagsException) ImmutableList(com.google.common.collect.ImmutableList) Charset(java.nio.charset.Charset) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) ANDROID_SERIAL(com.android.tools.build.bundletool.testing.FakeSystemEnvironmentProvider.ANDROID_SERIAL) CharSource(com.google.common.io.CharSource) ByteSource(com.google.common.io.ByteSource) CodeTransparencyTestUtils.createJwsToken(com.android.tools.build.bundletool.testing.CodeTransparencyTestUtils.createJwsToken) ANDROID_HOME(com.android.tools.build.bundletool.testing.FakeSystemEnvironmentProvider.ANDROID_HOME) VariantTargeting(com.android.bundle.Targeting.VariantTargeting) AdbServer(com.android.tools.build.bundletool.device.AdbServer) Before(org.junit.Before) PrintStream(java.io.PrintStream) CertificateFactory(com.android.tools.build.bundletool.testing.CertificateFactory) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Test(org.junit.Test) BundleMetadata(com.android.tools.build.bundletool.model.BundleMetadata) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Rule(org.junit.Rule) Paths(java.nio.file.Paths) InvalidCommandException(com.android.tools.build.bundletool.model.exceptions.InvalidCommandException) CodeRelatedFile(com.android.bundle.CodeTransparencyOuterClass.CodeRelatedFile) TemporaryFolder(org.junit.rules.TemporaryFolder) PrintStream(java.io.PrintStream) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) ByteString(com.google.protobuf.ByteString) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 99 with AppBundleBuilder

use of com.android.tools.build.bundletool.testing.AppBundleBuilder in project bundletool by google.

the class DumpManagerTest method dumpManifest_moduleNotBase.

@Test
public void dumpManifest_moduleNotBase() throws Exception {
    XmlNode manifestBase = XmlNode.newBuilder().setElement(XmlElement.newBuilder().setName("manifest").addAttribute(XmlAttribute.newBuilder().setName("package").setValue("base"))).build();
    XmlNode manifestModule = XmlNode.newBuilder().setElement(XmlElement.newBuilder().setName("manifest").addAttribute(XmlAttribute.newBuilder().setName("package").setValue("module"))).build();
    AppBundle appBundle = new AppBundleBuilder().addModule("base", module -> module.setManifest(manifestBase)).addModule("module", module -> module.setManifest(manifestModule)).build();
    new AppBundleSerializer().writeToDisk(appBundle, bundlePath);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    DumpCommand.builder().setBundlePath(bundlePath).setDumpTarget(DumpTarget.MANIFEST).setModuleName("module").setOutputStream(new PrintStream(outputStream)).build().execute();
    assertThat(new String(outputStream.toByteArray(), UTF_8)).isEqualTo(String.format("<manifest package=\"module\" split=\"module\"/>%n"));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ManifestProtoUtils.withMetadataValue(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withMetadataValue) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ManifestProtoUtils.androidManifest(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest) RunWith(org.junit.runner.RunWith) XmlAttribute(com.android.aapt.Resources.XmlAttribute) DumpTarget(com.android.tools.build.bundletool.commands.DumpCommand.DumpTarget) Bundletool(com.android.bundle.Config.Bundletool) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) Path(java.nio.file.Path) ResourceTable(com.android.aapt.Resources.ResourceTable) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Before(org.junit.Before) PrintStream(java.io.PrintStream) UTF_8(java.nio.charset.StandardCharsets.UTF_8) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) Rule(org.junit.Rule) XmlNode(com.android.aapt.Resources.XmlNode) BundleConfig(com.android.bundle.Config.BundleConfig) ManifestProtoUtils.withDebuggableAttribute(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withDebuggableAttribute) XmlElement(com.android.aapt.Resources.XmlElement) Compression(com.android.bundle.Config.Compression) AppBundle(com.android.tools.build.bundletool.model.AppBundle) TemporaryFolder(org.junit.rules.TemporaryFolder) PrintStream(java.io.PrintStream) XmlNode(com.android.aapt.Resources.XmlNode) AppBundle(com.android.tools.build.bundletool.model.AppBundle) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 100 with AppBundleBuilder

use of com.android.tools.build.bundletool.testing.AppBundleBuilder in project bundletool by google.

the class DumpManagerTest method dumpManifest_withXPath_predicate.

@Test
public void dumpManifest_withXPath_predicate() throws Exception {
    AppBundle appBundle = new AppBundleBuilder().addModule("base", module -> module.setManifest(androidManifest("com.app", withMetadataValue("key1", "value1"), withMetadataValue("key2", "value2")))).build();
    new AppBundleSerializer().writeToDisk(appBundle, bundlePath);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    DumpCommand.builder().setBundlePath(bundlePath).setDumpTarget(DumpTarget.MANIFEST).setXPathExpression("/manifest/application/meta-data[@android:name = \"key2\"]/@android:value").setOutputStream(new PrintStream(outputStream)).build().execute();
    assertThat(new String(outputStream.toByteArray(), UTF_8)).isEqualTo(String.format("value2%n"));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ManifestProtoUtils.withMetadataValue(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withMetadataValue) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ManifestProtoUtils.androidManifest(com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest) RunWith(org.junit.runner.RunWith) XmlAttribute(com.android.aapt.Resources.XmlAttribute) DumpTarget(com.android.tools.build.bundletool.commands.DumpCommand.DumpTarget) Bundletool(com.android.bundle.Config.Bundletool) ResourceTableBuilder(com.android.tools.build.bundletool.testing.ResourceTableBuilder) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) Path(java.nio.file.Path) ResourceTable(com.android.aapt.Resources.ResourceTable) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Before(org.junit.Before) PrintStream(java.io.PrintStream) UTF_8(java.nio.charset.StandardCharsets.UTF_8) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) Rule(org.junit.Rule) XmlNode(com.android.aapt.Resources.XmlNode) BundleConfig(com.android.bundle.Config.BundleConfig) ManifestProtoUtils.withDebuggableAttribute(com.android.tools.build.bundletool.testing.ManifestProtoUtils.withDebuggableAttribute) XmlElement(com.android.aapt.Resources.XmlElement) Compression(com.android.bundle.Config.Compression) AppBundle(com.android.tools.build.bundletool.model.AppBundle) TemporaryFolder(org.junit.rules.TemporaryFolder) PrintStream(java.io.PrintStream) AppBundle(com.android.tools.build.bundletool.model.AppBundle) AppBundleBuilder(com.android.tools.build.bundletool.testing.AppBundleBuilder) AppBundleSerializer(com.android.tools.build.bundletool.io.AppBundleSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

AppBundleBuilder (com.android.tools.build.bundletool.testing.AppBundleBuilder)155 Test (org.junit.Test)153 AppBundle (com.android.tools.build.bundletool.model.AppBundle)134 ManifestProtoUtils.androidManifest (com.android.tools.build.bundletool.testing.ManifestProtoUtils.androidManifest)123 Truth.assertThat (com.google.common.truth.Truth.assertThat)123 RunWith (org.junit.runner.RunWith)123 Path (java.nio.file.Path)113 ZipFile (java.util.zip.ZipFile)113 Before (org.junit.Before)113 Rule (org.junit.Rule)113 TemporaryFolder (org.junit.rules.TemporaryFolder)113 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)112 AppBundleSerializer (com.android.tools.build.bundletool.io.AppBundleSerializer)105 ImmutableList (com.google.common.collect.ImmutableList)102 BuildApksResult (com.android.bundle.Commands.BuildApksResult)101 Variant (com.android.bundle.Commands.Variant)94 InvalidBundleException (com.android.tools.build.bundletool.model.exceptions.InvalidBundleException)91 AndroidManifest (com.android.tools.build.bundletool.model.AndroidManifest)88 ApkDescription (com.android.bundle.Commands.ApkDescription)87 BundleConfig (com.android.bundle.Config.BundleConfig)87