blob: cbb1a76abeb714c644d081a2964b1f46bbb7a0e1 [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
Paul Duffin810f33d2021-03-09 14:12:32 +000085func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
86 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090087 for k, v := range targets {
88 config.Targets[k] = v
89 }
Paul Duffin810f33d2021-03-09 14:12:32 +000090 })
Jooyung Han344d5432019-08-23 11:17:39 +090091}
92
Jooyung Han35155c42020-02-06 17:33:20 +090093// withNativeBridgeTargets sets configuration with targets including:
94// - X86_64 (primary)
95// - X86 (secondary)
96// - Arm64 on X86_64 (native bridge)
97// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000098var withNativeBridgeEnabled = android.FixtureModifyConfig(
99 func(config android.Config) {
100 config.Targets[android.Android] = []android.Target{
101 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
102 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
103 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
104 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
105 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
106 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
107 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
108 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
109 }
110 },
111)
112
113func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
114 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
115 variables.ManifestPackageNameOverrides = specs
116 })
Jooyung Han35155c42020-02-06 17:33:20 +0900117}
118
Albert Martineefabcf2022-03-21 20:11:16 +0000119func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
120 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
121 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
122 })
123}
124
Paul Duffin810f33d2021-03-09 14:12:32 +0000125var withBinder32bit = android.FixtureModifyProductVariables(
126 func(variables android.FixtureProductVariables) {
127 variables.Binder32bit = proptools.BoolPtr(true)
128 },
129)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900130
Paul Duffin810f33d2021-03-09 14:12:32 +0000131var withUnbundledBuild = android.FixtureModifyProductVariables(
132 func(variables android.FixtureProductVariables) {
133 variables.Unbundled_build = proptools.BoolPtr(true)
134 },
135)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900136
Paul Duffin284165a2021-03-29 01:50:31 +0100137// Legacy preparer used for running tests within the apex package.
138//
139// This includes everything that was needed to run any test in the apex package prior to the
140// introduction of the test fixtures. Tests that are being converted to use fixtures directly
141// rather than through the testApex...() methods should avoid using this and instead use the
142// various preparers directly, using android.GroupFixturePreparers(...) to group them when
143// necessary.
144//
145// deprecated
146var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000147 // General preparers in alphabetical order as test infrastructure will enforce correct
148 // registration order.
149 android.PrepareForTestWithAndroidBuildComponents,
150 bpf.PrepareForTestWithBpf,
151 cc.PrepareForTestWithCcBuildComponents,
Jiakai Zhangb95998b2023-05-11 16:39:27 +0100152 java.PrepareForTestWithDexpreopt,
Paul Duffin37aad602021-03-08 09:47:16 +0000153 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
154 rust.PrepareForTestWithRustDefaultModules,
155 sh.PrepareForTestWithShBuildComponents,
Yu Liueae7b362023-11-16 17:05:47 -0800156 codegen.PrepareForTestWithAconfigBuildComponents,
Paul Duffin37aad602021-03-08 09:47:16 +0000157
158 PrepareForTestWithApexBuildComponents,
159
160 // Additional apex test specific preparers.
161 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
162 filegroup {
163 name: "myapex-file_contexts",
164 srcs: [
165 "apex/myapex-file_contexts",
166 ],
167 }
168 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000169 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000170 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000171 "a.java": nil,
172 "PrebuiltAppFoo.apk": nil,
173 "PrebuiltAppFooPriv.apk": nil,
174 "apex_manifest.json": nil,
175 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000176 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
177 "system/sepolicy/apex/myapex2-file_contexts": nil,
178 "system/sepolicy/apex/otherapex-file_contexts": nil,
179 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
180 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700181 "mylib.cpp": nil,
182 "mytest.cpp": nil,
183 "mytest1.cpp": nil,
184 "mytest2.cpp": nil,
185 "mytest3.cpp": nil,
186 "myprebuilt": nil,
187 "my_include": nil,
188 "foo/bar/MyClass.java": nil,
189 "prebuilt.jar": nil,
190 "prebuilt.so": nil,
191 "vendor/foo/devkeys/test.x509.pem": nil,
192 "vendor/foo/devkeys/test.pk8": nil,
193 "testkey.x509.pem": nil,
194 "testkey.pk8": nil,
195 "testkey.override.x509.pem": nil,
196 "testkey.override.pk8": nil,
197 "vendor/foo/devkeys/testkey.avbpubkey": nil,
198 "vendor/foo/devkeys/testkey.pem": nil,
199 "NOTICE": nil,
200 "custom_notice": nil,
201 "custom_notice_for_static_lib": nil,
202 "testkey2.avbpubkey": nil,
203 "testkey2.pem": nil,
204 "myapex-arm64.apex": nil,
205 "myapex-arm.apex": nil,
206 "myapex.apks": nil,
207 "frameworks/base/api/current.txt": nil,
208 "framework/aidl/a.aidl": nil,
209 "dummy.txt": nil,
210 "baz": nil,
211 "bar/baz": nil,
212 "testdata/baz": nil,
213 "AppSet.apks": nil,
214 "foo.rs": nil,
215 "libfoo.jar": nil,
216 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000217 },
218 ),
219
220 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
Paul Duffin37aad602021-03-08 09:47:16 +0000221 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
222 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
223 variables.Platform_sdk_codename = proptools.StringPtr("Q")
224 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000225 // "Tiramisu" needs to be in the next line for compatibility with soong code,
226 // not because of these tests specifically (it's not used by the tests)
227 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000228 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000229 }),
230)
231
Paul Duffin52bfaa42021-03-23 23:40:12 +0000232var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
233 "system/sepolicy/apex/myapex-file_contexts": nil,
234})
235
Jooyung Han643adc42020-02-27 13:50:06 +0900236// ensure that 'result' equals 'expected'
237func ensureEquals(t *testing.T, result string, expected string) {
238 t.Helper()
239 if result != expected {
240 t.Errorf("%q != %q", expected, result)
241 }
242}
243
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244// ensure that 'result' contains 'expected'
245func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900246 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247 if !strings.Contains(result, expected) {
248 t.Errorf("%q is not found in %q", expected, result)
249 }
250}
251
Liz Kammer5bd365f2020-05-27 15:15:11 -0700252// ensure that 'result' contains 'expected' exactly one time
253func ensureContainsOnce(t *testing.T, result string, expected string) {
254 t.Helper()
255 count := strings.Count(result, expected)
256 if count != 1 {
257 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
258 }
259}
260
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261// ensures that 'result' does not contain 'notExpected'
262func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900263 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900264 if strings.Contains(result, notExpected) {
265 t.Errorf("%q is found in %q", notExpected, result)
266 }
267}
268
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700269func ensureMatches(t *testing.T, result string, expectedRex string) {
Jooyung Hana3fddf42024-09-03 13:22:21 +0900270 t.Helper()
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700271 ok, err := regexp.MatchString(expectedRex, result)
272 if err != nil {
273 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
274 return
275 }
276 if !ok {
277 t.Errorf("%s does not match regular expession %s", result, expectedRex)
278 }
279}
280
Jooyung Hana3fddf42024-09-03 13:22:21 +0900281func ensureListContainsMatch(t *testing.T, result []string, expectedRex string) {
282 t.Helper()
283 p := regexp.MustCompile(expectedRex)
284 if android.IndexListPred(func(s string) bool { return p.MatchString(s) }, result) == -1 {
285 t.Errorf("%q is not found in %v", expectedRex, result)
286 }
287}
288
Jiyong Park25fc6a92018-11-18 18:02:45 +0900289func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900290 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900291 if !android.InList(expected, result) {
292 t.Errorf("%q is not found in %v", expected, result)
293 }
294}
295
296func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900297 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900298 if android.InList(notExpected, result) {
299 t.Errorf("%q is found in %v", notExpected, result)
300 }
301}
302
Jooyung Hane1633032019-08-01 17:41:43 +0900303func ensureListEmpty(t *testing.T, result []string) {
304 t.Helper()
305 if len(result) > 0 {
306 t.Errorf("%q is expected to be empty", result)
307 }
308}
309
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000310func ensureListNotEmpty(t *testing.T, result []string) {
311 t.Helper()
312 if len(result) == 0 {
313 t.Errorf("%q is expected to be not empty", result)
314 }
315}
316
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317// Minimal test
318func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800319 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900320 apex_defaults {
321 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900322 manifest: ":myapex.manifest",
323 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900324 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900325 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900326 native_shared_libs: [
327 "mylib",
328 "libfoo.ffi",
329 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900330 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800331 multilib: {
332 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900333 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800334 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900335 },
Jiyong Park77acec62020-06-01 21:39:15 +0900336 java_libs: [
337 "myjar",
338 "myjar_dex",
339 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000340 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900341 }
342
Jiyong Park30ca9372019-02-07 16:27:23 +0900343 apex {
344 name: "myapex",
345 defaults: ["myapex-defaults"],
346 }
347
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348 apex_key {
349 name: "myapex.key",
350 public_key: "testkey.avbpubkey",
351 private_key: "testkey.pem",
352 }
353
Jiyong Park809bb722019-02-13 21:33:49 +0900354 filegroup {
355 name: "myapex.manifest",
356 srcs: ["apex_manifest.json"],
357 }
358
359 filegroup {
360 name: "myapex.androidmanifest",
361 srcs: ["AndroidManifest.xml"],
362 }
363
Jiyong Park25fc6a92018-11-18 18:02:45 +0900364 cc_library {
365 name: "mylib",
366 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900367 shared_libs: [
368 "mylib2",
369 "libbar.ffi",
370 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900371 system_shared_libs: [],
372 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000373 // TODO: remove //apex_available:platform
374 apex_available: [
375 "//apex_available:platform",
376 "myapex",
377 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900378 }
379
Alex Light3d673592019-01-18 14:37:31 -0800380 cc_binary {
381 name: "foo",
382 srcs: ["mylib.cpp"],
383 compile_multilib: "both",
384 multilib: {
385 lib32: {
386 suffix: "32",
387 },
388 lib64: {
389 suffix: "64",
390 },
391 },
392 symlinks: ["foo_link_"],
393 symlink_preferred_arch: true,
394 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800395 stl: "none",
Jooyung Han40b79172024-08-16 16:00:33 +0900396 apex_available: [ "myapex" ],
Yifan Hongd22a84a2020-07-28 17:37:46 -0700397 }
398
Jiyong Park99644e92020-11-17 22:21:02 +0900399 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000400 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900401 srcs: ["foo.rs"],
402 rlibs: ["libfoo.rlib.rust"],
Vinh Tran4eeb2a92023-08-14 13:29:30 -0400403 rustlibs: ["libfoo.dylib.rust"],
Jiyong Park99644e92020-11-17 22:21:02 +0900404 apex_available: ["myapex"],
405 }
406
407 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000408 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900409 srcs: ["foo.rs"],
410 crate_name: "foo",
411 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900412 shared_libs: ["libfoo.shared_from_rust"],
413 }
414
415 cc_library_shared {
416 name: "libfoo.shared_from_rust",
417 srcs: ["mylib.cpp"],
418 system_shared_libs: [],
419 stl: "none",
420 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900421 }
422
423 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000424 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900425 srcs: ["foo.rs"],
426 crate_name: "foo",
427 apex_available: ["myapex"],
428 }
429
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900430 rust_ffi_shared {
431 name: "libfoo.ffi",
432 srcs: ["foo.rs"],
433 crate_name: "foo",
434 apex_available: ["myapex"],
435 }
436
437 rust_ffi_shared {
438 name: "libbar.ffi",
439 srcs: ["foo.rs"],
440 crate_name: "bar",
441 apex_available: ["myapex"],
442 }
443
Paul Duffindddd5462020-04-07 15:25:44 +0100444 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900445 name: "mylib2",
446 srcs: ["mylib.cpp"],
447 system_shared_libs: [],
448 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900449 static_libs: ["libstatic"],
450 // TODO: remove //apex_available:platform
451 apex_available: [
452 "//apex_available:platform",
453 "myapex",
454 ],
455 }
456
Paul Duffindddd5462020-04-07 15:25:44 +0100457 cc_prebuilt_library_shared {
458 name: "mylib2",
459 srcs: ["prebuilt.so"],
460 // TODO: remove //apex_available:platform
461 apex_available: [
462 "//apex_available:platform",
463 "myapex",
464 ],
465 }
466
Jiyong Park9918e1a2020-03-17 19:16:40 +0900467 cc_library_static {
468 name: "libstatic",
469 srcs: ["mylib.cpp"],
470 system_shared_libs: [],
471 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000472 // TODO: remove //apex_available:platform
473 apex_available: [
474 "//apex_available:platform",
475 "myapex",
476 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900477 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900478
479 java_library {
480 name: "myjar",
481 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900482 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900483 sdk_version: "none",
484 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900485 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900486 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000487 // TODO: remove //apex_available:platform
488 apex_available: [
489 "//apex_available:platform",
490 "myapex",
491 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900492 }
493
Jiyong Park77acec62020-06-01 21:39:15 +0900494 dex_import {
495 name: "myjar_dex",
496 jars: ["prebuilt.jar"],
497 apex_available: [
498 "//apex_available:platform",
499 "myapex",
500 ],
501 }
502
Jiyong Park7f7766d2019-07-25 22:02:35 +0900503 java_library {
504 name: "myotherjar",
505 srcs: ["foo/bar/MyClass.java"],
506 sdk_version: "none",
507 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900508 // TODO: remove //apex_available:platform
509 apex_available: [
510 "//apex_available:platform",
511 "myapex",
512 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900513 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900514
515 java_library {
516 name: "mysharedjar",
517 srcs: ["foo/bar/MyClass.java"],
518 sdk_version: "none",
519 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900520 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900521 `)
522
Jooyung Hana0503a52023-08-23 13:12:50 +0900523 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900524
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900525 // Make sure that Android.mk is created
Jooyung Hana0503a52023-08-23 13:12:50 +0900526 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700527 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900528 var builder strings.Builder
529 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
530
531 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000532 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900533 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
534
Jiyong Park42cca6c2019-04-01 11:15:50 +0900535 optFlags := apexRule.Args["opt_flags"]
536 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700537 // Ensure that the NOTICE output is being packaged as an asset.
Jooyung Hana0503a52023-08-23 13:12:50 +0900538 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900539
Jiyong Park25fc6a92018-11-18 18:02:45 +0900540 copyCmds := apexRule.Args["copy_commands"]
541
542 // Ensure that main rule creates an output
543 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
544
545 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700546 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
547 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
548 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900549 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900550 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900551
552 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700553 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
554 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
556 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900557 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900558 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900559
560 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800561 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
562 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900563 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900564 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900566 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
567 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900568 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900569 // .. but not for java libs
570 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900571 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800572
Colin Cross7113d202019-11-20 16:39:12 -0800573 // Ensure that the platform variant ends with _shared or _common
574 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
575 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900576 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
577 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900578 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
579
580 // Ensure that dynamic dependency to java libs are not included
581 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800582
583 // Ensure that all symlinks are present.
584 found_foo_link_64 := false
585 found_foo := false
586 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900587 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800588 if strings.HasSuffix(cmd, "bin/foo") {
589 found_foo = true
590 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
591 found_foo_link_64 = true
592 }
593 }
594 }
595 good := found_foo && found_foo_link_64
596 if !good {
597 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
598 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900599
Colin Crossf61d03d2023-11-02 16:56:39 -0700600 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
601 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100602 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100603 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
604 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
605 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100606
Colin Crossf61d03d2023-11-02 16:56:39 -0700607 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
608 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100609 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100610 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
611 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
612 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800613}
614
Jooyung Hanf21c7972019-12-16 22:32:06 +0900615func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800616 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900617 apex_defaults {
618 name: "myapex-defaults",
619 key: "myapex.key",
620 prebuilts: ["myetc"],
621 native_shared_libs: ["mylib"],
622 java_libs: ["myjar"],
623 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900624 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800625 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000626 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900627 }
628
629 prebuilt_etc {
630 name: "myetc",
631 src: "myprebuilt",
632 }
633
634 apex {
635 name: "myapex",
636 defaults: ["myapex-defaults"],
637 }
638
639 apex_key {
640 name: "myapex.key",
641 public_key: "testkey.avbpubkey",
642 private_key: "testkey.pem",
643 }
644
645 cc_library {
646 name: "mylib",
647 system_shared_libs: [],
648 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000649 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900650 }
651
652 java_library {
653 name: "myjar",
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 }
659
660 android_app {
661 name: "AppFoo",
662 srcs: ["foo/bar/MyClass.java"],
663 sdk_version: "none",
664 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000665 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900666 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900667
668 runtime_resource_overlay {
669 name: "rro",
670 theme: "blue",
671 }
672
markchien2f59ec92020-09-02 16:23:38 +0800673 bpf {
674 name: "bpf",
675 srcs: ["bpf.c", "bpf2.c"],
676 }
677
Ken Chenfad7f9d2021-11-10 22:02:57 +0800678 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800679 name: "netdTest",
680 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800681 sub_dir: "netd",
682 }
683
Jooyung Hanf21c7972019-12-16 22:32:06 +0900684 `)
Jooyung Hana0503a52023-08-23 13:12:50 +0900685 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900686 "etc/myetc",
687 "javalib/myjar.jar",
688 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000689 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900690 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800691 "etc/bpf/bpf.o",
692 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800693 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900694 })
695}
696
Jooyung Han01a3ee22019-11-02 02:52:25 +0900697func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800698 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900699 apex {
700 name: "myapex",
701 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000702 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900703 }
704
705 apex_key {
706 name: "myapex.key",
707 public_key: "testkey.avbpubkey",
708 private_key: "testkey.pem",
709 }
710 `)
711
Jooyung Hana0503a52023-08-23 13:12:50 +0900712 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +0900713 args := module.Rule("apexRule").Args
714 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
715 t.Error("manifest should be apex_manifest.pb, but " + manifest)
716 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900717}
718
Liz Kammer4854a7d2021-05-27 14:28:27 -0400719func TestApexManifestMinSdkVersion(t *testing.T) {
720 ctx := testApex(t, `
721 apex_defaults {
722 name: "my_defaults",
723 key: "myapex.key",
724 product_specific: true,
725 file_contexts: ":my-file-contexts",
726 updatable: false,
727 }
728 apex {
729 name: "myapex_30",
730 min_sdk_version: "30",
731 defaults: ["my_defaults"],
732 }
733
734 apex {
735 name: "myapex_current",
736 min_sdk_version: "current",
737 defaults: ["my_defaults"],
738 }
739
740 apex {
741 name: "myapex_none",
742 defaults: ["my_defaults"],
743 }
744
745 apex_key {
746 name: "myapex.key",
747 public_key: "testkey.avbpubkey",
748 private_key: "testkey.pem",
749 }
750
751 filegroup {
752 name: "my-file-contexts",
753 srcs: ["product_specific_file_contexts"],
754 }
755 `, withFiles(map[string][]byte{
756 "product_specific_file_contexts": nil,
757 }), android.FixtureModifyProductVariables(
758 func(variables android.FixtureProductVariables) {
759 variables.Unbundled_build = proptools.BoolPtr(true)
760 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
761 }), android.FixtureMergeEnv(map[string]string{
762 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
763 }))
764
765 testCases := []struct {
766 module string
767 minSdkVersion string
768 }{
769 {
770 module: "myapex_30",
771 minSdkVersion: "30",
772 },
773 {
774 module: "myapex_current",
775 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
776 },
777 {
778 module: "myapex_none",
779 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
780 },
781 }
782 for _, tc := range testCases {
Jooyung Hana0503a52023-08-23 13:12:50 +0900783 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
Liz Kammer4854a7d2021-05-27 14:28:27 -0400784 args := module.Rule("apexRule").Args
785 optFlags := args["opt_flags"]
786 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
787 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
788 }
789 }
790}
791
Jihoon Kang842b9992024-02-08 01:41:51 +0000792func TestApexWithDessertSha(t *testing.T) {
793 ctx := testApex(t, `
794 apex_defaults {
795 name: "my_defaults",
796 key: "myapex.key",
797 product_specific: true,
798 file_contexts: ":my-file-contexts",
799 updatable: false,
800 }
801 apex {
802 name: "myapex_30",
803 min_sdk_version: "30",
804 defaults: ["my_defaults"],
805 }
806
807 apex {
808 name: "myapex_current",
809 min_sdk_version: "current",
810 defaults: ["my_defaults"],
811 }
812
813 apex {
814 name: "myapex_none",
815 defaults: ["my_defaults"],
816 }
817
818 apex_key {
819 name: "myapex.key",
820 public_key: "testkey.avbpubkey",
821 private_key: "testkey.pem",
822 }
823
824 filegroup {
825 name: "my-file-contexts",
826 srcs: ["product_specific_file_contexts"],
827 }
828 `, withFiles(map[string][]byte{
829 "product_specific_file_contexts": nil,
830 }), android.FixtureModifyProductVariables(
831 func(variables android.FixtureProductVariables) {
832 variables.Unbundled_build = proptools.BoolPtr(true)
833 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
834 }), android.FixtureMergeEnv(map[string]string{
835 "UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA": "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
836 }))
837
838 testCases := []struct {
839 module string
840 minSdkVersion string
841 }{
842 {
843 module: "myapex_30",
844 minSdkVersion: "30",
845 },
846 {
847 module: "myapex_current",
848 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
849 },
850 {
851 module: "myapex_none",
852 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
853 },
854 }
855 for _, tc := range testCases {
856 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
857 args := module.Rule("apexRule").Args
858 optFlags := args["opt_flags"]
859 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
860 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
861 }
862 }
863}
864
Jooyung Hanaf730952023-02-28 14:13:38 +0900865func TestFileContexts(t *testing.T) {
Jooyung Hanbe953902023-05-31 16:42:16 +0900866 for _, vendor := range []bool{true, false} {
Jooyung Hanaf730952023-02-28 14:13:38 +0900867 prop := ""
Jooyung Hanbe953902023-05-31 16:42:16 +0900868 if vendor {
869 prop = "vendor: true,\n"
Jooyung Hanaf730952023-02-28 14:13:38 +0900870 }
871 ctx := testApex(t, `
872 apex {
873 name: "myapex",
874 key: "myapex.key",
Jooyung Hanaf730952023-02-28 14:13:38 +0900875 updatable: false,
Jooyung Hanaf730952023-02-28 14:13:38 +0900876 `+prop+`
877 }
878
879 apex_key {
880 name: "myapex.key",
881 public_key: "testkey.avbpubkey",
882 private_key: "testkey.pem",
883 }
Jooyung Hanbe953902023-05-31 16:42:16 +0900884 `)
Jooyung Hanaf730952023-02-28 14:13:38 +0900885
Jooyung Hana0503a52023-08-23 13:12:50 +0900886 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("file_contexts")
Jooyung Hanbe953902023-05-31 16:42:16 +0900887 if vendor {
888 android.AssertStringDoesContain(t, "should force-label as vendor_apex_metadata_file",
889 rule.RuleParams.Command,
890 "apex_manifest\\\\.pb u:object_r:vendor_apex_metadata_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900891 } else {
Jooyung Hanbe953902023-05-31 16:42:16 +0900892 android.AssertStringDoesContain(t, "should force-label as system_file",
893 rule.RuleParams.Command,
894 "apex_manifest\\\\.pb u:object_r:system_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900895 }
896 }
897}
898
Jiyong Park25fc6a92018-11-18 18:02:45 +0900899func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800900 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900901 apex {
902 name: "myapex",
903 key: "myapex.key",
904 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900905 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000906 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900907 }
908
909 apex_key {
910 name: "myapex.key",
911 public_key: "testkey.avbpubkey",
912 private_key: "testkey.pem",
913 }
914
915 cc_library {
916 name: "mylib",
917 srcs: ["mylib.cpp"],
Spandan Das357ffcc2024-07-24 18:07:48 +0000918 shared_libs: ["mylib2", "mylib3", "my_prebuilt_platform_lib", "my_prebuilt_platform_stub_only_lib"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900919 system_shared_libs: [],
920 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000921 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900922 }
923
924 cc_library {
925 name: "mylib2",
926 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900927 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900928 system_shared_libs: [],
929 stl: "none",
930 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +0000931 symbol_file: "mylib2.map.txt",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900932 versions: ["1", "2", "3"],
933 },
934 }
935
936 cc_library {
937 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900938 srcs: ["mylib.cpp"],
939 shared_libs: ["mylib4"],
940 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900941 stl: "none",
942 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +0000943 symbol_file: "mylib3.map.txt",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900944 versions: ["10", "11", "12"],
945 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000946 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900947 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900948
949 cc_library {
950 name: "mylib4",
951 srcs: ["mylib.cpp"],
952 system_shared_libs: [],
953 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000954 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900955 }
Jiyong Park105dc322021-06-11 17:22:09 +0900956
Spandan Das357ffcc2024-07-24 18:07:48 +0000957 cc_prebuilt_library_shared {
958 name: "my_prebuilt_platform_lib",
959 stubs: {
960 symbol_file: "my_prebuilt_platform_lib.map.txt",
961 versions: ["1", "2", "3"],
962 },
963 srcs: ["foo.so"],
964 }
965
966 // Similar to my_prebuilt_platform_lib, but this library only provides stubs, i.e. srcs is empty
967 cc_prebuilt_library_shared {
968 name: "my_prebuilt_platform_stub_only_lib",
969 stubs: {
970 symbol_file: "my_prebuilt_platform_stub_only_lib.map.txt",
971 versions: ["1", "2", "3"],
972 }
973 }
974
Jiyong Park105dc322021-06-11 17:22:09 +0900975 rust_binary {
976 name: "foo.rust",
977 srcs: ["foo.rs"],
978 shared_libs: ["libfoo.shared_from_rust"],
979 prefer_rlib: true,
980 apex_available: ["myapex"],
981 }
982
983 cc_library_shared {
984 name: "libfoo.shared_from_rust",
985 srcs: ["mylib.cpp"],
986 system_shared_libs: [],
987 stl: "none",
988 stubs: {
989 versions: ["10", "11", "12"],
990 },
991 }
992
Jiyong Park25fc6a92018-11-18 18:02:45 +0900993 `)
994
Jooyung Hana0503a52023-08-23 13:12:50 +0900995 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900996 copyCmds := apexRule.Args["copy_commands"]
997
998 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800999 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001000
1001 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -08001002 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001003
1004 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -08001005 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001006
Colin Crossaede88c2020-08-11 12:17:01 -07001007 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001008
1009 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001010 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001011 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +09001012 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001013
1014 // 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 -07001015 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001016 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -07001017 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +09001018
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -07001019 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
1020 // is replaced by sharing of "cFlags" in cc/builder.go.
1021 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
1022 // module variable representing "cflags". So it was not detected by ensureNotContains.
1023 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
1024 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
1025 // including the original cflags's "-include mylib.h".
1026 //
Jiyong Park64379952018-12-13 18:37:29 +09001027 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -07001028 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1029 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +09001030
Jiyong Park85cc35a2022-07-17 11:30:47 +09001031 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1032 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
1033 // Ensure that genstub for apex-provided lib is invoked with --apex
1034 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +09001035
Jooyung Hana0503a52023-08-23 13:12:50 +09001036 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +09001037 "lib64/mylib.so",
1038 "lib64/mylib3.so",
1039 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +09001040 "bin/foo.rust",
1041 "lib64/libc++.so", // by the implicit dependency from foo.rust
1042 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +09001043 })
Jiyong Park105dc322021-06-11 17:22:09 +09001044
1045 // Ensure that stub dependency from a rust module is not included
1046 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1047 // The rust module is linked to the stub cc library
Colin Cross004bd3f2023-10-02 11:39:17 -07001048 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park105dc322021-06-11 17:22:09 +09001049 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1050 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +09001051
Jooyung Hana0503a52023-08-23 13:12:50 +09001052 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jiyong Park34d5c332022-02-24 18:02:44 +09001053 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Spandan Das357ffcc2024-07-24 18:07:48 +00001054
1055 // Ensure that mylib is linking with the latest version of stubs for my_prebuilt_platform_lib
1056 ensureContains(t, mylibLdFlags, "my_prebuilt_platform_lib/android_arm64_armv8-a_shared_current/my_prebuilt_platform_lib.so")
1057 // ... and not linking to the non-stub (impl) variant of my_prebuilt_platform_lib
1058 ensureNotContains(t, mylibLdFlags, "my_prebuilt_platform_lib/android_arm64_armv8-a_shared/my_prebuilt_platform_lib.so")
1059 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1060 ensureContains(t, ctx.ModuleForTests("my_prebuilt_platform_lib", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
1061
1062 // Ensure that mylib is linking with the latest version of stubs for my_prebuilt_platform_lib
1063 ensureContains(t, mylibLdFlags, "my_prebuilt_platform_stub_only_lib/android_arm64_armv8-a_shared_current/my_prebuilt_platform_stub_only_lib.so")
1064 // ... and not linking to the non-stub (impl) variant of my_prebuilt_platform_lib
1065 ensureNotContains(t, mylibLdFlags, "my_prebuilt_platform_stub_only_lib/android_arm64_armv8-a_shared/my_prebuilt_platform_stub_only_lib.so")
1066 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1067 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 +09001068}
1069
Jooyung Han20348752023-12-05 15:23:56 +09001070func TestApexShouldNotEmbedStubVariant(t *testing.T) {
1071 testApexError(t, `module "myapex" .*: native_shared_libs: "libbar" is a stub`, `
1072 apex {
1073 name: "myapex",
1074 key: "myapex.key",
1075 vendor: true,
1076 updatable: false,
1077 native_shared_libs: ["libbar"], // should not add an LLNDK stub in a vendor apex
1078 }
1079
1080 apex_key {
1081 name: "myapex.key",
1082 public_key: "testkey.avbpubkey",
1083 private_key: "testkey.pem",
1084 }
1085
1086 cc_library {
1087 name: "libbar",
1088 srcs: ["mylib.cpp"],
1089 llndk: {
1090 symbol_file: "libbar.map.txt",
1091 }
1092 }
1093 `)
1094}
1095
Jiyong Park1bc84122021-06-22 20:23:05 +09001096func TestApexCanUsePrivateApis(t *testing.T) {
1097 ctx := testApex(t, `
1098 apex {
1099 name: "myapex",
1100 key: "myapex.key",
1101 native_shared_libs: ["mylib"],
1102 binaries: ["foo.rust"],
1103 updatable: false,
1104 platform_apis: true,
1105 }
1106
1107 apex_key {
1108 name: "myapex.key",
1109 public_key: "testkey.avbpubkey",
1110 private_key: "testkey.pem",
1111 }
1112
1113 cc_library {
1114 name: "mylib",
1115 srcs: ["mylib.cpp"],
1116 shared_libs: ["mylib2"],
1117 system_shared_libs: [],
1118 stl: "none",
1119 apex_available: [ "myapex" ],
1120 }
1121
1122 cc_library {
1123 name: "mylib2",
1124 srcs: ["mylib.cpp"],
1125 cflags: ["-include mylib.h"],
1126 system_shared_libs: [],
1127 stl: "none",
1128 stubs: {
1129 versions: ["1", "2", "3"],
1130 },
1131 }
1132
1133 rust_binary {
1134 name: "foo.rust",
1135 srcs: ["foo.rs"],
1136 shared_libs: ["libfoo.shared_from_rust"],
1137 prefer_rlib: true,
1138 apex_available: ["myapex"],
1139 }
1140
1141 cc_library_shared {
1142 name: "libfoo.shared_from_rust",
1143 srcs: ["mylib.cpp"],
1144 system_shared_libs: [],
1145 stl: "none",
1146 stubs: {
1147 versions: ["10", "11", "12"],
1148 },
1149 }
1150 `)
1151
Jooyung Hana0503a52023-08-23 13:12:50 +09001152 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park1bc84122021-06-22 20:23:05 +09001153 copyCmds := apexRule.Args["copy_commands"]
1154
1155 // Ensure that indirect stubs dep is not included
1156 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1157 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1158
1159 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1160 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001161 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001162 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1163 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Colin Cross004bd3f2023-10-02 11:39:17 -07001164 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001165 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1166 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1167}
1168
Colin Cross7812fd32020-09-25 12:35:10 -07001169func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1170 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001171 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001172 apex {
1173 name: "myapex",
1174 key: "myapex.key",
1175 native_shared_libs: ["mylib", "mylib3"],
1176 min_sdk_version: "29",
1177 }
1178
1179 apex_key {
1180 name: "myapex.key",
1181 public_key: "testkey.avbpubkey",
1182 private_key: "testkey.pem",
1183 }
1184
1185 cc_library {
1186 name: "mylib",
1187 srcs: ["mylib.cpp"],
1188 shared_libs: ["mylib2", "mylib3"],
1189 system_shared_libs: [],
1190 stl: "none",
1191 apex_available: [ "myapex" ],
1192 min_sdk_version: "28",
1193 }
1194
1195 cc_library {
1196 name: "mylib2",
1197 srcs: ["mylib.cpp"],
1198 cflags: ["-include mylib.h"],
1199 system_shared_libs: [],
1200 stl: "none",
1201 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +00001202 symbol_file: "mylib2.map.txt",
Colin Cross7812fd32020-09-25 12:35:10 -07001203 versions: ["28", "29", "30", "current"],
1204 },
1205 min_sdk_version: "28",
1206 }
1207
1208 cc_library {
1209 name: "mylib3",
1210 srcs: ["mylib.cpp"],
1211 shared_libs: ["mylib4"],
1212 system_shared_libs: [],
1213 stl: "none",
1214 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +00001215 symbol_file: "mylib3.map.txt",
Colin Cross7812fd32020-09-25 12:35:10 -07001216 versions: ["28", "29", "30", "current"],
1217 },
1218 apex_available: [ "myapex" ],
1219 min_sdk_version: "28",
1220 }
1221
1222 cc_library {
1223 name: "mylib4",
1224 srcs: ["mylib.cpp"],
1225 system_shared_libs: [],
1226 stl: "none",
1227 apex_available: [ "myapex" ],
1228 min_sdk_version: "28",
1229 }
1230 `)
1231
Jooyung Hana0503a52023-08-23 13:12:50 +09001232 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Colin Cross7812fd32020-09-25 12:35:10 -07001233 copyCmds := apexRule.Args["copy_commands"]
1234
1235 // Ensure that direct non-stubs dep is always included
1236 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1237
1238 // Ensure that indirect stubs dep is not included
1239 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1240
1241 // Ensure that direct stubs dep is included
1242 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1243
1244 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1245
Jiyong Park55549df2021-02-26 23:57:23 +09001246 // Ensure that mylib is linking with the latest version of stub for mylib2
1247 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001248 // ... and not linking to the non-stub (impl) variant of mylib2
1249 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1250
1251 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1252 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1253 // .. and not linking to the stubs variant of mylib3
1254 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1255
1256 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001257 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001258 ensureNotContains(t, mylib2Cflags, "-include ")
1259
Jiyong Park85cc35a2022-07-17 11:30:47 +09001260 // Ensure that genstub is invoked with --systemapi
1261 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001262
Jooyung Hana0503a52023-08-23 13:12:50 +09001263 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Colin Cross7812fd32020-09-25 12:35:10 -07001264 "lib64/mylib.so",
1265 "lib64/mylib3.so",
1266 "lib64/mylib4.so",
1267 })
1268}
1269
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001270func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1271 t.Parallel()
1272 // myapex (Z)
1273 // mylib -----------------.
1274 // |
1275 // otherapex (29) |
1276 // libstub's versions: 29 Z current
1277 // |
1278 // <platform> |
1279 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001280 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001281 apex {
1282 name: "myapex",
1283 key: "myapex.key",
1284 native_shared_libs: ["mylib"],
1285 min_sdk_version: "Z", // non-final
1286 }
1287
1288 cc_library {
1289 name: "mylib",
1290 srcs: ["mylib.cpp"],
1291 shared_libs: ["libstub"],
1292 apex_available: ["myapex"],
1293 min_sdk_version: "Z",
1294 }
1295
1296 apex_key {
1297 name: "myapex.key",
1298 public_key: "testkey.avbpubkey",
1299 private_key: "testkey.pem",
1300 }
1301
1302 apex {
1303 name: "otherapex",
1304 key: "myapex.key",
1305 native_shared_libs: ["libstub"],
1306 min_sdk_version: "29",
1307 }
1308
1309 cc_library {
1310 name: "libstub",
1311 srcs: ["mylib.cpp"],
1312 stubs: {
1313 versions: ["29", "Z", "current"],
1314 },
1315 apex_available: ["otherapex"],
1316 min_sdk_version: "29",
1317 }
1318
1319 // platform module depending on libstub from otherapex should use the latest stub("current")
1320 cc_library {
1321 name: "libplatform",
1322 srcs: ["mylib.cpp"],
1323 shared_libs: ["libstub"],
1324 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001325 `,
1326 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1327 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1328 variables.Platform_sdk_final = proptools.BoolPtr(false)
1329 variables.Platform_version_active_codenames = []string{"Z"}
1330 }),
1331 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001332
Jiyong Park55549df2021-02-26 23:57:23 +09001333 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001334 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001335 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001336 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001337 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001338
1339 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1340 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1341 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1342 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1343 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1344}
1345
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001346func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001347 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001348 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001349 name: "myapex2",
1350 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001351 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001352 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001353 }
1354
1355 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001356 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001357 public_key: "testkey.avbpubkey",
1358 private_key: "testkey.pem",
1359 }
1360
1361 cc_library {
1362 name: "mylib",
1363 srcs: ["mylib.cpp"],
1364 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001365 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001366 system_shared_libs: [],
1367 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001368 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001369 }
1370
1371 cc_library {
1372 name: "libfoo",
1373 srcs: ["mylib.cpp"],
1374 shared_libs: ["libbar"],
1375 system_shared_libs: [],
1376 stl: "none",
1377 stubs: {
1378 versions: ["10", "20", "30"],
1379 },
1380 }
1381
1382 cc_library {
1383 name: "libbar",
1384 srcs: ["mylib.cpp"],
1385 system_shared_libs: [],
1386 stl: "none",
1387 }
1388
Jiyong Park678c8812020-02-07 17:25:49 +09001389 cc_library_static {
1390 name: "libbaz",
1391 srcs: ["mylib.cpp"],
1392 system_shared_libs: [],
1393 stl: "none",
1394 apex_available: [ "myapex2" ],
1395 }
1396
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001397 `)
1398
Jooyung Hana0503a52023-08-23 13:12:50 +09001399 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001400 copyCmds := apexRule.Args["copy_commands"]
1401
1402 // Ensure that direct non-stubs dep is always included
1403 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1404
1405 // Ensure that indirect stubs dep is not included
1406 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1407
1408 // Ensure that dependency of stubs is not included
1409 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1410
Colin Crossaede88c2020-08-11 12:17:01 -07001411 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001412
1413 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001414 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001415 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001416 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001417
Jiyong Park3ff16992019-12-27 14:11:47 +09001418 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001419
1420 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1421 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001422
Colin Crossf61d03d2023-11-02 16:56:39 -07001423 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1424 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001425 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001426
Colin Crossf61d03d2023-11-02 16:56:39 -07001427 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1428 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001429 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001430}
1431
Jooyung Hand3639552019-08-09 12:57:43 +09001432func TestApexWithRuntimeLibsDependency(t *testing.T) {
1433 /*
1434 myapex
1435 |
1436 v (runtime_libs)
1437 mylib ------+------> libfoo [provides stub]
1438 |
1439 `------> libbar
1440 */
Colin Cross1c460562021-02-16 17:55:47 -08001441 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001442 apex {
1443 name: "myapex",
1444 key: "myapex.key",
1445 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001446 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001447 }
1448
1449 apex_key {
1450 name: "myapex.key",
1451 public_key: "testkey.avbpubkey",
1452 private_key: "testkey.pem",
1453 }
1454
1455 cc_library {
1456 name: "mylib",
1457 srcs: ["mylib.cpp"],
Liz Kammer5f108fa2023-05-11 14:33:17 -04001458 static_libs: ["libstatic"],
1459 shared_libs: ["libshared"],
Jooyung Hand3639552019-08-09 12:57:43 +09001460 runtime_libs: ["libfoo", "libbar"],
1461 system_shared_libs: [],
1462 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001463 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001464 }
1465
1466 cc_library {
1467 name: "libfoo",
1468 srcs: ["mylib.cpp"],
1469 system_shared_libs: [],
1470 stl: "none",
1471 stubs: {
1472 versions: ["10", "20", "30"],
1473 },
1474 }
1475
1476 cc_library {
1477 name: "libbar",
1478 srcs: ["mylib.cpp"],
1479 system_shared_libs: [],
1480 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001481 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001482 }
1483
Liz Kammer5f108fa2023-05-11 14:33:17 -04001484 cc_library {
1485 name: "libstatic",
1486 srcs: ["mylib.cpp"],
1487 system_shared_libs: [],
1488 stl: "none",
1489 apex_available: [ "myapex" ],
1490 runtime_libs: ["libstatic_to_runtime"],
1491 }
1492
1493 cc_library {
1494 name: "libshared",
1495 srcs: ["mylib.cpp"],
1496 system_shared_libs: [],
1497 stl: "none",
1498 apex_available: [ "myapex" ],
1499 runtime_libs: ["libshared_to_runtime"],
1500 }
1501
1502 cc_library {
1503 name: "libstatic_to_runtime",
1504 srcs: ["mylib.cpp"],
1505 system_shared_libs: [],
1506 stl: "none",
1507 apex_available: [ "myapex" ],
1508 }
1509
1510 cc_library {
1511 name: "libshared_to_runtime",
1512 srcs: ["mylib.cpp"],
1513 system_shared_libs: [],
1514 stl: "none",
1515 apex_available: [ "myapex" ],
1516 }
Jooyung Hand3639552019-08-09 12:57:43 +09001517 `)
1518
Jooyung Hana0503a52023-08-23 13:12:50 +09001519 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001520 copyCmds := apexRule.Args["copy_commands"]
1521
1522 // Ensure that direct non-stubs dep is always included
1523 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1524
1525 // Ensure that indirect stubs dep is not included
1526 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1527
1528 // Ensure that runtime_libs dep in included
1529 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
Liz Kammer5f108fa2023-05-11 14:33:17 -04001530 ensureContains(t, copyCmds, "image.apex/lib64/libshared.so")
1531 ensureContains(t, copyCmds, "image.apex/lib64/libshared_to_runtime.so")
1532
1533 ensureNotContains(t, copyCmds, "image.apex/lib64/libstatic_to_runtime.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001534
Jooyung Hana0503a52023-08-23 13:12:50 +09001535 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001536 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1537 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001538}
1539
Paul Duffina02cae32021-03-09 01:44:06 +00001540var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1541 cc.PrepareForTestWithCcBuildComponents,
1542 PrepareForTestWithApexBuildComponents,
1543 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001544 apex {
1545 name: "com.android.runtime",
1546 key: "com.android.runtime.key",
1547 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001548 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001549 }
1550
1551 apex_key {
1552 name: "com.android.runtime.key",
1553 public_key: "testkey.avbpubkey",
1554 private_key: "testkey.pem",
1555 }
Paul Duffina02cae32021-03-09 01:44:06 +00001556 `),
1557 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1558)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001559
Paul Duffina02cae32021-03-09 01:44:06 +00001560func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001561 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001562 cc_library {
1563 name: "libc",
1564 no_libcrt: true,
1565 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001566 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001567 stl: "none",
1568 system_shared_libs: [],
1569 stubs: { versions: ["1"] },
1570 apex_available: ["com.android.runtime"],
1571
1572 sanitize: {
1573 hwaddress: true,
1574 }
1575 }
1576
1577 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001578 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001579 no_libcrt: true,
1580 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001581 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001582 stl: "none",
1583 system_shared_libs: [],
1584 srcs: [""],
1585 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001586 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001587
1588 sanitize: {
1589 never: true,
1590 },
Spandan Das4de7b492023-05-05 21:13:01 +00001591 apex_available: [
1592 "//apex_available:anyapex",
1593 "//apex_available:platform",
1594 ],
Paul Duffina02cae32021-03-09 01:44:06 +00001595 } `)
1596 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001597
Jooyung Hana0503a52023-08-23 13:12:50 +09001598 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001599 "lib64/bionic/libc.so",
1600 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1601 })
1602
Colin Cross4c4c1be2022-02-10 11:41:18 -08001603 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001604
1605 installed := hwasan.Description("install libclang_rt.hwasan")
1606 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1607
1608 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1609 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1610 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1611}
1612
1613func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001614 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001615 prepareForTestOfRuntimeApexWithHwasan,
1616 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1617 variables.SanitizeDevice = []string{"hwaddress"}
1618 }),
1619 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001620 cc_library {
1621 name: "libc",
1622 no_libcrt: true,
1623 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001624 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001625 stl: "none",
1626 system_shared_libs: [],
1627 stubs: { versions: ["1"] },
1628 apex_available: ["com.android.runtime"],
1629 }
1630
1631 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001632 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001633 no_libcrt: true,
1634 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001635 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001636 stl: "none",
1637 system_shared_libs: [],
1638 srcs: [""],
1639 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001640 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001641
1642 sanitize: {
1643 never: true,
1644 },
Spandan Das4de7b492023-05-05 21:13:01 +00001645 apex_available: [
1646 "//apex_available:anyapex",
1647 "//apex_available:platform",
1648 ],
Jooyung Han8ce8db92020-05-15 19:05:05 +09001649 }
Paul Duffina02cae32021-03-09 01:44:06 +00001650 `)
1651 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001652
Jooyung Hana0503a52023-08-23 13:12:50 +09001653 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001654 "lib64/bionic/libc.so",
1655 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1656 })
1657
Colin Cross4c4c1be2022-02-10 11:41:18 -08001658 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001659
1660 installed := hwasan.Description("install libclang_rt.hwasan")
1661 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1662
1663 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1664 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1665 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1666}
1667
Jooyung Han61b66e92020-03-21 14:21:46 +00001668func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1669 testcases := []struct {
1670 name string
1671 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001672 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001673 shouldLink string
1674 shouldNotLink []string
1675 }{
1676 {
Jiyong Park55549df2021-02-26 23:57:23 +09001677 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001678 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001679 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001680 shouldLink: "current",
1681 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001682 },
1683 {
Jiyong Park55549df2021-02-26 23:57:23 +09001684 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001685 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001686 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001687 shouldLink: "current",
1688 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001689 },
1690 }
1691 for _, tc := range testcases {
1692 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001693 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001694 apex {
1695 name: "myapex",
1696 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001697 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001698 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001699 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001700 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001701
Jooyung Han61b66e92020-03-21 14:21:46 +00001702 apex_key {
1703 name: "myapex.key",
1704 public_key: "testkey.avbpubkey",
1705 private_key: "testkey.pem",
1706 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001707
Jooyung Han61b66e92020-03-21 14:21:46 +00001708 cc_library {
1709 name: "mylib",
1710 srcs: ["mylib.cpp"],
1711 vendor_available: true,
1712 shared_libs: ["libbar"],
1713 system_shared_libs: [],
1714 stl: "none",
1715 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001716 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001717 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001718
Jooyung Han61b66e92020-03-21 14:21:46 +00001719 cc_library {
1720 name: "libbar",
1721 srcs: ["mylib.cpp"],
1722 system_shared_libs: [],
1723 stl: "none",
1724 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001725 llndk: {
1726 symbol_file: "libbar.map.txt",
1727 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001728 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001729 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001730 withUnbundledBuild,
1731 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001732
Jooyung Han61b66e92020-03-21 14:21:46 +00001733 // Ensure that LLNDK dep is not included
Jooyung Hana0503a52023-08-23 13:12:50 +09001734 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00001735 "lib64/mylib.so",
1736 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001737
Jooyung Han61b66e92020-03-21 14:21:46 +00001738 // Ensure that LLNDK dep is required
Jooyung Hana0503a52023-08-23 13:12:50 +09001739 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han61b66e92020-03-21 14:21:46 +00001740 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1741 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001742
Steven Moreland2c4000c2021-04-27 02:08:49 +00001743 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1744 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001745 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001746 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001747 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001748
Steven Moreland2c4000c2021-04-27 02:08:49 +00001749 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001750 ver := tc.shouldLink
1751 if tc.shouldLink == "current" {
1752 ver = strconv.Itoa(android.FutureApiLevelInt)
1753 }
1754 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001755 })
1756 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001757}
1758
Jiyong Park25fc6a92018-11-18 18:02:45 +09001759func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001760 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001761 apex {
1762 name: "myapex",
1763 key: "myapex.key",
1764 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001765 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001766 }
1767
1768 apex_key {
1769 name: "myapex.key",
1770 public_key: "testkey.avbpubkey",
1771 private_key: "testkey.pem",
1772 }
1773
1774 cc_library {
1775 name: "mylib",
1776 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001777 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001778 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_shared {
1784 name: "mylib_shared",
1785 srcs: ["mylib.cpp"],
1786 shared_libs: ["libdl#27"],
1787 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001788 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001789 }
1790
1791 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001792 name: "libBootstrap",
1793 srcs: ["mylib.cpp"],
1794 stl: "none",
1795 bootstrap: true,
1796 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001797 `)
1798
Jooyung Hana0503a52023-08-23 13:12:50 +09001799 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001800 copyCmds := apexRule.Args["copy_commands"]
1801
1802 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001803 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001804 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1805 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001806
1807 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001808 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001809
Colin Crossaede88c2020-08-11 12:17:01 -07001810 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1811 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1812 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001813
1814 // For dependency to libc
1815 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001816 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001817 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001818 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001819 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001820 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1821 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001822
1823 // For dependency to libm
1824 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001825 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001826 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001827 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001828 // ... and is not compiling with the stub
1829 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1830 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1831
1832 // For dependency to libdl
1833 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001834 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001835 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001836 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1837 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001838 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001839 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001840 // ... Cflags from stub is correctly exported to mylib
1841 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1842 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001843
1844 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001845 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1846 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1847 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1848 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001849}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001850
Jooyung Han749dc692020-04-15 11:03:39 +09001851func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001852 // there are three links between liba --> libz.
1853 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001854 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001855 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001856 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001857 apex {
1858 name: "myapex",
1859 key: "myapex.key",
1860 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001861 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001862 }
1863
1864 apex {
1865 name: "otherapex",
1866 key: "myapex.key",
1867 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001868 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001869 }
1870
1871 apex_key {
1872 name: "myapex.key",
1873 public_key: "testkey.avbpubkey",
1874 private_key: "testkey.pem",
1875 }
1876
1877 cc_library {
1878 name: "libx",
1879 shared_libs: ["liba"],
1880 system_shared_libs: [],
1881 stl: "none",
1882 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001883 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001884 }
1885
1886 cc_library {
1887 name: "liby",
1888 shared_libs: ["liba"],
1889 system_shared_libs: [],
1890 stl: "none",
1891 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001892 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001893 }
1894
1895 cc_library {
1896 name: "liba",
1897 shared_libs: ["libz"],
1898 system_shared_libs: [],
1899 stl: "none",
1900 apex_available: [
1901 "//apex_available:anyapex",
1902 "//apex_available:platform",
1903 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001904 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001905 }
1906
1907 cc_library {
1908 name: "libz",
1909 system_shared_libs: [],
1910 stl: "none",
1911 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001912 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001913 },
1914 }
Jooyung Han749dc692020-04-15 11:03:39 +09001915 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001916
1917 expectLink := func(from, from_variant, to, to_variant string) {
1918 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1919 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1920 }
1921 expectNoLink := func(from, from_variant, to, to_variant string) {
1922 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1923 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1924 }
1925 // platform liba is linked to non-stub version
1926 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001927 // liba in myapex is linked to current
1928 expectLink("liba", "shared_apex29", "libz", "shared_current")
1929 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001930 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001931 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001932 // liba in otherapex is linked to current
1933 expectLink("liba", "shared_apex30", "libz", "shared_current")
1934 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001935 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1936 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001937}
1938
Jooyung Hanaed150d2020-04-02 01:41:41 +09001939func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001940 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001941 apex {
1942 name: "myapex",
1943 key: "myapex.key",
1944 native_shared_libs: ["libx"],
1945 min_sdk_version: "R",
1946 }
1947
1948 apex_key {
1949 name: "myapex.key",
1950 public_key: "testkey.avbpubkey",
1951 private_key: "testkey.pem",
1952 }
1953
1954 cc_library {
1955 name: "libx",
1956 shared_libs: ["libz"],
1957 system_shared_libs: [],
1958 stl: "none",
1959 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001960 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001961 }
1962
1963 cc_library {
1964 name: "libz",
1965 system_shared_libs: [],
1966 stl: "none",
1967 stubs: {
1968 versions: ["29", "R"],
1969 },
1970 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001971 `,
1972 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1973 variables.Platform_version_active_codenames = []string{"R"}
1974 }),
1975 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001976
1977 expectLink := func(from, from_variant, to, to_variant string) {
1978 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1979 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1980 }
1981 expectNoLink := func(from, from_variant, to, to_variant string) {
1982 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1983 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1984 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001985 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1986 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001987 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1988 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001989}
1990
Jooyung Han4c4da062021-06-23 10:23:16 +09001991func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1992 testApex(t, `
1993 apex {
1994 name: "myapex",
1995 key: "myapex.key",
1996 java_libs: ["libx"],
1997 min_sdk_version: "S",
1998 }
1999
2000 apex_key {
2001 name: "myapex.key",
2002 public_key: "testkey.avbpubkey",
2003 private_key: "testkey.pem",
2004 }
2005
2006 java_library {
2007 name: "libx",
2008 srcs: ["a.java"],
2009 apex_available: [ "myapex" ],
2010 sdk_version: "current",
2011 min_sdk_version: "S", // should be okay
2012 }
2013 `,
2014 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2015 variables.Platform_version_active_codenames = []string{"S"}
2016 variables.Platform_sdk_codename = proptools.StringPtr("S")
2017 }),
2018 )
2019}
2020
Jooyung Han749dc692020-04-15 11:03:39 +09002021func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002022 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002023 apex {
2024 name: "myapex",
2025 key: "myapex.key",
2026 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002027 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002028 }
2029
2030 apex_key {
2031 name: "myapex.key",
2032 public_key: "testkey.avbpubkey",
2033 private_key: "testkey.pem",
2034 }
2035
2036 cc_library {
2037 name: "libx",
2038 shared_libs: ["libz"],
2039 system_shared_libs: [],
2040 stl: "none",
2041 apex_available: [ "myapex" ],
2042 }
2043
2044 cc_library {
2045 name: "libz",
2046 system_shared_libs: [],
2047 stl: "none",
2048 stubs: {
2049 versions: ["1", "2"],
2050 },
2051 }
2052 `)
2053
2054 expectLink := func(from, from_variant, to, to_variant string) {
2055 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2056 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2057 }
2058 expectNoLink := func(from, from_variant, to, to_variant string) {
2059 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2060 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2061 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002062 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002063 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002064 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07002065 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09002066}
2067
Jooyung Handfc864c2023-03-20 18:19:07 +09002068func TestApexMinSdkVersion_InVendorApex(t *testing.T) {
Jiyong Park5df7bd32021-08-25 16:18:46 +09002069 ctx := testApex(t, `
2070 apex {
2071 name: "myapex",
2072 key: "myapex.key",
2073 native_shared_libs: ["mylib"],
Jooyung Handfc864c2023-03-20 18:19:07 +09002074 updatable: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002075 vendor: true,
2076 min_sdk_version: "29",
2077 }
2078
2079 apex_key {
2080 name: "myapex.key",
2081 public_key: "testkey.avbpubkey",
2082 private_key: "testkey.pem",
2083 }
2084
2085 cc_library {
2086 name: "mylib",
Jooyung Handfc864c2023-03-20 18:19:07 +09002087 srcs: ["mylib.cpp"],
Jiyong Park5df7bd32021-08-25 16:18:46 +09002088 vendor_available: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002089 min_sdk_version: "29",
Jooyung Handfc864c2023-03-20 18:19:07 +09002090 shared_libs: ["libbar"],
2091 }
2092
2093 cc_library {
2094 name: "libbar",
2095 stubs: { versions: ["29", "30"] },
2096 llndk: { symbol_file: "libbar.map.txt" },
Jiyong Park5df7bd32021-08-25 16:18:46 +09002097 }
2098 `)
2099
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002100 vendorVariant := "android_vendor_arm64_armv8-a"
Jiyong Park5df7bd32021-08-25 16:18:46 +09002101
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002102 mylib := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29")
Jooyung Handfc864c2023-03-20 18:19:07 +09002103
2104 // Ensure that mylib links with "current" LLNDK
2105 libFlags := names(mylib.Rule("ld").Args["libFlags"])
Jooyung Han5e8994e2024-03-12 14:12:12 +09002106 ensureListContains(t, libFlags, "out/soong/.intermediates/libbar/"+vendorVariant+"_shared/libbar.so")
Jooyung Handfc864c2023-03-20 18:19:07 +09002107
2108 // Ensure that mylib is targeting 29
2109 ccRule := ctx.ModuleForTests("mylib", vendorVariant+"_static_apex29").Output("obj/mylib.o")
2110 ensureContains(t, ccRule.Args["cFlags"], "-target aarch64-linux-android29")
2111
2112 // Ensure that the correct variant of crtbegin_so is used.
2113 crtBegin := mylib.Rule("ld").Args["crtBegin"]
2114 ensureContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
Jiyong Park5df7bd32021-08-25 16:18:46 +09002115
2116 // Ensure that the crtbegin_so used by the APEX is targeting 29
2117 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
2118 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
2119}
2120
Jooyung Han4495f842023-04-25 16:39:59 +09002121func TestTrackAllowedDeps(t *testing.T) {
2122 ctx := testApex(t, `
2123 apex {
2124 name: "myapex",
2125 key: "myapex.key",
2126 updatable: true,
2127 native_shared_libs: [
2128 "mylib",
2129 "yourlib",
2130 ],
2131 min_sdk_version: "29",
2132 }
2133
2134 apex {
2135 name: "myapex2",
2136 key: "myapex.key",
2137 updatable: false,
2138 native_shared_libs: ["yourlib"],
2139 }
2140
2141 apex_key {
2142 name: "myapex.key",
2143 public_key: "testkey.avbpubkey",
2144 private_key: "testkey.pem",
2145 }
2146
2147 cc_library {
2148 name: "mylib",
2149 srcs: ["mylib.cpp"],
2150 shared_libs: ["libbar"],
2151 min_sdk_version: "29",
2152 apex_available: ["myapex"],
2153 }
2154
2155 cc_library {
2156 name: "libbar",
2157 stubs: { versions: ["29", "30"] },
2158 }
2159
2160 cc_library {
2161 name: "yourlib",
2162 srcs: ["mylib.cpp"],
2163 min_sdk_version: "29",
2164 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2165 }
2166 `, withFiles(android.MockFS{
2167 "packages/modules/common/build/allowed_deps.txt": nil,
2168 }))
2169
2170 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2171 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2172 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002173 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002174 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002175 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002176
Jooyung Hana0503a52023-08-23 13:12:50 +09002177 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07002178 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2179 myapex.Output("depsinfo/flatlist.txt")), "\n")
Jooyung Han4495f842023-04-25 16:39:59 +09002180 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2181 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2182 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2183 flatlist, "mylib:(minSdkVersion:29)")
2184 android.AssertStringListContains(t, "track platform-available lib",
2185 flatlist, "yourlib(minSdkVersion:29)")
2186}
2187
Nouby Mohamed8609a552024-09-04 22:19:51 +00002188func TestTrackCustomAllowedDepsInvalidDefaultTxt(t *testing.T) {
2189 ctx := testApex(t, `
2190 apex {
2191 name: "myapex",
2192 key: "myapex.key",
2193 updatable: true,
2194 native_shared_libs: [
2195 "mylib",
2196 "yourlib",
2197 ],
2198 min_sdk_version: "29",
2199 }
2200
2201 apex {
2202 name: "myapex2",
2203 key: "myapex.key",
2204 updatable: false,
2205 native_shared_libs: ["yourlib"],
2206 }
2207
2208 apex_key {
2209 name: "myapex.key",
2210 public_key: "testkey.avbpubkey",
2211 private_key: "testkey.pem",
2212 }
2213
2214 cc_library {
2215 name: "mylib",
2216 srcs: ["mylib.cpp"],
2217 shared_libs: ["libbar"],
2218 min_sdk_version: "29",
2219 apex_available: ["myapex"],
2220 }
2221
2222 cc_library {
2223 name: "libbar",
2224 stubs: { versions: ["29", "30"] },
2225 }
2226
2227 cc_library {
2228 name: "yourlib",
2229 srcs: ["mylib.cpp"],
2230 min_sdk_version: "29",
2231 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2232 }
2233 `, withFiles(android.MockFS{
2234 "packages/modules/common/build/custom_allowed_deps.txt": nil,
2235 }),
2236 android.FixtureModifyProductVariables(
2237 func(variables android.FixtureProductVariables) {
2238 variables.ExtraAllowedDepsTxt = proptools.StringPtr("packages/modules/common/build/custom_allowed_deps.txt")
2239 },
2240 ))
2241
2242 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2243 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2244 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
2245 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
2246 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
2247 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
2248
2249 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
2250 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2251 myapex.Output("depsinfo/flatlist.txt")), "\n")
2252 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2253 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2254 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2255 flatlist, "mylib:(minSdkVersion:29)")
2256 android.AssertStringListContains(t, "track platform-available lib",
2257 flatlist, "yourlib(minSdkVersion:29)")
2258}
2259
2260func TestTrackCustomAllowedDepsWithDefaultTxt(t *testing.T) {
2261 ctx := testApex(t, `
2262 apex {
2263 name: "myapex",
2264 key: "myapex.key",
2265 updatable: true,
2266 native_shared_libs: [
2267 "mylib",
2268 "yourlib",
2269 ],
2270 min_sdk_version: "29",
2271 }
2272
2273 apex {
2274 name: "myapex2",
2275 key: "myapex.key",
2276 updatable: false,
2277 native_shared_libs: ["yourlib"],
2278 }
2279
2280 apex_key {
2281 name: "myapex.key",
2282 public_key: "testkey.avbpubkey",
2283 private_key: "testkey.pem",
2284 }
2285
2286 cc_library {
2287 name: "mylib",
2288 srcs: ["mylib.cpp"],
2289 shared_libs: ["libbar"],
2290 min_sdk_version: "29",
2291 apex_available: ["myapex"],
2292 }
2293
2294 cc_library {
2295 name: "libbar",
2296 stubs: { versions: ["29", "30"] },
2297 }
2298
2299 cc_library {
2300 name: "yourlib",
2301 srcs: ["mylib.cpp"],
2302 min_sdk_version: "29",
2303 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2304 }
2305 `, withFiles(android.MockFS{
2306 "packages/modules/common/build/custom_allowed_deps.txt": nil,
2307 "packages/modules/common/build/allowed_deps.txt": nil,
2308 }),
2309 android.FixtureModifyProductVariables(
2310 func(variables android.FixtureProductVariables) {
2311 variables.ExtraAllowedDepsTxt = proptools.StringPtr("packages/modules/common/build/custom_allowed_deps.txt")
2312 },
2313 ))
2314
2315 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2316 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2317 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
2318 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
2319 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
2320 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
2321
2322 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
2323 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2324 myapex.Output("depsinfo/flatlist.txt")), "\n")
2325 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2326 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2327 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2328 flatlist, "mylib:(minSdkVersion:29)")
2329 android.AssertStringListContains(t, "track platform-available lib",
2330 flatlist, "yourlib(minSdkVersion:29)")
2331}
2332
Jooyung Han4495f842023-04-25 16:39:59 +09002333func TestTrackAllowedDeps_SkipWithoutAllowedDepsTxt(t *testing.T) {
2334 ctx := testApex(t, `
2335 apex {
2336 name: "myapex",
2337 key: "myapex.key",
2338 updatable: true,
2339 min_sdk_version: "29",
2340 }
2341
2342 apex_key {
2343 name: "myapex.key",
2344 public_key: "testkey.avbpubkey",
2345 private_key: "testkey.pem",
2346 }
2347 `)
2348 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2349 if nil != depsinfo.MaybeRule("generateApexDepsInfoFilesRule").Output {
2350 t.Error("apex_depsinfo_singleton shouldn't run when allowed_deps.txt doesn't exist")
2351 }
2352}
2353
Jooyung Han03b51852020-02-26 22:45:42 +09002354func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002355 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002356 apex {
2357 name: "myapex",
2358 key: "myapex.key",
2359 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002360 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002361 }
2362
2363 apex_key {
2364 name: "myapex.key",
2365 public_key: "testkey.avbpubkey",
2366 private_key: "testkey.pem",
2367 }
2368
2369 cc_library {
2370 name: "libx",
2371 system_shared_libs: [],
2372 stl: "none",
2373 apex_available: [ "myapex" ],
2374 stubs: {
2375 versions: ["1", "2"],
2376 },
2377 }
2378
2379 cc_library {
2380 name: "libz",
2381 shared_libs: ["libx"],
2382 system_shared_libs: [],
2383 stl: "none",
2384 }
2385 `)
2386
2387 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002388 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002389 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2390 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2391 }
2392 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002393 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002394 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2395 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2396 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002397 expectLink("libz", "shared", "libx", "shared_current")
2398 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002399 expectNoLink("libz", "shared", "libz", "shared_1")
2400 expectNoLink("libz", "shared", "libz", "shared")
2401}
2402
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002403var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2404 func(variables android.FixtureProductVariables) {
2405 variables.SanitizeDevice = []string{"hwaddress"}
2406 },
2407)
2408
Jooyung Han75568392020-03-20 04:29:24 +09002409func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002410 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002411 apex {
2412 name: "myapex",
2413 key: "myapex.key",
2414 native_shared_libs: ["libx"],
2415 min_sdk_version: "29",
2416 }
2417
2418 apex_key {
2419 name: "myapex.key",
2420 public_key: "testkey.avbpubkey",
2421 private_key: "testkey.pem",
2422 }
2423
2424 cc_library {
2425 name: "libx",
2426 shared_libs: ["libbar"],
2427 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002428 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002429 }
2430
2431 cc_library {
2432 name: "libbar",
2433 stubs: {
2434 versions: ["29", "30"],
2435 },
2436 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002437 `,
2438 prepareForTestWithSantitizeHwaddress,
2439 )
Jooyung Han03b51852020-02-26 22:45:42 +09002440 expectLink := func(from, from_variant, to, to_variant string) {
2441 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2442 libFlags := ld.Args["libFlags"]
2443 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2444 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002445 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002446}
2447
Jooyung Han75568392020-03-20 04:29:24 +09002448func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002449 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002450 apex {
2451 name: "myapex",
2452 key: "myapex.key",
2453 native_shared_libs: ["libx"],
2454 min_sdk_version: "29",
2455 }
2456
2457 apex_key {
2458 name: "myapex.key",
2459 public_key: "testkey.avbpubkey",
2460 private_key: "testkey.pem",
2461 }
2462
2463 cc_library {
2464 name: "libx",
2465 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002466 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002467 }
Jooyung Han75568392020-03-20 04:29:24 +09002468 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002469
2470 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002471 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002472 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002473 // note that platform variant is not.
2474 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002475 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002476}
2477
Jooyung Han749dc692020-04-15 11:03:39 +09002478func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2479 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002480 apex {
2481 name: "myapex",
2482 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002483 native_shared_libs: ["mylib"],
2484 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002485 }
2486
2487 apex_key {
2488 name: "myapex.key",
2489 public_key: "testkey.avbpubkey",
2490 private_key: "testkey.pem",
2491 }
Jooyung Han749dc692020-04-15 11:03:39 +09002492
2493 cc_library {
2494 name: "mylib",
2495 srcs: ["mylib.cpp"],
2496 system_shared_libs: [],
2497 stl: "none",
2498 apex_available: [
2499 "myapex",
2500 ],
2501 min_sdk_version: "30",
2502 }
2503 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002504
2505 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2506 apex {
2507 name: "myapex",
2508 key: "myapex.key",
2509 native_shared_libs: ["libfoo.ffi"],
2510 min_sdk_version: "29",
2511 }
2512
2513 apex_key {
2514 name: "myapex.key",
2515 public_key: "testkey.avbpubkey",
2516 private_key: "testkey.pem",
2517 }
2518
2519 rust_ffi_shared {
2520 name: "libfoo.ffi",
2521 srcs: ["foo.rs"],
2522 crate_name: "foo",
2523 apex_available: [
2524 "myapex",
2525 ],
2526 min_sdk_version: "30",
2527 }
2528 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002529
2530 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2531 apex {
2532 name: "myapex",
2533 key: "myapex.key",
2534 java_libs: ["libfoo"],
2535 min_sdk_version: "29",
2536 }
2537
2538 apex_key {
2539 name: "myapex.key",
2540 public_key: "testkey.avbpubkey",
2541 private_key: "testkey.pem",
2542 }
2543
2544 java_import {
2545 name: "libfoo",
2546 jars: ["libfoo.jar"],
2547 apex_available: [
2548 "myapex",
2549 ],
2550 min_sdk_version: "30",
2551 }
2552 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002553
2554 // Skip check for modules compiling against core API surface
2555 testApex(t, `
2556 apex {
2557 name: "myapex",
2558 key: "myapex.key",
2559 java_libs: ["libfoo"],
2560 min_sdk_version: "29",
2561 }
2562
2563 apex_key {
2564 name: "myapex.key",
2565 public_key: "testkey.avbpubkey",
2566 private_key: "testkey.pem",
2567 }
2568
2569 java_library {
2570 name: "libfoo",
2571 srcs: ["Foo.java"],
2572 apex_available: [
2573 "myapex",
2574 ],
2575 // Compile against core API surface
2576 sdk_version: "core_current",
2577 min_sdk_version: "30",
2578 }
2579 `)
2580
Jooyung Han749dc692020-04-15 11:03:39 +09002581}
2582
2583func TestApexMinSdkVersion_Okay(t *testing.T) {
2584 testApex(t, `
2585 apex {
2586 name: "myapex",
2587 key: "myapex.key",
2588 native_shared_libs: ["libfoo"],
2589 java_libs: ["libbar"],
2590 min_sdk_version: "29",
2591 }
2592
2593 apex_key {
2594 name: "myapex.key",
2595 public_key: "testkey.avbpubkey",
2596 private_key: "testkey.pem",
2597 }
2598
2599 cc_library {
2600 name: "libfoo",
2601 srcs: ["mylib.cpp"],
2602 shared_libs: ["libfoo_dep"],
2603 apex_available: ["myapex"],
2604 min_sdk_version: "29",
2605 }
2606
2607 cc_library {
2608 name: "libfoo_dep",
2609 srcs: ["mylib.cpp"],
2610 apex_available: ["myapex"],
2611 min_sdk_version: "29",
2612 }
2613
2614 java_library {
2615 name: "libbar",
2616 sdk_version: "current",
2617 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002618 static_libs: [
2619 "libbar_dep",
2620 "libbar_import_dep",
2621 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002622 apex_available: ["myapex"],
2623 min_sdk_version: "29",
2624 }
2625
2626 java_library {
2627 name: "libbar_dep",
2628 sdk_version: "current",
2629 srcs: ["a.java"],
2630 apex_available: ["myapex"],
2631 min_sdk_version: "29",
2632 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002633
2634 java_import {
2635 name: "libbar_import_dep",
2636 jars: ["libbar.jar"],
2637 apex_available: ["myapex"],
2638 min_sdk_version: "29",
2639 }
Jooyung Han03b51852020-02-26 22:45:42 +09002640 `)
2641}
2642
Colin Cross8ca61c12022-10-06 21:00:14 -07002643func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2644 // Tests that an apex dependency with min_sdk_version higher than the
2645 // min_sdk_version of the apex is allowed as long as the dependency's
2646 // min_sdk_version is less than or equal to the api level that the
2647 // architecture was introduced in. In this case, arm64 didn't exist
2648 // until api level 21, so the arm64 code will never need to run on
2649 // an api level 20 device, even if other architectures of the apex
2650 // will.
2651 testApex(t, `
2652 apex {
2653 name: "myapex",
2654 key: "myapex.key",
2655 native_shared_libs: ["libfoo"],
2656 min_sdk_version: "20",
2657 }
2658
2659 apex_key {
2660 name: "myapex.key",
2661 public_key: "testkey.avbpubkey",
2662 private_key: "testkey.pem",
2663 }
2664
2665 cc_library {
2666 name: "libfoo",
2667 srcs: ["mylib.cpp"],
2668 apex_available: ["myapex"],
2669 min_sdk_version: "21",
2670 stl: "none",
2671 }
2672 `)
2673}
2674
Artur Satayev8cf899a2020-04-15 17:29:42 +01002675func TestJavaStableSdkVersion(t *testing.T) {
2676 testCases := []struct {
2677 name string
2678 expectedError string
2679 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002680 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002681 }{
2682 {
2683 name: "Non-updatable apex with non-stable dep",
2684 bp: `
2685 apex {
2686 name: "myapex",
2687 java_libs: ["myjar"],
2688 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002689 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002690 }
2691 apex_key {
2692 name: "myapex.key",
2693 public_key: "testkey.avbpubkey",
2694 private_key: "testkey.pem",
2695 }
2696 java_library {
2697 name: "myjar",
2698 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002699 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002700 apex_available: ["myapex"],
2701 }
2702 `,
2703 },
2704 {
2705 name: "Updatable apex with stable dep",
2706 bp: `
2707 apex {
2708 name: "myapex",
2709 java_libs: ["myjar"],
2710 key: "myapex.key",
2711 updatable: true,
2712 min_sdk_version: "29",
2713 }
2714 apex_key {
2715 name: "myapex.key",
2716 public_key: "testkey.avbpubkey",
2717 private_key: "testkey.pem",
2718 }
2719 java_library {
2720 name: "myjar",
2721 srcs: ["foo/bar/MyClass.java"],
2722 sdk_version: "current",
2723 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002724 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002725 }
2726 `,
2727 },
2728 {
2729 name: "Updatable apex with non-stable dep",
2730 expectedError: "cannot depend on \"myjar\"",
2731 bp: `
2732 apex {
2733 name: "myapex",
2734 java_libs: ["myjar"],
2735 key: "myapex.key",
2736 updatable: true,
2737 }
2738 apex_key {
2739 name: "myapex.key",
2740 public_key: "testkey.avbpubkey",
2741 private_key: "testkey.pem",
2742 }
2743 java_library {
2744 name: "myjar",
2745 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002746 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002747 apex_available: ["myapex"],
2748 }
2749 `,
2750 },
2751 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002752 name: "Updatable apex with non-stable legacy core platform dep",
2753 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2754 bp: `
2755 apex {
2756 name: "myapex",
2757 java_libs: ["myjar-uses-legacy"],
2758 key: "myapex.key",
2759 updatable: true,
2760 }
2761 apex_key {
2762 name: "myapex.key",
2763 public_key: "testkey.avbpubkey",
2764 private_key: "testkey.pem",
2765 }
2766 java_library {
2767 name: "myjar-uses-legacy",
2768 srcs: ["foo/bar/MyClass.java"],
2769 sdk_version: "core_platform",
2770 apex_available: ["myapex"],
2771 }
2772 `,
2773 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2774 },
2775 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002776 name: "Updatable apex with non-stable transitive dep",
2777 // This is not actually detecting that the transitive dependency is unstable, rather it is
2778 // detecting that the transitive dependency is building against a wider API surface than the
2779 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002780 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002781 bp: `
2782 apex {
2783 name: "myapex",
2784 java_libs: ["myjar"],
2785 key: "myapex.key",
2786 updatable: true,
2787 }
2788 apex_key {
2789 name: "myapex.key",
2790 public_key: "testkey.avbpubkey",
2791 private_key: "testkey.pem",
2792 }
2793 java_library {
2794 name: "myjar",
2795 srcs: ["foo/bar/MyClass.java"],
2796 sdk_version: "current",
2797 apex_available: ["myapex"],
2798 static_libs: ["transitive-jar"],
2799 }
2800 java_library {
2801 name: "transitive-jar",
2802 srcs: ["foo/bar/MyClass.java"],
2803 sdk_version: "core_platform",
2804 apex_available: ["myapex"],
2805 }
2806 `,
2807 },
2808 }
2809
2810 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002811 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2812 continue
2813 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002814 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002815 errorHandler := android.FixtureExpectsNoErrors
2816 if test.expectedError != "" {
2817 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002818 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002819 android.GroupFixturePreparers(
2820 java.PrepareForTestWithJavaDefaultModules,
2821 PrepareForTestWithApexBuildComponents,
2822 prepareForTestWithMyapex,
2823 android.OptionalFixturePreparer(test.preparer),
2824 ).
2825 ExtendWithErrorHandler(errorHandler).
2826 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002827 })
2828 }
2829}
2830
Jooyung Han749dc692020-04-15 11:03:39 +09002831func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2832 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2833 apex {
2834 name: "myapex",
2835 key: "myapex.key",
2836 native_shared_libs: ["mylib"],
2837 min_sdk_version: "29",
2838 }
2839
2840 apex_key {
2841 name: "myapex.key",
2842 public_key: "testkey.avbpubkey",
2843 private_key: "testkey.pem",
2844 }
2845
2846 cc_library {
2847 name: "mylib",
2848 srcs: ["mylib.cpp"],
2849 shared_libs: ["mylib2"],
2850 system_shared_libs: [],
2851 stl: "none",
2852 apex_available: [
2853 "myapex",
2854 ],
2855 min_sdk_version: "29",
2856 }
2857
2858 // indirect part of the apex
2859 cc_library {
2860 name: "mylib2",
2861 srcs: ["mylib.cpp"],
2862 system_shared_libs: [],
2863 stl: "none",
2864 apex_available: [
2865 "myapex",
2866 ],
2867 min_sdk_version: "30",
2868 }
2869 `)
2870}
2871
2872func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2873 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2874 apex {
2875 name: "myapex",
2876 key: "myapex.key",
2877 apps: ["AppFoo"],
2878 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002879 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002880 }
2881
2882 apex_key {
2883 name: "myapex.key",
2884 public_key: "testkey.avbpubkey",
2885 private_key: "testkey.pem",
2886 }
2887
2888 android_app {
2889 name: "AppFoo",
2890 srcs: ["foo/bar/MyClass.java"],
2891 sdk_version: "current",
2892 min_sdk_version: "29",
2893 system_modules: "none",
2894 stl: "none",
2895 static_libs: ["bar"],
2896 apex_available: [ "myapex" ],
2897 }
2898
2899 java_library {
2900 name: "bar",
2901 sdk_version: "current",
2902 srcs: ["a.java"],
2903 apex_available: [ "myapex" ],
2904 }
2905 `)
2906}
2907
2908func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002909 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002910 apex {
2911 name: "myapex",
2912 key: "myapex.key",
2913 native_shared_libs: ["mylib"],
2914 min_sdk_version: "29",
2915 }
2916
2917 apex_key {
2918 name: "myapex.key",
2919 public_key: "testkey.avbpubkey",
2920 private_key: "testkey.pem",
2921 }
2922
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002923 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002924 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2925 cc_library {
2926 name: "mylib",
2927 srcs: ["mylib.cpp"],
2928 shared_libs: ["mylib2"],
2929 system_shared_libs: [],
2930 stl: "none",
2931 apex_available: ["myapex", "otherapex"],
2932 min_sdk_version: "29",
2933 }
2934
2935 cc_library {
2936 name: "mylib2",
2937 srcs: ["mylib.cpp"],
2938 system_shared_libs: [],
2939 stl: "none",
2940 apex_available: ["otherapex"],
2941 stubs: { versions: ["29", "30"] },
2942 min_sdk_version: "30",
2943 }
2944
2945 apex {
2946 name: "otherapex",
2947 key: "myapex.key",
2948 native_shared_libs: ["mylib", "mylib2"],
2949 min_sdk_version: "30",
2950 }
2951 `)
2952 expectLink := func(from, from_variant, to, to_variant string) {
2953 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2954 libFlags := ld.Args["libFlags"]
2955 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2956 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002957 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002958 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002959}
2960
Jooyung Haned124c32021-01-26 11:43:46 +09002961func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002962 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2963 func(variables android.FixtureProductVariables) {
2964 variables.Platform_sdk_codename = proptools.StringPtr("S")
2965 variables.Platform_version_active_codenames = []string{"S"}
2966 },
2967 )
Jooyung Haned124c32021-01-26 11:43:46 +09002968 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2969 apex {
2970 name: "myapex",
2971 key: "myapex.key",
2972 native_shared_libs: ["libfoo"],
2973 min_sdk_version: "S",
2974 }
2975 apex_key {
2976 name: "myapex.key",
2977 public_key: "testkey.avbpubkey",
2978 private_key: "testkey.pem",
2979 }
2980 cc_library {
2981 name: "libfoo",
2982 shared_libs: ["libbar"],
2983 apex_available: ["myapex"],
2984 min_sdk_version: "29",
2985 }
2986 cc_library {
2987 name: "libbar",
2988 apex_available: ["myapex"],
2989 }
2990 `, withSAsActiveCodeNames)
2991}
2992
2993func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002994 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2995 variables.Platform_sdk_codename = proptools.StringPtr("S")
2996 variables.Platform_version_active_codenames = []string{"S", "T"}
2997 })
Colin Cross1c460562021-02-16 17:55:47 -08002998 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002999 apex {
3000 name: "myapex",
3001 key: "myapex.key",
3002 native_shared_libs: ["libfoo"],
3003 min_sdk_version: "S",
3004 }
3005 apex_key {
3006 name: "myapex.key",
3007 public_key: "testkey.avbpubkey",
3008 private_key: "testkey.pem",
3009 }
3010 cc_library {
3011 name: "libfoo",
3012 shared_libs: ["libbar"],
3013 apex_available: ["myapex"],
3014 min_sdk_version: "S",
3015 }
3016 cc_library {
3017 name: "libbar",
3018 stubs: {
3019 symbol_file: "libbar.map.txt",
3020 versions: ["30", "S", "T"],
3021 },
3022 }
3023 `, withSAsActiveCodeNames)
3024
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003025 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09003026 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
3027 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003028 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09003029}
3030
Jiyong Park7c2ee712018-12-07 00:42:25 +09003031func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003032 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09003033 apex {
3034 name: "myapex",
3035 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003036 native_shared_libs: ["mylib"],
Jooyung Han4ed512b2023-08-11 16:30:04 +09003037 binaries: ["mybin", "mybin.rust"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09003038 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003039 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003040 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09003041 }
3042
3043 apex_key {
3044 name: "myapex.key",
3045 public_key: "testkey.avbpubkey",
3046 private_key: "testkey.pem",
3047 }
3048
3049 prebuilt_etc {
3050 name: "myetc",
3051 src: "myprebuilt",
3052 sub_dir: "foo/bar",
3053 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003054
3055 cc_library {
3056 name: "mylib",
3057 srcs: ["mylib.cpp"],
3058 relative_install_path: "foo/bar",
3059 system_shared_libs: [],
3060 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003061 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003062 }
3063
3064 cc_binary {
3065 name: "mybin",
3066 srcs: ["mylib.cpp"],
3067 relative_install_path: "foo/bar",
3068 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003069 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003070 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003071 }
Jooyung Han4ed512b2023-08-11 16:30:04 +09003072
3073 rust_binary {
3074 name: "mybin.rust",
3075 srcs: ["foo.rs"],
3076 relative_install_path: "rust_subdir",
3077 apex_available: [ "myapex" ],
3078 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09003079 `)
3080
Jooyung Hana0503a52023-08-23 13:12:50 +09003081 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09003082 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09003083
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003084 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09003085 ensureContains(t, cmd, "/etc ")
3086 ensureContains(t, cmd, "/etc/foo ")
3087 ensureContains(t, cmd, "/etc/foo/bar ")
3088 ensureContains(t, cmd, "/lib64 ")
3089 ensureContains(t, cmd, "/lib64/foo ")
3090 ensureContains(t, cmd, "/lib64/foo/bar ")
3091 ensureContains(t, cmd, "/lib ")
3092 ensureContains(t, cmd, "/lib/foo ")
3093 ensureContains(t, cmd, "/lib/foo/bar ")
3094 ensureContains(t, cmd, "/bin ")
3095 ensureContains(t, cmd, "/bin/foo ")
3096 ensureContains(t, cmd, "/bin/foo/bar ")
Jooyung Han4ed512b2023-08-11 16:30:04 +09003097 ensureContains(t, cmd, "/bin/rust_subdir ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09003098}
Jiyong Parkda6eb592018-12-19 17:12:36 +09003099
Jooyung Han35155c42020-02-06 17:33:20 +09003100func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003101 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09003102 apex {
3103 name: "myapex",
3104 key: "myapex.key",
3105 multilib: {
3106 both: {
3107 native_shared_libs: ["mylib"],
3108 binaries: ["mybin"],
3109 },
3110 },
3111 compile_multilib: "both",
3112 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003113 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09003114 }
3115
3116 apex_key {
3117 name: "myapex.key",
3118 public_key: "testkey.avbpubkey",
3119 private_key: "testkey.pem",
3120 }
3121
3122 cc_library {
3123 name: "mylib",
3124 relative_install_path: "foo/bar",
3125 system_shared_libs: [],
3126 stl: "none",
3127 apex_available: [ "myapex" ],
3128 native_bridge_supported: true,
3129 }
3130
3131 cc_binary {
3132 name: "mybin",
3133 relative_install_path: "foo/bar",
3134 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09003135 stl: "none",
3136 apex_available: [ "myapex" ],
3137 native_bridge_supported: true,
3138 compile_multilib: "both", // default is "first" for binary
3139 multilib: {
3140 lib64: {
3141 suffix: "64",
3142 },
3143 },
3144 }
3145 `, withNativeBridgeEnabled)
Jooyung Hana0503a52023-08-23 13:12:50 +09003146 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han35155c42020-02-06 17:33:20 +09003147 "bin/foo/bar/mybin",
3148 "bin/foo/bar/mybin64",
3149 "bin/arm/foo/bar/mybin",
3150 "bin/arm64/foo/bar/mybin64",
3151 "lib/foo/bar/mylib.so",
3152 "lib/arm/foo/bar/mylib.so",
3153 "lib64/foo/bar/mylib.so",
3154 "lib64/arm64/foo/bar/mylib.so",
3155 })
3156}
3157
Jooyung Han85d61762020-06-24 23:50:26 +09003158func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08003159 result := android.GroupFixturePreparers(
3160 prepareForApexTest,
3161 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
3162 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09003163 apex {
3164 name: "myapex",
3165 key: "myapex.key",
3166 binaries: ["mybin"],
3167 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003168 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09003169 }
3170 apex_key {
3171 name: "myapex.key",
3172 public_key: "testkey.avbpubkey",
3173 private_key: "testkey.pem",
3174 }
3175 cc_binary {
3176 name: "mybin",
3177 vendor: true,
3178 shared_libs: ["libfoo"],
3179 }
3180 cc_library {
3181 name: "libfoo",
3182 proprietary: true,
3183 }
3184 `)
3185
Jooyung Hana0503a52023-08-23 13:12:50 +09003186 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09003187 "bin/mybin",
3188 "lib64/libfoo.so",
3189 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
3190 "lib64/libc++.so",
3191 })
3192
Jooyung Hana0503a52023-08-23 13:12:50 +09003193 apexBundle := result.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossc68db4b2021-11-11 18:59:15 -08003194 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09003195 name := apexBundle.BaseModuleName()
3196 prefix := "TARGET_"
3197 var builder strings.Builder
3198 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08003199 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01003200 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01003201 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003202
Jooyung Hana0503a52023-08-23 13:12:50 +09003203 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003204 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
3205 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09003206}
3207
Justin Yun13decfb2021-03-08 19:25:55 +09003208func TestProductVariant(t *testing.T) {
3209 ctx := testApex(t, `
3210 apex {
3211 name: "myapex",
3212 key: "myapex.key",
3213 updatable: false,
3214 product_specific: true,
3215 binaries: ["foo"],
3216 }
3217
3218 apex_key {
3219 name: "myapex.key",
3220 public_key: "testkey.avbpubkey",
3221 private_key: "testkey.pem",
3222 }
3223
3224 cc_binary {
3225 name: "foo",
3226 product_available: true,
3227 apex_available: ["myapex"],
3228 srcs: ["foo.cpp"],
3229 }
Justin Yunaf1fde42023-09-27 16:22:10 +09003230 `)
Justin Yun13decfb2021-03-08 19:25:55 +09003231
3232 cflags := strings.Fields(
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003233 ctx.ModuleForTests("foo", "android_product_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09003234 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
3235 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
3236 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
3237 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
3238}
3239
Jooyung Han8e5685d2020-09-21 11:02:57 +09003240func TestApex_withPrebuiltFirmware(t *testing.T) {
3241 testCases := []struct {
3242 name string
3243 additionalProp string
3244 }{
3245 {"system apex with prebuilt_firmware", ""},
3246 {"vendor apex with prebuilt_firmware", "vendor: true,"},
3247 }
3248 for _, tc := range testCases {
3249 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003250 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09003251 apex {
3252 name: "myapex",
3253 key: "myapex.key",
3254 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003255 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09003256 `+tc.additionalProp+`
3257 }
3258 apex_key {
3259 name: "myapex.key",
3260 public_key: "testkey.avbpubkey",
3261 private_key: "testkey.pem",
3262 }
3263 prebuilt_firmware {
3264 name: "myfirmware",
3265 src: "myfirmware.bin",
3266 filename_from_src: true,
3267 `+tc.additionalProp+`
3268 }
3269 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09003270 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han8e5685d2020-09-21 11:02:57 +09003271 "etc/firmware/myfirmware.bin",
3272 })
3273 })
3274 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003275}
3276
Jooyung Hanefb184e2020-06-25 17:14:25 +09003277func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003278 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003279 apex {
3280 name: "myapex",
3281 key: "myapex.key",
3282 vendor: true,
3283 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003284 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003285 }
3286
3287 apex_key {
3288 name: "myapex.key",
3289 public_key: "testkey.avbpubkey",
3290 private_key: "testkey.pem",
3291 }
3292
3293 cc_library {
3294 name: "mylib",
3295 vendor_available: true,
3296 }
3297 `)
3298
Jooyung Hana0503a52023-08-23 13:12:50 +09003299 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003300 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003301 name := apexBundle.BaseModuleName()
3302 prefix := "TARGET_"
3303 var builder strings.Builder
3304 data.Custom(&builder, name, prefix, "", data)
3305 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09003306 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 +09003307}
3308
Jooyung Han2ed99d02020-06-24 23:26:26 +09003309func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003310 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003311 apex {
3312 name: "myapex",
3313 key: "myapex.key",
3314 vintf_fragments: ["fragment.xml"],
3315 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003316 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003317 }
3318 apex_key {
3319 name: "myapex.key",
3320 public_key: "testkey.avbpubkey",
3321 private_key: "testkey.pem",
3322 }
3323 cc_binary {
3324 name: "mybin",
3325 }
3326 `)
3327
Jooyung Hana0503a52023-08-23 13:12:50 +09003328 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003329 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003330 name := apexBundle.BaseModuleName()
3331 prefix := "TARGET_"
3332 var builder strings.Builder
3333 data.Custom(&builder, name, prefix, "", data)
3334 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003335 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003336 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003337}
3338
Jiyong Park16e91a02018-12-20 18:18:08 +09003339func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003340 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003341 apex {
3342 name: "myapex",
3343 key: "myapex.key",
3344 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003345 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003346 }
3347
3348 apex_key {
3349 name: "myapex.key",
3350 public_key: "testkey.avbpubkey",
3351 private_key: "testkey.pem",
3352 }
3353
3354 cc_library {
3355 name: "mylib",
3356 srcs: ["mylib.cpp"],
3357 system_shared_libs: [],
3358 stl: "none",
3359 stubs: {
3360 versions: ["1", "2", "3"],
3361 },
Spandan Das20fce2d2023-04-12 17:21:39 +00003362 apex_available: ["myapex"],
Jiyong Park16e91a02018-12-20 18:18:08 +09003363 }
3364
3365 cc_binary {
3366 name: "not_in_apex",
3367 srcs: ["mylib.cpp"],
3368 static_libs: ["mylib"],
3369 static_executable: true,
3370 system_shared_libs: [],
3371 stl: "none",
3372 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003373 `)
3374
Colin Cross7113d202019-11-20 16:39:12 -08003375 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003376
3377 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003378 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003379}
Jiyong Park9335a262018-12-24 11:31:58 +09003380
3381func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003382 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003383 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003384 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003385 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003386 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003387 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003388 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003389 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003390 }
3391
3392 cc_library {
3393 name: "mylib",
3394 srcs: ["mylib.cpp"],
3395 system_shared_libs: [],
3396 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003397 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003398 }
3399
3400 apex_key {
3401 name: "myapex.key",
3402 public_key: "testkey.avbpubkey",
3403 private_key: "testkey.pem",
3404 }
3405
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003406 android_app_certificate {
3407 name: "myapex.certificate",
3408 certificate: "testkey",
3409 }
3410
3411 android_app_certificate {
3412 name: "myapex.certificate.override",
3413 certificate: "testkey.override",
3414 }
3415
Jiyong Park9335a262018-12-24 11:31:58 +09003416 `)
3417
3418 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003419 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003420
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003421 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3422 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003423 "vendor/foo/devkeys/testkey.avbpubkey")
3424 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003425 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3426 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003427 "vendor/foo/devkeys/testkey.pem")
3428 }
3429
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003430 // check the APK certs. It should be overridden to myapex.certificate.override
Jooyung Hana0503a52023-08-23 13:12:50 +09003431 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003432 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003433 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003434 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003435 }
3436}
Jiyong Park58e364a2019-01-19 19:24:06 +09003437
Jooyung Hanf121a652019-12-17 14:30:11 +09003438func TestCertificate(t *testing.T) {
3439 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003440 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003441 apex {
3442 name: "myapex",
3443 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003444 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003445 }
3446 apex_key {
3447 name: "myapex.key",
3448 public_key: "testkey.avbpubkey",
3449 private_key: "testkey.pem",
3450 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003451 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003452 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3453 if actual := rule.Args["certificates"]; actual != expected {
3454 t.Errorf("certificates should be %q, not %q", expected, actual)
3455 }
3456 })
3457 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003458 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003459 apex {
3460 name: "myapex_keytest",
3461 key: "myapex.key",
3462 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003463 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003464 }
3465 apex_key {
3466 name: "myapex.key",
3467 public_key: "testkey.avbpubkey",
3468 private_key: "testkey.pem",
3469 }
3470 android_app_certificate {
3471 name: "myapex.certificate.override",
3472 certificate: "testkey.override",
3473 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003474 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003475 expected := "testkey.override.x509.pem testkey.override.pk8"
3476 if actual := rule.Args["certificates"]; actual != expected {
3477 t.Errorf("certificates should be %q, not %q", expected, actual)
3478 }
3479 })
3480 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003481 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003482 apex {
3483 name: "myapex",
3484 key: "myapex.key",
3485 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003486 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003487 }
3488 apex_key {
3489 name: "myapex.key",
3490 public_key: "testkey.avbpubkey",
3491 private_key: "testkey.pem",
3492 }
3493 android_app_certificate {
3494 name: "myapex.certificate",
3495 certificate: "testkey",
3496 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003497 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003498 expected := "testkey.x509.pem testkey.pk8"
3499 if actual := rule.Args["certificates"]; actual != expected {
3500 t.Errorf("certificates should be %q, not %q", expected, actual)
3501 }
3502 })
3503 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003504 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003505 apex {
3506 name: "myapex_keytest",
3507 key: "myapex.key",
3508 file_contexts: ":myapex-file_contexts",
3509 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003510 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003511 }
3512 apex_key {
3513 name: "myapex.key",
3514 public_key: "testkey.avbpubkey",
3515 private_key: "testkey.pem",
3516 }
3517 android_app_certificate {
3518 name: "myapex.certificate.override",
3519 certificate: "testkey.override",
3520 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003521 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003522 expected := "testkey.override.x509.pem testkey.override.pk8"
3523 if actual := rule.Args["certificates"]; actual != expected {
3524 t.Errorf("certificates should be %q, not %q", expected, actual)
3525 }
3526 })
3527 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003528 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003529 apex {
3530 name: "myapex",
3531 key: "myapex.key",
3532 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003533 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003534 }
3535 apex_key {
3536 name: "myapex.key",
3537 public_key: "testkey.avbpubkey",
3538 private_key: "testkey.pem",
3539 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003540 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003541 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3542 if actual := rule.Args["certificates"]; actual != expected {
3543 t.Errorf("certificates should be %q, not %q", expected, actual)
3544 }
3545 })
3546 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003547 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003548 apex {
3549 name: "myapex_keytest",
3550 key: "myapex.key",
3551 file_contexts: ":myapex-file_contexts",
3552 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003553 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003554 }
3555 apex_key {
3556 name: "myapex.key",
3557 public_key: "testkey.avbpubkey",
3558 private_key: "testkey.pem",
3559 }
3560 android_app_certificate {
3561 name: "myapex.certificate.override",
3562 certificate: "testkey.override",
3563 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003564 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003565 expected := "testkey.override.x509.pem testkey.override.pk8"
3566 if actual := rule.Args["certificates"]; actual != expected {
3567 t.Errorf("certificates should be %q, not %q", expected, actual)
3568 }
3569 })
3570}
3571
Jiyong Park58e364a2019-01-19 19:24:06 +09003572func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003573 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003574 apex {
3575 name: "myapex",
3576 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003577 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003578 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003579 }
3580
3581 apex {
3582 name: "otherapex",
3583 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003584 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003585 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003586 }
3587
3588 apex_key {
3589 name: "myapex.key",
3590 public_key: "testkey.avbpubkey",
3591 private_key: "testkey.pem",
3592 }
3593
3594 cc_library {
3595 name: "mylib",
3596 srcs: ["mylib.cpp"],
3597 system_shared_libs: [],
3598 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003599 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003600 "myapex",
3601 "otherapex",
3602 ],
Jooyung Han24282772020-03-21 23:20:55 +09003603 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003604 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003605 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003606 cc_library {
3607 name: "mylib2",
3608 srcs: ["mylib.cpp"],
3609 system_shared_libs: [],
3610 stl: "none",
3611 apex_available: [
3612 "myapex",
3613 "otherapex",
3614 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003615 static_libs: ["mylib3"],
3616 recovery_available: true,
3617 min_sdk_version: "29",
3618 }
3619 cc_library {
3620 name: "mylib3",
3621 srcs: ["mylib.cpp"],
3622 system_shared_libs: [],
3623 stl: "none",
3624 apex_available: [
3625 "myapex",
3626 "otherapex",
3627 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003628 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003629 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003630 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003631 `)
3632
Jooyung Hanc87a0592020-03-02 17:44:33 +09003633 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003634 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003635 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003636
Vinh Tranf9754732023-01-19 22:41:46 -05003637 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003638 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003639 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003640
Vinh Tranf9754732023-01-19 22:41:46 -05003641 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003642 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003643 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003644
Colin Crossaede88c2020-08-11 12:17:01 -07003645 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3646 // each variant defines additional macros to distinguish which apex variant it is built for
3647
3648 // non-APEX variant does not have __ANDROID_APEX__ defined
3649 mylibCFlags = ctx.ModuleForTests("mylib3", "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("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3654 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003655
Jooyung Hanc87a0592020-03-02 17:44:33 +09003656 // non-APEX variant does not have __ANDROID_APEX__ defined
3657 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3658 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3659
Vinh Tranf9754732023-01-19 22:41:46 -05003660 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003661 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003662 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003663}
Jiyong Park7e636d02019-01-28 16:16:54 +09003664
3665func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003666 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003667 apex {
3668 name: "myapex",
3669 key: "myapex.key",
3670 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003671 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003672 }
3673
3674 apex_key {
3675 name: "myapex.key",
3676 public_key: "testkey.avbpubkey",
3677 private_key: "testkey.pem",
3678 }
3679
3680 cc_library_headers {
3681 name: "mylib_headers",
3682 export_include_dirs: ["my_include"],
3683 system_shared_libs: [],
3684 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003685 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003686 }
3687
3688 cc_library {
3689 name: "mylib",
3690 srcs: ["mylib.cpp"],
3691 system_shared_libs: [],
3692 stl: "none",
3693 header_libs: ["mylib_headers"],
3694 export_header_lib_headers: ["mylib_headers"],
3695 stubs: {
3696 versions: ["1", "2", "3"],
3697 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003698 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003699 }
3700
3701 cc_library {
3702 name: "otherlib",
3703 srcs: ["mylib.cpp"],
3704 system_shared_libs: [],
3705 stl: "none",
3706 shared_libs: ["mylib"],
3707 }
3708 `)
3709
Colin Cross7113d202019-11-20 16:39:12 -08003710 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003711
3712 // Ensure that the include path of the header lib is exported to 'otherlib'
3713 ensureContains(t, cFlags, "-Imy_include")
3714}
Alex Light9670d332019-01-29 18:07:33 -08003715
Jiyong Park7cd10e32020-01-14 09:22:18 +09003716type fileInApex struct {
3717 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003718 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003719 isLink bool
3720}
3721
Jooyung Han1724d582022-12-21 10:17:44 +09003722func (f fileInApex) String() string {
3723 return f.src + ":" + f.path
3724}
3725
3726func (f fileInApex) match(expectation string) bool {
3727 parts := strings.Split(expectation, ":")
3728 if len(parts) == 1 {
3729 match, _ := path.Match(parts[0], f.path)
3730 return match
3731 }
3732 if len(parts) == 2 {
3733 matchSrc, _ := path.Match(parts[0], f.src)
3734 matchDst, _ := path.Match(parts[1], f.path)
3735 return matchSrc && matchDst
3736 }
3737 panic("invalid expected file specification: " + expectation)
3738}
3739
Jooyung Hana57af4a2020-01-23 05:36:59 +00003740func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003741 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003742 module := ctx.ModuleForTests(moduleName, variant)
3743 apexRule := module.MaybeRule("apexRule")
3744 apexDir := "/image.apex/"
Jooyung Han31c470b2019-10-18 16:26:59 +09003745 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003746 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003747 for _, cmd := range strings.Split(copyCmds, "&&") {
3748 cmd = strings.TrimSpace(cmd)
3749 if cmd == "" {
3750 continue
3751 }
3752 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003753 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003754 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003755 switch terms[0] {
3756 case "mkdir":
3757 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003758 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003759 t.Fatal("copyCmds contains invalid cp command", cmd)
3760 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003761 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 = false
3764 case "ln":
3765 if len(terms) != 3 && len(terms) != 4 {
3766 // ln LINK TARGET or ln -s LINK TARGET
3767 t.Fatal("copyCmds contains invalid ln command", cmd)
3768 }
3769 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003770 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003771 isLink = true
3772 default:
3773 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3774 }
3775 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003776 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003777 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003778 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003779 }
Jooyung Han1724d582022-12-21 10:17:44 +09003780 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003781 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003782 }
3783 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003784 return ret
3785}
3786
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003787func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003788 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003789 var failed bool
3790 var surplus []string
3791 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003792 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003793 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003794 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003795 if file.match(expected) {
3796 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003797 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003798 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003799 }
3800 }
Jooyung Han1724d582022-12-21 10:17:44 +09003801 if !matchFound {
3802 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003803 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003804 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003805
Jooyung Han31c470b2019-10-18 16:26:59 +09003806 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003807 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003808 t.Log("surplus files", surplus)
3809 failed = true
3810 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003811
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003812 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003813 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003814 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003815 if !filesMatched[expected] {
3816 missing = append(missing, expected)
3817 }
3818 }
3819 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003820 t.Log("missing files", missing)
3821 failed = true
3822 }
3823 if failed {
3824 t.Fail()
3825 }
3826}
3827
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003828func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3829 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3830}
3831
3832func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
Spandan Das52c01a12024-09-20 01:09:48 +00003833 deapexer := ctx.ModuleForTests(moduleName, variant).Description("deapex")
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003834 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3835 if deapexer.Output != nil {
3836 outputs = append(outputs, deapexer.Output.String())
3837 }
3838 for _, output := range deapexer.ImplicitOutputs {
3839 outputs = append(outputs, output.String())
3840 }
3841 actualFiles := make([]fileInApex, 0, len(outputs))
3842 for _, output := range outputs {
3843 dir := "/deapexer/"
3844 pos := strings.LastIndex(output, dir)
3845 if pos == -1 {
3846 t.Fatal("Unknown deapexer output ", output)
3847 }
3848 path := output[pos+len(dir):]
3849 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3850 }
3851 assertFileListEquals(t, files, actualFiles)
3852}
3853
Jooyung Han39edb6c2019-11-06 16:53:07 +09003854func vndkLibrariesTxtFiles(vers ...string) (result string) {
3855 for _, v := range vers {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +09003856 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Justin Yund5784122023-10-25 13:25:32 +09003857 result += `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003858 prebuilt_etc {
3859 name: "` + txt + `.libraries.` + v + `.txt",
3860 src: "dummy.txt",
3861 }
3862 `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003863 }
3864 }
3865 return
3866}
3867
Jooyung Han344d5432019-08-23 11:17:39 +09003868func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003869 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003870 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003871 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003872 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003873 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003874 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003875 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003876 }
3877
3878 apex_key {
3879 name: "myapex.key",
3880 public_key: "testkey.avbpubkey",
3881 private_key: "testkey.pem",
3882 }
3883
Jooyung Han31c470b2019-10-18 16:26:59 +09003884 vndk_prebuilt_shared {
3885 name: "libvndk27",
3886 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003887 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003888 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003889 vndk: {
3890 enabled: true,
3891 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003892 target_arch: "arm64",
3893 arch: {
3894 arm: {
3895 srcs: ["libvndk27_arm.so"],
3896 },
3897 arm64: {
3898 srcs: ["libvndk27_arm64.so"],
3899 },
3900 },
Colin Cross2807f002021-03-02 10:15:29 -08003901 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003902 }
3903
3904 vndk_prebuilt_shared {
3905 name: "libvndk27",
3906 version: "27",
3907 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003908 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003909 vndk: {
3910 enabled: true,
3911 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003912 target_arch: "x86_64",
3913 arch: {
3914 x86: {
3915 srcs: ["libvndk27_x86.so"],
3916 },
3917 x86_64: {
3918 srcs: ["libvndk27_x86_64.so"],
3919 },
3920 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003921 }
3922 `+vndkLibrariesTxtFiles("27"),
3923 withFiles(map[string][]byte{
3924 "libvndk27_arm.so": nil,
3925 "libvndk27_arm64.so": nil,
3926 "libvndk27_x86.so": nil,
3927 "libvndk27_x86_64.so": nil,
3928 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003929
Jooyung Hana0503a52023-08-23 13:12:50 +09003930 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003931 "lib/libvndk27_arm.so",
3932 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003933 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003934 })
Jooyung Han344d5432019-08-23 11:17:39 +09003935}
3936
Jooyung Han90eee022019-10-01 20:02:42 +09003937func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003938 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003939 apex_vndk {
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003940 name: "com.android.vndk.v29",
Jooyung Han90eee022019-10-01 20:02:42 +09003941 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003942 file_contexts: ":myapex-file_contexts",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003943 vndk_version: "29",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003944 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003945 }
3946 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003947 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003948 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003949 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003950 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003951 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003952 }
3953 apex_key {
3954 name: "myapex.key",
3955 public_key: "testkey.avbpubkey",
3956 private_key: "testkey.pem",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003957 }`+vndkLibrariesTxtFiles("28", "29"))
Jooyung Han90eee022019-10-01 20:02:42 +09003958
3959 assertApexName := func(expected, moduleName string) {
Jooyung Hana0503a52023-08-23 13:12:50 +09003960 module := ctx.ModuleForTests(moduleName, "android_common")
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003961 apexManifestRule := module.Rule("apexManifestRule")
3962 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003963 }
3964
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003965 assertApexName("com.android.vndk.v29", "com.android.vndk.v29")
Colin Cross2807f002021-03-02 10:15:29 -08003966 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003967}
3968
Jooyung Han344d5432019-08-23 11:17:39 +09003969func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003970 testApexError(t, `module "com.android.vndk.v30" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003971 apex_vndk {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003972 name: "com.android.vndk.v30",
3973 key: "com.android.vndk.v30.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003974 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003975 native_bridge_supported: true,
3976 }
3977
3978 apex_key {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003979 name: "com.android.vndk.v30.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003980 public_key: "testkey.avbpubkey",
3981 private_key: "testkey.pem",
3982 }
3983
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003984 vndk_prebuilt_shared {
Jooyung Han344d5432019-08-23 11:17:39 +09003985 name: "libvndk",
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003986 version: "30",
3987 target_arch: "arm",
Jooyung Han344d5432019-08-23 11:17:39 +09003988 srcs: ["mylib.cpp"],
3989 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003990 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003991 native_bridge_supported: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003992 vndk: {
3993 enabled: true,
3994 },
Jooyung Han344d5432019-08-23 11:17:39 +09003995 }
3996 `)
3997}
3998
Jooyung Han31c470b2019-10-18 16:26:59 +09003999func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004000 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09004001 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004002 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09004003 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004004 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09004005 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004006 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09004007 }
4008
4009 apex_key {
4010 name: "myapex.key",
4011 public_key: "testkey.avbpubkey",
4012 private_key: "testkey.pem",
4013 }
4014
4015 vndk_prebuilt_shared {
4016 name: "libvndk27",
4017 version: "27",
4018 target_arch: "arm",
4019 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004020 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004021 vndk: {
4022 enabled: true,
4023 },
4024 arch: {
4025 arm: {
4026 srcs: ["libvndk27.so"],
4027 }
4028 },
4029 }
4030
4031 vndk_prebuilt_shared {
4032 name: "libvndk27",
4033 version: "27",
4034 target_arch: "arm",
4035 binder32bit: true,
4036 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004037 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004038 vndk: {
4039 enabled: true,
4040 },
4041 arch: {
4042 arm: {
4043 srcs: ["libvndk27binder32.so"],
4044 }
4045 },
Colin Cross2807f002021-03-02 10:15:29 -08004046 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09004047 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09004048 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09004049 withFiles(map[string][]byte{
4050 "libvndk27.so": nil,
4051 "libvndk27binder32.so": nil,
4052 }),
4053 withBinder32bit,
4054 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07004055 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09004056 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
4057 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09004058 },
4059 }),
4060 )
4061
Jooyung Hana0503a52023-08-23 13:12:50 +09004062 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09004063 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004064 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09004065 })
4066}
4067
Jooyung Hane1633032019-08-01 17:41:43 +09004068func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004069 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09004070 apex {
4071 name: "myapex_nodep",
4072 key: "myapex.key",
4073 native_shared_libs: ["lib_nodep"],
4074 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004075 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004076 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004077 }
4078
4079 apex {
4080 name: "myapex_dep",
4081 key: "myapex.key",
4082 native_shared_libs: ["lib_dep"],
4083 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004084 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004085 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004086 }
4087
4088 apex {
4089 name: "myapex_provider",
4090 key: "myapex.key",
4091 native_shared_libs: ["libfoo"],
4092 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004093 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004094 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004095 }
4096
4097 apex {
4098 name: "myapex_selfcontained",
4099 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00004100 native_shared_libs: ["lib_dep_on_bar", "libbar"],
Jooyung Hane1633032019-08-01 17:41:43 +09004101 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004102 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004103 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004104 }
4105
4106 apex_key {
4107 name: "myapex.key",
4108 public_key: "testkey.avbpubkey",
4109 private_key: "testkey.pem",
4110 }
4111
4112 cc_library {
4113 name: "lib_nodep",
4114 srcs: ["mylib.cpp"],
4115 system_shared_libs: [],
4116 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004117 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004118 }
4119
4120 cc_library {
4121 name: "lib_dep",
4122 srcs: ["mylib.cpp"],
4123 shared_libs: ["libfoo"],
4124 system_shared_libs: [],
4125 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004126 apex_available: [
4127 "myapex_dep",
4128 "myapex_provider",
4129 "myapex_selfcontained",
4130 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004131 }
4132
4133 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00004134 name: "lib_dep_on_bar",
4135 srcs: ["mylib.cpp"],
4136 shared_libs: ["libbar"],
4137 system_shared_libs: [],
4138 stl: "none",
4139 apex_available: [
4140 "myapex_selfcontained",
4141 ],
4142 }
4143
4144
4145 cc_library {
Jooyung Hane1633032019-08-01 17:41:43 +09004146 name: "libfoo",
4147 srcs: ["mytest.cpp"],
4148 stubs: {
4149 versions: ["1"],
4150 },
4151 system_shared_libs: [],
4152 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004153 apex_available: [
4154 "myapex_provider",
Spandan Das20fce2d2023-04-12 17:21:39 +00004155 ],
4156 }
4157
4158 cc_library {
4159 name: "libbar",
4160 srcs: ["mytest.cpp"],
4161 stubs: {
4162 versions: ["1"],
4163 },
4164 system_shared_libs: [],
4165 stl: "none",
4166 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004167 "myapex_selfcontained",
4168 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004169 }
Spandan Das20fce2d2023-04-12 17:21:39 +00004170
Jooyung Hane1633032019-08-01 17:41:43 +09004171 `)
4172
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004173 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004174 var provideNativeLibs, requireNativeLibs []string
4175
Jooyung Hana0503a52023-08-23 13:12:50 +09004176 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004177 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4178 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004179 ensureListEmpty(t, provideNativeLibs)
4180 ensureListEmpty(t, requireNativeLibs)
4181
Jooyung Hana0503a52023-08-23 13:12:50 +09004182 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004183 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4184 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004185 ensureListEmpty(t, provideNativeLibs)
4186 ensureListContains(t, requireNativeLibs, "libfoo.so")
4187
Jooyung Hana0503a52023-08-23 13:12:50 +09004188 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004189 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4190 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004191 ensureListContains(t, provideNativeLibs, "libfoo.so")
4192 ensureListEmpty(t, requireNativeLibs)
4193
Jooyung Hana0503a52023-08-23 13:12:50 +09004194 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004195 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4196 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Spandan Das20fce2d2023-04-12 17:21:39 +00004197 ensureListContains(t, provideNativeLibs, "libbar.so")
Jooyung Hane1633032019-08-01 17:41:43 +09004198 ensureListEmpty(t, requireNativeLibs)
4199}
4200
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004201func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4202 ctx := testApex(t, `
4203 apex {
4204 name: "myapex",
4205 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004206 native_shared_libs: ["mylib"],
4207 updatable: false,
4208 }
4209
4210 apex_key {
4211 name: "myapex.key",
4212 public_key: "testkey.avbpubkey",
4213 private_key: "testkey.pem",
4214 }
4215
4216 cc_library {
4217 name: "mylib",
4218 srcs: ["mylib.cpp"],
4219 system_shared_libs: [],
4220 stl: "none",
4221 apex_available: [
4222 "//apex_available:platform",
4223 "myapex",
4224 ],
4225 }
4226 `, android.FixtureMergeEnv(map[string]string{
4227 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4228 }))
4229
Jooyung Hana0503a52023-08-23 13:12:50 +09004230 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004231 apexManifestRule := module.Rule("apexManifestRule")
4232 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4233}
4234
Vinh Tran8f5310f2022-10-07 18:16:47 -04004235func TestCompileMultilibProp(t *testing.T) {
4236 testCases := []struct {
4237 compileMultiLibProp string
4238 containedLibs []string
4239 notContainedLibs []string
4240 }{
4241 {
4242 containedLibs: []string{
4243 "image.apex/lib64/mylib.so",
4244 "image.apex/lib/mylib.so",
4245 },
4246 compileMultiLibProp: `compile_multilib: "both",`,
4247 },
4248 {
4249 containedLibs: []string{"image.apex/lib64/mylib.so"},
4250 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4251 compileMultiLibProp: `compile_multilib: "first",`,
4252 },
4253 {
4254 containedLibs: []string{"image.apex/lib64/mylib.so"},
4255 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4256 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4257 },
4258 {
4259 containedLibs: []string{"image.apex/lib64/mylib.so"},
4260 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4261 compileMultiLibProp: `compile_multilib: "64",`,
4262 },
4263 {
4264 containedLibs: []string{"image.apex/lib/mylib.so"},
4265 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4266 compileMultiLibProp: `compile_multilib: "32",`,
4267 },
4268 }
4269 for _, testCase := range testCases {
4270 ctx := testApex(t, fmt.Sprintf(`
4271 apex {
4272 name: "myapex",
4273 key: "myapex.key",
4274 %s
4275 native_shared_libs: ["mylib"],
4276 updatable: false,
4277 }
4278 apex_key {
4279 name: "myapex.key",
4280 public_key: "testkey.avbpubkey",
4281 private_key: "testkey.pem",
4282 }
4283 cc_library {
4284 name: "mylib",
4285 srcs: ["mylib.cpp"],
4286 apex_available: [
4287 "//apex_available:platform",
4288 "myapex",
4289 ],
4290 }
4291 `, testCase.compileMultiLibProp),
4292 )
Jooyung Hana0503a52023-08-23 13:12:50 +09004293 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Vinh Tran8f5310f2022-10-07 18:16:47 -04004294 apexRule := module.Rule("apexRule")
4295 copyCmds := apexRule.Args["copy_commands"]
4296 for _, containedLib := range testCase.containedLibs {
4297 ensureContains(t, copyCmds, containedLib)
4298 }
4299 for _, notContainedLib := range testCase.notContainedLibs {
4300 ensureNotContains(t, copyCmds, notContainedLib)
4301 }
4302 }
4303}
4304
Alex Light0851b882019-02-07 13:20:53 -08004305func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004306 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004307 apex {
4308 name: "myapex",
4309 key: "myapex.key",
4310 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004311 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004312 }
4313
4314 apex_key {
4315 name: "myapex.key",
4316 public_key: "testkey.avbpubkey",
4317 private_key: "testkey.pem",
4318 }
4319
4320 cc_library {
4321 name: "mylib_common",
4322 srcs: ["mylib.cpp"],
4323 system_shared_libs: [],
4324 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004325 apex_available: [
4326 "//apex_available:platform",
4327 "myapex",
4328 ],
Alex Light0851b882019-02-07 13:20:53 -08004329 }
4330 `)
4331
Jooyung Hana0503a52023-08-23 13:12:50 +09004332 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004333 apexRule := module.Rule("apexRule")
4334 copyCmds := apexRule.Args["copy_commands"]
4335
4336 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4337 t.Log("Apex was a test apex!")
4338 t.Fail()
4339 }
4340 // Ensure that main rule creates an output
4341 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4342
4343 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004344 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004345
4346 // Ensure that both direct and indirect deps are copied into apex
4347 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4348
Colin Cross7113d202019-11-20 16:39:12 -08004349 // Ensure that the platform variant ends with _shared
4350 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004351
Colin Cross56a83212020-09-15 18:30:11 -07004352 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004353 t.Log("Found mylib_common not in any apex!")
4354 t.Fail()
4355 }
4356}
4357
4358func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004359 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004360 apex_test {
4361 name: "myapex",
4362 key: "myapex.key",
4363 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004364 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004365 }
4366
4367 apex_key {
4368 name: "myapex.key",
4369 public_key: "testkey.avbpubkey",
4370 private_key: "testkey.pem",
4371 }
4372
4373 cc_library {
4374 name: "mylib_common_test",
4375 srcs: ["mylib.cpp"],
4376 system_shared_libs: [],
4377 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004378 // TODO: remove //apex_available:platform
4379 apex_available: [
4380 "//apex_available:platform",
4381 "myapex",
4382 ],
Alex Light0851b882019-02-07 13:20:53 -08004383 }
4384 `)
4385
Jooyung Hana0503a52023-08-23 13:12:50 +09004386 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004387 apexRule := module.Rule("apexRule")
4388 copyCmds := apexRule.Args["copy_commands"]
4389
4390 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4391 t.Log("Apex was not a test apex!")
4392 t.Fail()
4393 }
4394 // Ensure that main rule creates an output
4395 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4396
4397 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004398 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004399
4400 // Ensure that both direct and indirect deps are copied into apex
4401 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4402
Colin Cross7113d202019-11-20 16:39:12 -08004403 // Ensure that the platform variant ends with _shared
4404 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004405}
4406
Jooyung Han85707de2023-12-01 14:21:13 +09004407func TestLibzVendorIsntStable(t *testing.T) {
4408 ctx := testApex(t, `
4409 apex {
4410 name: "myapex",
4411 key: "myapex.key",
4412 updatable: false,
4413 binaries: ["mybin"],
4414 }
4415 apex {
4416 name: "myvendorapex",
4417 key: "myapex.key",
4418 file_contexts: "myvendorapex_file_contexts",
4419 vendor: true,
4420 updatable: false,
4421 binaries: ["mybin"],
4422 }
4423 apex_key {
4424 name: "myapex.key",
4425 public_key: "testkey.avbpubkey",
4426 private_key: "testkey.pem",
4427 }
4428 cc_binary {
4429 name: "mybin",
4430 vendor_available: true,
4431 system_shared_libs: [],
4432 stl: "none",
4433 shared_libs: ["libz"],
4434 apex_available: ["//apex_available:anyapex"],
4435 }
4436 cc_library {
4437 name: "libz",
4438 vendor_available: true,
4439 system_shared_libs: [],
4440 stl: "none",
4441 stubs: {
4442 versions: ["28", "30"],
4443 },
4444 target: {
4445 vendor: {
4446 no_stubs: true,
4447 },
4448 },
4449 }
4450 `, withFiles(map[string][]byte{
4451 "myvendorapex_file_contexts": nil,
4452 }))
4453
4454 // libz provides stubs for core variant.
4455 {
4456 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
4457 "bin/mybin",
4458 })
4459 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
4460 android.AssertStringEquals(t, "should require libz", apexManifestRule.Args["requireNativeLibs"], "libz.so")
4461 }
4462 // libz doesn't provide stubs for vendor variant.
4463 {
4464 ensureExactContents(t, ctx, "myvendorapex", "android_common_myvendorapex", []string{
4465 "bin/mybin",
4466 "lib64/libz.so",
4467 })
4468 apexManifestRule := ctx.ModuleForTests("myvendorapex", "android_common_myvendorapex").Rule("apexManifestRule")
4469 android.AssertStringEquals(t, "should not require libz", apexManifestRule.Args["requireNativeLibs"], "")
4470 }
4471}
4472
Alex Light9670d332019-01-29 18:07:33 -08004473func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004474 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004475 apex {
4476 name: "myapex",
4477 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004478 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004479 multilib: {
4480 first: {
4481 native_shared_libs: ["mylib_common"],
4482 }
4483 },
4484 target: {
4485 android: {
4486 multilib: {
4487 first: {
4488 native_shared_libs: ["mylib"],
4489 }
4490 }
4491 },
4492 host: {
4493 multilib: {
4494 first: {
4495 native_shared_libs: ["mylib2"],
4496 }
4497 }
4498 }
4499 }
4500 }
4501
4502 apex_key {
4503 name: "myapex.key",
4504 public_key: "testkey.avbpubkey",
4505 private_key: "testkey.pem",
4506 }
4507
4508 cc_library {
4509 name: "mylib",
4510 srcs: ["mylib.cpp"],
4511 system_shared_libs: [],
4512 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004513 // TODO: remove //apex_available:platform
4514 apex_available: [
4515 "//apex_available:platform",
4516 "myapex",
4517 ],
Alex Light9670d332019-01-29 18:07:33 -08004518 }
4519
4520 cc_library {
4521 name: "mylib_common",
4522 srcs: ["mylib.cpp"],
4523 system_shared_libs: [],
4524 stl: "none",
4525 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004526 // TODO: remove //apex_available:platform
4527 apex_available: [
4528 "//apex_available:platform",
4529 "myapex",
4530 ],
Alex Light9670d332019-01-29 18:07:33 -08004531 }
4532
4533 cc_library {
4534 name: "mylib2",
4535 srcs: ["mylib.cpp"],
4536 system_shared_libs: [],
4537 stl: "none",
4538 compile_multilib: "first",
4539 }
4540 `)
4541
Jooyung Hana0503a52023-08-23 13:12:50 +09004542 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004543 copyCmds := apexRule.Args["copy_commands"]
4544
4545 // Ensure that main rule creates an output
4546 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4547
4548 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004549 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4550 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4551 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004552
4553 // Ensure that both direct and indirect deps are copied into apex
4554 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4555 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4556 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4557
Colin Cross7113d202019-11-20 16:39:12 -08004558 // Ensure that the platform variant ends with _shared
4559 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4560 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4561 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004562}
Jiyong Park04480cf2019-02-06 00:16:29 +09004563
Jiyong Park59140302020-12-14 18:44:04 +09004564func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004565 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004566 apex {
4567 name: "myapex",
4568 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004569 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004570 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004571 arch: {
4572 arm64: {
4573 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004574 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004575 },
4576 x86_64: {
4577 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004578 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004579 },
4580 }
4581 }
4582
4583 apex_key {
4584 name: "myapex.key",
4585 public_key: "testkey.avbpubkey",
4586 private_key: "testkey.pem",
4587 }
4588
4589 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004590 name: "mylib.generic",
4591 srcs: ["mylib.cpp"],
4592 system_shared_libs: [],
4593 stl: "none",
4594 // TODO: remove //apex_available:platform
4595 apex_available: [
4596 "//apex_available:platform",
4597 "myapex",
4598 ],
4599 }
4600
4601 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004602 name: "mylib.arm64",
4603 srcs: ["mylib.cpp"],
4604 system_shared_libs: [],
4605 stl: "none",
4606 // TODO: remove //apex_available:platform
4607 apex_available: [
4608 "//apex_available:platform",
4609 "myapex",
4610 ],
4611 }
4612
4613 cc_library {
4614 name: "mylib.x64",
4615 srcs: ["mylib.cpp"],
4616 system_shared_libs: [],
4617 stl: "none",
4618 // TODO: remove //apex_available:platform
4619 apex_available: [
4620 "//apex_available:platform",
4621 "myapex",
4622 ],
4623 }
4624 `)
4625
Jooyung Hana0503a52023-08-23 13:12:50 +09004626 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park59140302020-12-14 18:44:04 +09004627 copyCmds := apexRule.Args["copy_commands"]
4628
4629 // Ensure that apex variant is created for the direct dep
4630 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004631 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004632 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4633
4634 // Ensure that both direct and indirect deps are copied into apex
4635 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4636 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4637}
4638
Jiyong Park04480cf2019-02-06 00:16:29 +09004639func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004640 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004641 apex {
4642 name: "myapex",
4643 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004644 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004645 updatable: false,
Riya Thakur654461c2024-02-27 07:21:05 +00004646 compile_multilib: "both",
Jiyong Park04480cf2019-02-06 00:16:29 +09004647 }
4648
4649 apex_key {
4650 name: "myapex.key",
4651 public_key: "testkey.avbpubkey",
4652 private_key: "testkey.pem",
4653 }
4654
4655 sh_binary {
4656 name: "myscript",
4657 src: "mylib.cpp",
4658 filename: "myscript.sh",
4659 sub_dir: "script",
4660 }
4661 `)
4662
Jooyung Hana0503a52023-08-23 13:12:50 +09004663 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004664 copyCmds := apexRule.Args["copy_commands"]
4665
4666 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4667}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004668
Jooyung Han91df2082019-11-20 01:49:42 +09004669func TestApexInVariousPartition(t *testing.T) {
4670 testcases := []struct {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004671 propName, partition string
Jooyung Han91df2082019-11-20 01:49:42 +09004672 }{
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004673 {"", "system"},
4674 {"product_specific: true", "product"},
4675 {"soc_specific: true", "vendor"},
4676 {"proprietary: true", "vendor"},
4677 {"vendor: true", "vendor"},
4678 {"system_ext_specific: true", "system_ext"},
Jooyung Han91df2082019-11-20 01:49:42 +09004679 }
4680 for _, tc := range testcases {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004681 t.Run(tc.propName+":"+tc.partition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004682 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004683 apex {
4684 name: "myapex",
4685 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004686 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004687 `+tc.propName+`
4688 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004689
Jooyung Han91df2082019-11-20 01:49:42 +09004690 apex_key {
4691 name: "myapex.key",
4692 public_key: "testkey.avbpubkey",
4693 private_key: "testkey.pem",
4694 }
4695 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004696
Jooyung Hana0503a52023-08-23 13:12:50 +09004697 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004698 expected := "out/soong/target/product/test_device/" + tc.partition + "/apex"
Paul Duffin37ba3442021-03-29 00:21:08 +01004699 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004700 if actual != expected {
4701 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4702 }
Jooyung Han91df2082019-11-20 01:49:42 +09004703 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004704 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004705}
Jiyong Park67882562019-03-21 01:11:21 +09004706
Jooyung Han580eb4f2020-06-24 19:33:06 +09004707func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004708 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004709 apex {
4710 name: "myapex",
4711 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004712 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004713 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004714
Jooyung Han580eb4f2020-06-24 19:33:06 +09004715 apex_key {
4716 name: "myapex.key",
4717 public_key: "testkey.avbpubkey",
4718 private_key: "testkey.pem",
4719 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004720 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09004721 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004722 rule := module.Output("file_contexts")
4723 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4724}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004725
Jooyung Han580eb4f2020-06-24 19:33:06 +09004726func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004727 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004728 apex {
4729 name: "myapex",
4730 key: "myapex.key",
4731 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004732 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004733 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004734
Jooyung Han580eb4f2020-06-24 19:33:06 +09004735 apex_key {
4736 name: "myapex.key",
4737 public_key: "testkey.avbpubkey",
4738 private_key: "testkey.pem",
4739 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004740 `, withFiles(map[string][]byte{
4741 "my_own_file_contexts": nil,
4742 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004743}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004744
Jooyung Han580eb4f2020-06-24 19:33:06 +09004745func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004746 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004747 apex {
4748 name: "myapex",
4749 key: "myapex.key",
4750 product_specific: true,
4751 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004752 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004753 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004754
Jooyung Han580eb4f2020-06-24 19:33:06 +09004755 apex_key {
4756 name: "myapex.key",
4757 public_key: "testkey.avbpubkey",
4758 private_key: "testkey.pem",
4759 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004760 `)
4761
Colin Cross1c460562021-02-16 17:55:47 -08004762 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004763 apex {
4764 name: "myapex",
4765 key: "myapex.key",
4766 product_specific: true,
4767 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004768 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004769 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004770
Jooyung Han580eb4f2020-06-24 19:33:06 +09004771 apex_key {
4772 name: "myapex.key",
4773 public_key: "testkey.avbpubkey",
4774 private_key: "testkey.pem",
4775 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004776 `, withFiles(map[string][]byte{
4777 "product_specific_file_contexts": nil,
4778 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004779 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004780 rule := module.Output("file_contexts")
4781 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4782}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004783
Jooyung Han580eb4f2020-06-24 19:33:06 +09004784func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004785 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004786 apex {
4787 name: "myapex",
4788 key: "myapex.key",
4789 product_specific: true,
4790 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004791 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004792 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004793
Jooyung Han580eb4f2020-06-24 19:33:06 +09004794 apex_key {
4795 name: "myapex.key",
4796 public_key: "testkey.avbpubkey",
4797 private_key: "testkey.pem",
4798 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004799
Jooyung Han580eb4f2020-06-24 19:33:06 +09004800 filegroup {
4801 name: "my-file-contexts",
4802 srcs: ["product_specific_file_contexts"],
4803 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004804 `, withFiles(map[string][]byte{
4805 "product_specific_file_contexts": nil,
4806 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004807 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004808 rule := module.Output("file_contexts")
4809 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004810}
4811
Jiyong Park67882562019-03-21 01:11:21 +09004812func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004813 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004814 apex_key {
4815 name: "myapex.key",
4816 public_key: ":my.avbpubkey",
4817 private_key: ":my.pem",
4818 product_specific: true,
4819 }
4820
4821 filegroup {
4822 name: "my.avbpubkey",
4823 srcs: ["testkey2.avbpubkey"],
4824 }
4825
4826 filegroup {
4827 name: "my.pem",
4828 srcs: ["testkey2.pem"],
4829 }
4830 `)
4831
4832 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4833 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004834 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004835 if actual_pubkey != expected_pubkey {
4836 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4837 }
4838 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004839 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004840 if actual_privkey != expected_privkey {
4841 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4842 }
4843}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004844
4845func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004846 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004847 prebuilt_apex {
4848 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004849 arch: {
4850 arm64: {
4851 src: "myapex-arm64.apex",
4852 },
4853 arm: {
4854 src: "myapex-arm.apex",
4855 },
4856 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004857 }
4858 `)
4859
Wei Li340ee8e2022-03-18 17:33:24 -07004860 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4861 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004862
Jiyong Parkc95714e2019-03-29 14:23:10 +09004863 expectedInput := "myapex-arm64.apex"
4864 if prebuilt.inputApex.String() != expectedInput {
4865 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4866 }
Wei Li340ee8e2022-03-18 17:33:24 -07004867 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4868 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4869 rule := testingModule.Rule("genProvenanceMetaData")
4870 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4871 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4872 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4873 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08004874
4875 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
4876 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004877}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004878
Paul Duffinc0609c62021-03-01 17:27:16 +00004879func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004880 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004881 prebuilt_apex {
4882 name: "myapex",
4883 }
4884 `)
4885}
4886
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004887func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004888 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004889 prebuilt_apex {
4890 name: "myapex",
4891 src: "myapex-arm.apex",
4892 filename: "notmyapex.apex",
4893 }
4894 `)
4895
Wei Li340ee8e2022-03-18 17:33:24 -07004896 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4897 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004898
4899 expected := "notmyapex.apex"
4900 if p.installFilename != expected {
4901 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4902 }
Wei Li340ee8e2022-03-18 17:33:24 -07004903 rule := testingModule.Rule("genProvenanceMetaData")
4904 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4905 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4906 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4907 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004908}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004909
Samiul Islam7c02e262021-09-08 17:48:28 +01004910func TestApexSetFilenameOverride(t *testing.T) {
4911 testApex(t, `
4912 apex_set {
4913 name: "com.company.android.myapex",
4914 apex_name: "com.android.myapex",
4915 set: "company-myapex.apks",
4916 filename: "com.company.android.myapex.apex"
4917 }
4918 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4919
4920 testApex(t, `
4921 apex_set {
4922 name: "com.company.android.myapex",
4923 apex_name: "com.android.myapex",
4924 set: "company-myapex.apks",
4925 filename: "com.company.android.myapex.capex"
4926 }
4927 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4928
4929 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4930 apex_set {
4931 name: "com.company.android.myapex",
4932 apex_name: "com.android.myapex",
4933 set: "company-myapex.apks",
4934 filename: "some-random-suffix"
4935 }
4936 `)
4937}
4938
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004939func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004940 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004941 prebuilt_apex {
4942 name: "myapex.prebuilt",
4943 src: "myapex-arm.apex",
4944 overrides: [
4945 "myapex",
4946 ],
4947 }
4948 `)
4949
Wei Li340ee8e2022-03-18 17:33:24 -07004950 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4951 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004952
4953 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004954 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004955 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004956 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004957 }
Wei Li340ee8e2022-03-18 17:33:24 -07004958 rule := testingModule.Rule("genProvenanceMetaData")
4959 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4960 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4961 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4962 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004963}
4964
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004965func TestPrebuiltApexName(t *testing.T) {
4966 testApex(t, `
4967 prebuilt_apex {
4968 name: "com.company.android.myapex",
4969 apex_name: "com.android.myapex",
4970 src: "company-myapex-arm.apex",
4971 }
4972 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4973
4974 testApex(t, `
4975 apex_set {
4976 name: "com.company.android.myapex",
4977 apex_name: "com.android.myapex",
4978 set: "company-myapex.apks",
4979 }
4980 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4981}
4982
4983func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4984 _ = android.GroupFixturePreparers(
4985 java.PrepareForTestWithJavaDefaultModules,
4986 PrepareForTestWithApexBuildComponents,
4987 android.FixtureWithRootAndroidBp(`
4988 platform_bootclasspath {
4989 name: "platform-bootclasspath",
4990 fragments: [
4991 {
4992 apex: "com.android.art",
4993 module: "art-bootclasspath-fragment",
4994 },
4995 ],
4996 }
4997
4998 prebuilt_apex {
4999 name: "com.company.android.art",
5000 apex_name: "com.android.art",
5001 src: "com.company.android.art-arm.apex",
5002 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
5003 }
5004
5005 prebuilt_bootclasspath_fragment {
5006 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01005007 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01005008 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01005009 hidden_api: {
5010 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5011 metadata: "my-bootclasspath-fragment/metadata.csv",
5012 index: "my-bootclasspath-fragment/index.csv",
5013 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5014 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5015 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01005016 }
5017
5018 java_import {
5019 name: "core-oj",
5020 jars: ["prebuilt.jar"],
5021 }
5022 `),
5023 ).RunTest(t)
5024}
5025
Spandan Das59a4a2b2024-01-09 21:35:56 +00005026// A minimal context object for use with DexJarBuildPath
5027type moduleErrorfTestCtx struct {
5028}
5029
5030func (ctx moduleErrorfTestCtx) ModuleErrorf(format string, args ...interface{}) {
5031}
5032
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005033func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005034 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005035 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005036 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5037 // is disabled.
5038 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
Spandan Das81fe4d12024-05-15 18:43:47 +00005039
5040 // Make sure that we have atleast one platform library so that we can check the monolithic hiddenapi
5041 // file creation.
5042 java.FixtureConfigureBootJars("platform:foo"),
5043 android.FixtureModifyMockFS(func(fs android.MockFS) {
5044 fs["platform/Android.bp"] = []byte(`
5045 java_library {
5046 name: "foo",
5047 srcs: ["Test.java"],
5048 compile_dex: true,
5049 }
5050 `)
5051 fs["platform/Test.java"] = nil
5052 }),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005053 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005054
Paul Duffin40a3f652021-07-19 13:11:24 +01005055 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005056 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005057 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005058 var rule android.TestingBuildParams
5059
5060 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5061 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005062 }
5063
Paul Duffin40a3f652021-07-19 13:11:24 +01005064 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5065 t.Helper()
5066 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5067 var rule android.TestingBuildParams
5068
5069 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5070 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5071 }
5072
Paul Duffin89f570a2021-06-16 01:42:33 +01005073 fragment := java.ApexVariantReference{
5074 Apex: proptools.StringPtr("myapex"),
5075 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5076 }
5077
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005078 t.Run("prebuilt only", func(t *testing.T) {
5079 bp := `
5080 prebuilt_apex {
5081 name: "myapex",
5082 arch: {
5083 arm64: {
5084 src: "myapex-arm64.apex",
5085 },
5086 arm: {
5087 src: "myapex-arm.apex",
5088 },
5089 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005090 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5091 }
5092
5093 prebuilt_bootclasspath_fragment {
5094 name: "my-bootclasspath-fragment",
5095 contents: ["libfoo", "libbar"],
5096 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005097 hidden_api: {
5098 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5099 metadata: "my-bootclasspath-fragment/metadata.csv",
5100 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005101 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5102 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5103 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005104 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005105 }
5106
Spandan Das52c01a12024-09-20 01:09:48 +00005107 java_sdk_library_import {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005108 name: "libfoo",
Spandan Das52c01a12024-09-20 01:09:48 +00005109 public: {
5110 jars: ["libfoo.jar"],
5111 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005112 apex_available: ["myapex"],
Spandan Das52c01a12024-09-20 01:09:48 +00005113 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005114 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005115 }
Paul Duffin37856732021-02-26 14:24:15 +00005116
5117 java_sdk_library_import {
5118 name: "libbar",
5119 public: {
5120 jars: ["libbar.jar"],
5121 },
5122 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005123 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005124 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005125 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005126 `
5127
Paul Duffin89f570a2021-06-16 01:42:33 +01005128 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005129
Paul Duffin537ea3d2021-05-14 10:38:00 +01005130 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005131 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005132 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005133 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005134 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005135 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 +01005136 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005137 })
5138
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005139 t.Run("apex_set only", func(t *testing.T) {
5140 bp := `
5141 apex_set {
5142 name: "myapex",
5143 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005144 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
Liz Kammer2dc72442023-04-20 10:10:48 -04005145 exported_systemserverclasspath_fragments: ["my-systemserverclasspath-fragment"],
5146 }
5147
Paul Duffin89f570a2021-06-16 01:42:33 +01005148 prebuilt_bootclasspath_fragment {
5149 name: "my-bootclasspath-fragment",
5150 contents: ["libfoo", "libbar"],
5151 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005152 hidden_api: {
5153 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5154 metadata: "my-bootclasspath-fragment/metadata.csv",
5155 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005156 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5157 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5158 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005159 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005160 }
5161
Liz Kammer2dc72442023-04-20 10:10:48 -04005162 prebuilt_systemserverclasspath_fragment {
5163 name: "my-systemserverclasspath-fragment",
5164 contents: ["libbaz"],
5165 apex_available: ["myapex"],
5166 }
5167
Spandan Das52c01a12024-09-20 01:09:48 +00005168 java_sdk_library_import {
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005169 name: "libfoo",
Spandan Das52c01a12024-09-20 01:09:48 +00005170 public: {
5171 jars: ["libfoo.jar"],
5172 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005173 apex_available: ["myapex"],
Spandan Das52c01a12024-09-20 01:09:48 +00005174 shared_library: false,
5175 permitted_packages: ["libfoo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005176 }
5177
Spandan Das52c01a12024-09-20 01:09:48 +00005178
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005179 java_sdk_library_import {
5180 name: "libbar",
5181 public: {
5182 jars: ["libbar.jar"],
5183 },
5184 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005185 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005186 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005187 }
Liz Kammer2dc72442023-04-20 10:10:48 -04005188
5189 java_sdk_library_import {
5190 name: "libbaz",
5191 public: {
5192 jars: ["libbaz.jar"],
5193 },
5194 apex_available: ["myapex"],
5195 shared_library: false,
5196 permitted_packages: ["baz"],
5197 }
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005198 `
5199
Paul Duffin89f570a2021-06-16 01:42:33 +01005200 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005201
Paul Duffin537ea3d2021-05-14 10:38:00 +01005202 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005203 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005204 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005205 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005206 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005207 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 +01005208 `)
Liz Kammer2dc72442023-04-20 10:10:48 -04005209
5210 myApex := ctx.ModuleForTests("myapex", "android_common_myapex").Module()
5211
5212 overrideNames := []string{
Spandan Dasa8e2d612024-07-26 19:24:27 +00005213 "",
Liz Kammer2dc72442023-04-20 10:10:48 -04005214 "myjavalib.myapex",
5215 "libfoo.myapex",
5216 "libbar.myapex",
5217 "libbaz.myapex",
5218 }
5219 mkEntries := android.AndroidMkEntriesForTest(t, ctx, myApex)
5220 for i, e := range mkEntries {
5221 g := e.OverrideName
5222 if w := overrideNames[i]; w != g {
5223 t.Errorf("Expected override name %q, got %q", w, g)
5224 }
5225 }
5226
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005227 })
5228
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005229 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5230 bp := `
5231 prebuilt_apex {
5232 name: "myapex",
5233 arch: {
5234 arm64: {
5235 src: "myapex-arm64.apex",
5236 },
5237 arm: {
5238 src: "myapex-arm.apex",
5239 },
5240 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005241 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5242 }
5243
5244 prebuilt_bootclasspath_fragment {
5245 name: "my-bootclasspath-fragment",
5246 contents: ["libfoo", "libbar"],
5247 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005248 hidden_api: {
5249 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5250 metadata: "my-bootclasspath-fragment/metadata.csv",
5251 index: "my-bootclasspath-fragment/index.csv",
5252 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5253 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5254 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005255 }
5256
5257 java_import {
5258 name: "libfoo",
5259 jars: ["libfoo.jar"],
5260 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005261 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005262 }
5263
5264 java_library {
5265 name: "libfoo",
5266 srcs: ["foo/bar/MyClass.java"],
5267 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005268 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005269 }
Paul Duffin37856732021-02-26 14:24:15 +00005270
5271 java_sdk_library_import {
5272 name: "libbar",
5273 public: {
5274 jars: ["libbar.jar"],
5275 },
5276 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005277 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005278 }
5279
5280 java_sdk_library {
5281 name: "libbar",
5282 srcs: ["foo/bar/MyClass.java"],
5283 unsafe_ignore_missing_latest_api: true,
5284 apex_available: ["myapex"],
5285 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005286 `
5287
5288 // In this test the source (java_library) libfoo is active since the
5289 // prebuilt (java_import) defaults to prefer:false. However the
5290 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5291 // find the dex boot jar in it. We either need to disable the source libfoo
5292 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005293 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005294 // dexbootjar check is skipped if AllowMissingDependencies is true
5295 preparerAllowMissingDeps := android.GroupFixturePreparers(
5296 preparer,
5297 android.PrepareForTestWithAllowMissingDependencies,
5298 )
5299 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005300 })
5301
5302 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5303 bp := `
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005304 apex {
5305 name: "myapex",
5306 key: "myapex.key",
5307 updatable: false,
5308 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5309 }
5310
5311 apex_key {
5312 name: "myapex.key",
5313 public_key: "testkey.avbpubkey",
5314 private_key: "testkey.pem",
5315 }
5316
5317 bootclasspath_fragment {
5318 name: "my-bootclasspath-fragment",
5319 contents: ["libfoo", "libbar"],
5320 apex_available: ["myapex"],
5321 hidden_api: {
5322 split_packages: ["*"],
5323 },
5324 }
5325
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005326 prebuilt_apex {
5327 name: "myapex",
5328 arch: {
5329 arm64: {
5330 src: "myapex-arm64.apex",
5331 },
5332 arm: {
5333 src: "myapex-arm.apex",
5334 },
5335 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005336 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5337 }
5338
5339 prebuilt_bootclasspath_fragment {
5340 name: "my-bootclasspath-fragment",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005341 prefer: true,
Paul Duffin89f570a2021-06-16 01:42:33 +01005342 contents: ["libfoo", "libbar"],
5343 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005344 hidden_api: {
5345 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5346 metadata: "my-bootclasspath-fragment/metadata.csv",
5347 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005348 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5349 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5350 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005351 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005352 }
5353
Spandan Das52c01a12024-09-20 01:09:48 +00005354 java_sdk_library_import {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005355 name: "libfoo",
5356 prefer: true,
Spandan Das52c01a12024-09-20 01:09:48 +00005357 public: {
5358 jars: ["libfoo.jar"],
5359 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005360 apex_available: ["myapex"],
Spandan Das52c01a12024-09-20 01:09:48 +00005361 shared_library: false,
5362 permitted_packages: ["libfoo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005363 }
5364
5365 java_library {
5366 name: "libfoo",
5367 srcs: ["foo/bar/MyClass.java"],
5368 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005369 installable: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00005370 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005371 }
Paul Duffin37856732021-02-26 14:24:15 +00005372
5373 java_sdk_library_import {
5374 name: "libbar",
5375 prefer: true,
5376 public: {
5377 jars: ["libbar.jar"],
5378 },
5379 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005380 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005381 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005382 }
5383
5384 java_sdk_library {
5385 name: "libbar",
5386 srcs: ["foo/bar/MyClass.java"],
5387 unsafe_ignore_missing_latest_api: true,
5388 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005389 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005390 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005391 `
5392
Paul Duffin89f570a2021-06-16 01:42:33 +01005393 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005394
Paul Duffin537ea3d2021-05-14 10:38:00 +01005395 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005396 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005397 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005398 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005399 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005400 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 +01005401 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005402 })
5403
5404 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5405 bp := `
5406 apex {
5407 name: "myapex",
5408 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005409 updatable: false,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005410 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005411 }
5412
5413 apex_key {
5414 name: "myapex.key",
5415 public_key: "testkey.avbpubkey",
5416 private_key: "testkey.pem",
5417 }
5418
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005419 bootclasspath_fragment {
5420 name: "my-bootclasspath-fragment",
5421 contents: ["libfoo", "libbar"],
5422 apex_available: ["myapex"],
5423 hidden_api: {
5424 split_packages: ["*"],
5425 },
5426 }
5427
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005428 prebuilt_apex {
5429 name: "myapex",
5430 arch: {
5431 arm64: {
5432 src: "myapex-arm64.apex",
5433 },
5434 arm: {
5435 src: "myapex-arm.apex",
5436 },
5437 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005438 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5439 }
5440
5441 prebuilt_bootclasspath_fragment {
5442 name: "my-bootclasspath-fragment",
5443 contents: ["libfoo", "libbar"],
5444 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005445 hidden_api: {
5446 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5447 metadata: "my-bootclasspath-fragment/metadata.csv",
5448 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005449 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5450 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5451 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005452 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005453 }
5454
5455 java_import {
5456 name: "libfoo",
5457 jars: ["libfoo.jar"],
5458 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005459 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005460 }
5461
5462 java_library {
5463 name: "libfoo",
5464 srcs: ["foo/bar/MyClass.java"],
5465 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005466 permitted_packages: ["foo"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005467 installable: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00005468 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005469 }
Paul Duffin37856732021-02-26 14:24:15 +00005470
5471 java_sdk_library_import {
5472 name: "libbar",
5473 public: {
5474 jars: ["libbar.jar"],
5475 },
5476 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005477 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005478 }
5479
5480 java_sdk_library {
5481 name: "libbar",
5482 srcs: ["foo/bar/MyClass.java"],
5483 unsafe_ignore_missing_latest_api: true,
5484 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005485 permitted_packages: ["bar"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005486 compile_dex: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00005487 sdk_version: "core_current",
Paul Duffin37856732021-02-26 14:24:15 +00005488 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005489 `
5490
Paul Duffin89f570a2021-06-16 01:42:33 +01005491 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005492
Paul Duffin537ea3d2021-05-14 10:38:00 +01005493 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005494 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005495 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
5496 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005497 out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/modular-hiddenapi/index.csv
5498 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 +01005499 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005500 })
5501
5502 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5503 bp := `
5504 apex {
5505 name: "myapex",
5506 enabled: false,
5507 key: "myapex.key",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005508 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005509 }
5510
5511 apex_key {
5512 name: "myapex.key",
5513 public_key: "testkey.avbpubkey",
5514 private_key: "testkey.pem",
5515 }
5516
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005517 bootclasspath_fragment {
5518 name: "my-bootclasspath-fragment",
5519 enabled: false,
5520 contents: ["libfoo", "libbar"],
5521 apex_available: ["myapex"],
5522 hidden_api: {
5523 split_packages: ["*"],
5524 },
5525 }
5526
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005527 prebuilt_apex {
5528 name: "myapex",
5529 arch: {
5530 arm64: {
5531 src: "myapex-arm64.apex",
5532 },
5533 arm: {
5534 src: "myapex-arm.apex",
5535 },
5536 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005537 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5538 }
5539
5540 prebuilt_bootclasspath_fragment {
5541 name: "my-bootclasspath-fragment",
5542 contents: ["libfoo", "libbar"],
5543 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005544 hidden_api: {
5545 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5546 metadata: "my-bootclasspath-fragment/metadata.csv",
5547 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005548 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5549 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5550 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005551 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005552 }
5553
5554 java_import {
5555 name: "libfoo",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005556 jars: ["libfoo.jar"],
5557 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005558 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005559 }
5560
5561 java_library {
5562 name: "libfoo",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005563 enabled: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005564 srcs: ["foo/bar/MyClass.java"],
5565 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005566 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005567 }
Paul Duffin37856732021-02-26 14:24:15 +00005568
5569 java_sdk_library_import {
5570 name: "libbar",
Paul Duffin37856732021-02-26 14:24:15 +00005571 public: {
5572 jars: ["libbar.jar"],
5573 },
5574 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005575 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005576 permitted_packages: ["bar"],
Jihoon Kanga6d0aa82024-09-24 00:34:49 +00005577 prefer: true,
Paul Duffin37856732021-02-26 14:24:15 +00005578 }
5579
5580 java_sdk_library {
5581 name: "libbar",
5582 srcs: ["foo/bar/MyClass.java"],
5583 unsafe_ignore_missing_latest_api: true,
5584 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005585 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005586 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005587 `
Cole Fausta963b942024-04-11 17:43:00 -07005588 // This test disables libbar, which causes the ComponentDepsMutator to add
5589 // deps on libbar.stubs and other sub-modules that don't exist. We can
5590 // enable AllowMissingDependencies to work around that, but enabling that
5591 // causes extra checks for missing source files to dex_bootjars, so add those
5592 // to the mock fs as well.
5593 preparer2 := android.GroupFixturePreparers(
5594 preparer,
5595 android.PrepareForTestWithAllowMissingDependencies,
5596 android.FixtureMergeMockFs(map[string][]byte{
5597 "build/soong/scripts/check_boot_jars/package_allowed_list.txt": nil,
Colin Cross99939e92024-10-01 16:02:46 -07005598 "frameworks/base/boot/boot-profile.txt": nil,
Cole Fausta963b942024-04-11 17:43:00 -07005599 }),
5600 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005601
Cole Fausta963b942024-04-11 17:43:00 -07005602 ctx := testDexpreoptWithApexes(t, bp, "", preparer2, fragment)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005603
Paul Duffin537ea3d2021-05-14 10:38:00 +01005604 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005605 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005606 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005607 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005608 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005609 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 +01005610 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005611 })
Spandan Das3a392012024-01-17 18:26:27 +00005612
Spandan Dasf2c10572024-02-27 04:49:52 +00005613 t.Run("Co-existing unflagged apexes should create a duplicate module error", func(t *testing.T) {
Spandan Das3a392012024-01-17 18:26:27 +00005614 bp := `
5615 // Source
5616 apex {
5617 name: "myapex",
5618 enabled: false,
5619 key: "myapex.key",
5620 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5621 }
5622
5623 apex_key {
5624 name: "myapex.key",
5625 public_key: "testkey.avbpubkey",
5626 private_key: "testkey.pem",
5627 }
5628
5629 // Prebuilt
5630 prebuilt_apex {
5631 name: "myapex.v1",
5632 source_apex_name: "myapex",
5633 arch: {
5634 arm64: {
5635 src: "myapex-arm64.apex",
5636 },
5637 arm: {
5638 src: "myapex-arm.apex",
5639 },
5640 },
5641 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5642 prefer: true,
5643 }
5644 prebuilt_apex {
5645 name: "myapex.v2",
5646 source_apex_name: "myapex",
5647 arch: {
5648 arm64: {
5649 src: "myapex-arm64.apex",
5650 },
5651 arm: {
5652 src: "myapex-arm.apex",
5653 },
5654 },
5655 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5656 prefer: true,
5657 }
5658
5659 prebuilt_bootclasspath_fragment {
5660 name: "my-bootclasspath-fragment",
5661 contents: ["libfoo", "libbar"],
5662 apex_available: ["myapex"],
5663 hidden_api: {
5664 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5665 metadata: "my-bootclasspath-fragment/metadata.csv",
5666 index: "my-bootclasspath-fragment/index.csv",
5667 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5668 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5669 },
5670 prefer: true,
5671 }
5672
5673 java_import {
5674 name: "libfoo",
5675 jars: ["libfoo.jar"],
5676 apex_available: ["myapex"],
5677 prefer: true,
5678 }
5679 java_import {
5680 name: "libbar",
5681 jars: ["libbar.jar"],
5682 apex_available: ["myapex"],
5683 prefer: true,
5684 }
5685 `
5686
Spandan Dasf2c10572024-02-27 04:49:52 +00005687 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 +00005688 })
5689
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005690}
5691
Roland Levillain630846d2019-06-26 12:48:34 +01005692func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005693 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005694 apex_test {
5695 name: "myapex",
5696 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005697 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005698 tests: [
5699 "mytest",
5700 ],
5701 }
5702
5703 apex_key {
5704 name: "myapex.key",
5705 public_key: "testkey.avbpubkey",
5706 private_key: "testkey.pem",
5707 }
5708
Liz Kammer1c14a212020-05-12 15:26:55 -07005709 filegroup {
5710 name: "fg",
5711 srcs: [
5712 "baz",
5713 "bar/baz"
5714 ],
5715 }
5716
Roland Levillain630846d2019-06-26 12:48:34 +01005717 cc_test {
5718 name: "mytest",
5719 gtest: false,
5720 srcs: ["mytest.cpp"],
5721 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005722 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005723 system_shared_libs: [],
5724 static_executable: true,
5725 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005726 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005727 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005728
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005729 cc_library {
5730 name: "mylib",
5731 srcs: ["mylib.cpp"],
5732 system_shared_libs: [],
5733 stl: "none",
5734 }
5735
Liz Kammer5bd365f2020-05-27 15:15:11 -07005736 filegroup {
5737 name: "fg2",
5738 srcs: [
5739 "testdata/baz"
5740 ],
5741 }
Roland Levillain630846d2019-06-26 12:48:34 +01005742 `)
5743
Jooyung Hana0503a52023-08-23 13:12:50 +09005744 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005745 copyCmds := apexRule.Args["copy_commands"]
5746
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005747 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005748 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005749 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005750
Liz Kammer1c14a212020-05-12 15:26:55 -07005751 //Ensure that test data are copied into apex.
5752 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5753 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5754
Roland Levillainf89cd092019-07-29 16:22:59 +01005755 // Ensure the module is correctly translated.
Jooyung Hana0503a52023-08-23 13:12:50 +09005756 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005757 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005758 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005759 prefix := "TARGET_"
5760 var builder strings.Builder
5761 data.Custom(&builder, name, prefix, "", data)
5762 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005763 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005764 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005765}
5766
Jooyung Hand48f3c32019-08-23 11:18:57 +09005767func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5768 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5769 apex {
5770 name: "myapex",
5771 key: "myapex.key",
5772 native_shared_libs: ["libfoo"],
5773 }
5774
5775 apex_key {
5776 name: "myapex.key",
5777 public_key: "testkey.avbpubkey",
5778 private_key: "testkey.pem",
5779 }
5780
5781 cc_library {
5782 name: "libfoo",
5783 stl: "none",
5784 system_shared_libs: [],
5785 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005786 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005787 }
5788 `)
5789 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5790 apex {
5791 name: "myapex",
5792 key: "myapex.key",
5793 java_libs: ["myjar"],
5794 }
5795
5796 apex_key {
5797 name: "myapex.key",
5798 public_key: "testkey.avbpubkey",
5799 private_key: "testkey.pem",
5800 }
5801
5802 java_library {
5803 name: "myjar",
5804 srcs: ["foo/bar/MyClass.java"],
5805 sdk_version: "none",
5806 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005807 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005808 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005809 }
5810 `)
5811}
5812
Bill Peckhama41a6962021-01-11 10:58:54 -08005813func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005814 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005815 apex {
5816 name: "myapex",
5817 key: "myapex.key",
5818 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005819 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005820 }
5821
5822 apex_key {
5823 name: "myapex.key",
5824 public_key: "testkey.avbpubkey",
5825 private_key: "testkey.pem",
5826 }
5827
5828 java_import {
5829 name: "myjavaimport",
5830 apex_available: ["myapex"],
5831 jars: ["my.jar"],
5832 compile_dex: true,
5833 }
5834 `)
5835
Jooyung Hana0503a52023-08-23 13:12:50 +09005836 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Bill Peckhama41a6962021-01-11 10:58:54 -08005837 apexRule := module.Rule("apexRule")
5838 copyCmds := apexRule.Args["copy_commands"]
5839 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5840}
5841
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005842func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005843 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005844 apex {
5845 name: "myapex",
5846 key: "myapex.key",
5847 apps: [
5848 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005849 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005850 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005851 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005852 }
5853
5854 apex_key {
5855 name: "myapex.key",
5856 public_key: "testkey.avbpubkey",
5857 private_key: "testkey.pem",
5858 }
5859
5860 android_app {
5861 name: "AppFoo",
5862 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005863 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005864 system_modules: "none",
Jiyong Park970c5242024-05-17 22:58:54 +00005865 use_embedded_native_libs: true,
Jiyong Park8be103b2019-11-08 15:53:48 +09005866 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005867 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005868 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005869 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005870
5871 android_app {
5872 name: "AppFooPriv",
5873 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005874 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005875 system_modules: "none",
5876 privileged: true,
Sam Delmerico15809f82023-05-15 17:21:47 -04005877 privapp_allowlist: "privapp_allowlist_com.android.AppFooPriv.xml",
Colin Cross094cde42020-02-15 10:38:00 -08005878 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005879 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005880 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005881
5882 cc_library_shared {
5883 name: "libjni",
5884 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005885 shared_libs: ["libfoo"],
5886 stl: "none",
5887 system_shared_libs: [],
5888 apex_available: [ "myapex" ],
5889 sdk_version: "current",
5890 }
5891
5892 cc_library_shared {
5893 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005894 stl: "none",
5895 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005896 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005897 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005898 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005899 `)
5900
Jooyung Hana0503a52023-08-23 13:12:50 +09005901 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005902 apexRule := module.Rule("apexRule")
5903 copyCmds := apexRule.Args["copy_commands"]
5904
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005905 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5906 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Andrei Onea580636b2022-08-17 16:53:46 +00005907 ensureContains(t, copyCmds, "image.apex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005908
Colin Crossaede88c2020-08-11 12:17:01 -07005909 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005910 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005911 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005912 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005913 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005914 // JNI libraries including transitive deps are
5915 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005916 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005917 // ... embedded inside APK (jnilibs.zip)
5918 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5919 // ... and not directly inside the APEX
5920 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5921 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04005922
5923 apexBundle := module.Module().(*apexBundle)
5924 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
5925 var builder strings.Builder
5926 data.Custom(&builder, apexBundle.Name(), "TARGET_", "", data)
5927 androidMk := builder.String()
5928 ensureContains(t, androidMk, "LOCAL_MODULE := AppFooPriv.myapex")
5929 ensureContains(t, androidMk, "LOCAL_MODULE := AppFoo.myapex")
5930 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFooPriv.apk")
5931 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFoo.apk")
5932 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := \\S+AppFooPriv.apk")
5933 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 +01005934}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005935
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005936func TestApexWithAppImportBuildId(t *testing.T) {
5937 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5938 for _, id := range invalidBuildIds {
5939 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5940 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5941 variables.BuildId = proptools.StringPtr(id)
5942 })
5943 testApexError(t, message, `apex {
5944 name: "myapex",
5945 key: "myapex.key",
5946 apps: ["AppFooPrebuilt"],
5947 updatable: false,
5948 }
5949
5950 apex_key {
5951 name: "myapex.key",
5952 public_key: "testkey.avbpubkey",
5953 private_key: "testkey.pem",
5954 }
5955
5956 android_app_import {
5957 name: "AppFooPrebuilt",
5958 apk: "PrebuiltAppFoo.apk",
5959 presigned: true,
5960 apex_available: ["myapex"],
5961 }
5962 `, fixture)
5963 }
5964}
5965
Dario Frenicde2a032019-10-27 00:29:22 +01005966func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005967 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005968 apex {
5969 name: "myapex",
5970 key: "myapex.key",
5971 apps: [
5972 "AppFooPrebuilt",
5973 "AppFooPrivPrebuilt",
5974 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005975 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005976 }
5977
5978 apex_key {
5979 name: "myapex.key",
5980 public_key: "testkey.avbpubkey",
5981 private_key: "testkey.pem",
5982 }
5983
5984 android_app_import {
5985 name: "AppFooPrebuilt",
5986 apk: "PrebuiltAppFoo.apk",
5987 presigned: true,
5988 dex_preopt: {
5989 enabled: false,
5990 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005991 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005992 }
5993
5994 android_app_import {
5995 name: "AppFooPrivPrebuilt",
5996 apk: "PrebuiltAppFooPriv.apk",
5997 privileged: true,
5998 presigned: true,
5999 dex_preopt: {
6000 enabled: false,
6001 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006002 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006003 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006004 }
6005 `)
6006
Jooyung Hana0503a52023-08-23 13:12:50 +09006007 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Frenicde2a032019-10-27 00:29:22 +01006008 apexRule := module.Rule("apexRule")
6009 copyCmds := apexRule.Args["copy_commands"]
6010
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006011 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6012 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006013}
6014
6015func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006016 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006017 apex {
6018 name: "myapex",
6019 key: "myapex.key",
6020 apps: [
6021 "AppFoo",
6022 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006023 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006024 }
6025
6026 apex_key {
6027 name: "myapex.key",
6028 public_key: "testkey.avbpubkey",
6029 private_key: "testkey.pem",
6030 }
6031
6032 android_app {
6033 name: "AppFoo",
6034 srcs: ["foo/bar/MyClass.java"],
6035 sdk_version: "none",
6036 system_modules: "none",
6037 apex_available: [ "myapex" ],
6038 }
6039
6040 android_app_import {
6041 name: "AppFoo",
6042 apk: "AppFooPrebuilt.apk",
6043 filename: "AppFooPrebuilt.apk",
6044 presigned: true,
6045 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006046 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006047 }
6048 `, withFiles(map[string][]byte{
6049 "AppFooPrebuilt.apk": nil,
6050 }))
6051
Jooyung Hana0503a52023-08-23 13:12:50 +09006052 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006053 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006054 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006055}
6056
Dario Freni6f3937c2019-12-20 22:58:03 +00006057func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006058 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006059 apex {
6060 name: "myapex",
6061 key: "myapex.key",
6062 apps: [
6063 "TesterHelpAppFoo",
6064 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006065 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006066 }
6067
6068 apex_key {
6069 name: "myapex.key",
6070 public_key: "testkey.avbpubkey",
6071 private_key: "testkey.pem",
6072 }
6073
6074 android_test_helper_app {
6075 name: "TesterHelpAppFoo",
6076 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006077 apex_available: [ "myapex" ],
Jihoon Kang85bc1932024-07-01 17:04:46 +00006078 sdk_version: "test_current",
Dario Freni6f3937c2019-12-20 22:58:03 +00006079 }
6080
6081 `)
6082
Jooyung Hana0503a52023-08-23 13:12:50 +09006083 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Freni6f3937c2019-12-20 22:58:03 +00006084 apexRule := module.Rule("apexRule")
6085 copyCmds := apexRule.Args["copy_commands"]
6086
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006087 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006088}
6089
Jooyung Han18020ea2019-11-13 10:50:48 +09006090func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6091 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006092 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006093 apex {
6094 name: "myapex",
6095 key: "myapex.key",
6096 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006097 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006098 }
6099
6100 apex_key {
6101 name: "myapex.key",
6102 public_key: "testkey.avbpubkey",
6103 private_key: "testkey.pem",
6104 }
6105
6106 apex {
6107 name: "otherapex",
6108 key: "myapex.key",
6109 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006110 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006111 }
6112
6113 cc_defaults {
6114 name: "libfoo-defaults",
6115 apex_available: ["otherapex"],
6116 }
6117
6118 cc_library {
6119 name: "libfoo",
6120 defaults: ["libfoo-defaults"],
6121 stl: "none",
6122 system_shared_libs: [],
6123 }`)
6124}
6125
Paul Duffine52e66f2020-03-30 17:54:29 +01006126func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006127 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006128 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006129 apex {
6130 name: "myapex",
6131 key: "myapex.key",
6132 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006133 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006134 }
6135
6136 apex_key {
6137 name: "myapex.key",
6138 public_key: "testkey.avbpubkey",
6139 private_key: "testkey.pem",
6140 }
6141
6142 apex {
6143 name: "otherapex",
6144 key: "otherapex.key",
6145 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006146 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006147 }
6148
6149 apex_key {
6150 name: "otherapex.key",
6151 public_key: "testkey.avbpubkey",
6152 private_key: "testkey.pem",
6153 }
6154
6155 cc_library {
6156 name: "libfoo",
6157 stl: "none",
6158 system_shared_libs: [],
6159 apex_available: ["otherapex"],
6160 }`)
Chan Wang490a6f92024-09-23 11:52:00 +00006161
6162 // 'apex_available' check is bypassed for /product apex with a specific prefix.
6163 // TODO: b/352818241 - Remove below two cases after APEX availability is enforced for /product APEXes.
6164 testApex(t, `
6165 apex {
6166 name: "com.sdv.myapex",
6167 key: "myapex.key",
6168 native_shared_libs: ["libfoo"],
6169 updatable: false,
6170 product_specific: true,
6171 }
6172
6173 apex_key {
6174 name: "myapex.key",
6175 public_key: "testkey.avbpubkey",
6176 private_key: "testkey.pem",
6177 }
6178
6179 apex {
6180 name: "com.any.otherapex",
6181 key: "otherapex.key",
6182 native_shared_libs: ["libfoo"],
6183 updatable: false,
6184 }
6185
6186 apex_key {
6187 name: "otherapex.key",
6188 public_key: "testkey.avbpubkey",
6189 private_key: "testkey.pem",
6190 }
6191
6192 cc_library {
6193 name: "libfoo",
6194 stl: "none",
6195 system_shared_libs: [],
6196 apex_available: ["com.any.otherapex"],
6197 product_specific: true,
6198 }`,
6199 android.FixtureMergeMockFs(android.MockFS{
6200 "system/sepolicy/apex/com.sdv.myapex-file_contexts": nil,
6201 "system/sepolicy/apex/com.any.otherapex-file_contexts": nil,
6202 }))
6203
6204 // 'apex_available' check is not bypassed for non-product apex with a specific prefix.
6205 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
6206 apex {
6207 name: "com.sdv.myapex",
6208 key: "myapex.key",
6209 native_shared_libs: ["libfoo"],
6210 updatable: false,
6211 }
6212
6213 apex_key {
6214 name: "myapex.key",
6215 public_key: "testkey.avbpubkey",
6216 private_key: "testkey.pem",
6217 }
6218
6219 apex {
6220 name: "com.any.otherapex",
6221 key: "otherapex.key",
6222 native_shared_libs: ["libfoo"],
6223 updatable: false,
6224 }
6225
6226 apex_key {
6227 name: "otherapex.key",
6228 public_key: "testkey.avbpubkey",
6229 private_key: "testkey.pem",
6230 }
6231
6232 cc_library {
6233 name: "libfoo",
6234 stl: "none",
6235 system_shared_libs: [],
6236 apex_available: ["com.any.otherapex"],
6237 }`,
6238 android.FixtureMergeMockFs(android.MockFS{
6239 "system/sepolicy/apex/com.sdv.myapex-file_contexts": nil,
6240 "system/sepolicy/apex/com.any.otherapex-file_contexts": nil,
6241 }))
Paul Duffine52e66f2020-03-30 17:54:29 +01006242}
Jiyong Park127b40b2019-09-30 16:04:35 +09006243
Paul Duffine52e66f2020-03-30 17:54:29 +01006244func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006245 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006246 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006247.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006248.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006249.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006250.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006251.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006252.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006253 apex {
6254 name: "myapex",
6255 key: "myapex.key",
6256 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006257 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006258 }
6259
6260 apex_key {
6261 name: "myapex.key",
6262 public_key: "testkey.avbpubkey",
6263 private_key: "testkey.pem",
6264 }
6265
Jiyong Park127b40b2019-09-30 16:04:35 +09006266 cc_library {
6267 name: "libfoo",
6268 stl: "none",
6269 shared_libs: ["libbar"],
6270 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006271 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006272 }
6273
6274 cc_library {
6275 name: "libbar",
6276 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006277 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006278 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006279 apex_available: ["myapex"],
6280 }
6281
6282 cc_library {
6283 name: "libbaz",
6284 stl: "none",
6285 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006286 }`)
Chan Wang490a6f92024-09-23 11:52:00 +00006287
6288 // 'apex_available' check is bypassed for /product apex with a specific prefix.
6289 // TODO: b/352818241 - Remove below two cases after APEX availability is enforced for /product APEXes.
6290 testApex(t, `
6291 apex {
6292 name: "com.sdv.myapex",
6293 key: "myapex.key",
6294 native_shared_libs: ["libfoo"],
6295 updatable: false,
6296 product_specific: true,
6297 }
6298
6299 apex_key {
6300 name: "myapex.key",
6301 public_key: "testkey.avbpubkey",
6302 private_key: "testkey.pem",
6303 }
6304
6305 cc_library {
6306 name: "libfoo",
6307 stl: "none",
6308 shared_libs: ["libbar"],
6309 system_shared_libs: [],
6310 apex_available: ["com.sdv.myapex"],
6311 product_specific: true,
6312 }
6313
6314 cc_library {
6315 name: "libbar",
6316 stl: "none",
6317 shared_libs: ["libbaz"],
6318 system_shared_libs: [],
6319 apex_available: ["com.sdv.myapex"],
6320 product_specific: true,
6321 }
6322
6323 cc_library {
6324 name: "libbaz",
6325 stl: "none",
6326 system_shared_libs: [],
6327 product_specific: true,
6328 }`,
6329 android.FixtureMergeMockFs(android.MockFS{
6330 "system/sepolicy/apex/com.sdv.myapex-file_contexts": nil,
6331 }))
6332
6333 // 'apex_available' check is not bypassed for non-product apex with a specific prefix.
6334 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.`, `
6335 apex {
6336 name: "com.sdv.myapex",
6337 key: "myapex.key",
6338 native_shared_libs: ["libfoo"],
6339 updatable: false,
6340 }
6341
6342 apex_key {
6343 name: "myapex.key",
6344 public_key: "testkey.avbpubkey",
6345 private_key: "testkey.pem",
6346 }
6347
6348 cc_library {
6349 name: "libfoo",
6350 stl: "none",
6351 shared_libs: ["libbar"],
6352 system_shared_libs: [],
6353 apex_available: ["com.sdv.myapex"],
6354 }
6355
6356 cc_library {
6357 name: "libbar",
6358 stl: "none",
6359 shared_libs: ["libbaz"],
6360 system_shared_libs: [],
6361 apex_available: ["com.sdv.myapex"],
6362 }
6363
6364 cc_library {
6365 name: "libbaz",
6366 stl: "none",
6367 system_shared_libs: [],
6368 }`,
6369 android.FixtureMergeMockFs(android.MockFS{
6370 "system/sepolicy/apex/com.sdv.myapex-file_contexts": nil,
6371 }))
Paul Duffine52e66f2020-03-30 17:54:29 +01006372}
Jiyong Park127b40b2019-09-30 16:04:35 +09006373
Liz Kammer5f108fa2023-05-11 14:33:17 -04006374func TestApexAvailable_IndirectStaticDep(t *testing.T) {
6375 testApex(t, `
6376 apex {
6377 name: "myapex",
6378 key: "myapex.key",
6379 native_shared_libs: ["libfoo"],
6380 updatable: false,
6381 }
6382
6383 apex_key {
6384 name: "myapex.key",
6385 public_key: "testkey.avbpubkey",
6386 private_key: "testkey.pem",
6387 }
6388
6389 cc_library {
6390 name: "libfoo",
6391 stl: "none",
6392 static_libs: ["libbar"],
6393 system_shared_libs: [],
6394 apex_available: ["myapex"],
6395 }
6396
6397 cc_library {
6398 name: "libbar",
6399 stl: "none",
6400 shared_libs: ["libbaz"],
6401 system_shared_libs: [],
6402 apex_available: ["myapex"],
6403 }
6404
6405 cc_library {
6406 name: "libbaz",
6407 stl: "none",
6408 system_shared_libs: [],
6409 }`)
6410
6411 testApexError(t, `requires "libbar" that doesn't list the APEX under 'apex_available'.`, `
6412 apex {
6413 name: "myapex",
6414 key: "myapex.key",
6415 native_shared_libs: ["libfoo"],
6416 updatable: false,
6417 }
6418
6419 apex_key {
6420 name: "myapex.key",
6421 public_key: "testkey.avbpubkey",
6422 private_key: "testkey.pem",
6423 }
6424
6425 cc_library {
6426 name: "libfoo",
6427 stl: "none",
6428 static_libs: ["libbar"],
6429 system_shared_libs: [],
6430 apex_available: ["myapex"],
6431 }
6432
6433 cc_library {
6434 name: "libbar",
6435 stl: "none",
6436 system_shared_libs: [],
6437 }`)
6438}
6439
Paul Duffine52e66f2020-03-30 17:54:29 +01006440func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006441 testApexError(t, "\"otherapex\" is not a valid module name", `
6442 apex {
6443 name: "myapex",
6444 key: "myapex.key",
6445 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006446 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006447 }
6448
6449 apex_key {
6450 name: "myapex.key",
6451 public_key: "testkey.avbpubkey",
6452 private_key: "testkey.pem",
6453 }
6454
6455 cc_library {
6456 name: "libfoo",
6457 stl: "none",
6458 system_shared_libs: [],
6459 apex_available: ["otherapex"],
6460 }`)
6461
Paul Duffine52e66f2020-03-30 17:54:29 +01006462 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006463 apex {
6464 name: "myapex",
6465 key: "myapex.key",
6466 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006467 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006468 }
6469
6470 apex_key {
6471 name: "myapex.key",
6472 public_key: "testkey.avbpubkey",
6473 private_key: "testkey.pem",
6474 }
6475
6476 cc_library {
6477 name: "libfoo",
6478 stl: "none",
6479 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006480 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006481 apex_available: ["myapex"],
6482 }
6483
6484 cc_library {
6485 name: "libbar",
6486 stl: "none",
6487 system_shared_libs: [],
6488 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006489 }
6490
6491 cc_library {
6492 name: "libbaz",
6493 stl: "none",
6494 system_shared_libs: [],
6495 stubs: {
6496 versions: ["10", "20", "30"],
6497 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006498 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006499}
Jiyong Park127b40b2019-09-30 16:04:35 +09006500
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006501func TestApexAvailable_ApexAvailableNameWithVersionCodeError(t *testing.T) {
6502 t.Run("negative variant_version produces error", func(t *testing.T) {
6503 testApexError(t, "expected an integer between 0-9; got -1", `
6504 apex {
6505 name: "myapex",
6506 key: "myapex.key",
6507 apex_available_name: "com.android.foo",
6508 variant_version: "-1",
6509 updatable: false,
6510 }
6511 apex_key {
6512 name: "myapex.key",
6513 public_key: "testkey.avbpubkey",
6514 private_key: "testkey.pem",
6515 }
6516 `)
6517 })
6518
6519 t.Run("variant_version greater than 9 produces error", func(t *testing.T) {
6520 testApexError(t, "expected an integer between 0-9; got 10", `
6521 apex {
6522 name: "myapex",
6523 key: "myapex.key",
6524 apex_available_name: "com.android.foo",
6525 variant_version: "10",
6526 updatable: false,
6527 }
6528 apex_key {
6529 name: "myapex.key",
6530 public_key: "testkey.avbpubkey",
6531 private_key: "testkey.pem",
6532 }
6533 `)
6534 })
6535}
6536
6537func TestApexAvailable_ApexAvailableNameWithVersionCode(t *testing.T) {
6538 context := android.GroupFixturePreparers(
6539 android.PrepareForIntegrationTestWithAndroid,
6540 PrepareForTestWithApexBuildComponents,
6541 android.FixtureMergeMockFs(android.MockFS{
6542 "system/sepolicy/apex/foo-file_contexts": nil,
6543 "system/sepolicy/apex/bar-file_contexts": nil,
6544 }),
6545 )
6546 result := context.RunTestWithBp(t, `
6547 apex {
6548 name: "foo",
6549 key: "myapex.key",
6550 apex_available_name: "com.android.foo",
6551 variant_version: "0",
6552 updatable: false,
6553 }
6554 apex {
6555 name: "bar",
6556 key: "myapex.key",
6557 apex_available_name: "com.android.foo",
6558 variant_version: "3",
6559 updatable: false,
6560 }
6561 apex_key {
6562 name: "myapex.key",
6563 public_key: "testkey.avbpubkey",
6564 private_key: "testkey.pem",
6565 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006566 override_apex {
6567 name: "myoverrideapex",
6568 base: "bar",
6569 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006570 `)
6571
Jooyung Hana0503a52023-08-23 13:12:50 +09006572 fooManifestRule := result.ModuleForTests("foo", "android_common_foo").Rule("apexManifestRule")
Alyssa Ketpreechasawat3a6eced2024-08-22 15:09:16 +00006573 fooExpectedDefaultVersion := testDefaultUpdatableModuleVersion
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006574 fooActualDefaultVersion := fooManifestRule.Args["default_version"]
6575 if fooActualDefaultVersion != fooExpectedDefaultVersion {
6576 t.Errorf("expected to find defaultVersion %q; got %q", fooExpectedDefaultVersion, fooActualDefaultVersion)
6577 }
6578
Jooyung Hana0503a52023-08-23 13:12:50 +09006579 barManifestRule := result.ModuleForTests("bar", "android_common_bar").Rule("apexManifestRule")
Alyssa Ketpreechasawat3a6eced2024-08-22 15:09:16 +00006580 defaultVersionInt, _ := strconv.Atoi(testDefaultUpdatableModuleVersion)
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006581 barExpectedDefaultVersion := fmt.Sprint(defaultVersionInt + 3)
6582 barActualDefaultVersion := barManifestRule.Args["default_version"]
6583 if barActualDefaultVersion != barExpectedDefaultVersion {
6584 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6585 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006586
Spandan Das50801e22024-05-13 18:29:45 +00006587 overrideBarManifestRule := result.ModuleForTests("bar", "android_common_myoverrideapex_myoverrideapex").Rule("apexManifestRule")
Sam Delmerico419f9a32023-07-21 12:00:13 -04006588 overrideBarActualDefaultVersion := overrideBarManifestRule.Args["default_version"]
6589 if overrideBarActualDefaultVersion != barExpectedDefaultVersion {
6590 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6591 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006592}
6593
Sam Delmericoca816532023-06-02 14:09:50 -04006594func TestApexAvailable_ApexAvailableName(t *testing.T) {
6595 t.Run("using name of apex that sets apex_available_name is not allowed", func(t *testing.T) {
6596 testApexError(t, "Consider adding \"myapex\" to 'apex_available' property of \"AppFoo\"", `
6597 apex {
6598 name: "myapex_sminus",
6599 key: "myapex.key",
6600 apps: ["AppFoo"],
6601 apex_available_name: "myapex",
6602 updatable: false,
6603 }
6604 apex {
6605 name: "myapex",
6606 key: "myapex.key",
6607 apps: ["AppFoo"],
6608 updatable: false,
6609 }
6610 apex_key {
6611 name: "myapex.key",
6612 public_key: "testkey.avbpubkey",
6613 private_key: "testkey.pem",
6614 }
6615 android_app {
6616 name: "AppFoo",
6617 srcs: ["foo/bar/MyClass.java"],
6618 sdk_version: "none",
6619 system_modules: "none",
6620 apex_available: [ "myapex_sminus" ],
6621 }`,
6622 android.FixtureMergeMockFs(android.MockFS{
6623 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6624 }),
6625 )
6626 })
6627
6628 t.Run("apex_available_name allows module to be used in two different apexes", func(t *testing.T) {
6629 testApex(t, `
6630 apex {
6631 name: "myapex_sminus",
6632 key: "myapex.key",
6633 apps: ["AppFoo"],
6634 apex_available_name: "myapex",
6635 updatable: false,
6636 }
6637 apex {
6638 name: "myapex",
6639 key: "myapex.key",
6640 apps: ["AppFoo"],
6641 updatable: false,
6642 }
6643 apex_key {
6644 name: "myapex.key",
6645 public_key: "testkey.avbpubkey",
6646 private_key: "testkey.pem",
6647 }
6648 android_app {
6649 name: "AppFoo",
6650 srcs: ["foo/bar/MyClass.java"],
6651 sdk_version: "none",
6652 system_modules: "none",
6653 apex_available: [ "myapex" ],
6654 }`,
6655 android.FixtureMergeMockFs(android.MockFS{
6656 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6657 }),
6658 )
6659 })
6660
6661 t.Run("override_apexes work with apex_available_name", func(t *testing.T) {
6662 testApex(t, `
6663 override_apex {
6664 name: "myoverrideapex_sminus",
6665 base: "myapex_sminus",
6666 key: "myapex.key",
6667 apps: ["AppFooOverride"],
6668 }
6669 override_apex {
6670 name: "myoverrideapex",
6671 base: "myapex",
6672 key: "myapex.key",
6673 apps: ["AppFooOverride"],
6674 }
6675 apex {
6676 name: "myapex_sminus",
6677 key: "myapex.key",
6678 apps: ["AppFoo"],
6679 apex_available_name: "myapex",
6680 updatable: false,
6681 }
6682 apex {
6683 name: "myapex",
6684 key: "myapex.key",
6685 apps: ["AppFoo"],
6686 updatable: false,
6687 }
6688 apex_key {
6689 name: "myapex.key",
6690 public_key: "testkey.avbpubkey",
6691 private_key: "testkey.pem",
6692 }
6693 android_app {
6694 name: "AppFooOverride",
6695 srcs: ["foo/bar/MyClass.java"],
6696 sdk_version: "none",
6697 system_modules: "none",
6698 apex_available: [ "myapex" ],
6699 }
6700 android_app {
6701 name: "AppFoo",
6702 srcs: ["foo/bar/MyClass.java"],
6703 sdk_version: "none",
6704 system_modules: "none",
6705 apex_available: [ "myapex" ],
6706 }`,
6707 android.FixtureMergeMockFs(android.MockFS{
6708 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6709 }),
6710 )
6711 })
6712}
6713
6714func TestApexAvailable_ApexAvailableNameWithOverrides(t *testing.T) {
6715 context := android.GroupFixturePreparers(
6716 android.PrepareForIntegrationTestWithAndroid,
6717 PrepareForTestWithApexBuildComponents,
6718 java.PrepareForTestWithDexpreopt,
6719 android.FixtureMergeMockFs(android.MockFS{
6720 "system/sepolicy/apex/myapex-file_contexts": nil,
6721 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6722 }),
6723 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6724 variables.BuildId = proptools.StringPtr("buildid")
6725 }),
6726 )
6727 context.RunTestWithBp(t, `
6728 override_apex {
6729 name: "myoverrideapex_sminus",
6730 base: "myapex_sminus",
6731 }
6732 override_apex {
6733 name: "myoverrideapex",
6734 base: "myapex",
6735 }
6736 apex {
6737 name: "myapex",
6738 key: "myapex.key",
6739 apps: ["AppFoo"],
6740 updatable: false,
6741 }
6742 apex {
6743 name: "myapex_sminus",
6744 apex_available_name: "myapex",
6745 key: "myapex.key",
6746 apps: ["AppFoo_sminus"],
6747 updatable: false,
6748 }
6749 apex_key {
6750 name: "myapex.key",
6751 public_key: "testkey.avbpubkey",
6752 private_key: "testkey.pem",
6753 }
6754 android_app {
6755 name: "AppFoo",
6756 srcs: ["foo/bar/MyClass.java"],
6757 sdk_version: "none",
6758 system_modules: "none",
6759 apex_available: [ "myapex" ],
6760 }
6761 android_app {
6762 name: "AppFoo_sminus",
6763 srcs: ["foo/bar/MyClass.java"],
6764 sdk_version: "none",
6765 min_sdk_version: "29",
6766 system_modules: "none",
6767 apex_available: [ "myapex" ],
6768 }`)
6769}
6770
Jiyong Park89e850a2020-04-07 16:37:39 +09006771func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006772 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006773 apex {
6774 name: "myapex",
6775 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006776 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006777 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006778 }
6779
6780 apex_key {
6781 name: "myapex.key",
6782 public_key: "testkey.avbpubkey",
6783 private_key: "testkey.pem",
6784 }
6785
6786 cc_library {
6787 name: "libfoo",
6788 stl: "none",
6789 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006790 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006791 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006792 }
6793
6794 cc_library {
6795 name: "libfoo2",
6796 stl: "none",
6797 system_shared_libs: [],
6798 shared_libs: ["libbaz"],
6799 apex_available: ["//apex_available:platform"],
6800 }
6801
6802 cc_library {
6803 name: "libbar",
6804 stl: "none",
6805 system_shared_libs: [],
6806 apex_available: ["myapex"],
6807 }
6808
6809 cc_library {
6810 name: "libbaz",
6811 stl: "none",
6812 system_shared_libs: [],
6813 apex_available: ["myapex"],
6814 stubs: {
6815 versions: ["1"],
6816 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006817 }`)
6818
Jiyong Park89e850a2020-04-07 16:37:39 +09006819 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6820 // because it depends on libbar which isn't available to platform
6821 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6822 if libfoo.NotAvailableForPlatform() != true {
6823 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6824 }
6825
6826 // libfoo2 however can be available to platform because it depends on libbaz which provides
6827 // stubs
6828 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6829 if libfoo2.NotAvailableForPlatform() == true {
6830 t.Errorf("%q should be available to platform", libfoo2.String())
6831 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006832}
Jiyong Parka90ca002019-10-07 15:47:24 +09006833
Paul Duffine52e66f2020-03-30 17:54:29 +01006834func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006835 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006836 apex {
6837 name: "myapex",
6838 key: "myapex.key",
6839 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006840 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006841 }
6842
6843 apex_key {
6844 name: "myapex.key",
6845 public_key: "testkey.avbpubkey",
6846 private_key: "testkey.pem",
6847 }
6848
6849 cc_library {
6850 name: "libfoo",
6851 stl: "none",
6852 system_shared_libs: [],
6853 apex_available: ["myapex"],
6854 static: {
6855 apex_available: ["//apex_available:platform"],
6856 },
6857 }`)
6858
Jiyong Park89e850a2020-04-07 16:37:39 +09006859 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6860 if libfooShared.NotAvailableForPlatform() != true {
6861 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6862 }
6863 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6864 if libfooStatic.NotAvailableForPlatform() != false {
6865 t.Errorf("%q should be available to platform", libfooStatic.String())
6866 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006867}
6868
Jooyung Han9a419e22024-08-16 17:14:21 +09006869func TestApexAvailable_PrefixMatch(t *testing.T) {
6870
6871 for _, tc := range []struct {
6872 name string
6873 apexAvailable string
6874 expectedError string
6875 }{
6876 {
6877 name: "prefix matches correctly",
6878 apexAvailable: "com.foo.*",
6879 },
6880 {
6881 name: "prefix doesn't match",
6882 apexAvailable: "com.bar.*",
6883 expectedError: `Consider .* "com.foo\.\*"`,
6884 },
6885 {
6886 name: "short prefix",
6887 apexAvailable: "com.*",
6888 expectedError: "requires two or more components",
6889 },
6890 {
6891 name: "wildcard not in the end",
6892 apexAvailable: "com.*.foo",
6893 expectedError: "should end with .*",
6894 },
6895 {
6896 name: "wildcard in the middle",
6897 apexAvailable: "com.foo*.*",
6898 expectedError: "not allowed in the middle",
6899 },
6900 {
6901 name: "hint with prefix pattern",
6902 apexAvailable: "//apex_available:platform",
6903 expectedError: "Consider adding \"com.foo.bar\" or \"com.foo.*\"",
6904 },
6905 } {
6906 t.Run(tc.name, func(t *testing.T) {
6907 errorHandler := android.FixtureExpectsNoErrors
6908 if tc.expectedError != "" {
6909 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(tc.expectedError)
6910 }
6911 context := android.GroupFixturePreparers(
6912 prepareForApexTest,
6913 android.FixtureMergeMockFs(android.MockFS{
6914 "system/sepolicy/apex/com.foo.bar-file_contexts": nil,
6915 }),
6916 ).ExtendWithErrorHandler(errorHandler)
6917
6918 context.RunTestWithBp(t, `
6919 apex {
6920 name: "com.foo.bar",
6921 key: "myapex.key",
6922 native_shared_libs: ["libfoo"],
6923 updatable: false,
6924 }
6925
6926 apex_key {
6927 name: "myapex.key",
6928 public_key: "testkey.avbpubkey",
6929 private_key: "testkey.pem",
6930 }
6931
6932 cc_library {
6933 name: "libfoo",
6934 stl: "none",
6935 system_shared_libs: [],
6936 apex_available: ["`+tc.apexAvailable+`"],
6937 }`)
6938 })
6939 }
6940 testApexError(t, `Consider adding "com.foo" to`, `
6941 apex {
6942 name: "com.foo", // too short for a partner apex
6943 key: "myapex.key",
6944 native_shared_libs: ["libfoo"],
6945 updatable: false,
6946 }
6947
6948 apex_key {
6949 name: "myapex.key",
6950 public_key: "testkey.avbpubkey",
6951 private_key: "testkey.pem",
6952 }
6953
6954 cc_library {
6955 name: "libfoo",
6956 stl: "none",
6957 system_shared_libs: [],
6958 }
6959 `)
6960}
6961
Jiyong Park5d790c32019-11-15 18:40:32 +09006962func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006963 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006964 apex {
6965 name: "myapex",
6966 key: "myapex.key",
6967 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006968 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006969 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006970 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006971 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006972 }
6973
6974 override_apex {
6975 name: "override_myapex",
6976 base: "myapex",
6977 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006978 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006979 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006980 overrides: ["unknownapex"],
Jesse Melhuishec60e252024-03-29 19:08:20 +00006981 compile_multilib: "first",
6982 multilib: {
6983 lib32: {
6984 native_shared_libs: ["mylib32"],
6985 },
6986 lib64: {
6987 native_shared_libs: ["mylib64"],
6988 },
6989 },
Baligh Uddin004d7172020-02-19 21:29:28 -08006990 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006991 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006992 key: "mynewapex.key",
6993 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006994 }
6995
6996 apex_key {
6997 name: "myapex.key",
6998 public_key: "testkey.avbpubkey",
6999 private_key: "testkey.pem",
7000 }
7001
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07007002 apex_key {
7003 name: "mynewapex.key",
7004 public_key: "testkey2.avbpubkey",
7005 private_key: "testkey2.pem",
7006 }
7007
7008 android_app_certificate {
7009 name: "myapex.certificate",
7010 certificate: "testkey",
7011 }
7012
Jiyong Park5d790c32019-11-15 18:40:32 +09007013 android_app {
7014 name: "app",
7015 srcs: ["foo/bar/MyClass.java"],
7016 package_name: "foo",
7017 sdk_version: "none",
7018 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007019 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09007020 }
7021
7022 override_android_app {
7023 name: "override_app",
7024 base: "app",
7025 package_name: "bar",
7026 }
markchien7c803b82021-08-26 22:10:06 +08007027
7028 bpf {
7029 name: "bpf",
7030 srcs: ["bpf.c"],
7031 }
7032
7033 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08007034 name: "overrideBpf",
7035 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08007036 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07007037
7038 prebuilt_etc {
7039 name: "myetc",
7040 src: "myprebuilt",
7041 }
7042
7043 prebuilt_etc {
7044 name: "override_myetc",
7045 src: "override_myprebuilt",
7046 }
Jesse Melhuishec60e252024-03-29 19:08:20 +00007047
7048 cc_library {
7049 name: "mylib32",
7050 apex_available: [ "myapex" ],
7051 }
7052
7053 cc_library {
7054 name: "mylib64",
7055 apex_available: [ "myapex" ],
7056 }
Jiyong Park20bacab2020-03-03 11:45:41 +09007057 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09007058
Jooyung Hana0503a52023-08-23 13:12:50 +09007059 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(android.OverridableModule)
Spandan Das50801e22024-05-13 18:29:45 +00007060 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex").Module().(android.OverridableModule)
Jiyong Park317645e2019-12-05 13:20:58 +09007061 if originalVariant.GetOverriddenBy() != "" {
7062 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
7063 }
7064 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
7065 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
7066 }
7067
Spandan Das50801e22024-05-13 18:29:45 +00007068 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex")
Jiyong Park5d790c32019-11-15 18:40:32 +09007069 apexRule := module.Rule("apexRule")
7070 copyCmds := apexRule.Args["copy_commands"]
7071
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007072 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
7073 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007074
markchien7c803b82021-08-26 22:10:06 +08007075 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08007076 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08007077
Daniel Norman5a3ce132021-08-26 15:44:43 -07007078 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
7079 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
7080
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007081 apexBundle := module.Module().(*apexBundle)
7082 name := apexBundle.Name()
7083 if name != "override_myapex" {
7084 t.Errorf("name should be \"override_myapex\", but was %q", name)
7085 }
7086
Baligh Uddin004d7172020-02-19 21:29:28 -08007087 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
7088 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
7089 }
7090
Jiyong Park20bacab2020-03-03 11:45:41 +09007091 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07007092 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07007093 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
7094
7095 signApkRule := module.Rule("signapk")
7096 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09007097
Colin Crossaa255532020-07-03 13:18:24 -07007098 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007099 var builder strings.Builder
7100 data.Custom(&builder, name, "TARGET_", "", data)
7101 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007102 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
7103 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007104 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08007105 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007106 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08007107 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09007108 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007109 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09007110}
7111
Albert Martineefabcf2022-03-21 20:11:16 +00007112func TestMinSdkVersionOverride(t *testing.T) {
7113 // Override from 29 to 31
7114 minSdkOverride31 := "31"
7115 ctx := testApex(t, `
7116 apex {
7117 name: "myapex",
7118 key: "myapex.key",
7119 native_shared_libs: ["mylib"],
7120 updatable: true,
7121 min_sdk_version: "29"
7122 }
7123
7124 override_apex {
7125 name: "override_myapex",
7126 base: "myapex",
7127 logging_parent: "com.foo.bar",
7128 package_name: "test.overridden.package"
7129 }
7130
7131 apex_key {
7132 name: "myapex.key",
7133 public_key: "testkey.avbpubkey",
7134 private_key: "testkey.pem",
7135 }
7136
7137 cc_library {
7138 name: "mylib",
7139 srcs: ["mylib.cpp"],
7140 runtime_libs: ["libbar"],
7141 system_shared_libs: [],
7142 stl: "none",
7143 apex_available: [ "myapex" ],
7144 min_sdk_version: "apex_inherit"
7145 }
7146
7147 cc_library {
7148 name: "libbar",
7149 srcs: ["mylib.cpp"],
7150 system_shared_libs: [],
7151 stl: "none",
7152 apex_available: [ "myapex" ],
7153 min_sdk_version: "apex_inherit"
7154 }
7155
7156 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
7157
Jooyung Hana0503a52023-08-23 13:12:50 +09007158 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007159 copyCmds := apexRule.Args["copy_commands"]
7160
7161 // Ensure that direct non-stubs dep is always included
7162 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7163
7164 // Ensure that runtime_libs dep in included
7165 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7166
7167 // Ensure libraries target overridden min_sdk_version value
7168 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7169}
7170
7171func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
7172 // Attempt to override from 31 to 29, should be a NOOP
7173 minSdkOverride29 := "29"
7174 ctx := testApex(t, `
7175 apex {
7176 name: "myapex",
7177 key: "myapex.key",
7178 native_shared_libs: ["mylib"],
7179 updatable: true,
7180 min_sdk_version: "31"
7181 }
7182
7183 override_apex {
7184 name: "override_myapex",
7185 base: "myapex",
7186 logging_parent: "com.foo.bar",
7187 package_name: "test.overridden.package"
7188 }
7189
7190 apex_key {
7191 name: "myapex.key",
7192 public_key: "testkey.avbpubkey",
7193 private_key: "testkey.pem",
7194 }
7195
7196 cc_library {
7197 name: "mylib",
7198 srcs: ["mylib.cpp"],
7199 runtime_libs: ["libbar"],
7200 system_shared_libs: [],
7201 stl: "none",
7202 apex_available: [ "myapex" ],
7203 min_sdk_version: "apex_inherit"
7204 }
7205
7206 cc_library {
7207 name: "libbar",
7208 srcs: ["mylib.cpp"],
7209 system_shared_libs: [],
7210 stl: "none",
7211 apex_available: [ "myapex" ],
7212 min_sdk_version: "apex_inherit"
7213 }
7214
7215 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
7216
Jooyung Hana0503a52023-08-23 13:12:50 +09007217 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007218 copyCmds := apexRule.Args["copy_commands"]
7219
7220 // Ensure that direct non-stubs dep is always included
7221 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7222
7223 // Ensure that runtime_libs dep in included
7224 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7225
7226 // Ensure libraries target the original min_sdk_version value rather than the overridden
7227 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7228}
7229
Jooyung Han214bf372019-11-12 13:03:50 +09007230func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007231 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09007232 apex {
7233 name: "myapex",
7234 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007235 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09007236 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09007237 }
7238
7239 apex_key {
7240 name: "myapex.key",
7241 public_key: "testkey.avbpubkey",
7242 private_key: "testkey.pem",
7243 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007244
7245 cc_library {
7246 name: "mylib",
7247 srcs: ["mylib.cpp"],
7248 stl: "libc++",
7249 system_shared_libs: [],
7250 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09007251 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007252 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007253 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09007254
Jooyung Hana0503a52023-08-23 13:12:50 +09007255 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +09007256 args := module.Rule("apexRule").Args
7257 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00007258 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007259
7260 // The copies of the libraries in the apex should have one more dependency than
7261 // the ones outside the apex, namely the unwinder. Ideally we should check
7262 // the dependency names directly here but for some reason the names are blank in
7263 // this test.
7264 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07007265 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007266 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
7267 if len(apexImplicits) != len(nonApexImplicits)+1 {
7268 t.Errorf("%q missing unwinder dep", lib)
7269 }
7270 }
Jooyung Han214bf372019-11-12 13:03:50 +09007271}
7272
Paul Duffine05480a2021-03-08 15:07:14 +00007273var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01007274 "api/current.txt": nil,
7275 "api/removed.txt": nil,
7276 "api/system-current.txt": nil,
7277 "api/system-removed.txt": nil,
7278 "api/test-current.txt": nil,
7279 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01007280
Anton Hanssondff2c782020-12-21 17:10:01 +00007281 "100/public/api/foo.txt": nil,
7282 "100/public/api/foo-removed.txt": nil,
7283 "100/system/api/foo.txt": nil,
7284 "100/system/api/foo-removed.txt": nil,
7285
Paul Duffineedc5d52020-06-12 17:46:39 +01007286 // For java_sdk_library_import
7287 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01007288}
7289
Jooyung Han58f26ab2019-12-18 15:34:32 +09007290func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007291 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09007292 apex {
7293 name: "myapex",
7294 key: "myapex.key",
7295 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007296 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09007297 }
7298
7299 apex_key {
7300 name: "myapex.key",
7301 public_key: "testkey.avbpubkey",
7302 private_key: "testkey.pem",
7303 }
7304
7305 java_sdk_library {
7306 name: "foo",
7307 srcs: ["a.java"],
7308 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007309 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09007310 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007311
7312 prebuilt_apis {
7313 name: "sdk",
7314 api_dirs: ["100"],
7315 }
Paul Duffin9b879592020-05-26 13:21:35 +01007316 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09007317
7318 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007319 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09007320 "javalib/foo.jar",
7321 "etc/permissions/foo.xml",
7322 })
7323 // Permission XML should point to the activated path of impl jar of java_sdk_library
Paul Duffin1816cde2024-04-10 10:58:21 +01007324 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Output("foo.xml")
7325 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7326 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
Jooyung Han58f26ab2019-12-18 15:34:32 +09007327}
7328
Spandan Das3ee19692024-06-19 04:47:40 +00007329func TestJavaSDKLibraryOverrideApexes(t *testing.T) {
7330 ctx := testApex(t, `
7331 override_apex {
7332 name: "mycompanyapex",
7333 base: "myapex",
7334 }
7335 apex {
7336 name: "myapex",
7337 key: "myapex.key",
7338 java_libs: ["foo"],
7339 updatable: false,
7340 }
7341
7342 apex_key {
7343 name: "myapex.key",
7344 public_key: "testkey.avbpubkey",
7345 private_key: "testkey.pem",
7346 }
7347
7348 java_sdk_library {
7349 name: "foo",
7350 srcs: ["a.java"],
7351 api_packages: ["foo"],
7352 apex_available: [ "myapex" ],
7353 }
7354
7355 prebuilt_apis {
7356 name: "sdk",
7357 api_dirs: ["100"],
7358 }
7359 `, withFiles(filesForSdkLibrary))
7360
7361 // Permission XML should point to the activated path of impl jar of java_sdk_library.
7362 // Since override variants (com.mycompany.android.foo) are installed in the same package as the overridden variant
7363 // (com.android.foo), the filepath should not contain override apex name.
7364 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_mycompanyapex").Output("foo.xml")
7365 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7366 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
7367}
7368
Paul Duffin9b879592020-05-26 13:21:35 +01007369func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007370 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007371 apex {
7372 name: "myapex",
7373 key: "myapex.key",
7374 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007375 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007376 }
7377
7378 apex_key {
7379 name: "myapex.key",
7380 public_key: "testkey.avbpubkey",
7381 private_key: "testkey.pem",
7382 }
7383
7384 java_sdk_library {
7385 name: "foo",
7386 srcs: ["a.java"],
7387 api_packages: ["foo"],
7388 apex_available: ["myapex"],
7389 sdk_version: "none",
7390 system_modules: "none",
7391 }
7392
7393 java_library {
7394 name: "bar",
7395 srcs: ["a.java"],
Jihoon Kang28c96572024-09-11 23:44:44 +00007396 libs: ["foo.impl"],
Paul Duffin9b879592020-05-26 13:21:35 +01007397 apex_available: ["myapex"],
7398 sdk_version: "none",
7399 system_modules: "none",
7400 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007401
7402 prebuilt_apis {
7403 name: "sdk",
7404 api_dirs: ["100"],
7405 }
Paul Duffin9b879592020-05-26 13:21:35 +01007406 `, withFiles(filesForSdkLibrary))
7407
7408 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007409 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007410 "javalib/bar.jar",
7411 "javalib/foo.jar",
7412 "etc/permissions/foo.xml",
7413 })
7414
7415 // The bar library should depend on the implementation jar.
7416 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kanga3a05462024-04-05 00:36:44 +00007417 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007418 t.Errorf("expected %q, found %#q", expected, actual)
7419 }
7420}
7421
7422func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007423 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007424 apex {
7425 name: "myapex",
7426 key: "myapex.key",
7427 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007428 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007429 }
7430
7431 apex_key {
7432 name: "myapex.key",
7433 public_key: "testkey.avbpubkey",
7434 private_key: "testkey.pem",
7435 }
7436
7437 java_sdk_library {
7438 name: "foo",
7439 srcs: ["a.java"],
7440 api_packages: ["foo"],
7441 apex_available: ["myapex"],
7442 sdk_version: "none",
7443 system_modules: "none",
7444 }
7445
7446 java_library {
7447 name: "bar",
7448 srcs: ["a.java"],
Jihoon Kang28c96572024-09-11 23:44:44 +00007449 libs: ["foo.stubs"],
Paul Duffin9b879592020-05-26 13:21:35 +01007450 sdk_version: "none",
7451 system_modules: "none",
7452 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007453
7454 prebuilt_apis {
7455 name: "sdk",
7456 api_dirs: ["100"],
7457 }
Paul Duffin9b879592020-05-26 13:21:35 +01007458 `, withFiles(filesForSdkLibrary))
7459
7460 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007461 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007462 "javalib/foo.jar",
7463 "etc/permissions/foo.xml",
7464 })
7465
7466 // The bar library should depend on the stubs jar.
7467 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007468 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007469 t.Errorf("expected %q, found %#q", expected, actual)
7470 }
7471}
7472
Paul Duffineedc5d52020-06-12 17:46:39 +01007473func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007474 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00007475 prebuilt_apis {
7476 name: "sdk",
7477 api_dirs: ["100"],
7478 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01007479 withFiles(map[string][]byte{
7480 "apex/a.java": nil,
7481 "apex/apex_manifest.json": nil,
7482 "apex/Android.bp": []byte(`
7483 package {
7484 default_visibility: ["//visibility:private"],
7485 }
7486
7487 apex {
7488 name: "myapex",
7489 key: "myapex.key",
7490 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007491 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007492 }
7493
7494 apex_key {
7495 name: "myapex.key",
7496 public_key: "testkey.avbpubkey",
7497 private_key: "testkey.pem",
7498 }
7499
7500 java_library {
7501 name: "bar",
7502 srcs: ["a.java"],
Jihoon Kang28c96572024-09-11 23:44:44 +00007503 libs: ["foo.impl"],
Paul Duffineedc5d52020-06-12 17:46:39 +01007504 apex_available: ["myapex"],
7505 sdk_version: "none",
7506 system_modules: "none",
7507 }
7508`),
7509 "source/a.java": nil,
7510 "source/api/current.txt": nil,
7511 "source/api/removed.txt": nil,
7512 "source/Android.bp": []byte(`
7513 package {
7514 default_visibility: ["//visibility:private"],
7515 }
7516
7517 java_sdk_library {
7518 name: "foo",
7519 visibility: ["//apex"],
7520 srcs: ["a.java"],
7521 api_packages: ["foo"],
7522 apex_available: ["myapex"],
7523 sdk_version: "none",
7524 system_modules: "none",
7525 public: {
7526 enabled: true,
7527 },
7528 }
7529`),
7530 "prebuilt/a.jar": nil,
7531 "prebuilt/Android.bp": []byte(`
7532 package {
7533 default_visibility: ["//visibility:private"],
7534 }
7535
7536 java_sdk_library_import {
7537 name: "foo",
7538 visibility: ["//apex", "//source"],
7539 apex_available: ["myapex"],
7540 prefer: true,
7541 public: {
7542 jars: ["a.jar"],
7543 },
7544 }
7545`),
Anton Hanssondff2c782020-12-21 17:10:01 +00007546 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01007547 )
7548
7549 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007550 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffineedc5d52020-06-12 17:46:39 +01007551 "javalib/bar.jar",
7552 "javalib/foo.jar",
7553 "etc/permissions/foo.xml",
7554 })
7555
7556 // The bar library should depend on the implementation jar.
7557 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kanga3a05462024-04-05 00:36:44 +00007558 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01007559 t.Errorf("expected %q, found %#q", expected, actual)
7560 }
7561}
7562
7563func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
7564 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
7565 apex {
7566 name: "myapex",
7567 key: "myapex.key",
7568 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007569 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007570 }
7571
7572 apex_key {
7573 name: "myapex.key",
7574 public_key: "testkey.avbpubkey",
7575 private_key: "testkey.pem",
7576 }
7577
7578 java_sdk_library_import {
7579 name: "foo",
7580 apex_available: ["myapex"],
7581 prefer: true,
7582 public: {
7583 jars: ["a.jar"],
7584 },
7585 }
7586
7587 `, withFiles(filesForSdkLibrary))
7588}
7589
atrost6e126252020-01-27 17:01:16 +00007590func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007591 result := android.GroupFixturePreparers(
7592 prepareForApexTest,
7593 java.PrepareForTestWithPlatformCompatConfig,
7594 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007595 apex {
7596 name: "myapex",
7597 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007598 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007599 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007600 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007601 }
7602
7603 apex_key {
7604 name: "myapex.key",
7605 public_key: "testkey.avbpubkey",
7606 private_key: "testkey.pem",
7607 }
7608
7609 platform_compat_config {
7610 name: "myjar-platform-compat-config",
7611 src: ":myjar",
7612 }
7613
7614 java_library {
7615 name: "myjar",
7616 srcs: ["foo/bar/MyClass.java"],
7617 sdk_version: "none",
7618 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007619 apex_available: [ "myapex" ],
7620 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007621
7622 // Make sure that a preferred prebuilt does not affect the apex contents.
7623 prebuilt_platform_compat_config {
7624 name: "myjar-platform-compat-config",
7625 metadata: "compat-config/metadata.xml",
7626 prefer: true,
7627 }
atrost6e126252020-01-27 17:01:16 +00007628 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007629 ctx := result.TestContext
Jooyung Hana0503a52023-08-23 13:12:50 +09007630 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
atrost6e126252020-01-27 17:01:16 +00007631 "etc/compatconfig/myjar-platform-compat-config.xml",
7632 "javalib/myjar.jar",
7633 })
7634}
7635
Jooyung Han862c0d62022-12-21 10:15:37 +09007636func TestNoDupeApexFiles(t *testing.T) {
7637 android.GroupFixturePreparers(
7638 android.PrepareForTestWithAndroidBuildComponents,
7639 PrepareForTestWithApexBuildComponents,
7640 prepareForTestWithMyapex,
7641 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7642 ).
7643 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7644 RunTestWithBp(t, `
7645 apex {
7646 name: "myapex",
7647 key: "myapex.key",
7648 prebuilts: ["foo", "bar"],
7649 updatable: false,
7650 }
7651
7652 apex_key {
7653 name: "myapex.key",
7654 public_key: "testkey.avbpubkey",
7655 private_key: "testkey.pem",
7656 }
7657
7658 prebuilt_etc {
7659 name: "foo",
7660 src: "myprebuilt",
7661 filename_from_src: true,
7662 }
7663
7664 prebuilt_etc {
7665 name: "bar",
7666 src: "myprebuilt",
7667 filename_from_src: true,
7668 }
7669 `)
7670}
7671
Jooyung Hana8bd72a2023-11-02 11:56:48 +09007672func TestApexUnwantedTransitiveDeps(t *testing.T) {
7673 bp := `
7674 apex {
7675 name: "myapex",
7676 key: "myapex.key",
7677 native_shared_libs: ["libfoo"],
7678 updatable: false,
7679 unwanted_transitive_deps: ["libbar"],
7680 }
7681
7682 apex_key {
7683 name: "myapex.key",
7684 public_key: "testkey.avbpubkey",
7685 private_key: "testkey.pem",
7686 }
7687
7688 cc_library {
7689 name: "libfoo",
7690 srcs: ["foo.cpp"],
7691 shared_libs: ["libbar"],
7692 apex_available: ["myapex"],
7693 }
7694
7695 cc_library {
7696 name: "libbar",
7697 srcs: ["bar.cpp"],
7698 apex_available: ["myapex"],
7699 }`
7700 ctx := testApex(t, bp)
7701 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
7702 "*/libc++.so",
7703 "*/libfoo.so",
7704 // not libbar.so
7705 })
7706}
7707
Jiyong Park479321d2019-12-16 11:47:12 +09007708func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7709 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7710 apex {
7711 name: "myapex",
7712 key: "myapex.key",
7713 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007714 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007715 }
7716
7717 apex_key {
7718 name: "myapex.key",
7719 public_key: "testkey.avbpubkey",
7720 private_key: "testkey.pem",
7721 }
7722
7723 java_library {
7724 name: "myjar",
7725 srcs: ["foo/bar/MyClass.java"],
7726 sdk_version: "none",
7727 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007728 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007729 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007730 }
7731 `)
7732}
7733
Jiyong Park7afd1072019-12-30 16:56:33 +09007734func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007735 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007736 apex {
7737 name: "myapex",
7738 key: "myapex.key",
7739 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007740 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007741 }
7742
7743 apex_key {
7744 name: "myapex.key",
7745 public_key: "testkey.avbpubkey",
7746 private_key: "testkey.pem",
7747 }
7748
7749 cc_library {
7750 name: "mylib",
7751 srcs: ["mylib.cpp"],
7752 system_shared_libs: [],
7753 stl: "none",
7754 required: ["a", "b"],
7755 host_required: ["c", "d"],
7756 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007757 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007758 }
7759 `)
7760
Jooyung Hana0503a52023-08-23 13:12:50 +09007761 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007762 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007763 name := apexBundle.BaseModuleName()
7764 prefix := "TARGET_"
7765 var builder strings.Builder
7766 data.Custom(&builder, name, prefix, "", data)
7767 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007768 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 a b\n")
Sasha Smundakdcb61292022-12-08 10:41:33 -08007769 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7770 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007771}
7772
Jiyong Park7cd10e32020-01-14 09:22:18 +09007773func TestSymlinksFromApexToSystem(t *testing.T) {
7774 bp := `
7775 apex {
7776 name: "myapex",
7777 key: "myapex.key",
7778 native_shared_libs: ["mylib"],
7779 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007780 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007781 }
7782
Jiyong Park9d677202020-02-19 16:29:35 +09007783 apex {
7784 name: "myapex.updatable",
7785 key: "myapex.key",
7786 native_shared_libs: ["mylib"],
7787 java_libs: ["myjar"],
7788 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007789 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007790 }
7791
Jiyong Park7cd10e32020-01-14 09:22:18 +09007792 apex_key {
7793 name: "myapex.key",
7794 public_key: "testkey.avbpubkey",
7795 private_key: "testkey.pem",
7796 }
7797
7798 cc_library {
7799 name: "mylib",
7800 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007801 shared_libs: [
7802 "myotherlib",
7803 "myotherlib_ext",
7804 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007805 system_shared_libs: [],
7806 stl: "none",
7807 apex_available: [
7808 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007809 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007810 "//apex_available:platform",
7811 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007812 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007813 }
7814
7815 cc_library {
7816 name: "myotherlib",
7817 srcs: ["mylib.cpp"],
7818 system_shared_libs: [],
7819 stl: "none",
7820 apex_available: [
7821 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007822 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007823 "//apex_available:platform",
7824 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007825 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007826 }
7827
Jiyong Parkce243632023-02-17 18:22:25 +09007828 cc_library {
7829 name: "myotherlib_ext",
7830 srcs: ["mylib.cpp"],
7831 system_shared_libs: [],
7832 system_ext_specific: true,
7833 stl: "none",
7834 apex_available: [
7835 "myapex",
7836 "myapex.updatable",
7837 "//apex_available:platform",
7838 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007839 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007840 }
7841
Jiyong Park7cd10e32020-01-14 09:22:18 +09007842 java_library {
7843 name: "myjar",
7844 srcs: ["foo/bar/MyClass.java"],
7845 sdk_version: "none",
7846 system_modules: "none",
Jihoon Kang85bc1932024-07-01 17:04:46 +00007847 static_libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007848 apex_available: [
7849 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007850 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007851 "//apex_available:platform",
7852 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007853 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007854 }
7855
7856 java_library {
7857 name: "myotherjar",
7858 srcs: ["foo/bar/MyClass.java"],
7859 sdk_version: "none",
7860 system_modules: "none",
7861 apex_available: [
7862 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007863 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007864 "//apex_available:platform",
7865 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007866 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007867 }
7868 `
7869
7870 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7871 for _, f := range files {
7872 if f.path == file {
7873 if f.isLink {
7874 t.Errorf("%q is not a real file", file)
7875 }
7876 return
7877 }
7878 }
7879 t.Errorf("%q is not found", file)
7880 }
7881
Jiyong Parkce243632023-02-17 18:22:25 +09007882 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007883 for _, f := range files {
7884 if f.path == file {
7885 if !f.isLink {
7886 t.Errorf("%q is not a symlink", file)
7887 }
Jiyong Parkce243632023-02-17 18:22:25 +09007888 if f.src != target {
7889 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7890 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007891 return
7892 }
7893 }
7894 t.Errorf("%q is not found", file)
7895 }
7896
Jiyong Park9d677202020-02-19 16:29:35 +09007897 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7898 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007899 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana0503a52023-08-23 13:12:50 +09007900 files := getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007901 ensureRealfileExists(t, files, "javalib/myjar.jar")
7902 ensureRealfileExists(t, files, "lib64/mylib.so")
7903 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007904 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007905
Jooyung Hana0503a52023-08-23 13:12:50 +09007906 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007907 ensureRealfileExists(t, files, "javalib/myjar.jar")
7908 ensureRealfileExists(t, files, "lib64/mylib.so")
7909 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007910 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007911
7912 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007913 ctx = testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +09007914 files = getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007915 ensureRealfileExists(t, files, "javalib/myjar.jar")
7916 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007917 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7918 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007919
Jooyung Hana0503a52023-08-23 13:12:50 +09007920 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007921 ensureRealfileExists(t, files, "javalib/myjar.jar")
7922 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007923 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7924 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007925}
7926
Yo Chiange8128052020-07-23 20:09:18 +08007927func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007928 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007929 apex {
7930 name: "myapex",
7931 key: "myapex.key",
7932 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007933 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007934 }
7935
7936 apex_key {
7937 name: "myapex.key",
7938 public_key: "testkey.avbpubkey",
7939 private_key: "testkey.pem",
7940 }
7941
7942 cc_library_shared {
7943 name: "mylib",
7944 srcs: ["mylib.cpp"],
7945 shared_libs: ["myotherlib"],
7946 system_shared_libs: [],
7947 stl: "none",
7948 apex_available: [
7949 "myapex",
7950 "//apex_available:platform",
7951 ],
7952 }
7953
7954 cc_prebuilt_library_shared {
7955 name: "myotherlib",
7956 srcs: ["prebuilt.so"],
7957 system_shared_libs: [],
7958 stl: "none",
7959 apex_available: [
7960 "myapex",
7961 "//apex_available:platform",
7962 ],
7963 }
7964 `)
7965
Jooyung Hana0503a52023-08-23 13:12:50 +09007966 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007967 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007968 var builder strings.Builder
7969 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7970 androidMk := builder.String()
7971 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007972 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007973 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7974 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7975 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007976 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 myotherlib:64\n")
Yo Chiange8128052020-07-23 20:09:18 +08007977}
7978
Jooyung Han643adc42020-02-27 13:50:06 +09007979func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007980 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007981 apex {
7982 name: "myapex",
7983 key: "myapex.key",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007984 binaries: ["mybin"],
7985 jni_libs: ["mylib", "mylib3", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007986 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007987 }
7988
7989 apex_key {
7990 name: "myapex.key",
7991 public_key: "testkey.avbpubkey",
7992 private_key: "testkey.pem",
7993 }
7994
7995 cc_library {
7996 name: "mylib",
7997 srcs: ["mylib.cpp"],
7998 shared_libs: ["mylib2"],
7999 system_shared_libs: [],
8000 stl: "none",
8001 apex_available: [ "myapex" ],
8002 }
8003
8004 cc_library {
8005 name: "mylib2",
8006 srcs: ["mylib.cpp"],
8007 system_shared_libs: [],
8008 stl: "none",
8009 apex_available: [ "myapex" ],
8010 }
Jiyong Park34d5c332022-02-24 18:02:44 +09008011
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008012 // Used as both a JNI library and a regular shared library.
8013 cc_library {
8014 name: "mylib3",
8015 srcs: ["mylib.cpp"],
8016 system_shared_libs: [],
8017 stl: "none",
8018 apex_available: [ "myapex" ],
8019 }
8020
8021 cc_binary {
8022 name: "mybin",
8023 srcs: ["mybin.cpp"],
8024 shared_libs: ["mylib3"],
8025 system_shared_libs: [],
8026 stl: "none",
8027 apex_available: [ "myapex" ],
8028 }
8029
Jiyong Park34d5c332022-02-24 18:02:44 +09008030 rust_ffi_shared {
8031 name: "libfoo.rust",
8032 crate_name: "foo",
8033 srcs: ["foo.rs"],
8034 shared_libs: ["libfoo.shared_from_rust"],
8035 prefer_rlib: true,
8036 apex_available: ["myapex"],
8037 }
8038
8039 cc_library_shared {
8040 name: "libfoo.shared_from_rust",
8041 srcs: ["mylib.cpp"],
8042 system_shared_libs: [],
8043 stl: "none",
8044 stubs: {
8045 versions: ["10", "11", "12"],
8046 },
8047 }
8048
Jooyung Han643adc42020-02-27 13:50:06 +09008049 `)
8050
Jooyung Hana0503a52023-08-23 13:12:50 +09008051 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han643adc42020-02-27 13:50:06 +09008052 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008053 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so mylib3.so")
Jooyung Hana0503a52023-08-23 13:12:50 +09008054 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008055 "bin/mybin",
Jooyung Han643adc42020-02-27 13:50:06 +09008056 "lib64/mylib.so",
8057 "lib64/mylib2.so",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008058 "lib64/mylib3.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09008059 "lib64/libfoo.rust.so",
8060 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
8061 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09008062 })
Jiyong Park34d5c332022-02-24 18:02:44 +09008063
8064 // b/220397949
8065 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09008066}
8067
Jooyung Han49f67012020-04-17 13:43:10 +09008068func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008069 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09008070 apex {
8071 name: "myapex",
8072 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008073 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09008074 }
8075 apex_key {
8076 name: "myapex.key",
8077 public_key: "testkey.avbpubkey",
8078 private_key: "testkey.pem",
8079 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008080 `,
8081 android.FixtureModifyConfig(func(config android.Config) {
8082 delete(config.Targets, android.Android)
8083 config.AndroidCommonTarget = android.Target{}
8084 }),
8085 )
Jooyung Han49f67012020-04-17 13:43:10 +09008086
8087 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
8088 t.Errorf("Expected variants: %v, but got: %v", expected, got)
8089 }
8090}
8091
Jiyong Parkbd159612020-02-28 15:22:21 +09008092func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008093 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09008094 apex {
8095 name: "myapex",
8096 key: "myapex.key",
8097 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008098 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09008099 }
8100
8101 apex_key {
8102 name: "myapex.key",
8103 public_key: "testkey.avbpubkey",
8104 private_key: "testkey.pem",
8105 }
8106
8107 android_app {
8108 name: "AppFoo",
8109 srcs: ["foo/bar/MyClass.java"],
8110 sdk_version: "none",
8111 system_modules: "none",
8112 apex_available: [ "myapex" ],
8113 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09008114 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09008115
Jooyung Hana0503a52023-08-23 13:12:50 +09008116 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07008117 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Jiyong Parkbd159612020-02-28 15:22:21 +09008118
8119 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00008120 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 +09008121}
8122
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008123func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008124 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008125 apex {
8126 name: "myapex",
8127 key: "myapex.key",
8128 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008129 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008130 }
8131
8132 apex_key {
8133 name: "myapex.key",
8134 public_key: "testkey.avbpubkey",
8135 private_key: "testkey.pem",
8136 }
8137
8138 android_app_set {
8139 name: "AppSet",
8140 set: "AppSet.apks",
8141 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09008142 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crosscf371cc2020-11-13 11:48:42 -08008143 bundleConfigRule := mod.Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07008144 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008145 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
8146 s := mod.Rule("apexRule").Args["copy_commands"]
8147 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09008148 if len(copyCmds) != 4 {
8149 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008150 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00008151 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
8152 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09008153 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
8154 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09008155
8156 // Ensure that canned_fs_config has an entry for the app set zip file
8157 generateFsRule := mod.Rule("generateFsConfig")
8158 cmd := generateFsRule.RuleParams.Command
8159 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008160}
8161
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008162func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01008163 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008164 apex_set {
8165 name: "myapex",
8166 filename: "foo_v2.apex",
8167 sanitized: {
8168 none: { set: "myapex.apks", },
8169 hwaddress: { set: "myapex.hwasan.apks", },
8170 },
Paul Duffin24704672021-04-06 16:09:30 +01008171 }
8172 `
8173 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008174
Paul Duffin24704672021-04-06 16:09:30 +01008175 // Check that the extractor produces the correct output file from the correct input file.
Spandan Das9d6e2092024-09-21 02:50:00 +00008176 extractorOutput := "out/soong/.intermediates/myapex/android_common_myapex/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008177
Spandan Das9d6e2092024-09-21 02:50:00 +00008178 m := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin24704672021-04-06 16:09:30 +01008179 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008180
Paul Duffin24704672021-04-06 16:09:30 +01008181 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
8182
8183 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01008184 m = ctx.ModuleForTests("myapex", "android_common_myapex")
8185 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01008186
8187 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008188}
8189
Pranav Guptaeba03b02022-09-27 00:27:08 +00008190func TestApexSetApksModuleAssignment(t *testing.T) {
8191 ctx := testApex(t, `
8192 apex_set {
8193 name: "myapex",
8194 set: ":myapex_apks_file",
8195 }
8196
8197 filegroup {
8198 name: "myapex_apks_file",
8199 srcs: ["myapex.apks"],
8200 }
8201 `)
8202
Spandan Das9d6e2092024-09-21 02:50:00 +00008203 m := ctx.ModuleForTests("myapex", "android_common_myapex")
Pranav Guptaeba03b02022-09-27 00:27:08 +00008204
8205 // Check that the extractor produces the correct apks file from the input module
Spandan Das9d6e2092024-09-21 02:50:00 +00008206 extractorOutput := "out/soong/.intermediates/myapex/android_common_myapex/extracted/myapex.apks"
Pranav Guptaeba03b02022-09-27 00:27:08 +00008207 extractedApex := m.Output(extractorOutput)
8208
8209 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
8210}
8211
Paul Duffin89f570a2021-06-16 01:42:33 +01008212func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00008213 t.Helper()
8214
Paul Duffin55607122021-03-30 23:32:51 +01008215 fs := android.MockFS{
8216 "a.java": nil,
8217 "a.jar": nil,
8218 "apex_manifest.json": nil,
8219 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00008220 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00008221 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
8222 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
8223 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00008224 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008225 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008226
Paul Duffin55607122021-03-30 23:32:51 +01008227 errorHandler := android.FixtureExpectsNoErrors
8228 if errmsg != "" {
8229 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008230 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008231
Paul Duffin55607122021-03-30 23:32:51 +01008232 result := android.GroupFixturePreparers(
8233 cc.PrepareForTestWithCcDefaultModules,
8234 java.PrepareForTestWithHiddenApiBuildComponents,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008235 java.PrepareForTestWithDexpreopt,
Paul Duffin55607122021-03-30 23:32:51 +01008236 java.PrepareForTestWithJavaSdkLibraryFiles,
8237 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01008238 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01008239 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01008240 android.FixtureModifyMockFS(func(fs android.MockFS) {
8241 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
8242 insert := ""
8243 for _, fragment := range fragments {
8244 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
8245 }
8246 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
8247 platform_bootclasspath {
8248 name: "platform-bootclasspath",
8249 fragments: [
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008250 {apex: "com.android.art", module: "art-bootclasspath-fragment"},
Paul Duffin89f570a2021-06-16 01:42:33 +01008251 %s
8252 ],
8253 }
8254 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01008255 }
Paul Duffin89f570a2021-06-16 01:42:33 +01008256 }),
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008257 // Dexpreopt for boot jars requires the ART boot image profile.
8258 java.PrepareApexBootJarModule("com.android.art", "core-oj"),
8259 dexpreopt.FixtureSetArtBootJars("com.android.art:core-oj"),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00008260 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01008261 ).
8262 ExtendWithErrorHandler(errorHandler).
8263 RunTestWithBp(t, bp)
8264
8265 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008266}
8267
Jooyung Han548640b2020-04-27 12:10:30 +09008268func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
8269 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8270 apex {
8271 name: "myapex",
8272 key: "myapex.key",
8273 updatable: true,
8274 }
8275
8276 apex_key {
8277 name: "myapex.key",
8278 public_key: "testkey.avbpubkey",
8279 private_key: "testkey.pem",
8280 }
8281 `)
8282}
8283
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008284func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
8285 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8286 apex {
8287 name: "myapex",
8288 key: "myapex.key",
8289 }
8290
8291 apex_key {
8292 name: "myapex.key",
8293 public_key: "testkey.avbpubkey",
8294 private_key: "testkey.pem",
8295 }
8296 `)
8297}
8298
satayevb98371c2021-06-15 16:49:50 +01008299func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
8300 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
8301 apex {
8302 name: "myapex",
8303 key: "myapex.key",
8304 systemserverclasspath_fragments: [
8305 "mysystemserverclasspathfragment",
8306 ],
8307 min_sdk_version: "29",
8308 updatable: true,
8309 }
8310
8311 apex_key {
8312 name: "myapex.key",
8313 public_key: "testkey.avbpubkey",
8314 private_key: "testkey.pem",
8315 }
8316
8317 java_library {
8318 name: "foo",
8319 srcs: ["b.java"],
8320 min_sdk_version: "29",
8321 installable: true,
8322 apex_available: [
8323 "myapex",
8324 ],
Jihoon Kang85bc1932024-07-01 17:04:46 +00008325 sdk_version: "current",
satayevb98371c2021-06-15 16:49:50 +01008326 }
8327
8328 systemserverclasspath_fragment {
8329 name: "mysystemserverclasspathfragment",
8330 generate_classpaths_proto: false,
8331 contents: [
8332 "foo",
8333 ],
8334 apex_available: [
8335 "myapex",
8336 ],
8337 }
satayevabcd5972021-08-06 17:49:46 +01008338 `,
8339 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8340 )
satayevb98371c2021-06-15 16:49:50 +01008341}
8342
Paul Duffin064b70c2020-11-02 17:32:38 +00008343func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008344 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008345 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008346 fragment := java.ApexVariantReference{
8347 Apex: proptools.StringPtr("myapex"),
8348 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8349 }
8350
Paul Duffin064b70c2020-11-02 17:32:38 +00008351 testDexpreoptWithApexes(t, `
8352 prebuilt_apex {
8353 name: "myapex" ,
8354 arch: {
8355 arm64: {
8356 src: "myapex-arm64.apex",
8357 },
8358 arm: {
8359 src: "myapex-arm.apex",
8360 },
8361 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008362 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8363 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008364
Paul Duffin89f570a2021-06-16 01:42:33 +01008365 prebuilt_bootclasspath_fragment {
8366 name: "my-bootclasspath-fragment",
8367 contents: ["libfoo"],
8368 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008369 hidden_api: {
8370 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8371 metadata: "my-bootclasspath-fragment/metadata.csv",
8372 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008373 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8374 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8375 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008376 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008377 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008378
Spandan Das52c01a12024-09-20 01:09:48 +00008379 java_sdk_library_import {
8380 name: "libfoo",
8381 prefer: true,
8382 public: {
Paul Duffin89f570a2021-06-16 01:42:33 +01008383 jars: ["libfoo.jar"],
Spandan Das52c01a12024-09-20 01:09:48 +00008384 },
8385 apex_available: ["myapex"],
8386 shared_library: false,
8387 permitted_packages: ["libfoo"],
8388 }
Paul Duffin89f570a2021-06-16 01:42:33 +01008389 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008390 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008391}
8392
Spandan Dasf14e2542021-11-12 00:01:37 +00008393func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008394 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008395 bp += `
8396 apex_key {
8397 name: "myapex.key",
8398 public_key: "testkey.avbpubkey",
8399 private_key: "testkey.pem",
8400 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008401 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008402 "lib1/src/A.java": nil,
8403 "lib2/src/B.java": nil,
8404 "system/sepolicy/apex/myapex-file_contexts": nil,
8405 }
8406
Paul Duffin45338f02021-03-30 23:07:52 +01008407 errorHandler := android.FixtureExpectsNoErrors
8408 if errmsg != "" {
8409 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008410 }
Colin Crossae8600b2020-10-29 17:09:13 -07008411
Paul Duffin45338f02021-03-30 23:07:52 +01008412 android.GroupFixturePreparers(
8413 android.PrepareForTestWithAndroidBuildComponents,
8414 java.PrepareForTestWithJavaBuildComponents,
8415 PrepareForTestWithApexBuildComponents,
8416 android.PrepareForTestWithNeverallowRules(rules),
8417 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008418 apexBootJars := make([]string, 0, len(bootJars))
8419 for _, apexBootJar := range bootJars {
8420 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008421 }
satayevd604b212021-07-21 14:23:52 +01008422 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008423 }),
8424 fs.AddToFixture(),
8425 ).
8426 ExtendWithErrorHandler(errorHandler).
8427 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008428}
8429
8430func TestApexPermittedPackagesRules(t *testing.T) {
8431 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008432 name string
8433 expectedError string
8434 bp string
8435 bootJars []string
8436 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008437 }{
8438
8439 {
8440 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8441 expectedError: "",
8442 bp: `
8443 java_library {
8444 name: "bcp_lib1",
8445 srcs: ["lib1/src/*.java"],
8446 permitted_packages: ["foo.bar"],
8447 apex_available: ["myapex"],
8448 sdk_version: "none",
8449 system_modules: "none",
8450 }
8451 java_library {
8452 name: "nonbcp_lib2",
8453 srcs: ["lib2/src/*.java"],
8454 apex_available: ["myapex"],
8455 permitted_packages: ["a.b"],
8456 sdk_version: "none",
8457 system_modules: "none",
8458 }
8459 apex {
8460 name: "myapex",
8461 key: "myapex.key",
8462 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008463 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008464 }`,
8465 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008466 bcpPermittedPackages: map[string][]string{
8467 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008468 "foo.bar",
8469 },
8470 },
8471 },
8472 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008473 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008474 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 +01008475 bp: `
8476 java_library {
8477 name: "bcp_lib1",
8478 srcs: ["lib1/src/*.java"],
8479 apex_available: ["myapex"],
8480 permitted_packages: ["foo.bar"],
8481 sdk_version: "none",
8482 system_modules: "none",
8483 }
8484 java_library {
8485 name: "bcp_lib2",
8486 srcs: ["lib2/src/*.java"],
8487 apex_available: ["myapex"],
8488 permitted_packages: ["foo.bar", "bar.baz"],
8489 sdk_version: "none",
8490 system_modules: "none",
8491 }
8492 apex {
8493 name: "myapex",
8494 key: "myapex.key",
8495 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008496 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008497 }
8498 `,
8499 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008500 bcpPermittedPackages: map[string][]string{
8501 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008502 "foo.bar",
8503 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008504 "bcp_lib2": []string{
8505 "foo.bar",
8506 },
8507 },
8508 },
8509 {
8510 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8511 expectedError: "",
8512 bp: `
8513 java_library {
8514 name: "bcp_lib_restricted",
8515 srcs: ["lib1/src/*.java"],
8516 apex_available: ["myapex"],
8517 permitted_packages: ["foo.bar"],
8518 sdk_version: "none",
8519 min_sdk_version: "29",
8520 system_modules: "none",
8521 }
8522 java_library {
8523 name: "bcp_lib_unrestricted",
8524 srcs: ["lib2/src/*.java"],
8525 apex_available: ["myapex"],
8526 permitted_packages: ["foo.bar", "bar.baz"],
8527 sdk_version: "none",
8528 min_sdk_version: "29",
8529 system_modules: "none",
8530 }
8531 apex {
8532 name: "myapex",
8533 key: "myapex.key",
8534 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8535 updatable: true,
8536 min_sdk_version: "29",
8537 }
8538 `,
8539 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8540 bcpPermittedPackages: map[string][]string{
8541 "bcp_lib1_non_updateable": []string{
8542 "foo.bar",
8543 },
8544 // 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 +01008545 },
8546 },
8547 }
8548 for _, tc := range testcases {
8549 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008550 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8551 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008552 })
8553 }
8554}
8555
Jiyong Park62304bb2020-04-13 16:19:48 +09008556func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008557 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008558 apex {
8559 name: "myapex",
8560 key: "myapex.key",
8561 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008562 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008563 }
8564
8565 apex_key {
8566 name: "myapex.key",
8567 public_key: "testkey.avbpubkey",
8568 private_key: "testkey.pem",
8569 }
8570
8571 cc_library {
8572 name: "mylib",
8573 srcs: ["mylib.cpp"],
8574 system_shared_libs: [],
8575 stl: "none",
8576 stubs: {
8577 versions: ["1"],
8578 },
8579 apex_available: ["myapex"],
8580 }
8581
8582 cc_library {
8583 name: "myprivlib",
8584 srcs: ["mylib.cpp"],
8585 system_shared_libs: [],
8586 stl: "none",
8587 apex_available: ["myapex"],
8588 }
8589
8590
8591 cc_test {
8592 name: "mytest",
8593 gtest: false,
8594 srcs: ["mylib.cpp"],
8595 system_shared_libs: [],
8596 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008597 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008598 test_for: ["myapex"]
8599 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008600
8601 cc_library {
8602 name: "mytestlib",
8603 srcs: ["mylib.cpp"],
8604 system_shared_libs: [],
8605 shared_libs: ["mylib", "myprivlib"],
8606 stl: "none",
8607 test_for: ["myapex"],
8608 }
8609
8610 cc_benchmark {
8611 name: "mybench",
8612 srcs: ["mylib.cpp"],
8613 system_shared_libs: [],
8614 shared_libs: ["mylib", "myprivlib"],
8615 stl: "none",
8616 test_for: ["myapex"],
8617 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008618 `)
8619
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008620 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008621 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008622 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8623 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8624 }
8625
8626 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008627 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008628 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8629 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8630 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8631}
Jiyong Park46a512f2020-12-04 18:02:13 +09008632
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008633func TestIndirectTestFor(t *testing.T) {
8634 ctx := testApex(t, `
8635 apex {
8636 name: "myapex",
8637 key: "myapex.key",
8638 native_shared_libs: ["mylib", "myprivlib"],
8639 updatable: false,
8640 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008641
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008642 apex_key {
8643 name: "myapex.key",
8644 public_key: "testkey.avbpubkey",
8645 private_key: "testkey.pem",
8646 }
8647
8648 cc_library {
8649 name: "mylib",
8650 srcs: ["mylib.cpp"],
8651 system_shared_libs: [],
8652 stl: "none",
8653 stubs: {
8654 versions: ["1"],
8655 },
8656 apex_available: ["myapex"],
8657 }
8658
8659 cc_library {
8660 name: "myprivlib",
8661 srcs: ["mylib.cpp"],
8662 system_shared_libs: [],
8663 stl: "none",
8664 shared_libs: ["mylib"],
8665 apex_available: ["myapex"],
8666 }
8667
8668 cc_library {
8669 name: "mytestlib",
8670 srcs: ["mylib.cpp"],
8671 system_shared_libs: [],
8672 shared_libs: ["myprivlib"],
8673 stl: "none",
8674 test_for: ["myapex"],
8675 }
8676 `)
8677
8678 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008679 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008680 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8681 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8682 }
8683
8684 // The platform variant of mytestlib links to the platform variant of the
8685 // internal myprivlib.
8686 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8687
8688 // The platform variant of myprivlib links to the platform variant of mylib
8689 // and bypasses its stubs.
8690 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 +09008691}
8692
Martin Stjernholmec009002021-03-27 15:18:31 +00008693func TestTestForForLibInOtherApex(t *testing.T) {
8694 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8695 _ = testApex(t, `
8696 apex {
8697 name: "com.android.art",
8698 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008699 native_shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008700 updatable: false,
8701 }
8702
8703 apex {
8704 name: "com.android.art.debug",
8705 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008706 native_shared_libs: ["libnativebridge", "libnativebrdige_test"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008707 updatable: false,
8708 }
8709
8710 apex_key {
8711 name: "myapex.key",
8712 public_key: "testkey.avbpubkey",
8713 private_key: "testkey.pem",
8714 }
8715
8716 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008717 name: "libnativebridge",
8718 srcs: ["libnativebridge.cpp"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008719 system_shared_libs: [],
8720 stl: "none",
8721 stubs: {
8722 versions: ["1"],
8723 },
8724 apex_available: ["com.android.art", "com.android.art.debug"],
8725 }
8726
8727 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008728 name: "libnativebrdige_test",
Martin Stjernholmec009002021-03-27 15:18:31 +00008729 srcs: ["mylib.cpp"],
8730 system_shared_libs: [],
Spandan Das20fce2d2023-04-12 17:21:39 +00008731 shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008732 stl: "none",
8733 apex_available: ["com.android.art.debug"],
8734 test_for: ["com.android.art"],
8735 }
8736 `,
8737 android.MockFS{
8738 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8739 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8740 }.AddToFixture())
8741}
8742
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008743// TODO(jungjw): Move this to proptools
8744func intPtr(i int) *int {
8745 return &i
8746}
8747
8748func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008749 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008750 apex_set {
8751 name: "myapex",
8752 set: "myapex.apks",
8753 filename: "foo_v2.apex",
8754 overrides: ["foo"],
8755 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008756 `,
8757 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8758 variables.Platform_sdk_version = intPtr(30)
8759 }),
8760 android.FixtureModifyConfig(func(config android.Config) {
8761 config.Targets[android.Android] = []android.Target{
8762 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8763 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8764 }
8765 }),
8766 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008767
Spandan Das9d6e2092024-09-21 02:50:00 +00008768 m := ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008769
8770 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008771 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008772 actual := extractedApex.Args["abis"]
8773 expected := "ARMEABI_V7A,ARM64_V8A"
8774 if actual != expected {
8775 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8776 }
8777 actual = extractedApex.Args["sdk-version"]
8778 expected = "30"
8779 if actual != expected {
8780 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8781 }
8782
Paul Duffin6717d882021-06-15 19:09:41 +01008783 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008784 a := m.Module().(*ApexSet)
8785 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008786 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008787 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8788 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8789 }
8790}
8791
Anton Hansson805e0a52022-11-25 14:06:46 +00008792func TestApexSet_NativeBridge(t *testing.T) {
8793 ctx := testApex(t, `
8794 apex_set {
8795 name: "myapex",
8796 set: "myapex.apks",
8797 filename: "foo_v2.apex",
8798 overrides: ["foo"],
8799 }
8800 `,
8801 android.FixtureModifyConfig(func(config android.Config) {
8802 config.Targets[android.Android] = []android.Target{
8803 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8804 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8805 }
8806 }),
8807 )
8808
Spandan Das9d6e2092024-09-21 02:50:00 +00008809 m := ctx.ModuleForTests("myapex", "android_common_myapex")
Anton Hansson805e0a52022-11-25 14:06:46 +00008810
8811 // Check extract_apks tool parameters. No native bridge arch expected
8812 extractedApex := m.Output("extracted/myapex.apks")
8813 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8814}
8815
Jiyong Park7d95a512020-05-10 15:16:24 +09008816func TestNoStaticLinkingToStubsLib(t *testing.T) {
8817 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8818 apex {
8819 name: "myapex",
8820 key: "myapex.key",
8821 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008822 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008823 }
8824
8825 apex_key {
8826 name: "myapex.key",
8827 public_key: "testkey.avbpubkey",
8828 private_key: "testkey.pem",
8829 }
8830
8831 cc_library {
8832 name: "mylib",
8833 srcs: ["mylib.cpp"],
8834 static_libs: ["otherlib"],
8835 system_shared_libs: [],
8836 stl: "none",
8837 apex_available: [ "myapex" ],
8838 }
8839
8840 cc_library {
8841 name: "otherlib",
8842 srcs: ["mylib.cpp"],
8843 system_shared_libs: [],
8844 stl: "none",
8845 stubs: {
8846 versions: ["1", "2", "3"],
8847 },
8848 apex_available: [ "myapex" ],
8849 }
8850 `)
8851}
8852
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008853func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008854 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008855 apex {
8856 name: "myapex",
8857 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008858 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008859 custom_sign_tool: "sign_myapex",
8860 }
8861
8862 apex_key {
8863 name: "myapex.key",
8864 public_key: "testkey.avbpubkey",
8865 private_key: "testkey.pem",
8866 }
8867 `)
8868
Jooyung Han286957d2023-10-30 16:17:56 +09008869 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07008870 content := android.ContentFromFileRuleForTests(t, ctx, myapex.Output("apexkeys.txt"))
Jooyung Haneec1b3f2023-06-20 16:25:59 +09008871 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 +09008872}
8873
8874func TestApexKeysTxtOverrides(t *testing.T) {
8875 ctx := testApex(t, `
8876 apex {
8877 name: "myapex",
8878 key: "myapex.key",
8879 updatable: false,
8880 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008881 }
8882
8883 apex_key {
8884 name: "myapex.key",
8885 public_key: "testkey.avbpubkey",
8886 private_key: "testkey.pem",
8887 }
8888
8889 prebuilt_apex {
8890 name: "myapex",
8891 prefer: true,
8892 arch: {
8893 arm64: {
8894 src: "myapex-arm64.apex",
8895 },
8896 arm: {
8897 src: "myapex-arm.apex",
8898 },
8899 },
8900 }
8901
8902 apex_set {
8903 name: "myapex_set",
8904 set: "myapex.apks",
8905 filename: "myapex_set.apex",
8906 overrides: ["myapex"],
8907 }
8908 `)
8909
Colin Crossf61d03d2023-11-02 16:56:39 -07008910 content := android.ContentFromFileRuleForTests(t, ctx,
8911 ctx.ModuleForTests("myapex", "android_common_myapex").Output("apexkeys.txt"))
Jooyung Han286957d2023-10-30 16:17:56 +09008912 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 -07008913 content = android.ContentFromFileRuleForTests(t, ctx,
8914 ctx.ModuleForTests("myapex_set", "android_common_myapex_set").Output("apexkeys.txt"))
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008915 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 +09008916}
8917
Jooyung Han938b5932020-06-20 12:47:47 +09008918func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008919 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008920 apex {
8921 name: "myapex",
8922 key: "myapex.key",
8923 apps: ["app"],
8924 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008925 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008926 }
8927
8928 apex_key {
8929 name: "myapex.key",
8930 public_key: "testkey.avbpubkey",
8931 private_key: "testkey.pem",
8932 }
8933
8934 android_app {
8935 name: "app",
8936 srcs: ["foo/bar/MyClass.java"],
8937 package_name: "foo",
8938 sdk_version: "none",
8939 system_modules: "none",
8940 apex_available: [ "myapex" ],
8941 }
8942 `, withFiles(map[string][]byte{
8943 "sub/Android.bp": []byte(`
8944 override_apex {
8945 name: "override_myapex",
8946 base: "myapex",
8947 apps: ["override_app"],
8948 allowed_files: ":allowed",
8949 }
8950 // Overridable "path" property should be referenced indirectly
8951 filegroup {
8952 name: "allowed",
8953 srcs: ["allowed.txt"],
8954 }
8955 override_android_app {
8956 name: "override_app",
8957 base: "app",
8958 package_name: "bar",
8959 }
8960 `),
8961 }))
8962
Jooyung Hana0503a52023-08-23 13:12:50 +09008963 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008964 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8965 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8966 }
8967
Spandan Das50801e22024-05-13 18:29:45 +00008968 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008969 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8970 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8971 }
8972}
8973
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008974func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008975 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008976 apex {
8977 name: "myapex",
8978 key: "myapex.key",
8979 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008980 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008981 }
8982
8983 apex_key {
8984 name: "myapex.key",
8985 public_key: "testkey.avbpubkey",
8986 private_key: "testkey.pem",
8987 }
8988
8989 cc_library {
8990 name: "mylib",
8991 srcs: ["mylib.cpp"],
8992 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008993 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008994 },
8995 apex_available: ["myapex"],
8996 }
8997
8998 cc_prebuilt_library_shared {
8999 name: "mylib",
9000 prefer: false,
9001 srcs: ["prebuilt.so"],
9002 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07009003 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009004 },
9005 apex_available: ["myapex"],
9006 }
9007 `)
9008}
9009
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009010func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009011 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009012 apex {
9013 name: "myapex",
9014 key: "myapex.key",
9015 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009016 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009017 }
9018 apex_key {
9019 name: "myapex.key",
9020 public_key: "testkey.avbpubkey",
9021 private_key: "testkey.pem",
9022 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00009023 `,
9024 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9025 variables.CompressedApex = proptools.BoolPtr(true)
9026 }),
9027 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009028
Jooyung Hana0503a52023-08-23 13:12:50 +09009029 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("compressRule")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009030 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
9031
Jooyung Hana0503a52023-08-23 13:12:50 +09009032 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex").Description("sign compressedApex")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009033 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
9034
9035 // Make sure output of bundle is .capex
Jooyung Hana0503a52023-08-23 13:12:50 +09009036 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009037 ensureContains(t, ab.outputFile.String(), "myapex.capex")
9038
9039 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07009040 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009041 var builder strings.Builder
9042 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9043 androidMk := builder.String()
9044 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
9045}
9046
Jooyung Han26ec8482024-07-31 15:04:05 +09009047func TestApexSet_ShouldRespectCompressedApexFlag(t *testing.T) {
9048 for _, compressionEnabled := range []bool{true, false} {
9049 t.Run(fmt.Sprintf("compressionEnabled=%v", compressionEnabled), func(t *testing.T) {
9050 ctx := testApex(t, `
9051 apex_set {
9052 name: "com.company.android.myapex",
9053 apex_name: "com.android.myapex",
9054 set: "company-myapex.apks",
9055 }
9056 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9057 variables.CompressedApex = proptools.BoolPtr(compressionEnabled)
9058 }),
9059 )
9060
9061 build := ctx.ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex").Output("com.company.android.myapex.apex")
9062 if compressionEnabled {
9063 ensureEquals(t, build.Rule.String(), "android/soong/android.Cp")
9064 } else {
9065 ensureEquals(t, build.Rule.String(), "android/apex.decompressApex")
9066 }
9067 })
9068 }
9069}
9070
Martin Stjernholm2856c662020-12-02 15:03:42 +00009071func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009072 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00009073 apex {
9074 name: "myapex",
9075 key: "myapex.key",
9076 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009077 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00009078 }
9079
9080 apex_key {
9081 name: "myapex.key",
9082 public_key: "testkey.avbpubkey",
9083 private_key: "testkey.pem",
9084 }
9085
9086 cc_library {
9087 name: "mylib",
9088 srcs: ["mylib.cpp"],
9089 apex_available: ["myapex"],
9090 shared_libs: ["otherlib"],
9091 system_shared_libs: [],
9092 }
9093
9094 cc_library {
9095 name: "otherlib",
9096 srcs: ["mylib.cpp"],
9097 stubs: {
9098 versions: ["current"],
9099 },
9100 }
9101
9102 cc_prebuilt_library_shared {
9103 name: "otherlib",
9104 prefer: true,
9105 srcs: ["prebuilt.so"],
9106 stubs: {
9107 versions: ["current"],
9108 },
9109 }
9110 `)
9111
Jooyung Hana0503a52023-08-23 13:12:50 +09009112 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07009113 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00009114 var builder strings.Builder
9115 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9116 androidMk := builder.String()
9117
9118 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
9119 // a thing there.
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009120 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 otherlib\n")
Martin Stjernholm2856c662020-12-02 15:03:42 +00009121}
9122
Jiyong Parke3867542020-12-03 17:28:25 +09009123func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009124 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09009125 apex {
9126 name: "myapex",
9127 key: "myapex.key",
9128 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009129 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09009130 }
9131
9132 apex_key {
9133 name: "myapex.key",
9134 public_key: "testkey.avbpubkey",
9135 private_key: "testkey.pem",
9136 }
9137
9138 cc_library {
9139 name: "mylib",
9140 srcs: ["mylib.cpp"],
9141 system_shared_libs: [],
9142 stl: "none",
9143 apex_available: ["myapex"],
9144 shared_libs: ["mylib2"],
9145 target: {
9146 apex: {
9147 exclude_shared_libs: ["mylib2"],
9148 },
9149 },
9150 }
9151
9152 cc_library {
9153 name: "mylib2",
9154 srcs: ["mylib.cpp"],
9155 system_shared_libs: [],
9156 stl: "none",
9157 }
9158 `)
9159
9160 // Check if mylib is linked to mylib2 for the non-apex target
9161 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
9162 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
9163
9164 // Make sure that the link doesn't occur for the apex target
9165 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
9166 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
9167
9168 // It shouldn't appear in the copy cmd as well.
Jooyung Hana0503a52023-08-23 13:12:50 +09009169 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule").Args["copy_commands"]
Jiyong Parke3867542020-12-03 17:28:25 +09009170 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
9171}
9172
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009173func TestPrebuiltStubLibDep(t *testing.T) {
9174 bpBase := `
9175 apex {
9176 name: "myapex",
9177 key: "myapex.key",
9178 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009179 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009180 }
9181 apex_key {
9182 name: "myapex.key",
9183 public_key: "testkey.avbpubkey",
9184 private_key: "testkey.pem",
9185 }
9186 cc_library {
9187 name: "mylib",
9188 srcs: ["mylib.cpp"],
9189 apex_available: ["myapex"],
9190 shared_libs: ["stublib"],
9191 system_shared_libs: [],
9192 }
9193 apex {
9194 name: "otherapex",
9195 enabled: %s,
9196 key: "myapex.key",
9197 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009198 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009199 }
9200 `
9201
9202 stublibSourceBp := `
9203 cc_library {
9204 name: "stublib",
9205 srcs: ["mylib.cpp"],
9206 apex_available: ["otherapex"],
9207 system_shared_libs: [],
9208 stl: "none",
9209 stubs: {
9210 versions: ["1"],
9211 },
9212 }
9213 `
9214
9215 stublibPrebuiltBp := `
9216 cc_prebuilt_library_shared {
9217 name: "stublib",
9218 srcs: ["prebuilt.so"],
9219 apex_available: ["otherapex"],
9220 stubs: {
9221 versions: ["1"],
9222 },
9223 %s
9224 }
9225 `
9226
9227 tests := []struct {
9228 name string
9229 stublibBp string
9230 usePrebuilt bool
9231 modNames []string // Modules to collect AndroidMkEntries for
9232 otherApexEnabled []string
9233 }{
9234 {
9235 name: "only_source",
9236 stublibBp: stublibSourceBp,
9237 usePrebuilt: false,
9238 modNames: []string{"stublib"},
9239 otherApexEnabled: []string{"true", "false"},
9240 },
9241 {
9242 name: "source_preferred",
9243 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9244 usePrebuilt: false,
9245 modNames: []string{"stublib", "prebuilt_stublib"},
9246 otherApexEnabled: []string{"true", "false"},
9247 },
9248 {
9249 name: "prebuilt_preferred",
9250 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9251 usePrebuilt: true,
9252 modNames: []string{"stublib", "prebuilt_stublib"},
9253 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9254 },
9255 {
9256 name: "only_prebuilt",
9257 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9258 usePrebuilt: true,
9259 modNames: []string{"stublib"},
9260 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9261 },
9262 }
9263
9264 for _, test := range tests {
9265 t.Run(test.name, func(t *testing.T) {
9266 for _, otherApexEnabled := range test.otherApexEnabled {
9267 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009268 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009269
9270 type modAndMkEntries struct {
9271 mod *cc.Module
9272 mkEntries android.AndroidMkEntries
9273 }
9274 entries := []*modAndMkEntries{}
9275
9276 // Gather shared lib modules that are installable
9277 for _, modName := range test.modNames {
9278 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9279 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9280 continue
9281 }
9282 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Cole Fausta963b942024-04-11 17:43:00 -07009283 if !mod.Enabled(android.PanickingConfigAndErrorContext(ctx)) || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009284 continue
9285 }
Colin Crossaa255532020-07-03 13:18:24 -07009286 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009287 if ent.Disabled {
9288 continue
9289 }
9290 entries = append(entries, &modAndMkEntries{
9291 mod: mod,
9292 mkEntries: ent,
9293 })
9294 }
9295 }
9296 }
9297
9298 var entry *modAndMkEntries = nil
9299 for _, ent := range entries {
9300 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9301 if entry != nil {
9302 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9303 } else {
9304 entry = ent
9305 }
9306 }
9307 }
9308
9309 if entry == nil {
9310 t.Errorf("AndroidMk entry for \"stublib\" missing")
9311 } else {
9312 isPrebuilt := entry.mod.Prebuilt() != nil
9313 if isPrebuilt != test.usePrebuilt {
9314 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9315 }
9316 if !entry.mod.IsStubs() {
9317 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9318 }
9319 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9320 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9321 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009322 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009323 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009324 if !android.InList(expected, cflags) {
9325 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9326 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009327 }
9328 })
9329 }
9330 })
9331 }
9332}
9333
Colin Crossc33e5212021-05-25 18:16:02 -07009334func TestApexJavaCoverage(t *testing.T) {
9335 bp := `
9336 apex {
9337 name: "myapex",
9338 key: "myapex.key",
9339 java_libs: ["mylib"],
9340 bootclasspath_fragments: ["mybootclasspathfragment"],
9341 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9342 updatable: false,
9343 }
9344
9345 apex_key {
9346 name: "myapex.key",
9347 public_key: "testkey.avbpubkey",
9348 private_key: "testkey.pem",
9349 }
9350
9351 java_library {
9352 name: "mylib",
9353 srcs: ["mylib.java"],
9354 apex_available: ["myapex"],
9355 compile_dex: true,
9356 }
9357
9358 bootclasspath_fragment {
9359 name: "mybootclasspathfragment",
9360 contents: ["mybootclasspathlib"],
9361 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009362 hidden_api: {
9363 split_packages: ["*"],
9364 },
Colin Crossc33e5212021-05-25 18:16:02 -07009365 }
9366
9367 java_library {
9368 name: "mybootclasspathlib",
9369 srcs: ["mybootclasspathlib.java"],
9370 apex_available: ["myapex"],
9371 compile_dex: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00009372 sdk_version: "current",
Colin Crossc33e5212021-05-25 18:16:02 -07009373 }
9374
9375 systemserverclasspath_fragment {
9376 name: "mysystemserverclasspathfragment",
9377 contents: ["mysystemserverclasspathlib"],
9378 apex_available: ["myapex"],
9379 }
9380
9381 java_library {
9382 name: "mysystemserverclasspathlib",
9383 srcs: ["mysystemserverclasspathlib.java"],
9384 apex_available: ["myapex"],
9385 compile_dex: true,
9386 }
9387 `
9388
9389 result := android.GroupFixturePreparers(
9390 PrepareForTestWithApexBuildComponents,
9391 prepareForTestWithMyapex,
9392 java.PrepareForTestWithJavaDefaultModules,
9393 android.PrepareForTestWithAndroidBuildComponents,
9394 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009395 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9396 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009397 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009398 ).RunTest(t)
9399
9400 // Make sure jacoco ran on both mylib and mybootclasspathlib
9401 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9402 t.Errorf("Failed to find jacoco rule for mylib")
9403 }
9404 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9405 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9406 }
9407 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9408 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9409 }
9410}
9411
Jiyong Park192600a2021-08-03 07:52:17 +00009412func TestProhibitStaticExecutable(t *testing.T) {
9413 testApexError(t, `executable mybin is static`, `
9414 apex {
9415 name: "myapex",
9416 key: "myapex.key",
9417 binaries: ["mybin"],
9418 min_sdk_version: "29",
9419 }
9420
9421 apex_key {
9422 name: "myapex.key",
9423 public_key: "testkey.avbpubkey",
9424 private_key: "testkey.pem",
9425 }
9426
9427 cc_binary {
9428 name: "mybin",
9429 srcs: ["mylib.cpp"],
9430 relative_install_path: "foo/bar",
9431 static_executable: true,
9432 system_shared_libs: [],
9433 stl: "none",
9434 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009435 min_sdk_version: "29",
9436 }
9437 `)
9438
9439 testApexError(t, `executable mybin.rust is static`, `
9440 apex {
9441 name: "myapex",
9442 key: "myapex.key",
9443 binaries: ["mybin.rust"],
9444 min_sdk_version: "29",
9445 }
9446
9447 apex_key {
9448 name: "myapex.key",
9449 public_key: "testkey.avbpubkey",
9450 private_key: "testkey.pem",
9451 }
9452
9453 rust_binary {
9454 name: "mybin.rust",
9455 srcs: ["foo.rs"],
9456 static_executable: true,
9457 apex_available: ["myapex"],
9458 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009459 }
9460 `)
9461}
9462
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009463func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9464 ctx := testApex(t, `
9465 apex {
9466 name: "myapex",
9467 key: "myapex.key",
9468 updatable: false,
9469 java_libs: ["foo"],
9470 }
9471
9472 apex_key {
9473 name: "myapex.key",
9474 public_key: "testkey.avbpubkey",
9475 private_key: "testkey.pem",
9476 }
9477
9478 java_library {
9479 name: "foo",
9480 srcs: ["foo.java"],
9481 apex_available: ["myapex"],
9482 installable: true,
9483 }
9484 `,
9485 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9486 )
9487
Jooyung Hana0503a52023-08-23 13:12:50 +09009488 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009489 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9490 var builder strings.Builder
9491 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9492 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009493 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 +00009494}
9495
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009496func TestAndroidMk_RequiredModules(t *testing.T) {
9497 ctx := testApex(t, `
9498 apex {
9499 name: "myapex",
9500 key: "myapex.key",
9501 updatable: false,
9502 java_libs: ["foo"],
9503 required: ["otherapex"],
9504 }
9505
9506 apex {
9507 name: "otherapex",
9508 key: "myapex.key",
9509 updatable: false,
9510 java_libs: ["foo"],
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009511 }
9512
9513 apex_key {
9514 name: "myapex.key",
9515 public_key: "testkey.avbpubkey",
9516 private_key: "testkey.pem",
9517 }
9518
9519 java_library {
9520 name: "foo",
9521 srcs: ["foo.java"],
9522 apex_available: ["myapex", "otherapex"],
9523 installable: true,
9524 }
9525 `)
9526
Jooyung Hana0503a52023-08-23 13:12:50 +09009527 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009528 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9529 var builder strings.Builder
9530 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9531 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009532 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009533}
9534
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009535func TestAndroidMk_RequiredDeps(t *testing.T) {
9536 ctx := testApex(t, `
9537 apex {
9538 name: "myapex",
9539 key: "myapex.key",
9540 updatable: false,
9541 }
9542
9543 apex_key {
9544 name: "myapex.key",
9545 public_key: "testkey.avbpubkey",
9546 private_key: "testkey.pem",
9547 }
9548 `)
9549
Jooyung Hana0503a52023-08-23 13:12:50 +09009550 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009551 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009552 data := android.AndroidMkDataForTest(t, ctx, bundle)
9553 var builder strings.Builder
9554 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9555 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009556 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009557}
9558
Jooyung Hana6d36672022-02-24 13:58:07 +09009559func TestApexOutputFileProducer(t *testing.T) {
9560 for _, tc := range []struct {
9561 name string
9562 ref string
9563 expected_data []string
9564 }{
9565 {
9566 name: "test_using_output",
9567 ref: ":myapex",
Jooyung Hana0503a52023-08-23 13:12:50 +09009568 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.capex:myapex.capex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009569 },
9570 {
9571 name: "test_using_apex",
9572 ref: ":myapex{.apex}",
Jooyung Hana0503a52023-08-23 13:12:50 +09009573 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.apex:myapex.apex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009574 },
9575 } {
9576 t.Run(tc.name, func(t *testing.T) {
9577 ctx := testApex(t, `
9578 apex {
9579 name: "myapex",
9580 key: "myapex.key",
9581 compressible: true,
9582 updatable: false,
9583 }
9584
9585 apex_key {
9586 name: "myapex.key",
9587 public_key: "testkey.avbpubkey",
9588 private_key: "testkey.pem",
9589 }
9590
9591 java_test {
9592 name: "`+tc.name+`",
9593 srcs: ["a.java"],
9594 data: ["`+tc.ref+`"],
9595 }
9596 `,
9597 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9598 variables.CompressedApex = proptools.BoolPtr(true)
9599 }))
9600 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9601 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9602 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9603 })
9604 }
9605}
9606
satayev758968a2021-12-06 11:42:40 +00009607func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9608 preparer := android.GroupFixturePreparers(
9609 PrepareForTestWithApexBuildComponents,
9610 prepareForTestWithMyapex,
9611 java.PrepareForTestWithJavaSdkLibraryFiles,
9612 java.PrepareForTestWithJavaDefaultModules,
9613 android.PrepareForTestWithAndroidBuildComponents,
9614 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9615 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9616 )
9617
9618 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9619 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9620 preparer.RunTestWithBp(t, `
9621 apex {
9622 name: "myapex",
9623 key: "myapex.key",
9624 bootclasspath_fragments: ["mybootclasspathfragment"],
9625 min_sdk_version: "30",
9626 updatable: false,
9627 }
9628
9629 apex_key {
9630 name: "myapex.key",
9631 public_key: "testkey.avbpubkey",
9632 private_key: "testkey.pem",
9633 }
9634
9635 bootclasspath_fragment {
9636 name: "mybootclasspathfragment",
9637 contents: ["mybootclasspathlib"],
9638 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009639 hidden_api: {
9640 split_packages: ["*"],
9641 },
satayev758968a2021-12-06 11:42:40 +00009642 }
9643
9644 java_sdk_library {
9645 name: "mybootclasspathlib",
9646 srcs: ["mybootclasspathlib.java"],
9647 apex_available: ["myapex"],
9648 compile_dex: true,
9649 unsafe_ignore_missing_latest_api: true,
9650 min_sdk_version: "31",
9651 static_libs: ["util"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00009652 sdk_version: "core_current",
satayev758968a2021-12-06 11:42:40 +00009653 }
9654
9655 java_library {
9656 name: "util",
9657 srcs: ["a.java"],
9658 apex_available: ["myapex"],
9659 min_sdk_version: "31",
9660 static_libs: ["another_util"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00009661 sdk_version: "core_current",
satayev758968a2021-12-06 11:42:40 +00009662 }
9663
9664 java_library {
9665 name: "another_util",
9666 srcs: ["a.java"],
9667 min_sdk_version: "31",
9668 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00009669 sdk_version: "core_current",
satayev758968a2021-12-06 11:42:40 +00009670 }
9671 `)
9672 })
9673
9674 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9675 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9676 preparer.RunTestWithBp(t, `
9677 apex {
9678 name: "myapex",
9679 key: "myapex.key",
9680 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9681 min_sdk_version: "30",
9682 updatable: false,
9683 }
9684
9685 apex_key {
9686 name: "myapex.key",
9687 public_key: "testkey.avbpubkey",
9688 private_key: "testkey.pem",
9689 }
9690
9691 systemserverclasspath_fragment {
9692 name: "mysystemserverclasspathfragment",
9693 contents: ["mysystemserverclasspathlib"],
9694 apex_available: ["myapex"],
9695 }
9696
9697 java_sdk_library {
9698 name: "mysystemserverclasspathlib",
9699 srcs: ["mysystemserverclasspathlib.java"],
9700 apex_available: ["myapex"],
9701 compile_dex: true,
9702 min_sdk_version: "32",
9703 unsafe_ignore_missing_latest_api: true,
9704 static_libs: ["util"],
9705 }
9706
9707 java_library {
9708 name: "util",
9709 srcs: ["a.java"],
9710 apex_available: ["myapex"],
9711 min_sdk_version: "31",
9712 static_libs: ["another_util"],
9713 }
9714
9715 java_library {
9716 name: "another_util",
9717 srcs: ["a.java"],
9718 min_sdk_version: "31",
9719 apex_available: ["myapex"],
9720 }
9721 `)
9722 })
9723
9724 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
Jihoon Kang85bc1932024-07-01 17:04:46 +00009725 preparer.
satayev758968a2021-12-06 11:42:40 +00009726 RunTestWithBp(t, `
9727 apex {
9728 name: "myapex",
9729 key: "myapex.key",
9730 bootclasspath_fragments: ["mybootclasspathfragment"],
9731 min_sdk_version: "30",
9732 updatable: false,
9733 }
9734
9735 apex_key {
9736 name: "myapex.key",
9737 public_key: "testkey.avbpubkey",
9738 private_key: "testkey.pem",
9739 }
9740
9741 bootclasspath_fragment {
9742 name: "mybootclasspathfragment",
9743 contents: ["mybootclasspathlib"],
9744 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009745 hidden_api: {
9746 split_packages: ["*"],
9747 },
satayev758968a2021-12-06 11:42:40 +00009748 }
9749
9750 java_sdk_library {
9751 name: "mybootclasspathlib",
9752 srcs: ["mybootclasspathlib.java"],
9753 apex_available: ["myapex"],
9754 compile_dex: true,
9755 unsafe_ignore_missing_latest_api: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00009756 sdk_version: "current",
9757 min_sdk_version: "30",
satayev758968a2021-12-06 11:42:40 +00009758 }
9759 `)
9760 })
9761
9762 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9763 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9764 RunTestWithBp(t, `
9765 apex {
9766 name: "myapex",
9767 key: "myapex.key",
9768 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9769 min_sdk_version: "30",
9770 updatable: false,
9771 }
9772
9773 apex_key {
9774 name: "myapex.key",
9775 public_key: "testkey.avbpubkey",
9776 private_key: "testkey.pem",
9777 }
9778
9779 systemserverclasspath_fragment {
9780 name: "mysystemserverclasspathfragment",
9781 contents: ["mysystemserverclasspathlib"],
9782 apex_available: ["myapex"],
9783 }
9784
9785 java_sdk_library {
9786 name: "mysystemserverclasspathlib",
9787 srcs: ["mysystemserverclasspathlib.java"],
9788 apex_available: ["myapex"],
9789 compile_dex: true,
9790 unsafe_ignore_missing_latest_api: true,
9791 }
9792 `)
9793 })
9794}
9795
Jiakai Zhang6decef92022-01-12 17:56:19 +00009796// Verifies that the APEX depends on all the Make modules in the list.
9797func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9798 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9799 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009800 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009801 }
9802}
9803
9804// Verifies that the APEX does not depend on any of the Make modules in the list.
9805func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9806 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9807 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009808 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009809 }
9810}
9811
Cole Faust24e25c02024-01-19 14:12:17 -08009812func TestApexStrictUpdtabilityLint(t *testing.T) {
9813 bpTemplate := `
9814 apex {
9815 name: "myapex",
9816 key: "myapex.key",
9817 java_libs: ["myjavalib"],
9818 updatable: %v,
9819 min_sdk_version: "29",
9820 }
9821 apex_key {
9822 name: "myapex.key",
9823 }
9824 java_library {
9825 name: "myjavalib",
9826 srcs: ["MyClass.java"],
9827 apex_available: [ "myapex" ],
9828 lint: {
9829 strict_updatability_linting: %v,
9830 %s
9831 },
9832 sdk_version: "current",
9833 min_sdk_version: "29",
9834 }
9835 `
9836 fs := android.MockFS{
9837 "lint-baseline.xml": nil,
9838 }
9839
9840 testCases := []struct {
Colin Cross87427352024-09-25 15:41:19 -07009841 testCaseName string
9842 apexUpdatable bool
9843 javaStrictUpdtabilityLint bool
9844 lintFileExists bool
9845 disallowedFlagExpectedOnApex bool
9846 disallowedFlagExpectedOnJavalib bool
Cole Faust24e25c02024-01-19 14:12:17 -08009847 }{
9848 {
Colin Cross87427352024-09-25 15:41:19 -07009849 testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9850 apexUpdatable: true,
9851 javaStrictUpdtabilityLint: true,
9852 lintFileExists: false,
9853 disallowedFlagExpectedOnApex: false,
9854 disallowedFlagExpectedOnJavalib: false,
Cole Faust24e25c02024-01-19 14:12:17 -08009855 },
9856 {
Colin Cross87427352024-09-25 15:41:19 -07009857 testCaseName: "non-updatable apex respects strict_updatability of javalib",
9858 apexUpdatable: false,
9859 javaStrictUpdtabilityLint: false,
9860 lintFileExists: true,
9861 disallowedFlagExpectedOnApex: false,
9862 disallowedFlagExpectedOnJavalib: false,
Cole Faust24e25c02024-01-19 14:12:17 -08009863 },
9864 {
Colin Cross87427352024-09-25 15:41:19 -07009865 testCaseName: "non-updatable apex respects strict updatability of javalib",
9866 apexUpdatable: false,
9867 javaStrictUpdtabilityLint: true,
9868 lintFileExists: true,
9869 disallowedFlagExpectedOnApex: false,
9870 disallowedFlagExpectedOnJavalib: true,
Cole Faust24e25c02024-01-19 14:12:17 -08009871 },
9872 {
Colin Cross87427352024-09-25 15:41:19 -07009873 testCaseName: "updatable apex checks strict updatability of javalib",
9874 apexUpdatable: true,
9875 javaStrictUpdtabilityLint: false,
9876 lintFileExists: true,
9877 disallowedFlagExpectedOnApex: true,
9878 disallowedFlagExpectedOnJavalib: false,
Cole Faust24e25c02024-01-19 14:12:17 -08009879 },
9880 }
9881
9882 for _, testCase := range testCases {
Colin Cross87427352024-09-25 15:41:19 -07009883 t.Run(testCase.testCaseName, func(t *testing.T) {
9884 fixtures := []android.FixturePreparer{}
9885 baselineProperty := ""
9886 if testCase.lintFileExists {
9887 fixtures = append(fixtures, fs.AddToFixture())
9888 baselineProperty = "baseline_filename: \"lint-baseline.xml\""
9889 }
9890 bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint, baselineProperty)
Cole Faust24e25c02024-01-19 14:12:17 -08009891
Colin Cross87427352024-09-25 15:41:19 -07009892 result := testApex(t, bp, fixtures...)
Cole Faust24e25c02024-01-19 14:12:17 -08009893
Colin Cross87427352024-09-25 15:41:19 -07009894 checkModule := func(m android.TestingBuildParams, name string, expectStrictUpdatability bool) {
9895 if expectStrictUpdatability {
9896 if m.Rule == nil {
9897 t.Errorf("expected strict updatability check rule on %s", name)
9898 } else {
9899 android.AssertStringDoesContain(t, fmt.Sprintf("strict updatability check rule for %s", name),
9900 m.RuleParams.Command, "--disallowed_issues NewApi")
9901 android.AssertStringListContains(t, fmt.Sprintf("strict updatability check baselines for %s", name),
9902 m.Inputs.Strings(), "lint-baseline.xml")
9903 }
9904 } else {
9905 if m.Rule != nil {
9906 t.Errorf("expected no strict updatability check rule on %s", name)
9907 }
9908 }
9909 }
9910
9911 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9912 apex := result.ModuleForTests("myapex", "android_common_myapex")
9913 apexStrictUpdatabilityCheck := apex.MaybeOutput("lint_strict_updatability_check.stamp")
9914 javalibStrictUpdatabilityCheck := myjavalib.MaybeOutput("lint_strict_updatability_check.stamp")
9915
9916 checkModule(apexStrictUpdatabilityCheck, "myapex", testCase.disallowedFlagExpectedOnApex)
9917 checkModule(javalibStrictUpdatabilityCheck, "myjavalib", testCase.disallowedFlagExpectedOnJavalib)
9918 })
Cole Faust24e25c02024-01-19 14:12:17 -08009919 }
9920}
9921
Cole Faust24e25c02024-01-19 14:12:17 -08009922// checks transtive deps of an apex coming from bootclasspath_fragment
9923func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9924 bp := `
9925 apex {
9926 name: "myapex",
9927 key: "myapex.key",
9928 bootclasspath_fragments: ["mybootclasspathfragment"],
9929 updatable: true,
9930 min_sdk_version: "29",
9931 }
9932 apex_key {
9933 name: "myapex.key",
9934 }
9935 bootclasspath_fragment {
9936 name: "mybootclasspathfragment",
9937 contents: ["myjavalib"],
9938 apex_available: ["myapex"],
9939 hidden_api: {
9940 split_packages: ["*"],
9941 },
9942 }
9943 java_library {
9944 name: "myjavalib",
9945 srcs: ["MyClass.java"],
9946 apex_available: [ "myapex" ],
9947 sdk_version: "current",
9948 min_sdk_version: "29",
9949 compile_dex: true,
9950 lint: {
9951 baseline_filename: "lint-baseline.xml",
9952 }
9953 }
9954 `
9955 fs := android.MockFS{
9956 "lint-baseline.xml": nil,
9957 }
9958
9959 result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
Colin Cross87427352024-09-25 15:41:19 -07009960 apex := result.ModuleForTests("myapex", "android_common_myapex")
9961 apexStrictUpdatabilityCheck := apex.Output("lint_strict_updatability_check.stamp")
9962 android.AssertStringDoesContain(t, "strict updatability check rule for myapex",
9963 apexStrictUpdatabilityCheck.RuleParams.Command, "--disallowed_issues NewApi")
9964 android.AssertStringListContains(t, "strict updatability check baselines for myapex",
9965 apexStrictUpdatabilityCheck.Inputs.Strings(), "lint-baseline.xml")
Cole Faust24e25c02024-01-19 14:12:17 -08009966}
Spandan Das66773252022-01-15 00:23:18 +00009967
Jihoon Kang784c0052024-06-25 22:15:39 +00009968func TestApexLintBcpFragmentSdkLibDeps(t *testing.T) {
9969 bp := `
9970 apex {
9971 name: "myapex",
9972 key: "myapex.key",
9973 bootclasspath_fragments: ["mybootclasspathfragment"],
9974 min_sdk_version: "29",
Jihoon Kang85bc1932024-07-01 17:04:46 +00009975 java_libs: [
9976 "jacocoagent",
9977 ],
Jihoon Kang784c0052024-06-25 22:15:39 +00009978 }
9979 apex_key {
9980 name: "myapex.key",
9981 }
9982 bootclasspath_fragment {
9983 name: "mybootclasspathfragment",
9984 contents: ["foo"],
9985 apex_available: ["myapex"],
9986 hidden_api: {
9987 split_packages: ["*"],
9988 },
9989 }
9990 java_sdk_library {
9991 name: "foo",
9992 srcs: ["MyClass.java"],
9993 apex_available: [ "myapex" ],
9994 sdk_version: "current",
9995 min_sdk_version: "29",
9996 compile_dex: true,
9997 }
9998 `
9999 fs := android.MockFS{
10000 "lint-baseline.xml": nil,
10001 }
10002
10003 result := android.GroupFixturePreparers(
10004 prepareForApexTest,
10005 java.PrepareForTestWithJavaSdkLibraryFiles,
10006 java.PrepareForTestWithJacocoInstrumentation,
10007 java.FixtureWithLastReleaseApis("foo"),
Jihoon Kang784c0052024-06-25 22:15:39 +000010008 android.FixtureMergeMockFs(fs),
10009 ).RunTestWithBp(t, bp)
10010
10011 myapex := result.ModuleForTests("myapex", "android_common_myapex")
10012 lintReportInputs := strings.Join(myapex.Output("lint-report-xml.zip").Inputs.Strings(), " ")
10013 android.AssertStringDoesContain(t,
10014 "myapex lint report expected to contain that of the sdk library impl lib as an input",
10015 lintReportInputs, "foo.impl")
10016}
10017
Spandan Das42e89502022-05-06 22:12:55 +000010018// updatable apexes should propagate updatable=true to its apps
10019func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
10020 bp := `
10021 apex {
10022 name: "myapex",
10023 key: "myapex.key",
Colin Cross99939e92024-10-01 16:02:46 -070010024 updatable: true,
Spandan Das42e89502022-05-06 22:12:55 +000010025 apps: [
10026 "myapp",
10027 ],
10028 min_sdk_version: "30",
10029 }
10030 apex_key {
10031 name: "myapex.key",
10032 }
10033 android_app {
10034 name: "myapp",
Spandan Das42e89502022-05-06 22:12:55 +000010035 apex_available: [
10036 "myapex",
10037 ],
10038 sdk_version: "current",
10039 min_sdk_version: "30",
10040 }
10041 `
Colin Cross99939e92024-10-01 16:02:46 -070010042 _ = android.GroupFixturePreparers(
10043 prepareForApexTest,
10044 ).ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("app dependency myapp must have updatable: true")).
10045 RunTestWithBp(t, bp)
Spandan Das42e89502022-05-06 22:12:55 +000010046}
10047
Dennis Shend4f5d932023-01-31 20:27:21 +000010048func TestTrimmedApex(t *testing.T) {
10049 bp := `
10050 apex {
10051 name: "myapex",
10052 key: "myapex.key",
10053 native_shared_libs: ["libfoo","libbaz"],
10054 min_sdk_version: "29",
10055 trim_against: "mydcla",
10056 }
10057 apex {
10058 name: "mydcla",
10059 key: "myapex.key",
10060 native_shared_libs: ["libfoo","libbar"],
10061 min_sdk_version: "29",
10062 file_contexts: ":myapex-file_contexts",
10063 dynamic_common_lib_apex: true,
10064 }
10065 apex_key {
10066 name: "myapex.key",
10067 }
10068 cc_library {
10069 name: "libfoo",
10070 shared_libs: ["libc"],
10071 apex_available: ["myapex","mydcla"],
10072 min_sdk_version: "29",
10073 }
10074 cc_library {
10075 name: "libbar",
10076 shared_libs: ["libc"],
10077 apex_available: ["myapex","mydcla"],
10078 min_sdk_version: "29",
10079 }
10080 cc_library {
10081 name: "libbaz",
10082 shared_libs: ["libc"],
10083 apex_available: ["myapex","mydcla"],
10084 min_sdk_version: "29",
10085 }
Dennis Shend4f5d932023-01-31 20:27:21 +000010086 `
10087 ctx := testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +090010088 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dennis Shend4f5d932023-01-31 20:27:21 +000010089 apexRule := module.MaybeRule("apexRule")
10090 if apexRule.Rule == nil {
10091 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10092 }
10093
10094 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
Jooyung Hana0503a52023-08-23 13:12:50 +090010095 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("TrimmedApexRule")
Dennis Shend4f5d932023-01-31 20:27:21 +000010096 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10097 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10098 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10099 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10100}
Jingwen Chendea7a642023-03-28 11:30:50 +000010101
10102func TestCannedFsConfig(t *testing.T) {
10103 ctx := testApex(t, `
10104 apex {
10105 name: "myapex",
10106 key: "myapex.key",
10107 updatable: false,
10108 }
10109
10110 apex_key {
10111 name: "myapex.key",
10112 public_key: "testkey.avbpubkey",
10113 private_key: "testkey.pem",
10114 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010115 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010116 generateFsRule := mod.Rule("generateFsConfig")
10117 cmd := generateFsRule.RuleParams.Command
10118
10119 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10120}
10121
10122func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10123 ctx := testApex(t, `
10124 apex {
10125 name: "myapex",
10126 key: "myapex.key",
10127 canned_fs_config: "my_config",
10128 updatable: false,
10129 }
10130
10131 apex_key {
10132 name: "myapex.key",
10133 public_key: "testkey.avbpubkey",
10134 private_key: "testkey.pem",
10135 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010136 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010137 generateFsRule := mod.Rule("generateFsConfig")
10138 cmd := generateFsRule.RuleParams.Command
10139
10140 // Ensure that canned_fs_config has "cat my_config" at the end
10141 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10142}
Spandan Das20fce2d2023-04-12 17:21:39 +000010143
10144func TestStubLibrariesMultipleApexViolation(t *testing.T) {
10145 testCases := []struct {
10146 desc string
10147 hasStubs bool
10148 apexAvailable string
10149 expectedError string
10150 }{
10151 {
10152 desc: "non-stub library can have multiple apex_available",
10153 hasStubs: false,
10154 apexAvailable: `["myapex", "otherapex"]`,
10155 },
10156 {
10157 desc: "stub library should not be available to anyapex",
10158 hasStubs: true,
10159 apexAvailable: `["//apex_available:anyapex"]`,
10160 expectedError: "Stub libraries should have a single apex_available.*anyapex",
10161 },
10162 {
10163 desc: "stub library should not be available to multiple apexes",
10164 hasStubs: true,
10165 apexAvailable: `["myapex", "otherapex"]`,
10166 expectedError: "Stub libraries should have a single apex_available.*myapex.*otherapex",
10167 },
10168 {
10169 desc: "stub library can be available to a core apex and a test apex",
10170 hasStubs: true,
10171 apexAvailable: `["myapex", "test_myapex"]`,
10172 },
10173 }
10174 bpTemplate := `
10175 cc_library {
10176 name: "libfoo",
10177 %v
10178 apex_available: %v,
10179 }
10180 apex {
10181 name: "myapex",
10182 key: "apex.key",
10183 updatable: false,
10184 native_shared_libs: ["libfoo"],
10185 }
10186 apex {
10187 name: "otherapex",
10188 key: "apex.key",
10189 updatable: false,
10190 }
10191 apex_test {
10192 name: "test_myapex",
10193 key: "apex.key",
10194 updatable: false,
10195 native_shared_libs: ["libfoo"],
10196 }
10197 apex_key {
10198 name: "apex.key",
10199 }
10200 `
10201 for _, tc := range testCases {
10202 stubs := ""
10203 if tc.hasStubs {
10204 stubs = `stubs: {symbol_file: "libfoo.map.txt"},`
10205 }
10206 bp := fmt.Sprintf(bpTemplate, stubs, tc.apexAvailable)
10207 mockFsFixturePreparer := android.FixtureModifyMockFS(func(fs android.MockFS) {
10208 fs["system/sepolicy/apex/test_myapex-file_contexts"] = nil
10209 })
10210 if tc.expectedError == "" {
10211 testApex(t, bp, mockFsFixturePreparer)
10212 } else {
10213 testApexError(t, tc.expectedError, bp, mockFsFixturePreparer)
10214 }
10215 }
10216}
Colin Crossbd3a16b2023-04-25 11:30:51 -070010217
10218func TestFileSystemShouldSkipApexLibraries(t *testing.T) {
10219 context := android.GroupFixturePreparers(
10220 android.PrepareForIntegrationTestWithAndroid,
10221 cc.PrepareForIntegrationTestWithCc,
10222 PrepareForTestWithApexBuildComponents,
10223 prepareForTestWithMyapex,
10224 filesystem.PrepareForTestWithFilesystemBuildComponents,
10225 )
10226 result := context.RunTestWithBp(t, `
10227 android_system_image {
10228 name: "myfilesystem",
10229 deps: [
10230 "libfoo",
10231 ],
10232 linker_config_src: "linker.config.json",
10233 }
10234
10235 cc_library {
10236 name: "libfoo",
10237 shared_libs: [
10238 "libbar",
10239 ],
10240 stl: "none",
10241 }
10242
10243 cc_library {
10244 name: "libbar",
10245 stl: "none",
10246 apex_available: ["myapex"],
10247 }
10248
10249 apex {
10250 name: "myapex",
10251 native_shared_libs: ["libbar"],
10252 key: "myapex.key",
10253 updatable: false,
10254 }
10255
10256 apex_key {
10257 name: "myapex.key",
10258 public_key: "testkey.avbpubkey",
10259 private_key: "testkey.pem",
10260 }
10261 `)
10262
Cole Faust3b806d32024-03-11 15:15:03 -070010263 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits
Colin Crossbd3a16b2023-04-25 11:30:51 -070010264 android.AssertStringListDoesNotContain(t, "filesystem should not have libbar",
10265 inputs.Strings(),
10266 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
10267}
Yu Liueae7b362023-11-16 17:05:47 -080010268
10269var apex_default_bp = `
10270 apex_key {
10271 name: "myapex.key",
10272 public_key: "testkey.avbpubkey",
10273 private_key: "testkey.pem",
10274 }
10275
10276 filegroup {
10277 name: "myapex.manifest",
10278 srcs: ["apex_manifest.json"],
10279 }
10280
10281 filegroup {
10282 name: "myapex.androidmanifest",
10283 srcs: ["AndroidManifest.xml"],
10284 }
10285`
10286
10287func TestAconfigFilesJavaDeps(t *testing.T) {
10288 ctx := testApex(t, apex_default_bp+`
10289 apex {
10290 name: "myapex",
10291 manifest: ":myapex.manifest",
10292 androidManifest: ":myapex.androidmanifest",
10293 key: "myapex.key",
10294 java_libs: [
10295 "my_java_library_foo",
10296 "my_java_library_bar",
10297 ],
10298 updatable: false,
10299 }
10300
10301 java_library {
10302 name: "my_java_library_foo",
10303 srcs: ["foo/bar/MyClass.java"],
10304 sdk_version: "none",
10305 system_modules: "none",
10306 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010307 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010308 "myapex",
10309 ],
10310 }
10311
10312 java_library {
10313 name: "my_java_library_bar",
10314 srcs: ["foo/bar/MyClass.java"],
10315 sdk_version: "none",
10316 system_modules: "none",
10317 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010318 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010319 "myapex",
10320 ],
10321 }
10322
10323 aconfig_declarations {
10324 name: "my_aconfig_declarations_foo",
10325 package: "com.example.package",
10326 container: "myapex",
10327 srcs: ["foo.aconfig"],
10328 }
10329
10330 java_aconfig_library {
10331 name: "my_java_aconfig_library_foo",
10332 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010333 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010334 "myapex",
10335 ],
10336 }
10337
10338 aconfig_declarations {
10339 name: "my_aconfig_declarations_bar",
10340 package: "com.example.package",
10341 container: "myapex",
10342 srcs: ["bar.aconfig"],
10343 }
10344
10345 java_aconfig_library {
10346 name: "my_java_aconfig_library_bar",
10347 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010348 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010349 "myapex",
10350 ],
10351 }
10352 `)
10353
10354 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10355 s := mod.Rule("apexRule").Args["copy_commands"]
10356 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Dennis Shen343d09e2024-09-23 17:19:15 +000010357 if len(copyCmds) != 14 {
10358 t.Fatalf("Expected 14 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010359 }
10360
Jooyung Hana3fddf42024-09-03 13:22:21 +090010361 ensureListContainsMatch(t, copyCmds, "^cp -f .*/aconfig_flags.pb .*/image.apex/etc/aconfig_flags.pb")
10362 ensureListContainsMatch(t, copyCmds, "^cp -f .*/package.map .*/image.apex/etc/package.map")
10363 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.map .*/image.apex/etc/flag.map")
10364 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.val .*/image.apex/etc/flag.val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010365 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.info.*/image.apex/etc/flag.info")
Yu Liueae7b362023-11-16 17:05:47 -080010366
Yu Liubba555e2024-02-17 00:36:42 +000010367 inputs := []string{
10368 "my_aconfig_declarations_foo/intermediate.pb",
10369 "my_aconfig_declarations_bar/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010370 }
Yu Liubba555e2024-02-17 00:36:42 +000010371 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10372 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10373 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10374 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010375 VerifyAconfigRule(t, &mod, "create_aconfig_flag_info_file", inputs, "android_common_myapex/flag.info", "myapex", "flag_info")
Yu Liueae7b362023-11-16 17:05:47 -080010376}
10377
10378func TestAconfigFilesJavaAndCcDeps(t *testing.T) {
10379 ctx := testApex(t, apex_default_bp+`
10380 apex {
10381 name: "myapex",
10382 manifest: ":myapex.manifest",
10383 androidManifest: ":myapex.androidmanifest",
10384 key: "myapex.key",
10385 java_libs: [
10386 "my_java_library_foo",
10387 ],
10388 native_shared_libs: [
10389 "my_cc_library_bar",
10390 ],
10391 binaries: [
10392 "my_cc_binary_baz",
10393 ],
10394 updatable: false,
10395 }
10396
10397 java_library {
10398 name: "my_java_library_foo",
10399 srcs: ["foo/bar/MyClass.java"],
10400 sdk_version: "none",
10401 system_modules: "none",
10402 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010403 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010404 "myapex",
10405 ],
10406 }
10407
10408 cc_library {
10409 name: "my_cc_library_bar",
10410 srcs: ["foo/bar/MyClass.cc"],
Yu Liucec0e412023-11-30 16:45:50 -080010411 static_libs: [
10412 "my_cc_aconfig_library_bar",
10413 "my_cc_aconfig_library_baz",
10414 ],
Yu Liueae7b362023-11-16 17:05:47 -080010415 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010416 "myapex",
10417 ],
10418 }
10419
10420 cc_binary {
10421 name: "my_cc_binary_baz",
10422 srcs: ["foo/bar/MyClass.cc"],
10423 static_libs: ["my_cc_aconfig_library_baz"],
Yu Liueae7b362023-11-16 17:05:47 -080010424 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010425 "myapex",
10426 ],
10427 }
10428
10429 aconfig_declarations {
10430 name: "my_aconfig_declarations_foo",
10431 package: "com.example.package",
10432 container: "myapex",
10433 srcs: ["foo.aconfig"],
10434 }
10435
10436 java_aconfig_library {
10437 name: "my_java_aconfig_library_foo",
10438 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010439 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010440 "myapex",
10441 ],
10442 }
10443
10444 aconfig_declarations {
10445 name: "my_aconfig_declarations_bar",
10446 package: "com.example.package",
10447 container: "myapex",
10448 srcs: ["bar.aconfig"],
10449 }
10450
10451 cc_aconfig_library {
10452 name: "my_cc_aconfig_library_bar",
10453 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010454 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010455 "myapex",
10456 ],
10457 }
10458
10459 aconfig_declarations {
10460 name: "my_aconfig_declarations_baz",
10461 package: "com.example.package",
10462 container: "myapex",
10463 srcs: ["baz.aconfig"],
10464 }
10465
10466 cc_aconfig_library {
10467 name: "my_cc_aconfig_library_baz",
10468 aconfig_declarations: "my_aconfig_declarations_baz",
Yu Liueae7b362023-11-16 17:05:47 -080010469 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010470 "myapex",
10471 ],
10472 }
10473
10474 cc_library {
10475 name: "server_configurable_flags",
10476 srcs: ["server_configurable_flags.cc"],
10477 }
Ted Bauerf0f18592024-04-23 18:25:26 +000010478 cc_library {
10479 name: "libbase",
10480 srcs: ["libbase.cc"],
Ted Bauer1e96f8c2024-04-25 19:50:01 +000010481 apex_available: [
10482 "myapex",
10483 ],
Ted Bauerf0f18592024-04-23 18:25:26 +000010484 }
10485 cc_library {
10486 name: "libaconfig_storage_read_api_cc",
10487 srcs: ["libaconfig_storage_read_api_cc.cc"],
10488 }
Yu Liueae7b362023-11-16 17:05:47 -080010489 `)
10490
10491 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10492 s := mod.Rule("apexRule").Args["copy_commands"]
10493 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Dennis Shen343d09e2024-09-23 17:19:15 +000010494 if len(copyCmds) != 18 {
10495 t.Fatalf("Expected 18 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010496 }
10497
Jooyung Hana3fddf42024-09-03 13:22:21 +090010498 ensureListContainsMatch(t, copyCmds, "^cp -f .*/aconfig_flags.pb .*/image.apex/etc/aconfig_flags.pb")
10499 ensureListContainsMatch(t, copyCmds, "^cp -f .*/package.map .*/image.apex/etc/package.map")
10500 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.map .*/image.apex/etc/flag.map")
10501 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.val .*/image.apex/etc/flag.val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010502 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.info .*/image.apex/etc/flag.info")
Yu Liueae7b362023-11-16 17:05:47 -080010503
Yu Liubba555e2024-02-17 00:36:42 +000010504 inputs := []string{
10505 "my_aconfig_declarations_foo/intermediate.pb",
10506 "my_cc_library_bar/android_arm64_armv8-a_shared_apex10000/myapex/aconfig_merged.pb",
10507 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010508 }
Yu Liubba555e2024-02-17 00:36:42 +000010509 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10510 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10511 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10512 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010513 VerifyAconfigRule(t, &mod, "create_aconfig_flag_info_file", inputs, "android_common_myapex/flag.info", "myapex", "flag_info")
Yu Liueae7b362023-11-16 17:05:47 -080010514}
10515
Yu Liucec0e412023-11-30 16:45:50 -080010516func TestAconfigFilesRustDeps(t *testing.T) {
10517 ctx := testApex(t, apex_default_bp+`
10518 apex {
10519 name: "myapex",
10520 manifest: ":myapex.manifest",
10521 androidManifest: ":myapex.androidmanifest",
10522 key: "myapex.key",
10523 native_shared_libs: [
10524 "libmy_rust_library",
10525 ],
10526 binaries: [
10527 "my_rust_binary",
10528 ],
10529 rust_dyn_libs: [
10530 "libmy_rust_dylib",
10531 ],
10532 updatable: false,
10533 }
10534
10535 rust_library {
10536 name: "libflags_rust", // test mock
10537 crate_name: "flags_rust",
10538 srcs: ["lib.rs"],
10539 apex_available: [
10540 "myapex",
10541 ],
10542 }
10543
10544 rust_library {
10545 name: "liblazy_static", // test mock
10546 crate_name: "lazy_static",
10547 srcs: ["src/lib.rs"],
10548 apex_available: [
10549 "myapex",
10550 ],
10551 }
10552
Ted Bauer02d475c2024-03-27 20:56:26 +000010553 rust_library {
10554 name: "libaconfig_storage_read_api", // test mock
10555 crate_name: "aconfig_storage_read_api",
10556 srcs: ["src/lib.rs"],
10557 apex_available: [
10558 "myapex",
10559 ],
10560 }
10561
Ted Bauer6ef40db2024-03-29 14:04:10 +000010562 rust_library {
10563 name: "liblogger", // test mock
10564 crate_name: "logger",
10565 srcs: ["src/lib.rs"],
10566 apex_available: [
10567 "myapex",
10568 ],
10569 }
10570
10571 rust_library {
10572 name: "liblog_rust", // test mock
10573 crate_name: "log_rust",
10574 srcs: ["src/lib.rs"],
10575 apex_available: [
10576 "myapex",
10577 ],
10578 }
10579
Yu Liucec0e412023-11-30 16:45:50 -080010580 rust_ffi_shared {
10581 name: "libmy_rust_library",
10582 srcs: ["src/lib.rs"],
10583 rustlibs: ["libmy_rust_aconfig_library_foo"],
10584 crate_name: "my_rust_library",
10585 apex_available: [
10586 "myapex",
10587 ],
10588 }
10589
10590 rust_library_dylib {
10591 name: "libmy_rust_dylib",
10592 srcs: ["foo/bar/MyClass.rs"],
10593 rustlibs: ["libmy_rust_aconfig_library_bar"],
10594 crate_name: "my_rust_dylib",
10595 apex_available: [
10596 "myapex",
10597 ],
10598 }
10599
10600 rust_binary {
10601 name: "my_rust_binary",
10602 srcs: ["foo/bar/MyClass.rs"],
10603 rustlibs: [
10604 "libmy_rust_aconfig_library_baz",
10605 "libmy_rust_dylib",
10606 ],
10607 apex_available: [
10608 "myapex",
10609 ],
10610 }
10611
10612 aconfig_declarations {
10613 name: "my_aconfig_declarations_foo",
10614 package: "com.example.package",
10615 container: "myapex",
10616 srcs: ["foo.aconfig"],
10617 }
10618
10619 aconfig_declarations {
10620 name: "my_aconfig_declarations_bar",
10621 package: "com.example.package",
10622 container: "myapex",
10623 srcs: ["bar.aconfig"],
10624 }
10625
10626 aconfig_declarations {
10627 name: "my_aconfig_declarations_baz",
10628 package: "com.example.package",
10629 container: "myapex",
10630 srcs: ["baz.aconfig"],
10631 }
10632
10633 rust_aconfig_library {
10634 name: "libmy_rust_aconfig_library_foo",
10635 aconfig_declarations: "my_aconfig_declarations_foo",
10636 crate_name: "my_rust_aconfig_library_foo",
10637 apex_available: [
10638 "myapex",
10639 ],
10640 }
10641
10642 rust_aconfig_library {
10643 name: "libmy_rust_aconfig_library_bar",
10644 aconfig_declarations: "my_aconfig_declarations_bar",
10645 crate_name: "my_rust_aconfig_library_bar",
10646 apex_available: [
10647 "myapex",
10648 ],
10649 }
10650
10651 rust_aconfig_library {
10652 name: "libmy_rust_aconfig_library_baz",
10653 aconfig_declarations: "my_aconfig_declarations_baz",
10654 crate_name: "my_rust_aconfig_library_baz",
10655 apex_available: [
10656 "myapex",
10657 ],
10658 }
10659 `)
10660
10661 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10662 s := mod.Rule("apexRule").Args["copy_commands"]
10663 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Dennis Shen343d09e2024-09-23 17:19:15 +000010664 if len(copyCmds) != 38 {
10665 t.Fatalf("Expected 38 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liucec0e412023-11-30 16:45:50 -080010666 }
10667
Jooyung Hana3fddf42024-09-03 13:22:21 +090010668 ensureListContainsMatch(t, copyCmds, "^cp -f .*/aconfig_flags.pb .*/image.apex/etc/aconfig_flags.pb")
10669 ensureListContainsMatch(t, copyCmds, "^cp -f .*/package.map .*/image.apex/etc/package.map")
10670 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.map .*/image.apex/etc/flag.map")
10671 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.val .*/image.apex/etc/flag.val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010672 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.info .*/image.apex/etc/flag.info")
Yu Liucec0e412023-11-30 16:45:50 -080010673
Yu Liubba555e2024-02-17 00:36:42 +000010674 inputs := []string{
10675 "my_aconfig_declarations_foo/intermediate.pb",
Yu Liuab31c822024-02-28 22:21:31 +000010676 "my_aconfig_declarations_bar/intermediate.pb",
10677 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liubba555e2024-02-17 00:36:42 +000010678 "my_rust_binary/android_arm64_armv8-a_apex10000/myapex/aconfig_merged.pb",
10679 }
10680 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10681 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10682 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10683 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010684 VerifyAconfigRule(t, &mod, "create_aconfig_flag_info_file", inputs, "android_common_myapex/flag.info", "myapex", "flag_info")
Yu Liubba555e2024-02-17 00:36:42 +000010685}
10686
10687func VerifyAconfigRule(t *testing.T, mod *android.TestingModule, desc string, inputs []string, output string, container string, file_type string) {
10688 aconfigRule := mod.Description(desc)
10689 s := " " + aconfigRule.Args["cache_files"]
Yu Liucec0e412023-11-30 16:45:50 -080010690 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
Yu Liubba555e2024-02-17 00:36:42 +000010691 if len(aconfigArgs) != len(inputs) {
10692 t.Fatalf("Expected %d commands, got %d in:\n%s", len(inputs), len(aconfigArgs), s)
Yu Liucec0e412023-11-30 16:45:50 -080010693 }
Yu Liucec0e412023-11-30 16:45:50 -080010694
Yu Liubba555e2024-02-17 00:36:42 +000010695 ensureEquals(t, container, aconfigRule.Args["container"])
10696 ensureEquals(t, file_type, aconfigRule.Args["file_type"])
10697
10698 buildParams := aconfigRule.BuildParams
10699 for _, input := range inputs {
10700 android.EnsureListContainsSuffix(t, aconfigArgs, input)
10701 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), input)
Yu Liucec0e412023-11-30 16:45:50 -080010702 }
Yu Liubba555e2024-02-17 00:36:42 +000010703
10704 ensureContains(t, buildParams.Output.String(), output)
Yu Liucec0e412023-11-30 16:45:50 -080010705}
10706
Yu Liueae7b362023-11-16 17:05:47 -080010707func TestAconfigFilesOnlyMatchCurrentApex(t *testing.T) {
10708 ctx := testApex(t, apex_default_bp+`
10709 apex {
10710 name: "myapex",
10711 manifest: ":myapex.manifest",
10712 androidManifest: ":myapex.androidmanifest",
10713 key: "myapex.key",
10714 java_libs: [
10715 "my_java_library_foo",
10716 "other_java_library_bar",
10717 ],
10718 updatable: false,
10719 }
10720
10721 java_library {
10722 name: "my_java_library_foo",
10723 srcs: ["foo/bar/MyClass.java"],
10724 sdk_version: "none",
10725 system_modules: "none",
10726 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010727 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010728 "myapex",
10729 ],
10730 }
10731
10732 java_library {
10733 name: "other_java_library_bar",
10734 srcs: ["foo/bar/MyClass.java"],
10735 sdk_version: "none",
10736 system_modules: "none",
10737 static_libs: ["other_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010738 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010739 "myapex",
10740 ],
10741 }
10742
10743 aconfig_declarations {
10744 name: "my_aconfig_declarations_foo",
10745 package: "com.example.package",
10746 container: "myapex",
10747 srcs: ["foo.aconfig"],
10748 }
10749
10750 java_aconfig_library {
10751 name: "my_java_aconfig_library_foo",
10752 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010753 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010754 "myapex",
10755 ],
10756 }
10757
10758 aconfig_declarations {
10759 name: "other_aconfig_declarations_bar",
10760 package: "com.example.package",
10761 container: "otherapex",
10762 srcs: ["bar.aconfig"],
10763 }
10764
10765 java_aconfig_library {
10766 name: "other_java_aconfig_library_bar",
10767 aconfig_declarations: "other_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010768 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010769 "myapex",
10770 ],
10771 }
10772 `)
10773
10774 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10775 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
10776 s := " " + combineAconfigRule.Args["cache_files"]
10777 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
10778 if len(aconfigArgs) != 1 {
10779 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
10780 }
10781 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
10782
10783 buildParams := combineAconfigRule.BuildParams
10784 if len(buildParams.Inputs) != 1 {
10785 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
10786 }
10787 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
10788 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
10789}
10790
10791func TestAconfigFilesRemoveDuplicates(t *testing.T) {
10792 ctx := testApex(t, apex_default_bp+`
10793 apex {
10794 name: "myapex",
10795 manifest: ":myapex.manifest",
10796 androidManifest: ":myapex.androidmanifest",
10797 key: "myapex.key",
10798 java_libs: [
10799 "my_java_library_foo",
10800 "my_java_library_bar",
10801 ],
10802 updatable: false,
10803 }
10804
10805 java_library {
10806 name: "my_java_library_foo",
10807 srcs: ["foo/bar/MyClass.java"],
10808 sdk_version: "none",
10809 system_modules: "none",
10810 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010811 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010812 "myapex",
10813 ],
10814 }
10815
10816 java_library {
10817 name: "my_java_library_bar",
10818 srcs: ["foo/bar/MyClass.java"],
10819 sdk_version: "none",
10820 system_modules: "none",
10821 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010822 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010823 "myapex",
10824 ],
10825 }
10826
10827 aconfig_declarations {
10828 name: "my_aconfig_declarations_foo",
10829 package: "com.example.package",
10830 container: "myapex",
10831 srcs: ["foo.aconfig"],
10832 }
10833
10834 java_aconfig_library {
10835 name: "my_java_aconfig_library_foo",
10836 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010837 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010838 "myapex",
10839 ],
10840 }
10841
10842 java_aconfig_library {
10843 name: "my_java_aconfig_library_bar",
10844 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010845 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010846 "myapex",
10847 ],
10848 }
10849 `)
10850
10851 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10852 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
10853 s := " " + combineAconfigRule.Args["cache_files"]
10854 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
10855 if len(aconfigArgs) != 1 {
10856 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
10857 }
10858 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
10859
10860 buildParams := combineAconfigRule.BuildParams
10861 if len(buildParams.Inputs) != 1 {
10862 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
10863 }
10864 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
10865 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
10866}
Spandan Das5be63332023-12-13 00:06:32 +000010867
10868// Test that the boot jars come from the _selected_ apex prebuilt
10869// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
10870func TestBootDexJarsMultipleApexPrebuilts(t *testing.T) {
10871 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
10872 t.Helper()
10873 s := ctx.ModuleForTests("dex_bootjars", "android_common")
10874 foundLibfooJar := false
10875 base := stem + ".jar"
10876 for _, output := range s.AllOutputs() {
10877 if filepath.Base(output) == base {
10878 foundLibfooJar = true
10879 buildRule := s.Output(output)
10880 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
10881 }
10882 }
10883 if !foundLibfooJar {
10884 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
10885 }
10886 }
10887
Spandan Das64c9e0c2023-12-20 20:13:34 +000010888 // Check that the boot jars of the selected apex are run through boot_jars_package_check
10889 // This validates that the jars on the bootclasspath do not contain packages outside an allowlist
10890 checkBootJarsPackageCheck := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
10891 platformBcp := ctx.ModuleForTests("platform-bootclasspath", "android_common")
10892 bootJarsCheckRule := platformBcp.Rule("boot_jars_package_check")
10893 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)
10894 }
10895
10896 // Check that the boot jars used to generate the monolithic hiddenapi flags come from the selected apex
10897 checkBootJarsForMonolithicHiddenapi := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
10898 monolithicHiddenapiFlagsCmd := ctx.ModuleForTests("platform-bootclasspath", "android_common").Output("out/soong/hiddenapi/hiddenapi-stub-flags.txt").RuleParams.Command
10899 android.AssertStringMatches(t, "Could not find the correct boot dex jar in monolithic hiddenapi flags generation command", monolithicHiddenapiFlagsCmd, "--boot-dex="+expectedBootJar)
10900 }
10901
Spandan Das5be63332023-12-13 00:06:32 +000010902 bp := `
10903 // Source APEX.
10904
10905 java_library {
10906 name: "framework-foo",
10907 srcs: ["foo.java"],
10908 installable: true,
10909 apex_available: [
10910 "com.android.foo",
10911 ],
10912 }
10913
10914 bootclasspath_fragment {
10915 name: "foo-bootclasspath-fragment",
10916 contents: ["framework-foo"],
10917 apex_available: [
10918 "com.android.foo",
10919 ],
10920 hidden_api: {
10921 split_packages: ["*"],
10922 },
10923 }
10924
10925 apex_key {
10926 name: "com.android.foo.key",
10927 public_key: "com.android.foo.avbpubkey",
10928 private_key: "com.android.foo.pem",
10929 }
10930
10931 apex {
10932 name: "com.android.foo",
10933 key: "com.android.foo.key",
10934 bootclasspath_fragments: ["foo-bootclasspath-fragment"],
10935 updatable: false,
10936 }
10937
10938 // Prebuilt APEX.
10939
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000010940 java_sdk_library_import {
Spandan Das5be63332023-12-13 00:06:32 +000010941 name: "framework-foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000010942 public: {
10943 jars: ["foo.jar"],
10944 },
Spandan Das5be63332023-12-13 00:06:32 +000010945 apex_available: ["com.android.foo"],
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000010946 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +000010947 }
10948
10949 prebuilt_bootclasspath_fragment {
10950 name: "foo-bootclasspath-fragment",
10951 contents: ["framework-foo"],
10952 hidden_api: {
10953 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
10954 metadata: "my-bootclasspath-fragment/metadata.csv",
10955 index: "my-bootclasspath-fragment/index.csv",
10956 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
10957 all_flags: "my-bootclasspath-fragment/all-flags.csv",
10958 },
10959 apex_available: [
10960 "com.android.foo",
10961 ],
10962 }
10963
10964 prebuilt_apex {
10965 name: "com.android.foo",
10966 apex_name: "com.android.foo",
10967 src: "com.android.foo-arm.apex",
10968 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
10969 }
10970
10971 // Another Prebuilt ART APEX
10972 prebuilt_apex {
10973 name: "com.android.foo.v2",
10974 apex_name: "com.android.foo", // Used to determine the API domain
10975 src: "com.android.foo-arm.apex",
10976 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
10977 }
10978
10979 // APEX contribution modules
10980
10981 apex_contributions {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000010982 name: "foo.source.contributions",
Spandan Das5be63332023-12-13 00:06:32 +000010983 api_domain: "com.android.foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000010984 contents: ["com.android.foo"],
10985 }
10986
10987 apex_contributions {
10988 name: "foo.prebuilt.contributions",
10989 api_domain: "com.android.foo",
10990 contents: ["prebuilt_com.android.foo"],
10991 }
10992
10993 apex_contributions {
10994 name: "foo.prebuilt.v2.contributions",
10995 api_domain: "com.android.foo",
10996 contents: ["com.android.foo.v2"], // prebuilt_ prefix is missing because of prebuilt_rename mutator
Spandan Das5be63332023-12-13 00:06:32 +000010997 }
10998 `
10999
11000 testCases := []struct {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011001 desc string
11002 selectedApexContributions string
11003 expectedBootJar string
Spandan Das5be63332023-12-13 00:06:32 +000011004 }{
11005 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011006 desc: "Source apex com.android.foo is selected, bootjar should come from source java library",
11007 selectedApexContributions: "foo.source.contributions",
11008 expectedBootJar: "out/soong/.intermediates/foo-bootclasspath-fragment/android_common_apex10000/hiddenapi-modular/encoded/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011009 },
11010 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011011 desc: "Prebuilt apex prebuilt_com.android.foo is selected, profile should come from .prof deapexed from the prebuilt",
11012 selectedApexContributions: "foo.prebuilt.contributions",
Spandan Das52c01a12024-09-20 01:09:48 +000011013 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 +000011014 },
11015 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011016 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected, profile should come from .prof deapexed from the prebuilt",
11017 selectedApexContributions: "foo.prebuilt.v2.contributions",
Spandan Das52c01a12024-09-20 01:09:48 +000011018 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 +000011019 },
11020 }
11021
11022 fragment := java.ApexVariantReference{
11023 Apex: proptools.StringPtr("com.android.foo"),
11024 Module: proptools.StringPtr("foo-bootclasspath-fragment"),
11025 }
11026
11027 for _, tc := range testCases {
11028 preparer := android.GroupFixturePreparers(
11029 java.FixtureConfigureApexBootJars("com.android.foo:framework-foo"),
11030 android.FixtureMergeMockFs(map[string][]byte{
11031 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11032 }),
Spandan Das81fe4d12024-05-15 18:43:47 +000011033 // Make sure that we have atleast one platform library so that we can check the monolithic hiddenapi
11034 // file creation.
11035 java.FixtureConfigureBootJars("platform:foo"),
11036 android.FixtureModifyMockFS(func(fs android.MockFS) {
11037 fs["platform/Android.bp"] = []byte(`
11038 java_library {
11039 name: "foo",
11040 srcs: ["Test.java"],
11041 compile_dex: true,
11042 }
11043 `)
11044 fs["platform/Test.java"] = nil
11045 }),
11046
Colin Crossa66b4632024-08-08 15:50:47 -070011047 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das5be63332023-12-13 00:06:32 +000011048 )
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011049 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das5be63332023-12-13 00:06:32 +000011050 checkBootDexJarPath(t, ctx, "framework-foo", tc.expectedBootJar)
Spandan Das64c9e0c2023-12-20 20:13:34 +000011051 checkBootJarsPackageCheck(t, ctx, tc.expectedBootJar)
11052 checkBootJarsForMonolithicHiddenapi(t, ctx, tc.expectedBootJar)
Spandan Das5be63332023-12-13 00:06:32 +000011053 }
11054}
Spandan Das3576e762024-01-03 18:57:03 +000011055
11056// Test that product packaging installs the selected mainline module (either source or a specific prebuilt)
11057// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11058func TestInstallationRulesForMultipleApexPrebuilts(t *testing.T) {
Spandan Das3576e762024-01-03 18:57:03 +000011059 // for a mainline module family, check that only the flagged soong module is visible to make
11060 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleName string, hiddenModuleNames []string) {
11061 variation := func(moduleName string) string {
11062 ret := "android_common_com.android.foo"
11063 if moduleName == "com.google.android.foo" {
Spandan Das50801e22024-05-13 18:29:45 +000011064 ret = "android_common_com.google.android.foo_com.google.android.foo"
Spandan Das3576e762024-01-03 18:57:03 +000011065 }
11066 return ret
11067 }
11068
11069 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11070 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11071
11072 for _, hiddenModuleName := range hiddenModuleNames {
11073 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11074 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11075
11076 }
11077 }
11078
11079 bp := `
11080 apex_key {
11081 name: "com.android.foo.key",
11082 public_key: "com.android.foo.avbpubkey",
11083 private_key: "com.android.foo.pem",
11084 }
11085
11086 // AOSP source apex
11087 apex {
11088 name: "com.android.foo",
11089 key: "com.android.foo.key",
11090 updatable: false,
11091 }
11092
11093 // Google source apex
11094 override_apex {
11095 name: "com.google.android.foo",
11096 base: "com.android.foo",
11097 key: "com.android.foo.key",
11098 }
11099
11100 // Prebuilt Google APEX.
11101
11102 prebuilt_apex {
11103 name: "com.google.android.foo",
11104 apex_name: "com.android.foo",
11105 src: "com.android.foo-arm.apex",
11106 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11107 }
11108
11109 // Another Prebuilt Google APEX
11110 prebuilt_apex {
11111 name: "com.google.android.foo.v2",
11112 apex_name: "com.android.foo",
Spandan Dasa8e2d612024-07-26 19:24:27 +000011113 source_apex_name: "com.google.android.foo",
Spandan Das3576e762024-01-03 18:57:03 +000011114 src: "com.android.foo-arm.apex",
11115 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11116 }
11117
11118 // APEX contribution modules
11119
11120 apex_contributions {
11121 name: "foo.source.contributions",
11122 api_domain: "com.android.foo",
11123 contents: ["com.google.android.foo"],
11124 }
11125
11126 apex_contributions {
11127 name: "foo.prebuilt.contributions",
11128 api_domain: "com.android.foo",
11129 contents: ["prebuilt_com.google.android.foo"],
11130 }
11131
11132 apex_contributions {
11133 name: "foo.prebuilt.v2.contributions",
11134 api_domain: "com.android.foo",
11135 contents: ["prebuilt_com.google.android.foo.v2"],
11136 }
11137
11138 // This is an incompatible module because it selects multiple versions of the same mainline module
11139 apex_contributions {
11140 name: "foo.prebuilt.duplicate.contributions",
11141 api_domain: "com.android.foo",
11142 contents: [
11143 "prebuilt_com.google.android.foo",
11144 "prebuilt_com.google.android.foo.v2",
11145 ],
11146 }
11147 `
11148
11149 testCases := []struct {
11150 desc string
11151 selectedApexContributions string
11152 expectedVisibleModuleName string
11153 expectedHiddenModuleNames []string
11154 expectedError string
11155 }{
11156 {
11157 desc: "Source apex is selected, prebuilts should be hidden from make",
11158 selectedApexContributions: "foo.source.contributions",
11159 expectedVisibleModuleName: "com.google.android.foo",
11160 expectedHiddenModuleNames: []string{"prebuilt_com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11161 },
11162 {
11163 desc: "Prebuilt apex prebuilt_com.android.foo is selected, source and the other prebuilt should be hidden from make",
11164 selectedApexContributions: "foo.prebuilt.contributions",
11165 expectedVisibleModuleName: "prebuilt_com.google.android.foo",
11166 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11167 },
11168 {
11169 desc: "Prebuilt apex prebuilt_com.android.fooi.v2 is selected, source and the other prebuilt should be hidden from make",
11170 selectedApexContributions: "foo.prebuilt.v2.contributions",
11171 expectedVisibleModuleName: "prebuilt_com.google.android.foo.v2",
11172 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo"},
11173 },
11174 {
11175 desc: "Multiple versions of a prebuilt apex is selected in the same release config",
11176 selectedApexContributions: "foo.prebuilt.duplicate.contributions",
11177 expectedError: "Found duplicate variations of the same module in apex_contributions: prebuilt_com.google.android.foo and prebuilt_com.google.android.foo.v2",
11178 },
11179 }
11180
11181 for _, tc := range testCases {
11182 preparer := android.GroupFixturePreparers(
11183 android.FixtureMergeMockFs(map[string][]byte{
11184 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11185 }),
Colin Crossa66b4632024-08-08 15:50:47 -070011186 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das3576e762024-01-03 18:57:03 +000011187 )
11188 if tc.expectedError != "" {
11189 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(tc.expectedError))
11190 testApex(t, bp, preparer)
11191 return
11192 }
11193 ctx := testApex(t, bp, preparer)
11194
Spandan Das3576e762024-01-03 18:57:03 +000011195 // Check that
11196 // 1. The contents of the selected apex_contributions are visible to make
11197 // 2. The rest of the apexes in the mainline module family (source or other prebuilt) is hidden from make
11198 checkHideFromMake(t, ctx, tc.expectedVisibleModuleName, tc.expectedHiddenModuleNames)
11199 }
11200}
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011201
Spandan Das85bd4622024-08-01 00:51:20 +000011202// Test that product packaging installs the selected mainline module in workspaces withtout source mainline module
11203func TestInstallationRulesForMultipleApexPrebuiltsWithoutSource(t *testing.T) {
11204 // for a mainline module family, check that only the flagged soong module is visible to make
11205 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleNames []string, hiddenModuleNames []string) {
11206 variation := func(moduleName string) string {
11207 ret := "android_common_com.android.adservices"
11208 if moduleName == "com.google.android.foo" {
11209 ret = "android_common_com.google.android.foo_com.google.android.foo"
11210 }
11211 return ret
11212 }
11213
11214 for _, visibleModuleName := range visibleModuleNames {
11215 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11216 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11217 }
11218
11219 for _, hiddenModuleName := range hiddenModuleNames {
11220 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11221 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11222
11223 }
11224 }
11225
11226 bp := `
11227 apex_key {
11228 name: "com.android.adservices.key",
11229 public_key: "com.android.adservices.avbpubkey",
11230 private_key: "com.android.adservices.pem",
11231 }
11232
11233 // AOSP source apex
11234 apex {
11235 name: "com.android.adservices",
11236 key: "com.android.adservices.key",
11237 updatable: false,
11238 }
11239
11240 // Prebuilt Google APEX.
11241
11242 prebuilt_apex {
11243 name: "com.google.android.adservices",
11244 apex_name: "com.android.adservices",
11245 src: "com.android.foo-arm.apex",
11246 }
11247
11248 // Another Prebuilt Google APEX
11249 prebuilt_apex {
11250 name: "com.google.android.adservices.v2",
11251 apex_name: "com.android.adservices",
11252 src: "com.android.foo-arm.apex",
11253 }
11254
11255 // APEX contribution modules
11256
11257
11258 apex_contributions {
11259 name: "adservices.prebuilt.contributions",
11260 api_domain: "com.android.adservices",
11261 contents: ["prebuilt_com.google.android.adservices"],
11262 }
11263
11264 apex_contributions {
11265 name: "adservices.prebuilt.v2.contributions",
11266 api_domain: "com.android.adservices",
11267 contents: ["prebuilt_com.google.android.adservices.v2"],
11268 }
11269 `
11270
11271 testCases := []struct {
11272 desc string
11273 selectedApexContributions string
11274 expectedVisibleModuleNames []string
11275 expectedHiddenModuleNames []string
11276 }{
11277 {
11278 desc: "No apex contributions selected, source aosp apex should be visible, and mainline prebuilts should be hidden",
11279 selectedApexContributions: "",
11280 expectedVisibleModuleNames: []string{"com.android.adservices"},
11281 expectedHiddenModuleNames: []string{"com.google.android.adservices", "com.google.android.adservices.v2"},
11282 },
11283 {
11284 desc: "Prebuilt apex prebuilt_com.android.foo is selected",
11285 selectedApexContributions: "adservices.prebuilt.contributions",
11286 expectedVisibleModuleNames: []string{"com.android.adservices", "com.google.android.adservices"},
11287 expectedHiddenModuleNames: []string{"com.google.android.adservices.v2"},
11288 },
11289 {
11290 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected",
11291 selectedApexContributions: "adservices.prebuilt.v2.contributions",
11292 expectedVisibleModuleNames: []string{"com.android.adservices", "com.google.android.adservices.v2"},
11293 expectedHiddenModuleNames: []string{"com.google.android.adservices"},
11294 },
11295 }
11296
11297 for _, tc := range testCases {
11298 preparer := android.GroupFixturePreparers(
11299 android.FixtureMergeMockFs(map[string][]byte{
11300 "system/sepolicy/apex/com.android.adservices-file_contexts": nil,
11301 }),
Colin Crossa66b4632024-08-08 15:50:47 -070011302 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das85bd4622024-08-01 00:51:20 +000011303 )
11304 ctx := testApex(t, bp, preparer)
11305
11306 checkHideFromMake(t, ctx, tc.expectedVisibleModuleNames, tc.expectedHiddenModuleNames)
11307 }
11308}
11309
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011310func TestAconfifDeclarationsValidation(t *testing.T) {
11311 aconfigDeclarationLibraryString := func(moduleNames []string) (ret string) {
11312 for _, moduleName := range moduleNames {
11313 ret += fmt.Sprintf(`
11314 aconfig_declarations {
11315 name: "%[1]s",
11316 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +000011317 container: "system",
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011318 srcs: [
11319 "%[1]s.aconfig",
11320 ],
11321 }
11322 java_aconfig_library {
11323 name: "%[1]s-lib",
11324 aconfig_declarations: "%[1]s",
11325 }
11326 `, moduleName)
11327 }
11328 return ret
11329 }
11330
11331 result := android.GroupFixturePreparers(
11332 prepareForApexTest,
11333 java.PrepareForTestWithJavaSdkLibraryFiles,
11334 java.FixtureWithLastReleaseApis("foo"),
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011335 ).RunTestWithBp(t, `
11336 java_library {
11337 name: "baz-java-lib",
11338 static_libs: [
11339 "baz-lib",
11340 ],
11341 }
11342 filegroup {
11343 name: "qux-filegroup",
11344 srcs: [
11345 ":qux-lib{.generated_srcjars}",
11346 ],
11347 }
11348 filegroup {
11349 name: "qux-another-filegroup",
11350 srcs: [
11351 ":qux-filegroup",
11352 ],
11353 }
11354 java_library {
11355 name: "quux-java-lib",
11356 srcs: [
11357 "a.java",
11358 ],
11359 libs: [
11360 "quux-lib",
11361 ],
11362 }
11363 java_sdk_library {
11364 name: "foo",
11365 srcs: [
11366 ":qux-another-filegroup",
11367 ],
11368 api_packages: ["foo"],
11369 system: {
11370 enabled: true,
11371 },
11372 module_lib: {
11373 enabled: true,
11374 },
11375 test: {
11376 enabled: true,
11377 },
11378 static_libs: [
11379 "bar-lib",
11380 ],
11381 libs: [
11382 "baz-java-lib",
11383 "quux-java-lib",
11384 ],
11385 aconfig_declarations: [
11386 "bar",
11387 ],
11388 }
11389 `+aconfigDeclarationLibraryString([]string{"bar", "baz", "qux", "quux"}))
11390
11391 m := result.ModuleForTests("foo.stubs.source", "android_common")
11392 outDir := "out/soong/.intermediates"
11393
11394 // Arguments passed to aconfig to retrieve the state of the flags defined in the
11395 // textproto files
11396 aconfigFlagArgs := m.Output("released-flagged-apis-exportable.txt").Args["flags_path"]
11397
11398 // "bar-lib" is a static_lib of "foo" and is passed to metalava as classpath. Thus the
11399 // cache file provided by the associated aconfig_declarations module "bar" should be passed
11400 // to aconfig.
11401 android.AssertStringDoesContain(t, "cache file of a java_aconfig_library static_lib "+
11402 "passed as an input",
11403 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "bar"))
11404
11405 // "baz-java-lib", which statically depends on "baz-lib", is a lib of "foo" and is passed
11406 // to metalava as classpath. Thus the cache file provided by the associated
11407 // aconfig_declarations module "baz" should be passed to aconfig.
11408 android.AssertStringDoesContain(t, "cache file of a lib that statically depends on "+
11409 "java_aconfig_library passed as an input",
11410 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "baz"))
11411
11412 // "qux-lib" is passed to metalava as src via the filegroup, thus the cache file provided by
11413 // the associated aconfig_declarations module "qux" should be passed to aconfig.
11414 android.AssertStringDoesContain(t, "cache file of srcs java_aconfig_library passed as an "+
11415 "input",
11416 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "qux"))
11417
11418 // "quux-java-lib" is a lib of "foo" and is passed to metalava as classpath, but does not
11419 // statically depend on "quux-lib". Therefore, the cache file provided by the associated
11420 // aconfig_declarations module "quux" should not be passed to aconfig.
11421 android.AssertStringDoesNotContain(t, "cache file of a lib that does not statically "+
11422 "depend on java_aconfig_library not passed as an input",
11423 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "quux"))
11424}
Cole Faust7c991b42024-05-15 11:17:55 -070011425
11426func TestMultiplePrebuiltsWithSameBase(t *testing.T) {
11427 ctx := testApex(t, `
11428 apex {
11429 name: "myapex",
11430 key: "myapex.key",
11431 prebuilts: ["myetc", "myetc2"],
11432 min_sdk_version: "29",
11433 }
11434 apex_key {
11435 name: "myapex.key",
11436 public_key: "testkey.avbpubkey",
11437 private_key: "testkey.pem",
11438 }
11439
11440 prebuilt_etc {
11441 name: "myetc",
11442 src: "myprebuilt",
11443 filename: "myfilename",
11444 }
11445 prebuilt_etc {
11446 name: "myetc2",
11447 sub_dir: "mysubdir",
11448 src: "myprebuilt",
11449 filename: "myfilename",
11450 }
11451 `, withFiles(android.MockFS{
11452 "packages/modules/common/build/allowed_deps.txt": nil,
11453 }))
11454
11455 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
11456 data := android.AndroidMkDataForTest(t, ctx, ab)
11457 var builder strings.Builder
11458 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
11459 androidMk := builder.String()
11460
11461 android.AssertStringDoesContain(t, "not found", androidMk, "LOCAL_MODULE := etc_myfilename.myapex")
11462 android.AssertStringDoesContain(t, "not found", androidMk, "LOCAL_MODULE := etc_mysubdir_myfilename.myapex")
11463}
Spandan Das50801e22024-05-13 18:29:45 +000011464
11465func TestApexMinSdkVersionOverride(t *testing.T) {
11466 checkMinSdkVersion := func(t *testing.T, module android.TestingModule, expectedMinSdkVersion string) {
11467 args := module.Rule("apexRule").Args
11468 optFlags := args["opt_flags"]
11469 if !strings.Contains(optFlags, "--min_sdk_version "+expectedMinSdkVersion) {
11470 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", module.Module(), expectedMinSdkVersion, optFlags)
11471 }
11472 }
11473
11474 checkHasDep := func(t *testing.T, ctx *android.TestContext, m android.Module, wantDep android.Module) {
11475 t.Helper()
11476 found := false
11477 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
11478 if dep == wantDep {
11479 found = true
11480 }
11481 })
11482 if !found {
11483 t.Errorf("Could not find a dependency from %v to %v\n", m, wantDep)
11484 }
11485 }
11486
11487 ctx := testApex(t, `
11488 apex {
11489 name: "com.android.apex30",
11490 min_sdk_version: "30",
11491 key: "apex30.key",
11492 java_libs: ["javalib"],
11493 }
11494
11495 java_library {
11496 name: "javalib",
11497 srcs: ["A.java"],
11498 apex_available: ["com.android.apex30"],
11499 min_sdk_version: "30",
11500 sdk_version: "current",
11501 }
11502
11503 override_apex {
11504 name: "com.mycompany.android.apex30",
11505 base: "com.android.apex30",
11506 }
11507
11508 override_apex {
11509 name: "com.mycompany.android.apex31",
11510 base: "com.android.apex30",
11511 min_sdk_version: "31",
11512 }
11513
11514 apex_key {
11515 name: "apex30.key",
11516 public_key: "testkey.avbpubkey",
11517 private_key: "testkey.pem",
11518 }
11519
11520 `, android.FixtureMergeMockFs(android.MockFS{
11521 "system/sepolicy/apex/com.android.apex30-file_contexts": nil,
11522 }),
11523 )
11524
11525 baseModule := ctx.ModuleForTests("com.android.apex30", "android_common_com.android.apex30")
11526 checkMinSdkVersion(t, baseModule, "30")
11527
11528 // Override module, but uses same min_sdk_version
11529 overridingModuleSameMinSdkVersion := ctx.ModuleForTests("com.android.apex30", "android_common_com.mycompany.android.apex30_com.mycompany.android.apex30")
11530 javalibApex30Variant := ctx.ModuleForTests("javalib", "android_common_apex30")
11531 checkMinSdkVersion(t, overridingModuleSameMinSdkVersion, "30")
11532 checkHasDep(t, ctx, overridingModuleSameMinSdkVersion.Module(), javalibApex30Variant.Module())
11533
11534 // Override module, uses different min_sdk_version
11535 overridingModuleDifferentMinSdkVersion := ctx.ModuleForTests("com.android.apex30", "android_common_com.mycompany.android.apex31_com.mycompany.android.apex31")
11536 javalibApex31Variant := ctx.ModuleForTests("javalib", "android_common_apex31")
11537 checkMinSdkVersion(t, overridingModuleDifferentMinSdkVersion, "31")
11538 checkHasDep(t, ctx, overridingModuleDifferentMinSdkVersion.Module(), javalibApex31Variant.Module())
11539}
Spandan Das0b28fa02024-05-28 23:40:17 +000011540
11541func TestOverrideApexWithPrebuiltApexPreferred(t *testing.T) {
11542 context := android.GroupFixturePreparers(
11543 android.PrepareForIntegrationTestWithAndroid,
11544 PrepareForTestWithApexBuildComponents,
11545 android.FixtureMergeMockFs(android.MockFS{
11546 "system/sepolicy/apex/foo-file_contexts": nil,
11547 }),
11548 )
11549 res := context.RunTestWithBp(t, `
11550 apex {
11551 name: "foo",
11552 key: "myapex.key",
11553 apex_available_name: "com.android.foo",
11554 variant_version: "0",
11555 updatable: false,
11556 }
11557 apex_key {
11558 name: "myapex.key",
11559 public_key: "testkey.avbpubkey",
11560 private_key: "testkey.pem",
11561 }
11562 prebuilt_apex {
11563 name: "foo",
11564 src: "foo.apex",
11565 prefer: true,
11566 }
11567 override_apex {
11568 name: "myoverrideapex",
11569 base: "foo",
11570 }
11571 `)
11572
11573 java.CheckModuleHasDependency(t, res.TestContext, "myoverrideapex", "android_common_myoverrideapex_myoverrideapex", "foo")
11574}
Spandan Dasca1d63e2024-07-01 22:53:49 +000011575
11576func TestUpdatableApexMinSdkVersionCurrent(t *testing.T) {
11577 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`, `
11578 apex {
11579 name: "myapex",
11580 key: "myapex.key",
11581 updatable: true,
11582 min_sdk_version: "current",
11583 }
11584
11585 apex_key {
11586 name: "myapex.key",
11587 public_key: "testkey.avbpubkey",
11588 private_key: "testkey.pem",
11589 }
11590 `)
11591}
Spandan Das2f68f192024-07-22 19:25:50 +000011592
11593func TestPrebuiltStubNoinstall(t *testing.T) {
11594 testFunc := func(t *testing.T, expectLibfooOnSystemLib bool, fs android.MockFS) {
11595 result := android.GroupFixturePreparers(
11596 prepareForApexTest,
11597 android.PrepareForTestWithAndroidMk,
11598 android.PrepareForTestWithMakevars,
11599 android.FixtureMergeMockFs(fs),
11600 ).RunTest(t)
11601
11602 ldRule := result.ModuleForTests("installedlib", "android_arm64_armv8-a_shared").Rule("ld")
Spandan Das357ffcc2024-07-24 18:07:48 +000011603 android.AssertStringDoesContain(t, "", ldRule.Args["libFlags"], "android_arm64_armv8-a_shared_current/libfoo.so")
Spandan Das2f68f192024-07-22 19:25:50 +000011604
11605 installRules := result.InstallMakeRulesForTesting(t)
11606
11607 var installedlibRule *android.InstallMakeRule
11608 for i, rule := range installRules {
11609 if rule.Target == "out/target/product/test_device/system/lib/installedlib.so" {
11610 if installedlibRule != nil {
11611 t.Errorf("Duplicate install rules for %s", rule.Target)
11612 }
11613 installedlibRule = &installRules[i]
11614 }
11615 }
11616 if installedlibRule == nil {
11617 t.Errorf("No install rule found for installedlib")
11618 return
11619 }
11620
11621 if expectLibfooOnSystemLib {
11622 android.AssertStringListContains(t,
11623 "installedlib doesn't have install dependency on libfoo impl",
11624 installedlibRule.OrderOnlyDeps,
11625 "out/target/product/test_device/system/lib/libfoo.so")
11626 } else {
11627 android.AssertStringListDoesNotContain(t,
11628 "installedlib has install dependency on libfoo stub",
11629 installedlibRule.Deps,
11630 "out/target/product/test_device/system/lib/libfoo.so")
11631 android.AssertStringListDoesNotContain(t,
11632 "installedlib has order-only install dependency on libfoo stub",
11633 installedlibRule.OrderOnlyDeps,
11634 "out/target/product/test_device/system/lib/libfoo.so")
11635 }
11636 }
11637
11638 prebuiltLibfooBp := []byte(`
11639 cc_prebuilt_library {
11640 name: "libfoo",
11641 prefer: true,
11642 srcs: ["libfoo.so"],
11643 stubs: {
11644 versions: ["1"],
11645 },
11646 apex_available: ["apexfoo"],
11647 }
11648 `)
11649
11650 apexfooBp := []byte(`
11651 apex {
11652 name: "apexfoo",
11653 key: "apexfoo.key",
11654 native_shared_libs: ["libfoo"],
11655 updatable: false,
11656 compile_multilib: "both",
11657 }
11658 apex_key {
11659 name: "apexfoo.key",
11660 public_key: "testkey.avbpubkey",
11661 private_key: "testkey.pem",
11662 }
11663 `)
11664
11665 installedlibBp := []byte(`
11666 cc_library {
11667 name: "installedlib",
11668 shared_libs: ["libfoo"],
11669 }
11670 `)
11671
11672 t.Run("prebuilt stub (without source): no install", func(t *testing.T) {
11673 testFunc(
11674 t,
11675 /*expectLibfooOnSystemLib=*/ false,
11676 android.MockFS{
11677 "prebuilts/module_sdk/art/current/Android.bp": prebuiltLibfooBp,
11678 "apexfoo/Android.bp": apexfooBp,
11679 "system/sepolicy/apex/apexfoo-file_contexts": nil,
11680 "Android.bp": installedlibBp,
11681 },
11682 )
11683 })
11684
11685 disabledSourceLibfooBp := []byte(`
11686 cc_library {
11687 name: "libfoo",
11688 enabled: false,
11689 stubs: {
11690 versions: ["1"],
11691 },
11692 apex_available: ["apexfoo"],
11693 }
11694 `)
11695
11696 t.Run("prebuilt stub (with disabled source): no install", func(t *testing.T) {
11697 testFunc(
11698 t,
11699 /*expectLibfooOnSystemLib=*/ false,
11700 android.MockFS{
11701 "prebuilts/module_sdk/art/current/Android.bp": prebuiltLibfooBp,
11702 "impl/Android.bp": disabledSourceLibfooBp,
11703 "apexfoo/Android.bp": apexfooBp,
11704 "system/sepolicy/apex/apexfoo-file_contexts": nil,
11705 "Android.bp": installedlibBp,
11706 },
11707 )
11708 })
11709}
Jihoon Kange246bb72024-09-18 22:26:22 +000011710
11711func TestSdkLibraryTransitiveClassLoaderContext(t *testing.T) {
11712 // This test case tests that listing the impl lib instead of the top level java_sdk_library
11713 // in libs of android_app and java_library does not lead to class loader context device/host
11714 // path mismatch errors.
11715 android.GroupFixturePreparers(
11716 prepareForApexTest,
11717 android.PrepareForIntegrationTestWithAndroid,
11718 PrepareForTestWithApexBuildComponents,
11719 android.FixtureModifyEnv(func(env map[string]string) {
11720 env["DISABLE_CONTAINER_CHECK"] = "true"
11721 }),
11722 withFiles(filesForSdkLibrary),
11723 android.FixtureMergeMockFs(android.MockFS{
11724 "system/sepolicy/apex/com.android.foo30-file_contexts": nil,
11725 }),
11726 ).RunTestWithBp(t, `
11727 apex {
11728 name: "com.android.foo30",
11729 key: "myapex.key",
11730 updatable: true,
11731 bootclasspath_fragments: [
11732 "foo-bootclasspath-fragment",
11733 ],
11734 java_libs: [
11735 "bar",
11736 ],
11737 apps: [
11738 "bar-app",
11739 ],
11740 min_sdk_version: "30",
11741 }
11742 apex_key {
11743 name: "myapex.key",
11744 public_key: "testkey.avbpubkey",
11745 private_key: "testkey.pem",
11746 }
11747 bootclasspath_fragment {
11748 name: "foo-bootclasspath-fragment",
11749 contents: [
11750 "framework-foo",
11751 ],
11752 apex_available: [
11753 "com.android.foo30",
11754 ],
11755 hidden_api: {
11756 split_packages: ["*"]
11757 },
11758 }
11759
11760 java_sdk_library {
11761 name: "framework-foo",
11762 srcs: [
11763 "A.java"
11764 ],
11765 unsafe_ignore_missing_latest_api: true,
11766 apex_available: [
11767 "com.android.foo30",
11768 ],
11769 compile_dex: true,
11770 sdk_version: "core_current",
11771 shared_library: false,
11772 }
11773
11774 java_library {
11775 name: "bar",
11776 srcs: [
11777 "A.java"
11778 ],
11779 libs: [
11780 "framework-foo.impl",
11781 ],
11782 apex_available: [
11783 "com.android.foo30",
11784 ],
11785 sdk_version: "core_current",
11786 }
11787
11788 java_library {
11789 name: "baz",
11790 srcs: [
11791 "A.java"
11792 ],
11793 libs: [
11794 "bar",
11795 ],
11796 sdk_version: "core_current",
11797 }
11798
11799 android_app {
11800 name: "bar-app",
11801 srcs: [
11802 "A.java"
11803 ],
11804 libs: [
11805 "baz",
11806 "framework-foo.impl",
11807 ],
11808 apex_available: [
11809 "com.android.foo30",
11810 ],
11811 sdk_version: "core_current",
11812 min_sdk_version: "30",
11813 manifest: "AndroidManifest.xml",
Colin Cross99939e92024-10-01 16:02:46 -070011814 updatable: true,
Jihoon Kange246bb72024-09-18 22:26:22 +000011815 }
11816 `)
11817}