blob: 4fb2109a3fef424da5a4a4536227d855429583de [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 "io/ioutil"
20 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090021 "path"
Paul Duffin37856732021-02-26 14:24:15 +000022 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070023 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010024 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090025 "sort"
Jiyong Park25fc6a92018-11-18 18:02:45 +090026 "strings"
27 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090028
29 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jaewoong Jung14f5ff62019-06-18 13:09:13 -070041var buildDir string
42
Jooyung Hand3639552019-08-09 12:57:43 +090043// names returns name list from white space separated string
44func names(s string) (ns []string) {
45 for _, n := range strings.Split(s, " ") {
46 if len(n) > 0 {
47 ns = append(ns, n)
48 }
49 }
50 return
51}
52
Paul Duffin40b62572021-03-20 11:39:01 +000053func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090054 t.Helper()
Paul Duffin40b62572021-03-20 11:39:01 +000055 apexFixtureFactory.Extend(preparers...).
Paul Duffine05480a2021-03-08 15:07:14 +000056 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000057 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090058}
59
Paul Duffin40b62572021-03-20 11:39:01 +000060func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090061 t.Helper()
Paul Duffin40b62572021-03-20 11:39:01 +000062 factory := apexFixtureFactory.Extend(preparers...)
63 if bp != "" {
64 factory = factory.Extend(android.FixtureWithRootAndroidBp(bp))
65 }
66 result := factory.RunTest(t)
Paul Duffine05480a2021-03-08 15:07:14 +000067 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090068}
69
Paul Duffin810f33d2021-03-09 14:12:32 +000070func withFiles(files android.MockFS) android.FixturePreparer {
71 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090072}
73
Paul Duffin810f33d2021-03-09 14:12:32 +000074func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
75 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090076 for k, v := range targets {
77 config.Targets[k] = v
78 }
Paul Duffin810f33d2021-03-09 14:12:32 +000079 })
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Jooyung Han35155c42020-02-06 17:33:20 +090082// withNativeBridgeTargets sets configuration with targets including:
83// - X86_64 (primary)
84// - X86 (secondary)
85// - Arm64 on X86_64 (native bridge)
86// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000087var withNativeBridgeEnabled = android.FixtureModifyConfig(
88 func(config android.Config) {
89 config.Targets[android.Android] = []android.Target{
90 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
91 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
92 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
93 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
94 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
95 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
96 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
97 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
98 }
99 },
100)
101
102func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
103 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
104 variables.ManifestPackageNameOverrides = specs
105 })
Jooyung Han35155c42020-02-06 17:33:20 +0900106}
107
Paul Duffin810f33d2021-03-09 14:12:32 +0000108var withBinder32bit = android.FixtureModifyProductVariables(
109 func(variables android.FixtureProductVariables) {
110 variables.Binder32bit = proptools.BoolPtr(true)
111 },
112)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900113
Paul Duffin810f33d2021-03-09 14:12:32 +0000114var withUnbundledBuild = android.FixtureModifyProductVariables(
115 func(variables android.FixtureProductVariables) {
116 variables.Unbundled_build = proptools.BoolPtr(true)
117 },
118)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900119
Paul Duffin37aad602021-03-08 09:47:16 +0000120var apexFixtureFactory = android.NewFixtureFactory(
121 &buildDir,
122 // General preparers in alphabetical order as test infrastructure will enforce correct
123 // registration order.
124 android.PrepareForTestWithAndroidBuildComponents,
125 bpf.PrepareForTestWithBpf,
126 cc.PrepareForTestWithCcBuildComponents,
127 java.PrepareForTestWithJavaDefaultModules,
128 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
129 rust.PrepareForTestWithRustDefaultModules,
130 sh.PrepareForTestWithShBuildComponents,
131
132 PrepareForTestWithApexBuildComponents,
133
134 // Additional apex test specific preparers.
135 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
136 filegroup {
137 name: "myapex-file_contexts",
138 srcs: [
139 "apex/myapex-file_contexts",
140 ],
141 }
142 `),
143 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin76e5c8a2021-03-20 14:19:46 +0000144 "a.java": nil,
145 "PrebuiltAppFoo.apk": nil,
146 "PrebuiltAppFooPriv.apk": nil,
147 "apex_manifest.json": nil,
148 "AndroidManifest.xml": nil,
149 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000150 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
151 "system/sepolicy/apex/myapex2-file_contexts": nil,
152 "system/sepolicy/apex/otherapex-file_contexts": nil,
153 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
154 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
155 "mylib.cpp": nil,
156 "mytest.cpp": nil,
157 "mytest1.cpp": nil,
158 "mytest2.cpp": nil,
159 "mytest3.cpp": nil,
160 "myprebuilt": nil,
161 "my_include": nil,
162 "foo/bar/MyClass.java": nil,
163 "prebuilt.jar": nil,
164 "prebuilt.so": nil,
165 "vendor/foo/devkeys/test.x509.pem": nil,
166 "vendor/foo/devkeys/test.pk8": nil,
167 "testkey.x509.pem": nil,
168 "testkey.pk8": nil,
169 "testkey.override.x509.pem": nil,
170 "testkey.override.pk8": nil,
171 "vendor/foo/devkeys/testkey.avbpubkey": nil,
172 "vendor/foo/devkeys/testkey.pem": nil,
173 "NOTICE": nil,
174 "custom_notice": nil,
175 "custom_notice_for_static_lib": nil,
176 "testkey2.avbpubkey": nil,
177 "testkey2.pem": nil,
178 "myapex-arm64.apex": nil,
179 "myapex-arm.apex": nil,
180 "myapex.apks": nil,
181 "frameworks/base/api/current.txt": nil,
182 "framework/aidl/a.aidl": nil,
183 "build/make/core/proguard.flags": nil,
184 "build/make/core/proguard_basic_keeps.flags": nil,
185 "dummy.txt": nil,
186 "baz": nil,
187 "bar/baz": nil,
188 "testdata/baz": nil,
189 "AppSet.apks": nil,
190 "foo.rs": nil,
191 "libfoo.jar": nil,
192 "libbar.jar": nil,
193 },
194 ),
195
196 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
197 variables.DeviceVndkVersion = proptools.StringPtr("current")
198 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
199 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
200 variables.Platform_sdk_codename = proptools.StringPtr("Q")
201 variables.Platform_sdk_final = proptools.BoolPtr(false)
202 variables.Platform_version_active_codenames = []string{"Q"}
203 variables.Platform_vndk_version = proptools.StringPtr("VER")
204 }),
205)
206
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700207func setUp() {
208 var err error
209 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900210 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700211 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900212 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900213}
214
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700215func tearDown() {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700216 _ = os.RemoveAll(buildDir)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900217}
218
Jooyung Han643adc42020-02-27 13:50:06 +0900219// ensure that 'result' equals 'expected'
220func ensureEquals(t *testing.T, result string, expected string) {
221 t.Helper()
222 if result != expected {
223 t.Errorf("%q != %q", expected, result)
224 }
225}
226
Jiyong Park25fc6a92018-11-18 18:02:45 +0900227// ensure that 'result' contains 'expected'
228func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900229 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900230 if !strings.Contains(result, expected) {
231 t.Errorf("%q is not found in %q", expected, result)
232 }
233}
234
Liz Kammer5bd365f2020-05-27 15:15:11 -0700235// ensure that 'result' contains 'expected' exactly one time
236func ensureContainsOnce(t *testing.T, result string, expected string) {
237 t.Helper()
238 count := strings.Count(result, expected)
239 if count != 1 {
240 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
241 }
242}
243
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244// ensures that 'result' does not contain 'notExpected'
245func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900246 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247 if strings.Contains(result, notExpected) {
248 t.Errorf("%q is found in %q", notExpected, result)
249 }
250}
251
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700252func ensureMatches(t *testing.T, result string, expectedRex string) {
253 ok, err := regexp.MatchString(expectedRex, result)
254 if err != nil {
255 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
256 return
257 }
258 if !ok {
259 t.Errorf("%s does not match regular expession %s", result, expectedRex)
260 }
261}
262
Jiyong Park25fc6a92018-11-18 18:02:45 +0900263func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900264 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900265 if !android.InList(expected, result) {
266 t.Errorf("%q is not found in %v", expected, result)
267 }
268}
269
270func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900271 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900272 if android.InList(notExpected, result) {
273 t.Errorf("%q is found in %v", notExpected, result)
274 }
275}
276
Jooyung Hane1633032019-08-01 17:41:43 +0900277func ensureListEmpty(t *testing.T, result []string) {
278 t.Helper()
279 if len(result) > 0 {
280 t.Errorf("%q is expected to be empty", result)
281 }
282}
283
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000284func ensureListNotEmpty(t *testing.T, result []string) {
285 t.Helper()
286 if len(result) == 0 {
287 t.Errorf("%q is expected to be not empty", result)
288 }
289}
290
Jiyong Park25fc6a92018-11-18 18:02:45 +0900291// Minimal test
292func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800293 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900294 apex_defaults {
295 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900296 manifest: ":myapex.manifest",
297 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900298 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900299 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900300 native_shared_libs: [
301 "mylib",
302 "libfoo.ffi",
303 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900304 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800305 multilib: {
306 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900307 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800308 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900309 },
Jiyong Park77acec62020-06-01 21:39:15 +0900310 java_libs: [
311 "myjar",
312 "myjar_dex",
313 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000314 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315 }
316
Jiyong Park30ca9372019-02-07 16:27:23 +0900317 apex {
318 name: "myapex",
319 defaults: ["myapex-defaults"],
320 }
321
Jiyong Park25fc6a92018-11-18 18:02:45 +0900322 apex_key {
323 name: "myapex.key",
324 public_key: "testkey.avbpubkey",
325 private_key: "testkey.pem",
326 }
327
Jiyong Park809bb722019-02-13 21:33:49 +0900328 filegroup {
329 name: "myapex.manifest",
330 srcs: ["apex_manifest.json"],
331 }
332
333 filegroup {
334 name: "myapex.androidmanifest",
335 srcs: ["AndroidManifest.xml"],
336 }
337
Jiyong Park25fc6a92018-11-18 18:02:45 +0900338 cc_library {
339 name: "mylib",
340 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900341 shared_libs: [
342 "mylib2",
343 "libbar.ffi",
344 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900345 system_shared_libs: [],
346 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000347 // TODO: remove //apex_available:platform
348 apex_available: [
349 "//apex_available:platform",
350 "myapex",
351 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900352 }
353
Alex Light3d673592019-01-18 14:37:31 -0800354 cc_binary {
355 name: "foo",
356 srcs: ["mylib.cpp"],
357 compile_multilib: "both",
358 multilib: {
359 lib32: {
360 suffix: "32",
361 },
362 lib64: {
363 suffix: "64",
364 },
365 },
366 symlinks: ["foo_link_"],
367 symlink_preferred_arch: true,
368 system_shared_libs: [],
369 static_executable: true,
370 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700371 apex_available: [ "myapex", "com.android.gki.*" ],
372 }
373
Jiyong Park99644e92020-11-17 22:21:02 +0900374 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000375 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900376 srcs: ["foo.rs"],
377 rlibs: ["libfoo.rlib.rust"],
378 dylibs: ["libfoo.dylib.rust"],
379 apex_available: ["myapex"],
380 }
381
382 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000383 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900384 srcs: ["foo.rs"],
385 crate_name: "foo",
386 apex_available: ["myapex"],
387 }
388
389 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000390 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900391 srcs: ["foo.rs"],
392 crate_name: "foo",
393 apex_available: ["myapex"],
394 }
395
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900396 rust_ffi_shared {
397 name: "libfoo.ffi",
398 srcs: ["foo.rs"],
399 crate_name: "foo",
400 apex_available: ["myapex"],
401 }
402
403 rust_ffi_shared {
404 name: "libbar.ffi",
405 srcs: ["foo.rs"],
406 crate_name: "bar",
407 apex_available: ["myapex"],
408 }
409
Yifan Hongd22a84a2020-07-28 17:37:46 -0700410 apex {
411 name: "com.android.gki.fake",
412 binaries: ["foo"],
413 key: "myapex.key",
414 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000415 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800416 }
417
Paul Duffindddd5462020-04-07 15:25:44 +0100418 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900419 name: "mylib2",
420 srcs: ["mylib.cpp"],
421 system_shared_libs: [],
422 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900423 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900424 static_libs: ["libstatic"],
425 // TODO: remove //apex_available:platform
426 apex_available: [
427 "//apex_available:platform",
428 "myapex",
429 ],
430 }
431
Paul Duffindddd5462020-04-07 15:25:44 +0100432 cc_prebuilt_library_shared {
433 name: "mylib2",
434 srcs: ["prebuilt.so"],
435 // TODO: remove //apex_available:platform
436 apex_available: [
437 "//apex_available:platform",
438 "myapex",
439 ],
440 }
441
Jiyong Park9918e1a2020-03-17 19:16:40 +0900442 cc_library_static {
443 name: "libstatic",
444 srcs: ["mylib.cpp"],
445 system_shared_libs: [],
446 stl: "none",
447 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000448 // TODO: remove //apex_available:platform
449 apex_available: [
450 "//apex_available:platform",
451 "myapex",
452 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900453 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900454
455 java_library {
456 name: "myjar",
457 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900458 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900459 sdk_version: "none",
460 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900461 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900462 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000463 // TODO: remove //apex_available:platform
464 apex_available: [
465 "//apex_available:platform",
466 "myapex",
467 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900468 }
469
Jiyong Park77acec62020-06-01 21:39:15 +0900470 dex_import {
471 name: "myjar_dex",
472 jars: ["prebuilt.jar"],
473 apex_available: [
474 "//apex_available:platform",
475 "myapex",
476 ],
477 }
478
Jiyong Park7f7766d2019-07-25 22:02:35 +0900479 java_library {
480 name: "myotherjar",
481 srcs: ["foo/bar/MyClass.java"],
482 sdk_version: "none",
483 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900484 // TODO: remove //apex_available:platform
485 apex_available: [
486 "//apex_available:platform",
487 "myapex",
488 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900489 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900490
491 java_library {
492 name: "mysharedjar",
493 srcs: ["foo/bar/MyClass.java"],
494 sdk_version: "none",
495 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900496 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900497 `)
498
Sundong Ahnabb64432019-10-22 13:58:29 +0900499 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900500
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900501 // Make sure that Android.mk is created
502 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700503 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900504 var builder strings.Builder
505 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
506
507 androidMk := builder.String()
508 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
509 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
510
Jiyong Park42cca6c2019-04-01 11:15:50 +0900511 optFlags := apexRule.Args["opt_flags"]
512 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700513 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900514 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900515
Jiyong Park25fc6a92018-11-18 18:02:45 +0900516 copyCmds := apexRule.Args["copy_commands"]
517
518 // Ensure that main rule creates an output
519 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
520
521 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700522 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
523 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
524 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900525 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900526 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900527
528 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700529 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
530 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900531 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
532 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900533 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900534
535 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800536 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
537 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900538 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900539 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900540 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900541 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
542 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900543 // .. but not for java libs
544 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900545 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800546
Colin Cross7113d202019-11-20 16:39:12 -0800547 // Ensure that the platform variant ends with _shared or _common
548 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
549 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900550 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
551 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900552 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
553
554 // Ensure that dynamic dependency to java libs are not included
555 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800556
557 // Ensure that all symlinks are present.
558 found_foo_link_64 := false
559 found_foo := false
560 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900561 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800562 if strings.HasSuffix(cmd, "bin/foo") {
563 found_foo = true
564 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
565 found_foo_link_64 = true
566 }
567 }
568 }
569 good := found_foo && found_foo_link_64
570 if !good {
571 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
572 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900573
Sundong Ahnabb64432019-10-22 13:58:29 +0900574 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700575 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900576 if len(noticeInputs) != 3 {
577 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900578 }
579 ensureListContains(t, noticeInputs, "NOTICE")
580 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900581 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900582
Artur Satayeva8bd1132020-04-27 18:07:06 +0100583 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100584 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100585 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
586 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
587 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100588
589 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100590 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100591 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
592 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
593 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800594}
595
Jooyung Hanf21c7972019-12-16 22:32:06 +0900596func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800597 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900598 apex_defaults {
599 name: "myapex-defaults",
600 key: "myapex.key",
601 prebuilts: ["myetc"],
602 native_shared_libs: ["mylib"],
603 java_libs: ["myjar"],
604 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900605 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800606 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000607 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900608 }
609
610 prebuilt_etc {
611 name: "myetc",
612 src: "myprebuilt",
613 }
614
615 apex {
616 name: "myapex",
617 defaults: ["myapex-defaults"],
618 }
619
620 apex_key {
621 name: "myapex.key",
622 public_key: "testkey.avbpubkey",
623 private_key: "testkey.pem",
624 }
625
626 cc_library {
627 name: "mylib",
628 system_shared_libs: [],
629 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000630 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900631 }
632
633 java_library {
634 name: "myjar",
635 srcs: ["foo/bar/MyClass.java"],
636 sdk_version: "none",
637 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000638 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900639 }
640
641 android_app {
642 name: "AppFoo",
643 srcs: ["foo/bar/MyClass.java"],
644 sdk_version: "none",
645 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000646 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900647 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900648
649 runtime_resource_overlay {
650 name: "rro",
651 theme: "blue",
652 }
653
markchien2f59ec92020-09-02 16:23:38 +0800654 bpf {
655 name: "bpf",
656 srcs: ["bpf.c", "bpf2.c"],
657 }
658
Jooyung Hanf21c7972019-12-16 22:32:06 +0900659 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000660 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900661 "etc/myetc",
662 "javalib/myjar.jar",
663 "lib64/mylib.so",
664 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900665 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800666 "etc/bpf/bpf.o",
667 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900668 })
669}
670
Jooyung Han01a3ee22019-11-02 02:52:25 +0900671func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800672 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900673 apex {
674 name: "myapex",
675 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000676 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900677 }
678
679 apex_key {
680 name: "myapex.key",
681 public_key: "testkey.avbpubkey",
682 private_key: "testkey.pem",
683 }
684 `)
685
686 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900687 args := module.Rule("apexRule").Args
688 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
689 t.Error("manifest should be apex_manifest.pb, but " + manifest)
690 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900691}
692
Alex Light5098a612018-11-29 17:12:15 -0800693func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800694 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800695 apex {
696 name: "myapex",
697 key: "myapex.key",
698 payload_type: "zip",
699 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000700 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800701 }
702
703 apex_key {
704 name: "myapex.key",
705 public_key: "testkey.avbpubkey",
706 private_key: "testkey.pem",
707 }
708
709 cc_library {
710 name: "mylib",
711 srcs: ["mylib.cpp"],
712 shared_libs: ["mylib2"],
713 system_shared_libs: [],
714 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000715 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800716 }
717
718 cc_library {
719 name: "mylib2",
720 srcs: ["mylib.cpp"],
721 system_shared_libs: [],
722 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000723 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800724 }
725 `)
726
Sundong Ahnabb64432019-10-22 13:58:29 +0900727 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800728 copyCmds := zipApexRule.Args["copy_commands"]
729
730 // Ensure that main rule creates an output
731 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
732
733 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700734 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800735
736 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700737 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800738
739 // Ensure that both direct and indirect deps are copied into apex
740 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
741 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900742}
743
744func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800745 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900746 apex {
747 name: "myapex",
748 key: "myapex.key",
749 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000750 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900751 }
752
753 apex_key {
754 name: "myapex.key",
755 public_key: "testkey.avbpubkey",
756 private_key: "testkey.pem",
757 }
758
759 cc_library {
760 name: "mylib",
761 srcs: ["mylib.cpp"],
762 shared_libs: ["mylib2", "mylib3"],
763 system_shared_libs: [],
764 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000765 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900766 }
767
768 cc_library {
769 name: "mylib2",
770 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900771 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900772 system_shared_libs: [],
773 stl: "none",
774 stubs: {
775 versions: ["1", "2", "3"],
776 },
777 }
778
779 cc_library {
780 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900781 srcs: ["mylib.cpp"],
782 shared_libs: ["mylib4"],
783 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900784 stl: "none",
785 stubs: {
786 versions: ["10", "11", "12"],
787 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000788 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900789 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900790
791 cc_library {
792 name: "mylib4",
793 srcs: ["mylib.cpp"],
794 system_shared_libs: [],
795 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000796 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900797 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900798 `)
799
Sundong Ahnabb64432019-10-22 13:58:29 +0900800 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900801 copyCmds := apexRule.Args["copy_commands"]
802
803 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800804 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900805
806 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800807 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900808
809 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800810 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900811
Colin Crossaede88c2020-08-11 12:17:01 -0700812 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900813
814 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900815 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900816 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900817 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900818
819 // 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 -0700820 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900821 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700822 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900823
824 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900825 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900826 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900827
828 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700829 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900830
Jooyung Hana57af4a2020-01-23 05:36:59 +0000831 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900832 "lib64/mylib.so",
833 "lib64/mylib3.so",
834 "lib64/mylib4.so",
835 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900836}
837
Colin Cross7812fd32020-09-25 12:35:10 -0700838func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
839 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800840 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700841 apex {
842 name: "myapex",
843 key: "myapex.key",
844 native_shared_libs: ["mylib", "mylib3"],
845 min_sdk_version: "29",
846 }
847
848 apex_key {
849 name: "myapex.key",
850 public_key: "testkey.avbpubkey",
851 private_key: "testkey.pem",
852 }
853
854 cc_library {
855 name: "mylib",
856 srcs: ["mylib.cpp"],
857 shared_libs: ["mylib2", "mylib3"],
858 system_shared_libs: [],
859 stl: "none",
860 apex_available: [ "myapex" ],
861 min_sdk_version: "28",
862 }
863
864 cc_library {
865 name: "mylib2",
866 srcs: ["mylib.cpp"],
867 cflags: ["-include mylib.h"],
868 system_shared_libs: [],
869 stl: "none",
870 stubs: {
871 versions: ["28", "29", "30", "current"],
872 },
873 min_sdk_version: "28",
874 }
875
876 cc_library {
877 name: "mylib3",
878 srcs: ["mylib.cpp"],
879 shared_libs: ["mylib4"],
880 system_shared_libs: [],
881 stl: "none",
882 stubs: {
883 versions: ["28", "29", "30", "current"],
884 },
885 apex_available: [ "myapex" ],
886 min_sdk_version: "28",
887 }
888
889 cc_library {
890 name: "mylib4",
891 srcs: ["mylib.cpp"],
892 system_shared_libs: [],
893 stl: "none",
894 apex_available: [ "myapex" ],
895 min_sdk_version: "28",
896 }
897 `)
898
899 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
900 copyCmds := apexRule.Args["copy_commands"]
901
902 // Ensure that direct non-stubs dep is always included
903 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
904
905 // Ensure that indirect stubs dep is not included
906 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
907
908 // Ensure that direct stubs dep is included
909 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
910
911 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
912
Jiyong Park55549df2021-02-26 23:57:23 +0900913 // Ensure that mylib is linking with the latest version of stub for mylib2
914 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700915 // ... and not linking to the non-stub (impl) variant of mylib2
916 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
917
918 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
919 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
920 // .. and not linking to the stubs variant of mylib3
921 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
922
923 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700924 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700925 ensureNotContains(t, mylib2Cflags, "-include ")
926
927 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700928 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700929
930 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
931 "lib64/mylib.so",
932 "lib64/mylib3.so",
933 "lib64/mylib4.so",
934 })
935}
936
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900937func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
938 t.Parallel()
939 // myapex (Z)
940 // mylib -----------------.
941 // |
942 // otherapex (29) |
943 // libstub's versions: 29 Z current
944 // |
945 // <platform> |
946 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800947 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900948 apex {
949 name: "myapex",
950 key: "myapex.key",
951 native_shared_libs: ["mylib"],
952 min_sdk_version: "Z", // non-final
953 }
954
955 cc_library {
956 name: "mylib",
957 srcs: ["mylib.cpp"],
958 shared_libs: ["libstub"],
959 apex_available: ["myapex"],
960 min_sdk_version: "Z",
961 }
962
963 apex_key {
964 name: "myapex.key",
965 public_key: "testkey.avbpubkey",
966 private_key: "testkey.pem",
967 }
968
969 apex {
970 name: "otherapex",
971 key: "myapex.key",
972 native_shared_libs: ["libstub"],
973 min_sdk_version: "29",
974 }
975
976 cc_library {
977 name: "libstub",
978 srcs: ["mylib.cpp"],
979 stubs: {
980 versions: ["29", "Z", "current"],
981 },
982 apex_available: ["otherapex"],
983 min_sdk_version: "29",
984 }
985
986 // platform module depending on libstub from otherapex should use the latest stub("current")
987 cc_library {
988 name: "libplatform",
989 srcs: ["mylib.cpp"],
990 shared_libs: ["libstub"],
991 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +0000992 `,
993 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
994 variables.Platform_sdk_codename = proptools.StringPtr("Z")
995 variables.Platform_sdk_final = proptools.BoolPtr(false)
996 variables.Platform_version_active_codenames = []string{"Z"}
997 }),
998 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900999
Jiyong Park55549df2021-02-26 23:57:23 +09001000 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001001 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001002 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001003 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001004 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001005
1006 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1007 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1008 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1009 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1010 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1011}
1012
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001013func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001014 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001015 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001016 name: "myapex2",
1017 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001018 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001019 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001020 }
1021
1022 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001023 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001024 public_key: "testkey.avbpubkey",
1025 private_key: "testkey.pem",
1026 }
1027
1028 cc_library {
1029 name: "mylib",
1030 srcs: ["mylib.cpp"],
1031 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001032 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001033 system_shared_libs: [],
1034 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001035 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001036 }
1037
1038 cc_library {
1039 name: "libfoo",
1040 srcs: ["mylib.cpp"],
1041 shared_libs: ["libbar"],
1042 system_shared_libs: [],
1043 stl: "none",
1044 stubs: {
1045 versions: ["10", "20", "30"],
1046 },
1047 }
1048
1049 cc_library {
1050 name: "libbar",
1051 srcs: ["mylib.cpp"],
1052 system_shared_libs: [],
1053 stl: "none",
1054 }
1055
Jiyong Park678c8812020-02-07 17:25:49 +09001056 cc_library_static {
1057 name: "libbaz",
1058 srcs: ["mylib.cpp"],
1059 system_shared_libs: [],
1060 stl: "none",
1061 apex_available: [ "myapex2" ],
1062 }
1063
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001064 `)
1065
Jiyong Park83dc74b2020-01-14 18:38:44 +09001066 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001067 copyCmds := apexRule.Args["copy_commands"]
1068
1069 // Ensure that direct non-stubs dep is always included
1070 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1071
1072 // Ensure that indirect stubs dep is not included
1073 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1074
1075 // Ensure that dependency of stubs is not included
1076 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1077
Colin Crossaede88c2020-08-11 12:17:01 -07001078 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001079
1080 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001081 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001082 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001083 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001084
Jiyong Park3ff16992019-12-27 14:11:47 +09001085 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001086
1087 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1088 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001089
Artur Satayeva8bd1132020-04-27 18:07:06 +01001090 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001091 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001092
Artur Satayeva8bd1132020-04-27 18:07:06 +01001093 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001094 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001095}
1096
Jooyung Hand3639552019-08-09 12:57:43 +09001097func TestApexWithRuntimeLibsDependency(t *testing.T) {
1098 /*
1099 myapex
1100 |
1101 v (runtime_libs)
1102 mylib ------+------> libfoo [provides stub]
1103 |
1104 `------> libbar
1105 */
Colin Cross1c460562021-02-16 17:55:47 -08001106 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001107 apex {
1108 name: "myapex",
1109 key: "myapex.key",
1110 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001111 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001112 }
1113
1114 apex_key {
1115 name: "myapex.key",
1116 public_key: "testkey.avbpubkey",
1117 private_key: "testkey.pem",
1118 }
1119
1120 cc_library {
1121 name: "mylib",
1122 srcs: ["mylib.cpp"],
1123 runtime_libs: ["libfoo", "libbar"],
1124 system_shared_libs: [],
1125 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001126 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001127 }
1128
1129 cc_library {
1130 name: "libfoo",
1131 srcs: ["mylib.cpp"],
1132 system_shared_libs: [],
1133 stl: "none",
1134 stubs: {
1135 versions: ["10", "20", "30"],
1136 },
1137 }
1138
1139 cc_library {
1140 name: "libbar",
1141 srcs: ["mylib.cpp"],
1142 system_shared_libs: [],
1143 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001144 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001145 }
1146
1147 `)
1148
Sundong Ahnabb64432019-10-22 13:58:29 +09001149 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001150 copyCmds := apexRule.Args["copy_commands"]
1151
1152 // Ensure that direct non-stubs dep is always included
1153 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1154
1155 // Ensure that indirect stubs dep is not included
1156 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1157
1158 // Ensure that runtime_libs dep in included
1159 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1160
Sundong Ahnabb64432019-10-22 13:58:29 +09001161 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001162 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1163 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001164
1165}
1166
Paul Duffina02cae32021-03-09 01:44:06 +00001167var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1168 cc.PrepareForTestWithCcBuildComponents,
1169 PrepareForTestWithApexBuildComponents,
1170 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001171 apex {
1172 name: "com.android.runtime",
1173 key: "com.android.runtime.key",
1174 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001175 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001176 }
1177
1178 apex_key {
1179 name: "com.android.runtime.key",
1180 public_key: "testkey.avbpubkey",
1181 private_key: "testkey.pem",
1182 }
Paul Duffina02cae32021-03-09 01:44:06 +00001183 `),
1184 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1185)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001186
Paul Duffina02cae32021-03-09 01:44:06 +00001187func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001188 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001189 cc_library {
1190 name: "libc",
1191 no_libcrt: true,
1192 nocrt: true,
1193 stl: "none",
1194 system_shared_libs: [],
1195 stubs: { versions: ["1"] },
1196 apex_available: ["com.android.runtime"],
1197
1198 sanitize: {
1199 hwaddress: true,
1200 }
1201 }
1202
1203 cc_prebuilt_library_shared {
1204 name: "libclang_rt.hwasan-aarch64-android",
1205 no_libcrt: true,
1206 nocrt: true,
1207 stl: "none",
1208 system_shared_libs: [],
1209 srcs: [""],
1210 stubs: { versions: ["1"] },
1211
1212 sanitize: {
1213 never: true,
1214 },
Paul Duffina02cae32021-03-09 01:44:06 +00001215 } `)
1216 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001217
1218 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1219 "lib64/bionic/libc.so",
1220 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1221 })
1222
1223 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1224
1225 installed := hwasan.Description("install libclang_rt.hwasan")
1226 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1227
1228 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1229 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1230 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1231}
1232
1233func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001234 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001235 prepareForTestOfRuntimeApexWithHwasan,
1236 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1237 variables.SanitizeDevice = []string{"hwaddress"}
1238 }),
1239 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001240 cc_library {
1241 name: "libc",
1242 no_libcrt: true,
1243 nocrt: true,
1244 stl: "none",
1245 system_shared_libs: [],
1246 stubs: { versions: ["1"] },
1247 apex_available: ["com.android.runtime"],
1248 }
1249
1250 cc_prebuilt_library_shared {
1251 name: "libclang_rt.hwasan-aarch64-android",
1252 no_libcrt: true,
1253 nocrt: true,
1254 stl: "none",
1255 system_shared_libs: [],
1256 srcs: [""],
1257 stubs: { versions: ["1"] },
1258
1259 sanitize: {
1260 never: true,
1261 },
1262 }
Paul Duffina02cae32021-03-09 01:44:06 +00001263 `)
1264 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001265
1266 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1267 "lib64/bionic/libc.so",
1268 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1269 })
1270
1271 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1272
1273 installed := hwasan.Description("install libclang_rt.hwasan")
1274 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1275
1276 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1277 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1278 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1279}
1280
Jooyung Han61b66e92020-03-21 14:21:46 +00001281func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1282 testcases := []struct {
1283 name string
1284 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001285 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001286 shouldLink string
1287 shouldNotLink []string
1288 }{
1289 {
Jiyong Park55549df2021-02-26 23:57:23 +09001290 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001291 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001292 apexVariant: "apex10000",
Jooyung Han61b66e92020-03-21 14:21:46 +00001293 shouldLink: "30",
1294 shouldNotLink: []string{"29"},
1295 },
1296 {
Jiyong Park55549df2021-02-26 23:57:23 +09001297 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001298 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001299 apexVariant: "apex29",
Jiyong Park55549df2021-02-26 23:57:23 +09001300 shouldLink: "30",
1301 shouldNotLink: []string{"29"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001302 },
1303 }
1304 for _, tc := range testcases {
1305 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001306 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001307 apex {
1308 name: "myapex",
1309 key: "myapex.key",
1310 use_vendor: true,
1311 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001312 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001313 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001314 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001315
Jooyung Han61b66e92020-03-21 14:21:46 +00001316 apex_key {
1317 name: "myapex.key",
1318 public_key: "testkey.avbpubkey",
1319 private_key: "testkey.pem",
1320 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001321
Jooyung Han61b66e92020-03-21 14:21:46 +00001322 cc_library {
1323 name: "mylib",
1324 srcs: ["mylib.cpp"],
1325 vendor_available: true,
1326 shared_libs: ["libbar"],
1327 system_shared_libs: [],
1328 stl: "none",
1329 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001330 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001331 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001332
Jooyung Han61b66e92020-03-21 14:21:46 +00001333 cc_library {
1334 name: "libbar",
1335 srcs: ["mylib.cpp"],
1336 system_shared_libs: [],
1337 stl: "none",
1338 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001339 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001340 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001341
Jooyung Han61b66e92020-03-21 14:21:46 +00001342 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001343 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001344 symbol_file: "",
1345 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001346 `,
1347 setUseVendorAllowListForTest([]string{"myapex"}),
1348 withUnbundledBuild,
1349 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001350
Jooyung Han61b66e92020-03-21 14:21:46 +00001351 // Ensure that LLNDK dep is not included
1352 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1353 "lib64/mylib.so",
1354 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001355
Jooyung Han61b66e92020-03-21 14:21:46 +00001356 // Ensure that LLNDK dep is required
1357 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1358 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1359 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001360
Colin Crossaede88c2020-08-11 12:17:01 -07001361 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
Colin Cross127bb8b2020-12-16 16:46:01 -08001362 ensureContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001363 for _, ver := range tc.shouldNotLink {
Colin Cross127bb8b2020-12-16 16:46:01 -08001364 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001365 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001366
Colin Crossaede88c2020-08-11 12:17:01 -07001367 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jooyung Han61b66e92020-03-21 14:21:46 +00001368 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1369 })
1370 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001371}
1372
Jiyong Park25fc6a92018-11-18 18:02:45 +09001373func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001374 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001375 apex {
1376 name: "myapex",
1377 key: "myapex.key",
1378 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001379 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001380 }
1381
1382 apex_key {
1383 name: "myapex.key",
1384 public_key: "testkey.avbpubkey",
1385 private_key: "testkey.pem",
1386 }
1387
1388 cc_library {
1389 name: "mylib",
1390 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001391 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001392 shared_libs: ["libdl#27"],
1393 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001394 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001395 }
1396
1397 cc_library_shared {
1398 name: "mylib_shared",
1399 srcs: ["mylib.cpp"],
1400 shared_libs: ["libdl#27"],
1401 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001402 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001403 }
1404
1405 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001406 name: "libBootstrap",
1407 srcs: ["mylib.cpp"],
1408 stl: "none",
1409 bootstrap: true,
1410 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001411 `)
1412
Sundong Ahnabb64432019-10-22 13:58:29 +09001413 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001414 copyCmds := apexRule.Args["copy_commands"]
1415
1416 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001417 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001418 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1419 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001420
1421 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001422 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001423
Colin Crossaede88c2020-08-11 12:17:01 -07001424 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1425 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1426 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001427
1428 // For dependency to libc
1429 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001430 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001431 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001432 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001433 // ... Cflags from stub is correctly exported to mylib
1434 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1435 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1436
1437 // For dependency to libm
1438 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001439 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001440 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001441 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001442 // ... and is not compiling with the stub
1443 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1444 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1445
1446 // For dependency to libdl
1447 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001448 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001449 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001450 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1451 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001452 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001453 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001454 // ... Cflags from stub is correctly exported to mylib
1455 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1456 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001457
1458 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001459 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1460 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1461 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1462 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001463}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001464
Jooyung Han749dc692020-04-15 11:03:39 +09001465func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001466 // there are three links between liba --> libz.
1467 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001468 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001469 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001470 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001471 apex {
1472 name: "myapex",
1473 key: "myapex.key",
1474 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001475 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001476 }
1477
1478 apex {
1479 name: "otherapex",
1480 key: "myapex.key",
1481 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001482 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001483 }
1484
1485 apex_key {
1486 name: "myapex.key",
1487 public_key: "testkey.avbpubkey",
1488 private_key: "testkey.pem",
1489 }
1490
1491 cc_library {
1492 name: "libx",
1493 shared_libs: ["liba"],
1494 system_shared_libs: [],
1495 stl: "none",
1496 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001497 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001498 }
1499
1500 cc_library {
1501 name: "liby",
1502 shared_libs: ["liba"],
1503 system_shared_libs: [],
1504 stl: "none",
1505 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001506 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001507 }
1508
1509 cc_library {
1510 name: "liba",
1511 shared_libs: ["libz"],
1512 system_shared_libs: [],
1513 stl: "none",
1514 apex_available: [
1515 "//apex_available:anyapex",
1516 "//apex_available:platform",
1517 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001518 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001519 }
1520
1521 cc_library {
1522 name: "libz",
1523 system_shared_libs: [],
1524 stl: "none",
1525 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001526 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001527 },
1528 }
Jooyung Han749dc692020-04-15 11:03:39 +09001529 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001530
1531 expectLink := func(from, from_variant, to, to_variant string) {
1532 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1533 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1534 }
1535 expectNoLink := func(from, from_variant, to, to_variant string) {
1536 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1537 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1538 }
1539 // platform liba is linked to non-stub version
1540 expectLink("liba", "shared", "libz", "shared")
Jiyong Park55549df2021-02-26 23:57:23 +09001541 // liba in myapex is linked to #30
1542 expectLink("liba", "shared_apex29", "libz", "shared_30")
1543 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001544 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001545 // liba in otherapex is linked to #30
Colin Crossaede88c2020-08-11 12:17:01 -07001546 expectLink("liba", "shared_apex30", "libz", "shared_30")
1547 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1548 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001549}
1550
Jooyung Hanaed150d2020-04-02 01:41:41 +09001551func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001552 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001553 apex {
1554 name: "myapex",
1555 key: "myapex.key",
1556 native_shared_libs: ["libx"],
1557 min_sdk_version: "R",
1558 }
1559
1560 apex_key {
1561 name: "myapex.key",
1562 public_key: "testkey.avbpubkey",
1563 private_key: "testkey.pem",
1564 }
1565
1566 cc_library {
1567 name: "libx",
1568 shared_libs: ["libz"],
1569 system_shared_libs: [],
1570 stl: "none",
1571 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001572 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001573 }
1574
1575 cc_library {
1576 name: "libz",
1577 system_shared_libs: [],
1578 stl: "none",
1579 stubs: {
1580 versions: ["29", "R"],
1581 },
1582 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001583 `,
1584 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1585 variables.Platform_version_active_codenames = []string{"R"}
1586 }),
1587 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001588
1589 expectLink := func(from, from_variant, to, to_variant string) {
1590 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1591 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1592 }
1593 expectNoLink := func(from, from_variant, to, to_variant string) {
1594 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1595 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1596 }
Dan Albertc8060532020-07-22 22:32:17 -07001597 expectLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001598 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1599 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001600}
1601
Jooyung Han749dc692020-04-15 11:03:39 +09001602func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001603 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001604 apex {
1605 name: "myapex",
1606 key: "myapex.key",
1607 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001608 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001609 }
1610
1611 apex_key {
1612 name: "myapex.key",
1613 public_key: "testkey.avbpubkey",
1614 private_key: "testkey.pem",
1615 }
1616
1617 cc_library {
1618 name: "libx",
1619 shared_libs: ["libz"],
1620 system_shared_libs: [],
1621 stl: "none",
1622 apex_available: [ "myapex" ],
1623 }
1624
1625 cc_library {
1626 name: "libz",
1627 system_shared_libs: [],
1628 stl: "none",
1629 stubs: {
1630 versions: ["1", "2"],
1631 },
1632 }
1633 `)
1634
1635 expectLink := func(from, from_variant, to, to_variant string) {
1636 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1637 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1638 }
1639 expectNoLink := func(from, from_variant, to, to_variant string) {
1640 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1641 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1642 }
Colin Crossaede88c2020-08-11 12:17:01 -07001643 expectLink("libx", "shared_apex10000", "libz", "shared_2")
1644 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
1645 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001646}
1647
1648func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001649 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001650 apex {
1651 name: "myapex",
1652 key: "myapex.key",
1653 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001654 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001655 }
1656
1657 apex_key {
1658 name: "myapex.key",
1659 public_key: "testkey.avbpubkey",
1660 private_key: "testkey.pem",
1661 }
1662
1663 cc_library {
1664 name: "libx",
1665 system_shared_libs: [],
1666 stl: "none",
1667 apex_available: [ "myapex" ],
1668 stubs: {
1669 versions: ["1", "2"],
1670 },
1671 }
1672
1673 cc_library {
1674 name: "libz",
1675 shared_libs: ["libx"],
1676 system_shared_libs: [],
1677 stl: "none",
1678 }
1679 `)
1680
1681 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001682 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001683 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1684 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1685 }
1686 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001687 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001688 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1689 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1690 }
1691 expectLink("libz", "shared", "libx", "shared_2")
1692 expectNoLink("libz", "shared", "libz", "shared_1")
1693 expectNoLink("libz", "shared", "libz", "shared")
1694}
1695
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001696var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1697 func(variables android.FixtureProductVariables) {
1698 variables.SanitizeDevice = []string{"hwaddress"}
1699 },
1700)
1701
Jooyung Han75568392020-03-20 04:29:24 +09001702func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001703 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001704 apex {
1705 name: "myapex",
1706 key: "myapex.key",
1707 native_shared_libs: ["libx"],
1708 min_sdk_version: "29",
1709 }
1710
1711 apex_key {
1712 name: "myapex.key",
1713 public_key: "testkey.avbpubkey",
1714 private_key: "testkey.pem",
1715 }
1716
1717 cc_library {
1718 name: "libx",
1719 shared_libs: ["libbar"],
1720 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001721 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001722 }
1723
1724 cc_library {
1725 name: "libbar",
1726 stubs: {
1727 versions: ["29", "30"],
1728 },
1729 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001730 `,
1731 prepareForTestWithSantitizeHwaddress,
1732 )
Jooyung Han03b51852020-02-26 22:45:42 +09001733 expectLink := func(from, from_variant, to, to_variant string) {
1734 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1735 libFlags := ld.Args["libFlags"]
1736 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1737 }
Colin Crossaede88c2020-08-11 12:17:01 -07001738 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001739}
1740
Jooyung Han75568392020-03-20 04:29:24 +09001741func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001742 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001743 apex {
1744 name: "myapex",
1745 key: "myapex.key",
1746 native_shared_libs: ["libx"],
1747 min_sdk_version: "29",
1748 }
1749
1750 apex_key {
1751 name: "myapex.key",
1752 public_key: "testkey.avbpubkey",
1753 private_key: "testkey.pem",
1754 }
1755
1756 cc_library {
1757 name: "libx",
1758 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001759 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001760 }
Jooyung Han75568392020-03-20 04:29:24 +09001761 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001762
1763 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001764 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001765 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001766 // note that platform variant is not.
1767 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001768 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001769}
1770
Jooyung Han749dc692020-04-15 11:03:39 +09001771func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1772 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001773 apex {
1774 name: "myapex",
1775 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001776 native_shared_libs: ["mylib"],
1777 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001778 }
1779
1780 apex_key {
1781 name: "myapex.key",
1782 public_key: "testkey.avbpubkey",
1783 private_key: "testkey.pem",
1784 }
Jooyung Han749dc692020-04-15 11:03:39 +09001785
1786 cc_library {
1787 name: "mylib",
1788 srcs: ["mylib.cpp"],
1789 system_shared_libs: [],
1790 stl: "none",
1791 apex_available: [
1792 "myapex",
1793 ],
1794 min_sdk_version: "30",
1795 }
1796 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001797
1798 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1799 apex {
1800 name: "myapex",
1801 key: "myapex.key",
1802 native_shared_libs: ["libfoo.ffi"],
1803 min_sdk_version: "29",
1804 }
1805
1806 apex_key {
1807 name: "myapex.key",
1808 public_key: "testkey.avbpubkey",
1809 private_key: "testkey.pem",
1810 }
1811
1812 rust_ffi_shared {
1813 name: "libfoo.ffi",
1814 srcs: ["foo.rs"],
1815 crate_name: "foo",
1816 apex_available: [
1817 "myapex",
1818 ],
1819 min_sdk_version: "30",
1820 }
1821 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001822}
1823
1824func TestApexMinSdkVersion_Okay(t *testing.T) {
1825 testApex(t, `
1826 apex {
1827 name: "myapex",
1828 key: "myapex.key",
1829 native_shared_libs: ["libfoo"],
1830 java_libs: ["libbar"],
1831 min_sdk_version: "29",
1832 }
1833
1834 apex_key {
1835 name: "myapex.key",
1836 public_key: "testkey.avbpubkey",
1837 private_key: "testkey.pem",
1838 }
1839
1840 cc_library {
1841 name: "libfoo",
1842 srcs: ["mylib.cpp"],
1843 shared_libs: ["libfoo_dep"],
1844 apex_available: ["myapex"],
1845 min_sdk_version: "29",
1846 }
1847
1848 cc_library {
1849 name: "libfoo_dep",
1850 srcs: ["mylib.cpp"],
1851 apex_available: ["myapex"],
1852 min_sdk_version: "29",
1853 }
1854
1855 java_library {
1856 name: "libbar",
1857 sdk_version: "current",
1858 srcs: ["a.java"],
1859 static_libs: ["libbar_dep"],
1860 apex_available: ["myapex"],
1861 min_sdk_version: "29",
1862 }
1863
1864 java_library {
1865 name: "libbar_dep",
1866 sdk_version: "current",
1867 srcs: ["a.java"],
1868 apex_available: ["myapex"],
1869 min_sdk_version: "29",
1870 }
Jooyung Han03b51852020-02-26 22:45:42 +09001871 `)
1872}
1873
Artur Satayev8cf899a2020-04-15 17:29:42 +01001874func TestJavaStableSdkVersion(t *testing.T) {
1875 testCases := []struct {
1876 name string
1877 expectedError string
1878 bp string
1879 }{
1880 {
1881 name: "Non-updatable apex with non-stable dep",
1882 bp: `
1883 apex {
1884 name: "myapex",
1885 java_libs: ["myjar"],
1886 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001887 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001888 }
1889 apex_key {
1890 name: "myapex.key",
1891 public_key: "testkey.avbpubkey",
1892 private_key: "testkey.pem",
1893 }
1894 java_library {
1895 name: "myjar",
1896 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001897 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001898 apex_available: ["myapex"],
1899 }
1900 `,
1901 },
1902 {
1903 name: "Updatable apex with stable dep",
1904 bp: `
1905 apex {
1906 name: "myapex",
1907 java_libs: ["myjar"],
1908 key: "myapex.key",
1909 updatable: true,
1910 min_sdk_version: "29",
1911 }
1912 apex_key {
1913 name: "myapex.key",
1914 public_key: "testkey.avbpubkey",
1915 private_key: "testkey.pem",
1916 }
1917 java_library {
1918 name: "myjar",
1919 srcs: ["foo/bar/MyClass.java"],
1920 sdk_version: "current",
1921 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001922 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001923 }
1924 `,
1925 },
1926 {
1927 name: "Updatable apex with non-stable dep",
1928 expectedError: "cannot depend on \"myjar\"",
1929 bp: `
1930 apex {
1931 name: "myapex",
1932 java_libs: ["myjar"],
1933 key: "myapex.key",
1934 updatable: true,
1935 }
1936 apex_key {
1937 name: "myapex.key",
1938 public_key: "testkey.avbpubkey",
1939 private_key: "testkey.pem",
1940 }
1941 java_library {
1942 name: "myjar",
1943 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001944 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001945 apex_available: ["myapex"],
1946 }
1947 `,
1948 },
1949 {
Paul Duffin043f5e72021-03-05 00:00:01 +00001950 name: "Updatable apex with non-stable transitive dep",
1951 // This is not actually detecting that the transitive dependency is unstable, rather it is
1952 // detecting that the transitive dependency is building against a wider API surface than the
1953 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09001954 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001955 bp: `
1956 apex {
1957 name: "myapex",
1958 java_libs: ["myjar"],
1959 key: "myapex.key",
1960 updatable: true,
1961 }
1962 apex_key {
1963 name: "myapex.key",
1964 public_key: "testkey.avbpubkey",
1965 private_key: "testkey.pem",
1966 }
1967 java_library {
1968 name: "myjar",
1969 srcs: ["foo/bar/MyClass.java"],
1970 sdk_version: "current",
1971 apex_available: ["myapex"],
1972 static_libs: ["transitive-jar"],
1973 }
1974 java_library {
1975 name: "transitive-jar",
1976 srcs: ["foo/bar/MyClass.java"],
1977 sdk_version: "core_platform",
1978 apex_available: ["myapex"],
1979 }
1980 `,
1981 },
1982 }
1983
1984 for _, test := range testCases {
1985 t.Run(test.name, func(t *testing.T) {
1986 if test.expectedError == "" {
1987 testApex(t, test.bp)
1988 } else {
1989 testApexError(t, test.expectedError, test.bp)
1990 }
1991 })
1992 }
1993}
1994
Jooyung Han749dc692020-04-15 11:03:39 +09001995func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
1996 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
1997 apex {
1998 name: "myapex",
1999 key: "myapex.key",
2000 native_shared_libs: ["mylib"],
2001 min_sdk_version: "29",
2002 }
2003
2004 apex_key {
2005 name: "myapex.key",
2006 public_key: "testkey.avbpubkey",
2007 private_key: "testkey.pem",
2008 }
2009
2010 cc_library {
2011 name: "mylib",
2012 srcs: ["mylib.cpp"],
2013 shared_libs: ["mylib2"],
2014 system_shared_libs: [],
2015 stl: "none",
2016 apex_available: [
2017 "myapex",
2018 ],
2019 min_sdk_version: "29",
2020 }
2021
2022 // indirect part of the apex
2023 cc_library {
2024 name: "mylib2",
2025 srcs: ["mylib.cpp"],
2026 system_shared_libs: [],
2027 stl: "none",
2028 apex_available: [
2029 "myapex",
2030 ],
2031 min_sdk_version: "30",
2032 }
2033 `)
2034}
2035
2036func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2037 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2038 apex {
2039 name: "myapex",
2040 key: "myapex.key",
2041 apps: ["AppFoo"],
2042 min_sdk_version: "29",
2043 }
2044
2045 apex_key {
2046 name: "myapex.key",
2047 public_key: "testkey.avbpubkey",
2048 private_key: "testkey.pem",
2049 }
2050
2051 android_app {
2052 name: "AppFoo",
2053 srcs: ["foo/bar/MyClass.java"],
2054 sdk_version: "current",
2055 min_sdk_version: "29",
2056 system_modules: "none",
2057 stl: "none",
2058 static_libs: ["bar"],
2059 apex_available: [ "myapex" ],
2060 }
2061
2062 java_library {
2063 name: "bar",
2064 sdk_version: "current",
2065 srcs: ["a.java"],
2066 apex_available: [ "myapex" ],
2067 }
2068 `)
2069}
2070
2071func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002072 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002073 apex {
2074 name: "myapex",
2075 key: "myapex.key",
2076 native_shared_libs: ["mylib"],
2077 min_sdk_version: "29",
2078 }
2079
2080 apex_key {
2081 name: "myapex.key",
2082 public_key: "testkey.avbpubkey",
2083 private_key: "testkey.pem",
2084 }
2085
Jiyong Park55549df2021-02-26 23:57:23 +09002086 // mylib in myapex will link to mylib2#30
Jooyung Han749dc692020-04-15 11:03:39 +09002087 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2088 cc_library {
2089 name: "mylib",
2090 srcs: ["mylib.cpp"],
2091 shared_libs: ["mylib2"],
2092 system_shared_libs: [],
2093 stl: "none",
2094 apex_available: ["myapex", "otherapex"],
2095 min_sdk_version: "29",
2096 }
2097
2098 cc_library {
2099 name: "mylib2",
2100 srcs: ["mylib.cpp"],
2101 system_shared_libs: [],
2102 stl: "none",
2103 apex_available: ["otherapex"],
2104 stubs: { versions: ["29", "30"] },
2105 min_sdk_version: "30",
2106 }
2107
2108 apex {
2109 name: "otherapex",
2110 key: "myapex.key",
2111 native_shared_libs: ["mylib", "mylib2"],
2112 min_sdk_version: "30",
2113 }
2114 `)
2115 expectLink := func(from, from_variant, to, to_variant string) {
2116 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2117 libFlags := ld.Args["libFlags"]
2118 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2119 }
Jiyong Park55549df2021-02-26 23:57:23 +09002120 expectLink("mylib", "shared_apex29", "mylib2", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07002121 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002122}
2123
Jooyung Haned124c32021-01-26 11:43:46 +09002124func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002125 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2126 func(variables android.FixtureProductVariables) {
2127 variables.Platform_sdk_codename = proptools.StringPtr("S")
2128 variables.Platform_version_active_codenames = []string{"S"}
2129 },
2130 )
Jooyung Haned124c32021-01-26 11:43:46 +09002131 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2132 apex {
2133 name: "myapex",
2134 key: "myapex.key",
2135 native_shared_libs: ["libfoo"],
2136 min_sdk_version: "S",
2137 }
2138 apex_key {
2139 name: "myapex.key",
2140 public_key: "testkey.avbpubkey",
2141 private_key: "testkey.pem",
2142 }
2143 cc_library {
2144 name: "libfoo",
2145 shared_libs: ["libbar"],
2146 apex_available: ["myapex"],
2147 min_sdk_version: "29",
2148 }
2149 cc_library {
2150 name: "libbar",
2151 apex_available: ["myapex"],
2152 }
2153 `, withSAsActiveCodeNames)
2154}
2155
2156func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002157 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2158 variables.Platform_sdk_codename = proptools.StringPtr("S")
2159 variables.Platform_version_active_codenames = []string{"S", "T"}
2160 })
Colin Cross1c460562021-02-16 17:55:47 -08002161 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002162 apex {
2163 name: "myapex",
2164 key: "myapex.key",
2165 native_shared_libs: ["libfoo"],
2166 min_sdk_version: "S",
2167 }
2168 apex_key {
2169 name: "myapex.key",
2170 public_key: "testkey.avbpubkey",
2171 private_key: "testkey.pem",
2172 }
2173 cc_library {
2174 name: "libfoo",
2175 shared_libs: ["libbar"],
2176 apex_available: ["myapex"],
2177 min_sdk_version: "S",
2178 }
2179 cc_library {
2180 name: "libbar",
2181 stubs: {
2182 symbol_file: "libbar.map.txt",
2183 versions: ["30", "S", "T"],
2184 },
2185 }
2186 `, withSAsActiveCodeNames)
2187
2188 // ensure libfoo is linked with "S" version of libbar stub
2189 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2190 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09002191 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_T/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002192}
2193
Jiyong Park7c2ee712018-12-07 00:42:25 +09002194func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002195 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002196 apex {
2197 name: "myapex",
2198 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002199 native_shared_libs: ["mylib"],
2200 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002201 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002202 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002203 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002204 }
2205
2206 apex_key {
2207 name: "myapex.key",
2208 public_key: "testkey.avbpubkey",
2209 private_key: "testkey.pem",
2210 }
2211
2212 prebuilt_etc {
2213 name: "myetc",
2214 src: "myprebuilt",
2215 sub_dir: "foo/bar",
2216 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002217
2218 cc_library {
2219 name: "mylib",
2220 srcs: ["mylib.cpp"],
2221 relative_install_path: "foo/bar",
2222 system_shared_libs: [],
2223 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002224 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002225 }
2226
2227 cc_binary {
2228 name: "mybin",
2229 srcs: ["mylib.cpp"],
2230 relative_install_path: "foo/bar",
2231 system_shared_libs: [],
2232 static_executable: true,
2233 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002234 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002235 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002236 `)
2237
Sundong Ahnabb64432019-10-22 13:58:29 +09002238 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002239 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2240
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002241 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002242 ensureListContains(t, dirs, "etc")
2243 ensureListContains(t, dirs, "etc/foo")
2244 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002245 ensureListContains(t, dirs, "lib64")
2246 ensureListContains(t, dirs, "lib64/foo")
2247 ensureListContains(t, dirs, "lib64/foo/bar")
2248 ensureListContains(t, dirs, "lib")
2249 ensureListContains(t, dirs, "lib/foo")
2250 ensureListContains(t, dirs, "lib/foo/bar")
2251
Jiyong Parkbd13e442019-03-15 18:10:35 +09002252 ensureListContains(t, dirs, "bin")
2253 ensureListContains(t, dirs, "bin/foo")
2254 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002255}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002256
Jooyung Han35155c42020-02-06 17:33:20 +09002257func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002258 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002259 apex {
2260 name: "myapex",
2261 key: "myapex.key",
2262 multilib: {
2263 both: {
2264 native_shared_libs: ["mylib"],
2265 binaries: ["mybin"],
2266 },
2267 },
2268 compile_multilib: "both",
2269 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002270 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002271 }
2272
2273 apex_key {
2274 name: "myapex.key",
2275 public_key: "testkey.avbpubkey",
2276 private_key: "testkey.pem",
2277 }
2278
2279 cc_library {
2280 name: "mylib",
2281 relative_install_path: "foo/bar",
2282 system_shared_libs: [],
2283 stl: "none",
2284 apex_available: [ "myapex" ],
2285 native_bridge_supported: true,
2286 }
2287
2288 cc_binary {
2289 name: "mybin",
2290 relative_install_path: "foo/bar",
2291 system_shared_libs: [],
2292 static_executable: true,
2293 stl: "none",
2294 apex_available: [ "myapex" ],
2295 native_bridge_supported: true,
2296 compile_multilib: "both", // default is "first" for binary
2297 multilib: {
2298 lib64: {
2299 suffix: "64",
2300 },
2301 },
2302 }
2303 `, withNativeBridgeEnabled)
2304 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2305 "bin/foo/bar/mybin",
2306 "bin/foo/bar/mybin64",
2307 "bin/arm/foo/bar/mybin",
2308 "bin/arm64/foo/bar/mybin64",
2309 "lib/foo/bar/mylib.so",
2310 "lib/arm/foo/bar/mylib.so",
2311 "lib64/foo/bar/mylib.so",
2312 "lib64/arm64/foo/bar/mylib.so",
2313 })
2314}
2315
Jiyong Parkda6eb592018-12-19 17:12:36 +09002316func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002317 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002318 apex {
2319 name: "myapex",
2320 key: "myapex.key",
2321 native_shared_libs: ["mylib"],
2322 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002323 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002324 }
2325
2326 apex_key {
2327 name: "myapex.key",
2328 public_key: "testkey.avbpubkey",
2329 private_key: "testkey.pem",
2330 }
2331
2332 cc_library {
2333 name: "mylib",
2334 srcs: ["mylib.cpp"],
2335 shared_libs: ["mylib2"],
2336 system_shared_libs: [],
2337 vendor_available: true,
2338 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002339 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002340 }
2341
2342 cc_library {
2343 name: "mylib2",
2344 srcs: ["mylib.cpp"],
2345 system_shared_libs: [],
2346 vendor_available: true,
2347 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002348 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002349 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002350 `,
2351 setUseVendorAllowListForTest([]string{"myapex"}),
2352 )
Jiyong Parkda6eb592018-12-19 17:12:36 +09002353
2354 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002355 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002356 for _, implicit := range i.Implicits {
2357 inputsList = append(inputsList, implicit.String())
2358 }
2359 }
2360 inputsString := strings.Join(inputsList, " ")
2361
2362 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossaede88c2020-08-11 12:17:01 -07002363 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib.so")
2364 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002365
2366 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002367 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2368 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002369}
Jiyong Park16e91a02018-12-20 18:18:08 +09002370
Jooyung Han85d61762020-06-24 23:50:26 +09002371func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002372 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2373 apex {
2374 name: "myapex",
2375 key: "myapex.key",
2376 use_vendor: true,
2377 }
2378 apex_key {
2379 name: "myapex.key",
2380 public_key: "testkey.avbpubkey",
2381 private_key: "testkey.pem",
2382 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002383 `,
2384 setUseVendorAllowListForTest([]string{""}),
2385 )
Colin Cross440e0d02020-06-11 11:32:11 -07002386 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002387 testApex(t, `
2388 apex {
2389 name: "myapex",
2390 key: "myapex.key",
2391 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002392 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002393 }
2394 apex_key {
2395 name: "myapex.key",
2396 public_key: "testkey.avbpubkey",
2397 private_key: "testkey.pem",
2398 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002399 `,
2400 setUseVendorAllowListForTest([]string{"myapex"}),
2401 )
Jooyung Handc782442019-11-01 03:14:38 +09002402}
2403
Jooyung Han5c998b92019-06-27 11:30:33 +09002404func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2405 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2406 apex {
2407 name: "myapex",
2408 key: "myapex.key",
2409 native_shared_libs: ["mylib"],
2410 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002411 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002412 }
2413
2414 apex_key {
2415 name: "myapex.key",
2416 public_key: "testkey.avbpubkey",
2417 private_key: "testkey.pem",
2418 }
2419
2420 cc_library {
2421 name: "mylib",
2422 srcs: ["mylib.cpp"],
2423 system_shared_libs: [],
2424 stl: "none",
2425 }
2426 `)
2427}
2428
Jooyung Han85d61762020-06-24 23:50:26 +09002429func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002430 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002431 apex {
2432 name: "myapex",
2433 key: "myapex.key",
2434 binaries: ["mybin"],
2435 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002436 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002437 }
2438 apex_key {
2439 name: "myapex.key",
2440 public_key: "testkey.avbpubkey",
2441 private_key: "testkey.pem",
2442 }
2443 cc_binary {
2444 name: "mybin",
2445 vendor: true,
2446 shared_libs: ["libfoo"],
2447 }
2448 cc_library {
2449 name: "libfoo",
2450 proprietary: true,
2451 }
2452 `)
2453
2454 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2455 "bin/mybin",
2456 "lib64/libfoo.so",
2457 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2458 "lib64/libc++.so",
2459 })
2460
2461 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002462 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002463 name := apexBundle.BaseModuleName()
2464 prefix := "TARGET_"
2465 var builder strings.Builder
2466 data.Custom(&builder, name, prefix, "", data)
2467 androidMk := builder.String()
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002468 installPath := path.Join(buildDir, "../target/product/test_device/vendor/apex")
2469 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002470
2471 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2472 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2473 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002474}
2475
Jooyung Handf78e212020-07-22 15:54:47 +09002476func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002477 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002478 apex {
2479 name: "myapex",
2480 key: "myapex.key",
2481 binaries: ["mybin"],
2482 vendor: true,
2483 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002484 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002485 }
2486 apex_key {
2487 name: "myapex.key",
2488 public_key: "testkey.avbpubkey",
2489 private_key: "testkey.pem",
2490 }
2491 cc_binary {
2492 name: "mybin",
2493 vendor: true,
2494 shared_libs: ["libvndk", "libvendor"],
2495 }
2496 cc_library {
2497 name: "libvndk",
2498 vndk: {
2499 enabled: true,
2500 },
2501 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002502 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002503 }
2504 cc_library {
2505 name: "libvendor",
2506 vendor: true,
2507 }
2508 `)
2509
2510 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2511
Colin Crossaede88c2020-08-11 12:17:01 -07002512 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002513 libs := names(ldRule.Args["libFlags"])
2514 // VNDK libs(libvndk/libc++) as they are
2515 ensureListContains(t, libs, buildDir+"/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
Paul Duffine05480a2021-03-08 15:07:14 +00002516 ensureListContains(t, libs, buildDir+"/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002517 // non-stable Vendor libs as APEX variants
Colin Crossaede88c2020-08-11 12:17:01 -07002518 ensureListContains(t, libs, buildDir+"/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002519
2520 // VNDK libs are not included when use_vndk_as_stable: true
2521 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2522 "bin/mybin",
2523 "lib64/libvendor.so",
2524 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002525
2526 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2527 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2528 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002529}
2530
Justin Yun13decfb2021-03-08 19:25:55 +09002531func TestProductVariant(t *testing.T) {
2532 ctx := testApex(t, `
2533 apex {
2534 name: "myapex",
2535 key: "myapex.key",
2536 updatable: false,
2537 product_specific: true,
2538 binaries: ["foo"],
2539 }
2540
2541 apex_key {
2542 name: "myapex.key",
2543 public_key: "testkey.avbpubkey",
2544 private_key: "testkey.pem",
2545 }
2546
2547 cc_binary {
2548 name: "foo",
2549 product_available: true,
2550 apex_available: ["myapex"],
2551 srcs: ["foo.cpp"],
2552 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002553 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2554 variables.ProductVndkVersion = proptools.StringPtr("current")
2555 }),
2556 )
Justin Yun13decfb2021-03-08 19:25:55 +09002557
2558 cflags := strings.Fields(
2559 ctx.ModuleForTests("foo", "android_product.VER_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
2560 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2561 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2562 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2563 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2564}
2565
Jooyung Han8e5685d2020-09-21 11:02:57 +09002566func TestApex_withPrebuiltFirmware(t *testing.T) {
2567 testCases := []struct {
2568 name string
2569 additionalProp string
2570 }{
2571 {"system apex with prebuilt_firmware", ""},
2572 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2573 }
2574 for _, tc := range testCases {
2575 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002576 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002577 apex {
2578 name: "myapex",
2579 key: "myapex.key",
2580 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002581 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002582 `+tc.additionalProp+`
2583 }
2584 apex_key {
2585 name: "myapex.key",
2586 public_key: "testkey.avbpubkey",
2587 private_key: "testkey.pem",
2588 }
2589 prebuilt_firmware {
2590 name: "myfirmware",
2591 src: "myfirmware.bin",
2592 filename_from_src: true,
2593 `+tc.additionalProp+`
2594 }
2595 `)
2596 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2597 "etc/firmware/myfirmware.bin",
2598 })
2599 })
2600 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002601}
2602
Jooyung Hanefb184e2020-06-25 17:14:25 +09002603func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002604 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002605 apex {
2606 name: "myapex",
2607 key: "myapex.key",
2608 use_vendor: true,
2609 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002610 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002611 }
2612
2613 apex_key {
2614 name: "myapex.key",
2615 public_key: "testkey.avbpubkey",
2616 private_key: "testkey.pem",
2617 }
2618
2619 cc_library {
2620 name: "mylib",
2621 vendor_available: true,
2622 apex_available: ["myapex"],
2623 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002624 `,
2625 setUseVendorAllowListForTest([]string{"myapex"}),
2626 )
Jooyung Hanefb184e2020-06-25 17:14:25 +09002627
2628 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002629 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002630 name := apexBundle.BaseModuleName()
2631 prefix := "TARGET_"
2632 var builder strings.Builder
2633 data.Custom(&builder, name, prefix, "", data)
2634 androidMk := builder.String()
2635 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2636}
2637
2638func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002639 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002640 apex {
2641 name: "myapex",
2642 key: "myapex.key",
2643 vendor: true,
2644 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002645 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002646 }
2647
2648 apex_key {
2649 name: "myapex.key",
2650 public_key: "testkey.avbpubkey",
2651 private_key: "testkey.pem",
2652 }
2653
2654 cc_library {
2655 name: "mylib",
2656 vendor_available: true,
2657 }
2658 `)
2659
2660 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002661 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002662 name := apexBundle.BaseModuleName()
2663 prefix := "TARGET_"
2664 var builder strings.Builder
2665 data.Custom(&builder, name, prefix, "", data)
2666 androidMk := builder.String()
2667 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2668}
2669
Jooyung Han2ed99d02020-06-24 23:26:26 +09002670func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002671 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002672 apex {
2673 name: "myapex",
2674 key: "myapex.key",
2675 vintf_fragments: ["fragment.xml"],
2676 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002677 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002678 }
2679 apex_key {
2680 name: "myapex.key",
2681 public_key: "testkey.avbpubkey",
2682 private_key: "testkey.pem",
2683 }
2684 cc_binary {
2685 name: "mybin",
2686 }
2687 `)
2688
2689 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002690 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002691 name := apexBundle.BaseModuleName()
2692 prefix := "TARGET_"
2693 var builder strings.Builder
2694 data.Custom(&builder, name, prefix, "", data)
2695 androidMk := builder.String()
2696 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2697 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2698}
2699
Jiyong Park16e91a02018-12-20 18:18:08 +09002700func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002701 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002702 apex {
2703 name: "myapex",
2704 key: "myapex.key",
2705 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002706 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002707 }
2708
2709 apex_key {
2710 name: "myapex.key",
2711 public_key: "testkey.avbpubkey",
2712 private_key: "testkey.pem",
2713 }
2714
2715 cc_library {
2716 name: "mylib",
2717 srcs: ["mylib.cpp"],
2718 system_shared_libs: [],
2719 stl: "none",
2720 stubs: {
2721 versions: ["1", "2", "3"],
2722 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002723 apex_available: [
2724 "//apex_available:platform",
2725 "myapex",
2726 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002727 }
2728
2729 cc_binary {
2730 name: "not_in_apex",
2731 srcs: ["mylib.cpp"],
2732 static_libs: ["mylib"],
2733 static_executable: true,
2734 system_shared_libs: [],
2735 stl: "none",
2736 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002737 `)
2738
Colin Cross7113d202019-11-20 16:39:12 -08002739 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002740
2741 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002742 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002743}
Jiyong Park9335a262018-12-24 11:31:58 +09002744
2745func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002746 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002747 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002748 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002749 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002750 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002751 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002752 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002753 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002754 }
2755
2756 cc_library {
2757 name: "mylib",
2758 srcs: ["mylib.cpp"],
2759 system_shared_libs: [],
2760 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002761 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002762 }
2763
2764 apex_key {
2765 name: "myapex.key",
2766 public_key: "testkey.avbpubkey",
2767 private_key: "testkey.pem",
2768 }
2769
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002770 android_app_certificate {
2771 name: "myapex.certificate",
2772 certificate: "testkey",
2773 }
2774
2775 android_app_certificate {
2776 name: "myapex.certificate.override",
2777 certificate: "testkey.override",
2778 }
2779
Jiyong Park9335a262018-12-24 11:31:58 +09002780 `)
2781
2782 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002783 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002784
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002785 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2786 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002787 "vendor/foo/devkeys/testkey.avbpubkey")
2788 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002789 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2790 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002791 "vendor/foo/devkeys/testkey.pem")
2792 }
2793
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002794 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002795 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002796 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002797 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002798 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002799 }
2800}
Jiyong Park58e364a2019-01-19 19:24:06 +09002801
Jooyung Hanf121a652019-12-17 14:30:11 +09002802func TestCertificate(t *testing.T) {
2803 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002804 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002805 apex {
2806 name: "myapex",
2807 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002808 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002809 }
2810 apex_key {
2811 name: "myapex.key",
2812 public_key: "testkey.avbpubkey",
2813 private_key: "testkey.pem",
2814 }`)
2815 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2816 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2817 if actual := rule.Args["certificates"]; actual != expected {
2818 t.Errorf("certificates should be %q, not %q", expected, actual)
2819 }
2820 })
2821 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002822 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002823 apex {
2824 name: "myapex_keytest",
2825 key: "myapex.key",
2826 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002827 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002828 }
2829 apex_key {
2830 name: "myapex.key",
2831 public_key: "testkey.avbpubkey",
2832 private_key: "testkey.pem",
2833 }
2834 android_app_certificate {
2835 name: "myapex.certificate.override",
2836 certificate: "testkey.override",
2837 }`)
2838 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2839 expected := "testkey.override.x509.pem testkey.override.pk8"
2840 if actual := rule.Args["certificates"]; actual != expected {
2841 t.Errorf("certificates should be %q, not %q", expected, actual)
2842 }
2843 })
2844 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002845 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002846 apex {
2847 name: "myapex",
2848 key: "myapex.key",
2849 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002850 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002851 }
2852 apex_key {
2853 name: "myapex.key",
2854 public_key: "testkey.avbpubkey",
2855 private_key: "testkey.pem",
2856 }
2857 android_app_certificate {
2858 name: "myapex.certificate",
2859 certificate: "testkey",
2860 }`)
2861 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2862 expected := "testkey.x509.pem testkey.pk8"
2863 if actual := rule.Args["certificates"]; actual != expected {
2864 t.Errorf("certificates should be %q, not %q", expected, actual)
2865 }
2866 })
2867 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002868 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002869 apex {
2870 name: "myapex_keytest",
2871 key: "myapex.key",
2872 file_contexts: ":myapex-file_contexts",
2873 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002874 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002875 }
2876 apex_key {
2877 name: "myapex.key",
2878 public_key: "testkey.avbpubkey",
2879 private_key: "testkey.pem",
2880 }
2881 android_app_certificate {
2882 name: "myapex.certificate.override",
2883 certificate: "testkey.override",
2884 }`)
2885 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2886 expected := "testkey.override.x509.pem testkey.override.pk8"
2887 if actual := rule.Args["certificates"]; actual != expected {
2888 t.Errorf("certificates should be %q, not %q", expected, actual)
2889 }
2890 })
2891 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002892 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002893 apex {
2894 name: "myapex",
2895 key: "myapex.key",
2896 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002897 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002898 }
2899 apex_key {
2900 name: "myapex.key",
2901 public_key: "testkey.avbpubkey",
2902 private_key: "testkey.pem",
2903 }`)
2904 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2905 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2906 if actual := rule.Args["certificates"]; actual != expected {
2907 t.Errorf("certificates should be %q, not %q", expected, actual)
2908 }
2909 })
2910 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002911 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002912 apex {
2913 name: "myapex_keytest",
2914 key: "myapex.key",
2915 file_contexts: ":myapex-file_contexts",
2916 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002917 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002918 }
2919 apex_key {
2920 name: "myapex.key",
2921 public_key: "testkey.avbpubkey",
2922 private_key: "testkey.pem",
2923 }
2924 android_app_certificate {
2925 name: "myapex.certificate.override",
2926 certificate: "testkey.override",
2927 }`)
2928 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2929 expected := "testkey.override.x509.pem testkey.override.pk8"
2930 if actual := rule.Args["certificates"]; actual != expected {
2931 t.Errorf("certificates should be %q, not %q", expected, actual)
2932 }
2933 })
2934}
2935
Jiyong Park58e364a2019-01-19 19:24:06 +09002936func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002937 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002938 apex {
2939 name: "myapex",
2940 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002941 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002942 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002943 }
2944
2945 apex {
2946 name: "otherapex",
2947 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002948 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002949 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002950 }
2951
2952 apex_key {
2953 name: "myapex.key",
2954 public_key: "testkey.avbpubkey",
2955 private_key: "testkey.pem",
2956 }
2957
2958 cc_library {
2959 name: "mylib",
2960 srcs: ["mylib.cpp"],
2961 system_shared_libs: [],
2962 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002963 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002964 "myapex",
2965 "otherapex",
2966 ],
Jooyung Han24282772020-03-21 23:20:55 +09002967 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002968 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002969 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002970 cc_library {
2971 name: "mylib2",
2972 srcs: ["mylib.cpp"],
2973 system_shared_libs: [],
2974 stl: "none",
2975 apex_available: [
2976 "myapex",
2977 "otherapex",
2978 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002979 static_libs: ["mylib3"],
2980 recovery_available: true,
2981 min_sdk_version: "29",
2982 }
2983 cc_library {
2984 name: "mylib3",
2985 srcs: ["mylib.cpp"],
2986 system_shared_libs: [],
2987 stl: "none",
2988 apex_available: [
2989 "myapex",
2990 "otherapex",
2991 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09002992 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07002993 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002994 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002995 }
Jiyong Park58e364a2019-01-19 19:24:06 +09002996 `)
2997
Jooyung Hanc87a0592020-03-02 17:44:33 +09002998 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08002999 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003000 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003001 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003002
Jooyung Hanccce2f22020-03-07 03:45:53 +09003003 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003004 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003005 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003006 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003007 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003008
Jooyung Hanccce2f22020-03-07 03:45:53 +09003009 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003010 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003011 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003012 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003013 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003014
Colin Crossaede88c2020-08-11 12:17:01 -07003015 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3016 // each variant defines additional macros to distinguish which apex variant it is built for
3017
3018 // non-APEX variant does not have __ANDROID_APEX__ defined
3019 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3020 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3021
3022 // APEX variant has __ANDROID_APEX__ defined
3023 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3024 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3025 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3026 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3027
3028 // APEX variant has __ANDROID_APEX__ defined
3029 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3030 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3031 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3032 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3033
Dan Albertb19953d2020-11-17 15:29:36 -08003034 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003035 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3036 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003037 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003038
3039 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3040 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003041
3042 // non-APEX variant does not have __ANDROID_APEX__ defined
3043 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3044 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3045
3046 // APEX variant has __ANDROID_APEX__ defined
3047 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003048 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003049 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003050 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003051
Jooyung Hanc87a0592020-03-02 17:44:33 +09003052 // APEX variant has __ANDROID_APEX__ defined
3053 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003054 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003055 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003056 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003057
Dan Albertb19953d2020-11-17 15:29:36 -08003058 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003059 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003060 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003061 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003062}
Jiyong Park7e636d02019-01-28 16:16:54 +09003063
3064func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003065 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003066 apex {
3067 name: "myapex",
3068 key: "myapex.key",
3069 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003070 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003071 }
3072
3073 apex_key {
3074 name: "myapex.key",
3075 public_key: "testkey.avbpubkey",
3076 private_key: "testkey.pem",
3077 }
3078
3079 cc_library_headers {
3080 name: "mylib_headers",
3081 export_include_dirs: ["my_include"],
3082 system_shared_libs: [],
3083 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003084 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003085 }
3086
3087 cc_library {
3088 name: "mylib",
3089 srcs: ["mylib.cpp"],
3090 system_shared_libs: [],
3091 stl: "none",
3092 header_libs: ["mylib_headers"],
3093 export_header_lib_headers: ["mylib_headers"],
3094 stubs: {
3095 versions: ["1", "2", "3"],
3096 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003097 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003098 }
3099
3100 cc_library {
3101 name: "otherlib",
3102 srcs: ["mylib.cpp"],
3103 system_shared_libs: [],
3104 stl: "none",
3105 shared_libs: ["mylib"],
3106 }
3107 `)
3108
Colin Cross7113d202019-11-20 16:39:12 -08003109 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003110
3111 // Ensure that the include path of the header lib is exported to 'otherlib'
3112 ensureContains(t, cFlags, "-Imy_include")
3113}
Alex Light9670d332019-01-29 18:07:33 -08003114
Jiyong Park7cd10e32020-01-14 09:22:18 +09003115type fileInApex struct {
3116 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003117 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003118 isLink bool
3119}
3120
Jooyung Hana57af4a2020-01-23 05:36:59 +00003121func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003122 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003123 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003124 copyCmds := apexRule.Args["copy_commands"]
3125 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003126 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003127 for _, cmd := range strings.Split(copyCmds, "&&") {
3128 cmd = strings.TrimSpace(cmd)
3129 if cmd == "" {
3130 continue
3131 }
3132 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003133 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003134 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003135 switch terms[0] {
3136 case "mkdir":
3137 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003138 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003139 t.Fatal("copyCmds contains invalid cp command", cmd)
3140 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003141 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003142 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003143 isLink = false
3144 case "ln":
3145 if len(terms) != 3 && len(terms) != 4 {
3146 // ln LINK TARGET or ln -s LINK TARGET
3147 t.Fatal("copyCmds contains invalid ln command", cmd)
3148 }
3149 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003150 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003151 isLink = true
3152 default:
3153 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3154 }
3155 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003156 index := strings.Index(dst, imageApexDir)
3157 if index == -1 {
3158 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3159 }
3160 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003161 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003162 }
3163 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003164 return ret
3165}
3166
Jooyung Hana57af4a2020-01-23 05:36:59 +00003167func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3168 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003169 var failed bool
3170 var surplus []string
3171 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003172 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003173 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003174 for _, expected := range files {
3175 if matched, _ := path.Match(expected, file.path); matched {
3176 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003177 mactchFound = true
3178 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003179 }
3180 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003181 if !mactchFound {
3182 surplus = append(surplus, file.path)
3183 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003184 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003185
Jooyung Han31c470b2019-10-18 16:26:59 +09003186 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003187 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003188 t.Log("surplus files", surplus)
3189 failed = true
3190 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003191
3192 if len(files) > len(filesMatched) {
3193 var missing []string
3194 for _, expected := range files {
3195 if !filesMatched[expected] {
3196 missing = append(missing, expected)
3197 }
3198 }
3199 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003200 t.Log("missing files", missing)
3201 failed = true
3202 }
3203 if failed {
3204 t.Fail()
3205 }
3206}
3207
Jooyung Han344d5432019-08-23 11:17:39 +09003208func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003209 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003210 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003211 name: "com.android.vndk.current",
3212 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003213 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003214 }
3215
3216 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003217 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003218 public_key: "testkey.avbpubkey",
3219 private_key: "testkey.pem",
3220 }
3221
3222 cc_library {
3223 name: "libvndk",
3224 srcs: ["mylib.cpp"],
3225 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003226 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003227 vndk: {
3228 enabled: true,
3229 },
3230 system_shared_libs: [],
3231 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003232 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003233 }
3234
3235 cc_library {
3236 name: "libvndksp",
3237 srcs: ["mylib.cpp"],
3238 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003239 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003240 vndk: {
3241 enabled: true,
3242 support_system_process: true,
3243 },
3244 system_shared_libs: [],
3245 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003246 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003247 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003248 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003249
Colin Cross2807f002021-03-02 10:15:29 -08003250 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003251 "lib/libvndk.so",
3252 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003253 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003254 "lib64/libvndk.so",
3255 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003256 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003257 "etc/llndk.libraries.VER.txt",
3258 "etc/vndkcore.libraries.VER.txt",
3259 "etc/vndksp.libraries.VER.txt",
3260 "etc/vndkprivate.libraries.VER.txt",
Justin Yun8a2600c2020-12-07 12:44:03 +09003261 "etc/vndkproduct.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003262 })
Jooyung Han344d5432019-08-23 11:17:39 +09003263}
3264
3265func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003266 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003267 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003268 name: "com.android.vndk.current",
3269 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003270 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003271 }
3272
3273 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003274 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003275 public_key: "testkey.avbpubkey",
3276 private_key: "testkey.pem",
3277 }
3278
3279 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003280 name: "libvndk",
3281 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003282 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003283 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003284 vndk: {
3285 enabled: true,
3286 },
3287 system_shared_libs: [],
3288 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003289 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003290 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003291
3292 cc_prebuilt_library_shared {
3293 name: "libvndk.arm",
3294 srcs: ["libvndk.arm.so"],
3295 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003296 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003297 vndk: {
3298 enabled: true,
3299 },
3300 enabled: false,
3301 arch: {
3302 arm: {
3303 enabled: true,
3304 },
3305 },
3306 system_shared_libs: [],
3307 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003308 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003309 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003310 `+vndkLibrariesTxtFiles("current"),
3311 withFiles(map[string][]byte{
3312 "libvndk.so": nil,
3313 "libvndk.arm.so": nil,
3314 }))
Colin Cross2807f002021-03-02 10:15:29 -08003315 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003316 "lib/libvndk.so",
3317 "lib/libvndk.arm.so",
3318 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003319 "lib/libc++.so",
3320 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003321 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003322 })
Jooyung Han344d5432019-08-23 11:17:39 +09003323}
3324
Jooyung Han39edb6c2019-11-06 16:53:07 +09003325func vndkLibrariesTxtFiles(vers ...string) (result string) {
3326 for _, v := range vers {
3327 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003328 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003329 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003330 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003331 name: "` + txt + `.libraries.txt",
3332 }
3333 `
3334 }
3335 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003336 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003337 result += `
3338 prebuilt_etc {
3339 name: "` + txt + `.libraries.` + v + `.txt",
3340 src: "dummy.txt",
3341 }
3342 `
3343 }
3344 }
3345 }
3346 return
3347}
3348
Jooyung Han344d5432019-08-23 11:17:39 +09003349func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003350 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003351 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003352 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003353 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003354 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003355 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003356 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003357 }
3358
3359 apex_key {
3360 name: "myapex.key",
3361 public_key: "testkey.avbpubkey",
3362 private_key: "testkey.pem",
3363 }
3364
Jooyung Han31c470b2019-10-18 16:26:59 +09003365 vndk_prebuilt_shared {
3366 name: "libvndk27",
3367 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003368 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003369 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003370 vndk: {
3371 enabled: true,
3372 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003373 target_arch: "arm64",
3374 arch: {
3375 arm: {
3376 srcs: ["libvndk27_arm.so"],
3377 },
3378 arm64: {
3379 srcs: ["libvndk27_arm64.so"],
3380 },
3381 },
Colin Cross2807f002021-03-02 10:15:29 -08003382 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003383 }
3384
3385 vndk_prebuilt_shared {
3386 name: "libvndk27",
3387 version: "27",
3388 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003389 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003390 vndk: {
3391 enabled: true,
3392 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003393 target_arch: "x86_64",
3394 arch: {
3395 x86: {
3396 srcs: ["libvndk27_x86.so"],
3397 },
3398 x86_64: {
3399 srcs: ["libvndk27_x86_64.so"],
3400 },
3401 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003402 }
3403 `+vndkLibrariesTxtFiles("27"),
3404 withFiles(map[string][]byte{
3405 "libvndk27_arm.so": nil,
3406 "libvndk27_arm64.so": nil,
3407 "libvndk27_x86.so": nil,
3408 "libvndk27_x86_64.so": nil,
3409 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003410
Colin Cross2807f002021-03-02 10:15:29 -08003411 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003412 "lib/libvndk27_arm.so",
3413 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003414 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003415 })
Jooyung Han344d5432019-08-23 11:17:39 +09003416}
3417
Jooyung Han90eee022019-10-01 20:02:42 +09003418func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003419 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003420 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003421 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003422 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003423 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003424 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003425 }
3426 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003427 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003428 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003429 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003430 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003431 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003432 }
3433 apex_key {
3434 name: "myapex.key",
3435 public_key: "testkey.avbpubkey",
3436 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003437 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003438
3439 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003440 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003441 actual := proptools.String(bundle.properties.Apex_name)
3442 if !reflect.DeepEqual(actual, expected) {
3443 t.Errorf("Got '%v', expected '%v'", actual, expected)
3444 }
3445 }
3446
Colin Cross2807f002021-03-02 10:15:29 -08003447 assertApexName("com.android.vndk.vVER", "com.android.vndk.current")
3448 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003449}
3450
Jooyung Han344d5432019-08-23 11:17:39 +09003451func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003452 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003453 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003454 name: "com.android.vndk.current",
3455 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003456 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003457 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003458 }
3459
3460 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003461 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003462 public_key: "testkey.avbpubkey",
3463 private_key: "testkey.pem",
3464 }
3465
3466 cc_library {
3467 name: "libvndk",
3468 srcs: ["mylib.cpp"],
3469 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003470 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003471 native_bridge_supported: true,
3472 host_supported: true,
3473 vndk: {
3474 enabled: true,
3475 },
3476 system_shared_libs: [],
3477 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003478 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003479 }
Colin Cross2807f002021-03-02 10:15:29 -08003480 `+vndkLibrariesTxtFiles("current"),
3481 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003482
Colin Cross2807f002021-03-02 10:15:29 -08003483 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003484 "lib/libvndk.so",
3485 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003486 "lib/libc++.so",
3487 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003488 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003489 })
Jooyung Han344d5432019-08-23 11:17:39 +09003490}
3491
3492func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003493 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003494 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003495 name: "com.android.vndk.current",
3496 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003497 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003498 native_bridge_supported: true,
3499 }
3500
3501 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003502 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003503 public_key: "testkey.avbpubkey",
3504 private_key: "testkey.pem",
3505 }
3506
3507 cc_library {
3508 name: "libvndk",
3509 srcs: ["mylib.cpp"],
3510 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003511 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003512 native_bridge_supported: true,
3513 host_supported: true,
3514 vndk: {
3515 enabled: true,
3516 },
3517 system_shared_libs: [],
3518 stl: "none",
3519 }
3520 `)
3521}
3522
Jooyung Han31c470b2019-10-18 16:26:59 +09003523func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003524 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003525 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003526 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003527 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003528 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003529 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003530 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003531 }
3532
3533 apex_key {
3534 name: "myapex.key",
3535 public_key: "testkey.avbpubkey",
3536 private_key: "testkey.pem",
3537 }
3538
3539 vndk_prebuilt_shared {
3540 name: "libvndk27",
3541 version: "27",
3542 target_arch: "arm",
3543 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003544 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003545 vndk: {
3546 enabled: true,
3547 },
3548 arch: {
3549 arm: {
3550 srcs: ["libvndk27.so"],
3551 }
3552 },
3553 }
3554
3555 vndk_prebuilt_shared {
3556 name: "libvndk27",
3557 version: "27",
3558 target_arch: "arm",
3559 binder32bit: true,
3560 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003561 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003562 vndk: {
3563 enabled: true,
3564 },
3565 arch: {
3566 arm: {
3567 srcs: ["libvndk27binder32.so"],
3568 }
3569 },
Colin Cross2807f002021-03-02 10:15:29 -08003570 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003571 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003572 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003573 withFiles(map[string][]byte{
3574 "libvndk27.so": nil,
3575 "libvndk27binder32.so": nil,
3576 }),
3577 withBinder32bit,
3578 withTargets(map[android.OsType][]android.Target{
3579 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003580 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3581 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003582 },
3583 }),
3584 )
3585
Colin Cross2807f002021-03-02 10:15:29 -08003586 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003587 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003588 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003589 })
3590}
3591
Jooyung Han45a96772020-06-15 14:59:42 +09003592func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003593 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003594 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003595 name: "com.android.vndk.current",
3596 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003597 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003598 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003599 }
3600
3601 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003602 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003603 public_key: "testkey.avbpubkey",
3604 private_key: "testkey.pem",
3605 }
3606
3607 cc_library {
3608 name: "libz",
3609 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003610 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003611 vndk: {
3612 enabled: true,
3613 },
3614 stubs: {
3615 symbol_file: "libz.map.txt",
3616 versions: ["30"],
3617 }
3618 }
3619 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3620 "libz.map.txt": nil,
3621 }))
3622
Colin Cross2807f002021-03-02 10:15:29 -08003623 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003624 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3625 ensureListEmpty(t, provideNativeLibs)
3626}
3627
Jooyung Hane1633032019-08-01 17:41:43 +09003628func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003629 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003630 apex {
3631 name: "myapex_nodep",
3632 key: "myapex.key",
3633 native_shared_libs: ["lib_nodep"],
3634 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003635 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003636 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003637 }
3638
3639 apex {
3640 name: "myapex_dep",
3641 key: "myapex.key",
3642 native_shared_libs: ["lib_dep"],
3643 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003644 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003645 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003646 }
3647
3648 apex {
3649 name: "myapex_provider",
3650 key: "myapex.key",
3651 native_shared_libs: ["libfoo"],
3652 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003653 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003654 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003655 }
3656
3657 apex {
3658 name: "myapex_selfcontained",
3659 key: "myapex.key",
3660 native_shared_libs: ["lib_dep", "libfoo"],
3661 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003662 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003663 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003664 }
3665
3666 apex_key {
3667 name: "myapex.key",
3668 public_key: "testkey.avbpubkey",
3669 private_key: "testkey.pem",
3670 }
3671
3672 cc_library {
3673 name: "lib_nodep",
3674 srcs: ["mylib.cpp"],
3675 system_shared_libs: [],
3676 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003677 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003678 }
3679
3680 cc_library {
3681 name: "lib_dep",
3682 srcs: ["mylib.cpp"],
3683 shared_libs: ["libfoo"],
3684 system_shared_libs: [],
3685 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003686 apex_available: [
3687 "myapex_dep",
3688 "myapex_provider",
3689 "myapex_selfcontained",
3690 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003691 }
3692
3693 cc_library {
3694 name: "libfoo",
3695 srcs: ["mytest.cpp"],
3696 stubs: {
3697 versions: ["1"],
3698 },
3699 system_shared_libs: [],
3700 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003701 apex_available: [
3702 "myapex_provider",
3703 "myapex_selfcontained",
3704 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003705 }
3706 `)
3707
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003708 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003709 var provideNativeLibs, requireNativeLibs []string
3710
Sundong Ahnabb64432019-10-22 13:58:29 +09003711 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003712 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3713 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003714 ensureListEmpty(t, provideNativeLibs)
3715 ensureListEmpty(t, requireNativeLibs)
3716
Sundong Ahnabb64432019-10-22 13:58:29 +09003717 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003718 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3719 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003720 ensureListEmpty(t, provideNativeLibs)
3721 ensureListContains(t, requireNativeLibs, "libfoo.so")
3722
Sundong Ahnabb64432019-10-22 13:58:29 +09003723 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003724 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3725 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003726 ensureListContains(t, provideNativeLibs, "libfoo.so")
3727 ensureListEmpty(t, requireNativeLibs)
3728
Sundong Ahnabb64432019-10-22 13:58:29 +09003729 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003730 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3731 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003732 ensureListContains(t, provideNativeLibs, "libfoo.so")
3733 ensureListEmpty(t, requireNativeLibs)
3734}
3735
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003736func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003737 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003738 apex {
3739 name: "myapex",
3740 key: "myapex.key",
3741 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003742 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003743 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003744 }
3745
3746 apex_key {
3747 name: "myapex.key",
3748 public_key: "testkey.avbpubkey",
3749 private_key: "testkey.pem",
3750 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003751
3752 cc_library {
3753 name: "mylib",
3754 srcs: ["mylib.cpp"],
3755 system_shared_libs: [],
3756 stl: "none",
3757 apex_available: [
3758 "//apex_available:platform",
3759 "myapex",
3760 ],
3761 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003762 `)
3763
Sundong Ahnabb64432019-10-22 13:58:29 +09003764 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003765 apexManifestRule := module.Rule("apexManifestRule")
3766 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3767 apexRule := module.Rule("apexRule")
3768 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003769
3770 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003771 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003772 name := apexBundle.BaseModuleName()
3773 prefix := "TARGET_"
3774 var builder strings.Builder
3775 data.Custom(&builder, name, prefix, "", data)
3776 androidMk := builder.String()
3777 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3778 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003779}
3780
Alex Light0851b882019-02-07 13:20:53 -08003781func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003782 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003783 apex {
3784 name: "myapex",
3785 key: "myapex.key",
3786 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003787 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003788 }
3789
3790 apex_key {
3791 name: "myapex.key",
3792 public_key: "testkey.avbpubkey",
3793 private_key: "testkey.pem",
3794 }
3795
3796 cc_library {
3797 name: "mylib_common",
3798 srcs: ["mylib.cpp"],
3799 system_shared_libs: [],
3800 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003801 apex_available: [
3802 "//apex_available:platform",
3803 "myapex",
3804 ],
Alex Light0851b882019-02-07 13:20:53 -08003805 }
3806 `)
3807
Sundong Ahnabb64432019-10-22 13:58:29 +09003808 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003809 apexRule := module.Rule("apexRule")
3810 copyCmds := apexRule.Args["copy_commands"]
3811
3812 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3813 t.Log("Apex was a test apex!")
3814 t.Fail()
3815 }
3816 // Ensure that main rule creates an output
3817 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3818
3819 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003820 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003821
3822 // Ensure that both direct and indirect deps are copied into apex
3823 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3824
Colin Cross7113d202019-11-20 16:39:12 -08003825 // Ensure that the platform variant ends with _shared
3826 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003827
Colin Cross56a83212020-09-15 18:30:11 -07003828 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003829 t.Log("Found mylib_common not in any apex!")
3830 t.Fail()
3831 }
3832}
3833
3834func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003835 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003836 apex_test {
3837 name: "myapex",
3838 key: "myapex.key",
3839 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003840 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003841 }
3842
3843 apex_key {
3844 name: "myapex.key",
3845 public_key: "testkey.avbpubkey",
3846 private_key: "testkey.pem",
3847 }
3848
3849 cc_library {
3850 name: "mylib_common_test",
3851 srcs: ["mylib.cpp"],
3852 system_shared_libs: [],
3853 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003854 // TODO: remove //apex_available:platform
3855 apex_available: [
3856 "//apex_available:platform",
3857 "myapex",
3858 ],
Alex Light0851b882019-02-07 13:20:53 -08003859 }
3860 `)
3861
Sundong Ahnabb64432019-10-22 13:58:29 +09003862 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003863 apexRule := module.Rule("apexRule")
3864 copyCmds := apexRule.Args["copy_commands"]
3865
3866 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3867 t.Log("Apex was not a test apex!")
3868 t.Fail()
3869 }
3870 // Ensure that main rule creates an output
3871 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3872
3873 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003874 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003875
3876 // Ensure that both direct and indirect deps are copied into apex
3877 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3878
Colin Cross7113d202019-11-20 16:39:12 -08003879 // Ensure that the platform variant ends with _shared
3880 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003881}
3882
Alex Light9670d332019-01-29 18:07:33 -08003883func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003884 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003885 apex {
3886 name: "myapex",
3887 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003888 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003889 multilib: {
3890 first: {
3891 native_shared_libs: ["mylib_common"],
3892 }
3893 },
3894 target: {
3895 android: {
3896 multilib: {
3897 first: {
3898 native_shared_libs: ["mylib"],
3899 }
3900 }
3901 },
3902 host: {
3903 multilib: {
3904 first: {
3905 native_shared_libs: ["mylib2"],
3906 }
3907 }
3908 }
3909 }
3910 }
3911
3912 apex_key {
3913 name: "myapex.key",
3914 public_key: "testkey.avbpubkey",
3915 private_key: "testkey.pem",
3916 }
3917
3918 cc_library {
3919 name: "mylib",
3920 srcs: ["mylib.cpp"],
3921 system_shared_libs: [],
3922 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003923 // TODO: remove //apex_available:platform
3924 apex_available: [
3925 "//apex_available:platform",
3926 "myapex",
3927 ],
Alex Light9670d332019-01-29 18:07:33 -08003928 }
3929
3930 cc_library {
3931 name: "mylib_common",
3932 srcs: ["mylib.cpp"],
3933 system_shared_libs: [],
3934 stl: "none",
3935 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003936 // TODO: remove //apex_available:platform
3937 apex_available: [
3938 "//apex_available:platform",
3939 "myapex",
3940 ],
Alex Light9670d332019-01-29 18:07:33 -08003941 }
3942
3943 cc_library {
3944 name: "mylib2",
3945 srcs: ["mylib.cpp"],
3946 system_shared_libs: [],
3947 stl: "none",
3948 compile_multilib: "first",
3949 }
3950 `)
3951
Sundong Ahnabb64432019-10-22 13:58:29 +09003952 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003953 copyCmds := apexRule.Args["copy_commands"]
3954
3955 // Ensure that main rule creates an output
3956 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3957
3958 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003959 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3960 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3961 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003962
3963 // Ensure that both direct and indirect deps are copied into apex
3964 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3965 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3966 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3967
Colin Cross7113d202019-11-20 16:39:12 -08003968 // Ensure that the platform variant ends with _shared
3969 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3970 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3971 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003972}
Jiyong Park04480cf2019-02-06 00:16:29 +09003973
Jiyong Park59140302020-12-14 18:44:04 +09003974func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003975 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09003976 apex {
3977 name: "myapex",
3978 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003979 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09003980 arch: {
3981 arm64: {
3982 native_shared_libs: ["mylib.arm64"],
3983 },
3984 x86_64: {
3985 native_shared_libs: ["mylib.x64"],
3986 },
3987 }
3988 }
3989
3990 apex_key {
3991 name: "myapex.key",
3992 public_key: "testkey.avbpubkey",
3993 private_key: "testkey.pem",
3994 }
3995
3996 cc_library {
3997 name: "mylib.arm64",
3998 srcs: ["mylib.cpp"],
3999 system_shared_libs: [],
4000 stl: "none",
4001 // TODO: remove //apex_available:platform
4002 apex_available: [
4003 "//apex_available:platform",
4004 "myapex",
4005 ],
4006 }
4007
4008 cc_library {
4009 name: "mylib.x64",
4010 srcs: ["mylib.cpp"],
4011 system_shared_libs: [],
4012 stl: "none",
4013 // TODO: remove //apex_available:platform
4014 apex_available: [
4015 "//apex_available:platform",
4016 "myapex",
4017 ],
4018 }
4019 `)
4020
4021 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4022 copyCmds := apexRule.Args["copy_commands"]
4023
4024 // Ensure that apex variant is created for the direct dep
4025 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4026 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4027
4028 // Ensure that both direct and indirect deps are copied into apex
4029 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4030 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4031}
4032
Jiyong Park04480cf2019-02-06 00:16:29 +09004033func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004034 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004035 apex {
4036 name: "myapex",
4037 key: "myapex.key",
4038 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004039 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004040 }
4041
4042 apex_key {
4043 name: "myapex.key",
4044 public_key: "testkey.avbpubkey",
4045 private_key: "testkey.pem",
4046 }
4047
4048 sh_binary {
4049 name: "myscript",
4050 src: "mylib.cpp",
4051 filename: "myscript.sh",
4052 sub_dir: "script",
4053 }
4054 `)
4055
Sundong Ahnabb64432019-10-22 13:58:29 +09004056 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004057 copyCmds := apexRule.Args["copy_commands"]
4058
4059 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4060}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004061
Jooyung Han91df2082019-11-20 01:49:42 +09004062func TestApexInVariousPartition(t *testing.T) {
4063 testcases := []struct {
4064 propName, parition, flattenedPartition string
4065 }{
4066 {"", "system", "system_ext"},
4067 {"product_specific: true", "product", "product"},
4068 {"soc_specific: true", "vendor", "vendor"},
4069 {"proprietary: true", "vendor", "vendor"},
4070 {"vendor: true", "vendor", "vendor"},
4071 {"system_ext_specific: true", "system_ext", "system_ext"},
4072 }
4073 for _, tc := range testcases {
4074 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004075 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004076 apex {
4077 name: "myapex",
4078 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004079 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004080 `+tc.propName+`
4081 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004082
Jooyung Han91df2082019-11-20 01:49:42 +09004083 apex_key {
4084 name: "myapex.key",
4085 public_key: "testkey.avbpubkey",
4086 private_key: "testkey.pem",
4087 }
4088 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004089
Jooyung Han91df2082019-11-20 01:49:42 +09004090 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4091 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
4092 actual := apex.installDir.String()
4093 if actual != expected {
4094 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4095 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004096
Jooyung Han91df2082019-11-20 01:49:42 +09004097 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4098 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
4099 actual = flattened.installDir.String()
4100 if actual != expected {
4101 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4102 }
4103 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004104 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004105}
Jiyong Park67882562019-03-21 01:11:21 +09004106
Jooyung Han580eb4f2020-06-24 19:33:06 +09004107func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004108 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004109 apex {
4110 name: "myapex",
4111 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004112 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004113 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004114
Jooyung Han580eb4f2020-06-24 19:33:06 +09004115 apex_key {
4116 name: "myapex.key",
4117 public_key: "testkey.avbpubkey",
4118 private_key: "testkey.pem",
4119 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004120 `)
4121 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004122 rule := module.Output("file_contexts")
4123 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4124}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004125
Jooyung Han580eb4f2020-06-24 19:33:06 +09004126func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004127 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004128 apex {
4129 name: "myapex",
4130 key: "myapex.key",
4131 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004132 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004133 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004134
Jooyung Han580eb4f2020-06-24 19:33:06 +09004135 apex_key {
4136 name: "myapex.key",
4137 public_key: "testkey.avbpubkey",
4138 private_key: "testkey.pem",
4139 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004140 `, withFiles(map[string][]byte{
4141 "my_own_file_contexts": nil,
4142 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004143}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004144
Jooyung Han580eb4f2020-06-24 19:33:06 +09004145func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004146 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004147 apex {
4148 name: "myapex",
4149 key: "myapex.key",
4150 product_specific: true,
4151 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004152 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004153 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004154
Jooyung Han580eb4f2020-06-24 19:33:06 +09004155 apex_key {
4156 name: "myapex.key",
4157 public_key: "testkey.avbpubkey",
4158 private_key: "testkey.pem",
4159 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004160 `)
4161
Colin Cross1c460562021-02-16 17:55:47 -08004162 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004163 apex {
4164 name: "myapex",
4165 key: "myapex.key",
4166 product_specific: true,
4167 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004168 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004169 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004170
Jooyung Han580eb4f2020-06-24 19:33:06 +09004171 apex_key {
4172 name: "myapex.key",
4173 public_key: "testkey.avbpubkey",
4174 private_key: "testkey.pem",
4175 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004176 `, withFiles(map[string][]byte{
4177 "product_specific_file_contexts": nil,
4178 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004179 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4180 rule := module.Output("file_contexts")
4181 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4182}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004183
Jooyung Han580eb4f2020-06-24 19:33:06 +09004184func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004185 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004186 apex {
4187 name: "myapex",
4188 key: "myapex.key",
4189 product_specific: true,
4190 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004191 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004192 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004193
Jooyung Han580eb4f2020-06-24 19:33:06 +09004194 apex_key {
4195 name: "myapex.key",
4196 public_key: "testkey.avbpubkey",
4197 private_key: "testkey.pem",
4198 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004199
Jooyung Han580eb4f2020-06-24 19:33:06 +09004200 filegroup {
4201 name: "my-file-contexts",
4202 srcs: ["product_specific_file_contexts"],
4203 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004204 `, withFiles(map[string][]byte{
4205 "product_specific_file_contexts": nil,
4206 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004207 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4208 rule := module.Output("file_contexts")
4209 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004210}
4211
Jiyong Park67882562019-03-21 01:11:21 +09004212func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004213 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004214 apex_key {
4215 name: "myapex.key",
4216 public_key: ":my.avbpubkey",
4217 private_key: ":my.pem",
4218 product_specific: true,
4219 }
4220
4221 filegroup {
4222 name: "my.avbpubkey",
4223 srcs: ["testkey2.avbpubkey"],
4224 }
4225
4226 filegroup {
4227 name: "my.pem",
4228 srcs: ["testkey2.pem"],
4229 }
4230 `)
4231
4232 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4233 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004234 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004235 if actual_pubkey != expected_pubkey {
4236 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4237 }
4238 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004239 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004240 if actual_privkey != expected_privkey {
4241 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4242 }
4243}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004244
4245func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004246 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004247 prebuilt_apex {
4248 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004249 arch: {
4250 arm64: {
4251 src: "myapex-arm64.apex",
4252 },
4253 arm: {
4254 src: "myapex-arm.apex",
4255 },
4256 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004257 }
4258 `)
4259
4260 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4261
Jiyong Parkc95714e2019-03-29 14:23:10 +09004262 expectedInput := "myapex-arm64.apex"
4263 if prebuilt.inputApex.String() != expectedInput {
4264 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4265 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004266}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004267
4268func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004269 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004270 prebuilt_apex {
4271 name: "myapex",
4272 src: "myapex-arm.apex",
4273 filename: "notmyapex.apex",
4274 }
4275 `)
4276
4277 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4278
4279 expected := "notmyapex.apex"
4280 if p.installFilename != expected {
4281 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4282 }
4283}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004284
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004285func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004286 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004287 prebuilt_apex {
4288 name: "myapex.prebuilt",
4289 src: "myapex-arm.apex",
4290 overrides: [
4291 "myapex",
4292 ],
4293 }
4294 `)
4295
4296 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4297
4298 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004299 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004300 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004301 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004302 }
4303}
4304
Paul Duffin092153d2021-01-26 11:42:39 +00004305// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4306// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004307func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4308 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004309 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004310 }
4311
Paul Duffin89886cb2021-02-05 16:44:03 +00004312 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004313 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004314 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004315 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004316 stem := android.RemoveOptionalPrebuiltPrefix(name)
4317 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004318 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4319 }
4320 }
4321
Paul Duffin39853512021-02-26 11:09:39 +00004322 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004323 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004324 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004325 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4326 }
4327 }
4328
4329 t.Run("prebuilt only", func(t *testing.T) {
4330 bp := `
4331 prebuilt_apex {
4332 name: "myapex",
4333 arch: {
4334 arm64: {
4335 src: "myapex-arm64.apex",
4336 },
4337 arm: {
4338 src: "myapex-arm.apex",
4339 },
4340 },
Paul Duffin39853512021-02-26 11:09:39 +00004341 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004342 }
4343
4344 java_import {
4345 name: "libfoo",
4346 jars: ["libfoo.jar"],
4347 }
Paul Duffin39853512021-02-26 11:09:39 +00004348
4349 java_sdk_library_import {
4350 name: "libbar",
4351 public: {
4352 jars: ["libbar.jar"],
4353 },
4354 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004355 `
4356
4357 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4358 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4359
Paul Duffinf6932af2021-02-26 18:21:56 +00004360 // Make sure that the deapexer has the correct input APEX.
4361 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4362 rule := deapexer.Rule("deapexer")
4363 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4364 t.Errorf("expected: %q, found: %q", expected, actual)
4365 }
4366
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004367 // Make sure that the prebuilt_apex has the correct input APEX.
4368 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4369 rule = prebuiltApex.Rule("android/soong/android.Cp")
4370 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4371 t.Errorf("expected: %q, found: %q", expected, actual)
4372 }
4373
Paul Duffin89886cb2021-02-05 16:44:03 +00004374 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004375
4376 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004377 })
4378
4379 t.Run("prebuilt with source preferred", func(t *testing.T) {
4380
4381 bp := `
4382 prebuilt_apex {
4383 name: "myapex",
4384 arch: {
4385 arm64: {
4386 src: "myapex-arm64.apex",
4387 },
4388 arm: {
4389 src: "myapex-arm.apex",
4390 },
4391 },
Paul Duffin39853512021-02-26 11:09:39 +00004392 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004393 }
4394
4395 java_import {
4396 name: "libfoo",
4397 jars: ["libfoo.jar"],
4398 }
4399
4400 java_library {
4401 name: "libfoo",
4402 }
Paul Duffin39853512021-02-26 11:09:39 +00004403
4404 java_sdk_library_import {
4405 name: "libbar",
4406 public: {
4407 jars: ["libbar.jar"],
4408 },
4409 }
4410
4411 java_sdk_library {
4412 name: "libbar",
4413 srcs: ["foo/bar/MyClass.java"],
4414 unsafe_ignore_missing_latest_api: true,
4415 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004416 `
4417
4418 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4419 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4420
Paul Duffin89886cb2021-02-05 16:44:03 +00004421 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004422 ensureNoSourceVariant(t, ctx, "libfoo")
4423
4424 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4425 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004426 })
4427
4428 t.Run("prebuilt preferred with source", func(t *testing.T) {
4429 bp := `
4430 prebuilt_apex {
4431 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004432 arch: {
4433 arm64: {
4434 src: "myapex-arm64.apex",
4435 },
4436 arm: {
4437 src: "myapex-arm.apex",
4438 },
4439 },
Paul Duffin39853512021-02-26 11:09:39 +00004440 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004441 }
4442
4443 java_import {
4444 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004445 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004446 jars: ["libfoo.jar"],
4447 }
4448
4449 java_library {
4450 name: "libfoo",
4451 }
Paul Duffin39853512021-02-26 11:09:39 +00004452
4453 java_sdk_library_import {
4454 name: "libbar",
4455 prefer: true,
4456 public: {
4457 jars: ["libbar.jar"],
4458 },
4459 }
4460
4461 java_sdk_library {
4462 name: "libbar",
4463 srcs: ["foo/bar/MyClass.java"],
4464 unsafe_ignore_missing_latest_api: true,
4465 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004466 `
4467
4468 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4469 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4470
Paul Duffin89886cb2021-02-05 16:44:03 +00004471 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004472 ensureNoSourceVariant(t, ctx, "libfoo")
4473
4474 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4475 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004476 })
4477}
4478
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004479func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4480 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin37856732021-02-26 14:24:15 +00004481 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo", "myapex:libbar"})
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004482 }
4483
Paul Duffin37856732021-02-26 14:24:15 +00004484 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4485 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004486 s := ctx.SingletonForTests("dex_bootjars")
4487 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004488 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004489 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004490 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004491 foundLibfooJar = true
4492 buildRule := s.Output(output)
4493 actual := android.NormalizePathForTesting(buildRule.Input)
4494 if actual != bootDexJarPath {
4495 t.Errorf("Incorrect boot dex jar path '%s', expected '%s'", actual, bootDexJarPath)
4496 }
4497 }
4498 }
4499 if !foundLibfooJar {
4500 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs")
4501 }
4502 }
4503
Paul Duffin4fd997b2021-02-03 20:06:33 +00004504 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004505 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +00004506 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4507 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4508 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4509 }
4510
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004511 t.Run("prebuilt only", func(t *testing.T) {
4512 bp := `
4513 prebuilt_apex {
4514 name: "myapex",
4515 arch: {
4516 arm64: {
4517 src: "myapex-arm64.apex",
4518 },
4519 arm: {
4520 src: "myapex-arm.apex",
4521 },
4522 },
Paul Duffin37856732021-02-26 14:24:15 +00004523 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004524 }
4525
4526 java_import {
4527 name: "libfoo",
4528 jars: ["libfoo.jar"],
4529 apex_available: ["myapex"],
4530 }
Paul Duffin37856732021-02-26 14:24:15 +00004531
4532 java_sdk_library_import {
4533 name: "libbar",
4534 public: {
4535 jars: ["libbar.jar"],
4536 },
4537 apex_available: ["myapex"],
4538 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004539 `
4540
4541 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004542 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4543 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004544
Paul Duffin9d67ca62021-02-03 20:06:33 +00004545 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4546 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004547.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004548.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4549`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004550 })
4551
4552 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4553 bp := `
4554 prebuilt_apex {
4555 name: "myapex",
4556 arch: {
4557 arm64: {
4558 src: "myapex-arm64.apex",
4559 },
4560 arm: {
4561 src: "myapex-arm.apex",
4562 },
4563 },
Paul Duffin37856732021-02-26 14:24:15 +00004564 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004565 }
4566
4567 java_import {
4568 name: "libfoo",
4569 jars: ["libfoo.jar"],
4570 apex_available: ["myapex"],
4571 }
4572
4573 java_library {
4574 name: "libfoo",
4575 srcs: ["foo/bar/MyClass.java"],
4576 apex_available: ["myapex"],
4577 }
Paul Duffin37856732021-02-26 14:24:15 +00004578
4579 java_sdk_library_import {
4580 name: "libbar",
4581 public: {
4582 jars: ["libbar.jar"],
4583 },
4584 apex_available: ["myapex"],
4585 }
4586
4587 java_sdk_library {
4588 name: "libbar",
4589 srcs: ["foo/bar/MyClass.java"],
4590 unsafe_ignore_missing_latest_api: true,
4591 apex_available: ["myapex"],
4592 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004593 `
4594
4595 // In this test the source (java_library) libfoo is active since the
4596 // prebuilt (java_import) defaults to prefer:false. However the
4597 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4598 // find the dex boot jar in it. We either need to disable the source libfoo
4599 // or make the prebuilt libfoo preferred.
4600 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4601 })
4602
4603 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4604 bp := `
4605 prebuilt_apex {
4606 name: "myapex",
4607 arch: {
4608 arm64: {
4609 src: "myapex-arm64.apex",
4610 },
4611 arm: {
4612 src: "myapex-arm.apex",
4613 },
4614 },
Paul Duffin37856732021-02-26 14:24:15 +00004615 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004616 }
4617
4618 java_import {
4619 name: "libfoo",
4620 prefer: true,
4621 jars: ["libfoo.jar"],
4622 apex_available: ["myapex"],
4623 }
4624
4625 java_library {
4626 name: "libfoo",
4627 srcs: ["foo/bar/MyClass.java"],
4628 apex_available: ["myapex"],
4629 }
Paul Duffin37856732021-02-26 14:24:15 +00004630
4631 java_sdk_library_import {
4632 name: "libbar",
4633 prefer: true,
4634 public: {
4635 jars: ["libbar.jar"],
4636 },
4637 apex_available: ["myapex"],
4638 }
4639
4640 java_sdk_library {
4641 name: "libbar",
4642 srcs: ["foo/bar/MyClass.java"],
4643 unsafe_ignore_missing_latest_api: true,
4644 apex_available: ["myapex"],
4645 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004646 `
4647
4648 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004649 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4650 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004651
Paul Duffin9d67ca62021-02-03 20:06:33 +00004652 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4653 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004654.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004655.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4656`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004657 })
4658
4659 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4660 bp := `
4661 apex {
4662 name: "myapex",
4663 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004664 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004665 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004666 }
4667
4668 apex_key {
4669 name: "myapex.key",
4670 public_key: "testkey.avbpubkey",
4671 private_key: "testkey.pem",
4672 }
4673
4674 prebuilt_apex {
4675 name: "myapex",
4676 arch: {
4677 arm64: {
4678 src: "myapex-arm64.apex",
4679 },
4680 arm: {
4681 src: "myapex-arm.apex",
4682 },
4683 },
Paul Duffin37856732021-02-26 14:24:15 +00004684 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004685 }
4686
4687 java_import {
4688 name: "libfoo",
4689 jars: ["libfoo.jar"],
4690 apex_available: ["myapex"],
4691 }
4692
4693 java_library {
4694 name: "libfoo",
4695 srcs: ["foo/bar/MyClass.java"],
4696 apex_available: ["myapex"],
4697 }
Paul Duffin37856732021-02-26 14:24:15 +00004698
4699 java_sdk_library_import {
4700 name: "libbar",
4701 public: {
4702 jars: ["libbar.jar"],
4703 },
4704 apex_available: ["myapex"],
4705 }
4706
4707 java_sdk_library {
4708 name: "libbar",
4709 srcs: ["foo/bar/MyClass.java"],
4710 unsafe_ignore_missing_latest_api: true,
4711 apex_available: ["myapex"],
4712 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004713 `
4714
4715 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004716 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4717 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004718
4719 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4720 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004721.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004722.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4723`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004724 })
4725
4726 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4727 bp := `
4728 apex {
4729 name: "myapex",
4730 enabled: false,
4731 key: "myapex.key",
4732 java_libs: ["libfoo"],
4733 }
4734
4735 apex_key {
4736 name: "myapex.key",
4737 public_key: "testkey.avbpubkey",
4738 private_key: "testkey.pem",
4739 }
4740
4741 prebuilt_apex {
4742 name: "myapex",
4743 arch: {
4744 arm64: {
4745 src: "myapex-arm64.apex",
4746 },
4747 arm: {
4748 src: "myapex-arm.apex",
4749 },
4750 },
Paul Duffin37856732021-02-26 14:24:15 +00004751 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004752 }
4753
4754 java_import {
4755 name: "libfoo",
4756 prefer: true,
4757 jars: ["libfoo.jar"],
4758 apex_available: ["myapex"],
4759 }
4760
4761 java_library {
4762 name: "libfoo",
4763 srcs: ["foo/bar/MyClass.java"],
4764 apex_available: ["myapex"],
4765 }
Paul Duffin37856732021-02-26 14:24:15 +00004766
4767 java_sdk_library_import {
4768 name: "libbar",
4769 prefer: true,
4770 public: {
4771 jars: ["libbar.jar"],
4772 },
4773 apex_available: ["myapex"],
4774 }
4775
4776 java_sdk_library {
4777 name: "libbar",
4778 srcs: ["foo/bar/MyClass.java"],
4779 unsafe_ignore_missing_latest_api: true,
4780 apex_available: ["myapex"],
4781 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004782 `
4783
4784 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004785 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4786 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004787
Paul Duffin9d67ca62021-02-03 20:06:33 +00004788 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4789 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004790.intermediates/prebuilt_libbar/android_common_prebuilt_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004791.intermediates/prebuilt_libfoo/android_common_prebuilt_myapex/hiddenapi/index.csv
4792`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004793 })
4794}
4795
Roland Levillain630846d2019-06-26 12:48:34 +01004796func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004797 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004798 apex_test {
4799 name: "myapex",
4800 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004801 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004802 tests: [
4803 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004804 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004805 ],
4806 }
4807
4808 apex_key {
4809 name: "myapex.key",
4810 public_key: "testkey.avbpubkey",
4811 private_key: "testkey.pem",
4812 }
4813
Liz Kammer1c14a212020-05-12 15:26:55 -07004814 filegroup {
4815 name: "fg",
4816 srcs: [
4817 "baz",
4818 "bar/baz"
4819 ],
4820 }
4821
Roland Levillain630846d2019-06-26 12:48:34 +01004822 cc_test {
4823 name: "mytest",
4824 gtest: false,
4825 srcs: ["mytest.cpp"],
4826 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004827 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004828 system_shared_libs: [],
4829 static_executable: true,
4830 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004831 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004832 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004833
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004834 cc_library {
4835 name: "mylib",
4836 srcs: ["mylib.cpp"],
4837 system_shared_libs: [],
4838 stl: "none",
4839 }
4840
Liz Kammer5bd365f2020-05-27 15:15:11 -07004841 filegroup {
4842 name: "fg2",
4843 srcs: [
4844 "testdata/baz"
4845 ],
4846 }
4847
Roland Levillain9b5fde92019-06-28 15:41:19 +01004848 cc_test {
4849 name: "mytests",
4850 gtest: false,
4851 srcs: [
4852 "mytest1.cpp",
4853 "mytest2.cpp",
4854 "mytest3.cpp",
4855 ],
4856 test_per_src: true,
4857 relative_install_path: "test",
4858 system_shared_libs: [],
4859 static_executable: true,
4860 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004861 data: [
4862 ":fg",
4863 ":fg2",
4864 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004865 }
Roland Levillain630846d2019-06-26 12:48:34 +01004866 `)
4867
Sundong Ahnabb64432019-10-22 13:58:29 +09004868 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004869 copyCmds := apexRule.Args["copy_commands"]
4870
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004871 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004872 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004873 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004874
Liz Kammer1c14a212020-05-12 15:26:55 -07004875 //Ensure that test data are copied into apex.
4876 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4877 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4878
Roland Levillain9b5fde92019-06-28 15:41:19 +01004879 // Ensure that test deps built with `test_per_src` are copied into apex.
4880 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4881 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4882 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004883
4884 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004885 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004886 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004887 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004888 prefix := "TARGET_"
4889 var builder strings.Builder
4890 data.Custom(&builder, name, prefix, "", data)
4891 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004892 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4893 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4894 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4895 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004896 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004897 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004898 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004899
4900 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004901 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004902 data.Custom(&builder, name, prefix, "", data)
4903 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004904 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4905 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004906}
4907
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004908func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004909 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004910 apex {
4911 name: "myapex",
4912 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004913 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004914 }
4915 apex_key {
4916 name: "myapex.key",
4917 public_key: "testkey.avbpubkey",
4918 private_key: "testkey.pem",
4919 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00004920 `,
4921 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4922 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4923 }),
4924 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004925 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004926 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004927 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004928 var builder strings.Builder
4929 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4930 androidMk := builder.String()
4931 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4932}
4933
Jooyung Hand48f3c32019-08-23 11:18:57 +09004934func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4935 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4936 apex {
4937 name: "myapex",
4938 key: "myapex.key",
4939 native_shared_libs: ["libfoo"],
4940 }
4941
4942 apex_key {
4943 name: "myapex.key",
4944 public_key: "testkey.avbpubkey",
4945 private_key: "testkey.pem",
4946 }
4947
4948 cc_library {
4949 name: "libfoo",
4950 stl: "none",
4951 system_shared_libs: [],
4952 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004953 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004954 }
4955 `)
4956 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4957 apex {
4958 name: "myapex",
4959 key: "myapex.key",
4960 java_libs: ["myjar"],
4961 }
4962
4963 apex_key {
4964 name: "myapex.key",
4965 public_key: "testkey.avbpubkey",
4966 private_key: "testkey.pem",
4967 }
4968
4969 java_library {
4970 name: "myjar",
4971 srcs: ["foo/bar/MyClass.java"],
4972 sdk_version: "none",
4973 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004974 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004975 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004976 }
4977 `)
4978}
4979
Bill Peckhama41a6962021-01-11 10:58:54 -08004980func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004981 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08004982 apex {
4983 name: "myapex",
4984 key: "myapex.key",
4985 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004986 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08004987 }
4988
4989 apex_key {
4990 name: "myapex.key",
4991 public_key: "testkey.avbpubkey",
4992 private_key: "testkey.pem",
4993 }
4994
4995 java_import {
4996 name: "myjavaimport",
4997 apex_available: ["myapex"],
4998 jars: ["my.jar"],
4999 compile_dex: true,
5000 }
5001 `)
5002
5003 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5004 apexRule := module.Rule("apexRule")
5005 copyCmds := apexRule.Args["copy_commands"]
5006 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5007}
5008
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005009func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005010 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005011 apex {
5012 name: "myapex",
5013 key: "myapex.key",
5014 apps: [
5015 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005016 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005017 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005018 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005019 }
5020
5021 apex_key {
5022 name: "myapex.key",
5023 public_key: "testkey.avbpubkey",
5024 private_key: "testkey.pem",
5025 }
5026
5027 android_app {
5028 name: "AppFoo",
5029 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005030 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005031 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005032 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005033 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005034 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005035 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005036
5037 android_app {
5038 name: "AppFooPriv",
5039 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005040 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005041 system_modules: "none",
5042 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005043 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005044 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005045 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005046
5047 cc_library_shared {
5048 name: "libjni",
5049 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005050 shared_libs: ["libfoo"],
5051 stl: "none",
5052 system_shared_libs: [],
5053 apex_available: [ "myapex" ],
5054 sdk_version: "current",
5055 }
5056
5057 cc_library_shared {
5058 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005059 stl: "none",
5060 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005061 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005062 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005063 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005064 `)
5065
Sundong Ahnabb64432019-10-22 13:58:29 +09005066 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005067 apexRule := module.Rule("apexRule")
5068 copyCmds := apexRule.Args["copy_commands"]
5069
5070 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005071 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005072
Colin Crossaede88c2020-08-11 12:17:01 -07005073 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005074 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005075 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005076 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005077 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005078 // JNI libraries including transitive deps are
5079 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005080 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005081 // ... embedded inside APK (jnilibs.zip)
5082 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5083 // ... and not directly inside the APEX
5084 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5085 }
Dario Frenicde2a032019-10-27 00:29:22 +01005086}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005087
Dario Frenicde2a032019-10-27 00:29:22 +01005088func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005089 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005090 apex {
5091 name: "myapex",
5092 key: "myapex.key",
5093 apps: [
5094 "AppFooPrebuilt",
5095 "AppFooPrivPrebuilt",
5096 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005097 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005098 }
5099
5100 apex_key {
5101 name: "myapex.key",
5102 public_key: "testkey.avbpubkey",
5103 private_key: "testkey.pem",
5104 }
5105
5106 android_app_import {
5107 name: "AppFooPrebuilt",
5108 apk: "PrebuiltAppFoo.apk",
5109 presigned: true,
5110 dex_preopt: {
5111 enabled: false,
5112 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005113 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005114 }
5115
5116 android_app_import {
5117 name: "AppFooPrivPrebuilt",
5118 apk: "PrebuiltAppFooPriv.apk",
5119 privileged: true,
5120 presigned: true,
5121 dex_preopt: {
5122 enabled: false,
5123 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005124 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005125 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005126 }
5127 `)
5128
Sundong Ahnabb64432019-10-22 13:58:29 +09005129 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005130 apexRule := module.Rule("apexRule")
5131 copyCmds := apexRule.Args["copy_commands"]
5132
5133 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005134 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5135}
5136
5137func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005138 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005139 apex {
5140 name: "myapex",
5141 key: "myapex.key",
5142 apps: [
5143 "AppFoo",
5144 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005145 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005146 }
5147
5148 apex_key {
5149 name: "myapex.key",
5150 public_key: "testkey.avbpubkey",
5151 private_key: "testkey.pem",
5152 }
5153
5154 android_app {
5155 name: "AppFoo",
5156 srcs: ["foo/bar/MyClass.java"],
5157 sdk_version: "none",
5158 system_modules: "none",
5159 apex_available: [ "myapex" ],
5160 }
5161
5162 android_app_import {
5163 name: "AppFoo",
5164 apk: "AppFooPrebuilt.apk",
5165 filename: "AppFooPrebuilt.apk",
5166 presigned: true,
5167 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005168 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005169 }
5170 `, withFiles(map[string][]byte{
5171 "AppFooPrebuilt.apk": nil,
5172 }))
5173
5174 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005175 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005176 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005177}
5178
Dario Freni6f3937c2019-12-20 22:58:03 +00005179func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005180 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005181 apex {
5182 name: "myapex",
5183 key: "myapex.key",
5184 apps: [
5185 "TesterHelpAppFoo",
5186 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005187 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005188 }
5189
5190 apex_key {
5191 name: "myapex.key",
5192 public_key: "testkey.avbpubkey",
5193 private_key: "testkey.pem",
5194 }
5195
5196 android_test_helper_app {
5197 name: "TesterHelpAppFoo",
5198 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005199 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005200 }
5201
5202 `)
5203
5204 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5205 apexRule := module.Rule("apexRule")
5206 copyCmds := apexRule.Args["copy_commands"]
5207
5208 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5209}
5210
Jooyung Han18020ea2019-11-13 10:50:48 +09005211func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5212 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005213 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005214 apex {
5215 name: "myapex",
5216 key: "myapex.key",
5217 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005218 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005219 }
5220
5221 apex_key {
5222 name: "myapex.key",
5223 public_key: "testkey.avbpubkey",
5224 private_key: "testkey.pem",
5225 }
5226
5227 apex {
5228 name: "otherapex",
5229 key: "myapex.key",
5230 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005231 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005232 }
5233
5234 cc_defaults {
5235 name: "libfoo-defaults",
5236 apex_available: ["otherapex"],
5237 }
5238
5239 cc_library {
5240 name: "libfoo",
5241 defaults: ["libfoo-defaults"],
5242 stl: "none",
5243 system_shared_libs: [],
5244 }`)
5245}
5246
Paul Duffine52e66f2020-03-30 17:54:29 +01005247func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005248 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005249 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005250 apex {
5251 name: "myapex",
5252 key: "myapex.key",
5253 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005254 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005255 }
5256
5257 apex_key {
5258 name: "myapex.key",
5259 public_key: "testkey.avbpubkey",
5260 private_key: "testkey.pem",
5261 }
5262
5263 apex {
5264 name: "otherapex",
5265 key: "otherapex.key",
5266 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005267 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005268 }
5269
5270 apex_key {
5271 name: "otherapex.key",
5272 public_key: "testkey.avbpubkey",
5273 private_key: "testkey.pem",
5274 }
5275
5276 cc_library {
5277 name: "libfoo",
5278 stl: "none",
5279 system_shared_libs: [],
5280 apex_available: ["otherapex"],
5281 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005282}
Jiyong Park127b40b2019-09-30 16:04:35 +09005283
Paul Duffine52e66f2020-03-30 17:54:29 +01005284func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005285 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005286 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005287.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005288.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005289.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005290.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005291.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005292.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005293 apex {
5294 name: "myapex",
5295 key: "myapex.key",
5296 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005297 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005298 }
5299
5300 apex_key {
5301 name: "myapex.key",
5302 public_key: "testkey.avbpubkey",
5303 private_key: "testkey.pem",
5304 }
5305
Jiyong Park127b40b2019-09-30 16:04:35 +09005306 cc_library {
5307 name: "libfoo",
5308 stl: "none",
5309 shared_libs: ["libbar"],
5310 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005311 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005312 }
5313
5314 cc_library {
5315 name: "libbar",
5316 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005317 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005318 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005319 apex_available: ["myapex"],
5320 }
5321
5322 cc_library {
5323 name: "libbaz",
5324 stl: "none",
5325 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005326 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005327}
Jiyong Park127b40b2019-09-30 16:04:35 +09005328
Paul Duffine52e66f2020-03-30 17:54:29 +01005329func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005330 testApexError(t, "\"otherapex\" is not a valid module name", `
5331 apex {
5332 name: "myapex",
5333 key: "myapex.key",
5334 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005335 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005336 }
5337
5338 apex_key {
5339 name: "myapex.key",
5340 public_key: "testkey.avbpubkey",
5341 private_key: "testkey.pem",
5342 }
5343
5344 cc_library {
5345 name: "libfoo",
5346 stl: "none",
5347 system_shared_libs: [],
5348 apex_available: ["otherapex"],
5349 }`)
5350
Paul Duffine52e66f2020-03-30 17:54:29 +01005351 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005352 apex {
5353 name: "myapex",
5354 key: "myapex.key",
5355 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005356 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005357 }
5358
5359 apex_key {
5360 name: "myapex.key",
5361 public_key: "testkey.avbpubkey",
5362 private_key: "testkey.pem",
5363 }
5364
5365 cc_library {
5366 name: "libfoo",
5367 stl: "none",
5368 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005369 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005370 apex_available: ["myapex"],
5371 }
5372
5373 cc_library {
5374 name: "libbar",
5375 stl: "none",
5376 system_shared_libs: [],
5377 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005378 }
5379
5380 cc_library {
5381 name: "libbaz",
5382 stl: "none",
5383 system_shared_libs: [],
5384 stubs: {
5385 versions: ["10", "20", "30"],
5386 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005387 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005388}
Jiyong Park127b40b2019-09-30 16:04:35 +09005389
Jiyong Park89e850a2020-04-07 16:37:39 +09005390func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005391 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005392 apex {
5393 name: "myapex",
5394 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005395 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005396 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005397 }
5398
5399 apex_key {
5400 name: "myapex.key",
5401 public_key: "testkey.avbpubkey",
5402 private_key: "testkey.pem",
5403 }
5404
5405 cc_library {
5406 name: "libfoo",
5407 stl: "none",
5408 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005409 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005410 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005411 }
5412
5413 cc_library {
5414 name: "libfoo2",
5415 stl: "none",
5416 system_shared_libs: [],
5417 shared_libs: ["libbaz"],
5418 apex_available: ["//apex_available:platform"],
5419 }
5420
5421 cc_library {
5422 name: "libbar",
5423 stl: "none",
5424 system_shared_libs: [],
5425 apex_available: ["myapex"],
5426 }
5427
5428 cc_library {
5429 name: "libbaz",
5430 stl: "none",
5431 system_shared_libs: [],
5432 apex_available: ["myapex"],
5433 stubs: {
5434 versions: ["1"],
5435 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005436 }`)
5437
Jiyong Park89e850a2020-04-07 16:37:39 +09005438 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5439 // because it depends on libbar which isn't available to platform
5440 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5441 if libfoo.NotAvailableForPlatform() != true {
5442 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5443 }
5444
5445 // libfoo2 however can be available to platform because it depends on libbaz which provides
5446 // stubs
5447 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5448 if libfoo2.NotAvailableForPlatform() == true {
5449 t.Errorf("%q should be available to platform", libfoo2.String())
5450 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005451}
Jiyong Parka90ca002019-10-07 15:47:24 +09005452
Paul Duffine52e66f2020-03-30 17:54:29 +01005453func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005454 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005455 apex {
5456 name: "myapex",
5457 key: "myapex.key",
5458 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005459 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005460 }
5461
5462 apex_key {
5463 name: "myapex.key",
5464 public_key: "testkey.avbpubkey",
5465 private_key: "testkey.pem",
5466 }
5467
5468 cc_library {
5469 name: "libfoo",
5470 stl: "none",
5471 system_shared_libs: [],
5472 apex_available: ["myapex"],
5473 static: {
5474 apex_available: ["//apex_available:platform"],
5475 },
5476 }`)
5477
Jiyong Park89e850a2020-04-07 16:37:39 +09005478 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5479 if libfooShared.NotAvailableForPlatform() != true {
5480 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5481 }
5482 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5483 if libfooStatic.NotAvailableForPlatform() != false {
5484 t.Errorf("%q should be available to platform", libfooStatic.String())
5485 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005486}
5487
Jiyong Park5d790c32019-11-15 18:40:32 +09005488func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005489 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005490 apex {
5491 name: "myapex",
5492 key: "myapex.key",
5493 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005494 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005495 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005496 }
5497
5498 override_apex {
5499 name: "override_myapex",
5500 base: "myapex",
5501 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005502 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005503 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005504 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005505 }
5506
5507 apex_key {
5508 name: "myapex.key",
5509 public_key: "testkey.avbpubkey",
5510 private_key: "testkey.pem",
5511 }
5512
5513 android_app {
5514 name: "app",
5515 srcs: ["foo/bar/MyClass.java"],
5516 package_name: "foo",
5517 sdk_version: "none",
5518 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005519 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005520 }
5521
5522 override_android_app {
5523 name: "override_app",
5524 base: "app",
5525 package_name: "bar",
5526 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005527 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005528
Jiyong Park317645e2019-12-05 13:20:58 +09005529 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5530 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5531 if originalVariant.GetOverriddenBy() != "" {
5532 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5533 }
5534 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5535 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5536 }
5537
Jiyong Park5d790c32019-11-15 18:40:32 +09005538 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5539 apexRule := module.Rule("apexRule")
5540 copyCmds := apexRule.Args["copy_commands"]
5541
5542 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005543 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005544
5545 apexBundle := module.Module().(*apexBundle)
5546 name := apexBundle.Name()
5547 if name != "override_myapex" {
5548 t.Errorf("name should be \"override_myapex\", but was %q", name)
5549 }
5550
Baligh Uddin004d7172020-02-19 21:29:28 -08005551 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5552 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5553 }
5554
Jiyong Park20bacab2020-03-03 11:45:41 +09005555 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005556 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005557
Colin Crossaa255532020-07-03 13:18:24 -07005558 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005559 var builder strings.Builder
5560 data.Custom(&builder, name, "TARGET_", "", data)
5561 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005562 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005563 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5564 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005565 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005566 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005567 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005568 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5569 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005570}
5571
Jooyung Han214bf372019-11-12 13:03:50 +09005572func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005573 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005574 apex {
5575 name: "myapex",
5576 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005577 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005578 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005579 }
5580
5581 apex_key {
5582 name: "myapex.key",
5583 public_key: "testkey.avbpubkey",
5584 private_key: "testkey.pem",
5585 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005586
5587 cc_library {
5588 name: "mylib",
5589 srcs: ["mylib.cpp"],
5590 stl: "libc++",
5591 system_shared_libs: [],
5592 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005593 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005594 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005595 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005596
5597 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5598 args := module.Rule("apexRule").Args
5599 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005600 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005601
5602 // The copies of the libraries in the apex should have one more dependency than
5603 // the ones outside the apex, namely the unwinder. Ideally we should check
5604 // the dependency names directly here but for some reason the names are blank in
5605 // this test.
5606 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005607 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005608 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5609 if len(apexImplicits) != len(nonApexImplicits)+1 {
5610 t.Errorf("%q missing unwinder dep", lib)
5611 }
5612 }
Jooyung Han214bf372019-11-12 13:03:50 +09005613}
5614
Paul Duffine05480a2021-03-08 15:07:14 +00005615var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005616 "api/current.txt": nil,
5617 "api/removed.txt": nil,
5618 "api/system-current.txt": nil,
5619 "api/system-removed.txt": nil,
5620 "api/test-current.txt": nil,
5621 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005622
Anton Hanssondff2c782020-12-21 17:10:01 +00005623 "100/public/api/foo.txt": nil,
5624 "100/public/api/foo-removed.txt": nil,
5625 "100/system/api/foo.txt": nil,
5626 "100/system/api/foo-removed.txt": nil,
5627
Paul Duffineedc5d52020-06-12 17:46:39 +01005628 // For java_sdk_library_import
5629 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005630}
5631
Jooyung Han58f26ab2019-12-18 15:34:32 +09005632func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005633 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005634 apex {
5635 name: "myapex",
5636 key: "myapex.key",
5637 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005638 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005639 }
5640
5641 apex_key {
5642 name: "myapex.key",
5643 public_key: "testkey.avbpubkey",
5644 private_key: "testkey.pem",
5645 }
5646
5647 java_sdk_library {
5648 name: "foo",
5649 srcs: ["a.java"],
5650 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005651 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005652 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005653
5654 prebuilt_apis {
5655 name: "sdk",
5656 api_dirs: ["100"],
5657 }
Paul Duffin9b879592020-05-26 13:21:35 +01005658 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005659
5660 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005661 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005662 "javalib/foo.jar",
5663 "etc/permissions/foo.xml",
5664 })
5665 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005666 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5667 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005668}
5669
Paul Duffin9b879592020-05-26 13:21:35 +01005670func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005671 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005672 apex {
5673 name: "myapex",
5674 key: "myapex.key",
5675 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005676 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005677 }
5678
5679 apex_key {
5680 name: "myapex.key",
5681 public_key: "testkey.avbpubkey",
5682 private_key: "testkey.pem",
5683 }
5684
5685 java_sdk_library {
5686 name: "foo",
5687 srcs: ["a.java"],
5688 api_packages: ["foo"],
5689 apex_available: ["myapex"],
5690 sdk_version: "none",
5691 system_modules: "none",
5692 }
5693
5694 java_library {
5695 name: "bar",
5696 srcs: ["a.java"],
5697 libs: ["foo"],
5698 apex_available: ["myapex"],
5699 sdk_version: "none",
5700 system_modules: "none",
5701 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005702
5703 prebuilt_apis {
5704 name: "sdk",
5705 api_dirs: ["100"],
5706 }
Paul Duffin9b879592020-05-26 13:21:35 +01005707 `, withFiles(filesForSdkLibrary))
5708
5709 // java_sdk_library installs both impl jar and permission XML
5710 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5711 "javalib/bar.jar",
5712 "javalib/foo.jar",
5713 "etc/permissions/foo.xml",
5714 })
5715
5716 // The bar library should depend on the implementation jar.
5717 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5718 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5719 t.Errorf("expected %q, found %#q", expected, actual)
5720 }
5721}
5722
5723func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005724 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005725 apex {
5726 name: "myapex",
5727 key: "myapex.key",
5728 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005729 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005730 }
5731
5732 apex_key {
5733 name: "myapex.key",
5734 public_key: "testkey.avbpubkey",
5735 private_key: "testkey.pem",
5736 }
5737
5738 java_sdk_library {
5739 name: "foo",
5740 srcs: ["a.java"],
5741 api_packages: ["foo"],
5742 apex_available: ["myapex"],
5743 sdk_version: "none",
5744 system_modules: "none",
5745 }
5746
5747 java_library {
5748 name: "bar",
5749 srcs: ["a.java"],
5750 libs: ["foo"],
5751 sdk_version: "none",
5752 system_modules: "none",
5753 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005754
5755 prebuilt_apis {
5756 name: "sdk",
5757 api_dirs: ["100"],
5758 }
Paul Duffin9b879592020-05-26 13:21:35 +01005759 `, withFiles(filesForSdkLibrary))
5760
5761 // java_sdk_library installs both impl jar and permission XML
5762 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5763 "javalib/foo.jar",
5764 "etc/permissions/foo.xml",
5765 })
5766
5767 // The bar library should depend on the stubs jar.
5768 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
5769 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5770 t.Errorf("expected %q, found %#q", expected, actual)
5771 }
5772}
5773
Paul Duffineedc5d52020-06-12 17:46:39 +01005774func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005775 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005776 prebuilt_apis {
5777 name: "sdk",
5778 api_dirs: ["100"],
5779 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005780 withFiles(map[string][]byte{
5781 "apex/a.java": nil,
5782 "apex/apex_manifest.json": nil,
5783 "apex/Android.bp": []byte(`
5784 package {
5785 default_visibility: ["//visibility:private"],
5786 }
5787
5788 apex {
5789 name: "myapex",
5790 key: "myapex.key",
5791 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005792 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005793 }
5794
5795 apex_key {
5796 name: "myapex.key",
5797 public_key: "testkey.avbpubkey",
5798 private_key: "testkey.pem",
5799 }
5800
5801 java_library {
5802 name: "bar",
5803 srcs: ["a.java"],
5804 libs: ["foo"],
5805 apex_available: ["myapex"],
5806 sdk_version: "none",
5807 system_modules: "none",
5808 }
5809`),
5810 "source/a.java": nil,
5811 "source/api/current.txt": nil,
5812 "source/api/removed.txt": nil,
5813 "source/Android.bp": []byte(`
5814 package {
5815 default_visibility: ["//visibility:private"],
5816 }
5817
5818 java_sdk_library {
5819 name: "foo",
5820 visibility: ["//apex"],
5821 srcs: ["a.java"],
5822 api_packages: ["foo"],
5823 apex_available: ["myapex"],
5824 sdk_version: "none",
5825 system_modules: "none",
5826 public: {
5827 enabled: true,
5828 },
5829 }
5830`),
5831 "prebuilt/a.jar": nil,
5832 "prebuilt/Android.bp": []byte(`
5833 package {
5834 default_visibility: ["//visibility:private"],
5835 }
5836
5837 java_sdk_library_import {
5838 name: "foo",
5839 visibility: ["//apex", "//source"],
5840 apex_available: ["myapex"],
5841 prefer: true,
5842 public: {
5843 jars: ["a.jar"],
5844 },
5845 }
5846`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005847 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005848 )
5849
5850 // java_sdk_library installs both impl jar and permission XML
5851 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5852 "javalib/bar.jar",
5853 "javalib/foo.jar",
5854 "etc/permissions/foo.xml",
5855 })
5856
5857 // The bar library should depend on the implementation jar.
5858 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5859 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5860 t.Errorf("expected %q, found %#q", expected, actual)
5861 }
5862}
5863
5864func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5865 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5866 apex {
5867 name: "myapex",
5868 key: "myapex.key",
5869 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005870 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005871 }
5872
5873 apex_key {
5874 name: "myapex.key",
5875 public_key: "testkey.avbpubkey",
5876 private_key: "testkey.pem",
5877 }
5878
5879 java_sdk_library_import {
5880 name: "foo",
5881 apex_available: ["myapex"],
5882 prefer: true,
5883 public: {
5884 jars: ["a.jar"],
5885 },
5886 }
5887
5888 `, withFiles(filesForSdkLibrary))
5889}
5890
atrost6e126252020-01-27 17:01:16 +00005891func TestCompatConfig(t *testing.T) {
Paul Duffina369c7b2021-03-09 03:08:05 +00005892 result := apexFixtureFactory.
5893 Extend(java.PrepareForTestWithPlatformCompatConfig).
5894 RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005895 apex {
5896 name: "myapex",
5897 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005898 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005899 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005900 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005901 }
5902
5903 apex_key {
5904 name: "myapex.key",
5905 public_key: "testkey.avbpubkey",
5906 private_key: "testkey.pem",
5907 }
5908
5909 platform_compat_config {
5910 name: "myjar-platform-compat-config",
5911 src: ":myjar",
5912 }
5913
5914 java_library {
5915 name: "myjar",
5916 srcs: ["foo/bar/MyClass.java"],
5917 sdk_version: "none",
5918 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005919 apex_available: [ "myapex" ],
5920 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005921
5922 // Make sure that a preferred prebuilt does not affect the apex contents.
5923 prebuilt_platform_compat_config {
5924 name: "myjar-platform-compat-config",
5925 metadata: "compat-config/metadata.xml",
5926 prefer: true,
5927 }
atrost6e126252020-01-27 17:01:16 +00005928 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005929 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005930 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5931 "etc/compatconfig/myjar-platform-compat-config.xml",
5932 "javalib/myjar.jar",
5933 })
5934}
5935
Jiyong Park479321d2019-12-16 11:47:12 +09005936func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5937 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5938 apex {
5939 name: "myapex",
5940 key: "myapex.key",
5941 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005942 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09005943 }
5944
5945 apex_key {
5946 name: "myapex.key",
5947 public_key: "testkey.avbpubkey",
5948 private_key: "testkey.pem",
5949 }
5950
5951 java_library {
5952 name: "myjar",
5953 srcs: ["foo/bar/MyClass.java"],
5954 sdk_version: "none",
5955 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005956 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005957 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005958 }
5959 `)
5960}
5961
Jiyong Park7afd1072019-12-30 16:56:33 +09005962func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005963 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09005964 apex {
5965 name: "myapex",
5966 key: "myapex.key",
5967 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005968 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09005969 }
5970
5971 apex_key {
5972 name: "myapex.key",
5973 public_key: "testkey.avbpubkey",
5974 private_key: "testkey.pem",
5975 }
5976
5977 cc_library {
5978 name: "mylib",
5979 srcs: ["mylib.cpp"],
5980 system_shared_libs: [],
5981 stl: "none",
5982 required: ["a", "b"],
5983 host_required: ["c", "d"],
5984 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005985 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09005986 }
5987 `)
5988
5989 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005990 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09005991 name := apexBundle.BaseModuleName()
5992 prefix := "TARGET_"
5993 var builder strings.Builder
5994 data.Custom(&builder, name, prefix, "", data)
5995 androidMk := builder.String()
5996 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
5997 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
5998 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
5999}
6000
Jiyong Park7cd10e32020-01-14 09:22:18 +09006001func TestSymlinksFromApexToSystem(t *testing.T) {
6002 bp := `
6003 apex {
6004 name: "myapex",
6005 key: "myapex.key",
6006 native_shared_libs: ["mylib"],
6007 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006008 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006009 }
6010
Jiyong Park9d677202020-02-19 16:29:35 +09006011 apex {
6012 name: "myapex.updatable",
6013 key: "myapex.key",
6014 native_shared_libs: ["mylib"],
6015 java_libs: ["myjar"],
6016 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006017 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006018 }
6019
Jiyong Park7cd10e32020-01-14 09:22:18 +09006020 apex_key {
6021 name: "myapex.key",
6022 public_key: "testkey.avbpubkey",
6023 private_key: "testkey.pem",
6024 }
6025
6026 cc_library {
6027 name: "mylib",
6028 srcs: ["mylib.cpp"],
6029 shared_libs: ["myotherlib"],
6030 system_shared_libs: [],
6031 stl: "none",
6032 apex_available: [
6033 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006034 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006035 "//apex_available:platform",
6036 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006037 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006038 }
6039
6040 cc_library {
6041 name: "myotherlib",
6042 srcs: ["mylib.cpp"],
6043 system_shared_libs: [],
6044 stl: "none",
6045 apex_available: [
6046 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006047 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006048 "//apex_available:platform",
6049 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006050 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006051 }
6052
6053 java_library {
6054 name: "myjar",
6055 srcs: ["foo/bar/MyClass.java"],
6056 sdk_version: "none",
6057 system_modules: "none",
6058 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006059 apex_available: [
6060 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006061 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006062 "//apex_available:platform",
6063 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006064 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006065 }
6066
6067 java_library {
6068 name: "myotherjar",
6069 srcs: ["foo/bar/MyClass.java"],
6070 sdk_version: "none",
6071 system_modules: "none",
6072 apex_available: [
6073 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006074 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006075 "//apex_available:platform",
6076 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006077 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006078 }
6079 `
6080
6081 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6082 for _, f := range files {
6083 if f.path == file {
6084 if f.isLink {
6085 t.Errorf("%q is not a real file", file)
6086 }
6087 return
6088 }
6089 }
6090 t.Errorf("%q is not found", file)
6091 }
6092
6093 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6094 for _, f := range files {
6095 if f.path == file {
6096 if !f.isLink {
6097 t.Errorf("%q is not a symlink", file)
6098 }
6099 return
6100 }
6101 }
6102 t.Errorf("%q is not found", file)
6103 }
6104
Jiyong Park9d677202020-02-19 16:29:35 +09006105 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6106 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006107 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006108 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006109 ensureRealfileExists(t, files, "javalib/myjar.jar")
6110 ensureRealfileExists(t, files, "lib64/mylib.so")
6111 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6112
Jiyong Park9d677202020-02-19 16:29:35 +09006113 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6114 ensureRealfileExists(t, files, "javalib/myjar.jar")
6115 ensureRealfileExists(t, files, "lib64/mylib.so")
6116 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6117
6118 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006119 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006120 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006121 ensureRealfileExists(t, files, "javalib/myjar.jar")
6122 ensureRealfileExists(t, files, "lib64/mylib.so")
6123 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006124
6125 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6126 ensureRealfileExists(t, files, "javalib/myjar.jar")
6127 ensureRealfileExists(t, files, "lib64/mylib.so")
6128 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006129}
6130
Yo Chiange8128052020-07-23 20:09:18 +08006131func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006132 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006133 apex {
6134 name: "myapex",
6135 key: "myapex.key",
6136 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006137 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006138 }
6139
6140 apex_key {
6141 name: "myapex.key",
6142 public_key: "testkey.avbpubkey",
6143 private_key: "testkey.pem",
6144 }
6145
6146 cc_library_shared {
6147 name: "mylib",
6148 srcs: ["mylib.cpp"],
6149 shared_libs: ["myotherlib"],
6150 system_shared_libs: [],
6151 stl: "none",
6152 apex_available: [
6153 "myapex",
6154 "//apex_available:platform",
6155 ],
6156 }
6157
6158 cc_prebuilt_library_shared {
6159 name: "myotherlib",
6160 srcs: ["prebuilt.so"],
6161 system_shared_libs: [],
6162 stl: "none",
6163 apex_available: [
6164 "myapex",
6165 "//apex_available:platform",
6166 ],
6167 }
6168 `)
6169
6170 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006171 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006172 var builder strings.Builder
6173 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6174 androidMk := builder.String()
6175 // `myotherlib` is added to `myapex` as symlink
6176 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6177 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6178 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6179 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006180 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 +08006181}
6182
Jooyung Han643adc42020-02-27 13:50:06 +09006183func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006184 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006185 apex {
6186 name: "myapex",
6187 key: "myapex.key",
6188 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006189 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006190 }
6191
6192 apex_key {
6193 name: "myapex.key",
6194 public_key: "testkey.avbpubkey",
6195 private_key: "testkey.pem",
6196 }
6197
6198 cc_library {
6199 name: "mylib",
6200 srcs: ["mylib.cpp"],
6201 shared_libs: ["mylib2"],
6202 system_shared_libs: [],
6203 stl: "none",
6204 apex_available: [ "myapex" ],
6205 }
6206
6207 cc_library {
6208 name: "mylib2",
6209 srcs: ["mylib.cpp"],
6210 system_shared_libs: [],
6211 stl: "none",
6212 apex_available: [ "myapex" ],
6213 }
6214 `)
6215
6216 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6217 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6218 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6219 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6220 "lib64/mylib.so",
6221 "lib64/mylib2.so",
6222 })
6223}
6224
Jooyung Han49f67012020-04-17 13:43:10 +09006225func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006226 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006227 apex {
6228 name: "myapex",
6229 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006230 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006231 }
6232 apex_key {
6233 name: "myapex.key",
6234 public_key: "testkey.avbpubkey",
6235 private_key: "testkey.pem",
6236 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006237 `,
6238 android.FixtureModifyConfig(func(config android.Config) {
6239 delete(config.Targets, android.Android)
6240 config.AndroidCommonTarget = android.Target{}
6241 }),
6242 )
Jooyung Han49f67012020-04-17 13:43:10 +09006243
6244 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6245 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6246 }
6247}
6248
Jiyong Parkbd159612020-02-28 15:22:21 +09006249func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006250 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006251 apex {
6252 name: "myapex",
6253 key: "myapex.key",
6254 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006255 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006256 }
6257
6258 apex_key {
6259 name: "myapex.key",
6260 public_key: "testkey.avbpubkey",
6261 private_key: "testkey.pem",
6262 }
6263
6264 android_app {
6265 name: "AppFoo",
6266 srcs: ["foo/bar/MyClass.java"],
6267 sdk_version: "none",
6268 system_modules: "none",
6269 apex_available: [ "myapex" ],
6270 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006271 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006272
Colin Crosscf371cc2020-11-13 11:48:42 -08006273 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006274 content := bundleConfigRule.Args["content"]
6275
6276 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006277 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 +09006278}
6279
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006280func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006281 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006282 apex {
6283 name: "myapex",
6284 key: "myapex.key",
6285 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006286 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006287 }
6288
6289 apex_key {
6290 name: "myapex.key",
6291 public_key: "testkey.avbpubkey",
6292 private_key: "testkey.pem",
6293 }
6294
6295 android_app_set {
6296 name: "AppSet",
6297 set: "AppSet.apks",
6298 }`)
6299 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006300 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006301 content := bundleConfigRule.Args["content"]
6302 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6303 s := mod.Rule("apexRule").Args["copy_commands"]
6304 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6305 if len(copyCmds) != 3 {
6306 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6307 }
6308 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6309 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6310 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6311}
6312
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006313func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006314 ctx := testApex(t, "", android.FixtureModifyMockFS(func(fs android.MockFS) {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006315 bp := `
6316 apex_set {
6317 name: "myapex",
6318 filename: "foo_v2.apex",
6319 sanitized: {
6320 none: { set: "myapex.apks", },
6321 hwaddress: { set: "myapex.hwasan.apks", },
6322 },
6323 }`
6324 fs["Android.bp"] = []byte(bp)
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006325 }),
6326 prepareForTestWithSantitizeHwaddress,
6327 )
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006328
6329 m := ctx.ModuleForTests("myapex", "android_common")
6330 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6331
6332 actual := extractedApex.Inputs
6333 if len(actual) != 1 {
6334 t.Errorf("expected a single input")
6335 }
6336
6337 expected := "myapex.hwasan.apks"
6338 if actual[0].String() != expected {
6339 t.Errorf("expected %s, got %s", expected, actual[0].String())
6340 }
6341}
6342
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006343func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006344 t.Helper()
6345
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006346 bp := `
6347 java_library {
6348 name: "some-updatable-apex-lib",
6349 srcs: ["a.java"],
6350 sdk_version: "current",
6351 apex_available: [
6352 "some-updatable-apex",
6353 ],
6354 }
6355
6356 java_library {
6357 name: "some-non-updatable-apex-lib",
6358 srcs: ["a.java"],
6359 apex_available: [
6360 "some-non-updatable-apex",
6361 ],
6362 }
6363
6364 java_library {
6365 name: "some-platform-lib",
6366 srcs: ["a.java"],
6367 sdk_version: "current",
6368 installable: true,
6369 }
6370
6371 java_library {
6372 name: "some-art-lib",
6373 srcs: ["a.java"],
6374 sdk_version: "current",
6375 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006376 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006377 ],
6378 hostdex: true,
6379 }
6380
6381 apex {
6382 name: "some-updatable-apex",
6383 key: "some-updatable-apex.key",
6384 java_libs: ["some-updatable-apex-lib"],
6385 updatable: true,
6386 min_sdk_version: "current",
6387 }
6388
6389 apex {
6390 name: "some-non-updatable-apex",
6391 key: "some-non-updatable-apex.key",
6392 java_libs: ["some-non-updatable-apex-lib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006393 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006394 }
6395
6396 apex_key {
6397 name: "some-updatable-apex.key",
6398 }
6399
6400 apex_key {
6401 name: "some-non-updatable-apex.key",
6402 }
6403
6404 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006405 name: "com.android.art.debug",
6406 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006407 java_libs: ["some-art-lib"],
6408 updatable: true,
6409 min_sdk_version: "current",
6410 }
6411
6412 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006413 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006414 }
6415
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006416 filegroup {
6417 name: "some-updatable-apex-file_contexts",
6418 srcs: [
6419 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6420 ],
6421 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006422
6423 filegroup {
6424 name: "some-non-updatable-apex-file_contexts",
6425 srcs: [
6426 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6427 ],
6428 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006429 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006430
6431 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6432}
6433
Paul Duffin064b70c2020-11-02 17:32:38 +00006434func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006435 t.Helper()
6436
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006437 bp += cc.GatherRequiredDepsForTest(android.Android)
6438 bp += java.GatherRequiredDepsForTest()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006439
6440 fs := map[string][]byte{
6441 "a.java": nil,
6442 "a.jar": nil,
6443 "build/make/target/product/security": nil,
6444 "apex_manifest.json": nil,
6445 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006446 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006447 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6448 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6449 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006450 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006451 }
6452 cc.GatherRequiredFilesForTest(fs)
6453
Paul Duffin39853512021-02-26 11:09:39 +00006454 for k, v := range filesForSdkLibrary {
6455 fs[k] = v
6456 }
Colin Crossae8600b2020-10-29 17:09:13 -07006457 config := android.TestArchConfig(buildDir, nil, bp, fs)
6458
6459 ctx := android.NewTestArchContext(config)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006460 ctx.RegisterModuleType("apex", BundleFactory)
6461 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Paul Duffin064b70c2020-11-02 17:32:38 +00006462 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006463 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01006464 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin37856732021-02-26 14:24:15 +00006465 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffin021f4e52020-07-30 16:04:17 +01006466 android.RegisterPrebuiltMutators(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006467 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006468 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinf38931c2021-02-05 16:58:28 +00006469 java.RegisterHiddenApiSingletonComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006470 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6471 ctx.PreDepsMutators(RegisterPreDepsMutators)
6472 ctx.PostDepsMutators(RegisterPostDepsMutators)
6473
Colin Crossae8600b2020-10-29 17:09:13 -07006474 ctx.Register()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006475
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006476 pathCtx := android.PathContextForTesting(config)
6477 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6478 transformDexpreoptConfig(dexpreoptConfig)
6479 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6480
Paul Duffinf38931c2021-02-05 16:58:28 +00006481 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
Paul Duffin4fd997b2021-02-03 20:06:33 +00006482 // product variables.
Paul Duffinf38931c2021-02-05 16:58:28 +00006483 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6484 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6485
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006486 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6487 android.FailIfErrored(t, errs)
6488
6489 _, errs = ctx.PrepareBuildActions(config)
6490 if errmsg == "" {
6491 android.FailIfErrored(t, errs)
6492 } else if len(errs) > 0 {
6493 android.FailIfNoMatchingErrors(t, errmsg, errs)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006494 } else {
6495 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6496 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006497
6498 return ctx
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006499}
6500
Jooyung Han548640b2020-04-27 12:10:30 +09006501func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6502 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6503 apex {
6504 name: "myapex",
6505 key: "myapex.key",
6506 updatable: true,
6507 }
6508
6509 apex_key {
6510 name: "myapex.key",
6511 public_key: "testkey.avbpubkey",
6512 private_key: "testkey.pem",
6513 }
6514 `)
6515}
6516
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006517func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6518 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6519 apex {
6520 name: "myapex",
6521 key: "myapex.key",
6522 }
6523
6524 apex_key {
6525 name: "myapex.key",
6526 public_key: "testkey.avbpubkey",
6527 private_key: "testkey.pem",
6528 }
6529 `)
6530}
6531
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006532func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006533 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006534 var transform func(*dexpreopt.GlobalConfig)
6535
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006536 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6537 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006538 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006539 }
6540 testNoUpdatableJarsInBootImage(t, "", transform)
6541 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006542
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006543 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006544 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 +01006545 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006546 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006547 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006548 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006549 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006550
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006551 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 -07006552 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 +01006553 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006554 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006555 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006556 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006557 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006558
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006559 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 -07006560 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006561 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006562 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006563 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006564 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006565 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006566
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006567 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 -07006568 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 +01006569 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006570 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006571 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006572 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006573 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006574
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006575 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6576 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006577 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006578 }
6579 testNoUpdatableJarsInBootImage(t, "", transform)
6580 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006581
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006582 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006583 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006584 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006585 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006586 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006587 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006588 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006589
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006590 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006591 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006592 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006593 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006594 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006595 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006596 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006597
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006598 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006599 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006600 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006601 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006602 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006603 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006604 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006605
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006606 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6607 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006608 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006609 }
6610 testNoUpdatableJarsInBootImage(t, "", transform)
6611 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006612
6613}
6614
6615func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6616 transform := func(config *dexpreopt.GlobalConfig) {
6617 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6618 }
6619 t.Run("prebuilt no source", func(t *testing.T) {
6620 testDexpreoptWithApexes(t, `
6621 prebuilt_apex {
6622 name: "myapex" ,
6623 arch: {
6624 arm64: {
6625 src: "myapex-arm64.apex",
6626 },
6627 arm: {
6628 src: "myapex-arm.apex",
6629 },
6630 },
6631 exported_java_libs: ["libfoo"],
6632 }
6633
6634 java_import {
6635 name: "libfoo",
6636 jars: ["libfoo.jar"],
6637 }
6638`, "", transform)
6639 })
6640
6641 t.Run("prebuilt no source", func(t *testing.T) {
6642 testDexpreoptWithApexes(t, `
6643 prebuilt_apex {
6644 name: "myapex" ,
6645 arch: {
6646 arm64: {
6647 src: "myapex-arm64.apex",
6648 },
6649 arm: {
6650 src: "myapex-arm.apex",
6651 },
6652 },
6653 exported_java_libs: ["libfoo"],
6654 }
6655
6656 java_import {
6657 name: "libfoo",
6658 jars: ["libfoo.jar"],
6659 }
6660`, "", transform)
6661 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006662}
6663
Andrei Onea115e7e72020-06-05 21:14:03 +01006664func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6665 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006666 bp += `
6667 apex_key {
6668 name: "myapex.key",
6669 public_key: "testkey.avbpubkey",
6670 private_key: "testkey.pem",
6671 }`
6672 fs := map[string][]byte{
6673 "lib1/src/A.java": nil,
6674 "lib2/src/B.java": nil,
6675 "system/sepolicy/apex/myapex-file_contexts": nil,
6676 }
6677
Colin Crossae8600b2020-10-29 17:09:13 -07006678 config := android.TestArchConfig(buildDir, nil, bp, fs)
6679 android.SetTestNeverallowRules(config, rules)
6680 updatableBootJars := make([]string, 0, len(apexBootJars))
6681 for _, apexBootJar := range apexBootJars {
6682 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6683 }
6684 config.TestProductVariables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6685
6686 ctx := android.NewTestArchContext(config)
Andrei Onea115e7e72020-06-05 21:14:03 +01006687 ctx.RegisterModuleType("apex", BundleFactory)
6688 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
6689 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
6690 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006691 java.RegisterRequiredBuildComponentsForTest(ctx)
Andrei Onea115e7e72020-06-05 21:14:03 +01006692 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6693 ctx.PreDepsMutators(RegisterPreDepsMutators)
6694 ctx.PostDepsMutators(RegisterPostDepsMutators)
6695 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
6696
Colin Crossae8600b2020-10-29 17:09:13 -07006697 ctx.Register()
Andrei Onea115e7e72020-06-05 21:14:03 +01006698
6699 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6700 android.FailIfErrored(t, errs)
6701
6702 _, errs = ctx.PrepareBuildActions(config)
6703 if errmsg == "" {
6704 android.FailIfErrored(t, errs)
6705 } else if len(errs) > 0 {
6706 android.FailIfNoMatchingErrors(t, errmsg, errs)
6707 return
6708 } else {
6709 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6710 }
6711}
6712
6713func TestApexPermittedPackagesRules(t *testing.T) {
6714 testcases := []struct {
6715 name string
6716 expectedError string
6717 bp string
6718 bootJars []string
6719 modulesPackages map[string][]string
6720 }{
6721
6722 {
6723 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6724 expectedError: "",
6725 bp: `
6726 java_library {
6727 name: "bcp_lib1",
6728 srcs: ["lib1/src/*.java"],
6729 permitted_packages: ["foo.bar"],
6730 apex_available: ["myapex"],
6731 sdk_version: "none",
6732 system_modules: "none",
6733 }
6734 java_library {
6735 name: "nonbcp_lib2",
6736 srcs: ["lib2/src/*.java"],
6737 apex_available: ["myapex"],
6738 permitted_packages: ["a.b"],
6739 sdk_version: "none",
6740 system_modules: "none",
6741 }
6742 apex {
6743 name: "myapex",
6744 key: "myapex.key",
6745 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006746 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006747 }`,
6748 bootJars: []string{"bcp_lib1"},
6749 modulesPackages: map[string][]string{
6750 "myapex": []string{
6751 "foo.bar",
6752 },
6753 },
6754 },
6755 {
6756 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6757 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.`,
6758 bp: `
6759 java_library {
6760 name: "bcp_lib1",
6761 srcs: ["lib1/src/*.java"],
6762 apex_available: ["myapex"],
6763 permitted_packages: ["foo.bar"],
6764 sdk_version: "none",
6765 system_modules: "none",
6766 }
6767 java_library {
6768 name: "bcp_lib2",
6769 srcs: ["lib2/src/*.java"],
6770 apex_available: ["myapex"],
6771 permitted_packages: ["foo.bar", "bar.baz"],
6772 sdk_version: "none",
6773 system_modules: "none",
6774 }
6775 apex {
6776 name: "myapex",
6777 key: "myapex.key",
6778 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006779 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006780 }
6781 `,
6782 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6783 modulesPackages: map[string][]string{
6784 "myapex": []string{
6785 "foo.bar",
6786 },
6787 },
6788 },
6789 }
6790 for _, tc := range testcases {
6791 t.Run(tc.name, func(t *testing.T) {
6792 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6793 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6794 })
6795 }
6796}
6797
Jiyong Park62304bb2020-04-13 16:19:48 +09006798func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006799 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006800 apex {
6801 name: "myapex",
6802 key: "myapex.key",
6803 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006804 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006805 }
6806
6807 apex_key {
6808 name: "myapex.key",
6809 public_key: "testkey.avbpubkey",
6810 private_key: "testkey.pem",
6811 }
6812
6813 cc_library {
6814 name: "mylib",
6815 srcs: ["mylib.cpp"],
6816 system_shared_libs: [],
6817 stl: "none",
6818 stubs: {
6819 versions: ["1"],
6820 },
6821 apex_available: ["myapex"],
6822 }
6823
6824 cc_library {
6825 name: "myprivlib",
6826 srcs: ["mylib.cpp"],
6827 system_shared_libs: [],
6828 stl: "none",
6829 apex_available: ["myapex"],
6830 }
6831
6832
6833 cc_test {
6834 name: "mytest",
6835 gtest: false,
6836 srcs: ["mylib.cpp"],
6837 system_shared_libs: [],
6838 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006839 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006840 test_for: ["myapex"]
6841 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006842
6843 cc_library {
6844 name: "mytestlib",
6845 srcs: ["mylib.cpp"],
6846 system_shared_libs: [],
6847 shared_libs: ["mylib", "myprivlib"],
6848 stl: "none",
6849 test_for: ["myapex"],
6850 }
6851
6852 cc_benchmark {
6853 name: "mybench",
6854 srcs: ["mylib.cpp"],
6855 system_shared_libs: [],
6856 shared_libs: ["mylib", "myprivlib"],
6857 stl: "none",
6858 test_for: ["myapex"],
6859 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006860 `)
6861
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006862 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
6863 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").RelativeToTop().Args["libFlags"], " ")
6864 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6865 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6866 }
6867
6868 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09006869 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006870 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6871 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6872 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
6873}
Jiyong Park46a512f2020-12-04 18:02:13 +09006874
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006875func TestIndirectTestFor(t *testing.T) {
6876 ctx := testApex(t, `
6877 apex {
6878 name: "myapex",
6879 key: "myapex.key",
6880 native_shared_libs: ["mylib", "myprivlib"],
6881 updatable: false,
6882 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006883
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00006884 apex_key {
6885 name: "myapex.key",
6886 public_key: "testkey.avbpubkey",
6887 private_key: "testkey.pem",
6888 }
6889
6890 cc_library {
6891 name: "mylib",
6892 srcs: ["mylib.cpp"],
6893 system_shared_libs: [],
6894 stl: "none",
6895 stubs: {
6896 versions: ["1"],
6897 },
6898 apex_available: ["myapex"],
6899 }
6900
6901 cc_library {
6902 name: "myprivlib",
6903 srcs: ["mylib.cpp"],
6904 system_shared_libs: [],
6905 stl: "none",
6906 shared_libs: ["mylib"],
6907 apex_available: ["myapex"],
6908 }
6909
6910 cc_library {
6911 name: "mytestlib",
6912 srcs: ["mylib.cpp"],
6913 system_shared_libs: [],
6914 shared_libs: ["myprivlib"],
6915 stl: "none",
6916 test_for: ["myapex"],
6917 }
6918 `)
6919
6920 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
6921 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").RelativeToTop().Args["libFlags"], " ")
6922 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
6923 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
6924 }
6925
6926 // The platform variant of mytestlib links to the platform variant of the
6927 // internal myprivlib.
6928 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
6929
6930 // The platform variant of myprivlib links to the platform variant of mylib
6931 // and bypasses its stubs.
6932 ensureLinkedLibIs("myprivlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09006933}
6934
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006935// TODO(jungjw): Move this to proptools
6936func intPtr(i int) *int {
6937 return &i
6938}
6939
6940func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006941 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006942 apex_set {
6943 name: "myapex",
6944 set: "myapex.apks",
6945 filename: "foo_v2.apex",
6946 overrides: ["foo"],
6947 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006948 `,
6949 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6950 variables.Platform_sdk_version = intPtr(30)
6951 }),
6952 android.FixtureModifyConfig(func(config android.Config) {
6953 config.Targets[android.Android] = []android.Target{
6954 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
6955 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
6956 }
6957 }),
6958 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006959
6960 m := ctx.ModuleForTests("myapex", "android_common")
6961
6962 // Check extract_apks tool parameters.
6963 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6964 actual := extractedApex.Args["abis"]
6965 expected := "ARMEABI_V7A,ARM64_V8A"
6966 if actual != expected {
6967 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6968 }
6969 actual = extractedApex.Args["sdk-version"]
6970 expected = "30"
6971 if actual != expected {
6972 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6973 }
6974
6975 a := m.Module().(*ApexSet)
6976 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07006977 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006978 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
6979 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
6980 }
6981}
6982
Jiyong Park7d95a512020-05-10 15:16:24 +09006983func TestNoStaticLinkingToStubsLib(t *testing.T) {
6984 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
6985 apex {
6986 name: "myapex",
6987 key: "myapex.key",
6988 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006989 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09006990 }
6991
6992 apex_key {
6993 name: "myapex.key",
6994 public_key: "testkey.avbpubkey",
6995 private_key: "testkey.pem",
6996 }
6997
6998 cc_library {
6999 name: "mylib",
7000 srcs: ["mylib.cpp"],
7001 static_libs: ["otherlib"],
7002 system_shared_libs: [],
7003 stl: "none",
7004 apex_available: [ "myapex" ],
7005 }
7006
7007 cc_library {
7008 name: "otherlib",
7009 srcs: ["mylib.cpp"],
7010 system_shared_libs: [],
7011 stl: "none",
7012 stubs: {
7013 versions: ["1", "2", "3"],
7014 },
7015 apex_available: [ "myapex" ],
7016 }
7017 `)
7018}
7019
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007020func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007021 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007022 apex {
7023 name: "myapex",
7024 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007025 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007026 }
7027
7028 apex_key {
7029 name: "myapex.key",
7030 public_key: "testkey.avbpubkey",
7031 private_key: "testkey.pem",
7032 }
7033
7034 prebuilt_apex {
7035 name: "myapex",
7036 prefer: true,
7037 arch: {
7038 arm64: {
7039 src: "myapex-arm64.apex",
7040 },
7041 arm: {
7042 src: "myapex-arm.apex",
7043 },
7044 },
7045 }
7046
7047 apex_set {
7048 name: "myapex_set",
7049 set: "myapex.apks",
7050 filename: "myapex_set.apex",
7051 overrides: ["myapex"],
7052 }
7053 `)
7054
7055 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7056 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7057 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 +09007058 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 +09007059}
7060
Jooyung Han938b5932020-06-20 12:47:47 +09007061func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007062 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007063 apex {
7064 name: "myapex",
7065 key: "myapex.key",
7066 apps: ["app"],
7067 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007068 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007069 }
7070
7071 apex_key {
7072 name: "myapex.key",
7073 public_key: "testkey.avbpubkey",
7074 private_key: "testkey.pem",
7075 }
7076
7077 android_app {
7078 name: "app",
7079 srcs: ["foo/bar/MyClass.java"],
7080 package_name: "foo",
7081 sdk_version: "none",
7082 system_modules: "none",
7083 apex_available: [ "myapex" ],
7084 }
7085 `, withFiles(map[string][]byte{
7086 "sub/Android.bp": []byte(`
7087 override_apex {
7088 name: "override_myapex",
7089 base: "myapex",
7090 apps: ["override_app"],
7091 allowed_files: ":allowed",
7092 }
7093 // Overridable "path" property should be referenced indirectly
7094 filegroup {
7095 name: "allowed",
7096 srcs: ["allowed.txt"],
7097 }
7098 override_android_app {
7099 name: "override_app",
7100 base: "app",
7101 package_name: "bar",
7102 }
7103 `),
7104 }))
7105
7106 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7107 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7108 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7109 }
7110
7111 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7112 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7113 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7114 }
7115}
7116
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007117func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007118 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007119 apex {
7120 name: "myapex",
7121 key: "myapex.key",
7122 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007123 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007124 }
7125
7126 apex_key {
7127 name: "myapex.key",
7128 public_key: "testkey.avbpubkey",
7129 private_key: "testkey.pem",
7130 }
7131
7132 cc_library {
7133 name: "mylib",
7134 srcs: ["mylib.cpp"],
7135 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007136 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007137 },
7138 apex_available: ["myapex"],
7139 }
7140
7141 cc_prebuilt_library_shared {
7142 name: "mylib",
7143 prefer: false,
7144 srcs: ["prebuilt.so"],
7145 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007146 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007147 },
7148 apex_available: ["myapex"],
7149 }
7150 `)
7151}
7152
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007153func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007154 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007155 apex {
7156 name: "myapex",
7157 key: "myapex.key",
7158 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007159 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007160 }
7161 apex_key {
7162 name: "myapex.key",
7163 public_key: "testkey.avbpubkey",
7164 private_key: "testkey.pem",
7165 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007166 `,
7167 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7168 variables.CompressedApex = proptools.BoolPtr(true)
7169 }),
7170 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007171
7172 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7173 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7174
7175 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7176 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7177
7178 // Make sure output of bundle is .capex
7179 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7180 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7181
7182 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007183 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007184 var builder strings.Builder
7185 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7186 androidMk := builder.String()
7187 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7188}
7189
Martin Stjernholm2856c662020-12-02 15:03:42 +00007190func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007191 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007192 apex {
7193 name: "myapex",
7194 key: "myapex.key",
7195 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007196 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007197 }
7198
7199 apex_key {
7200 name: "myapex.key",
7201 public_key: "testkey.avbpubkey",
7202 private_key: "testkey.pem",
7203 }
7204
7205 cc_library {
7206 name: "mylib",
7207 srcs: ["mylib.cpp"],
7208 apex_available: ["myapex"],
7209 shared_libs: ["otherlib"],
7210 system_shared_libs: [],
7211 }
7212
7213 cc_library {
7214 name: "otherlib",
7215 srcs: ["mylib.cpp"],
7216 stubs: {
7217 versions: ["current"],
7218 },
7219 }
7220
7221 cc_prebuilt_library_shared {
7222 name: "otherlib",
7223 prefer: true,
7224 srcs: ["prebuilt.so"],
7225 stubs: {
7226 versions: ["current"],
7227 },
7228 }
7229 `)
7230
7231 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007232 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007233 var builder strings.Builder
7234 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7235 androidMk := builder.String()
7236
7237 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7238 // a thing there.
7239 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7240}
7241
Jiyong Parke3867542020-12-03 17:28:25 +09007242func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007243 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007244 apex {
7245 name: "myapex",
7246 key: "myapex.key",
7247 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007248 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007249 }
7250
7251 apex_key {
7252 name: "myapex.key",
7253 public_key: "testkey.avbpubkey",
7254 private_key: "testkey.pem",
7255 }
7256
7257 cc_library {
7258 name: "mylib",
7259 srcs: ["mylib.cpp"],
7260 system_shared_libs: [],
7261 stl: "none",
7262 apex_available: ["myapex"],
7263 shared_libs: ["mylib2"],
7264 target: {
7265 apex: {
7266 exclude_shared_libs: ["mylib2"],
7267 },
7268 },
7269 }
7270
7271 cc_library {
7272 name: "mylib2",
7273 srcs: ["mylib.cpp"],
7274 system_shared_libs: [],
7275 stl: "none",
7276 }
7277 `)
7278
7279 // Check if mylib is linked to mylib2 for the non-apex target
7280 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7281 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7282
7283 // Make sure that the link doesn't occur for the apex target
7284 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7285 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7286
7287 // It shouldn't appear in the copy cmd as well.
7288 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7289 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7290}
7291
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007292func TestPrebuiltStubLibDep(t *testing.T) {
7293 bpBase := `
7294 apex {
7295 name: "myapex",
7296 key: "myapex.key",
7297 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007298 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007299 }
7300 apex_key {
7301 name: "myapex.key",
7302 public_key: "testkey.avbpubkey",
7303 private_key: "testkey.pem",
7304 }
7305 cc_library {
7306 name: "mylib",
7307 srcs: ["mylib.cpp"],
7308 apex_available: ["myapex"],
7309 shared_libs: ["stublib"],
7310 system_shared_libs: [],
7311 }
7312 apex {
7313 name: "otherapex",
7314 enabled: %s,
7315 key: "myapex.key",
7316 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007317 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007318 }
7319 `
7320
7321 stublibSourceBp := `
7322 cc_library {
7323 name: "stublib",
7324 srcs: ["mylib.cpp"],
7325 apex_available: ["otherapex"],
7326 system_shared_libs: [],
7327 stl: "none",
7328 stubs: {
7329 versions: ["1"],
7330 },
7331 }
7332 `
7333
7334 stublibPrebuiltBp := `
7335 cc_prebuilt_library_shared {
7336 name: "stublib",
7337 srcs: ["prebuilt.so"],
7338 apex_available: ["otherapex"],
7339 stubs: {
7340 versions: ["1"],
7341 },
7342 %s
7343 }
7344 `
7345
7346 tests := []struct {
7347 name string
7348 stublibBp string
7349 usePrebuilt bool
7350 modNames []string // Modules to collect AndroidMkEntries for
7351 otherApexEnabled []string
7352 }{
7353 {
7354 name: "only_source",
7355 stublibBp: stublibSourceBp,
7356 usePrebuilt: false,
7357 modNames: []string{"stublib"},
7358 otherApexEnabled: []string{"true", "false"},
7359 },
7360 {
7361 name: "source_preferred",
7362 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7363 usePrebuilt: false,
7364 modNames: []string{"stublib", "prebuilt_stublib"},
7365 otherApexEnabled: []string{"true", "false"},
7366 },
7367 {
7368 name: "prebuilt_preferred",
7369 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7370 usePrebuilt: true,
7371 modNames: []string{"stublib", "prebuilt_stublib"},
7372 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7373 },
7374 {
7375 name: "only_prebuilt",
7376 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7377 usePrebuilt: true,
7378 modNames: []string{"stublib"},
7379 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7380 },
7381 }
7382
7383 for _, test := range tests {
7384 t.Run(test.name, func(t *testing.T) {
7385 for _, otherApexEnabled := range test.otherApexEnabled {
7386 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007387 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007388
7389 type modAndMkEntries struct {
7390 mod *cc.Module
7391 mkEntries android.AndroidMkEntries
7392 }
7393 entries := []*modAndMkEntries{}
7394
7395 // Gather shared lib modules that are installable
7396 for _, modName := range test.modNames {
7397 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7398 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7399 continue
7400 }
7401 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007402 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007403 continue
7404 }
Colin Crossaa255532020-07-03 13:18:24 -07007405 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007406 if ent.Disabled {
7407 continue
7408 }
7409 entries = append(entries, &modAndMkEntries{
7410 mod: mod,
7411 mkEntries: ent,
7412 })
7413 }
7414 }
7415 }
7416
7417 var entry *modAndMkEntries = nil
7418 for _, ent := range entries {
7419 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7420 if entry != nil {
7421 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7422 } else {
7423 entry = ent
7424 }
7425 }
7426 }
7427
7428 if entry == nil {
7429 t.Errorf("AndroidMk entry for \"stublib\" missing")
7430 } else {
7431 isPrebuilt := entry.mod.Prebuilt() != nil
7432 if isPrebuilt != test.usePrebuilt {
7433 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7434 }
7435 if !entry.mod.IsStubs() {
7436 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7437 }
7438 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7439 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7440 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007441 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
7442 expected := "-D__STUBLIB_API__=1"
7443 if !android.InList(expected, cflags) {
7444 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7445 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007446 }
7447 })
7448 }
7449 })
7450 }
7451}
7452
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007453func TestMain(m *testing.M) {
7454 run := func() int {
7455 setUp()
7456 defer tearDown()
7457
7458 return m.Run()
7459 }
7460
7461 os.Exit(run())
7462}