blob: afbd702a304e2f77350c845f15fb73a1040dc68c [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 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 apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jiyong Park25fc6a92018-11-18 18:02:45 +090019 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Paul Duffin37856732021-02-26 14:24:15 +000021 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070022 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010023 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090024 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090025 "strings"
26 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090027
28 "github.com/google/blueprint/proptools"
29
30 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080031 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090032 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000033 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070034 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090035 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090036 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070037 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090038)
39
Jooyung Hand3639552019-08-09 12:57:43 +090040// names returns name list from white space separated string
41func names(s string) (ns []string) {
42 for _, n := range strings.Split(s, " ") {
43 if len(n) > 0 {
44 ns = append(ns, n)
45 }
46 }
47 return
48}
49
Paul Duffin40b62572021-03-20 11:39:01 +000050func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090051 t.Helper()
Paul Duffin40b62572021-03-20 11:39:01 +000052 apexFixtureFactory.Extend(preparers...).
Paul Duffine05480a2021-03-08 15:07:14 +000053 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000054 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090055}
56
Paul Duffin40b62572021-03-20 11:39:01 +000057func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090058 t.Helper()
Paul Duffin40b62572021-03-20 11:39:01 +000059 factory := apexFixtureFactory.Extend(preparers...)
60 if bp != "" {
61 factory = factory.Extend(android.FixtureWithRootAndroidBp(bp))
62 }
63 result := factory.RunTest(t)
Paul Duffine05480a2021-03-08 15:07:14 +000064 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090065}
66
Paul Duffin810f33d2021-03-09 14:12:32 +000067func withFiles(files android.MockFS) android.FixturePreparer {
68 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090069}
70
Paul Duffin810f33d2021-03-09 14:12:32 +000071func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
72 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090073 for k, v := range targets {
74 config.Targets[k] = v
75 }
Paul Duffin810f33d2021-03-09 14:12:32 +000076 })
Jooyung Han344d5432019-08-23 11:17:39 +090077}
78
Jooyung Han35155c42020-02-06 17:33:20 +090079// withNativeBridgeTargets sets configuration with targets including:
80// - X86_64 (primary)
81// - X86 (secondary)
82// - Arm64 on X86_64 (native bridge)
83// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000084var withNativeBridgeEnabled = android.FixtureModifyConfig(
85 func(config android.Config) {
86 config.Targets[android.Android] = []android.Target{
87 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
88 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
89 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
90 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
91 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
92 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
93 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
94 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
95 }
96 },
97)
98
99func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
100 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
101 variables.ManifestPackageNameOverrides = specs
102 })
Jooyung Han35155c42020-02-06 17:33:20 +0900103}
104
Paul Duffin810f33d2021-03-09 14:12:32 +0000105var withBinder32bit = android.FixtureModifyProductVariables(
106 func(variables android.FixtureProductVariables) {
107 variables.Binder32bit = proptools.BoolPtr(true)
108 },
109)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900110
Paul Duffin810f33d2021-03-09 14:12:32 +0000111var withUnbundledBuild = android.FixtureModifyProductVariables(
112 func(variables android.FixtureProductVariables) {
113 variables.Unbundled_build = proptools.BoolPtr(true)
114 },
115)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900116
Paul Duffin37aad602021-03-08 09:47:16 +0000117var apexFixtureFactory = android.NewFixtureFactory(
Paul Duffin37ba3442021-03-29 00:21:08 +0100118 nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000119 // General preparers in alphabetical order as test infrastructure will enforce correct
120 // registration order.
121 android.PrepareForTestWithAndroidBuildComponents,
122 bpf.PrepareForTestWithBpf,
123 cc.PrepareForTestWithCcBuildComponents,
124 java.PrepareForTestWithJavaDefaultModules,
125 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
126 rust.PrepareForTestWithRustDefaultModules,
127 sh.PrepareForTestWithShBuildComponents,
128
129 PrepareForTestWithApexBuildComponents,
130
131 // Additional apex test specific preparers.
132 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
133 filegroup {
134 name: "myapex-file_contexts",
135 srcs: [
136 "apex/myapex-file_contexts",
137 ],
138 }
139 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000140 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000141 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000142 "a.java": nil,
143 "PrebuiltAppFoo.apk": nil,
144 "PrebuiltAppFooPriv.apk": nil,
145 "apex_manifest.json": nil,
146 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000147 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
148 "system/sepolicy/apex/myapex2-file_contexts": nil,
149 "system/sepolicy/apex/otherapex-file_contexts": nil,
150 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
151 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
152 "mylib.cpp": nil,
153 "mytest.cpp": nil,
154 "mytest1.cpp": nil,
155 "mytest2.cpp": nil,
156 "mytest3.cpp": nil,
157 "myprebuilt": nil,
158 "my_include": nil,
159 "foo/bar/MyClass.java": nil,
160 "prebuilt.jar": nil,
161 "prebuilt.so": nil,
162 "vendor/foo/devkeys/test.x509.pem": nil,
163 "vendor/foo/devkeys/test.pk8": nil,
164 "testkey.x509.pem": nil,
165 "testkey.pk8": nil,
166 "testkey.override.x509.pem": nil,
167 "testkey.override.pk8": nil,
168 "vendor/foo/devkeys/testkey.avbpubkey": nil,
169 "vendor/foo/devkeys/testkey.pem": nil,
170 "NOTICE": nil,
171 "custom_notice": nil,
172 "custom_notice_for_static_lib": nil,
173 "testkey2.avbpubkey": nil,
174 "testkey2.pem": nil,
175 "myapex-arm64.apex": nil,
176 "myapex-arm.apex": nil,
177 "myapex.apks": nil,
178 "frameworks/base/api/current.txt": nil,
179 "framework/aidl/a.aidl": nil,
180 "build/make/core/proguard.flags": nil,
181 "build/make/core/proguard_basic_keeps.flags": nil,
182 "dummy.txt": nil,
183 "baz": nil,
184 "bar/baz": nil,
185 "testdata/baz": nil,
186 "AppSet.apks": nil,
187 "foo.rs": nil,
188 "libfoo.jar": nil,
189 "libbar.jar": nil,
190 },
191 ),
192
193 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
194 variables.DeviceVndkVersion = proptools.StringPtr("current")
195 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
196 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
197 variables.Platform_sdk_codename = proptools.StringPtr("Q")
198 variables.Platform_sdk_final = proptools.BoolPtr(false)
199 variables.Platform_version_active_codenames = []string{"Q"}
200 variables.Platform_vndk_version = proptools.StringPtr("VER")
201 }),
202)
203
Paul Duffin52bfaa42021-03-23 23:40:12 +0000204var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
205 "system/sepolicy/apex/myapex-file_contexts": nil,
206})
207
Jooyung Han643adc42020-02-27 13:50:06 +0900208// ensure that 'result' equals 'expected'
209func ensureEquals(t *testing.T, result string, expected string) {
210 t.Helper()
211 if result != expected {
212 t.Errorf("%q != %q", expected, result)
213 }
214}
215
Jiyong Park25fc6a92018-11-18 18:02:45 +0900216// ensure that 'result' contains 'expected'
217func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900218 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900219 if !strings.Contains(result, expected) {
220 t.Errorf("%q is not found in %q", expected, result)
221 }
222}
223
Liz Kammer5bd365f2020-05-27 15:15:11 -0700224// ensure that 'result' contains 'expected' exactly one time
225func ensureContainsOnce(t *testing.T, result string, expected string) {
226 t.Helper()
227 count := strings.Count(result, expected)
228 if count != 1 {
229 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
230 }
231}
232
Jiyong Park25fc6a92018-11-18 18:02:45 +0900233// ensures that 'result' does not contain 'notExpected'
234func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900235 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900236 if strings.Contains(result, notExpected) {
237 t.Errorf("%q is found in %q", notExpected, result)
238 }
239}
240
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700241func ensureMatches(t *testing.T, result string, expectedRex string) {
242 ok, err := regexp.MatchString(expectedRex, result)
243 if err != nil {
244 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
245 return
246 }
247 if !ok {
248 t.Errorf("%s does not match regular expession %s", result, expectedRex)
249 }
250}
251
Jiyong Park25fc6a92018-11-18 18:02:45 +0900252func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900253 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900254 if !android.InList(expected, result) {
255 t.Errorf("%q is not found in %v", expected, result)
256 }
257}
258
259func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900260 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261 if android.InList(notExpected, result) {
262 t.Errorf("%q is found in %v", notExpected, result)
263 }
264}
265
Jooyung Hane1633032019-08-01 17:41:43 +0900266func ensureListEmpty(t *testing.T, result []string) {
267 t.Helper()
268 if len(result) > 0 {
269 t.Errorf("%q is expected to be empty", result)
270 }
271}
272
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000273func ensureListNotEmpty(t *testing.T, result []string) {
274 t.Helper()
275 if len(result) == 0 {
276 t.Errorf("%q is expected to be not empty", result)
277 }
278}
279
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280// Minimal test
281func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800282 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900283 apex_defaults {
284 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900285 manifest: ":myapex.manifest",
286 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900288 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900289 native_shared_libs: [
290 "mylib",
291 "libfoo.ffi",
292 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900293 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800294 multilib: {
295 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900296 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800297 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900298 },
Jiyong Park77acec62020-06-01 21:39:15 +0900299 java_libs: [
300 "myjar",
301 "myjar_dex",
302 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000303 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900304 }
305
Jiyong Park30ca9372019-02-07 16:27:23 +0900306 apex {
307 name: "myapex",
308 defaults: ["myapex-defaults"],
309 }
310
Jiyong Park25fc6a92018-11-18 18:02:45 +0900311 apex_key {
312 name: "myapex.key",
313 public_key: "testkey.avbpubkey",
314 private_key: "testkey.pem",
315 }
316
Jiyong Park809bb722019-02-13 21:33:49 +0900317 filegroup {
318 name: "myapex.manifest",
319 srcs: ["apex_manifest.json"],
320 }
321
322 filegroup {
323 name: "myapex.androidmanifest",
324 srcs: ["AndroidManifest.xml"],
325 }
326
Jiyong Park25fc6a92018-11-18 18:02:45 +0900327 cc_library {
328 name: "mylib",
329 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900330 shared_libs: [
331 "mylib2",
332 "libbar.ffi",
333 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900334 system_shared_libs: [],
335 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000336 // TODO: remove //apex_available:platform
337 apex_available: [
338 "//apex_available:platform",
339 "myapex",
340 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900341 }
342
Alex Light3d673592019-01-18 14:37:31 -0800343 cc_binary {
344 name: "foo",
345 srcs: ["mylib.cpp"],
346 compile_multilib: "both",
347 multilib: {
348 lib32: {
349 suffix: "32",
350 },
351 lib64: {
352 suffix: "64",
353 },
354 },
355 symlinks: ["foo_link_"],
356 symlink_preferred_arch: true,
357 system_shared_libs: [],
358 static_executable: true,
359 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700360 apex_available: [ "myapex", "com.android.gki.*" ],
361 }
362
Jiyong Park99644e92020-11-17 22:21:02 +0900363 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000364 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900365 srcs: ["foo.rs"],
366 rlibs: ["libfoo.rlib.rust"],
367 dylibs: ["libfoo.dylib.rust"],
368 apex_available: ["myapex"],
369 }
370
371 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000372 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900373 srcs: ["foo.rs"],
374 crate_name: "foo",
375 apex_available: ["myapex"],
376 }
377
378 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000379 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900380 srcs: ["foo.rs"],
381 crate_name: "foo",
382 apex_available: ["myapex"],
383 }
384
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900385 rust_ffi_shared {
386 name: "libfoo.ffi",
387 srcs: ["foo.rs"],
388 crate_name: "foo",
389 apex_available: ["myapex"],
390 }
391
392 rust_ffi_shared {
393 name: "libbar.ffi",
394 srcs: ["foo.rs"],
395 crate_name: "bar",
396 apex_available: ["myapex"],
397 }
398
Yifan Hongd22a84a2020-07-28 17:37:46 -0700399 apex {
400 name: "com.android.gki.fake",
401 binaries: ["foo"],
402 key: "myapex.key",
403 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000404 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800405 }
406
Paul Duffindddd5462020-04-07 15:25:44 +0100407 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900408 name: "mylib2",
409 srcs: ["mylib.cpp"],
410 system_shared_libs: [],
411 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900412 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900413 static_libs: ["libstatic"],
414 // TODO: remove //apex_available:platform
415 apex_available: [
416 "//apex_available:platform",
417 "myapex",
418 ],
419 }
420
Paul Duffindddd5462020-04-07 15:25:44 +0100421 cc_prebuilt_library_shared {
422 name: "mylib2",
423 srcs: ["prebuilt.so"],
424 // TODO: remove //apex_available:platform
425 apex_available: [
426 "//apex_available:platform",
427 "myapex",
428 ],
429 }
430
Jiyong Park9918e1a2020-03-17 19:16:40 +0900431 cc_library_static {
432 name: "libstatic",
433 srcs: ["mylib.cpp"],
434 system_shared_libs: [],
435 stl: "none",
436 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000437 // TODO: remove //apex_available:platform
438 apex_available: [
439 "//apex_available:platform",
440 "myapex",
441 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900442 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900443
444 java_library {
445 name: "myjar",
446 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900447 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900448 sdk_version: "none",
449 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900450 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900451 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000452 // TODO: remove //apex_available:platform
453 apex_available: [
454 "//apex_available:platform",
455 "myapex",
456 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900457 }
458
Jiyong Park77acec62020-06-01 21:39:15 +0900459 dex_import {
460 name: "myjar_dex",
461 jars: ["prebuilt.jar"],
462 apex_available: [
463 "//apex_available:platform",
464 "myapex",
465 ],
466 }
467
Jiyong Park7f7766d2019-07-25 22:02:35 +0900468 java_library {
469 name: "myotherjar",
470 srcs: ["foo/bar/MyClass.java"],
471 sdk_version: "none",
472 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900473 // TODO: remove //apex_available:platform
474 apex_available: [
475 "//apex_available:platform",
476 "myapex",
477 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900478 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900479
480 java_library {
481 name: "mysharedjar",
482 srcs: ["foo/bar/MyClass.java"],
483 sdk_version: "none",
484 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900485 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900486 `)
487
Paul Duffin37ba3442021-03-29 00:21:08 +0100488 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").RelativeToTop()
Jiyong Park42cca6c2019-04-01 11:15:50 +0900489
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900490 // Make sure that Android.mk is created
491 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700492 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900493 var builder strings.Builder
494 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
495
496 androidMk := builder.String()
497 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
498 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
499
Jiyong Park42cca6c2019-04-01 11:15:50 +0900500 optFlags := apexRule.Args["opt_flags"]
501 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700502 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100503 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900504
Jiyong Park25fc6a92018-11-18 18:02:45 +0900505 copyCmds := apexRule.Args["copy_commands"]
506
507 // Ensure that main rule creates an output
508 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
509
510 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700511 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
512 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
513 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900514 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900515 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900516
517 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700518 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
519 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900520 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
521 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900522 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900523
524 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800525 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
526 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900527 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900528 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900529 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900530 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
531 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900532 // .. but not for java libs
533 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900534 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800535
Colin Cross7113d202019-11-20 16:39:12 -0800536 // Ensure that the platform variant ends with _shared or _common
537 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
538 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900539 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
540 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900541 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
542
543 // Ensure that dynamic dependency to java libs are not included
544 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800545
546 // Ensure that all symlinks are present.
547 found_foo_link_64 := false
548 found_foo := false
549 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900550 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800551 if strings.HasSuffix(cmd, "bin/foo") {
552 found_foo = true
553 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
554 found_foo_link_64 = true
555 }
556 }
557 }
558 good := found_foo && found_foo_link_64
559 if !good {
560 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
561 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900562
Sundong Ahnabb64432019-10-22 13:58:29 +0900563 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700564 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900565 if len(noticeInputs) != 3 {
566 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900567 }
568 ensureListContains(t, noticeInputs, "NOTICE")
569 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900570 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900571
Artur Satayeva8bd1132020-04-27 18:07:06 +0100572 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100573 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100574 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
575 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
576 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100577
578 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100579 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100580 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
581 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
582 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800583}
584
Jooyung Hanf21c7972019-12-16 22:32:06 +0900585func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800586 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900587 apex_defaults {
588 name: "myapex-defaults",
589 key: "myapex.key",
590 prebuilts: ["myetc"],
591 native_shared_libs: ["mylib"],
592 java_libs: ["myjar"],
593 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900594 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800595 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000596 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900597 }
598
599 prebuilt_etc {
600 name: "myetc",
601 src: "myprebuilt",
602 }
603
604 apex {
605 name: "myapex",
606 defaults: ["myapex-defaults"],
607 }
608
609 apex_key {
610 name: "myapex.key",
611 public_key: "testkey.avbpubkey",
612 private_key: "testkey.pem",
613 }
614
615 cc_library {
616 name: "mylib",
617 system_shared_libs: [],
618 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000619 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900620 }
621
622 java_library {
623 name: "myjar",
624 srcs: ["foo/bar/MyClass.java"],
625 sdk_version: "none",
626 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000627 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900628 }
629
630 android_app {
631 name: "AppFoo",
632 srcs: ["foo/bar/MyClass.java"],
633 sdk_version: "none",
634 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000635 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900636 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900637
638 runtime_resource_overlay {
639 name: "rro",
640 theme: "blue",
641 }
642
markchien2f59ec92020-09-02 16:23:38 +0800643 bpf {
644 name: "bpf",
645 srcs: ["bpf.c", "bpf2.c"],
646 }
647
Jooyung Hanf21c7972019-12-16 22:32:06 +0900648 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000649 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900650 "etc/myetc",
651 "javalib/myjar.jar",
652 "lib64/mylib.so",
653 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900654 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800655 "etc/bpf/bpf.o",
656 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900657 })
658}
659
Jooyung Han01a3ee22019-11-02 02:52:25 +0900660func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800661 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900662 apex {
663 name: "myapex",
664 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000665 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900666 }
667
668 apex_key {
669 name: "myapex.key",
670 public_key: "testkey.avbpubkey",
671 private_key: "testkey.pem",
672 }
673 `)
674
675 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900676 args := module.Rule("apexRule").Args
677 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
678 t.Error("manifest should be apex_manifest.pb, but " + manifest)
679 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900680}
681
Alex Light5098a612018-11-29 17:12:15 -0800682func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800683 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800684 apex {
685 name: "myapex",
686 key: "myapex.key",
687 payload_type: "zip",
688 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000689 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800690 }
691
692 apex_key {
693 name: "myapex.key",
694 public_key: "testkey.avbpubkey",
695 private_key: "testkey.pem",
696 }
697
698 cc_library {
699 name: "mylib",
700 srcs: ["mylib.cpp"],
701 shared_libs: ["mylib2"],
702 system_shared_libs: [],
703 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000704 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800705 }
706
707 cc_library {
708 name: "mylib2",
709 srcs: ["mylib.cpp"],
710 system_shared_libs: [],
711 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000712 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800713 }
714 `)
715
Sundong Ahnabb64432019-10-22 13:58:29 +0900716 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800717 copyCmds := zipApexRule.Args["copy_commands"]
718
719 // Ensure that main rule creates an output
720 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
721
722 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700723 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800724
725 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700726 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800727
728 // Ensure that both direct and indirect deps are copied into apex
729 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
730 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900731}
732
733func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800734 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900735 apex {
736 name: "myapex",
737 key: "myapex.key",
738 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000739 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900740 }
741
742 apex_key {
743 name: "myapex.key",
744 public_key: "testkey.avbpubkey",
745 private_key: "testkey.pem",
746 }
747
748 cc_library {
749 name: "mylib",
750 srcs: ["mylib.cpp"],
751 shared_libs: ["mylib2", "mylib3"],
752 system_shared_libs: [],
753 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000754 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900755 }
756
757 cc_library {
758 name: "mylib2",
759 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900760 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900761 system_shared_libs: [],
762 stl: "none",
763 stubs: {
764 versions: ["1", "2", "3"],
765 },
766 }
767
768 cc_library {
769 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900770 srcs: ["mylib.cpp"],
771 shared_libs: ["mylib4"],
772 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900773 stl: "none",
774 stubs: {
775 versions: ["10", "11", "12"],
776 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000777 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900778 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900779
780 cc_library {
781 name: "mylib4",
782 srcs: ["mylib.cpp"],
783 system_shared_libs: [],
784 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000785 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900786 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900787 `)
788
Sundong Ahnabb64432019-10-22 13:58:29 +0900789 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900790 copyCmds := apexRule.Args["copy_commands"]
791
792 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800793 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900794
795 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800796 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900797
798 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800799 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900800
Colin Crossaede88c2020-08-11 12:17:01 -0700801 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900802
803 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900804 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900805 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900806 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900807
808 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -0700809 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900810 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700811 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900812
813 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900814 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900815 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900816
817 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700818 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900819
Jooyung Hana57af4a2020-01-23 05:36:59 +0000820 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900821 "lib64/mylib.so",
822 "lib64/mylib3.so",
823 "lib64/mylib4.so",
824 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900825}
826
Colin Cross7812fd32020-09-25 12:35:10 -0700827func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
828 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800829 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700830 apex {
831 name: "myapex",
832 key: "myapex.key",
833 native_shared_libs: ["mylib", "mylib3"],
834 min_sdk_version: "29",
835 }
836
837 apex_key {
838 name: "myapex.key",
839 public_key: "testkey.avbpubkey",
840 private_key: "testkey.pem",
841 }
842
843 cc_library {
844 name: "mylib",
845 srcs: ["mylib.cpp"],
846 shared_libs: ["mylib2", "mylib3"],
847 system_shared_libs: [],
848 stl: "none",
849 apex_available: [ "myapex" ],
850 min_sdk_version: "28",
851 }
852
853 cc_library {
854 name: "mylib2",
855 srcs: ["mylib.cpp"],
856 cflags: ["-include mylib.h"],
857 system_shared_libs: [],
858 stl: "none",
859 stubs: {
860 versions: ["28", "29", "30", "current"],
861 },
862 min_sdk_version: "28",
863 }
864
865 cc_library {
866 name: "mylib3",
867 srcs: ["mylib.cpp"],
868 shared_libs: ["mylib4"],
869 system_shared_libs: [],
870 stl: "none",
871 stubs: {
872 versions: ["28", "29", "30", "current"],
873 },
874 apex_available: [ "myapex" ],
875 min_sdk_version: "28",
876 }
877
878 cc_library {
879 name: "mylib4",
880 srcs: ["mylib.cpp"],
881 system_shared_libs: [],
882 stl: "none",
883 apex_available: [ "myapex" ],
884 min_sdk_version: "28",
885 }
886 `)
887
888 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
889 copyCmds := apexRule.Args["copy_commands"]
890
891 // Ensure that direct non-stubs dep is always included
892 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
893
894 // Ensure that indirect stubs dep is not included
895 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
896
897 // Ensure that direct stubs dep is included
898 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
899
900 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
901
Jiyong Park55549df2021-02-26 23:57:23 +0900902 // Ensure that mylib is linking with the latest version of stub for mylib2
903 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700904 // ... and not linking to the non-stub (impl) variant of mylib2
905 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
906
907 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
908 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
909 // .. and not linking to the stubs variant of mylib3
910 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
911
912 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700913 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700914 ensureNotContains(t, mylib2Cflags, "-include ")
915
916 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700917 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700918
919 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
920 "lib64/mylib.so",
921 "lib64/mylib3.so",
922 "lib64/mylib4.so",
923 })
924}
925
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900926func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
927 t.Parallel()
928 // myapex (Z)
929 // mylib -----------------.
930 // |
931 // otherapex (29) |
932 // libstub's versions: 29 Z current
933 // |
934 // <platform> |
935 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800936 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900937 apex {
938 name: "myapex",
939 key: "myapex.key",
940 native_shared_libs: ["mylib"],
941 min_sdk_version: "Z", // non-final
942 }
943
944 cc_library {
945 name: "mylib",
946 srcs: ["mylib.cpp"],
947 shared_libs: ["libstub"],
948 apex_available: ["myapex"],
949 min_sdk_version: "Z",
950 }
951
952 apex_key {
953 name: "myapex.key",
954 public_key: "testkey.avbpubkey",
955 private_key: "testkey.pem",
956 }
957
958 apex {
959 name: "otherapex",
960 key: "myapex.key",
961 native_shared_libs: ["libstub"],
962 min_sdk_version: "29",
963 }
964
965 cc_library {
966 name: "libstub",
967 srcs: ["mylib.cpp"],
968 stubs: {
969 versions: ["29", "Z", "current"],
970 },
971 apex_available: ["otherapex"],
972 min_sdk_version: "29",
973 }
974
975 // platform module depending on libstub from otherapex should use the latest stub("current")
976 cc_library {
977 name: "libplatform",
978 srcs: ["mylib.cpp"],
979 shared_libs: ["libstub"],
980 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +0000981 `,
982 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
983 variables.Platform_sdk_codename = proptools.StringPtr("Z")
984 variables.Platform_sdk_final = proptools.BoolPtr(false)
985 variables.Platform_version_active_codenames = []string{"Z"}
986 }),
987 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900988
Jiyong Park55549df2021-02-26 23:57:23 +0900989 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900990 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +0900991 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900992 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +0900993 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900994
995 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
996 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
997 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
998 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
999 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1000}
1001
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001002func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001003 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001004 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001005 name: "myapex2",
1006 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001007 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001008 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001009 }
1010
1011 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001012 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001013 public_key: "testkey.avbpubkey",
1014 private_key: "testkey.pem",
1015 }
1016
1017 cc_library {
1018 name: "mylib",
1019 srcs: ["mylib.cpp"],
1020 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001021 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001022 system_shared_libs: [],
1023 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001024 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001025 }
1026
1027 cc_library {
1028 name: "libfoo",
1029 srcs: ["mylib.cpp"],
1030 shared_libs: ["libbar"],
1031 system_shared_libs: [],
1032 stl: "none",
1033 stubs: {
1034 versions: ["10", "20", "30"],
1035 },
1036 }
1037
1038 cc_library {
1039 name: "libbar",
1040 srcs: ["mylib.cpp"],
1041 system_shared_libs: [],
1042 stl: "none",
1043 }
1044
Jiyong Park678c8812020-02-07 17:25:49 +09001045 cc_library_static {
1046 name: "libbaz",
1047 srcs: ["mylib.cpp"],
1048 system_shared_libs: [],
1049 stl: "none",
1050 apex_available: [ "myapex2" ],
1051 }
1052
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001053 `)
1054
Jiyong Park83dc74b2020-01-14 18:38:44 +09001055 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001056 copyCmds := apexRule.Args["copy_commands"]
1057
1058 // Ensure that direct non-stubs dep is always included
1059 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1060
1061 // Ensure that indirect stubs dep is not included
1062 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1063
1064 // Ensure that dependency of stubs is not included
1065 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1066
Colin Crossaede88c2020-08-11 12:17:01 -07001067 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001068
1069 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001070 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001071 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001072 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001073
Jiyong Park3ff16992019-12-27 14:11:47 +09001074 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001075
1076 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1077 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001078
Artur Satayeva8bd1132020-04-27 18:07:06 +01001079 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001080 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001081
Artur Satayeva8bd1132020-04-27 18:07:06 +01001082 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001083 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001084}
1085
Jooyung Hand3639552019-08-09 12:57:43 +09001086func TestApexWithRuntimeLibsDependency(t *testing.T) {
1087 /*
1088 myapex
1089 |
1090 v (runtime_libs)
1091 mylib ------+------> libfoo [provides stub]
1092 |
1093 `------> libbar
1094 */
Colin Cross1c460562021-02-16 17:55:47 -08001095 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001096 apex {
1097 name: "myapex",
1098 key: "myapex.key",
1099 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001100 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001101 }
1102
1103 apex_key {
1104 name: "myapex.key",
1105 public_key: "testkey.avbpubkey",
1106 private_key: "testkey.pem",
1107 }
1108
1109 cc_library {
1110 name: "mylib",
1111 srcs: ["mylib.cpp"],
1112 runtime_libs: ["libfoo", "libbar"],
1113 system_shared_libs: [],
1114 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001115 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001116 }
1117
1118 cc_library {
1119 name: "libfoo",
1120 srcs: ["mylib.cpp"],
1121 system_shared_libs: [],
1122 stl: "none",
1123 stubs: {
1124 versions: ["10", "20", "30"],
1125 },
1126 }
1127
1128 cc_library {
1129 name: "libbar",
1130 srcs: ["mylib.cpp"],
1131 system_shared_libs: [],
1132 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001133 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001134 }
1135
1136 `)
1137
Sundong Ahnabb64432019-10-22 13:58:29 +09001138 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001139 copyCmds := apexRule.Args["copy_commands"]
1140
1141 // Ensure that direct non-stubs dep is always included
1142 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1143
1144 // Ensure that indirect stubs dep is not included
1145 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1146
1147 // Ensure that runtime_libs dep in included
1148 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1149
Sundong Ahnabb64432019-10-22 13:58:29 +09001150 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001151 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1152 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001153
1154}
1155
Paul Duffina02cae32021-03-09 01:44:06 +00001156var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1157 cc.PrepareForTestWithCcBuildComponents,
1158 PrepareForTestWithApexBuildComponents,
1159 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001160 apex {
1161 name: "com.android.runtime",
1162 key: "com.android.runtime.key",
1163 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001164 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001165 }
1166
1167 apex_key {
1168 name: "com.android.runtime.key",
1169 public_key: "testkey.avbpubkey",
1170 private_key: "testkey.pem",
1171 }
Paul Duffina02cae32021-03-09 01:44:06 +00001172 `),
1173 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1174)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001175
Paul Duffina02cae32021-03-09 01:44:06 +00001176func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001177 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001178 cc_library {
1179 name: "libc",
1180 no_libcrt: true,
1181 nocrt: true,
1182 stl: "none",
1183 system_shared_libs: [],
1184 stubs: { versions: ["1"] },
1185 apex_available: ["com.android.runtime"],
1186
1187 sanitize: {
1188 hwaddress: true,
1189 }
1190 }
1191
1192 cc_prebuilt_library_shared {
1193 name: "libclang_rt.hwasan-aarch64-android",
1194 no_libcrt: true,
1195 nocrt: true,
1196 stl: "none",
1197 system_shared_libs: [],
1198 srcs: [""],
1199 stubs: { versions: ["1"] },
1200
1201 sanitize: {
1202 never: true,
1203 },
Paul Duffina02cae32021-03-09 01:44:06 +00001204 } `)
1205 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001206
1207 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1208 "lib64/bionic/libc.so",
1209 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1210 })
1211
1212 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1213
1214 installed := hwasan.Description("install libclang_rt.hwasan")
1215 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1216
1217 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1218 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1219 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1220}
1221
1222func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001223 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001224 prepareForTestOfRuntimeApexWithHwasan,
1225 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1226 variables.SanitizeDevice = []string{"hwaddress"}
1227 }),
1228 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001229 cc_library {
1230 name: "libc",
1231 no_libcrt: true,
1232 nocrt: true,
1233 stl: "none",
1234 system_shared_libs: [],
1235 stubs: { versions: ["1"] },
1236 apex_available: ["com.android.runtime"],
1237 }
1238
1239 cc_prebuilt_library_shared {
1240 name: "libclang_rt.hwasan-aarch64-android",
1241 no_libcrt: true,
1242 nocrt: true,
1243 stl: "none",
1244 system_shared_libs: [],
1245 srcs: [""],
1246 stubs: { versions: ["1"] },
1247
1248 sanitize: {
1249 never: true,
1250 },
1251 }
Paul Duffina02cae32021-03-09 01:44:06 +00001252 `)
1253 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001254
1255 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1256 "lib64/bionic/libc.so",
1257 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1258 })
1259
1260 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1261
1262 installed := hwasan.Description("install libclang_rt.hwasan")
1263 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1264
1265 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1266 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1267 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1268}
1269
Jooyung Han61b66e92020-03-21 14:21:46 +00001270func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1271 testcases := []struct {
1272 name string
1273 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001274 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001275 shouldLink string
1276 shouldNotLink []string
1277 }{
1278 {
Jiyong Park55549df2021-02-26 23:57:23 +09001279 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001280 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001281 apexVariant: "apex10000",
Jooyung Han61b66e92020-03-21 14:21:46 +00001282 shouldLink: "30",
1283 shouldNotLink: []string{"29"},
1284 },
1285 {
Jiyong Park55549df2021-02-26 23:57:23 +09001286 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001287 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001288 apexVariant: "apex29",
Jiyong Park55549df2021-02-26 23:57:23 +09001289 shouldLink: "30",
1290 shouldNotLink: []string{"29"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001291 },
1292 }
1293 for _, tc := range testcases {
1294 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001295 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001296 apex {
1297 name: "myapex",
1298 key: "myapex.key",
1299 use_vendor: true,
1300 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001301 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001302 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001303 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001304
Jooyung Han61b66e92020-03-21 14:21:46 +00001305 apex_key {
1306 name: "myapex.key",
1307 public_key: "testkey.avbpubkey",
1308 private_key: "testkey.pem",
1309 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001310
Jooyung Han61b66e92020-03-21 14:21:46 +00001311 cc_library {
1312 name: "mylib",
1313 srcs: ["mylib.cpp"],
1314 vendor_available: true,
1315 shared_libs: ["libbar"],
1316 system_shared_libs: [],
1317 stl: "none",
1318 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001319 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001320 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001321
Jooyung Han61b66e92020-03-21 14:21:46 +00001322 cc_library {
1323 name: "libbar",
1324 srcs: ["mylib.cpp"],
1325 system_shared_libs: [],
1326 stl: "none",
1327 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001328 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001329 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001330
Jooyung Han61b66e92020-03-21 14:21:46 +00001331 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001332 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001333 symbol_file: "",
1334 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001335 `,
1336 setUseVendorAllowListForTest([]string{"myapex"}),
1337 withUnbundledBuild,
1338 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001339
Jooyung Han61b66e92020-03-21 14:21:46 +00001340 // Ensure that LLNDK dep is not included
1341 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1342 "lib64/mylib.so",
1343 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001344
Jooyung Han61b66e92020-03-21 14:21:46 +00001345 // Ensure that LLNDK dep is required
1346 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1347 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1348 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001349
Colin Crossaede88c2020-08-11 12:17:01 -07001350 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
Colin Cross127bb8b2020-12-16 16:46:01 -08001351 ensureContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001352 for _, ver := range tc.shouldNotLink {
Colin Cross127bb8b2020-12-16 16:46:01 -08001353 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001354 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001355
Colin Crossaede88c2020-08-11 12:17:01 -07001356 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jooyung Han61b66e92020-03-21 14:21:46 +00001357 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1358 })
1359 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001360}
1361
Jiyong Park25fc6a92018-11-18 18:02:45 +09001362func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001363 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001364 apex {
1365 name: "myapex",
1366 key: "myapex.key",
1367 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001368 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001369 }
1370
1371 apex_key {
1372 name: "myapex.key",
1373 public_key: "testkey.avbpubkey",
1374 private_key: "testkey.pem",
1375 }
1376
1377 cc_library {
1378 name: "mylib",
1379 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001380 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001381 shared_libs: ["libdl#27"],
1382 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001383 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001384 }
1385
1386 cc_library_shared {
1387 name: "mylib_shared",
1388 srcs: ["mylib.cpp"],
1389 shared_libs: ["libdl#27"],
1390 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001391 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001392 }
1393
1394 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001395 name: "libBootstrap",
1396 srcs: ["mylib.cpp"],
1397 stl: "none",
1398 bootstrap: true,
1399 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001400 `)
1401
Sundong Ahnabb64432019-10-22 13:58:29 +09001402 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001403 copyCmds := apexRule.Args["copy_commands"]
1404
1405 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001406 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001407 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1408 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001409
1410 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001411 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001412
Colin Crossaede88c2020-08-11 12:17:01 -07001413 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1414 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1415 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001416
1417 // For dependency to libc
1418 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001419 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001420 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001421 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001422 // ... Cflags from stub is correctly exported to mylib
1423 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1424 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1425
1426 // For dependency to libm
1427 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001428 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001429 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001430 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001431 // ... and is not compiling with the stub
1432 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1433 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1434
1435 // For dependency to libdl
1436 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001437 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001438 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001439 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1440 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001441 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001442 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001443 // ... Cflags from stub is correctly exported to mylib
1444 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1445 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001446
1447 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001448 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1449 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1450 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1451 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001452}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001453
Jooyung Han749dc692020-04-15 11:03:39 +09001454func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001455 // there are three links between liba --> libz.
1456 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001457 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001458 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001459 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001460 apex {
1461 name: "myapex",
1462 key: "myapex.key",
1463 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001464 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001465 }
1466
1467 apex {
1468 name: "otherapex",
1469 key: "myapex.key",
1470 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001471 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001472 }
1473
1474 apex_key {
1475 name: "myapex.key",
1476 public_key: "testkey.avbpubkey",
1477 private_key: "testkey.pem",
1478 }
1479
1480 cc_library {
1481 name: "libx",
1482 shared_libs: ["liba"],
1483 system_shared_libs: [],
1484 stl: "none",
1485 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001486 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001487 }
1488
1489 cc_library {
1490 name: "liby",
1491 shared_libs: ["liba"],
1492 system_shared_libs: [],
1493 stl: "none",
1494 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001495 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001496 }
1497
1498 cc_library {
1499 name: "liba",
1500 shared_libs: ["libz"],
1501 system_shared_libs: [],
1502 stl: "none",
1503 apex_available: [
1504 "//apex_available:anyapex",
1505 "//apex_available:platform",
1506 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001507 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001508 }
1509
1510 cc_library {
1511 name: "libz",
1512 system_shared_libs: [],
1513 stl: "none",
1514 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001515 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001516 },
1517 }
Jooyung Han749dc692020-04-15 11:03:39 +09001518 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001519
1520 expectLink := func(from, from_variant, to, to_variant string) {
1521 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1522 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1523 }
1524 expectNoLink := func(from, from_variant, to, to_variant string) {
1525 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1526 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1527 }
1528 // platform liba is linked to non-stub version
1529 expectLink("liba", "shared", "libz", "shared")
Jiyong Park55549df2021-02-26 23:57:23 +09001530 // liba in myapex is linked to #30
1531 expectLink("liba", "shared_apex29", "libz", "shared_30")
1532 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001533 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001534 // liba in otherapex is linked to #30
Colin Crossaede88c2020-08-11 12:17:01 -07001535 expectLink("liba", "shared_apex30", "libz", "shared_30")
1536 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1537 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001538}
1539
Jooyung Hanaed150d2020-04-02 01:41:41 +09001540func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001541 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001542 apex {
1543 name: "myapex",
1544 key: "myapex.key",
1545 native_shared_libs: ["libx"],
1546 min_sdk_version: "R",
1547 }
1548
1549 apex_key {
1550 name: "myapex.key",
1551 public_key: "testkey.avbpubkey",
1552 private_key: "testkey.pem",
1553 }
1554
1555 cc_library {
1556 name: "libx",
1557 shared_libs: ["libz"],
1558 system_shared_libs: [],
1559 stl: "none",
1560 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001561 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001562 }
1563
1564 cc_library {
1565 name: "libz",
1566 system_shared_libs: [],
1567 stl: "none",
1568 stubs: {
1569 versions: ["29", "R"],
1570 },
1571 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001572 `,
1573 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1574 variables.Platform_version_active_codenames = []string{"R"}
1575 }),
1576 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001577
1578 expectLink := func(from, from_variant, to, to_variant string) {
1579 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1580 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1581 }
1582 expectNoLink := func(from, from_variant, to, to_variant string) {
1583 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1584 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1585 }
Dan Albertc8060532020-07-22 22:32:17 -07001586 expectLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001587 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1588 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001589}
1590
Jooyung Han749dc692020-04-15 11:03:39 +09001591func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001592 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001593 apex {
1594 name: "myapex",
1595 key: "myapex.key",
1596 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001597 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001598 }
1599
1600 apex_key {
1601 name: "myapex.key",
1602 public_key: "testkey.avbpubkey",
1603 private_key: "testkey.pem",
1604 }
1605
1606 cc_library {
1607 name: "libx",
1608 shared_libs: ["libz"],
1609 system_shared_libs: [],
1610 stl: "none",
1611 apex_available: [ "myapex" ],
1612 }
1613
1614 cc_library {
1615 name: "libz",
1616 system_shared_libs: [],
1617 stl: "none",
1618 stubs: {
1619 versions: ["1", "2"],
1620 },
1621 }
1622 `)
1623
1624 expectLink := func(from, from_variant, to, to_variant string) {
1625 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1626 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1627 }
1628 expectNoLink := func(from, from_variant, to, to_variant string) {
1629 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1630 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1631 }
Colin Crossaede88c2020-08-11 12:17:01 -07001632 expectLink("libx", "shared_apex10000", "libz", "shared_2")
1633 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
1634 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001635}
1636
1637func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001638 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001639 apex {
1640 name: "myapex",
1641 key: "myapex.key",
1642 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001643 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001644 }
1645
1646 apex_key {
1647 name: "myapex.key",
1648 public_key: "testkey.avbpubkey",
1649 private_key: "testkey.pem",
1650 }
1651
1652 cc_library {
1653 name: "libx",
1654 system_shared_libs: [],
1655 stl: "none",
1656 apex_available: [ "myapex" ],
1657 stubs: {
1658 versions: ["1", "2"],
1659 },
1660 }
1661
1662 cc_library {
1663 name: "libz",
1664 shared_libs: ["libx"],
1665 system_shared_libs: [],
1666 stl: "none",
1667 }
1668 `)
1669
1670 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001671 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001672 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1673 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1674 }
1675 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001676 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001677 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1678 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1679 }
1680 expectLink("libz", "shared", "libx", "shared_2")
1681 expectNoLink("libz", "shared", "libz", "shared_1")
1682 expectNoLink("libz", "shared", "libz", "shared")
1683}
1684
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001685var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1686 func(variables android.FixtureProductVariables) {
1687 variables.SanitizeDevice = []string{"hwaddress"}
1688 },
1689)
1690
Jooyung Han75568392020-03-20 04:29:24 +09001691func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001692 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001693 apex {
1694 name: "myapex",
1695 key: "myapex.key",
1696 native_shared_libs: ["libx"],
1697 min_sdk_version: "29",
1698 }
1699
1700 apex_key {
1701 name: "myapex.key",
1702 public_key: "testkey.avbpubkey",
1703 private_key: "testkey.pem",
1704 }
1705
1706 cc_library {
1707 name: "libx",
1708 shared_libs: ["libbar"],
1709 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001710 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001711 }
1712
1713 cc_library {
1714 name: "libbar",
1715 stubs: {
1716 versions: ["29", "30"],
1717 },
1718 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001719 `,
1720 prepareForTestWithSantitizeHwaddress,
1721 )
Jooyung Han03b51852020-02-26 22:45:42 +09001722 expectLink := func(from, from_variant, to, to_variant string) {
1723 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1724 libFlags := ld.Args["libFlags"]
1725 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1726 }
Colin Crossaede88c2020-08-11 12:17:01 -07001727 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001728}
1729
Jooyung Han75568392020-03-20 04:29:24 +09001730func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001731 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001732 apex {
1733 name: "myapex",
1734 key: "myapex.key",
1735 native_shared_libs: ["libx"],
1736 min_sdk_version: "29",
1737 }
1738
1739 apex_key {
1740 name: "myapex.key",
1741 public_key: "testkey.avbpubkey",
1742 private_key: "testkey.pem",
1743 }
1744
1745 cc_library {
1746 name: "libx",
1747 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001748 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001749 }
Jooyung Han75568392020-03-20 04:29:24 +09001750 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001751
1752 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001753 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001754 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001755 // note that platform variant is not.
1756 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001757 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001758}
1759
Jooyung Han749dc692020-04-15 11:03:39 +09001760func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1761 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001762 apex {
1763 name: "myapex",
1764 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001765 native_shared_libs: ["mylib"],
1766 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001767 }
1768
1769 apex_key {
1770 name: "myapex.key",
1771 public_key: "testkey.avbpubkey",
1772 private_key: "testkey.pem",
1773 }
Jooyung Han749dc692020-04-15 11:03:39 +09001774
1775 cc_library {
1776 name: "mylib",
1777 srcs: ["mylib.cpp"],
1778 system_shared_libs: [],
1779 stl: "none",
1780 apex_available: [
1781 "myapex",
1782 ],
1783 min_sdk_version: "30",
1784 }
1785 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001786
1787 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1788 apex {
1789 name: "myapex",
1790 key: "myapex.key",
1791 native_shared_libs: ["libfoo.ffi"],
1792 min_sdk_version: "29",
1793 }
1794
1795 apex_key {
1796 name: "myapex.key",
1797 public_key: "testkey.avbpubkey",
1798 private_key: "testkey.pem",
1799 }
1800
1801 rust_ffi_shared {
1802 name: "libfoo.ffi",
1803 srcs: ["foo.rs"],
1804 crate_name: "foo",
1805 apex_available: [
1806 "myapex",
1807 ],
1808 min_sdk_version: "30",
1809 }
1810 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001811}
1812
1813func TestApexMinSdkVersion_Okay(t *testing.T) {
1814 testApex(t, `
1815 apex {
1816 name: "myapex",
1817 key: "myapex.key",
1818 native_shared_libs: ["libfoo"],
1819 java_libs: ["libbar"],
1820 min_sdk_version: "29",
1821 }
1822
1823 apex_key {
1824 name: "myapex.key",
1825 public_key: "testkey.avbpubkey",
1826 private_key: "testkey.pem",
1827 }
1828
1829 cc_library {
1830 name: "libfoo",
1831 srcs: ["mylib.cpp"],
1832 shared_libs: ["libfoo_dep"],
1833 apex_available: ["myapex"],
1834 min_sdk_version: "29",
1835 }
1836
1837 cc_library {
1838 name: "libfoo_dep",
1839 srcs: ["mylib.cpp"],
1840 apex_available: ["myapex"],
1841 min_sdk_version: "29",
1842 }
1843
1844 java_library {
1845 name: "libbar",
1846 sdk_version: "current",
1847 srcs: ["a.java"],
1848 static_libs: ["libbar_dep"],
1849 apex_available: ["myapex"],
1850 min_sdk_version: "29",
1851 }
1852
1853 java_library {
1854 name: "libbar_dep",
1855 sdk_version: "current",
1856 srcs: ["a.java"],
1857 apex_available: ["myapex"],
1858 min_sdk_version: "29",
1859 }
Jooyung Han03b51852020-02-26 22:45:42 +09001860 `)
1861}
1862
Artur Satayev8cf899a2020-04-15 17:29:42 +01001863func TestJavaStableSdkVersion(t *testing.T) {
1864 testCases := []struct {
1865 name string
1866 expectedError string
1867 bp string
1868 }{
1869 {
1870 name: "Non-updatable apex with non-stable dep",
1871 bp: `
1872 apex {
1873 name: "myapex",
1874 java_libs: ["myjar"],
1875 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001876 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001877 }
1878 apex_key {
1879 name: "myapex.key",
1880 public_key: "testkey.avbpubkey",
1881 private_key: "testkey.pem",
1882 }
1883 java_library {
1884 name: "myjar",
1885 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001886 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001887 apex_available: ["myapex"],
1888 }
1889 `,
1890 },
1891 {
1892 name: "Updatable apex with stable dep",
1893 bp: `
1894 apex {
1895 name: "myapex",
1896 java_libs: ["myjar"],
1897 key: "myapex.key",
1898 updatable: true,
1899 min_sdk_version: "29",
1900 }
1901 apex_key {
1902 name: "myapex.key",
1903 public_key: "testkey.avbpubkey",
1904 private_key: "testkey.pem",
1905 }
1906 java_library {
1907 name: "myjar",
1908 srcs: ["foo/bar/MyClass.java"],
1909 sdk_version: "current",
1910 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001911 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001912 }
1913 `,
1914 },
1915 {
1916 name: "Updatable apex with non-stable dep",
1917 expectedError: "cannot depend on \"myjar\"",
1918 bp: `
1919 apex {
1920 name: "myapex",
1921 java_libs: ["myjar"],
1922 key: "myapex.key",
1923 updatable: true,
1924 }
1925 apex_key {
1926 name: "myapex.key",
1927 public_key: "testkey.avbpubkey",
1928 private_key: "testkey.pem",
1929 }
1930 java_library {
1931 name: "myjar",
1932 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001933 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001934 apex_available: ["myapex"],
1935 }
1936 `,
1937 },
1938 {
Paul Duffin043f5e72021-03-05 00:00:01 +00001939 name: "Updatable apex with non-stable transitive dep",
1940 // This is not actually detecting that the transitive dependency is unstable, rather it is
1941 // detecting that the transitive dependency is building against a wider API surface than the
1942 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09001943 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001944 bp: `
1945 apex {
1946 name: "myapex",
1947 java_libs: ["myjar"],
1948 key: "myapex.key",
1949 updatable: true,
1950 }
1951 apex_key {
1952 name: "myapex.key",
1953 public_key: "testkey.avbpubkey",
1954 private_key: "testkey.pem",
1955 }
1956 java_library {
1957 name: "myjar",
1958 srcs: ["foo/bar/MyClass.java"],
1959 sdk_version: "current",
1960 apex_available: ["myapex"],
1961 static_libs: ["transitive-jar"],
1962 }
1963 java_library {
1964 name: "transitive-jar",
1965 srcs: ["foo/bar/MyClass.java"],
1966 sdk_version: "core_platform",
1967 apex_available: ["myapex"],
1968 }
1969 `,
1970 },
1971 }
1972
1973 for _, test := range testCases {
1974 t.Run(test.name, func(t *testing.T) {
1975 if test.expectedError == "" {
1976 testApex(t, test.bp)
1977 } else {
1978 testApexError(t, test.expectedError, test.bp)
1979 }
1980 })
1981 }
1982}
1983
Jooyung Han749dc692020-04-15 11:03:39 +09001984func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
1985 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
1986 apex {
1987 name: "myapex",
1988 key: "myapex.key",
1989 native_shared_libs: ["mylib"],
1990 min_sdk_version: "29",
1991 }
1992
1993 apex_key {
1994 name: "myapex.key",
1995 public_key: "testkey.avbpubkey",
1996 private_key: "testkey.pem",
1997 }
1998
1999 cc_library {
2000 name: "mylib",
2001 srcs: ["mylib.cpp"],
2002 shared_libs: ["mylib2"],
2003 system_shared_libs: [],
2004 stl: "none",
2005 apex_available: [
2006 "myapex",
2007 ],
2008 min_sdk_version: "29",
2009 }
2010
2011 // indirect part of the apex
2012 cc_library {
2013 name: "mylib2",
2014 srcs: ["mylib.cpp"],
2015 system_shared_libs: [],
2016 stl: "none",
2017 apex_available: [
2018 "myapex",
2019 ],
2020 min_sdk_version: "30",
2021 }
2022 `)
2023}
2024
2025func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2026 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2027 apex {
2028 name: "myapex",
2029 key: "myapex.key",
2030 apps: ["AppFoo"],
2031 min_sdk_version: "29",
2032 }
2033
2034 apex_key {
2035 name: "myapex.key",
2036 public_key: "testkey.avbpubkey",
2037 private_key: "testkey.pem",
2038 }
2039
2040 android_app {
2041 name: "AppFoo",
2042 srcs: ["foo/bar/MyClass.java"],
2043 sdk_version: "current",
2044 min_sdk_version: "29",
2045 system_modules: "none",
2046 stl: "none",
2047 static_libs: ["bar"],
2048 apex_available: [ "myapex" ],
2049 }
2050
2051 java_library {
2052 name: "bar",
2053 sdk_version: "current",
2054 srcs: ["a.java"],
2055 apex_available: [ "myapex" ],
2056 }
2057 `)
2058}
2059
2060func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002061 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002062 apex {
2063 name: "myapex",
2064 key: "myapex.key",
2065 native_shared_libs: ["mylib"],
2066 min_sdk_version: "29",
2067 }
2068
2069 apex_key {
2070 name: "myapex.key",
2071 public_key: "testkey.avbpubkey",
2072 private_key: "testkey.pem",
2073 }
2074
Jiyong Park55549df2021-02-26 23:57:23 +09002075 // mylib in myapex will link to mylib2#30
Jooyung Han749dc692020-04-15 11:03:39 +09002076 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2077 cc_library {
2078 name: "mylib",
2079 srcs: ["mylib.cpp"],
2080 shared_libs: ["mylib2"],
2081 system_shared_libs: [],
2082 stl: "none",
2083 apex_available: ["myapex", "otherapex"],
2084 min_sdk_version: "29",
2085 }
2086
2087 cc_library {
2088 name: "mylib2",
2089 srcs: ["mylib.cpp"],
2090 system_shared_libs: [],
2091 stl: "none",
2092 apex_available: ["otherapex"],
2093 stubs: { versions: ["29", "30"] },
2094 min_sdk_version: "30",
2095 }
2096
2097 apex {
2098 name: "otherapex",
2099 key: "myapex.key",
2100 native_shared_libs: ["mylib", "mylib2"],
2101 min_sdk_version: "30",
2102 }
2103 `)
2104 expectLink := func(from, from_variant, to, to_variant string) {
2105 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2106 libFlags := ld.Args["libFlags"]
2107 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2108 }
Jiyong Park55549df2021-02-26 23:57:23 +09002109 expectLink("mylib", "shared_apex29", "mylib2", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07002110 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002111}
2112
Jooyung Haned124c32021-01-26 11:43:46 +09002113func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002114 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2115 func(variables android.FixtureProductVariables) {
2116 variables.Platform_sdk_codename = proptools.StringPtr("S")
2117 variables.Platform_version_active_codenames = []string{"S"}
2118 },
2119 )
Jooyung Haned124c32021-01-26 11:43:46 +09002120 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2121 apex {
2122 name: "myapex",
2123 key: "myapex.key",
2124 native_shared_libs: ["libfoo"],
2125 min_sdk_version: "S",
2126 }
2127 apex_key {
2128 name: "myapex.key",
2129 public_key: "testkey.avbpubkey",
2130 private_key: "testkey.pem",
2131 }
2132 cc_library {
2133 name: "libfoo",
2134 shared_libs: ["libbar"],
2135 apex_available: ["myapex"],
2136 min_sdk_version: "29",
2137 }
2138 cc_library {
2139 name: "libbar",
2140 apex_available: ["myapex"],
2141 }
2142 `, withSAsActiveCodeNames)
2143}
2144
2145func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002146 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2147 variables.Platform_sdk_codename = proptools.StringPtr("S")
2148 variables.Platform_version_active_codenames = []string{"S", "T"}
2149 })
Colin Cross1c460562021-02-16 17:55:47 -08002150 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002151 apex {
2152 name: "myapex",
2153 key: "myapex.key",
2154 native_shared_libs: ["libfoo"],
2155 min_sdk_version: "S",
2156 }
2157 apex_key {
2158 name: "myapex.key",
2159 public_key: "testkey.avbpubkey",
2160 private_key: "testkey.pem",
2161 }
2162 cc_library {
2163 name: "libfoo",
2164 shared_libs: ["libbar"],
2165 apex_available: ["myapex"],
2166 min_sdk_version: "S",
2167 }
2168 cc_library {
2169 name: "libbar",
2170 stubs: {
2171 symbol_file: "libbar.map.txt",
2172 versions: ["30", "S", "T"],
2173 },
2174 }
2175 `, withSAsActiveCodeNames)
2176
2177 // ensure libfoo is linked with "S" version of libbar stub
2178 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2179 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09002180 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_T/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002181}
2182
Jiyong Park7c2ee712018-12-07 00:42:25 +09002183func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002184 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002185 apex {
2186 name: "myapex",
2187 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002188 native_shared_libs: ["mylib"],
2189 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002190 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002191 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002192 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002193 }
2194
2195 apex_key {
2196 name: "myapex.key",
2197 public_key: "testkey.avbpubkey",
2198 private_key: "testkey.pem",
2199 }
2200
2201 prebuilt_etc {
2202 name: "myetc",
2203 src: "myprebuilt",
2204 sub_dir: "foo/bar",
2205 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002206
2207 cc_library {
2208 name: "mylib",
2209 srcs: ["mylib.cpp"],
2210 relative_install_path: "foo/bar",
2211 system_shared_libs: [],
2212 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002213 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002214 }
2215
2216 cc_binary {
2217 name: "mybin",
2218 srcs: ["mylib.cpp"],
2219 relative_install_path: "foo/bar",
2220 system_shared_libs: [],
2221 static_executable: true,
2222 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002223 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002224 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002225 `)
2226
Sundong Ahnabb64432019-10-22 13:58:29 +09002227 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002228 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2229
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002230 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002231 ensureListContains(t, dirs, "etc")
2232 ensureListContains(t, dirs, "etc/foo")
2233 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002234 ensureListContains(t, dirs, "lib64")
2235 ensureListContains(t, dirs, "lib64/foo")
2236 ensureListContains(t, dirs, "lib64/foo/bar")
2237 ensureListContains(t, dirs, "lib")
2238 ensureListContains(t, dirs, "lib/foo")
2239 ensureListContains(t, dirs, "lib/foo/bar")
2240
Jiyong Parkbd13e442019-03-15 18:10:35 +09002241 ensureListContains(t, dirs, "bin")
2242 ensureListContains(t, dirs, "bin/foo")
2243 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002244}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002245
Jooyung Han35155c42020-02-06 17:33:20 +09002246func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002247 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002248 apex {
2249 name: "myapex",
2250 key: "myapex.key",
2251 multilib: {
2252 both: {
2253 native_shared_libs: ["mylib"],
2254 binaries: ["mybin"],
2255 },
2256 },
2257 compile_multilib: "both",
2258 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002259 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002260 }
2261
2262 apex_key {
2263 name: "myapex.key",
2264 public_key: "testkey.avbpubkey",
2265 private_key: "testkey.pem",
2266 }
2267
2268 cc_library {
2269 name: "mylib",
2270 relative_install_path: "foo/bar",
2271 system_shared_libs: [],
2272 stl: "none",
2273 apex_available: [ "myapex" ],
2274 native_bridge_supported: true,
2275 }
2276
2277 cc_binary {
2278 name: "mybin",
2279 relative_install_path: "foo/bar",
2280 system_shared_libs: [],
2281 static_executable: true,
2282 stl: "none",
2283 apex_available: [ "myapex" ],
2284 native_bridge_supported: true,
2285 compile_multilib: "both", // default is "first" for binary
2286 multilib: {
2287 lib64: {
2288 suffix: "64",
2289 },
2290 },
2291 }
2292 `, withNativeBridgeEnabled)
2293 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2294 "bin/foo/bar/mybin",
2295 "bin/foo/bar/mybin64",
2296 "bin/arm/foo/bar/mybin",
2297 "bin/arm64/foo/bar/mybin64",
2298 "lib/foo/bar/mylib.so",
2299 "lib/arm/foo/bar/mylib.so",
2300 "lib64/foo/bar/mylib.so",
2301 "lib64/arm64/foo/bar/mylib.so",
2302 })
2303}
2304
Jiyong Parkda6eb592018-12-19 17:12:36 +09002305func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002306 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002307 apex {
2308 name: "myapex",
2309 key: "myapex.key",
2310 native_shared_libs: ["mylib"],
2311 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002312 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002313 }
2314
2315 apex_key {
2316 name: "myapex.key",
2317 public_key: "testkey.avbpubkey",
2318 private_key: "testkey.pem",
2319 }
2320
2321 cc_library {
2322 name: "mylib",
2323 srcs: ["mylib.cpp"],
2324 shared_libs: ["mylib2"],
2325 system_shared_libs: [],
2326 vendor_available: true,
2327 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002328 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002329 }
2330
2331 cc_library {
2332 name: "mylib2",
2333 srcs: ["mylib.cpp"],
2334 system_shared_libs: [],
2335 vendor_available: true,
2336 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002337 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002338 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002339 `,
2340 setUseVendorAllowListForTest([]string{"myapex"}),
2341 )
Jiyong Parkda6eb592018-12-19 17:12:36 +09002342
2343 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002344 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002345 for _, implicit := range i.Implicits {
2346 inputsList = append(inputsList, implicit.String())
2347 }
2348 }
2349 inputsString := strings.Join(inputsList, " ")
2350
2351 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossaede88c2020-08-11 12:17:01 -07002352 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib.so")
2353 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002354
2355 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002356 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2357 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002358}
Jiyong Park16e91a02018-12-20 18:18:08 +09002359
Jooyung Han85d61762020-06-24 23:50:26 +09002360func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002361 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2362 apex {
2363 name: "myapex",
2364 key: "myapex.key",
2365 use_vendor: true,
2366 }
2367 apex_key {
2368 name: "myapex.key",
2369 public_key: "testkey.avbpubkey",
2370 private_key: "testkey.pem",
2371 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002372 `,
2373 setUseVendorAllowListForTest([]string{""}),
2374 )
Colin Cross440e0d02020-06-11 11:32:11 -07002375 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002376 testApex(t, `
2377 apex {
2378 name: "myapex",
2379 key: "myapex.key",
2380 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002381 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002382 }
2383 apex_key {
2384 name: "myapex.key",
2385 public_key: "testkey.avbpubkey",
2386 private_key: "testkey.pem",
2387 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002388 `,
2389 setUseVendorAllowListForTest([]string{"myapex"}),
2390 )
Jooyung Handc782442019-11-01 03:14:38 +09002391}
2392
Jooyung Han5c998b92019-06-27 11:30:33 +09002393func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2394 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2395 apex {
2396 name: "myapex",
2397 key: "myapex.key",
2398 native_shared_libs: ["mylib"],
2399 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002400 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002401 }
2402
2403 apex_key {
2404 name: "myapex.key",
2405 public_key: "testkey.avbpubkey",
2406 private_key: "testkey.pem",
2407 }
2408
2409 cc_library {
2410 name: "mylib",
2411 srcs: ["mylib.cpp"],
2412 system_shared_libs: [],
2413 stl: "none",
2414 }
2415 `)
2416}
2417
Jooyung Han85d61762020-06-24 23:50:26 +09002418func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002419 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002420 apex {
2421 name: "myapex",
2422 key: "myapex.key",
2423 binaries: ["mybin"],
2424 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002425 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002426 }
2427 apex_key {
2428 name: "myapex.key",
2429 public_key: "testkey.avbpubkey",
2430 private_key: "testkey.pem",
2431 }
2432 cc_binary {
2433 name: "mybin",
2434 vendor: true,
2435 shared_libs: ["libfoo"],
2436 }
2437 cc_library {
2438 name: "libfoo",
2439 proprietary: true,
2440 }
2441 `)
2442
2443 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2444 "bin/mybin",
2445 "lib64/libfoo.so",
2446 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2447 "lib64/libc++.so",
2448 })
2449
2450 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002451 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002452 name := apexBundle.BaseModuleName()
2453 prefix := "TARGET_"
2454 var builder strings.Builder
2455 data.Custom(&builder, name, prefix, "", data)
Paul Duffin37ba3442021-03-29 00:21:08 +01002456 androidMk := android.StringRelativeToTop(ctx.Config(), builder.String())
2457 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002458 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002459
2460 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2461 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2462 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002463}
2464
Jooyung Handf78e212020-07-22 15:54:47 +09002465func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002466 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002467 apex {
2468 name: "myapex",
2469 key: "myapex.key",
2470 binaries: ["mybin"],
2471 vendor: true,
2472 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002473 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002474 }
2475 apex_key {
2476 name: "myapex.key",
2477 public_key: "testkey.avbpubkey",
2478 private_key: "testkey.pem",
2479 }
2480 cc_binary {
2481 name: "mybin",
2482 vendor: true,
2483 shared_libs: ["libvndk", "libvendor"],
2484 }
2485 cc_library {
2486 name: "libvndk",
2487 vndk: {
2488 enabled: true,
2489 },
2490 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002491 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002492 }
2493 cc_library {
2494 name: "libvendor",
2495 vendor: true,
2496 }
2497 `)
2498
2499 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2500
Paul Duffin37ba3442021-03-29 00:21:08 +01002501 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld").RelativeToTop()
Jooyung Handf78e212020-07-22 15:54:47 +09002502 libs := names(ldRule.Args["libFlags"])
2503 // VNDK libs(libvndk/libc++) as they are
Paul Duffin37ba3442021-03-29 00:21:08 +01002504 ensureListContains(t, libs, "out/soong/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2505 ensureListContains(t, libs, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002506 // non-stable Vendor libs as APEX variants
Paul Duffin37ba3442021-03-29 00:21:08 +01002507 ensureListContains(t, libs, "out/soong/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002508
2509 // VNDK libs are not included when use_vndk_as_stable: true
2510 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2511 "bin/mybin",
2512 "lib64/libvendor.so",
2513 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002514
2515 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2516 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2517 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002518}
2519
Justin Yun13decfb2021-03-08 19:25:55 +09002520func TestProductVariant(t *testing.T) {
2521 ctx := testApex(t, `
2522 apex {
2523 name: "myapex",
2524 key: "myapex.key",
2525 updatable: false,
2526 product_specific: true,
2527 binaries: ["foo"],
2528 }
2529
2530 apex_key {
2531 name: "myapex.key",
2532 public_key: "testkey.avbpubkey",
2533 private_key: "testkey.pem",
2534 }
2535
2536 cc_binary {
2537 name: "foo",
2538 product_available: true,
2539 apex_available: ["myapex"],
2540 srcs: ["foo.cpp"],
2541 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002542 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2543 variables.ProductVndkVersion = proptools.StringPtr("current")
2544 }),
2545 )
Justin Yun13decfb2021-03-08 19:25:55 +09002546
2547 cflags := strings.Fields(
2548 ctx.ModuleForTests("foo", "android_product.VER_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
2549 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2550 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2551 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2552 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2553}
2554
Jooyung Han8e5685d2020-09-21 11:02:57 +09002555func TestApex_withPrebuiltFirmware(t *testing.T) {
2556 testCases := []struct {
2557 name string
2558 additionalProp string
2559 }{
2560 {"system apex with prebuilt_firmware", ""},
2561 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2562 }
2563 for _, tc := range testCases {
2564 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002565 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002566 apex {
2567 name: "myapex",
2568 key: "myapex.key",
2569 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002570 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002571 `+tc.additionalProp+`
2572 }
2573 apex_key {
2574 name: "myapex.key",
2575 public_key: "testkey.avbpubkey",
2576 private_key: "testkey.pem",
2577 }
2578 prebuilt_firmware {
2579 name: "myfirmware",
2580 src: "myfirmware.bin",
2581 filename_from_src: true,
2582 `+tc.additionalProp+`
2583 }
2584 `)
2585 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2586 "etc/firmware/myfirmware.bin",
2587 })
2588 })
2589 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002590}
2591
Jooyung Hanefb184e2020-06-25 17:14:25 +09002592func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002593 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002594 apex {
2595 name: "myapex",
2596 key: "myapex.key",
2597 use_vendor: true,
2598 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002599 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002600 }
2601
2602 apex_key {
2603 name: "myapex.key",
2604 public_key: "testkey.avbpubkey",
2605 private_key: "testkey.pem",
2606 }
2607
2608 cc_library {
2609 name: "mylib",
2610 vendor_available: true,
2611 apex_available: ["myapex"],
2612 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002613 `,
2614 setUseVendorAllowListForTest([]string{"myapex"}),
2615 )
Jooyung Hanefb184e2020-06-25 17:14:25 +09002616
2617 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002618 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002619 name := apexBundle.BaseModuleName()
2620 prefix := "TARGET_"
2621 var builder strings.Builder
2622 data.Custom(&builder, name, prefix, "", data)
2623 androidMk := builder.String()
2624 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2625}
2626
2627func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002628 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002629 apex {
2630 name: "myapex",
2631 key: "myapex.key",
2632 vendor: true,
2633 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002634 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002635 }
2636
2637 apex_key {
2638 name: "myapex.key",
2639 public_key: "testkey.avbpubkey",
2640 private_key: "testkey.pem",
2641 }
2642
2643 cc_library {
2644 name: "mylib",
2645 vendor_available: true,
2646 }
2647 `)
2648
2649 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002650 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002651 name := apexBundle.BaseModuleName()
2652 prefix := "TARGET_"
2653 var builder strings.Builder
2654 data.Custom(&builder, name, prefix, "", data)
2655 androidMk := builder.String()
2656 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2657}
2658
Jooyung Han2ed99d02020-06-24 23:26:26 +09002659func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002660 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002661 apex {
2662 name: "myapex",
2663 key: "myapex.key",
2664 vintf_fragments: ["fragment.xml"],
2665 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002666 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002667 }
2668 apex_key {
2669 name: "myapex.key",
2670 public_key: "testkey.avbpubkey",
2671 private_key: "testkey.pem",
2672 }
2673 cc_binary {
2674 name: "mybin",
2675 }
2676 `)
2677
2678 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002679 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002680 name := apexBundle.BaseModuleName()
2681 prefix := "TARGET_"
2682 var builder strings.Builder
2683 data.Custom(&builder, name, prefix, "", data)
2684 androidMk := builder.String()
2685 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2686 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2687}
2688
Jiyong Park16e91a02018-12-20 18:18:08 +09002689func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002690 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002691 apex {
2692 name: "myapex",
2693 key: "myapex.key",
2694 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002695 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002696 }
2697
2698 apex_key {
2699 name: "myapex.key",
2700 public_key: "testkey.avbpubkey",
2701 private_key: "testkey.pem",
2702 }
2703
2704 cc_library {
2705 name: "mylib",
2706 srcs: ["mylib.cpp"],
2707 system_shared_libs: [],
2708 stl: "none",
2709 stubs: {
2710 versions: ["1", "2", "3"],
2711 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002712 apex_available: [
2713 "//apex_available:platform",
2714 "myapex",
2715 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002716 }
2717
2718 cc_binary {
2719 name: "not_in_apex",
2720 srcs: ["mylib.cpp"],
2721 static_libs: ["mylib"],
2722 static_executable: true,
2723 system_shared_libs: [],
2724 stl: "none",
2725 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002726 `)
2727
Colin Cross7113d202019-11-20 16:39:12 -08002728 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002729
2730 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002731 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002732}
Jiyong Park9335a262018-12-24 11:31:58 +09002733
2734func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002735 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002736 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002737 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002738 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002739 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002740 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002741 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002742 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002743 }
2744
2745 cc_library {
2746 name: "mylib",
2747 srcs: ["mylib.cpp"],
2748 system_shared_libs: [],
2749 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002750 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002751 }
2752
2753 apex_key {
2754 name: "myapex.key",
2755 public_key: "testkey.avbpubkey",
2756 private_key: "testkey.pem",
2757 }
2758
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002759 android_app_certificate {
2760 name: "myapex.certificate",
2761 certificate: "testkey",
2762 }
2763
2764 android_app_certificate {
2765 name: "myapex.certificate.override",
2766 certificate: "testkey.override",
2767 }
2768
Jiyong Park9335a262018-12-24 11:31:58 +09002769 `)
2770
2771 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002772 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002773
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002774 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2775 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002776 "vendor/foo/devkeys/testkey.avbpubkey")
2777 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002778 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2779 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002780 "vendor/foo/devkeys/testkey.pem")
2781 }
2782
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002783 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002784 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002785 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002786 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002787 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002788 }
2789}
Jiyong Park58e364a2019-01-19 19:24:06 +09002790
Jooyung Hanf121a652019-12-17 14:30:11 +09002791func TestCertificate(t *testing.T) {
2792 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002793 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002794 apex {
2795 name: "myapex",
2796 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002797 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002798 }
2799 apex_key {
2800 name: "myapex.key",
2801 public_key: "testkey.avbpubkey",
2802 private_key: "testkey.pem",
2803 }`)
2804 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2805 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2806 if actual := rule.Args["certificates"]; actual != expected {
2807 t.Errorf("certificates should be %q, not %q", expected, actual)
2808 }
2809 })
2810 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002811 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002812 apex {
2813 name: "myapex_keytest",
2814 key: "myapex.key",
2815 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002816 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002817 }
2818 apex_key {
2819 name: "myapex.key",
2820 public_key: "testkey.avbpubkey",
2821 private_key: "testkey.pem",
2822 }
2823 android_app_certificate {
2824 name: "myapex.certificate.override",
2825 certificate: "testkey.override",
2826 }`)
2827 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2828 expected := "testkey.override.x509.pem testkey.override.pk8"
2829 if actual := rule.Args["certificates"]; actual != expected {
2830 t.Errorf("certificates should be %q, not %q", expected, actual)
2831 }
2832 })
2833 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002834 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002835 apex {
2836 name: "myapex",
2837 key: "myapex.key",
2838 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002839 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002840 }
2841 apex_key {
2842 name: "myapex.key",
2843 public_key: "testkey.avbpubkey",
2844 private_key: "testkey.pem",
2845 }
2846 android_app_certificate {
2847 name: "myapex.certificate",
2848 certificate: "testkey",
2849 }`)
2850 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2851 expected := "testkey.x509.pem testkey.pk8"
2852 if actual := rule.Args["certificates"]; actual != expected {
2853 t.Errorf("certificates should be %q, not %q", expected, actual)
2854 }
2855 })
2856 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002857 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002858 apex {
2859 name: "myapex_keytest",
2860 key: "myapex.key",
2861 file_contexts: ":myapex-file_contexts",
2862 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002863 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002864 }
2865 apex_key {
2866 name: "myapex.key",
2867 public_key: "testkey.avbpubkey",
2868 private_key: "testkey.pem",
2869 }
2870 android_app_certificate {
2871 name: "myapex.certificate.override",
2872 certificate: "testkey.override",
2873 }`)
2874 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2875 expected := "testkey.override.x509.pem testkey.override.pk8"
2876 if actual := rule.Args["certificates"]; actual != expected {
2877 t.Errorf("certificates should be %q, not %q", expected, actual)
2878 }
2879 })
2880 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002881 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002882 apex {
2883 name: "myapex",
2884 key: "myapex.key",
2885 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002886 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002887 }
2888 apex_key {
2889 name: "myapex.key",
2890 public_key: "testkey.avbpubkey",
2891 private_key: "testkey.pem",
2892 }`)
2893 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2894 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2895 if actual := rule.Args["certificates"]; actual != expected {
2896 t.Errorf("certificates should be %q, not %q", expected, actual)
2897 }
2898 })
2899 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002900 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002901 apex {
2902 name: "myapex_keytest",
2903 key: "myapex.key",
2904 file_contexts: ":myapex-file_contexts",
2905 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002906 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002907 }
2908 apex_key {
2909 name: "myapex.key",
2910 public_key: "testkey.avbpubkey",
2911 private_key: "testkey.pem",
2912 }
2913 android_app_certificate {
2914 name: "myapex.certificate.override",
2915 certificate: "testkey.override",
2916 }`)
2917 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2918 expected := "testkey.override.x509.pem testkey.override.pk8"
2919 if actual := rule.Args["certificates"]; actual != expected {
2920 t.Errorf("certificates should be %q, not %q", expected, actual)
2921 }
2922 })
2923}
2924
Jiyong Park58e364a2019-01-19 19:24:06 +09002925func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002926 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002927 apex {
2928 name: "myapex",
2929 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002930 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002931 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002932 }
2933
2934 apex {
2935 name: "otherapex",
2936 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002937 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002938 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002939 }
2940
2941 apex_key {
2942 name: "myapex.key",
2943 public_key: "testkey.avbpubkey",
2944 private_key: "testkey.pem",
2945 }
2946
2947 cc_library {
2948 name: "mylib",
2949 srcs: ["mylib.cpp"],
2950 system_shared_libs: [],
2951 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002952 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002953 "myapex",
2954 "otherapex",
2955 ],
Jooyung Han24282772020-03-21 23:20:55 +09002956 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002957 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002958 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002959 cc_library {
2960 name: "mylib2",
2961 srcs: ["mylib.cpp"],
2962 system_shared_libs: [],
2963 stl: "none",
2964 apex_available: [
2965 "myapex",
2966 "otherapex",
2967 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002968 static_libs: ["mylib3"],
2969 recovery_available: true,
2970 min_sdk_version: "29",
2971 }
2972 cc_library {
2973 name: "mylib3",
2974 srcs: ["mylib.cpp"],
2975 system_shared_libs: [],
2976 stl: "none",
2977 apex_available: [
2978 "myapex",
2979 "otherapex",
2980 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09002981 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07002982 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002983 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002984 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002985 `)
2986
Jooyung Hanc87a0592020-03-02 17:44:33 +09002987 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002988 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09002989 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002990 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002991
Jooyung Hanccce2f22020-03-07 03:45:53 +09002992 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07002993 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09002994 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08002995 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09002996 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09002997
Jooyung Hanccce2f22020-03-07 03:45:53 +09002998 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07002999 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003000 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003001 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003002 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003003
Colin Crossaede88c2020-08-11 12:17:01 -07003004 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3005 // each variant defines additional macros to distinguish which apex variant it is built for
3006
3007 // non-APEX variant does not have __ANDROID_APEX__ defined
3008 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3009 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3010
3011 // APEX variant has __ANDROID_APEX__ defined
3012 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3013 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3014 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3015 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3016
3017 // APEX variant has __ANDROID_APEX__ defined
3018 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3019 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3020 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3021 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3022
Dan Albertb19953d2020-11-17 15:29:36 -08003023 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003024 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3025 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003026 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003027
3028 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3029 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003030
3031 // non-APEX variant does not have __ANDROID_APEX__ defined
3032 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3033 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3034
3035 // APEX variant has __ANDROID_APEX__ defined
3036 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003037 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003038 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003039 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003040
Jooyung Hanc87a0592020-03-02 17:44:33 +09003041 // APEX variant has __ANDROID_APEX__ defined
3042 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003043 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003044 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003045 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003046
Dan Albertb19953d2020-11-17 15:29:36 -08003047 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003048 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003049 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003050 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003051}
Jiyong Park7e636d02019-01-28 16:16:54 +09003052
3053func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003054 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003055 apex {
3056 name: "myapex",
3057 key: "myapex.key",
3058 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003059 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003060 }
3061
3062 apex_key {
3063 name: "myapex.key",
3064 public_key: "testkey.avbpubkey",
3065 private_key: "testkey.pem",
3066 }
3067
3068 cc_library_headers {
3069 name: "mylib_headers",
3070 export_include_dirs: ["my_include"],
3071 system_shared_libs: [],
3072 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003073 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003074 }
3075
3076 cc_library {
3077 name: "mylib",
3078 srcs: ["mylib.cpp"],
3079 system_shared_libs: [],
3080 stl: "none",
3081 header_libs: ["mylib_headers"],
3082 export_header_lib_headers: ["mylib_headers"],
3083 stubs: {
3084 versions: ["1", "2", "3"],
3085 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003086 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003087 }
3088
3089 cc_library {
3090 name: "otherlib",
3091 srcs: ["mylib.cpp"],
3092 system_shared_libs: [],
3093 stl: "none",
3094 shared_libs: ["mylib"],
3095 }
3096 `)
3097
Colin Cross7113d202019-11-20 16:39:12 -08003098 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003099
3100 // Ensure that the include path of the header lib is exported to 'otherlib'
3101 ensureContains(t, cFlags, "-Imy_include")
3102}
Alex Light9670d332019-01-29 18:07:33 -08003103
Jiyong Park7cd10e32020-01-14 09:22:18 +09003104type fileInApex struct {
3105 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003106 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003107 isLink bool
3108}
3109
Jooyung Hana57af4a2020-01-23 05:36:59 +00003110func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003111 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003112 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003113 copyCmds := apexRule.Args["copy_commands"]
3114 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003115 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003116 for _, cmd := range strings.Split(copyCmds, "&&") {
3117 cmd = strings.TrimSpace(cmd)
3118 if cmd == "" {
3119 continue
3120 }
3121 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003122 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003123 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003124 switch terms[0] {
3125 case "mkdir":
3126 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003127 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003128 t.Fatal("copyCmds contains invalid cp command", cmd)
3129 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003130 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003131 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003132 isLink = false
3133 case "ln":
3134 if len(terms) != 3 && len(terms) != 4 {
3135 // ln LINK TARGET or ln -s LINK TARGET
3136 t.Fatal("copyCmds contains invalid ln command", cmd)
3137 }
3138 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003139 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003140 isLink = true
3141 default:
3142 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3143 }
3144 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003145 index := strings.Index(dst, imageApexDir)
3146 if index == -1 {
3147 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3148 }
3149 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003150 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003151 }
3152 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003153 return ret
3154}
3155
Jooyung Hana57af4a2020-01-23 05:36:59 +00003156func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3157 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003158 var failed bool
3159 var surplus []string
3160 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003161 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003162 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003163 for _, expected := range files {
3164 if matched, _ := path.Match(expected, file.path); matched {
3165 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003166 mactchFound = true
3167 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003168 }
3169 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003170 if !mactchFound {
3171 surplus = append(surplus, file.path)
3172 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003173 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003174
Jooyung Han31c470b2019-10-18 16:26:59 +09003175 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003176 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003177 t.Log("surplus files", surplus)
3178 failed = true
3179 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003180
3181 if len(files) > len(filesMatched) {
3182 var missing []string
3183 for _, expected := range files {
3184 if !filesMatched[expected] {
3185 missing = append(missing, expected)
3186 }
3187 }
3188 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003189 t.Log("missing files", missing)
3190 failed = true
3191 }
3192 if failed {
3193 t.Fail()
3194 }
3195}
3196
Jooyung Han344d5432019-08-23 11:17:39 +09003197func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003198 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003199 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003200 name: "com.android.vndk.current",
3201 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003202 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003203 }
3204
3205 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003206 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003207 public_key: "testkey.avbpubkey",
3208 private_key: "testkey.pem",
3209 }
3210
3211 cc_library {
3212 name: "libvndk",
3213 srcs: ["mylib.cpp"],
3214 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003215 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003216 vndk: {
3217 enabled: true,
3218 },
3219 system_shared_libs: [],
3220 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003221 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003222 }
3223
3224 cc_library {
3225 name: "libvndksp",
3226 srcs: ["mylib.cpp"],
3227 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003228 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003229 vndk: {
3230 enabled: true,
3231 support_system_process: true,
3232 },
3233 system_shared_libs: [],
3234 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003235 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003236 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003237 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003238
Colin Cross2807f002021-03-02 10:15:29 -08003239 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003240 "lib/libvndk.so",
3241 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003242 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003243 "lib64/libvndk.so",
3244 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003245 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003246 "etc/llndk.libraries.VER.txt",
3247 "etc/vndkcore.libraries.VER.txt",
3248 "etc/vndksp.libraries.VER.txt",
3249 "etc/vndkprivate.libraries.VER.txt",
Justin Yun8a2600c2020-12-07 12:44:03 +09003250 "etc/vndkproduct.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003251 })
Jooyung Han344d5432019-08-23 11:17:39 +09003252}
3253
3254func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003255 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003256 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003257 name: "com.android.vndk.current",
3258 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003259 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003260 }
3261
3262 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003263 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003264 public_key: "testkey.avbpubkey",
3265 private_key: "testkey.pem",
3266 }
3267
3268 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003269 name: "libvndk",
3270 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003271 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003272 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003273 vndk: {
3274 enabled: true,
3275 },
3276 system_shared_libs: [],
3277 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003278 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003279 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003280
3281 cc_prebuilt_library_shared {
3282 name: "libvndk.arm",
3283 srcs: ["libvndk.arm.so"],
3284 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003285 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003286 vndk: {
3287 enabled: true,
3288 },
3289 enabled: false,
3290 arch: {
3291 arm: {
3292 enabled: true,
3293 },
3294 },
3295 system_shared_libs: [],
3296 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003297 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003298 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003299 `+vndkLibrariesTxtFiles("current"),
3300 withFiles(map[string][]byte{
3301 "libvndk.so": nil,
3302 "libvndk.arm.so": nil,
3303 }))
Colin Cross2807f002021-03-02 10:15:29 -08003304 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003305 "lib/libvndk.so",
3306 "lib/libvndk.arm.so",
3307 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003308 "lib/libc++.so",
3309 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003310 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003311 })
Jooyung Han344d5432019-08-23 11:17:39 +09003312}
3313
Jooyung Han39edb6c2019-11-06 16:53:07 +09003314func vndkLibrariesTxtFiles(vers ...string) (result string) {
3315 for _, v := range vers {
3316 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003317 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003318 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003319 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003320 name: "` + txt + `.libraries.txt",
3321 }
3322 `
3323 }
3324 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003325 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003326 result += `
3327 prebuilt_etc {
3328 name: "` + txt + `.libraries.` + v + `.txt",
3329 src: "dummy.txt",
3330 }
3331 `
3332 }
3333 }
3334 }
3335 return
3336}
3337
Jooyung Han344d5432019-08-23 11:17:39 +09003338func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003339 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003340 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003341 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003342 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003343 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003344 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003345 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003346 }
3347
3348 apex_key {
3349 name: "myapex.key",
3350 public_key: "testkey.avbpubkey",
3351 private_key: "testkey.pem",
3352 }
3353
Jooyung Han31c470b2019-10-18 16:26:59 +09003354 vndk_prebuilt_shared {
3355 name: "libvndk27",
3356 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003357 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003358 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003359 vndk: {
3360 enabled: true,
3361 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003362 target_arch: "arm64",
3363 arch: {
3364 arm: {
3365 srcs: ["libvndk27_arm.so"],
3366 },
3367 arm64: {
3368 srcs: ["libvndk27_arm64.so"],
3369 },
3370 },
Colin Cross2807f002021-03-02 10:15:29 -08003371 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003372 }
3373
3374 vndk_prebuilt_shared {
3375 name: "libvndk27",
3376 version: "27",
3377 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003378 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003379 vndk: {
3380 enabled: true,
3381 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003382 target_arch: "x86_64",
3383 arch: {
3384 x86: {
3385 srcs: ["libvndk27_x86.so"],
3386 },
3387 x86_64: {
3388 srcs: ["libvndk27_x86_64.so"],
3389 },
3390 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003391 }
3392 `+vndkLibrariesTxtFiles("27"),
3393 withFiles(map[string][]byte{
3394 "libvndk27_arm.so": nil,
3395 "libvndk27_arm64.so": nil,
3396 "libvndk27_x86.so": nil,
3397 "libvndk27_x86_64.so": nil,
3398 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003399
Colin Cross2807f002021-03-02 10:15:29 -08003400 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003401 "lib/libvndk27_arm.so",
3402 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003403 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003404 })
Jooyung Han344d5432019-08-23 11:17:39 +09003405}
3406
Jooyung Han90eee022019-10-01 20:02:42 +09003407func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003408 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003409 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003410 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003411 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003412 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003413 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003414 }
3415 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003416 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003417 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003418 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003419 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003420 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003421 }
3422 apex_key {
3423 name: "myapex.key",
3424 public_key: "testkey.avbpubkey",
3425 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003426 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003427
3428 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003429 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003430 actual := proptools.String(bundle.properties.Apex_name)
3431 if !reflect.DeepEqual(actual, expected) {
3432 t.Errorf("Got '%v', expected '%v'", actual, expected)
3433 }
3434 }
3435
Colin Cross2807f002021-03-02 10:15:29 -08003436 assertApexName("com.android.vndk.vVER", "com.android.vndk.current")
3437 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003438}
3439
Jooyung Han344d5432019-08-23 11:17:39 +09003440func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003441 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003442 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003443 name: "com.android.vndk.current",
3444 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003445 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003446 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003447 }
3448
3449 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003450 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003451 public_key: "testkey.avbpubkey",
3452 private_key: "testkey.pem",
3453 }
3454
3455 cc_library {
3456 name: "libvndk",
3457 srcs: ["mylib.cpp"],
3458 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003459 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003460 native_bridge_supported: true,
3461 host_supported: true,
3462 vndk: {
3463 enabled: true,
3464 },
3465 system_shared_libs: [],
3466 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003467 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003468 }
Colin Cross2807f002021-03-02 10:15:29 -08003469 `+vndkLibrariesTxtFiles("current"),
3470 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003471
Colin Cross2807f002021-03-02 10:15:29 -08003472 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003473 "lib/libvndk.so",
3474 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003475 "lib/libc++.so",
3476 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003477 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003478 })
Jooyung Han344d5432019-08-23 11:17:39 +09003479}
3480
3481func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003482 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003483 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003484 name: "com.android.vndk.current",
3485 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003486 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003487 native_bridge_supported: true,
3488 }
3489
3490 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003491 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003492 public_key: "testkey.avbpubkey",
3493 private_key: "testkey.pem",
3494 }
3495
3496 cc_library {
3497 name: "libvndk",
3498 srcs: ["mylib.cpp"],
3499 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003500 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003501 native_bridge_supported: true,
3502 host_supported: true,
3503 vndk: {
3504 enabled: true,
3505 },
3506 system_shared_libs: [],
3507 stl: "none",
3508 }
3509 `)
3510}
3511
Jooyung Han31c470b2019-10-18 16:26:59 +09003512func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003513 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003514 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003515 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003516 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003517 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003518 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003519 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003520 }
3521
3522 apex_key {
3523 name: "myapex.key",
3524 public_key: "testkey.avbpubkey",
3525 private_key: "testkey.pem",
3526 }
3527
3528 vndk_prebuilt_shared {
3529 name: "libvndk27",
3530 version: "27",
3531 target_arch: "arm",
3532 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003533 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003534 vndk: {
3535 enabled: true,
3536 },
3537 arch: {
3538 arm: {
3539 srcs: ["libvndk27.so"],
3540 }
3541 },
3542 }
3543
3544 vndk_prebuilt_shared {
3545 name: "libvndk27",
3546 version: "27",
3547 target_arch: "arm",
3548 binder32bit: true,
3549 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003550 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003551 vndk: {
3552 enabled: true,
3553 },
3554 arch: {
3555 arm: {
3556 srcs: ["libvndk27binder32.so"],
3557 }
3558 },
Colin Cross2807f002021-03-02 10:15:29 -08003559 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003560 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003561 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003562 withFiles(map[string][]byte{
3563 "libvndk27.so": nil,
3564 "libvndk27binder32.so": nil,
3565 }),
3566 withBinder32bit,
3567 withTargets(map[android.OsType][]android.Target{
3568 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003569 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3570 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003571 },
3572 }),
3573 )
3574
Colin Cross2807f002021-03-02 10:15:29 -08003575 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003576 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003577 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003578 })
3579}
3580
Jooyung Han45a96772020-06-15 14:59:42 +09003581func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003582 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003583 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003584 name: "com.android.vndk.current",
3585 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003586 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003587 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003588 }
3589
3590 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003591 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003592 public_key: "testkey.avbpubkey",
3593 private_key: "testkey.pem",
3594 }
3595
3596 cc_library {
3597 name: "libz",
3598 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003599 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003600 vndk: {
3601 enabled: true,
3602 },
3603 stubs: {
3604 symbol_file: "libz.map.txt",
3605 versions: ["30"],
3606 }
3607 }
3608 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3609 "libz.map.txt": nil,
3610 }))
3611
Colin Cross2807f002021-03-02 10:15:29 -08003612 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003613 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3614 ensureListEmpty(t, provideNativeLibs)
3615}
3616
Jooyung Hane1633032019-08-01 17:41:43 +09003617func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003618 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003619 apex {
3620 name: "myapex_nodep",
3621 key: "myapex.key",
3622 native_shared_libs: ["lib_nodep"],
3623 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003624 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003625 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003626 }
3627
3628 apex {
3629 name: "myapex_dep",
3630 key: "myapex.key",
3631 native_shared_libs: ["lib_dep"],
3632 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003633 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003634 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003635 }
3636
3637 apex {
3638 name: "myapex_provider",
3639 key: "myapex.key",
3640 native_shared_libs: ["libfoo"],
3641 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003642 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003643 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003644 }
3645
3646 apex {
3647 name: "myapex_selfcontained",
3648 key: "myapex.key",
3649 native_shared_libs: ["lib_dep", "libfoo"],
3650 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003651 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003652 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003653 }
3654
3655 apex_key {
3656 name: "myapex.key",
3657 public_key: "testkey.avbpubkey",
3658 private_key: "testkey.pem",
3659 }
3660
3661 cc_library {
3662 name: "lib_nodep",
3663 srcs: ["mylib.cpp"],
3664 system_shared_libs: [],
3665 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003666 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003667 }
3668
3669 cc_library {
3670 name: "lib_dep",
3671 srcs: ["mylib.cpp"],
3672 shared_libs: ["libfoo"],
3673 system_shared_libs: [],
3674 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003675 apex_available: [
3676 "myapex_dep",
3677 "myapex_provider",
3678 "myapex_selfcontained",
3679 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003680 }
3681
3682 cc_library {
3683 name: "libfoo",
3684 srcs: ["mytest.cpp"],
3685 stubs: {
3686 versions: ["1"],
3687 },
3688 system_shared_libs: [],
3689 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003690 apex_available: [
3691 "myapex_provider",
3692 "myapex_selfcontained",
3693 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003694 }
3695 `)
3696
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003697 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003698 var provideNativeLibs, requireNativeLibs []string
3699
Sundong Ahnabb64432019-10-22 13:58:29 +09003700 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003701 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3702 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003703 ensureListEmpty(t, provideNativeLibs)
3704 ensureListEmpty(t, requireNativeLibs)
3705
Sundong Ahnabb64432019-10-22 13:58:29 +09003706 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003707 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3708 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003709 ensureListEmpty(t, provideNativeLibs)
3710 ensureListContains(t, requireNativeLibs, "libfoo.so")
3711
Sundong Ahnabb64432019-10-22 13:58:29 +09003712 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003713 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3714 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003715 ensureListContains(t, provideNativeLibs, "libfoo.so")
3716 ensureListEmpty(t, requireNativeLibs)
3717
Sundong Ahnabb64432019-10-22 13:58:29 +09003718 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003719 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3720 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003721 ensureListContains(t, provideNativeLibs, "libfoo.so")
3722 ensureListEmpty(t, requireNativeLibs)
3723}
3724
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003725func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003726 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003727 apex {
3728 name: "myapex",
3729 key: "myapex.key",
3730 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003731 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003732 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003733 }
3734
3735 apex_key {
3736 name: "myapex.key",
3737 public_key: "testkey.avbpubkey",
3738 private_key: "testkey.pem",
3739 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003740
3741 cc_library {
3742 name: "mylib",
3743 srcs: ["mylib.cpp"],
3744 system_shared_libs: [],
3745 stl: "none",
3746 apex_available: [
3747 "//apex_available:platform",
3748 "myapex",
3749 ],
3750 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003751 `)
3752
Sundong Ahnabb64432019-10-22 13:58:29 +09003753 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003754 apexManifestRule := module.Rule("apexManifestRule")
3755 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3756 apexRule := module.Rule("apexRule")
3757 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003758
3759 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003760 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003761 name := apexBundle.BaseModuleName()
3762 prefix := "TARGET_"
3763 var builder strings.Builder
3764 data.Custom(&builder, name, prefix, "", data)
3765 androidMk := builder.String()
3766 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3767 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003768}
3769
Alex Light0851b882019-02-07 13:20:53 -08003770func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003771 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003772 apex {
3773 name: "myapex",
3774 key: "myapex.key",
3775 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003776 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003777 }
3778
3779 apex_key {
3780 name: "myapex.key",
3781 public_key: "testkey.avbpubkey",
3782 private_key: "testkey.pem",
3783 }
3784
3785 cc_library {
3786 name: "mylib_common",
3787 srcs: ["mylib.cpp"],
3788 system_shared_libs: [],
3789 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003790 apex_available: [
3791 "//apex_available:platform",
3792 "myapex",
3793 ],
Alex Light0851b882019-02-07 13:20:53 -08003794 }
3795 `)
3796
Sundong Ahnabb64432019-10-22 13:58:29 +09003797 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003798 apexRule := module.Rule("apexRule")
3799 copyCmds := apexRule.Args["copy_commands"]
3800
3801 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3802 t.Log("Apex was a test apex!")
3803 t.Fail()
3804 }
3805 // Ensure that main rule creates an output
3806 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3807
3808 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003809 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003810
3811 // Ensure that both direct and indirect deps are copied into apex
3812 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3813
Colin Cross7113d202019-11-20 16:39:12 -08003814 // Ensure that the platform variant ends with _shared
3815 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003816
Colin Cross56a83212020-09-15 18:30:11 -07003817 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003818 t.Log("Found mylib_common not in any apex!")
3819 t.Fail()
3820 }
3821}
3822
3823func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003824 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003825 apex_test {
3826 name: "myapex",
3827 key: "myapex.key",
3828 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003829 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003830 }
3831
3832 apex_key {
3833 name: "myapex.key",
3834 public_key: "testkey.avbpubkey",
3835 private_key: "testkey.pem",
3836 }
3837
3838 cc_library {
3839 name: "mylib_common_test",
3840 srcs: ["mylib.cpp"],
3841 system_shared_libs: [],
3842 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003843 // TODO: remove //apex_available:platform
3844 apex_available: [
3845 "//apex_available:platform",
3846 "myapex",
3847 ],
Alex Light0851b882019-02-07 13:20:53 -08003848 }
3849 `)
3850
Sundong Ahnabb64432019-10-22 13:58:29 +09003851 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003852 apexRule := module.Rule("apexRule")
3853 copyCmds := apexRule.Args["copy_commands"]
3854
3855 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3856 t.Log("Apex was not a test apex!")
3857 t.Fail()
3858 }
3859 // Ensure that main rule creates an output
3860 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3861
3862 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003863 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003864
3865 // Ensure that both direct and indirect deps are copied into apex
3866 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3867
Colin Cross7113d202019-11-20 16:39:12 -08003868 // Ensure that the platform variant ends with _shared
3869 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003870}
3871
Alex Light9670d332019-01-29 18:07:33 -08003872func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003873 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003874 apex {
3875 name: "myapex",
3876 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003877 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003878 multilib: {
3879 first: {
3880 native_shared_libs: ["mylib_common"],
3881 }
3882 },
3883 target: {
3884 android: {
3885 multilib: {
3886 first: {
3887 native_shared_libs: ["mylib"],
3888 }
3889 }
3890 },
3891 host: {
3892 multilib: {
3893 first: {
3894 native_shared_libs: ["mylib2"],
3895 }
3896 }
3897 }
3898 }
3899 }
3900
3901 apex_key {
3902 name: "myapex.key",
3903 public_key: "testkey.avbpubkey",
3904 private_key: "testkey.pem",
3905 }
3906
3907 cc_library {
3908 name: "mylib",
3909 srcs: ["mylib.cpp"],
3910 system_shared_libs: [],
3911 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003912 // TODO: remove //apex_available:platform
3913 apex_available: [
3914 "//apex_available:platform",
3915 "myapex",
3916 ],
Alex Light9670d332019-01-29 18:07:33 -08003917 }
3918
3919 cc_library {
3920 name: "mylib_common",
3921 srcs: ["mylib.cpp"],
3922 system_shared_libs: [],
3923 stl: "none",
3924 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003925 // TODO: remove //apex_available:platform
3926 apex_available: [
3927 "//apex_available:platform",
3928 "myapex",
3929 ],
Alex Light9670d332019-01-29 18:07:33 -08003930 }
3931
3932 cc_library {
3933 name: "mylib2",
3934 srcs: ["mylib.cpp"],
3935 system_shared_libs: [],
3936 stl: "none",
3937 compile_multilib: "first",
3938 }
3939 `)
3940
Sundong Ahnabb64432019-10-22 13:58:29 +09003941 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003942 copyCmds := apexRule.Args["copy_commands"]
3943
3944 // Ensure that main rule creates an output
3945 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3946
3947 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003948 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3949 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3950 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003951
3952 // Ensure that both direct and indirect deps are copied into apex
3953 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3954 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3955 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3956
Colin Cross7113d202019-11-20 16:39:12 -08003957 // Ensure that the platform variant ends with _shared
3958 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3959 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3960 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003961}
Jiyong Park04480cf2019-02-06 00:16:29 +09003962
Jiyong Park59140302020-12-14 18:44:04 +09003963func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003964 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09003965 apex {
3966 name: "myapex",
3967 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003968 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09003969 arch: {
3970 arm64: {
3971 native_shared_libs: ["mylib.arm64"],
3972 },
3973 x86_64: {
3974 native_shared_libs: ["mylib.x64"],
3975 },
3976 }
3977 }
3978
3979 apex_key {
3980 name: "myapex.key",
3981 public_key: "testkey.avbpubkey",
3982 private_key: "testkey.pem",
3983 }
3984
3985 cc_library {
3986 name: "mylib.arm64",
3987 srcs: ["mylib.cpp"],
3988 system_shared_libs: [],
3989 stl: "none",
3990 // TODO: remove //apex_available:platform
3991 apex_available: [
3992 "//apex_available:platform",
3993 "myapex",
3994 ],
3995 }
3996
3997 cc_library {
3998 name: "mylib.x64",
3999 srcs: ["mylib.cpp"],
4000 system_shared_libs: [],
4001 stl: "none",
4002 // TODO: remove //apex_available:platform
4003 apex_available: [
4004 "//apex_available:platform",
4005 "myapex",
4006 ],
4007 }
4008 `)
4009
4010 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4011 copyCmds := apexRule.Args["copy_commands"]
4012
4013 // Ensure that apex variant is created for the direct dep
4014 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4015 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4016
4017 // Ensure that both direct and indirect deps are copied into apex
4018 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4019 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4020}
4021
Jiyong Park04480cf2019-02-06 00:16:29 +09004022func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004023 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004024 apex {
4025 name: "myapex",
4026 key: "myapex.key",
4027 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004028 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004029 }
4030
4031 apex_key {
4032 name: "myapex.key",
4033 public_key: "testkey.avbpubkey",
4034 private_key: "testkey.pem",
4035 }
4036
4037 sh_binary {
4038 name: "myscript",
4039 src: "mylib.cpp",
4040 filename: "myscript.sh",
4041 sub_dir: "script",
4042 }
4043 `)
4044
Sundong Ahnabb64432019-10-22 13:58:29 +09004045 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004046 copyCmds := apexRule.Args["copy_commands"]
4047
4048 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4049}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004050
Jooyung Han91df2082019-11-20 01:49:42 +09004051func TestApexInVariousPartition(t *testing.T) {
4052 testcases := []struct {
4053 propName, parition, flattenedPartition string
4054 }{
4055 {"", "system", "system_ext"},
4056 {"product_specific: true", "product", "product"},
4057 {"soc_specific: true", "vendor", "vendor"},
4058 {"proprietary: true", "vendor", "vendor"},
4059 {"vendor: true", "vendor", "vendor"},
4060 {"system_ext_specific: true", "system_ext", "system_ext"},
4061 }
4062 for _, tc := range testcases {
4063 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004064 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004065 apex {
4066 name: "myapex",
4067 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004068 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004069 `+tc.propName+`
4070 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004071
Jooyung Han91df2082019-11-20 01:49:42 +09004072 apex_key {
4073 name: "myapex.key",
4074 public_key: "testkey.avbpubkey",
4075 private_key: "testkey.pem",
4076 }
4077 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004078
Jooyung Han91df2082019-11-20 01:49:42 +09004079 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004080 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4081 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004082 if actual != expected {
4083 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4084 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004085
Jooyung Han91df2082019-11-20 01:49:42 +09004086 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004087 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4088 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004089 if actual != expected {
4090 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4091 }
4092 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004093 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004094}
Jiyong Park67882562019-03-21 01:11:21 +09004095
Jooyung Han580eb4f2020-06-24 19:33:06 +09004096func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004097 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004098 apex {
4099 name: "myapex",
4100 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004101 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004102 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004103
Jooyung Han580eb4f2020-06-24 19:33:06 +09004104 apex_key {
4105 name: "myapex.key",
4106 public_key: "testkey.avbpubkey",
4107 private_key: "testkey.pem",
4108 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004109 `)
4110 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004111 rule := module.Output("file_contexts")
4112 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4113}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004114
Jooyung Han580eb4f2020-06-24 19:33:06 +09004115func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004116 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004117 apex {
4118 name: "myapex",
4119 key: "myapex.key",
4120 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004121 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004122 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004123
Jooyung Han580eb4f2020-06-24 19:33:06 +09004124 apex_key {
4125 name: "myapex.key",
4126 public_key: "testkey.avbpubkey",
4127 private_key: "testkey.pem",
4128 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004129 `, withFiles(map[string][]byte{
4130 "my_own_file_contexts": nil,
4131 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004132}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004133
Jooyung Han580eb4f2020-06-24 19:33:06 +09004134func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004135 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004136 apex {
4137 name: "myapex",
4138 key: "myapex.key",
4139 product_specific: true,
4140 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004141 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004142 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004143
Jooyung Han580eb4f2020-06-24 19:33:06 +09004144 apex_key {
4145 name: "myapex.key",
4146 public_key: "testkey.avbpubkey",
4147 private_key: "testkey.pem",
4148 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004149 `)
4150
Colin Cross1c460562021-02-16 17:55:47 -08004151 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004152 apex {
4153 name: "myapex",
4154 key: "myapex.key",
4155 product_specific: true,
4156 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004157 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004158 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004159
Jooyung Han580eb4f2020-06-24 19:33:06 +09004160 apex_key {
4161 name: "myapex.key",
4162 public_key: "testkey.avbpubkey",
4163 private_key: "testkey.pem",
4164 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004165 `, withFiles(map[string][]byte{
4166 "product_specific_file_contexts": nil,
4167 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004168 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4169 rule := module.Output("file_contexts")
4170 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4171}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004172
Jooyung Han580eb4f2020-06-24 19:33:06 +09004173func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004174 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004175 apex {
4176 name: "myapex",
4177 key: "myapex.key",
4178 product_specific: true,
4179 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004180 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004181 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004182
Jooyung Han580eb4f2020-06-24 19:33:06 +09004183 apex_key {
4184 name: "myapex.key",
4185 public_key: "testkey.avbpubkey",
4186 private_key: "testkey.pem",
4187 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004188
Jooyung Han580eb4f2020-06-24 19:33:06 +09004189 filegroup {
4190 name: "my-file-contexts",
4191 srcs: ["product_specific_file_contexts"],
4192 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004193 `, withFiles(map[string][]byte{
4194 "product_specific_file_contexts": nil,
4195 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004196 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4197 rule := module.Output("file_contexts")
4198 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004199}
4200
Jiyong Park67882562019-03-21 01:11:21 +09004201func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004202 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004203 apex_key {
4204 name: "myapex.key",
4205 public_key: ":my.avbpubkey",
4206 private_key: ":my.pem",
4207 product_specific: true,
4208 }
4209
4210 filegroup {
4211 name: "my.avbpubkey",
4212 srcs: ["testkey2.avbpubkey"],
4213 }
4214
4215 filegroup {
4216 name: "my.pem",
4217 srcs: ["testkey2.pem"],
4218 }
4219 `)
4220
4221 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4222 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004223 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004224 if actual_pubkey != expected_pubkey {
4225 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4226 }
4227 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004228 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004229 if actual_privkey != expected_privkey {
4230 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4231 }
4232}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004233
4234func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004235 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004236 prebuilt_apex {
4237 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004238 arch: {
4239 arm64: {
4240 src: "myapex-arm64.apex",
4241 },
4242 arm: {
4243 src: "myapex-arm.apex",
4244 },
4245 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004246 }
4247 `)
4248
4249 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4250
Jiyong Parkc95714e2019-03-29 14:23:10 +09004251 expectedInput := "myapex-arm64.apex"
4252 if prebuilt.inputApex.String() != expectedInput {
4253 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4254 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004255}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004256
4257func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004258 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004259 prebuilt_apex {
4260 name: "myapex",
4261 src: "myapex-arm.apex",
4262 filename: "notmyapex.apex",
4263 }
4264 `)
4265
4266 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4267
4268 expected := "notmyapex.apex"
4269 if p.installFilename != expected {
4270 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4271 }
4272}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004273
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004274func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004275 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004276 prebuilt_apex {
4277 name: "myapex.prebuilt",
4278 src: "myapex-arm.apex",
4279 overrides: [
4280 "myapex",
4281 ],
4282 }
4283 `)
4284
4285 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4286
4287 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004288 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004289 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004290 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004291 }
4292}
4293
Paul Duffin092153d2021-01-26 11:42:39 +00004294// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4295// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004296func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4297 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004298 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004299 }
4300
Paul Duffin89886cb2021-02-05 16:44:03 +00004301 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004302 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004303 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004304 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004305 stem := android.RemoveOptionalPrebuiltPrefix(name)
4306 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004307 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4308 }
4309 }
4310
Paul Duffin39853512021-02-26 11:09:39 +00004311 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004312 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004313 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004314 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4315 }
4316 }
4317
4318 t.Run("prebuilt only", func(t *testing.T) {
4319 bp := `
4320 prebuilt_apex {
4321 name: "myapex",
4322 arch: {
4323 arm64: {
4324 src: "myapex-arm64.apex",
4325 },
4326 arm: {
4327 src: "myapex-arm.apex",
4328 },
4329 },
Paul Duffin39853512021-02-26 11:09:39 +00004330 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004331 }
4332
4333 java_import {
4334 name: "libfoo",
4335 jars: ["libfoo.jar"],
4336 }
Paul Duffin39853512021-02-26 11:09:39 +00004337
4338 java_sdk_library_import {
4339 name: "libbar",
4340 public: {
4341 jars: ["libbar.jar"],
4342 },
4343 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004344 `
4345
4346 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4347 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4348
Paul Duffinf6932af2021-02-26 18:21:56 +00004349 // Make sure that the deapexer has the correct input APEX.
4350 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4351 rule := deapexer.Rule("deapexer")
4352 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4353 t.Errorf("expected: %q, found: %q", expected, actual)
4354 }
4355
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004356 // Make sure that the prebuilt_apex has the correct input APEX.
4357 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4358 rule = prebuiltApex.Rule("android/soong/android.Cp")
4359 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4360 t.Errorf("expected: %q, found: %q", expected, actual)
4361 }
4362
Paul Duffin89886cb2021-02-05 16:44:03 +00004363 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004364
4365 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004366 })
4367
4368 t.Run("prebuilt with source preferred", func(t *testing.T) {
4369
4370 bp := `
4371 prebuilt_apex {
4372 name: "myapex",
4373 arch: {
4374 arm64: {
4375 src: "myapex-arm64.apex",
4376 },
4377 arm: {
4378 src: "myapex-arm.apex",
4379 },
4380 },
Paul Duffin39853512021-02-26 11:09:39 +00004381 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004382 }
4383
4384 java_import {
4385 name: "libfoo",
4386 jars: ["libfoo.jar"],
4387 }
4388
4389 java_library {
4390 name: "libfoo",
4391 }
Paul Duffin39853512021-02-26 11:09:39 +00004392
4393 java_sdk_library_import {
4394 name: "libbar",
4395 public: {
4396 jars: ["libbar.jar"],
4397 },
4398 }
4399
4400 java_sdk_library {
4401 name: "libbar",
4402 srcs: ["foo/bar/MyClass.java"],
4403 unsafe_ignore_missing_latest_api: true,
4404 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004405 `
4406
4407 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4408 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4409
Paul Duffin89886cb2021-02-05 16:44:03 +00004410 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004411 ensureNoSourceVariant(t, ctx, "libfoo")
4412
4413 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4414 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004415 })
4416
4417 t.Run("prebuilt preferred with source", func(t *testing.T) {
4418 bp := `
4419 prebuilt_apex {
4420 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004421 arch: {
4422 arm64: {
4423 src: "myapex-arm64.apex",
4424 },
4425 arm: {
4426 src: "myapex-arm.apex",
4427 },
4428 },
Paul Duffin39853512021-02-26 11:09:39 +00004429 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004430 }
4431
4432 java_import {
4433 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004434 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004435 jars: ["libfoo.jar"],
4436 }
4437
4438 java_library {
4439 name: "libfoo",
4440 }
Paul Duffin39853512021-02-26 11:09:39 +00004441
4442 java_sdk_library_import {
4443 name: "libbar",
4444 prefer: true,
4445 public: {
4446 jars: ["libbar.jar"],
4447 },
4448 }
4449
4450 java_sdk_library {
4451 name: "libbar",
4452 srcs: ["foo/bar/MyClass.java"],
4453 unsafe_ignore_missing_latest_api: true,
4454 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004455 `
4456
4457 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4458 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4459
Paul Duffin89886cb2021-02-05 16:44:03 +00004460 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004461 ensureNoSourceVariant(t, ctx, "libfoo")
4462
4463 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4464 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004465 })
4466}
4467
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004468func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4469 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin37856732021-02-26 14:24:15 +00004470 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo", "myapex:libbar"})
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004471 }
4472
Paul Duffin37856732021-02-26 14:24:15 +00004473 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4474 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004475 s := ctx.SingletonForTests("dex_bootjars")
4476 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004477 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004478 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004479 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004480 foundLibfooJar = true
4481 buildRule := s.Output(output)
4482 actual := android.NormalizePathForTesting(buildRule.Input)
4483 if actual != bootDexJarPath {
4484 t.Errorf("Incorrect boot dex jar path '%s', expected '%s'", actual, bootDexJarPath)
4485 }
4486 }
4487 }
4488 if !foundLibfooJar {
4489 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs")
4490 }
4491 }
4492
Paul Duffin4fd997b2021-02-03 20:06:33 +00004493 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004494 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +00004495 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4496 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4497 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4498 }
4499
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004500 t.Run("prebuilt only", func(t *testing.T) {
4501 bp := `
4502 prebuilt_apex {
4503 name: "myapex",
4504 arch: {
4505 arm64: {
4506 src: "myapex-arm64.apex",
4507 },
4508 arm: {
4509 src: "myapex-arm.apex",
4510 },
4511 },
Paul Duffin37856732021-02-26 14:24:15 +00004512 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004513 }
4514
4515 java_import {
4516 name: "libfoo",
4517 jars: ["libfoo.jar"],
4518 apex_available: ["myapex"],
4519 }
Paul Duffin37856732021-02-26 14:24:15 +00004520
4521 java_sdk_library_import {
4522 name: "libbar",
4523 public: {
4524 jars: ["libbar.jar"],
4525 },
4526 apex_available: ["myapex"],
4527 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004528 `
4529
4530 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004531 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4532 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004533
Paul Duffin9d67ca62021-02-03 20:06:33 +00004534 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4535 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004536.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004537.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4538`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004539 })
4540
4541 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4542 bp := `
4543 prebuilt_apex {
4544 name: "myapex",
4545 arch: {
4546 arm64: {
4547 src: "myapex-arm64.apex",
4548 },
4549 arm: {
4550 src: "myapex-arm.apex",
4551 },
4552 },
Paul Duffin37856732021-02-26 14:24:15 +00004553 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004554 }
4555
4556 java_import {
4557 name: "libfoo",
4558 jars: ["libfoo.jar"],
4559 apex_available: ["myapex"],
4560 }
4561
4562 java_library {
4563 name: "libfoo",
4564 srcs: ["foo/bar/MyClass.java"],
4565 apex_available: ["myapex"],
4566 }
Paul Duffin37856732021-02-26 14:24:15 +00004567
4568 java_sdk_library_import {
4569 name: "libbar",
4570 public: {
4571 jars: ["libbar.jar"],
4572 },
4573 apex_available: ["myapex"],
4574 }
4575
4576 java_sdk_library {
4577 name: "libbar",
4578 srcs: ["foo/bar/MyClass.java"],
4579 unsafe_ignore_missing_latest_api: true,
4580 apex_available: ["myapex"],
4581 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004582 `
4583
4584 // In this test the source (java_library) libfoo is active since the
4585 // prebuilt (java_import) defaults to prefer:false. However the
4586 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4587 // find the dex boot jar in it. We either need to disable the source libfoo
4588 // or make the prebuilt libfoo preferred.
4589 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4590 })
4591
4592 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4593 bp := `
4594 prebuilt_apex {
4595 name: "myapex",
4596 arch: {
4597 arm64: {
4598 src: "myapex-arm64.apex",
4599 },
4600 arm: {
4601 src: "myapex-arm.apex",
4602 },
4603 },
Paul Duffin37856732021-02-26 14:24:15 +00004604 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004605 }
4606
4607 java_import {
4608 name: "libfoo",
4609 prefer: true,
4610 jars: ["libfoo.jar"],
4611 apex_available: ["myapex"],
4612 }
4613
4614 java_library {
4615 name: "libfoo",
4616 srcs: ["foo/bar/MyClass.java"],
4617 apex_available: ["myapex"],
4618 }
Paul Duffin37856732021-02-26 14:24:15 +00004619
4620 java_sdk_library_import {
4621 name: "libbar",
4622 prefer: true,
4623 public: {
4624 jars: ["libbar.jar"],
4625 },
4626 apex_available: ["myapex"],
4627 }
4628
4629 java_sdk_library {
4630 name: "libbar",
4631 srcs: ["foo/bar/MyClass.java"],
4632 unsafe_ignore_missing_latest_api: true,
4633 apex_available: ["myapex"],
4634 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004635 `
4636
4637 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004638 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4639 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004640
Paul Duffin9d67ca62021-02-03 20:06:33 +00004641 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4642 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004643.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004644.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4645`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004646 })
4647
4648 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4649 bp := `
4650 apex {
4651 name: "myapex",
4652 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004653 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004654 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004655 }
4656
4657 apex_key {
4658 name: "myapex.key",
4659 public_key: "testkey.avbpubkey",
4660 private_key: "testkey.pem",
4661 }
4662
4663 prebuilt_apex {
4664 name: "myapex",
4665 arch: {
4666 arm64: {
4667 src: "myapex-arm64.apex",
4668 },
4669 arm: {
4670 src: "myapex-arm.apex",
4671 },
4672 },
Paul Duffin37856732021-02-26 14:24:15 +00004673 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004674 }
4675
4676 java_import {
4677 name: "libfoo",
4678 jars: ["libfoo.jar"],
4679 apex_available: ["myapex"],
4680 }
4681
4682 java_library {
4683 name: "libfoo",
4684 srcs: ["foo/bar/MyClass.java"],
4685 apex_available: ["myapex"],
4686 }
Paul Duffin37856732021-02-26 14:24:15 +00004687
4688 java_sdk_library_import {
4689 name: "libbar",
4690 public: {
4691 jars: ["libbar.jar"],
4692 },
4693 apex_available: ["myapex"],
4694 }
4695
4696 java_sdk_library {
4697 name: "libbar",
4698 srcs: ["foo/bar/MyClass.java"],
4699 unsafe_ignore_missing_latest_api: true,
4700 apex_available: ["myapex"],
4701 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004702 `
4703
4704 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004705 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4706 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004707
4708 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4709 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004710.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004711.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4712`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004713 })
4714
4715 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4716 bp := `
4717 apex {
4718 name: "myapex",
4719 enabled: false,
4720 key: "myapex.key",
4721 java_libs: ["libfoo"],
4722 }
4723
4724 apex_key {
4725 name: "myapex.key",
4726 public_key: "testkey.avbpubkey",
4727 private_key: "testkey.pem",
4728 }
4729
4730 prebuilt_apex {
4731 name: "myapex",
4732 arch: {
4733 arm64: {
4734 src: "myapex-arm64.apex",
4735 },
4736 arm: {
4737 src: "myapex-arm.apex",
4738 },
4739 },
Paul Duffin37856732021-02-26 14:24:15 +00004740 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004741 }
4742
4743 java_import {
4744 name: "libfoo",
4745 prefer: true,
4746 jars: ["libfoo.jar"],
4747 apex_available: ["myapex"],
4748 }
4749
4750 java_library {
4751 name: "libfoo",
4752 srcs: ["foo/bar/MyClass.java"],
4753 apex_available: ["myapex"],
4754 }
Paul Duffin37856732021-02-26 14:24:15 +00004755
4756 java_sdk_library_import {
4757 name: "libbar",
4758 prefer: true,
4759 public: {
4760 jars: ["libbar.jar"],
4761 },
4762 apex_available: ["myapex"],
4763 }
4764
4765 java_sdk_library {
4766 name: "libbar",
4767 srcs: ["foo/bar/MyClass.java"],
4768 unsafe_ignore_missing_latest_api: true,
4769 apex_available: ["myapex"],
4770 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004771 `
4772
4773 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004774 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4775 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004776
Paul Duffin9d67ca62021-02-03 20:06:33 +00004777 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4778 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004779.intermediates/prebuilt_libbar/android_common_prebuilt_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004780.intermediates/prebuilt_libfoo/android_common_prebuilt_myapex/hiddenapi/index.csv
4781`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004782 })
4783}
4784
Roland Levillain630846d2019-06-26 12:48:34 +01004785func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004786 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004787 apex_test {
4788 name: "myapex",
4789 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004790 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004791 tests: [
4792 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004793 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004794 ],
4795 }
4796
4797 apex_key {
4798 name: "myapex.key",
4799 public_key: "testkey.avbpubkey",
4800 private_key: "testkey.pem",
4801 }
4802
Liz Kammer1c14a212020-05-12 15:26:55 -07004803 filegroup {
4804 name: "fg",
4805 srcs: [
4806 "baz",
4807 "bar/baz"
4808 ],
4809 }
4810
Roland Levillain630846d2019-06-26 12:48:34 +01004811 cc_test {
4812 name: "mytest",
4813 gtest: false,
4814 srcs: ["mytest.cpp"],
4815 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004816 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004817 system_shared_libs: [],
4818 static_executable: true,
4819 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004820 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004821 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004822
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004823 cc_library {
4824 name: "mylib",
4825 srcs: ["mylib.cpp"],
4826 system_shared_libs: [],
4827 stl: "none",
4828 }
4829
Liz Kammer5bd365f2020-05-27 15:15:11 -07004830 filegroup {
4831 name: "fg2",
4832 srcs: [
4833 "testdata/baz"
4834 ],
4835 }
4836
Roland Levillain9b5fde92019-06-28 15:41:19 +01004837 cc_test {
4838 name: "mytests",
4839 gtest: false,
4840 srcs: [
4841 "mytest1.cpp",
4842 "mytest2.cpp",
4843 "mytest3.cpp",
4844 ],
4845 test_per_src: true,
4846 relative_install_path: "test",
4847 system_shared_libs: [],
4848 static_executable: true,
4849 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004850 data: [
4851 ":fg",
4852 ":fg2",
4853 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004854 }
Roland Levillain630846d2019-06-26 12:48:34 +01004855 `)
4856
Sundong Ahnabb64432019-10-22 13:58:29 +09004857 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004858 copyCmds := apexRule.Args["copy_commands"]
4859
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004860 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004861 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004862 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004863
Liz Kammer1c14a212020-05-12 15:26:55 -07004864 //Ensure that test data are copied into apex.
4865 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4866 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4867
Roland Levillain9b5fde92019-06-28 15:41:19 +01004868 // Ensure that test deps built with `test_per_src` are copied into apex.
4869 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4870 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4871 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004872
4873 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004874 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004875 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004876 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004877 prefix := "TARGET_"
4878 var builder strings.Builder
4879 data.Custom(&builder, name, prefix, "", data)
4880 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004881 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4882 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4883 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4884 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004885 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004886 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004887 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004888
4889 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004890 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004891 data.Custom(&builder, name, prefix, "", data)
4892 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004893 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4894 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004895}
4896
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004897func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004898 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004899 apex {
4900 name: "myapex",
4901 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004902 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004903 }
4904 apex_key {
4905 name: "myapex.key",
4906 public_key: "testkey.avbpubkey",
4907 private_key: "testkey.pem",
4908 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004909 `,
4910 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4911 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4912 }),
4913 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004914 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004915 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004916 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004917 var builder strings.Builder
4918 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4919 androidMk := builder.String()
4920 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4921}
4922
Jooyung Hand48f3c32019-08-23 11:18:57 +09004923func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4924 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4925 apex {
4926 name: "myapex",
4927 key: "myapex.key",
4928 native_shared_libs: ["libfoo"],
4929 }
4930
4931 apex_key {
4932 name: "myapex.key",
4933 public_key: "testkey.avbpubkey",
4934 private_key: "testkey.pem",
4935 }
4936
4937 cc_library {
4938 name: "libfoo",
4939 stl: "none",
4940 system_shared_libs: [],
4941 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004942 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004943 }
4944 `)
4945 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4946 apex {
4947 name: "myapex",
4948 key: "myapex.key",
4949 java_libs: ["myjar"],
4950 }
4951
4952 apex_key {
4953 name: "myapex.key",
4954 public_key: "testkey.avbpubkey",
4955 private_key: "testkey.pem",
4956 }
4957
4958 java_library {
4959 name: "myjar",
4960 srcs: ["foo/bar/MyClass.java"],
4961 sdk_version: "none",
4962 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004963 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004964 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004965 }
4966 `)
4967}
4968
Bill Peckhama41a6962021-01-11 10:58:54 -08004969func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004970 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08004971 apex {
4972 name: "myapex",
4973 key: "myapex.key",
4974 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004975 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08004976 }
4977
4978 apex_key {
4979 name: "myapex.key",
4980 public_key: "testkey.avbpubkey",
4981 private_key: "testkey.pem",
4982 }
4983
4984 java_import {
4985 name: "myjavaimport",
4986 apex_available: ["myapex"],
4987 jars: ["my.jar"],
4988 compile_dex: true,
4989 }
4990 `)
4991
4992 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4993 apexRule := module.Rule("apexRule")
4994 copyCmds := apexRule.Args["copy_commands"]
4995 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
4996}
4997
Sundong Ahne1f05aa2019-08-27 13:55:42 +09004998func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004999 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005000 apex {
5001 name: "myapex",
5002 key: "myapex.key",
5003 apps: [
5004 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005005 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005006 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005007 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005008 }
5009
5010 apex_key {
5011 name: "myapex.key",
5012 public_key: "testkey.avbpubkey",
5013 private_key: "testkey.pem",
5014 }
5015
5016 android_app {
5017 name: "AppFoo",
5018 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005019 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005020 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005021 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005022 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005023 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005024 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005025
5026 android_app {
5027 name: "AppFooPriv",
5028 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005029 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005030 system_modules: "none",
5031 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005032 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005033 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005034 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005035
5036 cc_library_shared {
5037 name: "libjni",
5038 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005039 shared_libs: ["libfoo"],
5040 stl: "none",
5041 system_shared_libs: [],
5042 apex_available: [ "myapex" ],
5043 sdk_version: "current",
5044 }
5045
5046 cc_library_shared {
5047 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005048 stl: "none",
5049 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005050 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005051 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005052 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005053 `)
5054
Sundong Ahnabb64432019-10-22 13:58:29 +09005055 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005056 apexRule := module.Rule("apexRule")
5057 copyCmds := apexRule.Args["copy_commands"]
5058
5059 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005060 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005061
Colin Crossaede88c2020-08-11 12:17:01 -07005062 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005063 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005064 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005065 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005066 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005067 // JNI libraries including transitive deps are
5068 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005069 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005070 // ... embedded inside APK (jnilibs.zip)
5071 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5072 // ... and not directly inside the APEX
5073 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5074 }
Dario Frenicde2a032019-10-27 00:29:22 +01005075}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005076
Dario Frenicde2a032019-10-27 00:29:22 +01005077func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005078 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005079 apex {
5080 name: "myapex",
5081 key: "myapex.key",
5082 apps: [
5083 "AppFooPrebuilt",
5084 "AppFooPrivPrebuilt",
5085 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005086 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005087 }
5088
5089 apex_key {
5090 name: "myapex.key",
5091 public_key: "testkey.avbpubkey",
5092 private_key: "testkey.pem",
5093 }
5094
5095 android_app_import {
5096 name: "AppFooPrebuilt",
5097 apk: "PrebuiltAppFoo.apk",
5098 presigned: true,
5099 dex_preopt: {
5100 enabled: false,
5101 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005102 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005103 }
5104
5105 android_app_import {
5106 name: "AppFooPrivPrebuilt",
5107 apk: "PrebuiltAppFooPriv.apk",
5108 privileged: true,
5109 presigned: true,
5110 dex_preopt: {
5111 enabled: false,
5112 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005113 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005114 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005115 }
5116 `)
5117
Sundong Ahnabb64432019-10-22 13:58:29 +09005118 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005119 apexRule := module.Rule("apexRule")
5120 copyCmds := apexRule.Args["copy_commands"]
5121
5122 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005123 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5124}
5125
5126func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005127 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005128 apex {
5129 name: "myapex",
5130 key: "myapex.key",
5131 apps: [
5132 "AppFoo",
5133 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005134 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005135 }
5136
5137 apex_key {
5138 name: "myapex.key",
5139 public_key: "testkey.avbpubkey",
5140 private_key: "testkey.pem",
5141 }
5142
5143 android_app {
5144 name: "AppFoo",
5145 srcs: ["foo/bar/MyClass.java"],
5146 sdk_version: "none",
5147 system_modules: "none",
5148 apex_available: [ "myapex" ],
5149 }
5150
5151 android_app_import {
5152 name: "AppFoo",
5153 apk: "AppFooPrebuilt.apk",
5154 filename: "AppFooPrebuilt.apk",
5155 presigned: true,
5156 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005157 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005158 }
5159 `, withFiles(map[string][]byte{
5160 "AppFooPrebuilt.apk": nil,
5161 }))
5162
5163 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005164 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005165 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005166}
5167
Dario Freni6f3937c2019-12-20 22:58:03 +00005168func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005169 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005170 apex {
5171 name: "myapex",
5172 key: "myapex.key",
5173 apps: [
5174 "TesterHelpAppFoo",
5175 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005176 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005177 }
5178
5179 apex_key {
5180 name: "myapex.key",
5181 public_key: "testkey.avbpubkey",
5182 private_key: "testkey.pem",
5183 }
5184
5185 android_test_helper_app {
5186 name: "TesterHelpAppFoo",
5187 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005188 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005189 }
5190
5191 `)
5192
5193 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5194 apexRule := module.Rule("apexRule")
5195 copyCmds := apexRule.Args["copy_commands"]
5196
5197 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5198}
5199
Jooyung Han18020ea2019-11-13 10:50:48 +09005200func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5201 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005202 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005203 apex {
5204 name: "myapex",
5205 key: "myapex.key",
5206 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005207 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005208 }
5209
5210 apex_key {
5211 name: "myapex.key",
5212 public_key: "testkey.avbpubkey",
5213 private_key: "testkey.pem",
5214 }
5215
5216 apex {
5217 name: "otherapex",
5218 key: "myapex.key",
5219 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005220 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005221 }
5222
5223 cc_defaults {
5224 name: "libfoo-defaults",
5225 apex_available: ["otherapex"],
5226 }
5227
5228 cc_library {
5229 name: "libfoo",
5230 defaults: ["libfoo-defaults"],
5231 stl: "none",
5232 system_shared_libs: [],
5233 }`)
5234}
5235
Paul Duffine52e66f2020-03-30 17:54:29 +01005236func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005237 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005238 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005239 apex {
5240 name: "myapex",
5241 key: "myapex.key",
5242 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005243 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005244 }
5245
5246 apex_key {
5247 name: "myapex.key",
5248 public_key: "testkey.avbpubkey",
5249 private_key: "testkey.pem",
5250 }
5251
5252 apex {
5253 name: "otherapex",
5254 key: "otherapex.key",
5255 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005256 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005257 }
5258
5259 apex_key {
5260 name: "otherapex.key",
5261 public_key: "testkey.avbpubkey",
5262 private_key: "testkey.pem",
5263 }
5264
5265 cc_library {
5266 name: "libfoo",
5267 stl: "none",
5268 system_shared_libs: [],
5269 apex_available: ["otherapex"],
5270 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005271}
Jiyong Park127b40b2019-09-30 16:04:35 +09005272
Paul Duffine52e66f2020-03-30 17:54:29 +01005273func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005274 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005275 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005276.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005277.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005278.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005279.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005280.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005281.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005282 apex {
5283 name: "myapex",
5284 key: "myapex.key",
5285 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005286 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005287 }
5288
5289 apex_key {
5290 name: "myapex.key",
5291 public_key: "testkey.avbpubkey",
5292 private_key: "testkey.pem",
5293 }
5294
Jiyong Park127b40b2019-09-30 16:04:35 +09005295 cc_library {
5296 name: "libfoo",
5297 stl: "none",
5298 shared_libs: ["libbar"],
5299 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005300 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005301 }
5302
5303 cc_library {
5304 name: "libbar",
5305 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005306 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005307 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005308 apex_available: ["myapex"],
5309 }
5310
5311 cc_library {
5312 name: "libbaz",
5313 stl: "none",
5314 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005315 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005316}
Jiyong Park127b40b2019-09-30 16:04:35 +09005317
Paul Duffine52e66f2020-03-30 17:54:29 +01005318func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005319 testApexError(t, "\"otherapex\" is not a valid module name", `
5320 apex {
5321 name: "myapex",
5322 key: "myapex.key",
5323 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005324 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005325 }
5326
5327 apex_key {
5328 name: "myapex.key",
5329 public_key: "testkey.avbpubkey",
5330 private_key: "testkey.pem",
5331 }
5332
5333 cc_library {
5334 name: "libfoo",
5335 stl: "none",
5336 system_shared_libs: [],
5337 apex_available: ["otherapex"],
5338 }`)
5339
Paul Duffine52e66f2020-03-30 17:54:29 +01005340 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005341 apex {
5342 name: "myapex",
5343 key: "myapex.key",
5344 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005345 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005346 }
5347
5348 apex_key {
5349 name: "myapex.key",
5350 public_key: "testkey.avbpubkey",
5351 private_key: "testkey.pem",
5352 }
5353
5354 cc_library {
5355 name: "libfoo",
5356 stl: "none",
5357 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005358 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005359 apex_available: ["myapex"],
5360 }
5361
5362 cc_library {
5363 name: "libbar",
5364 stl: "none",
5365 system_shared_libs: [],
5366 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005367 }
5368
5369 cc_library {
5370 name: "libbaz",
5371 stl: "none",
5372 system_shared_libs: [],
5373 stubs: {
5374 versions: ["10", "20", "30"],
5375 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005376 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005377}
Jiyong Park127b40b2019-09-30 16:04:35 +09005378
Jiyong Park89e850a2020-04-07 16:37:39 +09005379func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005380 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005381 apex {
5382 name: "myapex",
5383 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005384 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005385 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005386 }
5387
5388 apex_key {
5389 name: "myapex.key",
5390 public_key: "testkey.avbpubkey",
5391 private_key: "testkey.pem",
5392 }
5393
5394 cc_library {
5395 name: "libfoo",
5396 stl: "none",
5397 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005398 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005399 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005400 }
5401
5402 cc_library {
5403 name: "libfoo2",
5404 stl: "none",
5405 system_shared_libs: [],
5406 shared_libs: ["libbaz"],
5407 apex_available: ["//apex_available:platform"],
5408 }
5409
5410 cc_library {
5411 name: "libbar",
5412 stl: "none",
5413 system_shared_libs: [],
5414 apex_available: ["myapex"],
5415 }
5416
5417 cc_library {
5418 name: "libbaz",
5419 stl: "none",
5420 system_shared_libs: [],
5421 apex_available: ["myapex"],
5422 stubs: {
5423 versions: ["1"],
5424 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005425 }`)
5426
Jiyong Park89e850a2020-04-07 16:37:39 +09005427 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5428 // because it depends on libbar which isn't available to platform
5429 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5430 if libfoo.NotAvailableForPlatform() != true {
5431 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5432 }
5433
5434 // libfoo2 however can be available to platform because it depends on libbaz which provides
5435 // stubs
5436 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5437 if libfoo2.NotAvailableForPlatform() == true {
5438 t.Errorf("%q should be available to platform", libfoo2.String())
5439 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005440}
Jiyong Parka90ca002019-10-07 15:47:24 +09005441
Paul Duffine52e66f2020-03-30 17:54:29 +01005442func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005443 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005444 apex {
5445 name: "myapex",
5446 key: "myapex.key",
5447 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005448 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005449 }
5450
5451 apex_key {
5452 name: "myapex.key",
5453 public_key: "testkey.avbpubkey",
5454 private_key: "testkey.pem",
5455 }
5456
5457 cc_library {
5458 name: "libfoo",
5459 stl: "none",
5460 system_shared_libs: [],
5461 apex_available: ["myapex"],
5462 static: {
5463 apex_available: ["//apex_available:platform"],
5464 },
5465 }`)
5466
Jiyong Park89e850a2020-04-07 16:37:39 +09005467 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5468 if libfooShared.NotAvailableForPlatform() != true {
5469 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5470 }
5471 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5472 if libfooStatic.NotAvailableForPlatform() != false {
5473 t.Errorf("%q should be available to platform", libfooStatic.String())
5474 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005475}
5476
Jiyong Park5d790c32019-11-15 18:40:32 +09005477func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005478 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005479 apex {
5480 name: "myapex",
5481 key: "myapex.key",
5482 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005483 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005484 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005485 }
5486
5487 override_apex {
5488 name: "override_myapex",
5489 base: "myapex",
5490 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005491 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005492 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005493 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005494 }
5495
5496 apex_key {
5497 name: "myapex.key",
5498 public_key: "testkey.avbpubkey",
5499 private_key: "testkey.pem",
5500 }
5501
5502 android_app {
5503 name: "app",
5504 srcs: ["foo/bar/MyClass.java"],
5505 package_name: "foo",
5506 sdk_version: "none",
5507 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005508 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005509 }
5510
5511 override_android_app {
5512 name: "override_app",
5513 base: "app",
5514 package_name: "bar",
5515 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005516 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005517
Jiyong Park317645e2019-12-05 13:20:58 +09005518 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5519 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5520 if originalVariant.GetOverriddenBy() != "" {
5521 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5522 }
5523 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5524 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5525 }
5526
Jiyong Park5d790c32019-11-15 18:40:32 +09005527 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5528 apexRule := module.Rule("apexRule")
5529 copyCmds := apexRule.Args["copy_commands"]
5530
5531 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005532 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005533
5534 apexBundle := module.Module().(*apexBundle)
5535 name := apexBundle.Name()
5536 if name != "override_myapex" {
5537 t.Errorf("name should be \"override_myapex\", but was %q", name)
5538 }
5539
Baligh Uddin004d7172020-02-19 21:29:28 -08005540 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5541 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5542 }
5543
Jiyong Park20bacab2020-03-03 11:45:41 +09005544 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005545 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005546
Colin Crossaa255532020-07-03 13:18:24 -07005547 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005548 var builder strings.Builder
5549 data.Custom(&builder, name, "TARGET_", "", data)
5550 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005551 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005552 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5553 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005554 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005555 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005556 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005557 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5558 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005559}
5560
Jooyung Han214bf372019-11-12 13:03:50 +09005561func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005562 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005563 apex {
5564 name: "myapex",
5565 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005566 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005567 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005568 }
5569
5570 apex_key {
5571 name: "myapex.key",
5572 public_key: "testkey.avbpubkey",
5573 private_key: "testkey.pem",
5574 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005575
5576 cc_library {
5577 name: "mylib",
5578 srcs: ["mylib.cpp"],
5579 stl: "libc++",
5580 system_shared_libs: [],
5581 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005582 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005583 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005584 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005585
5586 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5587 args := module.Rule("apexRule").Args
5588 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005589 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005590
5591 // The copies of the libraries in the apex should have one more dependency than
5592 // the ones outside the apex, namely the unwinder. Ideally we should check
5593 // the dependency names directly here but for some reason the names are blank in
5594 // this test.
5595 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005596 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005597 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5598 if len(apexImplicits) != len(nonApexImplicits)+1 {
5599 t.Errorf("%q missing unwinder dep", lib)
5600 }
5601 }
Jooyung Han214bf372019-11-12 13:03:50 +09005602}
5603
Paul Duffine05480a2021-03-08 15:07:14 +00005604var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005605 "api/current.txt": nil,
5606 "api/removed.txt": nil,
5607 "api/system-current.txt": nil,
5608 "api/system-removed.txt": nil,
5609 "api/test-current.txt": nil,
5610 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005611
Anton Hanssondff2c782020-12-21 17:10:01 +00005612 "100/public/api/foo.txt": nil,
5613 "100/public/api/foo-removed.txt": nil,
5614 "100/system/api/foo.txt": nil,
5615 "100/system/api/foo-removed.txt": nil,
5616
Paul Duffineedc5d52020-06-12 17:46:39 +01005617 // For java_sdk_library_import
5618 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005619}
5620
Jooyung Han58f26ab2019-12-18 15:34:32 +09005621func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005622 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005623 apex {
5624 name: "myapex",
5625 key: "myapex.key",
5626 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005627 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005628 }
5629
5630 apex_key {
5631 name: "myapex.key",
5632 public_key: "testkey.avbpubkey",
5633 private_key: "testkey.pem",
5634 }
5635
5636 java_sdk_library {
5637 name: "foo",
5638 srcs: ["a.java"],
5639 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005640 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005641 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005642
5643 prebuilt_apis {
5644 name: "sdk",
5645 api_dirs: ["100"],
5646 }
Paul Duffin9b879592020-05-26 13:21:35 +01005647 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005648
5649 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005650 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005651 "javalib/foo.jar",
5652 "etc/permissions/foo.xml",
5653 })
5654 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005655 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5656 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005657}
5658
Paul Duffin9b879592020-05-26 13:21:35 +01005659func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005660 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005661 apex {
5662 name: "myapex",
5663 key: "myapex.key",
5664 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005665 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005666 }
5667
5668 apex_key {
5669 name: "myapex.key",
5670 public_key: "testkey.avbpubkey",
5671 private_key: "testkey.pem",
5672 }
5673
5674 java_sdk_library {
5675 name: "foo",
5676 srcs: ["a.java"],
5677 api_packages: ["foo"],
5678 apex_available: ["myapex"],
5679 sdk_version: "none",
5680 system_modules: "none",
5681 }
5682
5683 java_library {
5684 name: "bar",
5685 srcs: ["a.java"],
5686 libs: ["foo"],
5687 apex_available: ["myapex"],
5688 sdk_version: "none",
5689 system_modules: "none",
5690 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005691
5692 prebuilt_apis {
5693 name: "sdk",
5694 api_dirs: ["100"],
5695 }
Paul Duffin9b879592020-05-26 13:21:35 +01005696 `, withFiles(filesForSdkLibrary))
5697
5698 // java_sdk_library installs both impl jar and permission XML
5699 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5700 "javalib/bar.jar",
5701 "javalib/foo.jar",
5702 "etc/permissions/foo.xml",
5703 })
5704
5705 // The bar library should depend on the implementation jar.
5706 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005707 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005708 t.Errorf("expected %q, found %#q", expected, actual)
5709 }
5710}
5711
5712func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005713 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005714 apex {
5715 name: "myapex",
5716 key: "myapex.key",
5717 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005718 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005719 }
5720
5721 apex_key {
5722 name: "myapex.key",
5723 public_key: "testkey.avbpubkey",
5724 private_key: "testkey.pem",
5725 }
5726
5727 java_sdk_library {
5728 name: "foo",
5729 srcs: ["a.java"],
5730 api_packages: ["foo"],
5731 apex_available: ["myapex"],
5732 sdk_version: "none",
5733 system_modules: "none",
5734 }
5735
5736 java_library {
5737 name: "bar",
5738 srcs: ["a.java"],
5739 libs: ["foo"],
5740 sdk_version: "none",
5741 system_modules: "none",
5742 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005743
5744 prebuilt_apis {
5745 name: "sdk",
5746 api_dirs: ["100"],
5747 }
Paul Duffin9b879592020-05-26 13:21:35 +01005748 `, withFiles(filesForSdkLibrary))
5749
5750 // java_sdk_library installs both impl jar and permission XML
5751 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5752 "javalib/foo.jar",
5753 "etc/permissions/foo.xml",
5754 })
5755
5756 // The bar library should depend on the stubs jar.
5757 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005758 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01005759 t.Errorf("expected %q, found %#q", expected, actual)
5760 }
5761}
5762
Paul Duffineedc5d52020-06-12 17:46:39 +01005763func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005764 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005765 prebuilt_apis {
5766 name: "sdk",
5767 api_dirs: ["100"],
5768 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005769 withFiles(map[string][]byte{
5770 "apex/a.java": nil,
5771 "apex/apex_manifest.json": nil,
5772 "apex/Android.bp": []byte(`
5773 package {
5774 default_visibility: ["//visibility:private"],
5775 }
5776
5777 apex {
5778 name: "myapex",
5779 key: "myapex.key",
5780 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005781 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005782 }
5783
5784 apex_key {
5785 name: "myapex.key",
5786 public_key: "testkey.avbpubkey",
5787 private_key: "testkey.pem",
5788 }
5789
5790 java_library {
5791 name: "bar",
5792 srcs: ["a.java"],
5793 libs: ["foo"],
5794 apex_available: ["myapex"],
5795 sdk_version: "none",
5796 system_modules: "none",
5797 }
5798`),
5799 "source/a.java": nil,
5800 "source/api/current.txt": nil,
5801 "source/api/removed.txt": nil,
5802 "source/Android.bp": []byte(`
5803 package {
5804 default_visibility: ["//visibility:private"],
5805 }
5806
5807 java_sdk_library {
5808 name: "foo",
5809 visibility: ["//apex"],
5810 srcs: ["a.java"],
5811 api_packages: ["foo"],
5812 apex_available: ["myapex"],
5813 sdk_version: "none",
5814 system_modules: "none",
5815 public: {
5816 enabled: true,
5817 },
5818 }
5819`),
5820 "prebuilt/a.jar": nil,
5821 "prebuilt/Android.bp": []byte(`
5822 package {
5823 default_visibility: ["//visibility:private"],
5824 }
5825
5826 java_sdk_library_import {
5827 name: "foo",
5828 visibility: ["//apex", "//source"],
5829 apex_available: ["myapex"],
5830 prefer: true,
5831 public: {
5832 jars: ["a.jar"],
5833 },
5834 }
5835`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005836 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005837 )
5838
5839 // java_sdk_library installs both impl jar and permission XML
5840 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5841 "javalib/bar.jar",
5842 "javalib/foo.jar",
5843 "etc/permissions/foo.xml",
5844 })
5845
5846 // The bar library should depend on the implementation jar.
5847 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01005848 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01005849 t.Errorf("expected %q, found %#q", expected, actual)
5850 }
5851}
5852
5853func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5854 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5855 apex {
5856 name: "myapex",
5857 key: "myapex.key",
5858 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005859 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005860 }
5861
5862 apex_key {
5863 name: "myapex.key",
5864 public_key: "testkey.avbpubkey",
5865 private_key: "testkey.pem",
5866 }
5867
5868 java_sdk_library_import {
5869 name: "foo",
5870 apex_available: ["myapex"],
5871 prefer: true,
5872 public: {
5873 jars: ["a.jar"],
5874 },
5875 }
5876
5877 `, withFiles(filesForSdkLibrary))
5878}
5879
atrost6e126252020-01-27 17:01:16 +00005880func TestCompatConfig(t *testing.T) {
Paul Duffina369c7b2021-03-09 03:08:05 +00005881 result := apexFixtureFactory.
5882 Extend(java.PrepareForTestWithPlatformCompatConfig).
5883 RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005884 apex {
5885 name: "myapex",
5886 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005887 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005888 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005889 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005890 }
5891
5892 apex_key {
5893 name: "myapex.key",
5894 public_key: "testkey.avbpubkey",
5895 private_key: "testkey.pem",
5896 }
5897
5898 platform_compat_config {
5899 name: "myjar-platform-compat-config",
5900 src: ":myjar",
5901 }
5902
5903 java_library {
5904 name: "myjar",
5905 srcs: ["foo/bar/MyClass.java"],
5906 sdk_version: "none",
5907 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005908 apex_available: [ "myapex" ],
5909 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005910
5911 // Make sure that a preferred prebuilt does not affect the apex contents.
5912 prebuilt_platform_compat_config {
5913 name: "myjar-platform-compat-config",
5914 metadata: "compat-config/metadata.xml",
5915 prefer: true,
5916 }
atrost6e126252020-01-27 17:01:16 +00005917 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005918 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005919 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5920 "etc/compatconfig/myjar-platform-compat-config.xml",
5921 "javalib/myjar.jar",
5922 })
5923}
5924
Jiyong Park479321d2019-12-16 11:47:12 +09005925func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5926 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5927 apex {
5928 name: "myapex",
5929 key: "myapex.key",
5930 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005931 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09005932 }
5933
5934 apex_key {
5935 name: "myapex.key",
5936 public_key: "testkey.avbpubkey",
5937 private_key: "testkey.pem",
5938 }
5939
5940 java_library {
5941 name: "myjar",
5942 srcs: ["foo/bar/MyClass.java"],
5943 sdk_version: "none",
5944 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005945 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005946 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005947 }
5948 `)
5949}
5950
Jiyong Park7afd1072019-12-30 16:56:33 +09005951func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005952 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09005953 apex {
5954 name: "myapex",
5955 key: "myapex.key",
5956 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005957 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09005958 }
5959
5960 apex_key {
5961 name: "myapex.key",
5962 public_key: "testkey.avbpubkey",
5963 private_key: "testkey.pem",
5964 }
5965
5966 cc_library {
5967 name: "mylib",
5968 srcs: ["mylib.cpp"],
5969 system_shared_libs: [],
5970 stl: "none",
5971 required: ["a", "b"],
5972 host_required: ["c", "d"],
5973 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005974 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09005975 }
5976 `)
5977
5978 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005979 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09005980 name := apexBundle.BaseModuleName()
5981 prefix := "TARGET_"
5982 var builder strings.Builder
5983 data.Custom(&builder, name, prefix, "", data)
5984 androidMk := builder.String()
5985 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
5986 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
5987 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
5988}
5989
Jiyong Park7cd10e32020-01-14 09:22:18 +09005990func TestSymlinksFromApexToSystem(t *testing.T) {
5991 bp := `
5992 apex {
5993 name: "myapex",
5994 key: "myapex.key",
5995 native_shared_libs: ["mylib"],
5996 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005997 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09005998 }
5999
Jiyong Park9d677202020-02-19 16:29:35 +09006000 apex {
6001 name: "myapex.updatable",
6002 key: "myapex.key",
6003 native_shared_libs: ["mylib"],
6004 java_libs: ["myjar"],
6005 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006006 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006007 }
6008
Jiyong Park7cd10e32020-01-14 09:22:18 +09006009 apex_key {
6010 name: "myapex.key",
6011 public_key: "testkey.avbpubkey",
6012 private_key: "testkey.pem",
6013 }
6014
6015 cc_library {
6016 name: "mylib",
6017 srcs: ["mylib.cpp"],
6018 shared_libs: ["myotherlib"],
6019 system_shared_libs: [],
6020 stl: "none",
6021 apex_available: [
6022 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006023 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006024 "//apex_available:platform",
6025 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006026 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006027 }
6028
6029 cc_library {
6030 name: "myotherlib",
6031 srcs: ["mylib.cpp"],
6032 system_shared_libs: [],
6033 stl: "none",
6034 apex_available: [
6035 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006036 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006037 "//apex_available:platform",
6038 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006039 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006040 }
6041
6042 java_library {
6043 name: "myjar",
6044 srcs: ["foo/bar/MyClass.java"],
6045 sdk_version: "none",
6046 system_modules: "none",
6047 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006048 apex_available: [
6049 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006050 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006051 "//apex_available:platform",
6052 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006053 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006054 }
6055
6056 java_library {
6057 name: "myotherjar",
6058 srcs: ["foo/bar/MyClass.java"],
6059 sdk_version: "none",
6060 system_modules: "none",
6061 apex_available: [
6062 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006063 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006064 "//apex_available:platform",
6065 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006066 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006067 }
6068 `
6069
6070 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6071 for _, f := range files {
6072 if f.path == file {
6073 if f.isLink {
6074 t.Errorf("%q is not a real file", file)
6075 }
6076 return
6077 }
6078 }
6079 t.Errorf("%q is not found", file)
6080 }
6081
6082 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6083 for _, f := range files {
6084 if f.path == file {
6085 if !f.isLink {
6086 t.Errorf("%q is not a symlink", file)
6087 }
6088 return
6089 }
6090 }
6091 t.Errorf("%q is not found", file)
6092 }
6093
Jiyong Park9d677202020-02-19 16:29:35 +09006094 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6095 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006096 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006097 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006098 ensureRealfileExists(t, files, "javalib/myjar.jar")
6099 ensureRealfileExists(t, files, "lib64/mylib.so")
6100 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6101
Jiyong Park9d677202020-02-19 16:29:35 +09006102 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6103 ensureRealfileExists(t, files, "javalib/myjar.jar")
6104 ensureRealfileExists(t, files, "lib64/mylib.so")
6105 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6106
6107 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006108 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006109 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006110 ensureRealfileExists(t, files, "javalib/myjar.jar")
6111 ensureRealfileExists(t, files, "lib64/mylib.so")
6112 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006113
6114 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6115 ensureRealfileExists(t, files, "javalib/myjar.jar")
6116 ensureRealfileExists(t, files, "lib64/mylib.so")
6117 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006118}
6119
Yo Chiange8128052020-07-23 20:09:18 +08006120func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006121 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006122 apex {
6123 name: "myapex",
6124 key: "myapex.key",
6125 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006126 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006127 }
6128
6129 apex_key {
6130 name: "myapex.key",
6131 public_key: "testkey.avbpubkey",
6132 private_key: "testkey.pem",
6133 }
6134
6135 cc_library_shared {
6136 name: "mylib",
6137 srcs: ["mylib.cpp"],
6138 shared_libs: ["myotherlib"],
6139 system_shared_libs: [],
6140 stl: "none",
6141 apex_available: [
6142 "myapex",
6143 "//apex_available:platform",
6144 ],
6145 }
6146
6147 cc_prebuilt_library_shared {
6148 name: "myotherlib",
6149 srcs: ["prebuilt.so"],
6150 system_shared_libs: [],
6151 stl: "none",
6152 apex_available: [
6153 "myapex",
6154 "//apex_available:platform",
6155 ],
6156 }
6157 `)
6158
6159 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006160 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006161 var builder strings.Builder
6162 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6163 androidMk := builder.String()
6164 // `myotherlib` is added to `myapex` as symlink
6165 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6166 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6167 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6168 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006169 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += mylib.myapex:64 myotherlib:64 apex_manifest.pb.myapex apex_pubkey.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08006170}
6171
Jooyung Han643adc42020-02-27 13:50:06 +09006172func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006173 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006174 apex {
6175 name: "myapex",
6176 key: "myapex.key",
6177 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006178 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006179 }
6180
6181 apex_key {
6182 name: "myapex.key",
6183 public_key: "testkey.avbpubkey",
6184 private_key: "testkey.pem",
6185 }
6186
6187 cc_library {
6188 name: "mylib",
6189 srcs: ["mylib.cpp"],
6190 shared_libs: ["mylib2"],
6191 system_shared_libs: [],
6192 stl: "none",
6193 apex_available: [ "myapex" ],
6194 }
6195
6196 cc_library {
6197 name: "mylib2",
6198 srcs: ["mylib.cpp"],
6199 system_shared_libs: [],
6200 stl: "none",
6201 apex_available: [ "myapex" ],
6202 }
6203 `)
6204
6205 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6206 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6207 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6208 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6209 "lib64/mylib.so",
6210 "lib64/mylib2.so",
6211 })
6212}
6213
Jooyung Han49f67012020-04-17 13:43:10 +09006214func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006215 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006216 apex {
6217 name: "myapex",
6218 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006219 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006220 }
6221 apex_key {
6222 name: "myapex.key",
6223 public_key: "testkey.avbpubkey",
6224 private_key: "testkey.pem",
6225 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006226 `,
6227 android.FixtureModifyConfig(func(config android.Config) {
6228 delete(config.Targets, android.Android)
6229 config.AndroidCommonTarget = android.Target{}
6230 }),
6231 )
Jooyung Han49f67012020-04-17 13:43:10 +09006232
6233 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6234 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6235 }
6236}
6237
Jiyong Parkbd159612020-02-28 15:22:21 +09006238func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006239 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006240 apex {
6241 name: "myapex",
6242 key: "myapex.key",
6243 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006244 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006245 }
6246
6247 apex_key {
6248 name: "myapex.key",
6249 public_key: "testkey.avbpubkey",
6250 private_key: "testkey.pem",
6251 }
6252
6253 android_app {
6254 name: "AppFoo",
6255 srcs: ["foo/bar/MyClass.java"],
6256 sdk_version: "none",
6257 system_modules: "none",
6258 apex_available: [ "myapex" ],
6259 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006260 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006261
Colin Crosscf371cc2020-11-13 11:48:42 -08006262 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006263 content := bundleConfigRule.Args["content"]
6264
6265 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006266 ensureContains(t, content, `"apex_config":{"apex_embedded_apk_config":[{"package_name":"com.android.foo","path":"app/AppFoo/AppFoo.apk"}]}`)
Jiyong Parkbd159612020-02-28 15:22:21 +09006267}
6268
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006269func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006270 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006271 apex {
6272 name: "myapex",
6273 key: "myapex.key",
6274 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006275 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006276 }
6277
6278 apex_key {
6279 name: "myapex.key",
6280 public_key: "testkey.avbpubkey",
6281 private_key: "testkey.pem",
6282 }
6283
6284 android_app_set {
6285 name: "AppSet",
6286 set: "AppSet.apks",
6287 }`)
6288 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006289 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006290 content := bundleConfigRule.Args["content"]
6291 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6292 s := mod.Rule("apexRule").Args["copy_commands"]
6293 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6294 if len(copyCmds) != 3 {
6295 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6296 }
6297 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6298 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6299 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6300}
6301
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006302func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006303 ctx := testApex(t, "", android.FixtureModifyMockFS(func(fs android.MockFS) {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006304 bp := `
6305 apex_set {
6306 name: "myapex",
6307 filename: "foo_v2.apex",
6308 sanitized: {
6309 none: { set: "myapex.apks", },
6310 hwaddress: { set: "myapex.hwasan.apks", },
6311 },
6312 }`
6313 fs["Android.bp"] = []byte(bp)
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006314 }),
6315 prepareForTestWithSantitizeHwaddress,
6316 )
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006317
6318 m := ctx.ModuleForTests("myapex", "android_common")
Paul Duffin37ba3442021-03-29 00:21:08 +01006319 extractedApex := m.Output("out/soong/.intermediates/myapex/android_common/foo_v2.apex")
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006320
6321 actual := extractedApex.Inputs
6322 if len(actual) != 1 {
6323 t.Errorf("expected a single input")
6324 }
6325
6326 expected := "myapex.hwasan.apks"
6327 if actual[0].String() != expected {
6328 t.Errorf("expected %s, got %s", expected, actual[0].String())
6329 }
6330}
6331
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006332func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006333 t.Helper()
6334
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006335 bp := `
6336 java_library {
6337 name: "some-updatable-apex-lib",
6338 srcs: ["a.java"],
6339 sdk_version: "current",
6340 apex_available: [
6341 "some-updatable-apex",
6342 ],
6343 }
6344
6345 java_library {
6346 name: "some-non-updatable-apex-lib",
6347 srcs: ["a.java"],
6348 apex_available: [
6349 "some-non-updatable-apex",
6350 ],
6351 }
6352
6353 java_library {
6354 name: "some-platform-lib",
6355 srcs: ["a.java"],
6356 sdk_version: "current",
6357 installable: true,
6358 }
6359
6360 java_library {
6361 name: "some-art-lib",
6362 srcs: ["a.java"],
6363 sdk_version: "current",
6364 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006365 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006366 ],
6367 hostdex: true,
6368 }
6369
6370 apex {
6371 name: "some-updatable-apex",
6372 key: "some-updatable-apex.key",
6373 java_libs: ["some-updatable-apex-lib"],
6374 updatable: true,
6375 min_sdk_version: "current",
6376 }
6377
6378 apex {
6379 name: "some-non-updatable-apex",
6380 key: "some-non-updatable-apex.key",
6381 java_libs: ["some-non-updatable-apex-lib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006382 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006383 }
6384
6385 apex_key {
6386 name: "some-updatable-apex.key",
6387 }
6388
6389 apex_key {
6390 name: "some-non-updatable-apex.key",
6391 }
6392
6393 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006394 name: "com.android.art.debug",
6395 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006396 java_libs: ["some-art-lib"],
6397 updatable: true,
6398 min_sdk_version: "current",
6399 }
6400
6401 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006402 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006403 }
6404
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006405 filegroup {
6406 name: "some-updatable-apex-file_contexts",
6407 srcs: [
6408 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6409 ],
6410 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006411
6412 filegroup {
6413 name: "some-non-updatable-apex-file_contexts",
6414 srcs: [
6415 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6416 ],
6417 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006418 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006419
6420 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6421}
6422
Paul Duffin064b70c2020-11-02 17:32:38 +00006423func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006424 t.Helper()
6425
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006426 bp += cc.GatherRequiredDepsForTest(android.Android)
6427 bp += java.GatherRequiredDepsForTest()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006428
6429 fs := map[string][]byte{
6430 "a.java": nil,
6431 "a.jar": nil,
6432 "build/make/target/product/security": nil,
6433 "apex_manifest.json": nil,
6434 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006435 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006436 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6437 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6438 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006439 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006440 }
6441 cc.GatherRequiredFilesForTest(fs)
6442
Paul Duffin39853512021-02-26 11:09:39 +00006443 for k, v := range filesForSdkLibrary {
6444 fs[k] = v
6445 }
Paul Duffin37ba3442021-03-29 00:21:08 +01006446 config := android.TestArchConfig(t.TempDir(), nil, bp, fs)
Colin Crossae8600b2020-10-29 17:09:13 -07006447
6448 ctx := android.NewTestArchContext(config)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006449 ctx.RegisterModuleType("apex", BundleFactory)
6450 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Paul Duffin064b70c2020-11-02 17:32:38 +00006451 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006452 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01006453 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin37856732021-02-26 14:24:15 +00006454 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffin021f4e52020-07-30 16:04:17 +01006455 android.RegisterPrebuiltMutators(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006456 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006457 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinf38931c2021-02-05 16:58:28 +00006458 java.RegisterHiddenApiSingletonComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006459 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6460 ctx.PreDepsMutators(RegisterPreDepsMutators)
6461 ctx.PostDepsMutators(RegisterPostDepsMutators)
6462
Colin Crossae8600b2020-10-29 17:09:13 -07006463 ctx.Register()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006464
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006465 pathCtx := android.PathContextForTesting(config)
6466 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6467 transformDexpreoptConfig(dexpreoptConfig)
6468 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6469
Paul Duffinf38931c2021-02-05 16:58:28 +00006470 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
Paul Duffin4fd997b2021-02-03 20:06:33 +00006471 // product variables.
Paul Duffinf38931c2021-02-05 16:58:28 +00006472 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6473 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6474
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006475 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6476 android.FailIfErrored(t, errs)
6477
6478 _, errs = ctx.PrepareBuildActions(config)
6479 if errmsg == "" {
6480 android.FailIfErrored(t, errs)
6481 } else if len(errs) > 0 {
6482 android.FailIfNoMatchingErrors(t, errmsg, errs)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006483 } else {
6484 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6485 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006486
6487 return ctx
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006488}
6489
Jooyung Han548640b2020-04-27 12:10:30 +09006490func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6491 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6492 apex {
6493 name: "myapex",
6494 key: "myapex.key",
6495 updatable: true,
6496 }
6497
6498 apex_key {
6499 name: "myapex.key",
6500 public_key: "testkey.avbpubkey",
6501 private_key: "testkey.pem",
6502 }
6503 `)
6504}
6505
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006506func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6507 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6508 apex {
6509 name: "myapex",
6510 key: "myapex.key",
6511 }
6512
6513 apex_key {
6514 name: "myapex.key",
6515 public_key: "testkey.avbpubkey",
6516 private_key: "testkey.pem",
6517 }
6518 `)
6519}
6520
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006521func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006522 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006523 var transform func(*dexpreopt.GlobalConfig)
6524
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006525 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6526 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006527 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006528 }
6529 testNoUpdatableJarsInBootImage(t, "", transform)
6530 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006531
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006532 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006533 err = `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006534 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006535 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006536 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006537 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006538 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006539
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006540 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006541 err = `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006542 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006543 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006544 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006545 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006546 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006547
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006548 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006549 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006550 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006551 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006552 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006553 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006554 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006555
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006556 t.Run("updatable jar from some other apex in the framework boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006557 err = `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006558 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006559 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006560 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006561 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006562 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006563
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006564 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6565 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006566 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006567 }
6568 testNoUpdatableJarsInBootImage(t, "", transform)
6569 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006570
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006571 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006572 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006573 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006574 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006575 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006576 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006577 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006578
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006579 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006580 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006581 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006582 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006583 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006584 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006585 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006586
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006587 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006588 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006589 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006590 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006591 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006592 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006593 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006594
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006595 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6596 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006597 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006598 }
6599 testNoUpdatableJarsInBootImage(t, "", transform)
6600 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006601
6602}
6603
6604func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6605 transform := func(config *dexpreopt.GlobalConfig) {
6606 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6607 }
6608 t.Run("prebuilt no source", func(t *testing.T) {
6609 testDexpreoptWithApexes(t, `
6610 prebuilt_apex {
6611 name: "myapex" ,
6612 arch: {
6613 arm64: {
6614 src: "myapex-arm64.apex",
6615 },
6616 arm: {
6617 src: "myapex-arm.apex",
6618 },
6619 },
6620 exported_java_libs: ["libfoo"],
6621 }
6622
6623 java_import {
6624 name: "libfoo",
6625 jars: ["libfoo.jar"],
6626 }
6627`, "", transform)
6628 })
6629
6630 t.Run("prebuilt no source", func(t *testing.T) {
6631 testDexpreoptWithApexes(t, `
6632 prebuilt_apex {
6633 name: "myapex" ,
6634 arch: {
6635 arm64: {
6636 src: "myapex-arm64.apex",
6637 },
6638 arm: {
6639 src: "myapex-arm.apex",
6640 },
6641 },
6642 exported_java_libs: ["libfoo"],
6643 }
6644
6645 java_import {
6646 name: "libfoo",
6647 jars: ["libfoo.jar"],
6648 }
6649`, "", transform)
6650 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006651}
6652
Andrei Onea115e7e72020-06-05 21:14:03 +01006653func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6654 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006655 bp += `
6656 apex_key {
6657 name: "myapex.key",
6658 public_key: "testkey.avbpubkey",
6659 private_key: "testkey.pem",
6660 }`
6661 fs := map[string][]byte{
6662 "lib1/src/A.java": nil,
6663 "lib2/src/B.java": nil,
6664 "system/sepolicy/apex/myapex-file_contexts": nil,
6665 }
6666
Paul Duffin37ba3442021-03-29 00:21:08 +01006667 config := android.TestArchConfig(t.TempDir(), nil, bp, fs)
Colin Crossae8600b2020-10-29 17:09:13 -07006668 android.SetTestNeverallowRules(config, rules)
6669 updatableBootJars := make([]string, 0, len(apexBootJars))
6670 for _, apexBootJar := range apexBootJars {
6671 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6672 }
6673 config.TestProductVariables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6674
6675 ctx := android.NewTestArchContext(config)
Andrei Onea115e7e72020-06-05 21:14:03 +01006676 ctx.RegisterModuleType("apex", BundleFactory)
6677 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
6678 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
6679 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006680 java.RegisterRequiredBuildComponentsForTest(ctx)
Andrei Onea115e7e72020-06-05 21:14:03 +01006681 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6682 ctx.PreDepsMutators(RegisterPreDepsMutators)
6683 ctx.PostDepsMutators(RegisterPostDepsMutators)
6684 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
6685
Colin Crossae8600b2020-10-29 17:09:13 -07006686 ctx.Register()
Andrei Onea115e7e72020-06-05 21:14:03 +01006687
6688 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6689 android.FailIfErrored(t, errs)
6690
6691 _, errs = ctx.PrepareBuildActions(config)
6692 if errmsg == "" {
6693 android.FailIfErrored(t, errs)
6694 } else if len(errs) > 0 {
6695 android.FailIfNoMatchingErrors(t, errmsg, errs)
6696 return
6697 } else {
6698 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6699 }
6700}
6701
6702func TestApexPermittedPackagesRules(t *testing.T) {
6703 testcases := []struct {
6704 name string
6705 expectedError string
6706 bp string
6707 bootJars []string
6708 modulesPackages map[string][]string
6709 }{
6710
6711 {
6712 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6713 expectedError: "",
6714 bp: `
6715 java_library {
6716 name: "bcp_lib1",
6717 srcs: ["lib1/src/*.java"],
6718 permitted_packages: ["foo.bar"],
6719 apex_available: ["myapex"],
6720 sdk_version: "none",
6721 system_modules: "none",
6722 }
6723 java_library {
6724 name: "nonbcp_lib2",
6725 srcs: ["lib2/src/*.java"],
6726 apex_available: ["myapex"],
6727 permitted_packages: ["a.b"],
6728 sdk_version: "none",
6729 system_modules: "none",
6730 }
6731 apex {
6732 name: "myapex",
6733 key: "myapex.key",
6734 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006735 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006736 }`,
6737 bootJars: []string{"bcp_lib1"},
6738 modulesPackages: map[string][]string{
6739 "myapex": []string{
6740 "foo.bar",
6741 },
6742 },
6743 },
6744 {
6745 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6746 expectedError: `module "bcp_lib2" .* which is restricted because jars that are part of the myapex module may only allow these packages: foo.bar. Please jarjar or move code around.`,
6747 bp: `
6748 java_library {
6749 name: "bcp_lib1",
6750 srcs: ["lib1/src/*.java"],
6751 apex_available: ["myapex"],
6752 permitted_packages: ["foo.bar"],
6753 sdk_version: "none",
6754 system_modules: "none",
6755 }
6756 java_library {
6757 name: "bcp_lib2",
6758 srcs: ["lib2/src/*.java"],
6759 apex_available: ["myapex"],
6760 permitted_packages: ["foo.bar", "bar.baz"],
6761 sdk_version: "none",
6762 system_modules: "none",
6763 }
6764 apex {
6765 name: "myapex",
6766 key: "myapex.key",
6767 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006768 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006769 }
6770 `,
6771 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6772 modulesPackages: map[string][]string{
6773 "myapex": []string{
6774 "foo.bar",
6775 },
6776 },
6777 },
6778 }
6779 for _, tc := range testcases {
6780 t.Run(tc.name, func(t *testing.T) {
6781 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6782 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6783 })
6784 }
6785}
6786
Jiyong Park62304bb2020-04-13 16:19:48 +09006787func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006788 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006789 apex {
6790 name: "myapex",
6791 key: "myapex.key",
6792 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006793 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006794 }
6795
6796 apex_key {
6797 name: "myapex.key",
6798 public_key: "testkey.avbpubkey",
6799 private_key: "testkey.pem",
6800 }
6801
6802 cc_library {
6803 name: "mylib",
6804 srcs: ["mylib.cpp"],
6805 system_shared_libs: [],
6806 stl: "none",
6807 stubs: {
6808 versions: ["1"],
6809 },
6810 apex_available: ["myapex"],
6811 }
6812
6813 cc_library {
6814 name: "myprivlib",
6815 srcs: ["mylib.cpp"],
6816 system_shared_libs: [],
6817 stl: "none",
6818 apex_available: ["myapex"],
6819 }
6820
6821
6822 cc_test {
6823 name: "mytest",
6824 gtest: false,
6825 srcs: ["mylib.cpp"],
6826 system_shared_libs: [],
6827 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006828 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006829 test_for: ["myapex"]
6830 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006831
6832 cc_library {
6833 name: "mytestlib",
6834 srcs: ["mylib.cpp"],
6835 system_shared_libs: [],
6836 shared_libs: ["mylib", "myprivlib"],
6837 stl: "none",
6838 test_for: ["myapex"],
6839 }
6840
6841 cc_benchmark {
6842 name: "mybench",
6843 srcs: ["mylib.cpp"],
6844 system_shared_libs: [],
6845 shared_libs: ["mylib", "myprivlib"],
6846 stl: "none",
6847 test_for: ["myapex"],
6848 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006849 `)
6850
6851 // the test 'mytest' is a test for the apex, therefore is linked to the
6852 // actual implementation of mylib instead of its stub.
6853 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6854 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6855 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park46a512f2020-12-04 18:02:13 +09006856
6857 // The same should be true for cc_library
6858 ldFlags = ctx.ModuleForTests("mytestlib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
6859 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6860 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
6861
6862 // ... and for cc_benchmark
6863 ldFlags = ctx.ModuleForTests("mybench", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6864 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6865 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09006866}
6867
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006868// TODO(jungjw): Move this to proptools
6869func intPtr(i int) *int {
6870 return &i
6871}
6872
6873func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006874 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006875 apex_set {
6876 name: "myapex",
6877 set: "myapex.apks",
6878 filename: "foo_v2.apex",
6879 overrides: ["foo"],
6880 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006881 `,
6882 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6883 variables.Platform_sdk_version = intPtr(30)
6884 }),
6885 android.FixtureModifyConfig(func(config android.Config) {
6886 config.Targets[android.Android] = []android.Target{
6887 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
6888 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
6889 }
6890 }),
6891 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006892
6893 m := ctx.ModuleForTests("myapex", "android_common")
6894
6895 // Check extract_apks tool parameters.
Paul Duffin37ba3442021-03-29 00:21:08 +01006896 extractedApex := m.Output("out/soong/.intermediates/myapex/android_common/foo_v2.apex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006897 actual := extractedApex.Args["abis"]
6898 expected := "ARMEABI_V7A,ARM64_V8A"
6899 if actual != expected {
6900 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6901 }
6902 actual = extractedApex.Args["sdk-version"]
6903 expected = "30"
6904 if actual != expected {
6905 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6906 }
6907
6908 a := m.Module().(*ApexSet)
6909 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07006910 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006911 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
6912 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
6913 }
6914}
6915
Jiyong Park7d95a512020-05-10 15:16:24 +09006916func TestNoStaticLinkingToStubsLib(t *testing.T) {
6917 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
6918 apex {
6919 name: "myapex",
6920 key: "myapex.key",
6921 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006922 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09006923 }
6924
6925 apex_key {
6926 name: "myapex.key",
6927 public_key: "testkey.avbpubkey",
6928 private_key: "testkey.pem",
6929 }
6930
6931 cc_library {
6932 name: "mylib",
6933 srcs: ["mylib.cpp"],
6934 static_libs: ["otherlib"],
6935 system_shared_libs: [],
6936 stl: "none",
6937 apex_available: [ "myapex" ],
6938 }
6939
6940 cc_library {
6941 name: "otherlib",
6942 srcs: ["mylib.cpp"],
6943 system_shared_libs: [],
6944 stl: "none",
6945 stubs: {
6946 versions: ["1", "2", "3"],
6947 },
6948 apex_available: [ "myapex" ],
6949 }
6950 `)
6951}
6952
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006953func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006954 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006955 apex {
6956 name: "myapex",
6957 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006958 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006959 }
6960
6961 apex_key {
6962 name: "myapex.key",
6963 public_key: "testkey.avbpubkey",
6964 private_key: "testkey.pem",
6965 }
6966
6967 prebuilt_apex {
6968 name: "myapex",
6969 prefer: true,
6970 arch: {
6971 arm64: {
6972 src: "myapex-arm64.apex",
6973 },
6974 arm: {
6975 src: "myapex-arm.apex",
6976 },
6977 },
6978 }
6979
6980 apex_set {
6981 name: "myapex_set",
6982 set: "myapex.apks",
6983 filename: "myapex_set.apex",
6984 overrides: ["myapex"],
6985 }
6986 `)
6987
6988 apexKeysText := ctx.SingletonForTests("apex_keys_text")
6989 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
6990 ensureContains(t, content, `name="myapex_set.apex" public_key="PRESIGNED" private_key="PRESIGNED" container_certificate="PRESIGNED" container_private_key="PRESIGNED" partition="system"`)
Jiyong Park03a7f3e2020-06-18 19:34:42 +09006991 ensureContains(t, content, `name="myapex.apex" public_key="PRESIGNED" private_key="PRESIGNED" container_certificate="PRESIGNED" container_private_key="PRESIGNED" partition="system"`)
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006992}
6993
Jooyung Han938b5932020-06-20 12:47:47 +09006994func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006995 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09006996 apex {
6997 name: "myapex",
6998 key: "myapex.key",
6999 apps: ["app"],
7000 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007001 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007002 }
7003
7004 apex_key {
7005 name: "myapex.key",
7006 public_key: "testkey.avbpubkey",
7007 private_key: "testkey.pem",
7008 }
7009
7010 android_app {
7011 name: "app",
7012 srcs: ["foo/bar/MyClass.java"],
7013 package_name: "foo",
7014 sdk_version: "none",
7015 system_modules: "none",
7016 apex_available: [ "myapex" ],
7017 }
7018 `, withFiles(map[string][]byte{
7019 "sub/Android.bp": []byte(`
7020 override_apex {
7021 name: "override_myapex",
7022 base: "myapex",
7023 apps: ["override_app"],
7024 allowed_files: ":allowed",
7025 }
7026 // Overridable "path" property should be referenced indirectly
7027 filegroup {
7028 name: "allowed",
7029 srcs: ["allowed.txt"],
7030 }
7031 override_android_app {
7032 name: "override_app",
7033 base: "app",
7034 package_name: "bar",
7035 }
7036 `),
7037 }))
7038
7039 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7040 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7041 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7042 }
7043
7044 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7045 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7046 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7047 }
7048}
7049
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007050func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007051 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007052 apex {
7053 name: "myapex",
7054 key: "myapex.key",
7055 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007056 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007057 }
7058
7059 apex_key {
7060 name: "myapex.key",
7061 public_key: "testkey.avbpubkey",
7062 private_key: "testkey.pem",
7063 }
7064
7065 cc_library {
7066 name: "mylib",
7067 srcs: ["mylib.cpp"],
7068 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007069 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007070 },
7071 apex_available: ["myapex"],
7072 }
7073
7074 cc_prebuilt_library_shared {
7075 name: "mylib",
7076 prefer: false,
7077 srcs: ["prebuilt.so"],
7078 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007079 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007080 },
7081 apex_available: ["myapex"],
7082 }
7083 `)
7084}
7085
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007086func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007087 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007088 apex {
7089 name: "myapex",
7090 key: "myapex.key",
7091 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007092 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007093 }
7094 apex_key {
7095 name: "myapex.key",
7096 public_key: "testkey.avbpubkey",
7097 private_key: "testkey.pem",
7098 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007099 `,
7100 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7101 variables.CompressedApex = proptools.BoolPtr(true)
7102 }),
7103 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007104
7105 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7106 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7107
7108 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7109 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7110
7111 // Make sure output of bundle is .capex
7112 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7113 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7114
7115 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007116 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007117 var builder strings.Builder
7118 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7119 androidMk := builder.String()
7120 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7121}
7122
Martin Stjernholm2856c662020-12-02 15:03:42 +00007123func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007124 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007125 apex {
7126 name: "myapex",
7127 key: "myapex.key",
7128 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007129 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007130 }
7131
7132 apex_key {
7133 name: "myapex.key",
7134 public_key: "testkey.avbpubkey",
7135 private_key: "testkey.pem",
7136 }
7137
7138 cc_library {
7139 name: "mylib",
7140 srcs: ["mylib.cpp"],
7141 apex_available: ["myapex"],
7142 shared_libs: ["otherlib"],
7143 system_shared_libs: [],
7144 }
7145
7146 cc_library {
7147 name: "otherlib",
7148 srcs: ["mylib.cpp"],
7149 stubs: {
7150 versions: ["current"],
7151 },
7152 }
7153
7154 cc_prebuilt_library_shared {
7155 name: "otherlib",
7156 prefer: true,
7157 srcs: ["prebuilt.so"],
7158 stubs: {
7159 versions: ["current"],
7160 },
7161 }
7162 `)
7163
7164 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007165 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007166 var builder strings.Builder
7167 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7168 androidMk := builder.String()
7169
7170 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7171 // a thing there.
7172 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7173}
7174
Jiyong Parke3867542020-12-03 17:28:25 +09007175func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007176 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007177 apex {
7178 name: "myapex",
7179 key: "myapex.key",
7180 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007181 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007182 }
7183
7184 apex_key {
7185 name: "myapex.key",
7186 public_key: "testkey.avbpubkey",
7187 private_key: "testkey.pem",
7188 }
7189
7190 cc_library {
7191 name: "mylib",
7192 srcs: ["mylib.cpp"],
7193 system_shared_libs: [],
7194 stl: "none",
7195 apex_available: ["myapex"],
7196 shared_libs: ["mylib2"],
7197 target: {
7198 apex: {
7199 exclude_shared_libs: ["mylib2"],
7200 },
7201 },
7202 }
7203
7204 cc_library {
7205 name: "mylib2",
7206 srcs: ["mylib.cpp"],
7207 system_shared_libs: [],
7208 stl: "none",
7209 }
7210 `)
7211
7212 // Check if mylib is linked to mylib2 for the non-apex target
7213 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7214 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7215
7216 // Make sure that the link doesn't occur for the apex target
7217 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7218 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7219
7220 // It shouldn't appear in the copy cmd as well.
7221 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7222 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7223}
7224
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007225func TestPrebuiltStubLibDep(t *testing.T) {
7226 bpBase := `
7227 apex {
7228 name: "myapex",
7229 key: "myapex.key",
7230 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007231 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007232 }
7233 apex_key {
7234 name: "myapex.key",
7235 public_key: "testkey.avbpubkey",
7236 private_key: "testkey.pem",
7237 }
7238 cc_library {
7239 name: "mylib",
7240 srcs: ["mylib.cpp"],
7241 apex_available: ["myapex"],
7242 shared_libs: ["stublib"],
7243 system_shared_libs: [],
7244 }
7245 apex {
7246 name: "otherapex",
7247 enabled: %s,
7248 key: "myapex.key",
7249 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007250 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007251 }
7252 `
7253
7254 stublibSourceBp := `
7255 cc_library {
7256 name: "stublib",
7257 srcs: ["mylib.cpp"],
7258 apex_available: ["otherapex"],
7259 system_shared_libs: [],
7260 stl: "none",
7261 stubs: {
7262 versions: ["1"],
7263 },
7264 }
7265 `
7266
7267 stublibPrebuiltBp := `
7268 cc_prebuilt_library_shared {
7269 name: "stublib",
7270 srcs: ["prebuilt.so"],
7271 apex_available: ["otherapex"],
7272 stubs: {
7273 versions: ["1"],
7274 },
7275 %s
7276 }
7277 `
7278
7279 tests := []struct {
7280 name string
7281 stublibBp string
7282 usePrebuilt bool
7283 modNames []string // Modules to collect AndroidMkEntries for
7284 otherApexEnabled []string
7285 }{
7286 {
7287 name: "only_source",
7288 stublibBp: stublibSourceBp,
7289 usePrebuilt: false,
7290 modNames: []string{"stublib"},
7291 otherApexEnabled: []string{"true", "false"},
7292 },
7293 {
7294 name: "source_preferred",
7295 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7296 usePrebuilt: false,
7297 modNames: []string{"stublib", "prebuilt_stublib"},
7298 otherApexEnabled: []string{"true", "false"},
7299 },
7300 {
7301 name: "prebuilt_preferred",
7302 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7303 usePrebuilt: true,
7304 modNames: []string{"stublib", "prebuilt_stublib"},
7305 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7306 },
7307 {
7308 name: "only_prebuilt",
7309 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7310 usePrebuilt: true,
7311 modNames: []string{"stublib"},
7312 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7313 },
7314 }
7315
7316 for _, test := range tests {
7317 t.Run(test.name, func(t *testing.T) {
7318 for _, otherApexEnabled := range test.otherApexEnabled {
7319 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007320 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007321
7322 type modAndMkEntries struct {
7323 mod *cc.Module
7324 mkEntries android.AndroidMkEntries
7325 }
7326 entries := []*modAndMkEntries{}
7327
7328 // Gather shared lib modules that are installable
7329 for _, modName := range test.modNames {
7330 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7331 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7332 continue
7333 }
7334 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007335 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007336 continue
7337 }
Colin Crossaa255532020-07-03 13:18:24 -07007338 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007339 if ent.Disabled {
7340 continue
7341 }
7342 entries = append(entries, &modAndMkEntries{
7343 mod: mod,
7344 mkEntries: ent,
7345 })
7346 }
7347 }
7348 }
7349
7350 var entry *modAndMkEntries = nil
7351 for _, ent := range entries {
7352 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7353 if entry != nil {
7354 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7355 } else {
7356 entry = ent
7357 }
7358 }
7359 }
7360
7361 if entry == nil {
7362 t.Errorf("AndroidMk entry for \"stublib\" missing")
7363 } else {
7364 isPrebuilt := entry.mod.Prebuilt() != nil
7365 if isPrebuilt != test.usePrebuilt {
7366 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7367 }
7368 if !entry.mod.IsStubs() {
7369 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7370 }
7371 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7372 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7373 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007374 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
7375 expected := "-D__STUBLIB_API__=1"
7376 if !android.InList(expected, cflags) {
7377 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7378 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007379 }
7380 })
7381 }
7382 })
7383 }
7384}
7385
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007386func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01007387 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007388}