Move AllowMissingDependencies handling into processMainCert

I046d75dbbd4f21f4a2b6851f558e430e9879fcff fixed android_app modules
with missing certificate dependencies when AllowMissingDependencies
was set, but the same problem can occur in android_app_import and
android_rro modules.  Move the AllowMissingDependencies handling
into processMainCert so that it applies to all of them.

Bug: 246649647
Test: TestAppImportMissingCertificateAllowMissingDependencies
Change-Id: Ic7dd3e61e0e3af15c53b583cf680b1e52394a018
This commit is contained in:
Colin Cross
2022-09-14 12:45:42 -07:00
parent a73e672503
commit bc2c8a7517
5 changed files with 44 additions and 26 deletions

View File

@@ -526,7 +526,8 @@ func (a *AndroidApp) JNISymbolsInstalls(installPath string) android.RuleBuilderI
// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate,
ctx android.ModuleContext) (mainCertificate Certificate, allCertificates []Certificate) {
if android.SrcIsModule(certPropValue) == "" {
var mainCert Certificate
if certPropValue != "" {
@@ -558,7 +559,22 @@ func processMainCert(m android.ModuleBase, certPropValue string, certificates []
}
}
return certificates
if len(certificates) > 0 {
mainCertificate = certificates[0]
} else {
// This can be reached with an empty certificate list if AllowMissingDependencies is set
// and the certificate property for this module is a module reference to a missing module.
if !ctx.Config().AllowMissingDependencies() && len(ctx.GetMissingDependencies()) > 0 {
panic("Should only get here if AllowMissingDependencies set and there are missing dependencies")
}
// Set a certificate to avoid panics later when accessing it.
mainCertificate = Certificate{
Key: android.PathForModuleOut(ctx, "missing.pk8"),
Pem: android.PathForModuleOut(ctx, "missing.pem"),
}
}
return mainCertificate, certificates
}
func (a *AndroidApp) InstallApkName() string {
@@ -632,29 +648,14 @@ func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
dexJarFile := a.dexBuildActions(ctx)
jniLibs, prebuiltJniPackages, certificateDeps := collectAppDeps(ctx, a, a.shouldEmbedJnis(ctx), !Bool(a.appProperties.Jni_uses_platform_apis))
jniLibs, prebuiltJniPackages, certificates := collectAppDeps(ctx, a, a.shouldEmbedJnis(ctx), !Bool(a.appProperties.Jni_uses_platform_apis))
jniJarFile := a.jniBuildActions(jniLibs, prebuiltJniPackages, ctx)
if ctx.Failed() {
return
}
certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
// This can be reached with an empty certificate list if AllowMissingDependencies is set
// and the certificate property for this module is a module reference to a missing module.
if len(certificates) > 0 {
a.certificate = certificates[0]
} else {
if !ctx.Config().AllowMissingDependencies() && len(ctx.GetMissingDependencies()) > 0 {
panic("Should only get here if AllowMissingDependencies set and there are missing dependencies")
}
// Set a certificate to avoid panics later when accessing it.
a.certificate = Certificate{
Key: android.PathForModuleOut(ctx, "missing.pk8"),
Pem: android.PathForModuleOut(ctx, "missing.pem"),
}
}
a.certificate, certificates = processMainCert(a.ModuleBase, a.getCertString(ctx), certificates, ctx)
// Build a final signed app package.
packageFile := android.PathForModuleOut(ctx, a.installApkName+".apk")

View File

@@ -318,19 +318,17 @@ func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext
if a.isPrebuiltFrameworkRes() {
a.outputFile = srcApk
certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
a.certificate, certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
if len(certificates) != 1 {
ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
}
a.certificate = certificates[0]
} else if a.preprocessed {
a.outputFile = srcApk
a.certificate = PresignedCertificate
} else if !Bool(a.properties.Presigned) {
// If the certificate property is empty at this point, default_dev_cert must be set to true.
// Which makes processMainCert's behavior for the empty cert string WAI.
certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
a.certificate = certificates[0]
a.certificate, certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
signed := android.PathForModuleOut(ctx, "signed", apkFilename)
var lineageFile android.Path
if lineage := String(a.properties.Lineage); lineage != "" {

View File

@@ -807,3 +807,23 @@ func TestAndroidTestImport_UncompressDex(t *testing.T) {
}
}
}
func TestAppImportMissingCertificateAllowMissingDependencies(t *testing.T) {
result := android.GroupFixturePreparers(
PrepareForTestWithJavaDefaultModules,
android.PrepareForTestWithAllowMissingDependencies,
android.PrepareForTestWithAndroidMk,
).RunTestWithBp(t, `
android_app_import {
name: "foo",
apk: "a.apk",
certificate: ":missing_certificate",
}`)
foo := result.ModuleForTests("foo", "android_common")
fooApk := foo.Output("signed/foo.apk")
if fooApk.Rule != android.ErrorRule {
t.Fatalf("expected ErrorRule for foo.apk, got %s", fooApk.Rule.String())
}
android.AssertStringDoesContain(t, "expected error rule message", fooApk.Args["error"], "missing dependencies: missing_certificate\n")
}

View File

@@ -684,7 +684,7 @@ func javadocCmd(ctx android.ModuleContext, rule *android.RuleBuilder, srcs andro
outDir, srcJarDir, srcJarList android.Path, sourcepaths android.Paths) *android.RuleBuilderCommand {
cmd := rule.Command().
BuiltTool("soong_javac_wrapper").Tool(config.JavadocCmd(ctx)).
BuiltTool("soong_javac_wrapper").Tool(android.PathForSource(ctx, "prebuilts/jdk/jdk11/linux-x86/bin/javadoc")).
Flag(config.JavacVmFlags).
FlagWithArg("-encoding ", "UTF-8").
FlagWithRspFileInputList("@", android.PathForModuleOut(ctx, "javadoc.rsp"), srcs).

View File

@@ -146,7 +146,7 @@ func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleC
// Sign the built package
_, _, certificates := collectAppDeps(ctx, r, false, false)
certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
r.certificate, certificates = processMainCert(r.ModuleBase, String(r.properties.Certificate), certificates, ctx)
signed := android.PathForModuleOut(ctx, "signed", r.Name()+".apk")
var lineageFile android.Path
if lineage := String(r.properties.Lineage); lineage != "" {
@@ -156,7 +156,6 @@ func (r *RuntimeResourceOverlay) GenerateAndroidBuildActions(ctx android.ModuleC
rotationMinSdkVersion := String(r.properties.RotationMinSdkVersion)
SignAppPackage(ctx, signed, r.aapt.exportPackage, certificates, nil, lineageFile, rotationMinSdkVersion)
r.certificate = certificates[0]
r.outputFile = signed
partition := rroPartition(ctx)