blob: 1006a62c1937c77f4dcdfd92699d9b02498458b1 [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 Duffin2be9dcd2021-03-09 13:18:10 +000053func testApexError(t *testing.T, pattern, bp string, handlers ...interface{}) {
Jooyung Han344d5432019-08-23 11:17:39 +090054 t.Helper()
Paul Duffine05480a2021-03-08 15:07:14 +000055 testApexFixtureFactory(bp, handlers).
56 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
57 RunTest(t)
Jooyung Han5c998b92019-06-27 11:30:33 +090058}
59
Paul Duffin2be9dcd2021-03-09 13:18:10 +000060func testApex(t *testing.T, bp string, handlers ...interface{}) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090061 t.Helper()
Paul Duffine05480a2021-03-08 15:07:14 +000062 result := testApexFixtureFactory(bp, handlers).RunTest(t)
63 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090064}
65
Paul Duffine05480a2021-03-08 15:07:14 +000066// apex package specific mechanism for customizing the test configuration.
67//
68// Use FixturePreparer instances instead.
69//
70// deprecated
Jooyung Han344d5432019-08-23 11:17:39 +090071type testCustomizer func(fs map[string][]byte, config android.Config)
72
73func withFiles(files map[string][]byte) testCustomizer {
74 return func(fs map[string][]byte, config android.Config) {
75 for k, v := range files {
76 fs[k] = v
77 }
78 }
79}
80
81func withTargets(targets map[android.OsType][]android.Target) testCustomizer {
82 return func(fs map[string][]byte, config android.Config) {
83 for k, v := range targets {
84 config.Targets[k] = v
85 }
86 }
87}
88
Jooyung Han35155c42020-02-06 17:33:20 +090089// withNativeBridgeTargets sets configuration with targets including:
90// - X86_64 (primary)
91// - X86 (secondary)
92// - Arm64 on X86_64 (native bridge)
93// - Arm on X86 (native bridge)
Sasha Smundak18d98bc2020-05-27 16:36:07 -070094func withNativeBridgeEnabled(_ map[string][]byte, config android.Config) {
Jooyung Han35155c42020-02-06 17:33:20 +090095 config.Targets[android.Android] = []android.Target{
96 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
97 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
101 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
104 }
105}
106
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900107func withManifestPackageNameOverrides(specs []string) testCustomizer {
108 return func(fs map[string][]byte, config android.Config) {
109 config.TestProductVariables.ManifestPackageNameOverrides = specs
110 }
111}
112
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700113func withBinder32bit(_ map[string][]byte, config android.Config) {
Jooyung Han31c470b2019-10-18 16:26:59 +0900114 config.TestProductVariables.Binder32bit = proptools.BoolPtr(true)
115}
116
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700117func withUnbundledBuild(_ map[string][]byte, config android.Config) {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900118 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
119}
120
Paul Duffina02cae32021-03-09 01:44:06 +0000121var emptyFixtureFactory = android.NewFixtureFactory(&buildDir)
122
Paul Duffin37aad602021-03-08 09:47:16 +0000123var apexFixtureFactory = android.NewFixtureFactory(
124 &buildDir,
125 // General preparers in alphabetical order as test infrastructure will enforce correct
126 // registration order.
127 android.PrepareForTestWithAndroidBuildComponents,
128 bpf.PrepareForTestWithBpf,
129 cc.PrepareForTestWithCcBuildComponents,
130 java.PrepareForTestWithJavaDefaultModules,
131 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
132 rust.PrepareForTestWithRustDefaultModules,
133 sh.PrepareForTestWithShBuildComponents,
134
135 PrepareForTestWithApexBuildComponents,
136
137 // Additional apex test specific preparers.
138 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
139 filegroup {
140 name: "myapex-file_contexts",
141 srcs: [
142 "apex/myapex-file_contexts",
143 ],
144 }
145 `),
146 android.FixtureMergeMockFs(android.MockFS{
147 "a.java": nil,
148 "PrebuiltAppFoo.apk": nil,
149 "PrebuiltAppFooPriv.apk": nil,
150 "build/make/target/product/security": nil,
151 "apex_manifest.json": nil,
152 "AndroidManifest.xml": nil,
153 "system/sepolicy/apex/myapex-file_contexts": nil,
154 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
155 "system/sepolicy/apex/myapex2-file_contexts": nil,
156 "system/sepolicy/apex/otherapex-file_contexts": nil,
157 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
158 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
159 "mylib.cpp": nil,
160 "mytest.cpp": nil,
161 "mytest1.cpp": nil,
162 "mytest2.cpp": nil,
163 "mytest3.cpp": nil,
164 "myprebuilt": nil,
165 "my_include": nil,
166 "foo/bar/MyClass.java": nil,
167 "prebuilt.jar": nil,
168 "prebuilt.so": nil,
169 "vendor/foo/devkeys/test.x509.pem": nil,
170 "vendor/foo/devkeys/test.pk8": nil,
171 "testkey.x509.pem": nil,
172 "testkey.pk8": nil,
173 "testkey.override.x509.pem": nil,
174 "testkey.override.pk8": nil,
175 "vendor/foo/devkeys/testkey.avbpubkey": nil,
176 "vendor/foo/devkeys/testkey.pem": nil,
177 "NOTICE": nil,
178 "custom_notice": nil,
179 "custom_notice_for_static_lib": nil,
180 "testkey2.avbpubkey": nil,
181 "testkey2.pem": nil,
182 "myapex-arm64.apex": nil,
183 "myapex-arm.apex": nil,
184 "myapex.apks": nil,
185 "frameworks/base/api/current.txt": nil,
186 "framework/aidl/a.aidl": nil,
187 "build/make/core/proguard.flags": nil,
188 "build/make/core/proguard_basic_keeps.flags": nil,
189 "dummy.txt": nil,
190 "baz": nil,
191 "bar/baz": nil,
192 "testdata/baz": nil,
193 "AppSet.apks": nil,
194 "foo.rs": nil,
195 "libfoo.jar": nil,
196 "libbar.jar": nil,
197 },
198 ),
199
200 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
201 variables.DeviceVndkVersion = proptools.StringPtr("current")
202 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
203 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
204 variables.Platform_sdk_codename = proptools.StringPtr("Q")
205 variables.Platform_sdk_final = proptools.BoolPtr(false)
206 variables.Platform_version_active_codenames = []string{"Q"}
207 variables.Platform_vndk_version = proptools.StringPtr("VER")
208 }),
209)
210
Paul Duffin2be9dcd2021-03-09 13:18:10 +0000211func testApexFixtureFactory(bp string, handlers []interface{}) android.FixtureFactory {
212 var preparers []android.FixturePreparer
213 for _, handler := range handlers {
214 var preparer android.FixturePreparer
215 if p, ok := handler.(android.FixturePreparer); ok {
216 preparer = p
217 } else {
218 var customizer testCustomizer
219 if c, ok := handler.(testCustomizer); ok {
220 customizer = c
221 } else {
222 customizer = handler.(func(fs map[string][]byte, config android.Config))
Paul Duffine05480a2021-03-08 15:07:14 +0000223 }
Paul Duffin2be9dcd2021-03-09 13:18:10 +0000224 preparer = android.FixtureCustomPreparer(func(fixture android.Fixture) {
225 customizer(fixture.MockFS(), fixture.Config())
226 })
227 }
228 preparers = append(preparers, preparer)
229 }
230 factory := apexFixtureFactory.Extend(preparers...)
Paul Duffine05480a2021-03-08 15:07:14 +0000231 if bp != "" {
232 factory = factory.Extend(android.FixtureWithRootAndroidBp(bp))
Jooyung Han344d5432019-08-23 11:17:39 +0900233 }
Paul Duffine05480a2021-03-08 15:07:14 +0000234 return factory
Jiyong Park25fc6a92018-11-18 18:02:45 +0900235}
236
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700237func setUp() {
238 var err error
239 buildDir, err = ioutil.TempDir("", "soong_apex_test")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900240 if err != nil {
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700241 panic(err)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900243}
244
Jaewoong Jungc1001ec2019-06-25 11:20:53 -0700245func tearDown() {
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700246 _ = os.RemoveAll(buildDir)
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247}
248
Jooyung Han643adc42020-02-27 13:50:06 +0900249// ensure that 'result' equals 'expected'
250func ensureEquals(t *testing.T, result string, expected string) {
251 t.Helper()
252 if result != expected {
253 t.Errorf("%q != %q", expected, result)
254 }
255}
256
Jiyong Park25fc6a92018-11-18 18:02:45 +0900257// ensure that 'result' contains 'expected'
258func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900259 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900260 if !strings.Contains(result, expected) {
261 t.Errorf("%q is not found in %q", expected, result)
262 }
263}
264
Liz Kammer5bd365f2020-05-27 15:15:11 -0700265// ensure that 'result' contains 'expected' exactly one time
266func ensureContainsOnce(t *testing.T, result string, expected string) {
267 t.Helper()
268 count := strings.Count(result, expected)
269 if count != 1 {
270 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
271 }
272}
273
Jiyong Park25fc6a92018-11-18 18:02:45 +0900274// ensures that 'result' does not contain 'notExpected'
275func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900276 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900277 if strings.Contains(result, notExpected) {
278 t.Errorf("%q is found in %q", notExpected, result)
279 }
280}
281
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700282func ensureMatches(t *testing.T, result string, expectedRex string) {
283 ok, err := regexp.MatchString(expectedRex, result)
284 if err != nil {
285 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
286 return
287 }
288 if !ok {
289 t.Errorf("%s does not match regular expession %s", result, expectedRex)
290 }
291}
292
Jiyong Park25fc6a92018-11-18 18:02:45 +0900293func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900294 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900295 if !android.InList(expected, result) {
296 t.Errorf("%q is not found in %v", expected, result)
297 }
298}
299
300func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900301 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900302 if android.InList(notExpected, result) {
303 t.Errorf("%q is found in %v", notExpected, result)
304 }
305}
306
Jooyung Hane1633032019-08-01 17:41:43 +0900307func ensureListEmpty(t *testing.T, result []string) {
308 t.Helper()
309 if len(result) > 0 {
310 t.Errorf("%q is expected to be empty", result)
311 }
312}
313
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000314func ensureListNotEmpty(t *testing.T, result []string) {
315 t.Helper()
316 if len(result) == 0 {
317 t.Errorf("%q is expected to be not empty", result)
318 }
319}
320
Jiyong Park25fc6a92018-11-18 18:02:45 +0900321// Minimal test
322func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800323 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900324 apex_defaults {
325 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900326 manifest: ":myapex.manifest",
327 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900328 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900329 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900330 native_shared_libs: [
331 "mylib",
332 "libfoo.ffi",
333 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900334 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800335 multilib: {
336 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900337 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800338 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900339 },
Jiyong Park77acec62020-06-01 21:39:15 +0900340 java_libs: [
341 "myjar",
342 "myjar_dex",
343 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000344 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900345 }
346
Jiyong Park30ca9372019-02-07 16:27:23 +0900347 apex {
348 name: "myapex",
349 defaults: ["myapex-defaults"],
350 }
351
Jiyong Park25fc6a92018-11-18 18:02:45 +0900352 apex_key {
353 name: "myapex.key",
354 public_key: "testkey.avbpubkey",
355 private_key: "testkey.pem",
356 }
357
Jiyong Park809bb722019-02-13 21:33:49 +0900358 filegroup {
359 name: "myapex.manifest",
360 srcs: ["apex_manifest.json"],
361 }
362
363 filegroup {
364 name: "myapex.androidmanifest",
365 srcs: ["AndroidManifest.xml"],
366 }
367
Jiyong Park25fc6a92018-11-18 18:02:45 +0900368 cc_library {
369 name: "mylib",
370 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900371 shared_libs: [
372 "mylib2",
373 "libbar.ffi",
374 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900375 system_shared_libs: [],
376 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000377 // TODO: remove //apex_available:platform
378 apex_available: [
379 "//apex_available:platform",
380 "myapex",
381 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900382 }
383
Alex Light3d673592019-01-18 14:37:31 -0800384 cc_binary {
385 name: "foo",
386 srcs: ["mylib.cpp"],
387 compile_multilib: "both",
388 multilib: {
389 lib32: {
390 suffix: "32",
391 },
392 lib64: {
393 suffix: "64",
394 },
395 },
396 symlinks: ["foo_link_"],
397 symlink_preferred_arch: true,
398 system_shared_libs: [],
399 static_executable: true,
400 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700401 apex_available: [ "myapex", "com.android.gki.*" ],
402 }
403
Jiyong Park99644e92020-11-17 22:21:02 +0900404 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000405 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900406 srcs: ["foo.rs"],
407 rlibs: ["libfoo.rlib.rust"],
408 dylibs: ["libfoo.dylib.rust"],
409 apex_available: ["myapex"],
410 }
411
412 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000413 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900414 srcs: ["foo.rs"],
415 crate_name: "foo",
416 apex_available: ["myapex"],
417 }
418
419 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000420 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900421 srcs: ["foo.rs"],
422 crate_name: "foo",
423 apex_available: ["myapex"],
424 }
425
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900426 rust_ffi_shared {
427 name: "libfoo.ffi",
428 srcs: ["foo.rs"],
429 crate_name: "foo",
430 apex_available: ["myapex"],
431 }
432
433 rust_ffi_shared {
434 name: "libbar.ffi",
435 srcs: ["foo.rs"],
436 crate_name: "bar",
437 apex_available: ["myapex"],
438 }
439
Yifan Hongd22a84a2020-07-28 17:37:46 -0700440 apex {
441 name: "com.android.gki.fake",
442 binaries: ["foo"],
443 key: "myapex.key",
444 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000445 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800446 }
447
Paul Duffindddd5462020-04-07 15:25:44 +0100448 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900449 name: "mylib2",
450 srcs: ["mylib.cpp"],
451 system_shared_libs: [],
452 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900453 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900454 static_libs: ["libstatic"],
455 // TODO: remove //apex_available:platform
456 apex_available: [
457 "//apex_available:platform",
458 "myapex",
459 ],
460 }
461
Paul Duffindddd5462020-04-07 15:25:44 +0100462 cc_prebuilt_library_shared {
463 name: "mylib2",
464 srcs: ["prebuilt.so"],
465 // TODO: remove //apex_available:platform
466 apex_available: [
467 "//apex_available:platform",
468 "myapex",
469 ],
470 }
471
Jiyong Park9918e1a2020-03-17 19:16:40 +0900472 cc_library_static {
473 name: "libstatic",
474 srcs: ["mylib.cpp"],
475 system_shared_libs: [],
476 stl: "none",
477 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000478 // TODO: remove //apex_available:platform
479 apex_available: [
480 "//apex_available:platform",
481 "myapex",
482 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900483 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484
485 java_library {
486 name: "myjar",
487 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900488 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900489 sdk_version: "none",
490 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900491 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900492 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000493 // TODO: remove //apex_available:platform
494 apex_available: [
495 "//apex_available:platform",
496 "myapex",
497 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900498 }
499
Jiyong Park77acec62020-06-01 21:39:15 +0900500 dex_import {
501 name: "myjar_dex",
502 jars: ["prebuilt.jar"],
503 apex_available: [
504 "//apex_available:platform",
505 "myapex",
506 ],
507 }
508
Jiyong Park7f7766d2019-07-25 22:02:35 +0900509 java_library {
510 name: "myotherjar",
511 srcs: ["foo/bar/MyClass.java"],
512 sdk_version: "none",
513 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900514 // TODO: remove //apex_available:platform
515 apex_available: [
516 "//apex_available:platform",
517 "myapex",
518 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900519 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900520
521 java_library {
522 name: "mysharedjar",
523 srcs: ["foo/bar/MyClass.java"],
524 sdk_version: "none",
525 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900526 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900527 `)
528
Sundong Ahnabb64432019-10-22 13:58:29 +0900529 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900530
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900531 // Make sure that Android.mk is created
532 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700533 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900534 var builder strings.Builder
535 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
536
537 androidMk := builder.String()
538 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
539 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
540
Jiyong Park42cca6c2019-04-01 11:15:50 +0900541 optFlags := apexRule.Args["opt_flags"]
542 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700543 // Ensure that the NOTICE output is being packaged as an asset.
Sundong Ahnabb64432019-10-22 13:58:29 +0900544 ensureContains(t, optFlags, "--assets_dir "+buildDir+"/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900545
Jiyong Park25fc6a92018-11-18 18:02:45 +0900546 copyCmds := apexRule.Args["copy_commands"]
547
548 // Ensure that main rule creates an output
549 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
550
551 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700552 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
553 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
554 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900556 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900557
558 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700559 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
560 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900561 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
562 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900563 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900564
565 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800566 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
567 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900568 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900569 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900570 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900571 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
572 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900573 // .. but not for java libs
574 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900575 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800576
Colin Cross7113d202019-11-20 16:39:12 -0800577 // Ensure that the platform variant ends with _shared or _common
578 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
579 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900580 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
581 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900582 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
583
584 // Ensure that dynamic dependency to java libs are not included
585 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800586
587 // Ensure that all symlinks are present.
588 found_foo_link_64 := false
589 found_foo := false
590 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900591 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800592 if strings.HasSuffix(cmd, "bin/foo") {
593 found_foo = true
594 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
595 found_foo_link_64 = true
596 }
597 }
598 }
599 good := found_foo && found_foo_link_64
600 if !good {
601 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
602 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900603
Sundong Ahnabb64432019-10-22 13:58:29 +0900604 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700605 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900606 if len(noticeInputs) != 3 {
607 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900608 }
609 ensureListContains(t, noticeInputs, "NOTICE")
610 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900611 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900612
Artur Satayeva8bd1132020-04-27 18:07:06 +0100613 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100614 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100615 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
616 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
617 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100618
619 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100620 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100621 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
622 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
623 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800624}
625
Jooyung Hanf21c7972019-12-16 22:32:06 +0900626func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800627 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900628 apex_defaults {
629 name: "myapex-defaults",
630 key: "myapex.key",
631 prebuilts: ["myetc"],
632 native_shared_libs: ["mylib"],
633 java_libs: ["myjar"],
634 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900635 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800636 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000637 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900638 }
639
640 prebuilt_etc {
641 name: "myetc",
642 src: "myprebuilt",
643 }
644
645 apex {
646 name: "myapex",
647 defaults: ["myapex-defaults"],
648 }
649
650 apex_key {
651 name: "myapex.key",
652 public_key: "testkey.avbpubkey",
653 private_key: "testkey.pem",
654 }
655
656 cc_library {
657 name: "mylib",
658 system_shared_libs: [],
659 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000660 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900661 }
662
663 java_library {
664 name: "myjar",
665 srcs: ["foo/bar/MyClass.java"],
666 sdk_version: "none",
667 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000668 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900669 }
670
671 android_app {
672 name: "AppFoo",
673 srcs: ["foo/bar/MyClass.java"],
674 sdk_version: "none",
675 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000676 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900677 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900678
679 runtime_resource_overlay {
680 name: "rro",
681 theme: "blue",
682 }
683
markchien2f59ec92020-09-02 16:23:38 +0800684 bpf {
685 name: "bpf",
686 srcs: ["bpf.c", "bpf2.c"],
687 }
688
Jooyung Hanf21c7972019-12-16 22:32:06 +0900689 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000690 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900691 "etc/myetc",
692 "javalib/myjar.jar",
693 "lib64/mylib.so",
694 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900695 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800696 "etc/bpf/bpf.o",
697 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900698 })
699}
700
Jooyung Han01a3ee22019-11-02 02:52:25 +0900701func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800702 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900703 apex {
704 name: "myapex",
705 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000706 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900707 }
708
709 apex_key {
710 name: "myapex.key",
711 public_key: "testkey.avbpubkey",
712 private_key: "testkey.pem",
713 }
714 `)
715
716 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900717 args := module.Rule("apexRule").Args
718 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
719 t.Error("manifest should be apex_manifest.pb, but " + manifest)
720 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900721}
722
Alex Light5098a612018-11-29 17:12:15 -0800723func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800724 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800725 apex {
726 name: "myapex",
727 key: "myapex.key",
728 payload_type: "zip",
729 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000730 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800731 }
732
733 apex_key {
734 name: "myapex.key",
735 public_key: "testkey.avbpubkey",
736 private_key: "testkey.pem",
737 }
738
739 cc_library {
740 name: "mylib",
741 srcs: ["mylib.cpp"],
742 shared_libs: ["mylib2"],
743 system_shared_libs: [],
744 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000745 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800746 }
747
748 cc_library {
749 name: "mylib2",
750 srcs: ["mylib.cpp"],
751 system_shared_libs: [],
752 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000753 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800754 }
755 `)
756
Sundong Ahnabb64432019-10-22 13:58:29 +0900757 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800758 copyCmds := zipApexRule.Args["copy_commands"]
759
760 // Ensure that main rule creates an output
761 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
762
763 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700764 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800765
766 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700767 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800768
769 // Ensure that both direct and indirect deps are copied into apex
770 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
771 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900772}
773
774func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800775 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900776 apex {
777 name: "myapex",
778 key: "myapex.key",
779 native_shared_libs: ["mylib", "mylib3"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000780 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900781 }
782
783 apex_key {
784 name: "myapex.key",
785 public_key: "testkey.avbpubkey",
786 private_key: "testkey.pem",
787 }
788
789 cc_library {
790 name: "mylib",
791 srcs: ["mylib.cpp"],
792 shared_libs: ["mylib2", "mylib3"],
793 system_shared_libs: [],
794 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000795 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900796 }
797
798 cc_library {
799 name: "mylib2",
800 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900801 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900802 system_shared_libs: [],
803 stl: "none",
804 stubs: {
805 versions: ["1", "2", "3"],
806 },
807 }
808
809 cc_library {
810 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900811 srcs: ["mylib.cpp"],
812 shared_libs: ["mylib4"],
813 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900814 stl: "none",
815 stubs: {
816 versions: ["10", "11", "12"],
817 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000818 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900819 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900820
821 cc_library {
822 name: "mylib4",
823 srcs: ["mylib.cpp"],
824 system_shared_libs: [],
825 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000826 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900827 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900828 `)
829
Sundong Ahnabb64432019-10-22 13:58:29 +0900830 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900831 copyCmds := apexRule.Args["copy_commands"]
832
833 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800834 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900835
836 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800837 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900838
839 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800840 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900841
Colin Crossaede88c2020-08-11 12:17:01 -0700842 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900843
844 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900845 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_3/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900846 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900847 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900848
849 // 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 -0700850 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900851 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700852 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900853
854 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900855 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900856 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900857
858 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700859 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900860
Jooyung Hana57af4a2020-01-23 05:36:59 +0000861 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900862 "lib64/mylib.so",
863 "lib64/mylib3.so",
864 "lib64/mylib4.so",
865 })
Jiyong Park25fc6a92018-11-18 18:02:45 +0900866}
867
Colin Cross7812fd32020-09-25 12:35:10 -0700868func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
869 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -0800870 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -0700871 apex {
872 name: "myapex",
873 key: "myapex.key",
874 native_shared_libs: ["mylib", "mylib3"],
875 min_sdk_version: "29",
876 }
877
878 apex_key {
879 name: "myapex.key",
880 public_key: "testkey.avbpubkey",
881 private_key: "testkey.pem",
882 }
883
884 cc_library {
885 name: "mylib",
886 srcs: ["mylib.cpp"],
887 shared_libs: ["mylib2", "mylib3"],
888 system_shared_libs: [],
889 stl: "none",
890 apex_available: [ "myapex" ],
891 min_sdk_version: "28",
892 }
893
894 cc_library {
895 name: "mylib2",
896 srcs: ["mylib.cpp"],
897 cflags: ["-include mylib.h"],
898 system_shared_libs: [],
899 stl: "none",
900 stubs: {
901 versions: ["28", "29", "30", "current"],
902 },
903 min_sdk_version: "28",
904 }
905
906 cc_library {
907 name: "mylib3",
908 srcs: ["mylib.cpp"],
909 shared_libs: ["mylib4"],
910 system_shared_libs: [],
911 stl: "none",
912 stubs: {
913 versions: ["28", "29", "30", "current"],
914 },
915 apex_available: [ "myapex" ],
916 min_sdk_version: "28",
917 }
918
919 cc_library {
920 name: "mylib4",
921 srcs: ["mylib.cpp"],
922 system_shared_libs: [],
923 stl: "none",
924 apex_available: [ "myapex" ],
925 min_sdk_version: "28",
926 }
927 `)
928
929 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
930 copyCmds := apexRule.Args["copy_commands"]
931
932 // Ensure that direct non-stubs dep is always included
933 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
934
935 // Ensure that indirect stubs dep is not included
936 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
937
938 // Ensure that direct stubs dep is included
939 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
940
941 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
942
Jiyong Park55549df2021-02-26 23:57:23 +0900943 // Ensure that mylib is linking with the latest version of stub for mylib2
944 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -0700945 // ... and not linking to the non-stub (impl) variant of mylib2
946 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
947
948 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
949 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
950 // .. and not linking to the stubs variant of mylib3
951 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
952
953 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -0700954 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -0700955 ensureNotContains(t, mylib2Cflags, "-include ")
956
957 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700958 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -0700959
960 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
961 "lib64/mylib.so",
962 "lib64/mylib3.so",
963 "lib64/mylib4.so",
964 })
965}
966
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900967func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
968 t.Parallel()
969 // myapex (Z)
970 // mylib -----------------.
971 // |
972 // otherapex (29) |
973 // libstub's versions: 29 Z current
974 // |
975 // <platform> |
976 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -0800977 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +0900978 apex {
979 name: "myapex",
980 key: "myapex.key",
981 native_shared_libs: ["mylib"],
982 min_sdk_version: "Z", // non-final
983 }
984
985 cc_library {
986 name: "mylib",
987 srcs: ["mylib.cpp"],
988 shared_libs: ["libstub"],
989 apex_available: ["myapex"],
990 min_sdk_version: "Z",
991 }
992
993 apex_key {
994 name: "myapex.key",
995 public_key: "testkey.avbpubkey",
996 private_key: "testkey.pem",
997 }
998
999 apex {
1000 name: "otherapex",
1001 key: "myapex.key",
1002 native_shared_libs: ["libstub"],
1003 min_sdk_version: "29",
1004 }
1005
1006 cc_library {
1007 name: "libstub",
1008 srcs: ["mylib.cpp"],
1009 stubs: {
1010 versions: ["29", "Z", "current"],
1011 },
1012 apex_available: ["otherapex"],
1013 min_sdk_version: "29",
1014 }
1015
1016 // platform module depending on libstub from otherapex should use the latest stub("current")
1017 cc_library {
1018 name: "libplatform",
1019 srcs: ["mylib.cpp"],
1020 shared_libs: ["libstub"],
1021 }
1022 `, func(fs map[string][]byte, config android.Config) {
1023 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("Z")
1024 config.TestProductVariables.Platform_sdk_final = proptools.BoolPtr(false)
1025 config.TestProductVariables.Platform_version_active_codenames = []string{"Z"}
1026 })
1027
Jiyong Park55549df2021-02-26 23:57:23 +09001028 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001029 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001030 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001031 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001032 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001033
1034 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1035 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1036 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1037 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1038 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1039}
1040
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001041func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001042 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001043 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001044 name: "myapex2",
1045 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001046 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001047 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001048 }
1049
1050 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001051 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001052 public_key: "testkey.avbpubkey",
1053 private_key: "testkey.pem",
1054 }
1055
1056 cc_library {
1057 name: "mylib",
1058 srcs: ["mylib.cpp"],
1059 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001060 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001061 system_shared_libs: [],
1062 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001063 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001064 }
1065
1066 cc_library {
1067 name: "libfoo",
1068 srcs: ["mylib.cpp"],
1069 shared_libs: ["libbar"],
1070 system_shared_libs: [],
1071 stl: "none",
1072 stubs: {
1073 versions: ["10", "20", "30"],
1074 },
1075 }
1076
1077 cc_library {
1078 name: "libbar",
1079 srcs: ["mylib.cpp"],
1080 system_shared_libs: [],
1081 stl: "none",
1082 }
1083
Jiyong Park678c8812020-02-07 17:25:49 +09001084 cc_library_static {
1085 name: "libbaz",
1086 srcs: ["mylib.cpp"],
1087 system_shared_libs: [],
1088 stl: "none",
1089 apex_available: [ "myapex2" ],
1090 }
1091
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001092 `)
1093
Jiyong Park83dc74b2020-01-14 18:38:44 +09001094 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001095 copyCmds := apexRule.Args["copy_commands"]
1096
1097 // Ensure that direct non-stubs dep is always included
1098 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1099
1100 // Ensure that indirect stubs dep is not included
1101 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1102
1103 // Ensure that dependency of stubs is not included
1104 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1105
Colin Crossaede88c2020-08-11 12:17:01 -07001106 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001107
1108 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001109 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001110 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001111 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001112
Jiyong Park3ff16992019-12-27 14:11:47 +09001113 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001114
1115 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1116 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001117
Artur Satayeva8bd1132020-04-27 18:07:06 +01001118 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001119 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001120
Artur Satayeva8bd1132020-04-27 18:07:06 +01001121 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001122 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001123}
1124
Jooyung Hand3639552019-08-09 12:57:43 +09001125func TestApexWithRuntimeLibsDependency(t *testing.T) {
1126 /*
1127 myapex
1128 |
1129 v (runtime_libs)
1130 mylib ------+------> libfoo [provides stub]
1131 |
1132 `------> libbar
1133 */
Colin Cross1c460562021-02-16 17:55:47 -08001134 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001135 apex {
1136 name: "myapex",
1137 key: "myapex.key",
1138 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001139 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001140 }
1141
1142 apex_key {
1143 name: "myapex.key",
1144 public_key: "testkey.avbpubkey",
1145 private_key: "testkey.pem",
1146 }
1147
1148 cc_library {
1149 name: "mylib",
1150 srcs: ["mylib.cpp"],
1151 runtime_libs: ["libfoo", "libbar"],
1152 system_shared_libs: [],
1153 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001154 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001155 }
1156
1157 cc_library {
1158 name: "libfoo",
1159 srcs: ["mylib.cpp"],
1160 system_shared_libs: [],
1161 stl: "none",
1162 stubs: {
1163 versions: ["10", "20", "30"],
1164 },
1165 }
1166
1167 cc_library {
1168 name: "libbar",
1169 srcs: ["mylib.cpp"],
1170 system_shared_libs: [],
1171 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001172 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001173 }
1174
1175 `)
1176
Sundong Ahnabb64432019-10-22 13:58:29 +09001177 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001178 copyCmds := apexRule.Args["copy_commands"]
1179
1180 // Ensure that direct non-stubs dep is always included
1181 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1182
1183 // Ensure that indirect stubs dep is not included
1184 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1185
1186 // Ensure that runtime_libs dep in included
1187 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1188
Sundong Ahnabb64432019-10-22 13:58:29 +09001189 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001190 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1191 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001192
1193}
1194
Paul Duffina02cae32021-03-09 01:44:06 +00001195var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1196 cc.PrepareForTestWithCcBuildComponents,
1197 PrepareForTestWithApexBuildComponents,
1198 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001199 apex {
1200 name: "com.android.runtime",
1201 key: "com.android.runtime.key",
1202 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001203 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001204 }
1205
1206 apex_key {
1207 name: "com.android.runtime.key",
1208 public_key: "testkey.avbpubkey",
1209 private_key: "testkey.pem",
1210 }
Paul Duffina02cae32021-03-09 01:44:06 +00001211 `),
1212 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1213)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001214
Paul Duffina02cae32021-03-09 01:44:06 +00001215func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
1216 result := emptyFixtureFactory.Extend(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001217 cc_library {
1218 name: "libc",
1219 no_libcrt: true,
1220 nocrt: true,
1221 stl: "none",
1222 system_shared_libs: [],
1223 stubs: { versions: ["1"] },
1224 apex_available: ["com.android.runtime"],
1225
1226 sanitize: {
1227 hwaddress: true,
1228 }
1229 }
1230
1231 cc_prebuilt_library_shared {
1232 name: "libclang_rt.hwasan-aarch64-android",
1233 no_libcrt: true,
1234 nocrt: true,
1235 stl: "none",
1236 system_shared_libs: [],
1237 srcs: [""],
1238 stubs: { versions: ["1"] },
1239
1240 sanitize: {
1241 never: true,
1242 },
Paul Duffina02cae32021-03-09 01:44:06 +00001243 } `)
1244 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001245
1246 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1247 "lib64/bionic/libc.so",
1248 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1249 })
1250
1251 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1252
1253 installed := hwasan.Description("install libclang_rt.hwasan")
1254 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1255
1256 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1257 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1258 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1259}
1260
1261func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffina02cae32021-03-09 01:44:06 +00001262 result := emptyFixtureFactory.Extend(
1263 prepareForTestOfRuntimeApexWithHwasan,
1264 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1265 variables.SanitizeDevice = []string{"hwaddress"}
1266 }),
1267 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001268 cc_library {
1269 name: "libc",
1270 no_libcrt: true,
1271 nocrt: true,
1272 stl: "none",
1273 system_shared_libs: [],
1274 stubs: { versions: ["1"] },
1275 apex_available: ["com.android.runtime"],
1276 }
1277
1278 cc_prebuilt_library_shared {
1279 name: "libclang_rt.hwasan-aarch64-android",
1280 no_libcrt: true,
1281 nocrt: true,
1282 stl: "none",
1283 system_shared_libs: [],
1284 srcs: [""],
1285 stubs: { versions: ["1"] },
1286
1287 sanitize: {
1288 never: true,
1289 },
1290 }
Paul Duffina02cae32021-03-09 01:44:06 +00001291 `)
1292 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001293
1294 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1295 "lib64/bionic/libc.so",
1296 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1297 })
1298
1299 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1300
1301 installed := hwasan.Description("install libclang_rt.hwasan")
1302 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1303
1304 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1305 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1306 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1307}
1308
Jooyung Han61b66e92020-03-21 14:21:46 +00001309func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1310 testcases := []struct {
1311 name string
1312 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001313 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001314 shouldLink string
1315 shouldNotLink []string
1316 }{
1317 {
Jiyong Park55549df2021-02-26 23:57:23 +09001318 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001319 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001320 apexVariant: "apex10000",
Jooyung Han61b66e92020-03-21 14:21:46 +00001321 shouldLink: "30",
1322 shouldNotLink: []string{"29"},
1323 },
1324 {
Jiyong Park55549df2021-02-26 23:57:23 +09001325 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001326 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001327 apexVariant: "apex29",
Jiyong Park55549df2021-02-26 23:57:23 +09001328 shouldLink: "30",
1329 shouldNotLink: []string{"29"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001330 },
1331 }
1332 for _, tc := range testcases {
1333 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001334 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001335 apex {
1336 name: "myapex",
1337 key: "myapex.key",
1338 use_vendor: true,
1339 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001340 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001341 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001342 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001343
Jooyung Han61b66e92020-03-21 14:21:46 +00001344 apex_key {
1345 name: "myapex.key",
1346 public_key: "testkey.avbpubkey",
1347 private_key: "testkey.pem",
1348 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001349
Jooyung Han61b66e92020-03-21 14:21:46 +00001350 cc_library {
1351 name: "mylib",
1352 srcs: ["mylib.cpp"],
1353 vendor_available: true,
1354 shared_libs: ["libbar"],
1355 system_shared_libs: [],
1356 stl: "none",
1357 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001358 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001359 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001360
Jooyung Han61b66e92020-03-21 14:21:46 +00001361 cc_library {
1362 name: "libbar",
1363 srcs: ["mylib.cpp"],
1364 system_shared_libs: [],
1365 stl: "none",
1366 stubs: { versions: ["29","30"] },
Colin Cross0477b422020-10-13 18:43:54 -07001367 llndk_stubs: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001368 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001369
Jooyung Han61b66e92020-03-21 14:21:46 +00001370 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001371 name: "libbar.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00001372 symbol_file: "",
1373 }
1374 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07001375 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Han61b66e92020-03-21 14:21:46 +00001376 }, withUnbundledBuild)
Jooyung Han9c80bae2019-08-20 17:30:57 +09001377
Jooyung Han61b66e92020-03-21 14:21:46 +00001378 // Ensure that LLNDK dep is not included
1379 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1380 "lib64/mylib.so",
1381 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001382
Jooyung Han61b66e92020-03-21 14:21:46 +00001383 // Ensure that LLNDK dep is required
1384 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1385 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1386 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001387
Colin Crossaede88c2020-08-11 12:17:01 -07001388 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
Colin Cross127bb8b2020-12-16 16:46:01 -08001389 ensureContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001390 for _, ver := range tc.shouldNotLink {
Colin Cross127bb8b2020-12-16 16:46:01 -08001391 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.VER_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001392 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001393
Colin Crossaede88c2020-08-11 12:17:01 -07001394 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.VER_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jooyung Han61b66e92020-03-21 14:21:46 +00001395 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+tc.shouldLink)
1396 })
1397 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001398}
1399
Jiyong Park25fc6a92018-11-18 18:02:45 +09001400func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001401 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001402 apex {
1403 name: "myapex",
1404 key: "myapex.key",
1405 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001406 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001407 }
1408
1409 apex_key {
1410 name: "myapex.key",
1411 public_key: "testkey.avbpubkey",
1412 private_key: "testkey.pem",
1413 }
1414
1415 cc_library {
1416 name: "mylib",
1417 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001418 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001419 shared_libs: ["libdl#27"],
1420 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001421 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001422 }
1423
1424 cc_library_shared {
1425 name: "mylib_shared",
1426 srcs: ["mylib.cpp"],
1427 shared_libs: ["libdl#27"],
1428 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001429 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001430 }
1431
1432 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001433 name: "libBootstrap",
1434 srcs: ["mylib.cpp"],
1435 stl: "none",
1436 bootstrap: true,
1437 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001438 `)
1439
Sundong Ahnabb64432019-10-22 13:58:29 +09001440 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001441 copyCmds := apexRule.Args["copy_commands"]
1442
1443 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001444 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001445 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1446 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001447
1448 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001449 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001450
Colin Crossaede88c2020-08-11 12:17:01 -07001451 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1452 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1453 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001454
1455 // For dependency to libc
1456 // Ensure that mylib is linking with the latest version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001457 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_29/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001458 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001459 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001460 // ... Cflags from stub is correctly exported to mylib
1461 ensureContains(t, mylibCFlags, "__LIBC_API__=29")
1462 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=29")
1463
1464 // For dependency to libm
1465 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001466 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001467 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001468 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001469 // ... and is not compiling with the stub
1470 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1471 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1472
1473 // For dependency to libdl
1474 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001475 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001476 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001477 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1478 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001479 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001480 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001481 // ... Cflags from stub is correctly exported to mylib
1482 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1483 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001484
1485 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001486 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1487 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1488 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1489 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001490}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001491
Jooyung Han749dc692020-04-15 11:03:39 +09001492func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001493 // there are three links between liba --> libz.
1494 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001495 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001496 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001497 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001498 apex {
1499 name: "myapex",
1500 key: "myapex.key",
1501 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001502 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001503 }
1504
1505 apex {
1506 name: "otherapex",
1507 key: "myapex.key",
1508 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001509 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001510 }
1511
1512 apex_key {
1513 name: "myapex.key",
1514 public_key: "testkey.avbpubkey",
1515 private_key: "testkey.pem",
1516 }
1517
1518 cc_library {
1519 name: "libx",
1520 shared_libs: ["liba"],
1521 system_shared_libs: [],
1522 stl: "none",
1523 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001524 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001525 }
1526
1527 cc_library {
1528 name: "liby",
1529 shared_libs: ["liba"],
1530 system_shared_libs: [],
1531 stl: "none",
1532 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001533 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001534 }
1535
1536 cc_library {
1537 name: "liba",
1538 shared_libs: ["libz"],
1539 system_shared_libs: [],
1540 stl: "none",
1541 apex_available: [
1542 "//apex_available:anyapex",
1543 "//apex_available:platform",
1544 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001545 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001546 }
1547
1548 cc_library {
1549 name: "libz",
1550 system_shared_libs: [],
1551 stl: "none",
1552 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001553 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001554 },
1555 }
Jooyung Han749dc692020-04-15 11:03:39 +09001556 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001557
1558 expectLink := func(from, from_variant, to, to_variant string) {
1559 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1560 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1561 }
1562 expectNoLink := func(from, from_variant, to, to_variant string) {
1563 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1564 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1565 }
1566 // platform liba is linked to non-stub version
1567 expectLink("liba", "shared", "libz", "shared")
Jiyong Park55549df2021-02-26 23:57:23 +09001568 // liba in myapex is linked to #30
1569 expectLink("liba", "shared_apex29", "libz", "shared_30")
1570 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001571 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jooyung Han749dc692020-04-15 11:03:39 +09001572 // liba in otherapex is linked to #30
Colin Crossaede88c2020-08-11 12:17:01 -07001573 expectLink("liba", "shared_apex30", "libz", "shared_30")
1574 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1575 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001576}
1577
Jooyung Hanaed150d2020-04-02 01:41:41 +09001578func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001579 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001580 apex {
1581 name: "myapex",
1582 key: "myapex.key",
1583 native_shared_libs: ["libx"],
1584 min_sdk_version: "R",
1585 }
1586
1587 apex_key {
1588 name: "myapex.key",
1589 public_key: "testkey.avbpubkey",
1590 private_key: "testkey.pem",
1591 }
1592
1593 cc_library {
1594 name: "libx",
1595 shared_libs: ["libz"],
1596 system_shared_libs: [],
1597 stl: "none",
1598 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001599 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001600 }
1601
1602 cc_library {
1603 name: "libz",
1604 system_shared_libs: [],
1605 stl: "none",
1606 stubs: {
1607 versions: ["29", "R"],
1608 },
1609 }
1610 `, func(fs map[string][]byte, config android.Config) {
1611 config.TestProductVariables.Platform_version_active_codenames = []string{"R"}
1612 })
1613
1614 expectLink := func(from, from_variant, to, to_variant string) {
1615 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1616 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1617 }
1618 expectNoLink := func(from, from_variant, to, to_variant string) {
1619 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1620 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1621 }
Dan Albertc8060532020-07-22 22:32:17 -07001622 expectLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001623 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1624 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001625}
1626
Jooyung Han749dc692020-04-15 11:03:39 +09001627func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001628 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001629 apex {
1630 name: "myapex",
1631 key: "myapex.key",
1632 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001633 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001634 }
1635
1636 apex_key {
1637 name: "myapex.key",
1638 public_key: "testkey.avbpubkey",
1639 private_key: "testkey.pem",
1640 }
1641
1642 cc_library {
1643 name: "libx",
1644 shared_libs: ["libz"],
1645 system_shared_libs: [],
1646 stl: "none",
1647 apex_available: [ "myapex" ],
1648 }
1649
1650 cc_library {
1651 name: "libz",
1652 system_shared_libs: [],
1653 stl: "none",
1654 stubs: {
1655 versions: ["1", "2"],
1656 },
1657 }
1658 `)
1659
1660 expectLink := func(from, from_variant, to, to_variant string) {
1661 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1662 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1663 }
1664 expectNoLink := func(from, from_variant, to, to_variant string) {
1665 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1666 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1667 }
Colin Crossaede88c2020-08-11 12:17:01 -07001668 expectLink("libx", "shared_apex10000", "libz", "shared_2")
1669 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
1670 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001671}
1672
1673func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001674 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001675 apex {
1676 name: "myapex",
1677 key: "myapex.key",
1678 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001679 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001680 }
1681
1682 apex_key {
1683 name: "myapex.key",
1684 public_key: "testkey.avbpubkey",
1685 private_key: "testkey.pem",
1686 }
1687
1688 cc_library {
1689 name: "libx",
1690 system_shared_libs: [],
1691 stl: "none",
1692 apex_available: [ "myapex" ],
1693 stubs: {
1694 versions: ["1", "2"],
1695 },
1696 }
1697
1698 cc_library {
1699 name: "libz",
1700 shared_libs: ["libx"],
1701 system_shared_libs: [],
1702 stl: "none",
1703 }
1704 `)
1705
1706 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001707 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001708 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1709 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1710 }
1711 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001712 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001713 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1714 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1715 }
1716 expectLink("libz", "shared", "libx", "shared_2")
1717 expectNoLink("libz", "shared", "libz", "shared_1")
1718 expectNoLink("libz", "shared", "libz", "shared")
1719}
1720
Jooyung Han75568392020-03-20 04:29:24 +09001721func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001722 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001723 apex {
1724 name: "myapex",
1725 key: "myapex.key",
1726 native_shared_libs: ["libx"],
1727 min_sdk_version: "29",
1728 }
1729
1730 apex_key {
1731 name: "myapex.key",
1732 public_key: "testkey.avbpubkey",
1733 private_key: "testkey.pem",
1734 }
1735
1736 cc_library {
1737 name: "libx",
1738 shared_libs: ["libbar"],
1739 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001740 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001741 }
1742
1743 cc_library {
1744 name: "libbar",
1745 stubs: {
1746 versions: ["29", "30"],
1747 },
1748 }
Jooyung Han75568392020-03-20 04:29:24 +09001749 `, func(fs map[string][]byte, config android.Config) {
1750 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
1751 })
Jooyung Han03b51852020-02-26 22:45:42 +09001752 expectLink := func(from, from_variant, to, to_variant string) {
1753 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1754 libFlags := ld.Args["libFlags"]
1755 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1756 }
Colin Crossaede88c2020-08-11 12:17:01 -07001757 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_30")
Jooyung Han03b51852020-02-26 22:45:42 +09001758}
1759
Jooyung Han75568392020-03-20 04:29:24 +09001760func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001761 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001762 apex {
1763 name: "myapex",
1764 key: "myapex.key",
1765 native_shared_libs: ["libx"],
1766 min_sdk_version: "29",
1767 }
1768
1769 apex_key {
1770 name: "myapex.key",
1771 public_key: "testkey.avbpubkey",
1772 private_key: "testkey.pem",
1773 }
1774
1775 cc_library {
1776 name: "libx",
1777 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001778 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001779 }
Jooyung Han75568392020-03-20 04:29:24 +09001780 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001781
1782 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001783 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001784 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001785 // note that platform variant is not.
1786 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001787 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001788}
1789
Jooyung Han749dc692020-04-15 11:03:39 +09001790func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
1791 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09001792 apex {
1793 name: "myapex",
1794 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09001795 native_shared_libs: ["mylib"],
1796 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001797 }
1798
1799 apex_key {
1800 name: "myapex.key",
1801 public_key: "testkey.avbpubkey",
1802 private_key: "testkey.pem",
1803 }
Jooyung Han749dc692020-04-15 11:03:39 +09001804
1805 cc_library {
1806 name: "mylib",
1807 srcs: ["mylib.cpp"],
1808 system_shared_libs: [],
1809 stl: "none",
1810 apex_available: [
1811 "myapex",
1812 ],
1813 min_sdk_version: "30",
1814 }
1815 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001816
1817 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
1818 apex {
1819 name: "myapex",
1820 key: "myapex.key",
1821 native_shared_libs: ["libfoo.ffi"],
1822 min_sdk_version: "29",
1823 }
1824
1825 apex_key {
1826 name: "myapex.key",
1827 public_key: "testkey.avbpubkey",
1828 private_key: "testkey.pem",
1829 }
1830
1831 rust_ffi_shared {
1832 name: "libfoo.ffi",
1833 srcs: ["foo.rs"],
1834 crate_name: "foo",
1835 apex_available: [
1836 "myapex",
1837 ],
1838 min_sdk_version: "30",
1839 }
1840 `)
Jooyung Han749dc692020-04-15 11:03:39 +09001841}
1842
1843func TestApexMinSdkVersion_Okay(t *testing.T) {
1844 testApex(t, `
1845 apex {
1846 name: "myapex",
1847 key: "myapex.key",
1848 native_shared_libs: ["libfoo"],
1849 java_libs: ["libbar"],
1850 min_sdk_version: "29",
1851 }
1852
1853 apex_key {
1854 name: "myapex.key",
1855 public_key: "testkey.avbpubkey",
1856 private_key: "testkey.pem",
1857 }
1858
1859 cc_library {
1860 name: "libfoo",
1861 srcs: ["mylib.cpp"],
1862 shared_libs: ["libfoo_dep"],
1863 apex_available: ["myapex"],
1864 min_sdk_version: "29",
1865 }
1866
1867 cc_library {
1868 name: "libfoo_dep",
1869 srcs: ["mylib.cpp"],
1870 apex_available: ["myapex"],
1871 min_sdk_version: "29",
1872 }
1873
1874 java_library {
1875 name: "libbar",
1876 sdk_version: "current",
1877 srcs: ["a.java"],
1878 static_libs: ["libbar_dep"],
1879 apex_available: ["myapex"],
1880 min_sdk_version: "29",
1881 }
1882
1883 java_library {
1884 name: "libbar_dep",
1885 sdk_version: "current",
1886 srcs: ["a.java"],
1887 apex_available: ["myapex"],
1888 min_sdk_version: "29",
1889 }
Jooyung Han03b51852020-02-26 22:45:42 +09001890 `)
1891}
1892
Artur Satayev8cf899a2020-04-15 17:29:42 +01001893func TestJavaStableSdkVersion(t *testing.T) {
1894 testCases := []struct {
1895 name string
1896 expectedError string
1897 bp string
1898 }{
1899 {
1900 name: "Non-updatable apex with non-stable dep",
1901 bp: `
1902 apex {
1903 name: "myapex",
1904 java_libs: ["myjar"],
1905 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001906 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01001907 }
1908 apex_key {
1909 name: "myapex.key",
1910 public_key: "testkey.avbpubkey",
1911 private_key: "testkey.pem",
1912 }
1913 java_library {
1914 name: "myjar",
1915 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001916 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001917 apex_available: ["myapex"],
1918 }
1919 `,
1920 },
1921 {
1922 name: "Updatable apex with stable dep",
1923 bp: `
1924 apex {
1925 name: "myapex",
1926 java_libs: ["myjar"],
1927 key: "myapex.key",
1928 updatable: true,
1929 min_sdk_version: "29",
1930 }
1931 apex_key {
1932 name: "myapex.key",
1933 public_key: "testkey.avbpubkey",
1934 private_key: "testkey.pem",
1935 }
1936 java_library {
1937 name: "myjar",
1938 srcs: ["foo/bar/MyClass.java"],
1939 sdk_version: "current",
1940 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09001941 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001942 }
1943 `,
1944 },
1945 {
1946 name: "Updatable apex with non-stable dep",
1947 expectedError: "cannot depend on \"myjar\"",
1948 bp: `
1949 apex {
1950 name: "myapex",
1951 java_libs: ["myjar"],
1952 key: "myapex.key",
1953 updatable: true,
1954 }
1955 apex_key {
1956 name: "myapex.key",
1957 public_key: "testkey.avbpubkey",
1958 private_key: "testkey.pem",
1959 }
1960 java_library {
1961 name: "myjar",
1962 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00001963 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001964 apex_available: ["myapex"],
1965 }
1966 `,
1967 },
1968 {
Paul Duffin043f5e72021-03-05 00:00:01 +00001969 name: "Updatable apex with non-stable transitive dep",
1970 // This is not actually detecting that the transitive dependency is unstable, rather it is
1971 // detecting that the transitive dependency is building against a wider API surface than the
1972 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09001973 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01001974 bp: `
1975 apex {
1976 name: "myapex",
1977 java_libs: ["myjar"],
1978 key: "myapex.key",
1979 updatable: true,
1980 }
1981 apex_key {
1982 name: "myapex.key",
1983 public_key: "testkey.avbpubkey",
1984 private_key: "testkey.pem",
1985 }
1986 java_library {
1987 name: "myjar",
1988 srcs: ["foo/bar/MyClass.java"],
1989 sdk_version: "current",
1990 apex_available: ["myapex"],
1991 static_libs: ["transitive-jar"],
1992 }
1993 java_library {
1994 name: "transitive-jar",
1995 srcs: ["foo/bar/MyClass.java"],
1996 sdk_version: "core_platform",
1997 apex_available: ["myapex"],
1998 }
1999 `,
2000 },
2001 }
2002
2003 for _, test := range testCases {
2004 t.Run(test.name, func(t *testing.T) {
2005 if test.expectedError == "" {
2006 testApex(t, test.bp)
2007 } else {
2008 testApexError(t, test.expectedError, test.bp)
2009 }
2010 })
2011 }
2012}
2013
Jooyung Han749dc692020-04-15 11:03:39 +09002014func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2015 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2016 apex {
2017 name: "myapex",
2018 key: "myapex.key",
2019 native_shared_libs: ["mylib"],
2020 min_sdk_version: "29",
2021 }
2022
2023 apex_key {
2024 name: "myapex.key",
2025 public_key: "testkey.avbpubkey",
2026 private_key: "testkey.pem",
2027 }
2028
2029 cc_library {
2030 name: "mylib",
2031 srcs: ["mylib.cpp"],
2032 shared_libs: ["mylib2"],
2033 system_shared_libs: [],
2034 stl: "none",
2035 apex_available: [
2036 "myapex",
2037 ],
2038 min_sdk_version: "29",
2039 }
2040
2041 // indirect part of the apex
2042 cc_library {
2043 name: "mylib2",
2044 srcs: ["mylib.cpp"],
2045 system_shared_libs: [],
2046 stl: "none",
2047 apex_available: [
2048 "myapex",
2049 ],
2050 min_sdk_version: "30",
2051 }
2052 `)
2053}
2054
2055func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2056 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2057 apex {
2058 name: "myapex",
2059 key: "myapex.key",
2060 apps: ["AppFoo"],
2061 min_sdk_version: "29",
2062 }
2063
2064 apex_key {
2065 name: "myapex.key",
2066 public_key: "testkey.avbpubkey",
2067 private_key: "testkey.pem",
2068 }
2069
2070 android_app {
2071 name: "AppFoo",
2072 srcs: ["foo/bar/MyClass.java"],
2073 sdk_version: "current",
2074 min_sdk_version: "29",
2075 system_modules: "none",
2076 stl: "none",
2077 static_libs: ["bar"],
2078 apex_available: [ "myapex" ],
2079 }
2080
2081 java_library {
2082 name: "bar",
2083 sdk_version: "current",
2084 srcs: ["a.java"],
2085 apex_available: [ "myapex" ],
2086 }
2087 `)
2088}
2089
2090func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002091 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002092 apex {
2093 name: "myapex",
2094 key: "myapex.key",
2095 native_shared_libs: ["mylib"],
2096 min_sdk_version: "29",
2097 }
2098
2099 apex_key {
2100 name: "myapex.key",
2101 public_key: "testkey.avbpubkey",
2102 private_key: "testkey.pem",
2103 }
2104
Jiyong Park55549df2021-02-26 23:57:23 +09002105 // mylib in myapex will link to mylib2#30
Jooyung Han749dc692020-04-15 11:03:39 +09002106 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2107 cc_library {
2108 name: "mylib",
2109 srcs: ["mylib.cpp"],
2110 shared_libs: ["mylib2"],
2111 system_shared_libs: [],
2112 stl: "none",
2113 apex_available: ["myapex", "otherapex"],
2114 min_sdk_version: "29",
2115 }
2116
2117 cc_library {
2118 name: "mylib2",
2119 srcs: ["mylib.cpp"],
2120 system_shared_libs: [],
2121 stl: "none",
2122 apex_available: ["otherapex"],
2123 stubs: { versions: ["29", "30"] },
2124 min_sdk_version: "30",
2125 }
2126
2127 apex {
2128 name: "otherapex",
2129 key: "myapex.key",
2130 native_shared_libs: ["mylib", "mylib2"],
2131 min_sdk_version: "30",
2132 }
2133 `)
2134 expectLink := func(from, from_variant, to, to_variant string) {
2135 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2136 libFlags := ld.Args["libFlags"]
2137 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2138 }
Jiyong Park55549df2021-02-26 23:57:23 +09002139 expectLink("mylib", "shared_apex29", "mylib2", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07002140 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002141}
2142
Jooyung Haned124c32021-01-26 11:43:46 +09002143func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
2144 withSAsActiveCodeNames := func(fs map[string][]byte, config android.Config) {
2145 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("S")
2146 config.TestProductVariables.Platform_version_active_codenames = []string{"S"}
2147 }
2148 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2149 apex {
2150 name: "myapex",
2151 key: "myapex.key",
2152 native_shared_libs: ["libfoo"],
2153 min_sdk_version: "S",
2154 }
2155 apex_key {
2156 name: "myapex.key",
2157 public_key: "testkey.avbpubkey",
2158 private_key: "testkey.pem",
2159 }
2160 cc_library {
2161 name: "libfoo",
2162 shared_libs: ["libbar"],
2163 apex_available: ["myapex"],
2164 min_sdk_version: "29",
2165 }
2166 cc_library {
2167 name: "libbar",
2168 apex_available: ["myapex"],
2169 }
2170 `, withSAsActiveCodeNames)
2171}
2172
2173func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
2174 withSAsActiveCodeNames := func(fs map[string][]byte, config android.Config) {
2175 config.TestProductVariables.Platform_sdk_codename = proptools.StringPtr("S")
2176 config.TestProductVariables.Platform_version_active_codenames = []string{"S", "T"}
2177 }
Colin Cross1c460562021-02-16 17:55:47 -08002178 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002179 apex {
2180 name: "myapex",
2181 key: "myapex.key",
2182 native_shared_libs: ["libfoo"],
2183 min_sdk_version: "S",
2184 }
2185 apex_key {
2186 name: "myapex.key",
2187 public_key: "testkey.avbpubkey",
2188 private_key: "testkey.pem",
2189 }
2190 cc_library {
2191 name: "libfoo",
2192 shared_libs: ["libbar"],
2193 apex_available: ["myapex"],
2194 min_sdk_version: "S",
2195 }
2196 cc_library {
2197 name: "libbar",
2198 stubs: {
2199 symbol_file: "libbar.map.txt",
2200 versions: ["30", "S", "T"],
2201 },
2202 }
2203 `, withSAsActiveCodeNames)
2204
2205 // ensure libfoo is linked with "S" version of libbar stub
2206 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2207 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09002208 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_T/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002209}
2210
Jiyong Park7c2ee712018-12-07 00:42:25 +09002211func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002212 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002213 apex {
2214 name: "myapex",
2215 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002216 native_shared_libs: ["mylib"],
2217 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002218 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002219 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002220 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002221 }
2222
2223 apex_key {
2224 name: "myapex.key",
2225 public_key: "testkey.avbpubkey",
2226 private_key: "testkey.pem",
2227 }
2228
2229 prebuilt_etc {
2230 name: "myetc",
2231 src: "myprebuilt",
2232 sub_dir: "foo/bar",
2233 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002234
2235 cc_library {
2236 name: "mylib",
2237 srcs: ["mylib.cpp"],
2238 relative_install_path: "foo/bar",
2239 system_shared_libs: [],
2240 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002241 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002242 }
2243
2244 cc_binary {
2245 name: "mybin",
2246 srcs: ["mylib.cpp"],
2247 relative_install_path: "foo/bar",
2248 system_shared_libs: [],
2249 static_executable: true,
2250 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002251 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002252 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002253 `)
2254
Sundong Ahnabb64432019-10-22 13:58:29 +09002255 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002256 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2257
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002258 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002259 ensureListContains(t, dirs, "etc")
2260 ensureListContains(t, dirs, "etc/foo")
2261 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002262 ensureListContains(t, dirs, "lib64")
2263 ensureListContains(t, dirs, "lib64/foo")
2264 ensureListContains(t, dirs, "lib64/foo/bar")
2265 ensureListContains(t, dirs, "lib")
2266 ensureListContains(t, dirs, "lib/foo")
2267 ensureListContains(t, dirs, "lib/foo/bar")
2268
Jiyong Parkbd13e442019-03-15 18:10:35 +09002269 ensureListContains(t, dirs, "bin")
2270 ensureListContains(t, dirs, "bin/foo")
2271 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002272}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002273
Jooyung Han35155c42020-02-06 17:33:20 +09002274func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002275 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002276 apex {
2277 name: "myapex",
2278 key: "myapex.key",
2279 multilib: {
2280 both: {
2281 native_shared_libs: ["mylib"],
2282 binaries: ["mybin"],
2283 },
2284 },
2285 compile_multilib: "both",
2286 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002287 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002288 }
2289
2290 apex_key {
2291 name: "myapex.key",
2292 public_key: "testkey.avbpubkey",
2293 private_key: "testkey.pem",
2294 }
2295
2296 cc_library {
2297 name: "mylib",
2298 relative_install_path: "foo/bar",
2299 system_shared_libs: [],
2300 stl: "none",
2301 apex_available: [ "myapex" ],
2302 native_bridge_supported: true,
2303 }
2304
2305 cc_binary {
2306 name: "mybin",
2307 relative_install_path: "foo/bar",
2308 system_shared_libs: [],
2309 static_executable: true,
2310 stl: "none",
2311 apex_available: [ "myapex" ],
2312 native_bridge_supported: true,
2313 compile_multilib: "both", // default is "first" for binary
2314 multilib: {
2315 lib64: {
2316 suffix: "64",
2317 },
2318 },
2319 }
2320 `, withNativeBridgeEnabled)
2321 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2322 "bin/foo/bar/mybin",
2323 "bin/foo/bar/mybin64",
2324 "bin/arm/foo/bar/mybin",
2325 "bin/arm64/foo/bar/mybin64",
2326 "lib/foo/bar/mylib.so",
2327 "lib/arm/foo/bar/mylib.so",
2328 "lib64/foo/bar/mylib.so",
2329 "lib64/arm64/foo/bar/mylib.so",
2330 })
2331}
2332
Jiyong Parkda6eb592018-12-19 17:12:36 +09002333func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002334 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002335 apex {
2336 name: "myapex",
2337 key: "myapex.key",
2338 native_shared_libs: ["mylib"],
2339 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002340 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002341 }
2342
2343 apex_key {
2344 name: "myapex.key",
2345 public_key: "testkey.avbpubkey",
2346 private_key: "testkey.pem",
2347 }
2348
2349 cc_library {
2350 name: "mylib",
2351 srcs: ["mylib.cpp"],
2352 shared_libs: ["mylib2"],
2353 system_shared_libs: [],
2354 vendor_available: true,
2355 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002356 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002357 }
2358
2359 cc_library {
2360 name: "mylib2",
2361 srcs: ["mylib.cpp"],
2362 system_shared_libs: [],
2363 vendor_available: true,
2364 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002365 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002366 }
Jooyung Handc782442019-11-01 03:14:38 +09002367 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002368 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002369 })
Jiyong Parkda6eb592018-12-19 17:12:36 +09002370
2371 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002372 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002373 for _, implicit := range i.Implicits {
2374 inputsList = append(inputsList, implicit.String())
2375 }
2376 }
2377 inputsString := strings.Join(inputsList, " ")
2378
2379 // ensure that the apex includes vendor variants of the direct and indirect deps
Colin Crossaede88c2020-08-11 12:17:01 -07002380 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib.so")
2381 ensureContains(t, inputsString, "android_vendor.VER_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002382
2383 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002384 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2385 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002386}
Jiyong Park16e91a02018-12-20 18:18:08 +09002387
Jooyung Han85d61762020-06-24 23:50:26 +09002388func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002389 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2390 apex {
2391 name: "myapex",
2392 key: "myapex.key",
2393 use_vendor: true,
2394 }
2395 apex_key {
2396 name: "myapex.key",
2397 public_key: "testkey.avbpubkey",
2398 private_key: "testkey.pem",
2399 }
2400 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002401 setUseVendorAllowListForTest(config, []string{""})
Jooyung Handc782442019-11-01 03:14:38 +09002402 })
Colin Cross440e0d02020-06-11 11:32:11 -07002403 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002404 testApex(t, `
2405 apex {
2406 name: "myapex",
2407 key: "myapex.key",
2408 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002409 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002410 }
2411 apex_key {
2412 name: "myapex.key",
2413 public_key: "testkey.avbpubkey",
2414 private_key: "testkey.pem",
2415 }
2416 `, func(fs map[string][]byte, config android.Config) {
Colin Cross440e0d02020-06-11 11:32:11 -07002417 setUseVendorAllowListForTest(config, []string{"myapex"})
Jooyung Handc782442019-11-01 03:14:38 +09002418 })
2419}
2420
Jooyung Han5c998b92019-06-27 11:30:33 +09002421func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2422 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2423 apex {
2424 name: "myapex",
2425 key: "myapex.key",
2426 native_shared_libs: ["mylib"],
2427 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002428 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002429 }
2430
2431 apex_key {
2432 name: "myapex.key",
2433 public_key: "testkey.avbpubkey",
2434 private_key: "testkey.pem",
2435 }
2436
2437 cc_library {
2438 name: "mylib",
2439 srcs: ["mylib.cpp"],
2440 system_shared_libs: [],
2441 stl: "none",
2442 }
2443 `)
2444}
2445
Jooyung Han85d61762020-06-24 23:50:26 +09002446func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002447 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002448 apex {
2449 name: "myapex",
2450 key: "myapex.key",
2451 binaries: ["mybin"],
2452 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002453 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002454 }
2455 apex_key {
2456 name: "myapex.key",
2457 public_key: "testkey.avbpubkey",
2458 private_key: "testkey.pem",
2459 }
2460 cc_binary {
2461 name: "mybin",
2462 vendor: true,
2463 shared_libs: ["libfoo"],
2464 }
2465 cc_library {
2466 name: "libfoo",
2467 proprietary: true,
2468 }
2469 `)
2470
2471 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2472 "bin/mybin",
2473 "lib64/libfoo.so",
2474 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2475 "lib64/libc++.so",
2476 })
2477
2478 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002479 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002480 name := apexBundle.BaseModuleName()
2481 prefix := "TARGET_"
2482 var builder strings.Builder
2483 data.Custom(&builder, name, prefix, "", data)
2484 androidMk := builder.String()
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002485 installPath := path.Join(buildDir, "../target/product/test_device/vendor/apex")
2486 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002487
2488 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2489 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2490 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002491}
2492
Jooyung Handf78e212020-07-22 15:54:47 +09002493func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002494 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002495 apex {
2496 name: "myapex",
2497 key: "myapex.key",
2498 binaries: ["mybin"],
2499 vendor: true,
2500 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002501 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002502 }
2503 apex_key {
2504 name: "myapex.key",
2505 public_key: "testkey.avbpubkey",
2506 private_key: "testkey.pem",
2507 }
2508 cc_binary {
2509 name: "mybin",
2510 vendor: true,
2511 shared_libs: ["libvndk", "libvendor"],
2512 }
2513 cc_library {
2514 name: "libvndk",
2515 vndk: {
2516 enabled: true,
2517 },
2518 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002519 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002520 }
2521 cc_library {
2522 name: "libvendor",
2523 vendor: true,
2524 }
2525 `)
2526
2527 vendorVariant := "android_vendor.VER_arm64_armv8-a"
2528
Colin Crossaede88c2020-08-11 12:17:01 -07002529 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002530 libs := names(ldRule.Args["libFlags"])
2531 // VNDK libs(libvndk/libc++) as they are
2532 ensureListContains(t, libs, buildDir+"/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
Paul Duffine05480a2021-03-08 15:07:14 +00002533 ensureListContains(t, libs, buildDir+"/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002534 // non-stable Vendor libs as APEX variants
Colin Crossaede88c2020-08-11 12:17:01 -07002535 ensureListContains(t, libs, buildDir+"/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002536
2537 // VNDK libs are not included when use_vndk_as_stable: true
2538 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2539 "bin/mybin",
2540 "lib64/libvendor.so",
2541 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002542
2543 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2544 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2545 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002546}
2547
Justin Yun13decfb2021-03-08 19:25:55 +09002548func TestProductVariant(t *testing.T) {
2549 ctx := testApex(t, `
2550 apex {
2551 name: "myapex",
2552 key: "myapex.key",
2553 updatable: false,
2554 product_specific: true,
2555 binaries: ["foo"],
2556 }
2557
2558 apex_key {
2559 name: "myapex.key",
2560 public_key: "testkey.avbpubkey",
2561 private_key: "testkey.pem",
2562 }
2563
2564 cc_binary {
2565 name: "foo",
2566 product_available: true,
2567 apex_available: ["myapex"],
2568 srcs: ["foo.cpp"],
2569 }
2570 `, func(fs map[string][]byte, config android.Config) {
2571 config.TestProductVariables.ProductVndkVersion = proptools.StringPtr("current")
2572 })
2573
2574 cflags := strings.Fields(
2575 ctx.ModuleForTests("foo", "android_product.VER_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
2576 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2577 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2578 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2579 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2580}
2581
Jooyung Han8e5685d2020-09-21 11:02:57 +09002582func TestApex_withPrebuiltFirmware(t *testing.T) {
2583 testCases := []struct {
2584 name string
2585 additionalProp string
2586 }{
2587 {"system apex with prebuilt_firmware", ""},
2588 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2589 }
2590 for _, tc := range testCases {
2591 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002592 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002593 apex {
2594 name: "myapex",
2595 key: "myapex.key",
2596 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002597 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002598 `+tc.additionalProp+`
2599 }
2600 apex_key {
2601 name: "myapex.key",
2602 public_key: "testkey.avbpubkey",
2603 private_key: "testkey.pem",
2604 }
2605 prebuilt_firmware {
2606 name: "myfirmware",
2607 src: "myfirmware.bin",
2608 filename_from_src: true,
2609 `+tc.additionalProp+`
2610 }
2611 `)
2612 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2613 "etc/firmware/myfirmware.bin",
2614 })
2615 })
2616 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002617}
2618
Jooyung Hanefb184e2020-06-25 17:14:25 +09002619func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002620 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002621 apex {
2622 name: "myapex",
2623 key: "myapex.key",
2624 use_vendor: true,
2625 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002626 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002627 }
2628
2629 apex_key {
2630 name: "myapex.key",
2631 public_key: "testkey.avbpubkey",
2632 private_key: "testkey.pem",
2633 }
2634
2635 cc_library {
2636 name: "mylib",
2637 vendor_available: true,
2638 apex_available: ["myapex"],
2639 }
2640 `, func(fs map[string][]byte, config android.Config) {
2641 setUseVendorAllowListForTest(config, []string{"myapex"})
2642 })
2643
2644 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002645 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002646 name := apexBundle.BaseModuleName()
2647 prefix := "TARGET_"
2648 var builder strings.Builder
2649 data.Custom(&builder, name, prefix, "", data)
2650 androidMk := builder.String()
2651 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2652}
2653
2654func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002655 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002656 apex {
2657 name: "myapex",
2658 key: "myapex.key",
2659 vendor: true,
2660 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002661 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002662 }
2663
2664 apex_key {
2665 name: "myapex.key",
2666 public_key: "testkey.avbpubkey",
2667 private_key: "testkey.pem",
2668 }
2669
2670 cc_library {
2671 name: "mylib",
2672 vendor_available: true,
2673 }
2674 `)
2675
2676 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002677 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002678 name := apexBundle.BaseModuleName()
2679 prefix := "TARGET_"
2680 var builder strings.Builder
2681 data.Custom(&builder, name, prefix, "", data)
2682 androidMk := builder.String()
2683 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2684}
2685
Jooyung Han2ed99d02020-06-24 23:26:26 +09002686func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002687 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002688 apex {
2689 name: "myapex",
2690 key: "myapex.key",
2691 vintf_fragments: ["fragment.xml"],
2692 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002693 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002694 }
2695 apex_key {
2696 name: "myapex.key",
2697 public_key: "testkey.avbpubkey",
2698 private_key: "testkey.pem",
2699 }
2700 cc_binary {
2701 name: "mybin",
2702 }
2703 `)
2704
2705 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002706 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002707 name := apexBundle.BaseModuleName()
2708 prefix := "TARGET_"
2709 var builder strings.Builder
2710 data.Custom(&builder, name, prefix, "", data)
2711 androidMk := builder.String()
2712 ensureContains(t, androidMk, "LOCAL_VINTF_FRAGMENTS := fragment.xml\n")
2713 ensureContains(t, androidMk, "LOCAL_INIT_RC := init.rc\n")
2714}
2715
Jiyong Park16e91a02018-12-20 18:18:08 +09002716func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002717 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002718 apex {
2719 name: "myapex",
2720 key: "myapex.key",
2721 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002722 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002723 }
2724
2725 apex_key {
2726 name: "myapex.key",
2727 public_key: "testkey.avbpubkey",
2728 private_key: "testkey.pem",
2729 }
2730
2731 cc_library {
2732 name: "mylib",
2733 srcs: ["mylib.cpp"],
2734 system_shared_libs: [],
2735 stl: "none",
2736 stubs: {
2737 versions: ["1", "2", "3"],
2738 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002739 apex_available: [
2740 "//apex_available:platform",
2741 "myapex",
2742 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002743 }
2744
2745 cc_binary {
2746 name: "not_in_apex",
2747 srcs: ["mylib.cpp"],
2748 static_libs: ["mylib"],
2749 static_executable: true,
2750 system_shared_libs: [],
2751 stl: "none",
2752 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002753 `)
2754
Colin Cross7113d202019-11-20 16:39:12 -08002755 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09002756
2757 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08002758 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09002759}
Jiyong Park9335a262018-12-24 11:31:58 +09002760
2761func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002762 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09002763 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002764 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09002765 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002766 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09002767 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09002768 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002769 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09002770 }
2771
2772 cc_library {
2773 name: "mylib",
2774 srcs: ["mylib.cpp"],
2775 system_shared_libs: [],
2776 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002777 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09002778 }
2779
2780 apex_key {
2781 name: "myapex.key",
2782 public_key: "testkey.avbpubkey",
2783 private_key: "testkey.pem",
2784 }
2785
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002786 android_app_certificate {
2787 name: "myapex.certificate",
2788 certificate: "testkey",
2789 }
2790
2791 android_app_certificate {
2792 name: "myapex.certificate.override",
2793 certificate: "testkey.override",
2794 }
2795
Jiyong Park9335a262018-12-24 11:31:58 +09002796 `)
2797
2798 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09002799 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09002800
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002801 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
2802 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002803 "vendor/foo/devkeys/testkey.avbpubkey")
2804 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08002805 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
2806 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09002807 "vendor/foo/devkeys/testkey.pem")
2808 }
2809
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002810 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09002811 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002812 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09002813 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09002814 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09002815 }
2816}
Jiyong Park58e364a2019-01-19 19:24:06 +09002817
Jooyung Hanf121a652019-12-17 14:30:11 +09002818func TestCertificate(t *testing.T) {
2819 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002820 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002821 apex {
2822 name: "myapex",
2823 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002824 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002825 }
2826 apex_key {
2827 name: "myapex.key",
2828 public_key: "testkey.avbpubkey",
2829 private_key: "testkey.pem",
2830 }`)
2831 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2832 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
2833 if actual := rule.Args["certificates"]; actual != expected {
2834 t.Errorf("certificates should be %q, not %q", expected, actual)
2835 }
2836 })
2837 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002838 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002839 apex {
2840 name: "myapex_keytest",
2841 key: "myapex.key",
2842 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002843 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002844 }
2845 apex_key {
2846 name: "myapex.key",
2847 public_key: "testkey.avbpubkey",
2848 private_key: "testkey.pem",
2849 }
2850 android_app_certificate {
2851 name: "myapex.certificate.override",
2852 certificate: "testkey.override",
2853 }`)
2854 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2855 expected := "testkey.override.x509.pem testkey.override.pk8"
2856 if actual := rule.Args["certificates"]; actual != expected {
2857 t.Errorf("certificates should be %q, not %q", expected, actual)
2858 }
2859 })
2860 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002861 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002862 apex {
2863 name: "myapex",
2864 key: "myapex.key",
2865 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002866 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002867 }
2868 apex_key {
2869 name: "myapex.key",
2870 public_key: "testkey.avbpubkey",
2871 private_key: "testkey.pem",
2872 }
2873 android_app_certificate {
2874 name: "myapex.certificate",
2875 certificate: "testkey",
2876 }`)
2877 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2878 expected := "testkey.x509.pem testkey.pk8"
2879 if actual := rule.Args["certificates"]; actual != expected {
2880 t.Errorf("certificates should be %q, not %q", expected, actual)
2881 }
2882 })
2883 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002884 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002885 apex {
2886 name: "myapex_keytest",
2887 key: "myapex.key",
2888 file_contexts: ":myapex-file_contexts",
2889 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002890 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002891 }
2892 apex_key {
2893 name: "myapex.key",
2894 public_key: "testkey.avbpubkey",
2895 private_key: "testkey.pem",
2896 }
2897 android_app_certificate {
2898 name: "myapex.certificate.override",
2899 certificate: "testkey.override",
2900 }`)
2901 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2902 expected := "testkey.override.x509.pem testkey.override.pk8"
2903 if actual := rule.Args["certificates"]; actual != expected {
2904 t.Errorf("certificates should be %q, not %q", expected, actual)
2905 }
2906 })
2907 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002908 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002909 apex {
2910 name: "myapex",
2911 key: "myapex.key",
2912 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002913 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002914 }
2915 apex_key {
2916 name: "myapex.key",
2917 public_key: "testkey.avbpubkey",
2918 private_key: "testkey.pem",
2919 }`)
2920 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
2921 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
2922 if actual := rule.Args["certificates"]; actual != expected {
2923 t.Errorf("certificates should be %q, not %q", expected, actual)
2924 }
2925 })
2926 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002927 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09002928 apex {
2929 name: "myapex_keytest",
2930 key: "myapex.key",
2931 file_contexts: ":myapex-file_contexts",
2932 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002933 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09002934 }
2935 apex_key {
2936 name: "myapex.key",
2937 public_key: "testkey.avbpubkey",
2938 private_key: "testkey.pem",
2939 }
2940 android_app_certificate {
2941 name: "myapex.certificate.override",
2942 certificate: "testkey.override",
2943 }`)
2944 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
2945 expected := "testkey.override.x509.pem testkey.override.pk8"
2946 if actual := rule.Args["certificates"]; actual != expected {
2947 t.Errorf("certificates should be %q, not %q", expected, actual)
2948 }
2949 })
2950}
2951
Jiyong Park58e364a2019-01-19 19:24:06 +09002952func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002953 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09002954 apex {
2955 name: "myapex",
2956 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002957 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002958 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09002959 }
2960
2961 apex {
2962 name: "otherapex",
2963 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09002964 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09002965 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002966 }
2967
2968 apex_key {
2969 name: "myapex.key",
2970 public_key: "testkey.avbpubkey",
2971 private_key: "testkey.pem",
2972 }
2973
2974 cc_library {
2975 name: "mylib",
2976 srcs: ["mylib.cpp"],
2977 system_shared_libs: [],
2978 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002979 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002980 "myapex",
2981 "otherapex",
2982 ],
Jooyung Han24282772020-03-21 23:20:55 +09002983 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09002984 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09002985 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09002986 cc_library {
2987 name: "mylib2",
2988 srcs: ["mylib.cpp"],
2989 system_shared_libs: [],
2990 stl: "none",
2991 apex_available: [
2992 "myapex",
2993 "otherapex",
2994 ],
Colin Crossaede88c2020-08-11 12:17:01 -07002995 static_libs: ["mylib3"],
2996 recovery_available: true,
2997 min_sdk_version: "29",
2998 }
2999 cc_library {
3000 name: "mylib3",
3001 srcs: ["mylib.cpp"],
3002 system_shared_libs: [],
3003 stl: "none",
3004 apex_available: [
3005 "myapex",
3006 "otherapex",
3007 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09003008 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07003009 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003010 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003011 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003012 `)
3013
Jooyung Hanc87a0592020-03-02 17:44:33 +09003014 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003015 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003016 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003017 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003018
Jooyung Hanccce2f22020-03-07 03:45:53 +09003019 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003020 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003021 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003022 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003023 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003024
Jooyung Hanccce2f22020-03-07 03:45:53 +09003025 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003026 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003027 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003028 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003029 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003030
Colin Crossaede88c2020-08-11 12:17:01 -07003031 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3032 // each variant defines additional macros to distinguish which apex variant it is built for
3033
3034 // non-APEX variant does not have __ANDROID_APEX__ defined
3035 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3036 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3037
3038 // APEX variant has __ANDROID_APEX__ defined
3039 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3040 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3041 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3042 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3043
3044 // APEX variant has __ANDROID_APEX__ defined
3045 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3046 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3047 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3048 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3049
Dan Albertb19953d2020-11-17 15:29:36 -08003050 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003051 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3052 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003053 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003054
3055 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3056 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003057
3058 // non-APEX variant does not have __ANDROID_APEX__ defined
3059 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3060 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3061
3062 // APEX variant has __ANDROID_APEX__ defined
3063 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003064 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003065 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003066 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003067
Jooyung Hanc87a0592020-03-02 17:44:33 +09003068 // APEX variant has __ANDROID_APEX__ defined
3069 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003070 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003071 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003072 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003073
Dan Albertb19953d2020-11-17 15:29:36 -08003074 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003075 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003076 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003077 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003078}
Jiyong Park7e636d02019-01-28 16:16:54 +09003079
3080func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003081 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003082 apex {
3083 name: "myapex",
3084 key: "myapex.key",
3085 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003086 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003087 }
3088
3089 apex_key {
3090 name: "myapex.key",
3091 public_key: "testkey.avbpubkey",
3092 private_key: "testkey.pem",
3093 }
3094
3095 cc_library_headers {
3096 name: "mylib_headers",
3097 export_include_dirs: ["my_include"],
3098 system_shared_libs: [],
3099 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003100 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003101 }
3102
3103 cc_library {
3104 name: "mylib",
3105 srcs: ["mylib.cpp"],
3106 system_shared_libs: [],
3107 stl: "none",
3108 header_libs: ["mylib_headers"],
3109 export_header_lib_headers: ["mylib_headers"],
3110 stubs: {
3111 versions: ["1", "2", "3"],
3112 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003113 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003114 }
3115
3116 cc_library {
3117 name: "otherlib",
3118 srcs: ["mylib.cpp"],
3119 system_shared_libs: [],
3120 stl: "none",
3121 shared_libs: ["mylib"],
3122 }
3123 `)
3124
Colin Cross7113d202019-11-20 16:39:12 -08003125 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003126
3127 // Ensure that the include path of the header lib is exported to 'otherlib'
3128 ensureContains(t, cFlags, "-Imy_include")
3129}
Alex Light9670d332019-01-29 18:07:33 -08003130
Jiyong Park7cd10e32020-01-14 09:22:18 +09003131type fileInApex struct {
3132 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003133 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003134 isLink bool
3135}
3136
Jooyung Hana57af4a2020-01-23 05:36:59 +00003137func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003138 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003139 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003140 copyCmds := apexRule.Args["copy_commands"]
3141 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003142 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003143 for _, cmd := range strings.Split(copyCmds, "&&") {
3144 cmd = strings.TrimSpace(cmd)
3145 if cmd == "" {
3146 continue
3147 }
3148 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003149 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003150 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003151 switch terms[0] {
3152 case "mkdir":
3153 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003154 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003155 t.Fatal("copyCmds contains invalid cp command", cmd)
3156 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003157 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003158 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003159 isLink = false
3160 case "ln":
3161 if len(terms) != 3 && len(terms) != 4 {
3162 // ln LINK TARGET or ln -s LINK TARGET
3163 t.Fatal("copyCmds contains invalid ln command", cmd)
3164 }
3165 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003166 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003167 isLink = true
3168 default:
3169 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3170 }
3171 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003172 index := strings.Index(dst, imageApexDir)
3173 if index == -1 {
3174 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3175 }
3176 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003177 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003178 }
3179 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003180 return ret
3181}
3182
Jooyung Hana57af4a2020-01-23 05:36:59 +00003183func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3184 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003185 var failed bool
3186 var surplus []string
3187 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003188 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003189 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003190 for _, expected := range files {
3191 if matched, _ := path.Match(expected, file.path); matched {
3192 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003193 mactchFound = true
3194 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003195 }
3196 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003197 if !mactchFound {
3198 surplus = append(surplus, file.path)
3199 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003200 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003201
Jooyung Han31c470b2019-10-18 16:26:59 +09003202 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003203 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003204 t.Log("surplus files", surplus)
3205 failed = true
3206 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003207
3208 if len(files) > len(filesMatched) {
3209 var missing []string
3210 for _, expected := range files {
3211 if !filesMatched[expected] {
3212 missing = append(missing, expected)
3213 }
3214 }
3215 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003216 t.Log("missing files", missing)
3217 failed = true
3218 }
3219 if failed {
3220 t.Fail()
3221 }
3222}
3223
Jooyung Han344d5432019-08-23 11:17:39 +09003224func TestVndkApexCurrent(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003225 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003226 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003227 name: "com.android.vndk.current",
3228 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003229 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003230 }
3231
3232 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003233 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003234 public_key: "testkey.avbpubkey",
3235 private_key: "testkey.pem",
3236 }
3237
3238 cc_library {
3239 name: "libvndk",
3240 srcs: ["mylib.cpp"],
3241 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003242 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003243 vndk: {
3244 enabled: true,
3245 },
3246 system_shared_libs: [],
3247 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003248 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003249 }
3250
3251 cc_library {
3252 name: "libvndksp",
3253 srcs: ["mylib.cpp"],
3254 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003255 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003256 vndk: {
3257 enabled: true,
3258 support_system_process: true,
3259 },
3260 system_shared_libs: [],
3261 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003262 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003263 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003264 `+vndkLibrariesTxtFiles("current"))
Jooyung Han344d5432019-08-23 11:17:39 +09003265
Colin Cross2807f002021-03-02 10:15:29 -08003266 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003267 "lib/libvndk.so",
3268 "lib/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003269 "lib/libc++.so",
Jooyung Han31c470b2019-10-18 16:26:59 +09003270 "lib64/libvndk.so",
3271 "lib64/libvndksp.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003272 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003273 "etc/llndk.libraries.VER.txt",
3274 "etc/vndkcore.libraries.VER.txt",
3275 "etc/vndksp.libraries.VER.txt",
3276 "etc/vndkprivate.libraries.VER.txt",
Justin Yun8a2600c2020-12-07 12:44:03 +09003277 "etc/vndkproduct.libraries.VER.txt",
Jooyung Han31c470b2019-10-18 16:26:59 +09003278 })
Jooyung Han344d5432019-08-23 11:17:39 +09003279}
3280
3281func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003282 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003283 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003284 name: "com.android.vndk.current",
3285 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003286 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003287 }
3288
3289 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003290 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003291 public_key: "testkey.avbpubkey",
3292 private_key: "testkey.pem",
3293 }
3294
3295 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003296 name: "libvndk",
3297 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003298 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003299 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003300 vndk: {
3301 enabled: true,
3302 },
3303 system_shared_libs: [],
3304 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003305 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003306 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003307
3308 cc_prebuilt_library_shared {
3309 name: "libvndk.arm",
3310 srcs: ["libvndk.arm.so"],
3311 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003312 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003313 vndk: {
3314 enabled: true,
3315 },
3316 enabled: false,
3317 arch: {
3318 arm: {
3319 enabled: true,
3320 },
3321 },
3322 system_shared_libs: [],
3323 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003324 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003325 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003326 `+vndkLibrariesTxtFiles("current"),
3327 withFiles(map[string][]byte{
3328 "libvndk.so": nil,
3329 "libvndk.arm.so": nil,
3330 }))
Colin Cross2807f002021-03-02 10:15:29 -08003331 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003332 "lib/libvndk.so",
3333 "lib/libvndk.arm.so",
3334 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003335 "lib/libc++.so",
3336 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003337 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003338 })
Jooyung Han344d5432019-08-23 11:17:39 +09003339}
3340
Jooyung Han39edb6c2019-11-06 16:53:07 +09003341func vndkLibrariesTxtFiles(vers ...string) (result string) {
3342 for _, v := range vers {
3343 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003344 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003345 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003346 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003347 name: "` + txt + `.libraries.txt",
3348 }
3349 `
3350 }
3351 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003352 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003353 result += `
3354 prebuilt_etc {
3355 name: "` + txt + `.libraries.` + v + `.txt",
3356 src: "dummy.txt",
3357 }
3358 `
3359 }
3360 }
3361 }
3362 return
3363}
3364
Jooyung Han344d5432019-08-23 11:17:39 +09003365func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003366 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003367 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003368 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003369 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003370 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003371 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003372 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003373 }
3374
3375 apex_key {
3376 name: "myapex.key",
3377 public_key: "testkey.avbpubkey",
3378 private_key: "testkey.pem",
3379 }
3380
Jooyung Han31c470b2019-10-18 16:26:59 +09003381 vndk_prebuilt_shared {
3382 name: "libvndk27",
3383 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003384 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003385 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003386 vndk: {
3387 enabled: true,
3388 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003389 target_arch: "arm64",
3390 arch: {
3391 arm: {
3392 srcs: ["libvndk27_arm.so"],
3393 },
3394 arm64: {
3395 srcs: ["libvndk27_arm64.so"],
3396 },
3397 },
Colin Cross2807f002021-03-02 10:15:29 -08003398 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003399 }
3400
3401 vndk_prebuilt_shared {
3402 name: "libvndk27",
3403 version: "27",
3404 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003405 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003406 vndk: {
3407 enabled: true,
3408 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003409 target_arch: "x86_64",
3410 arch: {
3411 x86: {
3412 srcs: ["libvndk27_x86.so"],
3413 },
3414 x86_64: {
3415 srcs: ["libvndk27_x86_64.so"],
3416 },
3417 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003418 }
3419 `+vndkLibrariesTxtFiles("27"),
3420 withFiles(map[string][]byte{
3421 "libvndk27_arm.so": nil,
3422 "libvndk27_arm64.so": nil,
3423 "libvndk27_x86.so": nil,
3424 "libvndk27_x86_64.so": nil,
3425 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003426
Colin Cross2807f002021-03-02 10:15:29 -08003427 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003428 "lib/libvndk27_arm.so",
3429 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003430 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003431 })
Jooyung Han344d5432019-08-23 11:17:39 +09003432}
3433
Jooyung Han90eee022019-10-01 20:02:42 +09003434func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003435 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003436 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003437 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003438 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003439 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003440 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003441 }
3442 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003443 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003444 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003445 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003446 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003447 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003448 }
3449 apex_key {
3450 name: "myapex.key",
3451 public_key: "testkey.avbpubkey",
3452 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003453 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003454
3455 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003456 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003457 actual := proptools.String(bundle.properties.Apex_name)
3458 if !reflect.DeepEqual(actual, expected) {
3459 t.Errorf("Got '%v', expected '%v'", actual, expected)
3460 }
3461 }
3462
Colin Cross2807f002021-03-02 10:15:29 -08003463 assertApexName("com.android.vndk.vVER", "com.android.vndk.current")
3464 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003465}
3466
Jooyung Han344d5432019-08-23 11:17:39 +09003467func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003468 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003469 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003470 name: "com.android.vndk.current",
3471 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003472 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003473 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003474 }
3475
3476 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003477 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003478 public_key: "testkey.avbpubkey",
3479 private_key: "testkey.pem",
3480 }
3481
3482 cc_library {
3483 name: "libvndk",
3484 srcs: ["mylib.cpp"],
3485 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003486 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003487 native_bridge_supported: true,
3488 host_supported: true,
3489 vndk: {
3490 enabled: true,
3491 },
3492 system_shared_libs: [],
3493 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003494 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003495 }
Colin Cross2807f002021-03-02 10:15:29 -08003496 `+vndkLibrariesTxtFiles("current"),
3497 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003498
Colin Cross2807f002021-03-02 10:15:29 -08003499 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003500 "lib/libvndk.so",
3501 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003502 "lib/libc++.so",
3503 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003504 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003505 })
Jooyung Han344d5432019-08-23 11:17:39 +09003506}
3507
3508func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003509 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003510 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003511 name: "com.android.vndk.current",
3512 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003513 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003514 native_bridge_supported: true,
3515 }
3516
3517 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003518 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003519 public_key: "testkey.avbpubkey",
3520 private_key: "testkey.pem",
3521 }
3522
3523 cc_library {
3524 name: "libvndk",
3525 srcs: ["mylib.cpp"],
3526 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003527 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003528 native_bridge_supported: true,
3529 host_supported: true,
3530 vndk: {
3531 enabled: true,
3532 },
3533 system_shared_libs: [],
3534 stl: "none",
3535 }
3536 `)
3537}
3538
Jooyung Han31c470b2019-10-18 16:26:59 +09003539func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003540 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003541 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003542 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003543 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003544 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003545 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003546 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003547 }
3548
3549 apex_key {
3550 name: "myapex.key",
3551 public_key: "testkey.avbpubkey",
3552 private_key: "testkey.pem",
3553 }
3554
3555 vndk_prebuilt_shared {
3556 name: "libvndk27",
3557 version: "27",
3558 target_arch: "arm",
3559 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003560 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003561 vndk: {
3562 enabled: true,
3563 },
3564 arch: {
3565 arm: {
3566 srcs: ["libvndk27.so"],
3567 }
3568 },
3569 }
3570
3571 vndk_prebuilt_shared {
3572 name: "libvndk27",
3573 version: "27",
3574 target_arch: "arm",
3575 binder32bit: true,
3576 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003577 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003578 vndk: {
3579 enabled: true,
3580 },
3581 arch: {
3582 arm: {
3583 srcs: ["libvndk27binder32.so"],
3584 }
3585 },
Colin Cross2807f002021-03-02 10:15:29 -08003586 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003587 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003588 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003589 withFiles(map[string][]byte{
3590 "libvndk27.so": nil,
3591 "libvndk27binder32.so": nil,
3592 }),
3593 withBinder32bit,
3594 withTargets(map[android.OsType][]android.Target{
3595 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003596 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3597 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003598 },
3599 }),
3600 )
3601
Colin Cross2807f002021-03-02 10:15:29 -08003602 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003603 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003604 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003605 })
3606}
3607
Jooyung Han45a96772020-06-15 14:59:42 +09003608func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003609 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003610 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003611 name: "com.android.vndk.current",
3612 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003613 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003614 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003615 }
3616
3617 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003618 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003619 public_key: "testkey.avbpubkey",
3620 private_key: "testkey.pem",
3621 }
3622
3623 cc_library {
3624 name: "libz",
3625 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003626 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003627 vndk: {
3628 enabled: true,
3629 },
3630 stubs: {
3631 symbol_file: "libz.map.txt",
3632 versions: ["30"],
3633 }
3634 }
3635 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3636 "libz.map.txt": nil,
3637 }))
3638
Colin Cross2807f002021-03-02 10:15:29 -08003639 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003640 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3641 ensureListEmpty(t, provideNativeLibs)
3642}
3643
Jooyung Hane1633032019-08-01 17:41:43 +09003644func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003645 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003646 apex {
3647 name: "myapex_nodep",
3648 key: "myapex.key",
3649 native_shared_libs: ["lib_nodep"],
3650 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003651 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003652 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003653 }
3654
3655 apex {
3656 name: "myapex_dep",
3657 key: "myapex.key",
3658 native_shared_libs: ["lib_dep"],
3659 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003660 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003661 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003662 }
3663
3664 apex {
3665 name: "myapex_provider",
3666 key: "myapex.key",
3667 native_shared_libs: ["libfoo"],
3668 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003669 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003670 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003671 }
3672
3673 apex {
3674 name: "myapex_selfcontained",
3675 key: "myapex.key",
3676 native_shared_libs: ["lib_dep", "libfoo"],
3677 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003678 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003679 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003680 }
3681
3682 apex_key {
3683 name: "myapex.key",
3684 public_key: "testkey.avbpubkey",
3685 private_key: "testkey.pem",
3686 }
3687
3688 cc_library {
3689 name: "lib_nodep",
3690 srcs: ["mylib.cpp"],
3691 system_shared_libs: [],
3692 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003693 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003694 }
3695
3696 cc_library {
3697 name: "lib_dep",
3698 srcs: ["mylib.cpp"],
3699 shared_libs: ["libfoo"],
3700 system_shared_libs: [],
3701 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003702 apex_available: [
3703 "myapex_dep",
3704 "myapex_provider",
3705 "myapex_selfcontained",
3706 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003707 }
3708
3709 cc_library {
3710 name: "libfoo",
3711 srcs: ["mytest.cpp"],
3712 stubs: {
3713 versions: ["1"],
3714 },
3715 system_shared_libs: [],
3716 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003717 apex_available: [
3718 "myapex_provider",
3719 "myapex_selfcontained",
3720 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003721 }
3722 `)
3723
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003724 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003725 var provideNativeLibs, requireNativeLibs []string
3726
Sundong Ahnabb64432019-10-22 13:58:29 +09003727 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003728 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3729 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003730 ensureListEmpty(t, provideNativeLibs)
3731 ensureListEmpty(t, requireNativeLibs)
3732
Sundong Ahnabb64432019-10-22 13:58:29 +09003733 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003734 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3735 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003736 ensureListEmpty(t, provideNativeLibs)
3737 ensureListContains(t, requireNativeLibs, "libfoo.so")
3738
Sundong Ahnabb64432019-10-22 13:58:29 +09003739 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003740 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3741 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003742 ensureListContains(t, provideNativeLibs, "libfoo.so")
3743 ensureListEmpty(t, requireNativeLibs)
3744
Sundong Ahnabb64432019-10-22 13:58:29 +09003745 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003746 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3747 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003748 ensureListContains(t, provideNativeLibs, "libfoo.so")
3749 ensureListEmpty(t, requireNativeLibs)
3750}
3751
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003752func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003753 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003754 apex {
3755 name: "myapex",
3756 key: "myapex.key",
3757 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09003758 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003759 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003760 }
3761
3762 apex_key {
3763 name: "myapex.key",
3764 public_key: "testkey.avbpubkey",
3765 private_key: "testkey.pem",
3766 }
Jiyong Parkdb334862020-02-05 17:19:28 +09003767
3768 cc_library {
3769 name: "mylib",
3770 srcs: ["mylib.cpp"],
3771 system_shared_libs: [],
3772 stl: "none",
3773 apex_available: [
3774 "//apex_available:platform",
3775 "myapex",
3776 ],
3777 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003778 `)
3779
Sundong Ahnabb64432019-10-22 13:58:29 +09003780 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003781 apexManifestRule := module.Rule("apexManifestRule")
3782 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
3783 apexRule := module.Rule("apexRule")
3784 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09003785
3786 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003787 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09003788 name := apexBundle.BaseModuleName()
3789 prefix := "TARGET_"
3790 var builder strings.Builder
3791 data.Custom(&builder, name, prefix, "", data)
3792 androidMk := builder.String()
3793 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
3794 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003795}
3796
Alex Light0851b882019-02-07 13:20:53 -08003797func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003798 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003799 apex {
3800 name: "myapex",
3801 key: "myapex.key",
3802 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003803 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003804 }
3805
3806 apex_key {
3807 name: "myapex.key",
3808 public_key: "testkey.avbpubkey",
3809 private_key: "testkey.pem",
3810 }
3811
3812 cc_library {
3813 name: "mylib_common",
3814 srcs: ["mylib.cpp"],
3815 system_shared_libs: [],
3816 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003817 apex_available: [
3818 "//apex_available:platform",
3819 "myapex",
3820 ],
Alex Light0851b882019-02-07 13:20:53 -08003821 }
3822 `)
3823
Sundong Ahnabb64432019-10-22 13:58:29 +09003824 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003825 apexRule := module.Rule("apexRule")
3826 copyCmds := apexRule.Args["copy_commands"]
3827
3828 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
3829 t.Log("Apex was a test apex!")
3830 t.Fail()
3831 }
3832 // Ensure that main rule creates an output
3833 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3834
3835 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003836 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003837
3838 // Ensure that both direct and indirect deps are copied into apex
3839 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3840
Colin Cross7113d202019-11-20 16:39:12 -08003841 // Ensure that the platform variant ends with _shared
3842 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003843
Colin Cross56a83212020-09-15 18:30:11 -07003844 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08003845 t.Log("Found mylib_common not in any apex!")
3846 t.Fail()
3847 }
3848}
3849
3850func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003851 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08003852 apex_test {
3853 name: "myapex",
3854 key: "myapex.key",
3855 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003856 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08003857 }
3858
3859 apex_key {
3860 name: "myapex.key",
3861 public_key: "testkey.avbpubkey",
3862 private_key: "testkey.pem",
3863 }
3864
3865 cc_library {
3866 name: "mylib_common_test",
3867 srcs: ["mylib.cpp"],
3868 system_shared_libs: [],
3869 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003870 // TODO: remove //apex_available:platform
3871 apex_available: [
3872 "//apex_available:platform",
3873 "myapex",
3874 ],
Alex Light0851b882019-02-07 13:20:53 -08003875 }
3876 `)
3877
Sundong Ahnabb64432019-10-22 13:58:29 +09003878 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08003879 apexRule := module.Rule("apexRule")
3880 copyCmds := apexRule.Args["copy_commands"]
3881
3882 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
3883 t.Log("Apex was not a test apex!")
3884 t.Fail()
3885 }
3886 // Ensure that main rule creates an output
3887 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3888
3889 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003890 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08003891
3892 // Ensure that both direct and indirect deps are copied into apex
3893 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
3894
Colin Cross7113d202019-11-20 16:39:12 -08003895 // Ensure that the platform variant ends with _shared
3896 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08003897}
3898
Alex Light9670d332019-01-29 18:07:33 -08003899func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003900 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08003901 apex {
3902 name: "myapex",
3903 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003904 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08003905 multilib: {
3906 first: {
3907 native_shared_libs: ["mylib_common"],
3908 }
3909 },
3910 target: {
3911 android: {
3912 multilib: {
3913 first: {
3914 native_shared_libs: ["mylib"],
3915 }
3916 }
3917 },
3918 host: {
3919 multilib: {
3920 first: {
3921 native_shared_libs: ["mylib2"],
3922 }
3923 }
3924 }
3925 }
3926 }
3927
3928 apex_key {
3929 name: "myapex.key",
3930 public_key: "testkey.avbpubkey",
3931 private_key: "testkey.pem",
3932 }
3933
3934 cc_library {
3935 name: "mylib",
3936 srcs: ["mylib.cpp"],
3937 system_shared_libs: [],
3938 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003939 // TODO: remove //apex_available:platform
3940 apex_available: [
3941 "//apex_available:platform",
3942 "myapex",
3943 ],
Alex Light9670d332019-01-29 18:07:33 -08003944 }
3945
3946 cc_library {
3947 name: "mylib_common",
3948 srcs: ["mylib.cpp"],
3949 system_shared_libs: [],
3950 stl: "none",
3951 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003952 // TODO: remove //apex_available:platform
3953 apex_available: [
3954 "//apex_available:platform",
3955 "myapex",
3956 ],
Alex Light9670d332019-01-29 18:07:33 -08003957 }
3958
3959 cc_library {
3960 name: "mylib2",
3961 srcs: ["mylib.cpp"],
3962 system_shared_libs: [],
3963 stl: "none",
3964 compile_multilib: "first",
3965 }
3966 `)
3967
Sundong Ahnabb64432019-10-22 13:58:29 +09003968 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08003969 copyCmds := apexRule.Args["copy_commands"]
3970
3971 // Ensure that main rule creates an output
3972 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
3973
3974 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07003975 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
3976 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
3977 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08003978
3979 // Ensure that both direct and indirect deps are copied into apex
3980 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
3981 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
3982 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
3983
Colin Cross7113d202019-11-20 16:39:12 -08003984 // Ensure that the platform variant ends with _shared
3985 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
3986 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
3987 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08003988}
Jiyong Park04480cf2019-02-06 00:16:29 +09003989
Jiyong Park59140302020-12-14 18:44:04 +09003990func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003991 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09003992 apex {
3993 name: "myapex",
3994 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003995 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09003996 arch: {
3997 arm64: {
3998 native_shared_libs: ["mylib.arm64"],
3999 },
4000 x86_64: {
4001 native_shared_libs: ["mylib.x64"],
4002 },
4003 }
4004 }
4005
4006 apex_key {
4007 name: "myapex.key",
4008 public_key: "testkey.avbpubkey",
4009 private_key: "testkey.pem",
4010 }
4011
4012 cc_library {
4013 name: "mylib.arm64",
4014 srcs: ["mylib.cpp"],
4015 system_shared_libs: [],
4016 stl: "none",
4017 // TODO: remove //apex_available:platform
4018 apex_available: [
4019 "//apex_available:platform",
4020 "myapex",
4021 ],
4022 }
4023
4024 cc_library {
4025 name: "mylib.x64",
4026 srcs: ["mylib.cpp"],
4027 system_shared_libs: [],
4028 stl: "none",
4029 // TODO: remove //apex_available:platform
4030 apex_available: [
4031 "//apex_available:platform",
4032 "myapex",
4033 ],
4034 }
4035 `)
4036
4037 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4038 copyCmds := apexRule.Args["copy_commands"]
4039
4040 // Ensure that apex variant is created for the direct dep
4041 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4042 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4043
4044 // Ensure that both direct and indirect deps are copied into apex
4045 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4046 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4047}
4048
Jiyong Park04480cf2019-02-06 00:16:29 +09004049func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004050 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004051 apex {
4052 name: "myapex",
4053 key: "myapex.key",
4054 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004055 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004056 }
4057
4058 apex_key {
4059 name: "myapex.key",
4060 public_key: "testkey.avbpubkey",
4061 private_key: "testkey.pem",
4062 }
4063
4064 sh_binary {
4065 name: "myscript",
4066 src: "mylib.cpp",
4067 filename: "myscript.sh",
4068 sub_dir: "script",
4069 }
4070 `)
4071
Sundong Ahnabb64432019-10-22 13:58:29 +09004072 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004073 copyCmds := apexRule.Args["copy_commands"]
4074
4075 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4076}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004077
Jooyung Han91df2082019-11-20 01:49:42 +09004078func TestApexInVariousPartition(t *testing.T) {
4079 testcases := []struct {
4080 propName, parition, flattenedPartition string
4081 }{
4082 {"", "system", "system_ext"},
4083 {"product_specific: true", "product", "product"},
4084 {"soc_specific: true", "vendor", "vendor"},
4085 {"proprietary: true", "vendor", "vendor"},
4086 {"vendor: true", "vendor", "vendor"},
4087 {"system_ext_specific: true", "system_ext", "system_ext"},
4088 }
4089 for _, tc := range testcases {
4090 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004091 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004092 apex {
4093 name: "myapex",
4094 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004095 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004096 `+tc.propName+`
4097 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004098
Jooyung Han91df2082019-11-20 01:49:42 +09004099 apex_key {
4100 name: "myapex.key",
4101 public_key: "testkey.avbpubkey",
4102 private_key: "testkey.pem",
4103 }
4104 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004105
Jooyung Han91df2082019-11-20 01:49:42 +09004106 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
4107 expected := buildDir + "/target/product/test_device/" + tc.parition + "/apex"
4108 actual := apex.installDir.String()
4109 if actual != expected {
4110 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4111 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004112
Jooyung Han91df2082019-11-20 01:49:42 +09004113 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
4114 expected = buildDir + "/target/product/test_device/" + tc.flattenedPartition + "/apex"
4115 actual = flattened.installDir.String()
4116 if actual != expected {
4117 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4118 }
4119 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004120 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004121}
Jiyong Park67882562019-03-21 01:11:21 +09004122
Jooyung Han580eb4f2020-06-24 19:33:06 +09004123func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004124 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004125 apex {
4126 name: "myapex",
4127 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004128 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004129 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004130
Jooyung Han580eb4f2020-06-24 19:33:06 +09004131 apex_key {
4132 name: "myapex.key",
4133 public_key: "testkey.avbpubkey",
4134 private_key: "testkey.pem",
4135 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004136 `)
4137 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004138 rule := module.Output("file_contexts")
4139 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4140}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004141
Jooyung Han580eb4f2020-06-24 19:33:06 +09004142func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004143 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004144 apex {
4145 name: "myapex",
4146 key: "myapex.key",
4147 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004148 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004149 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004150
Jooyung Han580eb4f2020-06-24 19:33:06 +09004151 apex_key {
4152 name: "myapex.key",
4153 public_key: "testkey.avbpubkey",
4154 private_key: "testkey.pem",
4155 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004156 `, withFiles(map[string][]byte{
4157 "my_own_file_contexts": nil,
4158 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004159}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004160
Jooyung Han580eb4f2020-06-24 19:33:06 +09004161func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004162 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
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 `)
4177
Colin Cross1c460562021-02-16 17:55:47 -08004178 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004179 apex {
4180 name: "myapex",
4181 key: "myapex.key",
4182 product_specific: true,
4183 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004184 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004185 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004186
Jooyung Han580eb4f2020-06-24 19:33:06 +09004187 apex_key {
4188 name: "myapex.key",
4189 public_key: "testkey.avbpubkey",
4190 private_key: "testkey.pem",
4191 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004192 `, withFiles(map[string][]byte{
4193 "product_specific_file_contexts": nil,
4194 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004195 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4196 rule := module.Output("file_contexts")
4197 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4198}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004199
Jooyung Han580eb4f2020-06-24 19:33:06 +09004200func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004201 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004202 apex {
4203 name: "myapex",
4204 key: "myapex.key",
4205 product_specific: true,
4206 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004207 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004208 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004209
Jooyung Han580eb4f2020-06-24 19:33:06 +09004210 apex_key {
4211 name: "myapex.key",
4212 public_key: "testkey.avbpubkey",
4213 private_key: "testkey.pem",
4214 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004215
Jooyung Han580eb4f2020-06-24 19:33:06 +09004216 filegroup {
4217 name: "my-file-contexts",
4218 srcs: ["product_specific_file_contexts"],
4219 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004220 `, withFiles(map[string][]byte{
4221 "product_specific_file_contexts": nil,
4222 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004223 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4224 rule := module.Output("file_contexts")
4225 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004226}
4227
Jiyong Park67882562019-03-21 01:11:21 +09004228func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004229 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004230 apex_key {
4231 name: "myapex.key",
4232 public_key: ":my.avbpubkey",
4233 private_key: ":my.pem",
4234 product_specific: true,
4235 }
4236
4237 filegroup {
4238 name: "my.avbpubkey",
4239 srcs: ["testkey2.avbpubkey"],
4240 }
4241
4242 filegroup {
4243 name: "my.pem",
4244 srcs: ["testkey2.pem"],
4245 }
4246 `)
4247
4248 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4249 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004250 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004251 if actual_pubkey != expected_pubkey {
4252 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4253 }
4254 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004255 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004256 if actual_privkey != expected_privkey {
4257 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4258 }
4259}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004260
4261func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004262 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004263 prebuilt_apex {
4264 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004265 arch: {
4266 arm64: {
4267 src: "myapex-arm64.apex",
4268 },
4269 arm: {
4270 src: "myapex-arm.apex",
4271 },
4272 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004273 }
4274 `)
4275
4276 prebuilt := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4277
Jiyong Parkc95714e2019-03-29 14:23:10 +09004278 expectedInput := "myapex-arm64.apex"
4279 if prebuilt.inputApex.String() != expectedInput {
4280 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4281 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004282}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004283
4284func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004285 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004286 prebuilt_apex {
4287 name: "myapex",
4288 src: "myapex-arm.apex",
4289 filename: "notmyapex.apex",
4290 }
4291 `)
4292
4293 p := ctx.ModuleForTests("myapex", "android_common").Module().(*Prebuilt)
4294
4295 expected := "notmyapex.apex"
4296 if p.installFilename != expected {
4297 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4298 }
4299}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004300
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004301func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004302 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004303 prebuilt_apex {
4304 name: "myapex.prebuilt",
4305 src: "myapex-arm.apex",
4306 overrides: [
4307 "myapex",
4308 ],
4309 }
4310 `)
4311
4312 p := ctx.ModuleForTests("myapex.prebuilt", "android_common").Module().(*Prebuilt)
4313
4314 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004315 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004316 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004317 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004318 }
4319}
4320
Paul Duffin092153d2021-01-26 11:42:39 +00004321// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4322// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004323func TestPrebuiltExportDexImplementationJars(t *testing.T) {
4324 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin092153d2021-01-26 11:42:39 +00004325 // Empty transformation.
Paul Duffin064b70c2020-11-02 17:32:38 +00004326 }
4327
Paul Duffin89886cb2021-02-05 16:44:03 +00004328 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004329 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004330 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004331 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004332 stem := android.RemoveOptionalPrebuiltPrefix(name)
4333 if expected, actual := ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar", android.NormalizePathForTesting(dexJarBuildPath); actual != expected {
Paul Duffin064b70c2020-11-02 17:32:38 +00004334 t.Errorf("Incorrect DexJarBuildPath value '%s', expected '%s'", actual, expected)
4335 }
4336 }
4337
Paul Duffin39853512021-02-26 11:09:39 +00004338 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004339 // Make sure that an apex variant is not created for the source module.
Paul Duffin39853512021-02-26 11:09:39 +00004340 if expected, actual := []string{"android_common"}, ctx.ModuleVariantsForTests(name); !reflect.DeepEqual(expected, actual) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004341 t.Errorf("invalid set of variants for %q: expected %q, found %q", "libfoo", expected, actual)
4342 }
4343 }
4344
4345 t.Run("prebuilt only", func(t *testing.T) {
4346 bp := `
4347 prebuilt_apex {
4348 name: "myapex",
4349 arch: {
4350 arm64: {
4351 src: "myapex-arm64.apex",
4352 },
4353 arm: {
4354 src: "myapex-arm.apex",
4355 },
4356 },
Paul Duffin39853512021-02-26 11:09:39 +00004357 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004358 }
4359
4360 java_import {
4361 name: "libfoo",
4362 jars: ["libfoo.jar"],
4363 }
Paul Duffin39853512021-02-26 11:09:39 +00004364
4365 java_sdk_library_import {
4366 name: "libbar",
4367 public: {
4368 jars: ["libbar.jar"],
4369 },
4370 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004371 `
4372
4373 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4374 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4375
Paul Duffinf6932af2021-02-26 18:21:56 +00004376 // Make sure that the deapexer has the correct input APEX.
4377 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4378 rule := deapexer.Rule("deapexer")
4379 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4380 t.Errorf("expected: %q, found: %q", expected, actual)
4381 }
4382
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004383 // Make sure that the prebuilt_apex has the correct input APEX.
4384 prebuiltApex := ctx.ModuleForTests("myapex", "android_common")
4385 rule = prebuiltApex.Rule("android/soong/android.Cp")
4386 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4387 t.Errorf("expected: %q, found: %q", expected, actual)
4388 }
4389
Paul Duffin89886cb2021-02-05 16:44:03 +00004390 checkDexJarBuildPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004391
4392 checkDexJarBuildPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004393 })
4394
4395 t.Run("prebuilt with source preferred", func(t *testing.T) {
4396
4397 bp := `
4398 prebuilt_apex {
4399 name: "myapex",
4400 arch: {
4401 arm64: {
4402 src: "myapex-arm64.apex",
4403 },
4404 arm: {
4405 src: "myapex-arm.apex",
4406 },
4407 },
Paul Duffin39853512021-02-26 11:09:39 +00004408 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004409 }
4410
4411 java_import {
4412 name: "libfoo",
4413 jars: ["libfoo.jar"],
4414 }
4415
4416 java_library {
4417 name: "libfoo",
4418 }
Paul Duffin39853512021-02-26 11:09:39 +00004419
4420 java_sdk_library_import {
4421 name: "libbar",
4422 public: {
4423 jars: ["libbar.jar"],
4424 },
4425 }
4426
4427 java_sdk_library {
4428 name: "libbar",
4429 srcs: ["foo/bar/MyClass.java"],
4430 unsafe_ignore_missing_latest_api: true,
4431 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004432 `
4433
4434 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4435 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4436
Paul Duffin89886cb2021-02-05 16:44:03 +00004437 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004438 ensureNoSourceVariant(t, ctx, "libfoo")
4439
4440 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4441 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004442 })
4443
4444 t.Run("prebuilt preferred with source", func(t *testing.T) {
4445 bp := `
4446 prebuilt_apex {
4447 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004448 arch: {
4449 arm64: {
4450 src: "myapex-arm64.apex",
4451 },
4452 arm: {
4453 src: "myapex-arm.apex",
4454 },
4455 },
Paul Duffin39853512021-02-26 11:09:39 +00004456 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004457 }
4458
4459 java_import {
4460 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004461 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004462 jars: ["libfoo.jar"],
4463 }
4464
4465 java_library {
4466 name: "libfoo",
4467 }
Paul Duffin39853512021-02-26 11:09:39 +00004468
4469 java_sdk_library_import {
4470 name: "libbar",
4471 prefer: true,
4472 public: {
4473 jars: ["libbar.jar"],
4474 },
4475 }
4476
4477 java_sdk_library {
4478 name: "libbar",
4479 srcs: ["foo/bar/MyClass.java"],
4480 unsafe_ignore_missing_latest_api: true,
4481 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004482 `
4483
4484 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4485 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4486
Paul Duffin89886cb2021-02-05 16:44:03 +00004487 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004488 ensureNoSourceVariant(t, ctx, "libfoo")
4489
4490 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
4491 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004492 })
4493}
4494
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004495func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
4496 transform := func(config *dexpreopt.GlobalConfig) {
Paul Duffin37856732021-02-26 14:24:15 +00004497 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo", "myapex:libbar"})
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004498 }
4499
Paul Duffin37856732021-02-26 14:24:15 +00004500 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4501 t.Helper()
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004502 s := ctx.SingletonForTests("dex_bootjars")
4503 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004504 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004505 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004506 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004507 foundLibfooJar = true
4508 buildRule := s.Output(output)
4509 actual := android.NormalizePathForTesting(buildRule.Input)
4510 if actual != bootDexJarPath {
4511 t.Errorf("Incorrect boot dex jar path '%s', expected '%s'", actual, bootDexJarPath)
4512 }
4513 }
4514 }
4515 if !foundLibfooJar {
4516 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs")
4517 }
4518 }
4519
Paul Duffin4fd997b2021-02-03 20:06:33 +00004520 checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004521 t.Helper()
Paul Duffin4fd997b2021-02-03 20:06:33 +00004522 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
4523 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
4524 java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule)
4525 }
4526
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004527 t.Run("prebuilt only", func(t *testing.T) {
4528 bp := `
4529 prebuilt_apex {
4530 name: "myapex",
4531 arch: {
4532 arm64: {
4533 src: "myapex-arm64.apex",
4534 },
4535 arm: {
4536 src: "myapex-arm.apex",
4537 },
4538 },
Paul Duffin37856732021-02-26 14:24:15 +00004539 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004540 }
4541
4542 java_import {
4543 name: "libfoo",
4544 jars: ["libfoo.jar"],
4545 apex_available: ["myapex"],
4546 }
Paul Duffin37856732021-02-26 14:24:15 +00004547
4548 java_sdk_library_import {
4549 name: "libbar",
4550 public: {
4551 jars: ["libbar.jar"],
4552 },
4553 apex_available: ["myapex"],
4554 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004555 `
4556
4557 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004558 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4559 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004560
Paul Duffin9d67ca62021-02-03 20:06:33 +00004561 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4562 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004563.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004564.intermediates/libfoo/android_common_myapex/hiddenapi/index.csv
4565`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004566 })
4567
4568 t.Run("prebuilt with source library preferred", func(t *testing.T) {
4569 bp := `
4570 prebuilt_apex {
4571 name: "myapex",
4572 arch: {
4573 arm64: {
4574 src: "myapex-arm64.apex",
4575 },
4576 arm: {
4577 src: "myapex-arm.apex",
4578 },
4579 },
Paul Duffin37856732021-02-26 14:24:15 +00004580 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004581 }
4582
4583 java_import {
4584 name: "libfoo",
4585 jars: ["libfoo.jar"],
4586 apex_available: ["myapex"],
4587 }
4588
4589 java_library {
4590 name: "libfoo",
4591 srcs: ["foo/bar/MyClass.java"],
4592 apex_available: ["myapex"],
4593 }
Paul Duffin37856732021-02-26 14:24:15 +00004594
4595 java_sdk_library_import {
4596 name: "libbar",
4597 public: {
4598 jars: ["libbar.jar"],
4599 },
4600 apex_available: ["myapex"],
4601 }
4602
4603 java_sdk_library {
4604 name: "libbar",
4605 srcs: ["foo/bar/MyClass.java"],
4606 unsafe_ignore_missing_latest_api: true,
4607 apex_available: ["myapex"],
4608 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004609 `
4610
4611 // In this test the source (java_library) libfoo is active since the
4612 // prebuilt (java_import) defaults to prefer:false. However the
4613 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
4614 // find the dex boot jar in it. We either need to disable the source libfoo
4615 // or make the prebuilt libfoo preferred.
4616 testDexpreoptWithApexes(t, bp, "failed to find a dex jar path for module 'libfoo'", transform)
4617 })
4618
4619 t.Run("prebuilt library preferred with source", func(t *testing.T) {
4620 bp := `
4621 prebuilt_apex {
4622 name: "myapex",
4623 arch: {
4624 arm64: {
4625 src: "myapex-arm64.apex",
4626 },
4627 arm: {
4628 src: "myapex-arm.apex",
4629 },
4630 },
Paul Duffin37856732021-02-26 14:24:15 +00004631 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004632 }
4633
4634 java_import {
4635 name: "libfoo",
4636 prefer: true,
4637 jars: ["libfoo.jar"],
4638 apex_available: ["myapex"],
4639 }
4640
4641 java_library {
4642 name: "libfoo",
4643 srcs: ["foo/bar/MyClass.java"],
4644 apex_available: ["myapex"],
4645 }
Paul Duffin37856732021-02-26 14:24:15 +00004646
4647 java_sdk_library_import {
4648 name: "libbar",
4649 prefer: true,
4650 public: {
4651 jars: ["libbar.jar"],
4652 },
4653 apex_available: ["myapex"],
4654 }
4655
4656 java_sdk_library {
4657 name: "libbar",
4658 srcs: ["foo/bar/MyClass.java"],
4659 unsafe_ignore_missing_latest_api: true,
4660 apex_available: ["myapex"],
4661 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004662 `
4663
4664 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004665 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4666 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004667
Paul Duffin9d67ca62021-02-03 20:06:33 +00004668 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4669 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004670.intermediates/prebuilt_libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004671.intermediates/prebuilt_libfoo/android_common_myapex/hiddenapi/index.csv
4672`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004673 })
4674
4675 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
4676 bp := `
4677 apex {
4678 name: "myapex",
4679 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00004680 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004681 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004682 }
4683
4684 apex_key {
4685 name: "myapex.key",
4686 public_key: "testkey.avbpubkey",
4687 private_key: "testkey.pem",
4688 }
4689
4690 prebuilt_apex {
4691 name: "myapex",
4692 arch: {
4693 arm64: {
4694 src: "myapex-arm64.apex",
4695 },
4696 arm: {
4697 src: "myapex-arm.apex",
4698 },
4699 },
Paul Duffin37856732021-02-26 14:24:15 +00004700 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004701 }
4702
4703 java_import {
4704 name: "libfoo",
4705 jars: ["libfoo.jar"],
4706 apex_available: ["myapex"],
4707 }
4708
4709 java_library {
4710 name: "libfoo",
4711 srcs: ["foo/bar/MyClass.java"],
4712 apex_available: ["myapex"],
4713 }
Paul Duffin37856732021-02-26 14:24:15 +00004714
4715 java_sdk_library_import {
4716 name: "libbar",
4717 public: {
4718 jars: ["libbar.jar"],
4719 },
4720 apex_available: ["myapex"],
4721 }
4722
4723 java_sdk_library {
4724 name: "libbar",
4725 srcs: ["foo/bar/MyClass.java"],
4726 unsafe_ignore_missing_latest_api: true,
4727 apex_available: ["myapex"],
4728 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004729 `
4730
4731 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004732 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
4733 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004734
4735 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4736 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004737.intermediates/libbar/android_common_myapex/hiddenapi/index.csv
Paul Duffin4fd997b2021-02-03 20:06:33 +00004738.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv
4739`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004740 })
4741
4742 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
4743 bp := `
4744 apex {
4745 name: "myapex",
4746 enabled: false,
4747 key: "myapex.key",
4748 java_libs: ["libfoo"],
4749 }
4750
4751 apex_key {
4752 name: "myapex.key",
4753 public_key: "testkey.avbpubkey",
4754 private_key: "testkey.pem",
4755 }
4756
4757 prebuilt_apex {
4758 name: "myapex",
4759 arch: {
4760 arm64: {
4761 src: "myapex-arm64.apex",
4762 },
4763 arm: {
4764 src: "myapex-arm.apex",
4765 },
4766 },
Paul Duffin37856732021-02-26 14:24:15 +00004767 exported_java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004768 }
4769
4770 java_import {
4771 name: "libfoo",
4772 prefer: true,
4773 jars: ["libfoo.jar"],
4774 apex_available: ["myapex"],
4775 }
4776
4777 java_library {
4778 name: "libfoo",
4779 srcs: ["foo/bar/MyClass.java"],
4780 apex_available: ["myapex"],
4781 }
Paul Duffin37856732021-02-26 14:24:15 +00004782
4783 java_sdk_library_import {
4784 name: "libbar",
4785 prefer: true,
4786 public: {
4787 jars: ["libbar.jar"],
4788 },
4789 apex_available: ["myapex"],
4790 }
4791
4792 java_sdk_library {
4793 name: "libbar",
4794 srcs: ["foo/bar/MyClass.java"],
4795 unsafe_ignore_missing_latest_api: true,
4796 apex_available: ["myapex"],
4797 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004798 `
4799
4800 ctx := testDexpreoptWithApexes(t, bp, "", transform)
Paul Duffin37856732021-02-26 14:24:15 +00004801 checkBootDexJarPath(t, ctx, "libfoo", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4802 checkBootDexJarPath(t, ctx, "libbar", ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004803
Paul Duffin9d67ca62021-02-03 20:06:33 +00004804 // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file.
4805 checkHiddenAPIIndexInputs(t, ctx, `
Paul Duffin37856732021-02-26 14:24:15 +00004806.intermediates/prebuilt_libbar/android_common_prebuilt_myapex/hiddenapi/index.csv
Paul Duffin9d67ca62021-02-03 20:06:33 +00004807.intermediates/prebuilt_libfoo/android_common_prebuilt_myapex/hiddenapi/index.csv
4808`)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004809 })
4810}
4811
Roland Levillain630846d2019-06-26 12:48:34 +01004812func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004813 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01004814 apex_test {
4815 name: "myapex",
4816 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004817 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01004818 tests: [
4819 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01004820 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01004821 ],
4822 }
4823
4824 apex_key {
4825 name: "myapex.key",
4826 public_key: "testkey.avbpubkey",
4827 private_key: "testkey.pem",
4828 }
4829
Liz Kammer1c14a212020-05-12 15:26:55 -07004830 filegroup {
4831 name: "fg",
4832 srcs: [
4833 "baz",
4834 "bar/baz"
4835 ],
4836 }
4837
Roland Levillain630846d2019-06-26 12:48:34 +01004838 cc_test {
4839 name: "mytest",
4840 gtest: false,
4841 srcs: ["mytest.cpp"],
4842 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004843 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01004844 system_shared_libs: [],
4845 static_executable: true,
4846 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07004847 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01004848 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01004849
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004850 cc_library {
4851 name: "mylib",
4852 srcs: ["mylib.cpp"],
4853 system_shared_libs: [],
4854 stl: "none",
4855 }
4856
Liz Kammer5bd365f2020-05-27 15:15:11 -07004857 filegroup {
4858 name: "fg2",
4859 srcs: [
4860 "testdata/baz"
4861 ],
4862 }
4863
Roland Levillain9b5fde92019-06-28 15:41:19 +01004864 cc_test {
4865 name: "mytests",
4866 gtest: false,
4867 srcs: [
4868 "mytest1.cpp",
4869 "mytest2.cpp",
4870 "mytest3.cpp",
4871 ],
4872 test_per_src: true,
4873 relative_install_path: "test",
4874 system_shared_libs: [],
4875 static_executable: true,
4876 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07004877 data: [
4878 ":fg",
4879 ":fg2",
4880 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01004881 }
Roland Levillain630846d2019-06-26 12:48:34 +01004882 `)
4883
Sundong Ahnabb64432019-10-22 13:58:29 +09004884 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01004885 copyCmds := apexRule.Args["copy_commands"]
4886
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004887 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01004888 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09004889 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01004890
Liz Kammer1c14a212020-05-12 15:26:55 -07004891 //Ensure that test data are copied into apex.
4892 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
4893 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
4894
Roland Levillain9b5fde92019-06-28 15:41:19 +01004895 // Ensure that test deps built with `test_per_src` are copied into apex.
4896 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
4897 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
4898 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01004899
4900 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07004901 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004902 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004903 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01004904 prefix := "TARGET_"
4905 var builder strings.Builder
4906 data.Custom(&builder, name, prefix, "", data)
4907 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09004908 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
4909 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
4910 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
4911 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09004912 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09004913 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01004914 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07004915
4916 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004917 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07004918 data.Custom(&builder, name, prefix, "", data)
4919 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07004920 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
4921 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01004922}
4923
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004924func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004925 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004926 apex {
4927 name: "myapex",
4928 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004929 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004930 }
4931 apex_key {
4932 name: "myapex.key",
4933 public_key: "testkey.avbpubkey",
4934 private_key: "testkey.pem",
4935 }
4936 `, func(fs map[string][]byte, config android.Config) {
4937 config.TestProductVariables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
4938 })
4939 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09004940 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07004941 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09004942 var builder strings.Builder
4943 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
4944 androidMk := builder.String()
4945 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
4946}
4947
Jooyung Hand48f3c32019-08-23 11:18:57 +09004948func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
4949 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
4950 apex {
4951 name: "myapex",
4952 key: "myapex.key",
4953 native_shared_libs: ["libfoo"],
4954 }
4955
4956 apex_key {
4957 name: "myapex.key",
4958 public_key: "testkey.avbpubkey",
4959 private_key: "testkey.pem",
4960 }
4961
4962 cc_library {
4963 name: "libfoo",
4964 stl: "none",
4965 system_shared_libs: [],
4966 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004967 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004968 }
4969 `)
4970 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
4971 apex {
4972 name: "myapex",
4973 key: "myapex.key",
4974 java_libs: ["myjar"],
4975 }
4976
4977 apex_key {
4978 name: "myapex.key",
4979 public_key: "testkey.avbpubkey",
4980 private_key: "testkey.pem",
4981 }
4982
4983 java_library {
4984 name: "myjar",
4985 srcs: ["foo/bar/MyClass.java"],
4986 sdk_version: "none",
4987 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09004988 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09004989 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09004990 }
4991 `)
4992}
4993
Bill Peckhama41a6962021-01-11 10:58:54 -08004994func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004995 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08004996 apex {
4997 name: "myapex",
4998 key: "myapex.key",
4999 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005000 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005001 }
5002
5003 apex_key {
5004 name: "myapex.key",
5005 public_key: "testkey.avbpubkey",
5006 private_key: "testkey.pem",
5007 }
5008
5009 java_import {
5010 name: "myjavaimport",
5011 apex_available: ["myapex"],
5012 jars: ["my.jar"],
5013 compile_dex: true,
5014 }
5015 `)
5016
5017 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5018 apexRule := module.Rule("apexRule")
5019 copyCmds := apexRule.Args["copy_commands"]
5020 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5021}
5022
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005023func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005024 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005025 apex {
5026 name: "myapex",
5027 key: "myapex.key",
5028 apps: [
5029 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005030 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005031 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005032 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005033 }
5034
5035 apex_key {
5036 name: "myapex.key",
5037 public_key: "testkey.avbpubkey",
5038 private_key: "testkey.pem",
5039 }
5040
5041 android_app {
5042 name: "AppFoo",
5043 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005044 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005045 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005046 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005047 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005048 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005049 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005050
5051 android_app {
5052 name: "AppFooPriv",
5053 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005054 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005055 system_modules: "none",
5056 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005057 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005058 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005059 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005060
5061 cc_library_shared {
5062 name: "libjni",
5063 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005064 shared_libs: ["libfoo"],
5065 stl: "none",
5066 system_shared_libs: [],
5067 apex_available: [ "myapex" ],
5068 sdk_version: "current",
5069 }
5070
5071 cc_library_shared {
5072 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005073 stl: "none",
5074 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005075 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005076 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005077 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005078 `)
5079
Sundong Ahnabb64432019-10-22 13:58:29 +09005080 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005081 apexRule := module.Rule("apexRule")
5082 copyCmds := apexRule.Args["copy_commands"]
5083
5084 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005085 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005086
Colin Crossaede88c2020-08-11 12:17:01 -07005087 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005088 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005089 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005090 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005091 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005092 // JNI libraries including transitive deps are
5093 for _, jni := range []string{"libjni", "libfoo"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005094 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005095 // ... embedded inside APK (jnilibs.zip)
5096 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5097 // ... and not directly inside the APEX
5098 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5099 }
Dario Frenicde2a032019-10-27 00:29:22 +01005100}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005101
Dario Frenicde2a032019-10-27 00:29:22 +01005102func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005103 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005104 apex {
5105 name: "myapex",
5106 key: "myapex.key",
5107 apps: [
5108 "AppFooPrebuilt",
5109 "AppFooPrivPrebuilt",
5110 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005111 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005112 }
5113
5114 apex_key {
5115 name: "myapex.key",
5116 public_key: "testkey.avbpubkey",
5117 private_key: "testkey.pem",
5118 }
5119
5120 android_app_import {
5121 name: "AppFooPrebuilt",
5122 apk: "PrebuiltAppFoo.apk",
5123 presigned: true,
5124 dex_preopt: {
5125 enabled: false,
5126 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005127 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005128 }
5129
5130 android_app_import {
5131 name: "AppFooPrivPrebuilt",
5132 apk: "PrebuiltAppFooPriv.apk",
5133 privileged: true,
5134 presigned: true,
5135 dex_preopt: {
5136 enabled: false,
5137 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005138 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005139 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005140 }
5141 `)
5142
Sundong Ahnabb64432019-10-22 13:58:29 +09005143 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005144 apexRule := module.Rule("apexRule")
5145 copyCmds := apexRule.Args["copy_commands"]
5146
5147 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005148 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5149}
5150
5151func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005152 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005153 apex {
5154 name: "myapex",
5155 key: "myapex.key",
5156 apps: [
5157 "AppFoo",
5158 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005159 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005160 }
5161
5162 apex_key {
5163 name: "myapex.key",
5164 public_key: "testkey.avbpubkey",
5165 private_key: "testkey.pem",
5166 }
5167
5168 android_app {
5169 name: "AppFoo",
5170 srcs: ["foo/bar/MyClass.java"],
5171 sdk_version: "none",
5172 system_modules: "none",
5173 apex_available: [ "myapex" ],
5174 }
5175
5176 android_app_import {
5177 name: "AppFoo",
5178 apk: "AppFooPrebuilt.apk",
5179 filename: "AppFooPrebuilt.apk",
5180 presigned: true,
5181 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005182 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005183 }
5184 `, withFiles(map[string][]byte{
5185 "AppFooPrebuilt.apk": nil,
5186 }))
5187
5188 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005189 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005190 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005191}
5192
Dario Freni6f3937c2019-12-20 22:58:03 +00005193func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005194 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005195 apex {
5196 name: "myapex",
5197 key: "myapex.key",
5198 apps: [
5199 "TesterHelpAppFoo",
5200 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005201 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005202 }
5203
5204 apex_key {
5205 name: "myapex.key",
5206 public_key: "testkey.avbpubkey",
5207 private_key: "testkey.pem",
5208 }
5209
5210 android_test_helper_app {
5211 name: "TesterHelpAppFoo",
5212 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005213 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005214 }
5215
5216 `)
5217
5218 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5219 apexRule := module.Rule("apexRule")
5220 copyCmds := apexRule.Args["copy_commands"]
5221
5222 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5223}
5224
Jooyung Han18020ea2019-11-13 10:50:48 +09005225func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5226 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005227 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005228 apex {
5229 name: "myapex",
5230 key: "myapex.key",
5231 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005232 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005233 }
5234
5235 apex_key {
5236 name: "myapex.key",
5237 public_key: "testkey.avbpubkey",
5238 private_key: "testkey.pem",
5239 }
5240
5241 apex {
5242 name: "otherapex",
5243 key: "myapex.key",
5244 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005245 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005246 }
5247
5248 cc_defaults {
5249 name: "libfoo-defaults",
5250 apex_available: ["otherapex"],
5251 }
5252
5253 cc_library {
5254 name: "libfoo",
5255 defaults: ["libfoo-defaults"],
5256 stl: "none",
5257 system_shared_libs: [],
5258 }`)
5259}
5260
Paul Duffine52e66f2020-03-30 17:54:29 +01005261func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005262 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005263 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005264 apex {
5265 name: "myapex",
5266 key: "myapex.key",
5267 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005268 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005269 }
5270
5271 apex_key {
5272 name: "myapex.key",
5273 public_key: "testkey.avbpubkey",
5274 private_key: "testkey.pem",
5275 }
5276
5277 apex {
5278 name: "otherapex",
5279 key: "otherapex.key",
5280 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005281 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005282 }
5283
5284 apex_key {
5285 name: "otherapex.key",
5286 public_key: "testkey.avbpubkey",
5287 private_key: "testkey.pem",
5288 }
5289
5290 cc_library {
5291 name: "libfoo",
5292 stl: "none",
5293 system_shared_libs: [],
5294 apex_available: ["otherapex"],
5295 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005296}
Jiyong Park127b40b2019-09-30 16:04:35 +09005297
Paul Duffine52e66f2020-03-30 17:54:29 +01005298func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005299 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005300 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005301.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005302.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005303.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005304.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005305.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005306.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005307 apex {
5308 name: "myapex",
5309 key: "myapex.key",
5310 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005311 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005312 }
5313
5314 apex_key {
5315 name: "myapex.key",
5316 public_key: "testkey.avbpubkey",
5317 private_key: "testkey.pem",
5318 }
5319
Jiyong Park127b40b2019-09-30 16:04:35 +09005320 cc_library {
5321 name: "libfoo",
5322 stl: "none",
5323 shared_libs: ["libbar"],
5324 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005325 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005326 }
5327
5328 cc_library {
5329 name: "libbar",
5330 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005331 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005332 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005333 apex_available: ["myapex"],
5334 }
5335
5336 cc_library {
5337 name: "libbaz",
5338 stl: "none",
5339 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005340 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005341}
Jiyong Park127b40b2019-09-30 16:04:35 +09005342
Paul Duffine52e66f2020-03-30 17:54:29 +01005343func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005344 testApexError(t, "\"otherapex\" is not a valid module name", `
5345 apex {
5346 name: "myapex",
5347 key: "myapex.key",
5348 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005349 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005350 }
5351
5352 apex_key {
5353 name: "myapex.key",
5354 public_key: "testkey.avbpubkey",
5355 private_key: "testkey.pem",
5356 }
5357
5358 cc_library {
5359 name: "libfoo",
5360 stl: "none",
5361 system_shared_libs: [],
5362 apex_available: ["otherapex"],
5363 }`)
5364
Paul Duffine52e66f2020-03-30 17:54:29 +01005365 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005366 apex {
5367 name: "myapex",
5368 key: "myapex.key",
5369 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005370 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005371 }
5372
5373 apex_key {
5374 name: "myapex.key",
5375 public_key: "testkey.avbpubkey",
5376 private_key: "testkey.pem",
5377 }
5378
5379 cc_library {
5380 name: "libfoo",
5381 stl: "none",
5382 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005383 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005384 apex_available: ["myapex"],
5385 }
5386
5387 cc_library {
5388 name: "libbar",
5389 stl: "none",
5390 system_shared_libs: [],
5391 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005392 }
5393
5394 cc_library {
5395 name: "libbaz",
5396 stl: "none",
5397 system_shared_libs: [],
5398 stubs: {
5399 versions: ["10", "20", "30"],
5400 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005401 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005402}
Jiyong Park127b40b2019-09-30 16:04:35 +09005403
Jiyong Park89e850a2020-04-07 16:37:39 +09005404func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005405 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005406 apex {
5407 name: "myapex",
5408 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005409 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005410 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005411 }
5412
5413 apex_key {
5414 name: "myapex.key",
5415 public_key: "testkey.avbpubkey",
5416 private_key: "testkey.pem",
5417 }
5418
5419 cc_library {
5420 name: "libfoo",
5421 stl: "none",
5422 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005423 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005424 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005425 }
5426
5427 cc_library {
5428 name: "libfoo2",
5429 stl: "none",
5430 system_shared_libs: [],
5431 shared_libs: ["libbaz"],
5432 apex_available: ["//apex_available:platform"],
5433 }
5434
5435 cc_library {
5436 name: "libbar",
5437 stl: "none",
5438 system_shared_libs: [],
5439 apex_available: ["myapex"],
5440 }
5441
5442 cc_library {
5443 name: "libbaz",
5444 stl: "none",
5445 system_shared_libs: [],
5446 apex_available: ["myapex"],
5447 stubs: {
5448 versions: ["1"],
5449 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005450 }`)
5451
Jiyong Park89e850a2020-04-07 16:37:39 +09005452 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5453 // because it depends on libbar which isn't available to platform
5454 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5455 if libfoo.NotAvailableForPlatform() != true {
5456 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5457 }
5458
5459 // libfoo2 however can be available to platform because it depends on libbaz which provides
5460 // stubs
5461 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5462 if libfoo2.NotAvailableForPlatform() == true {
5463 t.Errorf("%q should be available to platform", libfoo2.String())
5464 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005465}
Jiyong Parka90ca002019-10-07 15:47:24 +09005466
Paul Duffine52e66f2020-03-30 17:54:29 +01005467func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005468 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005469 apex {
5470 name: "myapex",
5471 key: "myapex.key",
5472 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005473 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09005474 }
5475
5476 apex_key {
5477 name: "myapex.key",
5478 public_key: "testkey.avbpubkey",
5479 private_key: "testkey.pem",
5480 }
5481
5482 cc_library {
5483 name: "libfoo",
5484 stl: "none",
5485 system_shared_libs: [],
5486 apex_available: ["myapex"],
5487 static: {
5488 apex_available: ["//apex_available:platform"],
5489 },
5490 }`)
5491
Jiyong Park89e850a2020-04-07 16:37:39 +09005492 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5493 if libfooShared.NotAvailableForPlatform() != true {
5494 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
5495 }
5496 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
5497 if libfooStatic.NotAvailableForPlatform() != false {
5498 t.Errorf("%q should be available to platform", libfooStatic.String())
5499 }
Jiyong Park127b40b2019-09-30 16:04:35 +09005500}
5501
Jiyong Park5d790c32019-11-15 18:40:32 +09005502func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005503 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09005504 apex {
5505 name: "myapex",
5506 key: "myapex.key",
5507 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005508 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005509 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09005510 }
5511
5512 override_apex {
5513 name: "override_myapex",
5514 base: "myapex",
5515 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005516 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08005517 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005518 package_name: "test.overridden.package",
Jiyong Park5d790c32019-11-15 18:40:32 +09005519 }
5520
5521 apex_key {
5522 name: "myapex.key",
5523 public_key: "testkey.avbpubkey",
5524 private_key: "testkey.pem",
5525 }
5526
5527 android_app {
5528 name: "app",
5529 srcs: ["foo/bar/MyClass.java"],
5530 package_name: "foo",
5531 sdk_version: "none",
5532 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005533 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09005534 }
5535
5536 override_android_app {
5537 name: "override_app",
5538 base: "app",
5539 package_name: "bar",
5540 }
Jiyong Park20bacab2020-03-03 11:45:41 +09005541 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09005542
Jiyong Park317645e2019-12-05 13:20:58 +09005543 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
5544 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
5545 if originalVariant.GetOverriddenBy() != "" {
5546 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
5547 }
5548 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
5549 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
5550 }
5551
Jiyong Park5d790c32019-11-15 18:40:32 +09005552 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
5553 apexRule := module.Rule("apexRule")
5554 copyCmds := apexRule.Args["copy_commands"]
5555
5556 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005557 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005558
5559 apexBundle := module.Module().(*apexBundle)
5560 name := apexBundle.Name()
5561 if name != "override_myapex" {
5562 t.Errorf("name should be \"override_myapex\", but was %q", name)
5563 }
5564
Baligh Uddin004d7172020-02-19 21:29:28 -08005565 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
5566 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
5567 }
5568
Jiyong Park20bacab2020-03-03 11:45:41 +09005569 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07005570 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jiyong Park20bacab2020-03-03 11:45:41 +09005571
Colin Crossaa255532020-07-03 13:18:24 -07005572 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005573 var builder strings.Builder
5574 data.Custom(&builder, name, "TARGET_", "", data)
5575 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09005576 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005577 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
5578 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08005579 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005580 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09005581 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08005582 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
5583 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09005584}
5585
Jooyung Han214bf372019-11-12 13:03:50 +09005586func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005587 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09005588 apex {
5589 name: "myapex",
5590 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005591 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09005592 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09005593 }
5594
5595 apex_key {
5596 name: "myapex.key",
5597 public_key: "testkey.avbpubkey",
5598 private_key: "testkey.pem",
5599 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005600
5601 cc_library {
5602 name: "mylib",
5603 srcs: ["mylib.cpp"],
5604 stl: "libc++",
5605 system_shared_libs: [],
5606 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09005607 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005608 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005609 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09005610
5611 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5612 args := module.Rule("apexRule").Args
5613 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00005614 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005615
5616 // The copies of the libraries in the apex should have one more dependency than
5617 // the ones outside the apex, namely the unwinder. Ideally we should check
5618 // the dependency names directly here but for some reason the names are blank in
5619 // this test.
5620 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07005621 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08005622 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
5623 if len(apexImplicits) != len(nonApexImplicits)+1 {
5624 t.Errorf("%q missing unwinder dep", lib)
5625 }
5626 }
Jooyung Han214bf372019-11-12 13:03:50 +09005627}
5628
Paul Duffine05480a2021-03-08 15:07:14 +00005629var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01005630 "api/current.txt": nil,
5631 "api/removed.txt": nil,
5632 "api/system-current.txt": nil,
5633 "api/system-removed.txt": nil,
5634 "api/test-current.txt": nil,
5635 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01005636
Anton Hanssondff2c782020-12-21 17:10:01 +00005637 "100/public/api/foo.txt": nil,
5638 "100/public/api/foo-removed.txt": nil,
5639 "100/system/api/foo.txt": nil,
5640 "100/system/api/foo-removed.txt": nil,
5641
Paul Duffineedc5d52020-06-12 17:46:39 +01005642 // For java_sdk_library_import
5643 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01005644}
5645
Jooyung Han58f26ab2019-12-18 15:34:32 +09005646func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005647 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09005648 apex {
5649 name: "myapex",
5650 key: "myapex.key",
5651 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005652 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09005653 }
5654
5655 apex_key {
5656 name: "myapex.key",
5657 public_key: "testkey.avbpubkey",
5658 private_key: "testkey.pem",
5659 }
5660
5661 java_sdk_library {
5662 name: "foo",
5663 srcs: ["a.java"],
5664 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005665 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09005666 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005667
5668 prebuilt_apis {
5669 name: "sdk",
5670 api_dirs: ["100"],
5671 }
Paul Duffin9b879592020-05-26 13:21:35 +01005672 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09005673
5674 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00005675 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09005676 "javalib/foo.jar",
5677 "etc/permissions/foo.xml",
5678 })
5679 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09005680 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
5681 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09005682}
5683
Paul Duffin9b879592020-05-26 13:21:35 +01005684func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005685 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005686 apex {
5687 name: "myapex",
5688 key: "myapex.key",
5689 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005690 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005691 }
5692
5693 apex_key {
5694 name: "myapex.key",
5695 public_key: "testkey.avbpubkey",
5696 private_key: "testkey.pem",
5697 }
5698
5699 java_sdk_library {
5700 name: "foo",
5701 srcs: ["a.java"],
5702 api_packages: ["foo"],
5703 apex_available: ["myapex"],
5704 sdk_version: "none",
5705 system_modules: "none",
5706 }
5707
5708 java_library {
5709 name: "bar",
5710 srcs: ["a.java"],
5711 libs: ["foo"],
5712 apex_available: ["myapex"],
5713 sdk_version: "none",
5714 system_modules: "none",
5715 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005716
5717 prebuilt_apis {
5718 name: "sdk",
5719 api_dirs: ["100"],
5720 }
Paul Duffin9b879592020-05-26 13:21:35 +01005721 `, withFiles(filesForSdkLibrary))
5722
5723 // java_sdk_library installs both impl jar and permission XML
5724 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5725 "javalib/bar.jar",
5726 "javalib/foo.jar",
5727 "etc/permissions/foo.xml",
5728 })
5729
5730 // The bar library should depend on the implementation jar.
5731 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5732 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5733 t.Errorf("expected %q, found %#q", expected, actual)
5734 }
5735}
5736
5737func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005738 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01005739 apex {
5740 name: "myapex",
5741 key: "myapex.key",
5742 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005743 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01005744 }
5745
5746 apex_key {
5747 name: "myapex.key",
5748 public_key: "testkey.avbpubkey",
5749 private_key: "testkey.pem",
5750 }
5751
5752 java_sdk_library {
5753 name: "foo",
5754 srcs: ["a.java"],
5755 api_packages: ["foo"],
5756 apex_available: ["myapex"],
5757 sdk_version: "none",
5758 system_modules: "none",
5759 }
5760
5761 java_library {
5762 name: "bar",
5763 srcs: ["a.java"],
5764 libs: ["foo"],
5765 sdk_version: "none",
5766 system_modules: "none",
5767 }
Anton Hanssondff2c782020-12-21 17:10:01 +00005768
5769 prebuilt_apis {
5770 name: "sdk",
5771 api_dirs: ["100"],
5772 }
Paul Duffin9b879592020-05-26 13:21:35 +01005773 `, withFiles(filesForSdkLibrary))
5774
5775 // java_sdk_library installs both impl jar and permission XML
5776 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5777 "javalib/foo.jar",
5778 "etc/permissions/foo.xml",
5779 })
5780
5781 // The bar library should depend on the stubs jar.
5782 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
5783 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5784 t.Errorf("expected %q, found %#q", expected, actual)
5785 }
5786}
5787
Paul Duffineedc5d52020-06-12 17:46:39 +01005788func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005789 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00005790 prebuilt_apis {
5791 name: "sdk",
5792 api_dirs: ["100"],
5793 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01005794 withFiles(map[string][]byte{
5795 "apex/a.java": nil,
5796 "apex/apex_manifest.json": nil,
5797 "apex/Android.bp": []byte(`
5798 package {
5799 default_visibility: ["//visibility:private"],
5800 }
5801
5802 apex {
5803 name: "myapex",
5804 key: "myapex.key",
5805 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005806 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005807 }
5808
5809 apex_key {
5810 name: "myapex.key",
5811 public_key: "testkey.avbpubkey",
5812 private_key: "testkey.pem",
5813 }
5814
5815 java_library {
5816 name: "bar",
5817 srcs: ["a.java"],
5818 libs: ["foo"],
5819 apex_available: ["myapex"],
5820 sdk_version: "none",
5821 system_modules: "none",
5822 }
5823`),
5824 "source/a.java": nil,
5825 "source/api/current.txt": nil,
5826 "source/api/removed.txt": nil,
5827 "source/Android.bp": []byte(`
5828 package {
5829 default_visibility: ["//visibility:private"],
5830 }
5831
5832 java_sdk_library {
5833 name: "foo",
5834 visibility: ["//apex"],
5835 srcs: ["a.java"],
5836 api_packages: ["foo"],
5837 apex_available: ["myapex"],
5838 sdk_version: "none",
5839 system_modules: "none",
5840 public: {
5841 enabled: true,
5842 },
5843 }
5844`),
5845 "prebuilt/a.jar": nil,
5846 "prebuilt/Android.bp": []byte(`
5847 package {
5848 default_visibility: ["//visibility:private"],
5849 }
5850
5851 java_sdk_library_import {
5852 name: "foo",
5853 visibility: ["//apex", "//source"],
5854 apex_available: ["myapex"],
5855 prefer: true,
5856 public: {
5857 jars: ["a.jar"],
5858 },
5859 }
5860`),
Anton Hanssondff2c782020-12-21 17:10:01 +00005861 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01005862 )
5863
5864 // java_sdk_library installs both impl jar and permission XML
5865 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5866 "javalib/bar.jar",
5867 "javalib/foo.jar",
5868 "etc/permissions/foo.xml",
5869 })
5870
5871 // The bar library should depend on the implementation jar.
5872 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
5873 if expected, actual := `^-classpath /[^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
5874 t.Errorf("expected %q, found %#q", expected, actual)
5875 }
5876}
5877
5878func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
5879 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
5880 apex {
5881 name: "myapex",
5882 key: "myapex.key",
5883 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005884 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01005885 }
5886
5887 apex_key {
5888 name: "myapex.key",
5889 public_key: "testkey.avbpubkey",
5890 private_key: "testkey.pem",
5891 }
5892
5893 java_sdk_library_import {
5894 name: "foo",
5895 apex_available: ["myapex"],
5896 prefer: true,
5897 public: {
5898 jars: ["a.jar"],
5899 },
5900 }
5901
5902 `, withFiles(filesForSdkLibrary))
5903}
5904
atrost6e126252020-01-27 17:01:16 +00005905func TestCompatConfig(t *testing.T) {
Paul Duffina369c7b2021-03-09 03:08:05 +00005906 result := apexFixtureFactory.
5907 Extend(java.PrepareForTestWithPlatformCompatConfig).
5908 RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00005909 apex {
5910 name: "myapex",
5911 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00005912 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00005913 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005914 updatable: false,
atrost6e126252020-01-27 17:01:16 +00005915 }
5916
5917 apex_key {
5918 name: "myapex.key",
5919 public_key: "testkey.avbpubkey",
5920 private_key: "testkey.pem",
5921 }
5922
5923 platform_compat_config {
5924 name: "myjar-platform-compat-config",
5925 src: ":myjar",
5926 }
5927
5928 java_library {
5929 name: "myjar",
5930 srcs: ["foo/bar/MyClass.java"],
5931 sdk_version: "none",
5932 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00005933 apex_available: [ "myapex" ],
5934 }
Paul Duffin1b29e002021-03-16 15:06:54 +00005935
5936 // Make sure that a preferred prebuilt does not affect the apex contents.
5937 prebuilt_platform_compat_config {
5938 name: "myjar-platform-compat-config",
5939 metadata: "compat-config/metadata.xml",
5940 prefer: true,
5941 }
atrost6e126252020-01-27 17:01:16 +00005942 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00005943 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00005944 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
5945 "etc/compatconfig/myjar-platform-compat-config.xml",
5946 "javalib/myjar.jar",
5947 })
5948}
5949
Jiyong Park479321d2019-12-16 11:47:12 +09005950func TestRejectNonInstallableJavaLibrary(t *testing.T) {
5951 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
5952 apex {
5953 name: "myapex",
5954 key: "myapex.key",
5955 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005956 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09005957 }
5958
5959 apex_key {
5960 name: "myapex.key",
5961 public_key: "testkey.avbpubkey",
5962 private_key: "testkey.pem",
5963 }
5964
5965 java_library {
5966 name: "myjar",
5967 srcs: ["foo/bar/MyClass.java"],
5968 sdk_version: "none",
5969 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09005970 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005971 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09005972 }
5973 `)
5974}
5975
Jiyong Park7afd1072019-12-30 16:56:33 +09005976func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005977 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09005978 apex {
5979 name: "myapex",
5980 key: "myapex.key",
5981 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005982 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09005983 }
5984
5985 apex_key {
5986 name: "myapex.key",
5987 public_key: "testkey.avbpubkey",
5988 private_key: "testkey.pem",
5989 }
5990
5991 cc_library {
5992 name: "mylib",
5993 srcs: ["mylib.cpp"],
5994 system_shared_libs: [],
5995 stl: "none",
5996 required: ["a", "b"],
5997 host_required: ["c", "d"],
5998 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005999 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006000 }
6001 `)
6002
6003 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006004 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006005 name := apexBundle.BaseModuleName()
6006 prefix := "TARGET_"
6007 var builder strings.Builder
6008 data.Custom(&builder, name, prefix, "", data)
6009 androidMk := builder.String()
6010 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6011 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6012 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6013}
6014
Jiyong Park7cd10e32020-01-14 09:22:18 +09006015func TestSymlinksFromApexToSystem(t *testing.T) {
6016 bp := `
6017 apex {
6018 name: "myapex",
6019 key: "myapex.key",
6020 native_shared_libs: ["mylib"],
6021 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006022 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006023 }
6024
Jiyong Park9d677202020-02-19 16:29:35 +09006025 apex {
6026 name: "myapex.updatable",
6027 key: "myapex.key",
6028 native_shared_libs: ["mylib"],
6029 java_libs: ["myjar"],
6030 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006031 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006032 }
6033
Jiyong Park7cd10e32020-01-14 09:22:18 +09006034 apex_key {
6035 name: "myapex.key",
6036 public_key: "testkey.avbpubkey",
6037 private_key: "testkey.pem",
6038 }
6039
6040 cc_library {
6041 name: "mylib",
6042 srcs: ["mylib.cpp"],
6043 shared_libs: ["myotherlib"],
6044 system_shared_libs: [],
6045 stl: "none",
6046 apex_available: [
6047 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006048 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006049 "//apex_available:platform",
6050 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006051 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006052 }
6053
6054 cc_library {
6055 name: "myotherlib",
6056 srcs: ["mylib.cpp"],
6057 system_shared_libs: [],
6058 stl: "none",
6059 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: "myjar",
6069 srcs: ["foo/bar/MyClass.java"],
6070 sdk_version: "none",
6071 system_modules: "none",
6072 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006073 apex_available: [
6074 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006075 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006076 "//apex_available:platform",
6077 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006078 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006079 }
6080
6081 java_library {
6082 name: "myotherjar",
6083 srcs: ["foo/bar/MyClass.java"],
6084 sdk_version: "none",
6085 system_modules: "none",
6086 apex_available: [
6087 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006088 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006089 "//apex_available:platform",
6090 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006091 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006092 }
6093 `
6094
6095 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6096 for _, f := range files {
6097 if f.path == file {
6098 if f.isLink {
6099 t.Errorf("%q is not a real file", file)
6100 }
6101 return
6102 }
6103 }
6104 t.Errorf("%q is not found", file)
6105 }
6106
6107 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6108 for _, f := range files {
6109 if f.path == file {
6110 if !f.isLink {
6111 t.Errorf("%q is not a symlink", file)
6112 }
6113 return
6114 }
6115 }
6116 t.Errorf("%q is not found", file)
6117 }
6118
Jiyong Park9d677202020-02-19 16:29:35 +09006119 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6120 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006121 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006122 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006123 ensureRealfileExists(t, files, "javalib/myjar.jar")
6124 ensureRealfileExists(t, files, "lib64/mylib.so")
6125 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6126
Jiyong Park9d677202020-02-19 16:29:35 +09006127 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6128 ensureRealfileExists(t, files, "javalib/myjar.jar")
6129 ensureRealfileExists(t, files, "lib64/mylib.so")
6130 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6131
6132 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006133 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006134 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006135 ensureRealfileExists(t, files, "javalib/myjar.jar")
6136 ensureRealfileExists(t, files, "lib64/mylib.so")
6137 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006138
6139 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6140 ensureRealfileExists(t, files, "javalib/myjar.jar")
6141 ensureRealfileExists(t, files, "lib64/mylib.so")
6142 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006143}
6144
Yo Chiange8128052020-07-23 20:09:18 +08006145func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006146 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006147 apex {
6148 name: "myapex",
6149 key: "myapex.key",
6150 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006151 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006152 }
6153
6154 apex_key {
6155 name: "myapex.key",
6156 public_key: "testkey.avbpubkey",
6157 private_key: "testkey.pem",
6158 }
6159
6160 cc_library_shared {
6161 name: "mylib",
6162 srcs: ["mylib.cpp"],
6163 shared_libs: ["myotherlib"],
6164 system_shared_libs: [],
6165 stl: "none",
6166 apex_available: [
6167 "myapex",
6168 "//apex_available:platform",
6169 ],
6170 }
6171
6172 cc_prebuilt_library_shared {
6173 name: "myotherlib",
6174 srcs: ["prebuilt.so"],
6175 system_shared_libs: [],
6176 stl: "none",
6177 apex_available: [
6178 "myapex",
6179 "//apex_available:platform",
6180 ],
6181 }
6182 `)
6183
6184 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006185 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006186 var builder strings.Builder
6187 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6188 androidMk := builder.String()
6189 // `myotherlib` is added to `myapex` as symlink
6190 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6191 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6192 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6193 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006194 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 +08006195}
6196
Jooyung Han643adc42020-02-27 13:50:06 +09006197func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006198 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006199 apex {
6200 name: "myapex",
6201 key: "myapex.key",
6202 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006203 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006204 }
6205
6206 apex_key {
6207 name: "myapex.key",
6208 public_key: "testkey.avbpubkey",
6209 private_key: "testkey.pem",
6210 }
6211
6212 cc_library {
6213 name: "mylib",
6214 srcs: ["mylib.cpp"],
6215 shared_libs: ["mylib2"],
6216 system_shared_libs: [],
6217 stl: "none",
6218 apex_available: [ "myapex" ],
6219 }
6220
6221 cc_library {
6222 name: "mylib2",
6223 srcs: ["mylib.cpp"],
6224 system_shared_libs: [],
6225 stl: "none",
6226 apex_available: [ "myapex" ],
6227 }
6228 `)
6229
6230 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6231 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6232 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6233 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6234 "lib64/mylib.so",
6235 "lib64/mylib2.so",
6236 })
6237}
6238
Jooyung Han49f67012020-04-17 13:43:10 +09006239func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006240 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006241 apex {
6242 name: "myapex",
6243 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006244 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006245 }
6246 apex_key {
6247 name: "myapex.key",
6248 public_key: "testkey.avbpubkey",
6249 private_key: "testkey.pem",
6250 }
6251 `, func(fs map[string][]byte, config android.Config) {
6252 delete(config.Targets, android.Android)
6253 config.AndroidCommonTarget = android.Target{}
6254 })
6255
6256 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6257 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6258 }
6259}
6260
Jiyong Parkbd159612020-02-28 15:22:21 +09006261func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006262 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006263 apex {
6264 name: "myapex",
6265 key: "myapex.key",
6266 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006267 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006268 }
6269
6270 apex_key {
6271 name: "myapex.key",
6272 public_key: "testkey.avbpubkey",
6273 private_key: "testkey.pem",
6274 }
6275
6276 android_app {
6277 name: "AppFoo",
6278 srcs: ["foo/bar/MyClass.java"],
6279 sdk_version: "none",
6280 system_modules: "none",
6281 apex_available: [ "myapex" ],
6282 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006283 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006284
Colin Crosscf371cc2020-11-13 11:48:42 -08006285 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006286 content := bundleConfigRule.Args["content"]
6287
6288 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006289 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 +09006290}
6291
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006292func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006293 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006294 apex {
6295 name: "myapex",
6296 key: "myapex.key",
6297 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006298 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006299 }
6300
6301 apex_key {
6302 name: "myapex.key",
6303 public_key: "testkey.avbpubkey",
6304 private_key: "testkey.pem",
6305 }
6306
6307 android_app_set {
6308 name: "AppSet",
6309 set: "AppSet.apks",
6310 }`)
6311 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006312 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006313 content := bundleConfigRule.Args["content"]
6314 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6315 s := mod.Rule("apexRule").Args["copy_commands"]
6316 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6317 if len(copyCmds) != 3 {
6318 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6319 }
6320 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6321 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6322 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6323}
6324
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006325func TestAppSetBundlePrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006326 ctx := testApex(t, "", func(fs map[string][]byte, config android.Config) {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006327 bp := `
6328 apex_set {
6329 name: "myapex",
6330 filename: "foo_v2.apex",
6331 sanitized: {
6332 none: { set: "myapex.apks", },
6333 hwaddress: { set: "myapex.hwasan.apks", },
6334 },
6335 }`
6336 fs["Android.bp"] = []byte(bp)
6337
6338 config.TestProductVariables.SanitizeDevice = []string{"hwaddress"}
6339 })
6340
6341 m := ctx.ModuleForTests("myapex", "android_common")
6342 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6343
6344 actual := extractedApex.Inputs
6345 if len(actual) != 1 {
6346 t.Errorf("expected a single input")
6347 }
6348
6349 expected := "myapex.hwasan.apks"
6350 if actual[0].String() != expected {
6351 t.Errorf("expected %s, got %s", expected, actual[0].String())
6352 }
6353}
6354
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006355func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006356 t.Helper()
6357
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006358 bp := `
6359 java_library {
6360 name: "some-updatable-apex-lib",
6361 srcs: ["a.java"],
6362 sdk_version: "current",
6363 apex_available: [
6364 "some-updatable-apex",
6365 ],
6366 }
6367
6368 java_library {
6369 name: "some-non-updatable-apex-lib",
6370 srcs: ["a.java"],
6371 apex_available: [
6372 "some-non-updatable-apex",
6373 ],
6374 }
6375
6376 java_library {
6377 name: "some-platform-lib",
6378 srcs: ["a.java"],
6379 sdk_version: "current",
6380 installable: true,
6381 }
6382
6383 java_library {
6384 name: "some-art-lib",
6385 srcs: ["a.java"],
6386 sdk_version: "current",
6387 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006388 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006389 ],
6390 hostdex: true,
6391 }
6392
6393 apex {
6394 name: "some-updatable-apex",
6395 key: "some-updatable-apex.key",
6396 java_libs: ["some-updatable-apex-lib"],
6397 updatable: true,
6398 min_sdk_version: "current",
6399 }
6400
6401 apex {
6402 name: "some-non-updatable-apex",
6403 key: "some-non-updatable-apex.key",
6404 java_libs: ["some-non-updatable-apex-lib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006405 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006406 }
6407
6408 apex_key {
6409 name: "some-updatable-apex.key",
6410 }
6411
6412 apex_key {
6413 name: "some-non-updatable-apex.key",
6414 }
6415
6416 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006417 name: "com.android.art.debug",
6418 key: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006419 java_libs: ["some-art-lib"],
6420 updatable: true,
6421 min_sdk_version: "current",
6422 }
6423
6424 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006425 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006426 }
6427
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006428 filegroup {
6429 name: "some-updatable-apex-file_contexts",
6430 srcs: [
6431 "system/sepolicy/apex/some-updatable-apex-file_contexts",
6432 ],
6433 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006434
6435 filegroup {
6436 name: "some-non-updatable-apex-file_contexts",
6437 srcs: [
6438 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
6439 ],
6440 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006441 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00006442
6443 testDexpreoptWithApexes(t, bp, errmsg, transformDexpreoptConfig)
6444}
6445
Paul Duffin064b70c2020-11-02 17:32:38 +00006446func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreoptConfig func(*dexpreopt.GlobalConfig)) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00006447 t.Helper()
6448
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006449 bp += cc.GatherRequiredDepsForTest(android.Android)
6450 bp += java.GatherRequiredDepsForTest()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006451
6452 fs := map[string][]byte{
6453 "a.java": nil,
6454 "a.jar": nil,
6455 "build/make/target/product/security": nil,
6456 "apex_manifest.json": nil,
6457 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006458 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00006459 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
6460 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
6461 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006462 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006463 }
6464 cc.GatherRequiredFilesForTest(fs)
6465
Paul Duffin39853512021-02-26 11:09:39 +00006466 for k, v := range filesForSdkLibrary {
6467 fs[k] = v
6468 }
Colin Crossae8600b2020-10-29 17:09:13 -07006469 config := android.TestArchConfig(buildDir, nil, bp, fs)
6470
6471 ctx := android.NewTestArchContext(config)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006472 ctx.RegisterModuleType("apex", BundleFactory)
6473 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
Paul Duffin064b70c2020-11-02 17:32:38 +00006474 ctx.RegisterModuleType("prebuilt_apex", PrebuiltFactory)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006475 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Paul Duffinc988c8e2020-04-29 18:27:14 +01006476 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin37856732021-02-26 14:24:15 +00006477 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffin021f4e52020-07-30 16:04:17 +01006478 android.RegisterPrebuiltMutators(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006479 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006480 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinf38931c2021-02-05 16:58:28 +00006481 java.RegisterHiddenApiSingletonComponents(ctx)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006482 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6483 ctx.PreDepsMutators(RegisterPreDepsMutators)
6484 ctx.PostDepsMutators(RegisterPostDepsMutators)
6485
Colin Crossae8600b2020-10-29 17:09:13 -07006486 ctx.Register()
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006487
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006488 pathCtx := android.PathContextForTesting(config)
6489 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
6490 transformDexpreoptConfig(dexpreoptConfig)
6491 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
6492
Paul Duffinf38931c2021-02-05 16:58:28 +00006493 // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding
Paul Duffin4fd997b2021-02-03 20:06:33 +00006494 // product variables.
Paul Duffinf38931c2021-02-05 16:58:28 +00006495 config.TestProductVariables.BootJars = dexpreoptConfig.BootJars
6496 config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars
6497
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006498 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6499 android.FailIfErrored(t, errs)
6500
6501 _, errs = ctx.PrepareBuildActions(config)
6502 if errmsg == "" {
6503 android.FailIfErrored(t, errs)
6504 } else if len(errs) > 0 {
6505 android.FailIfNoMatchingErrors(t, errmsg, errs)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006506 } else {
6507 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6508 }
Paul Duffin064b70c2020-11-02 17:32:38 +00006509
6510 return ctx
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006511}
6512
Jooyung Han548640b2020-04-27 12:10:30 +09006513func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
6514 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6515 apex {
6516 name: "myapex",
6517 key: "myapex.key",
6518 updatable: true,
6519 }
6520
6521 apex_key {
6522 name: "myapex.key",
6523 public_key: "testkey.avbpubkey",
6524 private_key: "testkey.pem",
6525 }
6526 `)
6527}
6528
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006529func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
6530 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
6531 apex {
6532 name: "myapex",
6533 key: "myapex.key",
6534 }
6535
6536 apex_key {
6537 name: "myapex.key",
6538 public_key: "testkey.avbpubkey",
6539 private_key: "testkey.pem",
6540 }
6541 `)
6542}
6543
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006544func TestNoUpdatableJarsInBootImage(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006545 var err string
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006546 var transform func(*dexpreopt.GlobalConfig)
6547
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006548 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
6549 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006550 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006551 }
6552 testNoUpdatableJarsInBootImage(t, "", transform)
6553 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006554
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006555 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffind376f792021-01-26 11:59:35 +00006556 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 +01006557 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffind376f792021-01-26 11:59:35 +00006558 config.BootJars = android.CreateTestConfiguredJarList([]string{"com.android.art.debug:some-art-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006559 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006560 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006561 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006562
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006563 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 -07006564 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 +01006565 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006566 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006567 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006568 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006569 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006570
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006571 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 -07006572 err = `module "some-non-updatable-apex-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006573 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006574 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006575 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006576 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006577 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006578
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006579 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 -07006580 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 +01006581 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006582 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-updatable-apex:some-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006583 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006584 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006585 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006586
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006587 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
6588 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006589 config.BootJars = android.CreateTestConfiguredJarList([]string{"some-non-updatable-apex:some-non-updatable-apex-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006590 }
6591 testNoUpdatableJarsInBootImage(t, "", transform)
6592 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01006593
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006594 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006595 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006596 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006597 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006598 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006599 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006600 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006601
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006602 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006603 err = "failed to find a dex jar path for module 'nonexistent'"
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006604 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006605 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:nonexistent"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006606 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006607 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006608 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006609
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006610 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Colin Crossaede88c2020-08-11 12:17:01 -07006611 err = `module "some-platform-lib" is not allowed in the ART boot image`
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006612 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006613 config.ArtApexJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006614 }
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006615 testNoUpdatableJarsInBootImage(t, err, transform)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006616 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006617
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006618 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
6619 transform = func(config *dexpreopt.GlobalConfig) {
Paul Duffine10dfa42020-10-23 21:23:44 +01006620 config.BootJars = android.CreateTestConfiguredJarList([]string{"platform:some-platform-lib"})
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006621 }
6622 testNoUpdatableJarsInBootImage(t, "", transform)
6623 })
Paul Duffin064b70c2020-11-02 17:32:38 +00006624
6625}
6626
6627func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
6628 transform := func(config *dexpreopt.GlobalConfig) {
6629 config.BootJars = android.CreateTestConfiguredJarList([]string{"myapex:libfoo"})
6630 }
6631 t.Run("prebuilt no source", func(t *testing.T) {
6632 testDexpreoptWithApexes(t, `
6633 prebuilt_apex {
6634 name: "myapex" ,
6635 arch: {
6636 arm64: {
6637 src: "myapex-arm64.apex",
6638 },
6639 arm: {
6640 src: "myapex-arm.apex",
6641 },
6642 },
6643 exported_java_libs: ["libfoo"],
6644 }
6645
6646 java_import {
6647 name: "libfoo",
6648 jars: ["libfoo.jar"],
6649 }
6650`, "", transform)
6651 })
6652
6653 t.Run("prebuilt no source", func(t *testing.T) {
6654 testDexpreoptWithApexes(t, `
6655 prebuilt_apex {
6656 name: "myapex" ,
6657 arch: {
6658 arm64: {
6659 src: "myapex-arm64.apex",
6660 },
6661 arm: {
6662 src: "myapex-arm.apex",
6663 },
6664 },
6665 exported_java_libs: ["libfoo"],
6666 }
6667
6668 java_import {
6669 name: "libfoo",
6670 jars: ["libfoo.jar"],
6671 }
6672`, "", transform)
6673 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006674}
6675
Andrei Onea115e7e72020-06-05 21:14:03 +01006676func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, apexBootJars []string, rules []android.Rule) {
6677 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01006678 bp += `
6679 apex_key {
6680 name: "myapex.key",
6681 public_key: "testkey.avbpubkey",
6682 private_key: "testkey.pem",
6683 }`
6684 fs := map[string][]byte{
6685 "lib1/src/A.java": nil,
6686 "lib2/src/B.java": nil,
6687 "system/sepolicy/apex/myapex-file_contexts": nil,
6688 }
6689
Colin Crossae8600b2020-10-29 17:09:13 -07006690 config := android.TestArchConfig(buildDir, nil, bp, fs)
6691 android.SetTestNeverallowRules(config, rules)
6692 updatableBootJars := make([]string, 0, len(apexBootJars))
6693 for _, apexBootJar := range apexBootJars {
6694 updatableBootJars = append(updatableBootJars, "myapex:"+apexBootJar)
6695 }
6696 config.TestProductVariables.UpdatableBootJars = android.CreateTestConfiguredJarList(updatableBootJars)
6697
6698 ctx := android.NewTestArchContext(config)
Andrei Onea115e7e72020-06-05 21:14:03 +01006699 ctx.RegisterModuleType("apex", BundleFactory)
6700 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
6701 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
6702 cc.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffinc059c8c2021-01-20 17:13:52 +00006703 java.RegisterRequiredBuildComponentsForTest(ctx)
Andrei Onea115e7e72020-06-05 21:14:03 +01006704 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
6705 ctx.PreDepsMutators(RegisterPreDepsMutators)
6706 ctx.PostDepsMutators(RegisterPostDepsMutators)
6707 ctx.PostDepsMutators(android.RegisterNeverallowMutator)
6708
Colin Crossae8600b2020-10-29 17:09:13 -07006709 ctx.Register()
Andrei Onea115e7e72020-06-05 21:14:03 +01006710
6711 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
6712 android.FailIfErrored(t, errs)
6713
6714 _, errs = ctx.PrepareBuildActions(config)
6715 if errmsg == "" {
6716 android.FailIfErrored(t, errs)
6717 } else if len(errs) > 0 {
6718 android.FailIfNoMatchingErrors(t, errmsg, errs)
6719 return
6720 } else {
6721 t.Fatalf("missing expected error %q (0 errors are returned)", errmsg)
6722 }
6723}
6724
6725func TestApexPermittedPackagesRules(t *testing.T) {
6726 testcases := []struct {
6727 name string
6728 expectedError string
6729 bp string
6730 bootJars []string
6731 modulesPackages map[string][]string
6732 }{
6733
6734 {
6735 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
6736 expectedError: "",
6737 bp: `
6738 java_library {
6739 name: "bcp_lib1",
6740 srcs: ["lib1/src/*.java"],
6741 permitted_packages: ["foo.bar"],
6742 apex_available: ["myapex"],
6743 sdk_version: "none",
6744 system_modules: "none",
6745 }
6746 java_library {
6747 name: "nonbcp_lib2",
6748 srcs: ["lib2/src/*.java"],
6749 apex_available: ["myapex"],
6750 permitted_packages: ["a.b"],
6751 sdk_version: "none",
6752 system_modules: "none",
6753 }
6754 apex {
6755 name: "myapex",
6756 key: "myapex.key",
6757 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006758 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006759 }`,
6760 bootJars: []string{"bcp_lib1"},
6761 modulesPackages: map[string][]string{
6762 "myapex": []string{
6763 "foo.bar",
6764 },
6765 },
6766 },
6767 {
6768 name: "Bootclasspath apex jar not satisfying allowed module packages.",
6769 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.`,
6770 bp: `
6771 java_library {
6772 name: "bcp_lib1",
6773 srcs: ["lib1/src/*.java"],
6774 apex_available: ["myapex"],
6775 permitted_packages: ["foo.bar"],
6776 sdk_version: "none",
6777 system_modules: "none",
6778 }
6779 java_library {
6780 name: "bcp_lib2",
6781 srcs: ["lib2/src/*.java"],
6782 apex_available: ["myapex"],
6783 permitted_packages: ["foo.bar", "bar.baz"],
6784 sdk_version: "none",
6785 system_modules: "none",
6786 }
6787 apex {
6788 name: "myapex",
6789 key: "myapex.key",
6790 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006791 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01006792 }
6793 `,
6794 bootJars: []string{"bcp_lib1", "bcp_lib2"},
6795 modulesPackages: map[string][]string{
6796 "myapex": []string{
6797 "foo.bar",
6798 },
6799 },
6800 },
6801 }
6802 for _, tc := range testcases {
6803 t.Run(tc.name, func(t *testing.T) {
6804 rules := createApexPermittedPackagesRules(tc.modulesPackages)
6805 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
6806 })
6807 }
6808}
6809
Jiyong Park62304bb2020-04-13 16:19:48 +09006810func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006811 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09006812 apex {
6813 name: "myapex",
6814 key: "myapex.key",
6815 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006816 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09006817 }
6818
6819 apex_key {
6820 name: "myapex.key",
6821 public_key: "testkey.avbpubkey",
6822 private_key: "testkey.pem",
6823 }
6824
6825 cc_library {
6826 name: "mylib",
6827 srcs: ["mylib.cpp"],
6828 system_shared_libs: [],
6829 stl: "none",
6830 stubs: {
6831 versions: ["1"],
6832 },
6833 apex_available: ["myapex"],
6834 }
6835
6836 cc_library {
6837 name: "myprivlib",
6838 srcs: ["mylib.cpp"],
6839 system_shared_libs: [],
6840 stl: "none",
6841 apex_available: ["myapex"],
6842 }
6843
6844
6845 cc_test {
6846 name: "mytest",
6847 gtest: false,
6848 srcs: ["mylib.cpp"],
6849 system_shared_libs: [],
6850 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09006851 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09006852 test_for: ["myapex"]
6853 }
Jiyong Park46a512f2020-12-04 18:02:13 +09006854
6855 cc_library {
6856 name: "mytestlib",
6857 srcs: ["mylib.cpp"],
6858 system_shared_libs: [],
6859 shared_libs: ["mylib", "myprivlib"],
6860 stl: "none",
6861 test_for: ["myapex"],
6862 }
6863
6864 cc_benchmark {
6865 name: "mybench",
6866 srcs: ["mylib.cpp"],
6867 system_shared_libs: [],
6868 shared_libs: ["mylib", "myprivlib"],
6869 stl: "none",
6870 test_for: ["myapex"],
6871 }
Jiyong Park62304bb2020-04-13 16:19:48 +09006872 `)
6873
6874 // the test 'mytest' is a test for the apex, therefore is linked to the
6875 // actual implementation of mylib instead of its stub.
6876 ldFlags := ctx.ModuleForTests("mytest", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6877 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6878 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park46a512f2020-12-04 18:02:13 +09006879
6880 // The same should be true for cc_library
6881 ldFlags = ctx.ModuleForTests("mytestlib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
6882 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6883 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
6884
6885 // ... and for cc_benchmark
6886 ldFlags = ctx.ModuleForTests("mybench", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
6887 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared/mylib.so")
6888 ensureNotContains(t, ldFlags, "mylib/android_arm64_armv8-a_shared_1/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09006889}
6890
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006891// TODO(jungjw): Move this to proptools
6892func intPtr(i int) *int {
6893 return &i
6894}
6895
6896func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006897 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006898 apex_set {
6899 name: "myapex",
6900 set: "myapex.apks",
6901 filename: "foo_v2.apex",
6902 overrides: ["foo"],
6903 }
6904 `, func(fs map[string][]byte, config android.Config) {
6905 config.TestProductVariables.Platform_sdk_version = intPtr(30)
Jaewoong Jung829b7132020-06-10 12:23:32 -07006906 config.Targets[android.Android] = []android.Target{
6907 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
6908 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
6909 }
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006910 })
6911
6912 m := ctx.ModuleForTests("myapex", "android_common")
6913
6914 // Check extract_apks tool parameters.
6915 extractedApex := m.Output(buildDir + "/.intermediates/myapex/android_common/foo_v2.apex")
6916 actual := extractedApex.Args["abis"]
6917 expected := "ARMEABI_V7A,ARM64_V8A"
6918 if actual != expected {
6919 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6920 }
6921 actual = extractedApex.Args["sdk-version"]
6922 expected = "30"
6923 if actual != expected {
6924 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
6925 }
6926
6927 a := m.Module().(*ApexSet)
6928 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07006929 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07006930 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
6931 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
6932 }
6933}
6934
Jiyong Park7d95a512020-05-10 15:16:24 +09006935func TestNoStaticLinkingToStubsLib(t *testing.T) {
6936 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
6937 apex {
6938 name: "myapex",
6939 key: "myapex.key",
6940 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006941 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09006942 }
6943
6944 apex_key {
6945 name: "myapex.key",
6946 public_key: "testkey.avbpubkey",
6947 private_key: "testkey.pem",
6948 }
6949
6950 cc_library {
6951 name: "mylib",
6952 srcs: ["mylib.cpp"],
6953 static_libs: ["otherlib"],
6954 system_shared_libs: [],
6955 stl: "none",
6956 apex_available: [ "myapex" ],
6957 }
6958
6959 cc_library {
6960 name: "otherlib",
6961 srcs: ["mylib.cpp"],
6962 system_shared_libs: [],
6963 stl: "none",
6964 stubs: {
6965 versions: ["1", "2", "3"],
6966 },
6967 apex_available: [ "myapex" ],
6968 }
6969 `)
6970}
6971
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006972func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006973 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006974 apex {
6975 name: "myapex",
6976 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006977 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09006978 }
6979
6980 apex_key {
6981 name: "myapex.key",
6982 public_key: "testkey.avbpubkey",
6983 private_key: "testkey.pem",
6984 }
6985
6986 prebuilt_apex {
6987 name: "myapex",
6988 prefer: true,
6989 arch: {
6990 arm64: {
6991 src: "myapex-arm64.apex",
6992 },
6993 arm: {
6994 src: "myapex-arm.apex",
6995 },
6996 },
6997 }
6998
6999 apex_set {
7000 name: "myapex_set",
7001 set: "myapex.apks",
7002 filename: "myapex_set.apex",
7003 overrides: ["myapex"],
7004 }
7005 `)
7006
7007 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7008 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7009 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 +09007010 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 +09007011}
7012
Jooyung Han938b5932020-06-20 12:47:47 +09007013func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007014 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007015 apex {
7016 name: "myapex",
7017 key: "myapex.key",
7018 apps: ["app"],
7019 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007020 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007021 }
7022
7023 apex_key {
7024 name: "myapex.key",
7025 public_key: "testkey.avbpubkey",
7026 private_key: "testkey.pem",
7027 }
7028
7029 android_app {
7030 name: "app",
7031 srcs: ["foo/bar/MyClass.java"],
7032 package_name: "foo",
7033 sdk_version: "none",
7034 system_modules: "none",
7035 apex_available: [ "myapex" ],
7036 }
7037 `, withFiles(map[string][]byte{
7038 "sub/Android.bp": []byte(`
7039 override_apex {
7040 name: "override_myapex",
7041 base: "myapex",
7042 apps: ["override_app"],
7043 allowed_files: ":allowed",
7044 }
7045 // Overridable "path" property should be referenced indirectly
7046 filegroup {
7047 name: "allowed",
7048 srcs: ["allowed.txt"],
7049 }
7050 override_android_app {
7051 name: "override_app",
7052 base: "app",
7053 package_name: "bar",
7054 }
7055 `),
7056 }))
7057
7058 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7059 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7060 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7061 }
7062
7063 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7064 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7065 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7066 }
7067}
7068
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007069func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007070 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007071 apex {
7072 name: "myapex",
7073 key: "myapex.key",
7074 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007075 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007076 }
7077
7078 apex_key {
7079 name: "myapex.key",
7080 public_key: "testkey.avbpubkey",
7081 private_key: "testkey.pem",
7082 }
7083
7084 cc_library {
7085 name: "mylib",
7086 srcs: ["mylib.cpp"],
7087 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007088 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007089 },
7090 apex_available: ["myapex"],
7091 }
7092
7093 cc_prebuilt_library_shared {
7094 name: "mylib",
7095 prefer: false,
7096 srcs: ["prebuilt.so"],
7097 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007098 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007099 },
7100 apex_available: ["myapex"],
7101 }
7102 `)
7103}
7104
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007105func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007106 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007107 apex {
7108 name: "myapex",
7109 key: "myapex.key",
7110 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007111 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007112 }
7113 apex_key {
7114 name: "myapex.key",
7115 public_key: "testkey.avbpubkey",
7116 private_key: "testkey.pem",
7117 }
7118 `, func(fs map[string][]byte, config android.Config) {
7119 config.TestProductVariables.CompressedApex = proptools.BoolPtr(true)
7120 })
7121
7122 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7123 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7124
7125 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7126 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7127
7128 // Make sure output of bundle is .capex
7129 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7130 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7131
7132 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007133 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007134 var builder strings.Builder
7135 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7136 androidMk := builder.String()
7137 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7138}
7139
Martin Stjernholm2856c662020-12-02 15:03:42 +00007140func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007141 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007142 apex {
7143 name: "myapex",
7144 key: "myapex.key",
7145 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007146 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007147 }
7148
7149 apex_key {
7150 name: "myapex.key",
7151 public_key: "testkey.avbpubkey",
7152 private_key: "testkey.pem",
7153 }
7154
7155 cc_library {
7156 name: "mylib",
7157 srcs: ["mylib.cpp"],
7158 apex_available: ["myapex"],
7159 shared_libs: ["otherlib"],
7160 system_shared_libs: [],
7161 }
7162
7163 cc_library {
7164 name: "otherlib",
7165 srcs: ["mylib.cpp"],
7166 stubs: {
7167 versions: ["current"],
7168 },
7169 }
7170
7171 cc_prebuilt_library_shared {
7172 name: "otherlib",
7173 prefer: true,
7174 srcs: ["prebuilt.so"],
7175 stubs: {
7176 versions: ["current"],
7177 },
7178 }
7179 `)
7180
7181 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007182 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007183 var builder strings.Builder
7184 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7185 androidMk := builder.String()
7186
7187 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7188 // a thing there.
7189 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7190}
7191
Jiyong Parke3867542020-12-03 17:28:25 +09007192func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007193 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007194 apex {
7195 name: "myapex",
7196 key: "myapex.key",
7197 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007198 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007199 }
7200
7201 apex_key {
7202 name: "myapex.key",
7203 public_key: "testkey.avbpubkey",
7204 private_key: "testkey.pem",
7205 }
7206
7207 cc_library {
7208 name: "mylib",
7209 srcs: ["mylib.cpp"],
7210 system_shared_libs: [],
7211 stl: "none",
7212 apex_available: ["myapex"],
7213 shared_libs: ["mylib2"],
7214 target: {
7215 apex: {
7216 exclude_shared_libs: ["mylib2"],
7217 },
7218 },
7219 }
7220
7221 cc_library {
7222 name: "mylib2",
7223 srcs: ["mylib.cpp"],
7224 system_shared_libs: [],
7225 stl: "none",
7226 }
7227 `)
7228
7229 // Check if mylib is linked to mylib2 for the non-apex target
7230 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7231 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7232
7233 // Make sure that the link doesn't occur for the apex target
7234 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7235 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7236
7237 // It shouldn't appear in the copy cmd as well.
7238 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7239 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7240}
7241
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007242func TestPrebuiltStubLibDep(t *testing.T) {
7243 bpBase := `
7244 apex {
7245 name: "myapex",
7246 key: "myapex.key",
7247 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007248 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007249 }
7250 apex_key {
7251 name: "myapex.key",
7252 public_key: "testkey.avbpubkey",
7253 private_key: "testkey.pem",
7254 }
7255 cc_library {
7256 name: "mylib",
7257 srcs: ["mylib.cpp"],
7258 apex_available: ["myapex"],
7259 shared_libs: ["stublib"],
7260 system_shared_libs: [],
7261 }
7262 apex {
7263 name: "otherapex",
7264 enabled: %s,
7265 key: "myapex.key",
7266 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007267 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007268 }
7269 `
7270
7271 stublibSourceBp := `
7272 cc_library {
7273 name: "stublib",
7274 srcs: ["mylib.cpp"],
7275 apex_available: ["otherapex"],
7276 system_shared_libs: [],
7277 stl: "none",
7278 stubs: {
7279 versions: ["1"],
7280 },
7281 }
7282 `
7283
7284 stublibPrebuiltBp := `
7285 cc_prebuilt_library_shared {
7286 name: "stublib",
7287 srcs: ["prebuilt.so"],
7288 apex_available: ["otherapex"],
7289 stubs: {
7290 versions: ["1"],
7291 },
7292 %s
7293 }
7294 `
7295
7296 tests := []struct {
7297 name string
7298 stublibBp string
7299 usePrebuilt bool
7300 modNames []string // Modules to collect AndroidMkEntries for
7301 otherApexEnabled []string
7302 }{
7303 {
7304 name: "only_source",
7305 stublibBp: stublibSourceBp,
7306 usePrebuilt: false,
7307 modNames: []string{"stublib"},
7308 otherApexEnabled: []string{"true", "false"},
7309 },
7310 {
7311 name: "source_preferred",
7312 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
7313 usePrebuilt: false,
7314 modNames: []string{"stublib", "prebuilt_stublib"},
7315 otherApexEnabled: []string{"true", "false"},
7316 },
7317 {
7318 name: "prebuilt_preferred",
7319 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
7320 usePrebuilt: true,
7321 modNames: []string{"stublib", "prebuilt_stublib"},
7322 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7323 },
7324 {
7325 name: "only_prebuilt",
7326 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
7327 usePrebuilt: true,
7328 modNames: []string{"stublib"},
7329 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
7330 },
7331 }
7332
7333 for _, test := range tests {
7334 t.Run(test.name, func(t *testing.T) {
7335 for _, otherApexEnabled := range test.otherApexEnabled {
7336 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007337 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007338
7339 type modAndMkEntries struct {
7340 mod *cc.Module
7341 mkEntries android.AndroidMkEntries
7342 }
7343 entries := []*modAndMkEntries{}
7344
7345 // Gather shared lib modules that are installable
7346 for _, modName := range test.modNames {
7347 for _, variant := range ctx.ModuleVariantsForTests(modName) {
7348 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
7349 continue
7350 }
7351 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08007352 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007353 continue
7354 }
Colin Crossaa255532020-07-03 13:18:24 -07007355 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007356 if ent.Disabled {
7357 continue
7358 }
7359 entries = append(entries, &modAndMkEntries{
7360 mod: mod,
7361 mkEntries: ent,
7362 })
7363 }
7364 }
7365 }
7366
7367 var entry *modAndMkEntries = nil
7368 for _, ent := range entries {
7369 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
7370 if entry != nil {
7371 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
7372 } else {
7373 entry = ent
7374 }
7375 }
7376 }
7377
7378 if entry == nil {
7379 t.Errorf("AndroidMk entry for \"stublib\" missing")
7380 } else {
7381 isPrebuilt := entry.mod.Prebuilt() != nil
7382 if isPrebuilt != test.usePrebuilt {
7383 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
7384 }
7385 if !entry.mod.IsStubs() {
7386 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
7387 }
7388 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
7389 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
7390 }
Jiyong Park892a98f2020-12-14 09:20:00 +09007391 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
7392 expected := "-D__STUBLIB_API__=1"
7393 if !android.InList(expected, cflags) {
7394 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
7395 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007396 }
7397 })
7398 }
7399 })
7400 }
7401}
7402
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07007403func TestMain(m *testing.M) {
7404 run := func() int {
7405 setUp()
7406 defer tearDown()
7407
7408 return m.Run()
7409 }
7410
7411 os.Exit(run())
7412}