blob: 31b7ef7ff08d146b49a0e6039920431b45d09111 [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"
Jooyung Han39edb6c2019-11-06 16:53:07 +090019 "path"
Paul Duffin37856732021-02-26 14:24:15 +000020 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010022 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090023 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090024 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090025 "strings"
26 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090027
Kiyoung Kim487689e2022-07-26 09:48:22 +090028 "github.com/google/blueprint"
Jiyong Parkda6eb592018-12-19 17:12:36 +090029 "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
Albert Martineefabcf2022-03-21 20:11:16 +0000116func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
117 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
118 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
119 })
120}
121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withBinder32bit = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Binder32bit = proptools.BoolPtr(true)
125 },
126)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900127
Paul Duffin810f33d2021-03-09 14:12:32 +0000128var withUnbundledBuild = android.FixtureModifyProductVariables(
129 func(variables android.FixtureProductVariables) {
130 variables.Unbundled_build = proptools.BoolPtr(true)
131 },
132)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900133
Paul Duffin284165a2021-03-29 01:50:31 +0100134// Legacy preparer used for running tests within the apex package.
135//
136// This includes everything that was needed to run any test in the apex package prior to the
137// introduction of the test fixtures. Tests that are being converted to use fixtures directly
138// rather than through the testApex...() methods should avoid using this and instead use the
139// various preparers directly, using android.GroupFixturePreparers(...) to group them when
140// necessary.
141//
142// deprecated
143var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000144 // General preparers in alphabetical order as test infrastructure will enforce correct
145 // registration order.
146 android.PrepareForTestWithAndroidBuildComponents,
147 bpf.PrepareForTestWithBpf,
148 cc.PrepareForTestWithCcBuildComponents,
149 java.PrepareForTestWithJavaDefaultModules,
150 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
151 rust.PrepareForTestWithRustDefaultModules,
152 sh.PrepareForTestWithShBuildComponents,
153
154 PrepareForTestWithApexBuildComponents,
155
156 // Additional apex test specific preparers.
157 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
158 filegroup {
159 name: "myapex-file_contexts",
160 srcs: [
161 "apex/myapex-file_contexts",
162 ],
163 }
164 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000165 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000167 "a.java": nil,
168 "PrebuiltAppFoo.apk": nil,
169 "PrebuiltAppFooPriv.apk": nil,
170 "apex_manifest.json": nil,
171 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000172 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
173 "system/sepolicy/apex/myapex2-file_contexts": nil,
174 "system/sepolicy/apex/otherapex-file_contexts": nil,
175 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
176 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700177 "mylib.cpp": nil,
178 "mytest.cpp": nil,
179 "mytest1.cpp": nil,
180 "mytest2.cpp": nil,
181 "mytest3.cpp": nil,
182 "myprebuilt": nil,
183 "my_include": nil,
184 "foo/bar/MyClass.java": nil,
185 "prebuilt.jar": nil,
186 "prebuilt.so": nil,
187 "vendor/foo/devkeys/test.x509.pem": nil,
188 "vendor/foo/devkeys/test.pk8": nil,
189 "testkey.x509.pem": nil,
190 "testkey.pk8": nil,
191 "testkey.override.x509.pem": nil,
192 "testkey.override.pk8": nil,
193 "vendor/foo/devkeys/testkey.avbpubkey": nil,
194 "vendor/foo/devkeys/testkey.pem": nil,
195 "NOTICE": nil,
196 "custom_notice": nil,
197 "custom_notice_for_static_lib": nil,
198 "testkey2.avbpubkey": nil,
199 "testkey2.pem": nil,
200 "myapex-arm64.apex": nil,
201 "myapex-arm.apex": nil,
202 "myapex.apks": nil,
203 "frameworks/base/api/current.txt": nil,
204 "framework/aidl/a.aidl": nil,
205 "dummy.txt": nil,
206 "baz": nil,
207 "bar/baz": nil,
208 "testdata/baz": nil,
209 "AppSet.apks": nil,
210 "foo.rs": nil,
211 "libfoo.jar": nil,
212 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000213 },
214 ),
215
216 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
217 variables.DeviceVndkVersion = proptools.StringPtr("current")
218 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
219 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
220 variables.Platform_sdk_codename = proptools.StringPtr("Q")
221 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000222 // "Tiramisu" needs to be in the next line for compatibility with soong code,
223 // not because of these tests specifically (it's not used by the tests)
224 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900225 variables.Platform_vndk_version = proptools.StringPtr("29")
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000226 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000227 }),
228)
229
Paul Duffin52bfaa42021-03-23 23:40:12 +0000230var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
231 "system/sepolicy/apex/myapex-file_contexts": nil,
232})
233
Jooyung Han643adc42020-02-27 13:50:06 +0900234// ensure that 'result' equals 'expected'
235func ensureEquals(t *testing.T, result string, expected string) {
236 t.Helper()
237 if result != expected {
238 t.Errorf("%q != %q", expected, result)
239 }
240}
241
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242// ensure that 'result' contains 'expected'
243func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900244 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245 if !strings.Contains(result, expected) {
246 t.Errorf("%q is not found in %q", expected, result)
247 }
248}
249
Liz Kammer5bd365f2020-05-27 15:15:11 -0700250// ensure that 'result' contains 'expected' exactly one time
251func ensureContainsOnce(t *testing.T, result string, expected string) {
252 t.Helper()
253 count := strings.Count(result, expected)
254 if count != 1 {
255 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
256 }
257}
258
Jiyong Park25fc6a92018-11-18 18:02:45 +0900259// ensures that 'result' does not contain 'notExpected'
260func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900261 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 if strings.Contains(result, notExpected) {
263 t.Errorf("%q is found in %q", notExpected, result)
264 }
265}
266
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700267func ensureMatches(t *testing.T, result string, expectedRex string) {
268 ok, err := regexp.MatchString(expectedRex, result)
269 if err != nil {
270 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
271 return
272 }
273 if !ok {
274 t.Errorf("%s does not match regular expession %s", result, expectedRex)
275 }
276}
277
Jiyong Park25fc6a92018-11-18 18:02:45 +0900278func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if !android.InList(expected, result) {
281 t.Errorf("%q is not found in %v", expected, result)
282 }
283}
284
285func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900286 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 if android.InList(notExpected, result) {
288 t.Errorf("%q is found in %v", notExpected, result)
289 }
290}
291
Jooyung Hane1633032019-08-01 17:41:43 +0900292func ensureListEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) > 0 {
295 t.Errorf("%q is expected to be empty", result)
296 }
297}
298
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000299func ensureListNotEmpty(t *testing.T, result []string) {
300 t.Helper()
301 if len(result) == 0 {
302 t.Errorf("%q is expected to be not empty", result)
303 }
304}
305
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306// Minimal test
307func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800308 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900309 apex_defaults {
310 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900311 manifest: ":myapex.manifest",
312 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900313 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900314 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900315 native_shared_libs: [
316 "mylib",
317 "libfoo.ffi",
318 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900319 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800320 multilib: {
321 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900322 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800323 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900324 },
Jiyong Park77acec62020-06-01 21:39:15 +0900325 java_libs: [
326 "myjar",
327 "myjar_dex",
328 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000329 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 }
331
Jiyong Park30ca9372019-02-07 16:27:23 +0900332 apex {
333 name: "myapex",
334 defaults: ["myapex-defaults"],
335 }
336
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337 apex_key {
338 name: "myapex.key",
339 public_key: "testkey.avbpubkey",
340 private_key: "testkey.pem",
341 }
342
Jiyong Park809bb722019-02-13 21:33:49 +0900343 filegroup {
344 name: "myapex.manifest",
345 srcs: ["apex_manifest.json"],
346 }
347
348 filegroup {
349 name: "myapex.androidmanifest",
350 srcs: ["AndroidManifest.xml"],
351 }
352
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 cc_library {
354 name: "mylib",
355 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900356 shared_libs: [
357 "mylib2",
358 "libbar.ffi",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 system_shared_libs: [],
361 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000362 // TODO: remove //apex_available:platform
363 apex_available: [
364 "//apex_available:platform",
365 "myapex",
366 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900367 }
368
Alex Light3d673592019-01-18 14:37:31 -0800369 cc_binary {
370 name: "foo",
371 srcs: ["mylib.cpp"],
372 compile_multilib: "both",
373 multilib: {
374 lib32: {
375 suffix: "32",
376 },
377 lib64: {
378 suffix: "64",
379 },
380 },
381 symlinks: ["foo_link_"],
382 symlink_preferred_arch: true,
383 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800384 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700385 apex_available: [ "myapex", "com.android.gki.*" ],
386 }
387
Jiyong Park99644e92020-11-17 22:21:02 +0900388 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000389 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900390 srcs: ["foo.rs"],
391 rlibs: ["libfoo.rlib.rust"],
392 dylibs: ["libfoo.dylib.rust"],
393 apex_available: ["myapex"],
394 }
395
396 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000397 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900398 srcs: ["foo.rs"],
399 crate_name: "foo",
400 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900401 shared_libs: ["libfoo.shared_from_rust"],
402 }
403
404 cc_library_shared {
405 name: "libfoo.shared_from_rust",
406 srcs: ["mylib.cpp"],
407 system_shared_libs: [],
408 stl: "none",
409 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900410 }
411
412 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000413 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900414 srcs: ["foo.rs"],
415 crate_name: "foo",
416 apex_available: ["myapex"],
417 }
418
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900419 rust_ffi_shared {
420 name: "libfoo.ffi",
421 srcs: ["foo.rs"],
422 crate_name: "foo",
423 apex_available: ["myapex"],
424 }
425
426 rust_ffi_shared {
427 name: "libbar.ffi",
428 srcs: ["foo.rs"],
429 crate_name: "bar",
430 apex_available: ["myapex"],
431 }
432
Yifan Hongd22a84a2020-07-28 17:37:46 -0700433 apex {
434 name: "com.android.gki.fake",
435 binaries: ["foo"],
436 key: "myapex.key",
437 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000438 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800439 }
440
Paul Duffindddd5462020-04-07 15:25:44 +0100441 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900442 name: "mylib2",
443 srcs: ["mylib.cpp"],
444 system_shared_libs: [],
445 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900446 static_libs: ["libstatic"],
447 // TODO: remove //apex_available:platform
448 apex_available: [
449 "//apex_available:platform",
450 "myapex",
451 ],
452 }
453
Paul Duffindddd5462020-04-07 15:25:44 +0100454 cc_prebuilt_library_shared {
455 name: "mylib2",
456 srcs: ["prebuilt.so"],
457 // TODO: remove //apex_available:platform
458 apex_available: [
459 "//apex_available:platform",
460 "myapex",
461 ],
462 }
463
Jiyong Park9918e1a2020-03-17 19:16:40 +0900464 cc_library_static {
465 name: "libstatic",
466 srcs: ["mylib.cpp"],
467 system_shared_libs: [],
468 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000469 // TODO: remove //apex_available:platform
470 apex_available: [
471 "//apex_available:platform",
472 "myapex",
473 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900474 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475
476 java_library {
477 name: "myjar",
478 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900479 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900480 sdk_version: "none",
481 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900483 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000484 // TODO: remove //apex_available:platform
485 apex_available: [
486 "//apex_available:platform",
487 "myapex",
488 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900489 }
490
Jiyong Park77acec62020-06-01 21:39:15 +0900491 dex_import {
492 name: "myjar_dex",
493 jars: ["prebuilt.jar"],
494 apex_available: [
495 "//apex_available:platform",
496 "myapex",
497 ],
498 }
499
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 java_library {
501 name: "myotherjar",
502 srcs: ["foo/bar/MyClass.java"],
503 sdk_version: "none",
504 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900505 // TODO: remove //apex_available:platform
506 apex_available: [
507 "//apex_available:platform",
508 "myapex",
509 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900510 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900511
512 java_library {
513 name: "mysharedjar",
514 srcs: ["foo/bar/MyClass.java"],
515 sdk_version: "none",
516 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900517 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900518 `)
519
Paul Duffina71a67a2021-03-29 00:42:57 +0100520 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900521
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900522 // Make sure that Android.mk is created
523 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700524 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900525 var builder strings.Builder
526 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
527
528 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000529 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900530 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
531
Jiyong Park42cca6c2019-04-01 11:15:50 +0900532 optFlags := apexRule.Args["opt_flags"]
533 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700534 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100535 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900536
Jiyong Park25fc6a92018-11-18 18:02:45 +0900537 copyCmds := apexRule.Args["copy_commands"]
538
539 // Ensure that main rule creates an output
540 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
541
542 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700543 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
544 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
545 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900546 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900547 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900548
549 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700550 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
551 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900552 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
553 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900554 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900556
557 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800558 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
559 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900560 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900561 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900562 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900563 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
564 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900566 // .. but not for java libs
567 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900568 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800569
Colin Cross7113d202019-11-20 16:39:12 -0800570 // Ensure that the platform variant ends with _shared or _common
571 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
572 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900573 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
574 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900575 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
576
577 // Ensure that dynamic dependency to java libs are not included
578 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800579
580 // Ensure that all symlinks are present.
581 found_foo_link_64 := false
582 found_foo := false
583 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900584 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800585 if strings.HasSuffix(cmd, "bin/foo") {
586 found_foo = true
587 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
588 found_foo_link_64 = true
589 }
590 }
591 }
592 good := found_foo && found_foo_link_64
593 if !good {
594 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
595 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900596
Artur Satayeva8bd1132020-04-27 18:07:06 +0100597 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100598 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100599 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
600 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
601 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100602
603 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100605 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
606 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
607 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800608}
609
Jooyung Hanf21c7972019-12-16 22:32:06 +0900610func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800611 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900612 apex_defaults {
613 name: "myapex-defaults",
614 key: "myapex.key",
615 prebuilts: ["myetc"],
616 native_shared_libs: ["mylib"],
617 java_libs: ["myjar"],
618 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900619 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800620 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000621 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900622 }
623
624 prebuilt_etc {
625 name: "myetc",
626 src: "myprebuilt",
627 }
628
629 apex {
630 name: "myapex",
631 defaults: ["myapex-defaults"],
632 }
633
634 apex_key {
635 name: "myapex.key",
636 public_key: "testkey.avbpubkey",
637 private_key: "testkey.pem",
638 }
639
640 cc_library {
641 name: "mylib",
642 system_shared_libs: [],
643 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000644 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900645 }
646
647 java_library {
648 name: "myjar",
649 srcs: ["foo/bar/MyClass.java"],
650 sdk_version: "none",
651 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000652 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900653 }
654
655 android_app {
656 name: "AppFoo",
657 srcs: ["foo/bar/MyClass.java"],
658 sdk_version: "none",
659 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000660 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900661 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900662
663 runtime_resource_overlay {
664 name: "rro",
665 theme: "blue",
666 }
667
markchien2f59ec92020-09-02 16:23:38 +0800668 bpf {
669 name: "bpf",
670 srcs: ["bpf.c", "bpf2.c"],
671 }
672
Ken Chenfad7f9d2021-11-10 22:02:57 +0800673 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800674 name: "netdTest",
675 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800676 sub_dir: "netd",
677 }
678
Jooyung Hanf21c7972019-12-16 22:32:06 +0900679 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000680 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900681 "etc/myetc",
682 "javalib/myjar.jar",
683 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000684 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900685 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800686 "etc/bpf/bpf.o",
687 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800688 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900689 })
690}
691
Jooyung Han01a3ee22019-11-02 02:52:25 +0900692func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800693 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900694 apex {
695 name: "myapex",
696 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000697 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900698 }
699
700 apex_key {
701 name: "myapex.key",
702 public_key: "testkey.avbpubkey",
703 private_key: "testkey.pem",
704 }
705 `)
706
707 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900708 args := module.Rule("apexRule").Args
709 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
710 t.Error("manifest should be apex_manifest.pb, but " + manifest)
711 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900712}
713
Liz Kammer4854a7d2021-05-27 14:28:27 -0400714func TestApexManifestMinSdkVersion(t *testing.T) {
715 ctx := testApex(t, `
716 apex_defaults {
717 name: "my_defaults",
718 key: "myapex.key",
719 product_specific: true,
720 file_contexts: ":my-file-contexts",
721 updatable: false,
722 }
723 apex {
724 name: "myapex_30",
725 min_sdk_version: "30",
726 defaults: ["my_defaults"],
727 }
728
729 apex {
730 name: "myapex_current",
731 min_sdk_version: "current",
732 defaults: ["my_defaults"],
733 }
734
735 apex {
736 name: "myapex_none",
737 defaults: ["my_defaults"],
738 }
739
740 apex_key {
741 name: "myapex.key",
742 public_key: "testkey.avbpubkey",
743 private_key: "testkey.pem",
744 }
745
746 filegroup {
747 name: "my-file-contexts",
748 srcs: ["product_specific_file_contexts"],
749 }
750 `, withFiles(map[string][]byte{
751 "product_specific_file_contexts": nil,
752 }), android.FixtureModifyProductVariables(
753 func(variables android.FixtureProductVariables) {
754 variables.Unbundled_build = proptools.BoolPtr(true)
755 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
756 }), android.FixtureMergeEnv(map[string]string{
757 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
758 }))
759
760 testCases := []struct {
761 module string
762 minSdkVersion string
763 }{
764 {
765 module: "myapex_30",
766 minSdkVersion: "30",
767 },
768 {
769 module: "myapex_current",
770 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
771 },
772 {
773 module: "myapex_none",
774 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
775 },
776 }
777 for _, tc := range testCases {
778 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
779 args := module.Rule("apexRule").Args
780 optFlags := args["opt_flags"]
781 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
782 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
783 }
784 }
785}
786
Jooyung Hanaf730952023-02-28 14:13:38 +0900787func TestFileContexts(t *testing.T) {
788 for _, useFileContextsAsIs := range []bool{true, false} {
789 prop := ""
790 if useFileContextsAsIs {
791 prop = "use_file_contexts_as_is: true,\n"
792 }
793 ctx := testApex(t, `
794 apex {
795 name: "myapex",
796 key: "myapex.key",
797 file_contexts: "file_contexts",
798 updatable: false,
799 vendor: true,
800 `+prop+`
801 }
802
803 apex_key {
804 name: "myapex.key",
805 public_key: "testkey.avbpubkey",
806 private_key: "testkey.pem",
807 }
808 `, withFiles(map[string][]byte{
809 "file_contexts": nil,
810 }))
811
812 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("file_contexts")
813 forceLabellingCommand := "apex_manifest\\\\.pb u:object_r:system_file:s0"
814 if useFileContextsAsIs {
815 android.AssertStringDoesNotContain(t, "should force-label",
816 rule.RuleParams.Command, forceLabellingCommand)
817 } else {
818 android.AssertStringDoesContain(t, "shouldn't force-label",
819 rule.RuleParams.Command, forceLabellingCommand)
820 }
821 }
822}
823
Alex Light5098a612018-11-29 17:12:15 -0800824func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800825 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800826 apex {
827 name: "myapex",
828 key: "myapex.key",
829 payload_type: "zip",
830 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000831 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800832 }
833
834 apex_key {
835 name: "myapex.key",
836 public_key: "testkey.avbpubkey",
837 private_key: "testkey.pem",
838 }
839
840 cc_library {
841 name: "mylib",
842 srcs: ["mylib.cpp"],
843 shared_libs: ["mylib2"],
844 system_shared_libs: [],
845 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000846 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800847 }
848
849 cc_library {
850 name: "mylib2",
851 srcs: ["mylib.cpp"],
852 system_shared_libs: [],
853 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000854 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800855 }
856 `)
857
Sundong Ahnabb64432019-10-22 13:58:29 +0900858 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800859 copyCmds := zipApexRule.Args["copy_commands"]
860
861 // Ensure that main rule creates an output
862 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
863
864 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700865 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800866
867 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700868 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800869
870 // Ensure that both direct and indirect deps are copied into apex
871 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
872 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900873}
874
875func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800876 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900877 apex {
878 name: "myapex",
879 key: "myapex.key",
880 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900881 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000882 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900883 }
884
885 apex_key {
886 name: "myapex.key",
887 public_key: "testkey.avbpubkey",
888 private_key: "testkey.pem",
889 }
890
891 cc_library {
892 name: "mylib",
893 srcs: ["mylib.cpp"],
894 shared_libs: ["mylib2", "mylib3"],
895 system_shared_libs: [],
896 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000897 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900898 }
899
900 cc_library {
901 name: "mylib2",
902 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900903 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900904 system_shared_libs: [],
905 stl: "none",
906 stubs: {
907 versions: ["1", "2", "3"],
908 },
909 }
910
911 cc_library {
912 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900913 srcs: ["mylib.cpp"],
914 shared_libs: ["mylib4"],
915 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900916 stl: "none",
917 stubs: {
918 versions: ["10", "11", "12"],
919 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000920 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900921 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900922
923 cc_library {
924 name: "mylib4",
925 srcs: ["mylib.cpp"],
926 system_shared_libs: [],
927 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000928 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900929 }
Jiyong Park105dc322021-06-11 17:22:09 +0900930
931 rust_binary {
932 name: "foo.rust",
933 srcs: ["foo.rs"],
934 shared_libs: ["libfoo.shared_from_rust"],
935 prefer_rlib: true,
936 apex_available: ["myapex"],
937 }
938
939 cc_library_shared {
940 name: "libfoo.shared_from_rust",
941 srcs: ["mylib.cpp"],
942 system_shared_libs: [],
943 stl: "none",
944 stubs: {
945 versions: ["10", "11", "12"],
946 },
947 }
948
Jiyong Park25fc6a92018-11-18 18:02:45 +0900949 `)
950
Sundong Ahnabb64432019-10-22 13:58:29 +0900951 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900952 copyCmds := apexRule.Args["copy_commands"]
953
954 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800955 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900956
957 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800958 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900959
960 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800961 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900962
Colin Crossaede88c2020-08-11 12:17:01 -0700963 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900964
965 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900966 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900967 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900968 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900969
970 // 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 -0700971 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900972 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700973 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900974
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700975 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
976 // is replaced by sharing of "cFlags" in cc/builder.go.
977 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
978 // module variable representing "cflags". So it was not detected by ensureNotContains.
979 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
980 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
981 // including the original cflags's "-include mylib.h".
982 //
Jiyong Park64379952018-12-13 18:37:29 +0900983 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700984 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
985 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900986
Jiyong Park85cc35a2022-07-17 11:30:47 +0900987 // Ensure that genstub for platform-provided lib is invoked with --systemapi
988 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
989 // Ensure that genstub for apex-provided lib is invoked with --apex
990 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +0900991
Jooyung Hana57af4a2020-01-23 05:36:59 +0000992 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900993 "lib64/mylib.so",
994 "lib64/mylib3.so",
995 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +0900996 "bin/foo.rust",
997 "lib64/libc++.so", // by the implicit dependency from foo.rust
998 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900999 })
Jiyong Park105dc322021-06-11 17:22:09 +09001000
1001 // Ensure that stub dependency from a rust module is not included
1002 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1003 // The rust module is linked to the stub cc library
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001004 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustLink").Args["linkFlags"]
Jiyong Park105dc322021-06-11 17:22:09 +09001005 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1006 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +09001007
1008 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1009 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010}
1011
Jiyong Park1bc84122021-06-22 20:23:05 +09001012func TestApexCanUsePrivateApis(t *testing.T) {
1013 ctx := testApex(t, `
1014 apex {
1015 name: "myapex",
1016 key: "myapex.key",
1017 native_shared_libs: ["mylib"],
1018 binaries: ["foo.rust"],
1019 updatable: false,
1020 platform_apis: true,
1021 }
1022
1023 apex_key {
1024 name: "myapex.key",
1025 public_key: "testkey.avbpubkey",
1026 private_key: "testkey.pem",
1027 }
1028
1029 cc_library {
1030 name: "mylib",
1031 srcs: ["mylib.cpp"],
1032 shared_libs: ["mylib2"],
1033 system_shared_libs: [],
1034 stl: "none",
1035 apex_available: [ "myapex" ],
1036 }
1037
1038 cc_library {
1039 name: "mylib2",
1040 srcs: ["mylib.cpp"],
1041 cflags: ["-include mylib.h"],
1042 system_shared_libs: [],
1043 stl: "none",
1044 stubs: {
1045 versions: ["1", "2", "3"],
1046 },
1047 }
1048
1049 rust_binary {
1050 name: "foo.rust",
1051 srcs: ["foo.rs"],
1052 shared_libs: ["libfoo.shared_from_rust"],
1053 prefer_rlib: true,
1054 apex_available: ["myapex"],
1055 }
1056
1057 cc_library_shared {
1058 name: "libfoo.shared_from_rust",
1059 srcs: ["mylib.cpp"],
1060 system_shared_libs: [],
1061 stl: "none",
1062 stubs: {
1063 versions: ["10", "11", "12"],
1064 },
1065 }
1066 `)
1067
1068 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1069 copyCmds := apexRule.Args["copy_commands"]
1070
1071 // Ensure that indirect stubs dep is not included
1072 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1073 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1074
1075 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1076 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001077 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001078 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1079 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001080 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustLink").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001081 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1082 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1083}
1084
Colin Cross7812fd32020-09-25 12:35:10 -07001085func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1086 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001087 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001088 apex {
1089 name: "myapex",
1090 key: "myapex.key",
1091 native_shared_libs: ["mylib", "mylib3"],
1092 min_sdk_version: "29",
1093 }
1094
1095 apex_key {
1096 name: "myapex.key",
1097 public_key: "testkey.avbpubkey",
1098 private_key: "testkey.pem",
1099 }
1100
1101 cc_library {
1102 name: "mylib",
1103 srcs: ["mylib.cpp"],
1104 shared_libs: ["mylib2", "mylib3"],
1105 system_shared_libs: [],
1106 stl: "none",
1107 apex_available: [ "myapex" ],
1108 min_sdk_version: "28",
1109 }
1110
1111 cc_library {
1112 name: "mylib2",
1113 srcs: ["mylib.cpp"],
1114 cflags: ["-include mylib.h"],
1115 system_shared_libs: [],
1116 stl: "none",
1117 stubs: {
1118 versions: ["28", "29", "30", "current"],
1119 },
1120 min_sdk_version: "28",
1121 }
1122
1123 cc_library {
1124 name: "mylib3",
1125 srcs: ["mylib.cpp"],
1126 shared_libs: ["mylib4"],
1127 system_shared_libs: [],
1128 stl: "none",
1129 stubs: {
1130 versions: ["28", "29", "30", "current"],
1131 },
1132 apex_available: [ "myapex" ],
1133 min_sdk_version: "28",
1134 }
1135
1136 cc_library {
1137 name: "mylib4",
1138 srcs: ["mylib.cpp"],
1139 system_shared_libs: [],
1140 stl: "none",
1141 apex_available: [ "myapex" ],
1142 min_sdk_version: "28",
1143 }
1144 `)
1145
1146 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1147 copyCmds := apexRule.Args["copy_commands"]
1148
1149 // Ensure that direct non-stubs dep is always included
1150 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1151
1152 // Ensure that indirect stubs dep is not included
1153 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1154
1155 // Ensure that direct stubs dep is included
1156 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1157
1158 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1159
Jiyong Park55549df2021-02-26 23:57:23 +09001160 // Ensure that mylib is linking with the latest version of stub for mylib2
1161 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001162 // ... and not linking to the non-stub (impl) variant of mylib2
1163 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1164
1165 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1166 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1167 // .. and not linking to the stubs variant of mylib3
1168 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1169
1170 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001171 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001172 ensureNotContains(t, mylib2Cflags, "-include ")
1173
Jiyong Park85cc35a2022-07-17 11:30:47 +09001174 // Ensure that genstub is invoked with --systemapi
1175 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001176
1177 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1178 "lib64/mylib.so",
1179 "lib64/mylib3.so",
1180 "lib64/mylib4.so",
1181 })
1182}
1183
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001184func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1185 t.Parallel()
1186 // myapex (Z)
1187 // mylib -----------------.
1188 // |
1189 // otherapex (29) |
1190 // libstub's versions: 29 Z current
1191 // |
1192 // <platform> |
1193 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001194 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001195 apex {
1196 name: "myapex",
1197 key: "myapex.key",
1198 native_shared_libs: ["mylib"],
1199 min_sdk_version: "Z", // non-final
1200 }
1201
1202 cc_library {
1203 name: "mylib",
1204 srcs: ["mylib.cpp"],
1205 shared_libs: ["libstub"],
1206 apex_available: ["myapex"],
1207 min_sdk_version: "Z",
1208 }
1209
1210 apex_key {
1211 name: "myapex.key",
1212 public_key: "testkey.avbpubkey",
1213 private_key: "testkey.pem",
1214 }
1215
1216 apex {
1217 name: "otherapex",
1218 key: "myapex.key",
1219 native_shared_libs: ["libstub"],
1220 min_sdk_version: "29",
1221 }
1222
1223 cc_library {
1224 name: "libstub",
1225 srcs: ["mylib.cpp"],
1226 stubs: {
1227 versions: ["29", "Z", "current"],
1228 },
1229 apex_available: ["otherapex"],
1230 min_sdk_version: "29",
1231 }
1232
1233 // platform module depending on libstub from otherapex should use the latest stub("current")
1234 cc_library {
1235 name: "libplatform",
1236 srcs: ["mylib.cpp"],
1237 shared_libs: ["libstub"],
1238 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001239 `,
1240 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1241 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1242 variables.Platform_sdk_final = proptools.BoolPtr(false)
1243 variables.Platform_version_active_codenames = []string{"Z"}
1244 }),
1245 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001246
Jiyong Park55549df2021-02-26 23:57:23 +09001247 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001248 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001249 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001250 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001251 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001252
1253 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1254 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1255 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1256 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1257 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1258}
1259
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001260func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001261 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001262 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001263 name: "myapex2",
1264 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001265 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001266 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001267 }
1268
1269 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001270 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001271 public_key: "testkey.avbpubkey",
1272 private_key: "testkey.pem",
1273 }
1274
1275 cc_library {
1276 name: "mylib",
1277 srcs: ["mylib.cpp"],
1278 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001279 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001280 system_shared_libs: [],
1281 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001282 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001283 }
1284
1285 cc_library {
1286 name: "libfoo",
1287 srcs: ["mylib.cpp"],
1288 shared_libs: ["libbar"],
1289 system_shared_libs: [],
1290 stl: "none",
1291 stubs: {
1292 versions: ["10", "20", "30"],
1293 },
1294 }
1295
1296 cc_library {
1297 name: "libbar",
1298 srcs: ["mylib.cpp"],
1299 system_shared_libs: [],
1300 stl: "none",
1301 }
1302
Jiyong Park678c8812020-02-07 17:25:49 +09001303 cc_library_static {
1304 name: "libbaz",
1305 srcs: ["mylib.cpp"],
1306 system_shared_libs: [],
1307 stl: "none",
1308 apex_available: [ "myapex2" ],
1309 }
1310
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001311 `)
1312
Jiyong Park83dc74b2020-01-14 18:38:44 +09001313 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001314 copyCmds := apexRule.Args["copy_commands"]
1315
1316 // Ensure that direct non-stubs dep is always included
1317 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1318
1319 // Ensure that indirect stubs dep is not included
1320 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1321
1322 // Ensure that dependency of stubs is not included
1323 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1324
Colin Crossaede88c2020-08-11 12:17:01 -07001325 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001326
1327 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001328 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001329 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001330 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001331
Jiyong Park3ff16992019-12-27 14:11:47 +09001332 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001333
1334 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1335 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001336
Artur Satayeva8bd1132020-04-27 18:07:06 +01001337 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001338 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001339
Artur Satayeva8bd1132020-04-27 18:07:06 +01001340 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001341 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001342}
1343
Jooyung Hand3639552019-08-09 12:57:43 +09001344func TestApexWithRuntimeLibsDependency(t *testing.T) {
1345 /*
1346 myapex
1347 |
1348 v (runtime_libs)
1349 mylib ------+------> libfoo [provides stub]
1350 |
1351 `------> libbar
1352 */
Colin Cross1c460562021-02-16 17:55:47 -08001353 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001354 apex {
1355 name: "myapex",
1356 key: "myapex.key",
1357 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001358 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001359 }
1360
1361 apex_key {
1362 name: "myapex.key",
1363 public_key: "testkey.avbpubkey",
1364 private_key: "testkey.pem",
1365 }
1366
1367 cc_library {
1368 name: "mylib",
1369 srcs: ["mylib.cpp"],
1370 runtime_libs: ["libfoo", "libbar"],
1371 system_shared_libs: [],
1372 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001373 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001374 }
1375
1376 cc_library {
1377 name: "libfoo",
1378 srcs: ["mylib.cpp"],
1379 system_shared_libs: [],
1380 stl: "none",
1381 stubs: {
1382 versions: ["10", "20", "30"],
1383 },
1384 }
1385
1386 cc_library {
1387 name: "libbar",
1388 srcs: ["mylib.cpp"],
1389 system_shared_libs: [],
1390 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001391 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001392 }
1393
1394 `)
1395
Sundong Ahnabb64432019-10-22 13:58:29 +09001396 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001397 copyCmds := apexRule.Args["copy_commands"]
1398
1399 // Ensure that direct non-stubs dep is always included
1400 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1401
1402 // Ensure that indirect stubs dep is not included
1403 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1404
1405 // Ensure that runtime_libs dep in included
1406 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1407
Sundong Ahnabb64432019-10-22 13:58:29 +09001408 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001409 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1410 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001411
1412}
1413
Paul Duffina02cae32021-03-09 01:44:06 +00001414var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1415 cc.PrepareForTestWithCcBuildComponents,
1416 PrepareForTestWithApexBuildComponents,
1417 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001418 apex {
1419 name: "com.android.runtime",
1420 key: "com.android.runtime.key",
1421 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001422 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001423 }
1424
1425 apex_key {
1426 name: "com.android.runtime.key",
1427 public_key: "testkey.avbpubkey",
1428 private_key: "testkey.pem",
1429 }
Paul Duffina02cae32021-03-09 01:44:06 +00001430 `),
1431 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1432)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001433
Paul Duffina02cae32021-03-09 01:44:06 +00001434func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001435 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001436 cc_library {
1437 name: "libc",
1438 no_libcrt: true,
1439 nocrt: true,
1440 stl: "none",
1441 system_shared_libs: [],
1442 stubs: { versions: ["1"] },
1443 apex_available: ["com.android.runtime"],
1444
1445 sanitize: {
1446 hwaddress: true,
1447 }
1448 }
1449
1450 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001451 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001452 no_libcrt: true,
1453 nocrt: true,
1454 stl: "none",
1455 system_shared_libs: [],
1456 srcs: [""],
1457 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001458 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001459
1460 sanitize: {
1461 never: true,
1462 },
Paul Duffina02cae32021-03-09 01:44:06 +00001463 } `)
1464 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001465
1466 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1467 "lib64/bionic/libc.so",
1468 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1469 })
1470
Colin Cross4c4c1be2022-02-10 11:41:18 -08001471 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001472
1473 installed := hwasan.Description("install libclang_rt.hwasan")
1474 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1475
1476 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1477 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1478 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1479}
1480
1481func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001482 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001483 prepareForTestOfRuntimeApexWithHwasan,
1484 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1485 variables.SanitizeDevice = []string{"hwaddress"}
1486 }),
1487 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001488 cc_library {
1489 name: "libc",
1490 no_libcrt: true,
1491 nocrt: true,
1492 stl: "none",
1493 system_shared_libs: [],
1494 stubs: { versions: ["1"] },
1495 apex_available: ["com.android.runtime"],
1496 }
1497
1498 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001499 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001500 no_libcrt: true,
1501 nocrt: true,
1502 stl: "none",
1503 system_shared_libs: [],
1504 srcs: [""],
1505 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001506 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001507
1508 sanitize: {
1509 never: true,
1510 },
1511 }
Paul Duffina02cae32021-03-09 01:44:06 +00001512 `)
1513 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001514
1515 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1516 "lib64/bionic/libc.so",
1517 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1518 })
1519
Colin Cross4c4c1be2022-02-10 11:41:18 -08001520 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001521
1522 installed := hwasan.Description("install libclang_rt.hwasan")
1523 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1524
1525 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1526 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1527 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1528}
1529
Jooyung Han61b66e92020-03-21 14:21:46 +00001530func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1531 testcases := []struct {
1532 name string
1533 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001534 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001535 shouldLink string
1536 shouldNotLink []string
1537 }{
1538 {
Jiyong Park55549df2021-02-26 23:57:23 +09001539 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001540 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001541 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001542 shouldLink: "current",
1543 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001544 },
1545 {
Jiyong Park55549df2021-02-26 23:57:23 +09001546 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001547 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001548 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001549 shouldLink: "current",
1550 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001551 },
1552 }
1553 for _, tc := range testcases {
1554 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001555 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001556 apex {
1557 name: "myapex",
1558 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001559 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001560 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001561 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001562 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001563
Jooyung Han61b66e92020-03-21 14:21:46 +00001564 apex_key {
1565 name: "myapex.key",
1566 public_key: "testkey.avbpubkey",
1567 private_key: "testkey.pem",
1568 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001569
Jooyung Han61b66e92020-03-21 14:21:46 +00001570 cc_library {
1571 name: "mylib",
1572 srcs: ["mylib.cpp"],
1573 vendor_available: true,
1574 shared_libs: ["libbar"],
1575 system_shared_libs: [],
1576 stl: "none",
1577 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001578 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001579 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001580
Jooyung Han61b66e92020-03-21 14:21:46 +00001581 cc_library {
1582 name: "libbar",
1583 srcs: ["mylib.cpp"],
1584 system_shared_libs: [],
1585 stl: "none",
1586 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001587 llndk: {
1588 symbol_file: "libbar.map.txt",
1589 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001590 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001591 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001592 withUnbundledBuild,
1593 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001594
Jooyung Han61b66e92020-03-21 14:21:46 +00001595 // Ensure that LLNDK dep is not included
1596 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1597 "lib64/mylib.so",
1598 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001599
Jooyung Han61b66e92020-03-21 14:21:46 +00001600 // Ensure that LLNDK dep is required
1601 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1602 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1603 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001604
Steven Moreland2c4000c2021-04-27 02:08:49 +00001605 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1606 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001607 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001608 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001609 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001610
Steven Moreland2c4000c2021-04-27 02:08:49 +00001611 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001612 ver := tc.shouldLink
1613 if tc.shouldLink == "current" {
1614 ver = strconv.Itoa(android.FutureApiLevelInt)
1615 }
1616 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001617 })
1618 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001619}
1620
Jiyong Park25fc6a92018-11-18 18:02:45 +09001621func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001622 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001623 apex {
1624 name: "myapex",
1625 key: "myapex.key",
1626 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001627 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001628 }
1629
1630 apex_key {
1631 name: "myapex.key",
1632 public_key: "testkey.avbpubkey",
1633 private_key: "testkey.pem",
1634 }
1635
1636 cc_library {
1637 name: "mylib",
1638 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001639 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001640 shared_libs: ["libdl#27"],
1641 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001642 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001643 }
1644
1645 cc_library_shared {
1646 name: "mylib_shared",
1647 srcs: ["mylib.cpp"],
1648 shared_libs: ["libdl#27"],
1649 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001650 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001651 }
1652
1653 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001654 name: "libBootstrap",
1655 srcs: ["mylib.cpp"],
1656 stl: "none",
1657 bootstrap: true,
1658 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001659 `)
1660
Sundong Ahnabb64432019-10-22 13:58:29 +09001661 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001662 copyCmds := apexRule.Args["copy_commands"]
1663
1664 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001665 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001666 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1667 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001668
1669 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001670 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001671
Colin Crossaede88c2020-08-11 12:17:01 -07001672 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1673 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1674 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001675
1676 // For dependency to libc
1677 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001678 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001679 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001680 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001681 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001682 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1683 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001684
1685 // For dependency to libm
1686 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001687 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001688 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001689 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001690 // ... and is not compiling with the stub
1691 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1692 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1693
1694 // For dependency to libdl
1695 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001696 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001697 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001698 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1699 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001700 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001701 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001702 // ... Cflags from stub is correctly exported to mylib
1703 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1704 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001705
1706 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001707 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1708 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1709 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1710 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001711}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001712
Jooyung Han749dc692020-04-15 11:03:39 +09001713func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001714 // there are three links between liba --> libz.
1715 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001716 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001717 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001718 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001719 apex {
1720 name: "myapex",
1721 key: "myapex.key",
1722 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001723 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001724 }
1725
1726 apex {
1727 name: "otherapex",
1728 key: "myapex.key",
1729 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001730 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001731 }
1732
1733 apex_key {
1734 name: "myapex.key",
1735 public_key: "testkey.avbpubkey",
1736 private_key: "testkey.pem",
1737 }
1738
1739 cc_library {
1740 name: "libx",
1741 shared_libs: ["liba"],
1742 system_shared_libs: [],
1743 stl: "none",
1744 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001745 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001746 }
1747
1748 cc_library {
1749 name: "liby",
1750 shared_libs: ["liba"],
1751 system_shared_libs: [],
1752 stl: "none",
1753 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001754 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001755 }
1756
1757 cc_library {
1758 name: "liba",
1759 shared_libs: ["libz"],
1760 system_shared_libs: [],
1761 stl: "none",
1762 apex_available: [
1763 "//apex_available:anyapex",
1764 "//apex_available:platform",
1765 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001766 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001767 }
1768
1769 cc_library {
1770 name: "libz",
1771 system_shared_libs: [],
1772 stl: "none",
1773 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001774 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001775 },
1776 }
Jooyung Han749dc692020-04-15 11:03:39 +09001777 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001778
1779 expectLink := func(from, from_variant, to, to_variant string) {
1780 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1781 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1782 }
1783 expectNoLink := func(from, from_variant, to, to_variant string) {
1784 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1785 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1786 }
1787 // platform liba is linked to non-stub version
1788 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001789 // liba in myapex is linked to current
1790 expectLink("liba", "shared_apex29", "libz", "shared_current")
1791 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001792 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001793 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001794 // liba in otherapex is linked to current
1795 expectLink("liba", "shared_apex30", "libz", "shared_current")
1796 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001797 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1798 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001799}
1800
Jooyung Hanaed150d2020-04-02 01:41:41 +09001801func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001802 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001803 apex {
1804 name: "myapex",
1805 key: "myapex.key",
1806 native_shared_libs: ["libx"],
1807 min_sdk_version: "R",
1808 }
1809
1810 apex_key {
1811 name: "myapex.key",
1812 public_key: "testkey.avbpubkey",
1813 private_key: "testkey.pem",
1814 }
1815
1816 cc_library {
1817 name: "libx",
1818 shared_libs: ["libz"],
1819 system_shared_libs: [],
1820 stl: "none",
1821 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001822 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001823 }
1824
1825 cc_library {
1826 name: "libz",
1827 system_shared_libs: [],
1828 stl: "none",
1829 stubs: {
1830 versions: ["29", "R"],
1831 },
1832 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001833 `,
1834 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1835 variables.Platform_version_active_codenames = []string{"R"}
1836 }),
1837 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001838
1839 expectLink := func(from, from_variant, to, to_variant string) {
1840 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1841 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1842 }
1843 expectNoLink := func(from, from_variant, to, to_variant string) {
1844 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1845 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1846 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001847 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1848 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001849 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1850 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001851}
1852
Jooyung Han4c4da062021-06-23 10:23:16 +09001853func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1854 testApex(t, `
1855 apex {
1856 name: "myapex",
1857 key: "myapex.key",
1858 java_libs: ["libx"],
1859 min_sdk_version: "S",
1860 }
1861
1862 apex_key {
1863 name: "myapex.key",
1864 public_key: "testkey.avbpubkey",
1865 private_key: "testkey.pem",
1866 }
1867
1868 java_library {
1869 name: "libx",
1870 srcs: ["a.java"],
1871 apex_available: [ "myapex" ],
1872 sdk_version: "current",
1873 min_sdk_version: "S", // should be okay
1874 }
1875 `,
1876 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1877 variables.Platform_version_active_codenames = []string{"S"}
1878 variables.Platform_sdk_codename = proptools.StringPtr("S")
1879 }),
1880 )
1881}
1882
Jooyung Han749dc692020-04-15 11:03:39 +09001883func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001884 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001885 apex {
1886 name: "myapex",
1887 key: "myapex.key",
1888 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001889 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001890 }
1891
1892 apex_key {
1893 name: "myapex.key",
1894 public_key: "testkey.avbpubkey",
1895 private_key: "testkey.pem",
1896 }
1897
1898 cc_library {
1899 name: "libx",
1900 shared_libs: ["libz"],
1901 system_shared_libs: [],
1902 stl: "none",
1903 apex_available: [ "myapex" ],
1904 }
1905
1906 cc_library {
1907 name: "libz",
1908 system_shared_libs: [],
1909 stl: "none",
1910 stubs: {
1911 versions: ["1", "2"],
1912 },
1913 }
1914 `)
1915
1916 expectLink := func(from, from_variant, to, to_variant string) {
1917 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1918 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1919 }
1920 expectNoLink := func(from, from_variant, to, to_variant string) {
1921 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1922 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1923 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001924 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001925 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001926 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001927 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001928}
1929
Jooyung Handfc864c2023-03-20 18:19:07 +09001930func TestApexMinSdkVersion_InVendorApex(t *testing.T) {
Jiyong Park5df7bd32021-08-25 16:18:46 +09001931 ctx := testApex(t, `
1932 apex {
1933 name: "myapex",
1934 key: "myapex.key",
1935 native_shared_libs: ["mylib"],
Jooyung Handfc864c2023-03-20 18:19:07 +09001936 updatable: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09001937 vendor: true,
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: "mylib",
Jooyung Handfc864c2023-03-20 18:19:07 +09001949 srcs: ["mylib.cpp"],
Jiyong Park5df7bd32021-08-25 16:18:46 +09001950 vendor_available: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09001951 min_sdk_version: "29",
Jooyung Handfc864c2023-03-20 18:19:07 +09001952 shared_libs: ["libbar"],
1953 }
1954
1955 cc_library {
1956 name: "libbar",
1957 stubs: { versions: ["29", "30"] },
1958 llndk: { symbol_file: "libbar.map.txt" },
Jiyong Park5df7bd32021-08-25 16:18:46 +09001959 }
1960 `)
1961
1962 vendorVariant := "android_vendor.29_arm64_armv8-a"
1963
Jooyung Handfc864c2023-03-20 18:19:07 +09001964 mylib := ctx.ModuleForTests("mylib", vendorVariant+"_shared_myapex")
1965
1966 // Ensure that mylib links with "current" LLNDK
1967 libFlags := names(mylib.Rule("ld").Args["libFlags"])
1968 ensureListContains(t, libFlags, "out/soong/.intermediates/libbar/"+vendorVariant+"_shared_current/libbar.so")
1969
1970 // Ensure that mylib is targeting 29
1971 ccRule := ctx.ModuleForTests("mylib", vendorVariant+"_static_apex29").Output("obj/mylib.o")
1972 ensureContains(t, ccRule.Args["cFlags"], "-target aarch64-linux-android29")
1973
1974 // Ensure that the correct variant of crtbegin_so is used.
1975 crtBegin := mylib.Rule("ld").Args["crtBegin"]
1976 ensureContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
Jiyong Park5df7bd32021-08-25 16:18:46 +09001977
1978 // Ensure that the crtbegin_so used by the APEX is targeting 29
1979 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
1980 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
1981}
1982
Jooyung Han4495f842023-04-25 16:39:59 +09001983func TestTrackAllowedDeps(t *testing.T) {
1984 ctx := testApex(t, `
1985 apex {
1986 name: "myapex",
1987 key: "myapex.key",
1988 updatable: true,
1989 native_shared_libs: [
1990 "mylib",
1991 "yourlib",
1992 ],
1993 min_sdk_version: "29",
1994 }
1995
1996 apex {
1997 name: "myapex2",
1998 key: "myapex.key",
1999 updatable: false,
2000 native_shared_libs: ["yourlib"],
2001 }
2002
2003 apex_key {
2004 name: "myapex.key",
2005 public_key: "testkey.avbpubkey",
2006 private_key: "testkey.pem",
2007 }
2008
2009 cc_library {
2010 name: "mylib",
2011 srcs: ["mylib.cpp"],
2012 shared_libs: ["libbar"],
2013 min_sdk_version: "29",
2014 apex_available: ["myapex"],
2015 }
2016
2017 cc_library {
2018 name: "libbar",
2019 stubs: { versions: ["29", "30"] },
2020 }
2021
2022 cc_library {
2023 name: "yourlib",
2024 srcs: ["mylib.cpp"],
2025 min_sdk_version: "29",
2026 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2027 }
2028 `, withFiles(android.MockFS{
2029 "packages/modules/common/build/allowed_deps.txt": nil,
2030 }))
2031
2032 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2033 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2034 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
2035 "out/soong/.intermediates/myapex/android_common_myapex_image/depsinfo/flatlist.txt")
2036 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
2037 "out/soong/.intermediates/myapex2/android_common_myapex2_image/depsinfo/flatlist.txt")
2038
2039 myapex := ctx.ModuleForTests("myapex", "android_common_myapex_image")
2040 flatlist := strings.Split(myapex.Output("depsinfo/flatlist.txt").BuildParams.Args["content"], "\\n")
2041 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2042 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2043 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2044 flatlist, "mylib:(minSdkVersion:29)")
2045 android.AssertStringListContains(t, "track platform-available lib",
2046 flatlist, "yourlib(minSdkVersion:29)")
2047}
2048
2049func TestTrackAllowedDeps_SkipWithoutAllowedDepsTxt(t *testing.T) {
2050 ctx := testApex(t, `
2051 apex {
2052 name: "myapex",
2053 key: "myapex.key",
2054 updatable: true,
2055 min_sdk_version: "29",
2056 }
2057
2058 apex_key {
2059 name: "myapex.key",
2060 public_key: "testkey.avbpubkey",
2061 private_key: "testkey.pem",
2062 }
2063 `)
2064 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2065 if nil != depsinfo.MaybeRule("generateApexDepsInfoFilesRule").Output {
2066 t.Error("apex_depsinfo_singleton shouldn't run when allowed_deps.txt doesn't exist")
2067 }
2068}
2069
Jooyung Han03b51852020-02-26 22:45:42 +09002070func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002071 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002072 apex {
2073 name: "myapex",
2074 key: "myapex.key",
2075 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002076 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002077 }
2078
2079 apex_key {
2080 name: "myapex.key",
2081 public_key: "testkey.avbpubkey",
2082 private_key: "testkey.pem",
2083 }
2084
2085 cc_library {
2086 name: "libx",
2087 system_shared_libs: [],
2088 stl: "none",
2089 apex_available: [ "myapex" ],
2090 stubs: {
2091 versions: ["1", "2"],
2092 },
2093 }
2094
2095 cc_library {
2096 name: "libz",
2097 shared_libs: ["libx"],
2098 system_shared_libs: [],
2099 stl: "none",
2100 }
2101 `)
2102
2103 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002104 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002105 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2106 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2107 }
2108 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002109 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002110 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2111 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2112 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002113 expectLink("libz", "shared", "libx", "shared_current")
2114 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002115 expectNoLink("libz", "shared", "libz", "shared_1")
2116 expectNoLink("libz", "shared", "libz", "shared")
2117}
2118
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002119var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2120 func(variables android.FixtureProductVariables) {
2121 variables.SanitizeDevice = []string{"hwaddress"}
2122 },
2123)
2124
Jooyung Han75568392020-03-20 04:29:24 +09002125func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002126 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002127 apex {
2128 name: "myapex",
2129 key: "myapex.key",
2130 native_shared_libs: ["libx"],
2131 min_sdk_version: "29",
2132 }
2133
2134 apex_key {
2135 name: "myapex.key",
2136 public_key: "testkey.avbpubkey",
2137 private_key: "testkey.pem",
2138 }
2139
2140 cc_library {
2141 name: "libx",
2142 shared_libs: ["libbar"],
2143 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002144 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002145 }
2146
2147 cc_library {
2148 name: "libbar",
2149 stubs: {
2150 versions: ["29", "30"],
2151 },
2152 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002153 `,
2154 prepareForTestWithSantitizeHwaddress,
2155 )
Jooyung Han03b51852020-02-26 22:45:42 +09002156 expectLink := func(from, from_variant, to, to_variant string) {
2157 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2158 libFlags := ld.Args["libFlags"]
2159 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2160 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002161 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002162}
2163
Jooyung Han75568392020-03-20 04:29:24 +09002164func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002165 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002166 apex {
2167 name: "myapex",
2168 key: "myapex.key",
2169 native_shared_libs: ["libx"],
2170 min_sdk_version: "29",
2171 }
2172
2173 apex_key {
2174 name: "myapex.key",
2175 public_key: "testkey.avbpubkey",
2176 private_key: "testkey.pem",
2177 }
2178
2179 cc_library {
2180 name: "libx",
2181 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002182 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002183 }
Jooyung Han75568392020-03-20 04:29:24 +09002184 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002185
2186 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002187 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002188 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002189 // note that platform variant is not.
2190 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002191 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002192}
2193
Jooyung Han749dc692020-04-15 11:03:39 +09002194func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2195 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002196 apex {
2197 name: "myapex",
2198 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002199 native_shared_libs: ["mylib"],
2200 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002201 }
2202
2203 apex_key {
2204 name: "myapex.key",
2205 public_key: "testkey.avbpubkey",
2206 private_key: "testkey.pem",
2207 }
Jooyung Han749dc692020-04-15 11:03:39 +09002208
2209 cc_library {
2210 name: "mylib",
2211 srcs: ["mylib.cpp"],
2212 system_shared_libs: [],
2213 stl: "none",
2214 apex_available: [
2215 "myapex",
2216 ],
2217 min_sdk_version: "30",
2218 }
2219 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002220
2221 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2222 apex {
2223 name: "myapex",
2224 key: "myapex.key",
2225 native_shared_libs: ["libfoo.ffi"],
2226 min_sdk_version: "29",
2227 }
2228
2229 apex_key {
2230 name: "myapex.key",
2231 public_key: "testkey.avbpubkey",
2232 private_key: "testkey.pem",
2233 }
2234
2235 rust_ffi_shared {
2236 name: "libfoo.ffi",
2237 srcs: ["foo.rs"],
2238 crate_name: "foo",
2239 apex_available: [
2240 "myapex",
2241 ],
2242 min_sdk_version: "30",
2243 }
2244 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002245
2246 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2247 apex {
2248 name: "myapex",
2249 key: "myapex.key",
2250 java_libs: ["libfoo"],
2251 min_sdk_version: "29",
2252 }
2253
2254 apex_key {
2255 name: "myapex.key",
2256 public_key: "testkey.avbpubkey",
2257 private_key: "testkey.pem",
2258 }
2259
2260 java_import {
2261 name: "libfoo",
2262 jars: ["libfoo.jar"],
2263 apex_available: [
2264 "myapex",
2265 ],
2266 min_sdk_version: "30",
2267 }
2268 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002269
2270 // Skip check for modules compiling against core API surface
2271 testApex(t, `
2272 apex {
2273 name: "myapex",
2274 key: "myapex.key",
2275 java_libs: ["libfoo"],
2276 min_sdk_version: "29",
2277 }
2278
2279 apex_key {
2280 name: "myapex.key",
2281 public_key: "testkey.avbpubkey",
2282 private_key: "testkey.pem",
2283 }
2284
2285 java_library {
2286 name: "libfoo",
2287 srcs: ["Foo.java"],
2288 apex_available: [
2289 "myapex",
2290 ],
2291 // Compile against core API surface
2292 sdk_version: "core_current",
2293 min_sdk_version: "30",
2294 }
2295 `)
2296
Jooyung Han749dc692020-04-15 11:03:39 +09002297}
2298
2299func TestApexMinSdkVersion_Okay(t *testing.T) {
2300 testApex(t, `
2301 apex {
2302 name: "myapex",
2303 key: "myapex.key",
2304 native_shared_libs: ["libfoo"],
2305 java_libs: ["libbar"],
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 cc_library {
2316 name: "libfoo",
2317 srcs: ["mylib.cpp"],
2318 shared_libs: ["libfoo_dep"],
2319 apex_available: ["myapex"],
2320 min_sdk_version: "29",
2321 }
2322
2323 cc_library {
2324 name: "libfoo_dep",
2325 srcs: ["mylib.cpp"],
2326 apex_available: ["myapex"],
2327 min_sdk_version: "29",
2328 }
2329
2330 java_library {
2331 name: "libbar",
2332 sdk_version: "current",
2333 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002334 static_libs: [
2335 "libbar_dep",
2336 "libbar_import_dep",
2337 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002338 apex_available: ["myapex"],
2339 min_sdk_version: "29",
2340 }
2341
2342 java_library {
2343 name: "libbar_dep",
2344 sdk_version: "current",
2345 srcs: ["a.java"],
2346 apex_available: ["myapex"],
2347 min_sdk_version: "29",
2348 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002349
2350 java_import {
2351 name: "libbar_import_dep",
2352 jars: ["libbar.jar"],
2353 apex_available: ["myapex"],
2354 min_sdk_version: "29",
2355 }
Jooyung Han03b51852020-02-26 22:45:42 +09002356 `)
2357}
2358
Colin Cross8ca61c12022-10-06 21:00:14 -07002359func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2360 // Tests that an apex dependency with min_sdk_version higher than the
2361 // min_sdk_version of the apex is allowed as long as the dependency's
2362 // min_sdk_version is less than or equal to the api level that the
2363 // architecture was introduced in. In this case, arm64 didn't exist
2364 // until api level 21, so the arm64 code will never need to run on
2365 // an api level 20 device, even if other architectures of the apex
2366 // will.
2367 testApex(t, `
2368 apex {
2369 name: "myapex",
2370 key: "myapex.key",
2371 native_shared_libs: ["libfoo"],
2372 min_sdk_version: "20",
2373 }
2374
2375 apex_key {
2376 name: "myapex.key",
2377 public_key: "testkey.avbpubkey",
2378 private_key: "testkey.pem",
2379 }
2380
2381 cc_library {
2382 name: "libfoo",
2383 srcs: ["mylib.cpp"],
2384 apex_available: ["myapex"],
2385 min_sdk_version: "21",
2386 stl: "none",
2387 }
2388 `)
2389}
2390
Artur Satayev8cf899a2020-04-15 17:29:42 +01002391func TestJavaStableSdkVersion(t *testing.T) {
2392 testCases := []struct {
2393 name string
2394 expectedError string
2395 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002396 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002397 }{
2398 {
2399 name: "Non-updatable apex with non-stable dep",
2400 bp: `
2401 apex {
2402 name: "myapex",
2403 java_libs: ["myjar"],
2404 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002405 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002406 }
2407 apex_key {
2408 name: "myapex.key",
2409 public_key: "testkey.avbpubkey",
2410 private_key: "testkey.pem",
2411 }
2412 java_library {
2413 name: "myjar",
2414 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002415 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002416 apex_available: ["myapex"],
2417 }
2418 `,
2419 },
2420 {
2421 name: "Updatable apex with stable dep",
2422 bp: `
2423 apex {
2424 name: "myapex",
2425 java_libs: ["myjar"],
2426 key: "myapex.key",
2427 updatable: true,
2428 min_sdk_version: "29",
2429 }
2430 apex_key {
2431 name: "myapex.key",
2432 public_key: "testkey.avbpubkey",
2433 private_key: "testkey.pem",
2434 }
2435 java_library {
2436 name: "myjar",
2437 srcs: ["foo/bar/MyClass.java"],
2438 sdk_version: "current",
2439 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002440 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002441 }
2442 `,
2443 },
2444 {
2445 name: "Updatable apex with non-stable dep",
2446 expectedError: "cannot depend on \"myjar\"",
2447 bp: `
2448 apex {
2449 name: "myapex",
2450 java_libs: ["myjar"],
2451 key: "myapex.key",
2452 updatable: true,
2453 }
2454 apex_key {
2455 name: "myapex.key",
2456 public_key: "testkey.avbpubkey",
2457 private_key: "testkey.pem",
2458 }
2459 java_library {
2460 name: "myjar",
2461 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002462 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002463 apex_available: ["myapex"],
2464 }
2465 `,
2466 },
2467 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002468 name: "Updatable apex with non-stable legacy core platform dep",
2469 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2470 bp: `
2471 apex {
2472 name: "myapex",
2473 java_libs: ["myjar-uses-legacy"],
2474 key: "myapex.key",
2475 updatable: true,
2476 }
2477 apex_key {
2478 name: "myapex.key",
2479 public_key: "testkey.avbpubkey",
2480 private_key: "testkey.pem",
2481 }
2482 java_library {
2483 name: "myjar-uses-legacy",
2484 srcs: ["foo/bar/MyClass.java"],
2485 sdk_version: "core_platform",
2486 apex_available: ["myapex"],
2487 }
2488 `,
2489 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2490 },
2491 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002492 name: "Updatable apex with non-stable transitive dep",
2493 // This is not actually detecting that the transitive dependency is unstable, rather it is
2494 // detecting that the transitive dependency is building against a wider API surface than the
2495 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002496 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002497 bp: `
2498 apex {
2499 name: "myapex",
2500 java_libs: ["myjar"],
2501 key: "myapex.key",
2502 updatable: true,
2503 }
2504 apex_key {
2505 name: "myapex.key",
2506 public_key: "testkey.avbpubkey",
2507 private_key: "testkey.pem",
2508 }
2509 java_library {
2510 name: "myjar",
2511 srcs: ["foo/bar/MyClass.java"],
2512 sdk_version: "current",
2513 apex_available: ["myapex"],
2514 static_libs: ["transitive-jar"],
2515 }
2516 java_library {
2517 name: "transitive-jar",
2518 srcs: ["foo/bar/MyClass.java"],
2519 sdk_version: "core_platform",
2520 apex_available: ["myapex"],
2521 }
2522 `,
2523 },
2524 }
2525
2526 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002527 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2528 continue
2529 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002530 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002531 errorHandler := android.FixtureExpectsNoErrors
2532 if test.expectedError != "" {
2533 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002534 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002535 android.GroupFixturePreparers(
2536 java.PrepareForTestWithJavaDefaultModules,
2537 PrepareForTestWithApexBuildComponents,
2538 prepareForTestWithMyapex,
2539 android.OptionalFixturePreparer(test.preparer),
2540 ).
2541 ExtendWithErrorHandler(errorHandler).
2542 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002543 })
2544 }
2545}
2546
Jooyung Han749dc692020-04-15 11:03:39 +09002547func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2548 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2549 apex {
2550 name: "myapex",
2551 key: "myapex.key",
2552 native_shared_libs: ["mylib"],
2553 min_sdk_version: "29",
2554 }
2555
2556 apex_key {
2557 name: "myapex.key",
2558 public_key: "testkey.avbpubkey",
2559 private_key: "testkey.pem",
2560 }
2561
2562 cc_library {
2563 name: "mylib",
2564 srcs: ["mylib.cpp"],
2565 shared_libs: ["mylib2"],
2566 system_shared_libs: [],
2567 stl: "none",
2568 apex_available: [
2569 "myapex",
2570 ],
2571 min_sdk_version: "29",
2572 }
2573
2574 // indirect part of the apex
2575 cc_library {
2576 name: "mylib2",
2577 srcs: ["mylib.cpp"],
2578 system_shared_libs: [],
2579 stl: "none",
2580 apex_available: [
2581 "myapex",
2582 ],
2583 min_sdk_version: "30",
2584 }
2585 `)
2586}
2587
2588func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2589 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2590 apex {
2591 name: "myapex",
2592 key: "myapex.key",
2593 apps: ["AppFoo"],
2594 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002595 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002596 }
2597
2598 apex_key {
2599 name: "myapex.key",
2600 public_key: "testkey.avbpubkey",
2601 private_key: "testkey.pem",
2602 }
2603
2604 android_app {
2605 name: "AppFoo",
2606 srcs: ["foo/bar/MyClass.java"],
2607 sdk_version: "current",
2608 min_sdk_version: "29",
2609 system_modules: "none",
2610 stl: "none",
2611 static_libs: ["bar"],
2612 apex_available: [ "myapex" ],
2613 }
2614
2615 java_library {
2616 name: "bar",
2617 sdk_version: "current",
2618 srcs: ["a.java"],
2619 apex_available: [ "myapex" ],
2620 }
2621 `)
2622}
2623
2624func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002625 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002626 apex {
2627 name: "myapex",
2628 key: "myapex.key",
2629 native_shared_libs: ["mylib"],
2630 min_sdk_version: "29",
2631 }
2632
2633 apex_key {
2634 name: "myapex.key",
2635 public_key: "testkey.avbpubkey",
2636 private_key: "testkey.pem",
2637 }
2638
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002639 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002640 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2641 cc_library {
2642 name: "mylib",
2643 srcs: ["mylib.cpp"],
2644 shared_libs: ["mylib2"],
2645 system_shared_libs: [],
2646 stl: "none",
2647 apex_available: ["myapex", "otherapex"],
2648 min_sdk_version: "29",
2649 }
2650
2651 cc_library {
2652 name: "mylib2",
2653 srcs: ["mylib.cpp"],
2654 system_shared_libs: [],
2655 stl: "none",
2656 apex_available: ["otherapex"],
2657 stubs: { versions: ["29", "30"] },
2658 min_sdk_version: "30",
2659 }
2660
2661 apex {
2662 name: "otherapex",
2663 key: "myapex.key",
2664 native_shared_libs: ["mylib", "mylib2"],
2665 min_sdk_version: "30",
2666 }
2667 `)
2668 expectLink := func(from, from_variant, to, to_variant string) {
2669 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2670 libFlags := ld.Args["libFlags"]
2671 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2672 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002673 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002674 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002675}
2676
Jooyung Haned124c32021-01-26 11:43:46 +09002677func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002678 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2679 func(variables android.FixtureProductVariables) {
2680 variables.Platform_sdk_codename = proptools.StringPtr("S")
2681 variables.Platform_version_active_codenames = []string{"S"}
2682 },
2683 )
Jooyung Haned124c32021-01-26 11:43:46 +09002684 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2685 apex {
2686 name: "myapex",
2687 key: "myapex.key",
2688 native_shared_libs: ["libfoo"],
2689 min_sdk_version: "S",
2690 }
2691 apex_key {
2692 name: "myapex.key",
2693 public_key: "testkey.avbpubkey",
2694 private_key: "testkey.pem",
2695 }
2696 cc_library {
2697 name: "libfoo",
2698 shared_libs: ["libbar"],
2699 apex_available: ["myapex"],
2700 min_sdk_version: "29",
2701 }
2702 cc_library {
2703 name: "libbar",
2704 apex_available: ["myapex"],
2705 }
2706 `, withSAsActiveCodeNames)
2707}
2708
2709func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002710 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2711 variables.Platform_sdk_codename = proptools.StringPtr("S")
2712 variables.Platform_version_active_codenames = []string{"S", "T"}
2713 })
Colin Cross1c460562021-02-16 17:55:47 -08002714 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002715 apex {
2716 name: "myapex",
2717 key: "myapex.key",
2718 native_shared_libs: ["libfoo"],
2719 min_sdk_version: "S",
2720 }
2721 apex_key {
2722 name: "myapex.key",
2723 public_key: "testkey.avbpubkey",
2724 private_key: "testkey.pem",
2725 }
2726 cc_library {
2727 name: "libfoo",
2728 shared_libs: ["libbar"],
2729 apex_available: ["myapex"],
2730 min_sdk_version: "S",
2731 }
2732 cc_library {
2733 name: "libbar",
2734 stubs: {
2735 symbol_file: "libbar.map.txt",
2736 versions: ["30", "S", "T"],
2737 },
2738 }
2739 `, withSAsActiveCodeNames)
2740
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002741 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002742 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2743 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002744 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002745}
2746
Jiyong Park7c2ee712018-12-07 00:42:25 +09002747func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002748 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002749 apex {
2750 name: "myapex",
2751 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002752 native_shared_libs: ["mylib"],
2753 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002754 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002755 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002756 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002757 }
2758
2759 apex_key {
2760 name: "myapex.key",
2761 public_key: "testkey.avbpubkey",
2762 private_key: "testkey.pem",
2763 }
2764
2765 prebuilt_etc {
2766 name: "myetc",
2767 src: "myprebuilt",
2768 sub_dir: "foo/bar",
2769 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002770
2771 cc_library {
2772 name: "mylib",
2773 srcs: ["mylib.cpp"],
2774 relative_install_path: "foo/bar",
2775 system_shared_libs: [],
2776 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002777 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002778 }
2779
2780 cc_binary {
2781 name: "mybin",
2782 srcs: ["mylib.cpp"],
2783 relative_install_path: "foo/bar",
2784 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002785 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002786 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002787 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002788 `)
2789
Sundong Ahnabb64432019-10-22 13:58:29 +09002790 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002791 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002792
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002793 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002794 ensureContains(t, cmd, "/etc ")
2795 ensureContains(t, cmd, "/etc/foo ")
2796 ensureContains(t, cmd, "/etc/foo/bar ")
2797 ensureContains(t, cmd, "/lib64 ")
2798 ensureContains(t, cmd, "/lib64/foo ")
2799 ensureContains(t, cmd, "/lib64/foo/bar ")
2800 ensureContains(t, cmd, "/lib ")
2801 ensureContains(t, cmd, "/lib/foo ")
2802 ensureContains(t, cmd, "/lib/foo/bar ")
2803 ensureContains(t, cmd, "/bin ")
2804 ensureContains(t, cmd, "/bin/foo ")
2805 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002806}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002807
Jooyung Han35155c42020-02-06 17:33:20 +09002808func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002809 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002810 apex {
2811 name: "myapex",
2812 key: "myapex.key",
2813 multilib: {
2814 both: {
2815 native_shared_libs: ["mylib"],
2816 binaries: ["mybin"],
2817 },
2818 },
2819 compile_multilib: "both",
2820 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002821 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002822 }
2823
2824 apex_key {
2825 name: "myapex.key",
2826 public_key: "testkey.avbpubkey",
2827 private_key: "testkey.pem",
2828 }
2829
2830 cc_library {
2831 name: "mylib",
2832 relative_install_path: "foo/bar",
2833 system_shared_libs: [],
2834 stl: "none",
2835 apex_available: [ "myapex" ],
2836 native_bridge_supported: true,
2837 }
2838
2839 cc_binary {
2840 name: "mybin",
2841 relative_install_path: "foo/bar",
2842 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002843 stl: "none",
2844 apex_available: [ "myapex" ],
2845 native_bridge_supported: true,
2846 compile_multilib: "both", // default is "first" for binary
2847 multilib: {
2848 lib64: {
2849 suffix: "64",
2850 },
2851 },
2852 }
2853 `, withNativeBridgeEnabled)
2854 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2855 "bin/foo/bar/mybin",
2856 "bin/foo/bar/mybin64",
2857 "bin/arm/foo/bar/mybin",
2858 "bin/arm64/foo/bar/mybin64",
2859 "lib/foo/bar/mylib.so",
2860 "lib/arm/foo/bar/mylib.so",
2861 "lib64/foo/bar/mylib.so",
2862 "lib64/arm64/foo/bar/mylib.so",
2863 })
2864}
2865
Jooyung Han85d61762020-06-24 23:50:26 +09002866func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002867 result := android.GroupFixturePreparers(
2868 prepareForApexTest,
2869 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2870 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002871 apex {
2872 name: "myapex",
2873 key: "myapex.key",
2874 binaries: ["mybin"],
2875 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002876 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002877 }
2878 apex_key {
2879 name: "myapex.key",
2880 public_key: "testkey.avbpubkey",
2881 private_key: "testkey.pem",
2882 }
2883 cc_binary {
2884 name: "mybin",
2885 vendor: true,
2886 shared_libs: ["libfoo"],
2887 }
2888 cc_library {
2889 name: "libfoo",
2890 proprietary: true,
2891 }
2892 `)
2893
Colin Crossc68db4b2021-11-11 18:59:15 -08002894 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002895 "bin/mybin",
2896 "lib64/libfoo.so",
2897 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2898 "lib64/libc++.so",
2899 })
2900
Colin Crossc68db4b2021-11-11 18:59:15 -08002901 apexBundle := result.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2902 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002903 name := apexBundle.BaseModuleName()
2904 prefix := "TARGET_"
2905 var builder strings.Builder
2906 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002907 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002908 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002909 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002910
Colin Crossc68db4b2021-11-11 18:59:15 -08002911 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002912 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2913 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002914}
2915
Jooyung Hanc5a96762022-02-04 11:54:50 +09002916func TestVendorApex_use_vndk_as_stable_TryingToIncludeVNDKLib(t *testing.T) {
2917 testApexError(t, `Trying to include a VNDK library`, `
2918 apex {
2919 name: "myapex",
2920 key: "myapex.key",
2921 native_shared_libs: ["libc++"], // libc++ is a VNDK lib
2922 vendor: true,
2923 use_vndk_as_stable: true,
2924 updatable: false,
2925 }
2926 apex_key {
2927 name: "myapex.key",
2928 public_key: "testkey.avbpubkey",
2929 private_key: "testkey.pem",
2930 }`)
2931}
2932
Jooyung Handf78e212020-07-22 15:54:47 +09002933func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Jooyung Han91f92032022-02-04 12:36:33 +09002934 // myapex myapex2
2935 // | |
2936 // mybin ------. mybin2
2937 // \ \ / |
2938 // (stable) .---\--------` |
2939 // \ / \ |
2940 // \ / \ /
2941 // libvndk libvendor
2942 // (vndk)
Colin Cross1c460562021-02-16 17:55:47 -08002943 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002944 apex {
2945 name: "myapex",
2946 key: "myapex.key",
2947 binaries: ["mybin"],
2948 vendor: true,
2949 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002950 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002951 }
2952 apex_key {
2953 name: "myapex.key",
2954 public_key: "testkey.avbpubkey",
2955 private_key: "testkey.pem",
2956 }
2957 cc_binary {
2958 name: "mybin",
2959 vendor: true,
2960 shared_libs: ["libvndk", "libvendor"],
2961 }
2962 cc_library {
2963 name: "libvndk",
2964 vndk: {
2965 enabled: true,
2966 },
2967 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002968 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002969 }
2970 cc_library {
2971 name: "libvendor",
2972 vendor: true,
Jooyung Han91f92032022-02-04 12:36:33 +09002973 stl: "none",
2974 }
2975 apex {
2976 name: "myapex2",
2977 key: "myapex.key",
2978 binaries: ["mybin2"],
2979 vendor: true,
2980 use_vndk_as_stable: false,
2981 updatable: false,
2982 }
2983 cc_binary {
2984 name: "mybin2",
2985 vendor: true,
2986 shared_libs: ["libvndk", "libvendor"],
Jooyung Handf78e212020-07-22 15:54:47 +09002987 }
2988 `)
2989
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002990 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002991
Jooyung Han91f92032022-02-04 12:36:33 +09002992 for _, tc := range []struct {
2993 name string
2994 apexName string
2995 moduleName string
2996 moduleVariant string
2997 libs []string
2998 contents []string
2999 requireVndkNamespace bool
3000 }{
3001 {
3002 name: "use_vndk_as_stable",
3003 apexName: "myapex",
3004 moduleName: "mybin",
3005 moduleVariant: vendorVariant + "_apex10000",
3006 libs: []string{
3007 // should link with vendor variants of VNDK libs(libvndk/libc++)
3008 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared/libvndk.so",
3009 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared/libc++.so",
3010 // unstable Vendor libs as APEX variant
3011 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
3012 },
3013 contents: []string{
3014 "bin/mybin",
3015 "lib64/libvendor.so",
3016 // VNDK libs (libvndk/libc++) are not included
3017 },
3018 requireVndkNamespace: true,
3019 },
3020 {
3021 name: "!use_vndk_as_stable",
3022 apexName: "myapex2",
3023 moduleName: "mybin2",
3024 moduleVariant: vendorVariant + "_myapex2",
3025 libs: []string{
3026 // should link with "unique" APEX(myapex2) variant of VNDK libs(libvndk/libc++)
3027 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared_myapex2/libvndk.so",
3028 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared_myapex2/libc++.so",
3029 // unstable vendor libs have "merged" APEX variants
3030 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
3031 },
3032 contents: []string{
3033 "bin/mybin2",
3034 "lib64/libvendor.so",
3035 // VNDK libs are included as well
3036 "lib64/libvndk.so",
3037 "lib64/libc++.so",
3038 },
3039 requireVndkNamespace: false,
3040 },
3041 } {
3042 t.Run(tc.name, func(t *testing.T) {
3043 // Check linked libs
3044 ldRule := ctx.ModuleForTests(tc.moduleName, tc.moduleVariant).Rule("ld")
3045 libs := names(ldRule.Args["libFlags"])
3046 for _, lib := range tc.libs {
3047 ensureListContains(t, libs, lib)
3048 }
3049 // Check apex contents
3050 ensureExactContents(t, ctx, tc.apexName, "android_common_"+tc.apexName+"_image", tc.contents)
Jooyung Handf78e212020-07-22 15:54:47 +09003051
Jooyung Han91f92032022-02-04 12:36:33 +09003052 // Check "requireNativeLibs"
3053 apexManifestRule := ctx.ModuleForTests(tc.apexName, "android_common_"+tc.apexName+"_image").Rule("apexManifestRule")
3054 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
3055 if tc.requireVndkNamespace {
3056 ensureListContains(t, requireNativeLibs, ":vndk")
3057 } else {
3058 ensureListNotContains(t, requireNativeLibs, ":vndk")
3059 }
3060 })
3061 }
Jooyung Handf78e212020-07-22 15:54:47 +09003062}
3063
Justin Yun13decfb2021-03-08 19:25:55 +09003064func TestProductVariant(t *testing.T) {
3065 ctx := testApex(t, `
3066 apex {
3067 name: "myapex",
3068 key: "myapex.key",
3069 updatable: false,
3070 product_specific: true,
3071 binaries: ["foo"],
3072 }
3073
3074 apex_key {
3075 name: "myapex.key",
3076 public_key: "testkey.avbpubkey",
3077 private_key: "testkey.pem",
3078 }
3079
3080 cc_binary {
3081 name: "foo",
3082 product_available: true,
3083 apex_available: ["myapex"],
3084 srcs: ["foo.cpp"],
3085 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00003086 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3087 variables.ProductVndkVersion = proptools.StringPtr("current")
3088 }),
3089 )
Justin Yun13decfb2021-03-08 19:25:55 +09003090
3091 cflags := strings.Fields(
Jooyung Han91f92032022-02-04 12:36:33 +09003092 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_myapex").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09003093 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
3094 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
3095 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
3096 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
3097}
3098
Jooyung Han8e5685d2020-09-21 11:02:57 +09003099func TestApex_withPrebuiltFirmware(t *testing.T) {
3100 testCases := []struct {
3101 name string
3102 additionalProp string
3103 }{
3104 {"system apex with prebuilt_firmware", ""},
3105 {"vendor apex with prebuilt_firmware", "vendor: true,"},
3106 }
3107 for _, tc := range testCases {
3108 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003109 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09003110 apex {
3111 name: "myapex",
3112 key: "myapex.key",
3113 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003114 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09003115 `+tc.additionalProp+`
3116 }
3117 apex_key {
3118 name: "myapex.key",
3119 public_key: "testkey.avbpubkey",
3120 private_key: "testkey.pem",
3121 }
3122 prebuilt_firmware {
3123 name: "myfirmware",
3124 src: "myfirmware.bin",
3125 filename_from_src: true,
3126 `+tc.additionalProp+`
3127 }
3128 `)
3129 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3130 "etc/firmware/myfirmware.bin",
3131 })
3132 })
3133 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003134}
3135
Jooyung Hanefb184e2020-06-25 17:14:25 +09003136func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003137 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003138 apex {
3139 name: "myapex",
3140 key: "myapex.key",
3141 vendor: true,
3142 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003143 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003144 }
3145
3146 apex_key {
3147 name: "myapex.key",
3148 public_key: "testkey.avbpubkey",
3149 private_key: "testkey.pem",
3150 }
3151
3152 cc_library {
3153 name: "mylib",
3154 vendor_available: true,
3155 }
3156 `)
3157
3158 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003159 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003160 name := apexBundle.BaseModuleName()
3161 prefix := "TARGET_"
3162 var builder strings.Builder
3163 data.Custom(&builder, name, prefix, "", data)
3164 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00003165 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++.vendor.myapex:64 mylib.vendor.myapex:64 apex_manifest.pb.myapex apex_pubkey.myapex libc.vendor libm.vendor libdl.vendor\n")
Jooyung Hanefb184e2020-06-25 17:14:25 +09003166}
3167
Jooyung Han2ed99d02020-06-24 23:26:26 +09003168func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003169 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003170 apex {
3171 name: "myapex",
3172 key: "myapex.key",
3173 vintf_fragments: ["fragment.xml"],
3174 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003175 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003176 }
3177 apex_key {
3178 name: "myapex.key",
3179 public_key: "testkey.avbpubkey",
3180 private_key: "testkey.pem",
3181 }
3182 cc_binary {
3183 name: "mybin",
3184 }
3185 `)
3186
3187 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003188 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003189 name := apexBundle.BaseModuleName()
3190 prefix := "TARGET_"
3191 var builder strings.Builder
3192 data.Custom(&builder, name, prefix, "", data)
3193 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003194 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003195 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003196}
3197
Jiyong Park16e91a02018-12-20 18:18:08 +09003198func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003199 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003200 apex {
3201 name: "myapex",
3202 key: "myapex.key",
3203 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003204 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003205 }
3206
3207 apex_key {
3208 name: "myapex.key",
3209 public_key: "testkey.avbpubkey",
3210 private_key: "testkey.pem",
3211 }
3212
3213 cc_library {
3214 name: "mylib",
3215 srcs: ["mylib.cpp"],
3216 system_shared_libs: [],
3217 stl: "none",
3218 stubs: {
3219 versions: ["1", "2", "3"],
3220 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003221 apex_available: [
3222 "//apex_available:platform",
3223 "myapex",
3224 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09003225 }
3226
3227 cc_binary {
3228 name: "not_in_apex",
3229 srcs: ["mylib.cpp"],
3230 static_libs: ["mylib"],
3231 static_executable: true,
3232 system_shared_libs: [],
3233 stl: "none",
3234 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003235 `)
3236
Colin Cross7113d202019-11-20 16:39:12 -08003237 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003238
3239 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003240 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003241}
Jiyong Park9335a262018-12-24 11:31:58 +09003242
3243func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003244 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003245 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003246 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003247 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003248 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003249 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003250 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003251 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003252 }
3253
3254 cc_library {
3255 name: "mylib",
3256 srcs: ["mylib.cpp"],
3257 system_shared_libs: [],
3258 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003259 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003260 }
3261
3262 apex_key {
3263 name: "myapex.key",
3264 public_key: "testkey.avbpubkey",
3265 private_key: "testkey.pem",
3266 }
3267
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003268 android_app_certificate {
3269 name: "myapex.certificate",
3270 certificate: "testkey",
3271 }
3272
3273 android_app_certificate {
3274 name: "myapex.certificate.override",
3275 certificate: "testkey.override",
3276 }
3277
Jiyong Park9335a262018-12-24 11:31:58 +09003278 `)
3279
3280 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003281 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003282
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003283 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3284 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003285 "vendor/foo/devkeys/testkey.avbpubkey")
3286 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003287 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3288 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003289 "vendor/foo/devkeys/testkey.pem")
3290 }
3291
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003292 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003293 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003294 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003295 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003296 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003297 }
3298}
Jiyong Park58e364a2019-01-19 19:24:06 +09003299
Jooyung Hanf121a652019-12-17 14:30:11 +09003300func TestCertificate(t *testing.T) {
3301 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003302 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003303 apex {
3304 name: "myapex",
3305 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003306 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003307 }
3308 apex_key {
3309 name: "myapex.key",
3310 public_key: "testkey.avbpubkey",
3311 private_key: "testkey.pem",
3312 }`)
3313 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3314 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3315 if actual := rule.Args["certificates"]; actual != expected {
3316 t.Errorf("certificates should be %q, not %q", expected, actual)
3317 }
3318 })
3319 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003320 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003321 apex {
3322 name: "myapex_keytest",
3323 key: "myapex.key",
3324 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003325 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003326 }
3327 apex_key {
3328 name: "myapex.key",
3329 public_key: "testkey.avbpubkey",
3330 private_key: "testkey.pem",
3331 }
3332 android_app_certificate {
3333 name: "myapex.certificate.override",
3334 certificate: "testkey.override",
3335 }`)
3336 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3337 expected := "testkey.override.x509.pem testkey.override.pk8"
3338 if actual := rule.Args["certificates"]; actual != expected {
3339 t.Errorf("certificates should be %q, not %q", expected, actual)
3340 }
3341 })
3342 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003343 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003344 apex {
3345 name: "myapex",
3346 key: "myapex.key",
3347 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003348 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003349 }
3350 apex_key {
3351 name: "myapex.key",
3352 public_key: "testkey.avbpubkey",
3353 private_key: "testkey.pem",
3354 }
3355 android_app_certificate {
3356 name: "myapex.certificate",
3357 certificate: "testkey",
3358 }`)
3359 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3360 expected := "testkey.x509.pem testkey.pk8"
3361 if actual := rule.Args["certificates"]; actual != expected {
3362 t.Errorf("certificates should be %q, not %q", expected, actual)
3363 }
3364 })
3365 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003366 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003367 apex {
3368 name: "myapex_keytest",
3369 key: "myapex.key",
3370 file_contexts: ":myapex-file_contexts",
3371 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003372 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003373 }
3374 apex_key {
3375 name: "myapex.key",
3376 public_key: "testkey.avbpubkey",
3377 private_key: "testkey.pem",
3378 }
3379 android_app_certificate {
3380 name: "myapex.certificate.override",
3381 certificate: "testkey.override",
3382 }`)
3383 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3384 expected := "testkey.override.x509.pem testkey.override.pk8"
3385 if actual := rule.Args["certificates"]; actual != expected {
3386 t.Errorf("certificates should be %q, not %q", expected, actual)
3387 }
3388 })
3389 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003390 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003391 apex {
3392 name: "myapex",
3393 key: "myapex.key",
3394 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003395 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003396 }
3397 apex_key {
3398 name: "myapex.key",
3399 public_key: "testkey.avbpubkey",
3400 private_key: "testkey.pem",
3401 }`)
3402 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3403 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3404 if actual := rule.Args["certificates"]; actual != expected {
3405 t.Errorf("certificates should be %q, not %q", expected, actual)
3406 }
3407 })
3408 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003409 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003410 apex {
3411 name: "myapex_keytest",
3412 key: "myapex.key",
3413 file_contexts: ":myapex-file_contexts",
3414 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003415 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003416 }
3417 apex_key {
3418 name: "myapex.key",
3419 public_key: "testkey.avbpubkey",
3420 private_key: "testkey.pem",
3421 }
3422 android_app_certificate {
3423 name: "myapex.certificate.override",
3424 certificate: "testkey.override",
3425 }`)
3426 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3427 expected := "testkey.override.x509.pem testkey.override.pk8"
3428 if actual := rule.Args["certificates"]; actual != expected {
3429 t.Errorf("certificates should be %q, not %q", expected, actual)
3430 }
3431 })
3432}
3433
Jiyong Park58e364a2019-01-19 19:24:06 +09003434func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003435 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003436 apex {
3437 name: "myapex",
3438 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003439 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003440 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003441 }
3442
3443 apex {
3444 name: "otherapex",
3445 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003446 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003447 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003448 }
3449
3450 apex_key {
3451 name: "myapex.key",
3452 public_key: "testkey.avbpubkey",
3453 private_key: "testkey.pem",
3454 }
3455
3456 cc_library {
3457 name: "mylib",
3458 srcs: ["mylib.cpp"],
3459 system_shared_libs: [],
3460 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003461 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003462 "myapex",
3463 "otherapex",
3464 ],
Jooyung Han24282772020-03-21 23:20:55 +09003465 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003466 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003467 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003468 cc_library {
3469 name: "mylib2",
3470 srcs: ["mylib.cpp"],
3471 system_shared_libs: [],
3472 stl: "none",
3473 apex_available: [
3474 "myapex",
3475 "otherapex",
3476 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003477 static_libs: ["mylib3"],
3478 recovery_available: true,
3479 min_sdk_version: "29",
3480 }
3481 cc_library {
3482 name: "mylib3",
3483 srcs: ["mylib.cpp"],
3484 system_shared_libs: [],
3485 stl: "none",
3486 apex_available: [
3487 "myapex",
3488 "otherapex",
3489 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003490 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003491 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003492 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003493 `)
3494
Jooyung Hanc87a0592020-03-02 17:44:33 +09003495 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003496 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003497 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003498
Vinh Tranf9754732023-01-19 22:41:46 -05003499 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003500 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003501 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003502
Vinh Tranf9754732023-01-19 22:41:46 -05003503 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003504 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003505 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003506
Colin Crossaede88c2020-08-11 12:17:01 -07003507 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3508 // each variant defines additional macros to distinguish which apex variant it is built for
3509
3510 // non-APEX variant does not have __ANDROID_APEX__ defined
3511 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3512 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3513
Vinh Tranf9754732023-01-19 22:41:46 -05003514 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003515 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3516 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003517
Jooyung Hanc87a0592020-03-02 17:44:33 +09003518 // non-APEX variant does not have __ANDROID_APEX__ defined
3519 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3520 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3521
Vinh Tranf9754732023-01-19 22:41:46 -05003522 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003523 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003524 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003525}
Jiyong Park7e636d02019-01-28 16:16:54 +09003526
3527func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003528 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003529 apex {
3530 name: "myapex",
3531 key: "myapex.key",
3532 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003533 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003534 }
3535
3536 apex_key {
3537 name: "myapex.key",
3538 public_key: "testkey.avbpubkey",
3539 private_key: "testkey.pem",
3540 }
3541
3542 cc_library_headers {
3543 name: "mylib_headers",
3544 export_include_dirs: ["my_include"],
3545 system_shared_libs: [],
3546 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003547 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003548 }
3549
3550 cc_library {
3551 name: "mylib",
3552 srcs: ["mylib.cpp"],
3553 system_shared_libs: [],
3554 stl: "none",
3555 header_libs: ["mylib_headers"],
3556 export_header_lib_headers: ["mylib_headers"],
3557 stubs: {
3558 versions: ["1", "2", "3"],
3559 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003560 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003561 }
3562
3563 cc_library {
3564 name: "otherlib",
3565 srcs: ["mylib.cpp"],
3566 system_shared_libs: [],
3567 stl: "none",
3568 shared_libs: ["mylib"],
3569 }
3570 `)
3571
Colin Cross7113d202019-11-20 16:39:12 -08003572 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003573
3574 // Ensure that the include path of the header lib is exported to 'otherlib'
3575 ensureContains(t, cFlags, "-Imy_include")
3576}
Alex Light9670d332019-01-29 18:07:33 -08003577
Jiyong Park7cd10e32020-01-14 09:22:18 +09003578type fileInApex struct {
3579 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003580 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003581 isLink bool
3582}
3583
Jooyung Han1724d582022-12-21 10:17:44 +09003584func (f fileInApex) String() string {
3585 return f.src + ":" + f.path
3586}
3587
3588func (f fileInApex) match(expectation string) bool {
3589 parts := strings.Split(expectation, ":")
3590 if len(parts) == 1 {
3591 match, _ := path.Match(parts[0], f.path)
3592 return match
3593 }
3594 if len(parts) == 2 {
3595 matchSrc, _ := path.Match(parts[0], f.src)
3596 matchDst, _ := path.Match(parts[1], f.path)
3597 return matchSrc && matchDst
3598 }
3599 panic("invalid expected file specification: " + expectation)
3600}
3601
Jooyung Hana57af4a2020-01-23 05:36:59 +00003602func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003603 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003604 module := ctx.ModuleForTests(moduleName, variant)
3605 apexRule := module.MaybeRule("apexRule")
3606 apexDir := "/image.apex/"
3607 if apexRule.Rule == nil {
3608 apexRule = module.Rule("zipApexRule")
3609 apexDir = "/image.zipapex/"
3610 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003611 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003612 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003613 for _, cmd := range strings.Split(copyCmds, "&&") {
3614 cmd = strings.TrimSpace(cmd)
3615 if cmd == "" {
3616 continue
3617 }
3618 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003619 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003620 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003621 switch terms[0] {
3622 case "mkdir":
3623 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003624 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003625 t.Fatal("copyCmds contains invalid cp command", cmd)
3626 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003627 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003628 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003629 isLink = false
3630 case "ln":
3631 if len(terms) != 3 && len(terms) != 4 {
3632 // ln LINK TARGET or ln -s LINK TARGET
3633 t.Fatal("copyCmds contains invalid ln command", cmd)
3634 }
3635 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003636 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003637 isLink = true
3638 default:
3639 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3640 }
3641 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003642 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003643 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003644 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003645 }
Jooyung Han1724d582022-12-21 10:17:44 +09003646 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003647 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003648 }
3649 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003650 return ret
3651}
3652
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003653func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003654 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003655 var failed bool
3656 var surplus []string
3657 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003658 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003659 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003660 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003661 if file.match(expected) {
3662 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003663 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003664 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003665 }
3666 }
Jooyung Han1724d582022-12-21 10:17:44 +09003667 if !matchFound {
3668 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003669 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003670 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003671
Jooyung Han31c470b2019-10-18 16:26:59 +09003672 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003673 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003674 t.Log("surplus files", surplus)
3675 failed = true
3676 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003677
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003678 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003679 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003680 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003681 if !filesMatched[expected] {
3682 missing = append(missing, expected)
3683 }
3684 }
3685 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003686 t.Log("missing files", missing)
3687 failed = true
3688 }
3689 if failed {
3690 t.Fail()
3691 }
3692}
3693
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003694func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3695 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3696}
3697
3698func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
3699 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Rule("deapexer")
3700 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3701 if deapexer.Output != nil {
3702 outputs = append(outputs, deapexer.Output.String())
3703 }
3704 for _, output := range deapexer.ImplicitOutputs {
3705 outputs = append(outputs, output.String())
3706 }
3707 actualFiles := make([]fileInApex, 0, len(outputs))
3708 for _, output := range outputs {
3709 dir := "/deapexer/"
3710 pos := strings.LastIndex(output, dir)
3711 if pos == -1 {
3712 t.Fatal("Unknown deapexer output ", output)
3713 }
3714 path := output[pos+len(dir):]
3715 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3716 }
3717 assertFileListEquals(t, files, actualFiles)
3718}
3719
Jooyung Han344d5432019-08-23 11:17:39 +09003720func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003721 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003722 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003723 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003724 "etc/llndk.libraries.29.txt",
3725 "etc/vndkcore.libraries.29.txt",
3726 "etc/vndksp.libraries.29.txt",
3727 "etc/vndkprivate.libraries.29.txt",
3728 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003729 }
3730 testCases := []struct {
3731 vndkVersion string
3732 expectedFiles []string
3733 }{
3734 {
3735 vndkVersion: "current",
3736 expectedFiles: append(commonFiles,
3737 "lib/libvndk.so",
3738 "lib/libvndksp.so",
3739 "lib64/libvndk.so",
3740 "lib64/libvndksp.so"),
3741 },
3742 {
3743 vndkVersion: "",
3744 expectedFiles: append(commonFiles,
3745 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3746 "lib/libvndksp.so",
3747 "lib64/libvndksp.so"),
3748 },
3749 }
3750 for _, tc := range testCases {
3751 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3752 ctx := testApex(t, `
3753 apex_vndk {
3754 name: "com.android.vndk.current",
3755 key: "com.android.vndk.current.key",
3756 updatable: false,
3757 }
3758
3759 apex_key {
3760 name: "com.android.vndk.current.key",
3761 public_key: "testkey.avbpubkey",
3762 private_key: "testkey.pem",
3763 }
3764
3765 cc_library {
3766 name: "libvndk",
3767 srcs: ["mylib.cpp"],
3768 vendor_available: true,
3769 product_available: true,
3770 vndk: {
3771 enabled: true,
3772 },
3773 system_shared_libs: [],
3774 stl: "none",
3775 apex_available: [ "com.android.vndk.current" ],
3776 }
3777
3778 cc_library {
3779 name: "libvndksp",
3780 srcs: ["mylib.cpp"],
3781 vendor_available: true,
3782 product_available: true,
3783 vndk: {
3784 enabled: true,
3785 support_system_process: true,
3786 },
3787 system_shared_libs: [],
3788 stl: "none",
3789 apex_available: [ "com.android.vndk.current" ],
3790 }
3791
3792 // VNDK-Ext should not cause any problems
3793
3794 cc_library {
3795 name: "libvndk.ext",
3796 srcs: ["mylib2.cpp"],
3797 vendor: true,
3798 vndk: {
3799 enabled: true,
3800 extends: "libvndk",
3801 },
3802 system_shared_libs: [],
3803 stl: "none",
3804 }
3805
3806 cc_library {
3807 name: "libvndksp.ext",
3808 srcs: ["mylib2.cpp"],
3809 vendor: true,
3810 vndk: {
3811 enabled: true,
3812 support_system_process: true,
3813 extends: "libvndksp",
3814 },
3815 system_shared_libs: [],
3816 stl: "none",
3817 }
3818 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3819 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3820 }))
3821 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3822 })
3823 }
Jooyung Han344d5432019-08-23 11:17:39 +09003824}
3825
3826func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003827 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003828 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003829 name: "com.android.vndk.current",
3830 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003831 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003832 }
3833
3834 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003835 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003836 public_key: "testkey.avbpubkey",
3837 private_key: "testkey.pem",
3838 }
3839
3840 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003841 name: "libvndk",
3842 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003843 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003844 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003845 vndk: {
3846 enabled: true,
3847 },
3848 system_shared_libs: [],
3849 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003850 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003851 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003852
3853 cc_prebuilt_library_shared {
3854 name: "libvndk.arm",
3855 srcs: ["libvndk.arm.so"],
3856 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003857 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003858 vndk: {
3859 enabled: true,
3860 },
3861 enabled: false,
3862 arch: {
3863 arm: {
3864 enabled: true,
3865 },
3866 },
3867 system_shared_libs: [],
3868 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003869 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003870 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003871 `+vndkLibrariesTxtFiles("current"),
3872 withFiles(map[string][]byte{
3873 "libvndk.so": nil,
3874 "libvndk.arm.so": nil,
3875 }))
Colin Cross2807f002021-03-02 10:15:29 -08003876 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003877 "lib/libvndk.so",
3878 "lib/libvndk.arm.so",
3879 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003880 "lib/libc++.so",
3881 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003882 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003883 })
Jooyung Han344d5432019-08-23 11:17:39 +09003884}
3885
Jooyung Han39edb6c2019-11-06 16:53:07 +09003886func vndkLibrariesTxtFiles(vers ...string) (result string) {
3887 for _, v := range vers {
3888 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003889 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003890 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003891 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003892 name: "` + txt + `.libraries.txt",
3893 }
3894 `
3895 }
3896 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003897 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003898 result += `
3899 prebuilt_etc {
3900 name: "` + txt + `.libraries.` + v + `.txt",
3901 src: "dummy.txt",
3902 }
3903 `
3904 }
3905 }
3906 }
3907 return
3908}
3909
Jooyung Han344d5432019-08-23 11:17:39 +09003910func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003911 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003912 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003913 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003914 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003915 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003916 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003917 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003918 }
3919
3920 apex_key {
3921 name: "myapex.key",
3922 public_key: "testkey.avbpubkey",
3923 private_key: "testkey.pem",
3924 }
3925
Jooyung Han31c470b2019-10-18 16:26:59 +09003926 vndk_prebuilt_shared {
3927 name: "libvndk27",
3928 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003929 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003930 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003931 vndk: {
3932 enabled: true,
3933 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003934 target_arch: "arm64",
3935 arch: {
3936 arm: {
3937 srcs: ["libvndk27_arm.so"],
3938 },
3939 arm64: {
3940 srcs: ["libvndk27_arm64.so"],
3941 },
3942 },
Colin Cross2807f002021-03-02 10:15:29 -08003943 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003944 }
3945
3946 vndk_prebuilt_shared {
3947 name: "libvndk27",
3948 version: "27",
3949 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003950 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003951 vndk: {
3952 enabled: true,
3953 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003954 target_arch: "x86_64",
3955 arch: {
3956 x86: {
3957 srcs: ["libvndk27_x86.so"],
3958 },
3959 x86_64: {
3960 srcs: ["libvndk27_x86_64.so"],
3961 },
3962 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003963 }
3964 `+vndkLibrariesTxtFiles("27"),
3965 withFiles(map[string][]byte{
3966 "libvndk27_arm.so": nil,
3967 "libvndk27_arm64.so": nil,
3968 "libvndk27_x86.so": nil,
3969 "libvndk27_x86_64.so": nil,
3970 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003971
Colin Cross2807f002021-03-02 10:15:29 -08003972 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003973 "lib/libvndk27_arm.so",
3974 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003975 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003976 })
Jooyung Han344d5432019-08-23 11:17:39 +09003977}
3978
Jooyung Han90eee022019-10-01 20:02:42 +09003979func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003980 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003981 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003982 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003983 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003984 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003985 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003986 }
3987 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003988 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003989 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003990 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003991 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003992 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003993 }
3994 apex_key {
3995 name: "myapex.key",
3996 public_key: "testkey.avbpubkey",
3997 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003998 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003999
4000 assertApexName := func(expected, moduleName string) {
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09004001 module := ctx.ModuleForTests(moduleName, "android_common_image")
4002 apexManifestRule := module.Rule("apexManifestRule")
4003 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09004004 }
4005
Jiyong Parkf58c46e2021-04-01 21:35:20 +09004006 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08004007 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09004008}
4009
Jooyung Han344d5432019-08-23 11:17:39 +09004010func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004011 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09004012 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004013 name: "com.android.vndk.current",
4014 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004015 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004016 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09004017 }
4018
4019 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08004020 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09004021 public_key: "testkey.avbpubkey",
4022 private_key: "testkey.pem",
4023 }
4024
4025 cc_library {
4026 name: "libvndk",
4027 srcs: ["mylib.cpp"],
4028 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004029 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09004030 native_bridge_supported: true,
4031 host_supported: true,
4032 vndk: {
4033 enabled: true,
4034 },
4035 system_shared_libs: [],
4036 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08004037 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09004038 }
Colin Cross2807f002021-03-02 10:15:29 -08004039 `+vndkLibrariesTxtFiles("current"),
4040 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09004041
Colin Cross2807f002021-03-02 10:15:29 -08004042 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09004043 "lib/libvndk.so",
4044 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09004045 "lib/libc++.so",
4046 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004047 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09004048 })
Jooyung Han344d5432019-08-23 11:17:39 +09004049}
4050
4051func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08004052 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09004053 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004054 name: "com.android.vndk.current",
4055 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004056 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09004057 native_bridge_supported: true,
4058 }
4059
4060 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08004061 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09004062 public_key: "testkey.avbpubkey",
4063 private_key: "testkey.pem",
4064 }
4065
4066 cc_library {
4067 name: "libvndk",
4068 srcs: ["mylib.cpp"],
4069 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004070 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09004071 native_bridge_supported: true,
4072 host_supported: true,
4073 vndk: {
4074 enabled: true,
4075 },
4076 system_shared_libs: [],
4077 stl: "none",
4078 }
4079 `)
4080}
4081
Jooyung Han31c470b2019-10-18 16:26:59 +09004082func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004083 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09004084 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004085 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09004086 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004087 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09004088 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004089 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09004090 }
4091
4092 apex_key {
4093 name: "myapex.key",
4094 public_key: "testkey.avbpubkey",
4095 private_key: "testkey.pem",
4096 }
4097
4098 vndk_prebuilt_shared {
4099 name: "libvndk27",
4100 version: "27",
4101 target_arch: "arm",
4102 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004103 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004104 vndk: {
4105 enabled: true,
4106 },
4107 arch: {
4108 arm: {
4109 srcs: ["libvndk27.so"],
4110 }
4111 },
4112 }
4113
4114 vndk_prebuilt_shared {
4115 name: "libvndk27",
4116 version: "27",
4117 target_arch: "arm",
4118 binder32bit: true,
4119 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004120 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004121 vndk: {
4122 enabled: true,
4123 },
4124 arch: {
4125 arm: {
4126 srcs: ["libvndk27binder32.so"],
4127 }
4128 },
Colin Cross2807f002021-03-02 10:15:29 -08004129 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09004130 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09004131 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09004132 withFiles(map[string][]byte{
4133 "libvndk27.so": nil,
4134 "libvndk27binder32.so": nil,
4135 }),
4136 withBinder32bit,
4137 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07004138 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09004139 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
4140 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09004141 },
4142 }),
4143 )
4144
Colin Cross2807f002021-03-02 10:15:29 -08004145 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09004146 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004147 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09004148 })
4149}
4150
Jooyung Han45a96772020-06-15 14:59:42 +09004151func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004152 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09004153 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004154 name: "com.android.vndk.current",
4155 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004156 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004157 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09004158 }
4159
4160 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08004161 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004162 public_key: "testkey.avbpubkey",
4163 private_key: "testkey.pem",
4164 }
4165
4166 cc_library {
4167 name: "libz",
4168 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004169 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09004170 vndk: {
4171 enabled: true,
4172 },
4173 stubs: {
4174 symbol_file: "libz.map.txt",
4175 versions: ["30"],
4176 }
4177 }
4178 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
4179 "libz.map.txt": nil,
4180 }))
4181
Colin Cross2807f002021-03-02 10:15:29 -08004182 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09004183 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
4184 ensureListEmpty(t, provideNativeLibs)
Jooyung Han1724d582022-12-21 10:17:44 +09004185 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
4186 "out/soong/.intermediates/libz/android_vendor.29_arm64_armv8-a_shared/libz.so:lib64/libz.so",
4187 "out/soong/.intermediates/libz/android_vendor.29_arm_armv7-a-neon_shared/libz.so:lib/libz.so",
4188 "*/*",
4189 })
Jooyung Han45a96772020-06-15 14:59:42 +09004190}
4191
Jooyung Hane1633032019-08-01 17:41:43 +09004192func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004193 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09004194 apex {
4195 name: "myapex_nodep",
4196 key: "myapex.key",
4197 native_shared_libs: ["lib_nodep"],
4198 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004199 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004200 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004201 }
4202
4203 apex {
4204 name: "myapex_dep",
4205 key: "myapex.key",
4206 native_shared_libs: ["lib_dep"],
4207 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004208 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004209 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004210 }
4211
4212 apex {
4213 name: "myapex_provider",
4214 key: "myapex.key",
4215 native_shared_libs: ["libfoo"],
4216 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004217 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004218 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004219 }
4220
4221 apex {
4222 name: "myapex_selfcontained",
4223 key: "myapex.key",
4224 native_shared_libs: ["lib_dep", "libfoo"],
4225 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004226 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004227 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004228 }
4229
4230 apex_key {
4231 name: "myapex.key",
4232 public_key: "testkey.avbpubkey",
4233 private_key: "testkey.pem",
4234 }
4235
4236 cc_library {
4237 name: "lib_nodep",
4238 srcs: ["mylib.cpp"],
4239 system_shared_libs: [],
4240 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004241 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004242 }
4243
4244 cc_library {
4245 name: "lib_dep",
4246 srcs: ["mylib.cpp"],
4247 shared_libs: ["libfoo"],
4248 system_shared_libs: [],
4249 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004250 apex_available: [
4251 "myapex_dep",
4252 "myapex_provider",
4253 "myapex_selfcontained",
4254 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004255 }
4256
4257 cc_library {
4258 name: "libfoo",
4259 srcs: ["mytest.cpp"],
4260 stubs: {
4261 versions: ["1"],
4262 },
4263 system_shared_libs: [],
4264 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004265 apex_available: [
4266 "myapex_provider",
4267 "myapex_selfcontained",
4268 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004269 }
4270 `)
4271
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004272 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004273 var provideNativeLibs, requireNativeLibs []string
4274
Sundong Ahnabb64432019-10-22 13:58:29 +09004275 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004276 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4277 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004278 ensureListEmpty(t, provideNativeLibs)
4279 ensureListEmpty(t, requireNativeLibs)
4280
Sundong Ahnabb64432019-10-22 13:58:29 +09004281 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004282 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4283 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004284 ensureListEmpty(t, provideNativeLibs)
4285 ensureListContains(t, requireNativeLibs, "libfoo.so")
4286
Sundong Ahnabb64432019-10-22 13:58:29 +09004287 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004288 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4289 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004290 ensureListContains(t, provideNativeLibs, "libfoo.so")
4291 ensureListEmpty(t, requireNativeLibs)
4292
Sundong Ahnabb64432019-10-22 13:58:29 +09004293 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004294 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4295 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004296 ensureListContains(t, provideNativeLibs, "libfoo.so")
4297 ensureListEmpty(t, requireNativeLibs)
4298}
4299
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004300func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4301 ctx := testApex(t, `
4302 apex {
4303 name: "myapex",
4304 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004305 native_shared_libs: ["mylib"],
4306 updatable: false,
4307 }
4308
4309 apex_key {
4310 name: "myapex.key",
4311 public_key: "testkey.avbpubkey",
4312 private_key: "testkey.pem",
4313 }
4314
4315 cc_library {
4316 name: "mylib",
4317 srcs: ["mylib.cpp"],
4318 system_shared_libs: [],
4319 stl: "none",
4320 apex_available: [
4321 "//apex_available:platform",
4322 "myapex",
4323 ],
4324 }
4325 `, android.FixtureMergeEnv(map[string]string{
4326 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4327 }))
4328
Jooyung Han63dff462023-02-09 00:11:27 +00004329 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004330 apexManifestRule := module.Rule("apexManifestRule")
4331 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4332}
4333
Vinh Tran8f5310f2022-10-07 18:16:47 -04004334func TestCompileMultilibProp(t *testing.T) {
4335 testCases := []struct {
4336 compileMultiLibProp string
4337 containedLibs []string
4338 notContainedLibs []string
4339 }{
4340 {
4341 containedLibs: []string{
4342 "image.apex/lib64/mylib.so",
4343 "image.apex/lib/mylib.so",
4344 },
4345 compileMultiLibProp: `compile_multilib: "both",`,
4346 },
4347 {
4348 containedLibs: []string{"image.apex/lib64/mylib.so"},
4349 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4350 compileMultiLibProp: `compile_multilib: "first",`,
4351 },
4352 {
4353 containedLibs: []string{"image.apex/lib64/mylib.so"},
4354 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4355 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4356 },
4357 {
4358 containedLibs: []string{"image.apex/lib64/mylib.so"},
4359 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4360 compileMultiLibProp: `compile_multilib: "64",`,
4361 },
4362 {
4363 containedLibs: []string{"image.apex/lib/mylib.so"},
4364 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4365 compileMultiLibProp: `compile_multilib: "32",`,
4366 },
4367 }
4368 for _, testCase := range testCases {
4369 ctx := testApex(t, fmt.Sprintf(`
4370 apex {
4371 name: "myapex",
4372 key: "myapex.key",
4373 %s
4374 native_shared_libs: ["mylib"],
4375 updatable: false,
4376 }
4377 apex_key {
4378 name: "myapex.key",
4379 public_key: "testkey.avbpubkey",
4380 private_key: "testkey.pem",
4381 }
4382 cc_library {
4383 name: "mylib",
4384 srcs: ["mylib.cpp"],
4385 apex_available: [
4386 "//apex_available:platform",
4387 "myapex",
4388 ],
4389 }
4390 `, testCase.compileMultiLibProp),
4391 )
4392 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4393 apexRule := module.Rule("apexRule")
4394 copyCmds := apexRule.Args["copy_commands"]
4395 for _, containedLib := range testCase.containedLibs {
4396 ensureContains(t, copyCmds, containedLib)
4397 }
4398 for _, notContainedLib := range testCase.notContainedLibs {
4399 ensureNotContains(t, copyCmds, notContainedLib)
4400 }
4401 }
4402}
4403
Alex Light0851b882019-02-07 13:20:53 -08004404func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004405 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004406 apex {
4407 name: "myapex",
4408 key: "myapex.key",
4409 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004410 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004411 }
4412
4413 apex_key {
4414 name: "myapex.key",
4415 public_key: "testkey.avbpubkey",
4416 private_key: "testkey.pem",
4417 }
4418
4419 cc_library {
4420 name: "mylib_common",
4421 srcs: ["mylib.cpp"],
4422 system_shared_libs: [],
4423 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004424 apex_available: [
4425 "//apex_available:platform",
4426 "myapex",
4427 ],
Alex Light0851b882019-02-07 13:20:53 -08004428 }
4429 `)
4430
Sundong Ahnabb64432019-10-22 13:58:29 +09004431 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004432 apexRule := module.Rule("apexRule")
4433 copyCmds := apexRule.Args["copy_commands"]
4434
4435 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4436 t.Log("Apex was a test apex!")
4437 t.Fail()
4438 }
4439 // Ensure that main rule creates an output
4440 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4441
4442 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004443 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004444
4445 // Ensure that both direct and indirect deps are copied into apex
4446 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4447
Colin Cross7113d202019-11-20 16:39:12 -08004448 // Ensure that the platform variant ends with _shared
4449 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004450
Colin Cross56a83212020-09-15 18:30:11 -07004451 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004452 t.Log("Found mylib_common not in any apex!")
4453 t.Fail()
4454 }
4455}
4456
4457func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004458 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004459 apex_test {
4460 name: "myapex",
4461 key: "myapex.key",
4462 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004463 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004464 }
4465
4466 apex_key {
4467 name: "myapex.key",
4468 public_key: "testkey.avbpubkey",
4469 private_key: "testkey.pem",
4470 }
4471
4472 cc_library {
4473 name: "mylib_common_test",
4474 srcs: ["mylib.cpp"],
4475 system_shared_libs: [],
4476 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004477 // TODO: remove //apex_available:platform
4478 apex_available: [
4479 "//apex_available:platform",
4480 "myapex",
4481 ],
Alex Light0851b882019-02-07 13:20:53 -08004482 }
4483 `)
4484
Sundong Ahnabb64432019-10-22 13:58:29 +09004485 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004486 apexRule := module.Rule("apexRule")
4487 copyCmds := apexRule.Args["copy_commands"]
4488
4489 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4490 t.Log("Apex was not a test apex!")
4491 t.Fail()
4492 }
4493 // Ensure that main rule creates an output
4494 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4495
4496 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004497 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004498
4499 // Ensure that both direct and indirect deps are copied into apex
4500 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4501
Colin Cross7113d202019-11-20 16:39:12 -08004502 // Ensure that the platform variant ends with _shared
4503 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004504}
4505
Alex Light9670d332019-01-29 18:07:33 -08004506func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004507 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004508 apex {
4509 name: "myapex",
4510 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004511 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004512 multilib: {
4513 first: {
4514 native_shared_libs: ["mylib_common"],
4515 }
4516 },
4517 target: {
4518 android: {
4519 multilib: {
4520 first: {
4521 native_shared_libs: ["mylib"],
4522 }
4523 }
4524 },
4525 host: {
4526 multilib: {
4527 first: {
4528 native_shared_libs: ["mylib2"],
4529 }
4530 }
4531 }
4532 }
4533 }
4534
4535 apex_key {
4536 name: "myapex.key",
4537 public_key: "testkey.avbpubkey",
4538 private_key: "testkey.pem",
4539 }
4540
4541 cc_library {
4542 name: "mylib",
4543 srcs: ["mylib.cpp"],
4544 system_shared_libs: [],
4545 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004546 // TODO: remove //apex_available:platform
4547 apex_available: [
4548 "//apex_available:platform",
4549 "myapex",
4550 ],
Alex Light9670d332019-01-29 18:07:33 -08004551 }
4552
4553 cc_library {
4554 name: "mylib_common",
4555 srcs: ["mylib.cpp"],
4556 system_shared_libs: [],
4557 stl: "none",
4558 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004559 // TODO: remove //apex_available:platform
4560 apex_available: [
4561 "//apex_available:platform",
4562 "myapex",
4563 ],
Alex Light9670d332019-01-29 18:07:33 -08004564 }
4565
4566 cc_library {
4567 name: "mylib2",
4568 srcs: ["mylib.cpp"],
4569 system_shared_libs: [],
4570 stl: "none",
4571 compile_multilib: "first",
4572 }
4573 `)
4574
Sundong Ahnabb64432019-10-22 13:58:29 +09004575 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004576 copyCmds := apexRule.Args["copy_commands"]
4577
4578 // Ensure that main rule creates an output
4579 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4580
4581 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004582 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4583 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4584 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004585
4586 // Ensure that both direct and indirect deps are copied into apex
4587 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4588 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4589 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4590
Colin Cross7113d202019-11-20 16:39:12 -08004591 // Ensure that the platform variant ends with _shared
4592 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4593 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4594 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004595}
Jiyong Park04480cf2019-02-06 00:16:29 +09004596
Jiyong Park59140302020-12-14 18:44:04 +09004597func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004598 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004599 apex {
4600 name: "myapex",
4601 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004602 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004603 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004604 arch: {
4605 arm64: {
4606 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004607 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004608 },
4609 x86_64: {
4610 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004611 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004612 },
4613 }
4614 }
4615
4616 apex_key {
4617 name: "myapex.key",
4618 public_key: "testkey.avbpubkey",
4619 private_key: "testkey.pem",
4620 }
4621
4622 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004623 name: "mylib.generic",
4624 srcs: ["mylib.cpp"],
4625 system_shared_libs: [],
4626 stl: "none",
4627 // TODO: remove //apex_available:platform
4628 apex_available: [
4629 "//apex_available:platform",
4630 "myapex",
4631 ],
4632 }
4633
4634 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004635 name: "mylib.arm64",
4636 srcs: ["mylib.cpp"],
4637 system_shared_libs: [],
4638 stl: "none",
4639 // TODO: remove //apex_available:platform
4640 apex_available: [
4641 "//apex_available:platform",
4642 "myapex",
4643 ],
4644 }
4645
4646 cc_library {
4647 name: "mylib.x64",
4648 srcs: ["mylib.cpp"],
4649 system_shared_libs: [],
4650 stl: "none",
4651 // TODO: remove //apex_available:platform
4652 apex_available: [
4653 "//apex_available:platform",
4654 "myapex",
4655 ],
4656 }
4657 `)
4658
4659 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4660 copyCmds := apexRule.Args["copy_commands"]
4661
4662 // Ensure that apex variant is created for the direct dep
4663 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004664 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004665 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4666
4667 // Ensure that both direct and indirect deps are copied into apex
4668 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4669 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4670}
4671
Jiyong Park04480cf2019-02-06 00:16:29 +09004672func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004673 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004674 apex {
4675 name: "myapex",
4676 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004677 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004678 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004679 }
4680
4681 apex_key {
4682 name: "myapex.key",
4683 public_key: "testkey.avbpubkey",
4684 private_key: "testkey.pem",
4685 }
4686
4687 sh_binary {
4688 name: "myscript",
4689 src: "mylib.cpp",
4690 filename: "myscript.sh",
4691 sub_dir: "script",
4692 }
4693 `)
4694
Sundong Ahnabb64432019-10-22 13:58:29 +09004695 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004696 copyCmds := apexRule.Args["copy_commands"]
4697
4698 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4699}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004700
Jooyung Han91df2082019-11-20 01:49:42 +09004701func TestApexInVariousPartition(t *testing.T) {
4702 testcases := []struct {
4703 propName, parition, flattenedPartition string
4704 }{
4705 {"", "system", "system_ext"},
4706 {"product_specific: true", "product", "product"},
4707 {"soc_specific: true", "vendor", "vendor"},
4708 {"proprietary: true", "vendor", "vendor"},
4709 {"vendor: true", "vendor", "vendor"},
4710 {"system_ext_specific: true", "system_ext", "system_ext"},
4711 }
4712 for _, tc := range testcases {
4713 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004714 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004715 apex {
4716 name: "myapex",
4717 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004718 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004719 `+tc.propName+`
4720 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004721
Jooyung Han91df2082019-11-20 01:49:42 +09004722 apex_key {
4723 name: "myapex.key",
4724 public_key: "testkey.avbpubkey",
4725 private_key: "testkey.pem",
4726 }
4727 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004728
Jooyung Han91df2082019-11-20 01:49:42 +09004729 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004730 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4731 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004732 if actual != expected {
4733 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4734 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004735
Jooyung Han91df2082019-11-20 01:49:42 +09004736 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004737 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4738 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004739 if actual != expected {
4740 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4741 }
4742 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004743 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004744}
Jiyong Park67882562019-03-21 01:11:21 +09004745
Jooyung Han580eb4f2020-06-24 19:33:06 +09004746func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004747 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004748 apex {
4749 name: "myapex",
4750 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004751 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004752 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004753
Jooyung Han580eb4f2020-06-24 19:33:06 +09004754 apex_key {
4755 name: "myapex.key",
4756 public_key: "testkey.avbpubkey",
4757 private_key: "testkey.pem",
4758 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004759 `)
4760 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004761 rule := module.Output("file_contexts")
4762 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4763}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004764
Jooyung Han580eb4f2020-06-24 19:33:06 +09004765func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004766 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004767 apex {
4768 name: "myapex",
4769 key: "myapex.key",
4770 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004771 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004772 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004773
Jooyung Han580eb4f2020-06-24 19:33:06 +09004774 apex_key {
4775 name: "myapex.key",
4776 public_key: "testkey.avbpubkey",
4777 private_key: "testkey.pem",
4778 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004779 `, withFiles(map[string][]byte{
4780 "my_own_file_contexts": nil,
4781 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004782}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004783
Jooyung Han580eb4f2020-06-24 19:33:06 +09004784func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004785 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004786 apex {
4787 name: "myapex",
4788 key: "myapex.key",
4789 product_specific: true,
4790 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004791 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004792 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004793
Jooyung Han580eb4f2020-06-24 19:33:06 +09004794 apex_key {
4795 name: "myapex.key",
4796 public_key: "testkey.avbpubkey",
4797 private_key: "testkey.pem",
4798 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004799 `)
4800
Colin Cross1c460562021-02-16 17:55:47 -08004801 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004802 apex {
4803 name: "myapex",
4804 key: "myapex.key",
4805 product_specific: true,
4806 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004807 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004808 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004809
Jooyung Han580eb4f2020-06-24 19:33:06 +09004810 apex_key {
4811 name: "myapex.key",
4812 public_key: "testkey.avbpubkey",
4813 private_key: "testkey.pem",
4814 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004815 `, withFiles(map[string][]byte{
4816 "product_specific_file_contexts": nil,
4817 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004818 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4819 rule := module.Output("file_contexts")
4820 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4821}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004822
Jooyung Han580eb4f2020-06-24 19:33:06 +09004823func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004824 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004825 apex {
4826 name: "myapex",
4827 key: "myapex.key",
4828 product_specific: true,
4829 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004830 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004831 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004832
Jooyung Han580eb4f2020-06-24 19:33:06 +09004833 apex_key {
4834 name: "myapex.key",
4835 public_key: "testkey.avbpubkey",
4836 private_key: "testkey.pem",
4837 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004838
Jooyung Han580eb4f2020-06-24 19:33:06 +09004839 filegroup {
4840 name: "my-file-contexts",
4841 srcs: ["product_specific_file_contexts"],
4842 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004843 `, withFiles(map[string][]byte{
4844 "product_specific_file_contexts": nil,
4845 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004846 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4847 rule := module.Output("file_contexts")
4848 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004849}
4850
Jiyong Park67882562019-03-21 01:11:21 +09004851func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004852 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004853 apex_key {
4854 name: "myapex.key",
4855 public_key: ":my.avbpubkey",
4856 private_key: ":my.pem",
4857 product_specific: true,
4858 }
4859
4860 filegroup {
4861 name: "my.avbpubkey",
4862 srcs: ["testkey2.avbpubkey"],
4863 }
4864
4865 filegroup {
4866 name: "my.pem",
4867 srcs: ["testkey2.pem"],
4868 }
4869 `)
4870
4871 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4872 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004873 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004874 if actual_pubkey != expected_pubkey {
4875 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4876 }
4877 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004878 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004879 if actual_privkey != expected_privkey {
4880 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4881 }
4882}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004883
4884func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004885 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004886 prebuilt_apex {
4887 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004888 arch: {
4889 arm64: {
4890 src: "myapex-arm64.apex",
4891 },
4892 arm: {
4893 src: "myapex-arm.apex",
4894 },
4895 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004896 }
4897 `)
4898
Wei Li340ee8e2022-03-18 17:33:24 -07004899 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4900 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004901
Jiyong Parkc95714e2019-03-29 14:23:10 +09004902 expectedInput := "myapex-arm64.apex"
4903 if prebuilt.inputApex.String() != expectedInput {
4904 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4905 }
Wei Li340ee8e2022-03-18 17:33:24 -07004906 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4907 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4908 rule := testingModule.Rule("genProvenanceMetaData")
4909 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4910 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4911 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4912 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08004913
4914 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
4915 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004916}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004917
Paul Duffinc0609c62021-03-01 17:27:16 +00004918func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004919 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004920 prebuilt_apex {
4921 name: "myapex",
4922 }
4923 `)
4924}
4925
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004926func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004927 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004928 prebuilt_apex {
4929 name: "myapex",
4930 src: "myapex-arm.apex",
4931 filename: "notmyapex.apex",
4932 }
4933 `)
4934
Wei Li340ee8e2022-03-18 17:33:24 -07004935 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4936 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004937
4938 expected := "notmyapex.apex"
4939 if p.installFilename != expected {
4940 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4941 }
Wei Li340ee8e2022-03-18 17:33:24 -07004942 rule := testingModule.Rule("genProvenanceMetaData")
4943 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4944 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4945 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4946 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004947}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004948
Samiul Islam7c02e262021-09-08 17:48:28 +01004949func TestApexSetFilenameOverride(t *testing.T) {
4950 testApex(t, `
4951 apex_set {
4952 name: "com.company.android.myapex",
4953 apex_name: "com.android.myapex",
4954 set: "company-myapex.apks",
4955 filename: "com.company.android.myapex.apex"
4956 }
4957 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4958
4959 testApex(t, `
4960 apex_set {
4961 name: "com.company.android.myapex",
4962 apex_name: "com.android.myapex",
4963 set: "company-myapex.apks",
4964 filename: "com.company.android.myapex.capex"
4965 }
4966 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4967
4968 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4969 apex_set {
4970 name: "com.company.android.myapex",
4971 apex_name: "com.android.myapex",
4972 set: "company-myapex.apks",
4973 filename: "some-random-suffix"
4974 }
4975 `)
4976}
4977
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004978func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004979 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004980 prebuilt_apex {
4981 name: "myapex.prebuilt",
4982 src: "myapex-arm.apex",
4983 overrides: [
4984 "myapex",
4985 ],
4986 }
4987 `)
4988
Wei Li340ee8e2022-03-18 17:33:24 -07004989 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4990 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004991
4992 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004993 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004994 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004995 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004996 }
Wei Li340ee8e2022-03-18 17:33:24 -07004997 rule := testingModule.Rule("genProvenanceMetaData")
4998 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4999 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
5000 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
5001 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07005002}
5003
Martin Stjernholmbfffae72021-06-24 14:37:13 +01005004func TestPrebuiltApexName(t *testing.T) {
5005 testApex(t, `
5006 prebuilt_apex {
5007 name: "com.company.android.myapex",
5008 apex_name: "com.android.myapex",
5009 src: "company-myapex-arm.apex",
5010 }
5011 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
5012
5013 testApex(t, `
5014 apex_set {
5015 name: "com.company.android.myapex",
5016 apex_name: "com.android.myapex",
5017 set: "company-myapex.apks",
5018 }
5019 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
5020}
5021
5022func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
5023 _ = android.GroupFixturePreparers(
5024 java.PrepareForTestWithJavaDefaultModules,
5025 PrepareForTestWithApexBuildComponents,
5026 android.FixtureWithRootAndroidBp(`
5027 platform_bootclasspath {
5028 name: "platform-bootclasspath",
5029 fragments: [
5030 {
5031 apex: "com.android.art",
5032 module: "art-bootclasspath-fragment",
5033 },
5034 ],
5035 }
5036
5037 prebuilt_apex {
5038 name: "com.company.android.art",
5039 apex_name: "com.android.art",
5040 src: "com.company.android.art-arm.apex",
5041 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
5042 }
5043
5044 prebuilt_bootclasspath_fragment {
5045 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01005046 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01005047 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01005048 hidden_api: {
5049 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5050 metadata: "my-bootclasspath-fragment/metadata.csv",
5051 index: "my-bootclasspath-fragment/index.csv",
5052 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5053 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5054 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01005055 }
5056
5057 java_import {
5058 name: "core-oj",
5059 jars: ["prebuilt.jar"],
5060 }
5061 `),
5062 ).RunTest(t)
5063}
5064
Paul Duffin092153d2021-01-26 11:42:39 +00005065// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
5066// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00005067func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01005068 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00005069
Paul Duffin89886cb2021-02-05 16:44:03 +00005070 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01005071 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00005072 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08005073 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01005074 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00005075 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09005076 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
5077 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
5078 android.NormalizePathForTesting(dexJarBuildPath))
5079 }
5080
5081 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01005082 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09005083 // Make sure the import has been given the correct path to the dex jar.
5084 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
5085 dexJarBuildPath := p.DexJarInstallPath()
5086 stem := android.RemoveOptionalPrebuiltPrefix(name)
5087 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
5088 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
5089 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00005090 }
5091
Paul Duffin39853512021-02-26 11:09:39 +00005092 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01005093 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00005094 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09005095 android.AssertArrayString(t, "Check if there is no source variant",
5096 []string{"android_common"},
5097 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00005098 }
5099
5100 t.Run("prebuilt only", func(t *testing.T) {
5101 bp := `
5102 prebuilt_apex {
5103 name: "myapex",
5104 arch: {
5105 arm64: {
5106 src: "myapex-arm64.apex",
5107 },
5108 arm: {
5109 src: "myapex-arm.apex",
5110 },
5111 },
Paul Duffin39853512021-02-26 11:09:39 +00005112 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005113 }
5114
5115 java_import {
5116 name: "libfoo",
5117 jars: ["libfoo.jar"],
5118 }
Paul Duffin39853512021-02-26 11:09:39 +00005119
5120 java_sdk_library_import {
5121 name: "libbar",
5122 public: {
5123 jars: ["libbar.jar"],
5124 },
5125 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005126 `
5127
5128 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5129 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5130
Martin Stjernholm44825602021-09-17 01:44:12 +01005131 deapexerName := deapexerModuleName("myapex")
5132 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
5133
Paul Duffinf6932af2021-02-26 18:21:56 +00005134 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01005135 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00005136 rule := deapexer.Rule("deapexer")
5137 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
5138 t.Errorf("expected: %q, found: %q", expected, actual)
5139 }
5140
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005141 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01005142 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005143 rule = prebuiltApex.Rule("android/soong/android.Cp")
5144 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
5145 t.Errorf("expected: %q, found: %q", expected, actual)
5146 }
5147
Paul Duffin89886cb2021-02-05 16:44:03 +00005148 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005149 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005150
5151 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005152 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005153 })
5154
5155 t.Run("prebuilt with source preferred", func(t *testing.T) {
5156
5157 bp := `
5158 prebuilt_apex {
5159 name: "myapex",
5160 arch: {
5161 arm64: {
5162 src: "myapex-arm64.apex",
5163 },
5164 arm: {
5165 src: "myapex-arm.apex",
5166 },
5167 },
Paul Duffin39853512021-02-26 11:09:39 +00005168 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005169 }
5170
5171 java_import {
5172 name: "libfoo",
5173 jars: ["libfoo.jar"],
5174 }
5175
5176 java_library {
5177 name: "libfoo",
5178 }
Paul Duffin39853512021-02-26 11:09:39 +00005179
5180 java_sdk_library_import {
5181 name: "libbar",
5182 public: {
5183 jars: ["libbar.jar"],
5184 },
5185 }
5186
5187 java_sdk_library {
5188 name: "libbar",
5189 srcs: ["foo/bar/MyClass.java"],
5190 unsafe_ignore_missing_latest_api: true,
5191 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005192 `
5193
5194 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5195 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5196
Paul Duffin89886cb2021-02-05 16:44:03 +00005197 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005198 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005199 ensureNoSourceVariant(t, ctx, "libfoo")
5200
5201 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005202 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005203 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005204 })
5205
5206 t.Run("prebuilt preferred with source", func(t *testing.T) {
5207 bp := `
5208 prebuilt_apex {
5209 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005210 arch: {
5211 arm64: {
5212 src: "myapex-arm64.apex",
5213 },
5214 arm: {
5215 src: "myapex-arm.apex",
5216 },
5217 },
Paul Duffin39853512021-02-26 11:09:39 +00005218 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005219 }
5220
5221 java_import {
5222 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005223 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005224 jars: ["libfoo.jar"],
5225 }
5226
5227 java_library {
5228 name: "libfoo",
5229 }
Paul Duffin39853512021-02-26 11:09:39 +00005230
5231 java_sdk_library_import {
5232 name: "libbar",
5233 prefer: true,
5234 public: {
5235 jars: ["libbar.jar"],
5236 },
5237 }
5238
5239 java_sdk_library {
5240 name: "libbar",
5241 srcs: ["foo/bar/MyClass.java"],
5242 unsafe_ignore_missing_latest_api: true,
5243 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005244 `
5245
5246 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5247 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5248
Paul Duffin89886cb2021-02-05 16:44:03 +00005249 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005250 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005251 ensureNoSourceVariant(t, ctx, "libfoo")
5252
5253 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005254 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005255 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005256 })
5257}
5258
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005259func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005260 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005261 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005262 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5263 // is disabled.
5264 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
5265 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005266
Paul Duffin37856732021-02-26 14:24:15 +00005267 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5268 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01005269 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005270 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005271 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005272 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005273 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005274 foundLibfooJar = true
5275 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005276 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005277 }
5278 }
5279 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005280 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005281 }
5282 }
5283
Paul Duffin40a3f652021-07-19 13:11:24 +01005284 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005285 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005286 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005287 var rule android.TestingBuildParams
5288
5289 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5290 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005291 }
5292
Paul Duffin40a3f652021-07-19 13:11:24 +01005293 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5294 t.Helper()
5295 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5296 var rule android.TestingBuildParams
5297
5298 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5299 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5300 }
5301
Paul Duffin89f570a2021-06-16 01:42:33 +01005302 fragment := java.ApexVariantReference{
5303 Apex: proptools.StringPtr("myapex"),
5304 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5305 }
5306
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005307 t.Run("prebuilt only", func(t *testing.T) {
5308 bp := `
5309 prebuilt_apex {
5310 name: "myapex",
5311 arch: {
5312 arm64: {
5313 src: "myapex-arm64.apex",
5314 },
5315 arm: {
5316 src: "myapex-arm.apex",
5317 },
5318 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005319 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5320 }
5321
5322 prebuilt_bootclasspath_fragment {
5323 name: "my-bootclasspath-fragment",
5324 contents: ["libfoo", "libbar"],
5325 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005326 hidden_api: {
5327 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5328 metadata: "my-bootclasspath-fragment/metadata.csv",
5329 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005330 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5331 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5332 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005333 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005334 }
5335
5336 java_import {
5337 name: "libfoo",
5338 jars: ["libfoo.jar"],
5339 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005340 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005341 }
Paul Duffin37856732021-02-26 14:24:15 +00005342
5343 java_sdk_library_import {
5344 name: "libbar",
5345 public: {
5346 jars: ["libbar.jar"],
5347 },
5348 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005349 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005350 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005351 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005352 `
5353
Paul Duffin89f570a2021-06-16 01:42:33 +01005354 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005355 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5356 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005357
Paul Duffin537ea3d2021-05-14 10:38:00 +01005358 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005359 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005360 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005361 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005362 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5363 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005364 })
5365
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005366 t.Run("apex_set only", func(t *testing.T) {
5367 bp := `
5368 apex_set {
5369 name: "myapex",
5370 set: "myapex.apks",
Liz Kammer2dc72442023-04-20 10:10:48 -04005371 exported_java_libs: ["myjavalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005372 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
Liz Kammer2dc72442023-04-20 10:10:48 -04005373 exported_systemserverclasspath_fragments: ["my-systemserverclasspath-fragment"],
5374 }
5375
5376 java_import {
5377 name: "myjavalib",
5378 jars: ["myjavalib.jar"],
5379 apex_available: ["myapex"],
5380 permitted_packages: ["javalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005381 }
5382
5383 prebuilt_bootclasspath_fragment {
5384 name: "my-bootclasspath-fragment",
5385 contents: ["libfoo", "libbar"],
5386 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005387 hidden_api: {
5388 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5389 metadata: "my-bootclasspath-fragment/metadata.csv",
5390 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005391 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5392 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5393 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005394 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005395 }
5396
Liz Kammer2dc72442023-04-20 10:10:48 -04005397 prebuilt_systemserverclasspath_fragment {
5398 name: "my-systemserverclasspath-fragment",
5399 contents: ["libbaz"],
5400 apex_available: ["myapex"],
5401 }
5402
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005403 java_import {
5404 name: "libfoo",
5405 jars: ["libfoo.jar"],
5406 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005407 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005408 }
5409
5410 java_sdk_library_import {
5411 name: "libbar",
5412 public: {
5413 jars: ["libbar.jar"],
5414 },
5415 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005416 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005417 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005418 }
Liz Kammer2dc72442023-04-20 10:10:48 -04005419
5420 java_sdk_library_import {
5421 name: "libbaz",
5422 public: {
5423 jars: ["libbaz.jar"],
5424 },
5425 apex_available: ["myapex"],
5426 shared_library: false,
5427 permitted_packages: ["baz"],
5428 }
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005429 `
5430
Paul Duffin89f570a2021-06-16 01:42:33 +01005431 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005432 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5433 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5434
Paul Duffin537ea3d2021-05-14 10:38:00 +01005435 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005436 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005437 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005438 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005439 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5440 `)
Liz Kammer2dc72442023-04-20 10:10:48 -04005441
5442 myApex := ctx.ModuleForTests("myapex", "android_common_myapex").Module()
5443
5444 overrideNames := []string{
5445 "",
5446 "myjavalib.myapex",
5447 "libfoo.myapex",
5448 "libbar.myapex",
5449 "libbaz.myapex",
5450 }
5451 mkEntries := android.AndroidMkEntriesForTest(t, ctx, myApex)
5452 for i, e := range mkEntries {
5453 g := e.OverrideName
5454 if w := overrideNames[i]; w != g {
5455 t.Errorf("Expected override name %q, got %q", w, g)
5456 }
5457 }
5458
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005459 })
5460
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005461 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5462 bp := `
5463 prebuilt_apex {
5464 name: "myapex",
5465 arch: {
5466 arm64: {
5467 src: "myapex-arm64.apex",
5468 },
5469 arm: {
5470 src: "myapex-arm.apex",
5471 },
5472 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005473 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5474 }
5475
5476 prebuilt_bootclasspath_fragment {
5477 name: "my-bootclasspath-fragment",
5478 contents: ["libfoo", "libbar"],
5479 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005480 hidden_api: {
5481 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5482 metadata: "my-bootclasspath-fragment/metadata.csv",
5483 index: "my-bootclasspath-fragment/index.csv",
5484 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5485 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5486 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005487 }
5488
5489 java_import {
5490 name: "libfoo",
5491 jars: ["libfoo.jar"],
5492 apex_available: ["myapex"],
5493 }
5494
5495 java_library {
5496 name: "libfoo",
5497 srcs: ["foo/bar/MyClass.java"],
5498 apex_available: ["myapex"],
5499 }
Paul Duffin37856732021-02-26 14:24:15 +00005500
5501 java_sdk_library_import {
5502 name: "libbar",
5503 public: {
5504 jars: ["libbar.jar"],
5505 },
5506 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005507 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005508 }
5509
5510 java_sdk_library {
5511 name: "libbar",
5512 srcs: ["foo/bar/MyClass.java"],
5513 unsafe_ignore_missing_latest_api: true,
5514 apex_available: ["myapex"],
5515 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005516 `
5517
5518 // In this test the source (java_library) libfoo is active since the
5519 // prebuilt (java_import) defaults to prefer:false. However the
5520 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5521 // find the dex boot jar in it. We either need to disable the source libfoo
5522 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005523 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005524 // dexbootjar check is skipped if AllowMissingDependencies is true
5525 preparerAllowMissingDeps := android.GroupFixturePreparers(
5526 preparer,
5527 android.PrepareForTestWithAllowMissingDependencies,
5528 )
5529 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005530 })
5531
5532 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5533 bp := `
5534 prebuilt_apex {
5535 name: "myapex",
5536 arch: {
5537 arm64: {
5538 src: "myapex-arm64.apex",
5539 },
5540 arm: {
5541 src: "myapex-arm.apex",
5542 },
5543 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005544 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5545 }
5546
5547 prebuilt_bootclasspath_fragment {
5548 name: "my-bootclasspath-fragment",
5549 contents: ["libfoo", "libbar"],
5550 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005551 hidden_api: {
5552 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5553 metadata: "my-bootclasspath-fragment/metadata.csv",
5554 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005555 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5556 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5557 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005558 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005559 }
5560
5561 java_import {
5562 name: "libfoo",
5563 prefer: true,
5564 jars: ["libfoo.jar"],
5565 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005566 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005567 }
5568
5569 java_library {
5570 name: "libfoo",
5571 srcs: ["foo/bar/MyClass.java"],
5572 apex_available: ["myapex"],
5573 }
Paul Duffin37856732021-02-26 14:24:15 +00005574
5575 java_sdk_library_import {
5576 name: "libbar",
5577 prefer: true,
5578 public: {
5579 jars: ["libbar.jar"],
5580 },
5581 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005582 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005583 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005584 }
5585
5586 java_sdk_library {
5587 name: "libbar",
5588 srcs: ["foo/bar/MyClass.java"],
5589 unsafe_ignore_missing_latest_api: true,
5590 apex_available: ["myapex"],
5591 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005592 `
5593
Paul Duffin89f570a2021-06-16 01:42:33 +01005594 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005595 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5596 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005597
Paul Duffin537ea3d2021-05-14 10:38:00 +01005598 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005599 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005600 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005601 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005602 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5603 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005604 })
5605
5606 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5607 bp := `
5608 apex {
5609 name: "myapex",
5610 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005611 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005612 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005613 }
5614
5615 apex_key {
5616 name: "myapex.key",
5617 public_key: "testkey.avbpubkey",
5618 private_key: "testkey.pem",
5619 }
5620
5621 prebuilt_apex {
5622 name: "myapex",
5623 arch: {
5624 arm64: {
5625 src: "myapex-arm64.apex",
5626 },
5627 arm: {
5628 src: "myapex-arm.apex",
5629 },
5630 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005631 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5632 }
5633
5634 prebuilt_bootclasspath_fragment {
5635 name: "my-bootclasspath-fragment",
5636 contents: ["libfoo", "libbar"],
5637 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005638 hidden_api: {
5639 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5640 metadata: "my-bootclasspath-fragment/metadata.csv",
5641 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005642 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5643 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5644 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005645 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005646 }
5647
5648 java_import {
5649 name: "libfoo",
5650 jars: ["libfoo.jar"],
5651 apex_available: ["myapex"],
5652 }
5653
5654 java_library {
5655 name: "libfoo",
5656 srcs: ["foo/bar/MyClass.java"],
5657 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005658 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005659 }
Paul Duffin37856732021-02-26 14:24:15 +00005660
5661 java_sdk_library_import {
5662 name: "libbar",
5663 public: {
5664 jars: ["libbar.jar"],
5665 },
5666 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005667 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005668 }
5669
5670 java_sdk_library {
5671 name: "libbar",
5672 srcs: ["foo/bar/MyClass.java"],
5673 unsafe_ignore_missing_latest_api: true,
5674 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005675 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005676 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005677 `
5678
Paul Duffin89f570a2021-06-16 01:42:33 +01005679 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005680 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5681 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005682
Paul Duffin537ea3d2021-05-14 10:38:00 +01005683 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005684 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005685 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005686 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005687 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5688 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005689 })
5690
5691 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5692 bp := `
5693 apex {
5694 name: "myapex",
5695 enabled: false,
5696 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005697 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005698 }
5699
5700 apex_key {
5701 name: "myapex.key",
5702 public_key: "testkey.avbpubkey",
5703 private_key: "testkey.pem",
5704 }
5705
5706 prebuilt_apex {
5707 name: "myapex",
5708 arch: {
5709 arm64: {
5710 src: "myapex-arm64.apex",
5711 },
5712 arm: {
5713 src: "myapex-arm.apex",
5714 },
5715 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005716 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5717 }
5718
5719 prebuilt_bootclasspath_fragment {
5720 name: "my-bootclasspath-fragment",
5721 contents: ["libfoo", "libbar"],
5722 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005723 hidden_api: {
5724 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5725 metadata: "my-bootclasspath-fragment/metadata.csv",
5726 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005727 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5728 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5729 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005730 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005731 }
5732
5733 java_import {
5734 name: "libfoo",
5735 prefer: true,
5736 jars: ["libfoo.jar"],
5737 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005738 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005739 }
5740
5741 java_library {
5742 name: "libfoo",
5743 srcs: ["foo/bar/MyClass.java"],
5744 apex_available: ["myapex"],
5745 }
Paul Duffin37856732021-02-26 14:24:15 +00005746
5747 java_sdk_library_import {
5748 name: "libbar",
5749 prefer: true,
5750 public: {
5751 jars: ["libbar.jar"],
5752 },
5753 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005754 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005755 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005756 }
5757
5758 java_sdk_library {
5759 name: "libbar",
5760 srcs: ["foo/bar/MyClass.java"],
5761 unsafe_ignore_missing_latest_api: true,
5762 apex_available: ["myapex"],
5763 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005764 `
5765
Paul Duffin89f570a2021-06-16 01:42:33 +01005766 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005767 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5768 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005769
Paul Duffin537ea3d2021-05-14 10:38:00 +01005770 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005771 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005772 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005773 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005774 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5775 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005776 })
5777}
5778
Roland Levillain630846d2019-06-26 12:48:34 +01005779func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005780 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005781 apex_test {
5782 name: "myapex",
5783 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005784 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005785 tests: [
5786 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005787 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005788 ],
5789 }
5790
5791 apex_key {
5792 name: "myapex.key",
5793 public_key: "testkey.avbpubkey",
5794 private_key: "testkey.pem",
5795 }
5796
Liz Kammer1c14a212020-05-12 15:26:55 -07005797 filegroup {
5798 name: "fg",
5799 srcs: [
5800 "baz",
5801 "bar/baz"
5802 ],
5803 }
5804
Roland Levillain630846d2019-06-26 12:48:34 +01005805 cc_test {
5806 name: "mytest",
5807 gtest: false,
5808 srcs: ["mytest.cpp"],
5809 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005810 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005811 system_shared_libs: [],
5812 static_executable: true,
5813 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005814 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005815 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005816
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005817 cc_library {
5818 name: "mylib",
5819 srcs: ["mylib.cpp"],
5820 system_shared_libs: [],
5821 stl: "none",
5822 }
5823
Liz Kammer5bd365f2020-05-27 15:15:11 -07005824 filegroup {
5825 name: "fg2",
5826 srcs: [
5827 "testdata/baz"
5828 ],
5829 }
5830
Roland Levillain9b5fde92019-06-28 15:41:19 +01005831 cc_test {
5832 name: "mytests",
5833 gtest: false,
5834 srcs: [
5835 "mytest1.cpp",
5836 "mytest2.cpp",
5837 "mytest3.cpp",
5838 ],
5839 test_per_src: true,
5840 relative_install_path: "test",
5841 system_shared_libs: [],
5842 static_executable: true,
5843 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005844 data: [
5845 ":fg",
5846 ":fg2",
5847 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005848 }
Roland Levillain630846d2019-06-26 12:48:34 +01005849 `)
5850
Sundong Ahnabb64432019-10-22 13:58:29 +09005851 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005852 copyCmds := apexRule.Args["copy_commands"]
5853
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005854 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005855 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005856 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005857
Liz Kammer1c14a212020-05-12 15:26:55 -07005858 //Ensure that test data are copied into apex.
5859 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5860 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5861
Roland Levillain9b5fde92019-06-28 15:41:19 +01005862 // Ensure that test deps built with `test_per_src` are copied into apex.
5863 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5864 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5865 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005866
5867 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005868 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005869 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005870 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005871 prefix := "TARGET_"
5872 var builder strings.Builder
5873 data.Custom(&builder, name, prefix, "", data)
5874 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005875 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5876 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5877 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5878 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
5879 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
5880 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005881 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005882
5883 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005884 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005885 data.Custom(&builder, name, prefix, "", data)
5886 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005887 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5888 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005889}
5890
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005891func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005892 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005893 apex {
5894 name: "myapex",
5895 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005896 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005897 }
5898 apex_key {
5899 name: "myapex.key",
5900 public_key: "testkey.avbpubkey",
5901 private_key: "testkey.pem",
5902 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005903 `,
5904 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5905 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5906 }),
5907 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005908 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00005909 ensureListContains(t, ab.makeModulesToInstall, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005910 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005911 var builder strings.Builder
5912 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5913 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005914 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex myapex.flattened\n")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005915}
5916
Jooyung Hand48f3c32019-08-23 11:18:57 +09005917func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5918 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5919 apex {
5920 name: "myapex",
5921 key: "myapex.key",
5922 native_shared_libs: ["libfoo"],
5923 }
5924
5925 apex_key {
5926 name: "myapex.key",
5927 public_key: "testkey.avbpubkey",
5928 private_key: "testkey.pem",
5929 }
5930
5931 cc_library {
5932 name: "libfoo",
5933 stl: "none",
5934 system_shared_libs: [],
5935 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005936 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005937 }
5938 `)
5939 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5940 apex {
5941 name: "myapex",
5942 key: "myapex.key",
5943 java_libs: ["myjar"],
5944 }
5945
5946 apex_key {
5947 name: "myapex.key",
5948 public_key: "testkey.avbpubkey",
5949 private_key: "testkey.pem",
5950 }
5951
5952 java_library {
5953 name: "myjar",
5954 srcs: ["foo/bar/MyClass.java"],
5955 sdk_version: "none",
5956 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005957 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005958 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005959 }
5960 `)
5961}
5962
Bill Peckhama41a6962021-01-11 10:58:54 -08005963func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005964 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005965 apex {
5966 name: "myapex",
5967 key: "myapex.key",
5968 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005969 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005970 }
5971
5972 apex_key {
5973 name: "myapex.key",
5974 public_key: "testkey.avbpubkey",
5975 private_key: "testkey.pem",
5976 }
5977
5978 java_import {
5979 name: "myjavaimport",
5980 apex_available: ["myapex"],
5981 jars: ["my.jar"],
5982 compile_dex: true,
5983 }
5984 `)
5985
5986 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5987 apexRule := module.Rule("apexRule")
5988 copyCmds := apexRule.Args["copy_commands"]
5989 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5990}
5991
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005992func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005993 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005994 apex {
5995 name: "myapex",
5996 key: "myapex.key",
5997 apps: [
5998 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005999 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006000 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006001 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006002 }
6003
6004 apex_key {
6005 name: "myapex.key",
6006 public_key: "testkey.avbpubkey",
6007 private_key: "testkey.pem",
6008 }
6009
6010 android_app {
6011 name: "AppFoo",
6012 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08006013 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006014 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09006015 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08006016 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006017 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006018 }
Jiyong Parkf7487312019-10-17 12:54:30 +09006019
6020 android_app {
6021 name: "AppFooPriv",
6022 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08006023 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09006024 system_modules: "none",
6025 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08006026 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006027 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09006028 }
Jiyong Park8be103b2019-11-08 15:53:48 +09006029
6030 cc_library_shared {
6031 name: "libjni",
6032 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006033 shared_libs: ["libfoo"],
6034 stl: "none",
6035 system_shared_libs: [],
6036 apex_available: [ "myapex" ],
6037 sdk_version: "current",
6038 }
6039
6040 cc_library_shared {
6041 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09006042 stl: "none",
6043 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09006044 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08006045 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09006046 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006047 `)
6048
Sundong Ahnabb64432019-10-22 13:58:29 +09006049 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006050 apexRule := module.Rule("apexRule")
6051 copyCmds := apexRule.Args["copy_commands"]
6052
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006053 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
6054 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09006055
Colin Crossaede88c2020-08-11 12:17:01 -07006056 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006057 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09006058 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006059 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09006060 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006061 // JNI libraries including transitive deps are
6062 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01006063 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006064 // ... embedded inside APK (jnilibs.zip)
6065 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
6066 // ... and not directly inside the APEX
6067 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
6068 }
Dario Frenicde2a032019-10-27 00:29:22 +01006069}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006070
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006071func TestApexWithAppImportBuildId(t *testing.T) {
6072 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
6073 for _, id := range invalidBuildIds {
6074 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
6075 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6076 variables.BuildId = proptools.StringPtr(id)
6077 })
6078 testApexError(t, message, `apex {
6079 name: "myapex",
6080 key: "myapex.key",
6081 apps: ["AppFooPrebuilt"],
6082 updatable: false,
6083 }
6084
6085 apex_key {
6086 name: "myapex.key",
6087 public_key: "testkey.avbpubkey",
6088 private_key: "testkey.pem",
6089 }
6090
6091 android_app_import {
6092 name: "AppFooPrebuilt",
6093 apk: "PrebuiltAppFoo.apk",
6094 presigned: true,
6095 apex_available: ["myapex"],
6096 }
6097 `, fixture)
6098 }
6099}
6100
Dario Frenicde2a032019-10-27 00:29:22 +01006101func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006102 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01006103 apex {
6104 name: "myapex",
6105 key: "myapex.key",
6106 apps: [
6107 "AppFooPrebuilt",
6108 "AppFooPrivPrebuilt",
6109 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006110 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01006111 }
6112
6113 apex_key {
6114 name: "myapex.key",
6115 public_key: "testkey.avbpubkey",
6116 private_key: "testkey.pem",
6117 }
6118
6119 android_app_import {
6120 name: "AppFooPrebuilt",
6121 apk: "PrebuiltAppFoo.apk",
6122 presigned: true,
6123 dex_preopt: {
6124 enabled: false,
6125 },
Jiyong Park592a6a42020-04-21 22:34:28 +09006126 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006127 }
6128
6129 android_app_import {
6130 name: "AppFooPrivPrebuilt",
6131 apk: "PrebuiltAppFooPriv.apk",
6132 privileged: true,
6133 presigned: true,
6134 dex_preopt: {
6135 enabled: false,
6136 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006137 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006138 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006139 }
6140 `)
6141
Sundong Ahnabb64432019-10-22 13:58:29 +09006142 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01006143 apexRule := module.Rule("apexRule")
6144 copyCmds := apexRule.Args["copy_commands"]
6145
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006146 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6147 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006148}
6149
6150func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006151 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006152 apex {
6153 name: "myapex",
6154 key: "myapex.key",
6155 apps: [
6156 "AppFoo",
6157 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006158 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006159 }
6160
6161 apex_key {
6162 name: "myapex.key",
6163 public_key: "testkey.avbpubkey",
6164 private_key: "testkey.pem",
6165 }
6166
6167 android_app {
6168 name: "AppFoo",
6169 srcs: ["foo/bar/MyClass.java"],
6170 sdk_version: "none",
6171 system_modules: "none",
6172 apex_available: [ "myapex" ],
6173 }
6174
6175 android_app_import {
6176 name: "AppFoo",
6177 apk: "AppFooPrebuilt.apk",
6178 filename: "AppFooPrebuilt.apk",
6179 presigned: true,
6180 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006181 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006182 }
6183 `, withFiles(map[string][]byte{
6184 "AppFooPrebuilt.apk": nil,
6185 }))
6186
6187 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006188 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006189 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006190}
6191
Dario Freni6f3937c2019-12-20 22:58:03 +00006192func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006193 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006194 apex {
6195 name: "myapex",
6196 key: "myapex.key",
6197 apps: [
6198 "TesterHelpAppFoo",
6199 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006200 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006201 }
6202
6203 apex_key {
6204 name: "myapex.key",
6205 public_key: "testkey.avbpubkey",
6206 private_key: "testkey.pem",
6207 }
6208
6209 android_test_helper_app {
6210 name: "TesterHelpAppFoo",
6211 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006212 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006213 }
6214
6215 `)
6216
6217 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6218 apexRule := module.Rule("apexRule")
6219 copyCmds := apexRule.Args["copy_commands"]
6220
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006221 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006222}
6223
Jooyung Han18020ea2019-11-13 10:50:48 +09006224func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6225 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006226 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006227 apex {
6228 name: "myapex",
6229 key: "myapex.key",
6230 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006231 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006232 }
6233
6234 apex_key {
6235 name: "myapex.key",
6236 public_key: "testkey.avbpubkey",
6237 private_key: "testkey.pem",
6238 }
6239
6240 apex {
6241 name: "otherapex",
6242 key: "myapex.key",
6243 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006244 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006245 }
6246
6247 cc_defaults {
6248 name: "libfoo-defaults",
6249 apex_available: ["otherapex"],
6250 }
6251
6252 cc_library {
6253 name: "libfoo",
6254 defaults: ["libfoo-defaults"],
6255 stl: "none",
6256 system_shared_libs: [],
6257 }`)
6258}
6259
Paul Duffine52e66f2020-03-30 17:54:29 +01006260func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006261 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006262 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006263 apex {
6264 name: "myapex",
6265 key: "myapex.key",
6266 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006267 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006268 }
6269
6270 apex_key {
6271 name: "myapex.key",
6272 public_key: "testkey.avbpubkey",
6273 private_key: "testkey.pem",
6274 }
6275
6276 apex {
6277 name: "otherapex",
6278 key: "otherapex.key",
6279 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006280 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006281 }
6282
6283 apex_key {
6284 name: "otherapex.key",
6285 public_key: "testkey.avbpubkey",
6286 private_key: "testkey.pem",
6287 }
6288
6289 cc_library {
6290 name: "libfoo",
6291 stl: "none",
6292 system_shared_libs: [],
6293 apex_available: ["otherapex"],
6294 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006295}
Jiyong Park127b40b2019-09-30 16:04:35 +09006296
Paul Duffine52e66f2020-03-30 17:54:29 +01006297func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006298 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006299 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006300.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006301.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006302.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006303.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006304.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006305.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006306 apex {
6307 name: "myapex",
6308 key: "myapex.key",
6309 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006310 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006311 }
6312
6313 apex_key {
6314 name: "myapex.key",
6315 public_key: "testkey.avbpubkey",
6316 private_key: "testkey.pem",
6317 }
6318
Jiyong Park127b40b2019-09-30 16:04:35 +09006319 cc_library {
6320 name: "libfoo",
6321 stl: "none",
6322 shared_libs: ["libbar"],
6323 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006324 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006325 }
6326
6327 cc_library {
6328 name: "libbar",
6329 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006330 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006331 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006332 apex_available: ["myapex"],
6333 }
6334
6335 cc_library {
6336 name: "libbaz",
6337 stl: "none",
6338 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006339 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006340}
Jiyong Park127b40b2019-09-30 16:04:35 +09006341
Paul Duffine52e66f2020-03-30 17:54:29 +01006342func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006343 testApexError(t, "\"otherapex\" is not a valid module name", `
6344 apex {
6345 name: "myapex",
6346 key: "myapex.key",
6347 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006348 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006349 }
6350
6351 apex_key {
6352 name: "myapex.key",
6353 public_key: "testkey.avbpubkey",
6354 private_key: "testkey.pem",
6355 }
6356
6357 cc_library {
6358 name: "libfoo",
6359 stl: "none",
6360 system_shared_libs: [],
6361 apex_available: ["otherapex"],
6362 }`)
6363
Paul Duffine52e66f2020-03-30 17:54:29 +01006364 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006365 apex {
6366 name: "myapex",
6367 key: "myapex.key",
6368 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006369 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006370 }
6371
6372 apex_key {
6373 name: "myapex.key",
6374 public_key: "testkey.avbpubkey",
6375 private_key: "testkey.pem",
6376 }
6377
6378 cc_library {
6379 name: "libfoo",
6380 stl: "none",
6381 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006382 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006383 apex_available: ["myapex"],
6384 }
6385
6386 cc_library {
6387 name: "libbar",
6388 stl: "none",
6389 system_shared_libs: [],
6390 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006391 }
6392
6393 cc_library {
6394 name: "libbaz",
6395 stl: "none",
6396 system_shared_libs: [],
6397 stubs: {
6398 versions: ["10", "20", "30"],
6399 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006400 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006401}
Jiyong Park127b40b2019-09-30 16:04:35 +09006402
Jiyong Park89e850a2020-04-07 16:37:39 +09006403func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006404 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006405 apex {
6406 name: "myapex",
6407 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006408 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006409 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006410 }
6411
6412 apex_key {
6413 name: "myapex.key",
6414 public_key: "testkey.avbpubkey",
6415 private_key: "testkey.pem",
6416 }
6417
6418 cc_library {
6419 name: "libfoo",
6420 stl: "none",
6421 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006422 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006423 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006424 }
6425
6426 cc_library {
6427 name: "libfoo2",
6428 stl: "none",
6429 system_shared_libs: [],
6430 shared_libs: ["libbaz"],
6431 apex_available: ["//apex_available:platform"],
6432 }
6433
6434 cc_library {
6435 name: "libbar",
6436 stl: "none",
6437 system_shared_libs: [],
6438 apex_available: ["myapex"],
6439 }
6440
6441 cc_library {
6442 name: "libbaz",
6443 stl: "none",
6444 system_shared_libs: [],
6445 apex_available: ["myapex"],
6446 stubs: {
6447 versions: ["1"],
6448 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006449 }`)
6450
Jiyong Park89e850a2020-04-07 16:37:39 +09006451 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6452 // because it depends on libbar which isn't available to platform
6453 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6454 if libfoo.NotAvailableForPlatform() != true {
6455 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6456 }
6457
6458 // libfoo2 however can be available to platform because it depends on libbaz which provides
6459 // stubs
6460 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6461 if libfoo2.NotAvailableForPlatform() == true {
6462 t.Errorf("%q should be available to platform", libfoo2.String())
6463 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006464}
Jiyong Parka90ca002019-10-07 15:47:24 +09006465
Paul Duffine52e66f2020-03-30 17:54:29 +01006466func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006467 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006468 apex {
6469 name: "myapex",
6470 key: "myapex.key",
6471 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006472 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006473 }
6474
6475 apex_key {
6476 name: "myapex.key",
6477 public_key: "testkey.avbpubkey",
6478 private_key: "testkey.pem",
6479 }
6480
6481 cc_library {
6482 name: "libfoo",
6483 stl: "none",
6484 system_shared_libs: [],
6485 apex_available: ["myapex"],
6486 static: {
6487 apex_available: ["//apex_available:platform"],
6488 },
6489 }`)
6490
Jiyong Park89e850a2020-04-07 16:37:39 +09006491 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6492 if libfooShared.NotAvailableForPlatform() != true {
6493 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6494 }
6495 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6496 if libfooStatic.NotAvailableForPlatform() != false {
6497 t.Errorf("%q should be available to platform", libfooStatic.String())
6498 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006499}
6500
Jiyong Park5d790c32019-11-15 18:40:32 +09006501func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006502 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006503 apex {
6504 name: "myapex",
6505 key: "myapex.key",
6506 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006507 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006508 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006509 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006510 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006511 }
6512
6513 override_apex {
6514 name: "override_myapex",
6515 base: "myapex",
6516 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006517 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006518 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006519 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006520 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006521 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006522 key: "mynewapex.key",
6523 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006524 }
6525
6526 apex_key {
6527 name: "myapex.key",
6528 public_key: "testkey.avbpubkey",
6529 private_key: "testkey.pem",
6530 }
6531
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006532 apex_key {
6533 name: "mynewapex.key",
6534 public_key: "testkey2.avbpubkey",
6535 private_key: "testkey2.pem",
6536 }
6537
6538 android_app_certificate {
6539 name: "myapex.certificate",
6540 certificate: "testkey",
6541 }
6542
Jiyong Park5d790c32019-11-15 18:40:32 +09006543 android_app {
6544 name: "app",
6545 srcs: ["foo/bar/MyClass.java"],
6546 package_name: "foo",
6547 sdk_version: "none",
6548 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006549 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006550 }
6551
6552 override_android_app {
6553 name: "override_app",
6554 base: "app",
6555 package_name: "bar",
6556 }
markchien7c803b82021-08-26 22:10:06 +08006557
6558 bpf {
6559 name: "bpf",
6560 srcs: ["bpf.c"],
6561 }
6562
6563 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006564 name: "overrideBpf",
6565 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006566 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006567
6568 prebuilt_etc {
6569 name: "myetc",
6570 src: "myprebuilt",
6571 }
6572
6573 prebuilt_etc {
6574 name: "override_myetc",
6575 src: "override_myprebuilt",
6576 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006577 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006578
Jiyong Park317645e2019-12-05 13:20:58 +09006579 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6580 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6581 if originalVariant.GetOverriddenBy() != "" {
6582 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6583 }
6584 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6585 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6586 }
6587
Jiyong Park5d790c32019-11-15 18:40:32 +09006588 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6589 apexRule := module.Rule("apexRule")
6590 copyCmds := apexRule.Args["copy_commands"]
6591
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006592 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6593 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006594
markchien7c803b82021-08-26 22:10:06 +08006595 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006596 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006597
Daniel Norman5a3ce132021-08-26 15:44:43 -07006598 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6599 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6600
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006601 apexBundle := module.Module().(*apexBundle)
6602 name := apexBundle.Name()
6603 if name != "override_myapex" {
6604 t.Errorf("name should be \"override_myapex\", but was %q", name)
6605 }
6606
Baligh Uddin004d7172020-02-19 21:29:28 -08006607 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6608 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6609 }
6610
Jiyong Park20bacab2020-03-03 11:45:41 +09006611 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006612 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006613 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6614
6615 signApkRule := module.Rule("signapk")
6616 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006617
Colin Crossaa255532020-07-03 13:18:24 -07006618 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006619 var builder strings.Builder
6620 data.Custom(&builder, name, "TARGET_", "", data)
6621 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006622 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6623 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
6624 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006625 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006626 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006627 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006628 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006629 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006630 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
6631 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006632}
6633
Albert Martineefabcf2022-03-21 20:11:16 +00006634func TestMinSdkVersionOverride(t *testing.T) {
6635 // Override from 29 to 31
6636 minSdkOverride31 := "31"
6637 ctx := testApex(t, `
6638 apex {
6639 name: "myapex",
6640 key: "myapex.key",
6641 native_shared_libs: ["mylib"],
6642 updatable: true,
6643 min_sdk_version: "29"
6644 }
6645
6646 override_apex {
6647 name: "override_myapex",
6648 base: "myapex",
6649 logging_parent: "com.foo.bar",
6650 package_name: "test.overridden.package"
6651 }
6652
6653 apex_key {
6654 name: "myapex.key",
6655 public_key: "testkey.avbpubkey",
6656 private_key: "testkey.pem",
6657 }
6658
6659 cc_library {
6660 name: "mylib",
6661 srcs: ["mylib.cpp"],
6662 runtime_libs: ["libbar"],
6663 system_shared_libs: [],
6664 stl: "none",
6665 apex_available: [ "myapex" ],
6666 min_sdk_version: "apex_inherit"
6667 }
6668
6669 cc_library {
6670 name: "libbar",
6671 srcs: ["mylib.cpp"],
6672 system_shared_libs: [],
6673 stl: "none",
6674 apex_available: [ "myapex" ],
6675 min_sdk_version: "apex_inherit"
6676 }
6677
6678 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6679
6680 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6681 copyCmds := apexRule.Args["copy_commands"]
6682
6683 // Ensure that direct non-stubs dep is always included
6684 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6685
6686 // Ensure that runtime_libs dep in included
6687 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6688
6689 // Ensure libraries target overridden min_sdk_version value
6690 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6691}
6692
6693func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6694 // Attempt to override from 31 to 29, should be a NOOP
6695 minSdkOverride29 := "29"
6696 ctx := testApex(t, `
6697 apex {
6698 name: "myapex",
6699 key: "myapex.key",
6700 native_shared_libs: ["mylib"],
6701 updatable: true,
6702 min_sdk_version: "31"
6703 }
6704
6705 override_apex {
6706 name: "override_myapex",
6707 base: "myapex",
6708 logging_parent: "com.foo.bar",
6709 package_name: "test.overridden.package"
6710 }
6711
6712 apex_key {
6713 name: "myapex.key",
6714 public_key: "testkey.avbpubkey",
6715 private_key: "testkey.pem",
6716 }
6717
6718 cc_library {
6719 name: "mylib",
6720 srcs: ["mylib.cpp"],
6721 runtime_libs: ["libbar"],
6722 system_shared_libs: [],
6723 stl: "none",
6724 apex_available: [ "myapex" ],
6725 min_sdk_version: "apex_inherit"
6726 }
6727
6728 cc_library {
6729 name: "libbar",
6730 srcs: ["mylib.cpp"],
6731 system_shared_libs: [],
6732 stl: "none",
6733 apex_available: [ "myapex" ],
6734 min_sdk_version: "apex_inherit"
6735 }
6736
6737 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6738
6739 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6740 copyCmds := apexRule.Args["copy_commands"]
6741
6742 // Ensure that direct non-stubs dep is always included
6743 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6744
6745 // Ensure that runtime_libs dep in included
6746 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6747
6748 // Ensure libraries target the original min_sdk_version value rather than the overridden
6749 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6750}
6751
Jooyung Han214bf372019-11-12 13:03:50 +09006752func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006753 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006754 apex {
6755 name: "myapex",
6756 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006757 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006758 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006759 }
6760
6761 apex_key {
6762 name: "myapex.key",
6763 public_key: "testkey.avbpubkey",
6764 private_key: "testkey.pem",
6765 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006766
6767 cc_library {
6768 name: "mylib",
6769 srcs: ["mylib.cpp"],
6770 stl: "libc++",
6771 system_shared_libs: [],
6772 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006773 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006774 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006775 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006776
6777 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6778 args := module.Rule("apexRule").Args
6779 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006780 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006781
6782 // The copies of the libraries in the apex should have one more dependency than
6783 // the ones outside the apex, namely the unwinder. Ideally we should check
6784 // the dependency names directly here but for some reason the names are blank in
6785 // this test.
6786 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006787 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006788 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6789 if len(apexImplicits) != len(nonApexImplicits)+1 {
6790 t.Errorf("%q missing unwinder dep", lib)
6791 }
6792 }
Jooyung Han214bf372019-11-12 13:03:50 +09006793}
6794
Paul Duffine05480a2021-03-08 15:07:14 +00006795var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006796 "api/current.txt": nil,
6797 "api/removed.txt": nil,
6798 "api/system-current.txt": nil,
6799 "api/system-removed.txt": nil,
6800 "api/test-current.txt": nil,
6801 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006802
Anton Hanssondff2c782020-12-21 17:10:01 +00006803 "100/public/api/foo.txt": nil,
6804 "100/public/api/foo-removed.txt": nil,
6805 "100/system/api/foo.txt": nil,
6806 "100/system/api/foo-removed.txt": nil,
6807
Paul Duffineedc5d52020-06-12 17:46:39 +01006808 // For java_sdk_library_import
6809 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006810}
6811
Jooyung Han58f26ab2019-12-18 15:34:32 +09006812func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006813 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006814 apex {
6815 name: "myapex",
6816 key: "myapex.key",
6817 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006818 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006819 }
6820
6821 apex_key {
6822 name: "myapex.key",
6823 public_key: "testkey.avbpubkey",
6824 private_key: "testkey.pem",
6825 }
6826
6827 java_sdk_library {
6828 name: "foo",
6829 srcs: ["a.java"],
6830 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006831 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006832 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006833
6834 prebuilt_apis {
6835 name: "sdk",
6836 api_dirs: ["100"],
6837 }
Paul Duffin9b879592020-05-26 13:21:35 +01006838 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006839
6840 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006841 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006842 "javalib/foo.jar",
6843 "etc/permissions/foo.xml",
6844 })
6845 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006846 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006847 ensureMatches(t, sdkLibrary.RuleParams.Command, `<library\\n\s+name=\\\"foo\\\"\\n\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09006848}
6849
Paul Duffin9b879592020-05-26 13:21:35 +01006850func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006851 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006852 apex {
6853 name: "myapex",
6854 key: "myapex.key",
6855 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006856 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006857 }
6858
6859 apex_key {
6860 name: "myapex.key",
6861 public_key: "testkey.avbpubkey",
6862 private_key: "testkey.pem",
6863 }
6864
6865 java_sdk_library {
6866 name: "foo",
6867 srcs: ["a.java"],
6868 api_packages: ["foo"],
6869 apex_available: ["myapex"],
6870 sdk_version: "none",
6871 system_modules: "none",
6872 }
6873
6874 java_library {
6875 name: "bar",
6876 srcs: ["a.java"],
6877 libs: ["foo"],
6878 apex_available: ["myapex"],
6879 sdk_version: "none",
6880 system_modules: "none",
6881 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006882
6883 prebuilt_apis {
6884 name: "sdk",
6885 api_dirs: ["100"],
6886 }
Paul Duffin9b879592020-05-26 13:21:35 +01006887 `, withFiles(filesForSdkLibrary))
6888
6889 // java_sdk_library installs both impl jar and permission XML
6890 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6891 "javalib/bar.jar",
6892 "javalib/foo.jar",
6893 "etc/permissions/foo.xml",
6894 })
6895
6896 // The bar library should depend on the implementation jar.
6897 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006898 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006899 t.Errorf("expected %q, found %#q", expected, actual)
6900 }
6901}
6902
6903func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006904 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006905 apex {
6906 name: "myapex",
6907 key: "myapex.key",
6908 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006909 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006910 }
6911
6912 apex_key {
6913 name: "myapex.key",
6914 public_key: "testkey.avbpubkey",
6915 private_key: "testkey.pem",
6916 }
6917
6918 java_sdk_library {
6919 name: "foo",
6920 srcs: ["a.java"],
6921 api_packages: ["foo"],
6922 apex_available: ["myapex"],
6923 sdk_version: "none",
6924 system_modules: "none",
6925 }
6926
6927 java_library {
6928 name: "bar",
6929 srcs: ["a.java"],
6930 libs: ["foo"],
6931 sdk_version: "none",
6932 system_modules: "none",
6933 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006934
6935 prebuilt_apis {
6936 name: "sdk",
6937 api_dirs: ["100"],
6938 }
Paul Duffin9b879592020-05-26 13:21:35 +01006939 `, withFiles(filesForSdkLibrary))
6940
6941 // java_sdk_library installs both impl jar and permission XML
6942 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6943 "javalib/foo.jar",
6944 "etc/permissions/foo.xml",
6945 })
6946
6947 // The bar library should depend on the stubs jar.
6948 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006949 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006950 t.Errorf("expected %q, found %#q", expected, actual)
6951 }
6952}
6953
Paul Duffineedc5d52020-06-12 17:46:39 +01006954func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006955 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006956 prebuilt_apis {
6957 name: "sdk",
6958 api_dirs: ["100"],
6959 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006960 withFiles(map[string][]byte{
6961 "apex/a.java": nil,
6962 "apex/apex_manifest.json": nil,
6963 "apex/Android.bp": []byte(`
6964 package {
6965 default_visibility: ["//visibility:private"],
6966 }
6967
6968 apex {
6969 name: "myapex",
6970 key: "myapex.key",
6971 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006972 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006973 }
6974
6975 apex_key {
6976 name: "myapex.key",
6977 public_key: "testkey.avbpubkey",
6978 private_key: "testkey.pem",
6979 }
6980
6981 java_library {
6982 name: "bar",
6983 srcs: ["a.java"],
6984 libs: ["foo"],
6985 apex_available: ["myapex"],
6986 sdk_version: "none",
6987 system_modules: "none",
6988 }
6989`),
6990 "source/a.java": nil,
6991 "source/api/current.txt": nil,
6992 "source/api/removed.txt": nil,
6993 "source/Android.bp": []byte(`
6994 package {
6995 default_visibility: ["//visibility:private"],
6996 }
6997
6998 java_sdk_library {
6999 name: "foo",
7000 visibility: ["//apex"],
7001 srcs: ["a.java"],
7002 api_packages: ["foo"],
7003 apex_available: ["myapex"],
7004 sdk_version: "none",
7005 system_modules: "none",
7006 public: {
7007 enabled: true,
7008 },
7009 }
7010`),
7011 "prebuilt/a.jar": nil,
7012 "prebuilt/Android.bp": []byte(`
7013 package {
7014 default_visibility: ["//visibility:private"],
7015 }
7016
7017 java_sdk_library_import {
7018 name: "foo",
7019 visibility: ["//apex", "//source"],
7020 apex_available: ["myapex"],
7021 prefer: true,
7022 public: {
7023 jars: ["a.jar"],
7024 },
7025 }
7026`),
Anton Hanssondff2c782020-12-21 17:10:01 +00007027 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01007028 )
7029
7030 // java_sdk_library installs both impl jar and permission XML
7031 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7032 "javalib/bar.jar",
7033 "javalib/foo.jar",
7034 "etc/permissions/foo.xml",
7035 })
7036
7037 // The bar library should depend on the implementation jar.
7038 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007039 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01007040 t.Errorf("expected %q, found %#q", expected, actual)
7041 }
7042}
7043
7044func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
7045 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
7046 apex {
7047 name: "myapex",
7048 key: "myapex.key",
7049 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007050 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007051 }
7052
7053 apex_key {
7054 name: "myapex.key",
7055 public_key: "testkey.avbpubkey",
7056 private_key: "testkey.pem",
7057 }
7058
7059 java_sdk_library_import {
7060 name: "foo",
7061 apex_available: ["myapex"],
7062 prefer: true,
7063 public: {
7064 jars: ["a.jar"],
7065 },
7066 }
7067
7068 `, withFiles(filesForSdkLibrary))
7069}
7070
atrost6e126252020-01-27 17:01:16 +00007071func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007072 result := android.GroupFixturePreparers(
7073 prepareForApexTest,
7074 java.PrepareForTestWithPlatformCompatConfig,
7075 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007076 apex {
7077 name: "myapex",
7078 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007079 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007080 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007081 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007082 }
7083
7084 apex_key {
7085 name: "myapex.key",
7086 public_key: "testkey.avbpubkey",
7087 private_key: "testkey.pem",
7088 }
7089
7090 platform_compat_config {
7091 name: "myjar-platform-compat-config",
7092 src: ":myjar",
7093 }
7094
7095 java_library {
7096 name: "myjar",
7097 srcs: ["foo/bar/MyClass.java"],
7098 sdk_version: "none",
7099 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007100 apex_available: [ "myapex" ],
7101 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007102
7103 // Make sure that a preferred prebuilt does not affect the apex contents.
7104 prebuilt_platform_compat_config {
7105 name: "myjar-platform-compat-config",
7106 metadata: "compat-config/metadata.xml",
7107 prefer: true,
7108 }
atrost6e126252020-01-27 17:01:16 +00007109 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007110 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00007111 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7112 "etc/compatconfig/myjar-platform-compat-config.xml",
7113 "javalib/myjar.jar",
7114 })
7115}
7116
Jooyung Han862c0d62022-12-21 10:15:37 +09007117func TestNoDupeApexFiles(t *testing.T) {
7118 android.GroupFixturePreparers(
7119 android.PrepareForTestWithAndroidBuildComponents,
7120 PrepareForTestWithApexBuildComponents,
7121 prepareForTestWithMyapex,
7122 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7123 ).
7124 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7125 RunTestWithBp(t, `
7126 apex {
7127 name: "myapex",
7128 key: "myapex.key",
7129 prebuilts: ["foo", "bar"],
7130 updatable: false,
7131 }
7132
7133 apex_key {
7134 name: "myapex.key",
7135 public_key: "testkey.avbpubkey",
7136 private_key: "testkey.pem",
7137 }
7138
7139 prebuilt_etc {
7140 name: "foo",
7141 src: "myprebuilt",
7142 filename_from_src: true,
7143 }
7144
7145 prebuilt_etc {
7146 name: "bar",
7147 src: "myprebuilt",
7148 filename_from_src: true,
7149 }
7150 `)
7151}
7152
Jiyong Park479321d2019-12-16 11:47:12 +09007153func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7154 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7155 apex {
7156 name: "myapex",
7157 key: "myapex.key",
7158 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007159 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007160 }
7161
7162 apex_key {
7163 name: "myapex.key",
7164 public_key: "testkey.avbpubkey",
7165 private_key: "testkey.pem",
7166 }
7167
7168 java_library {
7169 name: "myjar",
7170 srcs: ["foo/bar/MyClass.java"],
7171 sdk_version: "none",
7172 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007173 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007174 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007175 }
7176 `)
7177}
7178
Jiyong Park7afd1072019-12-30 16:56:33 +09007179func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007180 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007181 apex {
7182 name: "myapex",
7183 key: "myapex.key",
7184 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007185 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007186 }
7187
7188 apex_key {
7189 name: "myapex.key",
7190 public_key: "testkey.avbpubkey",
7191 private_key: "testkey.pem",
7192 }
7193
7194 cc_library {
7195 name: "mylib",
7196 srcs: ["mylib.cpp"],
7197 system_shared_libs: [],
7198 stl: "none",
7199 required: ["a", "b"],
7200 host_required: ["c", "d"],
7201 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007202 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007203 }
7204 `)
7205
7206 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007207 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007208 name := apexBundle.BaseModuleName()
7209 prefix := "TARGET_"
7210 var builder strings.Builder
7211 data.Custom(&builder, name, prefix, "", data)
7212 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007213 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 apex_manifest.pb.myapex apex_pubkey.myapex a b\n")
Sasha Smundakdcb61292022-12-08 10:41:33 -08007214 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7215 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007216}
7217
Jiyong Park7cd10e32020-01-14 09:22:18 +09007218func TestSymlinksFromApexToSystem(t *testing.T) {
7219 bp := `
7220 apex {
7221 name: "myapex",
7222 key: "myapex.key",
7223 native_shared_libs: ["mylib"],
7224 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007225 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007226 }
7227
Jiyong Park9d677202020-02-19 16:29:35 +09007228 apex {
7229 name: "myapex.updatable",
7230 key: "myapex.key",
7231 native_shared_libs: ["mylib"],
7232 java_libs: ["myjar"],
7233 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007234 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007235 }
7236
Jiyong Park7cd10e32020-01-14 09:22:18 +09007237 apex_key {
7238 name: "myapex.key",
7239 public_key: "testkey.avbpubkey",
7240 private_key: "testkey.pem",
7241 }
7242
7243 cc_library {
7244 name: "mylib",
7245 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007246 shared_libs: [
7247 "myotherlib",
7248 "myotherlib_ext",
7249 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007250 system_shared_libs: [],
7251 stl: "none",
7252 apex_available: [
7253 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007254 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007255 "//apex_available:platform",
7256 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007257 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007258 }
7259
7260 cc_library {
7261 name: "myotherlib",
7262 srcs: ["mylib.cpp"],
7263 system_shared_libs: [],
7264 stl: "none",
7265 apex_available: [
7266 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007267 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007268 "//apex_available:platform",
7269 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007270 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007271 }
7272
Jiyong Parkce243632023-02-17 18:22:25 +09007273 cc_library {
7274 name: "myotherlib_ext",
7275 srcs: ["mylib.cpp"],
7276 system_shared_libs: [],
7277 system_ext_specific: true,
7278 stl: "none",
7279 apex_available: [
7280 "myapex",
7281 "myapex.updatable",
7282 "//apex_available:platform",
7283 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007284 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007285 }
7286
Jiyong Park7cd10e32020-01-14 09:22:18 +09007287 java_library {
7288 name: "myjar",
7289 srcs: ["foo/bar/MyClass.java"],
7290 sdk_version: "none",
7291 system_modules: "none",
7292 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007293 apex_available: [
7294 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007295 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007296 "//apex_available:platform",
7297 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007298 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007299 }
7300
7301 java_library {
7302 name: "myotherjar",
7303 srcs: ["foo/bar/MyClass.java"],
7304 sdk_version: "none",
7305 system_modules: "none",
7306 apex_available: [
7307 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007308 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007309 "//apex_available:platform",
7310 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007311 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007312 }
7313 `
7314
7315 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7316 for _, f := range files {
7317 if f.path == file {
7318 if f.isLink {
7319 t.Errorf("%q is not a real file", file)
7320 }
7321 return
7322 }
7323 }
7324 t.Errorf("%q is not found", file)
7325 }
7326
Jiyong Parkce243632023-02-17 18:22:25 +09007327 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007328 for _, f := range files {
7329 if f.path == file {
7330 if !f.isLink {
7331 t.Errorf("%q is not a symlink", file)
7332 }
Jiyong Parkce243632023-02-17 18:22:25 +09007333 if f.src != target {
7334 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7335 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007336 return
7337 }
7338 }
7339 t.Errorf("%q is not found", file)
7340 }
7341
Jiyong Park9d677202020-02-19 16:29:35 +09007342 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7343 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007344 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007345 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007346 ensureRealfileExists(t, files, "javalib/myjar.jar")
7347 ensureRealfileExists(t, files, "lib64/mylib.so")
7348 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007349 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007350
Jiyong Park9d677202020-02-19 16:29:35 +09007351 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7352 ensureRealfileExists(t, files, "javalib/myjar.jar")
7353 ensureRealfileExists(t, files, "lib64/mylib.so")
7354 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007355 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007356
7357 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007358 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007359 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007360 ensureRealfileExists(t, files, "javalib/myjar.jar")
7361 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007362 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7363 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007364
7365 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7366 ensureRealfileExists(t, files, "javalib/myjar.jar")
7367 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007368 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7369 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007370}
7371
Yo Chiange8128052020-07-23 20:09:18 +08007372func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007373 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007374 apex {
7375 name: "myapex",
7376 key: "myapex.key",
7377 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007378 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007379 }
7380
7381 apex_key {
7382 name: "myapex.key",
7383 public_key: "testkey.avbpubkey",
7384 private_key: "testkey.pem",
7385 }
7386
7387 cc_library_shared {
7388 name: "mylib",
7389 srcs: ["mylib.cpp"],
7390 shared_libs: ["myotherlib"],
7391 system_shared_libs: [],
7392 stl: "none",
7393 apex_available: [
7394 "myapex",
7395 "//apex_available:platform",
7396 ],
7397 }
7398
7399 cc_prebuilt_library_shared {
7400 name: "myotherlib",
7401 srcs: ["prebuilt.so"],
7402 system_shared_libs: [],
7403 stl: "none",
7404 apex_available: [
7405 "myapex",
7406 "//apex_available:platform",
7407 ],
7408 }
7409 `)
7410
Prerana Patilb1896c82022-11-09 18:14:34 +00007411 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007412 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007413 var builder strings.Builder
7414 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7415 androidMk := builder.String()
7416 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007417 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007418 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7419 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7420 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007421 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 +08007422}
7423
Jooyung Han643adc42020-02-27 13:50:06 +09007424func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007425 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007426 apex {
7427 name: "myapex",
7428 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09007429 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007430 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007431 }
7432
7433 apex_key {
7434 name: "myapex.key",
7435 public_key: "testkey.avbpubkey",
7436 private_key: "testkey.pem",
7437 }
7438
7439 cc_library {
7440 name: "mylib",
7441 srcs: ["mylib.cpp"],
7442 shared_libs: ["mylib2"],
7443 system_shared_libs: [],
7444 stl: "none",
7445 apex_available: [ "myapex" ],
7446 }
7447
7448 cc_library {
7449 name: "mylib2",
7450 srcs: ["mylib.cpp"],
7451 system_shared_libs: [],
7452 stl: "none",
7453 apex_available: [ "myapex" ],
7454 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007455
7456 rust_ffi_shared {
7457 name: "libfoo.rust",
7458 crate_name: "foo",
7459 srcs: ["foo.rs"],
7460 shared_libs: ["libfoo.shared_from_rust"],
7461 prefer_rlib: true,
7462 apex_available: ["myapex"],
7463 }
7464
7465 cc_library_shared {
7466 name: "libfoo.shared_from_rust",
7467 srcs: ["mylib.cpp"],
7468 system_shared_libs: [],
7469 stl: "none",
7470 stubs: {
7471 versions: ["10", "11", "12"],
7472 },
7473 }
7474
Jooyung Han643adc42020-02-27 13:50:06 +09007475 `)
7476
7477 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
7478 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09007479 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007480 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7481 "lib64/mylib.so",
7482 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007483 "lib64/libfoo.rust.so",
7484 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7485 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007486 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007487
7488 // b/220397949
7489 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007490}
7491
Jooyung Han49f67012020-04-17 13:43:10 +09007492func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007493 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007494 apex {
7495 name: "myapex",
7496 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007497 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007498 }
7499 apex_key {
7500 name: "myapex.key",
7501 public_key: "testkey.avbpubkey",
7502 private_key: "testkey.pem",
7503 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007504 `,
7505 android.FixtureModifyConfig(func(config android.Config) {
7506 delete(config.Targets, android.Android)
7507 config.AndroidCommonTarget = android.Target{}
7508 }),
7509 )
Jooyung Han49f67012020-04-17 13:43:10 +09007510
7511 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7512 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7513 }
7514}
7515
Jiyong Parkbd159612020-02-28 15:22:21 +09007516func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007517 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007518 apex {
7519 name: "myapex",
7520 key: "myapex.key",
7521 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007522 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007523 }
7524
7525 apex_key {
7526 name: "myapex.key",
7527 public_key: "testkey.avbpubkey",
7528 private_key: "testkey.pem",
7529 }
7530
7531 android_app {
7532 name: "AppFoo",
7533 srcs: ["foo/bar/MyClass.java"],
7534 sdk_version: "none",
7535 system_modules: "none",
7536 apex_available: [ "myapex" ],
7537 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007538 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007539
Colin Crosscf371cc2020-11-13 11:48:42 -08007540 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007541 content := bundleConfigRule.Args["content"]
7542
7543 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007544 ensureContains(t, content, `"apex_config":{"apex_embedded_apk_config":[{"package_name":"com.android.foo","path":"app/AppFoo@TEST.BUILD_ID/AppFoo.apk"}]}`)
Jiyong Parkbd159612020-02-28 15:22:21 +09007545}
7546
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007547func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007548 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007549 apex {
7550 name: "myapex",
7551 key: "myapex.key",
7552 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007553 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007554 }
7555
7556 apex_key {
7557 name: "myapex.key",
7558 public_key: "testkey.avbpubkey",
7559 private_key: "testkey.pem",
7560 }
7561
7562 android_app_set {
7563 name: "AppSet",
7564 set: "AppSet.apks",
7565 }`)
7566 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007567 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007568 content := bundleConfigRule.Args["content"]
7569 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7570 s := mod.Rule("apexRule").Args["copy_commands"]
7571 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007572 if len(copyCmds) != 4 {
7573 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007574 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007575 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7576 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007577 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7578 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007579
7580 // Ensure that canned_fs_config has an entry for the app set zip file
7581 generateFsRule := mod.Rule("generateFsConfig")
7582 cmd := generateFsRule.RuleParams.Command
7583 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007584}
7585
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007586func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007587 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007588 apex_set {
7589 name: "myapex",
7590 filename: "foo_v2.apex",
7591 sanitized: {
7592 none: { set: "myapex.apks", },
7593 hwaddress: { set: "myapex.hwasan.apks", },
7594 },
Paul Duffin24704672021-04-06 16:09:30 +01007595 }
7596 `
7597 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007598
Paul Duffin24704672021-04-06 16:09:30 +01007599 // Check that the extractor produces the correct output file from the correct input file.
7600 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007601
Paul Duffin24704672021-04-06 16:09:30 +01007602 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7603 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007604
Paul Duffin24704672021-04-06 16:09:30 +01007605 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7606
7607 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007608 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7609 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007610
7611 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007612}
7613
Pranav Guptaeba03b02022-09-27 00:27:08 +00007614func TestApexSetApksModuleAssignment(t *testing.T) {
7615 ctx := testApex(t, `
7616 apex_set {
7617 name: "myapex",
7618 set: ":myapex_apks_file",
7619 }
7620
7621 filegroup {
7622 name: "myapex_apks_file",
7623 srcs: ["myapex.apks"],
7624 }
7625 `)
7626
7627 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7628
7629 // Check that the extractor produces the correct apks file from the input module
7630 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.apks"
7631 extractedApex := m.Output(extractorOutput)
7632
7633 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7634}
7635
Paul Duffin89f570a2021-06-16 01:42:33 +01007636func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007637 t.Helper()
7638
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007639 bp := `
7640 java_library {
7641 name: "some-updatable-apex-lib",
7642 srcs: ["a.java"],
7643 sdk_version: "current",
7644 apex_available: [
7645 "some-updatable-apex",
7646 ],
satayevabcd5972021-08-06 17:49:46 +01007647 permitted_packages: ["some.updatable.apex.lib"],
Spandan Dascc9d9422023-04-06 18:07:43 +00007648 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007649 }
7650
7651 java_library {
7652 name: "some-non-updatable-apex-lib",
7653 srcs: ["a.java"],
7654 apex_available: [
7655 "some-non-updatable-apex",
7656 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007657 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007658 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007659 }
7660
7661 bootclasspath_fragment {
7662 name: "some-non-updatable-fragment",
7663 contents: ["some-non-updatable-apex-lib"],
7664 apex_available: [
7665 "some-non-updatable-apex",
7666 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007667 hidden_api: {
7668 split_packages: ["*"],
7669 },
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007670 }
7671
7672 java_library {
7673 name: "some-platform-lib",
7674 srcs: ["a.java"],
7675 sdk_version: "current",
7676 installable: true,
7677 }
7678
7679 java_library {
7680 name: "some-art-lib",
7681 srcs: ["a.java"],
7682 sdk_version: "current",
7683 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007684 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007685 ],
7686 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007687 compile_dex: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007688 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007689 }
7690
7691 apex {
7692 name: "some-updatable-apex",
7693 key: "some-updatable-apex.key",
7694 java_libs: ["some-updatable-apex-lib"],
7695 updatable: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007696 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007697 }
7698
7699 apex {
7700 name: "some-non-updatable-apex",
7701 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007702 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007703 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007704 }
7705
7706 apex_key {
7707 name: "some-updatable-apex.key",
7708 }
7709
7710 apex_key {
7711 name: "some-non-updatable-apex.key",
7712 }
7713
7714 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007715 name: "com.android.art.debug",
7716 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007717 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007718 updatable: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007719 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007720 }
7721
Paul Duffinf23bc472021-04-27 12:42:20 +01007722 bootclasspath_fragment {
7723 name: "art-bootclasspath-fragment",
7724 image_name: "art",
7725 contents: ["some-art-lib"],
7726 apex_available: [
7727 "com.android.art.debug",
7728 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007729 hidden_api: {
7730 split_packages: ["*"],
7731 },
Paul Duffinf23bc472021-04-27 12:42:20 +01007732 }
7733
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007734 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007735 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007736 }
7737
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007738 filegroup {
7739 name: "some-updatable-apex-file_contexts",
7740 srcs: [
7741 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7742 ],
7743 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007744
7745 filegroup {
7746 name: "some-non-updatable-apex-file_contexts",
7747 srcs: [
7748 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7749 ],
7750 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007751 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007752
Paul Duffin89f570a2021-06-16 01:42:33 +01007753 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007754}
7755
Paul Duffin89f570a2021-06-16 01:42:33 +01007756func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007757 t.Helper()
7758
Paul Duffin55607122021-03-30 23:32:51 +01007759 fs := android.MockFS{
7760 "a.java": nil,
7761 "a.jar": nil,
7762 "apex_manifest.json": nil,
7763 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007764 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007765 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7766 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7767 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007768 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007769 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007770
Paul Duffin55607122021-03-30 23:32:51 +01007771 errorHandler := android.FixtureExpectsNoErrors
7772 if errmsg != "" {
7773 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007774 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007775
Paul Duffin55607122021-03-30 23:32:51 +01007776 result := android.GroupFixturePreparers(
7777 cc.PrepareForTestWithCcDefaultModules,
7778 java.PrepareForTestWithHiddenApiBuildComponents,
7779 java.PrepareForTestWithJavaDefaultModules,
7780 java.PrepareForTestWithJavaSdkLibraryFiles,
7781 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007782 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007783 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007784 android.FixtureModifyMockFS(func(fs android.MockFS) {
7785 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7786 insert := ""
7787 for _, fragment := range fragments {
7788 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7789 }
7790 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7791 platform_bootclasspath {
7792 name: "platform-bootclasspath",
7793 fragments: [
7794 %s
7795 ],
7796 }
7797 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007798 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007799 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007800 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007801 ).
7802 ExtendWithErrorHandler(errorHandler).
7803 RunTestWithBp(t, bp)
7804
7805 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007806}
7807
Paul Duffin5556c5f2022-06-09 17:32:21 +00007808func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007809 preparers := android.GroupFixturePreparers(
7810 java.PrepareForTestWithJavaDefaultModules,
7811 PrepareForTestWithApexBuildComponents,
7812 ).
7813 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7814 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7815
7816 bpBase := `
7817 apex_set {
7818 name: "com.android.myapex",
7819 installable: true,
7820 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7821 set: "myapex.apks",
7822 }
7823
7824 apex_set {
7825 name: "com.mycompany.android.myapex",
7826 apex_name: "com.android.myapex",
7827 installable: true,
7828 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7829 set: "company-myapex.apks",
7830 }
7831
7832 prebuilt_bootclasspath_fragment {
7833 name: "my-bootclasspath-fragment",
7834 apex_available: ["com.android.myapex"],
7835 %s
7836 }
7837 `
7838
7839 t.Run("java_import", func(t *testing.T) {
7840 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7841 java_import {
7842 name: "libfoo",
7843 jars: ["libfoo.jar"],
7844 apex_available: ["com.android.myapex"],
7845 }
7846 `)
7847 })
7848
7849 t.Run("java_sdk_library_import", func(t *testing.T) {
7850 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7851 java_sdk_library_import {
7852 name: "libfoo",
7853 public: {
7854 jars: ["libbar.jar"],
7855 },
7856 apex_available: ["com.android.myapex"],
7857 }
7858 `)
7859 })
7860
7861 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7862 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7863 image_name: "art",
7864 contents: ["libfoo"],
7865 `)+`
7866 java_sdk_library_import {
7867 name: "libfoo",
7868 public: {
7869 jars: ["libbar.jar"],
7870 },
7871 apex_available: ["com.android.myapex"],
7872 }
7873 `)
7874 })
7875}
7876
Paul Duffin5556c5f2022-06-09 17:32:21 +00007877func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
7878 preparers := android.GroupFixturePreparers(
7879 java.PrepareForTestWithJavaDefaultModules,
7880 PrepareForTestWithApexBuildComponents,
7881 )
7882
7883 bpBase := `
7884 apex_set {
7885 name: "com.android.myapex",
7886 installable: true,
7887 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7888 set: "myapex.apks",
7889 }
7890
7891 apex_set {
7892 name: "com.android.myapex_compressed",
7893 apex_name: "com.android.myapex",
7894 installable: true,
7895 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7896 set: "myapex_compressed.apks",
7897 }
7898
7899 prebuilt_bootclasspath_fragment {
7900 name: "my-bootclasspath-fragment",
7901 apex_available: [
7902 "com.android.myapex",
7903 "com.android.myapex_compressed",
7904 ],
7905 hidden_api: {
7906 annotation_flags: "annotation-flags.csv",
7907 metadata: "metadata.csv",
7908 index: "index.csv",
7909 signature_patterns: "signature_patterns.csv",
7910 },
7911 %s
7912 }
7913 `
7914
7915 t.Run("java_import", func(t *testing.T) {
7916 result := preparers.RunTestWithBp(t,
7917 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7918 java_import {
7919 name: "libfoo",
7920 jars: ["libfoo.jar"],
7921 apex_available: [
7922 "com.android.myapex",
7923 "com.android.myapex_compressed",
7924 ],
7925 }
7926 `)
7927
7928 module := result.Module("libfoo", "android_common_com.android.myapex")
7929 usesLibraryDep := module.(java.UsesLibraryDependency)
7930 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7931 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7932 usesLibraryDep.DexJarBuildPath().Path())
7933 })
7934
7935 t.Run("java_sdk_library_import", func(t *testing.T) {
7936 result := preparers.RunTestWithBp(t,
7937 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7938 java_sdk_library_import {
7939 name: "libfoo",
7940 public: {
7941 jars: ["libbar.jar"],
7942 },
7943 apex_available: [
7944 "com.android.myapex",
7945 "com.android.myapex_compressed",
7946 ],
7947 compile_dex: true,
7948 }
7949 `)
7950
7951 module := result.Module("libfoo", "android_common_com.android.myapex")
7952 usesLibraryDep := module.(java.UsesLibraryDependency)
7953 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7954 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7955 usesLibraryDep.DexJarBuildPath().Path())
7956 })
7957
7958 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7959 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7960 image_name: "art",
7961 contents: ["libfoo"],
7962 `)+`
7963 java_sdk_library_import {
7964 name: "libfoo",
7965 public: {
7966 jars: ["libbar.jar"],
7967 },
7968 apex_available: [
7969 "com.android.myapex",
7970 "com.android.myapex_compressed",
7971 ],
7972 compile_dex: true,
7973 }
7974 `)
7975 })
7976}
7977
Jooyung Han548640b2020-04-27 12:10:30 +09007978func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7979 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7980 apex {
7981 name: "myapex",
7982 key: "myapex.key",
7983 updatable: true,
7984 }
7985
7986 apex_key {
7987 name: "myapex.key",
7988 public_key: "testkey.avbpubkey",
7989 private_key: "testkey.pem",
7990 }
7991 `)
7992}
7993
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007994func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7995 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7996 apex {
7997 name: "myapex",
7998 key: "myapex.key",
7999 }
8000
8001 apex_key {
8002 name: "myapex.key",
8003 public_key: "testkey.avbpubkey",
8004 private_key: "testkey.pem",
8005 }
8006 `)
8007}
8008
Jooyung Handfc864c2023-03-20 18:19:07 +09008009func Test_use_vndk_as_stable_shouldnt_be_used_for_updatable_vendor_apexes(t *testing.T) {
8010 testApexError(t, `"myapex" .*: use_vndk_as_stable: updatable APEXes can't use external VNDK libs`, `
Daniel Norman69109112021-12-02 12:52:42 -08008011 apex {
8012 name: "myapex",
8013 key: "myapex.key",
8014 updatable: true,
Jooyung Handfc864c2023-03-20 18:19:07 +09008015 use_vndk_as_stable: true,
Daniel Norman69109112021-12-02 12:52:42 -08008016 soc_specific: true,
8017 }
8018
8019 apex_key {
8020 name: "myapex.key",
8021 public_key: "testkey.avbpubkey",
8022 private_key: "testkey.pem",
8023 }
8024 `)
8025}
8026
Jooyung Han02873da2023-03-22 17:41:03 +09008027func Test_use_vndk_as_stable_shouldnt_be_used_with_min_sdk_version(t *testing.T) {
8028 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported when min_sdk_version is set`, `
8029 apex {
8030 name: "myapex",
8031 key: "myapex.key",
8032 updatable: false,
8033 min_sdk_version: "29",
8034 use_vndk_as_stable: true,
8035 vendor: true,
8036 }
8037
8038 apex_key {
8039 name: "myapex.key",
8040 public_key: "testkey.avbpubkey",
8041 private_key: "testkey.pem",
8042 }
8043 `)
8044}
8045
Jooyung Handfc864c2023-03-20 18:19:07 +09008046func Test_use_vndk_as_stable_shouldnt_be_used_for_non_vendor_apexes(t *testing.T) {
8047 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported for system/system_ext APEXes`, `
8048 apex {
8049 name: "myapex",
8050 key: "myapex.key",
8051 updatable: false,
8052 use_vndk_as_stable: true,
8053 }
8054
8055 apex_key {
8056 name: "myapex.key",
8057 public_key: "testkey.avbpubkey",
8058 private_key: "testkey.pem",
8059 }
8060 `)
8061}
8062
satayevb98371c2021-06-15 16:49:50 +01008063func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
8064 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
8065 apex {
8066 name: "myapex",
8067 key: "myapex.key",
8068 systemserverclasspath_fragments: [
8069 "mysystemserverclasspathfragment",
8070 ],
8071 min_sdk_version: "29",
8072 updatable: true,
8073 }
8074
8075 apex_key {
8076 name: "myapex.key",
8077 public_key: "testkey.avbpubkey",
8078 private_key: "testkey.pem",
8079 }
8080
8081 java_library {
8082 name: "foo",
8083 srcs: ["b.java"],
8084 min_sdk_version: "29",
8085 installable: true,
8086 apex_available: [
8087 "myapex",
8088 ],
8089 }
8090
8091 systemserverclasspath_fragment {
8092 name: "mysystemserverclasspathfragment",
8093 generate_classpaths_proto: false,
8094 contents: [
8095 "foo",
8096 ],
8097 apex_available: [
8098 "myapex",
8099 ],
8100 }
satayevabcd5972021-08-06 17:49:46 +01008101 `,
8102 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8103 )
satayevb98371c2021-06-15 16:49:50 +01008104}
8105
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008106func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01008107 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
8108 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
8109 // modules to be included in the BootJars.
8110 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
8111 return android.GroupFixturePreparers(
8112 dexpreopt.FixtureSetBootJars(bootJars...),
8113 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8114 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
8115 }),
8116 )
8117 }
8118
8119 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
8120 // same value. This can result in an invalid configuration as it allows non art apex jars to be
8121 // specified in the ArtApexJars configuration.
8122 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
8123 return android.GroupFixturePreparers(
8124 dexpreopt.FixtureSetArtBootJars(bootJars...),
8125 dexpreopt.FixtureSetBootJars(bootJars...),
8126 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8127 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
8128 }),
8129 )
8130 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008131
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008132 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008133 preparer := android.GroupFixturePreparers(
8134 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
8135 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8136 )
8137 fragments := []java.ApexVariantReference{
8138 {
8139 Apex: proptools.StringPtr("com.android.art.debug"),
8140 Module: proptools.StringPtr("art-bootclasspath-fragment"),
8141 },
8142 {
8143 Apex: proptools.StringPtr("some-non-updatable-apex"),
8144 Module: proptools.StringPtr("some-non-updatable-fragment"),
8145 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008146 }
satayevabcd5972021-08-06 17:49:46 +01008147 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008148 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008149
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008150 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01008151 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
8152 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01008153 preparer := android.GroupFixturePreparers(
8154 prepareSetBootJars("com.android.art.debug:some-art-lib"),
8155 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8156 )
Paul Duffin60264a02021-04-12 20:02:36 +01008157 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008158 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008159
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008160 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 +01008161 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 +01008162 // Update the dexpreopt ArtApexJars directly.
8163 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
8164 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008165 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008166
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008167 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 +01008168 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 +01008169 // Update the dexpreopt ArtApexJars directly.
8170 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
8171 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008172 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008173
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008174 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 +01008175 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 +01008176 preparer := android.GroupFixturePreparers(
8177 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
8178 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8179 )
Paul Duffin60264a02021-04-12 20:02:36 +01008180 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008181 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008182
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008183 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 +01008184 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01008185 fragment := java.ApexVariantReference{
8186 Apex: proptools.StringPtr("some-non-updatable-apex"),
8187 Module: proptools.StringPtr("some-non-updatable-fragment"),
8188 }
8189 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008190 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008191
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008192 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008193 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008194 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8195 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008196 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008197
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008198 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008199 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008200 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8201 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008202 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008203
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008204 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01008205 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01008206 // Update the dexpreopt ArtApexJars directly.
8207 preparer := prepareSetArtJars("platform:some-platform-lib")
8208 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008209 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008210
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008211 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008212 preparer := android.GroupFixturePreparers(
8213 java.FixtureConfigureBootJars("platform:some-platform-lib"),
8214 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8215 )
8216 fragments := []java.ApexVariantReference{
8217 {
8218 Apex: proptools.StringPtr("some-non-updatable-apex"),
8219 Module: proptools.StringPtr("some-non-updatable-fragment"),
8220 },
8221 }
8222 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008223 })
Paul Duffin064b70c2020-11-02 17:32:38 +00008224}
8225
8226func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008227 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008228 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008229 fragment := java.ApexVariantReference{
8230 Apex: proptools.StringPtr("myapex"),
8231 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8232 }
8233
Paul Duffin064b70c2020-11-02 17:32:38 +00008234 testDexpreoptWithApexes(t, `
8235 prebuilt_apex {
8236 name: "myapex" ,
8237 arch: {
8238 arm64: {
8239 src: "myapex-arm64.apex",
8240 },
8241 arm: {
8242 src: "myapex-arm.apex",
8243 },
8244 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008245 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8246 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008247
Paul Duffin89f570a2021-06-16 01:42:33 +01008248 prebuilt_bootclasspath_fragment {
8249 name: "my-bootclasspath-fragment",
8250 contents: ["libfoo"],
8251 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008252 hidden_api: {
8253 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8254 metadata: "my-bootclasspath-fragment/metadata.csv",
8255 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008256 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8257 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8258 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008259 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008260 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008261
Paul Duffin89f570a2021-06-16 01:42:33 +01008262 java_import {
8263 name: "libfoo",
8264 jars: ["libfoo.jar"],
8265 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008266 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008267 }
8268 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008269 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008270}
8271
Spandan Dasf14e2542021-11-12 00:01:37 +00008272func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008273 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008274 bp += `
8275 apex_key {
8276 name: "myapex.key",
8277 public_key: "testkey.avbpubkey",
8278 private_key: "testkey.pem",
8279 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008280 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008281 "lib1/src/A.java": nil,
8282 "lib2/src/B.java": nil,
8283 "system/sepolicy/apex/myapex-file_contexts": nil,
8284 }
8285
Paul Duffin45338f02021-03-30 23:07:52 +01008286 errorHandler := android.FixtureExpectsNoErrors
8287 if errmsg != "" {
8288 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008289 }
Colin Crossae8600b2020-10-29 17:09:13 -07008290
Paul Duffin45338f02021-03-30 23:07:52 +01008291 android.GroupFixturePreparers(
8292 android.PrepareForTestWithAndroidBuildComponents,
8293 java.PrepareForTestWithJavaBuildComponents,
8294 PrepareForTestWithApexBuildComponents,
8295 android.PrepareForTestWithNeverallowRules(rules),
8296 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008297 apexBootJars := make([]string, 0, len(bootJars))
8298 for _, apexBootJar := range bootJars {
8299 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008300 }
satayevd604b212021-07-21 14:23:52 +01008301 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008302 }),
8303 fs.AddToFixture(),
8304 ).
8305 ExtendWithErrorHandler(errorHandler).
8306 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008307}
8308
8309func TestApexPermittedPackagesRules(t *testing.T) {
8310 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008311 name string
8312 expectedError string
8313 bp string
8314 bootJars []string
8315 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008316 }{
8317
8318 {
8319 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8320 expectedError: "",
8321 bp: `
8322 java_library {
8323 name: "bcp_lib1",
8324 srcs: ["lib1/src/*.java"],
8325 permitted_packages: ["foo.bar"],
8326 apex_available: ["myapex"],
8327 sdk_version: "none",
8328 system_modules: "none",
8329 }
8330 java_library {
8331 name: "nonbcp_lib2",
8332 srcs: ["lib2/src/*.java"],
8333 apex_available: ["myapex"],
8334 permitted_packages: ["a.b"],
8335 sdk_version: "none",
8336 system_modules: "none",
8337 }
8338 apex {
8339 name: "myapex",
8340 key: "myapex.key",
8341 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008342 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008343 }`,
8344 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008345 bcpPermittedPackages: map[string][]string{
8346 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008347 "foo.bar",
8348 },
8349 },
8350 },
8351 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008352 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008353 expectedError: `(?s)module "bcp_lib2" .* which is restricted because bcp_lib2 bootjar may only use these package prefixes: foo.bar. Please consider the following alternatives:\n 1. If the offending code is from a statically linked library, consider removing that dependency and using an alternative already in the bootclasspath, or perhaps a shared library. 2. Move the offending code into an allowed package.\n 3. Jarjar the offending code. Please be mindful of the potential system health implications of bundling that code, particularly if the offending jar is part of the bootclasspath.`,
Andrei Onea115e7e72020-06-05 21:14:03 +01008354 bp: `
8355 java_library {
8356 name: "bcp_lib1",
8357 srcs: ["lib1/src/*.java"],
8358 apex_available: ["myapex"],
8359 permitted_packages: ["foo.bar"],
8360 sdk_version: "none",
8361 system_modules: "none",
8362 }
8363 java_library {
8364 name: "bcp_lib2",
8365 srcs: ["lib2/src/*.java"],
8366 apex_available: ["myapex"],
8367 permitted_packages: ["foo.bar", "bar.baz"],
8368 sdk_version: "none",
8369 system_modules: "none",
8370 }
8371 apex {
8372 name: "myapex",
8373 key: "myapex.key",
8374 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008375 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008376 }
8377 `,
8378 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008379 bcpPermittedPackages: map[string][]string{
8380 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008381 "foo.bar",
8382 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008383 "bcp_lib2": []string{
8384 "foo.bar",
8385 },
8386 },
8387 },
8388 {
8389 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8390 expectedError: "",
8391 bp: `
8392 java_library {
8393 name: "bcp_lib_restricted",
8394 srcs: ["lib1/src/*.java"],
8395 apex_available: ["myapex"],
8396 permitted_packages: ["foo.bar"],
8397 sdk_version: "none",
8398 min_sdk_version: "29",
8399 system_modules: "none",
8400 }
8401 java_library {
8402 name: "bcp_lib_unrestricted",
8403 srcs: ["lib2/src/*.java"],
8404 apex_available: ["myapex"],
8405 permitted_packages: ["foo.bar", "bar.baz"],
8406 sdk_version: "none",
8407 min_sdk_version: "29",
8408 system_modules: "none",
8409 }
8410 apex {
8411 name: "myapex",
8412 key: "myapex.key",
8413 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8414 updatable: true,
8415 min_sdk_version: "29",
8416 }
8417 `,
8418 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8419 bcpPermittedPackages: map[string][]string{
8420 "bcp_lib1_non_updateable": []string{
8421 "foo.bar",
8422 },
8423 // bcp_lib2_updateable has no entry here since updateable bcp can contain new packages - tracking via an allowlist is not necessary
Andrei Onea115e7e72020-06-05 21:14:03 +01008424 },
8425 },
8426 }
8427 for _, tc := range testcases {
8428 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008429 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8430 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008431 })
8432 }
8433}
8434
Jiyong Park62304bb2020-04-13 16:19:48 +09008435func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008436 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008437 apex {
8438 name: "myapex",
8439 key: "myapex.key",
8440 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008441 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008442 }
8443
8444 apex_key {
8445 name: "myapex.key",
8446 public_key: "testkey.avbpubkey",
8447 private_key: "testkey.pem",
8448 }
8449
8450 cc_library {
8451 name: "mylib",
8452 srcs: ["mylib.cpp"],
8453 system_shared_libs: [],
8454 stl: "none",
8455 stubs: {
8456 versions: ["1"],
8457 },
8458 apex_available: ["myapex"],
8459 }
8460
8461 cc_library {
8462 name: "myprivlib",
8463 srcs: ["mylib.cpp"],
8464 system_shared_libs: [],
8465 stl: "none",
8466 apex_available: ["myapex"],
8467 }
8468
8469
8470 cc_test {
8471 name: "mytest",
8472 gtest: false,
8473 srcs: ["mylib.cpp"],
8474 system_shared_libs: [],
8475 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008476 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008477 test_for: ["myapex"]
8478 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008479
8480 cc_library {
8481 name: "mytestlib",
8482 srcs: ["mylib.cpp"],
8483 system_shared_libs: [],
8484 shared_libs: ["mylib", "myprivlib"],
8485 stl: "none",
8486 test_for: ["myapex"],
8487 }
8488
8489 cc_benchmark {
8490 name: "mybench",
8491 srcs: ["mylib.cpp"],
8492 system_shared_libs: [],
8493 shared_libs: ["mylib", "myprivlib"],
8494 stl: "none",
8495 test_for: ["myapex"],
8496 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008497 `)
8498
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008499 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008500 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008501 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8502 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8503 }
8504
8505 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008506 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008507 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8508 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8509 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8510}
Jiyong Park46a512f2020-12-04 18:02:13 +09008511
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008512func TestIndirectTestFor(t *testing.T) {
8513 ctx := testApex(t, `
8514 apex {
8515 name: "myapex",
8516 key: "myapex.key",
8517 native_shared_libs: ["mylib", "myprivlib"],
8518 updatable: false,
8519 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008520
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008521 apex_key {
8522 name: "myapex.key",
8523 public_key: "testkey.avbpubkey",
8524 private_key: "testkey.pem",
8525 }
8526
8527 cc_library {
8528 name: "mylib",
8529 srcs: ["mylib.cpp"],
8530 system_shared_libs: [],
8531 stl: "none",
8532 stubs: {
8533 versions: ["1"],
8534 },
8535 apex_available: ["myapex"],
8536 }
8537
8538 cc_library {
8539 name: "myprivlib",
8540 srcs: ["mylib.cpp"],
8541 system_shared_libs: [],
8542 stl: "none",
8543 shared_libs: ["mylib"],
8544 apex_available: ["myapex"],
8545 }
8546
8547 cc_library {
8548 name: "mytestlib",
8549 srcs: ["mylib.cpp"],
8550 system_shared_libs: [],
8551 shared_libs: ["myprivlib"],
8552 stl: "none",
8553 test_for: ["myapex"],
8554 }
8555 `)
8556
8557 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008558 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008559 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8560 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8561 }
8562
8563 // The platform variant of mytestlib links to the platform variant of the
8564 // internal myprivlib.
8565 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8566
8567 // The platform variant of myprivlib links to the platform variant of mylib
8568 // and bypasses its stubs.
8569 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 +09008570}
8571
Martin Stjernholmec009002021-03-27 15:18:31 +00008572func TestTestForForLibInOtherApex(t *testing.T) {
8573 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8574 _ = testApex(t, `
8575 apex {
8576 name: "com.android.art",
8577 key: "myapex.key",
8578 native_shared_libs: ["mylib"],
8579 updatable: false,
8580 }
8581
8582 apex {
8583 name: "com.android.art.debug",
8584 key: "myapex.key",
8585 native_shared_libs: ["mylib", "mytestlib"],
8586 updatable: false,
8587 }
8588
8589 apex_key {
8590 name: "myapex.key",
8591 public_key: "testkey.avbpubkey",
8592 private_key: "testkey.pem",
8593 }
8594
8595 cc_library {
8596 name: "mylib",
8597 srcs: ["mylib.cpp"],
8598 system_shared_libs: [],
8599 stl: "none",
8600 stubs: {
8601 versions: ["1"],
8602 },
8603 apex_available: ["com.android.art", "com.android.art.debug"],
8604 }
8605
8606 cc_library {
8607 name: "mytestlib",
8608 srcs: ["mylib.cpp"],
8609 system_shared_libs: [],
8610 shared_libs: ["mylib"],
8611 stl: "none",
8612 apex_available: ["com.android.art.debug"],
8613 test_for: ["com.android.art"],
8614 }
8615 `,
8616 android.MockFS{
8617 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8618 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8619 }.AddToFixture())
8620}
8621
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008622// TODO(jungjw): Move this to proptools
8623func intPtr(i int) *int {
8624 return &i
8625}
8626
8627func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008628 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008629 apex_set {
8630 name: "myapex",
8631 set: "myapex.apks",
8632 filename: "foo_v2.apex",
8633 overrides: ["foo"],
8634 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008635 `,
8636 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8637 variables.Platform_sdk_version = intPtr(30)
8638 }),
8639 android.FixtureModifyConfig(func(config android.Config) {
8640 config.Targets[android.Android] = []android.Target{
8641 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8642 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8643 }
8644 }),
8645 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008646
Paul Duffin24704672021-04-06 16:09:30 +01008647 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008648
8649 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008650 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008651 actual := extractedApex.Args["abis"]
8652 expected := "ARMEABI_V7A,ARM64_V8A"
8653 if actual != expected {
8654 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8655 }
8656 actual = extractedApex.Args["sdk-version"]
8657 expected = "30"
8658 if actual != expected {
8659 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8660 }
8661
Paul Duffin6717d882021-06-15 19:09:41 +01008662 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008663 a := m.Module().(*ApexSet)
8664 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008665 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008666 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8667 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8668 }
8669}
8670
Anton Hansson805e0a52022-11-25 14:06:46 +00008671func TestApexSet_NativeBridge(t *testing.T) {
8672 ctx := testApex(t, `
8673 apex_set {
8674 name: "myapex",
8675 set: "myapex.apks",
8676 filename: "foo_v2.apex",
8677 overrides: ["foo"],
8678 }
8679 `,
8680 android.FixtureModifyConfig(func(config android.Config) {
8681 config.Targets[android.Android] = []android.Target{
8682 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8683 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8684 }
8685 }),
8686 )
8687
8688 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
8689
8690 // Check extract_apks tool parameters. No native bridge arch expected
8691 extractedApex := m.Output("extracted/myapex.apks")
8692 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8693}
8694
Jiyong Park7d95a512020-05-10 15:16:24 +09008695func TestNoStaticLinkingToStubsLib(t *testing.T) {
8696 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8697 apex {
8698 name: "myapex",
8699 key: "myapex.key",
8700 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008701 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008702 }
8703
8704 apex_key {
8705 name: "myapex.key",
8706 public_key: "testkey.avbpubkey",
8707 private_key: "testkey.pem",
8708 }
8709
8710 cc_library {
8711 name: "mylib",
8712 srcs: ["mylib.cpp"],
8713 static_libs: ["otherlib"],
8714 system_shared_libs: [],
8715 stl: "none",
8716 apex_available: [ "myapex" ],
8717 }
8718
8719 cc_library {
8720 name: "otherlib",
8721 srcs: ["mylib.cpp"],
8722 system_shared_libs: [],
8723 stl: "none",
8724 stubs: {
8725 versions: ["1", "2", "3"],
8726 },
8727 apex_available: [ "myapex" ],
8728 }
8729 `)
8730}
8731
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008732func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008733 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008734 apex {
8735 name: "myapex",
8736 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008737 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008738 custom_sign_tool: "sign_myapex",
8739 }
8740
8741 apex_key {
8742 name: "myapex.key",
8743 public_key: "testkey.avbpubkey",
8744 private_key: "testkey.pem",
8745 }
8746 `)
8747
8748 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8749 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8750 ensureContains(t, content, `name="myapex.apex" public_key="vendor/foo/devkeys/testkey.avbpubkey" private_key="vendor/foo/devkeys/testkey.pem" container_certificate="vendor/foo/devkeys/test.x509.pem" container_private_key="vendor/foo/devkeys/test.pk8" partition="system_ext" sign_tool="sign_myapex"`)
8751}
8752
8753func TestApexKeysTxtOverrides(t *testing.T) {
8754 ctx := testApex(t, `
8755 apex {
8756 name: "myapex",
8757 key: "myapex.key",
8758 updatable: false,
8759 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008760 }
8761
8762 apex_key {
8763 name: "myapex.key",
8764 public_key: "testkey.avbpubkey",
8765 private_key: "testkey.pem",
8766 }
8767
8768 prebuilt_apex {
8769 name: "myapex",
8770 prefer: true,
8771 arch: {
8772 arm64: {
8773 src: "myapex-arm64.apex",
8774 },
8775 arm: {
8776 src: "myapex-arm.apex",
8777 },
8778 },
8779 }
8780
8781 apex_set {
8782 name: "myapex_set",
8783 set: "myapex.apks",
8784 filename: "myapex_set.apex",
8785 overrides: ["myapex"],
8786 }
8787 `)
8788
8789 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8790 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8791 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 +09008792 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 +09008793}
8794
Jooyung Han938b5932020-06-20 12:47:47 +09008795func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008796 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008797 apex {
8798 name: "myapex",
8799 key: "myapex.key",
8800 apps: ["app"],
8801 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008802 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008803 }
8804
8805 apex_key {
8806 name: "myapex.key",
8807 public_key: "testkey.avbpubkey",
8808 private_key: "testkey.pem",
8809 }
8810
8811 android_app {
8812 name: "app",
8813 srcs: ["foo/bar/MyClass.java"],
8814 package_name: "foo",
8815 sdk_version: "none",
8816 system_modules: "none",
8817 apex_available: [ "myapex" ],
8818 }
8819 `, withFiles(map[string][]byte{
8820 "sub/Android.bp": []byte(`
8821 override_apex {
8822 name: "override_myapex",
8823 base: "myapex",
8824 apps: ["override_app"],
8825 allowed_files: ":allowed",
8826 }
8827 // Overridable "path" property should be referenced indirectly
8828 filegroup {
8829 name: "allowed",
8830 srcs: ["allowed.txt"],
8831 }
8832 override_android_app {
8833 name: "override_app",
8834 base: "app",
8835 package_name: "bar",
8836 }
8837 `),
8838 }))
8839
8840 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8841 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8842 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8843 }
8844
8845 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8846 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8847 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8848 }
8849}
8850
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008851func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008852 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008853 apex {
8854 name: "myapex",
8855 key: "myapex.key",
8856 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008857 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008858 }
8859
8860 apex_key {
8861 name: "myapex.key",
8862 public_key: "testkey.avbpubkey",
8863 private_key: "testkey.pem",
8864 }
8865
8866 cc_library {
8867 name: "mylib",
8868 srcs: ["mylib.cpp"],
8869 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008870 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008871 },
8872 apex_available: ["myapex"],
8873 }
8874
8875 cc_prebuilt_library_shared {
8876 name: "mylib",
8877 prefer: false,
8878 srcs: ["prebuilt.so"],
8879 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008880 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008881 },
8882 apex_available: ["myapex"],
8883 }
8884 `)
8885}
8886
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008887func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008888 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008889 apex {
8890 name: "myapex",
8891 key: "myapex.key",
8892 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008893 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008894 }
8895 apex_key {
8896 name: "myapex.key",
8897 public_key: "testkey.avbpubkey",
8898 private_key: "testkey.pem",
8899 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008900 `,
8901 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8902 variables.CompressedApex = proptools.BoolPtr(true)
8903 }),
8904 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008905
8906 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8907 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8908
8909 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8910 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8911
8912 // Make sure output of bundle is .capex
8913 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8914 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8915
8916 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008917 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008918 var builder strings.Builder
8919 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8920 androidMk := builder.String()
8921 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8922}
8923
Martin Stjernholm2856c662020-12-02 15:03:42 +00008924func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008925 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008926 apex {
8927 name: "myapex",
8928 key: "myapex.key",
8929 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008930 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008931 }
8932
8933 apex_key {
8934 name: "myapex.key",
8935 public_key: "testkey.avbpubkey",
8936 private_key: "testkey.pem",
8937 }
8938
8939 cc_library {
8940 name: "mylib",
8941 srcs: ["mylib.cpp"],
8942 apex_available: ["myapex"],
8943 shared_libs: ["otherlib"],
8944 system_shared_libs: [],
8945 }
8946
8947 cc_library {
8948 name: "otherlib",
8949 srcs: ["mylib.cpp"],
8950 stubs: {
8951 versions: ["current"],
8952 },
8953 }
8954
8955 cc_prebuilt_library_shared {
8956 name: "otherlib",
8957 prefer: true,
8958 srcs: ["prebuilt.so"],
8959 stubs: {
8960 versions: ["current"],
8961 },
8962 }
8963 `)
8964
8965 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008966 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008967 var builder strings.Builder
8968 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8969 androidMk := builder.String()
8970
8971 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8972 // a thing there.
Diwas Sharmabb9202e2023-01-26 18:42:21 +00008973 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 apex_manifest.pb.myapex apex_pubkey.myapex otherlib\n")
Martin Stjernholm2856c662020-12-02 15:03:42 +00008974}
8975
Jiyong Parke3867542020-12-03 17:28:25 +09008976func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008977 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008978 apex {
8979 name: "myapex",
8980 key: "myapex.key",
8981 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008982 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008983 }
8984
8985 apex_key {
8986 name: "myapex.key",
8987 public_key: "testkey.avbpubkey",
8988 private_key: "testkey.pem",
8989 }
8990
8991 cc_library {
8992 name: "mylib",
8993 srcs: ["mylib.cpp"],
8994 system_shared_libs: [],
8995 stl: "none",
8996 apex_available: ["myapex"],
8997 shared_libs: ["mylib2"],
8998 target: {
8999 apex: {
9000 exclude_shared_libs: ["mylib2"],
9001 },
9002 },
9003 }
9004
9005 cc_library {
9006 name: "mylib2",
9007 srcs: ["mylib.cpp"],
9008 system_shared_libs: [],
9009 stl: "none",
9010 }
9011 `)
9012
9013 // Check if mylib is linked to mylib2 for the non-apex target
9014 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
9015 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
9016
9017 // Make sure that the link doesn't occur for the apex target
9018 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
9019 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
9020
9021 // It shouldn't appear in the copy cmd as well.
9022 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
9023 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
9024}
9025
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009026func TestPrebuiltStubLibDep(t *testing.T) {
9027 bpBase := `
9028 apex {
9029 name: "myapex",
9030 key: "myapex.key",
9031 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009032 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009033 }
9034 apex_key {
9035 name: "myapex.key",
9036 public_key: "testkey.avbpubkey",
9037 private_key: "testkey.pem",
9038 }
9039 cc_library {
9040 name: "mylib",
9041 srcs: ["mylib.cpp"],
9042 apex_available: ["myapex"],
9043 shared_libs: ["stublib"],
9044 system_shared_libs: [],
9045 }
9046 apex {
9047 name: "otherapex",
9048 enabled: %s,
9049 key: "myapex.key",
9050 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009051 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009052 }
9053 `
9054
9055 stublibSourceBp := `
9056 cc_library {
9057 name: "stublib",
9058 srcs: ["mylib.cpp"],
9059 apex_available: ["otherapex"],
9060 system_shared_libs: [],
9061 stl: "none",
9062 stubs: {
9063 versions: ["1"],
9064 },
9065 }
9066 `
9067
9068 stublibPrebuiltBp := `
9069 cc_prebuilt_library_shared {
9070 name: "stublib",
9071 srcs: ["prebuilt.so"],
9072 apex_available: ["otherapex"],
9073 stubs: {
9074 versions: ["1"],
9075 },
9076 %s
9077 }
9078 `
9079
9080 tests := []struct {
9081 name string
9082 stublibBp string
9083 usePrebuilt bool
9084 modNames []string // Modules to collect AndroidMkEntries for
9085 otherApexEnabled []string
9086 }{
9087 {
9088 name: "only_source",
9089 stublibBp: stublibSourceBp,
9090 usePrebuilt: false,
9091 modNames: []string{"stublib"},
9092 otherApexEnabled: []string{"true", "false"},
9093 },
9094 {
9095 name: "source_preferred",
9096 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9097 usePrebuilt: false,
9098 modNames: []string{"stublib", "prebuilt_stublib"},
9099 otherApexEnabled: []string{"true", "false"},
9100 },
9101 {
9102 name: "prebuilt_preferred",
9103 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9104 usePrebuilt: true,
9105 modNames: []string{"stublib", "prebuilt_stublib"},
9106 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9107 },
9108 {
9109 name: "only_prebuilt",
9110 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9111 usePrebuilt: true,
9112 modNames: []string{"stublib"},
9113 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9114 },
9115 }
9116
9117 for _, test := range tests {
9118 t.Run(test.name, func(t *testing.T) {
9119 for _, otherApexEnabled := range test.otherApexEnabled {
9120 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009121 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009122
9123 type modAndMkEntries struct {
9124 mod *cc.Module
9125 mkEntries android.AndroidMkEntries
9126 }
9127 entries := []*modAndMkEntries{}
9128
9129 // Gather shared lib modules that are installable
9130 for _, modName := range test.modNames {
9131 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9132 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9133 continue
9134 }
9135 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08009136 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009137 continue
9138 }
Colin Crossaa255532020-07-03 13:18:24 -07009139 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009140 if ent.Disabled {
9141 continue
9142 }
9143 entries = append(entries, &modAndMkEntries{
9144 mod: mod,
9145 mkEntries: ent,
9146 })
9147 }
9148 }
9149 }
9150
9151 var entry *modAndMkEntries = nil
9152 for _, ent := range entries {
9153 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9154 if entry != nil {
9155 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9156 } else {
9157 entry = ent
9158 }
9159 }
9160 }
9161
9162 if entry == nil {
9163 t.Errorf("AndroidMk entry for \"stublib\" missing")
9164 } else {
9165 isPrebuilt := entry.mod.Prebuilt() != nil
9166 if isPrebuilt != test.usePrebuilt {
9167 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9168 }
9169 if !entry.mod.IsStubs() {
9170 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9171 }
9172 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9173 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9174 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009175 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009176 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009177 if !android.InList(expected, cflags) {
9178 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9179 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009180 }
9181 })
9182 }
9183 })
9184 }
9185}
9186
Martin Stjernholmdf298b32021-05-21 20:57:29 +01009187func TestHostApexInHostOnlyBuild(t *testing.T) {
9188 testApex(t, `
9189 apex {
9190 name: "myapex",
9191 host_supported: true,
9192 key: "myapex.key",
9193 updatable: false,
9194 payload_type: "zip",
9195 }
9196 apex_key {
9197 name: "myapex.key",
9198 public_key: "testkey.avbpubkey",
9199 private_key: "testkey.pem",
9200 }
9201 `,
9202 android.FixtureModifyConfig(func(config android.Config) {
9203 // We may not have device targets in all builds, e.g. in
9204 // prebuilts/build-tools/build-prebuilts.sh
9205 config.Targets[android.Android] = []android.Target{}
9206 }))
9207}
9208
Colin Crossc33e5212021-05-25 18:16:02 -07009209func TestApexJavaCoverage(t *testing.T) {
9210 bp := `
9211 apex {
9212 name: "myapex",
9213 key: "myapex.key",
9214 java_libs: ["mylib"],
9215 bootclasspath_fragments: ["mybootclasspathfragment"],
9216 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9217 updatable: false,
9218 }
9219
9220 apex_key {
9221 name: "myapex.key",
9222 public_key: "testkey.avbpubkey",
9223 private_key: "testkey.pem",
9224 }
9225
9226 java_library {
9227 name: "mylib",
9228 srcs: ["mylib.java"],
9229 apex_available: ["myapex"],
9230 compile_dex: true,
9231 }
9232
9233 bootclasspath_fragment {
9234 name: "mybootclasspathfragment",
9235 contents: ["mybootclasspathlib"],
9236 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009237 hidden_api: {
9238 split_packages: ["*"],
9239 },
Colin Crossc33e5212021-05-25 18:16:02 -07009240 }
9241
9242 java_library {
9243 name: "mybootclasspathlib",
9244 srcs: ["mybootclasspathlib.java"],
9245 apex_available: ["myapex"],
9246 compile_dex: true,
9247 }
9248
9249 systemserverclasspath_fragment {
9250 name: "mysystemserverclasspathfragment",
9251 contents: ["mysystemserverclasspathlib"],
9252 apex_available: ["myapex"],
9253 }
9254
9255 java_library {
9256 name: "mysystemserverclasspathlib",
9257 srcs: ["mysystemserverclasspathlib.java"],
9258 apex_available: ["myapex"],
9259 compile_dex: true,
9260 }
9261 `
9262
9263 result := android.GroupFixturePreparers(
9264 PrepareForTestWithApexBuildComponents,
9265 prepareForTestWithMyapex,
9266 java.PrepareForTestWithJavaDefaultModules,
9267 android.PrepareForTestWithAndroidBuildComponents,
9268 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009269 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9270 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009271 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009272 ).RunTest(t)
9273
9274 // Make sure jacoco ran on both mylib and mybootclasspathlib
9275 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9276 t.Errorf("Failed to find jacoco rule for mylib")
9277 }
9278 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9279 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9280 }
9281 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9282 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9283 }
9284}
9285
Jiyong Park192600a2021-08-03 07:52:17 +00009286func TestProhibitStaticExecutable(t *testing.T) {
9287 testApexError(t, `executable mybin is static`, `
9288 apex {
9289 name: "myapex",
9290 key: "myapex.key",
9291 binaries: ["mybin"],
9292 min_sdk_version: "29",
9293 }
9294
9295 apex_key {
9296 name: "myapex.key",
9297 public_key: "testkey.avbpubkey",
9298 private_key: "testkey.pem",
9299 }
9300
9301 cc_binary {
9302 name: "mybin",
9303 srcs: ["mylib.cpp"],
9304 relative_install_path: "foo/bar",
9305 static_executable: true,
9306 system_shared_libs: [],
9307 stl: "none",
9308 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009309 min_sdk_version: "29",
9310 }
9311 `)
9312
9313 testApexError(t, `executable mybin.rust is static`, `
9314 apex {
9315 name: "myapex",
9316 key: "myapex.key",
9317 binaries: ["mybin.rust"],
9318 min_sdk_version: "29",
9319 }
9320
9321 apex_key {
9322 name: "myapex.key",
9323 public_key: "testkey.avbpubkey",
9324 private_key: "testkey.pem",
9325 }
9326
9327 rust_binary {
9328 name: "mybin.rust",
9329 srcs: ["foo.rs"],
9330 static_executable: true,
9331 apex_available: ["myapex"],
9332 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009333 }
9334 `)
9335}
9336
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009337func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9338 ctx := testApex(t, `
9339 apex {
9340 name: "myapex",
9341 key: "myapex.key",
9342 updatable: false,
9343 java_libs: ["foo"],
9344 }
9345
9346 apex_key {
9347 name: "myapex.key",
9348 public_key: "testkey.avbpubkey",
9349 private_key: "testkey.pem",
9350 }
9351
9352 java_library {
9353 name: "foo",
9354 srcs: ["foo.java"],
9355 apex_available: ["myapex"],
9356 installable: true,
9357 }
9358 `,
9359 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9360 )
9361
9362 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9363 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9364 var builder strings.Builder
9365 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9366 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009367 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.myapex foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex\n")
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009368}
9369
9370func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9371 ctx := testApex(t, `
9372 prebuilt_apex {
9373 name: "myapex",
9374 arch: {
9375 arm64: {
9376 src: "myapex-arm64.apex",
9377 },
9378 arm: {
9379 src: "myapex-arm.apex",
9380 },
9381 },
9382 exported_java_libs: ["foo"],
9383 }
9384
9385 java_import {
9386 name: "foo",
9387 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009388 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009389 }
9390 `,
9391 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9392 )
9393
9394 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9395 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9396 mainModuleEntries := entriesList[0]
9397 android.AssertArrayString(t,
9398 "LOCAL_REQUIRED_MODULES",
9399 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9400 []string{
9401 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9402 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9403 })
9404}
9405
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009406func TestAndroidMk_RequiredModules(t *testing.T) {
9407 ctx := testApex(t, `
9408 apex {
9409 name: "myapex",
9410 key: "myapex.key",
9411 updatable: false,
9412 java_libs: ["foo"],
9413 required: ["otherapex"],
9414 }
9415
9416 apex {
9417 name: "otherapex",
9418 key: "myapex.key",
9419 updatable: false,
9420 java_libs: ["foo"],
9421 required: ["otherapex"],
9422 }
9423
9424 apex_key {
9425 name: "myapex.key",
9426 public_key: "testkey.avbpubkey",
9427 private_key: "testkey.pem",
9428 }
9429
9430 java_library {
9431 name: "foo",
9432 srcs: ["foo.java"],
9433 apex_available: ["myapex", "otherapex"],
9434 installable: true,
9435 }
9436 `)
9437
9438 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9439 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9440 var builder strings.Builder
9441 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9442 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009443 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009444}
9445
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009446func TestAndroidMk_RequiredDeps(t *testing.T) {
9447 ctx := testApex(t, `
9448 apex {
9449 name: "myapex",
9450 key: "myapex.key",
9451 updatable: false,
9452 }
9453
9454 apex_key {
9455 name: "myapex.key",
9456 public_key: "testkey.avbpubkey",
9457 private_key: "testkey.pem",
9458 }
9459 `)
9460
9461 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009462 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009463 data := android.AndroidMkDataForTest(t, ctx, bundle)
9464 var builder strings.Builder
9465 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9466 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009467 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009468
9469 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009470 flattenedBundle.makeModulesToInstall = append(flattenedBundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009471 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
9472 var flattenedBuilder strings.Builder
9473 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
9474 flattenedAndroidMk := flattenedBuilder.String()
Sasha Smundakdcb61292022-12-08 10:41:33 -08009475 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex.flattened apex_pubkey.myapex.flattened foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009476}
9477
Jooyung Hana6d36672022-02-24 13:58:07 +09009478func TestApexOutputFileProducer(t *testing.T) {
9479 for _, tc := range []struct {
9480 name string
9481 ref string
9482 expected_data []string
9483 }{
9484 {
9485 name: "test_using_output",
9486 ref: ":myapex",
9487 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
9488 },
9489 {
9490 name: "test_using_apex",
9491 ref: ":myapex{.apex}",
9492 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
9493 },
9494 } {
9495 t.Run(tc.name, func(t *testing.T) {
9496 ctx := testApex(t, `
9497 apex {
9498 name: "myapex",
9499 key: "myapex.key",
9500 compressible: true,
9501 updatable: false,
9502 }
9503
9504 apex_key {
9505 name: "myapex.key",
9506 public_key: "testkey.avbpubkey",
9507 private_key: "testkey.pem",
9508 }
9509
9510 java_test {
9511 name: "`+tc.name+`",
9512 srcs: ["a.java"],
9513 data: ["`+tc.ref+`"],
9514 }
9515 `,
9516 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9517 variables.CompressedApex = proptools.BoolPtr(true)
9518 }))
9519 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9520 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9521 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9522 })
9523 }
9524}
9525
satayev758968a2021-12-06 11:42:40 +00009526func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9527 preparer := android.GroupFixturePreparers(
9528 PrepareForTestWithApexBuildComponents,
9529 prepareForTestWithMyapex,
9530 java.PrepareForTestWithJavaSdkLibraryFiles,
9531 java.PrepareForTestWithJavaDefaultModules,
9532 android.PrepareForTestWithAndroidBuildComponents,
9533 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9534 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9535 )
9536
9537 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9538 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9539 preparer.RunTestWithBp(t, `
9540 apex {
9541 name: "myapex",
9542 key: "myapex.key",
9543 bootclasspath_fragments: ["mybootclasspathfragment"],
9544 min_sdk_version: "30",
9545 updatable: false,
9546 }
9547
9548 apex_key {
9549 name: "myapex.key",
9550 public_key: "testkey.avbpubkey",
9551 private_key: "testkey.pem",
9552 }
9553
9554 bootclasspath_fragment {
9555 name: "mybootclasspathfragment",
9556 contents: ["mybootclasspathlib"],
9557 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009558 hidden_api: {
9559 split_packages: ["*"],
9560 },
satayev758968a2021-12-06 11:42:40 +00009561 }
9562
9563 java_sdk_library {
9564 name: "mybootclasspathlib",
9565 srcs: ["mybootclasspathlib.java"],
9566 apex_available: ["myapex"],
9567 compile_dex: true,
9568 unsafe_ignore_missing_latest_api: true,
9569 min_sdk_version: "31",
9570 static_libs: ["util"],
9571 }
9572
9573 java_library {
9574 name: "util",
9575 srcs: ["a.java"],
9576 apex_available: ["myapex"],
9577 min_sdk_version: "31",
9578 static_libs: ["another_util"],
9579 }
9580
9581 java_library {
9582 name: "another_util",
9583 srcs: ["a.java"],
9584 min_sdk_version: "31",
9585 apex_available: ["myapex"],
9586 }
9587 `)
9588 })
9589
9590 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9591 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9592 preparer.RunTestWithBp(t, `
9593 apex {
9594 name: "myapex",
9595 key: "myapex.key",
9596 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9597 min_sdk_version: "30",
9598 updatable: false,
9599 }
9600
9601 apex_key {
9602 name: "myapex.key",
9603 public_key: "testkey.avbpubkey",
9604 private_key: "testkey.pem",
9605 }
9606
9607 systemserverclasspath_fragment {
9608 name: "mysystemserverclasspathfragment",
9609 contents: ["mysystemserverclasspathlib"],
9610 apex_available: ["myapex"],
9611 }
9612
9613 java_sdk_library {
9614 name: "mysystemserverclasspathlib",
9615 srcs: ["mysystemserverclasspathlib.java"],
9616 apex_available: ["myapex"],
9617 compile_dex: true,
9618 min_sdk_version: "32",
9619 unsafe_ignore_missing_latest_api: true,
9620 static_libs: ["util"],
9621 }
9622
9623 java_library {
9624 name: "util",
9625 srcs: ["a.java"],
9626 apex_available: ["myapex"],
9627 min_sdk_version: "31",
9628 static_libs: ["another_util"],
9629 }
9630
9631 java_library {
9632 name: "another_util",
9633 srcs: ["a.java"],
9634 min_sdk_version: "31",
9635 apex_available: ["myapex"],
9636 }
9637 `)
9638 })
9639
9640 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9641 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9642 RunTestWithBp(t, `
9643 apex {
9644 name: "myapex",
9645 key: "myapex.key",
9646 bootclasspath_fragments: ["mybootclasspathfragment"],
9647 min_sdk_version: "30",
9648 updatable: false,
9649 }
9650
9651 apex_key {
9652 name: "myapex.key",
9653 public_key: "testkey.avbpubkey",
9654 private_key: "testkey.pem",
9655 }
9656
9657 bootclasspath_fragment {
9658 name: "mybootclasspathfragment",
9659 contents: ["mybootclasspathlib"],
9660 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009661 hidden_api: {
9662 split_packages: ["*"],
9663 },
satayev758968a2021-12-06 11:42:40 +00009664 }
9665
9666 java_sdk_library {
9667 name: "mybootclasspathlib",
9668 srcs: ["mybootclasspathlib.java"],
9669 apex_available: ["myapex"],
9670 compile_dex: true,
9671 unsafe_ignore_missing_latest_api: true,
9672 }
9673 `)
9674 })
9675
9676 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9677 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9678 RunTestWithBp(t, `
9679 apex {
9680 name: "myapex",
9681 key: "myapex.key",
9682 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9683 min_sdk_version: "30",
9684 updatable: false,
9685 }
9686
9687 apex_key {
9688 name: "myapex.key",
9689 public_key: "testkey.avbpubkey",
9690 private_key: "testkey.pem",
9691 }
9692
9693 systemserverclasspath_fragment {
9694 name: "mysystemserverclasspathfragment",
9695 contents: ["mysystemserverclasspathlib"],
9696 apex_available: ["myapex"],
9697 }
9698
9699 java_sdk_library {
9700 name: "mysystemserverclasspathlib",
9701 srcs: ["mysystemserverclasspathlib.java"],
9702 apex_available: ["myapex"],
9703 compile_dex: true,
9704 unsafe_ignore_missing_latest_api: true,
9705 }
9706 `)
9707 })
9708}
9709
Jiakai Zhang6decef92022-01-12 17:56:19 +00009710// Verifies that the APEX depends on all the Make modules in the list.
9711func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9712 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9713 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009714 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009715 }
9716}
9717
9718// Verifies that the APEX does not depend on any of the Make modules in the list.
9719func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9720 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9721 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009722 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009723 }
9724}
9725
Cole Faust1021ccd2023-02-26 21:15:25 -08009726// TODO(b/193460475): Re-enable this test
9727//func TestApexStrictUpdtabilityLint(t *testing.T) {
9728// bpTemplate := `
9729// apex {
9730// name: "myapex",
9731// key: "myapex.key",
9732// java_libs: ["myjavalib"],
9733// updatable: %v,
9734// min_sdk_version: "29",
9735// }
9736// apex_key {
9737// name: "myapex.key",
9738// }
9739// java_library {
9740// name: "myjavalib",
9741// srcs: ["MyClass.java"],
9742// apex_available: [ "myapex" ],
9743// lint: {
9744// strict_updatability_linting: %v,
9745// },
9746// sdk_version: "current",
9747// min_sdk_version: "29",
9748// }
9749// `
9750// fs := android.MockFS{
9751// "lint-baseline.xml": nil,
9752// }
9753//
9754// testCases := []struct {
9755// testCaseName string
9756// apexUpdatable bool
9757// javaStrictUpdtabilityLint bool
9758// lintFileExists bool
9759// disallowedFlagExpected bool
9760// }{
9761// {
9762// testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9763// apexUpdatable: true,
9764// javaStrictUpdtabilityLint: true,
9765// lintFileExists: false,
9766// disallowedFlagExpected: false,
9767// },
9768// {
9769// testCaseName: "non-updatable apex respects strict_updatability of javalib",
9770// apexUpdatable: false,
9771// javaStrictUpdtabilityLint: false,
9772// lintFileExists: true,
9773// disallowedFlagExpected: false,
9774// },
9775// {
9776// testCaseName: "non-updatable apex respects strict updatability of javalib",
9777// apexUpdatable: false,
9778// javaStrictUpdtabilityLint: true,
9779// lintFileExists: true,
9780// disallowedFlagExpected: true,
9781// },
9782// {
9783// testCaseName: "updatable apex sets strict updatability of javalib to true",
9784// apexUpdatable: true,
9785// javaStrictUpdtabilityLint: false, // will be set to true by mutator
9786// lintFileExists: true,
9787// disallowedFlagExpected: true,
9788// },
9789// }
9790//
9791// for _, testCase := range testCases {
9792// bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9793// fixtures := []android.FixturePreparer{}
9794// if testCase.lintFileExists {
9795// fixtures = append(fixtures, fs.AddToFixture())
9796// }
9797//
9798// result := testApex(t, bp, fixtures...)
9799// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9800// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9801// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9802//
9803// if disallowedFlagActual != testCase.disallowedFlagExpected {
9804// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9805// }
9806// }
9807//}
9808//
9809//func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9810// bp := `
9811// apex {
9812// name: "myapex",
9813// key: "myapex.key",
9814// java_libs: ["myjavalib"],
9815// updatable: true,
9816// min_sdk_version: "29",
9817// }
9818// apex_key {
9819// name: "myapex.key",
9820// }
9821// java_library {
9822// name: "myjavalib",
9823// srcs: ["MyClass.java"],
9824// apex_available: [ "myapex" ],
9825// sdk_version: "current",
9826// min_sdk_version: "29",
9827// }
9828// `
9829//
9830// testCases := []struct {
9831// testCaseName string
9832// moduleDirectory string
9833// disallowedFlagExpected bool
9834// }{
9835// {
9836// testCaseName: "lintable module defined outside libcore",
9837// moduleDirectory: "",
9838// disallowedFlagExpected: true,
9839// },
9840// {
9841// testCaseName: "lintable module defined in libcore root directory",
9842// moduleDirectory: "libcore/",
9843// disallowedFlagExpected: false,
9844// },
9845// {
9846// testCaseName: "lintable module defined in libcore child directory",
9847// moduleDirectory: "libcore/childdir/",
9848// disallowedFlagExpected: true,
9849// },
9850// }
9851//
9852// for _, testCase := range testCases {
9853// lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9854// bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9855// result := testApex(t, "", lintFileCreator, bpFileCreator)
9856// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9857// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9858// cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9859// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9860//
9861// if disallowedFlagActual != testCase.disallowedFlagExpected {
9862// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9863// }
9864// }
9865//}
9866//
9867//// checks transtive deps of an apex coming from bootclasspath_fragment
9868//func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9869// bp := `
9870// apex {
9871// name: "myapex",
9872// key: "myapex.key",
9873// bootclasspath_fragments: ["mybootclasspathfragment"],
9874// updatable: true,
9875// min_sdk_version: "29",
9876// }
9877// apex_key {
9878// name: "myapex.key",
9879// }
9880// bootclasspath_fragment {
9881// name: "mybootclasspathfragment",
9882// contents: ["myjavalib"],
9883// apex_available: ["myapex"],
9884// hidden_api: {
9885// split_packages: ["*"],
9886// },
9887// }
9888// java_library {
9889// name: "myjavalib",
9890// srcs: ["MyClass.java"],
9891// apex_available: [ "myapex" ],
9892// sdk_version: "current",
9893// min_sdk_version: "29",
9894// compile_dex: true,
9895// }
9896// `
9897// fs := android.MockFS{
9898// "lint-baseline.xml": nil,
9899// }
9900//
9901// result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9902// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9903// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9904// if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9905// t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9906// }
9907//}
Spandan Das66773252022-01-15 00:23:18 +00009908
Spandan Das42e89502022-05-06 22:12:55 +00009909// updatable apexes should propagate updatable=true to its apps
9910func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9911 bp := `
9912 apex {
9913 name: "myapex",
9914 key: "myapex.key",
9915 updatable: %v,
9916 apps: [
9917 "myapp",
9918 ],
9919 min_sdk_version: "30",
9920 }
9921 apex_key {
9922 name: "myapex.key",
9923 }
9924 android_app {
9925 name: "myapp",
9926 updatable: %v,
9927 apex_available: [
9928 "myapex",
9929 ],
9930 sdk_version: "current",
9931 min_sdk_version: "30",
9932 }
9933 `
9934 testCases := []struct {
9935 name string
9936 apex_is_updatable_bp bool
9937 app_is_updatable_bp bool
9938 app_is_updatable_expected bool
9939 }{
9940 {
9941 name: "Non-updatable apex respects updatable property of non-updatable app",
9942 apex_is_updatable_bp: false,
9943 app_is_updatable_bp: false,
9944 app_is_updatable_expected: false,
9945 },
9946 {
9947 name: "Non-updatable apex respects updatable property of updatable app",
9948 apex_is_updatable_bp: false,
9949 app_is_updatable_bp: true,
9950 app_is_updatable_expected: true,
9951 },
9952 {
9953 name: "Updatable apex respects updatable property of updatable app",
9954 apex_is_updatable_bp: true,
9955 app_is_updatable_bp: true,
9956 app_is_updatable_expected: true,
9957 },
9958 {
9959 name: "Updatable apex sets updatable=true on non-updatable app",
9960 apex_is_updatable_bp: true,
9961 app_is_updatable_bp: false,
9962 app_is_updatable_expected: true,
9963 },
9964 }
9965 for _, testCase := range testCases {
9966 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
9967 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
9968 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
9969 }
9970}
9971
Kiyoung Kim487689e2022-07-26 09:48:22 +09009972func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
9973 bp := `
9974 apex {
9975 name: "myapex",
9976 key: "myapex.key",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009977 native_shared_libs: ["libbaz"],
9978 binaries: ["binfoo"],
Kiyoung Kim487689e2022-07-26 09:48:22 +09009979 min_sdk_version: "29",
9980 }
9981 apex_key {
9982 name: "myapex.key",
9983 }
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009984 cc_binary {
9985 name: "binfoo",
9986 shared_libs: ["libbar", "libbaz", "libqux",],
Kiyoung Kim487689e2022-07-26 09:48:22 +09009987 apex_available: ["myapex"],
9988 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009989 recovery_available: false,
9990 }
9991 cc_library {
9992 name: "libbar",
9993 srcs: ["libbar.cc"],
9994 stubs: {
9995 symbol_file: "libbar.map.txt",
9996 versions: [
9997 "29",
9998 ],
9999 },
10000 }
10001 cc_library {
10002 name: "libbaz",
10003 srcs: ["libbaz.cc"],
10004 apex_available: ["myapex"],
10005 min_sdk_version: "29",
10006 stubs: {
10007 symbol_file: "libbaz.map.txt",
10008 versions: [
10009 "29",
10010 ],
10011 },
Kiyoung Kim487689e2022-07-26 09:48:22 +090010012 }
10013 cc_api_library {
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010014 name: "libbar",
10015 src: "libbar_stub.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010016 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010017 variants: ["apex.29"],
10018 }
10019 cc_api_variant {
10020 name: "libbar",
10021 variant: "apex",
10022 version: "29",
10023 src: "libbar_apex_29.so",
10024 }
10025 cc_api_library {
10026 name: "libbaz",
10027 src: "libbaz_stub.so",
10028 min_sdk_version: "29",
10029 variants: ["apex.29"],
10030 }
10031 cc_api_variant {
10032 name: "libbaz",
10033 variant: "apex",
10034 version: "29",
10035 src: "libbaz_apex_29.so",
10036 }
10037 cc_api_library {
10038 name: "libqux",
10039 src: "libqux_stub.so",
10040 min_sdk_version: "29",
10041 variants: ["apex.29"],
10042 }
10043 cc_api_variant {
10044 name: "libqux",
10045 variant: "apex",
10046 version: "29",
10047 src: "libqux_apex_29.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010048 }
10049 api_imports {
10050 name: "api_imports",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010051 apex_shared_libs: [
10052 "libbar",
10053 "libbaz",
10054 "libqux",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010055 ],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010056 }
10057 `
10058 result := testApex(t, bp)
10059
10060 hasDep := func(m android.Module, wantDep android.Module) bool {
10061 t.Helper()
10062 var found bool
10063 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10064 if dep == wantDep {
10065 found = true
10066 }
10067 })
10068 return found
10069 }
10070
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010071 // Library defines stubs and cc_api_library should be used with cc_api_library
10072 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Module()
10073 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10074 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
Kiyoung Kim487689e2022-07-26 09:48:22 +090010075
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010076 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10077 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
Kiyoung Kim487689e2022-07-26 09:48:22 +090010078
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010079 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Rule("ld").Args["libFlags"]
10080 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10081 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10082 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
10083
10084 // Library defined in the same APEX should be linked with original definition instead of cc_api_library
10085 libbazApexVariant := result.ModuleForTests("libbaz", "android_arm64_armv8-a_shared_apex29").Module()
10086 libbazApiImportCoreVariant := result.ModuleForTests("libbaz.apiimport", "android_arm64_armv8-a_shared").Module()
10087 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even from same APEX", true, hasDep(binfooApexVariant, libbazApiImportCoreVariant))
10088 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbazApexVariant))
10089
10090 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbaz.so")
10091 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbaz.apiimport.so")
10092 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbaz.apex.29.apiimport.so")
10093
10094 // cc_api_library defined without original library should be linked with cc_api_library
10095 libquxApiImportApexVariant := result.ModuleForTests("libqux.apiimport", "android_arm64_armv8-a_shared").Module()
10096 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even original library definition does not exist", true, hasDep(binfooApexVariant, libquxApiImportApexVariant))
10097 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libqux.apex.29.apiimport.so")
10098}
10099
10100func TestPlatformBinaryBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10101 bp := `
10102 apex {
10103 name: "myapex",
10104 key: "myapex.key",
10105 native_shared_libs: ["libbar"],
10106 min_sdk_version: "29",
10107 }
10108 apex_key {
10109 name: "myapex.key",
10110 }
10111 cc_binary {
10112 name: "binfoo",
10113 shared_libs: ["libbar"],
10114 recovery_available: false,
10115 }
10116 cc_library {
10117 name: "libbar",
10118 srcs: ["libbar.cc"],
10119 apex_available: ["myapex"],
10120 min_sdk_version: "29",
10121 stubs: {
10122 symbol_file: "libbar.map.txt",
10123 versions: [
10124 "29",
10125 ],
10126 },
10127 }
10128 cc_api_library {
10129 name: "libbar",
10130 src: "libbar_stub.so",
10131 variants: ["apex.29"],
10132 }
10133 cc_api_variant {
10134 name: "libbar",
10135 variant: "apex",
10136 version: "29",
10137 src: "libbar_apex_29.so",
10138 }
10139 api_imports {
10140 name: "api_imports",
10141 apex_shared_libs: [
10142 "libbar",
10143 ],
10144 }
10145 `
10146
10147 result := testApex(t, bp)
10148
10149 hasDep := func(m android.Module, wantDep android.Module) bool {
10150 t.Helper()
10151 var found bool
10152 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10153 if dep == wantDep {
10154 found = true
10155 }
10156 })
10157 return found
10158 }
10159
10160 // Library defines stubs and cc_api_library should be used with cc_api_library
10161 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Module()
10162 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10163 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
10164
10165 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10166 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
10167
10168 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
10169 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10170 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10171 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
Kiyoung Kim487689e2022-07-26 09:48:22 +090010172}
Dennis Shend4f5d932023-01-31 20:27:21 +000010173
10174func TestTrimmedApex(t *testing.T) {
10175 bp := `
10176 apex {
10177 name: "myapex",
10178 key: "myapex.key",
10179 native_shared_libs: ["libfoo","libbaz"],
10180 min_sdk_version: "29",
10181 trim_against: "mydcla",
10182 }
10183 apex {
10184 name: "mydcla",
10185 key: "myapex.key",
10186 native_shared_libs: ["libfoo","libbar"],
10187 min_sdk_version: "29",
10188 file_contexts: ":myapex-file_contexts",
10189 dynamic_common_lib_apex: true,
10190 }
10191 apex_key {
10192 name: "myapex.key",
10193 }
10194 cc_library {
10195 name: "libfoo",
10196 shared_libs: ["libc"],
10197 apex_available: ["myapex","mydcla"],
10198 min_sdk_version: "29",
10199 }
10200 cc_library {
10201 name: "libbar",
10202 shared_libs: ["libc"],
10203 apex_available: ["myapex","mydcla"],
10204 min_sdk_version: "29",
10205 }
10206 cc_library {
10207 name: "libbaz",
10208 shared_libs: ["libc"],
10209 apex_available: ["myapex","mydcla"],
10210 min_sdk_version: "29",
10211 }
10212 cc_api_library {
10213 name: "libc",
10214 src: "libc.so",
10215 min_sdk_version: "29",
10216 recovery_available: true,
10217 }
10218 api_imports {
10219 name: "api_imports",
10220 shared_libs: [
10221 "libc",
10222 ],
10223 header_libs: [],
10224 }
10225 `
10226 ctx := testApex(t, bp)
10227 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10228 apexRule := module.MaybeRule("apexRule")
10229 if apexRule.Rule == nil {
10230 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10231 }
10232
10233 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
10234 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("TrimmedApexRule")
10235 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10236 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10237 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10238 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10239}
Jingwen Chendea7a642023-03-28 11:30:50 +000010240
10241func TestCannedFsConfig(t *testing.T) {
10242 ctx := testApex(t, `
10243 apex {
10244 name: "myapex",
10245 key: "myapex.key",
10246 updatable: false,
10247 }
10248
10249 apex_key {
10250 name: "myapex.key",
10251 public_key: "testkey.avbpubkey",
10252 private_key: "testkey.pem",
10253 }`)
10254 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10255 generateFsRule := mod.Rule("generateFsConfig")
10256 cmd := generateFsRule.RuleParams.Command
10257
10258 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10259}
10260
10261func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10262 ctx := testApex(t, `
10263 apex {
10264 name: "myapex",
10265 key: "myapex.key",
10266 canned_fs_config: "my_config",
10267 updatable: false,
10268 }
10269
10270 apex_key {
10271 name: "myapex.key",
10272 public_key: "testkey.avbpubkey",
10273 private_key: "testkey.pem",
10274 }`)
10275 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10276 generateFsRule := mod.Rule("generateFsConfig")
10277 cmd := generateFsRule.RuleParams.Command
10278
10279 // Ensure that canned_fs_config has "cat my_config" at the end
10280 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10281}