blob: bf4158c42a3e25ae698a6d130aa3302c64a1e620 [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
Yu Liueae7b362023-11-16 17:05:47 -080028 "android/soong/aconfig/codegen"
Jooyung Han20348752023-12-05 15:23:56 +090029
Kiyoung Kim487689e2022-07-26 09:48:22 +090030 "github.com/google/blueprint"
Jiyong Parkda6eb592018-12-19 17:12:36 +090031 "github.com/google/blueprint/proptools"
32
33 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080034 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090035 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000036 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070037 prebuilt_etc "android/soong/etc"
Colin Crossbd3a16b2023-04-25 11:30:51 -070038 "android/soong/filesystem"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090039 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090040 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070041 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090042)
43
Jooyung Hand3639552019-08-09 12:57:43 +090044// names returns name list from white space separated string
45func names(s string) (ns []string) {
46 for _, n := range strings.Split(s, " ") {
47 if len(n) > 0 {
48 ns = append(ns, n)
49 }
50 }
51 return
52}
53
Paul Duffin40b62572021-03-20 11:39:01 +000054func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090055 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010056 android.GroupFixturePreparers(
57 prepareForApexTest,
58 android.GroupFixturePreparers(preparers...),
59 ).
Paul Duffine05480a2021-03-08 15:07:14 +000060 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000061 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090062}
63
Paul Duffin40b62572021-03-20 11:39:01 +000064func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090065 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010066
67 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000068 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010069 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000070 }
Paul Duffin284165a2021-03-29 01:50:31 +010071
72 result := android.GroupFixturePreparers(
73 prepareForApexTest,
74 android.GroupFixturePreparers(preparers...),
75 optionalBpPreparer,
76 ).RunTest(t)
77
Paul Duffine05480a2021-03-08 15:07:14 +000078 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090079}
80
Paul Duffin810f33d2021-03-09 14:12:32 +000081func withFiles(files android.MockFS) android.FixturePreparer {
82 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090083}
84
Jooyung Han35155c42020-02-06 17:33:20 +090085// withNativeBridgeTargets sets configuration with targets including:
86// - X86_64 (primary)
87// - X86 (secondary)
88// - Arm64 on X86_64 (native bridge)
89// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000090var withNativeBridgeEnabled = android.FixtureModifyConfig(
91 func(config android.Config) {
92 config.Targets[android.Android] = []android.Target{
93 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
94 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
95 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
96 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
97 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
98 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
99 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
100 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
101 }
102 },
103)
104
105func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
106 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
107 variables.ManifestPackageNameOverrides = specs
108 })
Jooyung Han35155c42020-02-06 17:33:20 +0900109}
110
Albert Martineefabcf2022-03-21 20:11:16 +0000111func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
112 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
113 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
114 })
115}
116
Paul Duffin810f33d2021-03-09 14:12:32 +0000117var withBinder32bit = android.FixtureModifyProductVariables(
118 func(variables android.FixtureProductVariables) {
119 variables.Binder32bit = proptools.BoolPtr(true)
120 },
121)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900122
Paul Duffin810f33d2021-03-09 14:12:32 +0000123var withUnbundledBuild = android.FixtureModifyProductVariables(
124 func(variables android.FixtureProductVariables) {
125 variables.Unbundled_build = proptools.BoolPtr(true)
126 },
127)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900128
Paul Duffin284165a2021-03-29 01:50:31 +0100129// Legacy preparer used for running tests within the apex package.
130//
131// This includes everything that was needed to run any test in the apex package prior to the
132// introduction of the test fixtures. Tests that are being converted to use fixtures directly
133// rather than through the testApex...() methods should avoid using this and instead use the
134// various preparers directly, using android.GroupFixturePreparers(...) to group them when
135// necessary.
136//
137// deprecated
138var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000139 // General preparers in alphabetical order as test infrastructure will enforce correct
140 // registration order.
141 android.PrepareForTestWithAndroidBuildComponents,
142 bpf.PrepareForTestWithBpf,
143 cc.PrepareForTestWithCcBuildComponents,
Jiakai Zhangb95998b2023-05-11 16:39:27 +0100144 java.PrepareForTestWithDexpreopt,
Paul Duffin37aad602021-03-08 09:47:16 +0000145 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
146 rust.PrepareForTestWithRustDefaultModules,
147 sh.PrepareForTestWithShBuildComponents,
Yu Liueae7b362023-11-16 17:05:47 -0800148 codegen.PrepareForTestWithAconfigBuildComponents,
Paul Duffin37aad602021-03-08 09:47:16 +0000149
150 PrepareForTestWithApexBuildComponents,
151
152 // Additional apex test specific preparers.
153 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
154 filegroup {
155 name: "myapex-file_contexts",
156 srcs: [
157 "apex/myapex-file_contexts",
158 ],
159 }
160 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000161 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000162 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000163 "a.java": nil,
164 "PrebuiltAppFoo.apk": nil,
165 "PrebuiltAppFooPriv.apk": nil,
166 "apex_manifest.json": nil,
167 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000168 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
169 "system/sepolicy/apex/myapex2-file_contexts": nil,
170 "system/sepolicy/apex/otherapex-file_contexts": nil,
171 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
172 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700173 "mylib.cpp": nil,
174 "mytest.cpp": nil,
175 "mytest1.cpp": nil,
176 "mytest2.cpp": nil,
177 "mytest3.cpp": nil,
178 "myprebuilt": nil,
179 "my_include": nil,
180 "foo/bar/MyClass.java": nil,
181 "prebuilt.jar": nil,
182 "prebuilt.so": nil,
183 "vendor/foo/devkeys/test.x509.pem": nil,
184 "vendor/foo/devkeys/test.pk8": nil,
185 "testkey.x509.pem": nil,
186 "testkey.pk8": nil,
187 "testkey.override.x509.pem": nil,
188 "testkey.override.pk8": nil,
189 "vendor/foo/devkeys/testkey.avbpubkey": nil,
190 "vendor/foo/devkeys/testkey.pem": nil,
191 "NOTICE": nil,
192 "custom_notice": nil,
193 "custom_notice_for_static_lib": nil,
194 "testkey2.avbpubkey": nil,
195 "testkey2.pem": nil,
196 "myapex-arm64.apex": nil,
197 "myapex-arm.apex": nil,
198 "myapex.apks": nil,
199 "frameworks/base/api/current.txt": nil,
200 "framework/aidl/a.aidl": nil,
201 "dummy.txt": nil,
202 "baz": nil,
203 "bar/baz": nil,
204 "testdata/baz": nil,
205 "AppSet.apks": nil,
206 "foo.rs": nil,
207 "libfoo.jar": nil,
208 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000209 },
210 ),
211
212 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
Paul Duffin37aad602021-03-08 09:47:16 +0000213 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
214 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
215 variables.Platform_sdk_codename = proptools.StringPtr("Q")
216 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000217 // "Tiramisu" needs to be in the next line for compatibility with soong code,
218 // not because of these tests specifically (it's not used by the tests)
219 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000220 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000221 }),
222)
223
Paul Duffin52bfaa42021-03-23 23:40:12 +0000224var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
225 "system/sepolicy/apex/myapex-file_contexts": nil,
226})
227
Jooyung Han643adc42020-02-27 13:50:06 +0900228// ensure that 'result' equals 'expected'
229func ensureEquals(t *testing.T, result string, expected string) {
230 t.Helper()
231 if result != expected {
232 t.Errorf("%q != %q", expected, result)
233 }
234}
235
Jiyong Park25fc6a92018-11-18 18:02:45 +0900236// ensure that 'result' contains 'expected'
237func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900238 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900239 if !strings.Contains(result, expected) {
240 t.Errorf("%q is not found in %q", expected, result)
241 }
242}
243
Liz Kammer5bd365f2020-05-27 15:15:11 -0700244// ensure that 'result' contains 'expected' exactly one time
245func ensureContainsOnce(t *testing.T, result string, expected string) {
246 t.Helper()
247 count := strings.Count(result, expected)
248 if count != 1 {
249 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
250 }
251}
252
Jiyong Park25fc6a92018-11-18 18:02:45 +0900253// ensures that 'result' does not contain 'notExpected'
254func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900255 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900256 if strings.Contains(result, notExpected) {
257 t.Errorf("%q is found in %q", notExpected, result)
258 }
259}
260
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700261func ensureMatches(t *testing.T, result string, expectedRex string) {
Jooyung Hana3fddf42024-09-03 13:22:21 +0900262 t.Helper()
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700263 ok, err := regexp.MatchString(expectedRex, result)
264 if err != nil {
265 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
266 return
267 }
268 if !ok {
269 t.Errorf("%s does not match regular expession %s", result, expectedRex)
270 }
271}
272
Jooyung Hana3fddf42024-09-03 13:22:21 +0900273func ensureListContainsMatch(t *testing.T, result []string, expectedRex string) {
274 t.Helper()
275 p := regexp.MustCompile(expectedRex)
276 if android.IndexListPred(func(s string) bool { return p.MatchString(s) }, result) == -1 {
277 t.Errorf("%q is not found in %v", expectedRex, result)
278 }
279}
280
Jiyong Park25fc6a92018-11-18 18:02:45 +0900281func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900282 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900283 if !android.InList(expected, result) {
284 t.Errorf("%q is not found in %v", expected, result)
285 }
286}
287
288func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900289 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900290 if android.InList(notExpected, result) {
291 t.Errorf("%q is found in %v", notExpected, result)
292 }
293}
294
Jooyung Hane1633032019-08-01 17:41:43 +0900295func ensureListEmpty(t *testing.T, result []string) {
296 t.Helper()
297 if len(result) > 0 {
298 t.Errorf("%q is expected to be empty", result)
299 }
300}
301
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000302func ensureListNotEmpty(t *testing.T, result []string) {
303 t.Helper()
304 if len(result) == 0 {
305 t.Errorf("%q is expected to be not empty", result)
306 }
307}
308
Jiyong Park25fc6a92018-11-18 18:02:45 +0900309// Minimal test
310func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800311 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900312 apex_defaults {
313 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900314 manifest: ":myapex.manifest",
315 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900316 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900317 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900318 native_shared_libs: [
319 "mylib",
320 "libfoo.ffi",
321 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900322 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800323 multilib: {
324 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900325 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800326 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900327 },
Jiyong Park77acec62020-06-01 21:39:15 +0900328 java_libs: [
329 "myjar",
330 "myjar_dex",
331 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000332 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900333 }
334
Jiyong Park30ca9372019-02-07 16:27:23 +0900335 apex {
336 name: "myapex",
337 defaults: ["myapex-defaults"],
338 }
339
Jiyong Park25fc6a92018-11-18 18:02:45 +0900340 apex_key {
341 name: "myapex.key",
342 public_key: "testkey.avbpubkey",
343 private_key: "testkey.pem",
344 }
345
Jiyong Park809bb722019-02-13 21:33:49 +0900346 filegroup {
347 name: "myapex.manifest",
348 srcs: ["apex_manifest.json"],
349 }
350
351 filegroup {
352 name: "myapex.androidmanifest",
353 srcs: ["AndroidManifest.xml"],
354 }
355
Jiyong Park25fc6a92018-11-18 18:02:45 +0900356 cc_library {
357 name: "mylib",
358 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900359 shared_libs: [
360 "mylib2",
361 "libbar.ffi",
362 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900363 system_shared_libs: [],
364 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000365 // TODO: remove //apex_available:platform
366 apex_available: [
367 "//apex_available:platform",
368 "myapex",
369 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900370 }
371
Alex Light3d673592019-01-18 14:37:31 -0800372 cc_binary {
373 name: "foo",
374 srcs: ["mylib.cpp"],
375 compile_multilib: "both",
376 multilib: {
377 lib32: {
378 suffix: "32",
379 },
380 lib64: {
381 suffix: "64",
382 },
383 },
384 symlinks: ["foo_link_"],
385 symlink_preferred_arch: true,
386 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800387 stl: "none",
Jooyung Han40b79172024-08-16 16:00:33 +0900388 apex_available: [ "myapex" ],
Yifan Hongd22a84a2020-07-28 17:37:46 -0700389 }
390
Jiyong Park99644e92020-11-17 22:21:02 +0900391 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000392 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900393 srcs: ["foo.rs"],
394 rlibs: ["libfoo.rlib.rust"],
Vinh Tran4eeb2a92023-08-14 13:29:30 -0400395 rustlibs: ["libfoo.dylib.rust"],
Jiyong Park99644e92020-11-17 22:21:02 +0900396 apex_available: ["myapex"],
397 }
398
399 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000400 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900401 srcs: ["foo.rs"],
402 crate_name: "foo",
403 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900404 shared_libs: ["libfoo.shared_from_rust"],
405 }
406
407 cc_library_shared {
408 name: "libfoo.shared_from_rust",
409 srcs: ["mylib.cpp"],
410 system_shared_libs: [],
411 stl: "none",
412 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900413 }
414
415 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000416 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900417 srcs: ["foo.rs"],
418 crate_name: "foo",
419 apex_available: ["myapex"],
420 }
421
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900422 rust_ffi_shared {
423 name: "libfoo.ffi",
424 srcs: ["foo.rs"],
425 crate_name: "foo",
426 apex_available: ["myapex"],
427 }
428
429 rust_ffi_shared {
430 name: "libbar.ffi",
431 srcs: ["foo.rs"],
432 crate_name: "bar",
433 apex_available: ["myapex"],
434 }
435
Paul Duffindddd5462020-04-07 15:25:44 +0100436 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900437 name: "mylib2",
438 srcs: ["mylib.cpp"],
439 system_shared_libs: [],
440 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900441 static_libs: ["libstatic"],
442 // TODO: remove //apex_available:platform
443 apex_available: [
444 "//apex_available:platform",
445 "myapex",
446 ],
447 }
448
Paul Duffindddd5462020-04-07 15:25:44 +0100449 cc_prebuilt_library_shared {
450 name: "mylib2",
451 srcs: ["prebuilt.so"],
452 // TODO: remove //apex_available:platform
453 apex_available: [
454 "//apex_available:platform",
455 "myapex",
456 ],
457 }
458
Jiyong Park9918e1a2020-03-17 19:16:40 +0900459 cc_library_static {
460 name: "libstatic",
461 srcs: ["mylib.cpp"],
462 system_shared_libs: [],
463 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000464 // TODO: remove //apex_available:platform
465 apex_available: [
466 "//apex_available:platform",
467 "myapex",
468 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900469 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900470
471 java_library {
472 name: "myjar",
473 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900474 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475 sdk_version: "none",
476 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900477 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900478 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000479 // TODO: remove //apex_available:platform
480 apex_available: [
481 "//apex_available:platform",
482 "myapex",
483 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484 }
485
Jiyong Park77acec62020-06-01 21:39:15 +0900486 dex_import {
487 name: "myjar_dex",
488 jars: ["prebuilt.jar"],
489 apex_available: [
490 "//apex_available:platform",
491 "myapex",
492 ],
493 }
494
Jiyong Park7f7766d2019-07-25 22:02:35 +0900495 java_library {
496 name: "myotherjar",
497 srcs: ["foo/bar/MyClass.java"],
498 sdk_version: "none",
499 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900500 // TODO: remove //apex_available:platform
501 apex_available: [
502 "//apex_available:platform",
503 "myapex",
504 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900505 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900506
507 java_library {
508 name: "mysharedjar",
509 srcs: ["foo/bar/MyClass.java"],
510 sdk_version: "none",
511 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900512 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900513 `)
514
Jooyung Hana0503a52023-08-23 13:12:50 +0900515 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900516
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900517 // Make sure that Android.mk is created
Jooyung Hana0503a52023-08-23 13:12:50 +0900518 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700519 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900520 var builder strings.Builder
521 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
522
523 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000524 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900525 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
526
Jiyong Park42cca6c2019-04-01 11:15:50 +0900527 optFlags := apexRule.Args["opt_flags"]
528 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700529 // Ensure that the NOTICE output is being packaged as an asset.
Jooyung Hana0503a52023-08-23 13:12:50 +0900530 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900531
Jiyong Park25fc6a92018-11-18 18:02:45 +0900532 copyCmds := apexRule.Args["copy_commands"]
533
534 // Ensure that main rule creates an output
535 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
536
537 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700538 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
539 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
540 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900541 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900542 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900543
544 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700545 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
546 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900547 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
548 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900549 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900550 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900551
552 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800553 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
554 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900555 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900556 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900557 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900558 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
559 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900560 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900561 // .. but not for java libs
562 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900563 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800564
Colin Cross7113d202019-11-20 16:39:12 -0800565 // Ensure that the platform variant ends with _shared or _common
566 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
567 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900568 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
569 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900570 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
571
572 // Ensure that dynamic dependency to java libs are not included
573 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800574
575 // Ensure that all symlinks are present.
576 found_foo_link_64 := false
577 found_foo := false
578 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900579 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800580 if strings.HasSuffix(cmd, "bin/foo") {
581 found_foo = true
582 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
583 found_foo_link_64 = true
584 }
585 }
586 }
587 good := found_foo && found_foo_link_64
588 if !good {
589 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
590 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900591
Colin Crossf61d03d2023-11-02 16:56:39 -0700592 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
593 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100594 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100595 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
596 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
597 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100598
Colin Crossf61d03d2023-11-02 16:56:39 -0700599 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
600 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100601 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100602 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
603 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
604 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800605}
606
Jooyung Hanf21c7972019-12-16 22:32:06 +0900607func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800608 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900609 apex_defaults {
610 name: "myapex-defaults",
611 key: "myapex.key",
612 prebuilts: ["myetc"],
613 native_shared_libs: ["mylib"],
614 java_libs: ["myjar"],
615 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900616 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800617 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000618 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900619 }
620
621 prebuilt_etc {
622 name: "myetc",
623 src: "myprebuilt",
624 }
625
626 apex {
627 name: "myapex",
628 defaults: ["myapex-defaults"],
629 }
630
631 apex_key {
632 name: "myapex.key",
633 public_key: "testkey.avbpubkey",
634 private_key: "testkey.pem",
635 }
636
637 cc_library {
638 name: "mylib",
639 system_shared_libs: [],
640 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000641 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900642 }
643
644 java_library {
645 name: "myjar",
646 srcs: ["foo/bar/MyClass.java"],
647 sdk_version: "none",
648 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000649 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900650 }
651
652 android_app {
653 name: "AppFoo",
654 srcs: ["foo/bar/MyClass.java"],
655 sdk_version: "none",
656 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000657 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900658 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900659
660 runtime_resource_overlay {
661 name: "rro",
662 theme: "blue",
663 }
664
markchien2f59ec92020-09-02 16:23:38 +0800665 bpf {
666 name: "bpf",
667 srcs: ["bpf.c", "bpf2.c"],
668 }
669
Ken Chenfad7f9d2021-11-10 22:02:57 +0800670 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800671 name: "netdTest",
672 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800673 sub_dir: "netd",
674 }
675
Jooyung Hanf21c7972019-12-16 22:32:06 +0900676 `)
Jooyung Hana0503a52023-08-23 13:12:50 +0900677 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900678 "etc/myetc",
679 "javalib/myjar.jar",
680 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000681 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900682 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800683 "etc/bpf/bpf.o",
684 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800685 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900686 })
687}
688
Jooyung Han01a3ee22019-11-02 02:52:25 +0900689func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800690 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900691 apex {
692 name: "myapex",
693 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000694 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900695 }
696
697 apex_key {
698 name: "myapex.key",
699 public_key: "testkey.avbpubkey",
700 private_key: "testkey.pem",
701 }
702 `)
703
Jooyung Hana0503a52023-08-23 13:12:50 +0900704 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +0900705 args := module.Rule("apexRule").Args
706 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
707 t.Error("manifest should be apex_manifest.pb, but " + manifest)
708 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900709}
710
Liz Kammer4854a7d2021-05-27 14:28:27 -0400711func TestApexManifestMinSdkVersion(t *testing.T) {
712 ctx := testApex(t, `
713 apex_defaults {
714 name: "my_defaults",
715 key: "myapex.key",
716 product_specific: true,
717 file_contexts: ":my-file-contexts",
718 updatable: false,
719 }
720 apex {
721 name: "myapex_30",
722 min_sdk_version: "30",
723 defaults: ["my_defaults"],
724 }
725
726 apex {
727 name: "myapex_current",
728 min_sdk_version: "current",
729 defaults: ["my_defaults"],
730 }
731
732 apex {
733 name: "myapex_none",
734 defaults: ["my_defaults"],
735 }
736
737 apex_key {
738 name: "myapex.key",
739 public_key: "testkey.avbpubkey",
740 private_key: "testkey.pem",
741 }
742
743 filegroup {
744 name: "my-file-contexts",
745 srcs: ["product_specific_file_contexts"],
746 }
747 `, withFiles(map[string][]byte{
748 "product_specific_file_contexts": nil,
749 }), android.FixtureModifyProductVariables(
750 func(variables android.FixtureProductVariables) {
751 variables.Unbundled_build = proptools.BoolPtr(true)
752 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
753 }), android.FixtureMergeEnv(map[string]string{
754 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
755 }))
756
757 testCases := []struct {
758 module string
759 minSdkVersion string
760 }{
761 {
762 module: "myapex_30",
763 minSdkVersion: "30",
764 },
765 {
766 module: "myapex_current",
767 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
768 },
769 {
770 module: "myapex_none",
771 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
772 },
773 }
774 for _, tc := range testCases {
Jooyung Hana0503a52023-08-23 13:12:50 +0900775 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
Liz Kammer4854a7d2021-05-27 14:28:27 -0400776 args := module.Rule("apexRule").Args
777 optFlags := args["opt_flags"]
778 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
779 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
780 }
781 }
782}
783
Jihoon Kang842b9992024-02-08 01:41:51 +0000784func TestApexWithDessertSha(t *testing.T) {
785 ctx := testApex(t, `
786 apex_defaults {
787 name: "my_defaults",
788 key: "myapex.key",
789 product_specific: true,
790 file_contexts: ":my-file-contexts",
791 updatable: false,
792 }
793 apex {
794 name: "myapex_30",
795 min_sdk_version: "30",
796 defaults: ["my_defaults"],
797 }
798
799 apex {
800 name: "myapex_current",
801 min_sdk_version: "current",
802 defaults: ["my_defaults"],
803 }
804
805 apex {
806 name: "myapex_none",
807 defaults: ["my_defaults"],
808 }
809
810 apex_key {
811 name: "myapex.key",
812 public_key: "testkey.avbpubkey",
813 private_key: "testkey.pem",
814 }
815
816 filegroup {
817 name: "my-file-contexts",
818 srcs: ["product_specific_file_contexts"],
819 }
820 `, withFiles(map[string][]byte{
821 "product_specific_file_contexts": nil,
822 }), android.FixtureModifyProductVariables(
823 func(variables android.FixtureProductVariables) {
824 variables.Unbundled_build = proptools.BoolPtr(true)
825 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
826 }), android.FixtureMergeEnv(map[string]string{
827 "UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA": "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
828 }))
829
830 testCases := []struct {
831 module string
832 minSdkVersion string
833 }{
834 {
835 module: "myapex_30",
836 minSdkVersion: "30",
837 },
838 {
839 module: "myapex_current",
840 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
841 },
842 {
843 module: "myapex_none",
844 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
845 },
846 }
847 for _, tc := range testCases {
848 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
849 args := module.Rule("apexRule").Args
850 optFlags := args["opt_flags"]
851 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
852 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
853 }
854 }
855}
856
Jooyung Hanaf730952023-02-28 14:13:38 +0900857func TestFileContexts(t *testing.T) {
Jooyung Hanbe953902023-05-31 16:42:16 +0900858 for _, vendor := range []bool{true, false} {
Jooyung Hanaf730952023-02-28 14:13:38 +0900859 prop := ""
Jooyung Hanbe953902023-05-31 16:42:16 +0900860 if vendor {
861 prop = "vendor: true,\n"
Jooyung Hanaf730952023-02-28 14:13:38 +0900862 }
863 ctx := testApex(t, `
864 apex {
865 name: "myapex",
866 key: "myapex.key",
Jooyung Hanaf730952023-02-28 14:13:38 +0900867 updatable: false,
Jooyung Hanaf730952023-02-28 14:13:38 +0900868 `+prop+`
869 }
870
871 apex_key {
872 name: "myapex.key",
873 public_key: "testkey.avbpubkey",
874 private_key: "testkey.pem",
875 }
Jooyung Hanbe953902023-05-31 16:42:16 +0900876 `)
Jooyung Hanaf730952023-02-28 14:13:38 +0900877
Jooyung Hana0503a52023-08-23 13:12:50 +0900878 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("file_contexts")
Jooyung Hanbe953902023-05-31 16:42:16 +0900879 if vendor {
880 android.AssertStringDoesContain(t, "should force-label as vendor_apex_metadata_file",
881 rule.RuleParams.Command,
882 "apex_manifest\\\\.pb u:object_r:vendor_apex_metadata_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900883 } else {
Jooyung Hanbe953902023-05-31 16:42:16 +0900884 android.AssertStringDoesContain(t, "should force-label as system_file",
885 rule.RuleParams.Command,
886 "apex_manifest\\\\.pb u:object_r:system_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900887 }
888 }
889}
890
Jiyong Park25fc6a92018-11-18 18:02:45 +0900891func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800892 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900893 apex {
894 name: "myapex",
895 key: "myapex.key",
896 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900897 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000898 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900899 }
900
901 apex_key {
902 name: "myapex.key",
903 public_key: "testkey.avbpubkey",
904 private_key: "testkey.pem",
905 }
906
907 cc_library {
908 name: "mylib",
909 srcs: ["mylib.cpp"],
Spandan Das357ffcc2024-07-24 18:07:48 +0000910 shared_libs: ["mylib2", "mylib3", "my_prebuilt_platform_lib", "my_prebuilt_platform_stub_only_lib"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900911 system_shared_libs: [],
912 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000913 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900914 }
915
916 cc_library {
917 name: "mylib2",
918 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900919 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900920 system_shared_libs: [],
921 stl: "none",
922 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +0000923 symbol_file: "mylib2.map.txt",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900924 versions: ["1", "2", "3"],
925 },
926 }
927
928 cc_library {
929 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900930 srcs: ["mylib.cpp"],
931 shared_libs: ["mylib4"],
932 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900933 stl: "none",
934 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +0000935 symbol_file: "mylib3.map.txt",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900936 versions: ["10", "11", "12"],
937 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000938 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900939 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900940
941 cc_library {
942 name: "mylib4",
943 srcs: ["mylib.cpp"],
944 system_shared_libs: [],
945 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000946 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900947 }
Jiyong Park105dc322021-06-11 17:22:09 +0900948
Spandan Das357ffcc2024-07-24 18:07:48 +0000949 cc_prebuilt_library_shared {
950 name: "my_prebuilt_platform_lib",
951 stubs: {
952 symbol_file: "my_prebuilt_platform_lib.map.txt",
953 versions: ["1", "2", "3"],
954 },
955 srcs: ["foo.so"],
956 }
957
958 // Similar to my_prebuilt_platform_lib, but this library only provides stubs, i.e. srcs is empty
959 cc_prebuilt_library_shared {
960 name: "my_prebuilt_platform_stub_only_lib",
961 stubs: {
962 symbol_file: "my_prebuilt_platform_stub_only_lib.map.txt",
963 versions: ["1", "2", "3"],
964 }
965 }
966
Jiyong Park105dc322021-06-11 17:22:09 +0900967 rust_binary {
968 name: "foo.rust",
969 srcs: ["foo.rs"],
970 shared_libs: ["libfoo.shared_from_rust"],
971 prefer_rlib: true,
972 apex_available: ["myapex"],
973 }
974
975 cc_library_shared {
976 name: "libfoo.shared_from_rust",
977 srcs: ["mylib.cpp"],
978 system_shared_libs: [],
979 stl: "none",
980 stubs: {
981 versions: ["10", "11", "12"],
982 },
983 }
984
Jiyong Park25fc6a92018-11-18 18:02:45 +0900985 `)
986
Jooyung Hana0503a52023-08-23 13:12:50 +0900987 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900988 copyCmds := apexRule.Args["copy_commands"]
989
990 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800991 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900992
993 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800994 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900995
996 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800997 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900998
Colin Crossaede88c2020-08-11 12:17:01 -0700999 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001000
1001 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001002 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001003 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +09001004 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001005
1006 // 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 -07001007 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001008 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -07001009 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +09001010
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -07001011 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
1012 // is replaced by sharing of "cFlags" in cc/builder.go.
1013 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
1014 // module variable representing "cflags". So it was not detected by ensureNotContains.
1015 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
1016 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
1017 // including the original cflags's "-include mylib.h".
1018 //
Jiyong Park64379952018-12-13 18:37:29 +09001019 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -07001020 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1021 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +09001022
Jiyong Park85cc35a2022-07-17 11:30:47 +09001023 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1024 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
1025 // Ensure that genstub for apex-provided lib is invoked with --apex
1026 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +09001027
Jooyung Hana0503a52023-08-23 13:12:50 +09001028 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +09001029 "lib64/mylib.so",
1030 "lib64/mylib3.so",
1031 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +09001032 "bin/foo.rust",
1033 "lib64/libc++.so", // by the implicit dependency from foo.rust
1034 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +09001035 })
Jiyong Park105dc322021-06-11 17:22:09 +09001036
1037 // Ensure that stub dependency from a rust module is not included
1038 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1039 // The rust module is linked to the stub cc library
Colin Cross004bd3f2023-10-02 11:39:17 -07001040 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park105dc322021-06-11 17:22:09 +09001041 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1042 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +09001043
Jooyung Hana0503a52023-08-23 13:12:50 +09001044 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jiyong Park34d5c332022-02-24 18:02:44 +09001045 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Spandan Das357ffcc2024-07-24 18:07:48 +00001046
1047 // Ensure that mylib is linking with the latest version of stubs for my_prebuilt_platform_lib
1048 ensureContains(t, mylibLdFlags, "my_prebuilt_platform_lib/android_arm64_armv8-a_shared_current/my_prebuilt_platform_lib.so")
1049 // ... and not linking to the non-stub (impl) variant of my_prebuilt_platform_lib
1050 ensureNotContains(t, mylibLdFlags, "my_prebuilt_platform_lib/android_arm64_armv8-a_shared/my_prebuilt_platform_lib.so")
1051 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1052 ensureContains(t, ctx.ModuleForTests("my_prebuilt_platform_lib", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
1053
1054 // Ensure that mylib is linking with the latest version of stubs for my_prebuilt_platform_lib
1055 ensureContains(t, mylibLdFlags, "my_prebuilt_platform_stub_only_lib/android_arm64_armv8-a_shared_current/my_prebuilt_platform_stub_only_lib.so")
1056 // ... and not linking to the non-stub (impl) variant of my_prebuilt_platform_lib
1057 ensureNotContains(t, mylibLdFlags, "my_prebuilt_platform_stub_only_lib/android_arm64_armv8-a_shared/my_prebuilt_platform_stub_only_lib.so")
1058 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1059 ensureContains(t, ctx.ModuleForTests("my_prebuilt_platform_stub_only_lib", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001060}
1061
Jooyung Han20348752023-12-05 15:23:56 +09001062func TestApexShouldNotEmbedStubVariant(t *testing.T) {
1063 testApexError(t, `module "myapex" .*: native_shared_libs: "libbar" is a stub`, `
1064 apex {
1065 name: "myapex",
1066 key: "myapex.key",
1067 vendor: true,
1068 updatable: false,
1069 native_shared_libs: ["libbar"], // should not add an LLNDK stub in a vendor apex
1070 }
1071
1072 apex_key {
1073 name: "myapex.key",
1074 public_key: "testkey.avbpubkey",
1075 private_key: "testkey.pem",
1076 }
1077
1078 cc_library {
1079 name: "libbar",
1080 srcs: ["mylib.cpp"],
1081 llndk: {
1082 symbol_file: "libbar.map.txt",
1083 }
1084 }
1085 `)
1086}
1087
Jiyong Park1bc84122021-06-22 20:23:05 +09001088func TestApexCanUsePrivateApis(t *testing.T) {
1089 ctx := testApex(t, `
1090 apex {
1091 name: "myapex",
1092 key: "myapex.key",
1093 native_shared_libs: ["mylib"],
1094 binaries: ["foo.rust"],
1095 updatable: false,
1096 platform_apis: true,
1097 }
1098
1099 apex_key {
1100 name: "myapex.key",
1101 public_key: "testkey.avbpubkey",
1102 private_key: "testkey.pem",
1103 }
1104
1105 cc_library {
1106 name: "mylib",
1107 srcs: ["mylib.cpp"],
1108 shared_libs: ["mylib2"],
1109 system_shared_libs: [],
1110 stl: "none",
1111 apex_available: [ "myapex" ],
1112 }
1113
1114 cc_library {
1115 name: "mylib2",
1116 srcs: ["mylib.cpp"],
1117 cflags: ["-include mylib.h"],
1118 system_shared_libs: [],
1119 stl: "none",
1120 stubs: {
1121 versions: ["1", "2", "3"],
1122 },
1123 }
1124
1125 rust_binary {
1126 name: "foo.rust",
1127 srcs: ["foo.rs"],
1128 shared_libs: ["libfoo.shared_from_rust"],
1129 prefer_rlib: true,
1130 apex_available: ["myapex"],
1131 }
1132
1133 cc_library_shared {
1134 name: "libfoo.shared_from_rust",
1135 srcs: ["mylib.cpp"],
1136 system_shared_libs: [],
1137 stl: "none",
1138 stubs: {
1139 versions: ["10", "11", "12"],
1140 },
1141 }
1142 `)
1143
Jooyung Hana0503a52023-08-23 13:12:50 +09001144 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park1bc84122021-06-22 20:23:05 +09001145 copyCmds := apexRule.Args["copy_commands"]
1146
1147 // Ensure that indirect stubs dep is not included
1148 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1149 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1150
1151 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1152 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001153 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001154 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1155 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Colin Cross004bd3f2023-10-02 11:39:17 -07001156 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001157 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1158 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1159}
1160
Colin Cross7812fd32020-09-25 12:35:10 -07001161func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1162 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001163 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001164 apex {
1165 name: "myapex",
1166 key: "myapex.key",
1167 native_shared_libs: ["mylib", "mylib3"],
1168 min_sdk_version: "29",
1169 }
1170
1171 apex_key {
1172 name: "myapex.key",
1173 public_key: "testkey.avbpubkey",
1174 private_key: "testkey.pem",
1175 }
1176
1177 cc_library {
1178 name: "mylib",
1179 srcs: ["mylib.cpp"],
1180 shared_libs: ["mylib2", "mylib3"],
1181 system_shared_libs: [],
1182 stl: "none",
1183 apex_available: [ "myapex" ],
1184 min_sdk_version: "28",
1185 }
1186
1187 cc_library {
1188 name: "mylib2",
1189 srcs: ["mylib.cpp"],
1190 cflags: ["-include mylib.h"],
1191 system_shared_libs: [],
1192 stl: "none",
1193 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +00001194 symbol_file: "mylib2.map.txt",
Colin Cross7812fd32020-09-25 12:35:10 -07001195 versions: ["28", "29", "30", "current"],
1196 },
1197 min_sdk_version: "28",
1198 }
1199
1200 cc_library {
1201 name: "mylib3",
1202 srcs: ["mylib.cpp"],
1203 shared_libs: ["mylib4"],
1204 system_shared_libs: [],
1205 stl: "none",
1206 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +00001207 symbol_file: "mylib3.map.txt",
Colin Cross7812fd32020-09-25 12:35:10 -07001208 versions: ["28", "29", "30", "current"],
1209 },
1210 apex_available: [ "myapex" ],
1211 min_sdk_version: "28",
1212 }
1213
1214 cc_library {
1215 name: "mylib4",
1216 srcs: ["mylib.cpp"],
1217 system_shared_libs: [],
1218 stl: "none",
1219 apex_available: [ "myapex" ],
1220 min_sdk_version: "28",
1221 }
1222 `)
1223
Jooyung Hana0503a52023-08-23 13:12:50 +09001224 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Colin Cross7812fd32020-09-25 12:35:10 -07001225 copyCmds := apexRule.Args["copy_commands"]
1226
1227 // Ensure that direct non-stubs dep is always included
1228 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1229
1230 // Ensure that indirect stubs dep is not included
1231 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1232
1233 // Ensure that direct stubs dep is included
1234 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1235
1236 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1237
Jiyong Park55549df2021-02-26 23:57:23 +09001238 // Ensure that mylib is linking with the latest version of stub for mylib2
1239 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001240 // ... and not linking to the non-stub (impl) variant of mylib2
1241 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1242
1243 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1244 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1245 // .. and not linking to the stubs variant of mylib3
1246 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1247
1248 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001249 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001250 ensureNotContains(t, mylib2Cflags, "-include ")
1251
Jiyong Park85cc35a2022-07-17 11:30:47 +09001252 // Ensure that genstub is invoked with --systemapi
1253 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001254
Jooyung Hana0503a52023-08-23 13:12:50 +09001255 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Colin Cross7812fd32020-09-25 12:35:10 -07001256 "lib64/mylib.so",
1257 "lib64/mylib3.so",
1258 "lib64/mylib4.so",
1259 })
1260}
1261
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001262func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1263 t.Parallel()
1264 // myapex (Z)
1265 // mylib -----------------.
1266 // |
1267 // otherapex (29) |
1268 // libstub's versions: 29 Z current
1269 // |
1270 // <platform> |
1271 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001272 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001273 apex {
1274 name: "myapex",
1275 key: "myapex.key",
1276 native_shared_libs: ["mylib"],
1277 min_sdk_version: "Z", // non-final
1278 }
1279
1280 cc_library {
1281 name: "mylib",
1282 srcs: ["mylib.cpp"],
1283 shared_libs: ["libstub"],
1284 apex_available: ["myapex"],
1285 min_sdk_version: "Z",
1286 }
1287
1288 apex_key {
1289 name: "myapex.key",
1290 public_key: "testkey.avbpubkey",
1291 private_key: "testkey.pem",
1292 }
1293
1294 apex {
1295 name: "otherapex",
1296 key: "myapex.key",
1297 native_shared_libs: ["libstub"],
1298 min_sdk_version: "29",
1299 }
1300
1301 cc_library {
1302 name: "libstub",
1303 srcs: ["mylib.cpp"],
1304 stubs: {
1305 versions: ["29", "Z", "current"],
1306 },
1307 apex_available: ["otherapex"],
1308 min_sdk_version: "29",
1309 }
1310
1311 // platform module depending on libstub from otherapex should use the latest stub("current")
1312 cc_library {
1313 name: "libplatform",
1314 srcs: ["mylib.cpp"],
1315 shared_libs: ["libstub"],
1316 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001317 `,
1318 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1319 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1320 variables.Platform_sdk_final = proptools.BoolPtr(false)
1321 variables.Platform_version_active_codenames = []string{"Z"}
1322 }),
1323 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001324
Jiyong Park55549df2021-02-26 23:57:23 +09001325 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001326 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001327 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001328 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001329 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001330
1331 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1332 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1333 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1334 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1335 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1336}
1337
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001338func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001339 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001340 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001341 name: "myapex2",
1342 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001343 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001344 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001345 }
1346
1347 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001348 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001349 public_key: "testkey.avbpubkey",
1350 private_key: "testkey.pem",
1351 }
1352
1353 cc_library {
1354 name: "mylib",
1355 srcs: ["mylib.cpp"],
1356 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001357 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001358 system_shared_libs: [],
1359 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001360 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001361 }
1362
1363 cc_library {
1364 name: "libfoo",
1365 srcs: ["mylib.cpp"],
1366 shared_libs: ["libbar"],
1367 system_shared_libs: [],
1368 stl: "none",
1369 stubs: {
1370 versions: ["10", "20", "30"],
1371 },
1372 }
1373
1374 cc_library {
1375 name: "libbar",
1376 srcs: ["mylib.cpp"],
1377 system_shared_libs: [],
1378 stl: "none",
1379 }
1380
Jiyong Park678c8812020-02-07 17:25:49 +09001381 cc_library_static {
1382 name: "libbaz",
1383 srcs: ["mylib.cpp"],
1384 system_shared_libs: [],
1385 stl: "none",
1386 apex_available: [ "myapex2" ],
1387 }
1388
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001389 `)
1390
Jooyung Hana0503a52023-08-23 13:12:50 +09001391 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001392 copyCmds := apexRule.Args["copy_commands"]
1393
1394 // Ensure that direct non-stubs dep is always included
1395 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1396
1397 // Ensure that indirect stubs dep is not included
1398 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1399
1400 // Ensure that dependency of stubs is not included
1401 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1402
Colin Crossaede88c2020-08-11 12:17:01 -07001403 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001404
1405 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001406 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001407 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001408 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001409
Jiyong Park3ff16992019-12-27 14:11:47 +09001410 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001411
1412 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1413 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001414
Colin Crossf61d03d2023-11-02 16:56:39 -07001415 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1416 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001417 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001418
Colin Crossf61d03d2023-11-02 16:56:39 -07001419 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1420 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001421 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001422}
1423
Jooyung Hand3639552019-08-09 12:57:43 +09001424func TestApexWithRuntimeLibsDependency(t *testing.T) {
1425 /*
1426 myapex
1427 |
1428 v (runtime_libs)
1429 mylib ------+------> libfoo [provides stub]
1430 |
1431 `------> libbar
1432 */
Colin Cross1c460562021-02-16 17:55:47 -08001433 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001434 apex {
1435 name: "myapex",
1436 key: "myapex.key",
1437 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001438 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001439 }
1440
1441 apex_key {
1442 name: "myapex.key",
1443 public_key: "testkey.avbpubkey",
1444 private_key: "testkey.pem",
1445 }
1446
1447 cc_library {
1448 name: "mylib",
1449 srcs: ["mylib.cpp"],
Liz Kammer5f108fa2023-05-11 14:33:17 -04001450 static_libs: ["libstatic"],
1451 shared_libs: ["libshared"],
Jooyung Hand3639552019-08-09 12:57:43 +09001452 runtime_libs: ["libfoo", "libbar"],
1453 system_shared_libs: [],
1454 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001455 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001456 }
1457
1458 cc_library {
1459 name: "libfoo",
1460 srcs: ["mylib.cpp"],
1461 system_shared_libs: [],
1462 stl: "none",
1463 stubs: {
1464 versions: ["10", "20", "30"],
1465 },
1466 }
1467
1468 cc_library {
1469 name: "libbar",
1470 srcs: ["mylib.cpp"],
1471 system_shared_libs: [],
1472 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001473 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001474 }
1475
Liz Kammer5f108fa2023-05-11 14:33:17 -04001476 cc_library {
1477 name: "libstatic",
1478 srcs: ["mylib.cpp"],
1479 system_shared_libs: [],
1480 stl: "none",
1481 apex_available: [ "myapex" ],
1482 runtime_libs: ["libstatic_to_runtime"],
1483 }
1484
1485 cc_library {
1486 name: "libshared",
1487 srcs: ["mylib.cpp"],
1488 system_shared_libs: [],
1489 stl: "none",
1490 apex_available: [ "myapex" ],
1491 runtime_libs: ["libshared_to_runtime"],
1492 }
1493
1494 cc_library {
1495 name: "libstatic_to_runtime",
1496 srcs: ["mylib.cpp"],
1497 system_shared_libs: [],
1498 stl: "none",
1499 apex_available: [ "myapex" ],
1500 }
1501
1502 cc_library {
1503 name: "libshared_to_runtime",
1504 srcs: ["mylib.cpp"],
1505 system_shared_libs: [],
1506 stl: "none",
1507 apex_available: [ "myapex" ],
1508 }
Jooyung Hand3639552019-08-09 12:57:43 +09001509 `)
1510
Jooyung Hana0503a52023-08-23 13:12:50 +09001511 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001512 copyCmds := apexRule.Args["copy_commands"]
1513
1514 // Ensure that direct non-stubs dep is always included
1515 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1516
1517 // Ensure that indirect stubs dep is not included
1518 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1519
1520 // Ensure that runtime_libs dep in included
1521 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
Liz Kammer5f108fa2023-05-11 14:33:17 -04001522 ensureContains(t, copyCmds, "image.apex/lib64/libshared.so")
1523 ensureContains(t, copyCmds, "image.apex/lib64/libshared_to_runtime.so")
1524
1525 ensureNotContains(t, copyCmds, "image.apex/lib64/libstatic_to_runtime.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001526
Jooyung Hana0503a52023-08-23 13:12:50 +09001527 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001528 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1529 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001530}
1531
Paul Duffina02cae32021-03-09 01:44:06 +00001532var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1533 cc.PrepareForTestWithCcBuildComponents,
1534 PrepareForTestWithApexBuildComponents,
1535 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001536 apex {
1537 name: "com.android.runtime",
1538 key: "com.android.runtime.key",
1539 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001540 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001541 }
1542
1543 apex_key {
1544 name: "com.android.runtime.key",
1545 public_key: "testkey.avbpubkey",
1546 private_key: "testkey.pem",
1547 }
Paul Duffina02cae32021-03-09 01:44:06 +00001548 `),
1549 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1550)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001551
Paul Duffina02cae32021-03-09 01:44:06 +00001552func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001553 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001554 cc_library {
1555 name: "libc",
1556 no_libcrt: true,
1557 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001558 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001559 stl: "none",
1560 system_shared_libs: [],
1561 stubs: { versions: ["1"] },
1562 apex_available: ["com.android.runtime"],
1563
1564 sanitize: {
1565 hwaddress: true,
1566 }
1567 }
1568
1569 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001570 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001571 no_libcrt: true,
1572 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001573 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001574 stl: "none",
1575 system_shared_libs: [],
1576 srcs: [""],
1577 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001578 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001579
1580 sanitize: {
1581 never: true,
1582 },
Spandan Das4de7b492023-05-05 21:13:01 +00001583 apex_available: [
1584 "//apex_available:anyapex",
1585 "//apex_available:platform",
1586 ],
Paul Duffina02cae32021-03-09 01:44:06 +00001587 } `)
1588 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001589
Jooyung Hana0503a52023-08-23 13:12:50 +09001590 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001591 "lib64/bionic/libc.so",
1592 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1593 })
1594
Colin Cross4c4c1be2022-02-10 11:41:18 -08001595 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001596
1597 installed := hwasan.Description("install libclang_rt.hwasan")
1598 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1599
1600 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1601 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1602 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1603}
1604
1605func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001606 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001607 prepareForTestOfRuntimeApexWithHwasan,
1608 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1609 variables.SanitizeDevice = []string{"hwaddress"}
1610 }),
1611 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001612 cc_library {
1613 name: "libc",
1614 no_libcrt: true,
1615 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001616 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001617 stl: "none",
1618 system_shared_libs: [],
1619 stubs: { versions: ["1"] },
1620 apex_available: ["com.android.runtime"],
1621 }
1622
1623 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001624 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001625 no_libcrt: true,
1626 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001627 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001628 stl: "none",
1629 system_shared_libs: [],
1630 srcs: [""],
1631 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001632 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001633
1634 sanitize: {
1635 never: true,
1636 },
Spandan Das4de7b492023-05-05 21:13:01 +00001637 apex_available: [
1638 "//apex_available:anyapex",
1639 "//apex_available:platform",
1640 ],
Jooyung Han8ce8db92020-05-15 19:05:05 +09001641 }
Paul Duffina02cae32021-03-09 01:44:06 +00001642 `)
1643 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001644
Jooyung Hana0503a52023-08-23 13:12:50 +09001645 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001646 "lib64/bionic/libc.so",
1647 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1648 })
1649
Colin Cross4c4c1be2022-02-10 11:41:18 -08001650 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001651
1652 installed := hwasan.Description("install libclang_rt.hwasan")
1653 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1654
1655 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1656 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1657 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1658}
1659
Jooyung Han61b66e92020-03-21 14:21:46 +00001660func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1661 testcases := []struct {
1662 name string
1663 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001664 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001665 shouldLink string
1666 shouldNotLink []string
1667 }{
1668 {
Jiyong Park55549df2021-02-26 23:57:23 +09001669 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001670 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001671 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001672 shouldLink: "current",
1673 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001674 },
1675 {
Jiyong Park55549df2021-02-26 23:57:23 +09001676 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001677 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001678 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001679 shouldLink: "current",
1680 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001681 },
1682 }
1683 for _, tc := range testcases {
1684 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001685 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001686 apex {
1687 name: "myapex",
1688 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001689 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001690 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001691 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001692 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001693
Jooyung Han61b66e92020-03-21 14:21:46 +00001694 apex_key {
1695 name: "myapex.key",
1696 public_key: "testkey.avbpubkey",
1697 private_key: "testkey.pem",
1698 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001699
Jooyung Han61b66e92020-03-21 14:21:46 +00001700 cc_library {
1701 name: "mylib",
1702 srcs: ["mylib.cpp"],
1703 vendor_available: true,
1704 shared_libs: ["libbar"],
1705 system_shared_libs: [],
1706 stl: "none",
1707 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001708 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001709 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001710
Jooyung Han61b66e92020-03-21 14:21:46 +00001711 cc_library {
1712 name: "libbar",
1713 srcs: ["mylib.cpp"],
1714 system_shared_libs: [],
1715 stl: "none",
1716 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001717 llndk: {
1718 symbol_file: "libbar.map.txt",
1719 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001720 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001721 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001722 withUnbundledBuild,
1723 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001724
Jooyung Han61b66e92020-03-21 14:21:46 +00001725 // Ensure that LLNDK dep is not included
Jooyung Hana0503a52023-08-23 13:12:50 +09001726 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00001727 "lib64/mylib.so",
1728 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001729
Jooyung Han61b66e92020-03-21 14:21:46 +00001730 // Ensure that LLNDK dep is required
Jooyung Hana0503a52023-08-23 13:12:50 +09001731 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han61b66e92020-03-21 14:21:46 +00001732 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1733 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001734
Steven Moreland2c4000c2021-04-27 02:08:49 +00001735 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1736 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001737 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001738 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001739 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001740
Steven Moreland2c4000c2021-04-27 02:08:49 +00001741 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001742 ver := tc.shouldLink
1743 if tc.shouldLink == "current" {
1744 ver = strconv.Itoa(android.FutureApiLevelInt)
1745 }
1746 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001747 })
1748 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001749}
1750
Jiyong Park25fc6a92018-11-18 18:02:45 +09001751func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001752 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001753 apex {
1754 name: "myapex",
1755 key: "myapex.key",
1756 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001757 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001758 }
1759
1760 apex_key {
1761 name: "myapex.key",
1762 public_key: "testkey.avbpubkey",
1763 private_key: "testkey.pem",
1764 }
1765
1766 cc_library {
1767 name: "mylib",
1768 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001769 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001770 shared_libs: ["libdl#27"],
1771 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001772 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001773 }
1774
1775 cc_library_shared {
1776 name: "mylib_shared",
1777 srcs: ["mylib.cpp"],
1778 shared_libs: ["libdl#27"],
1779 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001780 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001781 }
1782
1783 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001784 name: "libBootstrap",
1785 srcs: ["mylib.cpp"],
1786 stl: "none",
1787 bootstrap: true,
1788 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001789 `)
1790
Jooyung Hana0503a52023-08-23 13:12:50 +09001791 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001792 copyCmds := apexRule.Args["copy_commands"]
1793
1794 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001795 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001796 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1797 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001798
1799 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001800 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001801
Colin Crossaede88c2020-08-11 12:17:01 -07001802 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1803 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1804 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001805
1806 // For dependency to libc
1807 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001808 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001809 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001810 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001811 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001812 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1813 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001814
1815 // For dependency to libm
1816 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001817 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001818 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001819 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001820 // ... and is not compiling with the stub
1821 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1822 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1823
1824 // For dependency to libdl
1825 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001826 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001827 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001828 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1829 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001830 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001831 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001832 // ... Cflags from stub is correctly exported to mylib
1833 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1834 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001835
1836 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001837 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1838 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1839 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1840 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001841}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001842
Jooyung Han749dc692020-04-15 11:03:39 +09001843func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001844 // there are three links between liba --> libz.
1845 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001846 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001847 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001848 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001849 apex {
1850 name: "myapex",
1851 key: "myapex.key",
1852 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001853 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001854 }
1855
1856 apex {
1857 name: "otherapex",
1858 key: "myapex.key",
1859 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001860 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001861 }
1862
1863 apex_key {
1864 name: "myapex.key",
1865 public_key: "testkey.avbpubkey",
1866 private_key: "testkey.pem",
1867 }
1868
1869 cc_library {
1870 name: "libx",
1871 shared_libs: ["liba"],
1872 system_shared_libs: [],
1873 stl: "none",
1874 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001875 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001876 }
1877
1878 cc_library {
1879 name: "liby",
1880 shared_libs: ["liba"],
1881 system_shared_libs: [],
1882 stl: "none",
1883 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001884 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001885 }
1886
1887 cc_library {
1888 name: "liba",
1889 shared_libs: ["libz"],
1890 system_shared_libs: [],
1891 stl: "none",
1892 apex_available: [
1893 "//apex_available:anyapex",
1894 "//apex_available:platform",
1895 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001896 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001897 }
1898
1899 cc_library {
1900 name: "libz",
1901 system_shared_libs: [],
1902 stl: "none",
1903 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001904 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001905 },
1906 }
Jooyung Han749dc692020-04-15 11:03:39 +09001907 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001908
1909 expectLink := func(from, from_variant, to, to_variant string) {
1910 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1911 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1912 }
1913 expectNoLink := func(from, from_variant, to, to_variant string) {
1914 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1915 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1916 }
1917 // platform liba is linked to non-stub version
1918 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001919 // liba in myapex is linked to current
1920 expectLink("liba", "shared_apex29", "libz", "shared_current")
1921 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001922 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001923 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001924 // liba in otherapex is linked to current
1925 expectLink("liba", "shared_apex30", "libz", "shared_current")
1926 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001927 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1928 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001929}
1930
Jooyung Hanaed150d2020-04-02 01:41:41 +09001931func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001932 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001933 apex {
1934 name: "myapex",
1935 key: "myapex.key",
1936 native_shared_libs: ["libx"],
1937 min_sdk_version: "R",
1938 }
1939
1940 apex_key {
1941 name: "myapex.key",
1942 public_key: "testkey.avbpubkey",
1943 private_key: "testkey.pem",
1944 }
1945
1946 cc_library {
1947 name: "libx",
1948 shared_libs: ["libz"],
1949 system_shared_libs: [],
1950 stl: "none",
1951 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001952 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001953 }
1954
1955 cc_library {
1956 name: "libz",
1957 system_shared_libs: [],
1958 stl: "none",
1959 stubs: {
1960 versions: ["29", "R"],
1961 },
1962 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001963 `,
1964 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1965 variables.Platform_version_active_codenames = []string{"R"}
1966 }),
1967 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001968
1969 expectLink := func(from, from_variant, to, to_variant string) {
1970 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1971 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1972 }
1973 expectNoLink := func(from, from_variant, to, to_variant string) {
1974 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1975 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1976 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001977 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1978 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001979 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1980 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001981}
1982
Jooyung Han4c4da062021-06-23 10:23:16 +09001983func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1984 testApex(t, `
1985 apex {
1986 name: "myapex",
1987 key: "myapex.key",
1988 java_libs: ["libx"],
1989 min_sdk_version: "S",
1990 }
1991
1992 apex_key {
1993 name: "myapex.key",
1994 public_key: "testkey.avbpubkey",
1995 private_key: "testkey.pem",
1996 }
1997
1998 java_library {
1999 name: "libx",
2000 srcs: ["a.java"],
2001 apex_available: [ "myapex" ],
2002 sdk_version: "current",
2003 min_sdk_version: "S", // should be okay
2004 }
2005 `,
2006 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2007 variables.Platform_version_active_codenames = []string{"S"}
2008 variables.Platform_sdk_codename = proptools.StringPtr("S")
2009 }),
2010 )
2011}
2012
Jooyung Han749dc692020-04-15 11:03:39 +09002013func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002014 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002015 apex {
2016 name: "myapex",
2017 key: "myapex.key",
2018 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002019 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002020 }
2021
2022 apex_key {
2023 name: "myapex.key",
2024 public_key: "testkey.avbpubkey",
2025 private_key: "testkey.pem",
2026 }
2027
2028 cc_library {
2029 name: "libx",
2030 shared_libs: ["libz"],
2031 system_shared_libs: [],
2032 stl: "none",
2033 apex_available: [ "myapex" ],
2034 }
2035
2036 cc_library {
2037 name: "libz",
2038 system_shared_libs: [],
2039 stl: "none",
2040 stubs: {
2041 versions: ["1", "2"],
2042 },
2043 }
2044 `)
2045
2046 expectLink := func(from, from_variant, to, to_variant string) {
2047 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2048 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2049 }
2050 expectNoLink := func(from, from_variant, to, to_variant string) {
2051 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2052 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2053 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002054 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002055 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002056 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07002057 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09002058}
2059
Jooyung Handfc864c2023-03-20 18:19:07 +09002060func TestApexMinSdkVersion_InVendorApex(t *testing.T) {
Jiyong Park5df7bd32021-08-25 16:18:46 +09002061 ctx := testApex(t, `
2062 apex {
2063 name: "myapex",
2064 key: "myapex.key",
2065 native_shared_libs: ["mylib"],
Jooyung Handfc864c2023-03-20 18:19:07 +09002066 updatable: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002067 vendor: true,
2068 min_sdk_version: "29",
2069 }
2070
2071 apex_key {
2072 name: "myapex.key",
2073 public_key: "testkey.avbpubkey",
2074 private_key: "testkey.pem",
2075 }
2076
2077 cc_library {
2078 name: "mylib",
Jooyung Handfc864c2023-03-20 18:19:07 +09002079 srcs: ["mylib.cpp"],
Jiyong Park5df7bd32021-08-25 16:18:46 +09002080 vendor_available: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002081 min_sdk_version: "29",
Jooyung Handfc864c2023-03-20 18:19:07 +09002082 shared_libs: ["libbar"],
2083 }
2084
2085 cc_library {
2086 name: "libbar",
2087 stubs: { versions: ["29", "30"] },
2088 llndk: { symbol_file: "libbar.map.txt" },
Jiyong Park5df7bd32021-08-25 16:18:46 +09002089 }
2090 `)
2091
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002092 vendorVariant := "android_vendor_arm64_armv8-a"
Jiyong Park5df7bd32021-08-25 16:18:46 +09002093
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002094 mylib := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29")
Jooyung Handfc864c2023-03-20 18:19:07 +09002095
2096 // Ensure that mylib links with "current" LLNDK
2097 libFlags := names(mylib.Rule("ld").Args["libFlags"])
Jooyung Han5e8994e2024-03-12 14:12:12 +09002098 ensureListContains(t, libFlags, "out/soong/.intermediates/libbar/"+vendorVariant+"_shared/libbar.so")
Jooyung Handfc864c2023-03-20 18:19:07 +09002099
2100 // Ensure that mylib is targeting 29
2101 ccRule := ctx.ModuleForTests("mylib", vendorVariant+"_static_apex29").Output("obj/mylib.o")
2102 ensureContains(t, ccRule.Args["cFlags"], "-target aarch64-linux-android29")
2103
2104 // Ensure that the correct variant of crtbegin_so is used.
2105 crtBegin := mylib.Rule("ld").Args["crtBegin"]
2106 ensureContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
Jiyong Park5df7bd32021-08-25 16:18:46 +09002107
2108 // Ensure that the crtbegin_so used by the APEX is targeting 29
2109 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
2110 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
2111}
2112
Jooyung Han4495f842023-04-25 16:39:59 +09002113func TestTrackAllowedDeps(t *testing.T) {
2114 ctx := testApex(t, `
2115 apex {
2116 name: "myapex",
2117 key: "myapex.key",
2118 updatable: true,
2119 native_shared_libs: [
2120 "mylib",
2121 "yourlib",
2122 ],
2123 min_sdk_version: "29",
2124 }
2125
2126 apex {
2127 name: "myapex2",
2128 key: "myapex.key",
2129 updatable: false,
2130 native_shared_libs: ["yourlib"],
2131 }
2132
2133 apex_key {
2134 name: "myapex.key",
2135 public_key: "testkey.avbpubkey",
2136 private_key: "testkey.pem",
2137 }
2138
2139 cc_library {
2140 name: "mylib",
2141 srcs: ["mylib.cpp"],
2142 shared_libs: ["libbar"],
2143 min_sdk_version: "29",
2144 apex_available: ["myapex"],
2145 }
2146
2147 cc_library {
2148 name: "libbar",
2149 stubs: { versions: ["29", "30"] },
2150 }
2151
2152 cc_library {
2153 name: "yourlib",
2154 srcs: ["mylib.cpp"],
2155 min_sdk_version: "29",
2156 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2157 }
2158 `, withFiles(android.MockFS{
2159 "packages/modules/common/build/allowed_deps.txt": nil,
2160 }))
2161
2162 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2163 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2164 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002165 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002166 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002167 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002168
Jooyung Hana0503a52023-08-23 13:12:50 +09002169 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07002170 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2171 myapex.Output("depsinfo/flatlist.txt")), "\n")
Jooyung Han4495f842023-04-25 16:39:59 +09002172 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2173 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2174 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2175 flatlist, "mylib:(minSdkVersion:29)")
2176 android.AssertStringListContains(t, "track platform-available lib",
2177 flatlist, "yourlib(minSdkVersion:29)")
2178}
2179
Nouby Mohamed8609a552024-09-04 22:19:51 +00002180func TestTrackCustomAllowedDepsInvalidDefaultTxt(t *testing.T) {
2181 ctx := testApex(t, `
2182 apex {
2183 name: "myapex",
2184 key: "myapex.key",
2185 updatable: true,
2186 native_shared_libs: [
2187 "mylib",
2188 "yourlib",
2189 ],
2190 min_sdk_version: "29",
2191 }
2192
2193 apex {
2194 name: "myapex2",
2195 key: "myapex.key",
2196 updatable: false,
2197 native_shared_libs: ["yourlib"],
2198 }
2199
2200 apex_key {
2201 name: "myapex.key",
2202 public_key: "testkey.avbpubkey",
2203 private_key: "testkey.pem",
2204 }
2205
2206 cc_library {
2207 name: "mylib",
2208 srcs: ["mylib.cpp"],
2209 shared_libs: ["libbar"],
2210 min_sdk_version: "29",
2211 apex_available: ["myapex"],
2212 }
2213
2214 cc_library {
2215 name: "libbar",
2216 stubs: { versions: ["29", "30"] },
2217 }
2218
2219 cc_library {
2220 name: "yourlib",
2221 srcs: ["mylib.cpp"],
2222 min_sdk_version: "29",
2223 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2224 }
2225 `, withFiles(android.MockFS{
2226 "packages/modules/common/build/custom_allowed_deps.txt": nil,
2227 }),
2228 android.FixtureModifyProductVariables(
2229 func(variables android.FixtureProductVariables) {
2230 variables.ExtraAllowedDepsTxt = proptools.StringPtr("packages/modules/common/build/custom_allowed_deps.txt")
2231 },
2232 ))
2233
2234 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2235 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2236 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
2237 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
2238 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
2239 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
2240
2241 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
2242 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2243 myapex.Output("depsinfo/flatlist.txt")), "\n")
2244 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2245 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2246 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2247 flatlist, "mylib:(minSdkVersion:29)")
2248 android.AssertStringListContains(t, "track platform-available lib",
2249 flatlist, "yourlib(minSdkVersion:29)")
2250}
2251
2252func TestTrackCustomAllowedDepsWithDefaultTxt(t *testing.T) {
2253 ctx := testApex(t, `
2254 apex {
2255 name: "myapex",
2256 key: "myapex.key",
2257 updatable: true,
2258 native_shared_libs: [
2259 "mylib",
2260 "yourlib",
2261 ],
2262 min_sdk_version: "29",
2263 }
2264
2265 apex {
2266 name: "myapex2",
2267 key: "myapex.key",
2268 updatable: false,
2269 native_shared_libs: ["yourlib"],
2270 }
2271
2272 apex_key {
2273 name: "myapex.key",
2274 public_key: "testkey.avbpubkey",
2275 private_key: "testkey.pem",
2276 }
2277
2278 cc_library {
2279 name: "mylib",
2280 srcs: ["mylib.cpp"],
2281 shared_libs: ["libbar"],
2282 min_sdk_version: "29",
2283 apex_available: ["myapex"],
2284 }
2285
2286 cc_library {
2287 name: "libbar",
2288 stubs: { versions: ["29", "30"] },
2289 }
2290
2291 cc_library {
2292 name: "yourlib",
2293 srcs: ["mylib.cpp"],
2294 min_sdk_version: "29",
2295 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2296 }
2297 `, withFiles(android.MockFS{
2298 "packages/modules/common/build/custom_allowed_deps.txt": nil,
2299 "packages/modules/common/build/allowed_deps.txt": nil,
2300 }),
2301 android.FixtureModifyProductVariables(
2302 func(variables android.FixtureProductVariables) {
2303 variables.ExtraAllowedDepsTxt = proptools.StringPtr("packages/modules/common/build/custom_allowed_deps.txt")
2304 },
2305 ))
2306
2307 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2308 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2309 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
2310 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
2311 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
2312 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
2313
2314 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
2315 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2316 myapex.Output("depsinfo/flatlist.txt")), "\n")
2317 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2318 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2319 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2320 flatlist, "mylib:(minSdkVersion:29)")
2321 android.AssertStringListContains(t, "track platform-available lib",
2322 flatlist, "yourlib(minSdkVersion:29)")
2323}
2324
Jooyung Han4495f842023-04-25 16:39:59 +09002325func TestTrackAllowedDeps_SkipWithoutAllowedDepsTxt(t *testing.T) {
2326 ctx := testApex(t, `
2327 apex {
2328 name: "myapex",
2329 key: "myapex.key",
2330 updatable: true,
2331 min_sdk_version: "29",
2332 }
2333
2334 apex_key {
2335 name: "myapex.key",
2336 public_key: "testkey.avbpubkey",
2337 private_key: "testkey.pem",
2338 }
2339 `)
2340 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2341 if nil != depsinfo.MaybeRule("generateApexDepsInfoFilesRule").Output {
2342 t.Error("apex_depsinfo_singleton shouldn't run when allowed_deps.txt doesn't exist")
2343 }
2344}
2345
Jooyung Han03b51852020-02-26 22:45:42 +09002346func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002347 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002348 apex {
2349 name: "myapex",
2350 key: "myapex.key",
2351 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002352 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002353 }
2354
2355 apex_key {
2356 name: "myapex.key",
2357 public_key: "testkey.avbpubkey",
2358 private_key: "testkey.pem",
2359 }
2360
2361 cc_library {
2362 name: "libx",
2363 system_shared_libs: [],
2364 stl: "none",
2365 apex_available: [ "myapex" ],
2366 stubs: {
2367 versions: ["1", "2"],
2368 },
2369 }
2370
2371 cc_library {
2372 name: "libz",
2373 shared_libs: ["libx"],
2374 system_shared_libs: [],
2375 stl: "none",
2376 }
2377 `)
2378
2379 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002380 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002381 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2382 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2383 }
2384 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002385 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002386 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2387 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2388 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002389 expectLink("libz", "shared", "libx", "shared_current")
2390 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002391 expectNoLink("libz", "shared", "libz", "shared_1")
2392 expectNoLink("libz", "shared", "libz", "shared")
2393}
2394
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002395var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2396 func(variables android.FixtureProductVariables) {
2397 variables.SanitizeDevice = []string{"hwaddress"}
2398 },
2399)
2400
Jooyung Han75568392020-03-20 04:29:24 +09002401func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002402 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002403 apex {
2404 name: "myapex",
2405 key: "myapex.key",
2406 native_shared_libs: ["libx"],
2407 min_sdk_version: "29",
2408 }
2409
2410 apex_key {
2411 name: "myapex.key",
2412 public_key: "testkey.avbpubkey",
2413 private_key: "testkey.pem",
2414 }
2415
2416 cc_library {
2417 name: "libx",
2418 shared_libs: ["libbar"],
2419 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002420 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002421 }
2422
2423 cc_library {
2424 name: "libbar",
2425 stubs: {
2426 versions: ["29", "30"],
2427 },
2428 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002429 `,
2430 prepareForTestWithSantitizeHwaddress,
2431 )
Jooyung Han03b51852020-02-26 22:45:42 +09002432 expectLink := func(from, from_variant, to, to_variant string) {
2433 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2434 libFlags := ld.Args["libFlags"]
2435 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2436 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002437 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002438}
2439
Jooyung Han75568392020-03-20 04:29:24 +09002440func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002441 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002442 apex {
2443 name: "myapex",
2444 key: "myapex.key",
2445 native_shared_libs: ["libx"],
2446 min_sdk_version: "29",
2447 }
2448
2449 apex_key {
2450 name: "myapex.key",
2451 public_key: "testkey.avbpubkey",
2452 private_key: "testkey.pem",
2453 }
2454
2455 cc_library {
2456 name: "libx",
2457 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002458 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002459 }
Jooyung Han75568392020-03-20 04:29:24 +09002460 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002461
2462 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002463 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002464 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002465 // note that platform variant is not.
2466 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002467 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002468}
2469
Jooyung Han749dc692020-04-15 11:03:39 +09002470func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2471 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002472 apex {
2473 name: "myapex",
2474 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002475 native_shared_libs: ["mylib"],
2476 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002477 }
2478
2479 apex_key {
2480 name: "myapex.key",
2481 public_key: "testkey.avbpubkey",
2482 private_key: "testkey.pem",
2483 }
Jooyung Han749dc692020-04-15 11:03:39 +09002484
2485 cc_library {
2486 name: "mylib",
2487 srcs: ["mylib.cpp"],
2488 system_shared_libs: [],
2489 stl: "none",
2490 apex_available: [
2491 "myapex",
2492 ],
2493 min_sdk_version: "30",
2494 }
2495 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002496
2497 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2498 apex {
2499 name: "myapex",
2500 key: "myapex.key",
2501 native_shared_libs: ["libfoo.ffi"],
2502 min_sdk_version: "29",
2503 }
2504
2505 apex_key {
2506 name: "myapex.key",
2507 public_key: "testkey.avbpubkey",
2508 private_key: "testkey.pem",
2509 }
2510
2511 rust_ffi_shared {
2512 name: "libfoo.ffi",
2513 srcs: ["foo.rs"],
2514 crate_name: "foo",
2515 apex_available: [
2516 "myapex",
2517 ],
2518 min_sdk_version: "30",
2519 }
2520 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002521
2522 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2523 apex {
2524 name: "myapex",
2525 key: "myapex.key",
2526 java_libs: ["libfoo"],
2527 min_sdk_version: "29",
2528 }
2529
2530 apex_key {
2531 name: "myapex.key",
2532 public_key: "testkey.avbpubkey",
2533 private_key: "testkey.pem",
2534 }
2535
2536 java_import {
2537 name: "libfoo",
2538 jars: ["libfoo.jar"],
2539 apex_available: [
2540 "myapex",
2541 ],
2542 min_sdk_version: "30",
2543 }
2544 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002545
2546 // Skip check for modules compiling against core API surface
2547 testApex(t, `
2548 apex {
2549 name: "myapex",
2550 key: "myapex.key",
2551 java_libs: ["libfoo"],
2552 min_sdk_version: "29",
2553 }
2554
2555 apex_key {
2556 name: "myapex.key",
2557 public_key: "testkey.avbpubkey",
2558 private_key: "testkey.pem",
2559 }
2560
2561 java_library {
2562 name: "libfoo",
2563 srcs: ["Foo.java"],
2564 apex_available: [
2565 "myapex",
2566 ],
2567 // Compile against core API surface
2568 sdk_version: "core_current",
2569 min_sdk_version: "30",
2570 }
2571 `)
2572
Jooyung Han749dc692020-04-15 11:03:39 +09002573}
2574
2575func TestApexMinSdkVersion_Okay(t *testing.T) {
2576 testApex(t, `
2577 apex {
2578 name: "myapex",
2579 key: "myapex.key",
2580 native_shared_libs: ["libfoo"],
2581 java_libs: ["libbar"],
2582 min_sdk_version: "29",
2583 }
2584
2585 apex_key {
2586 name: "myapex.key",
2587 public_key: "testkey.avbpubkey",
2588 private_key: "testkey.pem",
2589 }
2590
2591 cc_library {
2592 name: "libfoo",
2593 srcs: ["mylib.cpp"],
2594 shared_libs: ["libfoo_dep"],
2595 apex_available: ["myapex"],
2596 min_sdk_version: "29",
2597 }
2598
2599 cc_library {
2600 name: "libfoo_dep",
2601 srcs: ["mylib.cpp"],
2602 apex_available: ["myapex"],
2603 min_sdk_version: "29",
2604 }
2605
2606 java_library {
2607 name: "libbar",
2608 sdk_version: "current",
2609 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002610 static_libs: [
2611 "libbar_dep",
2612 "libbar_import_dep",
2613 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002614 apex_available: ["myapex"],
2615 min_sdk_version: "29",
2616 }
2617
2618 java_library {
2619 name: "libbar_dep",
2620 sdk_version: "current",
2621 srcs: ["a.java"],
2622 apex_available: ["myapex"],
2623 min_sdk_version: "29",
2624 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002625
2626 java_import {
2627 name: "libbar_import_dep",
2628 jars: ["libbar.jar"],
2629 apex_available: ["myapex"],
2630 min_sdk_version: "29",
2631 }
Jooyung Han03b51852020-02-26 22:45:42 +09002632 `)
2633}
2634
Colin Cross8ca61c12022-10-06 21:00:14 -07002635func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2636 // Tests that an apex dependency with min_sdk_version higher than the
2637 // min_sdk_version of the apex is allowed as long as the dependency's
2638 // min_sdk_version is less than or equal to the api level that the
2639 // architecture was introduced in. In this case, arm64 didn't exist
2640 // until api level 21, so the arm64 code will never need to run on
2641 // an api level 20 device, even if other architectures of the apex
2642 // will.
2643 testApex(t, `
2644 apex {
2645 name: "myapex",
2646 key: "myapex.key",
2647 native_shared_libs: ["libfoo"],
2648 min_sdk_version: "20",
2649 }
2650
2651 apex_key {
2652 name: "myapex.key",
2653 public_key: "testkey.avbpubkey",
2654 private_key: "testkey.pem",
2655 }
2656
2657 cc_library {
2658 name: "libfoo",
2659 srcs: ["mylib.cpp"],
2660 apex_available: ["myapex"],
2661 min_sdk_version: "21",
2662 stl: "none",
2663 }
2664 `)
2665}
2666
Artur Satayev8cf899a2020-04-15 17:29:42 +01002667func TestJavaStableSdkVersion(t *testing.T) {
2668 testCases := []struct {
2669 name string
2670 expectedError string
2671 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002672 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002673 }{
2674 {
2675 name: "Non-updatable apex with non-stable dep",
2676 bp: `
2677 apex {
2678 name: "myapex",
2679 java_libs: ["myjar"],
2680 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002681 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002682 }
2683 apex_key {
2684 name: "myapex.key",
2685 public_key: "testkey.avbpubkey",
2686 private_key: "testkey.pem",
2687 }
2688 java_library {
2689 name: "myjar",
2690 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002691 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002692 apex_available: ["myapex"],
2693 }
2694 `,
2695 },
2696 {
2697 name: "Updatable apex with stable dep",
2698 bp: `
2699 apex {
2700 name: "myapex",
2701 java_libs: ["myjar"],
2702 key: "myapex.key",
2703 updatable: true,
2704 min_sdk_version: "29",
2705 }
2706 apex_key {
2707 name: "myapex.key",
2708 public_key: "testkey.avbpubkey",
2709 private_key: "testkey.pem",
2710 }
2711 java_library {
2712 name: "myjar",
2713 srcs: ["foo/bar/MyClass.java"],
2714 sdk_version: "current",
2715 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002716 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002717 }
2718 `,
2719 },
2720 {
2721 name: "Updatable apex with non-stable dep",
2722 expectedError: "cannot depend on \"myjar\"",
2723 bp: `
2724 apex {
2725 name: "myapex",
2726 java_libs: ["myjar"],
2727 key: "myapex.key",
2728 updatable: true,
2729 }
2730 apex_key {
2731 name: "myapex.key",
2732 public_key: "testkey.avbpubkey",
2733 private_key: "testkey.pem",
2734 }
2735 java_library {
2736 name: "myjar",
2737 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002738 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002739 apex_available: ["myapex"],
2740 }
2741 `,
2742 },
2743 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002744 name: "Updatable apex with non-stable legacy core platform dep",
2745 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2746 bp: `
2747 apex {
2748 name: "myapex",
2749 java_libs: ["myjar-uses-legacy"],
2750 key: "myapex.key",
2751 updatable: true,
2752 }
2753 apex_key {
2754 name: "myapex.key",
2755 public_key: "testkey.avbpubkey",
2756 private_key: "testkey.pem",
2757 }
2758 java_library {
2759 name: "myjar-uses-legacy",
2760 srcs: ["foo/bar/MyClass.java"],
2761 sdk_version: "core_platform",
2762 apex_available: ["myapex"],
2763 }
2764 `,
2765 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2766 },
2767 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002768 name: "Updatable apex with non-stable transitive dep",
2769 // This is not actually detecting that the transitive dependency is unstable, rather it is
2770 // detecting that the transitive dependency is building against a wider API surface than the
2771 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002772 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002773 bp: `
2774 apex {
2775 name: "myapex",
2776 java_libs: ["myjar"],
2777 key: "myapex.key",
2778 updatable: true,
2779 }
2780 apex_key {
2781 name: "myapex.key",
2782 public_key: "testkey.avbpubkey",
2783 private_key: "testkey.pem",
2784 }
2785 java_library {
2786 name: "myjar",
2787 srcs: ["foo/bar/MyClass.java"],
2788 sdk_version: "current",
2789 apex_available: ["myapex"],
2790 static_libs: ["transitive-jar"],
2791 }
2792 java_library {
2793 name: "transitive-jar",
2794 srcs: ["foo/bar/MyClass.java"],
2795 sdk_version: "core_platform",
2796 apex_available: ["myapex"],
2797 }
2798 `,
2799 },
2800 }
2801
2802 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002803 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2804 continue
2805 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002806 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002807 errorHandler := android.FixtureExpectsNoErrors
2808 if test.expectedError != "" {
2809 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002810 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002811 android.GroupFixturePreparers(
2812 java.PrepareForTestWithJavaDefaultModules,
2813 PrepareForTestWithApexBuildComponents,
2814 prepareForTestWithMyapex,
2815 android.OptionalFixturePreparer(test.preparer),
2816 ).
2817 ExtendWithErrorHandler(errorHandler).
2818 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002819 })
2820 }
2821}
2822
Jooyung Han749dc692020-04-15 11:03:39 +09002823func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2824 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2825 apex {
2826 name: "myapex",
2827 key: "myapex.key",
2828 native_shared_libs: ["mylib"],
2829 min_sdk_version: "29",
2830 }
2831
2832 apex_key {
2833 name: "myapex.key",
2834 public_key: "testkey.avbpubkey",
2835 private_key: "testkey.pem",
2836 }
2837
2838 cc_library {
2839 name: "mylib",
2840 srcs: ["mylib.cpp"],
2841 shared_libs: ["mylib2"],
2842 system_shared_libs: [],
2843 stl: "none",
2844 apex_available: [
2845 "myapex",
2846 ],
2847 min_sdk_version: "29",
2848 }
2849
2850 // indirect part of the apex
2851 cc_library {
2852 name: "mylib2",
2853 srcs: ["mylib.cpp"],
2854 system_shared_libs: [],
2855 stl: "none",
2856 apex_available: [
2857 "myapex",
2858 ],
2859 min_sdk_version: "30",
2860 }
2861 `)
2862}
2863
2864func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2865 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2866 apex {
2867 name: "myapex",
2868 key: "myapex.key",
2869 apps: ["AppFoo"],
2870 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002871 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002872 }
2873
2874 apex_key {
2875 name: "myapex.key",
2876 public_key: "testkey.avbpubkey",
2877 private_key: "testkey.pem",
2878 }
2879
2880 android_app {
2881 name: "AppFoo",
2882 srcs: ["foo/bar/MyClass.java"],
2883 sdk_version: "current",
2884 min_sdk_version: "29",
2885 system_modules: "none",
2886 stl: "none",
2887 static_libs: ["bar"],
2888 apex_available: [ "myapex" ],
2889 }
2890
2891 java_library {
2892 name: "bar",
2893 sdk_version: "current",
2894 srcs: ["a.java"],
2895 apex_available: [ "myapex" ],
2896 }
2897 `)
2898}
2899
2900func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002901 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002902 apex {
2903 name: "myapex",
2904 key: "myapex.key",
2905 native_shared_libs: ["mylib"],
2906 min_sdk_version: "29",
2907 }
2908
2909 apex_key {
2910 name: "myapex.key",
2911 public_key: "testkey.avbpubkey",
2912 private_key: "testkey.pem",
2913 }
2914
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002915 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002916 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2917 cc_library {
2918 name: "mylib",
2919 srcs: ["mylib.cpp"],
2920 shared_libs: ["mylib2"],
2921 system_shared_libs: [],
2922 stl: "none",
2923 apex_available: ["myapex", "otherapex"],
2924 min_sdk_version: "29",
2925 }
2926
2927 cc_library {
2928 name: "mylib2",
2929 srcs: ["mylib.cpp"],
2930 system_shared_libs: [],
2931 stl: "none",
2932 apex_available: ["otherapex"],
2933 stubs: { versions: ["29", "30"] },
2934 min_sdk_version: "30",
2935 }
2936
2937 apex {
2938 name: "otherapex",
2939 key: "myapex.key",
2940 native_shared_libs: ["mylib", "mylib2"],
2941 min_sdk_version: "30",
2942 }
2943 `)
2944 expectLink := func(from, from_variant, to, to_variant string) {
2945 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2946 libFlags := ld.Args["libFlags"]
2947 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2948 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002949 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002950 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002951}
2952
Jooyung Haned124c32021-01-26 11:43:46 +09002953func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002954 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2955 func(variables android.FixtureProductVariables) {
2956 variables.Platform_sdk_codename = proptools.StringPtr("S")
2957 variables.Platform_version_active_codenames = []string{"S"}
2958 },
2959 )
Jooyung Haned124c32021-01-26 11:43:46 +09002960 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2961 apex {
2962 name: "myapex",
2963 key: "myapex.key",
2964 native_shared_libs: ["libfoo"],
2965 min_sdk_version: "S",
2966 }
2967 apex_key {
2968 name: "myapex.key",
2969 public_key: "testkey.avbpubkey",
2970 private_key: "testkey.pem",
2971 }
2972 cc_library {
2973 name: "libfoo",
2974 shared_libs: ["libbar"],
2975 apex_available: ["myapex"],
2976 min_sdk_version: "29",
2977 }
2978 cc_library {
2979 name: "libbar",
2980 apex_available: ["myapex"],
2981 }
2982 `, withSAsActiveCodeNames)
2983}
2984
2985func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002986 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2987 variables.Platform_sdk_codename = proptools.StringPtr("S")
2988 variables.Platform_version_active_codenames = []string{"S", "T"}
2989 })
Colin Cross1c460562021-02-16 17:55:47 -08002990 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002991 apex {
2992 name: "myapex",
2993 key: "myapex.key",
2994 native_shared_libs: ["libfoo"],
2995 min_sdk_version: "S",
2996 }
2997 apex_key {
2998 name: "myapex.key",
2999 public_key: "testkey.avbpubkey",
3000 private_key: "testkey.pem",
3001 }
3002 cc_library {
3003 name: "libfoo",
3004 shared_libs: ["libbar"],
3005 apex_available: ["myapex"],
3006 min_sdk_version: "S",
3007 }
3008 cc_library {
3009 name: "libbar",
3010 stubs: {
3011 symbol_file: "libbar.map.txt",
3012 versions: ["30", "S", "T"],
3013 },
3014 }
3015 `, withSAsActiveCodeNames)
3016
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003017 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09003018 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
3019 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003020 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09003021}
3022
Jiyong Park7c2ee712018-12-07 00:42:25 +09003023func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003024 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09003025 apex {
3026 name: "myapex",
3027 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003028 native_shared_libs: ["mylib"],
Jooyung Han4ed512b2023-08-11 16:30:04 +09003029 binaries: ["mybin", "mybin.rust"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09003030 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003031 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003032 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09003033 }
3034
3035 apex_key {
3036 name: "myapex.key",
3037 public_key: "testkey.avbpubkey",
3038 private_key: "testkey.pem",
3039 }
3040
3041 prebuilt_etc {
3042 name: "myetc",
3043 src: "myprebuilt",
3044 sub_dir: "foo/bar",
3045 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003046
3047 cc_library {
3048 name: "mylib",
3049 srcs: ["mylib.cpp"],
3050 relative_install_path: "foo/bar",
3051 system_shared_libs: [],
3052 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003053 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003054 }
3055
3056 cc_binary {
3057 name: "mybin",
3058 srcs: ["mylib.cpp"],
3059 relative_install_path: "foo/bar",
3060 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003061 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003062 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003063 }
Jooyung Han4ed512b2023-08-11 16:30:04 +09003064
3065 rust_binary {
3066 name: "mybin.rust",
3067 srcs: ["foo.rs"],
3068 relative_install_path: "rust_subdir",
3069 apex_available: [ "myapex" ],
3070 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09003071 `)
3072
Jooyung Hana0503a52023-08-23 13:12:50 +09003073 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09003074 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09003075
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003076 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09003077 ensureContains(t, cmd, "/etc ")
3078 ensureContains(t, cmd, "/etc/foo ")
3079 ensureContains(t, cmd, "/etc/foo/bar ")
3080 ensureContains(t, cmd, "/lib64 ")
3081 ensureContains(t, cmd, "/lib64/foo ")
3082 ensureContains(t, cmd, "/lib64/foo/bar ")
3083 ensureContains(t, cmd, "/lib ")
3084 ensureContains(t, cmd, "/lib/foo ")
3085 ensureContains(t, cmd, "/lib/foo/bar ")
3086 ensureContains(t, cmd, "/bin ")
3087 ensureContains(t, cmd, "/bin/foo ")
3088 ensureContains(t, cmd, "/bin/foo/bar ")
Jooyung Han4ed512b2023-08-11 16:30:04 +09003089 ensureContains(t, cmd, "/bin/rust_subdir ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09003090}
Jiyong Parkda6eb592018-12-19 17:12:36 +09003091
Jooyung Han35155c42020-02-06 17:33:20 +09003092func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003093 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09003094 apex {
3095 name: "myapex",
3096 key: "myapex.key",
3097 multilib: {
3098 both: {
3099 native_shared_libs: ["mylib"],
3100 binaries: ["mybin"],
3101 },
3102 },
3103 compile_multilib: "both",
3104 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003105 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09003106 }
3107
3108 apex_key {
3109 name: "myapex.key",
3110 public_key: "testkey.avbpubkey",
3111 private_key: "testkey.pem",
3112 }
3113
3114 cc_library {
3115 name: "mylib",
3116 relative_install_path: "foo/bar",
3117 system_shared_libs: [],
3118 stl: "none",
3119 apex_available: [ "myapex" ],
3120 native_bridge_supported: true,
3121 }
3122
3123 cc_binary {
3124 name: "mybin",
3125 relative_install_path: "foo/bar",
3126 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09003127 stl: "none",
3128 apex_available: [ "myapex" ],
3129 native_bridge_supported: true,
3130 compile_multilib: "both", // default is "first" for binary
3131 multilib: {
3132 lib64: {
3133 suffix: "64",
3134 },
3135 },
3136 }
3137 `, withNativeBridgeEnabled)
Jooyung Hana0503a52023-08-23 13:12:50 +09003138 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han35155c42020-02-06 17:33:20 +09003139 "bin/foo/bar/mybin",
3140 "bin/foo/bar/mybin64",
3141 "bin/arm/foo/bar/mybin",
3142 "bin/arm64/foo/bar/mybin64",
3143 "lib/foo/bar/mylib.so",
3144 "lib/arm/foo/bar/mylib.so",
3145 "lib64/foo/bar/mylib.so",
3146 "lib64/arm64/foo/bar/mylib.so",
3147 })
3148}
3149
Jooyung Han85d61762020-06-24 23:50:26 +09003150func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08003151 result := android.GroupFixturePreparers(
3152 prepareForApexTest,
3153 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
3154 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09003155 apex {
3156 name: "myapex",
3157 key: "myapex.key",
3158 binaries: ["mybin"],
3159 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003160 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09003161 }
3162 apex_key {
3163 name: "myapex.key",
3164 public_key: "testkey.avbpubkey",
3165 private_key: "testkey.pem",
3166 }
3167 cc_binary {
3168 name: "mybin",
3169 vendor: true,
3170 shared_libs: ["libfoo"],
3171 }
3172 cc_library {
3173 name: "libfoo",
3174 proprietary: true,
3175 }
3176 `)
3177
Jooyung Hana0503a52023-08-23 13:12:50 +09003178 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09003179 "bin/mybin",
3180 "lib64/libfoo.so",
3181 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
3182 "lib64/libc++.so",
3183 })
3184
Jooyung Hana0503a52023-08-23 13:12:50 +09003185 apexBundle := result.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossc68db4b2021-11-11 18:59:15 -08003186 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09003187 name := apexBundle.BaseModuleName()
3188 prefix := "TARGET_"
3189 var builder strings.Builder
3190 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08003191 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01003192 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01003193 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003194
Jooyung Hana0503a52023-08-23 13:12:50 +09003195 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003196 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
3197 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09003198}
3199
Justin Yun13decfb2021-03-08 19:25:55 +09003200func TestProductVariant(t *testing.T) {
3201 ctx := testApex(t, `
3202 apex {
3203 name: "myapex",
3204 key: "myapex.key",
3205 updatable: false,
3206 product_specific: true,
3207 binaries: ["foo"],
3208 }
3209
3210 apex_key {
3211 name: "myapex.key",
3212 public_key: "testkey.avbpubkey",
3213 private_key: "testkey.pem",
3214 }
3215
3216 cc_binary {
3217 name: "foo",
3218 product_available: true,
3219 apex_available: ["myapex"],
3220 srcs: ["foo.cpp"],
3221 }
Justin Yunaf1fde42023-09-27 16:22:10 +09003222 `)
Justin Yun13decfb2021-03-08 19:25:55 +09003223
3224 cflags := strings.Fields(
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003225 ctx.ModuleForTests("foo", "android_product_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09003226 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
3227 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
3228 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
3229 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
3230}
3231
Jooyung Han8e5685d2020-09-21 11:02:57 +09003232func TestApex_withPrebuiltFirmware(t *testing.T) {
3233 testCases := []struct {
3234 name string
3235 additionalProp string
3236 }{
3237 {"system apex with prebuilt_firmware", ""},
3238 {"vendor apex with prebuilt_firmware", "vendor: true,"},
3239 }
3240 for _, tc := range testCases {
3241 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003242 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09003243 apex {
3244 name: "myapex",
3245 key: "myapex.key",
3246 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003247 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09003248 `+tc.additionalProp+`
3249 }
3250 apex_key {
3251 name: "myapex.key",
3252 public_key: "testkey.avbpubkey",
3253 private_key: "testkey.pem",
3254 }
3255 prebuilt_firmware {
3256 name: "myfirmware",
3257 src: "myfirmware.bin",
3258 filename_from_src: true,
3259 `+tc.additionalProp+`
3260 }
3261 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09003262 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han8e5685d2020-09-21 11:02:57 +09003263 "etc/firmware/myfirmware.bin",
3264 })
3265 })
3266 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003267}
3268
Jooyung Hanefb184e2020-06-25 17:14:25 +09003269func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003270 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003271 apex {
3272 name: "myapex",
3273 key: "myapex.key",
3274 vendor: true,
3275 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003276 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003277 }
3278
3279 apex_key {
3280 name: "myapex.key",
3281 public_key: "testkey.avbpubkey",
3282 private_key: "testkey.pem",
3283 }
3284
3285 cc_library {
3286 name: "mylib",
3287 vendor_available: true,
3288 }
3289 `)
3290
Jooyung Hana0503a52023-08-23 13:12:50 +09003291 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003292 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003293 name := apexBundle.BaseModuleName()
3294 prefix := "TARGET_"
3295 var builder strings.Builder
3296 data.Custom(&builder, name, prefix, "", data)
3297 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09003298 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++.vendor.myapex:64 mylib.vendor.myapex:64 libc.vendor libm.vendor libdl.vendor\n")
Jooyung Hanefb184e2020-06-25 17:14:25 +09003299}
3300
Jooyung Han2ed99d02020-06-24 23:26:26 +09003301func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003302 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003303 apex {
3304 name: "myapex",
3305 key: "myapex.key",
3306 vintf_fragments: ["fragment.xml"],
3307 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003308 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003309 }
3310 apex_key {
3311 name: "myapex.key",
3312 public_key: "testkey.avbpubkey",
3313 private_key: "testkey.pem",
3314 }
3315 cc_binary {
3316 name: "mybin",
3317 }
3318 `)
3319
Jooyung Hana0503a52023-08-23 13:12:50 +09003320 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003321 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003322 name := apexBundle.BaseModuleName()
3323 prefix := "TARGET_"
3324 var builder strings.Builder
3325 data.Custom(&builder, name, prefix, "", data)
3326 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003327 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003328 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003329}
3330
Jiyong Park16e91a02018-12-20 18:18:08 +09003331func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003332 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003333 apex {
3334 name: "myapex",
3335 key: "myapex.key",
3336 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003337 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003338 }
3339
3340 apex_key {
3341 name: "myapex.key",
3342 public_key: "testkey.avbpubkey",
3343 private_key: "testkey.pem",
3344 }
3345
3346 cc_library {
3347 name: "mylib",
3348 srcs: ["mylib.cpp"],
3349 system_shared_libs: [],
3350 stl: "none",
3351 stubs: {
3352 versions: ["1", "2", "3"],
3353 },
Spandan Das20fce2d2023-04-12 17:21:39 +00003354 apex_available: ["myapex"],
Jiyong Park16e91a02018-12-20 18:18:08 +09003355 }
3356
3357 cc_binary {
3358 name: "not_in_apex",
3359 srcs: ["mylib.cpp"],
3360 static_libs: ["mylib"],
3361 static_executable: true,
3362 system_shared_libs: [],
3363 stl: "none",
3364 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003365 `)
3366
Colin Cross7113d202019-11-20 16:39:12 -08003367 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003368
3369 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003370 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003371}
Jiyong Park9335a262018-12-24 11:31:58 +09003372
3373func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003374 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003375 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003376 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003377 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003378 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003379 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003380 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003381 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003382 }
3383
3384 cc_library {
3385 name: "mylib",
3386 srcs: ["mylib.cpp"],
3387 system_shared_libs: [],
3388 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003389 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003390 }
3391
3392 apex_key {
3393 name: "myapex.key",
3394 public_key: "testkey.avbpubkey",
3395 private_key: "testkey.pem",
3396 }
3397
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003398 android_app_certificate {
3399 name: "myapex.certificate",
3400 certificate: "testkey",
3401 }
3402
3403 android_app_certificate {
3404 name: "myapex.certificate.override",
3405 certificate: "testkey.override",
3406 }
3407
Jiyong Park9335a262018-12-24 11:31:58 +09003408 `)
3409
3410 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003411 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003412
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003413 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3414 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003415 "vendor/foo/devkeys/testkey.avbpubkey")
3416 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003417 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3418 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003419 "vendor/foo/devkeys/testkey.pem")
3420 }
3421
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003422 // check the APK certs. It should be overridden to myapex.certificate.override
Jooyung Hana0503a52023-08-23 13:12:50 +09003423 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003424 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003425 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003426 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003427 }
3428}
Jiyong Park58e364a2019-01-19 19:24:06 +09003429
Jooyung Hanf121a652019-12-17 14:30:11 +09003430func TestCertificate(t *testing.T) {
3431 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003432 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003433 apex {
3434 name: "myapex",
3435 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003436 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003437 }
3438 apex_key {
3439 name: "myapex.key",
3440 public_key: "testkey.avbpubkey",
3441 private_key: "testkey.pem",
3442 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003443 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003444 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3445 if actual := rule.Args["certificates"]; actual != expected {
3446 t.Errorf("certificates should be %q, not %q", expected, actual)
3447 }
3448 })
3449 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003450 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003451 apex {
3452 name: "myapex_keytest",
3453 key: "myapex.key",
3454 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003455 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003456 }
3457 apex_key {
3458 name: "myapex.key",
3459 public_key: "testkey.avbpubkey",
3460 private_key: "testkey.pem",
3461 }
3462 android_app_certificate {
3463 name: "myapex.certificate.override",
3464 certificate: "testkey.override",
3465 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003466 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003467 expected := "testkey.override.x509.pem testkey.override.pk8"
3468 if actual := rule.Args["certificates"]; actual != expected {
3469 t.Errorf("certificates should be %q, not %q", expected, actual)
3470 }
3471 })
3472 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003473 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003474 apex {
3475 name: "myapex",
3476 key: "myapex.key",
3477 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003478 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003479 }
3480 apex_key {
3481 name: "myapex.key",
3482 public_key: "testkey.avbpubkey",
3483 private_key: "testkey.pem",
3484 }
3485 android_app_certificate {
3486 name: "myapex.certificate",
3487 certificate: "testkey",
3488 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003489 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003490 expected := "testkey.x509.pem testkey.pk8"
3491 if actual := rule.Args["certificates"]; actual != expected {
3492 t.Errorf("certificates should be %q, not %q", expected, actual)
3493 }
3494 })
3495 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003496 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003497 apex {
3498 name: "myapex_keytest",
3499 key: "myapex.key",
3500 file_contexts: ":myapex-file_contexts",
3501 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003502 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003503 }
3504 apex_key {
3505 name: "myapex.key",
3506 public_key: "testkey.avbpubkey",
3507 private_key: "testkey.pem",
3508 }
3509 android_app_certificate {
3510 name: "myapex.certificate.override",
3511 certificate: "testkey.override",
3512 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003513 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003514 expected := "testkey.override.x509.pem testkey.override.pk8"
3515 if actual := rule.Args["certificates"]; actual != expected {
3516 t.Errorf("certificates should be %q, not %q", expected, actual)
3517 }
3518 })
3519 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003520 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003521 apex {
3522 name: "myapex",
3523 key: "myapex.key",
3524 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003525 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003526 }
3527 apex_key {
3528 name: "myapex.key",
3529 public_key: "testkey.avbpubkey",
3530 private_key: "testkey.pem",
3531 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003532 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003533 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3534 if actual := rule.Args["certificates"]; actual != expected {
3535 t.Errorf("certificates should be %q, not %q", expected, actual)
3536 }
3537 })
3538 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003539 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003540 apex {
3541 name: "myapex_keytest",
3542 key: "myapex.key",
3543 file_contexts: ":myapex-file_contexts",
3544 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003545 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003546 }
3547 apex_key {
3548 name: "myapex.key",
3549 public_key: "testkey.avbpubkey",
3550 private_key: "testkey.pem",
3551 }
3552 android_app_certificate {
3553 name: "myapex.certificate.override",
3554 certificate: "testkey.override",
3555 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003556 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003557 expected := "testkey.override.x509.pem testkey.override.pk8"
3558 if actual := rule.Args["certificates"]; actual != expected {
3559 t.Errorf("certificates should be %q, not %q", expected, actual)
3560 }
3561 })
3562}
3563
Jiyong Park58e364a2019-01-19 19:24:06 +09003564func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003565 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003566 apex {
3567 name: "myapex",
3568 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003569 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003570 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003571 }
3572
3573 apex {
3574 name: "otherapex",
3575 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003576 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003577 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003578 }
3579
3580 apex_key {
3581 name: "myapex.key",
3582 public_key: "testkey.avbpubkey",
3583 private_key: "testkey.pem",
3584 }
3585
3586 cc_library {
3587 name: "mylib",
3588 srcs: ["mylib.cpp"],
3589 system_shared_libs: [],
3590 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003591 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003592 "myapex",
3593 "otherapex",
3594 ],
Jooyung Han24282772020-03-21 23:20:55 +09003595 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003596 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003597 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003598 cc_library {
3599 name: "mylib2",
3600 srcs: ["mylib.cpp"],
3601 system_shared_libs: [],
3602 stl: "none",
3603 apex_available: [
3604 "myapex",
3605 "otherapex",
3606 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003607 static_libs: ["mylib3"],
3608 recovery_available: true,
3609 min_sdk_version: "29",
3610 }
3611 cc_library {
3612 name: "mylib3",
3613 srcs: ["mylib.cpp"],
3614 system_shared_libs: [],
3615 stl: "none",
3616 apex_available: [
3617 "myapex",
3618 "otherapex",
3619 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003620 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003621 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003622 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003623 `)
3624
Jooyung Hanc87a0592020-03-02 17:44:33 +09003625 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003626 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003627 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003628
Vinh Tranf9754732023-01-19 22:41:46 -05003629 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003630 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003631 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003632
Vinh Tranf9754732023-01-19 22:41:46 -05003633 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003634 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003635 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003636
Colin Crossaede88c2020-08-11 12:17:01 -07003637 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3638 // each variant defines additional macros to distinguish which apex variant it is built for
3639
3640 // non-APEX variant does not have __ANDROID_APEX__ defined
3641 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3642 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3643
Vinh Tranf9754732023-01-19 22:41:46 -05003644 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003645 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3646 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003647
Jooyung Hanc87a0592020-03-02 17:44:33 +09003648 // non-APEX variant does not have __ANDROID_APEX__ defined
3649 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3650 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3651
Vinh Tranf9754732023-01-19 22:41:46 -05003652 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003653 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003654 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003655}
Jiyong Park7e636d02019-01-28 16:16:54 +09003656
3657func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003658 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003659 apex {
3660 name: "myapex",
3661 key: "myapex.key",
3662 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003663 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003664 }
3665
3666 apex_key {
3667 name: "myapex.key",
3668 public_key: "testkey.avbpubkey",
3669 private_key: "testkey.pem",
3670 }
3671
3672 cc_library_headers {
3673 name: "mylib_headers",
3674 export_include_dirs: ["my_include"],
3675 system_shared_libs: [],
3676 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003677 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003678 }
3679
3680 cc_library {
3681 name: "mylib",
3682 srcs: ["mylib.cpp"],
3683 system_shared_libs: [],
3684 stl: "none",
3685 header_libs: ["mylib_headers"],
3686 export_header_lib_headers: ["mylib_headers"],
3687 stubs: {
3688 versions: ["1", "2", "3"],
3689 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003690 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003691 }
3692
3693 cc_library {
3694 name: "otherlib",
3695 srcs: ["mylib.cpp"],
3696 system_shared_libs: [],
3697 stl: "none",
3698 shared_libs: ["mylib"],
3699 }
3700 `)
3701
Colin Cross7113d202019-11-20 16:39:12 -08003702 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003703
3704 // Ensure that the include path of the header lib is exported to 'otherlib'
3705 ensureContains(t, cFlags, "-Imy_include")
3706}
Alex Light9670d332019-01-29 18:07:33 -08003707
Jiyong Park7cd10e32020-01-14 09:22:18 +09003708type fileInApex struct {
3709 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003710 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003711 isLink bool
3712}
3713
Jooyung Han1724d582022-12-21 10:17:44 +09003714func (f fileInApex) String() string {
3715 return f.src + ":" + f.path
3716}
3717
3718func (f fileInApex) match(expectation string) bool {
3719 parts := strings.Split(expectation, ":")
3720 if len(parts) == 1 {
3721 match, _ := path.Match(parts[0], f.path)
3722 return match
3723 }
3724 if len(parts) == 2 {
3725 matchSrc, _ := path.Match(parts[0], f.src)
3726 matchDst, _ := path.Match(parts[1], f.path)
3727 return matchSrc && matchDst
3728 }
3729 panic("invalid expected file specification: " + expectation)
3730}
3731
Jooyung Hana57af4a2020-01-23 05:36:59 +00003732func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003733 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003734 module := ctx.ModuleForTests(moduleName, variant)
3735 apexRule := module.MaybeRule("apexRule")
3736 apexDir := "/image.apex/"
Jooyung Han31c470b2019-10-18 16:26:59 +09003737 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003738 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003739 for _, cmd := range strings.Split(copyCmds, "&&") {
3740 cmd = strings.TrimSpace(cmd)
3741 if cmd == "" {
3742 continue
3743 }
3744 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003745 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003746 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003747 switch terms[0] {
3748 case "mkdir":
3749 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003750 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003751 t.Fatal("copyCmds contains invalid cp command", cmd)
3752 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003753 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003754 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003755 isLink = false
3756 case "ln":
3757 if len(terms) != 3 && len(terms) != 4 {
3758 // ln LINK TARGET or ln -s LINK TARGET
3759 t.Fatal("copyCmds contains invalid ln command", cmd)
3760 }
3761 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003762 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003763 isLink = true
3764 default:
3765 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3766 }
3767 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003768 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003769 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003770 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003771 }
Jooyung Han1724d582022-12-21 10:17:44 +09003772 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003773 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003774 }
3775 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003776 return ret
3777}
3778
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003779func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003780 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003781 var failed bool
3782 var surplus []string
3783 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003784 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003785 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003786 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003787 if file.match(expected) {
3788 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003789 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003790 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003791 }
3792 }
Jooyung Han1724d582022-12-21 10:17:44 +09003793 if !matchFound {
3794 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003795 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003796 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003797
Jooyung Han31c470b2019-10-18 16:26:59 +09003798 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003799 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003800 t.Log("surplus files", surplus)
3801 failed = true
3802 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003803
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003804 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003805 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003806 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003807 if !filesMatched[expected] {
3808 missing = append(missing, expected)
3809 }
3810 }
3811 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003812 t.Log("missing files", missing)
3813 failed = true
3814 }
3815 if failed {
3816 t.Fail()
3817 }
3818}
3819
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003820func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3821 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3822}
3823
3824func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
Spandan Das52c01a12024-09-20 01:09:48 +00003825 deapexer := ctx.ModuleForTests(moduleName, variant).Description("deapex")
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003826 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3827 if deapexer.Output != nil {
3828 outputs = append(outputs, deapexer.Output.String())
3829 }
3830 for _, output := range deapexer.ImplicitOutputs {
3831 outputs = append(outputs, output.String())
3832 }
3833 actualFiles := make([]fileInApex, 0, len(outputs))
3834 for _, output := range outputs {
3835 dir := "/deapexer/"
3836 pos := strings.LastIndex(output, dir)
3837 if pos == -1 {
3838 t.Fatal("Unknown deapexer output ", output)
3839 }
3840 path := output[pos+len(dir):]
3841 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3842 }
3843 assertFileListEquals(t, files, actualFiles)
3844}
3845
Jooyung Han39edb6c2019-11-06 16:53:07 +09003846func vndkLibrariesTxtFiles(vers ...string) (result string) {
3847 for _, v := range vers {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +09003848 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Justin Yund5784122023-10-25 13:25:32 +09003849 result += `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003850 prebuilt_etc {
3851 name: "` + txt + `.libraries.` + v + `.txt",
3852 src: "dummy.txt",
3853 }
3854 `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003855 }
3856 }
3857 return
3858}
3859
Jooyung Han344d5432019-08-23 11:17:39 +09003860func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003861 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003862 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003863 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003864 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003865 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003866 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003867 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003868 }
3869
3870 apex_key {
3871 name: "myapex.key",
3872 public_key: "testkey.avbpubkey",
3873 private_key: "testkey.pem",
3874 }
3875
Jooyung Han31c470b2019-10-18 16:26:59 +09003876 vndk_prebuilt_shared {
3877 name: "libvndk27",
3878 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003879 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003880 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003881 vndk: {
3882 enabled: true,
3883 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003884 target_arch: "arm64",
3885 arch: {
3886 arm: {
3887 srcs: ["libvndk27_arm.so"],
3888 },
3889 arm64: {
3890 srcs: ["libvndk27_arm64.so"],
3891 },
3892 },
Colin Cross2807f002021-03-02 10:15:29 -08003893 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003894 }
3895
3896 vndk_prebuilt_shared {
3897 name: "libvndk27",
3898 version: "27",
3899 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003900 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003901 vndk: {
3902 enabled: true,
3903 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003904 target_arch: "x86_64",
3905 arch: {
3906 x86: {
3907 srcs: ["libvndk27_x86.so"],
3908 },
3909 x86_64: {
3910 srcs: ["libvndk27_x86_64.so"],
3911 },
3912 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003913 }
3914 `+vndkLibrariesTxtFiles("27"),
3915 withFiles(map[string][]byte{
3916 "libvndk27_arm.so": nil,
3917 "libvndk27_arm64.so": nil,
3918 "libvndk27_x86.so": nil,
3919 "libvndk27_x86_64.so": nil,
3920 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003921
Jooyung Hana0503a52023-08-23 13:12:50 +09003922 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003923 "lib/libvndk27_arm.so",
3924 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003925 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003926 })
Jooyung Han344d5432019-08-23 11:17:39 +09003927}
3928
Jooyung Han90eee022019-10-01 20:02:42 +09003929func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003930 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003931 apex_vndk {
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003932 name: "com.android.vndk.v29",
Jooyung Han90eee022019-10-01 20:02:42 +09003933 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003934 file_contexts: ":myapex-file_contexts",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003935 vndk_version: "29",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003936 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003937 }
3938 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003939 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003940 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003941 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003942 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003943 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003944 }
3945 apex_key {
3946 name: "myapex.key",
3947 public_key: "testkey.avbpubkey",
3948 private_key: "testkey.pem",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003949 }`+vndkLibrariesTxtFiles("28", "29"))
Jooyung Han90eee022019-10-01 20:02:42 +09003950
3951 assertApexName := func(expected, moduleName string) {
Jooyung Hana0503a52023-08-23 13:12:50 +09003952 module := ctx.ModuleForTests(moduleName, "android_common")
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003953 apexManifestRule := module.Rule("apexManifestRule")
3954 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003955 }
3956
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003957 assertApexName("com.android.vndk.v29", "com.android.vndk.v29")
Colin Cross2807f002021-03-02 10:15:29 -08003958 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003959}
3960
Jooyung Han344d5432019-08-23 11:17:39 +09003961func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003962 testApexError(t, `module "com.android.vndk.v30" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003963 apex_vndk {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003964 name: "com.android.vndk.v30",
3965 key: "com.android.vndk.v30.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003966 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003967 native_bridge_supported: true,
3968 }
3969
3970 apex_key {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003971 name: "com.android.vndk.v30.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003972 public_key: "testkey.avbpubkey",
3973 private_key: "testkey.pem",
3974 }
3975
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003976 vndk_prebuilt_shared {
Jooyung Han344d5432019-08-23 11:17:39 +09003977 name: "libvndk",
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003978 version: "30",
3979 target_arch: "arm",
Jooyung Han344d5432019-08-23 11:17:39 +09003980 srcs: ["mylib.cpp"],
3981 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003982 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003983 native_bridge_supported: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003984 vndk: {
3985 enabled: true,
3986 },
Jooyung Han344d5432019-08-23 11:17:39 +09003987 }
3988 `)
3989}
3990
Jooyung Han31c470b2019-10-18 16:26:59 +09003991func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003992 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003993 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003994 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003995 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003996 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003997 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003998 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003999 }
4000
4001 apex_key {
4002 name: "myapex.key",
4003 public_key: "testkey.avbpubkey",
4004 private_key: "testkey.pem",
4005 }
4006
4007 vndk_prebuilt_shared {
4008 name: "libvndk27",
4009 version: "27",
4010 target_arch: "arm",
4011 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004012 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004013 vndk: {
4014 enabled: true,
4015 },
4016 arch: {
4017 arm: {
4018 srcs: ["libvndk27.so"],
4019 }
4020 },
4021 }
4022
4023 vndk_prebuilt_shared {
4024 name: "libvndk27",
4025 version: "27",
4026 target_arch: "arm",
4027 binder32bit: true,
4028 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004029 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004030 vndk: {
4031 enabled: true,
4032 },
4033 arch: {
4034 arm: {
4035 srcs: ["libvndk27binder32.so"],
4036 }
4037 },
Colin Cross2807f002021-03-02 10:15:29 -08004038 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09004039 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09004040 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09004041 withFiles(map[string][]byte{
4042 "libvndk27.so": nil,
4043 "libvndk27binder32.so": nil,
4044 }),
4045 withBinder32bit,
Cole Faust69788792024-10-10 11:00:36 -07004046 android.FixtureModifyConfig(func(config android.Config) {
4047 target := android.Target{
4048 Os: android.Android,
4049 Arch: android.Arch{
4050 ArchType: android.Arm,
4051 ArchVariant: "armv7-a-neon",
4052 Abi: []string{"armeabi-v7a"},
4053 },
4054 NativeBridge: android.NativeBridgeDisabled,
4055 NativeBridgeHostArchName: "",
4056 NativeBridgeRelativePath: "",
4057 }
4058 config.Targets[android.Android] = []android.Target{target}
4059 config.AndroidFirstDeviceTarget = target
Jooyung Han31c470b2019-10-18 16:26:59 +09004060 }),
4061 )
4062
Jooyung Hana0503a52023-08-23 13:12:50 +09004063 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09004064 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004065 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09004066 })
4067}
4068
Jooyung Hane1633032019-08-01 17:41:43 +09004069func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004070 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09004071 apex {
4072 name: "myapex_nodep",
4073 key: "myapex.key",
4074 native_shared_libs: ["lib_nodep"],
4075 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004076 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004077 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004078 }
4079
4080 apex {
4081 name: "myapex_dep",
4082 key: "myapex.key",
4083 native_shared_libs: ["lib_dep"],
4084 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004085 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004086 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004087 }
4088
4089 apex {
4090 name: "myapex_provider",
4091 key: "myapex.key",
4092 native_shared_libs: ["libfoo"],
4093 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004094 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004095 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004096 }
4097
4098 apex {
4099 name: "myapex_selfcontained",
4100 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00004101 native_shared_libs: ["lib_dep_on_bar", "libbar"],
Jooyung Hane1633032019-08-01 17:41:43 +09004102 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004103 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004104 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004105 }
4106
4107 apex_key {
4108 name: "myapex.key",
4109 public_key: "testkey.avbpubkey",
4110 private_key: "testkey.pem",
4111 }
4112
4113 cc_library {
4114 name: "lib_nodep",
4115 srcs: ["mylib.cpp"],
4116 system_shared_libs: [],
4117 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004118 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004119 }
4120
4121 cc_library {
4122 name: "lib_dep",
4123 srcs: ["mylib.cpp"],
4124 shared_libs: ["libfoo"],
4125 system_shared_libs: [],
4126 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004127 apex_available: [
4128 "myapex_dep",
4129 "myapex_provider",
4130 "myapex_selfcontained",
4131 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004132 }
4133
4134 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00004135 name: "lib_dep_on_bar",
4136 srcs: ["mylib.cpp"],
4137 shared_libs: ["libbar"],
4138 system_shared_libs: [],
4139 stl: "none",
4140 apex_available: [
4141 "myapex_selfcontained",
4142 ],
4143 }
4144
4145
4146 cc_library {
Jooyung Hane1633032019-08-01 17:41:43 +09004147 name: "libfoo",
4148 srcs: ["mytest.cpp"],
4149 stubs: {
4150 versions: ["1"],
4151 },
4152 system_shared_libs: [],
4153 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004154 apex_available: [
4155 "myapex_provider",
Spandan Das20fce2d2023-04-12 17:21:39 +00004156 ],
4157 }
4158
4159 cc_library {
4160 name: "libbar",
4161 srcs: ["mytest.cpp"],
4162 stubs: {
4163 versions: ["1"],
4164 },
4165 system_shared_libs: [],
4166 stl: "none",
4167 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004168 "myapex_selfcontained",
4169 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004170 }
Spandan Das20fce2d2023-04-12 17:21:39 +00004171
Jooyung Hane1633032019-08-01 17:41:43 +09004172 `)
4173
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004174 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004175 var provideNativeLibs, requireNativeLibs []string
4176
Jooyung Hana0503a52023-08-23 13:12:50 +09004177 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004178 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4179 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004180 ensureListEmpty(t, provideNativeLibs)
4181 ensureListEmpty(t, requireNativeLibs)
4182
Jooyung Hana0503a52023-08-23 13:12:50 +09004183 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004184 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4185 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004186 ensureListEmpty(t, provideNativeLibs)
4187 ensureListContains(t, requireNativeLibs, "libfoo.so")
4188
Jooyung Hana0503a52023-08-23 13:12:50 +09004189 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004190 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4191 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004192 ensureListContains(t, provideNativeLibs, "libfoo.so")
4193 ensureListEmpty(t, requireNativeLibs)
4194
Jooyung Hana0503a52023-08-23 13:12:50 +09004195 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004196 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4197 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Spandan Das20fce2d2023-04-12 17:21:39 +00004198 ensureListContains(t, provideNativeLibs, "libbar.so")
Jooyung Hane1633032019-08-01 17:41:43 +09004199 ensureListEmpty(t, requireNativeLibs)
4200}
4201
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004202func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4203 ctx := testApex(t, `
4204 apex {
4205 name: "myapex",
4206 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004207 native_shared_libs: ["mylib"],
4208 updatable: false,
4209 }
4210
4211 apex_key {
4212 name: "myapex.key",
4213 public_key: "testkey.avbpubkey",
4214 private_key: "testkey.pem",
4215 }
4216
4217 cc_library {
4218 name: "mylib",
4219 srcs: ["mylib.cpp"],
4220 system_shared_libs: [],
4221 stl: "none",
4222 apex_available: [
4223 "//apex_available:platform",
4224 "myapex",
4225 ],
4226 }
4227 `, android.FixtureMergeEnv(map[string]string{
4228 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4229 }))
4230
Jooyung Hana0503a52023-08-23 13:12:50 +09004231 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004232 apexManifestRule := module.Rule("apexManifestRule")
4233 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4234}
4235
Vinh Tran8f5310f2022-10-07 18:16:47 -04004236func TestCompileMultilibProp(t *testing.T) {
4237 testCases := []struct {
4238 compileMultiLibProp string
4239 containedLibs []string
4240 notContainedLibs []string
4241 }{
4242 {
4243 containedLibs: []string{
4244 "image.apex/lib64/mylib.so",
4245 "image.apex/lib/mylib.so",
4246 },
4247 compileMultiLibProp: `compile_multilib: "both",`,
4248 },
4249 {
4250 containedLibs: []string{"image.apex/lib64/mylib.so"},
4251 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4252 compileMultiLibProp: `compile_multilib: "first",`,
4253 },
4254 {
4255 containedLibs: []string{"image.apex/lib64/mylib.so"},
4256 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4257 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4258 },
4259 {
4260 containedLibs: []string{"image.apex/lib64/mylib.so"},
4261 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4262 compileMultiLibProp: `compile_multilib: "64",`,
4263 },
4264 {
4265 containedLibs: []string{"image.apex/lib/mylib.so"},
4266 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4267 compileMultiLibProp: `compile_multilib: "32",`,
4268 },
4269 }
4270 for _, testCase := range testCases {
4271 ctx := testApex(t, fmt.Sprintf(`
4272 apex {
4273 name: "myapex",
4274 key: "myapex.key",
4275 %s
4276 native_shared_libs: ["mylib"],
4277 updatable: false,
4278 }
4279 apex_key {
4280 name: "myapex.key",
4281 public_key: "testkey.avbpubkey",
4282 private_key: "testkey.pem",
4283 }
4284 cc_library {
4285 name: "mylib",
4286 srcs: ["mylib.cpp"],
4287 apex_available: [
4288 "//apex_available:platform",
4289 "myapex",
4290 ],
4291 }
4292 `, testCase.compileMultiLibProp),
4293 )
Jooyung Hana0503a52023-08-23 13:12:50 +09004294 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Vinh Tran8f5310f2022-10-07 18:16:47 -04004295 apexRule := module.Rule("apexRule")
4296 copyCmds := apexRule.Args["copy_commands"]
4297 for _, containedLib := range testCase.containedLibs {
4298 ensureContains(t, copyCmds, containedLib)
4299 }
4300 for _, notContainedLib := range testCase.notContainedLibs {
4301 ensureNotContains(t, copyCmds, notContainedLib)
4302 }
4303 }
4304}
4305
Alex Light0851b882019-02-07 13:20:53 -08004306func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004307 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004308 apex {
4309 name: "myapex",
4310 key: "myapex.key",
4311 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004312 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004313 }
4314
4315 apex_key {
4316 name: "myapex.key",
4317 public_key: "testkey.avbpubkey",
4318 private_key: "testkey.pem",
4319 }
4320
4321 cc_library {
4322 name: "mylib_common",
4323 srcs: ["mylib.cpp"],
4324 system_shared_libs: [],
4325 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004326 apex_available: [
4327 "//apex_available:platform",
4328 "myapex",
4329 ],
Alex Light0851b882019-02-07 13:20:53 -08004330 }
4331 `)
4332
Jooyung Hana0503a52023-08-23 13:12:50 +09004333 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004334 apexRule := module.Rule("apexRule")
4335 copyCmds := apexRule.Args["copy_commands"]
4336
4337 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4338 t.Log("Apex was a test apex!")
4339 t.Fail()
4340 }
4341 // Ensure that main rule creates an output
4342 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4343
4344 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004345 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004346
4347 // Ensure that both direct and indirect deps are copied into apex
4348 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4349
Colin Cross7113d202019-11-20 16:39:12 -08004350 // Ensure that the platform variant ends with _shared
4351 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004352
Colin Cross56a83212020-09-15 18:30:11 -07004353 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004354 t.Log("Found mylib_common not in any apex!")
4355 t.Fail()
4356 }
4357}
4358
4359func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004360 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004361 apex_test {
4362 name: "myapex",
4363 key: "myapex.key",
4364 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004365 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004366 }
4367
4368 apex_key {
4369 name: "myapex.key",
4370 public_key: "testkey.avbpubkey",
4371 private_key: "testkey.pem",
4372 }
4373
4374 cc_library {
4375 name: "mylib_common_test",
4376 srcs: ["mylib.cpp"],
4377 system_shared_libs: [],
4378 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004379 // TODO: remove //apex_available:platform
4380 apex_available: [
4381 "//apex_available:platform",
4382 "myapex",
4383 ],
Alex Light0851b882019-02-07 13:20:53 -08004384 }
4385 `)
4386
Jooyung Hana0503a52023-08-23 13:12:50 +09004387 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004388 apexRule := module.Rule("apexRule")
4389 copyCmds := apexRule.Args["copy_commands"]
4390
4391 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4392 t.Log("Apex was not a test apex!")
4393 t.Fail()
4394 }
4395 // Ensure that main rule creates an output
4396 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4397
4398 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004399 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004400
4401 // Ensure that both direct and indirect deps are copied into apex
4402 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4403
Colin Cross7113d202019-11-20 16:39:12 -08004404 // Ensure that the platform variant ends with _shared
4405 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004406}
4407
Jooyung Han85707de2023-12-01 14:21:13 +09004408func TestLibzVendorIsntStable(t *testing.T) {
4409 ctx := testApex(t, `
4410 apex {
4411 name: "myapex",
4412 key: "myapex.key",
4413 updatable: false,
4414 binaries: ["mybin"],
4415 }
4416 apex {
4417 name: "myvendorapex",
4418 key: "myapex.key",
4419 file_contexts: "myvendorapex_file_contexts",
4420 vendor: true,
4421 updatable: false,
4422 binaries: ["mybin"],
4423 }
4424 apex_key {
4425 name: "myapex.key",
4426 public_key: "testkey.avbpubkey",
4427 private_key: "testkey.pem",
4428 }
4429 cc_binary {
4430 name: "mybin",
4431 vendor_available: true,
4432 system_shared_libs: [],
4433 stl: "none",
4434 shared_libs: ["libz"],
4435 apex_available: ["//apex_available:anyapex"],
4436 }
4437 cc_library {
4438 name: "libz",
4439 vendor_available: true,
4440 system_shared_libs: [],
4441 stl: "none",
4442 stubs: {
4443 versions: ["28", "30"],
4444 },
4445 target: {
4446 vendor: {
4447 no_stubs: true,
4448 },
4449 },
4450 }
4451 `, withFiles(map[string][]byte{
4452 "myvendorapex_file_contexts": nil,
4453 }))
4454
4455 // libz provides stubs for core variant.
4456 {
4457 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
4458 "bin/mybin",
4459 })
4460 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
4461 android.AssertStringEquals(t, "should require libz", apexManifestRule.Args["requireNativeLibs"], "libz.so")
4462 }
4463 // libz doesn't provide stubs for vendor variant.
4464 {
4465 ensureExactContents(t, ctx, "myvendorapex", "android_common_myvendorapex", []string{
4466 "bin/mybin",
4467 "lib64/libz.so",
4468 })
4469 apexManifestRule := ctx.ModuleForTests("myvendorapex", "android_common_myvendorapex").Rule("apexManifestRule")
4470 android.AssertStringEquals(t, "should not require libz", apexManifestRule.Args["requireNativeLibs"], "")
4471 }
4472}
4473
Alex Light9670d332019-01-29 18:07:33 -08004474func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004475 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004476 apex {
4477 name: "myapex",
4478 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004479 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004480 multilib: {
4481 first: {
4482 native_shared_libs: ["mylib_common"],
4483 }
4484 },
4485 target: {
4486 android: {
4487 multilib: {
4488 first: {
4489 native_shared_libs: ["mylib"],
4490 }
4491 }
4492 },
4493 host: {
4494 multilib: {
4495 first: {
4496 native_shared_libs: ["mylib2"],
4497 }
4498 }
4499 }
4500 }
4501 }
4502
4503 apex_key {
4504 name: "myapex.key",
4505 public_key: "testkey.avbpubkey",
4506 private_key: "testkey.pem",
4507 }
4508
4509 cc_library {
4510 name: "mylib",
4511 srcs: ["mylib.cpp"],
4512 system_shared_libs: [],
4513 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004514 // TODO: remove //apex_available:platform
4515 apex_available: [
4516 "//apex_available:platform",
4517 "myapex",
4518 ],
Alex Light9670d332019-01-29 18:07:33 -08004519 }
4520
4521 cc_library {
4522 name: "mylib_common",
4523 srcs: ["mylib.cpp"],
4524 system_shared_libs: [],
4525 stl: "none",
4526 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004527 // TODO: remove //apex_available:platform
4528 apex_available: [
4529 "//apex_available:platform",
4530 "myapex",
4531 ],
Alex Light9670d332019-01-29 18:07:33 -08004532 }
4533
4534 cc_library {
4535 name: "mylib2",
4536 srcs: ["mylib.cpp"],
4537 system_shared_libs: [],
4538 stl: "none",
4539 compile_multilib: "first",
4540 }
4541 `)
4542
Jooyung Hana0503a52023-08-23 13:12:50 +09004543 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004544 copyCmds := apexRule.Args["copy_commands"]
4545
4546 // Ensure that main rule creates an output
4547 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4548
4549 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004550 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4551 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4552 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004553
4554 // Ensure that both direct and indirect deps are copied into apex
4555 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4556 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4557 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4558
Colin Cross7113d202019-11-20 16:39:12 -08004559 // Ensure that the platform variant ends with _shared
4560 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4561 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4562 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004563}
Jiyong Park04480cf2019-02-06 00:16:29 +09004564
Jiyong Park59140302020-12-14 18:44:04 +09004565func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004566 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004567 apex {
4568 name: "myapex",
4569 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004570 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004571 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004572 arch: {
4573 arm64: {
4574 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004575 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004576 },
4577 x86_64: {
4578 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004579 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004580 },
4581 }
4582 }
4583
4584 apex_key {
4585 name: "myapex.key",
4586 public_key: "testkey.avbpubkey",
4587 private_key: "testkey.pem",
4588 }
4589
4590 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004591 name: "mylib.generic",
4592 srcs: ["mylib.cpp"],
4593 system_shared_libs: [],
4594 stl: "none",
4595 // TODO: remove //apex_available:platform
4596 apex_available: [
4597 "//apex_available:platform",
4598 "myapex",
4599 ],
4600 }
4601
4602 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004603 name: "mylib.arm64",
4604 srcs: ["mylib.cpp"],
4605 system_shared_libs: [],
4606 stl: "none",
4607 // TODO: remove //apex_available:platform
4608 apex_available: [
4609 "//apex_available:platform",
4610 "myapex",
4611 ],
4612 }
4613
4614 cc_library {
4615 name: "mylib.x64",
4616 srcs: ["mylib.cpp"],
4617 system_shared_libs: [],
4618 stl: "none",
4619 // TODO: remove //apex_available:platform
4620 apex_available: [
4621 "//apex_available:platform",
4622 "myapex",
4623 ],
4624 }
4625 `)
4626
Jooyung Hana0503a52023-08-23 13:12:50 +09004627 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park59140302020-12-14 18:44:04 +09004628 copyCmds := apexRule.Args["copy_commands"]
4629
4630 // Ensure that apex variant is created for the direct dep
4631 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004632 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004633 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4634
4635 // Ensure that both direct and indirect deps are copied into apex
4636 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4637 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4638}
4639
Jiyong Park04480cf2019-02-06 00:16:29 +09004640func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004641 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004642 apex {
4643 name: "myapex",
4644 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004645 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004646 updatable: false,
Riya Thakur654461c2024-02-27 07:21:05 +00004647 compile_multilib: "both",
Jiyong Park04480cf2019-02-06 00:16:29 +09004648 }
4649
4650 apex_key {
4651 name: "myapex.key",
4652 public_key: "testkey.avbpubkey",
4653 private_key: "testkey.pem",
4654 }
4655
4656 sh_binary {
4657 name: "myscript",
4658 src: "mylib.cpp",
4659 filename: "myscript.sh",
4660 sub_dir: "script",
4661 }
4662 `)
4663
Jooyung Hana0503a52023-08-23 13:12:50 +09004664 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004665 copyCmds := apexRule.Args["copy_commands"]
4666
4667 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4668}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004669
Jooyung Han91df2082019-11-20 01:49:42 +09004670func TestApexInVariousPartition(t *testing.T) {
4671 testcases := []struct {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004672 propName, partition string
Jooyung Han91df2082019-11-20 01:49:42 +09004673 }{
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004674 {"", "system"},
4675 {"product_specific: true", "product"},
4676 {"soc_specific: true", "vendor"},
4677 {"proprietary: true", "vendor"},
4678 {"vendor: true", "vendor"},
4679 {"system_ext_specific: true", "system_ext"},
Jooyung Han91df2082019-11-20 01:49:42 +09004680 }
4681 for _, tc := range testcases {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004682 t.Run(tc.propName+":"+tc.partition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004683 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004684 apex {
4685 name: "myapex",
4686 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004687 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004688 `+tc.propName+`
4689 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004690
Jooyung Han91df2082019-11-20 01:49:42 +09004691 apex_key {
4692 name: "myapex.key",
4693 public_key: "testkey.avbpubkey",
4694 private_key: "testkey.pem",
4695 }
4696 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004697
Jooyung Hana0503a52023-08-23 13:12:50 +09004698 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004699 expected := "out/soong/target/product/test_device/" + tc.partition + "/apex"
Paul Duffin37ba3442021-03-29 00:21:08 +01004700 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004701 if actual != expected {
4702 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4703 }
Jooyung Han91df2082019-11-20 01:49:42 +09004704 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004705 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004706}
Jiyong Park67882562019-03-21 01:11:21 +09004707
Jooyung Han580eb4f2020-06-24 19:33:06 +09004708func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004709 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004710 apex {
4711 name: "myapex",
4712 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004713 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004714 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004715
Jooyung Han580eb4f2020-06-24 19:33:06 +09004716 apex_key {
4717 name: "myapex.key",
4718 public_key: "testkey.avbpubkey",
4719 private_key: "testkey.pem",
4720 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004721 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09004722 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004723 rule := module.Output("file_contexts")
4724 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4725}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004726
Jooyung Han580eb4f2020-06-24 19:33:06 +09004727func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004728 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004729 apex {
4730 name: "myapex",
4731 key: "myapex.key",
4732 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004733 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004734 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004735
Jooyung Han580eb4f2020-06-24 19:33:06 +09004736 apex_key {
4737 name: "myapex.key",
4738 public_key: "testkey.avbpubkey",
4739 private_key: "testkey.pem",
4740 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004741 `, withFiles(map[string][]byte{
4742 "my_own_file_contexts": nil,
4743 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004744}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004745
Jooyung Han580eb4f2020-06-24 19:33:06 +09004746func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004747 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004748 apex {
4749 name: "myapex",
4750 key: "myapex.key",
4751 product_specific: true,
4752 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004753 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004754 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004755
Jooyung Han580eb4f2020-06-24 19:33:06 +09004756 apex_key {
4757 name: "myapex.key",
4758 public_key: "testkey.avbpubkey",
4759 private_key: "testkey.pem",
4760 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004761 `)
4762
Colin Cross1c460562021-02-16 17:55:47 -08004763 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004764 apex {
4765 name: "myapex",
4766 key: "myapex.key",
4767 product_specific: true,
4768 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004769 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004770 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004771
Jooyung Han580eb4f2020-06-24 19:33:06 +09004772 apex_key {
4773 name: "myapex.key",
4774 public_key: "testkey.avbpubkey",
4775 private_key: "testkey.pem",
4776 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004777 `, withFiles(map[string][]byte{
4778 "product_specific_file_contexts": nil,
4779 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004780 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004781 rule := module.Output("file_contexts")
4782 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4783}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004784
Jooyung Han580eb4f2020-06-24 19:33:06 +09004785func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004786 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004787 apex {
4788 name: "myapex",
4789 key: "myapex.key",
4790 product_specific: true,
4791 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004792 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004793 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004794
Jooyung Han580eb4f2020-06-24 19:33:06 +09004795 apex_key {
4796 name: "myapex.key",
4797 public_key: "testkey.avbpubkey",
4798 private_key: "testkey.pem",
4799 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004800
Jooyung Han580eb4f2020-06-24 19:33:06 +09004801 filegroup {
4802 name: "my-file-contexts",
4803 srcs: ["product_specific_file_contexts"],
4804 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004805 `, withFiles(map[string][]byte{
4806 "product_specific_file_contexts": nil,
4807 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004808 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004809 rule := module.Output("file_contexts")
4810 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004811}
4812
Jiyong Park67882562019-03-21 01:11:21 +09004813func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004814 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004815 apex_key {
4816 name: "myapex.key",
4817 public_key: ":my.avbpubkey",
4818 private_key: ":my.pem",
4819 product_specific: true,
4820 }
4821
4822 filegroup {
4823 name: "my.avbpubkey",
4824 srcs: ["testkey2.avbpubkey"],
4825 }
4826
4827 filegroup {
4828 name: "my.pem",
4829 srcs: ["testkey2.pem"],
4830 }
4831 `)
4832
4833 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4834 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004835 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004836 if actual_pubkey != expected_pubkey {
4837 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4838 }
4839 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004840 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004841 if actual_privkey != expected_privkey {
4842 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4843 }
4844}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004845
4846func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004847 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004848 prebuilt_apex {
4849 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004850 arch: {
4851 arm64: {
4852 src: "myapex-arm64.apex",
4853 },
4854 arm: {
4855 src: "myapex-arm.apex",
4856 },
4857 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004858 }
4859 `)
4860
Wei Li340ee8e2022-03-18 17:33:24 -07004861 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4862 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004863
Jiyong Parkc95714e2019-03-29 14:23:10 +09004864 expectedInput := "myapex-arm64.apex"
4865 if prebuilt.inputApex.String() != expectedInput {
4866 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4867 }
Wei Li340ee8e2022-03-18 17:33:24 -07004868 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4869 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4870 rule := testingModule.Rule("genProvenanceMetaData")
4871 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4872 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4873 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4874 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08004875
4876 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
4877 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004878}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004879
Paul Duffinc0609c62021-03-01 17:27:16 +00004880func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004881 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004882 prebuilt_apex {
4883 name: "myapex",
4884 }
4885 `)
4886}
4887
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004888func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004889 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004890 prebuilt_apex {
4891 name: "myapex",
4892 src: "myapex-arm.apex",
4893 filename: "notmyapex.apex",
4894 }
4895 `)
4896
Wei Li340ee8e2022-03-18 17:33:24 -07004897 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4898 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004899
4900 expected := "notmyapex.apex"
4901 if p.installFilename != expected {
4902 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4903 }
Wei Li340ee8e2022-03-18 17:33:24 -07004904 rule := testingModule.Rule("genProvenanceMetaData")
4905 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4906 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4907 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4908 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004909}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004910
Samiul Islam7c02e262021-09-08 17:48:28 +01004911func TestApexSetFilenameOverride(t *testing.T) {
4912 testApex(t, `
4913 apex_set {
4914 name: "com.company.android.myapex",
4915 apex_name: "com.android.myapex",
4916 set: "company-myapex.apks",
4917 filename: "com.company.android.myapex.apex"
4918 }
4919 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4920
4921 testApex(t, `
4922 apex_set {
4923 name: "com.company.android.myapex",
4924 apex_name: "com.android.myapex",
4925 set: "company-myapex.apks",
4926 filename: "com.company.android.myapex.capex"
4927 }
4928 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4929
4930 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4931 apex_set {
4932 name: "com.company.android.myapex",
4933 apex_name: "com.android.myapex",
4934 set: "company-myapex.apks",
4935 filename: "some-random-suffix"
4936 }
4937 `)
4938}
4939
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004940func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004941 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004942 prebuilt_apex {
4943 name: "myapex.prebuilt",
4944 src: "myapex-arm.apex",
4945 overrides: [
4946 "myapex",
4947 ],
4948 }
4949 `)
4950
Wei Li340ee8e2022-03-18 17:33:24 -07004951 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4952 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004953
4954 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004955 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004956 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004957 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004958 }
Wei Li340ee8e2022-03-18 17:33:24 -07004959 rule := testingModule.Rule("genProvenanceMetaData")
4960 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4961 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4962 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4963 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004964}
4965
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004966func TestPrebuiltApexName(t *testing.T) {
4967 testApex(t, `
4968 prebuilt_apex {
4969 name: "com.company.android.myapex",
4970 apex_name: "com.android.myapex",
4971 src: "company-myapex-arm.apex",
4972 }
4973 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4974
4975 testApex(t, `
4976 apex_set {
4977 name: "com.company.android.myapex",
4978 apex_name: "com.android.myapex",
4979 set: "company-myapex.apks",
4980 }
4981 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4982}
4983
4984func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4985 _ = android.GroupFixturePreparers(
4986 java.PrepareForTestWithJavaDefaultModules,
4987 PrepareForTestWithApexBuildComponents,
4988 android.FixtureWithRootAndroidBp(`
4989 platform_bootclasspath {
4990 name: "platform-bootclasspath",
4991 fragments: [
4992 {
4993 apex: "com.android.art",
4994 module: "art-bootclasspath-fragment",
4995 },
4996 ],
4997 }
4998
4999 prebuilt_apex {
5000 name: "com.company.android.art",
5001 apex_name: "com.android.art",
5002 src: "com.company.android.art-arm.apex",
5003 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
5004 }
5005
5006 prebuilt_bootclasspath_fragment {
5007 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01005008 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01005009 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01005010 hidden_api: {
5011 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5012 metadata: "my-bootclasspath-fragment/metadata.csv",
5013 index: "my-bootclasspath-fragment/index.csv",
5014 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5015 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5016 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01005017 }
5018
5019 java_import {
5020 name: "core-oj",
5021 jars: ["prebuilt.jar"],
5022 }
5023 `),
5024 ).RunTest(t)
5025}
5026
Spandan Das59a4a2b2024-01-09 21:35:56 +00005027// A minimal context object for use with DexJarBuildPath
5028type moduleErrorfTestCtx struct {
5029}
5030
5031func (ctx moduleErrorfTestCtx) ModuleErrorf(format string, args ...interface{}) {
5032}
5033
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005034func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005035 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005036 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005037 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5038 // is disabled.
5039 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
Spandan Das81fe4d12024-05-15 18:43:47 +00005040
5041 // Make sure that we have atleast one platform library so that we can check the monolithic hiddenapi
5042 // file creation.
5043 java.FixtureConfigureBootJars("platform:foo"),
5044 android.FixtureModifyMockFS(func(fs android.MockFS) {
5045 fs["platform/Android.bp"] = []byte(`
5046 java_library {
5047 name: "foo",
5048 srcs: ["Test.java"],
5049 compile_dex: true,
5050 }
5051 `)
5052 fs["platform/Test.java"] = nil
5053 }),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005054 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005055
Paul Duffin40a3f652021-07-19 13:11:24 +01005056 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005057 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005058 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005059 var rule android.TestingBuildParams
5060
5061 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5062 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005063 }
5064
Paul Duffin40a3f652021-07-19 13:11:24 +01005065 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5066 t.Helper()
5067 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5068 var rule android.TestingBuildParams
5069
5070 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5071 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5072 }
5073
Paul Duffin89f570a2021-06-16 01:42:33 +01005074 fragment := java.ApexVariantReference{
5075 Apex: proptools.StringPtr("myapex"),
5076 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5077 }
5078
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005079 t.Run("prebuilt only", func(t *testing.T) {
5080 bp := `
5081 prebuilt_apex {
5082 name: "myapex",
5083 arch: {
5084 arm64: {
5085 src: "myapex-arm64.apex",
5086 },
5087 arm: {
5088 src: "myapex-arm.apex",
5089 },
5090 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005091 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5092 }
5093
5094 prebuilt_bootclasspath_fragment {
5095 name: "my-bootclasspath-fragment",
5096 contents: ["libfoo", "libbar"],
5097 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005098 hidden_api: {
5099 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5100 metadata: "my-bootclasspath-fragment/metadata.csv",
5101 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005102 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5103 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5104 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005105 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005106 }
5107
Spandan Das52c01a12024-09-20 01:09:48 +00005108 java_sdk_library_import {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005109 name: "libfoo",
Spandan Das52c01a12024-09-20 01:09:48 +00005110 public: {
5111 jars: ["libfoo.jar"],
5112 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005113 apex_available: ["myapex"],
Spandan Das52c01a12024-09-20 01:09:48 +00005114 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005115 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005116 }
Paul Duffin37856732021-02-26 14:24:15 +00005117
5118 java_sdk_library_import {
5119 name: "libbar",
5120 public: {
5121 jars: ["libbar.jar"],
5122 },
5123 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005124 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005125 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005126 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005127 `
5128
Paul Duffin89f570a2021-06-16 01:42:33 +01005129 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005130
Paul Duffin537ea3d2021-05-14 10:38:00 +01005131 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005132 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005133 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005134 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005135 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005136 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005137 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005138 })
5139
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005140 t.Run("apex_set only", func(t *testing.T) {
5141 bp := `
5142 apex_set {
5143 name: "myapex",
5144 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005145 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
Liz Kammer2dc72442023-04-20 10:10:48 -04005146 exported_systemserverclasspath_fragments: ["my-systemserverclasspath-fragment"],
5147 }
5148
Paul Duffin89f570a2021-06-16 01:42:33 +01005149 prebuilt_bootclasspath_fragment {
5150 name: "my-bootclasspath-fragment",
5151 contents: ["libfoo", "libbar"],
5152 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005153 hidden_api: {
5154 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5155 metadata: "my-bootclasspath-fragment/metadata.csv",
5156 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005157 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5158 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5159 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005160 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005161 }
5162
Liz Kammer2dc72442023-04-20 10:10:48 -04005163 prebuilt_systemserverclasspath_fragment {
5164 name: "my-systemserverclasspath-fragment",
5165 contents: ["libbaz"],
5166 apex_available: ["myapex"],
5167 }
5168
Spandan Das52c01a12024-09-20 01:09:48 +00005169 java_sdk_library_import {
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005170 name: "libfoo",
Spandan Das52c01a12024-09-20 01:09:48 +00005171 public: {
5172 jars: ["libfoo.jar"],
5173 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005174 apex_available: ["myapex"],
Spandan Das52c01a12024-09-20 01:09:48 +00005175 shared_library: false,
5176 permitted_packages: ["libfoo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005177 }
5178
Spandan Das52c01a12024-09-20 01:09:48 +00005179
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005180 java_sdk_library_import {
5181 name: "libbar",
5182 public: {
5183 jars: ["libbar.jar"],
5184 },
5185 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005186 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005187 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005188 }
Liz Kammer2dc72442023-04-20 10:10:48 -04005189
5190 java_sdk_library_import {
5191 name: "libbaz",
5192 public: {
5193 jars: ["libbaz.jar"],
5194 },
5195 apex_available: ["myapex"],
5196 shared_library: false,
5197 permitted_packages: ["baz"],
5198 }
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005199 `
5200
Paul Duffin89f570a2021-06-16 01:42:33 +01005201 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005202
Paul Duffin537ea3d2021-05-14 10:38:00 +01005203 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005204 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005205 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005206 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005207 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005208 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005209 `)
Liz Kammer2dc72442023-04-20 10:10:48 -04005210
5211 myApex := ctx.ModuleForTests("myapex", "android_common_myapex").Module()
5212
5213 overrideNames := []string{
Spandan Dasa8e2d612024-07-26 19:24:27 +00005214 "",
Liz Kammer2dc72442023-04-20 10:10:48 -04005215 "myjavalib.myapex",
5216 "libfoo.myapex",
5217 "libbar.myapex",
5218 "libbaz.myapex",
5219 }
5220 mkEntries := android.AndroidMkEntriesForTest(t, ctx, myApex)
5221 for i, e := range mkEntries {
5222 g := e.OverrideName
5223 if w := overrideNames[i]; w != g {
5224 t.Errorf("Expected override name %q, got %q", w, g)
5225 }
5226 }
5227
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005228 })
5229
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005230 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5231 bp := `
5232 prebuilt_apex {
5233 name: "myapex",
5234 arch: {
5235 arm64: {
5236 src: "myapex-arm64.apex",
5237 },
5238 arm: {
5239 src: "myapex-arm.apex",
5240 },
5241 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005242 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5243 }
5244
5245 prebuilt_bootclasspath_fragment {
5246 name: "my-bootclasspath-fragment",
5247 contents: ["libfoo", "libbar"],
5248 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005249 hidden_api: {
5250 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5251 metadata: "my-bootclasspath-fragment/metadata.csv",
5252 index: "my-bootclasspath-fragment/index.csv",
5253 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5254 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5255 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005256 }
5257
5258 java_import {
5259 name: "libfoo",
5260 jars: ["libfoo.jar"],
5261 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005262 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005263 }
5264
5265 java_library {
5266 name: "libfoo",
5267 srcs: ["foo/bar/MyClass.java"],
5268 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005269 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005270 }
Paul Duffin37856732021-02-26 14:24:15 +00005271
5272 java_sdk_library_import {
5273 name: "libbar",
5274 public: {
5275 jars: ["libbar.jar"],
5276 },
5277 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005278 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005279 }
5280
5281 java_sdk_library {
5282 name: "libbar",
5283 srcs: ["foo/bar/MyClass.java"],
5284 unsafe_ignore_missing_latest_api: true,
5285 apex_available: ["myapex"],
5286 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005287 `
5288
5289 // In this test the source (java_library) libfoo is active since the
5290 // prebuilt (java_import) defaults to prefer:false. However the
5291 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5292 // find the dex boot jar in it. We either need to disable the source libfoo
5293 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005294 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005295 // dexbootjar check is skipped if AllowMissingDependencies is true
5296 preparerAllowMissingDeps := android.GroupFixturePreparers(
5297 preparer,
5298 android.PrepareForTestWithAllowMissingDependencies,
5299 )
5300 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005301 })
5302
5303 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5304 bp := `
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005305 apex {
5306 name: "myapex",
5307 key: "myapex.key",
5308 updatable: false,
5309 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5310 }
5311
5312 apex_key {
5313 name: "myapex.key",
5314 public_key: "testkey.avbpubkey",
5315 private_key: "testkey.pem",
5316 }
5317
5318 bootclasspath_fragment {
5319 name: "my-bootclasspath-fragment",
5320 contents: ["libfoo", "libbar"],
5321 apex_available: ["myapex"],
5322 hidden_api: {
5323 split_packages: ["*"],
5324 },
5325 }
5326
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005327 prebuilt_apex {
5328 name: "myapex",
5329 arch: {
5330 arm64: {
5331 src: "myapex-arm64.apex",
5332 },
5333 arm: {
5334 src: "myapex-arm.apex",
5335 },
5336 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005337 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5338 }
5339
5340 prebuilt_bootclasspath_fragment {
5341 name: "my-bootclasspath-fragment",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005342 prefer: true,
Paul Duffin89f570a2021-06-16 01:42:33 +01005343 contents: ["libfoo", "libbar"],
5344 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005345 hidden_api: {
5346 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5347 metadata: "my-bootclasspath-fragment/metadata.csv",
5348 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005349 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5350 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5351 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005352 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005353 }
5354
Spandan Das52c01a12024-09-20 01:09:48 +00005355 java_sdk_library_import {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005356 name: "libfoo",
5357 prefer: true,
Spandan Das52c01a12024-09-20 01:09:48 +00005358 public: {
5359 jars: ["libfoo.jar"],
5360 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005361 apex_available: ["myapex"],
Spandan Das52c01a12024-09-20 01:09:48 +00005362 shared_library: false,
5363 permitted_packages: ["libfoo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005364 }
5365
5366 java_library {
5367 name: "libfoo",
5368 srcs: ["foo/bar/MyClass.java"],
5369 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005370 installable: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00005371 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005372 }
Paul Duffin37856732021-02-26 14:24:15 +00005373
5374 java_sdk_library_import {
5375 name: "libbar",
5376 prefer: true,
5377 public: {
5378 jars: ["libbar.jar"],
5379 },
5380 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005381 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005382 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005383 }
5384
5385 java_sdk_library {
5386 name: "libbar",
5387 srcs: ["foo/bar/MyClass.java"],
5388 unsafe_ignore_missing_latest_api: true,
5389 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005390 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005391 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005392 `
5393
Paul Duffin89f570a2021-06-16 01:42:33 +01005394 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005395
Paul Duffin537ea3d2021-05-14 10:38:00 +01005396 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005397 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005398 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005399 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005400 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005401 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005402 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005403 })
5404
5405 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5406 bp := `
5407 apex {
5408 name: "myapex",
5409 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005410 updatable: false,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005411 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005412 }
5413
5414 apex_key {
5415 name: "myapex.key",
5416 public_key: "testkey.avbpubkey",
5417 private_key: "testkey.pem",
5418 }
5419
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005420 bootclasspath_fragment {
5421 name: "my-bootclasspath-fragment",
5422 contents: ["libfoo", "libbar"],
5423 apex_available: ["myapex"],
5424 hidden_api: {
5425 split_packages: ["*"],
5426 },
5427 }
5428
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005429 prebuilt_apex {
5430 name: "myapex",
5431 arch: {
5432 arm64: {
5433 src: "myapex-arm64.apex",
5434 },
5435 arm: {
5436 src: "myapex-arm.apex",
5437 },
5438 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005439 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5440 }
5441
5442 prebuilt_bootclasspath_fragment {
5443 name: "my-bootclasspath-fragment",
5444 contents: ["libfoo", "libbar"],
5445 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005446 hidden_api: {
5447 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5448 metadata: "my-bootclasspath-fragment/metadata.csv",
5449 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005450 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5451 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5452 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005453 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005454 }
5455
5456 java_import {
5457 name: "libfoo",
5458 jars: ["libfoo.jar"],
5459 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005460 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005461 }
5462
5463 java_library {
5464 name: "libfoo",
5465 srcs: ["foo/bar/MyClass.java"],
5466 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005467 permitted_packages: ["foo"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005468 installable: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00005469 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005470 }
Paul Duffin37856732021-02-26 14:24:15 +00005471
5472 java_sdk_library_import {
5473 name: "libbar",
5474 public: {
5475 jars: ["libbar.jar"],
5476 },
5477 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005478 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005479 }
5480
5481 java_sdk_library {
5482 name: "libbar",
5483 srcs: ["foo/bar/MyClass.java"],
5484 unsafe_ignore_missing_latest_api: true,
5485 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005486 permitted_packages: ["bar"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005487 compile_dex: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00005488 sdk_version: "core_current",
Paul Duffin37856732021-02-26 14:24:15 +00005489 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005490 `
5491
Paul Duffin89f570a2021-06-16 01:42:33 +01005492 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005493
Paul Duffin537ea3d2021-05-14 10:38:00 +01005494 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005495 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005496 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
5497 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005498 out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/modular-hiddenapi/index.csv
5499 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005500 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005501 })
5502
5503 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5504 bp := `
5505 apex {
5506 name: "myapex",
5507 enabled: false,
5508 key: "myapex.key",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005509 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005510 }
5511
5512 apex_key {
5513 name: "myapex.key",
5514 public_key: "testkey.avbpubkey",
5515 private_key: "testkey.pem",
5516 }
5517
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005518 bootclasspath_fragment {
5519 name: "my-bootclasspath-fragment",
5520 enabled: false,
5521 contents: ["libfoo", "libbar"],
5522 apex_available: ["myapex"],
5523 hidden_api: {
5524 split_packages: ["*"],
5525 },
5526 }
5527
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005528 prebuilt_apex {
5529 name: "myapex",
5530 arch: {
5531 arm64: {
5532 src: "myapex-arm64.apex",
5533 },
5534 arm: {
5535 src: "myapex-arm.apex",
5536 },
5537 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005538 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5539 }
5540
5541 prebuilt_bootclasspath_fragment {
5542 name: "my-bootclasspath-fragment",
5543 contents: ["libfoo", "libbar"],
5544 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005545 hidden_api: {
5546 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5547 metadata: "my-bootclasspath-fragment/metadata.csv",
5548 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005549 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5550 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5551 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005552 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005553 }
5554
5555 java_import {
5556 name: "libfoo",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005557 jars: ["libfoo.jar"],
5558 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005559 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005560 }
5561
5562 java_library {
5563 name: "libfoo",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005564 enabled: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005565 srcs: ["foo/bar/MyClass.java"],
5566 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005567 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005568 }
Paul Duffin37856732021-02-26 14:24:15 +00005569
5570 java_sdk_library_import {
5571 name: "libbar",
Paul Duffin37856732021-02-26 14:24:15 +00005572 public: {
5573 jars: ["libbar.jar"],
5574 },
5575 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005576 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005577 permitted_packages: ["bar"],
Jihoon Kanga6d0aa82024-09-24 00:34:49 +00005578 prefer: true,
Paul Duffin37856732021-02-26 14:24:15 +00005579 }
5580
5581 java_sdk_library {
5582 name: "libbar",
5583 srcs: ["foo/bar/MyClass.java"],
5584 unsafe_ignore_missing_latest_api: true,
5585 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005586 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005587 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005588 `
Cole Fausta963b942024-04-11 17:43:00 -07005589 // This test disables libbar, which causes the ComponentDepsMutator to add
5590 // deps on libbar.stubs and other sub-modules that don't exist. We can
5591 // enable AllowMissingDependencies to work around that, but enabling that
5592 // causes extra checks for missing source files to dex_bootjars, so add those
5593 // to the mock fs as well.
5594 preparer2 := android.GroupFixturePreparers(
5595 preparer,
5596 android.PrepareForTestWithAllowMissingDependencies,
5597 android.FixtureMergeMockFs(map[string][]byte{
5598 "build/soong/scripts/check_boot_jars/package_allowed_list.txt": nil,
Colin Cross99939e92024-10-01 16:02:46 -07005599 "frameworks/base/boot/boot-profile.txt": nil,
Cole Fausta963b942024-04-11 17:43:00 -07005600 }),
5601 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005602
Cole Fausta963b942024-04-11 17:43:00 -07005603 ctx := testDexpreoptWithApexes(t, bp, "", preparer2, fragment)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005604
Paul Duffin537ea3d2021-05-14 10:38:00 +01005605 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005606 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005607 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005608 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005609 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005610 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005611 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005612 })
Spandan Das3a392012024-01-17 18:26:27 +00005613
Spandan Dasf2c10572024-02-27 04:49:52 +00005614 t.Run("Co-existing unflagged apexes should create a duplicate module error", func(t *testing.T) {
Spandan Das3a392012024-01-17 18:26:27 +00005615 bp := `
5616 // Source
5617 apex {
5618 name: "myapex",
5619 enabled: false,
5620 key: "myapex.key",
5621 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5622 }
5623
5624 apex_key {
5625 name: "myapex.key",
5626 public_key: "testkey.avbpubkey",
5627 private_key: "testkey.pem",
5628 }
5629
5630 // Prebuilt
5631 prebuilt_apex {
5632 name: "myapex.v1",
5633 source_apex_name: "myapex",
5634 arch: {
5635 arm64: {
5636 src: "myapex-arm64.apex",
5637 },
5638 arm: {
5639 src: "myapex-arm.apex",
5640 },
5641 },
5642 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5643 prefer: true,
5644 }
5645 prebuilt_apex {
5646 name: "myapex.v2",
5647 source_apex_name: "myapex",
5648 arch: {
5649 arm64: {
5650 src: "myapex-arm64.apex",
5651 },
5652 arm: {
5653 src: "myapex-arm.apex",
5654 },
5655 },
5656 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5657 prefer: true,
5658 }
5659
5660 prebuilt_bootclasspath_fragment {
5661 name: "my-bootclasspath-fragment",
5662 contents: ["libfoo", "libbar"],
5663 apex_available: ["myapex"],
5664 hidden_api: {
5665 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5666 metadata: "my-bootclasspath-fragment/metadata.csv",
5667 index: "my-bootclasspath-fragment/index.csv",
5668 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5669 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5670 },
5671 prefer: true,
5672 }
5673
5674 java_import {
5675 name: "libfoo",
5676 jars: ["libfoo.jar"],
5677 apex_available: ["myapex"],
5678 prefer: true,
5679 }
5680 java_import {
5681 name: "libbar",
5682 jars: ["libbar.jar"],
5683 apex_available: ["myapex"],
5684 prefer: true,
5685 }
5686 `
5687
Spandan Dasf2c10572024-02-27 04:49:52 +00005688 testDexpreoptWithApexes(t, bp, "Multiple prebuilt modules prebuilt_myapex.v1 and prebuilt_myapex.v2 have been marked as preferred for this source module", preparer, fragment)
Spandan Das3a392012024-01-17 18:26:27 +00005689 })
5690
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005691}
5692
Roland Levillain630846d2019-06-26 12:48:34 +01005693func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005694 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005695 apex_test {
5696 name: "myapex",
5697 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005698 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005699 tests: [
5700 "mytest",
5701 ],
5702 }
5703
5704 apex_key {
5705 name: "myapex.key",
5706 public_key: "testkey.avbpubkey",
5707 private_key: "testkey.pem",
5708 }
5709
Liz Kammer1c14a212020-05-12 15:26:55 -07005710 filegroup {
5711 name: "fg",
5712 srcs: [
5713 "baz",
5714 "bar/baz"
5715 ],
5716 }
5717
Roland Levillain630846d2019-06-26 12:48:34 +01005718 cc_test {
5719 name: "mytest",
5720 gtest: false,
5721 srcs: ["mytest.cpp"],
5722 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005723 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005724 system_shared_libs: [],
5725 static_executable: true,
5726 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005727 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005728 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005729
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005730 cc_library {
5731 name: "mylib",
5732 srcs: ["mylib.cpp"],
5733 system_shared_libs: [],
5734 stl: "none",
5735 }
5736
Liz Kammer5bd365f2020-05-27 15:15:11 -07005737 filegroup {
5738 name: "fg2",
5739 srcs: [
5740 "testdata/baz"
5741 ],
5742 }
Roland Levillain630846d2019-06-26 12:48:34 +01005743 `)
5744
Jooyung Hana0503a52023-08-23 13:12:50 +09005745 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005746 copyCmds := apexRule.Args["copy_commands"]
5747
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005748 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005749 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005750 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005751
Liz Kammer1c14a212020-05-12 15:26:55 -07005752 //Ensure that test data are copied into apex.
5753 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5754 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5755
Roland Levillainf89cd092019-07-29 16:22:59 +01005756 // Ensure the module is correctly translated.
Jooyung Hana0503a52023-08-23 13:12:50 +09005757 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005758 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005759 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005760 prefix := "TARGET_"
5761 var builder strings.Builder
5762 data.Custom(&builder, name, prefix, "", data)
5763 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005764 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005765 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005766}
5767
Jooyung Hand48f3c32019-08-23 11:18:57 +09005768func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5769 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5770 apex {
5771 name: "myapex",
5772 key: "myapex.key",
5773 native_shared_libs: ["libfoo"],
5774 }
5775
5776 apex_key {
5777 name: "myapex.key",
5778 public_key: "testkey.avbpubkey",
5779 private_key: "testkey.pem",
5780 }
5781
5782 cc_library {
5783 name: "libfoo",
5784 stl: "none",
5785 system_shared_libs: [],
5786 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005787 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005788 }
5789 `)
5790 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5791 apex {
5792 name: "myapex",
5793 key: "myapex.key",
5794 java_libs: ["myjar"],
5795 }
5796
5797 apex_key {
5798 name: "myapex.key",
5799 public_key: "testkey.avbpubkey",
5800 private_key: "testkey.pem",
5801 }
5802
5803 java_library {
5804 name: "myjar",
5805 srcs: ["foo/bar/MyClass.java"],
5806 sdk_version: "none",
5807 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005808 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005809 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005810 }
5811 `)
5812}
5813
Bill Peckhama41a6962021-01-11 10:58:54 -08005814func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005815 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005816 apex {
5817 name: "myapex",
5818 key: "myapex.key",
5819 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005820 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005821 }
5822
5823 apex_key {
5824 name: "myapex.key",
5825 public_key: "testkey.avbpubkey",
5826 private_key: "testkey.pem",
5827 }
5828
5829 java_import {
5830 name: "myjavaimport",
5831 apex_available: ["myapex"],
5832 jars: ["my.jar"],
5833 compile_dex: true,
5834 }
5835 `)
5836
Jooyung Hana0503a52023-08-23 13:12:50 +09005837 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Bill Peckhama41a6962021-01-11 10:58:54 -08005838 apexRule := module.Rule("apexRule")
5839 copyCmds := apexRule.Args["copy_commands"]
5840 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5841}
5842
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005843func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005844 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005845 apex {
5846 name: "myapex",
5847 key: "myapex.key",
5848 apps: [
5849 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005850 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005851 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005852 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005853 }
5854
5855 apex_key {
5856 name: "myapex.key",
5857 public_key: "testkey.avbpubkey",
5858 private_key: "testkey.pem",
5859 }
5860
5861 android_app {
5862 name: "AppFoo",
5863 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005864 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005865 system_modules: "none",
Jiyong Park970c5242024-05-17 22:58:54 +00005866 use_embedded_native_libs: true,
Jiyong Park8be103b2019-11-08 15:53:48 +09005867 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005868 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005869 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005870 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005871
5872 android_app {
5873 name: "AppFooPriv",
5874 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005875 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005876 system_modules: "none",
5877 privileged: true,
Sam Delmerico15809f82023-05-15 17:21:47 -04005878 privapp_allowlist: "privapp_allowlist_com.android.AppFooPriv.xml",
Colin Cross094cde42020-02-15 10:38:00 -08005879 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005880 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005881 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005882
5883 cc_library_shared {
5884 name: "libjni",
5885 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005886 shared_libs: ["libfoo"],
5887 stl: "none",
5888 system_shared_libs: [],
5889 apex_available: [ "myapex" ],
5890 sdk_version: "current",
5891 }
5892
5893 cc_library_shared {
5894 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005895 stl: "none",
5896 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005897 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005898 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005899 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005900 `)
5901
Jooyung Hana0503a52023-08-23 13:12:50 +09005902 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005903 apexRule := module.Rule("apexRule")
5904 copyCmds := apexRule.Args["copy_commands"]
5905
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005906 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5907 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Andrei Onea580636b2022-08-17 16:53:46 +00005908 ensureContains(t, copyCmds, "image.apex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005909
Colin Crossaede88c2020-08-11 12:17:01 -07005910 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005911 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005912 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005913 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005914 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005915 // JNI libraries including transitive deps are
5916 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005917 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005918 // ... embedded inside APK (jnilibs.zip)
5919 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5920 // ... and not directly inside the APEX
5921 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5922 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04005923
5924 apexBundle := module.Module().(*apexBundle)
5925 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
5926 var builder strings.Builder
5927 data.Custom(&builder, apexBundle.Name(), "TARGET_", "", data)
5928 androidMk := builder.String()
5929 ensureContains(t, androidMk, "LOCAL_MODULE := AppFooPriv.myapex")
5930 ensureContains(t, androidMk, "LOCAL_MODULE := AppFoo.myapex")
5931 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFooPriv.apk")
5932 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFoo.apk")
5933 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := \\S+AppFooPriv.apk")
5934 ensureContains(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := privapp_allowlist_com.android.AppFooPriv.xml:$(PRODUCT_OUT)/apex/myapex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Dario Frenicde2a032019-10-27 00:29:22 +01005935}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005936
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005937func TestApexWithAppImportBuildId(t *testing.T) {
5938 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5939 for _, id := range invalidBuildIds {
5940 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5941 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5942 variables.BuildId = proptools.StringPtr(id)
5943 })
5944 testApexError(t, message, `apex {
5945 name: "myapex",
5946 key: "myapex.key",
5947 apps: ["AppFooPrebuilt"],
5948 updatable: false,
5949 }
5950
5951 apex_key {
5952 name: "myapex.key",
5953 public_key: "testkey.avbpubkey",
5954 private_key: "testkey.pem",
5955 }
5956
5957 android_app_import {
5958 name: "AppFooPrebuilt",
5959 apk: "PrebuiltAppFoo.apk",
5960 presigned: true,
5961 apex_available: ["myapex"],
5962 }
5963 `, fixture)
5964 }
5965}
5966
Dario Frenicde2a032019-10-27 00:29:22 +01005967func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005968 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005969 apex {
5970 name: "myapex",
5971 key: "myapex.key",
5972 apps: [
5973 "AppFooPrebuilt",
5974 "AppFooPrivPrebuilt",
5975 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005976 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005977 }
5978
5979 apex_key {
5980 name: "myapex.key",
5981 public_key: "testkey.avbpubkey",
5982 private_key: "testkey.pem",
5983 }
5984
5985 android_app_import {
5986 name: "AppFooPrebuilt",
5987 apk: "PrebuiltAppFoo.apk",
5988 presigned: true,
5989 dex_preopt: {
5990 enabled: false,
5991 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005992 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005993 }
5994
5995 android_app_import {
5996 name: "AppFooPrivPrebuilt",
5997 apk: "PrebuiltAppFooPriv.apk",
5998 privileged: true,
5999 presigned: true,
6000 dex_preopt: {
6001 enabled: false,
6002 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006003 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006004 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006005 }
6006 `)
6007
Jooyung Hana0503a52023-08-23 13:12:50 +09006008 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Frenicde2a032019-10-27 00:29:22 +01006009 apexRule := module.Rule("apexRule")
6010 copyCmds := apexRule.Args["copy_commands"]
6011
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006012 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6013 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006014}
6015
6016func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006017 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006018 apex {
6019 name: "myapex",
6020 key: "myapex.key",
6021 apps: [
6022 "AppFoo",
6023 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006024 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006025 }
6026
6027 apex_key {
6028 name: "myapex.key",
6029 public_key: "testkey.avbpubkey",
6030 private_key: "testkey.pem",
6031 }
6032
6033 android_app {
6034 name: "AppFoo",
6035 srcs: ["foo/bar/MyClass.java"],
6036 sdk_version: "none",
6037 system_modules: "none",
6038 apex_available: [ "myapex" ],
6039 }
6040
6041 android_app_import {
6042 name: "AppFoo",
6043 apk: "AppFooPrebuilt.apk",
6044 filename: "AppFooPrebuilt.apk",
6045 presigned: true,
6046 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006047 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006048 }
6049 `, withFiles(map[string][]byte{
6050 "AppFooPrebuilt.apk": nil,
6051 }))
6052
Jooyung Hana0503a52023-08-23 13:12:50 +09006053 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006054 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006055 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006056}
6057
Dario Freni6f3937c2019-12-20 22:58:03 +00006058func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006059 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006060 apex {
6061 name: "myapex",
6062 key: "myapex.key",
6063 apps: [
6064 "TesterHelpAppFoo",
6065 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006066 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006067 }
6068
6069 apex_key {
6070 name: "myapex.key",
6071 public_key: "testkey.avbpubkey",
6072 private_key: "testkey.pem",
6073 }
6074
6075 android_test_helper_app {
6076 name: "TesterHelpAppFoo",
6077 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006078 apex_available: [ "myapex" ],
Jihoon Kang85bc1932024-07-01 17:04:46 +00006079 sdk_version: "test_current",
Dario Freni6f3937c2019-12-20 22:58:03 +00006080 }
6081
6082 `)
6083
Jooyung Hana0503a52023-08-23 13:12:50 +09006084 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Freni6f3937c2019-12-20 22:58:03 +00006085 apexRule := module.Rule("apexRule")
6086 copyCmds := apexRule.Args["copy_commands"]
6087
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006088 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006089}
6090
Jooyung Han18020ea2019-11-13 10:50:48 +09006091func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6092 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006093 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006094 apex {
6095 name: "myapex",
6096 key: "myapex.key",
6097 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006098 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006099 }
6100
6101 apex_key {
6102 name: "myapex.key",
6103 public_key: "testkey.avbpubkey",
6104 private_key: "testkey.pem",
6105 }
6106
6107 apex {
6108 name: "otherapex",
6109 key: "myapex.key",
6110 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006111 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006112 }
6113
6114 cc_defaults {
6115 name: "libfoo-defaults",
6116 apex_available: ["otherapex"],
6117 }
6118
6119 cc_library {
6120 name: "libfoo",
6121 defaults: ["libfoo-defaults"],
6122 stl: "none",
6123 system_shared_libs: [],
6124 }`)
6125}
6126
Paul Duffine52e66f2020-03-30 17:54:29 +01006127func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006128 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006129 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006130 apex {
6131 name: "myapex",
6132 key: "myapex.key",
6133 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006134 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006135 }
6136
6137 apex_key {
6138 name: "myapex.key",
6139 public_key: "testkey.avbpubkey",
6140 private_key: "testkey.pem",
6141 }
6142
6143 apex {
6144 name: "otherapex",
6145 key: "otherapex.key",
6146 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006147 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006148 }
6149
6150 apex_key {
6151 name: "otherapex.key",
6152 public_key: "testkey.avbpubkey",
6153 private_key: "testkey.pem",
6154 }
6155
6156 cc_library {
6157 name: "libfoo",
6158 stl: "none",
6159 system_shared_libs: [],
6160 apex_available: ["otherapex"],
6161 }`)
Chan Wang490a6f92024-09-23 11:52:00 +00006162
6163 // 'apex_available' check is bypassed for /product apex with a specific prefix.
6164 // TODO: b/352818241 - Remove below two cases after APEX availability is enforced for /product APEXes.
6165 testApex(t, `
6166 apex {
6167 name: "com.sdv.myapex",
6168 key: "myapex.key",
6169 native_shared_libs: ["libfoo"],
6170 updatable: false,
6171 product_specific: true,
6172 }
6173
6174 apex_key {
6175 name: "myapex.key",
6176 public_key: "testkey.avbpubkey",
6177 private_key: "testkey.pem",
6178 }
6179
6180 apex {
6181 name: "com.any.otherapex",
6182 key: "otherapex.key",
6183 native_shared_libs: ["libfoo"],
6184 updatable: false,
6185 }
6186
6187 apex_key {
6188 name: "otherapex.key",
6189 public_key: "testkey.avbpubkey",
6190 private_key: "testkey.pem",
6191 }
6192
6193 cc_library {
6194 name: "libfoo",
6195 stl: "none",
6196 system_shared_libs: [],
6197 apex_available: ["com.any.otherapex"],
6198 product_specific: true,
6199 }`,
6200 android.FixtureMergeMockFs(android.MockFS{
6201 "system/sepolicy/apex/com.sdv.myapex-file_contexts": nil,
6202 "system/sepolicy/apex/com.any.otherapex-file_contexts": nil,
6203 }))
6204
6205 // 'apex_available' check is not bypassed for non-product apex with a specific prefix.
6206 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
6207 apex {
6208 name: "com.sdv.myapex",
6209 key: "myapex.key",
6210 native_shared_libs: ["libfoo"],
6211 updatable: false,
6212 }
6213
6214 apex_key {
6215 name: "myapex.key",
6216 public_key: "testkey.avbpubkey",
6217 private_key: "testkey.pem",
6218 }
6219
6220 apex {
6221 name: "com.any.otherapex",
6222 key: "otherapex.key",
6223 native_shared_libs: ["libfoo"],
6224 updatable: false,
6225 }
6226
6227 apex_key {
6228 name: "otherapex.key",
6229 public_key: "testkey.avbpubkey",
6230 private_key: "testkey.pem",
6231 }
6232
6233 cc_library {
6234 name: "libfoo",
6235 stl: "none",
6236 system_shared_libs: [],
6237 apex_available: ["com.any.otherapex"],
6238 }`,
6239 android.FixtureMergeMockFs(android.MockFS{
6240 "system/sepolicy/apex/com.sdv.myapex-file_contexts": nil,
6241 "system/sepolicy/apex/com.any.otherapex-file_contexts": nil,
6242 }))
Paul Duffine52e66f2020-03-30 17:54:29 +01006243}
Jiyong Park127b40b2019-09-30 16:04:35 +09006244
Paul Duffine52e66f2020-03-30 17:54:29 +01006245func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006246 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006247 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006248.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006249.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006250.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006251.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006252.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006253.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006254 apex {
6255 name: "myapex",
6256 key: "myapex.key",
6257 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006258 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006259 }
6260
6261 apex_key {
6262 name: "myapex.key",
6263 public_key: "testkey.avbpubkey",
6264 private_key: "testkey.pem",
6265 }
6266
Jiyong Park127b40b2019-09-30 16:04:35 +09006267 cc_library {
6268 name: "libfoo",
6269 stl: "none",
6270 shared_libs: ["libbar"],
6271 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006272 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006273 }
6274
6275 cc_library {
6276 name: "libbar",
6277 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006278 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006279 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006280 apex_available: ["myapex"],
6281 }
6282
6283 cc_library {
6284 name: "libbaz",
6285 stl: "none",
6286 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006287 }`)
Chan Wang490a6f92024-09-23 11:52:00 +00006288
6289 // 'apex_available' check is bypassed for /product apex with a specific prefix.
6290 // TODO: b/352818241 - Remove below two cases after APEX availability is enforced for /product APEXes.
6291 testApex(t, `
6292 apex {
6293 name: "com.sdv.myapex",
6294 key: "myapex.key",
6295 native_shared_libs: ["libfoo"],
6296 updatable: false,
6297 product_specific: true,
6298 }
6299
6300 apex_key {
6301 name: "myapex.key",
6302 public_key: "testkey.avbpubkey",
6303 private_key: "testkey.pem",
6304 }
6305
6306 cc_library {
6307 name: "libfoo",
6308 stl: "none",
6309 shared_libs: ["libbar"],
6310 system_shared_libs: [],
6311 apex_available: ["com.sdv.myapex"],
6312 product_specific: true,
6313 }
6314
6315 cc_library {
6316 name: "libbar",
6317 stl: "none",
6318 shared_libs: ["libbaz"],
6319 system_shared_libs: [],
6320 apex_available: ["com.sdv.myapex"],
6321 product_specific: true,
6322 }
6323
6324 cc_library {
6325 name: "libbaz",
6326 stl: "none",
6327 system_shared_libs: [],
6328 product_specific: true,
6329 }`,
6330 android.FixtureMergeMockFs(android.MockFS{
6331 "system/sepolicy/apex/com.sdv.myapex-file_contexts": nil,
6332 }))
6333
6334 // 'apex_available' check is not bypassed for non-product apex with a specific prefix.
6335 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.`, `
6336 apex {
6337 name: "com.sdv.myapex",
6338 key: "myapex.key",
6339 native_shared_libs: ["libfoo"],
6340 updatable: false,
6341 }
6342
6343 apex_key {
6344 name: "myapex.key",
6345 public_key: "testkey.avbpubkey",
6346 private_key: "testkey.pem",
6347 }
6348
6349 cc_library {
6350 name: "libfoo",
6351 stl: "none",
6352 shared_libs: ["libbar"],
6353 system_shared_libs: [],
6354 apex_available: ["com.sdv.myapex"],
6355 }
6356
6357 cc_library {
6358 name: "libbar",
6359 stl: "none",
6360 shared_libs: ["libbaz"],
6361 system_shared_libs: [],
6362 apex_available: ["com.sdv.myapex"],
6363 }
6364
6365 cc_library {
6366 name: "libbaz",
6367 stl: "none",
6368 system_shared_libs: [],
6369 }`,
6370 android.FixtureMergeMockFs(android.MockFS{
6371 "system/sepolicy/apex/com.sdv.myapex-file_contexts": nil,
6372 }))
Paul Duffine52e66f2020-03-30 17:54:29 +01006373}
Jiyong Park127b40b2019-09-30 16:04:35 +09006374
Liz Kammer5f108fa2023-05-11 14:33:17 -04006375func TestApexAvailable_IndirectStaticDep(t *testing.T) {
6376 testApex(t, `
6377 apex {
6378 name: "myapex",
6379 key: "myapex.key",
6380 native_shared_libs: ["libfoo"],
6381 updatable: false,
6382 }
6383
6384 apex_key {
6385 name: "myapex.key",
6386 public_key: "testkey.avbpubkey",
6387 private_key: "testkey.pem",
6388 }
6389
6390 cc_library {
6391 name: "libfoo",
6392 stl: "none",
6393 static_libs: ["libbar"],
6394 system_shared_libs: [],
6395 apex_available: ["myapex"],
6396 }
6397
6398 cc_library {
6399 name: "libbar",
6400 stl: "none",
6401 shared_libs: ["libbaz"],
6402 system_shared_libs: [],
6403 apex_available: ["myapex"],
6404 }
6405
6406 cc_library {
6407 name: "libbaz",
6408 stl: "none",
6409 system_shared_libs: [],
6410 }`)
6411
6412 testApexError(t, `requires "libbar" that doesn't list the APEX under 'apex_available'.`, `
6413 apex {
6414 name: "myapex",
6415 key: "myapex.key",
6416 native_shared_libs: ["libfoo"],
6417 updatable: false,
6418 }
6419
6420 apex_key {
6421 name: "myapex.key",
6422 public_key: "testkey.avbpubkey",
6423 private_key: "testkey.pem",
6424 }
6425
6426 cc_library {
6427 name: "libfoo",
6428 stl: "none",
6429 static_libs: ["libbar"],
6430 system_shared_libs: [],
6431 apex_available: ["myapex"],
6432 }
6433
6434 cc_library {
6435 name: "libbar",
6436 stl: "none",
6437 system_shared_libs: [],
6438 }`)
6439}
6440
Paul Duffine52e66f2020-03-30 17:54:29 +01006441func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006442 testApexError(t, "\"otherapex\" is not a valid module name", `
6443 apex {
6444 name: "myapex",
6445 key: "myapex.key",
6446 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006447 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006448 }
6449
6450 apex_key {
6451 name: "myapex.key",
6452 public_key: "testkey.avbpubkey",
6453 private_key: "testkey.pem",
6454 }
6455
6456 cc_library {
6457 name: "libfoo",
6458 stl: "none",
6459 system_shared_libs: [],
6460 apex_available: ["otherapex"],
6461 }`)
6462
Paul Duffine52e66f2020-03-30 17:54:29 +01006463 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006464 apex {
6465 name: "myapex",
6466 key: "myapex.key",
6467 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006468 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006469 }
6470
6471 apex_key {
6472 name: "myapex.key",
6473 public_key: "testkey.avbpubkey",
6474 private_key: "testkey.pem",
6475 }
6476
6477 cc_library {
6478 name: "libfoo",
6479 stl: "none",
6480 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006481 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006482 apex_available: ["myapex"],
6483 }
6484
6485 cc_library {
6486 name: "libbar",
6487 stl: "none",
6488 system_shared_libs: [],
6489 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006490 }
6491
6492 cc_library {
6493 name: "libbaz",
6494 stl: "none",
6495 system_shared_libs: [],
6496 stubs: {
6497 versions: ["10", "20", "30"],
6498 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006499 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006500}
Jiyong Park127b40b2019-09-30 16:04:35 +09006501
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006502func TestApexAvailable_ApexAvailableNameWithVersionCodeError(t *testing.T) {
6503 t.Run("negative variant_version produces error", func(t *testing.T) {
6504 testApexError(t, "expected an integer between 0-9; got -1", `
6505 apex {
6506 name: "myapex",
6507 key: "myapex.key",
6508 apex_available_name: "com.android.foo",
6509 variant_version: "-1",
6510 updatable: false,
6511 }
6512 apex_key {
6513 name: "myapex.key",
6514 public_key: "testkey.avbpubkey",
6515 private_key: "testkey.pem",
6516 }
6517 `)
6518 })
6519
6520 t.Run("variant_version greater than 9 produces error", func(t *testing.T) {
6521 testApexError(t, "expected an integer between 0-9; got 10", `
6522 apex {
6523 name: "myapex",
6524 key: "myapex.key",
6525 apex_available_name: "com.android.foo",
6526 variant_version: "10",
6527 updatable: false,
6528 }
6529 apex_key {
6530 name: "myapex.key",
6531 public_key: "testkey.avbpubkey",
6532 private_key: "testkey.pem",
6533 }
6534 `)
6535 })
6536}
6537
6538func TestApexAvailable_ApexAvailableNameWithVersionCode(t *testing.T) {
6539 context := android.GroupFixturePreparers(
6540 android.PrepareForIntegrationTestWithAndroid,
6541 PrepareForTestWithApexBuildComponents,
6542 android.FixtureMergeMockFs(android.MockFS{
6543 "system/sepolicy/apex/foo-file_contexts": nil,
6544 "system/sepolicy/apex/bar-file_contexts": nil,
6545 }),
6546 )
6547 result := context.RunTestWithBp(t, `
6548 apex {
6549 name: "foo",
6550 key: "myapex.key",
6551 apex_available_name: "com.android.foo",
6552 variant_version: "0",
6553 updatable: false,
6554 }
6555 apex {
6556 name: "bar",
6557 key: "myapex.key",
6558 apex_available_name: "com.android.foo",
6559 variant_version: "3",
6560 updatable: false,
6561 }
6562 apex_key {
6563 name: "myapex.key",
6564 public_key: "testkey.avbpubkey",
6565 private_key: "testkey.pem",
6566 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006567 override_apex {
6568 name: "myoverrideapex",
6569 base: "bar",
6570 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006571 `)
6572
Jooyung Hana0503a52023-08-23 13:12:50 +09006573 fooManifestRule := result.ModuleForTests("foo", "android_common_foo").Rule("apexManifestRule")
Alyssa Ketpreechasawat3a6eced2024-08-22 15:09:16 +00006574 fooExpectedDefaultVersion := testDefaultUpdatableModuleVersion
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006575 fooActualDefaultVersion := fooManifestRule.Args["default_version"]
6576 if fooActualDefaultVersion != fooExpectedDefaultVersion {
6577 t.Errorf("expected to find defaultVersion %q; got %q", fooExpectedDefaultVersion, fooActualDefaultVersion)
6578 }
6579
Jooyung Hana0503a52023-08-23 13:12:50 +09006580 barManifestRule := result.ModuleForTests("bar", "android_common_bar").Rule("apexManifestRule")
Alyssa Ketpreechasawat3a6eced2024-08-22 15:09:16 +00006581 defaultVersionInt, _ := strconv.Atoi(testDefaultUpdatableModuleVersion)
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006582 barExpectedDefaultVersion := fmt.Sprint(defaultVersionInt + 3)
6583 barActualDefaultVersion := barManifestRule.Args["default_version"]
6584 if barActualDefaultVersion != barExpectedDefaultVersion {
6585 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6586 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006587
Spandan Das50801e22024-05-13 18:29:45 +00006588 overrideBarManifestRule := result.ModuleForTests("bar", "android_common_myoverrideapex_myoverrideapex").Rule("apexManifestRule")
Sam Delmerico419f9a32023-07-21 12:00:13 -04006589 overrideBarActualDefaultVersion := overrideBarManifestRule.Args["default_version"]
6590 if overrideBarActualDefaultVersion != barExpectedDefaultVersion {
6591 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6592 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006593}
6594
Sam Delmericoca816532023-06-02 14:09:50 -04006595func TestApexAvailable_ApexAvailableName(t *testing.T) {
6596 t.Run("using name of apex that sets apex_available_name is not allowed", func(t *testing.T) {
6597 testApexError(t, "Consider adding \"myapex\" to 'apex_available' property of \"AppFoo\"", `
6598 apex {
6599 name: "myapex_sminus",
6600 key: "myapex.key",
6601 apps: ["AppFoo"],
6602 apex_available_name: "myapex",
6603 updatable: false,
6604 }
6605 apex {
6606 name: "myapex",
6607 key: "myapex.key",
6608 apps: ["AppFoo"],
6609 updatable: false,
6610 }
6611 apex_key {
6612 name: "myapex.key",
6613 public_key: "testkey.avbpubkey",
6614 private_key: "testkey.pem",
6615 }
6616 android_app {
6617 name: "AppFoo",
6618 srcs: ["foo/bar/MyClass.java"],
6619 sdk_version: "none",
6620 system_modules: "none",
6621 apex_available: [ "myapex_sminus" ],
6622 }`,
6623 android.FixtureMergeMockFs(android.MockFS{
6624 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6625 }),
6626 )
6627 })
6628
6629 t.Run("apex_available_name allows module to be used in two different apexes", func(t *testing.T) {
6630 testApex(t, `
6631 apex {
6632 name: "myapex_sminus",
6633 key: "myapex.key",
6634 apps: ["AppFoo"],
6635 apex_available_name: "myapex",
6636 updatable: false,
6637 }
6638 apex {
6639 name: "myapex",
6640 key: "myapex.key",
6641 apps: ["AppFoo"],
6642 updatable: false,
6643 }
6644 apex_key {
6645 name: "myapex.key",
6646 public_key: "testkey.avbpubkey",
6647 private_key: "testkey.pem",
6648 }
6649 android_app {
6650 name: "AppFoo",
6651 srcs: ["foo/bar/MyClass.java"],
6652 sdk_version: "none",
6653 system_modules: "none",
6654 apex_available: [ "myapex" ],
6655 }`,
6656 android.FixtureMergeMockFs(android.MockFS{
6657 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6658 }),
6659 )
6660 })
6661
6662 t.Run("override_apexes work with apex_available_name", func(t *testing.T) {
6663 testApex(t, `
6664 override_apex {
6665 name: "myoverrideapex_sminus",
6666 base: "myapex_sminus",
6667 key: "myapex.key",
6668 apps: ["AppFooOverride"],
6669 }
6670 override_apex {
6671 name: "myoverrideapex",
6672 base: "myapex",
6673 key: "myapex.key",
6674 apps: ["AppFooOverride"],
6675 }
6676 apex {
6677 name: "myapex_sminus",
6678 key: "myapex.key",
6679 apps: ["AppFoo"],
6680 apex_available_name: "myapex",
6681 updatable: false,
6682 }
6683 apex {
6684 name: "myapex",
6685 key: "myapex.key",
6686 apps: ["AppFoo"],
6687 updatable: false,
6688 }
6689 apex_key {
6690 name: "myapex.key",
6691 public_key: "testkey.avbpubkey",
6692 private_key: "testkey.pem",
6693 }
6694 android_app {
6695 name: "AppFooOverride",
6696 srcs: ["foo/bar/MyClass.java"],
6697 sdk_version: "none",
6698 system_modules: "none",
6699 apex_available: [ "myapex" ],
6700 }
6701 android_app {
6702 name: "AppFoo",
6703 srcs: ["foo/bar/MyClass.java"],
6704 sdk_version: "none",
6705 system_modules: "none",
6706 apex_available: [ "myapex" ],
6707 }`,
6708 android.FixtureMergeMockFs(android.MockFS{
6709 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6710 }),
6711 )
6712 })
6713}
6714
6715func TestApexAvailable_ApexAvailableNameWithOverrides(t *testing.T) {
6716 context := android.GroupFixturePreparers(
6717 android.PrepareForIntegrationTestWithAndroid,
6718 PrepareForTestWithApexBuildComponents,
6719 java.PrepareForTestWithDexpreopt,
6720 android.FixtureMergeMockFs(android.MockFS{
6721 "system/sepolicy/apex/myapex-file_contexts": nil,
6722 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6723 }),
6724 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6725 variables.BuildId = proptools.StringPtr("buildid")
6726 }),
6727 )
6728 context.RunTestWithBp(t, `
6729 override_apex {
6730 name: "myoverrideapex_sminus",
6731 base: "myapex_sminus",
6732 }
6733 override_apex {
6734 name: "myoverrideapex",
6735 base: "myapex",
6736 }
6737 apex {
6738 name: "myapex",
6739 key: "myapex.key",
6740 apps: ["AppFoo"],
6741 updatable: false,
6742 }
6743 apex {
6744 name: "myapex_sminus",
6745 apex_available_name: "myapex",
6746 key: "myapex.key",
6747 apps: ["AppFoo_sminus"],
6748 updatable: false,
6749 }
6750 apex_key {
6751 name: "myapex.key",
6752 public_key: "testkey.avbpubkey",
6753 private_key: "testkey.pem",
6754 }
6755 android_app {
6756 name: "AppFoo",
6757 srcs: ["foo/bar/MyClass.java"],
6758 sdk_version: "none",
6759 system_modules: "none",
6760 apex_available: [ "myapex" ],
6761 }
6762 android_app {
6763 name: "AppFoo_sminus",
6764 srcs: ["foo/bar/MyClass.java"],
6765 sdk_version: "none",
6766 min_sdk_version: "29",
6767 system_modules: "none",
6768 apex_available: [ "myapex" ],
6769 }`)
6770}
6771
Jiyong Park89e850a2020-04-07 16:37:39 +09006772func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006773 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006774 apex {
6775 name: "myapex",
6776 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006777 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006778 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006779 }
6780
6781 apex_key {
6782 name: "myapex.key",
6783 public_key: "testkey.avbpubkey",
6784 private_key: "testkey.pem",
6785 }
6786
6787 cc_library {
6788 name: "libfoo",
6789 stl: "none",
6790 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006791 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006792 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006793 }
6794
6795 cc_library {
6796 name: "libfoo2",
6797 stl: "none",
6798 system_shared_libs: [],
6799 shared_libs: ["libbaz"],
6800 apex_available: ["//apex_available:platform"],
6801 }
6802
6803 cc_library {
6804 name: "libbar",
6805 stl: "none",
6806 system_shared_libs: [],
6807 apex_available: ["myapex"],
6808 }
6809
6810 cc_library {
6811 name: "libbaz",
6812 stl: "none",
6813 system_shared_libs: [],
6814 apex_available: ["myapex"],
6815 stubs: {
6816 versions: ["1"],
6817 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006818 }`)
6819
Jiyong Park89e850a2020-04-07 16:37:39 +09006820 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6821 // because it depends on libbar which isn't available to platform
6822 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6823 if libfoo.NotAvailableForPlatform() != true {
6824 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6825 }
6826
6827 // libfoo2 however can be available to platform because it depends on libbaz which provides
6828 // stubs
6829 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6830 if libfoo2.NotAvailableForPlatform() == true {
6831 t.Errorf("%q should be available to platform", libfoo2.String())
6832 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006833}
Jiyong Parka90ca002019-10-07 15:47:24 +09006834
Paul Duffine52e66f2020-03-30 17:54:29 +01006835func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006836 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006837 apex {
6838 name: "myapex",
6839 key: "myapex.key",
6840 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006841 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006842 }
6843
6844 apex_key {
6845 name: "myapex.key",
6846 public_key: "testkey.avbpubkey",
6847 private_key: "testkey.pem",
6848 }
6849
6850 cc_library {
6851 name: "libfoo",
6852 stl: "none",
6853 system_shared_libs: [],
6854 apex_available: ["myapex"],
6855 static: {
6856 apex_available: ["//apex_available:platform"],
6857 },
6858 }`)
6859
Jiyong Park89e850a2020-04-07 16:37:39 +09006860 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6861 if libfooShared.NotAvailableForPlatform() != true {
6862 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6863 }
6864 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6865 if libfooStatic.NotAvailableForPlatform() != false {
6866 t.Errorf("%q should be available to platform", libfooStatic.String())
6867 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006868}
6869
Jooyung Han9a419e22024-08-16 17:14:21 +09006870func TestApexAvailable_PrefixMatch(t *testing.T) {
6871
6872 for _, tc := range []struct {
6873 name string
6874 apexAvailable string
6875 expectedError string
6876 }{
6877 {
6878 name: "prefix matches correctly",
6879 apexAvailable: "com.foo.*",
6880 },
6881 {
6882 name: "prefix doesn't match",
6883 apexAvailable: "com.bar.*",
6884 expectedError: `Consider .* "com.foo\.\*"`,
6885 },
6886 {
6887 name: "short prefix",
6888 apexAvailable: "com.*",
6889 expectedError: "requires two or more components",
6890 },
6891 {
6892 name: "wildcard not in the end",
6893 apexAvailable: "com.*.foo",
6894 expectedError: "should end with .*",
6895 },
6896 {
6897 name: "wildcard in the middle",
6898 apexAvailable: "com.foo*.*",
6899 expectedError: "not allowed in the middle",
6900 },
6901 {
6902 name: "hint with prefix pattern",
6903 apexAvailable: "//apex_available:platform",
6904 expectedError: "Consider adding \"com.foo.bar\" or \"com.foo.*\"",
6905 },
6906 } {
6907 t.Run(tc.name, func(t *testing.T) {
6908 errorHandler := android.FixtureExpectsNoErrors
6909 if tc.expectedError != "" {
6910 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(tc.expectedError)
6911 }
6912 context := android.GroupFixturePreparers(
6913 prepareForApexTest,
6914 android.FixtureMergeMockFs(android.MockFS{
6915 "system/sepolicy/apex/com.foo.bar-file_contexts": nil,
6916 }),
6917 ).ExtendWithErrorHandler(errorHandler)
6918
6919 context.RunTestWithBp(t, `
6920 apex {
6921 name: "com.foo.bar",
6922 key: "myapex.key",
6923 native_shared_libs: ["libfoo"],
6924 updatable: false,
6925 }
6926
6927 apex_key {
6928 name: "myapex.key",
6929 public_key: "testkey.avbpubkey",
6930 private_key: "testkey.pem",
6931 }
6932
6933 cc_library {
6934 name: "libfoo",
6935 stl: "none",
6936 system_shared_libs: [],
6937 apex_available: ["`+tc.apexAvailable+`"],
6938 }`)
6939 })
6940 }
6941 testApexError(t, `Consider adding "com.foo" to`, `
6942 apex {
6943 name: "com.foo", // too short for a partner apex
6944 key: "myapex.key",
6945 native_shared_libs: ["libfoo"],
6946 updatable: false,
6947 }
6948
6949 apex_key {
6950 name: "myapex.key",
6951 public_key: "testkey.avbpubkey",
6952 private_key: "testkey.pem",
6953 }
6954
6955 cc_library {
6956 name: "libfoo",
6957 stl: "none",
6958 system_shared_libs: [],
6959 }
6960 `)
6961}
6962
Jiyong Park5d790c32019-11-15 18:40:32 +09006963func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006964 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006965 apex {
6966 name: "myapex",
6967 key: "myapex.key",
6968 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006969 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006970 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006971 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006972 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006973 }
6974
6975 override_apex {
6976 name: "override_myapex",
6977 base: "myapex",
6978 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006979 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006980 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006981 overrides: ["unknownapex"],
Jesse Melhuishec60e252024-03-29 19:08:20 +00006982 compile_multilib: "first",
6983 multilib: {
6984 lib32: {
6985 native_shared_libs: ["mylib32"],
6986 },
6987 lib64: {
6988 native_shared_libs: ["mylib64"],
6989 },
6990 },
Baligh Uddin004d7172020-02-19 21:29:28 -08006991 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006992 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006993 key: "mynewapex.key",
6994 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006995 }
6996
6997 apex_key {
6998 name: "myapex.key",
6999 public_key: "testkey.avbpubkey",
7000 private_key: "testkey.pem",
7001 }
7002
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07007003 apex_key {
7004 name: "mynewapex.key",
7005 public_key: "testkey2.avbpubkey",
7006 private_key: "testkey2.pem",
7007 }
7008
7009 android_app_certificate {
7010 name: "myapex.certificate",
7011 certificate: "testkey",
7012 }
7013
Jiyong Park5d790c32019-11-15 18:40:32 +09007014 android_app {
7015 name: "app",
7016 srcs: ["foo/bar/MyClass.java"],
7017 package_name: "foo",
7018 sdk_version: "none",
7019 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007020 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09007021 }
7022
7023 override_android_app {
7024 name: "override_app",
7025 base: "app",
7026 package_name: "bar",
7027 }
markchien7c803b82021-08-26 22:10:06 +08007028
7029 bpf {
7030 name: "bpf",
7031 srcs: ["bpf.c"],
7032 }
7033
7034 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08007035 name: "overrideBpf",
7036 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08007037 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07007038
7039 prebuilt_etc {
7040 name: "myetc",
7041 src: "myprebuilt",
7042 }
7043
7044 prebuilt_etc {
7045 name: "override_myetc",
7046 src: "override_myprebuilt",
7047 }
Jesse Melhuishec60e252024-03-29 19:08:20 +00007048
7049 cc_library {
7050 name: "mylib32",
7051 apex_available: [ "myapex" ],
7052 }
7053
7054 cc_library {
7055 name: "mylib64",
7056 apex_available: [ "myapex" ],
7057 }
Jiyong Park20bacab2020-03-03 11:45:41 +09007058 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09007059
Jooyung Hana0503a52023-08-23 13:12:50 +09007060 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(android.OverridableModule)
Spandan Das50801e22024-05-13 18:29:45 +00007061 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex").Module().(android.OverridableModule)
Jiyong Park317645e2019-12-05 13:20:58 +09007062 if originalVariant.GetOverriddenBy() != "" {
7063 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
7064 }
7065 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
7066 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
7067 }
7068
Spandan Das50801e22024-05-13 18:29:45 +00007069 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex")
Jiyong Park5d790c32019-11-15 18:40:32 +09007070 apexRule := module.Rule("apexRule")
7071 copyCmds := apexRule.Args["copy_commands"]
7072
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007073 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
7074 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007075
markchien7c803b82021-08-26 22:10:06 +08007076 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08007077 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08007078
Daniel Norman5a3ce132021-08-26 15:44:43 -07007079 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
7080 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
7081
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007082 apexBundle := module.Module().(*apexBundle)
7083 name := apexBundle.Name()
7084 if name != "override_myapex" {
7085 t.Errorf("name should be \"override_myapex\", but was %q", name)
7086 }
7087
Baligh Uddin004d7172020-02-19 21:29:28 -08007088 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
7089 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
7090 }
7091
Jiyong Park20bacab2020-03-03 11:45:41 +09007092 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07007093 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07007094 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
7095
7096 signApkRule := module.Rule("signapk")
7097 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09007098
Colin Crossaa255532020-07-03 13:18:24 -07007099 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007100 var builder strings.Builder
7101 data.Custom(&builder, name, "TARGET_", "", data)
7102 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007103 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
7104 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007105 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08007106 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007107 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08007108 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09007109 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007110 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09007111}
7112
Albert Martineefabcf2022-03-21 20:11:16 +00007113func TestMinSdkVersionOverride(t *testing.T) {
7114 // Override from 29 to 31
7115 minSdkOverride31 := "31"
7116 ctx := testApex(t, `
7117 apex {
7118 name: "myapex",
7119 key: "myapex.key",
7120 native_shared_libs: ["mylib"],
7121 updatable: true,
7122 min_sdk_version: "29"
7123 }
7124
7125 override_apex {
7126 name: "override_myapex",
7127 base: "myapex",
7128 logging_parent: "com.foo.bar",
7129 package_name: "test.overridden.package"
7130 }
7131
7132 apex_key {
7133 name: "myapex.key",
7134 public_key: "testkey.avbpubkey",
7135 private_key: "testkey.pem",
7136 }
7137
7138 cc_library {
7139 name: "mylib",
7140 srcs: ["mylib.cpp"],
7141 runtime_libs: ["libbar"],
7142 system_shared_libs: [],
7143 stl: "none",
7144 apex_available: [ "myapex" ],
7145 min_sdk_version: "apex_inherit"
7146 }
7147
7148 cc_library {
7149 name: "libbar",
7150 srcs: ["mylib.cpp"],
7151 system_shared_libs: [],
7152 stl: "none",
7153 apex_available: [ "myapex" ],
7154 min_sdk_version: "apex_inherit"
7155 }
7156
7157 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
7158
Jooyung Hana0503a52023-08-23 13:12:50 +09007159 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007160 copyCmds := apexRule.Args["copy_commands"]
7161
7162 // Ensure that direct non-stubs dep is always included
7163 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7164
7165 // Ensure that runtime_libs dep in included
7166 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7167
7168 // Ensure libraries target overridden min_sdk_version value
7169 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7170}
7171
7172func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
7173 // Attempt to override from 31 to 29, should be a NOOP
7174 minSdkOverride29 := "29"
7175 ctx := testApex(t, `
7176 apex {
7177 name: "myapex",
7178 key: "myapex.key",
7179 native_shared_libs: ["mylib"],
7180 updatable: true,
7181 min_sdk_version: "31"
7182 }
7183
7184 override_apex {
7185 name: "override_myapex",
7186 base: "myapex",
7187 logging_parent: "com.foo.bar",
7188 package_name: "test.overridden.package"
7189 }
7190
7191 apex_key {
7192 name: "myapex.key",
7193 public_key: "testkey.avbpubkey",
7194 private_key: "testkey.pem",
7195 }
7196
7197 cc_library {
7198 name: "mylib",
7199 srcs: ["mylib.cpp"],
7200 runtime_libs: ["libbar"],
7201 system_shared_libs: [],
7202 stl: "none",
7203 apex_available: [ "myapex" ],
7204 min_sdk_version: "apex_inherit"
7205 }
7206
7207 cc_library {
7208 name: "libbar",
7209 srcs: ["mylib.cpp"],
7210 system_shared_libs: [],
7211 stl: "none",
7212 apex_available: [ "myapex" ],
7213 min_sdk_version: "apex_inherit"
7214 }
7215
7216 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
7217
Jooyung Hana0503a52023-08-23 13:12:50 +09007218 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007219 copyCmds := apexRule.Args["copy_commands"]
7220
7221 // Ensure that direct non-stubs dep is always included
7222 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7223
7224 // Ensure that runtime_libs dep in included
7225 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7226
7227 // Ensure libraries target the original min_sdk_version value rather than the overridden
7228 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7229}
7230
Jooyung Han214bf372019-11-12 13:03:50 +09007231func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007232 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09007233 apex {
7234 name: "myapex",
7235 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007236 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09007237 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09007238 }
7239
7240 apex_key {
7241 name: "myapex.key",
7242 public_key: "testkey.avbpubkey",
7243 private_key: "testkey.pem",
7244 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007245
7246 cc_library {
7247 name: "mylib",
7248 srcs: ["mylib.cpp"],
7249 stl: "libc++",
7250 system_shared_libs: [],
7251 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09007252 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007253 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007254 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09007255
Jooyung Hana0503a52023-08-23 13:12:50 +09007256 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +09007257 args := module.Rule("apexRule").Args
7258 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00007259 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007260
7261 // The copies of the libraries in the apex should have one more dependency than
7262 // the ones outside the apex, namely the unwinder. Ideally we should check
7263 // the dependency names directly here but for some reason the names are blank in
7264 // this test.
7265 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07007266 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007267 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
7268 if len(apexImplicits) != len(nonApexImplicits)+1 {
7269 t.Errorf("%q missing unwinder dep", lib)
7270 }
7271 }
Jooyung Han214bf372019-11-12 13:03:50 +09007272}
7273
Paul Duffine05480a2021-03-08 15:07:14 +00007274var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01007275 "api/current.txt": nil,
7276 "api/removed.txt": nil,
7277 "api/system-current.txt": nil,
7278 "api/system-removed.txt": nil,
7279 "api/test-current.txt": nil,
7280 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01007281
Anton Hanssondff2c782020-12-21 17:10:01 +00007282 "100/public/api/foo.txt": nil,
7283 "100/public/api/foo-removed.txt": nil,
7284 "100/system/api/foo.txt": nil,
7285 "100/system/api/foo-removed.txt": nil,
7286
Paul Duffineedc5d52020-06-12 17:46:39 +01007287 // For java_sdk_library_import
7288 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01007289}
7290
Jooyung Han58f26ab2019-12-18 15:34:32 +09007291func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007292 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09007293 apex {
7294 name: "myapex",
7295 key: "myapex.key",
7296 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007297 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09007298 }
7299
7300 apex_key {
7301 name: "myapex.key",
7302 public_key: "testkey.avbpubkey",
7303 private_key: "testkey.pem",
7304 }
7305
7306 java_sdk_library {
7307 name: "foo",
7308 srcs: ["a.java"],
7309 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007310 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09007311 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007312
7313 prebuilt_apis {
7314 name: "sdk",
7315 api_dirs: ["100"],
7316 }
Paul Duffin9b879592020-05-26 13:21:35 +01007317 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09007318
7319 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007320 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09007321 "javalib/foo.jar",
7322 "etc/permissions/foo.xml",
7323 })
7324 // Permission XML should point to the activated path of impl jar of java_sdk_library
Paul Duffin1816cde2024-04-10 10:58:21 +01007325 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Output("foo.xml")
7326 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7327 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
Jooyung Han58f26ab2019-12-18 15:34:32 +09007328}
7329
Spandan Das3ee19692024-06-19 04:47:40 +00007330func TestJavaSDKLibraryOverrideApexes(t *testing.T) {
7331 ctx := testApex(t, `
7332 override_apex {
7333 name: "mycompanyapex",
7334 base: "myapex",
7335 }
7336 apex {
7337 name: "myapex",
7338 key: "myapex.key",
7339 java_libs: ["foo"],
7340 updatable: false,
7341 }
7342
7343 apex_key {
7344 name: "myapex.key",
7345 public_key: "testkey.avbpubkey",
7346 private_key: "testkey.pem",
7347 }
7348
7349 java_sdk_library {
7350 name: "foo",
7351 srcs: ["a.java"],
7352 api_packages: ["foo"],
7353 apex_available: [ "myapex" ],
7354 }
7355
7356 prebuilt_apis {
7357 name: "sdk",
7358 api_dirs: ["100"],
7359 }
7360 `, withFiles(filesForSdkLibrary))
7361
7362 // Permission XML should point to the activated path of impl jar of java_sdk_library.
7363 // Since override variants (com.mycompany.android.foo) are installed in the same package as the overridden variant
7364 // (com.android.foo), the filepath should not contain override apex name.
7365 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_mycompanyapex").Output("foo.xml")
7366 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7367 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
7368}
7369
Paul Duffin9b879592020-05-26 13:21:35 +01007370func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007371 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007372 apex {
7373 name: "myapex",
7374 key: "myapex.key",
7375 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007376 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007377 }
7378
7379 apex_key {
7380 name: "myapex.key",
7381 public_key: "testkey.avbpubkey",
7382 private_key: "testkey.pem",
7383 }
7384
7385 java_sdk_library {
7386 name: "foo",
7387 srcs: ["a.java"],
7388 api_packages: ["foo"],
7389 apex_available: ["myapex"],
7390 sdk_version: "none",
7391 system_modules: "none",
7392 }
7393
7394 java_library {
7395 name: "bar",
7396 srcs: ["a.java"],
Jihoon Kang28c96572024-09-11 23:44:44 +00007397 libs: ["foo.impl"],
Paul Duffin9b879592020-05-26 13:21:35 +01007398 apex_available: ["myapex"],
7399 sdk_version: "none",
7400 system_modules: "none",
7401 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007402
7403 prebuilt_apis {
7404 name: "sdk",
7405 api_dirs: ["100"],
7406 }
Paul Duffin9b879592020-05-26 13:21:35 +01007407 `, withFiles(filesForSdkLibrary))
7408
7409 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007410 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007411 "javalib/bar.jar",
7412 "javalib/foo.jar",
7413 "etc/permissions/foo.xml",
7414 })
7415
7416 // The bar library should depend on the implementation jar.
7417 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kanga3a05462024-04-05 00:36:44 +00007418 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007419 t.Errorf("expected %q, found %#q", expected, actual)
7420 }
7421}
7422
7423func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007424 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007425 apex {
7426 name: "myapex",
7427 key: "myapex.key",
7428 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007429 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007430 }
7431
7432 apex_key {
7433 name: "myapex.key",
7434 public_key: "testkey.avbpubkey",
7435 private_key: "testkey.pem",
7436 }
7437
7438 java_sdk_library {
7439 name: "foo",
7440 srcs: ["a.java"],
7441 api_packages: ["foo"],
7442 apex_available: ["myapex"],
7443 sdk_version: "none",
7444 system_modules: "none",
7445 }
7446
7447 java_library {
7448 name: "bar",
7449 srcs: ["a.java"],
Jihoon Kang28c96572024-09-11 23:44:44 +00007450 libs: ["foo.stubs"],
Paul Duffin9b879592020-05-26 13:21:35 +01007451 sdk_version: "none",
7452 system_modules: "none",
7453 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007454
7455 prebuilt_apis {
7456 name: "sdk",
7457 api_dirs: ["100"],
7458 }
Paul Duffin9b879592020-05-26 13:21:35 +01007459 `, withFiles(filesForSdkLibrary))
7460
7461 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007462 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007463 "javalib/foo.jar",
7464 "etc/permissions/foo.xml",
7465 })
7466
7467 // The bar library should depend on the stubs jar.
7468 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007469 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007470 t.Errorf("expected %q, found %#q", expected, actual)
7471 }
7472}
7473
Paul Duffineedc5d52020-06-12 17:46:39 +01007474func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007475 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00007476 prebuilt_apis {
7477 name: "sdk",
7478 api_dirs: ["100"],
7479 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01007480 withFiles(map[string][]byte{
7481 "apex/a.java": nil,
7482 "apex/apex_manifest.json": nil,
7483 "apex/Android.bp": []byte(`
7484 package {
7485 default_visibility: ["//visibility:private"],
7486 }
7487
7488 apex {
7489 name: "myapex",
7490 key: "myapex.key",
7491 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007492 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007493 }
7494
7495 apex_key {
7496 name: "myapex.key",
7497 public_key: "testkey.avbpubkey",
7498 private_key: "testkey.pem",
7499 }
7500
7501 java_library {
7502 name: "bar",
7503 srcs: ["a.java"],
Jihoon Kang28c96572024-09-11 23:44:44 +00007504 libs: ["foo.impl"],
Paul Duffineedc5d52020-06-12 17:46:39 +01007505 apex_available: ["myapex"],
7506 sdk_version: "none",
7507 system_modules: "none",
7508 }
7509`),
7510 "source/a.java": nil,
7511 "source/api/current.txt": nil,
7512 "source/api/removed.txt": nil,
7513 "source/Android.bp": []byte(`
7514 package {
7515 default_visibility: ["//visibility:private"],
7516 }
7517
7518 java_sdk_library {
7519 name: "foo",
7520 visibility: ["//apex"],
7521 srcs: ["a.java"],
7522 api_packages: ["foo"],
7523 apex_available: ["myapex"],
7524 sdk_version: "none",
7525 system_modules: "none",
7526 public: {
7527 enabled: true,
7528 },
7529 }
7530`),
7531 "prebuilt/a.jar": nil,
7532 "prebuilt/Android.bp": []byte(`
7533 package {
7534 default_visibility: ["//visibility:private"],
7535 }
7536
7537 java_sdk_library_import {
7538 name: "foo",
7539 visibility: ["//apex", "//source"],
7540 apex_available: ["myapex"],
7541 prefer: true,
7542 public: {
7543 jars: ["a.jar"],
7544 },
7545 }
7546`),
Anton Hanssondff2c782020-12-21 17:10:01 +00007547 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01007548 )
7549
7550 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007551 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffineedc5d52020-06-12 17:46:39 +01007552 "javalib/bar.jar",
7553 "javalib/foo.jar",
7554 "etc/permissions/foo.xml",
7555 })
7556
7557 // The bar library should depend on the implementation jar.
7558 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kanga3a05462024-04-05 00:36:44 +00007559 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01007560 t.Errorf("expected %q, found %#q", expected, actual)
7561 }
7562}
7563
7564func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
7565 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
7566 apex {
7567 name: "myapex",
7568 key: "myapex.key",
7569 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007570 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007571 }
7572
7573 apex_key {
7574 name: "myapex.key",
7575 public_key: "testkey.avbpubkey",
7576 private_key: "testkey.pem",
7577 }
7578
7579 java_sdk_library_import {
7580 name: "foo",
7581 apex_available: ["myapex"],
7582 prefer: true,
7583 public: {
7584 jars: ["a.jar"],
7585 },
7586 }
7587
7588 `, withFiles(filesForSdkLibrary))
7589}
7590
atrost6e126252020-01-27 17:01:16 +00007591func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007592 result := android.GroupFixturePreparers(
7593 prepareForApexTest,
7594 java.PrepareForTestWithPlatformCompatConfig,
7595 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007596 apex {
7597 name: "myapex",
7598 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007599 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007600 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007601 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007602 }
7603
7604 apex_key {
7605 name: "myapex.key",
7606 public_key: "testkey.avbpubkey",
7607 private_key: "testkey.pem",
7608 }
7609
7610 platform_compat_config {
7611 name: "myjar-platform-compat-config",
7612 src: ":myjar",
7613 }
7614
7615 java_library {
7616 name: "myjar",
7617 srcs: ["foo/bar/MyClass.java"],
7618 sdk_version: "none",
7619 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007620 apex_available: [ "myapex" ],
7621 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007622
7623 // Make sure that a preferred prebuilt does not affect the apex contents.
7624 prebuilt_platform_compat_config {
7625 name: "myjar-platform-compat-config",
7626 metadata: "compat-config/metadata.xml",
7627 prefer: true,
7628 }
atrost6e126252020-01-27 17:01:16 +00007629 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007630 ctx := result.TestContext
Jooyung Hana0503a52023-08-23 13:12:50 +09007631 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
atrost6e126252020-01-27 17:01:16 +00007632 "etc/compatconfig/myjar-platform-compat-config.xml",
7633 "javalib/myjar.jar",
7634 })
7635}
7636
Jooyung Han862c0d62022-12-21 10:15:37 +09007637func TestNoDupeApexFiles(t *testing.T) {
7638 android.GroupFixturePreparers(
7639 android.PrepareForTestWithAndroidBuildComponents,
7640 PrepareForTestWithApexBuildComponents,
7641 prepareForTestWithMyapex,
7642 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7643 ).
7644 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7645 RunTestWithBp(t, `
7646 apex {
7647 name: "myapex",
7648 key: "myapex.key",
7649 prebuilts: ["foo", "bar"],
7650 updatable: false,
7651 }
7652
7653 apex_key {
7654 name: "myapex.key",
7655 public_key: "testkey.avbpubkey",
7656 private_key: "testkey.pem",
7657 }
7658
7659 prebuilt_etc {
7660 name: "foo",
7661 src: "myprebuilt",
7662 filename_from_src: true,
7663 }
7664
7665 prebuilt_etc {
7666 name: "bar",
7667 src: "myprebuilt",
7668 filename_from_src: true,
7669 }
7670 `)
7671}
7672
Jooyung Hana8bd72a2023-11-02 11:56:48 +09007673func TestApexUnwantedTransitiveDeps(t *testing.T) {
7674 bp := `
7675 apex {
7676 name: "myapex",
7677 key: "myapex.key",
7678 native_shared_libs: ["libfoo"],
7679 updatable: false,
7680 unwanted_transitive_deps: ["libbar"],
7681 }
7682
7683 apex_key {
7684 name: "myapex.key",
7685 public_key: "testkey.avbpubkey",
7686 private_key: "testkey.pem",
7687 }
7688
7689 cc_library {
7690 name: "libfoo",
7691 srcs: ["foo.cpp"],
7692 shared_libs: ["libbar"],
7693 apex_available: ["myapex"],
7694 }
7695
7696 cc_library {
7697 name: "libbar",
7698 srcs: ["bar.cpp"],
7699 apex_available: ["myapex"],
7700 }`
7701 ctx := testApex(t, bp)
7702 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
7703 "*/libc++.so",
7704 "*/libfoo.so",
7705 // not libbar.so
7706 })
7707}
7708
Jiyong Park479321d2019-12-16 11:47:12 +09007709func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7710 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7711 apex {
7712 name: "myapex",
7713 key: "myapex.key",
7714 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007715 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007716 }
7717
7718 apex_key {
7719 name: "myapex.key",
7720 public_key: "testkey.avbpubkey",
7721 private_key: "testkey.pem",
7722 }
7723
7724 java_library {
7725 name: "myjar",
7726 srcs: ["foo/bar/MyClass.java"],
7727 sdk_version: "none",
7728 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007729 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007730 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007731 }
7732 `)
7733}
7734
Jiyong Park7afd1072019-12-30 16:56:33 +09007735func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007736 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007737 apex {
7738 name: "myapex",
7739 key: "myapex.key",
7740 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007741 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007742 }
7743
7744 apex_key {
7745 name: "myapex.key",
7746 public_key: "testkey.avbpubkey",
7747 private_key: "testkey.pem",
7748 }
7749
7750 cc_library {
7751 name: "mylib",
7752 srcs: ["mylib.cpp"],
7753 system_shared_libs: [],
7754 stl: "none",
7755 required: ["a", "b"],
7756 host_required: ["c", "d"],
7757 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007758 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007759 }
7760 `)
7761
Jooyung Hana0503a52023-08-23 13:12:50 +09007762 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007763 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007764 name := apexBundle.BaseModuleName()
7765 prefix := "TARGET_"
7766 var builder strings.Builder
7767 data.Custom(&builder, name, prefix, "", data)
7768 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007769 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 a b\n")
Sasha Smundakdcb61292022-12-08 10:41:33 -08007770 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7771 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007772}
7773
Jiyong Park7cd10e32020-01-14 09:22:18 +09007774func TestSymlinksFromApexToSystem(t *testing.T) {
7775 bp := `
7776 apex {
7777 name: "myapex",
7778 key: "myapex.key",
7779 native_shared_libs: ["mylib"],
7780 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007781 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007782 }
7783
Jiyong Park9d677202020-02-19 16:29:35 +09007784 apex {
7785 name: "myapex.updatable",
7786 key: "myapex.key",
7787 native_shared_libs: ["mylib"],
7788 java_libs: ["myjar"],
7789 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007790 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007791 }
7792
Jiyong Park7cd10e32020-01-14 09:22:18 +09007793 apex_key {
7794 name: "myapex.key",
7795 public_key: "testkey.avbpubkey",
7796 private_key: "testkey.pem",
7797 }
7798
7799 cc_library {
7800 name: "mylib",
7801 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007802 shared_libs: [
7803 "myotherlib",
7804 "myotherlib_ext",
7805 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007806 system_shared_libs: [],
7807 stl: "none",
7808 apex_available: [
7809 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007810 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007811 "//apex_available:platform",
7812 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007813 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007814 }
7815
7816 cc_library {
7817 name: "myotherlib",
7818 srcs: ["mylib.cpp"],
7819 system_shared_libs: [],
7820 stl: "none",
7821 apex_available: [
7822 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007823 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007824 "//apex_available:platform",
7825 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007826 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007827 }
7828
Jiyong Parkce243632023-02-17 18:22:25 +09007829 cc_library {
7830 name: "myotherlib_ext",
7831 srcs: ["mylib.cpp"],
7832 system_shared_libs: [],
7833 system_ext_specific: true,
7834 stl: "none",
7835 apex_available: [
7836 "myapex",
7837 "myapex.updatable",
7838 "//apex_available:platform",
7839 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007840 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007841 }
7842
Jiyong Park7cd10e32020-01-14 09:22:18 +09007843 java_library {
7844 name: "myjar",
7845 srcs: ["foo/bar/MyClass.java"],
7846 sdk_version: "none",
7847 system_modules: "none",
Jihoon Kang85bc1932024-07-01 17:04:46 +00007848 static_libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007849 apex_available: [
7850 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007851 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007852 "//apex_available:platform",
7853 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007854 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007855 }
7856
7857 java_library {
7858 name: "myotherjar",
7859 srcs: ["foo/bar/MyClass.java"],
7860 sdk_version: "none",
7861 system_modules: "none",
7862 apex_available: [
7863 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007864 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007865 "//apex_available:platform",
7866 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007867 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007868 }
7869 `
7870
7871 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7872 for _, f := range files {
7873 if f.path == file {
7874 if f.isLink {
7875 t.Errorf("%q is not a real file", file)
7876 }
7877 return
7878 }
7879 }
7880 t.Errorf("%q is not found", file)
7881 }
7882
Jiyong Parkce243632023-02-17 18:22:25 +09007883 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007884 for _, f := range files {
7885 if f.path == file {
7886 if !f.isLink {
7887 t.Errorf("%q is not a symlink", file)
7888 }
Jiyong Parkce243632023-02-17 18:22:25 +09007889 if f.src != target {
7890 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7891 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007892 return
7893 }
7894 }
7895 t.Errorf("%q is not found", file)
7896 }
7897
Jiyong Park9d677202020-02-19 16:29:35 +09007898 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7899 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007900 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana0503a52023-08-23 13:12:50 +09007901 files := getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007902 ensureRealfileExists(t, files, "javalib/myjar.jar")
7903 ensureRealfileExists(t, files, "lib64/mylib.so")
7904 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007905 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007906
Jooyung Hana0503a52023-08-23 13:12:50 +09007907 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007908 ensureRealfileExists(t, files, "javalib/myjar.jar")
7909 ensureRealfileExists(t, files, "lib64/mylib.so")
7910 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007911 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007912
7913 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007914 ctx = testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +09007915 files = getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007916 ensureRealfileExists(t, files, "javalib/myjar.jar")
7917 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007918 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7919 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007920
Jooyung Hana0503a52023-08-23 13:12:50 +09007921 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007922 ensureRealfileExists(t, files, "javalib/myjar.jar")
7923 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007924 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7925 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007926}
7927
Yo Chiange8128052020-07-23 20:09:18 +08007928func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007929 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007930 apex {
7931 name: "myapex",
7932 key: "myapex.key",
7933 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007934 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007935 }
7936
7937 apex_key {
7938 name: "myapex.key",
7939 public_key: "testkey.avbpubkey",
7940 private_key: "testkey.pem",
7941 }
7942
7943 cc_library_shared {
7944 name: "mylib",
7945 srcs: ["mylib.cpp"],
7946 shared_libs: ["myotherlib"],
7947 system_shared_libs: [],
7948 stl: "none",
7949 apex_available: [
7950 "myapex",
7951 "//apex_available:platform",
7952 ],
7953 }
7954
7955 cc_prebuilt_library_shared {
7956 name: "myotherlib",
7957 srcs: ["prebuilt.so"],
7958 system_shared_libs: [],
7959 stl: "none",
7960 apex_available: [
7961 "myapex",
7962 "//apex_available:platform",
7963 ],
7964 }
7965 `)
7966
Jooyung Hana0503a52023-08-23 13:12:50 +09007967 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007968 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007969 var builder strings.Builder
7970 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7971 androidMk := builder.String()
7972 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007973 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007974 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7975 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7976 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007977 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 myotherlib:64\n")
Yo Chiange8128052020-07-23 20:09:18 +08007978}
7979
Jooyung Han643adc42020-02-27 13:50:06 +09007980func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007981 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007982 apex {
7983 name: "myapex",
7984 key: "myapex.key",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007985 binaries: ["mybin"],
7986 jni_libs: ["mylib", "mylib3", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007987 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007988 }
7989
7990 apex_key {
7991 name: "myapex.key",
7992 public_key: "testkey.avbpubkey",
7993 private_key: "testkey.pem",
7994 }
7995
7996 cc_library {
7997 name: "mylib",
7998 srcs: ["mylib.cpp"],
7999 shared_libs: ["mylib2"],
8000 system_shared_libs: [],
8001 stl: "none",
8002 apex_available: [ "myapex" ],
8003 }
8004
8005 cc_library {
8006 name: "mylib2",
8007 srcs: ["mylib.cpp"],
8008 system_shared_libs: [],
8009 stl: "none",
8010 apex_available: [ "myapex" ],
8011 }
Jiyong Park34d5c332022-02-24 18:02:44 +09008012
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008013 // Used as both a JNI library and a regular shared library.
8014 cc_library {
8015 name: "mylib3",
8016 srcs: ["mylib.cpp"],
8017 system_shared_libs: [],
8018 stl: "none",
8019 apex_available: [ "myapex" ],
8020 }
8021
8022 cc_binary {
8023 name: "mybin",
8024 srcs: ["mybin.cpp"],
8025 shared_libs: ["mylib3"],
8026 system_shared_libs: [],
8027 stl: "none",
8028 apex_available: [ "myapex" ],
8029 }
8030
Jiyong Park34d5c332022-02-24 18:02:44 +09008031 rust_ffi_shared {
8032 name: "libfoo.rust",
8033 crate_name: "foo",
8034 srcs: ["foo.rs"],
8035 shared_libs: ["libfoo.shared_from_rust"],
8036 prefer_rlib: true,
8037 apex_available: ["myapex"],
8038 }
8039
8040 cc_library_shared {
8041 name: "libfoo.shared_from_rust",
8042 srcs: ["mylib.cpp"],
8043 system_shared_libs: [],
8044 stl: "none",
8045 stubs: {
8046 versions: ["10", "11", "12"],
8047 },
8048 }
8049
Jooyung Han643adc42020-02-27 13:50:06 +09008050 `)
8051
Jooyung Hana0503a52023-08-23 13:12:50 +09008052 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han643adc42020-02-27 13:50:06 +09008053 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008054 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so mylib3.so")
Jooyung Hana0503a52023-08-23 13:12:50 +09008055 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008056 "bin/mybin",
Jooyung Han643adc42020-02-27 13:50:06 +09008057 "lib64/mylib.so",
8058 "lib64/mylib2.so",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008059 "lib64/mylib3.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09008060 "lib64/libfoo.rust.so",
8061 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
8062 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09008063 })
Jiyong Park34d5c332022-02-24 18:02:44 +09008064
8065 // b/220397949
8066 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09008067}
8068
Jooyung Han49f67012020-04-17 13:43:10 +09008069func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008070 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09008071 apex {
8072 name: "myapex",
8073 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008074 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09008075 }
8076 apex_key {
8077 name: "myapex.key",
8078 public_key: "testkey.avbpubkey",
8079 private_key: "testkey.pem",
8080 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008081 `,
8082 android.FixtureModifyConfig(func(config android.Config) {
8083 delete(config.Targets, android.Android)
8084 config.AndroidCommonTarget = android.Target{}
8085 }),
8086 )
Jooyung Han49f67012020-04-17 13:43:10 +09008087
8088 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
8089 t.Errorf("Expected variants: %v, but got: %v", expected, got)
8090 }
8091}
8092
Jiyong Parkbd159612020-02-28 15:22:21 +09008093func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008094 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09008095 apex {
8096 name: "myapex",
8097 key: "myapex.key",
8098 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008099 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09008100 }
8101
8102 apex_key {
8103 name: "myapex.key",
8104 public_key: "testkey.avbpubkey",
8105 private_key: "testkey.pem",
8106 }
8107
8108 android_app {
8109 name: "AppFoo",
8110 srcs: ["foo/bar/MyClass.java"],
8111 sdk_version: "none",
8112 system_modules: "none",
8113 apex_available: [ "myapex" ],
8114 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09008115 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09008116
Jooyung Hana0503a52023-08-23 13:12:50 +09008117 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07008118 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Jiyong Parkbd159612020-02-28 15:22:21 +09008119
8120 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00008121 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 +09008122}
8123
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008124func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008125 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008126 apex {
8127 name: "myapex",
8128 key: "myapex.key",
8129 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008130 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008131 }
8132
8133 apex_key {
8134 name: "myapex.key",
8135 public_key: "testkey.avbpubkey",
8136 private_key: "testkey.pem",
8137 }
8138
8139 android_app_set {
8140 name: "AppSet",
8141 set: "AppSet.apks",
8142 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09008143 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crosscf371cc2020-11-13 11:48:42 -08008144 bundleConfigRule := mod.Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07008145 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008146 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
8147 s := mod.Rule("apexRule").Args["copy_commands"]
8148 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09008149 if len(copyCmds) != 4 {
8150 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008151 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00008152 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
8153 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09008154 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
8155 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09008156
8157 // Ensure that canned_fs_config has an entry for the app set zip file
8158 generateFsRule := mod.Rule("generateFsConfig")
8159 cmd := generateFsRule.RuleParams.Command
8160 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008161}
8162
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008163func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01008164 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008165 apex_set {
8166 name: "myapex",
8167 filename: "foo_v2.apex",
8168 sanitized: {
8169 none: { set: "myapex.apks", },
8170 hwaddress: { set: "myapex.hwasan.apks", },
8171 },
Paul Duffin24704672021-04-06 16:09:30 +01008172 }
8173 `
8174 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008175
Paul Duffin24704672021-04-06 16:09:30 +01008176 // Check that the extractor produces the correct output file from the correct input file.
Spandan Das9d6e2092024-09-21 02:50:00 +00008177 extractorOutput := "out/soong/.intermediates/myapex/android_common_myapex/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008178
Spandan Das9d6e2092024-09-21 02:50:00 +00008179 m := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin24704672021-04-06 16:09:30 +01008180 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008181
Paul Duffin24704672021-04-06 16:09:30 +01008182 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
8183
8184 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01008185 m = ctx.ModuleForTests("myapex", "android_common_myapex")
8186 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01008187
8188 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008189}
8190
Pranav Guptaeba03b02022-09-27 00:27:08 +00008191func TestApexSetApksModuleAssignment(t *testing.T) {
8192 ctx := testApex(t, `
8193 apex_set {
8194 name: "myapex",
8195 set: ":myapex_apks_file",
8196 }
8197
8198 filegroup {
8199 name: "myapex_apks_file",
8200 srcs: ["myapex.apks"],
8201 }
8202 `)
8203
Spandan Das9d6e2092024-09-21 02:50:00 +00008204 m := ctx.ModuleForTests("myapex", "android_common_myapex")
Pranav Guptaeba03b02022-09-27 00:27:08 +00008205
8206 // Check that the extractor produces the correct apks file from the input module
Spandan Das9d6e2092024-09-21 02:50:00 +00008207 extractorOutput := "out/soong/.intermediates/myapex/android_common_myapex/extracted/myapex.apks"
Pranav Guptaeba03b02022-09-27 00:27:08 +00008208 extractedApex := m.Output(extractorOutput)
8209
8210 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
8211}
8212
Paul Duffin89f570a2021-06-16 01:42:33 +01008213func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00008214 t.Helper()
8215
Paul Duffin55607122021-03-30 23:32:51 +01008216 fs := android.MockFS{
8217 "a.java": nil,
8218 "a.jar": nil,
8219 "apex_manifest.json": nil,
8220 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00008221 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00008222 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
8223 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
8224 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00008225 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008226 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008227
Paul Duffin55607122021-03-30 23:32:51 +01008228 errorHandler := android.FixtureExpectsNoErrors
8229 if errmsg != "" {
8230 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008231 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008232
Paul Duffin55607122021-03-30 23:32:51 +01008233 result := android.GroupFixturePreparers(
8234 cc.PrepareForTestWithCcDefaultModules,
8235 java.PrepareForTestWithHiddenApiBuildComponents,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008236 java.PrepareForTestWithDexpreopt,
Paul Duffin55607122021-03-30 23:32:51 +01008237 java.PrepareForTestWithJavaSdkLibraryFiles,
8238 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01008239 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01008240 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01008241 android.FixtureModifyMockFS(func(fs android.MockFS) {
8242 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
8243 insert := ""
8244 for _, fragment := range fragments {
8245 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
8246 }
8247 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
8248 platform_bootclasspath {
8249 name: "platform-bootclasspath",
8250 fragments: [
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008251 {apex: "com.android.art", module: "art-bootclasspath-fragment"},
Paul Duffin89f570a2021-06-16 01:42:33 +01008252 %s
8253 ],
8254 }
8255 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01008256 }
Paul Duffin89f570a2021-06-16 01:42:33 +01008257 }),
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008258 // Dexpreopt for boot jars requires the ART boot image profile.
8259 java.PrepareApexBootJarModule("com.android.art", "core-oj"),
8260 dexpreopt.FixtureSetArtBootJars("com.android.art:core-oj"),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00008261 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01008262 ).
8263 ExtendWithErrorHandler(errorHandler).
8264 RunTestWithBp(t, bp)
8265
8266 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008267}
8268
Jooyung Han548640b2020-04-27 12:10:30 +09008269func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
8270 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8271 apex {
8272 name: "myapex",
8273 key: "myapex.key",
8274 updatable: true,
8275 }
8276
8277 apex_key {
8278 name: "myapex.key",
8279 public_key: "testkey.avbpubkey",
8280 private_key: "testkey.pem",
8281 }
8282 `)
8283}
8284
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008285func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
8286 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8287 apex {
8288 name: "myapex",
8289 key: "myapex.key",
8290 }
8291
8292 apex_key {
8293 name: "myapex.key",
8294 public_key: "testkey.avbpubkey",
8295 private_key: "testkey.pem",
8296 }
8297 `)
8298}
8299
satayevb98371c2021-06-15 16:49:50 +01008300func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
8301 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
8302 apex {
8303 name: "myapex",
8304 key: "myapex.key",
8305 systemserverclasspath_fragments: [
8306 "mysystemserverclasspathfragment",
8307 ],
8308 min_sdk_version: "29",
8309 updatable: true,
8310 }
8311
8312 apex_key {
8313 name: "myapex.key",
8314 public_key: "testkey.avbpubkey",
8315 private_key: "testkey.pem",
8316 }
8317
8318 java_library {
8319 name: "foo",
8320 srcs: ["b.java"],
8321 min_sdk_version: "29",
8322 installable: true,
8323 apex_available: [
8324 "myapex",
8325 ],
Jihoon Kang85bc1932024-07-01 17:04:46 +00008326 sdk_version: "current",
satayevb98371c2021-06-15 16:49:50 +01008327 }
8328
8329 systemserverclasspath_fragment {
8330 name: "mysystemserverclasspathfragment",
8331 generate_classpaths_proto: false,
8332 contents: [
8333 "foo",
8334 ],
8335 apex_available: [
8336 "myapex",
8337 ],
8338 }
satayevabcd5972021-08-06 17:49:46 +01008339 `,
8340 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8341 )
satayevb98371c2021-06-15 16:49:50 +01008342}
8343
Paul Duffin064b70c2020-11-02 17:32:38 +00008344func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008345 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008346 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008347 fragment := java.ApexVariantReference{
8348 Apex: proptools.StringPtr("myapex"),
8349 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8350 }
8351
Paul Duffin064b70c2020-11-02 17:32:38 +00008352 testDexpreoptWithApexes(t, `
8353 prebuilt_apex {
8354 name: "myapex" ,
8355 arch: {
8356 arm64: {
8357 src: "myapex-arm64.apex",
8358 },
8359 arm: {
8360 src: "myapex-arm.apex",
8361 },
8362 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008363 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8364 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008365
Paul Duffin89f570a2021-06-16 01:42:33 +01008366 prebuilt_bootclasspath_fragment {
8367 name: "my-bootclasspath-fragment",
8368 contents: ["libfoo"],
8369 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008370 hidden_api: {
8371 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8372 metadata: "my-bootclasspath-fragment/metadata.csv",
8373 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008374 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8375 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8376 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008377 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008378 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008379
Spandan Das52c01a12024-09-20 01:09:48 +00008380 java_sdk_library_import {
8381 name: "libfoo",
8382 prefer: true,
8383 public: {
Paul Duffin89f570a2021-06-16 01:42:33 +01008384 jars: ["libfoo.jar"],
Spandan Das52c01a12024-09-20 01:09:48 +00008385 },
8386 apex_available: ["myapex"],
8387 shared_library: false,
8388 permitted_packages: ["libfoo"],
8389 }
Paul Duffin89f570a2021-06-16 01:42:33 +01008390 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008391 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008392}
8393
Spandan Dasf14e2542021-11-12 00:01:37 +00008394func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008395 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008396 bp += `
8397 apex_key {
8398 name: "myapex.key",
8399 public_key: "testkey.avbpubkey",
8400 private_key: "testkey.pem",
8401 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008402 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008403 "lib1/src/A.java": nil,
8404 "lib2/src/B.java": nil,
8405 "system/sepolicy/apex/myapex-file_contexts": nil,
8406 }
8407
Paul Duffin45338f02021-03-30 23:07:52 +01008408 errorHandler := android.FixtureExpectsNoErrors
8409 if errmsg != "" {
8410 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008411 }
Colin Crossae8600b2020-10-29 17:09:13 -07008412
Paul Duffin45338f02021-03-30 23:07:52 +01008413 android.GroupFixturePreparers(
8414 android.PrepareForTestWithAndroidBuildComponents,
8415 java.PrepareForTestWithJavaBuildComponents,
8416 PrepareForTestWithApexBuildComponents,
8417 android.PrepareForTestWithNeverallowRules(rules),
8418 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008419 apexBootJars := make([]string, 0, len(bootJars))
8420 for _, apexBootJar := range bootJars {
8421 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008422 }
satayevd604b212021-07-21 14:23:52 +01008423 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008424 }),
8425 fs.AddToFixture(),
8426 ).
8427 ExtendWithErrorHandler(errorHandler).
8428 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008429}
8430
8431func TestApexPermittedPackagesRules(t *testing.T) {
8432 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008433 name string
8434 expectedError string
8435 bp string
8436 bootJars []string
8437 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008438 }{
8439
8440 {
8441 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8442 expectedError: "",
8443 bp: `
8444 java_library {
8445 name: "bcp_lib1",
8446 srcs: ["lib1/src/*.java"],
8447 permitted_packages: ["foo.bar"],
8448 apex_available: ["myapex"],
8449 sdk_version: "none",
8450 system_modules: "none",
8451 }
8452 java_library {
8453 name: "nonbcp_lib2",
8454 srcs: ["lib2/src/*.java"],
8455 apex_available: ["myapex"],
8456 permitted_packages: ["a.b"],
8457 sdk_version: "none",
8458 system_modules: "none",
8459 }
8460 apex {
8461 name: "myapex",
8462 key: "myapex.key",
8463 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008464 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008465 }`,
8466 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008467 bcpPermittedPackages: map[string][]string{
8468 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008469 "foo.bar",
8470 },
8471 },
8472 },
8473 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008474 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008475 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 +01008476 bp: `
8477 java_library {
8478 name: "bcp_lib1",
8479 srcs: ["lib1/src/*.java"],
8480 apex_available: ["myapex"],
8481 permitted_packages: ["foo.bar"],
8482 sdk_version: "none",
8483 system_modules: "none",
8484 }
8485 java_library {
8486 name: "bcp_lib2",
8487 srcs: ["lib2/src/*.java"],
8488 apex_available: ["myapex"],
8489 permitted_packages: ["foo.bar", "bar.baz"],
8490 sdk_version: "none",
8491 system_modules: "none",
8492 }
8493 apex {
8494 name: "myapex",
8495 key: "myapex.key",
8496 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008497 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008498 }
8499 `,
8500 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008501 bcpPermittedPackages: map[string][]string{
8502 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008503 "foo.bar",
8504 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008505 "bcp_lib2": []string{
8506 "foo.bar",
8507 },
8508 },
8509 },
8510 {
8511 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8512 expectedError: "",
8513 bp: `
8514 java_library {
8515 name: "bcp_lib_restricted",
8516 srcs: ["lib1/src/*.java"],
8517 apex_available: ["myapex"],
8518 permitted_packages: ["foo.bar"],
8519 sdk_version: "none",
8520 min_sdk_version: "29",
8521 system_modules: "none",
8522 }
8523 java_library {
8524 name: "bcp_lib_unrestricted",
8525 srcs: ["lib2/src/*.java"],
8526 apex_available: ["myapex"],
8527 permitted_packages: ["foo.bar", "bar.baz"],
8528 sdk_version: "none",
8529 min_sdk_version: "29",
8530 system_modules: "none",
8531 }
8532 apex {
8533 name: "myapex",
8534 key: "myapex.key",
8535 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8536 updatable: true,
8537 min_sdk_version: "29",
8538 }
8539 `,
8540 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8541 bcpPermittedPackages: map[string][]string{
8542 "bcp_lib1_non_updateable": []string{
8543 "foo.bar",
8544 },
8545 // 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 +01008546 },
8547 },
8548 }
8549 for _, tc := range testcases {
8550 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008551 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8552 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008553 })
8554 }
8555}
8556
Jiyong Park62304bb2020-04-13 16:19:48 +09008557func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008558 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008559 apex {
8560 name: "myapex",
8561 key: "myapex.key",
8562 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008563 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008564 }
8565
8566 apex_key {
8567 name: "myapex.key",
8568 public_key: "testkey.avbpubkey",
8569 private_key: "testkey.pem",
8570 }
8571
8572 cc_library {
8573 name: "mylib",
8574 srcs: ["mylib.cpp"],
8575 system_shared_libs: [],
8576 stl: "none",
8577 stubs: {
8578 versions: ["1"],
8579 },
8580 apex_available: ["myapex"],
8581 }
8582
8583 cc_library {
8584 name: "myprivlib",
8585 srcs: ["mylib.cpp"],
8586 system_shared_libs: [],
8587 stl: "none",
8588 apex_available: ["myapex"],
8589 }
8590
8591
8592 cc_test {
8593 name: "mytest",
8594 gtest: false,
8595 srcs: ["mylib.cpp"],
8596 system_shared_libs: [],
8597 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008598 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008599 test_for: ["myapex"]
8600 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008601
8602 cc_library {
8603 name: "mytestlib",
8604 srcs: ["mylib.cpp"],
8605 system_shared_libs: [],
8606 shared_libs: ["mylib", "myprivlib"],
8607 stl: "none",
8608 test_for: ["myapex"],
8609 }
8610
8611 cc_benchmark {
8612 name: "mybench",
8613 srcs: ["mylib.cpp"],
8614 system_shared_libs: [],
8615 shared_libs: ["mylib", "myprivlib"],
8616 stl: "none",
8617 test_for: ["myapex"],
8618 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008619 `)
8620
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008621 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008622 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008623 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8624 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8625 }
8626
8627 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008628 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008629 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8630 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8631 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8632}
Jiyong Park46a512f2020-12-04 18:02:13 +09008633
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008634func TestIndirectTestFor(t *testing.T) {
8635 ctx := testApex(t, `
8636 apex {
8637 name: "myapex",
8638 key: "myapex.key",
8639 native_shared_libs: ["mylib", "myprivlib"],
8640 updatable: false,
8641 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008642
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008643 apex_key {
8644 name: "myapex.key",
8645 public_key: "testkey.avbpubkey",
8646 private_key: "testkey.pem",
8647 }
8648
8649 cc_library {
8650 name: "mylib",
8651 srcs: ["mylib.cpp"],
8652 system_shared_libs: [],
8653 stl: "none",
8654 stubs: {
8655 versions: ["1"],
8656 },
8657 apex_available: ["myapex"],
8658 }
8659
8660 cc_library {
8661 name: "myprivlib",
8662 srcs: ["mylib.cpp"],
8663 system_shared_libs: [],
8664 stl: "none",
8665 shared_libs: ["mylib"],
8666 apex_available: ["myapex"],
8667 }
8668
8669 cc_library {
8670 name: "mytestlib",
8671 srcs: ["mylib.cpp"],
8672 system_shared_libs: [],
8673 shared_libs: ["myprivlib"],
8674 stl: "none",
8675 test_for: ["myapex"],
8676 }
8677 `)
8678
8679 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008680 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008681 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8682 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8683 }
8684
8685 // The platform variant of mytestlib links to the platform variant of the
8686 // internal myprivlib.
8687 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8688
8689 // The platform variant of myprivlib links to the platform variant of mylib
8690 // and bypasses its stubs.
8691 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 +09008692}
8693
Martin Stjernholmec009002021-03-27 15:18:31 +00008694func TestTestForForLibInOtherApex(t *testing.T) {
8695 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8696 _ = testApex(t, `
8697 apex {
8698 name: "com.android.art",
8699 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008700 native_shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008701 updatable: false,
8702 }
8703
8704 apex {
8705 name: "com.android.art.debug",
8706 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008707 native_shared_libs: ["libnativebridge", "libnativebrdige_test"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008708 updatable: false,
8709 }
8710
8711 apex_key {
8712 name: "myapex.key",
8713 public_key: "testkey.avbpubkey",
8714 private_key: "testkey.pem",
8715 }
8716
8717 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008718 name: "libnativebridge",
8719 srcs: ["libnativebridge.cpp"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008720 system_shared_libs: [],
8721 stl: "none",
8722 stubs: {
8723 versions: ["1"],
8724 },
8725 apex_available: ["com.android.art", "com.android.art.debug"],
8726 }
8727
8728 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008729 name: "libnativebrdige_test",
Martin Stjernholmec009002021-03-27 15:18:31 +00008730 srcs: ["mylib.cpp"],
8731 system_shared_libs: [],
Spandan Das20fce2d2023-04-12 17:21:39 +00008732 shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008733 stl: "none",
8734 apex_available: ["com.android.art.debug"],
8735 test_for: ["com.android.art"],
8736 }
8737 `,
8738 android.MockFS{
8739 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8740 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8741 }.AddToFixture())
8742}
8743
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008744// TODO(jungjw): Move this to proptools
8745func intPtr(i int) *int {
8746 return &i
8747}
8748
8749func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008750 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008751 apex_set {
8752 name: "myapex",
8753 set: "myapex.apks",
8754 filename: "foo_v2.apex",
8755 overrides: ["foo"],
8756 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008757 `,
8758 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8759 variables.Platform_sdk_version = intPtr(30)
8760 }),
8761 android.FixtureModifyConfig(func(config android.Config) {
8762 config.Targets[android.Android] = []android.Target{
8763 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8764 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8765 }
8766 }),
8767 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008768
Spandan Das9d6e2092024-09-21 02:50:00 +00008769 m := ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008770
8771 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008772 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008773 actual := extractedApex.Args["abis"]
8774 expected := "ARMEABI_V7A,ARM64_V8A"
8775 if actual != expected {
8776 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8777 }
8778 actual = extractedApex.Args["sdk-version"]
8779 expected = "30"
8780 if actual != expected {
8781 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8782 }
8783
Paul Duffin6717d882021-06-15 19:09:41 +01008784 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008785 a := m.Module().(*ApexSet)
8786 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008787 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008788 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8789 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8790 }
8791}
8792
Anton Hansson805e0a52022-11-25 14:06:46 +00008793func TestApexSet_NativeBridge(t *testing.T) {
8794 ctx := testApex(t, `
8795 apex_set {
8796 name: "myapex",
8797 set: "myapex.apks",
8798 filename: "foo_v2.apex",
8799 overrides: ["foo"],
8800 }
8801 `,
8802 android.FixtureModifyConfig(func(config android.Config) {
8803 config.Targets[android.Android] = []android.Target{
8804 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8805 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8806 }
8807 }),
8808 )
8809
Spandan Das9d6e2092024-09-21 02:50:00 +00008810 m := ctx.ModuleForTests("myapex", "android_common_myapex")
Anton Hansson805e0a52022-11-25 14:06:46 +00008811
8812 // Check extract_apks tool parameters. No native bridge arch expected
8813 extractedApex := m.Output("extracted/myapex.apks")
8814 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8815}
8816
Jiyong Park7d95a512020-05-10 15:16:24 +09008817func TestNoStaticLinkingToStubsLib(t *testing.T) {
8818 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8819 apex {
8820 name: "myapex",
8821 key: "myapex.key",
8822 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008823 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008824 }
8825
8826 apex_key {
8827 name: "myapex.key",
8828 public_key: "testkey.avbpubkey",
8829 private_key: "testkey.pem",
8830 }
8831
8832 cc_library {
8833 name: "mylib",
8834 srcs: ["mylib.cpp"],
8835 static_libs: ["otherlib"],
8836 system_shared_libs: [],
8837 stl: "none",
8838 apex_available: [ "myapex" ],
8839 }
8840
8841 cc_library {
8842 name: "otherlib",
8843 srcs: ["mylib.cpp"],
8844 system_shared_libs: [],
8845 stl: "none",
8846 stubs: {
8847 versions: ["1", "2", "3"],
8848 },
8849 apex_available: [ "myapex" ],
8850 }
8851 `)
8852}
8853
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008854func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008855 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008856 apex {
8857 name: "myapex",
8858 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008859 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008860 custom_sign_tool: "sign_myapex",
8861 }
8862
8863 apex_key {
8864 name: "myapex.key",
8865 public_key: "testkey.avbpubkey",
8866 private_key: "testkey.pem",
8867 }
8868 `)
8869
Jooyung Han286957d2023-10-30 16:17:56 +09008870 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07008871 content := android.ContentFromFileRuleForTests(t, ctx, myapex.Output("apexkeys.txt"))
Jooyung Haneec1b3f2023-06-20 16:25:59 +09008872 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" sign_tool="sign_myapex"`)
Jooyung Han09c11ad2021-10-27 03:45:31 +09008873}
8874
8875func TestApexKeysTxtOverrides(t *testing.T) {
8876 ctx := testApex(t, `
8877 apex {
8878 name: "myapex",
8879 key: "myapex.key",
8880 updatable: false,
8881 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008882 }
8883
8884 apex_key {
8885 name: "myapex.key",
8886 public_key: "testkey.avbpubkey",
8887 private_key: "testkey.pem",
8888 }
8889
8890 prebuilt_apex {
8891 name: "myapex",
8892 prefer: true,
8893 arch: {
8894 arm64: {
8895 src: "myapex-arm64.apex",
8896 },
8897 arm: {
8898 src: "myapex-arm.apex",
8899 },
8900 },
8901 }
8902
8903 apex_set {
8904 name: "myapex_set",
8905 set: "myapex.apks",
8906 filename: "myapex_set.apex",
8907 overrides: ["myapex"],
8908 }
8909 `)
8910
Colin Crossf61d03d2023-11-02 16:56:39 -07008911 content := android.ContentFromFileRuleForTests(t, ctx,
8912 ctx.ModuleForTests("myapex", "android_common_myapex").Output("apexkeys.txt"))
Jooyung Han286957d2023-10-30 16:17:56 +09008913 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" sign_tool="sign_myapex"`)
Colin Crossf61d03d2023-11-02 16:56:39 -07008914 content = android.ContentFromFileRuleForTests(t, ctx,
8915 ctx.ModuleForTests("myapex_set", "android_common_myapex_set").Output("apexkeys.txt"))
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008916 ensureContains(t, content, `name="myapex_set.apex" public_key="PRESIGNED" private_key="PRESIGNED" container_certificate="PRESIGNED" container_private_key="PRESIGNED" partition="system"`)
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008917}
8918
Jooyung Han938b5932020-06-20 12:47:47 +09008919func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008920 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008921 apex {
8922 name: "myapex",
8923 key: "myapex.key",
8924 apps: ["app"],
8925 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008926 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008927 }
8928
8929 apex_key {
8930 name: "myapex.key",
8931 public_key: "testkey.avbpubkey",
8932 private_key: "testkey.pem",
8933 }
8934
8935 android_app {
8936 name: "app",
8937 srcs: ["foo/bar/MyClass.java"],
8938 package_name: "foo",
8939 sdk_version: "none",
8940 system_modules: "none",
8941 apex_available: [ "myapex" ],
8942 }
8943 `, withFiles(map[string][]byte{
8944 "sub/Android.bp": []byte(`
8945 override_apex {
8946 name: "override_myapex",
8947 base: "myapex",
8948 apps: ["override_app"],
8949 allowed_files: ":allowed",
8950 }
8951 // Overridable "path" property should be referenced indirectly
8952 filegroup {
8953 name: "allowed",
8954 srcs: ["allowed.txt"],
8955 }
8956 override_android_app {
8957 name: "override_app",
8958 base: "app",
8959 package_name: "bar",
8960 }
8961 `),
8962 }))
8963
Jooyung Hana0503a52023-08-23 13:12:50 +09008964 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008965 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8966 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8967 }
8968
Spandan Das50801e22024-05-13 18:29:45 +00008969 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008970 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8971 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8972 }
8973}
8974
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008975func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008976 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008977 apex {
8978 name: "myapex",
8979 key: "myapex.key",
8980 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008981 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008982 }
8983
8984 apex_key {
8985 name: "myapex.key",
8986 public_key: "testkey.avbpubkey",
8987 private_key: "testkey.pem",
8988 }
8989
8990 cc_library {
8991 name: "mylib",
8992 srcs: ["mylib.cpp"],
8993 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008994 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008995 },
8996 apex_available: ["myapex"],
8997 }
8998
8999 cc_prebuilt_library_shared {
9000 name: "mylib",
9001 prefer: false,
9002 srcs: ["prebuilt.so"],
9003 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07009004 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009005 },
9006 apex_available: ["myapex"],
9007 }
9008 `)
9009}
9010
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009011func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009012 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009013 apex {
9014 name: "myapex",
9015 key: "myapex.key",
9016 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009017 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009018 }
9019 apex_key {
9020 name: "myapex.key",
9021 public_key: "testkey.avbpubkey",
9022 private_key: "testkey.pem",
9023 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00009024 `,
9025 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9026 variables.CompressedApex = proptools.BoolPtr(true)
9027 }),
9028 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009029
Jooyung Hana0503a52023-08-23 13:12:50 +09009030 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("compressRule")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009031 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
9032
Jooyung Hana0503a52023-08-23 13:12:50 +09009033 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex").Description("sign compressedApex")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009034 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
9035
9036 // Make sure output of bundle is .capex
Jooyung Hana0503a52023-08-23 13:12:50 +09009037 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009038 ensureContains(t, ab.outputFile.String(), "myapex.capex")
9039
9040 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07009041 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009042 var builder strings.Builder
9043 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9044 androidMk := builder.String()
9045 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
9046}
9047
Jooyung Han26ec8482024-07-31 15:04:05 +09009048func TestApexSet_ShouldRespectCompressedApexFlag(t *testing.T) {
9049 for _, compressionEnabled := range []bool{true, false} {
9050 t.Run(fmt.Sprintf("compressionEnabled=%v", compressionEnabled), func(t *testing.T) {
9051 ctx := testApex(t, `
9052 apex_set {
9053 name: "com.company.android.myapex",
9054 apex_name: "com.android.myapex",
9055 set: "company-myapex.apks",
9056 }
9057 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9058 variables.CompressedApex = proptools.BoolPtr(compressionEnabled)
9059 }),
9060 )
9061
9062 build := ctx.ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex").Output("com.company.android.myapex.apex")
9063 if compressionEnabled {
9064 ensureEquals(t, build.Rule.String(), "android/soong/android.Cp")
9065 } else {
9066 ensureEquals(t, build.Rule.String(), "android/apex.decompressApex")
9067 }
9068 })
9069 }
9070}
9071
Martin Stjernholm2856c662020-12-02 15:03:42 +00009072func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009073 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00009074 apex {
9075 name: "myapex",
9076 key: "myapex.key",
9077 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009078 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00009079 }
9080
9081 apex_key {
9082 name: "myapex.key",
9083 public_key: "testkey.avbpubkey",
9084 private_key: "testkey.pem",
9085 }
9086
9087 cc_library {
9088 name: "mylib",
9089 srcs: ["mylib.cpp"],
9090 apex_available: ["myapex"],
9091 shared_libs: ["otherlib"],
9092 system_shared_libs: [],
9093 }
9094
9095 cc_library {
9096 name: "otherlib",
9097 srcs: ["mylib.cpp"],
9098 stubs: {
9099 versions: ["current"],
9100 },
9101 }
9102
9103 cc_prebuilt_library_shared {
9104 name: "otherlib",
9105 prefer: true,
9106 srcs: ["prebuilt.so"],
9107 stubs: {
9108 versions: ["current"],
9109 },
9110 }
9111 `)
9112
Jooyung Hana0503a52023-08-23 13:12:50 +09009113 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07009114 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00009115 var builder strings.Builder
9116 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9117 androidMk := builder.String()
9118
9119 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
9120 // a thing there.
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009121 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 otherlib\n")
Martin Stjernholm2856c662020-12-02 15:03:42 +00009122}
9123
Jiyong Parke3867542020-12-03 17:28:25 +09009124func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009125 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09009126 apex {
9127 name: "myapex",
9128 key: "myapex.key",
9129 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009130 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09009131 }
9132
9133 apex_key {
9134 name: "myapex.key",
9135 public_key: "testkey.avbpubkey",
9136 private_key: "testkey.pem",
9137 }
9138
9139 cc_library {
9140 name: "mylib",
9141 srcs: ["mylib.cpp"],
9142 system_shared_libs: [],
9143 stl: "none",
9144 apex_available: ["myapex"],
9145 shared_libs: ["mylib2"],
9146 target: {
9147 apex: {
9148 exclude_shared_libs: ["mylib2"],
9149 },
9150 },
9151 }
9152
9153 cc_library {
9154 name: "mylib2",
9155 srcs: ["mylib.cpp"],
9156 system_shared_libs: [],
9157 stl: "none",
9158 }
9159 `)
9160
9161 // Check if mylib is linked to mylib2 for the non-apex target
9162 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
9163 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
9164
9165 // Make sure that the link doesn't occur for the apex target
9166 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
9167 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
9168
9169 // It shouldn't appear in the copy cmd as well.
Jooyung Hana0503a52023-08-23 13:12:50 +09009170 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule").Args["copy_commands"]
Jiyong Parke3867542020-12-03 17:28:25 +09009171 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
9172}
9173
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009174func TestPrebuiltStubLibDep(t *testing.T) {
9175 bpBase := `
9176 apex {
9177 name: "myapex",
9178 key: "myapex.key",
9179 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009180 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009181 }
9182 apex_key {
9183 name: "myapex.key",
9184 public_key: "testkey.avbpubkey",
9185 private_key: "testkey.pem",
9186 }
9187 cc_library {
9188 name: "mylib",
9189 srcs: ["mylib.cpp"],
9190 apex_available: ["myapex"],
9191 shared_libs: ["stublib"],
9192 system_shared_libs: [],
9193 }
9194 apex {
9195 name: "otherapex",
9196 enabled: %s,
9197 key: "myapex.key",
9198 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009199 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009200 }
9201 `
9202
9203 stublibSourceBp := `
9204 cc_library {
9205 name: "stublib",
9206 srcs: ["mylib.cpp"],
9207 apex_available: ["otherapex"],
9208 system_shared_libs: [],
9209 stl: "none",
9210 stubs: {
9211 versions: ["1"],
9212 },
9213 }
9214 `
9215
9216 stublibPrebuiltBp := `
9217 cc_prebuilt_library_shared {
9218 name: "stublib",
9219 srcs: ["prebuilt.so"],
9220 apex_available: ["otherapex"],
9221 stubs: {
9222 versions: ["1"],
9223 },
9224 %s
9225 }
9226 `
9227
9228 tests := []struct {
9229 name string
9230 stublibBp string
9231 usePrebuilt bool
9232 modNames []string // Modules to collect AndroidMkEntries for
9233 otherApexEnabled []string
9234 }{
9235 {
9236 name: "only_source",
9237 stublibBp: stublibSourceBp,
9238 usePrebuilt: false,
9239 modNames: []string{"stublib"},
9240 otherApexEnabled: []string{"true", "false"},
9241 },
9242 {
9243 name: "source_preferred",
9244 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9245 usePrebuilt: false,
9246 modNames: []string{"stublib", "prebuilt_stublib"},
9247 otherApexEnabled: []string{"true", "false"},
9248 },
9249 {
9250 name: "prebuilt_preferred",
9251 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9252 usePrebuilt: true,
9253 modNames: []string{"stublib", "prebuilt_stublib"},
9254 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9255 },
9256 {
9257 name: "only_prebuilt",
9258 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9259 usePrebuilt: true,
9260 modNames: []string{"stublib"},
9261 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9262 },
9263 }
9264
9265 for _, test := range tests {
9266 t.Run(test.name, func(t *testing.T) {
9267 for _, otherApexEnabled := range test.otherApexEnabled {
9268 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009269 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009270
9271 type modAndMkEntries struct {
9272 mod *cc.Module
Priyanka Advani (xWF)37e67942024-10-17 18:58:08 +00009273 mkEntries android.AndroidMkEntries
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009274 }
9275 entries := []*modAndMkEntries{}
9276
9277 // Gather shared lib modules that are installable
9278 for _, modName := range test.modNames {
9279 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9280 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9281 continue
9282 }
9283 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Cole Fausta963b942024-04-11 17:43:00 -07009284 if !mod.Enabled(android.PanickingConfigAndErrorContext(ctx)) || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009285 continue
9286 }
Priyanka Advani (xWF)37e67942024-10-17 18:58:08 +00009287 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009288 if ent.Disabled {
9289 continue
9290 }
9291 entries = append(entries, &modAndMkEntries{
9292 mod: mod,
9293 mkEntries: ent,
9294 })
9295 }
9296 }
9297 }
9298
9299 var entry *modAndMkEntries = nil
9300 for _, ent := range entries {
9301 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9302 if entry != nil {
9303 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9304 } else {
9305 entry = ent
9306 }
9307 }
9308 }
9309
9310 if entry == nil {
9311 t.Errorf("AndroidMk entry for \"stublib\" missing")
9312 } else {
9313 isPrebuilt := entry.mod.Prebuilt() != nil
9314 if isPrebuilt != test.usePrebuilt {
9315 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9316 }
9317 if !entry.mod.IsStubs() {
9318 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9319 }
9320 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9321 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9322 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009323 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009324 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009325 if !android.InList(expected, cflags) {
9326 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9327 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009328 }
9329 })
9330 }
9331 })
9332 }
9333}
9334
Colin Crossc33e5212021-05-25 18:16:02 -07009335func TestApexJavaCoverage(t *testing.T) {
9336 bp := `
9337 apex {
9338 name: "myapex",
9339 key: "myapex.key",
9340 java_libs: ["mylib"],
9341 bootclasspath_fragments: ["mybootclasspathfragment"],
9342 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9343 updatable: false,
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: "mylib",
9354 srcs: ["mylib.java"],
9355 apex_available: ["myapex"],
9356 compile_dex: true,
9357 }
9358
9359 bootclasspath_fragment {
9360 name: "mybootclasspathfragment",
9361 contents: ["mybootclasspathlib"],
9362 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009363 hidden_api: {
9364 split_packages: ["*"],
9365 },
Colin Crossc33e5212021-05-25 18:16:02 -07009366 }
9367
9368 java_library {
9369 name: "mybootclasspathlib",
9370 srcs: ["mybootclasspathlib.java"],
9371 apex_available: ["myapex"],
9372 compile_dex: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00009373 sdk_version: "current",
Colin Crossc33e5212021-05-25 18:16:02 -07009374 }
9375
9376 systemserverclasspath_fragment {
9377 name: "mysystemserverclasspathfragment",
9378 contents: ["mysystemserverclasspathlib"],
9379 apex_available: ["myapex"],
9380 }
9381
9382 java_library {
9383 name: "mysystemserverclasspathlib",
9384 srcs: ["mysystemserverclasspathlib.java"],
9385 apex_available: ["myapex"],
9386 compile_dex: true,
9387 }
9388 `
9389
9390 result := android.GroupFixturePreparers(
9391 PrepareForTestWithApexBuildComponents,
9392 prepareForTestWithMyapex,
9393 java.PrepareForTestWithJavaDefaultModules,
9394 android.PrepareForTestWithAndroidBuildComponents,
9395 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009396 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9397 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009398 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009399 ).RunTest(t)
9400
9401 // Make sure jacoco ran on both mylib and mybootclasspathlib
9402 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9403 t.Errorf("Failed to find jacoco rule for mylib")
9404 }
9405 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9406 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9407 }
9408 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9409 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9410 }
9411}
9412
Jiyong Park192600a2021-08-03 07:52:17 +00009413func TestProhibitStaticExecutable(t *testing.T) {
9414 testApexError(t, `executable mybin is static`, `
9415 apex {
9416 name: "myapex",
9417 key: "myapex.key",
9418 binaries: ["mybin"],
9419 min_sdk_version: "29",
9420 }
9421
9422 apex_key {
9423 name: "myapex.key",
9424 public_key: "testkey.avbpubkey",
9425 private_key: "testkey.pem",
9426 }
9427
9428 cc_binary {
9429 name: "mybin",
9430 srcs: ["mylib.cpp"],
9431 relative_install_path: "foo/bar",
9432 static_executable: true,
9433 system_shared_libs: [],
9434 stl: "none",
9435 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009436 min_sdk_version: "29",
9437 }
9438 `)
9439
9440 testApexError(t, `executable mybin.rust is static`, `
9441 apex {
9442 name: "myapex",
9443 key: "myapex.key",
9444 binaries: ["mybin.rust"],
9445 min_sdk_version: "29",
9446 }
9447
9448 apex_key {
9449 name: "myapex.key",
9450 public_key: "testkey.avbpubkey",
9451 private_key: "testkey.pem",
9452 }
9453
9454 rust_binary {
9455 name: "mybin.rust",
9456 srcs: ["foo.rs"],
9457 static_executable: true,
9458 apex_available: ["myapex"],
9459 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009460 }
9461 `)
9462}
9463
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009464func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9465 ctx := testApex(t, `
9466 apex {
9467 name: "myapex",
9468 key: "myapex.key",
9469 updatable: false,
9470 java_libs: ["foo"],
9471 }
9472
9473 apex_key {
9474 name: "myapex.key",
9475 public_key: "testkey.avbpubkey",
9476 private_key: "testkey.pem",
9477 }
9478
9479 java_library {
9480 name: "foo",
9481 srcs: ["foo.java"],
9482 apex_available: ["myapex"],
9483 installable: true,
9484 }
9485 `,
9486 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9487 )
9488
Jooyung Hana0503a52023-08-23 13:12:50 +09009489 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009490 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9491 var builder strings.Builder
9492 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9493 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009494 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.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 +00009495}
9496
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009497func TestAndroidMk_RequiredModules(t *testing.T) {
9498 ctx := testApex(t, `
9499 apex {
9500 name: "myapex",
9501 key: "myapex.key",
9502 updatable: false,
9503 java_libs: ["foo"],
9504 required: ["otherapex"],
9505 }
9506
9507 apex {
9508 name: "otherapex",
9509 key: "myapex.key",
9510 updatable: false,
9511 java_libs: ["foo"],
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009512 }
9513
9514 apex_key {
9515 name: "myapex.key",
9516 public_key: "testkey.avbpubkey",
9517 private_key: "testkey.pem",
9518 }
9519
9520 java_library {
9521 name: "foo",
9522 srcs: ["foo.java"],
9523 apex_available: ["myapex", "otherapex"],
9524 installable: true,
9525 }
9526 `)
9527
Jooyung Hana0503a52023-08-23 13:12:50 +09009528 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009529 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9530 var builder strings.Builder
9531 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9532 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009533 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009534}
9535
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009536func TestAndroidMk_RequiredDeps(t *testing.T) {
9537 ctx := testApex(t, `
9538 apex {
9539 name: "myapex",
9540 key: "myapex.key",
9541 updatable: false,
9542 }
9543
9544 apex_key {
9545 name: "myapex.key",
9546 public_key: "testkey.avbpubkey",
9547 private_key: "testkey.pem",
9548 }
9549 `)
9550
Jooyung Hana0503a52023-08-23 13:12:50 +09009551 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009552 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009553 data := android.AndroidMkDataForTest(t, ctx, bundle)
9554 var builder strings.Builder
9555 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9556 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009557 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009558}
9559
Jooyung Hana6d36672022-02-24 13:58:07 +09009560func TestApexOutputFileProducer(t *testing.T) {
9561 for _, tc := range []struct {
9562 name string
9563 ref string
9564 expected_data []string
9565 }{
9566 {
9567 name: "test_using_output",
9568 ref: ":myapex",
Jooyung Hana0503a52023-08-23 13:12:50 +09009569 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.capex:myapex.capex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009570 },
9571 {
9572 name: "test_using_apex",
9573 ref: ":myapex{.apex}",
Jooyung Hana0503a52023-08-23 13:12:50 +09009574 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.apex:myapex.apex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009575 },
9576 } {
9577 t.Run(tc.name, func(t *testing.T) {
9578 ctx := testApex(t, `
9579 apex {
9580 name: "myapex",
9581 key: "myapex.key",
9582 compressible: true,
9583 updatable: false,
9584 }
9585
9586 apex_key {
9587 name: "myapex.key",
9588 public_key: "testkey.avbpubkey",
9589 private_key: "testkey.pem",
9590 }
9591
9592 java_test {
9593 name: "`+tc.name+`",
9594 srcs: ["a.java"],
9595 data: ["`+tc.ref+`"],
9596 }
9597 `,
9598 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9599 variables.CompressedApex = proptools.BoolPtr(true)
9600 }))
9601 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9602 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9603 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9604 })
9605 }
9606}
9607
satayev758968a2021-12-06 11:42:40 +00009608func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9609 preparer := android.GroupFixturePreparers(
9610 PrepareForTestWithApexBuildComponents,
9611 prepareForTestWithMyapex,
9612 java.PrepareForTestWithJavaSdkLibraryFiles,
9613 java.PrepareForTestWithJavaDefaultModules,
9614 android.PrepareForTestWithAndroidBuildComponents,
9615 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9616 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9617 )
9618
9619 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9620 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9621 preparer.RunTestWithBp(t, `
9622 apex {
9623 name: "myapex",
9624 key: "myapex.key",
9625 bootclasspath_fragments: ["mybootclasspathfragment"],
9626 min_sdk_version: "30",
9627 updatable: false,
9628 }
9629
9630 apex_key {
9631 name: "myapex.key",
9632 public_key: "testkey.avbpubkey",
9633 private_key: "testkey.pem",
9634 }
9635
9636 bootclasspath_fragment {
9637 name: "mybootclasspathfragment",
9638 contents: ["mybootclasspathlib"],
9639 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009640 hidden_api: {
9641 split_packages: ["*"],
9642 },
satayev758968a2021-12-06 11:42:40 +00009643 }
9644
9645 java_sdk_library {
9646 name: "mybootclasspathlib",
9647 srcs: ["mybootclasspathlib.java"],
9648 apex_available: ["myapex"],
9649 compile_dex: true,
9650 unsafe_ignore_missing_latest_api: true,
9651 min_sdk_version: "31",
9652 static_libs: ["util"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00009653 sdk_version: "core_current",
satayev758968a2021-12-06 11:42:40 +00009654 }
9655
9656 java_library {
9657 name: "util",
9658 srcs: ["a.java"],
9659 apex_available: ["myapex"],
9660 min_sdk_version: "31",
9661 static_libs: ["another_util"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00009662 sdk_version: "core_current",
satayev758968a2021-12-06 11:42:40 +00009663 }
9664
9665 java_library {
9666 name: "another_util",
9667 srcs: ["a.java"],
9668 min_sdk_version: "31",
9669 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00009670 sdk_version: "core_current",
satayev758968a2021-12-06 11:42:40 +00009671 }
9672 `)
9673 })
9674
9675 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9676 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9677 preparer.RunTestWithBp(t, `
9678 apex {
9679 name: "myapex",
9680 key: "myapex.key",
9681 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9682 min_sdk_version: "30",
9683 updatable: false,
9684 }
9685
9686 apex_key {
9687 name: "myapex.key",
9688 public_key: "testkey.avbpubkey",
9689 private_key: "testkey.pem",
9690 }
9691
9692 systemserverclasspath_fragment {
9693 name: "mysystemserverclasspathfragment",
9694 contents: ["mysystemserverclasspathlib"],
9695 apex_available: ["myapex"],
9696 }
9697
9698 java_sdk_library {
9699 name: "mysystemserverclasspathlib",
9700 srcs: ["mysystemserverclasspathlib.java"],
9701 apex_available: ["myapex"],
9702 compile_dex: true,
9703 min_sdk_version: "32",
9704 unsafe_ignore_missing_latest_api: true,
9705 static_libs: ["util"],
9706 }
9707
9708 java_library {
9709 name: "util",
9710 srcs: ["a.java"],
9711 apex_available: ["myapex"],
9712 min_sdk_version: "31",
9713 static_libs: ["another_util"],
9714 }
9715
9716 java_library {
9717 name: "another_util",
9718 srcs: ["a.java"],
9719 min_sdk_version: "31",
9720 apex_available: ["myapex"],
9721 }
9722 `)
9723 })
9724
9725 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
Jihoon Kang85bc1932024-07-01 17:04:46 +00009726 preparer.
satayev758968a2021-12-06 11:42:40 +00009727 RunTestWithBp(t, `
9728 apex {
9729 name: "myapex",
9730 key: "myapex.key",
9731 bootclasspath_fragments: ["mybootclasspathfragment"],
9732 min_sdk_version: "30",
9733 updatable: false,
9734 }
9735
9736 apex_key {
9737 name: "myapex.key",
9738 public_key: "testkey.avbpubkey",
9739 private_key: "testkey.pem",
9740 }
9741
9742 bootclasspath_fragment {
9743 name: "mybootclasspathfragment",
9744 contents: ["mybootclasspathlib"],
9745 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009746 hidden_api: {
9747 split_packages: ["*"],
9748 },
satayev758968a2021-12-06 11:42:40 +00009749 }
9750
9751 java_sdk_library {
9752 name: "mybootclasspathlib",
9753 srcs: ["mybootclasspathlib.java"],
9754 apex_available: ["myapex"],
9755 compile_dex: true,
9756 unsafe_ignore_missing_latest_api: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00009757 sdk_version: "current",
9758 min_sdk_version: "30",
satayev758968a2021-12-06 11:42:40 +00009759 }
9760 `)
9761 })
9762
9763 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9764 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9765 RunTestWithBp(t, `
9766 apex {
9767 name: "myapex",
9768 key: "myapex.key",
9769 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9770 min_sdk_version: "30",
9771 updatable: false,
9772 }
9773
9774 apex_key {
9775 name: "myapex.key",
9776 public_key: "testkey.avbpubkey",
9777 private_key: "testkey.pem",
9778 }
9779
9780 systemserverclasspath_fragment {
9781 name: "mysystemserverclasspathfragment",
9782 contents: ["mysystemserverclasspathlib"],
9783 apex_available: ["myapex"],
9784 }
9785
9786 java_sdk_library {
9787 name: "mysystemserverclasspathlib",
9788 srcs: ["mysystemserverclasspathlib.java"],
9789 apex_available: ["myapex"],
9790 compile_dex: true,
9791 unsafe_ignore_missing_latest_api: true,
9792 }
9793 `)
9794 })
9795}
9796
Jiakai Zhang6decef92022-01-12 17:56:19 +00009797// Verifies that the APEX depends on all the Make modules in the list.
9798func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9799 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9800 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009801 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009802 }
9803}
9804
9805// Verifies that the APEX does not depend on any of the Make modules in the list.
9806func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9807 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9808 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009809 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009810 }
9811}
9812
Cole Faust24e25c02024-01-19 14:12:17 -08009813func TestApexStrictUpdtabilityLint(t *testing.T) {
9814 bpTemplate := `
9815 apex {
9816 name: "myapex",
9817 key: "myapex.key",
9818 java_libs: ["myjavalib"],
9819 updatable: %v,
9820 min_sdk_version: "29",
9821 }
9822 apex_key {
9823 name: "myapex.key",
9824 }
9825 java_library {
9826 name: "myjavalib",
9827 srcs: ["MyClass.java"],
9828 apex_available: [ "myapex" ],
9829 lint: {
9830 strict_updatability_linting: %v,
9831 %s
9832 },
9833 sdk_version: "current",
9834 min_sdk_version: "29",
9835 }
9836 `
9837 fs := android.MockFS{
9838 "lint-baseline.xml": nil,
9839 }
9840
9841 testCases := []struct {
Colin Cross87427352024-09-25 15:41:19 -07009842 testCaseName string
9843 apexUpdatable bool
9844 javaStrictUpdtabilityLint bool
9845 lintFileExists bool
9846 disallowedFlagExpectedOnApex bool
9847 disallowedFlagExpectedOnJavalib bool
Cole Faust24e25c02024-01-19 14:12:17 -08009848 }{
9849 {
Colin Cross87427352024-09-25 15:41:19 -07009850 testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9851 apexUpdatable: true,
9852 javaStrictUpdtabilityLint: true,
9853 lintFileExists: false,
9854 disallowedFlagExpectedOnApex: false,
9855 disallowedFlagExpectedOnJavalib: false,
Cole Faust24e25c02024-01-19 14:12:17 -08009856 },
9857 {
Colin Cross87427352024-09-25 15:41:19 -07009858 testCaseName: "non-updatable apex respects strict_updatability of javalib",
9859 apexUpdatable: false,
9860 javaStrictUpdtabilityLint: false,
9861 lintFileExists: true,
9862 disallowedFlagExpectedOnApex: false,
9863 disallowedFlagExpectedOnJavalib: false,
Cole Faust24e25c02024-01-19 14:12:17 -08009864 },
9865 {
Colin Cross87427352024-09-25 15:41:19 -07009866 testCaseName: "non-updatable apex respects strict updatability of javalib",
9867 apexUpdatable: false,
9868 javaStrictUpdtabilityLint: true,
9869 lintFileExists: true,
9870 disallowedFlagExpectedOnApex: false,
9871 disallowedFlagExpectedOnJavalib: true,
Cole Faust24e25c02024-01-19 14:12:17 -08009872 },
9873 {
Colin Cross87427352024-09-25 15:41:19 -07009874 testCaseName: "updatable apex checks strict updatability of javalib",
9875 apexUpdatable: true,
9876 javaStrictUpdtabilityLint: false,
9877 lintFileExists: true,
9878 disallowedFlagExpectedOnApex: true,
9879 disallowedFlagExpectedOnJavalib: false,
Cole Faust24e25c02024-01-19 14:12:17 -08009880 },
9881 }
9882
9883 for _, testCase := range testCases {
Colin Cross87427352024-09-25 15:41:19 -07009884 t.Run(testCase.testCaseName, func(t *testing.T) {
9885 fixtures := []android.FixturePreparer{}
9886 baselineProperty := ""
9887 if testCase.lintFileExists {
9888 fixtures = append(fixtures, fs.AddToFixture())
9889 baselineProperty = "baseline_filename: \"lint-baseline.xml\""
9890 }
9891 bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint, baselineProperty)
Cole Faust24e25c02024-01-19 14:12:17 -08009892
Colin Cross87427352024-09-25 15:41:19 -07009893 result := testApex(t, bp, fixtures...)
Cole Faust24e25c02024-01-19 14:12:17 -08009894
Colin Cross87427352024-09-25 15:41:19 -07009895 checkModule := func(m android.TestingBuildParams, name string, expectStrictUpdatability bool) {
9896 if expectStrictUpdatability {
9897 if m.Rule == nil {
9898 t.Errorf("expected strict updatability check rule on %s", name)
9899 } else {
9900 android.AssertStringDoesContain(t, fmt.Sprintf("strict updatability check rule for %s", name),
9901 m.RuleParams.Command, "--disallowed_issues NewApi")
9902 android.AssertStringListContains(t, fmt.Sprintf("strict updatability check baselines for %s", name),
9903 m.Inputs.Strings(), "lint-baseline.xml")
9904 }
9905 } else {
9906 if m.Rule != nil {
9907 t.Errorf("expected no strict updatability check rule on %s", name)
9908 }
9909 }
9910 }
9911
9912 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9913 apex := result.ModuleForTests("myapex", "android_common_myapex")
9914 apexStrictUpdatabilityCheck := apex.MaybeOutput("lint_strict_updatability_check.stamp")
9915 javalibStrictUpdatabilityCheck := myjavalib.MaybeOutput("lint_strict_updatability_check.stamp")
9916
9917 checkModule(apexStrictUpdatabilityCheck, "myapex", testCase.disallowedFlagExpectedOnApex)
9918 checkModule(javalibStrictUpdatabilityCheck, "myjavalib", testCase.disallowedFlagExpectedOnJavalib)
9919 })
Cole Faust24e25c02024-01-19 14:12:17 -08009920 }
9921}
9922
Cole Faust24e25c02024-01-19 14:12:17 -08009923// checks transtive deps of an apex coming from bootclasspath_fragment
9924func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9925 bp := `
9926 apex {
9927 name: "myapex",
9928 key: "myapex.key",
9929 bootclasspath_fragments: ["mybootclasspathfragment"],
9930 updatable: true,
9931 min_sdk_version: "29",
9932 }
9933 apex_key {
9934 name: "myapex.key",
9935 }
9936 bootclasspath_fragment {
9937 name: "mybootclasspathfragment",
9938 contents: ["myjavalib"],
9939 apex_available: ["myapex"],
9940 hidden_api: {
9941 split_packages: ["*"],
9942 },
9943 }
9944 java_library {
9945 name: "myjavalib",
9946 srcs: ["MyClass.java"],
9947 apex_available: [ "myapex" ],
9948 sdk_version: "current",
9949 min_sdk_version: "29",
9950 compile_dex: true,
9951 lint: {
9952 baseline_filename: "lint-baseline.xml",
9953 }
9954 }
9955 `
9956 fs := android.MockFS{
9957 "lint-baseline.xml": nil,
9958 }
9959
9960 result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
Colin Cross87427352024-09-25 15:41:19 -07009961 apex := result.ModuleForTests("myapex", "android_common_myapex")
9962 apexStrictUpdatabilityCheck := apex.Output("lint_strict_updatability_check.stamp")
9963 android.AssertStringDoesContain(t, "strict updatability check rule for myapex",
9964 apexStrictUpdatabilityCheck.RuleParams.Command, "--disallowed_issues NewApi")
9965 android.AssertStringListContains(t, "strict updatability check baselines for myapex",
9966 apexStrictUpdatabilityCheck.Inputs.Strings(), "lint-baseline.xml")
Cole Faust24e25c02024-01-19 14:12:17 -08009967}
Spandan Das66773252022-01-15 00:23:18 +00009968
Jihoon Kang784c0052024-06-25 22:15:39 +00009969func TestApexLintBcpFragmentSdkLibDeps(t *testing.T) {
9970 bp := `
9971 apex {
9972 name: "myapex",
9973 key: "myapex.key",
9974 bootclasspath_fragments: ["mybootclasspathfragment"],
9975 min_sdk_version: "29",
Jihoon Kang85bc1932024-07-01 17:04:46 +00009976 java_libs: [
9977 "jacocoagent",
9978 ],
Jihoon Kang784c0052024-06-25 22:15:39 +00009979 }
9980 apex_key {
9981 name: "myapex.key",
9982 }
9983 bootclasspath_fragment {
9984 name: "mybootclasspathfragment",
9985 contents: ["foo"],
9986 apex_available: ["myapex"],
9987 hidden_api: {
9988 split_packages: ["*"],
9989 },
9990 }
9991 java_sdk_library {
9992 name: "foo",
9993 srcs: ["MyClass.java"],
9994 apex_available: [ "myapex" ],
9995 sdk_version: "current",
9996 min_sdk_version: "29",
9997 compile_dex: true,
9998 }
9999 `
10000 fs := android.MockFS{
10001 "lint-baseline.xml": nil,
10002 }
10003
10004 result := android.GroupFixturePreparers(
10005 prepareForApexTest,
10006 java.PrepareForTestWithJavaSdkLibraryFiles,
10007 java.PrepareForTestWithJacocoInstrumentation,
10008 java.FixtureWithLastReleaseApis("foo"),
Jihoon Kang784c0052024-06-25 22:15:39 +000010009 android.FixtureMergeMockFs(fs),
10010 ).RunTestWithBp(t, bp)
10011
10012 myapex := result.ModuleForTests("myapex", "android_common_myapex")
10013 lintReportInputs := strings.Join(myapex.Output("lint-report-xml.zip").Inputs.Strings(), " ")
10014 android.AssertStringDoesContain(t,
10015 "myapex lint report expected to contain that of the sdk library impl lib as an input",
10016 lintReportInputs, "foo.impl")
10017}
10018
Spandan Das42e89502022-05-06 22:12:55 +000010019// updatable apexes should propagate updatable=true to its apps
10020func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
10021 bp := `
10022 apex {
10023 name: "myapex",
10024 key: "myapex.key",
Colin Cross99939e92024-10-01 16:02:46 -070010025 updatable: true,
Spandan Das42e89502022-05-06 22:12:55 +000010026 apps: [
10027 "myapp",
10028 ],
10029 min_sdk_version: "30",
10030 }
10031 apex_key {
10032 name: "myapex.key",
10033 }
10034 android_app {
10035 name: "myapp",
Spandan Das42e89502022-05-06 22:12:55 +000010036 apex_available: [
10037 "myapex",
10038 ],
10039 sdk_version: "current",
10040 min_sdk_version: "30",
10041 }
10042 `
Colin Cross99939e92024-10-01 16:02:46 -070010043 _ = android.GroupFixturePreparers(
10044 prepareForApexTest,
10045 ).ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("app dependency myapp must have updatable: true")).
10046 RunTestWithBp(t, bp)
Spandan Das42e89502022-05-06 22:12:55 +000010047}
10048
Dennis Shend4f5d932023-01-31 20:27:21 +000010049func TestTrimmedApex(t *testing.T) {
10050 bp := `
10051 apex {
10052 name: "myapex",
10053 key: "myapex.key",
10054 native_shared_libs: ["libfoo","libbaz"],
10055 min_sdk_version: "29",
10056 trim_against: "mydcla",
10057 }
10058 apex {
10059 name: "mydcla",
10060 key: "myapex.key",
10061 native_shared_libs: ["libfoo","libbar"],
10062 min_sdk_version: "29",
10063 file_contexts: ":myapex-file_contexts",
10064 dynamic_common_lib_apex: true,
10065 }
10066 apex_key {
10067 name: "myapex.key",
10068 }
10069 cc_library {
10070 name: "libfoo",
10071 shared_libs: ["libc"],
10072 apex_available: ["myapex","mydcla"],
10073 min_sdk_version: "29",
10074 }
10075 cc_library {
10076 name: "libbar",
10077 shared_libs: ["libc"],
10078 apex_available: ["myapex","mydcla"],
10079 min_sdk_version: "29",
10080 }
10081 cc_library {
10082 name: "libbaz",
10083 shared_libs: ["libc"],
10084 apex_available: ["myapex","mydcla"],
10085 min_sdk_version: "29",
10086 }
Dennis Shend4f5d932023-01-31 20:27:21 +000010087 `
10088 ctx := testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +090010089 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dennis Shend4f5d932023-01-31 20:27:21 +000010090 apexRule := module.MaybeRule("apexRule")
10091 if apexRule.Rule == nil {
10092 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10093 }
10094
10095 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
Jooyung Hana0503a52023-08-23 13:12:50 +090010096 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("TrimmedApexRule")
Dennis Shend4f5d932023-01-31 20:27:21 +000010097 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10098 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10099 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10100 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10101}
Jingwen Chendea7a642023-03-28 11:30:50 +000010102
10103func TestCannedFsConfig(t *testing.T) {
10104 ctx := testApex(t, `
10105 apex {
10106 name: "myapex",
10107 key: "myapex.key",
10108 updatable: false,
10109 }
10110
10111 apex_key {
10112 name: "myapex.key",
10113 public_key: "testkey.avbpubkey",
10114 private_key: "testkey.pem",
10115 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010116 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010117 generateFsRule := mod.Rule("generateFsConfig")
10118 cmd := generateFsRule.RuleParams.Command
10119
10120 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10121}
10122
10123func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10124 ctx := testApex(t, `
10125 apex {
10126 name: "myapex",
10127 key: "myapex.key",
10128 canned_fs_config: "my_config",
10129 updatable: false,
10130 }
10131
10132 apex_key {
10133 name: "myapex.key",
10134 public_key: "testkey.avbpubkey",
10135 private_key: "testkey.pem",
10136 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010137 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010138 generateFsRule := mod.Rule("generateFsConfig")
10139 cmd := generateFsRule.RuleParams.Command
10140
10141 // Ensure that canned_fs_config has "cat my_config" at the end
10142 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10143}
Spandan Das20fce2d2023-04-12 17:21:39 +000010144
10145func TestStubLibrariesMultipleApexViolation(t *testing.T) {
10146 testCases := []struct {
10147 desc string
10148 hasStubs bool
10149 apexAvailable string
10150 expectedError string
10151 }{
10152 {
10153 desc: "non-stub library can have multiple apex_available",
10154 hasStubs: false,
10155 apexAvailable: `["myapex", "otherapex"]`,
10156 },
10157 {
10158 desc: "stub library should not be available to anyapex",
10159 hasStubs: true,
10160 apexAvailable: `["//apex_available:anyapex"]`,
10161 expectedError: "Stub libraries should have a single apex_available.*anyapex",
10162 },
10163 {
10164 desc: "stub library should not be available to multiple apexes",
10165 hasStubs: true,
10166 apexAvailable: `["myapex", "otherapex"]`,
10167 expectedError: "Stub libraries should have a single apex_available.*myapex.*otherapex",
10168 },
10169 {
10170 desc: "stub library can be available to a core apex and a test apex",
10171 hasStubs: true,
10172 apexAvailable: `["myapex", "test_myapex"]`,
10173 },
10174 }
10175 bpTemplate := `
10176 cc_library {
10177 name: "libfoo",
10178 %v
10179 apex_available: %v,
10180 }
10181 apex {
10182 name: "myapex",
10183 key: "apex.key",
10184 updatable: false,
10185 native_shared_libs: ["libfoo"],
10186 }
10187 apex {
10188 name: "otherapex",
10189 key: "apex.key",
10190 updatable: false,
10191 }
10192 apex_test {
10193 name: "test_myapex",
10194 key: "apex.key",
10195 updatable: false,
10196 native_shared_libs: ["libfoo"],
10197 }
10198 apex_key {
10199 name: "apex.key",
10200 }
10201 `
10202 for _, tc := range testCases {
10203 stubs := ""
10204 if tc.hasStubs {
10205 stubs = `stubs: {symbol_file: "libfoo.map.txt"},`
10206 }
10207 bp := fmt.Sprintf(bpTemplate, stubs, tc.apexAvailable)
10208 mockFsFixturePreparer := android.FixtureModifyMockFS(func(fs android.MockFS) {
10209 fs["system/sepolicy/apex/test_myapex-file_contexts"] = nil
10210 })
10211 if tc.expectedError == "" {
10212 testApex(t, bp, mockFsFixturePreparer)
10213 } else {
10214 testApexError(t, tc.expectedError, bp, mockFsFixturePreparer)
10215 }
10216 }
10217}
Colin Crossbd3a16b2023-04-25 11:30:51 -070010218
10219func TestFileSystemShouldSkipApexLibraries(t *testing.T) {
10220 context := android.GroupFixturePreparers(
10221 android.PrepareForIntegrationTestWithAndroid,
10222 cc.PrepareForIntegrationTestWithCc,
10223 PrepareForTestWithApexBuildComponents,
10224 prepareForTestWithMyapex,
10225 filesystem.PrepareForTestWithFilesystemBuildComponents,
10226 )
10227 result := context.RunTestWithBp(t, `
10228 android_system_image {
10229 name: "myfilesystem",
10230 deps: [
10231 "libfoo",
10232 ],
10233 linker_config_src: "linker.config.json",
10234 }
10235
10236 cc_library {
10237 name: "libfoo",
10238 shared_libs: [
10239 "libbar",
10240 ],
10241 stl: "none",
10242 }
10243
10244 cc_library {
10245 name: "libbar",
10246 stl: "none",
10247 apex_available: ["myapex"],
10248 }
10249
10250 apex {
10251 name: "myapex",
10252 native_shared_libs: ["libbar"],
10253 key: "myapex.key",
10254 updatable: false,
10255 }
10256
10257 apex_key {
10258 name: "myapex.key",
10259 public_key: "testkey.avbpubkey",
10260 private_key: "testkey.pem",
10261 }
10262 `)
10263
Cole Faust3b806d32024-03-11 15:15:03 -070010264 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits
Colin Crossbd3a16b2023-04-25 11:30:51 -070010265 android.AssertStringListDoesNotContain(t, "filesystem should not have libbar",
10266 inputs.Strings(),
10267 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
10268}
Yu Liueae7b362023-11-16 17:05:47 -080010269
10270var apex_default_bp = `
10271 apex_key {
10272 name: "myapex.key",
10273 public_key: "testkey.avbpubkey",
10274 private_key: "testkey.pem",
10275 }
10276
10277 filegroup {
10278 name: "myapex.manifest",
10279 srcs: ["apex_manifest.json"],
10280 }
10281
10282 filegroup {
10283 name: "myapex.androidmanifest",
10284 srcs: ["AndroidManifest.xml"],
10285 }
10286`
10287
10288func TestAconfigFilesJavaDeps(t *testing.T) {
10289 ctx := testApex(t, apex_default_bp+`
10290 apex {
10291 name: "myapex",
10292 manifest: ":myapex.manifest",
10293 androidManifest: ":myapex.androidmanifest",
10294 key: "myapex.key",
10295 java_libs: [
10296 "my_java_library_foo",
10297 "my_java_library_bar",
10298 ],
10299 updatable: false,
10300 }
10301
10302 java_library {
10303 name: "my_java_library_foo",
10304 srcs: ["foo/bar/MyClass.java"],
10305 sdk_version: "none",
10306 system_modules: "none",
10307 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010308 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010309 "myapex",
10310 ],
10311 }
10312
10313 java_library {
10314 name: "my_java_library_bar",
10315 srcs: ["foo/bar/MyClass.java"],
10316 sdk_version: "none",
10317 system_modules: "none",
10318 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010319 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010320 "myapex",
10321 ],
10322 }
10323
10324 aconfig_declarations {
10325 name: "my_aconfig_declarations_foo",
10326 package: "com.example.package",
10327 container: "myapex",
10328 srcs: ["foo.aconfig"],
10329 }
10330
10331 java_aconfig_library {
10332 name: "my_java_aconfig_library_foo",
10333 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010334 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010335 "myapex",
10336 ],
10337 }
10338
10339 aconfig_declarations {
10340 name: "my_aconfig_declarations_bar",
10341 package: "com.example.package",
10342 container: "myapex",
10343 srcs: ["bar.aconfig"],
10344 }
10345
10346 java_aconfig_library {
10347 name: "my_java_aconfig_library_bar",
10348 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010349 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010350 "myapex",
10351 ],
10352 }
10353 `)
10354
10355 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10356 s := mod.Rule("apexRule").Args["copy_commands"]
10357 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Dennis Shen343d09e2024-09-23 17:19:15 +000010358 if len(copyCmds) != 14 {
10359 t.Fatalf("Expected 14 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010360 }
10361
Jooyung Hana3fddf42024-09-03 13:22:21 +090010362 ensureListContainsMatch(t, copyCmds, "^cp -f .*/aconfig_flags.pb .*/image.apex/etc/aconfig_flags.pb")
10363 ensureListContainsMatch(t, copyCmds, "^cp -f .*/package.map .*/image.apex/etc/package.map")
10364 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.map .*/image.apex/etc/flag.map")
10365 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.val .*/image.apex/etc/flag.val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010366 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.info.*/image.apex/etc/flag.info")
Yu Liueae7b362023-11-16 17:05:47 -080010367
Yu Liubba555e2024-02-17 00:36:42 +000010368 inputs := []string{
10369 "my_aconfig_declarations_foo/intermediate.pb",
10370 "my_aconfig_declarations_bar/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010371 }
Yu Liubba555e2024-02-17 00:36:42 +000010372 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10373 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10374 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10375 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010376 VerifyAconfigRule(t, &mod, "create_aconfig_flag_info_file", inputs, "android_common_myapex/flag.info", "myapex", "flag_info")
Yu Liueae7b362023-11-16 17:05:47 -080010377}
10378
10379func TestAconfigFilesJavaAndCcDeps(t *testing.T) {
10380 ctx := testApex(t, apex_default_bp+`
10381 apex {
10382 name: "myapex",
10383 manifest: ":myapex.manifest",
10384 androidManifest: ":myapex.androidmanifest",
10385 key: "myapex.key",
10386 java_libs: [
10387 "my_java_library_foo",
10388 ],
10389 native_shared_libs: [
10390 "my_cc_library_bar",
10391 ],
10392 binaries: [
10393 "my_cc_binary_baz",
10394 ],
10395 updatable: false,
10396 }
10397
10398 java_library {
10399 name: "my_java_library_foo",
10400 srcs: ["foo/bar/MyClass.java"],
10401 sdk_version: "none",
10402 system_modules: "none",
10403 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010404 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010405 "myapex",
10406 ],
10407 }
10408
10409 cc_library {
10410 name: "my_cc_library_bar",
10411 srcs: ["foo/bar/MyClass.cc"],
Yu Liucec0e412023-11-30 16:45:50 -080010412 static_libs: [
10413 "my_cc_aconfig_library_bar",
10414 "my_cc_aconfig_library_baz",
10415 ],
Yu Liueae7b362023-11-16 17:05:47 -080010416 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010417 "myapex",
10418 ],
10419 }
10420
10421 cc_binary {
10422 name: "my_cc_binary_baz",
10423 srcs: ["foo/bar/MyClass.cc"],
10424 static_libs: ["my_cc_aconfig_library_baz"],
Yu Liueae7b362023-11-16 17:05:47 -080010425 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010426 "myapex",
10427 ],
10428 }
10429
10430 aconfig_declarations {
10431 name: "my_aconfig_declarations_foo",
10432 package: "com.example.package",
10433 container: "myapex",
10434 srcs: ["foo.aconfig"],
10435 }
10436
10437 java_aconfig_library {
10438 name: "my_java_aconfig_library_foo",
10439 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010440 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010441 "myapex",
10442 ],
10443 }
10444
10445 aconfig_declarations {
10446 name: "my_aconfig_declarations_bar",
10447 package: "com.example.package",
10448 container: "myapex",
10449 srcs: ["bar.aconfig"],
10450 }
10451
10452 cc_aconfig_library {
10453 name: "my_cc_aconfig_library_bar",
10454 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010455 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010456 "myapex",
10457 ],
10458 }
10459
10460 aconfig_declarations {
10461 name: "my_aconfig_declarations_baz",
10462 package: "com.example.package",
10463 container: "myapex",
10464 srcs: ["baz.aconfig"],
10465 }
10466
10467 cc_aconfig_library {
10468 name: "my_cc_aconfig_library_baz",
10469 aconfig_declarations: "my_aconfig_declarations_baz",
Yu Liueae7b362023-11-16 17:05:47 -080010470 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010471 "myapex",
10472 ],
10473 }
10474
10475 cc_library {
10476 name: "server_configurable_flags",
10477 srcs: ["server_configurable_flags.cc"],
10478 }
Ted Bauerf0f18592024-04-23 18:25:26 +000010479 cc_library {
10480 name: "libbase",
10481 srcs: ["libbase.cc"],
Ted Bauer1e96f8c2024-04-25 19:50:01 +000010482 apex_available: [
10483 "myapex",
10484 ],
Ted Bauerf0f18592024-04-23 18:25:26 +000010485 }
10486 cc_library {
10487 name: "libaconfig_storage_read_api_cc",
10488 srcs: ["libaconfig_storage_read_api_cc.cc"],
10489 }
Yu Liueae7b362023-11-16 17:05:47 -080010490 `)
10491
10492 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10493 s := mod.Rule("apexRule").Args["copy_commands"]
10494 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Dennis Shen343d09e2024-09-23 17:19:15 +000010495 if len(copyCmds) != 18 {
10496 t.Fatalf("Expected 18 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010497 }
10498
Jooyung Hana3fddf42024-09-03 13:22:21 +090010499 ensureListContainsMatch(t, copyCmds, "^cp -f .*/aconfig_flags.pb .*/image.apex/etc/aconfig_flags.pb")
10500 ensureListContainsMatch(t, copyCmds, "^cp -f .*/package.map .*/image.apex/etc/package.map")
10501 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.map .*/image.apex/etc/flag.map")
10502 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.val .*/image.apex/etc/flag.val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010503 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.info .*/image.apex/etc/flag.info")
Yu Liueae7b362023-11-16 17:05:47 -080010504
Yu Liubba555e2024-02-17 00:36:42 +000010505 inputs := []string{
10506 "my_aconfig_declarations_foo/intermediate.pb",
10507 "my_cc_library_bar/android_arm64_armv8-a_shared_apex10000/myapex/aconfig_merged.pb",
10508 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010509 }
Yu Liubba555e2024-02-17 00:36:42 +000010510 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10511 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10512 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10513 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010514 VerifyAconfigRule(t, &mod, "create_aconfig_flag_info_file", inputs, "android_common_myapex/flag.info", "myapex", "flag_info")
Yu Liueae7b362023-11-16 17:05:47 -080010515}
10516
Yu Liucec0e412023-11-30 16:45:50 -080010517func TestAconfigFilesRustDeps(t *testing.T) {
10518 ctx := testApex(t, apex_default_bp+`
10519 apex {
10520 name: "myapex",
10521 manifest: ":myapex.manifest",
10522 androidManifest: ":myapex.androidmanifest",
10523 key: "myapex.key",
10524 native_shared_libs: [
10525 "libmy_rust_library",
10526 ],
10527 binaries: [
10528 "my_rust_binary",
10529 ],
10530 rust_dyn_libs: [
10531 "libmy_rust_dylib",
10532 ],
10533 updatable: false,
10534 }
10535
10536 rust_library {
10537 name: "libflags_rust", // test mock
10538 crate_name: "flags_rust",
10539 srcs: ["lib.rs"],
10540 apex_available: [
10541 "myapex",
10542 ],
10543 }
10544
10545 rust_library {
10546 name: "liblazy_static", // test mock
10547 crate_name: "lazy_static",
10548 srcs: ["src/lib.rs"],
10549 apex_available: [
10550 "myapex",
10551 ],
10552 }
10553
Ted Bauer02d475c2024-03-27 20:56:26 +000010554 rust_library {
10555 name: "libaconfig_storage_read_api", // test mock
10556 crate_name: "aconfig_storage_read_api",
10557 srcs: ["src/lib.rs"],
10558 apex_available: [
10559 "myapex",
10560 ],
10561 }
10562
Ted Bauer6ef40db2024-03-29 14:04:10 +000010563 rust_library {
10564 name: "liblogger", // test mock
10565 crate_name: "logger",
10566 srcs: ["src/lib.rs"],
10567 apex_available: [
10568 "myapex",
10569 ],
10570 }
10571
10572 rust_library {
10573 name: "liblog_rust", // test mock
10574 crate_name: "log_rust",
10575 srcs: ["src/lib.rs"],
10576 apex_available: [
10577 "myapex",
10578 ],
10579 }
10580
Yu Liucec0e412023-11-30 16:45:50 -080010581 rust_ffi_shared {
10582 name: "libmy_rust_library",
10583 srcs: ["src/lib.rs"],
10584 rustlibs: ["libmy_rust_aconfig_library_foo"],
10585 crate_name: "my_rust_library",
10586 apex_available: [
10587 "myapex",
10588 ],
10589 }
10590
10591 rust_library_dylib {
10592 name: "libmy_rust_dylib",
10593 srcs: ["foo/bar/MyClass.rs"],
10594 rustlibs: ["libmy_rust_aconfig_library_bar"],
10595 crate_name: "my_rust_dylib",
10596 apex_available: [
10597 "myapex",
10598 ],
10599 }
10600
10601 rust_binary {
10602 name: "my_rust_binary",
10603 srcs: ["foo/bar/MyClass.rs"],
10604 rustlibs: [
10605 "libmy_rust_aconfig_library_baz",
10606 "libmy_rust_dylib",
10607 ],
10608 apex_available: [
10609 "myapex",
10610 ],
10611 }
10612
10613 aconfig_declarations {
10614 name: "my_aconfig_declarations_foo",
10615 package: "com.example.package",
10616 container: "myapex",
10617 srcs: ["foo.aconfig"],
10618 }
10619
10620 aconfig_declarations {
10621 name: "my_aconfig_declarations_bar",
10622 package: "com.example.package",
10623 container: "myapex",
10624 srcs: ["bar.aconfig"],
10625 }
10626
10627 aconfig_declarations {
10628 name: "my_aconfig_declarations_baz",
10629 package: "com.example.package",
10630 container: "myapex",
10631 srcs: ["baz.aconfig"],
10632 }
10633
10634 rust_aconfig_library {
10635 name: "libmy_rust_aconfig_library_foo",
10636 aconfig_declarations: "my_aconfig_declarations_foo",
10637 crate_name: "my_rust_aconfig_library_foo",
10638 apex_available: [
10639 "myapex",
10640 ],
10641 }
10642
10643 rust_aconfig_library {
10644 name: "libmy_rust_aconfig_library_bar",
10645 aconfig_declarations: "my_aconfig_declarations_bar",
10646 crate_name: "my_rust_aconfig_library_bar",
10647 apex_available: [
10648 "myapex",
10649 ],
10650 }
10651
10652 rust_aconfig_library {
10653 name: "libmy_rust_aconfig_library_baz",
10654 aconfig_declarations: "my_aconfig_declarations_baz",
10655 crate_name: "my_rust_aconfig_library_baz",
10656 apex_available: [
10657 "myapex",
10658 ],
10659 }
10660 `)
10661
10662 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10663 s := mod.Rule("apexRule").Args["copy_commands"]
10664 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Dennis Shen343d09e2024-09-23 17:19:15 +000010665 if len(copyCmds) != 38 {
10666 t.Fatalf("Expected 38 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liucec0e412023-11-30 16:45:50 -080010667 }
10668
Jooyung Hana3fddf42024-09-03 13:22:21 +090010669 ensureListContainsMatch(t, copyCmds, "^cp -f .*/aconfig_flags.pb .*/image.apex/etc/aconfig_flags.pb")
10670 ensureListContainsMatch(t, copyCmds, "^cp -f .*/package.map .*/image.apex/etc/package.map")
10671 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.map .*/image.apex/etc/flag.map")
10672 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.val .*/image.apex/etc/flag.val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010673 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.info .*/image.apex/etc/flag.info")
Yu Liucec0e412023-11-30 16:45:50 -080010674
Yu Liubba555e2024-02-17 00:36:42 +000010675 inputs := []string{
10676 "my_aconfig_declarations_foo/intermediate.pb",
Yu Liuab31c822024-02-28 22:21:31 +000010677 "my_aconfig_declarations_bar/intermediate.pb",
10678 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liubba555e2024-02-17 00:36:42 +000010679 "my_rust_binary/android_arm64_armv8-a_apex10000/myapex/aconfig_merged.pb",
10680 }
10681 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10682 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10683 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10684 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010685 VerifyAconfigRule(t, &mod, "create_aconfig_flag_info_file", inputs, "android_common_myapex/flag.info", "myapex", "flag_info")
Yu Liubba555e2024-02-17 00:36:42 +000010686}
10687
10688func VerifyAconfigRule(t *testing.T, mod *android.TestingModule, desc string, inputs []string, output string, container string, file_type string) {
10689 aconfigRule := mod.Description(desc)
10690 s := " " + aconfigRule.Args["cache_files"]
Yu Liucec0e412023-11-30 16:45:50 -080010691 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
Yu Liubba555e2024-02-17 00:36:42 +000010692 if len(aconfigArgs) != len(inputs) {
10693 t.Fatalf("Expected %d commands, got %d in:\n%s", len(inputs), len(aconfigArgs), s)
Yu Liucec0e412023-11-30 16:45:50 -080010694 }
Yu Liucec0e412023-11-30 16:45:50 -080010695
Yu Liubba555e2024-02-17 00:36:42 +000010696 ensureEquals(t, container, aconfigRule.Args["container"])
10697 ensureEquals(t, file_type, aconfigRule.Args["file_type"])
10698
10699 buildParams := aconfigRule.BuildParams
10700 for _, input := range inputs {
10701 android.EnsureListContainsSuffix(t, aconfigArgs, input)
10702 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), input)
Yu Liucec0e412023-11-30 16:45:50 -080010703 }
Yu Liubba555e2024-02-17 00:36:42 +000010704
10705 ensureContains(t, buildParams.Output.String(), output)
Yu Liucec0e412023-11-30 16:45:50 -080010706}
10707
Yu Liueae7b362023-11-16 17:05:47 -080010708func TestAconfigFilesOnlyMatchCurrentApex(t *testing.T) {
10709 ctx := testApex(t, apex_default_bp+`
10710 apex {
10711 name: "myapex",
10712 manifest: ":myapex.manifest",
10713 androidManifest: ":myapex.androidmanifest",
10714 key: "myapex.key",
10715 java_libs: [
10716 "my_java_library_foo",
10717 "other_java_library_bar",
10718 ],
10719 updatable: false,
10720 }
10721
10722 java_library {
10723 name: "my_java_library_foo",
10724 srcs: ["foo/bar/MyClass.java"],
10725 sdk_version: "none",
10726 system_modules: "none",
10727 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010728 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010729 "myapex",
10730 ],
10731 }
10732
10733 java_library {
10734 name: "other_java_library_bar",
10735 srcs: ["foo/bar/MyClass.java"],
10736 sdk_version: "none",
10737 system_modules: "none",
10738 static_libs: ["other_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010739 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010740 "myapex",
10741 ],
10742 }
10743
10744 aconfig_declarations {
10745 name: "my_aconfig_declarations_foo",
10746 package: "com.example.package",
10747 container: "myapex",
10748 srcs: ["foo.aconfig"],
10749 }
10750
10751 java_aconfig_library {
10752 name: "my_java_aconfig_library_foo",
10753 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010754 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010755 "myapex",
10756 ],
10757 }
10758
10759 aconfig_declarations {
10760 name: "other_aconfig_declarations_bar",
10761 package: "com.example.package",
10762 container: "otherapex",
10763 srcs: ["bar.aconfig"],
10764 }
10765
10766 java_aconfig_library {
10767 name: "other_java_aconfig_library_bar",
10768 aconfig_declarations: "other_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010769 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010770 "myapex",
10771 ],
10772 }
10773 `)
10774
10775 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10776 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
10777 s := " " + combineAconfigRule.Args["cache_files"]
10778 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
10779 if len(aconfigArgs) != 1 {
10780 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
10781 }
10782 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
10783
10784 buildParams := combineAconfigRule.BuildParams
10785 if len(buildParams.Inputs) != 1 {
10786 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
10787 }
10788 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
10789 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
10790}
10791
10792func TestAconfigFilesRemoveDuplicates(t *testing.T) {
10793 ctx := testApex(t, apex_default_bp+`
10794 apex {
10795 name: "myapex",
10796 manifest: ":myapex.manifest",
10797 androidManifest: ":myapex.androidmanifest",
10798 key: "myapex.key",
10799 java_libs: [
10800 "my_java_library_foo",
10801 "my_java_library_bar",
10802 ],
10803 updatable: false,
10804 }
10805
10806 java_library {
10807 name: "my_java_library_foo",
10808 srcs: ["foo/bar/MyClass.java"],
10809 sdk_version: "none",
10810 system_modules: "none",
10811 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010812 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010813 "myapex",
10814 ],
10815 }
10816
10817 java_library {
10818 name: "my_java_library_bar",
10819 srcs: ["foo/bar/MyClass.java"],
10820 sdk_version: "none",
10821 system_modules: "none",
10822 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010823 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010824 "myapex",
10825 ],
10826 }
10827
10828 aconfig_declarations {
10829 name: "my_aconfig_declarations_foo",
10830 package: "com.example.package",
10831 container: "myapex",
10832 srcs: ["foo.aconfig"],
10833 }
10834
10835 java_aconfig_library {
10836 name: "my_java_aconfig_library_foo",
10837 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010838 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010839 "myapex",
10840 ],
10841 }
10842
10843 java_aconfig_library {
10844 name: "my_java_aconfig_library_bar",
10845 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010846 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010847 "myapex",
10848 ],
10849 }
10850 `)
10851
10852 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10853 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
10854 s := " " + combineAconfigRule.Args["cache_files"]
10855 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
10856 if len(aconfigArgs) != 1 {
10857 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
10858 }
10859 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
10860
10861 buildParams := combineAconfigRule.BuildParams
10862 if len(buildParams.Inputs) != 1 {
10863 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
10864 }
10865 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
10866 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
10867}
Spandan Das5be63332023-12-13 00:06:32 +000010868
10869// Test that the boot jars come from the _selected_ apex prebuilt
10870// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
10871func TestBootDexJarsMultipleApexPrebuilts(t *testing.T) {
10872 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
10873 t.Helper()
10874 s := ctx.ModuleForTests("dex_bootjars", "android_common")
10875 foundLibfooJar := false
10876 base := stem + ".jar"
10877 for _, output := range s.AllOutputs() {
10878 if filepath.Base(output) == base {
10879 foundLibfooJar = true
10880 buildRule := s.Output(output)
10881 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
10882 }
10883 }
10884 if !foundLibfooJar {
10885 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
10886 }
10887 }
10888
Spandan Das64c9e0c2023-12-20 20:13:34 +000010889 // Check that the boot jars of the selected apex are run through boot_jars_package_check
10890 // This validates that the jars on the bootclasspath do not contain packages outside an allowlist
10891 checkBootJarsPackageCheck := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
10892 platformBcp := ctx.ModuleForTests("platform-bootclasspath", "android_common")
10893 bootJarsCheckRule := platformBcp.Rule("boot_jars_package_check")
10894 android.AssertStringMatches(t, "Could not find the correct boot dex jar in package check rule", bootJarsCheckRule.RuleParams.Command, "build/soong/scripts/check_boot_jars/package_allowed_list.txt.*"+expectedBootJar)
10895 }
10896
10897 // Check that the boot jars used to generate the monolithic hiddenapi flags come from the selected apex
10898 checkBootJarsForMonolithicHiddenapi := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
10899 monolithicHiddenapiFlagsCmd := ctx.ModuleForTests("platform-bootclasspath", "android_common").Output("out/soong/hiddenapi/hiddenapi-stub-flags.txt").RuleParams.Command
10900 android.AssertStringMatches(t, "Could not find the correct boot dex jar in monolithic hiddenapi flags generation command", monolithicHiddenapiFlagsCmd, "--boot-dex="+expectedBootJar)
10901 }
10902
Spandan Das5be63332023-12-13 00:06:32 +000010903 bp := `
10904 // Source APEX.
10905
10906 java_library {
10907 name: "framework-foo",
10908 srcs: ["foo.java"],
10909 installable: true,
10910 apex_available: [
10911 "com.android.foo",
10912 ],
10913 }
10914
10915 bootclasspath_fragment {
10916 name: "foo-bootclasspath-fragment",
10917 contents: ["framework-foo"],
10918 apex_available: [
10919 "com.android.foo",
10920 ],
10921 hidden_api: {
10922 split_packages: ["*"],
10923 },
10924 }
10925
10926 apex_key {
10927 name: "com.android.foo.key",
10928 public_key: "com.android.foo.avbpubkey",
10929 private_key: "com.android.foo.pem",
10930 }
10931
10932 apex {
10933 name: "com.android.foo",
10934 key: "com.android.foo.key",
10935 bootclasspath_fragments: ["foo-bootclasspath-fragment"],
10936 updatable: false,
10937 }
10938
10939 // Prebuilt APEX.
10940
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000010941 java_sdk_library_import {
Spandan Das5be63332023-12-13 00:06:32 +000010942 name: "framework-foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000010943 public: {
10944 jars: ["foo.jar"],
10945 },
Spandan Das5be63332023-12-13 00:06:32 +000010946 apex_available: ["com.android.foo"],
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000010947 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +000010948 }
10949
10950 prebuilt_bootclasspath_fragment {
10951 name: "foo-bootclasspath-fragment",
10952 contents: ["framework-foo"],
10953 hidden_api: {
10954 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
10955 metadata: "my-bootclasspath-fragment/metadata.csv",
10956 index: "my-bootclasspath-fragment/index.csv",
10957 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
10958 all_flags: "my-bootclasspath-fragment/all-flags.csv",
10959 },
10960 apex_available: [
10961 "com.android.foo",
10962 ],
10963 }
10964
10965 prebuilt_apex {
10966 name: "com.android.foo",
10967 apex_name: "com.android.foo",
10968 src: "com.android.foo-arm.apex",
10969 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
10970 }
10971
10972 // Another Prebuilt ART APEX
10973 prebuilt_apex {
10974 name: "com.android.foo.v2",
10975 apex_name: "com.android.foo", // Used to determine the API domain
10976 src: "com.android.foo-arm.apex",
10977 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
10978 }
10979
10980 // APEX contribution modules
10981
10982 apex_contributions {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000010983 name: "foo.source.contributions",
Spandan Das5be63332023-12-13 00:06:32 +000010984 api_domain: "com.android.foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000010985 contents: ["com.android.foo"],
10986 }
10987
10988 apex_contributions {
10989 name: "foo.prebuilt.contributions",
10990 api_domain: "com.android.foo",
10991 contents: ["prebuilt_com.android.foo"],
10992 }
10993
10994 apex_contributions {
10995 name: "foo.prebuilt.v2.contributions",
10996 api_domain: "com.android.foo",
10997 contents: ["com.android.foo.v2"], // prebuilt_ prefix is missing because of prebuilt_rename mutator
Spandan Das5be63332023-12-13 00:06:32 +000010998 }
10999 `
11000
11001 testCases := []struct {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011002 desc string
11003 selectedApexContributions string
11004 expectedBootJar string
Spandan Das5be63332023-12-13 00:06:32 +000011005 }{
11006 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011007 desc: "Source apex com.android.foo is selected, bootjar should come from source java library",
11008 selectedApexContributions: "foo.source.contributions",
11009 expectedBootJar: "out/soong/.intermediates/foo-bootclasspath-fragment/android_common_apex10000/hiddenapi-modular/encoded/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011010 },
11011 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011012 desc: "Prebuilt apex prebuilt_com.android.foo is selected, profile should come from .prof deapexed from the prebuilt",
11013 selectedApexContributions: "foo.prebuilt.contributions",
Spandan Das52c01a12024-09-20 01:09:48 +000011014 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo/android_common_com.android.foo/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011015 },
11016 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011017 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected, profile should come from .prof deapexed from the prebuilt",
11018 selectedApexContributions: "foo.prebuilt.v2.contributions",
Spandan Das52c01a12024-09-20 01:09:48 +000011019 expectedBootJar: "out/soong/.intermediates/com.android.foo.v2/android_common_com.android.foo/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011020 },
11021 }
11022
11023 fragment := java.ApexVariantReference{
11024 Apex: proptools.StringPtr("com.android.foo"),
11025 Module: proptools.StringPtr("foo-bootclasspath-fragment"),
11026 }
11027
11028 for _, tc := range testCases {
11029 preparer := android.GroupFixturePreparers(
11030 java.FixtureConfigureApexBootJars("com.android.foo:framework-foo"),
11031 android.FixtureMergeMockFs(map[string][]byte{
11032 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11033 }),
Spandan Das81fe4d12024-05-15 18:43:47 +000011034 // Make sure that we have atleast one platform library so that we can check the monolithic hiddenapi
11035 // file creation.
11036 java.FixtureConfigureBootJars("platform:foo"),
11037 android.FixtureModifyMockFS(func(fs android.MockFS) {
11038 fs["platform/Android.bp"] = []byte(`
11039 java_library {
11040 name: "foo",
11041 srcs: ["Test.java"],
11042 compile_dex: true,
11043 }
11044 `)
11045 fs["platform/Test.java"] = nil
11046 }),
11047
Colin Crossa66b4632024-08-08 15:50:47 -070011048 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das5be63332023-12-13 00:06:32 +000011049 )
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011050 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das5be63332023-12-13 00:06:32 +000011051 checkBootDexJarPath(t, ctx, "framework-foo", tc.expectedBootJar)
Spandan Das64c9e0c2023-12-20 20:13:34 +000011052 checkBootJarsPackageCheck(t, ctx, tc.expectedBootJar)
11053 checkBootJarsForMonolithicHiddenapi(t, ctx, tc.expectedBootJar)
Spandan Das5be63332023-12-13 00:06:32 +000011054 }
11055}
Spandan Das3576e762024-01-03 18:57:03 +000011056
11057// Test that product packaging installs the selected mainline module (either source or a specific prebuilt)
11058// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11059func TestInstallationRulesForMultipleApexPrebuilts(t *testing.T) {
Spandan Das3576e762024-01-03 18:57:03 +000011060 // for a mainline module family, check that only the flagged soong module is visible to make
11061 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleName string, hiddenModuleNames []string) {
11062 variation := func(moduleName string) string {
11063 ret := "android_common_com.android.foo"
11064 if moduleName == "com.google.android.foo" {
Spandan Das50801e22024-05-13 18:29:45 +000011065 ret = "android_common_com.google.android.foo_com.google.android.foo"
Spandan Das3576e762024-01-03 18:57:03 +000011066 }
11067 return ret
11068 }
11069
11070 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11071 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11072
11073 for _, hiddenModuleName := range hiddenModuleNames {
11074 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11075 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11076
11077 }
11078 }
11079
11080 bp := `
11081 apex_key {
11082 name: "com.android.foo.key",
11083 public_key: "com.android.foo.avbpubkey",
11084 private_key: "com.android.foo.pem",
11085 }
11086
11087 // AOSP source apex
11088 apex {
11089 name: "com.android.foo",
11090 key: "com.android.foo.key",
11091 updatable: false,
11092 }
11093
11094 // Google source apex
11095 override_apex {
11096 name: "com.google.android.foo",
11097 base: "com.android.foo",
11098 key: "com.android.foo.key",
11099 }
11100
11101 // Prebuilt Google APEX.
11102
11103 prebuilt_apex {
11104 name: "com.google.android.foo",
11105 apex_name: "com.android.foo",
11106 src: "com.android.foo-arm.apex",
11107 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11108 }
11109
11110 // Another Prebuilt Google APEX
11111 prebuilt_apex {
11112 name: "com.google.android.foo.v2",
11113 apex_name: "com.android.foo",
Spandan Dasa8e2d612024-07-26 19:24:27 +000011114 source_apex_name: "com.google.android.foo",
Spandan Das3576e762024-01-03 18:57:03 +000011115 src: "com.android.foo-arm.apex",
11116 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11117 }
11118
11119 // APEX contribution modules
11120
11121 apex_contributions {
11122 name: "foo.source.contributions",
11123 api_domain: "com.android.foo",
11124 contents: ["com.google.android.foo"],
11125 }
11126
11127 apex_contributions {
11128 name: "foo.prebuilt.contributions",
11129 api_domain: "com.android.foo",
11130 contents: ["prebuilt_com.google.android.foo"],
11131 }
11132
11133 apex_contributions {
11134 name: "foo.prebuilt.v2.contributions",
11135 api_domain: "com.android.foo",
11136 contents: ["prebuilt_com.google.android.foo.v2"],
11137 }
11138
11139 // This is an incompatible module because it selects multiple versions of the same mainline module
11140 apex_contributions {
11141 name: "foo.prebuilt.duplicate.contributions",
11142 api_domain: "com.android.foo",
11143 contents: [
11144 "prebuilt_com.google.android.foo",
11145 "prebuilt_com.google.android.foo.v2",
11146 ],
11147 }
11148 `
11149
11150 testCases := []struct {
11151 desc string
11152 selectedApexContributions string
11153 expectedVisibleModuleName string
11154 expectedHiddenModuleNames []string
11155 expectedError string
11156 }{
11157 {
11158 desc: "Source apex is selected, prebuilts should be hidden from make",
11159 selectedApexContributions: "foo.source.contributions",
11160 expectedVisibleModuleName: "com.google.android.foo",
11161 expectedHiddenModuleNames: []string{"prebuilt_com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11162 },
11163 {
11164 desc: "Prebuilt apex prebuilt_com.android.foo is selected, source and the other prebuilt should be hidden from make",
11165 selectedApexContributions: "foo.prebuilt.contributions",
11166 expectedVisibleModuleName: "prebuilt_com.google.android.foo",
11167 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11168 },
11169 {
11170 desc: "Prebuilt apex prebuilt_com.android.fooi.v2 is selected, source and the other prebuilt should be hidden from make",
11171 selectedApexContributions: "foo.prebuilt.v2.contributions",
11172 expectedVisibleModuleName: "prebuilt_com.google.android.foo.v2",
11173 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo"},
11174 },
11175 {
11176 desc: "Multiple versions of a prebuilt apex is selected in the same release config",
11177 selectedApexContributions: "foo.prebuilt.duplicate.contributions",
11178 expectedError: "Found duplicate variations of the same module in apex_contributions: prebuilt_com.google.android.foo and prebuilt_com.google.android.foo.v2",
11179 },
11180 }
11181
11182 for _, tc := range testCases {
11183 preparer := android.GroupFixturePreparers(
11184 android.FixtureMergeMockFs(map[string][]byte{
11185 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11186 }),
Colin Crossa66b4632024-08-08 15:50:47 -070011187 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das3576e762024-01-03 18:57:03 +000011188 )
11189 if tc.expectedError != "" {
11190 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(tc.expectedError))
11191 testApex(t, bp, preparer)
11192 return
11193 }
11194 ctx := testApex(t, bp, preparer)
11195
Spandan Das3576e762024-01-03 18:57:03 +000011196 // Check that
11197 // 1. The contents of the selected apex_contributions are visible to make
11198 // 2. The rest of the apexes in the mainline module family (source or other prebuilt) is hidden from make
11199 checkHideFromMake(t, ctx, tc.expectedVisibleModuleName, tc.expectedHiddenModuleNames)
11200 }
11201}
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011202
Spandan Das85bd4622024-08-01 00:51:20 +000011203// Test that product packaging installs the selected mainline module in workspaces withtout source mainline module
11204func TestInstallationRulesForMultipleApexPrebuiltsWithoutSource(t *testing.T) {
11205 // for a mainline module family, check that only the flagged soong module is visible to make
11206 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleNames []string, hiddenModuleNames []string) {
11207 variation := func(moduleName string) string {
11208 ret := "android_common_com.android.adservices"
11209 if moduleName == "com.google.android.foo" {
11210 ret = "android_common_com.google.android.foo_com.google.android.foo"
11211 }
11212 return ret
11213 }
11214
11215 for _, visibleModuleName := range visibleModuleNames {
11216 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11217 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11218 }
11219
11220 for _, hiddenModuleName := range hiddenModuleNames {
11221 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11222 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11223
11224 }
11225 }
11226
11227 bp := `
11228 apex_key {
11229 name: "com.android.adservices.key",
11230 public_key: "com.android.adservices.avbpubkey",
11231 private_key: "com.android.adservices.pem",
11232 }
11233
11234 // AOSP source apex
11235 apex {
11236 name: "com.android.adservices",
11237 key: "com.android.adservices.key",
11238 updatable: false,
11239 }
11240
11241 // Prebuilt Google APEX.
11242
11243 prebuilt_apex {
11244 name: "com.google.android.adservices",
11245 apex_name: "com.android.adservices",
11246 src: "com.android.foo-arm.apex",
11247 }
11248
11249 // Another Prebuilt Google APEX
11250 prebuilt_apex {
11251 name: "com.google.android.adservices.v2",
11252 apex_name: "com.android.adservices",
11253 src: "com.android.foo-arm.apex",
11254 }
11255
11256 // APEX contribution modules
11257
11258
11259 apex_contributions {
11260 name: "adservices.prebuilt.contributions",
11261 api_domain: "com.android.adservices",
11262 contents: ["prebuilt_com.google.android.adservices"],
11263 }
11264
11265 apex_contributions {
11266 name: "adservices.prebuilt.v2.contributions",
11267 api_domain: "com.android.adservices",
11268 contents: ["prebuilt_com.google.android.adservices.v2"],
11269 }
11270 `
11271
11272 testCases := []struct {
11273 desc string
11274 selectedApexContributions string
11275 expectedVisibleModuleNames []string
11276 expectedHiddenModuleNames []string
11277 }{
11278 {
11279 desc: "No apex contributions selected, source aosp apex should be visible, and mainline prebuilts should be hidden",
11280 selectedApexContributions: "",
11281 expectedVisibleModuleNames: []string{"com.android.adservices"},
11282 expectedHiddenModuleNames: []string{"com.google.android.adservices", "com.google.android.adservices.v2"},
11283 },
11284 {
11285 desc: "Prebuilt apex prebuilt_com.android.foo is selected",
11286 selectedApexContributions: "adservices.prebuilt.contributions",
11287 expectedVisibleModuleNames: []string{"com.android.adservices", "com.google.android.adservices"},
11288 expectedHiddenModuleNames: []string{"com.google.android.adservices.v2"},
11289 },
11290 {
11291 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected",
11292 selectedApexContributions: "adservices.prebuilt.v2.contributions",
11293 expectedVisibleModuleNames: []string{"com.android.adservices", "com.google.android.adservices.v2"},
11294 expectedHiddenModuleNames: []string{"com.google.android.adservices"},
11295 },
11296 }
11297
11298 for _, tc := range testCases {
11299 preparer := android.GroupFixturePreparers(
11300 android.FixtureMergeMockFs(map[string][]byte{
11301 "system/sepolicy/apex/com.android.adservices-file_contexts": nil,
11302 }),
Colin Crossa66b4632024-08-08 15:50:47 -070011303 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das85bd4622024-08-01 00:51:20 +000011304 )
11305 ctx := testApex(t, bp, preparer)
11306
11307 checkHideFromMake(t, ctx, tc.expectedVisibleModuleNames, tc.expectedHiddenModuleNames)
11308 }
11309}
11310
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011311func TestAconfifDeclarationsValidation(t *testing.T) {
11312 aconfigDeclarationLibraryString := func(moduleNames []string) (ret string) {
11313 for _, moduleName := range moduleNames {
11314 ret += fmt.Sprintf(`
11315 aconfig_declarations {
11316 name: "%[1]s",
11317 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +000011318 container: "system",
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011319 srcs: [
11320 "%[1]s.aconfig",
11321 ],
11322 }
11323 java_aconfig_library {
11324 name: "%[1]s-lib",
11325 aconfig_declarations: "%[1]s",
11326 }
11327 `, moduleName)
11328 }
11329 return ret
11330 }
11331
11332 result := android.GroupFixturePreparers(
11333 prepareForApexTest,
11334 java.PrepareForTestWithJavaSdkLibraryFiles,
11335 java.FixtureWithLastReleaseApis("foo"),
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011336 ).RunTestWithBp(t, `
11337 java_library {
11338 name: "baz-java-lib",
11339 static_libs: [
11340 "baz-lib",
11341 ],
11342 }
11343 filegroup {
11344 name: "qux-filegroup",
Cole Faust65cb40a2024-10-21 15:41:42 -070011345 device_common_srcs: [
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011346 ":qux-lib{.generated_srcjars}",
11347 ],
11348 }
11349 filegroup {
11350 name: "qux-another-filegroup",
11351 srcs: [
11352 ":qux-filegroup",
11353 ],
11354 }
11355 java_library {
11356 name: "quux-java-lib",
11357 srcs: [
11358 "a.java",
11359 ],
11360 libs: [
11361 "quux-lib",
11362 ],
11363 }
11364 java_sdk_library {
11365 name: "foo",
11366 srcs: [
11367 ":qux-another-filegroup",
11368 ],
11369 api_packages: ["foo"],
11370 system: {
11371 enabled: true,
11372 },
11373 module_lib: {
11374 enabled: true,
11375 },
11376 test: {
11377 enabled: true,
11378 },
11379 static_libs: [
11380 "bar-lib",
11381 ],
11382 libs: [
11383 "baz-java-lib",
11384 "quux-java-lib",
11385 ],
11386 aconfig_declarations: [
11387 "bar",
11388 ],
11389 }
11390 `+aconfigDeclarationLibraryString([]string{"bar", "baz", "qux", "quux"}))
11391
11392 m := result.ModuleForTests("foo.stubs.source", "android_common")
11393 outDir := "out/soong/.intermediates"
11394
11395 // Arguments passed to aconfig to retrieve the state of the flags defined in the
11396 // textproto files
11397 aconfigFlagArgs := m.Output("released-flagged-apis-exportable.txt").Args["flags_path"]
11398
11399 // "bar-lib" is a static_lib of "foo" and is passed to metalava as classpath. Thus the
11400 // cache file provided by the associated aconfig_declarations module "bar" should be passed
11401 // to aconfig.
11402 android.AssertStringDoesContain(t, "cache file of a java_aconfig_library static_lib "+
11403 "passed as an input",
11404 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "bar"))
11405
11406 // "baz-java-lib", which statically depends on "baz-lib", is a lib of "foo" and is passed
11407 // to metalava as classpath. Thus the cache file provided by the associated
11408 // aconfig_declarations module "baz" should be passed to aconfig.
11409 android.AssertStringDoesContain(t, "cache file of a lib that statically depends on "+
11410 "java_aconfig_library passed as an input",
11411 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "baz"))
11412
11413 // "qux-lib" is passed to metalava as src via the filegroup, thus the cache file provided by
11414 // the associated aconfig_declarations module "qux" should be passed to aconfig.
11415 android.AssertStringDoesContain(t, "cache file of srcs java_aconfig_library passed as an "+
11416 "input",
11417 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "qux"))
11418
11419 // "quux-java-lib" is a lib of "foo" and is passed to metalava as classpath, but does not
11420 // statically depend on "quux-lib". Therefore, the cache file provided by the associated
11421 // aconfig_declarations module "quux" should not be passed to aconfig.
11422 android.AssertStringDoesNotContain(t, "cache file of a lib that does not statically "+
11423 "depend on java_aconfig_library not passed as an input",
11424 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "quux"))
11425}
Cole Faust7c991b42024-05-15 11:17:55 -070011426
11427func TestMultiplePrebuiltsWithSameBase(t *testing.T) {
11428 ctx := testApex(t, `
11429 apex {
11430 name: "myapex",
11431 key: "myapex.key",
11432 prebuilts: ["myetc", "myetc2"],
11433 min_sdk_version: "29",
11434 }
11435 apex_key {
11436 name: "myapex.key",
11437 public_key: "testkey.avbpubkey",
11438 private_key: "testkey.pem",
11439 }
11440
11441 prebuilt_etc {
11442 name: "myetc",
11443 src: "myprebuilt",
11444 filename: "myfilename",
11445 }
11446 prebuilt_etc {
11447 name: "myetc2",
11448 sub_dir: "mysubdir",
11449 src: "myprebuilt",
11450 filename: "myfilename",
11451 }
11452 `, withFiles(android.MockFS{
11453 "packages/modules/common/build/allowed_deps.txt": nil,
11454 }))
11455
11456 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
11457 data := android.AndroidMkDataForTest(t, ctx, ab)
11458 var builder strings.Builder
11459 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
11460 androidMk := builder.String()
11461
11462 android.AssertStringDoesContain(t, "not found", androidMk, "LOCAL_MODULE := etc_myfilename.myapex")
11463 android.AssertStringDoesContain(t, "not found", androidMk, "LOCAL_MODULE := etc_mysubdir_myfilename.myapex")
11464}
Spandan Das50801e22024-05-13 18:29:45 +000011465
11466func TestApexMinSdkVersionOverride(t *testing.T) {
11467 checkMinSdkVersion := func(t *testing.T, module android.TestingModule, expectedMinSdkVersion string) {
11468 args := module.Rule("apexRule").Args
11469 optFlags := args["opt_flags"]
11470 if !strings.Contains(optFlags, "--min_sdk_version "+expectedMinSdkVersion) {
11471 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", module.Module(), expectedMinSdkVersion, optFlags)
11472 }
11473 }
11474
11475 checkHasDep := func(t *testing.T, ctx *android.TestContext, m android.Module, wantDep android.Module) {
11476 t.Helper()
11477 found := false
11478 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
11479 if dep == wantDep {
11480 found = true
11481 }
11482 })
11483 if !found {
11484 t.Errorf("Could not find a dependency from %v to %v\n", m, wantDep)
11485 }
11486 }
11487
11488 ctx := testApex(t, `
11489 apex {
11490 name: "com.android.apex30",
11491 min_sdk_version: "30",
11492 key: "apex30.key",
11493 java_libs: ["javalib"],
11494 }
11495
11496 java_library {
11497 name: "javalib",
11498 srcs: ["A.java"],
11499 apex_available: ["com.android.apex30"],
11500 min_sdk_version: "30",
11501 sdk_version: "current",
11502 }
11503
11504 override_apex {
11505 name: "com.mycompany.android.apex30",
11506 base: "com.android.apex30",
11507 }
11508
11509 override_apex {
11510 name: "com.mycompany.android.apex31",
11511 base: "com.android.apex30",
11512 min_sdk_version: "31",
11513 }
11514
11515 apex_key {
11516 name: "apex30.key",
11517 public_key: "testkey.avbpubkey",
11518 private_key: "testkey.pem",
11519 }
11520
11521 `, android.FixtureMergeMockFs(android.MockFS{
11522 "system/sepolicy/apex/com.android.apex30-file_contexts": nil,
11523 }),
11524 )
11525
11526 baseModule := ctx.ModuleForTests("com.android.apex30", "android_common_com.android.apex30")
11527 checkMinSdkVersion(t, baseModule, "30")
11528
11529 // Override module, but uses same min_sdk_version
11530 overridingModuleSameMinSdkVersion := ctx.ModuleForTests("com.android.apex30", "android_common_com.mycompany.android.apex30_com.mycompany.android.apex30")
11531 javalibApex30Variant := ctx.ModuleForTests("javalib", "android_common_apex30")
11532 checkMinSdkVersion(t, overridingModuleSameMinSdkVersion, "30")
11533 checkHasDep(t, ctx, overridingModuleSameMinSdkVersion.Module(), javalibApex30Variant.Module())
11534
11535 // Override module, uses different min_sdk_version
11536 overridingModuleDifferentMinSdkVersion := ctx.ModuleForTests("com.android.apex30", "android_common_com.mycompany.android.apex31_com.mycompany.android.apex31")
11537 javalibApex31Variant := ctx.ModuleForTests("javalib", "android_common_apex31")
11538 checkMinSdkVersion(t, overridingModuleDifferentMinSdkVersion, "31")
11539 checkHasDep(t, ctx, overridingModuleDifferentMinSdkVersion.Module(), javalibApex31Variant.Module())
11540}
Spandan Das0b28fa02024-05-28 23:40:17 +000011541
11542func TestOverrideApexWithPrebuiltApexPreferred(t *testing.T) {
11543 context := android.GroupFixturePreparers(
11544 android.PrepareForIntegrationTestWithAndroid,
11545 PrepareForTestWithApexBuildComponents,
11546 android.FixtureMergeMockFs(android.MockFS{
11547 "system/sepolicy/apex/foo-file_contexts": nil,
11548 }),
11549 )
11550 res := context.RunTestWithBp(t, `
11551 apex {
11552 name: "foo",
11553 key: "myapex.key",
11554 apex_available_name: "com.android.foo",
11555 variant_version: "0",
11556 updatable: false,
11557 }
11558 apex_key {
11559 name: "myapex.key",
11560 public_key: "testkey.avbpubkey",
11561 private_key: "testkey.pem",
11562 }
11563 prebuilt_apex {
11564 name: "foo",
11565 src: "foo.apex",
11566 prefer: true,
11567 }
11568 override_apex {
11569 name: "myoverrideapex",
11570 base: "foo",
11571 }
11572 `)
11573
11574 java.CheckModuleHasDependency(t, res.TestContext, "myoverrideapex", "android_common_myoverrideapex_myoverrideapex", "foo")
11575}
Spandan Dasca1d63e2024-07-01 22:53:49 +000011576
11577func TestUpdatableApexMinSdkVersionCurrent(t *testing.T) {
11578 testApexError(t, `"myapex" .*: updatable: updatable APEXes should not set min_sdk_version to current. Please use a finalized API level or a recognized in-development codename`, `
11579 apex {
11580 name: "myapex",
11581 key: "myapex.key",
11582 updatable: true,
11583 min_sdk_version: "current",
11584 }
11585
11586 apex_key {
11587 name: "myapex.key",
11588 public_key: "testkey.avbpubkey",
11589 private_key: "testkey.pem",
11590 }
11591 `)
11592}
Spandan Das2f68f192024-07-22 19:25:50 +000011593
11594func TestPrebuiltStubNoinstall(t *testing.T) {
11595 testFunc := func(t *testing.T, expectLibfooOnSystemLib bool, fs android.MockFS) {
11596 result := android.GroupFixturePreparers(
11597 prepareForApexTest,
11598 android.PrepareForTestWithAndroidMk,
11599 android.PrepareForTestWithMakevars,
11600 android.FixtureMergeMockFs(fs),
11601 ).RunTest(t)
11602
11603 ldRule := result.ModuleForTests("installedlib", "android_arm64_armv8-a_shared").Rule("ld")
Spandan Das357ffcc2024-07-24 18:07:48 +000011604 android.AssertStringDoesContain(t, "", ldRule.Args["libFlags"], "android_arm64_armv8-a_shared_current/libfoo.so")
Spandan Das2f68f192024-07-22 19:25:50 +000011605
11606 installRules := result.InstallMakeRulesForTesting(t)
11607
11608 var installedlibRule *android.InstallMakeRule
11609 for i, rule := range installRules {
11610 if rule.Target == "out/target/product/test_device/system/lib/installedlib.so" {
11611 if installedlibRule != nil {
11612 t.Errorf("Duplicate install rules for %s", rule.Target)
11613 }
11614 installedlibRule = &installRules[i]
11615 }
11616 }
11617 if installedlibRule == nil {
11618 t.Errorf("No install rule found for installedlib")
11619 return
11620 }
11621
11622 if expectLibfooOnSystemLib {
11623 android.AssertStringListContains(t,
11624 "installedlib doesn't have install dependency on libfoo impl",
11625 installedlibRule.OrderOnlyDeps,
11626 "out/target/product/test_device/system/lib/libfoo.so")
11627 } else {
11628 android.AssertStringListDoesNotContain(t,
11629 "installedlib has install dependency on libfoo stub",
11630 installedlibRule.Deps,
11631 "out/target/product/test_device/system/lib/libfoo.so")
11632 android.AssertStringListDoesNotContain(t,
11633 "installedlib has order-only install dependency on libfoo stub",
11634 installedlibRule.OrderOnlyDeps,
11635 "out/target/product/test_device/system/lib/libfoo.so")
11636 }
11637 }
11638
11639 prebuiltLibfooBp := []byte(`
11640 cc_prebuilt_library {
11641 name: "libfoo",
11642 prefer: true,
11643 srcs: ["libfoo.so"],
11644 stubs: {
11645 versions: ["1"],
11646 },
11647 apex_available: ["apexfoo"],
11648 }
11649 `)
11650
11651 apexfooBp := []byte(`
11652 apex {
11653 name: "apexfoo",
11654 key: "apexfoo.key",
11655 native_shared_libs: ["libfoo"],
11656 updatable: false,
11657 compile_multilib: "both",
11658 }
11659 apex_key {
11660 name: "apexfoo.key",
11661 public_key: "testkey.avbpubkey",
11662 private_key: "testkey.pem",
11663 }
11664 `)
11665
11666 installedlibBp := []byte(`
11667 cc_library {
11668 name: "installedlib",
11669 shared_libs: ["libfoo"],
11670 }
11671 `)
11672
11673 t.Run("prebuilt stub (without source): no install", func(t *testing.T) {
11674 testFunc(
11675 t,
11676 /*expectLibfooOnSystemLib=*/ false,
11677 android.MockFS{
11678 "prebuilts/module_sdk/art/current/Android.bp": prebuiltLibfooBp,
11679 "apexfoo/Android.bp": apexfooBp,
11680 "system/sepolicy/apex/apexfoo-file_contexts": nil,
11681 "Android.bp": installedlibBp,
11682 },
11683 )
11684 })
11685
11686 disabledSourceLibfooBp := []byte(`
11687 cc_library {
11688 name: "libfoo",
11689 enabled: false,
11690 stubs: {
11691 versions: ["1"],
11692 },
11693 apex_available: ["apexfoo"],
11694 }
11695 `)
11696
11697 t.Run("prebuilt stub (with disabled source): no install", func(t *testing.T) {
11698 testFunc(
11699 t,
11700 /*expectLibfooOnSystemLib=*/ false,
11701 android.MockFS{
11702 "prebuilts/module_sdk/art/current/Android.bp": prebuiltLibfooBp,
11703 "impl/Android.bp": disabledSourceLibfooBp,
11704 "apexfoo/Android.bp": apexfooBp,
11705 "system/sepolicy/apex/apexfoo-file_contexts": nil,
11706 "Android.bp": installedlibBp,
11707 },
11708 )
11709 })
11710}
Jihoon Kange246bb72024-09-18 22:26:22 +000011711
11712func TestSdkLibraryTransitiveClassLoaderContext(t *testing.T) {
11713 // This test case tests that listing the impl lib instead of the top level java_sdk_library
11714 // in libs of android_app and java_library does not lead to class loader context device/host
11715 // path mismatch errors.
11716 android.GroupFixturePreparers(
11717 prepareForApexTest,
11718 android.PrepareForIntegrationTestWithAndroid,
11719 PrepareForTestWithApexBuildComponents,
11720 android.FixtureModifyEnv(func(env map[string]string) {
11721 env["DISABLE_CONTAINER_CHECK"] = "true"
11722 }),
11723 withFiles(filesForSdkLibrary),
11724 android.FixtureMergeMockFs(android.MockFS{
11725 "system/sepolicy/apex/com.android.foo30-file_contexts": nil,
11726 }),
11727 ).RunTestWithBp(t, `
11728 apex {
11729 name: "com.android.foo30",
11730 key: "myapex.key",
11731 updatable: true,
11732 bootclasspath_fragments: [
11733 "foo-bootclasspath-fragment",
11734 ],
11735 java_libs: [
11736 "bar",
11737 ],
11738 apps: [
11739 "bar-app",
11740 ],
11741 min_sdk_version: "30",
11742 }
11743 apex_key {
11744 name: "myapex.key",
11745 public_key: "testkey.avbpubkey",
11746 private_key: "testkey.pem",
11747 }
11748 bootclasspath_fragment {
11749 name: "foo-bootclasspath-fragment",
11750 contents: [
11751 "framework-foo",
11752 ],
11753 apex_available: [
11754 "com.android.foo30",
11755 ],
11756 hidden_api: {
11757 split_packages: ["*"]
11758 },
11759 }
11760
11761 java_sdk_library {
11762 name: "framework-foo",
11763 srcs: [
11764 "A.java"
11765 ],
11766 unsafe_ignore_missing_latest_api: true,
11767 apex_available: [
11768 "com.android.foo30",
11769 ],
11770 compile_dex: true,
11771 sdk_version: "core_current",
11772 shared_library: false,
11773 }
11774
11775 java_library {
11776 name: "bar",
11777 srcs: [
11778 "A.java"
11779 ],
11780 libs: [
11781 "framework-foo.impl",
11782 ],
11783 apex_available: [
11784 "com.android.foo30",
11785 ],
11786 sdk_version: "core_current",
11787 }
11788
11789 java_library {
11790 name: "baz",
11791 srcs: [
11792 "A.java"
11793 ],
11794 libs: [
11795 "bar",
11796 ],
11797 sdk_version: "core_current",
11798 }
11799
11800 android_app {
11801 name: "bar-app",
11802 srcs: [
11803 "A.java"
11804 ],
11805 libs: [
11806 "baz",
11807 "framework-foo.impl",
11808 ],
11809 apex_available: [
11810 "com.android.foo30",
11811 ],
11812 sdk_version: "core_current",
11813 min_sdk_version: "30",
11814 manifest: "AndroidManifest.xml",
Colin Cross99939e92024-10-01 16:02:46 -070011815 updatable: true,
Jihoon Kange246bb72024-09-18 22:26:22 +000011816 }
11817 `)
11818}
Spandan Dasfbcd5fe2024-09-30 22:30:39 +000011819
11820// If an apex sets system_ext_specific: true, its systemserverclasspath libraries must set this property as well.
11821func TestApexSSCPJarMustBeInSamePartitionAsApex(t *testing.T) {
11822 testApexError(t, `foo is an apex systemserver jar, but its partition does not match the partition of its containing apex`, `
11823 apex {
11824 name: "myapex",
11825 key: "myapex.key",
11826 systemserverclasspath_fragments: [
11827 "mysystemserverclasspathfragment",
11828 ],
11829 min_sdk_version: "29",
11830 updatable: true,
11831 system_ext_specific: true,
11832 }
11833
11834 apex_key {
11835 name: "myapex.key",
11836 public_key: "testkey.avbpubkey",
11837 private_key: "testkey.pem",
11838 }
11839
11840 java_library {
11841 name: "foo",
11842 srcs: ["b.java"],
11843 min_sdk_version: "29",
11844 installable: true,
11845 apex_available: [
11846 "myapex",
11847 ],
11848 sdk_version: "current",
11849 }
11850
11851 systemserverclasspath_fragment {
11852 name: "mysystemserverclasspathfragment",
11853 contents: [
11854 "foo",
11855 ],
11856 apex_available: [
11857 "myapex",
11858 ],
11859 }
11860 `,
11861 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
11862 )
11863}
Spandan Das72d86c62024-10-15 19:27:41 +000011864
11865// partitions should not package the artifacts that are included inside the apex.
11866func TestFilesystemWithApexDeps(t *testing.T) {
11867 t.Parallel()
11868 result := testApex(t, `
11869 android_filesystem {
11870 name: "myfilesystem",
11871 deps: ["myapex"],
11872 }
11873 apex {
11874 name: "myapex",
11875 key: "myapex.key",
11876 binaries: ["binfoo"],
11877 native_shared_libs: ["libfoo"],
11878 apps: ["appfoo"],
11879 updatable: false,
11880 }
11881 apex_key {
11882 name: "myapex.key",
11883 }
11884 cc_binary {
11885 name: "binfoo",
11886 apex_available: ["myapex"],
11887 }
11888 cc_library {
11889 name: "libfoo",
11890 apex_available: ["myapex"],
11891 }
11892 android_app {
11893 name: "appfoo",
11894 sdk_version: "current",
11895 apex_available: ["myapex"],
11896 }
11897 `, filesystem.PrepareForTestWithFilesystemBuildComponents)
11898
11899 partition := result.ModuleForTests("myfilesystem", "android_common")
11900 fileList := android.ContentFromFileRuleForTests(t, result, partition.Output("fileList"))
11901 android.AssertDeepEquals(t, "filesystem with apex", "apex/myapex.apex\n", fileList)
11902}