blob: efa52c17871756d578041a2d4292fd53ba2a62d2 [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"
20 "regexp"
21 "strings"
22 "testing"
23
24 "github.com/google/blueprint/proptools"
25
26 "android/soong/android"
27)
28
29func TestAndroidAppImport(t *testing.T) {
30 ctx, _ := testJava(t, `
31 android_app_import {
32 name: "foo",
33 apk: "prebuilts/apk/app.apk",
34 certificate: "platform",
35 dex_preopt: {
36 enabled: true,
37 },
38 }
39 `)
40
41 variant := ctx.ModuleForTests("foo", "android_common")
42
43 // Check dexpreopt outputs.
44 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
45 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
46 t.Errorf("can't find dexpreopt outputs")
47 }
48
49 // Check cert signing flag.
50 signedApk := variant.Output("signed/foo.apk")
51 signingFlag := signedApk.Args["certificates"]
52 expected := "build/make/target/product/security/platform.x509.pem build/make/target/product/security/platform.pk8"
53 if expected != signingFlag {
54 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
55 }
56}
57
58func TestAndroidAppImport_NoDexPreopt(t *testing.T) {
59 ctx, _ := testJava(t, `
60 android_app_import {
61 name: "foo",
62 apk: "prebuilts/apk/app.apk",
63 certificate: "platform",
64 dex_preopt: {
65 enabled: false,
66 },
67 }
68 `)
69
70 variant := ctx.ModuleForTests("foo", "android_common")
71
72 // Check dexpreopt outputs. They shouldn't exist.
73 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule != nil ||
74 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule != nil {
75 t.Errorf("dexpreopt shouldn't have run.")
76 }
77}
78
79func TestAndroidAppImport_Presigned(t *testing.T) {
80 ctx, _ := testJava(t, `
81 android_app_import {
82 name: "foo",
83 apk: "prebuilts/apk/app.apk",
84 presigned: true,
85 dex_preopt: {
86 enabled: true,
87 },
88 }
89 `)
90
91 variant := ctx.ModuleForTests("foo", "android_common")
92
93 // Check dexpreopt outputs.
94 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
95 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
96 t.Errorf("can't find dexpreopt outputs")
97 }
98 // Make sure signing was skipped and aligning was done.
99 if variant.MaybeOutput("signed/foo.apk").Rule != nil {
100 t.Errorf("signing rule shouldn't be included.")
101 }
102 if variant.MaybeOutput("zip-aligned/foo.apk").Rule == nil {
103 t.Errorf("can't find aligning rule")
104 }
105}
106
107func TestAndroidAppImport_SigningLineage(t *testing.T) {
108 ctx, _ := testJava(t, `
109 android_app_import {
110 name: "foo",
111 apk: "prebuilts/apk/app.apk",
112 certificate: "platform",
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800113 additional_certificates: [":additional_certificate"],
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800114 lineage: "lineage.bin",
115 }
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800116
117 android_app_certificate {
118 name: "additional_certificate",
119 certificate: "cert/additional_cert",
120 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800121 `)
122
123 variant := ctx.ModuleForTests("foo", "android_common")
124
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800125 signedApk := variant.Output("signed/foo.apk")
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800126 // Check certificates
127 certificatesFlag := signedApk.Args["certificates"]
128 expected := "build/make/target/product/security/platform.x509.pem " +
129 "build/make/target/product/security/platform.pk8 " +
130 "cert/additional_cert.x509.pem cert/additional_cert.pk8"
131 if expected != certificatesFlag {
132 t.Errorf("Incorrect certificates flags, expected: %q, got: %q", expected, certificatesFlag)
133 }
134 // Check cert signing lineage flag.
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800135 signingFlag := signedApk.Args["flags"]
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800136 expected = "--lineage lineage.bin"
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800137 if expected != signingFlag {
138 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
139 }
140}
141
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800142func TestAndroidAppImport_SigningLineageFilegroup(t *testing.T) {
143 ctx, _ := testJava(t, `
144 android_app_import {
145 name: "foo",
146 apk: "prebuilts/apk/app.apk",
147 certificate: "platform",
148 lineage: ":lineage_bin",
149 }
150
151 filegroup {
152 name: "lineage_bin",
153 srcs: ["lineage.bin"],
154 }
155 `)
156
157 variant := ctx.ModuleForTests("foo", "android_common")
158
159 signedApk := variant.Output("signed/foo.apk")
160 // Check cert signing lineage flag.
161 signingFlag := signedApk.Args["flags"]
162 expected := "--lineage lineage.bin"
163 if expected != signingFlag {
164 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
165 }
166}
167
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800168func TestAndroidAppImport_DefaultDevCert(t *testing.T) {
169 ctx, _ := testJava(t, `
170 android_app_import {
171 name: "foo",
172 apk: "prebuilts/apk/app.apk",
173 default_dev_cert: true,
174 dex_preopt: {
175 enabled: true,
176 },
177 }
178 `)
179
180 variant := ctx.ModuleForTests("foo", "android_common")
181
182 // Check dexpreopt outputs.
183 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
184 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
185 t.Errorf("can't find dexpreopt outputs")
186 }
187
188 // Check cert signing flag.
189 signedApk := variant.Output("signed/foo.apk")
190 signingFlag := signedApk.Args["certificates"]
191 expected := "build/make/target/product/security/testkey.x509.pem build/make/target/product/security/testkey.pk8"
192 if expected != signingFlag {
193 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
194 }
195}
196
197func TestAndroidAppImport_DpiVariants(t *testing.T) {
198 bp := `
199 android_app_import {
200 name: "foo",
201 apk: "prebuilts/apk/app.apk",
202 dpi_variants: {
203 xhdpi: {
204 apk: "prebuilts/apk/app_xhdpi.apk",
205 },
206 xxhdpi: {
207 apk: "prebuilts/apk/app_xxhdpi.apk",
208 },
209 },
210 presigned: true,
211 dex_preopt: {
212 enabled: true,
213 },
214 }
215 `
216 testCases := []struct {
217 name string
218 aaptPreferredConfig *string
219 aaptPrebuiltDPI []string
220 expected string
221 }{
222 {
223 name: "no preferred",
224 aaptPreferredConfig: nil,
225 aaptPrebuiltDPI: []string{},
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000226 expected: "verify_uses_libraries/apk/app.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800227 },
228 {
229 name: "AAPTPreferredConfig matches",
230 aaptPreferredConfig: proptools.StringPtr("xhdpi"),
231 aaptPrebuiltDPI: []string{"xxhdpi", "ldpi"},
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000232 expected: "verify_uses_libraries/apk/app_xhdpi.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800233 },
234 {
235 name: "AAPTPrebuiltDPI matches",
236 aaptPreferredConfig: proptools.StringPtr("mdpi"),
237 aaptPrebuiltDPI: []string{"xxhdpi", "xhdpi"},
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000238 expected: "verify_uses_libraries/apk/app_xxhdpi.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800239 },
240 {
241 name: "non-first AAPTPrebuiltDPI matches",
242 aaptPreferredConfig: proptools.StringPtr("mdpi"),
243 aaptPrebuiltDPI: []string{"ldpi", "xhdpi"},
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000244 expected: "verify_uses_libraries/apk/app_xhdpi.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800245 },
246 {
247 name: "no matches",
248 aaptPreferredConfig: proptools.StringPtr("mdpi"),
249 aaptPrebuiltDPI: []string{"ldpi", "xxxhdpi"},
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000250 expected: "verify_uses_libraries/apk/app.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800251 },
252 }
253
254 jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)")
255 for _, test := range testCases {
Paul Duffinfb8bc952021-03-22 17:31:52 +0000256 result := android.GroupFixturePreparers(
257 PrepareForTestWithJavaDefaultModules,
258 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
259 variables.AAPTPreferredConfig = test.aaptPreferredConfig
260 variables.AAPTPrebuiltDPI = test.aaptPrebuiltDPI
261 }),
262 ).RunTestWithBp(t, bp)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800263
Paul Duffinfb8bc952021-03-22 17:31:52 +0000264 variant := result.ModuleForTests("foo", "android_common")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800265 jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command
266 matches := jniRuleRe.FindStringSubmatch(jniRuleCommand)
267 if len(matches) != 2 {
268 t.Errorf("failed to extract the src apk path from %q", jniRuleCommand)
269 }
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000270 if strings.HasSuffix(matches[1], test.expected) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800271 t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1])
272 }
273 }
274}
275
276func TestAndroidAppImport_Filename(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700277 ctx, _ := testJava(t, `
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800278 android_app_import {
279 name: "foo",
280 apk: "prebuilts/apk/app.apk",
281 presigned: true,
282 }
283
284 android_app_import {
285 name: "bar",
286 apk: "prebuilts/apk/app.apk",
287 presigned: true,
288 filename: "bar_sample.apk"
289 }
290 `)
291
292 testCases := []struct {
293 name string
294 expected string
295 }{
296 {
297 name: "foo",
298 expected: "foo.apk",
299 },
300 {
301 name: "bar",
302 expected: "bar_sample.apk",
303 },
304 }
305
306 for _, test := range testCases {
307 variant := ctx.ModuleForTests(test.name, "android_common")
308 if variant.MaybeOutput(test.expected).Rule == nil {
309 t.Errorf("can't find output named %q - all outputs: %v", test.expected, variant.AllOutputs())
310 }
311
312 a := variant.Module().(*AndroidAppImport)
313 expectedValues := []string{test.expected}
Colin Crossaa255532020-07-03 13:18:24 -0700314 actualValues := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_INSTALLED_MODULE_STEM"]
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800315 if !reflect.DeepEqual(actualValues, expectedValues) {
316 t.Errorf("Incorrect LOCAL_INSTALLED_MODULE_STEM value '%s', expected '%s'",
317 actualValues, expectedValues)
318 }
319 }
320}
321
322func TestAndroidAppImport_ArchVariants(t *testing.T) {
323 // The test config's target arch is ARM64.
324 testCases := []struct {
325 name string
326 bp string
327 expected string
328 }{
329 {
330 name: "matching arch",
331 bp: `
332 android_app_import {
333 name: "foo",
334 apk: "prebuilts/apk/app.apk",
335 arch: {
336 arm64: {
337 apk: "prebuilts/apk/app_arm64.apk",
338 },
339 },
340 presigned: true,
341 dex_preopt: {
342 enabled: true,
343 },
344 }
345 `,
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000346 expected: "verify_uses_libraries/apk/app_arm64.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800347 },
348 {
349 name: "no matching arch",
350 bp: `
351 android_app_import {
352 name: "foo",
353 apk: "prebuilts/apk/app.apk",
354 arch: {
355 arm: {
356 apk: "prebuilts/apk/app_arm.apk",
357 },
358 },
359 presigned: true,
360 dex_preopt: {
361 enabled: true,
362 },
363 }
364 `,
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000365 expected: "verify_uses_libraries/apk/app.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800366 },
367 {
368 name: "no matching arch without default",
369 bp: `
370 android_app_import {
371 name: "foo",
372 arch: {
373 arm: {
374 apk: "prebuilts/apk/app_arm.apk",
375 },
376 },
377 presigned: true,
378 dex_preopt: {
379 enabled: true,
380 },
381 }
382 `,
383 expected: "",
384 },
385 }
386
387 jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)")
388 for _, test := range testCases {
389 ctx, _ := testJava(t, test.bp)
390
391 variant := ctx.ModuleForTests("foo", "android_common")
392 if test.expected == "" {
393 if variant.Module().Enabled() {
394 t.Error("module should have been disabled, but wasn't")
395 }
396 continue
397 }
398 jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command
399 matches := jniRuleRe.FindStringSubmatch(jniRuleCommand)
400 if len(matches) != 2 {
401 t.Errorf("failed to extract the src apk path from %q", jniRuleCommand)
402 }
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000403 if strings.HasSuffix(matches[1], test.expected) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800404 t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1])
405 }
406 }
407}
408
409func TestAndroidAppImport_overridesDisabledAndroidApp(t *testing.T) {
410 ctx, _ := testJava(t, `
411 android_app {
412 name: "foo",
413 srcs: ["a.java"],
414 enabled: false,
415 }
416
417 android_app_import {
418 name: "foo",
419 apk: "prebuilts/apk/app.apk",
420 certificate: "platform",
421 prefer: true,
422 }
423 `)
424
425 variant := ctx.ModuleForTests("prebuilt_foo", "android_common")
426 a := variant.Module().(*AndroidAppImport)
427 // The prebuilt module should still be enabled and active even if the source-based counterpart
428 // is disabled.
429 if !a.prebuilt.UsePrebuilt() {
430 t.Errorf("prebuilt foo module is not active")
431 }
432 if !a.Enabled() {
433 t.Errorf("prebuilt foo module is disabled")
434 }
435}
436
Bill Peckhama036da92021-01-08 16:09:09 -0800437func TestAndroidAppImport_frameworkRes(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700438 ctx, _ := testJava(t, `
Bill Peckhama036da92021-01-08 16:09:09 -0800439 android_app_import {
440 name: "framework-res",
441 certificate: "platform",
442 apk: "package-res.apk",
443 prefer: true,
444 export_package_resources: true,
445 // Disable dexpreopt and verify_uses_libraries check as the app
446 // contains no Java code to be dexpreopted.
447 enforce_uses_libs: false,
448 dex_preopt: {
449 enabled: false,
450 },
451 }
452 `)
453
454 mod := ctx.ModuleForTests("prebuilt_framework-res", "android_common").Module()
455 a := mod.(*AndroidAppImport)
456
457 if !a.preprocessed {
458 t.Errorf("prebuilt framework-res is not preprocessed")
459 }
460
Paul Duffinfb8bc952021-03-22 17:31:52 +0000461 expectedInstallPath := "out/soong/target/product/test_device/system/framework/framework-res.apk"
Bill Peckhama036da92021-01-08 16:09:09 -0800462
Paul Duffinfb8bc952021-03-22 17:31:52 +0000463 android.AssertPathRelativeToTopEquals(t, "prebuilt framework-res install location", expectedInstallPath, a.dexpreopter.installPath)
Bill Peckhama036da92021-01-08 16:09:09 -0800464
Colin Crossaa255532020-07-03 13:18:24 -0700465 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Bill Peckhama036da92021-01-08 16:09:09 -0800466
467 expectedPath := "."
468 // From apk property above, in the root of the source tree.
469 expectedPrebuiltModuleFile := "package-res.apk"
470 // Verify that the apk is preprocessed: The export package is the same
471 // as the prebuilt.
472 expectedSoongResourceExportPackage := expectedPrebuiltModuleFile
473
474 actualPath := entries.EntryMap["LOCAL_PATH"]
475 actualPrebuiltModuleFile := entries.EntryMap["LOCAL_PREBUILT_MODULE_FILE"]
476 actualSoongResourceExportPackage := entries.EntryMap["LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE"]
477
478 if len(actualPath) != 1 {
479 t.Errorf("LOCAL_PATH incorrect len %d", len(actualPath))
480 } else if actualPath[0] != expectedPath {
481 t.Errorf("LOCAL_PATH mismatch, actual: %s, expected: %s", actualPath[0], expectedPath)
482 }
483
484 if len(actualPrebuiltModuleFile) != 1 {
485 t.Errorf("LOCAL_PREBUILT_MODULE_FILE incorrect len %d", len(actualPrebuiltModuleFile))
486 } else if actualPrebuiltModuleFile[0] != expectedPrebuiltModuleFile {
487 t.Errorf("LOCAL_PREBUILT_MODULE_FILE mismatch, actual: %s, expected: %s", actualPrebuiltModuleFile[0], expectedPrebuiltModuleFile)
488 }
489
490 if len(actualSoongResourceExportPackage) != 1 {
491 t.Errorf("LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE incorrect len %d", len(actualSoongResourceExportPackage))
492 } else if actualSoongResourceExportPackage[0] != expectedSoongResourceExportPackage {
493 t.Errorf("LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE mismatch, actual: %s, expected: %s", actualSoongResourceExportPackage[0], expectedSoongResourceExportPackage)
494 }
495}
496
Spandan Dasd1fac642021-05-18 17:01:41 +0000497func TestAndroidAppImport_relativeInstallPath(t *testing.T) {
498 bp := `
499 android_app_import {
500 name: "no_relative_install_path",
501 apk: "prebuilts/apk/app.apk",
502 presigned: true,
503 }
504
505 android_app_import {
506 name: "relative_install_path",
507 apk: "prebuilts/apk/app.apk",
508 presigned: true,
509 relative_install_path: "my/path",
510 }
511
512 android_app_import {
513 name: "framework-res",
514 apk: "prebuilts/apk/app.apk",
515 presigned: true,
516 prefer: true,
517 }
518
519 android_app_import {
520 name: "privileged_relative_install_path",
521 apk: "prebuilts/apk/app.apk",
522 presigned: true,
523 privileged: true,
524 relative_install_path: "my/path"
525 }
526 `
527 testCases := []struct {
528 name string
529 expectedInstallPath string
530 errorMessage string
531 }{
532 {
533 name: "no_relative_install_path",
534 expectedInstallPath: "out/soong/target/product/test_device/system/app/no_relative_install_path/no_relative_install_path.apk",
535 errorMessage: "Install path is not correct when relative_install_path is missing",
536 },
537 {
538 name: "relative_install_path",
539 expectedInstallPath: "out/soong/target/product/test_device/system/app/my/path/relative_install_path/relative_install_path.apk",
540 errorMessage: "Install path is not correct for app when relative_install_path is present",
541 },
542 {
543 name: "prebuilt_framework-res",
544 expectedInstallPath: "out/soong/target/product/test_device/system/framework/framework-res.apk",
545 errorMessage: "Install path is not correct for framework-res",
546 },
547 {
548 name: "privileged_relative_install_path",
549 expectedInstallPath: "out/soong/target/product/test_device/system/priv-app/my/path/privileged_relative_install_path/privileged_relative_install_path.apk",
550 errorMessage: "Install path is not correct for privileged app when relative_install_path is present",
551 },
552 }
553 for _, testCase := range testCases {
554 ctx, _ := testJava(t, bp)
555 mod := ctx.ModuleForTests(testCase.name, "android_common").Module().(*AndroidAppImport)
556 android.AssertPathRelativeToTopEquals(t, testCase.errorMessage, testCase.expectedInstallPath, mod.installPath)
557 }
558}
559
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800560func TestAndroidTestImport(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700561 ctx, _ := testJava(t, `
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800562 android_test_import {
563 name: "foo",
564 apk: "prebuilts/apk/app.apk",
565 presigned: true,
566 data: [
567 "testdata/data",
568 ],
569 }
570 `)
571
572 test := ctx.ModuleForTests("foo", "android_common").Module().(*AndroidTestImport)
573
574 // Check android mks.
Colin Crossaa255532020-07-03 13:18:24 -0700575 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800576 expected := []string{"tests"}
577 actual := entries.EntryMap["LOCAL_MODULE_TAGS"]
578 if !reflect.DeepEqual(expected, actual) {
579 t.Errorf("Unexpected module tags - expected: %q, actual: %q", expected, actual)
580 }
581 expected = []string{"testdata/data:testdata/data"}
582 actual = entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
583 if !reflect.DeepEqual(expected, actual) {
584 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
585 }
586}
587
588func TestAndroidTestImport_NoJinUncompressForPresigned(t *testing.T) {
589 ctx, _ := testJava(t, `
590 android_test_import {
591 name: "foo",
592 apk: "prebuilts/apk/app.apk",
593 certificate: "cert/new_cert",
594 data: [
595 "testdata/data",
596 ],
597 }
598
599 android_test_import {
600 name: "foo_presigned",
601 apk: "prebuilts/apk/app.apk",
602 presigned: true,
603 data: [
604 "testdata/data",
605 ],
606 }
607 `)
608
609 variant := ctx.ModuleForTests("foo", "android_common")
610 jniRule := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command
611 if !strings.HasPrefix(jniRule, "if (zipinfo") {
612 t.Errorf("Unexpected JNI uncompress rule command: " + jniRule)
613 }
614
615 variant = ctx.ModuleForTests("foo_presigned", "android_common")
616 jniRule = variant.Output("jnis-uncompressed/foo_presigned.apk").BuildParams.Rule.String()
617 if jniRule != android.Cp.String() {
618 t.Errorf("Unexpected JNI uncompress rule: " + jniRule)
619 }
620 if variant.MaybeOutput("zip-aligned/foo_presigned.apk").Rule == nil {
621 t.Errorf("Presigned test apk should be aligned")
622 }
623}
624
625func TestAndroidTestImport_Preprocessed(t *testing.T) {
626 ctx, _ := testJava(t, `
627 android_test_import {
628 name: "foo",
629 apk: "prebuilts/apk/app.apk",
630 presigned: true,
631 preprocessed: true,
632 }
633
634 android_test_import {
635 name: "foo_cert",
636 apk: "prebuilts/apk/app.apk",
637 certificate: "cert/new_cert",
638 preprocessed: true,
639 }
640 `)
641
642 testModules := []string{"foo", "foo_cert"}
643 for _, m := range testModules {
644 apkName := m + ".apk"
645 variant := ctx.ModuleForTests(m, "android_common")
646 jniRule := variant.Output("jnis-uncompressed/" + apkName).BuildParams.Rule.String()
647 if jniRule != android.Cp.String() {
648 t.Errorf("Unexpected JNI uncompress rule: " + jniRule)
649 }
650
651 // Make sure signing and aligning were skipped.
652 if variant.MaybeOutput("signed/"+apkName).Rule != nil {
653 t.Errorf("signing rule shouldn't be included for preprocessed.")
654 }
655 if variant.MaybeOutput("zip-aligned/"+apkName).Rule != nil {
656 t.Errorf("aligning rule shouldn't be for preprocessed")
657 }
658 }
659}
Ulya Trafimovich55f72d72021-09-01 14:13:57 +0100660
661func TestAndroidTestImport_UncompressDex(t *testing.T) {
662 testCases := []struct {
663 name string
664 bp string
665 }{
666 {
667 name: "normal",
668 bp: `
669 android_app_import {
670 name: "foo",
671 presigned: true,
672 apk: "prebuilts/apk/app.apk",
673 }
674 `,
675 },
676 {
677 name: "privileged",
678 bp: `
679 android_app_import {
680 name: "foo",
681 presigned: true,
682 privileged: true,
683 apk: "prebuilts/apk/app.apk",
684 }
685 `,
686 },
687 }
688
689 test := func(t *testing.T, bp string, unbundled bool, dontUncompressPrivAppDexs bool) {
690 t.Helper()
691
692 result := android.GroupFixturePreparers(
693 prepareForJavaTest,
694 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
695 if unbundled {
696 variables.Unbundled_build = proptools.BoolPtr(true)
697 }
698 variables.UncompressPrivAppDex = proptools.BoolPtr(!dontUncompressPrivAppDexs)
699 }),
700 ).RunTestWithBp(t, bp)
701
702 foo := result.ModuleForTests("foo", "android_common")
703 actual := foo.MaybeRule("uncompress-dex").Rule != nil
704
705 expect := !unbundled
706 if strings.Contains(bp, "privileged: true") {
707 if dontUncompressPrivAppDexs {
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100708 expect = false
Ulya Trafimovich55f72d72021-09-01 14:13:57 +0100709 } else {
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100710 // TODO(b/194504107): shouldn't priv-apps be always uncompressed unless
711 // DONT_UNCOMPRESS_PRIV_APPS_DEXS is true (regardless of unbundling)?
Ulya Trafimovich55f72d72021-09-01 14:13:57 +0100712 // expect = true
713 }
714 }
715
716 android.AssertBoolEquals(t, "uncompress dex", expect, actual)
717 }
718
719 for _, unbundled := range []bool{false, true} {
720 for _, dontUncompressPrivAppDexs := range []bool{false, true} {
721 for _, tt := range testCases {
722 name := fmt.Sprintf("%s,unbundled:%t,dontUncompressPrivAppDexs:%t",
723 tt.name, unbundled, dontUncompressPrivAppDexs)
724 t.Run(name, func(t *testing.T) {
725 test(t, tt.bp, unbundled, dontUncompressPrivAppDexs)
726 })
727 }
728 }
729 }
730}