blob: 8f29bb3733cdaabd1da7d74da2ce16a7016bf03e [file] [log] [blame]
Jaewoong Jungf9b44652020-12-21 12:29:12 -08001// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Ulya Trafimovich55f72d72021-09-01 14:13:57 +010018 "fmt"
Jaewoong Jungf9b44652020-12-21 12:29:12 -080019 "reflect"
Jaewoong Jungf9b44652020-12-21 12:29:12 -080020 "strings"
21 "testing"
22
23 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
26)
27
28func TestAndroidAppImport(t *testing.T) {
29 ctx, _ := testJava(t, `
30 android_app_import {
31 name: "foo",
32 apk: "prebuilts/apk/app.apk",
33 certificate: "platform",
34 dex_preopt: {
35 enabled: true,
36 },
37 }
38 `)
39
40 variant := ctx.ModuleForTests("foo", "android_common")
41
42 // Check dexpreopt outputs.
43 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
44 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
45 t.Errorf("can't find dexpreopt outputs")
46 }
47
48 // Check cert signing flag.
49 signedApk := variant.Output("signed/foo.apk")
50 signingFlag := signedApk.Args["certificates"]
51 expected := "build/make/target/product/security/platform.x509.pem build/make/target/product/security/platform.pk8"
52 if expected != signingFlag {
53 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
54 }
Wei Li340ee8e2022-03-18 17:33:24 -070055 rule := variant.Rule("genProvenanceMetaData")
56 android.AssertStringEquals(t, "Invalid input", "prebuilts/apk/app.apk", rule.Inputs[0].String())
57 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/foo/provenance_metadata.textproto", rule.Output.String())
58 android.AssertStringEquals(t, "Invalid args", "foo", rule.Args["module_name"])
59 android.AssertStringEquals(t, "Invalid args", "/system/app/foo/foo.apk", rule.Args["install_path"])
Jaewoong Jungf9b44652020-12-21 12:29:12 -080060}
61
62func TestAndroidAppImport_NoDexPreopt(t *testing.T) {
63 ctx, _ := testJava(t, `
64 android_app_import {
65 name: "foo",
66 apk: "prebuilts/apk/app.apk",
67 certificate: "platform",
68 dex_preopt: {
69 enabled: false,
70 },
71 }
72 `)
73
74 variant := ctx.ModuleForTests("foo", "android_common")
75
76 // Check dexpreopt outputs. They shouldn't exist.
77 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule != nil ||
78 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule != nil {
79 t.Errorf("dexpreopt shouldn't have run.")
80 }
Wei Li340ee8e2022-03-18 17:33:24 -070081
82 rule := variant.Rule("genProvenanceMetaData")
83 android.AssertStringEquals(t, "Invalid input", "prebuilts/apk/app.apk", rule.Inputs[0].String())
84 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/foo/provenance_metadata.textproto", rule.Output.String())
85 android.AssertStringEquals(t, "Invalid args", "foo", rule.Args["module_name"])
86 android.AssertStringEquals(t, "Invalid args", "/system/app/foo/foo.apk", rule.Args["install_path"])
Jaewoong Jungf9b44652020-12-21 12:29:12 -080087}
88
89func TestAndroidAppImport_Presigned(t *testing.T) {
90 ctx, _ := testJava(t, `
91 android_app_import {
92 name: "foo",
93 apk: "prebuilts/apk/app.apk",
94 presigned: true,
95 dex_preopt: {
96 enabled: true,
97 },
98 }
99 `)
100
101 variant := ctx.ModuleForTests("foo", "android_common")
102
103 // Check dexpreopt outputs.
104 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
105 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
106 t.Errorf("can't find dexpreopt outputs")
107 }
108 // Make sure signing was skipped and aligning was done.
109 if variant.MaybeOutput("signed/foo.apk").Rule != nil {
110 t.Errorf("signing rule shouldn't be included.")
111 }
112 if variant.MaybeOutput("zip-aligned/foo.apk").Rule == nil {
113 t.Errorf("can't find aligning rule")
114 }
Wei Li340ee8e2022-03-18 17:33:24 -0700115
116 rule := variant.Rule("genProvenanceMetaData")
117 android.AssertStringEquals(t, "Invalid input", "prebuilts/apk/app.apk", rule.Inputs[0].String())
118 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/foo/provenance_metadata.textproto", rule.Output.String())
119 android.AssertStringEquals(t, "Invalid args", "foo", rule.Args["module_name"])
120 android.AssertStringEquals(t, "Invalid args", "/system/app/foo/foo.apk", rule.Args["install_path"])
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800121}
122
123func TestAndroidAppImport_SigningLineage(t *testing.T) {
124 ctx, _ := testJava(t, `
125 android_app_import {
126 name: "foo",
127 apk: "prebuilts/apk/app.apk",
128 certificate: "platform",
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800129 additional_certificates: [":additional_certificate"],
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800130 lineage: "lineage.bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400131 rotationMinSdkVersion: "32",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800132 }
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800133
134 android_app_certificate {
135 name: "additional_certificate",
136 certificate: "cert/additional_cert",
137 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800138 `)
139
140 variant := ctx.ModuleForTests("foo", "android_common")
141
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800142 signedApk := variant.Output("signed/foo.apk")
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800143 // Check certificates
144 certificatesFlag := signedApk.Args["certificates"]
145 expected := "build/make/target/product/security/platform.x509.pem " +
146 "build/make/target/product/security/platform.pk8 " +
147 "cert/additional_cert.x509.pem cert/additional_cert.pk8"
148 if expected != certificatesFlag {
149 t.Errorf("Incorrect certificates flags, expected: %q, got: %q", expected, certificatesFlag)
150 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400151
152 // Check cert signing flags.
153 actualCertSigningFlags := signedApk.Args["flags"]
154 expectedCertSigningFlags := "--lineage lineage.bin --rotation-min-sdk-version 32"
155 if expectedCertSigningFlags != actualCertSigningFlags {
156 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expectedCertSigningFlags, actualCertSigningFlags)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800157 }
Wei Li340ee8e2022-03-18 17:33:24 -0700158
159 rule := variant.Rule("genProvenanceMetaData")
160 android.AssertStringEquals(t, "Invalid input", "prebuilts/apk/app.apk", rule.Inputs[0].String())
161 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/foo/provenance_metadata.textproto", rule.Output.String())
162 android.AssertStringEquals(t, "Invalid args", "foo", rule.Args["module_name"])
163 android.AssertStringEquals(t, "Invalid args", "/system/app/foo/foo.apk", rule.Args["install_path"])
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800164}
165
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800166func TestAndroidAppImport_SigningLineageFilegroup(t *testing.T) {
167 ctx, _ := testJava(t, `
168 android_app_import {
169 name: "foo",
170 apk: "prebuilts/apk/app.apk",
171 certificate: "platform",
172 lineage: ":lineage_bin",
173 }
174
175 filegroup {
176 name: "lineage_bin",
177 srcs: ["lineage.bin"],
178 }
179 `)
180
181 variant := ctx.ModuleForTests("foo", "android_common")
182
183 signedApk := variant.Output("signed/foo.apk")
184 // Check cert signing lineage flag.
185 signingFlag := signedApk.Args["flags"]
186 expected := "--lineage lineage.bin"
187 if expected != signingFlag {
188 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
189 }
Wei Li340ee8e2022-03-18 17:33:24 -0700190
191 rule := variant.Rule("genProvenanceMetaData")
192 android.AssertStringEquals(t, "Invalid input", "prebuilts/apk/app.apk", rule.Inputs[0].String())
193 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/foo/provenance_metadata.textproto", rule.Output.String())
194 android.AssertStringEquals(t, "Invalid args", "foo", rule.Args["module_name"])
195 android.AssertStringEquals(t, "Invalid args", "/system/app/foo/foo.apk", rule.Args["install_path"])
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800196}
197
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800198func TestAndroidAppImport_DefaultDevCert(t *testing.T) {
199 ctx, _ := testJava(t, `
200 android_app_import {
201 name: "foo",
202 apk: "prebuilts/apk/app.apk",
203 default_dev_cert: true,
204 dex_preopt: {
205 enabled: true,
206 },
207 }
208 `)
209
210 variant := ctx.ModuleForTests("foo", "android_common")
211
212 // Check dexpreopt outputs.
213 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
214 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
215 t.Errorf("can't find dexpreopt outputs")
216 }
217
218 // Check cert signing flag.
219 signedApk := variant.Output("signed/foo.apk")
220 signingFlag := signedApk.Args["certificates"]
221 expected := "build/make/target/product/security/testkey.x509.pem build/make/target/product/security/testkey.pk8"
222 if expected != signingFlag {
223 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
224 }
Wei Li340ee8e2022-03-18 17:33:24 -0700225
226 rule := variant.Rule("genProvenanceMetaData")
227 android.AssertStringEquals(t, "Invalid input", "prebuilts/apk/app.apk", rule.Inputs[0].String())
228 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/foo/provenance_metadata.textproto", rule.Output.String())
229 android.AssertStringEquals(t, "Invalid args", "foo", rule.Args["module_name"])
230 android.AssertStringEquals(t, "Invalid args", "/system/app/foo/foo.apk", rule.Args["install_path"])
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800231}
232
233func TestAndroidAppImport_DpiVariants(t *testing.T) {
234 bp := `
235 android_app_import {
236 name: "foo",
237 apk: "prebuilts/apk/app.apk",
238 dpi_variants: {
239 xhdpi: {
240 apk: "prebuilts/apk/app_xhdpi.apk",
241 },
242 xxhdpi: {
243 apk: "prebuilts/apk/app_xxhdpi.apk",
244 },
245 },
246 presigned: true,
247 dex_preopt: {
248 enabled: true,
249 },
250 }
251 `
252 testCases := []struct {
Wei Li340ee8e2022-03-18 17:33:24 -0700253 name string
254 aaptPreferredConfig *string
255 aaptPrebuiltDPI []string
256 expected string
257 expectedProvenanceMetaDataArtifactPath string
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800258 }{
259 {
Wei Li340ee8e2022-03-18 17:33:24 -0700260 name: "no preferred",
261 aaptPreferredConfig: nil,
262 aaptPrebuiltDPI: []string{},
263 expected: "verify_uses_libraries/apk/app.apk",
264 expectedProvenanceMetaDataArtifactPath: "prebuilts/apk/app.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800265 },
266 {
Wei Li340ee8e2022-03-18 17:33:24 -0700267 name: "AAPTPreferredConfig matches",
268 aaptPreferredConfig: proptools.StringPtr("xhdpi"),
269 aaptPrebuiltDPI: []string{"xxhdpi", "ldpi"},
270 expected: "verify_uses_libraries/apk/app_xhdpi.apk",
271 expectedProvenanceMetaDataArtifactPath: "prebuilts/apk/app_xhdpi.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800272 },
273 {
Wei Li340ee8e2022-03-18 17:33:24 -0700274 name: "AAPTPrebuiltDPI matches",
275 aaptPreferredConfig: proptools.StringPtr("mdpi"),
276 aaptPrebuiltDPI: []string{"xxhdpi", "xhdpi"},
277 expected: "verify_uses_libraries/apk/app_xxhdpi.apk",
278 expectedProvenanceMetaDataArtifactPath: "prebuilts/apk/app_xxhdpi.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800279 },
280 {
Wei Li340ee8e2022-03-18 17:33:24 -0700281 name: "non-first AAPTPrebuiltDPI matches",
282 aaptPreferredConfig: proptools.StringPtr("mdpi"),
283 aaptPrebuiltDPI: []string{"ldpi", "xhdpi"},
284 expected: "verify_uses_libraries/apk/app_xhdpi.apk",
285 expectedProvenanceMetaDataArtifactPath: "prebuilts/apk/app_xhdpi.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800286 },
287 {
Wei Li340ee8e2022-03-18 17:33:24 -0700288 name: "no matches",
289 aaptPreferredConfig: proptools.StringPtr("mdpi"),
290 aaptPrebuiltDPI: []string{"ldpi", "xxxhdpi"},
291 expected: "verify_uses_libraries/apk/app.apk",
292 expectedProvenanceMetaDataArtifactPath: "prebuilts/apk/app.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800293 },
294 }
295
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800296 for _, test := range testCases {
Paul Duffinfb8bc952021-03-22 17:31:52 +0000297 result := android.GroupFixturePreparers(
298 PrepareForTestWithJavaDefaultModules,
299 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
300 variables.AAPTPreferredConfig = test.aaptPreferredConfig
301 variables.AAPTPrebuiltDPI = test.aaptPrebuiltDPI
302 }),
303 ).RunTestWithBp(t, bp)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800304
Paul Duffinfb8bc952021-03-22 17:31:52 +0000305 variant := result.ModuleForTests("foo", "android_common")
Cole Faust4ec178c2023-01-13 12:03:38 -0800306 input := variant.Output("jnis-uncompressed/foo.apk").Input.String()
307 if strings.HasSuffix(input, test.expected) {
308 t.Errorf("wrong src apk, expected: %q got: %q", test.expected, input)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800309 }
Wei Li340ee8e2022-03-18 17:33:24 -0700310
311 provenanceMetaDataRule := variant.Rule("genProvenanceMetaData")
312 android.AssertStringEquals(t, "Invalid input", test.expectedProvenanceMetaDataArtifactPath, provenanceMetaDataRule.Inputs[0].String())
313 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/foo/provenance_metadata.textproto", provenanceMetaDataRule.Output.String())
314 android.AssertStringEquals(t, "Invalid args", "foo", provenanceMetaDataRule.Args["module_name"])
315 android.AssertStringEquals(t, "Invalid args", "/system/app/foo/foo.apk", provenanceMetaDataRule.Args["install_path"])
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800316 }
317}
318
319func TestAndroidAppImport_Filename(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700320 ctx, _ := testJava(t, `
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800321 android_app_import {
322 name: "foo",
323 apk: "prebuilts/apk/app.apk",
324 presigned: true,
325 }
326
327 android_app_import {
328 name: "bar",
329 apk: "prebuilts/apk/app.apk",
330 presigned: true,
331 filename: "bar_sample.apk"
332 }
333 `)
334
335 testCases := []struct {
Wei Li340ee8e2022-03-18 17:33:24 -0700336 name string
337 expected string
338 onDevice string
339 expectedArtifactPath string
340 expectedMetaDataPath string
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800341 }{
342 {
Wei Li340ee8e2022-03-18 17:33:24 -0700343 name: "foo",
344 expected: "foo.apk",
345 onDevice: "/system/app/foo/foo.apk",
346 expectedArtifactPath: "prebuilts/apk/app.apk",
347 expectedMetaDataPath: "out/soong/.intermediates/provenance_metadata/foo/provenance_metadata.textproto",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800348 },
349 {
Wei Li340ee8e2022-03-18 17:33:24 -0700350 name: "bar",
351 expected: "bar_sample.apk",
352 onDevice: "/system/app/bar/bar_sample.apk",
353 expectedArtifactPath: "prebuilts/apk/app.apk",
354 expectedMetaDataPath: "out/soong/.intermediates/provenance_metadata/bar/provenance_metadata.textproto",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800355 },
356 }
357
358 for _, test := range testCases {
359 variant := ctx.ModuleForTests(test.name, "android_common")
360 if variant.MaybeOutput(test.expected).Rule == nil {
361 t.Errorf("can't find output named %q - all outputs: %v", test.expected, variant.AllOutputs())
362 }
363
364 a := variant.Module().(*AndroidAppImport)
365 expectedValues := []string{test.expected}
Wei Li598f92d2023-01-04 17:12:24 -0800366 entries := android.AndroidMkEntriesForTest(t, ctx, a)[0]
367 actualValues := entries.EntryMap["LOCAL_INSTALLED_MODULE_STEM"]
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800368 if !reflect.DeepEqual(actualValues, expectedValues) {
369 t.Errorf("Incorrect LOCAL_INSTALLED_MODULE_STEM value '%s', expected '%s'",
370 actualValues, expectedValues)
371 }
Wei Li598f92d2023-01-04 17:12:24 -0800372 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "android_app_import", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
373
Wei Li340ee8e2022-03-18 17:33:24 -0700374 rule := variant.Rule("genProvenanceMetaData")
375 android.AssertStringEquals(t, "Invalid input", test.expectedArtifactPath, rule.Inputs[0].String())
376 android.AssertStringEquals(t, "Invalid output", test.expectedMetaDataPath, rule.Output.String())
377 android.AssertStringEquals(t, "Invalid args", test.name, rule.Args["module_name"])
378 android.AssertStringEquals(t, "Invalid args", test.onDevice, rule.Args["install_path"])
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800379 }
380}
381
382func TestAndroidAppImport_ArchVariants(t *testing.T) {
383 // The test config's target arch is ARM64.
384 testCases := []struct {
Wei Li340ee8e2022-03-18 17:33:24 -0700385 name string
386 bp string
387 expected string
388 artifactPath string
389 metaDataPath string
390 installPath string
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800391 }{
392 {
393 name: "matching arch",
394 bp: `
395 android_app_import {
396 name: "foo",
397 apk: "prebuilts/apk/app.apk",
398 arch: {
399 arm64: {
400 apk: "prebuilts/apk/app_arm64.apk",
401 },
402 },
403 presigned: true,
404 dex_preopt: {
405 enabled: true,
406 },
407 }
408 `,
Wei Li340ee8e2022-03-18 17:33:24 -0700409 expected: "verify_uses_libraries/apk/app_arm64.apk",
410 artifactPath: "prebuilts/apk/app_arm64.apk",
411 installPath: "/system/app/foo/foo.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800412 },
413 {
414 name: "no matching arch",
415 bp: `
416 android_app_import {
417 name: "foo",
418 apk: "prebuilts/apk/app.apk",
419 arch: {
420 arm: {
421 apk: "prebuilts/apk/app_arm.apk",
422 },
423 },
424 presigned: true,
425 dex_preopt: {
426 enabled: true,
427 },
428 }
429 `,
Wei Li340ee8e2022-03-18 17:33:24 -0700430 expected: "verify_uses_libraries/apk/app.apk",
431 artifactPath: "prebuilts/apk/app.apk",
432 installPath: "/system/app/foo/foo.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800433 },
434 {
435 name: "no matching arch without default",
436 bp: `
437 android_app_import {
438 name: "foo",
439 arch: {
440 arm: {
441 apk: "prebuilts/apk/app_arm.apk",
442 },
443 },
444 presigned: true,
445 dex_preopt: {
446 enabled: true,
447 },
448 }
449 `,
Wei Li340ee8e2022-03-18 17:33:24 -0700450 expected: "",
451 artifactPath: "prebuilts/apk/app_arm.apk",
452 installPath: "/system/app/foo/foo.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800453 },
454 }
455
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800456 for _, test := range testCases {
457 ctx, _ := testJava(t, test.bp)
458
459 variant := ctx.ModuleForTests("foo", "android_common")
460 if test.expected == "" {
461 if variant.Module().Enabled() {
462 t.Error("module should have been disabled, but wasn't")
463 }
Wei Li340ee8e2022-03-18 17:33:24 -0700464 rule := variant.MaybeRule("genProvenanceMetaData")
465 android.AssertDeepEquals(t, "Provenance metadata is not empty", android.TestingBuildParams{}, rule)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800466 continue
467 }
Cole Faust4ec178c2023-01-13 12:03:38 -0800468 input := variant.Output("jnis-uncompressed/foo.apk").Input.String()
469 if strings.HasSuffix(input, test.expected) {
470 t.Errorf("wrong src apk, expected: %q got: %q", test.expected, input)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800471 }
Wei Li340ee8e2022-03-18 17:33:24 -0700472 rule := variant.Rule("genProvenanceMetaData")
473 android.AssertStringEquals(t, "Invalid input", test.artifactPath, rule.Inputs[0].String())
474 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/foo/provenance_metadata.textproto", rule.Output.String())
475 android.AssertStringEquals(t, "Invalid args", "foo", rule.Args["module_name"])
476 android.AssertStringEquals(t, "Invalid args", test.installPath, rule.Args["install_path"])
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800477 }
478}
479
480func TestAndroidAppImport_overridesDisabledAndroidApp(t *testing.T) {
481 ctx, _ := testJava(t, `
482 android_app {
483 name: "foo",
484 srcs: ["a.java"],
485 enabled: false,
486 }
487
488 android_app_import {
489 name: "foo",
490 apk: "prebuilts/apk/app.apk",
491 certificate: "platform",
492 prefer: true,
493 }
494 `)
495
496 variant := ctx.ModuleForTests("prebuilt_foo", "android_common")
497 a := variant.Module().(*AndroidAppImport)
498 // The prebuilt module should still be enabled and active even if the source-based counterpart
499 // is disabled.
500 if !a.prebuilt.UsePrebuilt() {
501 t.Errorf("prebuilt foo module is not active")
502 }
503 if !a.Enabled() {
504 t.Errorf("prebuilt foo module is disabled")
505 }
506}
507
Spandan Dasd1fac642021-05-18 17:01:41 +0000508func TestAndroidAppImport_relativeInstallPath(t *testing.T) {
509 bp := `
510 android_app_import {
511 name: "no_relative_install_path",
512 apk: "prebuilts/apk/app.apk",
513 presigned: true,
514 }
515
516 android_app_import {
517 name: "relative_install_path",
518 apk: "prebuilts/apk/app.apk",
519 presigned: true,
520 relative_install_path: "my/path",
521 }
522
523 android_app_import {
Spandan Dasd1fac642021-05-18 17:01:41 +0000524 name: "privileged_relative_install_path",
525 apk: "prebuilts/apk/app.apk",
526 presigned: true,
527 privileged: true,
528 relative_install_path: "my/path"
529 }
530 `
531 testCases := []struct {
532 name string
533 expectedInstallPath string
534 errorMessage string
535 }{
536 {
537 name: "no_relative_install_path",
538 expectedInstallPath: "out/soong/target/product/test_device/system/app/no_relative_install_path/no_relative_install_path.apk",
539 errorMessage: "Install path is not correct when relative_install_path is missing",
540 },
541 {
542 name: "relative_install_path",
543 expectedInstallPath: "out/soong/target/product/test_device/system/app/my/path/relative_install_path/relative_install_path.apk",
544 errorMessage: "Install path is not correct for app when relative_install_path is present",
545 },
546 {
Spandan Dasd1fac642021-05-18 17:01:41 +0000547 name: "privileged_relative_install_path",
548 expectedInstallPath: "out/soong/target/product/test_device/system/priv-app/my/path/privileged_relative_install_path/privileged_relative_install_path.apk",
549 errorMessage: "Install path is not correct for privileged app when relative_install_path is present",
550 },
551 }
552 for _, testCase := range testCases {
553 ctx, _ := testJava(t, bp)
554 mod := ctx.ModuleForTests(testCase.name, "android_common").Module().(*AndroidAppImport)
555 android.AssertPathRelativeToTopEquals(t, testCase.errorMessage, testCase.expectedInstallPath, mod.installPath)
556 }
557}
558
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800559func TestAndroidTestImport(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700560 ctx, _ := testJava(t, `
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800561 android_test_import {
562 name: "foo",
563 apk: "prebuilts/apk/app.apk",
564 presigned: true,
565 data: [
566 "testdata/data",
567 ],
568 }
569 `)
570
571 test := ctx.ModuleForTests("foo", "android_common").Module().(*AndroidTestImport)
572
573 // Check android mks.
Colin Crossaa255532020-07-03 13:18:24 -0700574 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800575 expected := []string{"tests"}
576 actual := entries.EntryMap["LOCAL_MODULE_TAGS"]
577 if !reflect.DeepEqual(expected, actual) {
578 t.Errorf("Unexpected module tags - expected: %q, actual: %q", expected, actual)
579 }
580 expected = []string{"testdata/data:testdata/data"}
581 actual = entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
582 if !reflect.DeepEqual(expected, actual) {
583 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
584 }
585}
586
587func TestAndroidTestImport_NoJinUncompressForPresigned(t *testing.T) {
588 ctx, _ := testJava(t, `
589 android_test_import {
590 name: "foo",
591 apk: "prebuilts/apk/app.apk",
592 certificate: "cert/new_cert",
593 data: [
594 "testdata/data",
595 ],
596 }
597
598 android_test_import {
599 name: "foo_presigned",
600 apk: "prebuilts/apk/app.apk",
601 presigned: true,
602 data: [
603 "testdata/data",
604 ],
605 }
606 `)
607
608 variant := ctx.ModuleForTests("foo", "android_common")
Cole Faust4ec178c2023-01-13 12:03:38 -0800609 jniRule := variant.Output("jnis-uncompressed/foo.apk").BuildParams.Rule.String()
610 if jniRule == android.Cp.String() {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800611 t.Errorf("Unexpected JNI uncompress rule command: " + jniRule)
612 }
613
614 variant = ctx.ModuleForTests("foo_presigned", "android_common")
615 jniRule = variant.Output("jnis-uncompressed/foo_presigned.apk").BuildParams.Rule.String()
616 if jniRule != android.Cp.String() {
617 t.Errorf("Unexpected JNI uncompress rule: " + jniRule)
618 }
619 if variant.MaybeOutput("zip-aligned/foo_presigned.apk").Rule == nil {
620 t.Errorf("Presigned test apk should be aligned")
621 }
622}
623
624func TestAndroidTestImport_Preprocessed(t *testing.T) {
625 ctx, _ := testJava(t, `
626 android_test_import {
627 name: "foo",
628 apk: "prebuilts/apk/app.apk",
629 presigned: true,
630 preprocessed: true,
631 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800632 `)
633
Cole Faust61585282023-07-14 16:23:39 -0700634 apkName := "foo.apk"
635 variant := ctx.ModuleForTests("foo", "android_common")
636 jniRule := variant.Output("jnis-uncompressed/" + apkName).BuildParams.Rule.String()
637 if jniRule != android.Cp.String() {
638 t.Errorf("Unexpected JNI uncompress rule: " + jniRule)
639 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800640
Cole Faust61585282023-07-14 16:23:39 -0700641 // Make sure signing and aligning were skipped.
642 if variant.MaybeOutput("signed/"+apkName).Rule != nil {
643 t.Errorf("signing rule shouldn't be included for preprocessed.")
644 }
645 if variant.MaybeOutput("zip-aligned/"+apkName).Rule != nil {
646 t.Errorf("aligning rule shouldn't be for preprocessed")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800647 }
648}
Ulya Trafimovich55f72d72021-09-01 14:13:57 +0100649
Cole Faustccb20f42023-05-04 12:38:24 -0700650func TestAndroidAppImport_Preprocessed(t *testing.T) {
651 ctx, _ := testJava(t, `
652 android_app_import {
653 name: "foo",
654 apk: "prebuilts/apk/app.apk",
655 presigned: true,
656 preprocessed: true,
657 }
658 `)
659
660 apkName := "foo.apk"
661 variant := ctx.ModuleForTests("foo", "android_common")
Cole Faust9c5c09f2023-09-06 16:11:44 -0700662 outputBuildParams := variant.Output(apkName).BuildParams
Cole Faustccb20f42023-05-04 12:38:24 -0700663 if outputBuildParams.Rule.String() != android.Cp.String() {
664 t.Errorf("Unexpected prebuilt android_app_import rule: " + outputBuildParams.Rule.String())
665 }
666
667 // Make sure compression and aligning were validated.
Cole Faust9c5c09f2023-09-06 16:11:44 -0700668 if outputBuildParams.Validation == nil {
669 t.Errorf("Expected validation rule, but was not found")
670 }
671
672 validationBuildParams := variant.Output("validated-prebuilt/check.stamp").BuildParams
673 if validationBuildParams.Rule.String() != checkPresignedApkRule.String() {
674 t.Errorf("Unexpected validation rule: " + validationBuildParams.Rule.String())
Cole Faustccb20f42023-05-04 12:38:24 -0700675 }
676}
Cole Faust2f1da162023-04-17 15:06:56 -0700677
Ulya Trafimovich55f72d72021-09-01 14:13:57 +0100678func TestAndroidTestImport_UncompressDex(t *testing.T) {
679 testCases := []struct {
680 name string
681 bp string
682 }{
683 {
684 name: "normal",
685 bp: `
686 android_app_import {
687 name: "foo",
688 presigned: true,
689 apk: "prebuilts/apk/app.apk",
690 }
691 `,
692 },
693 {
694 name: "privileged",
695 bp: `
696 android_app_import {
697 name: "foo",
698 presigned: true,
699 privileged: true,
700 apk: "prebuilts/apk/app.apk",
701 }
702 `,
703 },
704 }
705
706 test := func(t *testing.T, bp string, unbundled bool, dontUncompressPrivAppDexs bool) {
707 t.Helper()
708
709 result := android.GroupFixturePreparers(
710 prepareForJavaTest,
711 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
712 if unbundled {
713 variables.Unbundled_build = proptools.BoolPtr(true)
714 }
715 variables.UncompressPrivAppDex = proptools.BoolPtr(!dontUncompressPrivAppDexs)
716 }),
717 ).RunTestWithBp(t, bp)
718
719 foo := result.ModuleForTests("foo", "android_common")
720 actual := foo.MaybeRule("uncompress-dex").Rule != nil
721
722 expect := !unbundled
723 if strings.Contains(bp, "privileged: true") {
724 if dontUncompressPrivAppDexs {
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100725 expect = false
Ulya Trafimovich55f72d72021-09-01 14:13:57 +0100726 } else {
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100727 // TODO(b/194504107): shouldn't priv-apps be always uncompressed unless
728 // DONT_UNCOMPRESS_PRIV_APPS_DEXS is true (regardless of unbundling)?
Ulya Trafimovich55f72d72021-09-01 14:13:57 +0100729 // expect = true
730 }
731 }
732
733 android.AssertBoolEquals(t, "uncompress dex", expect, actual)
734 }
735
736 for _, unbundled := range []bool{false, true} {
737 for _, dontUncompressPrivAppDexs := range []bool{false, true} {
738 for _, tt := range testCases {
739 name := fmt.Sprintf("%s,unbundled:%t,dontUncompressPrivAppDexs:%t",
740 tt.name, unbundled, dontUncompressPrivAppDexs)
741 t.Run(name, func(t *testing.T) {
742 test(t, tt.bp, unbundled, dontUncompressPrivAppDexs)
743 })
744 }
745 }
746 }
747}
Colin Crossbc2c8a72022-09-14 12:45:42 -0700748
749func TestAppImportMissingCertificateAllowMissingDependencies(t *testing.T) {
750 result := android.GroupFixturePreparers(
751 PrepareForTestWithJavaDefaultModules,
752 android.PrepareForTestWithAllowMissingDependencies,
753 android.PrepareForTestWithAndroidMk,
754 ).RunTestWithBp(t, `
755 android_app_import {
756 name: "foo",
757 apk: "a.apk",
758 certificate: ":missing_certificate",
759 }`)
760
761 foo := result.ModuleForTests("foo", "android_common")
762 fooApk := foo.Output("signed/foo.apk")
763 if fooApk.Rule != android.ErrorRule {
764 t.Fatalf("expected ErrorRule for foo.apk, got %s", fooApk.Rule.String())
765 }
766 android.AssertStringDoesContain(t, "expected error rule message", fooApk.Args["error"], "missing dependencies: missing_certificate\n")
767}