blob: f07bf6379166143376cd4041e34476c68eaa5985 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jiyong Park25fc6a92018-11-18 18:02:45 +090019 "os"
Jooyung Han39edb6c2019-11-06 16:53:07 +090020 "path"
Paul Duffin37856732021-02-26 14:24:15 +000021 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070022 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010023 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090024 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090025 "strconv"
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
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Paul Duffin810f33d2021-03-09 14:12:32 +0000116var withBinder32bit = android.FixtureModifyProductVariables(
117 func(variables android.FixtureProductVariables) {
118 variables.Binder32bit = proptools.BoolPtr(true)
119 },
120)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withUnbundledBuild = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Unbundled_build = proptools.BoolPtr(true)
125 },
126)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900127
Paul Duffin284165a2021-03-29 01:50:31 +0100128// Legacy preparer used for running tests within the apex package.
129//
130// This includes everything that was needed to run any test in the apex package prior to the
131// introduction of the test fixtures. Tests that are being converted to use fixtures directly
132// rather than through the testApex...() methods should avoid using this and instead use the
133// various preparers directly, using android.GroupFixturePreparers(...) to group them when
134// necessary.
135//
136// deprecated
137var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000138 // General preparers in alphabetical order as test infrastructure will enforce correct
139 // registration order.
140 android.PrepareForTestWithAndroidBuildComponents,
141 bpf.PrepareForTestWithBpf,
142 cc.PrepareForTestWithCcBuildComponents,
143 java.PrepareForTestWithJavaDefaultModules,
144 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
145 rust.PrepareForTestWithRustDefaultModules,
146 sh.PrepareForTestWithShBuildComponents,
147
148 PrepareForTestWithApexBuildComponents,
149
150 // Additional apex test specific preparers.
151 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
152 filegroup {
153 name: "myapex-file_contexts",
154 srcs: [
155 "apex/myapex-file_contexts",
156 ],
157 }
158 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000159 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000160 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000161 "a.java": nil,
162 "PrebuiltAppFoo.apk": nil,
163 "PrebuiltAppFooPriv.apk": nil,
164 "apex_manifest.json": nil,
165 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
167 "system/sepolicy/apex/myapex2-file_contexts": nil,
168 "system/sepolicy/apex/otherapex-file_contexts": nil,
169 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
170 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
171 "mylib.cpp": nil,
172 "mytest.cpp": nil,
173 "mytest1.cpp": nil,
174 "mytest2.cpp": nil,
175 "mytest3.cpp": nil,
176 "myprebuilt": nil,
177 "my_include": nil,
178 "foo/bar/MyClass.java": nil,
179 "prebuilt.jar": nil,
180 "prebuilt.so": nil,
181 "vendor/foo/devkeys/test.x509.pem": nil,
182 "vendor/foo/devkeys/test.pk8": nil,
183 "testkey.x509.pem": nil,
184 "testkey.pk8": nil,
185 "testkey.override.x509.pem": nil,
186 "testkey.override.pk8": nil,
187 "vendor/foo/devkeys/testkey.avbpubkey": nil,
188 "vendor/foo/devkeys/testkey.pem": nil,
189 "NOTICE": nil,
190 "custom_notice": nil,
191 "custom_notice_for_static_lib": nil,
192 "testkey2.avbpubkey": nil,
193 "testkey2.pem": nil,
194 "myapex-arm64.apex": nil,
195 "myapex-arm.apex": nil,
196 "myapex.apks": nil,
197 "frameworks/base/api/current.txt": nil,
198 "framework/aidl/a.aidl": nil,
199 "build/make/core/proguard.flags": nil,
200 "build/make/core/proguard_basic_keeps.flags": nil,
201 "dummy.txt": nil,
202 "baz": nil,
203 "bar/baz": nil,
204 "testdata/baz": nil,
205 "AppSet.apks": nil,
206 "foo.rs": nil,
207 "libfoo.jar": nil,
208 "libbar.jar": nil,
209 },
210 ),
211
212 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
213 variables.DeviceVndkVersion = proptools.StringPtr("current")
214 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
215 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
216 variables.Platform_sdk_codename = proptools.StringPtr("Q")
217 variables.Platform_sdk_final = proptools.BoolPtr(false)
218 variables.Platform_version_active_codenames = []string{"Q"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900219 variables.Platform_vndk_version = proptools.StringPtr("29")
Paul Duffin37aad602021-03-08 09:47:16 +0000220 }),
221)
222
Paul Duffin52bfaa42021-03-23 23:40:12 +0000223var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
224 "system/sepolicy/apex/myapex-file_contexts": nil,
225})
226
Jooyung Han643adc42020-02-27 13:50:06 +0900227// ensure that 'result' equals 'expected'
228func ensureEquals(t *testing.T, result string, expected string) {
229 t.Helper()
230 if result != expected {
231 t.Errorf("%q != %q", expected, result)
232 }
233}
234
Jiyong Park25fc6a92018-11-18 18:02:45 +0900235// ensure that 'result' contains 'expected'
236func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900237 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900238 if !strings.Contains(result, expected) {
239 t.Errorf("%q is not found in %q", expected, result)
240 }
241}
242
Liz Kammer5bd365f2020-05-27 15:15:11 -0700243// ensure that 'result' contains 'expected' exactly one time
244func ensureContainsOnce(t *testing.T, result string, expected string) {
245 t.Helper()
246 count := strings.Count(result, expected)
247 if count != 1 {
248 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
249 }
250}
251
Jiyong Park25fc6a92018-11-18 18:02:45 +0900252// ensures that 'result' does not contain 'notExpected'
253func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900254 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900255 if strings.Contains(result, notExpected) {
256 t.Errorf("%q is found in %q", notExpected, result)
257 }
258}
259
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700260func ensureMatches(t *testing.T, result string, expectedRex string) {
261 ok, err := regexp.MatchString(expectedRex, result)
262 if err != nil {
263 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
264 return
265 }
266 if !ok {
267 t.Errorf("%s does not match regular expession %s", result, expectedRex)
268 }
269}
270
Jiyong Park25fc6a92018-11-18 18:02:45 +0900271func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900272 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900273 if !android.InList(expected, result) {
274 t.Errorf("%q is not found in %v", expected, result)
275 }
276}
277
278func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if android.InList(notExpected, result) {
281 t.Errorf("%q is found in %v", notExpected, result)
282 }
283}
284
Jooyung Hane1633032019-08-01 17:41:43 +0900285func ensureListEmpty(t *testing.T, result []string) {
286 t.Helper()
287 if len(result) > 0 {
288 t.Errorf("%q is expected to be empty", result)
289 }
290}
291
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000292func ensureListNotEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) == 0 {
295 t.Errorf("%q is expected to be not empty", result)
296 }
297}
298
Jiyong Park25fc6a92018-11-18 18:02:45 +0900299// Minimal test
300func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800301 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900302 apex_defaults {
303 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900304 manifest: ":myapex.manifest",
305 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900307 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900308 native_shared_libs: [
309 "mylib",
310 "libfoo.ffi",
311 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900312 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800313 multilib: {
314 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900315 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800316 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900317 },
Jiyong Park77acec62020-06-01 21:39:15 +0900318 java_libs: [
319 "myjar",
320 "myjar_dex",
321 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000322 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900323 }
324
Jiyong Park30ca9372019-02-07 16:27:23 +0900325 apex {
326 name: "myapex",
327 defaults: ["myapex-defaults"],
328 }
329
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 apex_key {
331 name: "myapex.key",
332 public_key: "testkey.avbpubkey",
333 private_key: "testkey.pem",
334 }
335
Jiyong Park809bb722019-02-13 21:33:49 +0900336 filegroup {
337 name: "myapex.manifest",
338 srcs: ["apex_manifest.json"],
339 }
340
341 filegroup {
342 name: "myapex.androidmanifest",
343 srcs: ["AndroidManifest.xml"],
344 }
345
Jiyong Park25fc6a92018-11-18 18:02:45 +0900346 cc_library {
347 name: "mylib",
348 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900349 shared_libs: [
350 "mylib2",
351 "libbar.ffi",
352 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 system_shared_libs: [],
354 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000355 // TODO: remove //apex_available:platform
356 apex_available: [
357 "//apex_available:platform",
358 "myapex",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 }
361
Alex Light3d673592019-01-18 14:37:31 -0800362 cc_binary {
363 name: "foo",
364 srcs: ["mylib.cpp"],
365 compile_multilib: "both",
366 multilib: {
367 lib32: {
368 suffix: "32",
369 },
370 lib64: {
371 suffix: "64",
372 },
373 },
374 symlinks: ["foo_link_"],
375 symlink_preferred_arch: true,
376 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800377 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700378 apex_available: [ "myapex", "com.android.gki.*" ],
379 }
380
Jiyong Park99644e92020-11-17 22:21:02 +0900381 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000382 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900383 srcs: ["foo.rs"],
384 rlibs: ["libfoo.rlib.rust"],
385 dylibs: ["libfoo.dylib.rust"],
386 apex_available: ["myapex"],
387 }
388
389 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000390 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900391 srcs: ["foo.rs"],
392 crate_name: "foo",
393 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900394 shared_libs: ["libfoo.shared_from_rust"],
395 }
396
397 cc_library_shared {
398 name: "libfoo.shared_from_rust",
399 srcs: ["mylib.cpp"],
400 system_shared_libs: [],
401 stl: "none",
402 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900403 }
404
405 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000406 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900407 srcs: ["foo.rs"],
408 crate_name: "foo",
409 apex_available: ["myapex"],
410 }
411
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900412 rust_ffi_shared {
413 name: "libfoo.ffi",
414 srcs: ["foo.rs"],
415 crate_name: "foo",
416 apex_available: ["myapex"],
417 }
418
419 rust_ffi_shared {
420 name: "libbar.ffi",
421 srcs: ["foo.rs"],
422 crate_name: "bar",
423 apex_available: ["myapex"],
424 }
425
Yifan Hongd22a84a2020-07-28 17:37:46 -0700426 apex {
427 name: "com.android.gki.fake",
428 binaries: ["foo"],
429 key: "myapex.key",
430 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000431 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800432 }
433
Paul Duffindddd5462020-04-07 15:25:44 +0100434 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900435 name: "mylib2",
436 srcs: ["mylib.cpp"],
437 system_shared_libs: [],
438 stl: "none",
Jiyong Park52818fc2019-03-18 12:01:38 +0900439 notice: "custom_notice",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900440 static_libs: ["libstatic"],
441 // TODO: remove //apex_available:platform
442 apex_available: [
443 "//apex_available:platform",
444 "myapex",
445 ],
446 }
447
Paul Duffindddd5462020-04-07 15:25:44 +0100448 cc_prebuilt_library_shared {
449 name: "mylib2",
450 srcs: ["prebuilt.so"],
451 // TODO: remove //apex_available:platform
452 apex_available: [
453 "//apex_available:platform",
454 "myapex",
455 ],
456 }
457
Jiyong Park9918e1a2020-03-17 19:16:40 +0900458 cc_library_static {
459 name: "libstatic",
460 srcs: ["mylib.cpp"],
461 system_shared_libs: [],
462 stl: "none",
463 notice: "custom_notice_for_static_lib",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000464 // TODO: remove //apex_available:platform
465 apex_available: [
466 "//apex_available:platform",
467 "myapex",
468 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900469 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900470
471 java_library {
472 name: "myjar",
473 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900474 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475 sdk_version: "none",
476 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900477 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900478 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000479 // TODO: remove //apex_available:platform
480 apex_available: [
481 "//apex_available:platform",
482 "myapex",
483 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484 }
485
Jiyong Park77acec62020-06-01 21:39:15 +0900486 dex_import {
487 name: "myjar_dex",
488 jars: ["prebuilt.jar"],
489 apex_available: [
490 "//apex_available:platform",
491 "myapex",
492 ],
493 }
494
Jiyong Park7f7766d2019-07-25 22:02:35 +0900495 java_library {
496 name: "myotherjar",
497 srcs: ["foo/bar/MyClass.java"],
498 sdk_version: "none",
499 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900500 // TODO: remove //apex_available:platform
501 apex_available: [
502 "//apex_available:platform",
503 "myapex",
504 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900505 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900506
507 java_library {
508 name: "mysharedjar",
509 srcs: ["foo/bar/MyClass.java"],
510 sdk_version: "none",
511 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900512 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900513 `)
514
Paul Duffina71a67a2021-03-29 00:42:57 +0100515 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900516
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900517 // Make sure that Android.mk is created
518 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700519 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900520 var builder strings.Builder
521 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
522
523 androidMk := builder.String()
524 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
525 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
526
Jiyong Park42cca6c2019-04-01 11:15:50 +0900527 optFlags := apexRule.Args["opt_flags"]
528 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700529 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100530 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900531
Jiyong Park25fc6a92018-11-18 18:02:45 +0900532 copyCmds := apexRule.Args["copy_commands"]
533
534 // Ensure that main rule creates an output
535 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
536
537 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700538 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
539 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
540 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900541 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900542 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900543
544 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700545 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
546 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900547 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
548 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900549 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900550 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900551
552 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800553 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
554 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900555 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900556 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900557 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900558 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
559 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900560 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900561 // .. but not for java libs
562 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900563 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800564
Colin Cross7113d202019-11-20 16:39:12 -0800565 // Ensure that the platform variant ends with _shared or _common
566 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
567 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900568 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
569 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900570 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
571
572 // Ensure that dynamic dependency to java libs are not included
573 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800574
575 // Ensure that all symlinks are present.
576 found_foo_link_64 := false
577 found_foo := false
578 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900579 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800580 if strings.HasSuffix(cmd, "bin/foo") {
581 found_foo = true
582 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
583 found_foo_link_64 = true
584 }
585 }
586 }
587 good := found_foo && found_foo_link_64
588 if !good {
589 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
590 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900591
Sundong Ahnabb64432019-10-22 13:58:29 +0900592 mergeNoticesRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("mergeNoticesRule")
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700593 noticeInputs := mergeNoticesRule.Inputs.Strings()
Jiyong Park9918e1a2020-03-17 19:16:40 +0900594 if len(noticeInputs) != 3 {
595 t.Errorf("number of input notice files: expected = 3, actual = %q", len(noticeInputs))
Jiyong Park52818fc2019-03-18 12:01:38 +0900596 }
597 ensureListContains(t, noticeInputs, "NOTICE")
598 ensureListContains(t, noticeInputs, "custom_notice")
Jiyong Park9918e1a2020-03-17 19:16:40 +0900599 ensureListContains(t, noticeInputs, "custom_notice_for_static_lib")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900600
Artur Satayeva8bd1132020-04-27 18:07:06 +0100601 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100602 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100603 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
604 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
605 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100606
607 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100608 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100609 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
610 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
611 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800612}
613
Jooyung Hanf21c7972019-12-16 22:32:06 +0900614func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800615 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900616 apex_defaults {
617 name: "myapex-defaults",
618 key: "myapex.key",
619 prebuilts: ["myetc"],
620 native_shared_libs: ["mylib"],
621 java_libs: ["myjar"],
622 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900623 rros: ["rro"],
markchien2f59ec92020-09-02 16:23:38 +0800624 bpfs: ["bpf"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000625 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900626 }
627
628 prebuilt_etc {
629 name: "myetc",
630 src: "myprebuilt",
631 }
632
633 apex {
634 name: "myapex",
635 defaults: ["myapex-defaults"],
636 }
637
638 apex_key {
639 name: "myapex.key",
640 public_key: "testkey.avbpubkey",
641 private_key: "testkey.pem",
642 }
643
644 cc_library {
645 name: "mylib",
646 system_shared_libs: [],
647 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000648 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900649 }
650
651 java_library {
652 name: "myjar",
653 srcs: ["foo/bar/MyClass.java"],
654 sdk_version: "none",
655 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000656 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900657 }
658
659 android_app {
660 name: "AppFoo",
661 srcs: ["foo/bar/MyClass.java"],
662 sdk_version: "none",
663 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000664 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900665 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900666
667 runtime_resource_overlay {
668 name: "rro",
669 theme: "blue",
670 }
671
markchien2f59ec92020-09-02 16:23:38 +0800672 bpf {
673 name: "bpf",
674 srcs: ["bpf.c", "bpf2.c"],
675 }
676
Jooyung Hanf21c7972019-12-16 22:32:06 +0900677 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000678 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900679 "etc/myetc",
680 "javalib/myjar.jar",
681 "lib64/mylib.so",
682 "app/AppFoo/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900683 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800684 "etc/bpf/bpf.o",
685 "etc/bpf/bpf2.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900686 })
687}
688
Jooyung Han01a3ee22019-11-02 02:52:25 +0900689func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800690 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900691 apex {
692 name: "myapex",
693 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000694 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900695 }
696
697 apex_key {
698 name: "myapex.key",
699 public_key: "testkey.avbpubkey",
700 private_key: "testkey.pem",
701 }
702 `)
703
704 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900705 args := module.Rule("apexRule").Args
706 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
707 t.Error("manifest should be apex_manifest.pb, but " + manifest)
708 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900709}
710
Liz Kammer4854a7d2021-05-27 14:28:27 -0400711func TestApexManifestMinSdkVersion(t *testing.T) {
712 ctx := testApex(t, `
713 apex_defaults {
714 name: "my_defaults",
715 key: "myapex.key",
716 product_specific: true,
717 file_contexts: ":my-file-contexts",
718 updatable: false,
719 }
720 apex {
721 name: "myapex_30",
722 min_sdk_version: "30",
723 defaults: ["my_defaults"],
724 }
725
726 apex {
727 name: "myapex_current",
728 min_sdk_version: "current",
729 defaults: ["my_defaults"],
730 }
731
732 apex {
733 name: "myapex_none",
734 defaults: ["my_defaults"],
735 }
736
737 apex_key {
738 name: "myapex.key",
739 public_key: "testkey.avbpubkey",
740 private_key: "testkey.pem",
741 }
742
743 filegroup {
744 name: "my-file-contexts",
745 srcs: ["product_specific_file_contexts"],
746 }
747 `, withFiles(map[string][]byte{
748 "product_specific_file_contexts": nil,
749 }), android.FixtureModifyProductVariables(
750 func(variables android.FixtureProductVariables) {
751 variables.Unbundled_build = proptools.BoolPtr(true)
752 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
753 }), android.FixtureMergeEnv(map[string]string{
754 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
755 }))
756
757 testCases := []struct {
758 module string
759 minSdkVersion string
760 }{
761 {
762 module: "myapex_30",
763 minSdkVersion: "30",
764 },
765 {
766 module: "myapex_current",
767 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
768 },
769 {
770 module: "myapex_none",
771 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
772 },
773 }
774 for _, tc := range testCases {
775 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
776 args := module.Rule("apexRule").Args
777 optFlags := args["opt_flags"]
778 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
779 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
780 }
781 }
782}
783
Alex Light5098a612018-11-29 17:12:15 -0800784func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800785 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800786 apex {
787 name: "myapex",
788 key: "myapex.key",
789 payload_type: "zip",
790 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000791 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800792 }
793
794 apex_key {
795 name: "myapex.key",
796 public_key: "testkey.avbpubkey",
797 private_key: "testkey.pem",
798 }
799
800 cc_library {
801 name: "mylib",
802 srcs: ["mylib.cpp"],
803 shared_libs: ["mylib2"],
804 system_shared_libs: [],
805 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000806 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800807 }
808
809 cc_library {
810 name: "mylib2",
811 srcs: ["mylib.cpp"],
812 system_shared_libs: [],
813 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000814 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800815 }
816 `)
817
Sundong Ahnabb64432019-10-22 13:58:29 +0900818 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800819 copyCmds := zipApexRule.Args["copy_commands"]
820
821 // Ensure that main rule creates an output
822 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
823
824 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700825 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800826
827 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700828 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800829
830 // Ensure that both direct and indirect deps are copied into apex
831 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
832 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900833}
834
835func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800836 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900837 apex {
838 name: "myapex",
839 key: "myapex.key",
840 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park7d55b612021-06-11 17:22:09 +0900841 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000842 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900843 }
844
845 apex_key {
846 name: "myapex.key",
847 public_key: "testkey.avbpubkey",
848 private_key: "testkey.pem",
849 }
850
851 cc_library {
852 name: "mylib",
853 srcs: ["mylib.cpp"],
854 shared_libs: ["mylib2", "mylib3"],
855 system_shared_libs: [],
856 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000857 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900858 }
859
860 cc_library {
861 name: "mylib2",
862 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900863 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900864 system_shared_libs: [],
865 stl: "none",
866 stubs: {
867 versions: ["1", "2", "3"],
868 },
869 }
870
871 cc_library {
872 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900873 srcs: ["mylib.cpp"],
874 shared_libs: ["mylib4"],
875 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900876 stl: "none",
877 stubs: {
878 versions: ["10", "11", "12"],
879 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000880 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900881 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900882
883 cc_library {
884 name: "mylib4",
885 srcs: ["mylib.cpp"],
886 system_shared_libs: [],
887 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000888 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900889 }
Jiyong Park7d55b612021-06-11 17:22:09 +0900890
891 rust_binary {
892 name: "foo.rust",
893 srcs: ["foo.rs"],
894 shared_libs: ["libfoo.shared_from_rust"],
895 prefer_rlib: true,
896 apex_available: ["myapex"],
897 }
898
899 cc_library_shared {
900 name: "libfoo.shared_from_rust",
901 srcs: ["mylib.cpp"],
902 system_shared_libs: [],
903 stl: "none",
904 stubs: {
905 versions: ["10", "11", "12"],
906 },
907 }
908
Jiyong Park25fc6a92018-11-18 18:02:45 +0900909 `)
910
Sundong Ahnabb64432019-10-22 13:58:29 +0900911 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900912 copyCmds := apexRule.Args["copy_commands"]
913
914 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800915 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900916
917 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800918 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900919
920 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800921 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900922
Colin Crossaede88c2020-08-11 12:17:01 -0700923 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900924
925 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900926 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900928 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900929
930 // 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 -0700931 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900932 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700933 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900934
935 // Ensure that stubs libs are built without -include flags
Jiyong Park0f80c182020-01-31 02:49:53 +0900936 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park64379952018-12-13 18:37:29 +0900937 ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900938
939 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -0700940 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"])
Jooyung Han671f1ce2019-12-17 12:47:13 +0900941
Jooyung Hana57af4a2020-01-23 05:36:59 +0000942 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900943 "lib64/mylib.so",
944 "lib64/mylib3.so",
945 "lib64/mylib4.so",
Jiyong Park7d55b612021-06-11 17:22:09 +0900946 "bin/foo.rust",
947 "lib64/libc++.so", // by the implicit dependency from foo.rust
948 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900949 })
Jiyong Park7d55b612021-06-11 17:22:09 +0900950
951 // Ensure that stub dependency from a rust module is not included
952 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
953 // The rust module is linked to the stub cc library
954 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
955 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
956 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900957}
958
Jiyong Park9477c262021-06-22 20:23:05 +0900959func TestApexCanUsePrivateApis(t *testing.T) {
960 ctx := testApex(t, `
961 apex {
962 name: "myapex",
963 key: "myapex.key",
964 native_shared_libs: ["mylib"],
965 binaries: ["foo.rust"],
966 updatable: false,
967 platform_apis: true,
968 }
969
970 apex_key {
971 name: "myapex.key",
972 public_key: "testkey.avbpubkey",
973 private_key: "testkey.pem",
974 }
975
976 cc_library {
977 name: "mylib",
978 srcs: ["mylib.cpp"],
979 shared_libs: ["mylib2"],
980 system_shared_libs: [],
981 stl: "none",
982 apex_available: [ "myapex" ],
983 }
984
985 cc_library {
986 name: "mylib2",
987 srcs: ["mylib.cpp"],
988 cflags: ["-include mylib.h"],
989 system_shared_libs: [],
990 stl: "none",
991 stubs: {
992 versions: ["1", "2", "3"],
993 },
994 }
995
996 rust_binary {
997 name: "foo.rust",
998 srcs: ["foo.rs"],
999 shared_libs: ["libfoo.shared_from_rust"],
1000 prefer_rlib: true,
1001 apex_available: ["myapex"],
1002 }
1003
1004 cc_library_shared {
1005 name: "libfoo.shared_from_rust",
1006 srcs: ["mylib.cpp"],
1007 system_shared_libs: [],
1008 stl: "none",
1009 stubs: {
1010 versions: ["10", "11", "12"],
1011 },
1012 }
1013 `)
1014
1015 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1016 copyCmds := apexRule.Args["copy_commands"]
1017
1018 // Ensure that indirect stubs dep is not included
1019 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1020 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1021
1022 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1023 // of the platform_apis: true)
1024 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000_private").Rule("ld").Args["libFlags"]
1025 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1026 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1027 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000_private").Rule("rustc").Args["linkFlags"]
1028 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1029 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1030}
1031
Colin Cross7812fd32020-09-25 12:35:10 -07001032func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1033 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001034 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001035 apex {
1036 name: "myapex",
1037 key: "myapex.key",
1038 native_shared_libs: ["mylib", "mylib3"],
1039 min_sdk_version: "29",
1040 }
1041
1042 apex_key {
1043 name: "myapex.key",
1044 public_key: "testkey.avbpubkey",
1045 private_key: "testkey.pem",
1046 }
1047
1048 cc_library {
1049 name: "mylib",
1050 srcs: ["mylib.cpp"],
1051 shared_libs: ["mylib2", "mylib3"],
1052 system_shared_libs: [],
1053 stl: "none",
1054 apex_available: [ "myapex" ],
1055 min_sdk_version: "28",
1056 }
1057
1058 cc_library {
1059 name: "mylib2",
1060 srcs: ["mylib.cpp"],
1061 cflags: ["-include mylib.h"],
1062 system_shared_libs: [],
1063 stl: "none",
1064 stubs: {
1065 versions: ["28", "29", "30", "current"],
1066 },
1067 min_sdk_version: "28",
1068 }
1069
1070 cc_library {
1071 name: "mylib3",
1072 srcs: ["mylib.cpp"],
1073 shared_libs: ["mylib4"],
1074 system_shared_libs: [],
1075 stl: "none",
1076 stubs: {
1077 versions: ["28", "29", "30", "current"],
1078 },
1079 apex_available: [ "myapex" ],
1080 min_sdk_version: "28",
1081 }
1082
1083 cc_library {
1084 name: "mylib4",
1085 srcs: ["mylib.cpp"],
1086 system_shared_libs: [],
1087 stl: "none",
1088 apex_available: [ "myapex" ],
1089 min_sdk_version: "28",
1090 }
1091 `)
1092
1093 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1094 copyCmds := apexRule.Args["copy_commands"]
1095
1096 // Ensure that direct non-stubs dep is always included
1097 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1098
1099 // Ensure that indirect stubs dep is not included
1100 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1101
1102 // Ensure that direct stubs dep is included
1103 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1104
1105 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1106
Jiyong Park55549df2021-02-26 23:57:23 +09001107 // Ensure that mylib is linking with the latest version of stub for mylib2
1108 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001109 // ... and not linking to the non-stub (impl) variant of mylib2
1110 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1111
1112 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1113 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1114 // .. and not linking to the stubs variant of mylib3
1115 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1116
1117 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001118 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001119 ensureNotContains(t, mylib2Cflags, "-include ")
1120
1121 // Ensure that genstub is invoked with --apex
Colin Crossa717db72020-10-23 14:53:06 -07001122 ensureContains(t, "--apex", ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"])
Colin Cross7812fd32020-09-25 12:35:10 -07001123
1124 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1125 "lib64/mylib.so",
1126 "lib64/mylib3.so",
1127 "lib64/mylib4.so",
1128 })
1129}
1130
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001131func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1132 t.Parallel()
1133 // myapex (Z)
1134 // mylib -----------------.
1135 // |
1136 // otherapex (29) |
1137 // libstub's versions: 29 Z current
1138 // |
1139 // <platform> |
1140 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001141 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001142 apex {
1143 name: "myapex",
1144 key: "myapex.key",
1145 native_shared_libs: ["mylib"],
1146 min_sdk_version: "Z", // non-final
1147 }
1148
1149 cc_library {
1150 name: "mylib",
1151 srcs: ["mylib.cpp"],
1152 shared_libs: ["libstub"],
1153 apex_available: ["myapex"],
1154 min_sdk_version: "Z",
1155 }
1156
1157 apex_key {
1158 name: "myapex.key",
1159 public_key: "testkey.avbpubkey",
1160 private_key: "testkey.pem",
1161 }
1162
1163 apex {
1164 name: "otherapex",
1165 key: "myapex.key",
1166 native_shared_libs: ["libstub"],
1167 min_sdk_version: "29",
1168 }
1169
1170 cc_library {
1171 name: "libstub",
1172 srcs: ["mylib.cpp"],
1173 stubs: {
1174 versions: ["29", "Z", "current"],
1175 },
1176 apex_available: ["otherapex"],
1177 min_sdk_version: "29",
1178 }
1179
1180 // platform module depending on libstub from otherapex should use the latest stub("current")
1181 cc_library {
1182 name: "libplatform",
1183 srcs: ["mylib.cpp"],
1184 shared_libs: ["libstub"],
1185 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001186 `,
1187 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1188 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1189 variables.Platform_sdk_final = proptools.BoolPtr(false)
1190 variables.Platform_version_active_codenames = []string{"Z"}
1191 }),
1192 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001193
Jiyong Park55549df2021-02-26 23:57:23 +09001194 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001195 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001196 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001197 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001198 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001199
1200 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1201 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1202 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1203 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1204 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1205}
1206
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001207func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001208 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001209 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001210 name: "myapex2",
1211 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001212 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001213 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001214 }
1215
1216 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001217 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001218 public_key: "testkey.avbpubkey",
1219 private_key: "testkey.pem",
1220 }
1221
1222 cc_library {
1223 name: "mylib",
1224 srcs: ["mylib.cpp"],
1225 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001226 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001227 system_shared_libs: [],
1228 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001229 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001230 }
1231
1232 cc_library {
1233 name: "libfoo",
1234 srcs: ["mylib.cpp"],
1235 shared_libs: ["libbar"],
1236 system_shared_libs: [],
1237 stl: "none",
1238 stubs: {
1239 versions: ["10", "20", "30"],
1240 },
1241 }
1242
1243 cc_library {
1244 name: "libbar",
1245 srcs: ["mylib.cpp"],
1246 system_shared_libs: [],
1247 stl: "none",
1248 }
1249
Jiyong Park678c8812020-02-07 17:25:49 +09001250 cc_library_static {
1251 name: "libbaz",
1252 srcs: ["mylib.cpp"],
1253 system_shared_libs: [],
1254 stl: "none",
1255 apex_available: [ "myapex2" ],
1256 }
1257
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001258 `)
1259
Jiyong Park83dc74b2020-01-14 18:38:44 +09001260 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001261 copyCmds := apexRule.Args["copy_commands"]
1262
1263 // Ensure that direct non-stubs dep is always included
1264 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1265
1266 // Ensure that indirect stubs dep is not included
1267 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1268
1269 // Ensure that dependency of stubs is not included
1270 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1271
Colin Crossaede88c2020-08-11 12:17:01 -07001272 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001273
1274 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001275 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001276 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001277 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001278
Jiyong Park3ff16992019-12-27 14:11:47 +09001279 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001280
1281 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1282 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001283
Artur Satayeva8bd1132020-04-27 18:07:06 +01001284 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001285 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001286
Artur Satayeva8bd1132020-04-27 18:07:06 +01001287 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001288 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001289}
1290
Jooyung Hand3639552019-08-09 12:57:43 +09001291func TestApexWithRuntimeLibsDependency(t *testing.T) {
1292 /*
1293 myapex
1294 |
1295 v (runtime_libs)
1296 mylib ------+------> libfoo [provides stub]
1297 |
1298 `------> libbar
1299 */
Colin Cross1c460562021-02-16 17:55:47 -08001300 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001301 apex {
1302 name: "myapex",
1303 key: "myapex.key",
1304 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001305 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001306 }
1307
1308 apex_key {
1309 name: "myapex.key",
1310 public_key: "testkey.avbpubkey",
1311 private_key: "testkey.pem",
1312 }
1313
1314 cc_library {
1315 name: "mylib",
1316 srcs: ["mylib.cpp"],
1317 runtime_libs: ["libfoo", "libbar"],
1318 system_shared_libs: [],
1319 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001320 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001321 }
1322
1323 cc_library {
1324 name: "libfoo",
1325 srcs: ["mylib.cpp"],
1326 system_shared_libs: [],
1327 stl: "none",
1328 stubs: {
1329 versions: ["10", "20", "30"],
1330 },
1331 }
1332
1333 cc_library {
1334 name: "libbar",
1335 srcs: ["mylib.cpp"],
1336 system_shared_libs: [],
1337 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001338 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001339 }
1340
1341 `)
1342
Sundong Ahnabb64432019-10-22 13:58:29 +09001343 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001344 copyCmds := apexRule.Args["copy_commands"]
1345
1346 // Ensure that direct non-stubs dep is always included
1347 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1348
1349 // Ensure that indirect stubs dep is not included
1350 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1351
1352 // Ensure that runtime_libs dep in included
1353 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1354
Sundong Ahnabb64432019-10-22 13:58:29 +09001355 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001356 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1357 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001358
1359}
1360
Paul Duffina02cae32021-03-09 01:44:06 +00001361var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1362 cc.PrepareForTestWithCcBuildComponents,
1363 PrepareForTestWithApexBuildComponents,
1364 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001365 apex {
1366 name: "com.android.runtime",
1367 key: "com.android.runtime.key",
1368 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001369 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001370 }
1371
1372 apex_key {
1373 name: "com.android.runtime.key",
1374 public_key: "testkey.avbpubkey",
1375 private_key: "testkey.pem",
1376 }
Paul Duffina02cae32021-03-09 01:44:06 +00001377 `),
1378 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1379)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001380
Paul Duffina02cae32021-03-09 01:44:06 +00001381func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001382 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001383 cc_library {
1384 name: "libc",
1385 no_libcrt: true,
1386 nocrt: true,
1387 stl: "none",
1388 system_shared_libs: [],
1389 stubs: { versions: ["1"] },
1390 apex_available: ["com.android.runtime"],
1391
1392 sanitize: {
1393 hwaddress: true,
1394 }
1395 }
1396
1397 cc_prebuilt_library_shared {
1398 name: "libclang_rt.hwasan-aarch64-android",
1399 no_libcrt: true,
1400 nocrt: true,
1401 stl: "none",
1402 system_shared_libs: [],
1403 srcs: [""],
1404 stubs: { versions: ["1"] },
1405
1406 sanitize: {
1407 never: true,
1408 },
Paul Duffina02cae32021-03-09 01:44:06 +00001409 } `)
1410 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001411
1412 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1413 "lib64/bionic/libc.so",
1414 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1415 })
1416
1417 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1418
1419 installed := hwasan.Description("install libclang_rt.hwasan")
1420 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1421
1422 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1423 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1424 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1425}
1426
1427func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001428 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001429 prepareForTestOfRuntimeApexWithHwasan,
1430 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1431 variables.SanitizeDevice = []string{"hwaddress"}
1432 }),
1433 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001434 cc_library {
1435 name: "libc",
1436 no_libcrt: true,
1437 nocrt: true,
1438 stl: "none",
1439 system_shared_libs: [],
1440 stubs: { versions: ["1"] },
1441 apex_available: ["com.android.runtime"],
1442 }
1443
1444 cc_prebuilt_library_shared {
1445 name: "libclang_rt.hwasan-aarch64-android",
1446 no_libcrt: true,
1447 nocrt: true,
1448 stl: "none",
1449 system_shared_libs: [],
1450 srcs: [""],
1451 stubs: { versions: ["1"] },
1452
1453 sanitize: {
1454 never: true,
1455 },
1456 }
Paul Duffina02cae32021-03-09 01:44:06 +00001457 `)
1458 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001459
1460 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1461 "lib64/bionic/libc.so",
1462 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1463 })
1464
1465 hwasan := ctx.ModuleForTests("libclang_rt.hwasan-aarch64-android", "android_arm64_armv8-a_shared")
1466
1467 installed := hwasan.Description("install libclang_rt.hwasan")
1468 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1469
1470 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1471 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1472 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1473}
1474
Jooyung Han61b66e92020-03-21 14:21:46 +00001475func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1476 testcases := []struct {
1477 name string
1478 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001479 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001480 shouldLink string
1481 shouldNotLink []string
1482 }{
1483 {
Jiyong Park55549df2021-02-26 23:57:23 +09001484 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001485 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001486 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001487 shouldLink: "current",
1488 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001489 },
1490 {
Jiyong Park55549df2021-02-26 23:57:23 +09001491 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001492 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001493 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001494 shouldLink: "current",
1495 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001496 },
1497 }
1498 for _, tc := range testcases {
1499 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001500 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001501 apex {
1502 name: "myapex",
1503 key: "myapex.key",
1504 use_vendor: true,
1505 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001506 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001507 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001508 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001509
Jooyung Han61b66e92020-03-21 14:21:46 +00001510 apex_key {
1511 name: "myapex.key",
1512 public_key: "testkey.avbpubkey",
1513 private_key: "testkey.pem",
1514 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001515
Jooyung Han61b66e92020-03-21 14:21:46 +00001516 cc_library {
1517 name: "mylib",
1518 srcs: ["mylib.cpp"],
1519 vendor_available: true,
1520 shared_libs: ["libbar"],
1521 system_shared_libs: [],
1522 stl: "none",
1523 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001524 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001525 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001526
Jooyung Han61b66e92020-03-21 14:21:46 +00001527 cc_library {
1528 name: "libbar",
1529 srcs: ["mylib.cpp"],
1530 system_shared_libs: [],
1531 stl: "none",
1532 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001533 llndk: {
1534 symbol_file: "libbar.map.txt",
1535 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001536 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001537 `,
1538 setUseVendorAllowListForTest([]string{"myapex"}),
1539 withUnbundledBuild,
1540 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001541
Jooyung Han61b66e92020-03-21 14:21:46 +00001542 // Ensure that LLNDK dep is not included
1543 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1544 "lib64/mylib.so",
1545 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001546
Jooyung Han61b66e92020-03-21 14:21:46 +00001547 // Ensure that LLNDK dep is required
1548 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1549 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1550 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001551
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001552 mylibLdFlags := ctx.ModuleForTests("mylib", "android_vendor.29_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1553 ensureContains(t, mylibLdFlags, "libbar/android_vendor.29_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001554 for _, ver := range tc.shouldNotLink {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001555 ensureNotContains(t, mylibLdFlags, "libbar/android_vendor.29_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001556 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001557
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001558 mylibCFlags := ctx.ModuleForTests("mylib", "android_vendor.29_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001559 ver := tc.shouldLink
1560 if tc.shouldLink == "current" {
1561 ver = strconv.Itoa(android.FutureApiLevelInt)
1562 }
1563 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001564 })
1565 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001566}
1567
Jiyong Park25fc6a92018-11-18 18:02:45 +09001568func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001569 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001570 apex {
1571 name: "myapex",
1572 key: "myapex.key",
1573 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001574 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001575 }
1576
1577 apex_key {
1578 name: "myapex.key",
1579 public_key: "testkey.avbpubkey",
1580 private_key: "testkey.pem",
1581 }
1582
1583 cc_library {
1584 name: "mylib",
1585 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001586 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001587 shared_libs: ["libdl#27"],
1588 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001589 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001590 }
1591
1592 cc_library_shared {
1593 name: "mylib_shared",
1594 srcs: ["mylib.cpp"],
1595 shared_libs: ["libdl#27"],
1596 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001597 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001598 }
1599
1600 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001601 name: "libBootstrap",
1602 srcs: ["mylib.cpp"],
1603 stl: "none",
1604 bootstrap: true,
1605 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001606 `)
1607
Sundong Ahnabb64432019-10-22 13:58:29 +09001608 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001609 copyCmds := apexRule.Args["copy_commands"]
1610
1611 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001612 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001613 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1614 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001615
1616 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001617 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001618
Colin Crossaede88c2020-08-11 12:17:01 -07001619 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1620 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1621 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001622
1623 // For dependency to libc
1624 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001625 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001626 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001627 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001628 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001629 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1630 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001631
1632 // For dependency to libm
1633 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001634 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001635 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001636 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001637 // ... and is not compiling with the stub
1638 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1639 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1640
1641 // For dependency to libdl
1642 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001643 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001644 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001645 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1646 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001647 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001648 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001649 // ... Cflags from stub is correctly exported to mylib
1650 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1651 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001652
1653 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001654 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1655 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1656 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1657 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001658}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001659
Jooyung Han749dc692020-04-15 11:03:39 +09001660func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001661 // there are three links between liba --> libz.
1662 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001663 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001664 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001665 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001666 apex {
1667 name: "myapex",
1668 key: "myapex.key",
1669 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001670 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001671 }
1672
1673 apex {
1674 name: "otherapex",
1675 key: "myapex.key",
1676 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001677 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001678 }
1679
1680 apex_key {
1681 name: "myapex.key",
1682 public_key: "testkey.avbpubkey",
1683 private_key: "testkey.pem",
1684 }
1685
1686 cc_library {
1687 name: "libx",
1688 shared_libs: ["liba"],
1689 system_shared_libs: [],
1690 stl: "none",
1691 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001692 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001693 }
1694
1695 cc_library {
1696 name: "liby",
1697 shared_libs: ["liba"],
1698 system_shared_libs: [],
1699 stl: "none",
1700 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001701 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001702 }
1703
1704 cc_library {
1705 name: "liba",
1706 shared_libs: ["libz"],
1707 system_shared_libs: [],
1708 stl: "none",
1709 apex_available: [
1710 "//apex_available:anyapex",
1711 "//apex_available:platform",
1712 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001713 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001714 }
1715
1716 cc_library {
1717 name: "libz",
1718 system_shared_libs: [],
1719 stl: "none",
1720 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001721 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001722 },
1723 }
Jooyung Han749dc692020-04-15 11:03:39 +09001724 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001725
1726 expectLink := func(from, from_variant, to, to_variant string) {
1727 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1728 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1729 }
1730 expectNoLink := func(from, from_variant, to, to_variant string) {
1731 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1732 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1733 }
1734 // platform liba is linked to non-stub version
1735 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001736 // liba in myapex is linked to current
1737 expectLink("liba", "shared_apex29", "libz", "shared_current")
1738 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001739 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001740 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001741 // liba in otherapex is linked to current
1742 expectLink("liba", "shared_apex30", "libz", "shared_current")
1743 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001744 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1745 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001746}
1747
Jooyung Hanaed150d2020-04-02 01:41:41 +09001748func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001749 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001750 apex {
1751 name: "myapex",
1752 key: "myapex.key",
1753 native_shared_libs: ["libx"],
1754 min_sdk_version: "R",
1755 }
1756
1757 apex_key {
1758 name: "myapex.key",
1759 public_key: "testkey.avbpubkey",
1760 private_key: "testkey.pem",
1761 }
1762
1763 cc_library {
1764 name: "libx",
1765 shared_libs: ["libz"],
1766 system_shared_libs: [],
1767 stl: "none",
1768 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001769 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001770 }
1771
1772 cc_library {
1773 name: "libz",
1774 system_shared_libs: [],
1775 stl: "none",
1776 stubs: {
1777 versions: ["29", "R"],
1778 },
1779 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001780 `,
1781 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1782 variables.Platform_version_active_codenames = []string{"R"}
1783 }),
1784 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001785
1786 expectLink := func(from, from_variant, to, to_variant string) {
1787 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1788 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1789 }
1790 expectNoLink := func(from, from_variant, to, to_variant string) {
1791 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1792 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1793 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001794 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1795 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001796 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1797 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001798}
1799
Jooyung Han4c4da062021-06-23 10:23:16 +09001800func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1801 testApex(t, `
1802 apex {
1803 name: "myapex",
1804 key: "myapex.key",
1805 java_libs: ["libx"],
1806 min_sdk_version: "S",
1807 }
1808
1809 apex_key {
1810 name: "myapex.key",
1811 public_key: "testkey.avbpubkey",
1812 private_key: "testkey.pem",
1813 }
1814
1815 java_library {
1816 name: "libx",
1817 srcs: ["a.java"],
1818 apex_available: [ "myapex" ],
1819 sdk_version: "current",
1820 min_sdk_version: "S", // should be okay
1821 }
1822 `,
1823 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1824 variables.Platform_version_active_codenames = []string{"S"}
1825 variables.Platform_sdk_codename = proptools.StringPtr("S")
1826 }),
1827 )
1828}
1829
Jooyung Han749dc692020-04-15 11:03:39 +09001830func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001831 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001832 apex {
1833 name: "myapex",
1834 key: "myapex.key",
1835 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001836 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001837 }
1838
1839 apex_key {
1840 name: "myapex.key",
1841 public_key: "testkey.avbpubkey",
1842 private_key: "testkey.pem",
1843 }
1844
1845 cc_library {
1846 name: "libx",
1847 shared_libs: ["libz"],
1848 system_shared_libs: [],
1849 stl: "none",
1850 apex_available: [ "myapex" ],
1851 }
1852
1853 cc_library {
1854 name: "libz",
1855 system_shared_libs: [],
1856 stl: "none",
1857 stubs: {
1858 versions: ["1", "2"],
1859 },
1860 }
1861 `)
1862
1863 expectLink := func(from, from_variant, to, to_variant string) {
1864 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1865 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1866 }
1867 expectNoLink := func(from, from_variant, to, to_variant string) {
1868 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1869 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1870 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001871 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001872 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001873 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001874 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001875}
1876
1877func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001878 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001879 apex {
1880 name: "myapex",
1881 key: "myapex.key",
1882 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001883 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001884 }
1885
1886 apex_key {
1887 name: "myapex.key",
1888 public_key: "testkey.avbpubkey",
1889 private_key: "testkey.pem",
1890 }
1891
1892 cc_library {
1893 name: "libx",
1894 system_shared_libs: [],
1895 stl: "none",
1896 apex_available: [ "myapex" ],
1897 stubs: {
1898 versions: ["1", "2"],
1899 },
1900 }
1901
1902 cc_library {
1903 name: "libz",
1904 shared_libs: ["libx"],
1905 system_shared_libs: [],
1906 stl: "none",
1907 }
1908 `)
1909
1910 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001911 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001912 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1913 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1914 }
1915 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001916 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001917 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1918 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1919 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001920 expectLink("libz", "shared", "libx", "shared_current")
1921 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001922 expectNoLink("libz", "shared", "libz", "shared_1")
1923 expectNoLink("libz", "shared", "libz", "shared")
1924}
1925
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001926var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1927 func(variables android.FixtureProductVariables) {
1928 variables.SanitizeDevice = []string{"hwaddress"}
1929 },
1930)
1931
Jooyung Han75568392020-03-20 04:29:24 +09001932func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001933 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001934 apex {
1935 name: "myapex",
1936 key: "myapex.key",
1937 native_shared_libs: ["libx"],
1938 min_sdk_version: "29",
1939 }
1940
1941 apex_key {
1942 name: "myapex.key",
1943 public_key: "testkey.avbpubkey",
1944 private_key: "testkey.pem",
1945 }
1946
1947 cc_library {
1948 name: "libx",
1949 shared_libs: ["libbar"],
1950 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001951 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001952 }
1953
1954 cc_library {
1955 name: "libbar",
1956 stubs: {
1957 versions: ["29", "30"],
1958 },
1959 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001960 `,
1961 prepareForTestWithSantitizeHwaddress,
1962 )
Jooyung Han03b51852020-02-26 22:45:42 +09001963 expectLink := func(from, from_variant, to, to_variant string) {
1964 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
1965 libFlags := ld.Args["libFlags"]
1966 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1967 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001968 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09001969}
1970
Jooyung Han75568392020-03-20 04:29:24 +09001971func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001972 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001973 apex {
1974 name: "myapex",
1975 key: "myapex.key",
1976 native_shared_libs: ["libx"],
1977 min_sdk_version: "29",
1978 }
1979
1980 apex_key {
1981 name: "myapex.key",
1982 public_key: "testkey.avbpubkey",
1983 private_key: "testkey.pem",
1984 }
1985
1986 cc_library {
1987 name: "libx",
1988 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001989 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001990 }
Jooyung Han75568392020-03-20 04:29:24 +09001991 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001992
1993 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07001994 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001995 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001996 // note that platform variant is not.
1997 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08001998 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09001999}
2000
Jooyung Han749dc692020-04-15 11:03:39 +09002001func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2002 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002003 apex {
2004 name: "myapex",
2005 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002006 native_shared_libs: ["mylib"],
2007 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002008 }
2009
2010 apex_key {
2011 name: "myapex.key",
2012 public_key: "testkey.avbpubkey",
2013 private_key: "testkey.pem",
2014 }
Jooyung Han749dc692020-04-15 11:03:39 +09002015
2016 cc_library {
2017 name: "mylib",
2018 srcs: ["mylib.cpp"],
2019 system_shared_libs: [],
2020 stl: "none",
2021 apex_available: [
2022 "myapex",
2023 ],
2024 min_sdk_version: "30",
2025 }
2026 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002027
2028 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2029 apex {
2030 name: "myapex",
2031 key: "myapex.key",
2032 native_shared_libs: ["libfoo.ffi"],
2033 min_sdk_version: "29",
2034 }
2035
2036 apex_key {
2037 name: "myapex.key",
2038 public_key: "testkey.avbpubkey",
2039 private_key: "testkey.pem",
2040 }
2041
2042 rust_ffi_shared {
2043 name: "libfoo.ffi",
2044 srcs: ["foo.rs"],
2045 crate_name: "foo",
2046 apex_available: [
2047 "myapex",
2048 ],
2049 min_sdk_version: "30",
2050 }
2051 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002052
2053 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2054 apex {
2055 name: "myapex",
2056 key: "myapex.key",
2057 java_libs: ["libfoo"],
2058 min_sdk_version: "29",
2059 }
2060
2061 apex_key {
2062 name: "myapex.key",
2063 public_key: "testkey.avbpubkey",
2064 private_key: "testkey.pem",
2065 }
2066
2067 java_import {
2068 name: "libfoo",
2069 jars: ["libfoo.jar"],
2070 apex_available: [
2071 "myapex",
2072 ],
2073 min_sdk_version: "30",
2074 }
2075 `)
Jooyung Han749dc692020-04-15 11:03:39 +09002076}
2077
2078func TestApexMinSdkVersion_Okay(t *testing.T) {
2079 testApex(t, `
2080 apex {
2081 name: "myapex",
2082 key: "myapex.key",
2083 native_shared_libs: ["libfoo"],
2084 java_libs: ["libbar"],
2085 min_sdk_version: "29",
2086 }
2087
2088 apex_key {
2089 name: "myapex.key",
2090 public_key: "testkey.avbpubkey",
2091 private_key: "testkey.pem",
2092 }
2093
2094 cc_library {
2095 name: "libfoo",
2096 srcs: ["mylib.cpp"],
2097 shared_libs: ["libfoo_dep"],
2098 apex_available: ["myapex"],
2099 min_sdk_version: "29",
2100 }
2101
2102 cc_library {
2103 name: "libfoo_dep",
2104 srcs: ["mylib.cpp"],
2105 apex_available: ["myapex"],
2106 min_sdk_version: "29",
2107 }
2108
2109 java_library {
2110 name: "libbar",
2111 sdk_version: "current",
2112 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002113 static_libs: [
2114 "libbar_dep",
2115 "libbar_import_dep",
2116 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002117 apex_available: ["myapex"],
2118 min_sdk_version: "29",
2119 }
2120
2121 java_library {
2122 name: "libbar_dep",
2123 sdk_version: "current",
2124 srcs: ["a.java"],
2125 apex_available: ["myapex"],
2126 min_sdk_version: "29",
2127 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002128
2129 java_import {
2130 name: "libbar_import_dep",
2131 jars: ["libbar.jar"],
2132 apex_available: ["myapex"],
2133 min_sdk_version: "29",
2134 }
Jooyung Han03b51852020-02-26 22:45:42 +09002135 `)
2136}
2137
Artur Satayev8cf899a2020-04-15 17:29:42 +01002138func TestJavaStableSdkVersion(t *testing.T) {
2139 testCases := []struct {
2140 name string
2141 expectedError string
2142 bp string
2143 }{
2144 {
2145 name: "Non-updatable apex with non-stable dep",
2146 bp: `
2147 apex {
2148 name: "myapex",
2149 java_libs: ["myjar"],
2150 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002151 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002152 }
2153 apex_key {
2154 name: "myapex.key",
2155 public_key: "testkey.avbpubkey",
2156 private_key: "testkey.pem",
2157 }
2158 java_library {
2159 name: "myjar",
2160 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002161 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002162 apex_available: ["myapex"],
2163 }
2164 `,
2165 },
2166 {
2167 name: "Updatable apex with stable dep",
2168 bp: `
2169 apex {
2170 name: "myapex",
2171 java_libs: ["myjar"],
2172 key: "myapex.key",
2173 updatable: true,
2174 min_sdk_version: "29",
2175 }
2176 apex_key {
2177 name: "myapex.key",
2178 public_key: "testkey.avbpubkey",
2179 private_key: "testkey.pem",
2180 }
2181 java_library {
2182 name: "myjar",
2183 srcs: ["foo/bar/MyClass.java"],
2184 sdk_version: "current",
2185 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002186 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002187 }
2188 `,
2189 },
2190 {
2191 name: "Updatable apex with non-stable dep",
2192 expectedError: "cannot depend on \"myjar\"",
2193 bp: `
2194 apex {
2195 name: "myapex",
2196 java_libs: ["myjar"],
2197 key: "myapex.key",
2198 updatable: true,
2199 }
2200 apex_key {
2201 name: "myapex.key",
2202 public_key: "testkey.avbpubkey",
2203 private_key: "testkey.pem",
2204 }
2205 java_library {
2206 name: "myjar",
2207 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002208 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002209 apex_available: ["myapex"],
2210 }
2211 `,
2212 },
2213 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002214 name: "Updatable apex with non-stable transitive dep",
2215 // This is not actually detecting that the transitive dependency is unstable, rather it is
2216 // detecting that the transitive dependency is building against a wider API surface than the
2217 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002218 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002219 bp: `
2220 apex {
2221 name: "myapex",
2222 java_libs: ["myjar"],
2223 key: "myapex.key",
2224 updatable: true,
2225 }
2226 apex_key {
2227 name: "myapex.key",
2228 public_key: "testkey.avbpubkey",
2229 private_key: "testkey.pem",
2230 }
2231 java_library {
2232 name: "myjar",
2233 srcs: ["foo/bar/MyClass.java"],
2234 sdk_version: "current",
2235 apex_available: ["myapex"],
2236 static_libs: ["transitive-jar"],
2237 }
2238 java_library {
2239 name: "transitive-jar",
2240 srcs: ["foo/bar/MyClass.java"],
2241 sdk_version: "core_platform",
2242 apex_available: ["myapex"],
2243 }
2244 `,
2245 },
2246 }
2247
2248 for _, test := range testCases {
2249 t.Run(test.name, func(t *testing.T) {
2250 if test.expectedError == "" {
2251 testApex(t, test.bp)
2252 } else {
2253 testApexError(t, test.expectedError, test.bp)
2254 }
2255 })
2256 }
2257}
2258
Jooyung Han749dc692020-04-15 11:03:39 +09002259func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2260 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2261 apex {
2262 name: "myapex",
2263 key: "myapex.key",
2264 native_shared_libs: ["mylib"],
2265 min_sdk_version: "29",
2266 }
2267
2268 apex_key {
2269 name: "myapex.key",
2270 public_key: "testkey.avbpubkey",
2271 private_key: "testkey.pem",
2272 }
2273
2274 cc_library {
2275 name: "mylib",
2276 srcs: ["mylib.cpp"],
2277 shared_libs: ["mylib2"],
2278 system_shared_libs: [],
2279 stl: "none",
2280 apex_available: [
2281 "myapex",
2282 ],
2283 min_sdk_version: "29",
2284 }
2285
2286 // indirect part of the apex
2287 cc_library {
2288 name: "mylib2",
2289 srcs: ["mylib.cpp"],
2290 system_shared_libs: [],
2291 stl: "none",
2292 apex_available: [
2293 "myapex",
2294 ],
2295 min_sdk_version: "30",
2296 }
2297 `)
2298}
2299
2300func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2301 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2302 apex {
2303 name: "myapex",
2304 key: "myapex.key",
2305 apps: ["AppFoo"],
2306 min_sdk_version: "29",
2307 }
2308
2309 apex_key {
2310 name: "myapex.key",
2311 public_key: "testkey.avbpubkey",
2312 private_key: "testkey.pem",
2313 }
2314
2315 android_app {
2316 name: "AppFoo",
2317 srcs: ["foo/bar/MyClass.java"],
2318 sdk_version: "current",
2319 min_sdk_version: "29",
2320 system_modules: "none",
2321 stl: "none",
2322 static_libs: ["bar"],
2323 apex_available: [ "myapex" ],
2324 }
2325
2326 java_library {
2327 name: "bar",
2328 sdk_version: "current",
2329 srcs: ["a.java"],
2330 apex_available: [ "myapex" ],
2331 }
2332 `)
2333}
2334
2335func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002336 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002337 apex {
2338 name: "myapex",
2339 key: "myapex.key",
2340 native_shared_libs: ["mylib"],
2341 min_sdk_version: "29",
2342 }
2343
2344 apex_key {
2345 name: "myapex.key",
2346 public_key: "testkey.avbpubkey",
2347 private_key: "testkey.pem",
2348 }
2349
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002350 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002351 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2352 cc_library {
2353 name: "mylib",
2354 srcs: ["mylib.cpp"],
2355 shared_libs: ["mylib2"],
2356 system_shared_libs: [],
2357 stl: "none",
2358 apex_available: ["myapex", "otherapex"],
2359 min_sdk_version: "29",
2360 }
2361
2362 cc_library {
2363 name: "mylib2",
2364 srcs: ["mylib.cpp"],
2365 system_shared_libs: [],
2366 stl: "none",
2367 apex_available: ["otherapex"],
2368 stubs: { versions: ["29", "30"] },
2369 min_sdk_version: "30",
2370 }
2371
2372 apex {
2373 name: "otherapex",
2374 key: "myapex.key",
2375 native_shared_libs: ["mylib", "mylib2"],
2376 min_sdk_version: "30",
2377 }
2378 `)
2379 expectLink := func(from, from_variant, to, to_variant string) {
2380 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2381 libFlags := ld.Args["libFlags"]
2382 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2383 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002384 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002385 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002386}
2387
Jooyung Haned124c32021-01-26 11:43:46 +09002388func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002389 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2390 func(variables android.FixtureProductVariables) {
2391 variables.Platform_sdk_codename = proptools.StringPtr("S")
2392 variables.Platform_version_active_codenames = []string{"S"}
2393 },
2394 )
Jooyung Haned124c32021-01-26 11:43:46 +09002395 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2396 apex {
2397 name: "myapex",
2398 key: "myapex.key",
2399 native_shared_libs: ["libfoo"],
2400 min_sdk_version: "S",
2401 }
2402 apex_key {
2403 name: "myapex.key",
2404 public_key: "testkey.avbpubkey",
2405 private_key: "testkey.pem",
2406 }
2407 cc_library {
2408 name: "libfoo",
2409 shared_libs: ["libbar"],
2410 apex_available: ["myapex"],
2411 min_sdk_version: "29",
2412 }
2413 cc_library {
2414 name: "libbar",
2415 apex_available: ["myapex"],
2416 }
2417 `, withSAsActiveCodeNames)
2418}
2419
2420func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002421 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2422 variables.Platform_sdk_codename = proptools.StringPtr("S")
2423 variables.Platform_version_active_codenames = []string{"S", "T"}
2424 })
Colin Cross1c460562021-02-16 17:55:47 -08002425 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002426 apex {
2427 name: "myapex",
2428 key: "myapex.key",
2429 native_shared_libs: ["libfoo"],
2430 min_sdk_version: "S",
2431 }
2432 apex_key {
2433 name: "myapex.key",
2434 public_key: "testkey.avbpubkey",
2435 private_key: "testkey.pem",
2436 }
2437 cc_library {
2438 name: "libfoo",
2439 shared_libs: ["libbar"],
2440 apex_available: ["myapex"],
2441 min_sdk_version: "S",
2442 }
2443 cc_library {
2444 name: "libbar",
2445 stubs: {
2446 symbol_file: "libbar.map.txt",
2447 versions: ["30", "S", "T"],
2448 },
2449 }
2450 `, withSAsActiveCodeNames)
2451
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002452 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002453 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2454 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002455 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002456}
2457
Jiyong Park7c2ee712018-12-07 00:42:25 +09002458func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002459 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002460 apex {
2461 name: "myapex",
2462 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002463 native_shared_libs: ["mylib"],
2464 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002465 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002466 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002467 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002468 }
2469
2470 apex_key {
2471 name: "myapex.key",
2472 public_key: "testkey.avbpubkey",
2473 private_key: "testkey.pem",
2474 }
2475
2476 prebuilt_etc {
2477 name: "myetc",
2478 src: "myprebuilt",
2479 sub_dir: "foo/bar",
2480 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002481
2482 cc_library {
2483 name: "mylib",
2484 srcs: ["mylib.cpp"],
2485 relative_install_path: "foo/bar",
2486 system_shared_libs: [],
2487 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002488 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002489 }
2490
2491 cc_binary {
2492 name: "mybin",
2493 srcs: ["mylib.cpp"],
2494 relative_install_path: "foo/bar",
2495 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002496 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002497 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002498 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002499 `)
2500
Sundong Ahnabb64432019-10-22 13:58:29 +09002501 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002502 dirs := strings.Split(generateFsRule.Args["exec_paths"], " ")
2503
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002504 // Ensure that the subdirectories are all listed
Jiyong Park7c2ee712018-12-07 00:42:25 +09002505 ensureListContains(t, dirs, "etc")
2506 ensureListContains(t, dirs, "etc/foo")
2507 ensureListContains(t, dirs, "etc/foo/bar")
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002508 ensureListContains(t, dirs, "lib64")
2509 ensureListContains(t, dirs, "lib64/foo")
2510 ensureListContains(t, dirs, "lib64/foo/bar")
2511 ensureListContains(t, dirs, "lib")
2512 ensureListContains(t, dirs, "lib/foo")
2513 ensureListContains(t, dirs, "lib/foo/bar")
2514
Jiyong Parkbd13e442019-03-15 18:10:35 +09002515 ensureListContains(t, dirs, "bin")
2516 ensureListContains(t, dirs, "bin/foo")
2517 ensureListContains(t, dirs, "bin/foo/bar")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002518}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002519
Jooyung Han35155c42020-02-06 17:33:20 +09002520func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002521 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002522 apex {
2523 name: "myapex",
2524 key: "myapex.key",
2525 multilib: {
2526 both: {
2527 native_shared_libs: ["mylib"],
2528 binaries: ["mybin"],
2529 },
2530 },
2531 compile_multilib: "both",
2532 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002533 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002534 }
2535
2536 apex_key {
2537 name: "myapex.key",
2538 public_key: "testkey.avbpubkey",
2539 private_key: "testkey.pem",
2540 }
2541
2542 cc_library {
2543 name: "mylib",
2544 relative_install_path: "foo/bar",
2545 system_shared_libs: [],
2546 stl: "none",
2547 apex_available: [ "myapex" ],
2548 native_bridge_supported: true,
2549 }
2550
2551 cc_binary {
2552 name: "mybin",
2553 relative_install_path: "foo/bar",
2554 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002555 stl: "none",
2556 apex_available: [ "myapex" ],
2557 native_bridge_supported: true,
2558 compile_multilib: "both", // default is "first" for binary
2559 multilib: {
2560 lib64: {
2561 suffix: "64",
2562 },
2563 },
2564 }
2565 `, withNativeBridgeEnabled)
2566 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2567 "bin/foo/bar/mybin",
2568 "bin/foo/bar/mybin64",
2569 "bin/arm/foo/bar/mybin",
2570 "bin/arm64/foo/bar/mybin64",
2571 "lib/foo/bar/mylib.so",
2572 "lib/arm/foo/bar/mylib.so",
2573 "lib64/foo/bar/mylib.so",
2574 "lib64/arm64/foo/bar/mylib.so",
2575 })
2576}
2577
Jiyong Parkda6eb592018-12-19 17:12:36 +09002578func TestUseVendor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002579 ctx := testApex(t, `
Jiyong Parkda6eb592018-12-19 17:12:36 +09002580 apex {
2581 name: "myapex",
2582 key: "myapex.key",
2583 native_shared_libs: ["mylib"],
2584 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002585 updatable: false,
Jiyong Parkda6eb592018-12-19 17:12:36 +09002586 }
2587
2588 apex_key {
2589 name: "myapex.key",
2590 public_key: "testkey.avbpubkey",
2591 private_key: "testkey.pem",
2592 }
2593
2594 cc_library {
2595 name: "mylib",
2596 srcs: ["mylib.cpp"],
2597 shared_libs: ["mylib2"],
2598 system_shared_libs: [],
2599 vendor_available: true,
2600 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002601 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002602 }
2603
2604 cc_library {
2605 name: "mylib2",
2606 srcs: ["mylib.cpp"],
2607 system_shared_libs: [],
2608 vendor_available: true,
2609 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002610 apex_available: [ "myapex" ],
Jiyong Parkda6eb592018-12-19 17:12:36 +09002611 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002612 `,
2613 setUseVendorAllowListForTest([]string{"myapex"}),
2614 )
Jiyong Parkda6eb592018-12-19 17:12:36 +09002615
2616 inputsList := []string{}
Sundong Ahnabb64432019-10-22 13:58:29 +09002617 for _, i := range ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().BuildParamsForTests() {
Jiyong Parkda6eb592018-12-19 17:12:36 +09002618 for _, implicit := range i.Implicits {
2619 inputsList = append(inputsList, implicit.String())
2620 }
2621 }
2622 inputsString := strings.Join(inputsList, " ")
2623
2624 // ensure that the apex includes vendor variants of the direct and indirect deps
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002625 ensureContains(t, inputsString, "android_vendor.29_arm64_armv8-a_shared_apex10000/mylib.so")
2626 ensureContains(t, inputsString, "android_vendor.29_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002627
2628 // ensure that the apex does not include core variants
Colin Crossaede88c2020-08-11 12:17:01 -07002629 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib.so")
2630 ensureNotContains(t, inputsString, "android_arm64_armv8-a_shared_apex10000/mylib2.so")
Jiyong Parkda6eb592018-12-19 17:12:36 +09002631}
Jiyong Park16e91a02018-12-20 18:18:08 +09002632
Jooyung Han85d61762020-06-24 23:50:26 +09002633func TestUseVendorNotAllowedForSystemApexes(t *testing.T) {
Jooyung Handc782442019-11-01 03:14:38 +09002634 testApexError(t, `module "myapex" .*: use_vendor: not allowed`, `
2635 apex {
2636 name: "myapex",
2637 key: "myapex.key",
2638 use_vendor: true,
2639 }
2640 apex_key {
2641 name: "myapex.key",
2642 public_key: "testkey.avbpubkey",
2643 private_key: "testkey.pem",
2644 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002645 `,
2646 setUseVendorAllowListForTest([]string{""}),
2647 )
Colin Cross440e0d02020-06-11 11:32:11 -07002648 // no error with allow list
Jooyung Handc782442019-11-01 03:14:38 +09002649 testApex(t, `
2650 apex {
2651 name: "myapex",
2652 key: "myapex.key",
2653 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002654 updatable: false,
Jooyung Handc782442019-11-01 03:14:38 +09002655 }
2656 apex_key {
2657 name: "myapex.key",
2658 public_key: "testkey.avbpubkey",
2659 private_key: "testkey.pem",
2660 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002661 `,
2662 setUseVendorAllowListForTest([]string{"myapex"}),
2663 )
Jooyung Handc782442019-11-01 03:14:38 +09002664}
2665
Jooyung Han5c998b92019-06-27 11:30:33 +09002666func TestUseVendorFailsIfNotVendorAvailable(t *testing.T) {
2667 testApexError(t, `dependency "mylib" of "myapex" missing variant:\n.*image:vendor`, `
2668 apex {
2669 name: "myapex",
2670 key: "myapex.key",
2671 native_shared_libs: ["mylib"],
2672 use_vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002673 updatable: false,
Jooyung Han5c998b92019-06-27 11:30:33 +09002674 }
2675
2676 apex_key {
2677 name: "myapex.key",
2678 public_key: "testkey.avbpubkey",
2679 private_key: "testkey.pem",
2680 }
2681
2682 cc_library {
2683 name: "mylib",
2684 srcs: ["mylib.cpp"],
2685 system_shared_libs: [],
2686 stl: "none",
2687 }
2688 `)
2689}
2690
Jooyung Han85d61762020-06-24 23:50:26 +09002691func TestVendorApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002692 ctx := testApex(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002693 apex {
2694 name: "myapex",
2695 key: "myapex.key",
2696 binaries: ["mybin"],
2697 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002698 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002699 }
2700 apex_key {
2701 name: "myapex.key",
2702 public_key: "testkey.avbpubkey",
2703 private_key: "testkey.pem",
2704 }
2705 cc_binary {
2706 name: "mybin",
2707 vendor: true,
2708 shared_libs: ["libfoo"],
2709 }
2710 cc_library {
2711 name: "libfoo",
2712 proprietary: true,
2713 }
2714 `)
2715
2716 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2717 "bin/mybin",
2718 "lib64/libfoo.so",
2719 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2720 "lib64/libc++.so",
2721 })
2722
2723 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002724 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002725 name := apexBundle.BaseModuleName()
2726 prefix := "TARGET_"
2727 var builder strings.Builder
2728 data.Custom(&builder, name, prefix, "", data)
Paul Duffin37ba3442021-03-29 00:21:08 +01002729 androidMk := android.StringRelativeToTop(ctx.Config(), builder.String())
2730 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002731 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002732
2733 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2734 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2735 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002736}
2737
Jooyung Handf78e212020-07-22 15:54:47 +09002738func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002739 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002740 apex {
2741 name: "myapex",
2742 key: "myapex.key",
2743 binaries: ["mybin"],
2744 vendor: true,
2745 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002746 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002747 }
2748 apex_key {
2749 name: "myapex.key",
2750 public_key: "testkey.avbpubkey",
2751 private_key: "testkey.pem",
2752 }
2753 cc_binary {
2754 name: "mybin",
2755 vendor: true,
2756 shared_libs: ["libvndk", "libvendor"],
2757 }
2758 cc_library {
2759 name: "libvndk",
2760 vndk: {
2761 enabled: true,
2762 },
2763 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002764 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002765 }
2766 cc_library {
2767 name: "libvendor",
2768 vendor: true,
2769 }
2770 `)
2771
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002772 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002773
Paul Duffina71a67a2021-03-29 00:42:57 +01002774 ldRule := ctx.ModuleForTests("mybin", vendorVariant+"_apex10000").Rule("ld")
Jooyung Handf78e212020-07-22 15:54:47 +09002775 libs := names(ldRule.Args["libFlags"])
2776 // VNDK libs(libvndk/libc++) as they are
Paul Duffin37ba3442021-03-29 00:21:08 +01002777 ensureListContains(t, libs, "out/soong/.intermediates/libvndk/"+vendorVariant+"_shared/libvndk.so")
2778 ensureListContains(t, libs, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"libc++/"+vendorVariant+"_shared/libc++.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002779 // non-stable Vendor libs as APEX variants
Paul Duffin37ba3442021-03-29 00:21:08 +01002780 ensureListContains(t, libs, "out/soong/.intermediates/libvendor/"+vendorVariant+"_shared_apex10000/libvendor.so")
Jooyung Handf78e212020-07-22 15:54:47 +09002781
2782 // VNDK libs are not included when use_vndk_as_stable: true
2783 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2784 "bin/mybin",
2785 "lib64/libvendor.so",
2786 })
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002787
2788 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
2789 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2790 ensureListContains(t, requireNativeLibs, ":vndk")
Jooyung Handf78e212020-07-22 15:54:47 +09002791}
2792
Justin Yun13decfb2021-03-08 19:25:55 +09002793func TestProductVariant(t *testing.T) {
2794 ctx := testApex(t, `
2795 apex {
2796 name: "myapex",
2797 key: "myapex.key",
2798 updatable: false,
2799 product_specific: true,
2800 binaries: ["foo"],
2801 }
2802
2803 apex_key {
2804 name: "myapex.key",
2805 public_key: "testkey.avbpubkey",
2806 private_key: "testkey.pem",
2807 }
2808
2809 cc_binary {
2810 name: "foo",
2811 product_available: true,
2812 apex_available: ["myapex"],
2813 srcs: ["foo.cpp"],
2814 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002815 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2816 variables.ProductVndkVersion = proptools.StringPtr("current")
2817 }),
2818 )
Justin Yun13decfb2021-03-08 19:25:55 +09002819
2820 cflags := strings.Fields(
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002821 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002822 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2823 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2824 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2825 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2826}
2827
Jooyung Han8e5685d2020-09-21 11:02:57 +09002828func TestApex_withPrebuiltFirmware(t *testing.T) {
2829 testCases := []struct {
2830 name string
2831 additionalProp string
2832 }{
2833 {"system apex with prebuilt_firmware", ""},
2834 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2835 }
2836 for _, tc := range testCases {
2837 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002838 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002839 apex {
2840 name: "myapex",
2841 key: "myapex.key",
2842 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002843 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002844 `+tc.additionalProp+`
2845 }
2846 apex_key {
2847 name: "myapex.key",
2848 public_key: "testkey.avbpubkey",
2849 private_key: "testkey.pem",
2850 }
2851 prebuilt_firmware {
2852 name: "myfirmware",
2853 src: "myfirmware.bin",
2854 filename_from_src: true,
2855 `+tc.additionalProp+`
2856 }
2857 `)
2858 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2859 "etc/firmware/myfirmware.bin",
2860 })
2861 })
2862 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002863}
2864
Jooyung Hanefb184e2020-06-25 17:14:25 +09002865func TestAndroidMk_UseVendorRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002866 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002867 apex {
2868 name: "myapex",
2869 key: "myapex.key",
2870 use_vendor: true,
2871 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002872 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002873 }
2874
2875 apex_key {
2876 name: "myapex.key",
2877 public_key: "testkey.avbpubkey",
2878 private_key: "testkey.pem",
2879 }
2880
2881 cc_library {
2882 name: "mylib",
2883 vendor_available: true,
2884 apex_available: ["myapex"],
2885 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002886 `,
2887 setUseVendorAllowListForTest([]string{"myapex"}),
2888 )
Jooyung Hanefb184e2020-06-25 17:14:25 +09002889
2890 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002891 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002892 name := apexBundle.BaseModuleName()
2893 prefix := "TARGET_"
2894 var builder strings.Builder
2895 data.Custom(&builder, name, prefix, "", data)
2896 androidMk := builder.String()
2897 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc libm libdl\n")
2898}
2899
2900func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002901 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002902 apex {
2903 name: "myapex",
2904 key: "myapex.key",
2905 vendor: true,
2906 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002907 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002908 }
2909
2910 apex_key {
2911 name: "myapex.key",
2912 public_key: "testkey.avbpubkey",
2913 private_key: "testkey.pem",
2914 }
2915
2916 cc_library {
2917 name: "mylib",
2918 vendor_available: true,
2919 }
2920 `)
2921
2922 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002923 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002924 name := apexBundle.BaseModuleName()
2925 prefix := "TARGET_"
2926 var builder strings.Builder
2927 data.Custom(&builder, name, prefix, "", data)
2928 androidMk := builder.String()
2929 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += libc.vendor libm.vendor libdl.vendor\n")
2930}
2931
Jooyung Han2ed99d02020-06-24 23:26:26 +09002932func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002933 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09002934 apex {
2935 name: "myapex",
2936 key: "myapex.key",
2937 vintf_fragments: ["fragment.xml"],
2938 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002939 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09002940 }
2941 apex_key {
2942 name: "myapex.key",
2943 public_key: "testkey.avbpubkey",
2944 private_key: "testkey.pem",
2945 }
2946 cc_binary {
2947 name: "mybin",
2948 }
2949 `)
2950
2951 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002952 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09002953 name := apexBundle.BaseModuleName()
2954 prefix := "TARGET_"
2955 var builder strings.Builder
2956 data.Custom(&builder, name, prefix, "", data)
2957 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04002958 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04002959 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09002960}
2961
Jiyong Park16e91a02018-12-20 18:18:08 +09002962func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002963 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09002964 apex {
2965 name: "myapex",
2966 key: "myapex.key",
2967 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002968 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09002969 }
2970
2971 apex_key {
2972 name: "myapex.key",
2973 public_key: "testkey.avbpubkey",
2974 private_key: "testkey.pem",
2975 }
2976
2977 cc_library {
2978 name: "mylib",
2979 srcs: ["mylib.cpp"],
2980 system_shared_libs: [],
2981 stl: "none",
2982 stubs: {
2983 versions: ["1", "2", "3"],
2984 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002985 apex_available: [
2986 "//apex_available:platform",
2987 "myapex",
2988 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09002989 }
2990
2991 cc_binary {
2992 name: "not_in_apex",
2993 srcs: ["mylib.cpp"],
2994 static_libs: ["mylib"],
2995 static_executable: true,
2996 system_shared_libs: [],
2997 stl: "none",
2998 }
Jiyong Park16e91a02018-12-20 18:18:08 +09002999 `)
3000
Colin Cross7113d202019-11-20 16:39:12 -08003001 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003002
3003 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003004 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003005}
Jiyong Park9335a262018-12-24 11:31:58 +09003006
3007func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003008 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003009 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003010 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003011 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003012 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003013 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003014 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003015 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003016 }
3017
3018 cc_library {
3019 name: "mylib",
3020 srcs: ["mylib.cpp"],
3021 system_shared_libs: [],
3022 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003023 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003024 }
3025
3026 apex_key {
3027 name: "myapex.key",
3028 public_key: "testkey.avbpubkey",
3029 private_key: "testkey.pem",
3030 }
3031
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003032 android_app_certificate {
3033 name: "myapex.certificate",
3034 certificate: "testkey",
3035 }
3036
3037 android_app_certificate {
3038 name: "myapex.certificate.override",
3039 certificate: "testkey.override",
3040 }
3041
Jiyong Park9335a262018-12-24 11:31:58 +09003042 `)
3043
3044 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003045 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003046
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003047 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3048 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003049 "vendor/foo/devkeys/testkey.avbpubkey")
3050 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003051 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3052 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003053 "vendor/foo/devkeys/testkey.pem")
3054 }
3055
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003056 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003057 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003058 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003059 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003060 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003061 }
3062}
Jiyong Park58e364a2019-01-19 19:24:06 +09003063
Jooyung Hanf121a652019-12-17 14:30:11 +09003064func TestCertificate(t *testing.T) {
3065 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003066 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003067 apex {
3068 name: "myapex",
3069 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003070 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003071 }
3072 apex_key {
3073 name: "myapex.key",
3074 public_key: "testkey.avbpubkey",
3075 private_key: "testkey.pem",
3076 }`)
3077 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3078 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3079 if actual := rule.Args["certificates"]; actual != expected {
3080 t.Errorf("certificates should be %q, not %q", expected, actual)
3081 }
3082 })
3083 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003084 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003085 apex {
3086 name: "myapex_keytest",
3087 key: "myapex.key",
3088 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003089 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003090 }
3091 apex_key {
3092 name: "myapex.key",
3093 public_key: "testkey.avbpubkey",
3094 private_key: "testkey.pem",
3095 }
3096 android_app_certificate {
3097 name: "myapex.certificate.override",
3098 certificate: "testkey.override",
3099 }`)
3100 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3101 expected := "testkey.override.x509.pem testkey.override.pk8"
3102 if actual := rule.Args["certificates"]; actual != expected {
3103 t.Errorf("certificates should be %q, not %q", expected, actual)
3104 }
3105 })
3106 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003107 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003108 apex {
3109 name: "myapex",
3110 key: "myapex.key",
3111 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003112 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003113 }
3114 apex_key {
3115 name: "myapex.key",
3116 public_key: "testkey.avbpubkey",
3117 private_key: "testkey.pem",
3118 }
3119 android_app_certificate {
3120 name: "myapex.certificate",
3121 certificate: "testkey",
3122 }`)
3123 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3124 expected := "testkey.x509.pem testkey.pk8"
3125 if actual := rule.Args["certificates"]; actual != expected {
3126 t.Errorf("certificates should be %q, not %q", expected, actual)
3127 }
3128 })
3129 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003130 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003131 apex {
3132 name: "myapex_keytest",
3133 key: "myapex.key",
3134 file_contexts: ":myapex-file_contexts",
3135 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003136 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003137 }
3138 apex_key {
3139 name: "myapex.key",
3140 public_key: "testkey.avbpubkey",
3141 private_key: "testkey.pem",
3142 }
3143 android_app_certificate {
3144 name: "myapex.certificate.override",
3145 certificate: "testkey.override",
3146 }`)
3147 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3148 expected := "testkey.override.x509.pem testkey.override.pk8"
3149 if actual := rule.Args["certificates"]; actual != expected {
3150 t.Errorf("certificates should be %q, not %q", expected, actual)
3151 }
3152 })
3153 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003154 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003155 apex {
3156 name: "myapex",
3157 key: "myapex.key",
3158 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003159 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003160 }
3161 apex_key {
3162 name: "myapex.key",
3163 public_key: "testkey.avbpubkey",
3164 private_key: "testkey.pem",
3165 }`)
3166 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3167 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3168 if actual := rule.Args["certificates"]; actual != expected {
3169 t.Errorf("certificates should be %q, not %q", expected, actual)
3170 }
3171 })
3172 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003173 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003174 apex {
3175 name: "myapex_keytest",
3176 key: "myapex.key",
3177 file_contexts: ":myapex-file_contexts",
3178 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003179 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003180 }
3181 apex_key {
3182 name: "myapex.key",
3183 public_key: "testkey.avbpubkey",
3184 private_key: "testkey.pem",
3185 }
3186 android_app_certificate {
3187 name: "myapex.certificate.override",
3188 certificate: "testkey.override",
3189 }`)
3190 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3191 expected := "testkey.override.x509.pem testkey.override.pk8"
3192 if actual := rule.Args["certificates"]; actual != expected {
3193 t.Errorf("certificates should be %q, not %q", expected, actual)
3194 }
3195 })
3196}
3197
Jiyong Park58e364a2019-01-19 19:24:06 +09003198func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003199 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003200 apex {
3201 name: "myapex",
3202 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003203 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003204 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003205 }
3206
3207 apex {
3208 name: "otherapex",
3209 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003210 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003211 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003212 }
3213
3214 apex_key {
3215 name: "myapex.key",
3216 public_key: "testkey.avbpubkey",
3217 private_key: "testkey.pem",
3218 }
3219
3220 cc_library {
3221 name: "mylib",
3222 srcs: ["mylib.cpp"],
3223 system_shared_libs: [],
3224 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003225 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003226 "myapex",
3227 "otherapex",
3228 ],
Jooyung Han24282772020-03-21 23:20:55 +09003229 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003230 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003231 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003232 cc_library {
3233 name: "mylib2",
3234 srcs: ["mylib.cpp"],
3235 system_shared_libs: [],
3236 stl: "none",
3237 apex_available: [
3238 "myapex",
3239 "otherapex",
3240 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003241 static_libs: ["mylib3"],
3242 recovery_available: true,
3243 min_sdk_version: "29",
3244 }
3245 cc_library {
3246 name: "mylib3",
3247 srcs: ["mylib.cpp"],
3248 system_shared_libs: [],
3249 stl: "none",
3250 apex_available: [
3251 "myapex",
3252 "otherapex",
3253 ],
Jooyung Hanc87a0592020-03-02 17:44:33 +09003254 use_apex_name_macro: true,
Colin Crossaede88c2020-08-11 12:17:01 -07003255 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003256 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003257 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003258 `)
3259
Jooyung Hanc87a0592020-03-02 17:44:33 +09003260 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003261 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003262 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003263 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003264
Jooyung Hanccce2f22020-03-07 03:45:53 +09003265 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003266 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003267 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003268 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=10000")
Jooyung Han77988572019-10-18 16:26:16 +09003269 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003270
Jooyung Hanccce2f22020-03-07 03:45:53 +09003271 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX_SDK__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003272 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003273 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003274 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__=29")
Jooyung Han77988572019-10-18 16:26:16 +09003275 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003276
Colin Crossaede88c2020-08-11 12:17:01 -07003277 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3278 // each variant defines additional macros to distinguish which apex variant it is built for
3279
3280 // non-APEX variant does not have __ANDROID_APEX__ defined
3281 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3282 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3283
3284 // APEX variant has __ANDROID_APEX__ defined
3285 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
3286 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3287 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3288 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3289
3290 // APEX variant has __ANDROID_APEX__ defined
3291 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
3292 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3293 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
3294 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
3295
Dan Albertb19953d2020-11-17 15:29:36 -08003296 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003297 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3298 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003299 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Colin Crossaede88c2020-08-11 12:17:01 -07003300
3301 // When a dependency of a cc_library sets use_apex_name_macro: true each apex gets a unique
3302 // variant.
Jooyung Hanc87a0592020-03-02 17:44:33 +09003303
3304 // non-APEX variant does not have __ANDROID_APEX__ defined
3305 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3306 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3307
3308 // APEX variant has __ANDROID_APEX__ defined
3309 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_myapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003310 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003311 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003312 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003313
Jooyung Hanc87a0592020-03-02 17:44:33 +09003314 // APEX variant has __ANDROID_APEX__ defined
3315 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static_otherapex").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003316 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Han77988572019-10-18 16:26:16 +09003317 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MYAPEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003318 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_OTHERAPEX__")
Jooyung Han24282772020-03-21 23:20:55 +09003319
Dan Albertb19953d2020-11-17 15:29:36 -08003320 // recovery variant does not set __ANDROID_APEX_MIN_SDK_VERSION__
Colin Crossaede88c2020-08-11 12:17:01 -07003321 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003322 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Dan Albertb19953d2020-11-17 15:29:36 -08003323 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX_MIN_SDK_VERSION__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003324}
Jiyong Park7e636d02019-01-28 16:16:54 +09003325
3326func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003327 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003328 apex {
3329 name: "myapex",
3330 key: "myapex.key",
3331 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003332 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003333 }
3334
3335 apex_key {
3336 name: "myapex.key",
3337 public_key: "testkey.avbpubkey",
3338 private_key: "testkey.pem",
3339 }
3340
3341 cc_library_headers {
3342 name: "mylib_headers",
3343 export_include_dirs: ["my_include"],
3344 system_shared_libs: [],
3345 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003346 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003347 }
3348
3349 cc_library {
3350 name: "mylib",
3351 srcs: ["mylib.cpp"],
3352 system_shared_libs: [],
3353 stl: "none",
3354 header_libs: ["mylib_headers"],
3355 export_header_lib_headers: ["mylib_headers"],
3356 stubs: {
3357 versions: ["1", "2", "3"],
3358 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003359 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003360 }
3361
3362 cc_library {
3363 name: "otherlib",
3364 srcs: ["mylib.cpp"],
3365 system_shared_libs: [],
3366 stl: "none",
3367 shared_libs: ["mylib"],
3368 }
3369 `)
3370
Colin Cross7113d202019-11-20 16:39:12 -08003371 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003372
3373 // Ensure that the include path of the header lib is exported to 'otherlib'
3374 ensureContains(t, cFlags, "-Imy_include")
3375}
Alex Light9670d332019-01-29 18:07:33 -08003376
Jiyong Park7cd10e32020-01-14 09:22:18 +09003377type fileInApex struct {
3378 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003379 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003380 isLink bool
3381}
3382
Jooyung Hana57af4a2020-01-23 05:36:59 +00003383func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003384 t.Helper()
Jooyung Hana57af4a2020-01-23 05:36:59 +00003385 apexRule := ctx.ModuleForTests(moduleName, variant).Rule("apexRule")
Jooyung Han31c470b2019-10-18 16:26:59 +09003386 copyCmds := apexRule.Args["copy_commands"]
3387 imageApexDir := "/image.apex/"
Jiyong Park7cd10e32020-01-14 09:22:18 +09003388 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003389 for _, cmd := range strings.Split(copyCmds, "&&") {
3390 cmd = strings.TrimSpace(cmd)
3391 if cmd == "" {
3392 continue
3393 }
3394 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003395 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003396 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003397 switch terms[0] {
3398 case "mkdir":
3399 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003400 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003401 t.Fatal("copyCmds contains invalid cp command", cmd)
3402 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003403 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003404 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003405 isLink = false
3406 case "ln":
3407 if len(terms) != 3 && len(terms) != 4 {
3408 // ln LINK TARGET or ln -s LINK TARGET
3409 t.Fatal("copyCmds contains invalid ln command", cmd)
3410 }
3411 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003412 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003413 isLink = true
3414 default:
3415 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3416 }
3417 if dst != "" {
Jooyung Han31c470b2019-10-18 16:26:59 +09003418 index := strings.Index(dst, imageApexDir)
3419 if index == -1 {
3420 t.Fatal("copyCmds should copy a file to image.apex/", cmd)
3421 }
3422 dstFile := dst[index+len(imageApexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003423 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003424 }
3425 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003426 return ret
3427}
3428
Jooyung Hana57af4a2020-01-23 05:36:59 +00003429func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3430 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003431 var failed bool
3432 var surplus []string
3433 filesMatched := make(map[string]bool)
Jooyung Hana57af4a2020-01-23 05:36:59 +00003434 for _, file := range getFiles(t, ctx, moduleName, variant) {
Jooyung Hane6436d72020-02-27 13:31:56 +09003435 mactchFound := false
Jiyong Park7cd10e32020-01-14 09:22:18 +09003436 for _, expected := range files {
3437 if matched, _ := path.Match(expected, file.path); matched {
3438 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003439 mactchFound = true
3440 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003441 }
3442 }
Jooyung Hane6436d72020-02-27 13:31:56 +09003443 if !mactchFound {
3444 surplus = append(surplus, file.path)
3445 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003446 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003447
Jooyung Han31c470b2019-10-18 16:26:59 +09003448 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003449 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003450 t.Log("surplus files", surplus)
3451 failed = true
3452 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003453
3454 if len(files) > len(filesMatched) {
3455 var missing []string
3456 for _, expected := range files {
3457 if !filesMatched[expected] {
3458 missing = append(missing, expected)
3459 }
3460 }
3461 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003462 t.Log("missing files", missing)
3463 failed = true
3464 }
3465 if failed {
3466 t.Fail()
3467 }
3468}
3469
Jooyung Han344d5432019-08-23 11:17:39 +09003470func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003471 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003472 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003473 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003474 "etc/llndk.libraries.29.txt",
3475 "etc/vndkcore.libraries.29.txt",
3476 "etc/vndksp.libraries.29.txt",
3477 "etc/vndkprivate.libraries.29.txt",
3478 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003479 }
3480 testCases := []struct {
3481 vndkVersion string
3482 expectedFiles []string
3483 }{
3484 {
3485 vndkVersion: "current",
3486 expectedFiles: append(commonFiles,
3487 "lib/libvndk.so",
3488 "lib/libvndksp.so",
3489 "lib64/libvndk.so",
3490 "lib64/libvndksp.so"),
3491 },
3492 {
3493 vndkVersion: "",
3494 expectedFiles: append(commonFiles,
3495 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3496 "lib/libvndksp.so",
3497 "lib64/libvndksp.so"),
3498 },
3499 }
3500 for _, tc := range testCases {
3501 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3502 ctx := testApex(t, `
3503 apex_vndk {
3504 name: "com.android.vndk.current",
3505 key: "com.android.vndk.current.key",
3506 updatable: false,
3507 }
3508
3509 apex_key {
3510 name: "com.android.vndk.current.key",
3511 public_key: "testkey.avbpubkey",
3512 private_key: "testkey.pem",
3513 }
3514
3515 cc_library {
3516 name: "libvndk",
3517 srcs: ["mylib.cpp"],
3518 vendor_available: true,
3519 product_available: true,
3520 vndk: {
3521 enabled: true,
3522 },
3523 system_shared_libs: [],
3524 stl: "none",
3525 apex_available: [ "com.android.vndk.current" ],
3526 }
3527
3528 cc_library {
3529 name: "libvndksp",
3530 srcs: ["mylib.cpp"],
3531 vendor_available: true,
3532 product_available: true,
3533 vndk: {
3534 enabled: true,
3535 support_system_process: true,
3536 },
3537 system_shared_libs: [],
3538 stl: "none",
3539 apex_available: [ "com.android.vndk.current" ],
3540 }
3541
3542 // VNDK-Ext should not cause any problems
3543
3544 cc_library {
3545 name: "libvndk.ext",
3546 srcs: ["mylib2.cpp"],
3547 vendor: true,
3548 vndk: {
3549 enabled: true,
3550 extends: "libvndk",
3551 },
3552 system_shared_libs: [],
3553 stl: "none",
3554 }
3555
3556 cc_library {
3557 name: "libvndksp.ext",
3558 srcs: ["mylib2.cpp"],
3559 vendor: true,
3560 vndk: {
3561 enabled: true,
3562 support_system_process: true,
3563 extends: "libvndksp",
3564 },
3565 system_shared_libs: [],
3566 stl: "none",
3567 }
3568 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3569 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3570 }))
3571 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3572 })
3573 }
Jooyung Han344d5432019-08-23 11:17:39 +09003574}
3575
3576func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003577 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003578 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003579 name: "com.android.vndk.current",
3580 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003581 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003582 }
3583
3584 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003585 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003586 public_key: "testkey.avbpubkey",
3587 private_key: "testkey.pem",
3588 }
3589
3590 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003591 name: "libvndk",
3592 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003593 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003594 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003595 vndk: {
3596 enabled: true,
3597 },
3598 system_shared_libs: [],
3599 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003600 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003601 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003602
3603 cc_prebuilt_library_shared {
3604 name: "libvndk.arm",
3605 srcs: ["libvndk.arm.so"],
3606 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003607 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003608 vndk: {
3609 enabled: true,
3610 },
3611 enabled: false,
3612 arch: {
3613 arm: {
3614 enabled: true,
3615 },
3616 },
3617 system_shared_libs: [],
3618 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003619 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003620 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003621 `+vndkLibrariesTxtFiles("current"),
3622 withFiles(map[string][]byte{
3623 "libvndk.so": nil,
3624 "libvndk.arm.so": nil,
3625 }))
Colin Cross2807f002021-03-02 10:15:29 -08003626 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003627 "lib/libvndk.so",
3628 "lib/libvndk.arm.so",
3629 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003630 "lib/libc++.so",
3631 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003632 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003633 })
Jooyung Han344d5432019-08-23 11:17:39 +09003634}
3635
Jooyung Han39edb6c2019-11-06 16:53:07 +09003636func vndkLibrariesTxtFiles(vers ...string) (result string) {
3637 for _, v := range vers {
3638 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003639 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003640 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003641 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003642 name: "` + txt + `.libraries.txt",
3643 }
3644 `
3645 }
3646 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003647 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003648 result += `
3649 prebuilt_etc {
3650 name: "` + txt + `.libraries.` + v + `.txt",
3651 src: "dummy.txt",
3652 }
3653 `
3654 }
3655 }
3656 }
3657 return
3658}
3659
Jooyung Han344d5432019-08-23 11:17:39 +09003660func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003661 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003662 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003663 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003664 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003665 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003666 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003667 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003668 }
3669
3670 apex_key {
3671 name: "myapex.key",
3672 public_key: "testkey.avbpubkey",
3673 private_key: "testkey.pem",
3674 }
3675
Jooyung Han31c470b2019-10-18 16:26:59 +09003676 vndk_prebuilt_shared {
3677 name: "libvndk27",
3678 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003679 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003680 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003681 vndk: {
3682 enabled: true,
3683 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003684 target_arch: "arm64",
3685 arch: {
3686 arm: {
3687 srcs: ["libvndk27_arm.so"],
3688 },
3689 arm64: {
3690 srcs: ["libvndk27_arm64.so"],
3691 },
3692 },
Colin Cross2807f002021-03-02 10:15:29 -08003693 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003694 }
3695
3696 vndk_prebuilt_shared {
3697 name: "libvndk27",
3698 version: "27",
3699 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003700 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003701 vndk: {
3702 enabled: true,
3703 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003704 target_arch: "x86_64",
3705 arch: {
3706 x86: {
3707 srcs: ["libvndk27_x86.so"],
3708 },
3709 x86_64: {
3710 srcs: ["libvndk27_x86_64.so"],
3711 },
3712 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003713 }
3714 `+vndkLibrariesTxtFiles("27"),
3715 withFiles(map[string][]byte{
3716 "libvndk27_arm.so": nil,
3717 "libvndk27_arm64.so": nil,
3718 "libvndk27_x86.so": nil,
3719 "libvndk27_x86_64.so": nil,
3720 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003721
Colin Cross2807f002021-03-02 10:15:29 -08003722 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003723 "lib/libvndk27_arm.so",
3724 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003725 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003726 })
Jooyung Han344d5432019-08-23 11:17:39 +09003727}
3728
Jooyung Han90eee022019-10-01 20:02:42 +09003729func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003730 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003731 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003732 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003733 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003734 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003735 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003736 }
3737 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003738 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003739 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003740 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003741 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003742 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003743 }
3744 apex_key {
3745 name: "myapex.key",
3746 public_key: "testkey.avbpubkey",
3747 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003748 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003749
3750 assertApexName := func(expected, moduleName string) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003751 bundle := ctx.ModuleForTests(moduleName, "android_common_image").Module().(*apexBundle)
Jooyung Han90eee022019-10-01 20:02:42 +09003752 actual := proptools.String(bundle.properties.Apex_name)
3753 if !reflect.DeepEqual(actual, expected) {
3754 t.Errorf("Got '%v', expected '%v'", actual, expected)
3755 }
3756 }
3757
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003758 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003759 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003760}
3761
Jooyung Han344d5432019-08-23 11:17:39 +09003762func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003763 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003764 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003765 name: "com.android.vndk.current",
3766 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003767 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003768 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003769 }
3770
3771 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003772 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003773 public_key: "testkey.avbpubkey",
3774 private_key: "testkey.pem",
3775 }
3776
3777 cc_library {
3778 name: "libvndk",
3779 srcs: ["mylib.cpp"],
3780 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003781 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003782 native_bridge_supported: true,
3783 host_supported: true,
3784 vndk: {
3785 enabled: true,
3786 },
3787 system_shared_libs: [],
3788 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003789 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003790 }
Colin Cross2807f002021-03-02 10:15:29 -08003791 `+vndkLibrariesTxtFiles("current"),
3792 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003793
Colin Cross2807f002021-03-02 10:15:29 -08003794 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003795 "lib/libvndk.so",
3796 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003797 "lib/libc++.so",
3798 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003799 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003800 })
Jooyung Han344d5432019-08-23 11:17:39 +09003801}
3802
3803func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003804 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003805 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003806 name: "com.android.vndk.current",
3807 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003808 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003809 native_bridge_supported: true,
3810 }
3811
3812 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003813 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003814 public_key: "testkey.avbpubkey",
3815 private_key: "testkey.pem",
3816 }
3817
3818 cc_library {
3819 name: "libvndk",
3820 srcs: ["mylib.cpp"],
3821 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003822 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003823 native_bridge_supported: true,
3824 host_supported: true,
3825 vndk: {
3826 enabled: true,
3827 },
3828 system_shared_libs: [],
3829 stl: "none",
3830 }
3831 `)
3832}
3833
Jooyung Han31c470b2019-10-18 16:26:59 +09003834func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003835 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003836 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003837 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003838 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003839 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003840 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003841 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003842 }
3843
3844 apex_key {
3845 name: "myapex.key",
3846 public_key: "testkey.avbpubkey",
3847 private_key: "testkey.pem",
3848 }
3849
3850 vndk_prebuilt_shared {
3851 name: "libvndk27",
3852 version: "27",
3853 target_arch: "arm",
3854 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003855 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003856 vndk: {
3857 enabled: true,
3858 },
3859 arch: {
3860 arm: {
3861 srcs: ["libvndk27.so"],
3862 }
3863 },
3864 }
3865
3866 vndk_prebuilt_shared {
3867 name: "libvndk27",
3868 version: "27",
3869 target_arch: "arm",
3870 binder32bit: true,
3871 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003872 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003873 vndk: {
3874 enabled: true,
3875 },
3876 arch: {
3877 arm: {
3878 srcs: ["libvndk27binder32.so"],
3879 }
3880 },
Colin Cross2807f002021-03-02 10:15:29 -08003881 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003882 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003883 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003884 withFiles(map[string][]byte{
3885 "libvndk27.so": nil,
3886 "libvndk27binder32.so": nil,
3887 }),
3888 withBinder32bit,
3889 withTargets(map[android.OsType][]android.Target{
3890 android.Android: []android.Target{
Jooyung Han35155c42020-02-06 17:33:20 +09003891 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3892 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003893 },
3894 }),
3895 )
3896
Colin Cross2807f002021-03-02 10:15:29 -08003897 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003898 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003899 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003900 })
3901}
3902
Jooyung Han45a96772020-06-15 14:59:42 +09003903func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003904 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003905 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003906 name: "com.android.vndk.current",
3907 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003908 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003909 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003910 }
3911
3912 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003913 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003914 public_key: "testkey.avbpubkey",
3915 private_key: "testkey.pem",
3916 }
3917
3918 cc_library {
3919 name: "libz",
3920 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003921 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09003922 vndk: {
3923 enabled: true,
3924 },
3925 stubs: {
3926 symbol_file: "libz.map.txt",
3927 versions: ["30"],
3928 }
3929 }
3930 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
3931 "libz.map.txt": nil,
3932 }))
3933
Colin Cross2807f002021-03-02 10:15:29 -08003934 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09003935 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
3936 ensureListEmpty(t, provideNativeLibs)
3937}
3938
Jooyung Hane1633032019-08-01 17:41:43 +09003939func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003940 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003941 apex {
3942 name: "myapex_nodep",
3943 key: "myapex.key",
3944 native_shared_libs: ["lib_nodep"],
3945 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003946 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003947 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003948 }
3949
3950 apex {
3951 name: "myapex_dep",
3952 key: "myapex.key",
3953 native_shared_libs: ["lib_dep"],
3954 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003955 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003956 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003957 }
3958
3959 apex {
3960 name: "myapex_provider",
3961 key: "myapex.key",
3962 native_shared_libs: ["libfoo"],
3963 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003964 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003965 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003966 }
3967
3968 apex {
3969 name: "myapex_selfcontained",
3970 key: "myapex.key",
3971 native_shared_libs: ["lib_dep", "libfoo"],
3972 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003973 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003974 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003975 }
3976
3977 apex_key {
3978 name: "myapex.key",
3979 public_key: "testkey.avbpubkey",
3980 private_key: "testkey.pem",
3981 }
3982
3983 cc_library {
3984 name: "lib_nodep",
3985 srcs: ["mylib.cpp"],
3986 system_shared_libs: [],
3987 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003988 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003989 }
3990
3991 cc_library {
3992 name: "lib_dep",
3993 srcs: ["mylib.cpp"],
3994 shared_libs: ["libfoo"],
3995 system_shared_libs: [],
3996 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003997 apex_available: [
3998 "myapex_dep",
3999 "myapex_provider",
4000 "myapex_selfcontained",
4001 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004002 }
4003
4004 cc_library {
4005 name: "libfoo",
4006 srcs: ["mytest.cpp"],
4007 stubs: {
4008 versions: ["1"],
4009 },
4010 system_shared_libs: [],
4011 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004012 apex_available: [
4013 "myapex_provider",
4014 "myapex_selfcontained",
4015 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004016 }
4017 `)
4018
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004019 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004020 var provideNativeLibs, requireNativeLibs []string
4021
Sundong Ahnabb64432019-10-22 13:58:29 +09004022 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004023 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4024 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004025 ensureListEmpty(t, provideNativeLibs)
4026 ensureListEmpty(t, requireNativeLibs)
4027
Sundong Ahnabb64432019-10-22 13:58:29 +09004028 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004029 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4030 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004031 ensureListEmpty(t, provideNativeLibs)
4032 ensureListContains(t, requireNativeLibs, "libfoo.so")
4033
Sundong Ahnabb64432019-10-22 13:58:29 +09004034 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004035 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4036 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004037 ensureListContains(t, provideNativeLibs, "libfoo.so")
4038 ensureListEmpty(t, requireNativeLibs)
4039
Sundong Ahnabb64432019-10-22 13:58:29 +09004040 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004041 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4042 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004043 ensureListContains(t, provideNativeLibs, "libfoo.so")
4044 ensureListEmpty(t, requireNativeLibs)
4045}
4046
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004047func TestApexName(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004048 ctx := testApex(t, `
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004049 apex {
4050 name: "myapex",
4051 key: "myapex.key",
4052 apex_name: "com.android.myapex",
Jiyong Parkdb334862020-02-05 17:19:28 +09004053 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004054 updatable: false,
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004055 }
4056
4057 apex_key {
4058 name: "myapex.key",
4059 public_key: "testkey.avbpubkey",
4060 private_key: "testkey.pem",
4061 }
Jiyong Parkdb334862020-02-05 17:19:28 +09004062
4063 cc_library {
4064 name: "mylib",
4065 srcs: ["mylib.cpp"],
4066 system_shared_libs: [],
4067 stl: "none",
4068 apex_available: [
4069 "//apex_available:platform",
4070 "myapex",
4071 ],
4072 }
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004073 `)
4074
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004075 module := ctx.ModuleForTests("myapex", "android_common_com.android.myapex_image")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004076 apexManifestRule := module.Rule("apexManifestRule")
4077 ensureContains(t, apexManifestRule.Args["opt"], "-v name com.android.myapex")
4078 apexRule := module.Rule("apexRule")
4079 ensureContains(t, apexRule.Args["opt_flags"], "--do_not_check_keyname")
Jiyong Parkdb334862020-02-05 17:19:28 +09004080
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004081 apexBundle := module.Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07004082 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Parkdb334862020-02-05 17:19:28 +09004083 name := apexBundle.BaseModuleName()
4084 prefix := "TARGET_"
4085 var builder strings.Builder
4086 data.Custom(&builder, name, prefix, "", data)
4087 androidMk := builder.String()
4088 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
4089 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004090}
4091
Alex Light0851b882019-02-07 13:20:53 -08004092func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004093 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004094 apex {
4095 name: "myapex",
4096 key: "myapex.key",
4097 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004098 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004099 }
4100
4101 apex_key {
4102 name: "myapex.key",
4103 public_key: "testkey.avbpubkey",
4104 private_key: "testkey.pem",
4105 }
4106
4107 cc_library {
4108 name: "mylib_common",
4109 srcs: ["mylib.cpp"],
4110 system_shared_libs: [],
4111 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004112 apex_available: [
4113 "//apex_available:platform",
4114 "myapex",
4115 ],
Alex Light0851b882019-02-07 13:20:53 -08004116 }
4117 `)
4118
Sundong Ahnabb64432019-10-22 13:58:29 +09004119 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004120 apexRule := module.Rule("apexRule")
4121 copyCmds := apexRule.Args["copy_commands"]
4122
4123 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4124 t.Log("Apex was a test apex!")
4125 t.Fail()
4126 }
4127 // Ensure that main rule creates an output
4128 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4129
4130 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004131 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004132
4133 // Ensure that both direct and indirect deps are copied into apex
4134 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4135
Colin Cross7113d202019-11-20 16:39:12 -08004136 // Ensure that the platform variant ends with _shared
4137 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004138
Colin Cross56a83212020-09-15 18:30:11 -07004139 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004140 t.Log("Found mylib_common not in any apex!")
4141 t.Fail()
4142 }
4143}
4144
4145func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004146 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004147 apex_test {
4148 name: "myapex",
4149 key: "myapex.key",
4150 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004151 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004152 }
4153
4154 apex_key {
4155 name: "myapex.key",
4156 public_key: "testkey.avbpubkey",
4157 private_key: "testkey.pem",
4158 }
4159
4160 cc_library {
4161 name: "mylib_common_test",
4162 srcs: ["mylib.cpp"],
4163 system_shared_libs: [],
4164 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004165 // TODO: remove //apex_available:platform
4166 apex_available: [
4167 "//apex_available:platform",
4168 "myapex",
4169 ],
Alex Light0851b882019-02-07 13:20:53 -08004170 }
4171 `)
4172
Sundong Ahnabb64432019-10-22 13:58:29 +09004173 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004174 apexRule := module.Rule("apexRule")
4175 copyCmds := apexRule.Args["copy_commands"]
4176
4177 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4178 t.Log("Apex was not a test apex!")
4179 t.Fail()
4180 }
4181 // Ensure that main rule creates an output
4182 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4183
4184 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004185 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004186
4187 // Ensure that both direct and indirect deps are copied into apex
4188 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4189
Colin Cross7113d202019-11-20 16:39:12 -08004190 // Ensure that the platform variant ends with _shared
4191 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004192}
4193
Alex Light9670d332019-01-29 18:07:33 -08004194func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004195 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004196 apex {
4197 name: "myapex",
4198 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004199 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004200 multilib: {
4201 first: {
4202 native_shared_libs: ["mylib_common"],
4203 }
4204 },
4205 target: {
4206 android: {
4207 multilib: {
4208 first: {
4209 native_shared_libs: ["mylib"],
4210 }
4211 }
4212 },
4213 host: {
4214 multilib: {
4215 first: {
4216 native_shared_libs: ["mylib2"],
4217 }
4218 }
4219 }
4220 }
4221 }
4222
4223 apex_key {
4224 name: "myapex.key",
4225 public_key: "testkey.avbpubkey",
4226 private_key: "testkey.pem",
4227 }
4228
4229 cc_library {
4230 name: "mylib",
4231 srcs: ["mylib.cpp"],
4232 system_shared_libs: [],
4233 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004234 // TODO: remove //apex_available:platform
4235 apex_available: [
4236 "//apex_available:platform",
4237 "myapex",
4238 ],
Alex Light9670d332019-01-29 18:07:33 -08004239 }
4240
4241 cc_library {
4242 name: "mylib_common",
4243 srcs: ["mylib.cpp"],
4244 system_shared_libs: [],
4245 stl: "none",
4246 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004247 // TODO: remove //apex_available:platform
4248 apex_available: [
4249 "//apex_available:platform",
4250 "myapex",
4251 ],
Alex Light9670d332019-01-29 18:07:33 -08004252 }
4253
4254 cc_library {
4255 name: "mylib2",
4256 srcs: ["mylib.cpp"],
4257 system_shared_libs: [],
4258 stl: "none",
4259 compile_multilib: "first",
4260 }
4261 `)
4262
Sundong Ahnabb64432019-10-22 13:58:29 +09004263 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004264 copyCmds := apexRule.Args["copy_commands"]
4265
4266 // Ensure that main rule creates an output
4267 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4268
4269 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004270 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4271 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4272 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004273
4274 // Ensure that both direct and indirect deps are copied into apex
4275 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4276 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4277 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4278
Colin Cross7113d202019-11-20 16:39:12 -08004279 // Ensure that the platform variant ends with _shared
4280 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4281 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4282 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004283}
Jiyong Park04480cf2019-02-06 00:16:29 +09004284
Jiyong Park59140302020-12-14 18:44:04 +09004285func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004286 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004287 apex {
4288 name: "myapex",
4289 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004290 updatable: false,
Jiyong Park59140302020-12-14 18:44:04 +09004291 arch: {
4292 arm64: {
4293 native_shared_libs: ["mylib.arm64"],
4294 },
4295 x86_64: {
4296 native_shared_libs: ["mylib.x64"],
4297 },
4298 }
4299 }
4300
4301 apex_key {
4302 name: "myapex.key",
4303 public_key: "testkey.avbpubkey",
4304 private_key: "testkey.pem",
4305 }
4306
4307 cc_library {
4308 name: "mylib.arm64",
4309 srcs: ["mylib.cpp"],
4310 system_shared_libs: [],
4311 stl: "none",
4312 // TODO: remove //apex_available:platform
4313 apex_available: [
4314 "//apex_available:platform",
4315 "myapex",
4316 ],
4317 }
4318
4319 cc_library {
4320 name: "mylib.x64",
4321 srcs: ["mylib.cpp"],
4322 system_shared_libs: [],
4323 stl: "none",
4324 // TODO: remove //apex_available:platform
4325 apex_available: [
4326 "//apex_available:platform",
4327 "myapex",
4328 ],
4329 }
4330 `)
4331
4332 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4333 copyCmds := apexRule.Args["copy_commands"]
4334
4335 // Ensure that apex variant is created for the direct dep
4336 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
4337 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4338
4339 // Ensure that both direct and indirect deps are copied into apex
4340 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4341 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4342}
4343
Jiyong Park04480cf2019-02-06 00:16:29 +09004344func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004345 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004346 apex {
4347 name: "myapex",
4348 key: "myapex.key",
4349 binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004350 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004351 }
4352
4353 apex_key {
4354 name: "myapex.key",
4355 public_key: "testkey.avbpubkey",
4356 private_key: "testkey.pem",
4357 }
4358
4359 sh_binary {
4360 name: "myscript",
4361 src: "mylib.cpp",
4362 filename: "myscript.sh",
4363 sub_dir: "script",
4364 }
4365 `)
4366
Sundong Ahnabb64432019-10-22 13:58:29 +09004367 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004368 copyCmds := apexRule.Args["copy_commands"]
4369
4370 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4371}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004372
Jooyung Han91df2082019-11-20 01:49:42 +09004373func TestApexInVariousPartition(t *testing.T) {
4374 testcases := []struct {
4375 propName, parition, flattenedPartition string
4376 }{
4377 {"", "system", "system_ext"},
4378 {"product_specific: true", "product", "product"},
4379 {"soc_specific: true", "vendor", "vendor"},
4380 {"proprietary: true", "vendor", "vendor"},
4381 {"vendor: true", "vendor", "vendor"},
4382 {"system_ext_specific: true", "system_ext", "system_ext"},
4383 }
4384 for _, tc := range testcases {
4385 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004386 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004387 apex {
4388 name: "myapex",
4389 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004390 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004391 `+tc.propName+`
4392 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004393
Jooyung Han91df2082019-11-20 01:49:42 +09004394 apex_key {
4395 name: "myapex.key",
4396 public_key: "testkey.avbpubkey",
4397 private_key: "testkey.pem",
4398 }
4399 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004400
Jooyung Han91df2082019-11-20 01:49:42 +09004401 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004402 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4403 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004404 if actual != expected {
4405 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4406 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004407
Jooyung Han91df2082019-11-20 01:49:42 +09004408 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004409 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4410 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004411 if actual != expected {
4412 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4413 }
4414 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004415 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004416}
Jiyong Park67882562019-03-21 01:11:21 +09004417
Jooyung Han580eb4f2020-06-24 19:33:06 +09004418func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004419 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004420 apex {
4421 name: "myapex",
4422 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004423 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004424 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004425
Jooyung Han580eb4f2020-06-24 19:33:06 +09004426 apex_key {
4427 name: "myapex.key",
4428 public_key: "testkey.avbpubkey",
4429 private_key: "testkey.pem",
4430 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004431 `)
4432 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004433 rule := module.Output("file_contexts")
4434 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4435}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004436
Jooyung Han580eb4f2020-06-24 19:33:06 +09004437func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004438 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004439 apex {
4440 name: "myapex",
4441 key: "myapex.key",
4442 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004443 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004444 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004445
Jooyung Han580eb4f2020-06-24 19:33:06 +09004446 apex_key {
4447 name: "myapex.key",
4448 public_key: "testkey.avbpubkey",
4449 private_key: "testkey.pem",
4450 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004451 `, withFiles(map[string][]byte{
4452 "my_own_file_contexts": nil,
4453 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004454}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004455
Jooyung Han580eb4f2020-06-24 19:33:06 +09004456func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004457 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004458 apex {
4459 name: "myapex",
4460 key: "myapex.key",
4461 product_specific: true,
4462 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004463 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004464 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004465
Jooyung Han580eb4f2020-06-24 19:33:06 +09004466 apex_key {
4467 name: "myapex.key",
4468 public_key: "testkey.avbpubkey",
4469 private_key: "testkey.pem",
4470 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004471 `)
4472
Colin Cross1c460562021-02-16 17:55:47 -08004473 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004474 apex {
4475 name: "myapex",
4476 key: "myapex.key",
4477 product_specific: true,
4478 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004479 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004480 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004481
Jooyung Han580eb4f2020-06-24 19:33:06 +09004482 apex_key {
4483 name: "myapex.key",
4484 public_key: "testkey.avbpubkey",
4485 private_key: "testkey.pem",
4486 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004487 `, withFiles(map[string][]byte{
4488 "product_specific_file_contexts": nil,
4489 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004490 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4491 rule := module.Output("file_contexts")
4492 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4493}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004494
Jooyung Han580eb4f2020-06-24 19:33:06 +09004495func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004496 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004497 apex {
4498 name: "myapex",
4499 key: "myapex.key",
4500 product_specific: true,
4501 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004502 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004503 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004504
Jooyung Han580eb4f2020-06-24 19:33:06 +09004505 apex_key {
4506 name: "myapex.key",
4507 public_key: "testkey.avbpubkey",
4508 private_key: "testkey.pem",
4509 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004510
Jooyung Han580eb4f2020-06-24 19:33:06 +09004511 filegroup {
4512 name: "my-file-contexts",
4513 srcs: ["product_specific_file_contexts"],
4514 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004515 `, withFiles(map[string][]byte{
4516 "product_specific_file_contexts": nil,
4517 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004518 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4519 rule := module.Output("file_contexts")
4520 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004521}
4522
Jiyong Park67882562019-03-21 01:11:21 +09004523func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004524 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004525 apex_key {
4526 name: "myapex.key",
4527 public_key: ":my.avbpubkey",
4528 private_key: ":my.pem",
4529 product_specific: true,
4530 }
4531
4532 filegroup {
4533 name: "my.avbpubkey",
4534 srcs: ["testkey2.avbpubkey"],
4535 }
4536
4537 filegroup {
4538 name: "my.pem",
4539 srcs: ["testkey2.pem"],
4540 }
4541 `)
4542
4543 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4544 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004545 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004546 if actual_pubkey != expected_pubkey {
4547 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4548 }
4549 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004550 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004551 if actual_privkey != expected_privkey {
4552 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4553 }
4554}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004555
4556func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004557 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004558 prebuilt_apex {
4559 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004560 arch: {
4561 arm64: {
4562 src: "myapex-arm64.apex",
4563 },
4564 arm: {
4565 src: "myapex-arm.apex",
4566 },
4567 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004568 }
4569 `)
4570
Paul Duffin6717d882021-06-15 19:09:41 +01004571 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004572
Jiyong Parkc95714e2019-03-29 14:23:10 +09004573 expectedInput := "myapex-arm64.apex"
4574 if prebuilt.inputApex.String() != expectedInput {
4575 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4576 }
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004577}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004578
Paul Duffinc0609c62021-03-01 17:27:16 +00004579func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004580 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004581 prebuilt_apex {
4582 name: "myapex",
4583 }
4584 `)
4585}
4586
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004587func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004588 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004589 prebuilt_apex {
4590 name: "myapex",
4591 src: "myapex-arm.apex",
4592 filename: "notmyapex.apex",
4593 }
4594 `)
4595
Paul Duffin6717d882021-06-15 19:09:41 +01004596 p := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004597
4598 expected := "notmyapex.apex"
4599 if p.installFilename != expected {
4600 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4601 }
4602}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004603
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004604func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004605 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004606 prebuilt_apex {
4607 name: "myapex.prebuilt",
4608 src: "myapex-arm.apex",
4609 overrides: [
4610 "myapex",
4611 ],
4612 }
4613 `)
4614
Paul Duffin6717d882021-06-15 19:09:41 +01004615 p := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt").Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004616
4617 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004618 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004619 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004620 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004621 }
4622}
4623
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004624func TestPrebuiltApexName(t *testing.T) {
4625 testApex(t, `
4626 prebuilt_apex {
4627 name: "com.company.android.myapex",
4628 apex_name: "com.android.myapex",
4629 src: "company-myapex-arm.apex",
4630 }
4631 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4632
4633 testApex(t, `
4634 apex_set {
4635 name: "com.company.android.myapex",
4636 apex_name: "com.android.myapex",
4637 set: "company-myapex.apks",
4638 }
4639 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4640}
4641
4642func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4643 _ = android.GroupFixturePreparers(
4644 java.PrepareForTestWithJavaDefaultModules,
4645 PrepareForTestWithApexBuildComponents,
4646 android.FixtureWithRootAndroidBp(`
4647 platform_bootclasspath {
4648 name: "platform-bootclasspath",
4649 fragments: [
4650 {
4651 apex: "com.android.art",
4652 module: "art-bootclasspath-fragment",
4653 },
4654 ],
4655 }
4656
4657 prebuilt_apex {
4658 name: "com.company.android.art",
4659 apex_name: "com.android.art",
4660 src: "com.company.android.art-arm.apex",
4661 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4662 }
4663
4664 prebuilt_bootclasspath_fragment {
4665 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004666 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004667 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004668 hidden_api: {
4669 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4670 metadata: "my-bootclasspath-fragment/metadata.csv",
4671 index: "my-bootclasspath-fragment/index.csv",
4672 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4673 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4674 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004675 }
4676
4677 java_import {
4678 name: "core-oj",
4679 jars: ["prebuilt.jar"],
4680 }
4681 `),
4682 ).RunTest(t)
4683}
4684
Paul Duffin092153d2021-01-26 11:42:39 +00004685// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4686// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004687func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004688 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004689
Paul Duffin89886cb2021-02-05 16:44:03 +00004690 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004691 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004692 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Paul Duffin064b70c2020-11-02 17:32:38 +00004693 dexJarBuildPath := p.DexJarBuildPath()
Paul Duffin39853512021-02-26 11:09:39 +00004694 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004695 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4696 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4697 android.NormalizePathForTesting(dexJarBuildPath))
4698 }
4699
4700 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
4701 // Make sure the import has been given the correct path to the dex jar.
4702 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4703 dexJarBuildPath := p.DexJarInstallPath()
4704 stem := android.RemoveOptionalPrebuiltPrefix(name)
4705 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4706 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4707 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004708 }
4709
Paul Duffin39853512021-02-26 11:09:39 +00004710 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Paul Duffin064b70c2020-11-02 17:32:38 +00004711 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004712 android.AssertArrayString(t, "Check if there is no source variant",
4713 []string{"android_common"},
4714 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004715 }
4716
4717 t.Run("prebuilt only", func(t *testing.T) {
4718 bp := `
4719 prebuilt_apex {
4720 name: "myapex",
4721 arch: {
4722 arm64: {
4723 src: "myapex-arm64.apex",
4724 },
4725 arm: {
4726 src: "myapex-arm.apex",
4727 },
4728 },
Paul Duffin39853512021-02-26 11:09:39 +00004729 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004730 }
4731
4732 java_import {
4733 name: "libfoo",
4734 jars: ["libfoo.jar"],
4735 }
Paul Duffin39853512021-02-26 11:09:39 +00004736
4737 java_sdk_library_import {
4738 name: "libbar",
4739 public: {
4740 jars: ["libbar.jar"],
4741 },
4742 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004743 `
4744
4745 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4746 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4747
Paul Duffinf6932af2021-02-26 18:21:56 +00004748 // Make sure that the deapexer has the correct input APEX.
4749 deapexer := ctx.ModuleForTests("myapex.deapexer", "android_common")
4750 rule := deapexer.Rule("deapexer")
4751 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4752 t.Errorf("expected: %q, found: %q", expected, actual)
4753 }
4754
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004755 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004756 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004757 rule = prebuiltApex.Rule("android/soong/android.Cp")
4758 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4759 t.Errorf("expected: %q, found: %q", expected, actual)
4760 }
4761
Paul Duffin89886cb2021-02-05 16:44:03 +00004762 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004763 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004764
4765 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004766 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004767 })
4768
4769 t.Run("prebuilt with source preferred", func(t *testing.T) {
4770
4771 bp := `
4772 prebuilt_apex {
4773 name: "myapex",
4774 arch: {
4775 arm64: {
4776 src: "myapex-arm64.apex",
4777 },
4778 arm: {
4779 src: "myapex-arm.apex",
4780 },
4781 },
Paul Duffin39853512021-02-26 11:09:39 +00004782 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004783 }
4784
4785 java_import {
4786 name: "libfoo",
4787 jars: ["libfoo.jar"],
4788 }
4789
4790 java_library {
4791 name: "libfoo",
4792 }
Paul Duffin39853512021-02-26 11:09:39 +00004793
4794 java_sdk_library_import {
4795 name: "libbar",
4796 public: {
4797 jars: ["libbar.jar"],
4798 },
4799 }
4800
4801 java_sdk_library {
4802 name: "libbar",
4803 srcs: ["foo/bar/MyClass.java"],
4804 unsafe_ignore_missing_latest_api: true,
4805 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004806 `
4807
4808 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4809 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4810
Paul Duffin89886cb2021-02-05 16:44:03 +00004811 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004812 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004813 ensureNoSourceVariant(t, ctx, "libfoo")
4814
4815 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004816 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004817 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004818 })
4819
4820 t.Run("prebuilt preferred with source", func(t *testing.T) {
4821 bp := `
4822 prebuilt_apex {
4823 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004824 arch: {
4825 arm64: {
4826 src: "myapex-arm64.apex",
4827 },
4828 arm: {
4829 src: "myapex-arm.apex",
4830 },
4831 },
Paul Duffin39853512021-02-26 11:09:39 +00004832 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004833 }
4834
4835 java_import {
4836 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00004837 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00004838 jars: ["libfoo.jar"],
4839 }
4840
4841 java_library {
4842 name: "libfoo",
4843 }
Paul Duffin39853512021-02-26 11:09:39 +00004844
4845 java_sdk_library_import {
4846 name: "libbar",
4847 prefer: true,
4848 public: {
4849 jars: ["libbar.jar"],
4850 },
4851 }
4852
4853 java_sdk_library {
4854 name: "libbar",
4855 srcs: ["foo/bar/MyClass.java"],
4856 unsafe_ignore_missing_latest_api: true,
4857 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004858 `
4859
4860 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4861 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4862
Paul Duffin89886cb2021-02-05 16:44:03 +00004863 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004864 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004865 ensureNoSourceVariant(t, ctx, "libfoo")
4866
4867 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004868 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004869 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004870 })
4871}
4872
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004873func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01004874 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01004875 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01004876 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
4877 // is disabled.
4878 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
4879 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004880
Paul Duffin37856732021-02-26 14:24:15 +00004881 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
4882 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01004883 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004884 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00004885 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004886 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00004887 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004888 foundLibfooJar = true
4889 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01004890 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004891 }
4892 }
4893 if !foundLibfooJar {
Paul Duffin55607122021-03-30 23:32:51 +01004894 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().BuildDir(), s.AllOutputs()))
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004895 }
4896 }
4897
Paul Duffin40a3f652021-07-19 13:11:24 +01004898 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00004899 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01004900 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01004901 var rule android.TestingBuildParams
4902
4903 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
4904 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00004905 }
4906
Paul Duffin40a3f652021-07-19 13:11:24 +01004907 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
4908 t.Helper()
4909 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
4910 var rule android.TestingBuildParams
4911
4912 rule = platformBootclasspath.Output("hiddenapi-index.csv")
4913 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
4914 }
4915
Paul Duffin89f570a2021-06-16 01:42:33 +01004916 fragment := java.ApexVariantReference{
4917 Apex: proptools.StringPtr("myapex"),
4918 Module: proptools.StringPtr("my-bootclasspath-fragment"),
4919 }
4920
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004921 t.Run("prebuilt only", func(t *testing.T) {
4922 bp := `
4923 prebuilt_apex {
4924 name: "myapex",
4925 arch: {
4926 arm64: {
4927 src: "myapex-arm64.apex",
4928 },
4929 arm: {
4930 src: "myapex-arm.apex",
4931 },
4932 },
Paul Duffin89f570a2021-06-16 01:42:33 +01004933 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4934 }
4935
4936 prebuilt_bootclasspath_fragment {
4937 name: "my-bootclasspath-fragment",
4938 contents: ["libfoo", "libbar"],
4939 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01004940 hidden_api: {
4941 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4942 metadata: "my-bootclasspath-fragment/metadata.csv",
4943 index: "my-bootclasspath-fragment/index.csv",
4944 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4945 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4946 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004947 }
4948
4949 java_import {
4950 name: "libfoo",
4951 jars: ["libfoo.jar"],
4952 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01004953 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004954 }
Paul Duffin37856732021-02-26 14:24:15 +00004955
4956 java_sdk_library_import {
4957 name: "libbar",
4958 public: {
4959 jars: ["libbar.jar"],
4960 },
4961 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01004962 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01004963 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00004964 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004965 `
4966
Paul Duffin89f570a2021-06-16 01:42:33 +01004967 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01004968 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
4969 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00004970
Paul Duffin537ea3d2021-05-14 10:38:00 +01004971 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01004972 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01004973 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01004974 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01004975 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
4976 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00004977 })
4978
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004979 t.Run("apex_set only", func(t *testing.T) {
4980 bp := `
4981 apex_set {
4982 name: "myapex",
4983 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01004984 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
4985 }
4986
4987 prebuilt_bootclasspath_fragment {
4988 name: "my-bootclasspath-fragment",
4989 contents: ["libfoo", "libbar"],
4990 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01004991 hidden_api: {
4992 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4993 metadata: "my-bootclasspath-fragment/metadata.csv",
4994 index: "my-bootclasspath-fragment/index.csv",
4995 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4996 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4997 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01004998 }
4999
5000 java_import {
5001 name: "libfoo",
5002 jars: ["libfoo.jar"],
5003 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005004 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005005 }
5006
5007 java_sdk_library_import {
5008 name: "libbar",
5009 public: {
5010 jars: ["libbar.jar"],
5011 },
5012 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005013 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005014 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005015 }
5016 `
5017
Paul Duffin89f570a2021-06-16 01:42:33 +01005018 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005019 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5020 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5021
Paul Duffin537ea3d2021-05-14 10:38:00 +01005022 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005023 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005024 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005025 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005026 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5027 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005028 })
5029
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005030 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5031 bp := `
5032 prebuilt_apex {
5033 name: "myapex",
5034 arch: {
5035 arm64: {
5036 src: "myapex-arm64.apex",
5037 },
5038 arm: {
5039 src: "myapex-arm.apex",
5040 },
5041 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005042 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5043 }
5044
5045 prebuilt_bootclasspath_fragment {
5046 name: "my-bootclasspath-fragment",
5047 contents: ["libfoo", "libbar"],
5048 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005049 hidden_api: {
5050 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5051 metadata: "my-bootclasspath-fragment/metadata.csv",
5052 index: "my-bootclasspath-fragment/index.csv",
5053 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5054 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5055 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005056 }
5057
5058 java_import {
5059 name: "libfoo",
5060 jars: ["libfoo.jar"],
5061 apex_available: ["myapex"],
5062 }
5063
5064 java_library {
5065 name: "libfoo",
5066 srcs: ["foo/bar/MyClass.java"],
5067 apex_available: ["myapex"],
5068 }
Paul Duffin37856732021-02-26 14:24:15 +00005069
5070 java_sdk_library_import {
5071 name: "libbar",
5072 public: {
5073 jars: ["libbar.jar"],
5074 },
5075 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005076 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005077 }
5078
5079 java_sdk_library {
5080 name: "libbar",
5081 srcs: ["foo/bar/MyClass.java"],
5082 unsafe_ignore_missing_latest_api: true,
5083 apex_available: ["myapex"],
5084 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005085 `
5086
5087 // In this test the source (java_library) libfoo is active since the
5088 // prebuilt (java_import) defaults to prefer:false. However the
5089 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5090 // find the dex boot jar in it. We either need to disable the source libfoo
5091 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005092 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005093 })
5094
5095 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5096 bp := `
5097 prebuilt_apex {
5098 name: "myapex",
5099 arch: {
5100 arm64: {
5101 src: "myapex-arm64.apex",
5102 },
5103 arm: {
5104 src: "myapex-arm.apex",
5105 },
5106 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005107 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5108 }
5109
5110 prebuilt_bootclasspath_fragment {
5111 name: "my-bootclasspath-fragment",
5112 contents: ["libfoo", "libbar"],
5113 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005114 hidden_api: {
5115 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5116 metadata: "my-bootclasspath-fragment/metadata.csv",
5117 index: "my-bootclasspath-fragment/index.csv",
5118 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5119 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5120 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005121 }
5122
5123 java_import {
5124 name: "libfoo",
5125 prefer: true,
5126 jars: ["libfoo.jar"],
5127 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005128 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005129 }
5130
5131 java_library {
5132 name: "libfoo",
5133 srcs: ["foo/bar/MyClass.java"],
5134 apex_available: ["myapex"],
5135 }
Paul Duffin37856732021-02-26 14:24:15 +00005136
5137 java_sdk_library_import {
5138 name: "libbar",
5139 prefer: true,
5140 public: {
5141 jars: ["libbar.jar"],
5142 },
5143 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005144 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005145 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005146 }
5147
5148 java_sdk_library {
5149 name: "libbar",
5150 srcs: ["foo/bar/MyClass.java"],
5151 unsafe_ignore_missing_latest_api: true,
5152 apex_available: ["myapex"],
5153 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005154 `
5155
Paul Duffin89f570a2021-06-16 01:42:33 +01005156 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005157 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5158 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005159
Paul Duffin537ea3d2021-05-14 10:38:00 +01005160 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005161 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005162 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005163 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005164 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5165 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005166 })
5167
5168 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5169 bp := `
5170 apex {
5171 name: "myapex",
5172 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005173 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005174 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005175 }
5176
5177 apex_key {
5178 name: "myapex.key",
5179 public_key: "testkey.avbpubkey",
5180 private_key: "testkey.pem",
5181 }
5182
5183 prebuilt_apex {
5184 name: "myapex",
5185 arch: {
5186 arm64: {
5187 src: "myapex-arm64.apex",
5188 },
5189 arm: {
5190 src: "myapex-arm.apex",
5191 },
5192 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005193 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5194 }
5195
5196 prebuilt_bootclasspath_fragment {
5197 name: "my-bootclasspath-fragment",
5198 contents: ["libfoo", "libbar"],
5199 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005200 hidden_api: {
5201 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5202 metadata: "my-bootclasspath-fragment/metadata.csv",
5203 index: "my-bootclasspath-fragment/index.csv",
5204 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5205 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5206 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005207 }
5208
5209 java_import {
5210 name: "libfoo",
5211 jars: ["libfoo.jar"],
5212 apex_available: ["myapex"],
5213 }
5214
5215 java_library {
5216 name: "libfoo",
5217 srcs: ["foo/bar/MyClass.java"],
5218 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005219 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005220 }
Paul Duffin37856732021-02-26 14:24:15 +00005221
5222 java_sdk_library_import {
5223 name: "libbar",
5224 public: {
5225 jars: ["libbar.jar"],
5226 },
5227 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005228 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005229 }
5230
5231 java_sdk_library {
5232 name: "libbar",
5233 srcs: ["foo/bar/MyClass.java"],
5234 unsafe_ignore_missing_latest_api: true,
5235 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005236 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005237 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005238 `
5239
Paul Duffin89f570a2021-06-16 01:42:33 +01005240 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005241 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5242 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005243
Paul Duffin537ea3d2021-05-14 10:38:00 +01005244 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005245 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005246 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005247 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005248 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5249 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005250 })
5251
5252 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5253 bp := `
5254 apex {
5255 name: "myapex",
5256 enabled: false,
5257 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005258 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005259 }
5260
5261 apex_key {
5262 name: "myapex.key",
5263 public_key: "testkey.avbpubkey",
5264 private_key: "testkey.pem",
5265 }
5266
5267 prebuilt_apex {
5268 name: "myapex",
5269 arch: {
5270 arm64: {
5271 src: "myapex-arm64.apex",
5272 },
5273 arm: {
5274 src: "myapex-arm.apex",
5275 },
5276 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005277 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5278 }
5279
5280 prebuilt_bootclasspath_fragment {
5281 name: "my-bootclasspath-fragment",
5282 contents: ["libfoo", "libbar"],
5283 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005284 hidden_api: {
5285 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5286 metadata: "my-bootclasspath-fragment/metadata.csv",
5287 index: "my-bootclasspath-fragment/index.csv",
5288 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5289 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5290 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005291 }
5292
5293 java_import {
5294 name: "libfoo",
5295 prefer: true,
5296 jars: ["libfoo.jar"],
5297 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005298 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005299 }
5300
5301 java_library {
5302 name: "libfoo",
5303 srcs: ["foo/bar/MyClass.java"],
5304 apex_available: ["myapex"],
5305 }
Paul Duffin37856732021-02-26 14:24:15 +00005306
5307 java_sdk_library_import {
5308 name: "libbar",
5309 prefer: true,
5310 public: {
5311 jars: ["libbar.jar"],
5312 },
5313 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005314 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005315 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005316 }
5317
5318 java_sdk_library {
5319 name: "libbar",
5320 srcs: ["foo/bar/MyClass.java"],
5321 unsafe_ignore_missing_latest_api: true,
5322 apex_available: ["myapex"],
5323 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005324 `
5325
Paul Duffin89f570a2021-06-16 01:42:33 +01005326 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005327 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5328 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005329
Paul Duffin537ea3d2021-05-14 10:38:00 +01005330 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005331 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005332 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005333 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005334 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5335 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005336 })
5337}
5338
Roland Levillain630846d2019-06-26 12:48:34 +01005339func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005340 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005341 apex_test {
5342 name: "myapex",
5343 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005344 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005345 tests: [
5346 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005347 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005348 ],
5349 }
5350
5351 apex_key {
5352 name: "myapex.key",
5353 public_key: "testkey.avbpubkey",
5354 private_key: "testkey.pem",
5355 }
5356
Liz Kammer1c14a212020-05-12 15:26:55 -07005357 filegroup {
5358 name: "fg",
5359 srcs: [
5360 "baz",
5361 "bar/baz"
5362 ],
5363 }
5364
Roland Levillain630846d2019-06-26 12:48:34 +01005365 cc_test {
5366 name: "mytest",
5367 gtest: false,
5368 srcs: ["mytest.cpp"],
5369 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005370 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005371 system_shared_libs: [],
5372 static_executable: true,
5373 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005374 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005375 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005376
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005377 cc_library {
5378 name: "mylib",
5379 srcs: ["mylib.cpp"],
5380 system_shared_libs: [],
5381 stl: "none",
5382 }
5383
Liz Kammer5bd365f2020-05-27 15:15:11 -07005384 filegroup {
5385 name: "fg2",
5386 srcs: [
5387 "testdata/baz"
5388 ],
5389 }
5390
Roland Levillain9b5fde92019-06-28 15:41:19 +01005391 cc_test {
5392 name: "mytests",
5393 gtest: false,
5394 srcs: [
5395 "mytest1.cpp",
5396 "mytest2.cpp",
5397 "mytest3.cpp",
5398 ],
5399 test_per_src: true,
5400 relative_install_path: "test",
5401 system_shared_libs: [],
5402 static_executable: true,
5403 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005404 data: [
5405 ":fg",
5406 ":fg2",
5407 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005408 }
Roland Levillain630846d2019-06-26 12:48:34 +01005409 `)
5410
Sundong Ahnabb64432019-10-22 13:58:29 +09005411 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005412 copyCmds := apexRule.Args["copy_commands"]
5413
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005414 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005415 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005416 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005417
Liz Kammer1c14a212020-05-12 15:26:55 -07005418 //Ensure that test data are copied into apex.
5419 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5420 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5421
Roland Levillain9b5fde92019-06-28 15:41:19 +01005422 // Ensure that test deps built with `test_per_src` are copied into apex.
5423 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5424 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5425 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005426
5427 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005428 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005429 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005430 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005431 prefix := "TARGET_"
5432 var builder strings.Builder
5433 data.Custom(&builder, name, prefix, "", data)
5434 androidMk := builder.String()
Jooyung Han31c470b2019-10-18 16:26:59 +09005435 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5436 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5437 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5438 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Jooyung Han214bf372019-11-12 13:03:50 +09005439 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
Jooyung Han31c470b2019-10-18 16:26:59 +09005440 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005441 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005442
5443 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005444 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005445 data.Custom(&builder, name, prefix, "", data)
5446 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005447 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5448 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005449}
5450
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005451func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005452 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005453 apex {
5454 name: "myapex",
5455 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005456 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005457 }
5458 apex_key {
5459 name: "myapex.key",
5460 public_key: "testkey.avbpubkey",
5461 private_key: "testkey.pem",
5462 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005463 `,
5464 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5465 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5466 }),
5467 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005468 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jiyong Park83dc74b2020-01-14 18:38:44 +09005469 ensureListContains(t, ab.requiredDeps, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005470 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005471 var builder strings.Builder
5472 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5473 androidMk := builder.String()
5474 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += myapex.flattened")
5475}
5476
Jooyung Hand48f3c32019-08-23 11:18:57 +09005477func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5478 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5479 apex {
5480 name: "myapex",
5481 key: "myapex.key",
5482 native_shared_libs: ["libfoo"],
5483 }
5484
5485 apex_key {
5486 name: "myapex.key",
5487 public_key: "testkey.avbpubkey",
5488 private_key: "testkey.pem",
5489 }
5490
5491 cc_library {
5492 name: "libfoo",
5493 stl: "none",
5494 system_shared_libs: [],
5495 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005496 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005497 }
5498 `)
5499 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5500 apex {
5501 name: "myapex",
5502 key: "myapex.key",
5503 java_libs: ["myjar"],
5504 }
5505
5506 apex_key {
5507 name: "myapex.key",
5508 public_key: "testkey.avbpubkey",
5509 private_key: "testkey.pem",
5510 }
5511
5512 java_library {
5513 name: "myjar",
5514 srcs: ["foo/bar/MyClass.java"],
5515 sdk_version: "none",
5516 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005517 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005518 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005519 }
5520 `)
5521}
5522
Bill Peckhama41a6962021-01-11 10:58:54 -08005523func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005524 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005525 apex {
5526 name: "myapex",
5527 key: "myapex.key",
5528 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005529 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005530 }
5531
5532 apex_key {
5533 name: "myapex.key",
5534 public_key: "testkey.avbpubkey",
5535 private_key: "testkey.pem",
5536 }
5537
5538 java_import {
5539 name: "myjavaimport",
5540 apex_available: ["myapex"],
5541 jars: ["my.jar"],
5542 compile_dex: true,
5543 }
5544 `)
5545
5546 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5547 apexRule := module.Rule("apexRule")
5548 copyCmds := apexRule.Args["copy_commands"]
5549 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5550}
5551
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005552func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005553 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005554 apex {
5555 name: "myapex",
5556 key: "myapex.key",
5557 apps: [
5558 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005559 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005560 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005561 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005562 }
5563
5564 apex_key {
5565 name: "myapex.key",
5566 public_key: "testkey.avbpubkey",
5567 private_key: "testkey.pem",
5568 }
5569
5570 android_app {
5571 name: "AppFoo",
5572 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005573 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005574 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005575 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005576 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005577 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005578 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005579
5580 android_app {
5581 name: "AppFooPriv",
5582 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005583 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005584 system_modules: "none",
5585 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005586 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005587 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005588 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005589
5590 cc_library_shared {
5591 name: "libjni",
5592 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005593 shared_libs: ["libfoo"],
5594 stl: "none",
5595 system_shared_libs: [],
5596 apex_available: [ "myapex" ],
5597 sdk_version: "current",
5598 }
5599
5600 cc_library_shared {
5601 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005602 stl: "none",
5603 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005604 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005605 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005606 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005607 `)
5608
Sundong Ahnabb64432019-10-22 13:58:29 +09005609 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005610 apexRule := module.Rule("apexRule")
5611 copyCmds := apexRule.Args["copy_commands"]
5612
5613 ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
Jiyong Parkf7487312019-10-17 12:54:30 +09005614 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005615
Colin Crossaede88c2020-08-11 12:17:01 -07005616 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005617 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005618 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005619 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005620 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005621 // JNI libraries including transitive deps are
5622 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005623 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005624 // ... embedded inside APK (jnilibs.zip)
5625 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5626 // ... and not directly inside the APEX
5627 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5628 }
Dario Frenicde2a032019-10-27 00:29:22 +01005629}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005630
Dario Frenicde2a032019-10-27 00:29:22 +01005631func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005632 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005633 apex {
5634 name: "myapex",
5635 key: "myapex.key",
5636 apps: [
5637 "AppFooPrebuilt",
5638 "AppFooPrivPrebuilt",
5639 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005640 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005641 }
5642
5643 apex_key {
5644 name: "myapex.key",
5645 public_key: "testkey.avbpubkey",
5646 private_key: "testkey.pem",
5647 }
5648
5649 android_app_import {
5650 name: "AppFooPrebuilt",
5651 apk: "PrebuiltAppFoo.apk",
5652 presigned: true,
5653 dex_preopt: {
5654 enabled: false,
5655 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005656 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005657 }
5658
5659 android_app_import {
5660 name: "AppFooPrivPrebuilt",
5661 apk: "PrebuiltAppFooPriv.apk",
5662 privileged: true,
5663 presigned: true,
5664 dex_preopt: {
5665 enabled: false,
5666 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005667 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005668 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005669 }
5670 `)
5671
Sundong Ahnabb64432019-10-22 13:58:29 +09005672 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005673 apexRule := module.Rule("apexRule")
5674 copyCmds := apexRule.Args["copy_commands"]
5675
5676 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt/AppFooPrebuilt.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005677 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt/AwesomePrebuiltAppFooPriv.apk")
5678}
5679
5680func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005681 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005682 apex {
5683 name: "myapex",
5684 key: "myapex.key",
5685 apps: [
5686 "AppFoo",
5687 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005688 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005689 }
5690
5691 apex_key {
5692 name: "myapex.key",
5693 public_key: "testkey.avbpubkey",
5694 private_key: "testkey.pem",
5695 }
5696
5697 android_app {
5698 name: "AppFoo",
5699 srcs: ["foo/bar/MyClass.java"],
5700 sdk_version: "none",
5701 system_modules: "none",
5702 apex_available: [ "myapex" ],
5703 }
5704
5705 android_app_import {
5706 name: "AppFoo",
5707 apk: "AppFooPrebuilt.apk",
5708 filename: "AppFooPrebuilt.apk",
5709 presigned: true,
5710 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09005711 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09005712 }
5713 `, withFiles(map[string][]byte{
5714 "AppFooPrebuilt.apk": nil,
5715 }))
5716
5717 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han86feead2021-03-08 13:11:48 +09005718 "app/AppFoo/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09005719 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005720}
5721
Dario Freni6f3937c2019-12-20 22:58:03 +00005722func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005723 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00005724 apex {
5725 name: "myapex",
5726 key: "myapex.key",
5727 apps: [
5728 "TesterHelpAppFoo",
5729 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005730 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00005731 }
5732
5733 apex_key {
5734 name: "myapex.key",
5735 public_key: "testkey.avbpubkey",
5736 private_key: "testkey.pem",
5737 }
5738
5739 android_test_helper_app {
5740 name: "TesterHelpAppFoo",
5741 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005742 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00005743 }
5744
5745 `)
5746
5747 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5748 apexRule := module.Rule("apexRule")
5749 copyCmds := apexRule.Args["copy_commands"]
5750
5751 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo/TesterHelpAppFoo.apk")
5752}
5753
Jooyung Han18020ea2019-11-13 10:50:48 +09005754func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
5755 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00005756 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09005757 apex {
5758 name: "myapex",
5759 key: "myapex.key",
5760 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005761 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005762 }
5763
5764 apex_key {
5765 name: "myapex.key",
5766 public_key: "testkey.avbpubkey",
5767 private_key: "testkey.pem",
5768 }
5769
5770 apex {
5771 name: "otherapex",
5772 key: "myapex.key",
5773 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005774 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09005775 }
5776
5777 cc_defaults {
5778 name: "libfoo-defaults",
5779 apex_available: ["otherapex"],
5780 }
5781
5782 cc_library {
5783 name: "libfoo",
5784 defaults: ["libfoo-defaults"],
5785 stl: "none",
5786 system_shared_libs: [],
5787 }`)
5788}
5789
Paul Duffine52e66f2020-03-30 17:54:29 +01005790func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005791 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00005792 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09005793 apex {
5794 name: "myapex",
5795 key: "myapex.key",
5796 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005797 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005798 }
5799
5800 apex_key {
5801 name: "myapex.key",
5802 public_key: "testkey.avbpubkey",
5803 private_key: "testkey.pem",
5804 }
5805
5806 apex {
5807 name: "otherapex",
5808 key: "otherapex.key",
5809 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005810 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005811 }
5812
5813 apex_key {
5814 name: "otherapex.key",
5815 public_key: "testkey.avbpubkey",
5816 private_key: "testkey.pem",
5817 }
5818
5819 cc_library {
5820 name: "libfoo",
5821 stl: "none",
5822 system_shared_libs: [],
5823 apex_available: ["otherapex"],
5824 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005825}
Jiyong Park127b40b2019-09-30 16:04:35 +09005826
Paul Duffine52e66f2020-03-30 17:54:29 +01005827func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09005828 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09005829 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Colin Cross6e511a92020-07-27 21:26:48 -07005830.*via tag apex\.dependencyTag.*name:sharedLib.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005831.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005832.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01005833.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07005834.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01005835.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005836 apex {
5837 name: "myapex",
5838 key: "myapex.key",
5839 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005840 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005841 }
5842
5843 apex_key {
5844 name: "myapex.key",
5845 public_key: "testkey.avbpubkey",
5846 private_key: "testkey.pem",
5847 }
5848
Jiyong Park127b40b2019-09-30 16:04:35 +09005849 cc_library {
5850 name: "libfoo",
5851 stl: "none",
5852 shared_libs: ["libbar"],
5853 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005854 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005855 }
5856
5857 cc_library {
5858 name: "libbar",
5859 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09005860 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005861 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09005862 apex_available: ["myapex"],
5863 }
5864
5865 cc_library {
5866 name: "libbaz",
5867 stl: "none",
5868 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09005869 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005870}
Jiyong Park127b40b2019-09-30 16:04:35 +09005871
Paul Duffine52e66f2020-03-30 17:54:29 +01005872func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09005873 testApexError(t, "\"otherapex\" is not a valid module name", `
5874 apex {
5875 name: "myapex",
5876 key: "myapex.key",
5877 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005878 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005879 }
5880
5881 apex_key {
5882 name: "myapex.key",
5883 public_key: "testkey.avbpubkey",
5884 private_key: "testkey.pem",
5885 }
5886
5887 cc_library {
5888 name: "libfoo",
5889 stl: "none",
5890 system_shared_libs: [],
5891 apex_available: ["otherapex"],
5892 }`)
5893
Paul Duffine52e66f2020-03-30 17:54:29 +01005894 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005895 apex {
5896 name: "myapex",
5897 key: "myapex.key",
5898 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005899 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005900 }
5901
5902 apex_key {
5903 name: "myapex.key",
5904 public_key: "testkey.avbpubkey",
5905 private_key: "testkey.pem",
5906 }
5907
5908 cc_library {
5909 name: "libfoo",
5910 stl: "none",
5911 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09005912 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005913 apex_available: ["myapex"],
5914 }
5915
5916 cc_library {
5917 name: "libbar",
5918 stl: "none",
5919 system_shared_libs: [],
5920 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09005921 }
5922
5923 cc_library {
5924 name: "libbaz",
5925 stl: "none",
5926 system_shared_libs: [],
5927 stubs: {
5928 versions: ["10", "20", "30"],
5929 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005930 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01005931}
Jiyong Park127b40b2019-09-30 16:04:35 +09005932
Jiyong Park89e850a2020-04-07 16:37:39 +09005933func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005934 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09005935 apex {
5936 name: "myapex",
5937 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09005938 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005939 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09005940 }
5941
5942 apex_key {
5943 name: "myapex.key",
5944 public_key: "testkey.avbpubkey",
5945 private_key: "testkey.pem",
5946 }
5947
5948 cc_library {
5949 name: "libfoo",
5950 stl: "none",
5951 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09005952 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09005953 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09005954 }
5955
5956 cc_library {
5957 name: "libfoo2",
5958 stl: "none",
5959 system_shared_libs: [],
5960 shared_libs: ["libbaz"],
5961 apex_available: ["//apex_available:platform"],
5962 }
5963
5964 cc_library {
5965 name: "libbar",
5966 stl: "none",
5967 system_shared_libs: [],
5968 apex_available: ["myapex"],
5969 }
5970
5971 cc_library {
5972 name: "libbaz",
5973 stl: "none",
5974 system_shared_libs: [],
5975 apex_available: ["myapex"],
5976 stubs: {
5977 versions: ["1"],
5978 },
Jiyong Park127b40b2019-09-30 16:04:35 +09005979 }`)
5980
Jiyong Park89e850a2020-04-07 16:37:39 +09005981 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
5982 // because it depends on libbar which isn't available to platform
5983 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5984 if libfoo.NotAvailableForPlatform() != true {
5985 t.Errorf("%q shouldn't be available to platform", libfoo.String())
5986 }
5987
5988 // libfoo2 however can be available to platform because it depends on libbaz which provides
5989 // stubs
5990 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
5991 if libfoo2.NotAvailableForPlatform() == true {
5992 t.Errorf("%q should be available to platform", libfoo2.String())
5993 }
Paul Duffine52e66f2020-03-30 17:54:29 +01005994}
Jiyong Parka90ca002019-10-07 15:47:24 +09005995
Paul Duffine52e66f2020-03-30 17:54:29 +01005996func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005997 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09005998 apex {
5999 name: "myapex",
6000 key: "myapex.key",
6001 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006002 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006003 }
6004
6005 apex_key {
6006 name: "myapex.key",
6007 public_key: "testkey.avbpubkey",
6008 private_key: "testkey.pem",
6009 }
6010
6011 cc_library {
6012 name: "libfoo",
6013 stl: "none",
6014 system_shared_libs: [],
6015 apex_available: ["myapex"],
6016 static: {
6017 apex_available: ["//apex_available:platform"],
6018 },
6019 }`)
6020
Jiyong Park89e850a2020-04-07 16:37:39 +09006021 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6022 if libfooShared.NotAvailableForPlatform() != true {
6023 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6024 }
6025 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6026 if libfooStatic.NotAvailableForPlatform() != false {
6027 t.Errorf("%q should be available to platform", libfooStatic.String())
6028 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006029}
6030
Jiyong Park5d790c32019-11-15 18:40:32 +09006031func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006032 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006033 apex {
6034 name: "myapex",
6035 key: "myapex.key",
6036 apps: ["app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006037 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006038 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006039 }
6040
6041 override_apex {
6042 name: "override_myapex",
6043 base: "myapex",
6044 apps: ["override_app"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006045 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006046 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006047 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006048 key: "mynewapex.key",
6049 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006050 }
6051
6052 apex_key {
6053 name: "myapex.key",
6054 public_key: "testkey.avbpubkey",
6055 private_key: "testkey.pem",
6056 }
6057
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006058 apex_key {
6059 name: "mynewapex.key",
6060 public_key: "testkey2.avbpubkey",
6061 private_key: "testkey2.pem",
6062 }
6063
6064 android_app_certificate {
6065 name: "myapex.certificate",
6066 certificate: "testkey",
6067 }
6068
Jiyong Park5d790c32019-11-15 18:40:32 +09006069 android_app {
6070 name: "app",
6071 srcs: ["foo/bar/MyClass.java"],
6072 package_name: "foo",
6073 sdk_version: "none",
6074 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006075 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006076 }
6077
6078 override_android_app {
6079 name: "override_app",
6080 base: "app",
6081 package_name: "bar",
6082 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006083 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006084
Jiyong Park317645e2019-12-05 13:20:58 +09006085 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6086 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6087 if originalVariant.GetOverriddenBy() != "" {
6088 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6089 }
6090 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6091 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6092 }
6093
Jiyong Park5d790c32019-11-15 18:40:32 +09006094 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6095 apexRule := module.Rule("apexRule")
6096 copyCmds := apexRule.Args["copy_commands"]
6097
6098 ensureNotContains(t, copyCmds, "image.apex/app/app/app.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006099 ensureContains(t, copyCmds, "image.apex/app/override_app/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006100
6101 apexBundle := module.Module().(*apexBundle)
6102 name := apexBundle.Name()
6103 if name != "override_myapex" {
6104 t.Errorf("name should be \"override_myapex\", but was %q", name)
6105 }
6106
Baligh Uddin004d7172020-02-19 21:29:28 -08006107 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6108 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6109 }
6110
Jiyong Park20bacab2020-03-03 11:45:41 +09006111 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006112 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006113 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6114
6115 signApkRule := module.Rule("signapk")
6116 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006117
Colin Crossaa255532020-07-03 13:18:24 -07006118 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006119 var builder strings.Builder
6120 data.Custom(&builder, name, "TARGET_", "", data)
6121 androidMk := builder.String()
Jiyong Parkf653b052019-11-18 15:39:01 +09006122 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006123 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
6124 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006125 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006126 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006127 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006128 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
6129 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006130}
6131
Jooyung Han214bf372019-11-12 13:03:50 +09006132func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006133 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006134 apex {
6135 name: "myapex",
6136 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006137 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006138 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006139 }
6140
6141 apex_key {
6142 name: "myapex.key",
6143 public_key: "testkey.avbpubkey",
6144 private_key: "testkey.pem",
6145 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006146
6147 cc_library {
6148 name: "mylib",
6149 srcs: ["mylib.cpp"],
6150 stl: "libc++",
6151 system_shared_libs: [],
6152 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006153 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006154 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006155 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006156
6157 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6158 args := module.Rule("apexRule").Args
6159 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006160 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006161
6162 // The copies of the libraries in the apex should have one more dependency than
6163 // the ones outside the apex, namely the unwinder. Ideally we should check
6164 // the dependency names directly here but for some reason the names are blank in
6165 // this test.
6166 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006167 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006168 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6169 if len(apexImplicits) != len(nonApexImplicits)+1 {
6170 t.Errorf("%q missing unwinder dep", lib)
6171 }
6172 }
Jooyung Han214bf372019-11-12 13:03:50 +09006173}
6174
Paul Duffine05480a2021-03-08 15:07:14 +00006175var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006176 "api/current.txt": nil,
6177 "api/removed.txt": nil,
6178 "api/system-current.txt": nil,
6179 "api/system-removed.txt": nil,
6180 "api/test-current.txt": nil,
6181 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006182
Anton Hanssondff2c782020-12-21 17:10:01 +00006183 "100/public/api/foo.txt": nil,
6184 "100/public/api/foo-removed.txt": nil,
6185 "100/system/api/foo.txt": nil,
6186 "100/system/api/foo-removed.txt": nil,
6187
Paul Duffineedc5d52020-06-12 17:46:39 +01006188 // For java_sdk_library_import
6189 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006190}
6191
Jooyung Han58f26ab2019-12-18 15:34:32 +09006192func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006193 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006194 apex {
6195 name: "myapex",
6196 key: "myapex.key",
6197 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006198 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006199 }
6200
6201 apex_key {
6202 name: "myapex.key",
6203 public_key: "testkey.avbpubkey",
6204 private_key: "testkey.pem",
6205 }
6206
6207 java_sdk_library {
6208 name: "foo",
6209 srcs: ["a.java"],
6210 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006211 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006212 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006213
6214 prebuilt_apis {
6215 name: "sdk",
6216 api_dirs: ["100"],
6217 }
Paul Duffin9b879592020-05-26 13:21:35 +01006218 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006219
6220 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006221 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006222 "javalib/foo.jar",
6223 "etc/permissions/foo.xml",
6224 })
6225 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006226 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
6227 ensureContains(t, sdkLibrary.RuleParams.Command, `<library name=\"foo\" file=\"/apex/myapex/javalib/foo.jar\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09006228}
6229
Paul Duffin9b879592020-05-26 13:21:35 +01006230func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006231 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006232 apex {
6233 name: "myapex",
6234 key: "myapex.key",
6235 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006236 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006237 }
6238
6239 apex_key {
6240 name: "myapex.key",
6241 public_key: "testkey.avbpubkey",
6242 private_key: "testkey.pem",
6243 }
6244
6245 java_sdk_library {
6246 name: "foo",
6247 srcs: ["a.java"],
6248 api_packages: ["foo"],
6249 apex_available: ["myapex"],
6250 sdk_version: "none",
6251 system_modules: "none",
6252 }
6253
6254 java_library {
6255 name: "bar",
6256 srcs: ["a.java"],
6257 libs: ["foo"],
6258 apex_available: ["myapex"],
6259 sdk_version: "none",
6260 system_modules: "none",
6261 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006262
6263 prebuilt_apis {
6264 name: "sdk",
6265 api_dirs: ["100"],
6266 }
Paul Duffin9b879592020-05-26 13:21:35 +01006267 `, withFiles(filesForSdkLibrary))
6268
6269 // java_sdk_library installs both impl jar and permission XML
6270 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6271 "javalib/bar.jar",
6272 "javalib/foo.jar",
6273 "etc/permissions/foo.xml",
6274 })
6275
6276 // The bar library should depend on the implementation jar.
6277 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006278 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006279 t.Errorf("expected %q, found %#q", expected, actual)
6280 }
6281}
6282
6283func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006284 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006285 apex {
6286 name: "myapex",
6287 key: "myapex.key",
6288 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006289 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006290 }
6291
6292 apex_key {
6293 name: "myapex.key",
6294 public_key: "testkey.avbpubkey",
6295 private_key: "testkey.pem",
6296 }
6297
6298 java_sdk_library {
6299 name: "foo",
6300 srcs: ["a.java"],
6301 api_packages: ["foo"],
6302 apex_available: ["myapex"],
6303 sdk_version: "none",
6304 system_modules: "none",
6305 }
6306
6307 java_library {
6308 name: "bar",
6309 srcs: ["a.java"],
6310 libs: ["foo"],
6311 sdk_version: "none",
6312 system_modules: "none",
6313 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006314
6315 prebuilt_apis {
6316 name: "sdk",
6317 api_dirs: ["100"],
6318 }
Paul Duffin9b879592020-05-26 13:21:35 +01006319 `, withFiles(filesForSdkLibrary))
6320
6321 // java_sdk_library installs both impl jar and permission XML
6322 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6323 "javalib/foo.jar",
6324 "etc/permissions/foo.xml",
6325 })
6326
6327 // The bar library should depend on the stubs jar.
6328 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006329 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006330 t.Errorf("expected %q, found %#q", expected, actual)
6331 }
6332}
6333
Paul Duffineedc5d52020-06-12 17:46:39 +01006334func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006335 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006336 prebuilt_apis {
6337 name: "sdk",
6338 api_dirs: ["100"],
6339 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006340 withFiles(map[string][]byte{
6341 "apex/a.java": nil,
6342 "apex/apex_manifest.json": nil,
6343 "apex/Android.bp": []byte(`
6344 package {
6345 default_visibility: ["//visibility:private"],
6346 }
6347
6348 apex {
6349 name: "myapex",
6350 key: "myapex.key",
6351 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006352 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006353 }
6354
6355 apex_key {
6356 name: "myapex.key",
6357 public_key: "testkey.avbpubkey",
6358 private_key: "testkey.pem",
6359 }
6360
6361 java_library {
6362 name: "bar",
6363 srcs: ["a.java"],
6364 libs: ["foo"],
6365 apex_available: ["myapex"],
6366 sdk_version: "none",
6367 system_modules: "none",
6368 }
6369`),
6370 "source/a.java": nil,
6371 "source/api/current.txt": nil,
6372 "source/api/removed.txt": nil,
6373 "source/Android.bp": []byte(`
6374 package {
6375 default_visibility: ["//visibility:private"],
6376 }
6377
6378 java_sdk_library {
6379 name: "foo",
6380 visibility: ["//apex"],
6381 srcs: ["a.java"],
6382 api_packages: ["foo"],
6383 apex_available: ["myapex"],
6384 sdk_version: "none",
6385 system_modules: "none",
6386 public: {
6387 enabled: true,
6388 },
6389 }
6390`),
6391 "prebuilt/a.jar": nil,
6392 "prebuilt/Android.bp": []byte(`
6393 package {
6394 default_visibility: ["//visibility:private"],
6395 }
6396
6397 java_sdk_library_import {
6398 name: "foo",
6399 visibility: ["//apex", "//source"],
6400 apex_available: ["myapex"],
6401 prefer: true,
6402 public: {
6403 jars: ["a.jar"],
6404 },
6405 }
6406`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006407 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006408 )
6409
6410 // java_sdk_library installs both impl jar and permission XML
6411 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6412 "javalib/bar.jar",
6413 "javalib/foo.jar",
6414 "etc/permissions/foo.xml",
6415 })
6416
6417 // The bar library should depend on the implementation jar.
6418 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006419 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006420 t.Errorf("expected %q, found %#q", expected, actual)
6421 }
6422}
6423
6424func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6425 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6426 apex {
6427 name: "myapex",
6428 key: "myapex.key",
6429 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006430 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006431 }
6432
6433 apex_key {
6434 name: "myapex.key",
6435 public_key: "testkey.avbpubkey",
6436 private_key: "testkey.pem",
6437 }
6438
6439 java_sdk_library_import {
6440 name: "foo",
6441 apex_available: ["myapex"],
6442 prefer: true,
6443 public: {
6444 jars: ["a.jar"],
6445 },
6446 }
6447
6448 `, withFiles(filesForSdkLibrary))
6449}
6450
atrost6e126252020-01-27 17:01:16 +00006451func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006452 result := android.GroupFixturePreparers(
6453 prepareForApexTest,
6454 java.PrepareForTestWithPlatformCompatConfig,
6455 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006456 apex {
6457 name: "myapex",
6458 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006459 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00006460 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006461 updatable: false,
atrost6e126252020-01-27 17:01:16 +00006462 }
6463
6464 apex_key {
6465 name: "myapex.key",
6466 public_key: "testkey.avbpubkey",
6467 private_key: "testkey.pem",
6468 }
6469
6470 platform_compat_config {
6471 name: "myjar-platform-compat-config",
6472 src: ":myjar",
6473 }
6474
6475 java_library {
6476 name: "myjar",
6477 srcs: ["foo/bar/MyClass.java"],
6478 sdk_version: "none",
6479 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00006480 apex_available: [ "myapex" ],
6481 }
Paul Duffin1b29e002021-03-16 15:06:54 +00006482
6483 // Make sure that a preferred prebuilt does not affect the apex contents.
6484 prebuilt_platform_compat_config {
6485 name: "myjar-platform-compat-config",
6486 metadata: "compat-config/metadata.xml",
6487 prefer: true,
6488 }
atrost6e126252020-01-27 17:01:16 +00006489 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00006490 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00006491 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6492 "etc/compatconfig/myjar-platform-compat-config.xml",
6493 "javalib/myjar.jar",
6494 })
6495}
6496
Jiyong Park479321d2019-12-16 11:47:12 +09006497func TestRejectNonInstallableJavaLibrary(t *testing.T) {
6498 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
6499 apex {
6500 name: "myapex",
6501 key: "myapex.key",
6502 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006503 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09006504 }
6505
6506 apex_key {
6507 name: "myapex.key",
6508 public_key: "testkey.avbpubkey",
6509 private_key: "testkey.pem",
6510 }
6511
6512 java_library {
6513 name: "myjar",
6514 srcs: ["foo/bar/MyClass.java"],
6515 sdk_version: "none",
6516 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09006517 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006518 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09006519 }
6520 `)
6521}
6522
Jiyong Park7afd1072019-12-30 16:56:33 +09006523func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006524 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09006525 apex {
6526 name: "myapex",
6527 key: "myapex.key",
6528 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006529 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09006530 }
6531
6532 apex_key {
6533 name: "myapex.key",
6534 public_key: "testkey.avbpubkey",
6535 private_key: "testkey.pem",
6536 }
6537
6538 cc_library {
6539 name: "mylib",
6540 srcs: ["mylib.cpp"],
6541 system_shared_libs: [],
6542 stl: "none",
6543 required: ["a", "b"],
6544 host_required: ["c", "d"],
6545 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006546 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09006547 }
6548 `)
6549
6550 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006551 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09006552 name := apexBundle.BaseModuleName()
6553 prefix := "TARGET_"
6554 var builder strings.Builder
6555 data.Custom(&builder, name, prefix, "", data)
6556 androidMk := builder.String()
6557 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += a b\n")
6558 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES += c d\n")
6559 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES += e f\n")
6560}
6561
Jiyong Park7cd10e32020-01-14 09:22:18 +09006562func TestSymlinksFromApexToSystem(t *testing.T) {
6563 bp := `
6564 apex {
6565 name: "myapex",
6566 key: "myapex.key",
6567 native_shared_libs: ["mylib"],
6568 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006569 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09006570 }
6571
Jiyong Park9d677202020-02-19 16:29:35 +09006572 apex {
6573 name: "myapex.updatable",
6574 key: "myapex.key",
6575 native_shared_libs: ["mylib"],
6576 java_libs: ["myjar"],
6577 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09006578 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09006579 }
6580
Jiyong Park7cd10e32020-01-14 09:22:18 +09006581 apex_key {
6582 name: "myapex.key",
6583 public_key: "testkey.avbpubkey",
6584 private_key: "testkey.pem",
6585 }
6586
6587 cc_library {
6588 name: "mylib",
6589 srcs: ["mylib.cpp"],
6590 shared_libs: ["myotherlib"],
6591 system_shared_libs: [],
6592 stl: "none",
6593 apex_available: [
6594 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006595 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006596 "//apex_available:platform",
6597 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006598 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006599 }
6600
6601 cc_library {
6602 name: "myotherlib",
6603 srcs: ["mylib.cpp"],
6604 system_shared_libs: [],
6605 stl: "none",
6606 apex_available: [
6607 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006608 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006609 "//apex_available:platform",
6610 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006611 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006612 }
6613
6614 java_library {
6615 name: "myjar",
6616 srcs: ["foo/bar/MyClass.java"],
6617 sdk_version: "none",
6618 system_modules: "none",
6619 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09006620 apex_available: [
6621 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006622 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006623 "//apex_available:platform",
6624 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006625 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006626 }
6627
6628 java_library {
6629 name: "myotherjar",
6630 srcs: ["foo/bar/MyClass.java"],
6631 sdk_version: "none",
6632 system_modules: "none",
6633 apex_available: [
6634 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09006635 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006636 "//apex_available:platform",
6637 ],
Jooyung Han749dc692020-04-15 11:03:39 +09006638 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09006639 }
6640 `
6641
6642 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
6643 for _, f := range files {
6644 if f.path == file {
6645 if f.isLink {
6646 t.Errorf("%q is not a real file", file)
6647 }
6648 return
6649 }
6650 }
6651 t.Errorf("%q is not found", file)
6652 }
6653
6654 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
6655 for _, f := range files {
6656 if f.path == file {
6657 if !f.isLink {
6658 t.Errorf("%q is not a symlink", file)
6659 }
6660 return
6661 }
6662 }
6663 t.Errorf("%q is not found", file)
6664 }
6665
Jiyong Park9d677202020-02-19 16:29:35 +09006666 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
6667 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08006668 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006669 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006670 ensureRealfileExists(t, files, "javalib/myjar.jar")
6671 ensureRealfileExists(t, files, "lib64/mylib.so")
6672 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6673
Jiyong Park9d677202020-02-19 16:29:35 +09006674 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6675 ensureRealfileExists(t, files, "javalib/myjar.jar")
6676 ensureRealfileExists(t, files, "lib64/mylib.so")
6677 ensureRealfileExists(t, files, "lib64/myotherlib.so")
6678
6679 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08006680 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00006681 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09006682 ensureRealfileExists(t, files, "javalib/myjar.jar")
6683 ensureRealfileExists(t, files, "lib64/mylib.so")
6684 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09006685
6686 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
6687 ensureRealfileExists(t, files, "javalib/myjar.jar")
6688 ensureRealfileExists(t, files, "lib64/mylib.so")
6689 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09006690}
6691
Yo Chiange8128052020-07-23 20:09:18 +08006692func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006693 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08006694 apex {
6695 name: "myapex",
6696 key: "myapex.key",
6697 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006698 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08006699 }
6700
6701 apex_key {
6702 name: "myapex.key",
6703 public_key: "testkey.avbpubkey",
6704 private_key: "testkey.pem",
6705 }
6706
6707 cc_library_shared {
6708 name: "mylib",
6709 srcs: ["mylib.cpp"],
6710 shared_libs: ["myotherlib"],
6711 system_shared_libs: [],
6712 stl: "none",
6713 apex_available: [
6714 "myapex",
6715 "//apex_available:platform",
6716 ],
6717 }
6718
6719 cc_prebuilt_library_shared {
6720 name: "myotherlib",
6721 srcs: ["prebuilt.so"],
6722 system_shared_libs: [],
6723 stl: "none",
6724 apex_available: [
6725 "myapex",
6726 "//apex_available:platform",
6727 ],
6728 }
6729 `)
6730
6731 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006732 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08006733 var builder strings.Builder
6734 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
6735 androidMk := builder.String()
6736 // `myotherlib` is added to `myapex` as symlink
6737 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
6738 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
6739 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
6740 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jiyong Park57621b22021-01-20 20:33:11 +09006741 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 +08006742}
6743
Jooyung Han643adc42020-02-27 13:50:06 +09006744func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006745 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09006746 apex {
6747 name: "myapex",
6748 key: "myapex.key",
6749 jni_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006750 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09006751 }
6752
6753 apex_key {
6754 name: "myapex.key",
6755 public_key: "testkey.avbpubkey",
6756 private_key: "testkey.pem",
6757 }
6758
6759 cc_library {
6760 name: "mylib",
6761 srcs: ["mylib.cpp"],
6762 shared_libs: ["mylib2"],
6763 system_shared_libs: [],
6764 stl: "none",
6765 apex_available: [ "myapex" ],
6766 }
6767
6768 cc_library {
6769 name: "mylib2",
6770 srcs: ["mylib.cpp"],
6771 system_shared_libs: [],
6772 stl: "none",
6773 apex_available: [ "myapex" ],
6774 }
6775 `)
6776
6777 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
6778 // Notice mylib2.so (transitive dep) is not added as a jni_lib
6779 ensureEquals(t, rule.Args["opt"], "-a jniLibs mylib.so")
6780 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6781 "lib64/mylib.so",
6782 "lib64/mylib2.so",
6783 })
6784}
6785
Jooyung Han49f67012020-04-17 13:43:10 +09006786func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006787 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09006788 apex {
6789 name: "myapex",
6790 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006791 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09006792 }
6793 apex_key {
6794 name: "myapex.key",
6795 public_key: "testkey.avbpubkey",
6796 private_key: "testkey.pem",
6797 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00006798 `,
6799 android.FixtureModifyConfig(func(config android.Config) {
6800 delete(config.Targets, android.Android)
6801 config.AndroidCommonTarget = android.Target{}
6802 }),
6803 )
Jooyung Han49f67012020-04-17 13:43:10 +09006804
6805 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
6806 t.Errorf("Expected variants: %v, but got: %v", expected, got)
6807 }
6808}
6809
Jiyong Parkbd159612020-02-28 15:22:21 +09006810func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006811 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09006812 apex {
6813 name: "myapex",
6814 key: "myapex.key",
6815 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006816 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09006817 }
6818
6819 apex_key {
6820 name: "myapex.key",
6821 public_key: "testkey.avbpubkey",
6822 private_key: "testkey.pem",
6823 }
6824
6825 android_app {
6826 name: "AppFoo",
6827 srcs: ["foo/bar/MyClass.java"],
6828 sdk_version: "none",
6829 system_modules: "none",
6830 apex_available: [ "myapex" ],
6831 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006832 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09006833
Colin Crosscf371cc2020-11-13 11:48:42 -08006834 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09006835 content := bundleConfigRule.Args["content"]
6836
6837 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Jiyong Parkcfaa1642020-02-28 16:51:07 +09006838 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 +09006839}
6840
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006841func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006842 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006843 apex {
6844 name: "myapex",
6845 key: "myapex.key",
6846 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006847 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006848 }
6849
6850 apex_key {
6851 name: "myapex.key",
6852 public_key: "testkey.avbpubkey",
6853 private_key: "testkey.pem",
6854 }
6855
6856 android_app_set {
6857 name: "AppSet",
6858 set: "AppSet.apks",
6859 }`)
6860 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08006861 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07006862 content := bundleConfigRule.Args["content"]
6863 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
6864 s := mod.Rule("apexRule").Args["copy_commands"]
6865 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
6866 if len(copyCmds) != 3 {
6867 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(copyCmds), s)
6868 }
6869 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet$")
6870 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet$")
6871 ensureMatches(t, copyCmds[2], "^unzip .*-d .*/app/AppSet .*/AppSet.zip$")
6872}
6873
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006874func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01006875 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006876 apex_set {
6877 name: "myapex",
6878 filename: "foo_v2.apex",
6879 sanitized: {
6880 none: { set: "myapex.apks", },
6881 hwaddress: { set: "myapex.hwasan.apks", },
6882 },
Paul Duffin24704672021-04-06 16:09:30 +01006883 }
6884 `
6885 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006886
Paul Duffin24704672021-04-06 16:09:30 +01006887 // Check that the extractor produces the correct output file from the correct input file.
6888 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006889
Paul Duffin24704672021-04-06 16:09:30 +01006890 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
6891 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006892
Paul Duffin24704672021-04-06 16:09:30 +01006893 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
6894
6895 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01006896 m = ctx.ModuleForTests("myapex", "android_common_myapex")
6897 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01006898
6899 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07006900}
6901
Paul Duffin89f570a2021-06-16 01:42:33 +01006902func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006903 t.Helper()
6904
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006905 bp := `
6906 java_library {
6907 name: "some-updatable-apex-lib",
6908 srcs: ["a.java"],
6909 sdk_version: "current",
6910 apex_available: [
6911 "some-updatable-apex",
6912 ],
satayevabcd5972021-08-06 17:49:46 +01006913 permitted_packages: ["some.updatable.apex.lib"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006914 }
6915
6916 java_library {
6917 name: "some-non-updatable-apex-lib",
6918 srcs: ["a.java"],
6919 apex_available: [
6920 "some-non-updatable-apex",
6921 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01006922 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01006923 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01006924 }
6925
6926 bootclasspath_fragment {
6927 name: "some-non-updatable-fragment",
6928 contents: ["some-non-updatable-apex-lib"],
6929 apex_available: [
6930 "some-non-updatable-apex",
6931 ],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006932 }
6933
6934 java_library {
6935 name: "some-platform-lib",
6936 srcs: ["a.java"],
6937 sdk_version: "current",
6938 installable: true,
6939 }
6940
6941 java_library {
6942 name: "some-art-lib",
6943 srcs: ["a.java"],
6944 sdk_version: "current",
6945 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00006946 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006947 ],
6948 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01006949 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006950 }
6951
6952 apex {
6953 name: "some-updatable-apex",
6954 key: "some-updatable-apex.key",
6955 java_libs: ["some-updatable-apex-lib"],
6956 updatable: true,
6957 min_sdk_version: "current",
6958 }
6959
6960 apex {
6961 name: "some-non-updatable-apex",
6962 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01006963 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006964 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006965 }
6966
6967 apex_key {
6968 name: "some-updatable-apex.key",
6969 }
6970
6971 apex_key {
6972 name: "some-non-updatable-apex.key",
6973 }
6974
6975 apex {
Paul Duffind376f792021-01-26 11:59:35 +00006976 name: "com.android.art.debug",
6977 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01006978 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006979 updatable: true,
6980 min_sdk_version: "current",
6981 }
6982
Paul Duffinf23bc472021-04-27 12:42:20 +01006983 bootclasspath_fragment {
6984 name: "art-bootclasspath-fragment",
6985 image_name: "art",
6986 contents: ["some-art-lib"],
6987 apex_available: [
6988 "com.android.art.debug",
6989 ],
6990 }
6991
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006992 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00006993 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01006994 }
6995
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00006996 filegroup {
6997 name: "some-updatable-apex-file_contexts",
6998 srcs: [
6999 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7000 ],
7001 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007002
7003 filegroup {
7004 name: "some-non-updatable-apex-file_contexts",
7005 srcs: [
7006 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7007 ],
7008 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007009 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007010
Paul Duffin89f570a2021-06-16 01:42:33 +01007011 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007012}
7013
Paul Duffin89f570a2021-06-16 01:42:33 +01007014func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007015 t.Helper()
7016
Paul Duffin55607122021-03-30 23:32:51 +01007017 fs := android.MockFS{
7018 "a.java": nil,
7019 "a.jar": nil,
7020 "apex_manifest.json": nil,
7021 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007022 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007023 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7024 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7025 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007026 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007027 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007028
Paul Duffin55607122021-03-30 23:32:51 +01007029 errorHandler := android.FixtureExpectsNoErrors
7030 if errmsg != "" {
7031 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007032 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007033
Paul Duffin55607122021-03-30 23:32:51 +01007034 result := android.GroupFixturePreparers(
7035 cc.PrepareForTestWithCcDefaultModules,
7036 java.PrepareForTestWithHiddenApiBuildComponents,
7037 java.PrepareForTestWithJavaDefaultModules,
7038 java.PrepareForTestWithJavaSdkLibraryFiles,
7039 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007040 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007041 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007042 android.FixtureModifyMockFS(func(fs android.MockFS) {
7043 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7044 insert := ""
7045 for _, fragment := range fragments {
7046 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7047 }
7048 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7049 platform_bootclasspath {
7050 name: "platform-bootclasspath",
7051 fragments: [
7052 %s
7053 ],
7054 }
7055 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007056 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007057 }),
Paul Duffin55607122021-03-30 23:32:51 +01007058 ).
7059 ExtendWithErrorHandler(errorHandler).
7060 RunTestWithBp(t, bp)
7061
7062 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007063}
7064
Jooyung Han548640b2020-04-27 12:10:30 +09007065func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7066 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7067 apex {
7068 name: "myapex",
7069 key: "myapex.key",
7070 updatable: true,
7071 }
7072
7073 apex_key {
7074 name: "myapex.key",
7075 public_key: "testkey.avbpubkey",
7076 private_key: "testkey.pem",
7077 }
7078 `)
7079}
7080
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007081func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7082 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7083 apex {
7084 name: "myapex",
7085 key: "myapex.key",
7086 }
7087
7088 apex_key {
7089 name: "myapex.key",
7090 public_key: "testkey.avbpubkey",
7091 private_key: "testkey.pem",
7092 }
7093 `)
7094}
7095
satayevb98371c2021-06-15 16:49:50 +01007096func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7097 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7098 apex {
7099 name: "myapex",
7100 key: "myapex.key",
7101 systemserverclasspath_fragments: [
7102 "mysystemserverclasspathfragment",
7103 ],
7104 min_sdk_version: "29",
7105 updatable: true,
7106 }
7107
7108 apex_key {
7109 name: "myapex.key",
7110 public_key: "testkey.avbpubkey",
7111 private_key: "testkey.pem",
7112 }
7113
7114 java_library {
7115 name: "foo",
7116 srcs: ["b.java"],
7117 min_sdk_version: "29",
7118 installable: true,
7119 apex_available: [
7120 "myapex",
7121 ],
7122 }
7123
7124 systemserverclasspath_fragment {
7125 name: "mysystemserverclasspathfragment",
7126 generate_classpaths_proto: false,
7127 contents: [
7128 "foo",
7129 ],
7130 apex_available: [
7131 "myapex",
7132 ],
7133 }
satayevabcd5972021-08-06 17:49:46 +01007134 `,
7135 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
7136 )
satayevb98371c2021-06-15 16:49:50 +01007137}
7138
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007139func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007140 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
7141 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
7142 // modules to be included in the BootJars.
7143 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
7144 return android.GroupFixturePreparers(
7145 dexpreopt.FixtureSetBootJars(bootJars...),
7146 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7147 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7148 }),
7149 )
7150 }
7151
7152 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
7153 // same value. This can result in an invalid configuration as it allows non art apex jars to be
7154 // specified in the ArtApexJars configuration.
7155 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
7156 return android.GroupFixturePreparers(
7157 dexpreopt.FixtureSetArtBootJars(bootJars...),
7158 dexpreopt.FixtureSetBootJars(bootJars...),
7159 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7160 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7161 }),
7162 )
7163 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007164
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007165 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007166 preparer := android.GroupFixturePreparers(
7167 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
7168 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7169 )
7170 fragments := []java.ApexVariantReference{
7171 {
7172 Apex: proptools.StringPtr("com.android.art.debug"),
7173 Module: proptools.StringPtr("art-bootclasspath-fragment"),
7174 },
7175 {
7176 Apex: proptools.StringPtr("some-non-updatable-apex"),
7177 Module: proptools.StringPtr("some-non-updatable-fragment"),
7178 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007179 }
satayevabcd5972021-08-06 17:49:46 +01007180 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007181 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007182
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007183 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007184 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
7185 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01007186 preparer := android.GroupFixturePreparers(
7187 prepareSetBootJars("com.android.art.debug:some-art-lib"),
7188 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7189 )
Paul Duffin60264a02021-04-12 20:02:36 +01007190 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007191 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007192
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007193 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007194 err := `ArtApexJars expects this to be in apex "some-updatable-apex" but this is only in apexes.*"com.android.art.debug"`
Paul Duffin60264a02021-04-12 20:02:36 +01007195 // Update the dexpreopt ArtApexJars directly.
7196 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
7197 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007198 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007199
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007200 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007201 err := `ArtApexJars expects this to be in apex "some-non-updatable-apex" but this is only in apexes.*"com.android.art.debug"`
Paul Duffin60264a02021-04-12 20:02:36 +01007202 // Update the dexpreopt ArtApexJars directly.
7203 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
7204 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007205 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007206
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007207 t.Run("updatable jar from some other apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007208 err := `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
satayevabcd5972021-08-06 17:49:46 +01007209 preparer := android.GroupFixturePreparers(
7210 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
7211 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7212 )
Paul Duffin60264a02021-04-12 20:02:36 +01007213 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007214 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007215
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007216 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007217 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01007218 fragment := java.ApexVariantReference{
7219 Apex: proptools.StringPtr("some-non-updatable-apex"),
7220 Module: proptools.StringPtr("some-non-updatable-fragment"),
7221 }
7222 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007223 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007224
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007225 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007226 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007227 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7228 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007229 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007230
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007231 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01007232 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01007233 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
7234 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007235 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007236
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007237 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01007238 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01007239 // Update the dexpreopt ArtApexJars directly.
7240 preparer := prepareSetArtJars("platform:some-platform-lib")
7241 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007242 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007243
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007244 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007245 preparer := android.GroupFixturePreparers(
7246 java.FixtureConfigureBootJars("platform:some-platform-lib"),
7247 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7248 )
7249 fragments := []java.ApexVariantReference{
7250 {
7251 Apex: proptools.StringPtr("some-non-updatable-apex"),
7252 Module: proptools.StringPtr("some-non-updatable-fragment"),
7253 },
7254 }
7255 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007256 })
Paul Duffin064b70c2020-11-02 17:32:38 +00007257}
7258
7259func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007260 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00007261 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01007262 fragment := java.ApexVariantReference{
7263 Apex: proptools.StringPtr("myapex"),
7264 Module: proptools.StringPtr("my-bootclasspath-fragment"),
7265 }
7266
Paul Duffin064b70c2020-11-02 17:32:38 +00007267 testDexpreoptWithApexes(t, `
7268 prebuilt_apex {
7269 name: "myapex" ,
7270 arch: {
7271 arm64: {
7272 src: "myapex-arm64.apex",
7273 },
7274 arm: {
7275 src: "myapex-arm.apex",
7276 },
7277 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007278 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7279 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007280
Paul Duffin89f570a2021-06-16 01:42:33 +01007281 prebuilt_bootclasspath_fragment {
7282 name: "my-bootclasspath-fragment",
7283 contents: ["libfoo"],
7284 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01007285 hidden_api: {
7286 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
7287 metadata: "my-bootclasspath-fragment/metadata.csv",
7288 index: "my-bootclasspath-fragment/index.csv",
7289 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
7290 all_flags: "my-bootclasspath-fragment/all-flags.csv",
7291 },
Paul Duffin89f570a2021-06-16 01:42:33 +01007292 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007293
Paul Duffin89f570a2021-06-16 01:42:33 +01007294 java_import {
7295 name: "libfoo",
7296 jars: ["libfoo.jar"],
7297 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01007298 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007299 }
7300 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00007301 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007302}
7303
satayevd604b212021-07-21 14:23:52 +01007304func testApexPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01007305 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01007306 bp += `
7307 apex_key {
7308 name: "myapex.key",
7309 public_key: "testkey.avbpubkey",
7310 private_key: "testkey.pem",
7311 }`
Paul Duffin45338f02021-03-30 23:07:52 +01007312 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01007313 "lib1/src/A.java": nil,
7314 "lib2/src/B.java": nil,
7315 "system/sepolicy/apex/myapex-file_contexts": nil,
7316 }
7317
Paul Duffin45338f02021-03-30 23:07:52 +01007318 errorHandler := android.FixtureExpectsNoErrors
7319 if errmsg != "" {
7320 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07007321 }
Colin Crossae8600b2020-10-29 17:09:13 -07007322
Paul Duffin45338f02021-03-30 23:07:52 +01007323 android.GroupFixturePreparers(
7324 android.PrepareForTestWithAndroidBuildComponents,
7325 java.PrepareForTestWithJavaBuildComponents,
7326 PrepareForTestWithApexBuildComponents,
7327 android.PrepareForTestWithNeverallowRules(rules),
7328 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01007329 apexBootJars := make([]string, 0, len(bootJars))
7330 for _, apexBootJar := range bootJars {
7331 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01007332 }
satayevd604b212021-07-21 14:23:52 +01007333 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01007334 }),
7335 fs.AddToFixture(),
7336 ).
7337 ExtendWithErrorHandler(errorHandler).
7338 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01007339}
7340
7341func TestApexPermittedPackagesRules(t *testing.T) {
7342 testcases := []struct {
7343 name string
7344 expectedError string
7345 bp string
7346 bootJars []string
7347 modulesPackages map[string][]string
7348 }{
7349
7350 {
7351 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
7352 expectedError: "",
7353 bp: `
7354 java_library {
7355 name: "bcp_lib1",
7356 srcs: ["lib1/src/*.java"],
7357 permitted_packages: ["foo.bar"],
7358 apex_available: ["myapex"],
7359 sdk_version: "none",
7360 system_modules: "none",
7361 }
7362 java_library {
7363 name: "nonbcp_lib2",
7364 srcs: ["lib2/src/*.java"],
7365 apex_available: ["myapex"],
7366 permitted_packages: ["a.b"],
7367 sdk_version: "none",
7368 system_modules: "none",
7369 }
7370 apex {
7371 name: "myapex",
7372 key: "myapex.key",
7373 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007374 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007375 }`,
7376 bootJars: []string{"bcp_lib1"},
7377 modulesPackages: map[string][]string{
7378 "myapex": []string{
7379 "foo.bar",
7380 },
7381 },
7382 },
7383 {
7384 name: "Bootclasspath apex jar not satisfying allowed module packages.",
7385 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.`,
7386 bp: `
7387 java_library {
7388 name: "bcp_lib1",
7389 srcs: ["lib1/src/*.java"],
7390 apex_available: ["myapex"],
7391 permitted_packages: ["foo.bar"],
7392 sdk_version: "none",
7393 system_modules: "none",
7394 }
7395 java_library {
7396 name: "bcp_lib2",
7397 srcs: ["lib2/src/*.java"],
7398 apex_available: ["myapex"],
7399 permitted_packages: ["foo.bar", "bar.baz"],
7400 sdk_version: "none",
7401 system_modules: "none",
7402 }
7403 apex {
7404 name: "myapex",
7405 key: "myapex.key",
7406 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007407 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01007408 }
7409 `,
7410 bootJars: []string{"bcp_lib1", "bcp_lib2"},
7411 modulesPackages: map[string][]string{
7412 "myapex": []string{
7413 "foo.bar",
7414 },
7415 },
7416 },
7417 }
7418 for _, tc := range testcases {
7419 t.Run(tc.name, func(t *testing.T) {
7420 rules := createApexPermittedPackagesRules(tc.modulesPackages)
7421 testApexPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
7422 })
7423 }
7424}
7425
Jiyong Park62304bb2020-04-13 16:19:48 +09007426func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007427 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09007428 apex {
7429 name: "myapex",
7430 key: "myapex.key",
7431 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007432 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09007433 }
7434
7435 apex_key {
7436 name: "myapex.key",
7437 public_key: "testkey.avbpubkey",
7438 private_key: "testkey.pem",
7439 }
7440
7441 cc_library {
7442 name: "mylib",
7443 srcs: ["mylib.cpp"],
7444 system_shared_libs: [],
7445 stl: "none",
7446 stubs: {
7447 versions: ["1"],
7448 },
7449 apex_available: ["myapex"],
7450 }
7451
7452 cc_library {
7453 name: "myprivlib",
7454 srcs: ["mylib.cpp"],
7455 system_shared_libs: [],
7456 stl: "none",
7457 apex_available: ["myapex"],
7458 }
7459
7460
7461 cc_test {
7462 name: "mytest",
7463 gtest: false,
7464 srcs: ["mylib.cpp"],
7465 system_shared_libs: [],
7466 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09007467 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09007468 test_for: ["myapex"]
7469 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007470
7471 cc_library {
7472 name: "mytestlib",
7473 srcs: ["mylib.cpp"],
7474 system_shared_libs: [],
7475 shared_libs: ["mylib", "myprivlib"],
7476 stl: "none",
7477 test_for: ["myapex"],
7478 }
7479
7480 cc_benchmark {
7481 name: "mybench",
7482 srcs: ["mylib.cpp"],
7483 system_shared_libs: [],
7484 shared_libs: ["mylib", "myprivlib"],
7485 stl: "none",
7486 test_for: ["myapex"],
7487 }
Jiyong Park62304bb2020-04-13 16:19:48 +09007488 `)
7489
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007490 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007491 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007492 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7493 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7494 }
7495
7496 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09007497 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007498 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7499 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7500 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
7501}
Jiyong Park46a512f2020-12-04 18:02:13 +09007502
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007503func TestIndirectTestFor(t *testing.T) {
7504 ctx := testApex(t, `
7505 apex {
7506 name: "myapex",
7507 key: "myapex.key",
7508 native_shared_libs: ["mylib", "myprivlib"],
7509 updatable: false,
7510 }
Jiyong Park46a512f2020-12-04 18:02:13 +09007511
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007512 apex_key {
7513 name: "myapex.key",
7514 public_key: "testkey.avbpubkey",
7515 private_key: "testkey.pem",
7516 }
7517
7518 cc_library {
7519 name: "mylib",
7520 srcs: ["mylib.cpp"],
7521 system_shared_libs: [],
7522 stl: "none",
7523 stubs: {
7524 versions: ["1"],
7525 },
7526 apex_available: ["myapex"],
7527 }
7528
7529 cc_library {
7530 name: "myprivlib",
7531 srcs: ["mylib.cpp"],
7532 system_shared_libs: [],
7533 stl: "none",
7534 shared_libs: ["mylib"],
7535 apex_available: ["myapex"],
7536 }
7537
7538 cc_library {
7539 name: "mytestlib",
7540 srcs: ["mylib.cpp"],
7541 system_shared_libs: [],
7542 shared_libs: ["myprivlib"],
7543 stl: "none",
7544 test_for: ["myapex"],
7545 }
7546 `)
7547
7548 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01007549 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00007550 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
7551 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
7552 }
7553
7554 // The platform variant of mytestlib links to the platform variant of the
7555 // internal myprivlib.
7556 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
7557
7558 // The platform variant of myprivlib links to the platform variant of mylib
7559 // and bypasses its stubs.
7560 ensureLinkedLibIs("myprivlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09007561}
7562
Martin Stjernholmec009002021-03-27 15:18:31 +00007563func TestTestForForLibInOtherApex(t *testing.T) {
7564 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
7565 _ = testApex(t, `
7566 apex {
7567 name: "com.android.art",
7568 key: "myapex.key",
7569 native_shared_libs: ["mylib"],
7570 updatable: false,
7571 }
7572
7573 apex {
7574 name: "com.android.art.debug",
7575 key: "myapex.key",
7576 native_shared_libs: ["mylib", "mytestlib"],
7577 updatable: false,
7578 }
7579
7580 apex_key {
7581 name: "myapex.key",
7582 public_key: "testkey.avbpubkey",
7583 private_key: "testkey.pem",
7584 }
7585
7586 cc_library {
7587 name: "mylib",
7588 srcs: ["mylib.cpp"],
7589 system_shared_libs: [],
7590 stl: "none",
7591 stubs: {
7592 versions: ["1"],
7593 },
7594 apex_available: ["com.android.art", "com.android.art.debug"],
7595 }
7596
7597 cc_library {
7598 name: "mytestlib",
7599 srcs: ["mylib.cpp"],
7600 system_shared_libs: [],
7601 shared_libs: ["mylib"],
7602 stl: "none",
7603 apex_available: ["com.android.art.debug"],
7604 test_for: ["com.android.art"],
7605 }
7606 `,
7607 android.MockFS{
7608 "system/sepolicy/apex/com.android.art-file_contexts": nil,
7609 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
7610 }.AddToFixture())
7611}
7612
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007613// TODO(jungjw): Move this to proptools
7614func intPtr(i int) *int {
7615 return &i
7616}
7617
7618func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007619 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007620 apex_set {
7621 name: "myapex",
7622 set: "myapex.apks",
7623 filename: "foo_v2.apex",
7624 overrides: ["foo"],
7625 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007626 `,
7627 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7628 variables.Platform_sdk_version = intPtr(30)
7629 }),
7630 android.FixtureModifyConfig(func(config android.Config) {
7631 config.Targets[android.Android] = []android.Target{
7632 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
7633 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
7634 }
7635 }),
7636 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007637
Paul Duffin24704672021-04-06 16:09:30 +01007638 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007639
7640 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01007641 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007642 actual := extractedApex.Args["abis"]
7643 expected := "ARMEABI_V7A,ARM64_V8A"
7644 if actual != expected {
7645 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7646 }
7647 actual = extractedApex.Args["sdk-version"]
7648 expected = "30"
7649 if actual != expected {
7650 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
7651 }
7652
Paul Duffin6717d882021-06-15 19:09:41 +01007653 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007654 a := m.Module().(*ApexSet)
7655 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07007656 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07007657 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
7658 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
7659 }
7660}
7661
Jiyong Park7d95a512020-05-10 15:16:24 +09007662func TestNoStaticLinkingToStubsLib(t *testing.T) {
7663 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
7664 apex {
7665 name: "myapex",
7666 key: "myapex.key",
7667 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007668 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09007669 }
7670
7671 apex_key {
7672 name: "myapex.key",
7673 public_key: "testkey.avbpubkey",
7674 private_key: "testkey.pem",
7675 }
7676
7677 cc_library {
7678 name: "mylib",
7679 srcs: ["mylib.cpp"],
7680 static_libs: ["otherlib"],
7681 system_shared_libs: [],
7682 stl: "none",
7683 apex_available: [ "myapex" ],
7684 }
7685
7686 cc_library {
7687 name: "otherlib",
7688 srcs: ["mylib.cpp"],
7689 system_shared_libs: [],
7690 stl: "none",
7691 stubs: {
7692 versions: ["1", "2", "3"],
7693 },
7694 apex_available: [ "myapex" ],
7695 }
7696 `)
7697}
7698
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007699func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007700 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007701 apex {
7702 name: "myapex",
7703 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007704 updatable: false,
Jiyong Park8d6c51e2020-06-12 17:26:31 +09007705 }
7706
7707 apex_key {
7708 name: "myapex.key",
7709 public_key: "testkey.avbpubkey",
7710 private_key: "testkey.pem",
7711 }
7712
7713 prebuilt_apex {
7714 name: "myapex",
7715 prefer: true,
7716 arch: {
7717 arm64: {
7718 src: "myapex-arm64.apex",
7719 },
7720 arm: {
7721 src: "myapex-arm.apex",
7722 },
7723 },
7724 }
7725
7726 apex_set {
7727 name: "myapex_set",
7728 set: "myapex.apks",
7729 filename: "myapex_set.apex",
7730 overrides: ["myapex"],
7731 }
7732 `)
7733
7734 apexKeysText := ctx.SingletonForTests("apex_keys_text")
7735 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
7736 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 +09007737 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 +09007738}
7739
Jooyung Han938b5932020-06-20 12:47:47 +09007740func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007741 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09007742 apex {
7743 name: "myapex",
7744 key: "myapex.key",
7745 apps: ["app"],
7746 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007747 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09007748 }
7749
7750 apex_key {
7751 name: "myapex.key",
7752 public_key: "testkey.avbpubkey",
7753 private_key: "testkey.pem",
7754 }
7755
7756 android_app {
7757 name: "app",
7758 srcs: ["foo/bar/MyClass.java"],
7759 package_name: "foo",
7760 sdk_version: "none",
7761 system_modules: "none",
7762 apex_available: [ "myapex" ],
7763 }
7764 `, withFiles(map[string][]byte{
7765 "sub/Android.bp": []byte(`
7766 override_apex {
7767 name: "override_myapex",
7768 base: "myapex",
7769 apps: ["override_app"],
7770 allowed_files: ":allowed",
7771 }
7772 // Overridable "path" property should be referenced indirectly
7773 filegroup {
7774 name: "allowed",
7775 srcs: ["allowed.txt"],
7776 }
7777 override_android_app {
7778 name: "override_app",
7779 base: "app",
7780 package_name: "bar",
7781 }
7782 `),
7783 }))
7784
7785 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
7786 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
7787 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7788 }
7789
7790 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
7791 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
7792 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
7793 }
7794}
7795
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007796func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007797 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007798 apex {
7799 name: "myapex",
7800 key: "myapex.key",
7801 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007802 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007803 }
7804
7805 apex_key {
7806 name: "myapex.key",
7807 public_key: "testkey.avbpubkey",
7808 private_key: "testkey.pem",
7809 }
7810
7811 cc_library {
7812 name: "mylib",
7813 srcs: ["mylib.cpp"],
7814 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007815 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007816 },
7817 apex_available: ["myapex"],
7818 }
7819
7820 cc_prebuilt_library_shared {
7821 name: "mylib",
7822 prefer: false,
7823 srcs: ["prebuilt.so"],
7824 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07007825 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01007826 },
7827 apex_available: ["myapex"],
7828 }
7829 `)
7830}
7831
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007832func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007833 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007834 apex {
7835 name: "myapex",
7836 key: "myapex.key",
7837 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007838 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007839 }
7840 apex_key {
7841 name: "myapex.key",
7842 public_key: "testkey.avbpubkey",
7843 private_key: "testkey.pem",
7844 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007845 `,
7846 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7847 variables.CompressedApex = proptools.BoolPtr(true)
7848 }),
7849 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007850
7851 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
7852 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
7853
7854 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
7855 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
7856
7857 // Make sure output of bundle is .capex
7858 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
7859 ensureContains(t, ab.outputFile.String(), "myapex.capex")
7860
7861 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07007862 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00007863 var builder strings.Builder
7864 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7865 androidMk := builder.String()
7866 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
7867}
7868
Martin Stjernholm2856c662020-12-02 15:03:42 +00007869func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007870 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00007871 apex {
7872 name: "myapex",
7873 key: "myapex.key",
7874 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007875 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00007876 }
7877
7878 apex_key {
7879 name: "myapex.key",
7880 public_key: "testkey.avbpubkey",
7881 private_key: "testkey.pem",
7882 }
7883
7884 cc_library {
7885 name: "mylib",
7886 srcs: ["mylib.cpp"],
7887 apex_available: ["myapex"],
7888 shared_libs: ["otherlib"],
7889 system_shared_libs: [],
7890 }
7891
7892 cc_library {
7893 name: "otherlib",
7894 srcs: ["mylib.cpp"],
7895 stubs: {
7896 versions: ["current"],
7897 },
7898 }
7899
7900 cc_prebuilt_library_shared {
7901 name: "otherlib",
7902 prefer: true,
7903 srcs: ["prebuilt.so"],
7904 stubs: {
7905 versions: ["current"],
7906 },
7907 }
7908 `)
7909
7910 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007911 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00007912 var builder strings.Builder
7913 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
7914 androidMk := builder.String()
7915
7916 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
7917 // a thing there.
7918 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES += otherlib\n")
7919}
7920
Jiyong Parke3867542020-12-03 17:28:25 +09007921func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007922 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09007923 apex {
7924 name: "myapex",
7925 key: "myapex.key",
7926 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007927 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09007928 }
7929
7930 apex_key {
7931 name: "myapex.key",
7932 public_key: "testkey.avbpubkey",
7933 private_key: "testkey.pem",
7934 }
7935
7936 cc_library {
7937 name: "mylib",
7938 srcs: ["mylib.cpp"],
7939 system_shared_libs: [],
7940 stl: "none",
7941 apex_available: ["myapex"],
7942 shared_libs: ["mylib2"],
7943 target: {
7944 apex: {
7945 exclude_shared_libs: ["mylib2"],
7946 },
7947 },
7948 }
7949
7950 cc_library {
7951 name: "mylib2",
7952 srcs: ["mylib.cpp"],
7953 system_shared_libs: [],
7954 stl: "none",
7955 }
7956 `)
7957
7958 // Check if mylib is linked to mylib2 for the non-apex target
7959 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
7960 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
7961
7962 // Make sure that the link doesn't occur for the apex target
7963 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
7964 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
7965
7966 // It shouldn't appear in the copy cmd as well.
7967 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
7968 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
7969}
7970
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007971func TestPrebuiltStubLibDep(t *testing.T) {
7972 bpBase := `
7973 apex {
7974 name: "myapex",
7975 key: "myapex.key",
7976 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007977 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007978 }
7979 apex_key {
7980 name: "myapex.key",
7981 public_key: "testkey.avbpubkey",
7982 private_key: "testkey.pem",
7983 }
7984 cc_library {
7985 name: "mylib",
7986 srcs: ["mylib.cpp"],
7987 apex_available: ["myapex"],
7988 shared_libs: ["stublib"],
7989 system_shared_libs: [],
7990 }
7991 apex {
7992 name: "otherapex",
7993 enabled: %s,
7994 key: "myapex.key",
7995 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007996 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09007997 }
7998 `
7999
8000 stublibSourceBp := `
8001 cc_library {
8002 name: "stublib",
8003 srcs: ["mylib.cpp"],
8004 apex_available: ["otherapex"],
8005 system_shared_libs: [],
8006 stl: "none",
8007 stubs: {
8008 versions: ["1"],
8009 },
8010 }
8011 `
8012
8013 stublibPrebuiltBp := `
8014 cc_prebuilt_library_shared {
8015 name: "stublib",
8016 srcs: ["prebuilt.so"],
8017 apex_available: ["otherapex"],
8018 stubs: {
8019 versions: ["1"],
8020 },
8021 %s
8022 }
8023 `
8024
8025 tests := []struct {
8026 name string
8027 stublibBp string
8028 usePrebuilt bool
8029 modNames []string // Modules to collect AndroidMkEntries for
8030 otherApexEnabled []string
8031 }{
8032 {
8033 name: "only_source",
8034 stublibBp: stublibSourceBp,
8035 usePrebuilt: false,
8036 modNames: []string{"stublib"},
8037 otherApexEnabled: []string{"true", "false"},
8038 },
8039 {
8040 name: "source_preferred",
8041 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
8042 usePrebuilt: false,
8043 modNames: []string{"stublib", "prebuilt_stublib"},
8044 otherApexEnabled: []string{"true", "false"},
8045 },
8046 {
8047 name: "prebuilt_preferred",
8048 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
8049 usePrebuilt: true,
8050 modNames: []string{"stublib", "prebuilt_stublib"},
8051 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8052 },
8053 {
8054 name: "only_prebuilt",
8055 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
8056 usePrebuilt: true,
8057 modNames: []string{"stublib"},
8058 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8059 },
8060 }
8061
8062 for _, test := range tests {
8063 t.Run(test.name, func(t *testing.T) {
8064 for _, otherApexEnabled := range test.otherApexEnabled {
8065 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008066 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008067
8068 type modAndMkEntries struct {
8069 mod *cc.Module
8070 mkEntries android.AndroidMkEntries
8071 }
8072 entries := []*modAndMkEntries{}
8073
8074 // Gather shared lib modules that are installable
8075 for _, modName := range test.modNames {
8076 for _, variant := range ctx.ModuleVariantsForTests(modName) {
8077 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
8078 continue
8079 }
8080 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08008081 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008082 continue
8083 }
Colin Crossaa255532020-07-03 13:18:24 -07008084 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008085 if ent.Disabled {
8086 continue
8087 }
8088 entries = append(entries, &modAndMkEntries{
8089 mod: mod,
8090 mkEntries: ent,
8091 })
8092 }
8093 }
8094 }
8095
8096 var entry *modAndMkEntries = nil
8097 for _, ent := range entries {
8098 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
8099 if entry != nil {
8100 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
8101 } else {
8102 entry = ent
8103 }
8104 }
8105 }
8106
8107 if entry == nil {
8108 t.Errorf("AndroidMk entry for \"stublib\" missing")
8109 } else {
8110 isPrebuilt := entry.mod.Prebuilt() != nil
8111 if isPrebuilt != test.usePrebuilt {
8112 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
8113 }
8114 if !entry.mod.IsStubs() {
8115 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
8116 }
8117 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
8118 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
8119 }
Jiyong Park892a98f2020-12-14 09:20:00 +09008120 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09008121 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09008122 if !android.InList(expected, cflags) {
8123 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
8124 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008125 }
8126 })
8127 }
8128 })
8129 }
8130}
8131
Martin Stjernholmdf298b32021-05-21 20:57:29 +01008132func TestHostApexInHostOnlyBuild(t *testing.T) {
8133 testApex(t, `
8134 apex {
8135 name: "myapex",
8136 host_supported: true,
8137 key: "myapex.key",
8138 updatable: false,
8139 payload_type: "zip",
8140 }
8141 apex_key {
8142 name: "myapex.key",
8143 public_key: "testkey.avbpubkey",
8144 private_key: "testkey.pem",
8145 }
8146 `,
8147 android.FixtureModifyConfig(func(config android.Config) {
8148 // We may not have device targets in all builds, e.g. in
8149 // prebuilts/build-tools/build-prebuilts.sh
8150 config.Targets[android.Android] = []android.Target{}
8151 }))
8152}
8153
Colin Crossc33e5212021-05-25 18:16:02 -07008154func TestApexJavaCoverage(t *testing.T) {
8155 bp := `
8156 apex {
8157 name: "myapex",
8158 key: "myapex.key",
8159 java_libs: ["mylib"],
8160 bootclasspath_fragments: ["mybootclasspathfragment"],
8161 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
8162 updatable: false,
8163 }
8164
8165 apex_key {
8166 name: "myapex.key",
8167 public_key: "testkey.avbpubkey",
8168 private_key: "testkey.pem",
8169 }
8170
8171 java_library {
8172 name: "mylib",
8173 srcs: ["mylib.java"],
8174 apex_available: ["myapex"],
8175 compile_dex: true,
8176 }
8177
8178 bootclasspath_fragment {
8179 name: "mybootclasspathfragment",
8180 contents: ["mybootclasspathlib"],
8181 apex_available: ["myapex"],
8182 }
8183
8184 java_library {
8185 name: "mybootclasspathlib",
8186 srcs: ["mybootclasspathlib.java"],
8187 apex_available: ["myapex"],
8188 compile_dex: true,
8189 }
8190
8191 systemserverclasspath_fragment {
8192 name: "mysystemserverclasspathfragment",
8193 contents: ["mysystemserverclasspathlib"],
8194 apex_available: ["myapex"],
8195 }
8196
8197 java_library {
8198 name: "mysystemserverclasspathlib",
8199 srcs: ["mysystemserverclasspathlib.java"],
8200 apex_available: ["myapex"],
8201 compile_dex: true,
8202 }
8203 `
8204
8205 result := android.GroupFixturePreparers(
8206 PrepareForTestWithApexBuildComponents,
8207 prepareForTestWithMyapex,
8208 java.PrepareForTestWithJavaDefaultModules,
8209 android.PrepareForTestWithAndroidBuildComponents,
8210 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01008211 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
8212 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Colin Crossc33e5212021-05-25 18:16:02 -07008213 android.FixtureMergeEnv(map[string]string{
8214 "EMMA_INSTRUMENT": "true",
8215 }),
8216 ).RunTest(t)
8217
8218 // Make sure jacoco ran on both mylib and mybootclasspathlib
8219 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8220 t.Errorf("Failed to find jacoco rule for mylib")
8221 }
8222 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8223 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
8224 }
8225 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
8226 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
8227 }
8228}
8229
Jiyong Park192600a2021-08-03 07:52:17 +00008230func TestProhibitStaticExecutable(t *testing.T) {
8231 testApexError(t, `executable mybin is static`, `
8232 apex {
8233 name: "myapex",
8234 key: "myapex.key",
8235 binaries: ["mybin"],
8236 min_sdk_version: "29",
8237 }
8238
8239 apex_key {
8240 name: "myapex.key",
8241 public_key: "testkey.avbpubkey",
8242 private_key: "testkey.pem",
8243 }
8244
8245 cc_binary {
8246 name: "mybin",
8247 srcs: ["mylib.cpp"],
8248 relative_install_path: "foo/bar",
8249 static_executable: true,
8250 system_shared_libs: [],
8251 stl: "none",
8252 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09008253 min_sdk_version: "29",
8254 }
8255 `)
8256
8257 testApexError(t, `executable mybin.rust is static`, `
8258 apex {
8259 name: "myapex",
8260 key: "myapex.key",
8261 binaries: ["mybin.rust"],
8262 min_sdk_version: "29",
8263 }
8264
8265 apex_key {
8266 name: "myapex.key",
8267 public_key: "testkey.avbpubkey",
8268 private_key: "testkey.pem",
8269 }
8270
8271 rust_binary {
8272 name: "mybin.rust",
8273 srcs: ["foo.rs"],
8274 static_executable: true,
8275 apex_available: ["myapex"],
8276 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00008277 }
8278 `)
8279}
8280
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07008281func TestMain(m *testing.M) {
Paul Duffin37ba3442021-03-29 00:21:08 +01008282 os.Exit(m.Run())
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07008283}