blob: 55d24c4b84729fc218e33f24b890704d0b2d3145 [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",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400115 rotationMinSdkVersion: "32",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800116 }
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800117
118 android_app_certificate {
119 name: "additional_certificate",
120 certificate: "cert/additional_cert",
121 }
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800122 `)
123
124 variant := ctx.ModuleForTests("foo", "android_common")
125
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800126 signedApk := variant.Output("signed/foo.apk")
Jaewoong Jung25ae8de2021-03-08 17:37:46 -0800127 // Check certificates
128 certificatesFlag := signedApk.Args["certificates"]
129 expected := "build/make/target/product/security/platform.x509.pem " +
130 "build/make/target/product/security/platform.pk8 " +
131 "cert/additional_cert.x509.pem cert/additional_cert.pk8"
132 if expected != certificatesFlag {
133 t.Errorf("Incorrect certificates flags, expected: %q, got: %q", expected, certificatesFlag)
134 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400135
136 // Check cert signing flags.
137 actualCertSigningFlags := signedApk.Args["flags"]
138 expectedCertSigningFlags := "--lineage lineage.bin --rotation-min-sdk-version 32"
139 if expectedCertSigningFlags != actualCertSigningFlags {
140 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expectedCertSigningFlags, actualCertSigningFlags)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800141 }
142}
143
Jaewoong Jung1c1b6e62021-03-09 15:02:31 -0800144func TestAndroidAppImport_SigningLineageFilegroup(t *testing.T) {
145 ctx, _ := testJava(t, `
146 android_app_import {
147 name: "foo",
148 apk: "prebuilts/apk/app.apk",
149 certificate: "platform",
150 lineage: ":lineage_bin",
151 }
152
153 filegroup {
154 name: "lineage_bin",
155 srcs: ["lineage.bin"],
156 }
157 `)
158
159 variant := ctx.ModuleForTests("foo", "android_common")
160
161 signedApk := variant.Output("signed/foo.apk")
162 // Check cert signing lineage flag.
163 signingFlag := signedApk.Args["flags"]
164 expected := "--lineage lineage.bin"
165 if expected != signingFlag {
166 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
167 }
168}
169
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800170func TestAndroidAppImport_DefaultDevCert(t *testing.T) {
171 ctx, _ := testJava(t, `
172 android_app_import {
173 name: "foo",
174 apk: "prebuilts/apk/app.apk",
175 default_dev_cert: true,
176 dex_preopt: {
177 enabled: true,
178 },
179 }
180 `)
181
182 variant := ctx.ModuleForTests("foo", "android_common")
183
184 // Check dexpreopt outputs.
185 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
186 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
187 t.Errorf("can't find dexpreopt outputs")
188 }
189
190 // Check cert signing flag.
191 signedApk := variant.Output("signed/foo.apk")
192 signingFlag := signedApk.Args["certificates"]
193 expected := "build/make/target/product/security/testkey.x509.pem build/make/target/product/security/testkey.pk8"
194 if expected != signingFlag {
195 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
196 }
197}
198
199func TestAndroidAppImport_DpiVariants(t *testing.T) {
200 bp := `
201 android_app_import {
202 name: "foo",
203 apk: "prebuilts/apk/app.apk",
204 dpi_variants: {
205 xhdpi: {
206 apk: "prebuilts/apk/app_xhdpi.apk",
207 },
208 xxhdpi: {
209 apk: "prebuilts/apk/app_xxhdpi.apk",
210 },
211 },
212 presigned: true,
213 dex_preopt: {
214 enabled: true,
215 },
216 }
217 `
218 testCases := []struct {
219 name string
220 aaptPreferredConfig *string
221 aaptPrebuiltDPI []string
222 expected string
223 }{
224 {
225 name: "no preferred",
226 aaptPreferredConfig: nil,
227 aaptPrebuiltDPI: []string{},
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000228 expected: "verify_uses_libraries/apk/app.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800229 },
230 {
231 name: "AAPTPreferredConfig matches",
232 aaptPreferredConfig: proptools.StringPtr("xhdpi"),
233 aaptPrebuiltDPI: []string{"xxhdpi", "ldpi"},
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000234 expected: "verify_uses_libraries/apk/app_xhdpi.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800235 },
236 {
237 name: "AAPTPrebuiltDPI matches",
238 aaptPreferredConfig: proptools.StringPtr("mdpi"),
239 aaptPrebuiltDPI: []string{"xxhdpi", "xhdpi"},
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000240 expected: "verify_uses_libraries/apk/app_xxhdpi.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800241 },
242 {
243 name: "non-first AAPTPrebuiltDPI matches",
244 aaptPreferredConfig: proptools.StringPtr("mdpi"),
245 aaptPrebuiltDPI: []string{"ldpi", "xhdpi"},
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000246 expected: "verify_uses_libraries/apk/app_xhdpi.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800247 },
248 {
249 name: "no matches",
250 aaptPreferredConfig: proptools.StringPtr("mdpi"),
251 aaptPrebuiltDPI: []string{"ldpi", "xxxhdpi"},
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000252 expected: "verify_uses_libraries/apk/app.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800253 },
254 }
255
256 jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)")
257 for _, test := range testCases {
Paul Duffinfb8bc952021-03-22 17:31:52 +0000258 result := android.GroupFixturePreparers(
259 PrepareForTestWithJavaDefaultModules,
260 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
261 variables.AAPTPreferredConfig = test.aaptPreferredConfig
262 variables.AAPTPrebuiltDPI = test.aaptPrebuiltDPI
263 }),
264 ).RunTestWithBp(t, bp)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800265
Paul Duffinfb8bc952021-03-22 17:31:52 +0000266 variant := result.ModuleForTests("foo", "android_common")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800267 jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command
268 matches := jniRuleRe.FindStringSubmatch(jniRuleCommand)
269 if len(matches) != 2 {
270 t.Errorf("failed to extract the src apk path from %q", jniRuleCommand)
271 }
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000272 if strings.HasSuffix(matches[1], test.expected) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800273 t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1])
274 }
275 }
276}
277
278func TestAndroidAppImport_Filename(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700279 ctx, _ := testJava(t, `
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800280 android_app_import {
281 name: "foo",
282 apk: "prebuilts/apk/app.apk",
283 presigned: true,
284 }
285
286 android_app_import {
287 name: "bar",
288 apk: "prebuilts/apk/app.apk",
289 presigned: true,
290 filename: "bar_sample.apk"
291 }
292 `)
293
294 testCases := []struct {
295 name string
296 expected string
297 }{
298 {
299 name: "foo",
300 expected: "foo.apk",
301 },
302 {
303 name: "bar",
304 expected: "bar_sample.apk",
305 },
306 }
307
308 for _, test := range testCases {
309 variant := ctx.ModuleForTests(test.name, "android_common")
310 if variant.MaybeOutput(test.expected).Rule == nil {
311 t.Errorf("can't find output named %q - all outputs: %v", test.expected, variant.AllOutputs())
312 }
313
314 a := variant.Module().(*AndroidAppImport)
315 expectedValues := []string{test.expected}
Colin Crossaa255532020-07-03 13:18:24 -0700316 actualValues := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_INSTALLED_MODULE_STEM"]
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800317 if !reflect.DeepEqual(actualValues, expectedValues) {
318 t.Errorf("Incorrect LOCAL_INSTALLED_MODULE_STEM value '%s', expected '%s'",
319 actualValues, expectedValues)
320 }
321 }
322}
323
324func TestAndroidAppImport_ArchVariants(t *testing.T) {
325 // The test config's target arch is ARM64.
326 testCases := []struct {
327 name string
328 bp string
329 expected string
330 }{
331 {
332 name: "matching arch",
333 bp: `
334 android_app_import {
335 name: "foo",
336 apk: "prebuilts/apk/app.apk",
337 arch: {
338 arm64: {
339 apk: "prebuilts/apk/app_arm64.apk",
340 },
341 },
342 presigned: true,
343 dex_preopt: {
344 enabled: true,
345 },
346 }
347 `,
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000348 expected: "verify_uses_libraries/apk/app_arm64.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800349 },
350 {
351 name: "no matching arch",
352 bp: `
353 android_app_import {
354 name: "foo",
355 apk: "prebuilts/apk/app.apk",
356 arch: {
357 arm: {
358 apk: "prebuilts/apk/app_arm.apk",
359 },
360 },
361 presigned: true,
362 dex_preopt: {
363 enabled: true,
364 },
365 }
366 `,
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000367 expected: "verify_uses_libraries/apk/app.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800368 },
369 {
370 name: "no matching arch without default",
371 bp: `
372 android_app_import {
373 name: "foo",
374 arch: {
375 arm: {
376 apk: "prebuilts/apk/app_arm.apk",
377 },
378 },
379 presigned: true,
380 dex_preopt: {
381 enabled: true,
382 },
383 }
384 `,
385 expected: "",
386 },
387 }
388
389 jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)")
390 for _, test := range testCases {
391 ctx, _ := testJava(t, test.bp)
392
393 variant := ctx.ModuleForTests("foo", "android_common")
394 if test.expected == "" {
395 if variant.Module().Enabled() {
396 t.Error("module should have been disabled, but wasn't")
397 }
398 continue
399 }
400 jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command
401 matches := jniRuleRe.FindStringSubmatch(jniRuleCommand)
402 if len(matches) != 2 {
403 t.Errorf("failed to extract the src apk path from %q", jniRuleCommand)
404 }
Ulya Trafimovich22890c42021-01-05 12:04:17 +0000405 if strings.HasSuffix(matches[1], test.expected) {
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800406 t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1])
407 }
408 }
409}
410
411func TestAndroidAppImport_overridesDisabledAndroidApp(t *testing.T) {
412 ctx, _ := testJava(t, `
413 android_app {
414 name: "foo",
415 srcs: ["a.java"],
416 enabled: false,
417 }
418
419 android_app_import {
420 name: "foo",
421 apk: "prebuilts/apk/app.apk",
422 certificate: "platform",
423 prefer: true,
424 }
425 `)
426
427 variant := ctx.ModuleForTests("prebuilt_foo", "android_common")
428 a := variant.Module().(*AndroidAppImport)
429 // The prebuilt module should still be enabled and active even if the source-based counterpart
430 // is disabled.
431 if !a.prebuilt.UsePrebuilt() {
432 t.Errorf("prebuilt foo module is not active")
433 }
434 if !a.Enabled() {
435 t.Errorf("prebuilt foo module is disabled")
436 }
437}
438
Bill Peckhama036da92021-01-08 16:09:09 -0800439func TestAndroidAppImport_frameworkRes(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700440 ctx, _ := testJava(t, `
Bill Peckhama036da92021-01-08 16:09:09 -0800441 android_app_import {
442 name: "framework-res",
443 certificate: "platform",
444 apk: "package-res.apk",
445 prefer: true,
446 export_package_resources: true,
447 // Disable dexpreopt and verify_uses_libraries check as the app
448 // contains no Java code to be dexpreopted.
449 enforce_uses_libs: false,
450 dex_preopt: {
451 enabled: false,
452 },
453 }
454 `)
455
456 mod := ctx.ModuleForTests("prebuilt_framework-res", "android_common").Module()
457 a := mod.(*AndroidAppImport)
458
459 if !a.preprocessed {
460 t.Errorf("prebuilt framework-res is not preprocessed")
461 }
462
Paul Duffinfb8bc952021-03-22 17:31:52 +0000463 expectedInstallPath := "out/soong/target/product/test_device/system/framework/framework-res.apk"
Bill Peckhama036da92021-01-08 16:09:09 -0800464
Paul Duffinfb8bc952021-03-22 17:31:52 +0000465 android.AssertPathRelativeToTopEquals(t, "prebuilt framework-res install location", expectedInstallPath, a.dexpreopter.installPath)
Bill Peckhama036da92021-01-08 16:09:09 -0800466
Colin Crossaa255532020-07-03 13:18:24 -0700467 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Bill Peckhama036da92021-01-08 16:09:09 -0800468
469 expectedPath := "."
470 // From apk property above, in the root of the source tree.
471 expectedPrebuiltModuleFile := "package-res.apk"
472 // Verify that the apk is preprocessed: The export package is the same
473 // as the prebuilt.
474 expectedSoongResourceExportPackage := expectedPrebuiltModuleFile
475
476 actualPath := entries.EntryMap["LOCAL_PATH"]
477 actualPrebuiltModuleFile := entries.EntryMap["LOCAL_PREBUILT_MODULE_FILE"]
478 actualSoongResourceExportPackage := entries.EntryMap["LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE"]
479
480 if len(actualPath) != 1 {
481 t.Errorf("LOCAL_PATH incorrect len %d", len(actualPath))
482 } else if actualPath[0] != expectedPath {
483 t.Errorf("LOCAL_PATH mismatch, actual: %s, expected: %s", actualPath[0], expectedPath)
484 }
485
486 if len(actualPrebuiltModuleFile) != 1 {
487 t.Errorf("LOCAL_PREBUILT_MODULE_FILE incorrect len %d", len(actualPrebuiltModuleFile))
488 } else if actualPrebuiltModuleFile[0] != expectedPrebuiltModuleFile {
489 t.Errorf("LOCAL_PREBUILT_MODULE_FILE mismatch, actual: %s, expected: %s", actualPrebuiltModuleFile[0], expectedPrebuiltModuleFile)
490 }
491
492 if len(actualSoongResourceExportPackage) != 1 {
493 t.Errorf("LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE incorrect len %d", len(actualSoongResourceExportPackage))
494 } else if actualSoongResourceExportPackage[0] != expectedSoongResourceExportPackage {
495 t.Errorf("LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE mismatch, actual: %s, expected: %s", actualSoongResourceExportPackage[0], expectedSoongResourceExportPackage)
496 }
497}
498
Spandan Dasd1fac642021-05-18 17:01:41 +0000499func TestAndroidAppImport_relativeInstallPath(t *testing.T) {
500 bp := `
501 android_app_import {
502 name: "no_relative_install_path",
503 apk: "prebuilts/apk/app.apk",
504 presigned: true,
505 }
506
507 android_app_import {
508 name: "relative_install_path",
509 apk: "prebuilts/apk/app.apk",
510 presigned: true,
511 relative_install_path: "my/path",
512 }
513
514 android_app_import {
515 name: "framework-res",
516 apk: "prebuilts/apk/app.apk",
517 presigned: true,
518 prefer: true,
519 }
520
521 android_app_import {
522 name: "privileged_relative_install_path",
523 apk: "prebuilts/apk/app.apk",
524 presigned: true,
525 privileged: true,
526 relative_install_path: "my/path"
527 }
528 `
529 testCases := []struct {
530 name string
531 expectedInstallPath string
532 errorMessage string
533 }{
534 {
535 name: "no_relative_install_path",
536 expectedInstallPath: "out/soong/target/product/test_device/system/app/no_relative_install_path/no_relative_install_path.apk",
537 errorMessage: "Install path is not correct when relative_install_path is missing",
538 },
539 {
540 name: "relative_install_path",
541 expectedInstallPath: "out/soong/target/product/test_device/system/app/my/path/relative_install_path/relative_install_path.apk",
542 errorMessage: "Install path is not correct for app when relative_install_path is present",
543 },
544 {
545 name: "prebuilt_framework-res",
546 expectedInstallPath: "out/soong/target/product/test_device/system/framework/framework-res.apk",
547 errorMessage: "Install path is not correct for framework-res",
548 },
549 {
550 name: "privileged_relative_install_path",
551 expectedInstallPath: "out/soong/target/product/test_device/system/priv-app/my/path/privileged_relative_install_path/privileged_relative_install_path.apk",
552 errorMessage: "Install path is not correct for privileged app when relative_install_path is present",
553 },
554 }
555 for _, testCase := range testCases {
556 ctx, _ := testJava(t, bp)
557 mod := ctx.ModuleForTests(testCase.name, "android_common").Module().(*AndroidAppImport)
558 android.AssertPathRelativeToTopEquals(t, testCase.errorMessage, testCase.expectedInstallPath, mod.installPath)
559 }
560}
561
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800562func TestAndroidTestImport(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700563 ctx, _ := testJava(t, `
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800564 android_test_import {
565 name: "foo",
566 apk: "prebuilts/apk/app.apk",
567 presigned: true,
568 data: [
569 "testdata/data",
570 ],
571 }
572 `)
573
574 test := ctx.ModuleForTests("foo", "android_common").Module().(*AndroidTestImport)
575
576 // Check android mks.
Colin Crossaa255532020-07-03 13:18:24 -0700577 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800578 expected := []string{"tests"}
579 actual := entries.EntryMap["LOCAL_MODULE_TAGS"]
580 if !reflect.DeepEqual(expected, actual) {
581 t.Errorf("Unexpected module tags - expected: %q, actual: %q", expected, actual)
582 }
583 expected = []string{"testdata/data:testdata/data"}
584 actual = entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
585 if !reflect.DeepEqual(expected, actual) {
586 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
587 }
588}
589
590func TestAndroidTestImport_NoJinUncompressForPresigned(t *testing.T) {
591 ctx, _ := testJava(t, `
592 android_test_import {
593 name: "foo",
594 apk: "prebuilts/apk/app.apk",
595 certificate: "cert/new_cert",
596 data: [
597 "testdata/data",
598 ],
599 }
600
601 android_test_import {
602 name: "foo_presigned",
603 apk: "prebuilts/apk/app.apk",
604 presigned: true,
605 data: [
606 "testdata/data",
607 ],
608 }
609 `)
610
611 variant := ctx.ModuleForTests("foo", "android_common")
612 jniRule := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command
613 if !strings.HasPrefix(jniRule, "if (zipinfo") {
614 t.Errorf("Unexpected JNI uncompress rule command: " + jniRule)
615 }
616
617 variant = ctx.ModuleForTests("foo_presigned", "android_common")
618 jniRule = variant.Output("jnis-uncompressed/foo_presigned.apk").BuildParams.Rule.String()
619 if jniRule != android.Cp.String() {
620 t.Errorf("Unexpected JNI uncompress rule: " + jniRule)
621 }
622 if variant.MaybeOutput("zip-aligned/foo_presigned.apk").Rule == nil {
623 t.Errorf("Presigned test apk should be aligned")
624 }
625}
626
627func TestAndroidTestImport_Preprocessed(t *testing.T) {
628 ctx, _ := testJava(t, `
629 android_test_import {
630 name: "foo",
631 apk: "prebuilts/apk/app.apk",
632 presigned: true,
633 preprocessed: true,
634 }
635
636 android_test_import {
637 name: "foo_cert",
638 apk: "prebuilts/apk/app.apk",
639 certificate: "cert/new_cert",
640 preprocessed: true,
641 }
642 `)
643
644 testModules := []string{"foo", "foo_cert"}
645 for _, m := range testModules {
646 apkName := m + ".apk"
647 variant := ctx.ModuleForTests(m, "android_common")
648 jniRule := variant.Output("jnis-uncompressed/" + apkName).BuildParams.Rule.String()
649 if jniRule != android.Cp.String() {
650 t.Errorf("Unexpected JNI uncompress rule: " + jniRule)
651 }
652
653 // Make sure signing and aligning were skipped.
654 if variant.MaybeOutput("signed/"+apkName).Rule != nil {
655 t.Errorf("signing rule shouldn't be included for preprocessed.")
656 }
657 if variant.MaybeOutput("zip-aligned/"+apkName).Rule != nil {
658 t.Errorf("aligning rule shouldn't be for preprocessed")
659 }
660 }
661}
Ulya Trafimovich55f72d72021-09-01 14:13:57 +0100662
663func TestAndroidTestImport_UncompressDex(t *testing.T) {
664 testCases := []struct {
665 name string
666 bp string
667 }{
668 {
669 name: "normal",
670 bp: `
671 android_app_import {
672 name: "foo",
673 presigned: true,
674 apk: "prebuilts/apk/app.apk",
675 }
676 `,
677 },
678 {
679 name: "privileged",
680 bp: `
681 android_app_import {
682 name: "foo",
683 presigned: true,
684 privileged: true,
685 apk: "prebuilts/apk/app.apk",
686 }
687 `,
688 },
689 }
690
691 test := func(t *testing.T, bp string, unbundled bool, dontUncompressPrivAppDexs bool) {
692 t.Helper()
693
694 result := android.GroupFixturePreparers(
695 prepareForJavaTest,
696 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
697 if unbundled {
698 variables.Unbundled_build = proptools.BoolPtr(true)
699 }
700 variables.UncompressPrivAppDex = proptools.BoolPtr(!dontUncompressPrivAppDexs)
701 }),
702 ).RunTestWithBp(t, bp)
703
704 foo := result.ModuleForTests("foo", "android_common")
705 actual := foo.MaybeRule("uncompress-dex").Rule != nil
706
707 expect := !unbundled
708 if strings.Contains(bp, "privileged: true") {
709 if dontUncompressPrivAppDexs {
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100710 expect = false
Ulya Trafimovich55f72d72021-09-01 14:13:57 +0100711 } else {
Ulya Trafimovich0061c0d2021-09-01 15:40:38 +0100712 // TODO(b/194504107): shouldn't priv-apps be always uncompressed unless
713 // DONT_UNCOMPRESS_PRIV_APPS_DEXS is true (regardless of unbundling)?
Ulya Trafimovich55f72d72021-09-01 14:13:57 +0100714 // expect = true
715 }
716 }
717
718 android.AssertBoolEquals(t, "uncompress dex", expect, actual)
719 }
720
721 for _, unbundled := range []bool{false, true} {
722 for _, dontUncompressPrivAppDexs := range []bool{false, true} {
723 for _, tt := range testCases {
724 name := fmt.Sprintf("%s,unbundled:%t,dontUncompressPrivAppDexs:%t",
725 tt.name, unbundled, dontUncompressPrivAppDexs)
726 t.Run(name, func(t *testing.T) {
727 test(t, tt.bp, unbundled, dontUncompressPrivAppDexs)
728 })
729 }
730 }
731 }
732}