blob: c82385b036ac65f5f27d52c8655422007992c2ea [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
2188func TestTrackAllowedDeps_SkipWithoutAllowedDepsTxt(t *testing.T) {
2189 ctx := testApex(t, `
2190 apex {
2191 name: "myapex",
2192 key: "myapex.key",
2193 updatable: true,
2194 min_sdk_version: "29",
2195 }
2196
2197 apex_key {
2198 name: "myapex.key",
2199 public_key: "testkey.avbpubkey",
2200 private_key: "testkey.pem",
2201 }
2202 `)
2203 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2204 if nil != depsinfo.MaybeRule("generateApexDepsInfoFilesRule").Output {
2205 t.Error("apex_depsinfo_singleton shouldn't run when allowed_deps.txt doesn't exist")
2206 }
2207}
2208
Jooyung Han03b51852020-02-26 22:45:42 +09002209func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002210 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002211 apex {
2212 name: "myapex",
2213 key: "myapex.key",
2214 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002215 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002216 }
2217
2218 apex_key {
2219 name: "myapex.key",
2220 public_key: "testkey.avbpubkey",
2221 private_key: "testkey.pem",
2222 }
2223
2224 cc_library {
2225 name: "libx",
2226 system_shared_libs: [],
2227 stl: "none",
2228 apex_available: [ "myapex" ],
2229 stubs: {
2230 versions: ["1", "2"],
2231 },
2232 }
2233
2234 cc_library {
2235 name: "libz",
2236 shared_libs: ["libx"],
2237 system_shared_libs: [],
2238 stl: "none",
2239 }
2240 `)
2241
2242 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002243 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002244 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2245 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2246 }
2247 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002248 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002249 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2250 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2251 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002252 expectLink("libz", "shared", "libx", "shared_current")
2253 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002254 expectNoLink("libz", "shared", "libz", "shared_1")
2255 expectNoLink("libz", "shared", "libz", "shared")
2256}
2257
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002258var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2259 func(variables android.FixtureProductVariables) {
2260 variables.SanitizeDevice = []string{"hwaddress"}
2261 },
2262)
2263
Jooyung Han75568392020-03-20 04:29:24 +09002264func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002265 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002266 apex {
2267 name: "myapex",
2268 key: "myapex.key",
2269 native_shared_libs: ["libx"],
2270 min_sdk_version: "29",
2271 }
2272
2273 apex_key {
2274 name: "myapex.key",
2275 public_key: "testkey.avbpubkey",
2276 private_key: "testkey.pem",
2277 }
2278
2279 cc_library {
2280 name: "libx",
2281 shared_libs: ["libbar"],
2282 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002283 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002284 }
2285
2286 cc_library {
2287 name: "libbar",
2288 stubs: {
2289 versions: ["29", "30"],
2290 },
2291 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002292 `,
2293 prepareForTestWithSantitizeHwaddress,
2294 )
Jooyung Han03b51852020-02-26 22:45:42 +09002295 expectLink := func(from, from_variant, to, to_variant string) {
2296 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2297 libFlags := ld.Args["libFlags"]
2298 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2299 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002300 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002301}
2302
Jooyung Han75568392020-03-20 04:29:24 +09002303func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002304 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002305 apex {
2306 name: "myapex",
2307 key: "myapex.key",
2308 native_shared_libs: ["libx"],
2309 min_sdk_version: "29",
2310 }
2311
2312 apex_key {
2313 name: "myapex.key",
2314 public_key: "testkey.avbpubkey",
2315 private_key: "testkey.pem",
2316 }
2317
2318 cc_library {
2319 name: "libx",
2320 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002321 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002322 }
Jooyung Han75568392020-03-20 04:29:24 +09002323 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002324
2325 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002326 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002327 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002328 // note that platform variant is not.
2329 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002330 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002331}
2332
Jooyung Han749dc692020-04-15 11:03:39 +09002333func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2334 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002335 apex {
2336 name: "myapex",
2337 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002338 native_shared_libs: ["mylib"],
2339 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002340 }
2341
2342 apex_key {
2343 name: "myapex.key",
2344 public_key: "testkey.avbpubkey",
2345 private_key: "testkey.pem",
2346 }
Jooyung Han749dc692020-04-15 11:03:39 +09002347
2348 cc_library {
2349 name: "mylib",
2350 srcs: ["mylib.cpp"],
2351 system_shared_libs: [],
2352 stl: "none",
2353 apex_available: [
2354 "myapex",
2355 ],
2356 min_sdk_version: "30",
2357 }
2358 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002359
2360 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2361 apex {
2362 name: "myapex",
2363 key: "myapex.key",
2364 native_shared_libs: ["libfoo.ffi"],
2365 min_sdk_version: "29",
2366 }
2367
2368 apex_key {
2369 name: "myapex.key",
2370 public_key: "testkey.avbpubkey",
2371 private_key: "testkey.pem",
2372 }
2373
2374 rust_ffi_shared {
2375 name: "libfoo.ffi",
2376 srcs: ["foo.rs"],
2377 crate_name: "foo",
2378 apex_available: [
2379 "myapex",
2380 ],
2381 min_sdk_version: "30",
2382 }
2383 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002384
2385 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2386 apex {
2387 name: "myapex",
2388 key: "myapex.key",
2389 java_libs: ["libfoo"],
2390 min_sdk_version: "29",
2391 }
2392
2393 apex_key {
2394 name: "myapex.key",
2395 public_key: "testkey.avbpubkey",
2396 private_key: "testkey.pem",
2397 }
2398
2399 java_import {
2400 name: "libfoo",
2401 jars: ["libfoo.jar"],
2402 apex_available: [
2403 "myapex",
2404 ],
2405 min_sdk_version: "30",
2406 }
2407 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002408
2409 // Skip check for modules compiling against core API surface
2410 testApex(t, `
2411 apex {
2412 name: "myapex",
2413 key: "myapex.key",
2414 java_libs: ["libfoo"],
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 java_library {
2425 name: "libfoo",
2426 srcs: ["Foo.java"],
2427 apex_available: [
2428 "myapex",
2429 ],
2430 // Compile against core API surface
2431 sdk_version: "core_current",
2432 min_sdk_version: "30",
2433 }
2434 `)
2435
Jooyung Han749dc692020-04-15 11:03:39 +09002436}
2437
2438func TestApexMinSdkVersion_Okay(t *testing.T) {
2439 testApex(t, `
2440 apex {
2441 name: "myapex",
2442 key: "myapex.key",
2443 native_shared_libs: ["libfoo"],
2444 java_libs: ["libbar"],
2445 min_sdk_version: "29",
2446 }
2447
2448 apex_key {
2449 name: "myapex.key",
2450 public_key: "testkey.avbpubkey",
2451 private_key: "testkey.pem",
2452 }
2453
2454 cc_library {
2455 name: "libfoo",
2456 srcs: ["mylib.cpp"],
2457 shared_libs: ["libfoo_dep"],
2458 apex_available: ["myapex"],
2459 min_sdk_version: "29",
2460 }
2461
2462 cc_library {
2463 name: "libfoo_dep",
2464 srcs: ["mylib.cpp"],
2465 apex_available: ["myapex"],
2466 min_sdk_version: "29",
2467 }
2468
2469 java_library {
2470 name: "libbar",
2471 sdk_version: "current",
2472 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002473 static_libs: [
2474 "libbar_dep",
2475 "libbar_import_dep",
2476 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002477 apex_available: ["myapex"],
2478 min_sdk_version: "29",
2479 }
2480
2481 java_library {
2482 name: "libbar_dep",
2483 sdk_version: "current",
2484 srcs: ["a.java"],
2485 apex_available: ["myapex"],
2486 min_sdk_version: "29",
2487 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002488
2489 java_import {
2490 name: "libbar_import_dep",
2491 jars: ["libbar.jar"],
2492 apex_available: ["myapex"],
2493 min_sdk_version: "29",
2494 }
Jooyung Han03b51852020-02-26 22:45:42 +09002495 `)
2496}
2497
Colin Cross8ca61c12022-10-06 21:00:14 -07002498func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2499 // Tests that an apex dependency with min_sdk_version higher than the
2500 // min_sdk_version of the apex is allowed as long as the dependency's
2501 // min_sdk_version is less than or equal to the api level that the
2502 // architecture was introduced in. In this case, arm64 didn't exist
2503 // until api level 21, so the arm64 code will never need to run on
2504 // an api level 20 device, even if other architectures of the apex
2505 // will.
2506 testApex(t, `
2507 apex {
2508 name: "myapex",
2509 key: "myapex.key",
2510 native_shared_libs: ["libfoo"],
2511 min_sdk_version: "20",
2512 }
2513
2514 apex_key {
2515 name: "myapex.key",
2516 public_key: "testkey.avbpubkey",
2517 private_key: "testkey.pem",
2518 }
2519
2520 cc_library {
2521 name: "libfoo",
2522 srcs: ["mylib.cpp"],
2523 apex_available: ["myapex"],
2524 min_sdk_version: "21",
2525 stl: "none",
2526 }
2527 `)
2528}
2529
Artur Satayev8cf899a2020-04-15 17:29:42 +01002530func TestJavaStableSdkVersion(t *testing.T) {
2531 testCases := []struct {
2532 name string
2533 expectedError string
2534 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002535 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002536 }{
2537 {
2538 name: "Non-updatable apex with non-stable dep",
2539 bp: `
2540 apex {
2541 name: "myapex",
2542 java_libs: ["myjar"],
2543 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002544 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002545 }
2546 apex_key {
2547 name: "myapex.key",
2548 public_key: "testkey.avbpubkey",
2549 private_key: "testkey.pem",
2550 }
2551 java_library {
2552 name: "myjar",
2553 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002554 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002555 apex_available: ["myapex"],
2556 }
2557 `,
2558 },
2559 {
2560 name: "Updatable apex with stable dep",
2561 bp: `
2562 apex {
2563 name: "myapex",
2564 java_libs: ["myjar"],
2565 key: "myapex.key",
2566 updatable: true,
2567 min_sdk_version: "29",
2568 }
2569 apex_key {
2570 name: "myapex.key",
2571 public_key: "testkey.avbpubkey",
2572 private_key: "testkey.pem",
2573 }
2574 java_library {
2575 name: "myjar",
2576 srcs: ["foo/bar/MyClass.java"],
2577 sdk_version: "current",
2578 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002579 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002580 }
2581 `,
2582 },
2583 {
2584 name: "Updatable apex with non-stable dep",
2585 expectedError: "cannot depend on \"myjar\"",
2586 bp: `
2587 apex {
2588 name: "myapex",
2589 java_libs: ["myjar"],
2590 key: "myapex.key",
2591 updatable: true,
2592 }
2593 apex_key {
2594 name: "myapex.key",
2595 public_key: "testkey.avbpubkey",
2596 private_key: "testkey.pem",
2597 }
2598 java_library {
2599 name: "myjar",
2600 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002601 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002602 apex_available: ["myapex"],
2603 }
2604 `,
2605 },
2606 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002607 name: "Updatable apex with non-stable legacy core platform dep",
2608 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2609 bp: `
2610 apex {
2611 name: "myapex",
2612 java_libs: ["myjar-uses-legacy"],
2613 key: "myapex.key",
2614 updatable: true,
2615 }
2616 apex_key {
2617 name: "myapex.key",
2618 public_key: "testkey.avbpubkey",
2619 private_key: "testkey.pem",
2620 }
2621 java_library {
2622 name: "myjar-uses-legacy",
2623 srcs: ["foo/bar/MyClass.java"],
2624 sdk_version: "core_platform",
2625 apex_available: ["myapex"],
2626 }
2627 `,
2628 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2629 },
2630 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002631 name: "Updatable apex with non-stable transitive dep",
2632 // This is not actually detecting that the transitive dependency is unstable, rather it is
2633 // detecting that the transitive dependency is building against a wider API surface than the
2634 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002635 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002636 bp: `
2637 apex {
2638 name: "myapex",
2639 java_libs: ["myjar"],
2640 key: "myapex.key",
2641 updatable: true,
2642 }
2643 apex_key {
2644 name: "myapex.key",
2645 public_key: "testkey.avbpubkey",
2646 private_key: "testkey.pem",
2647 }
2648 java_library {
2649 name: "myjar",
2650 srcs: ["foo/bar/MyClass.java"],
2651 sdk_version: "current",
2652 apex_available: ["myapex"],
2653 static_libs: ["transitive-jar"],
2654 }
2655 java_library {
2656 name: "transitive-jar",
2657 srcs: ["foo/bar/MyClass.java"],
2658 sdk_version: "core_platform",
2659 apex_available: ["myapex"],
2660 }
2661 `,
2662 },
2663 }
2664
2665 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002666 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2667 continue
2668 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002669 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002670 errorHandler := android.FixtureExpectsNoErrors
2671 if test.expectedError != "" {
2672 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002673 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002674 android.GroupFixturePreparers(
2675 java.PrepareForTestWithJavaDefaultModules,
2676 PrepareForTestWithApexBuildComponents,
2677 prepareForTestWithMyapex,
2678 android.OptionalFixturePreparer(test.preparer),
2679 ).
2680 ExtendWithErrorHandler(errorHandler).
2681 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002682 })
2683 }
2684}
2685
Jooyung Han749dc692020-04-15 11:03:39 +09002686func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2687 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2688 apex {
2689 name: "myapex",
2690 key: "myapex.key",
2691 native_shared_libs: ["mylib"],
2692 min_sdk_version: "29",
2693 }
2694
2695 apex_key {
2696 name: "myapex.key",
2697 public_key: "testkey.avbpubkey",
2698 private_key: "testkey.pem",
2699 }
2700
2701 cc_library {
2702 name: "mylib",
2703 srcs: ["mylib.cpp"],
2704 shared_libs: ["mylib2"],
2705 system_shared_libs: [],
2706 stl: "none",
2707 apex_available: [
2708 "myapex",
2709 ],
2710 min_sdk_version: "29",
2711 }
2712
2713 // indirect part of the apex
2714 cc_library {
2715 name: "mylib2",
2716 srcs: ["mylib.cpp"],
2717 system_shared_libs: [],
2718 stl: "none",
2719 apex_available: [
2720 "myapex",
2721 ],
2722 min_sdk_version: "30",
2723 }
2724 `)
2725}
2726
2727func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2728 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2729 apex {
2730 name: "myapex",
2731 key: "myapex.key",
2732 apps: ["AppFoo"],
2733 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002734 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002735 }
2736
2737 apex_key {
2738 name: "myapex.key",
2739 public_key: "testkey.avbpubkey",
2740 private_key: "testkey.pem",
2741 }
2742
2743 android_app {
2744 name: "AppFoo",
2745 srcs: ["foo/bar/MyClass.java"],
2746 sdk_version: "current",
2747 min_sdk_version: "29",
2748 system_modules: "none",
2749 stl: "none",
2750 static_libs: ["bar"],
2751 apex_available: [ "myapex" ],
2752 }
2753
2754 java_library {
2755 name: "bar",
2756 sdk_version: "current",
2757 srcs: ["a.java"],
2758 apex_available: [ "myapex" ],
2759 }
2760 `)
2761}
2762
2763func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002764 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002765 apex {
2766 name: "myapex",
2767 key: "myapex.key",
2768 native_shared_libs: ["mylib"],
2769 min_sdk_version: "29",
2770 }
2771
2772 apex_key {
2773 name: "myapex.key",
2774 public_key: "testkey.avbpubkey",
2775 private_key: "testkey.pem",
2776 }
2777
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002778 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002779 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2780 cc_library {
2781 name: "mylib",
2782 srcs: ["mylib.cpp"],
2783 shared_libs: ["mylib2"],
2784 system_shared_libs: [],
2785 stl: "none",
2786 apex_available: ["myapex", "otherapex"],
2787 min_sdk_version: "29",
2788 }
2789
2790 cc_library {
2791 name: "mylib2",
2792 srcs: ["mylib.cpp"],
2793 system_shared_libs: [],
2794 stl: "none",
2795 apex_available: ["otherapex"],
2796 stubs: { versions: ["29", "30"] },
2797 min_sdk_version: "30",
2798 }
2799
2800 apex {
2801 name: "otherapex",
2802 key: "myapex.key",
2803 native_shared_libs: ["mylib", "mylib2"],
2804 min_sdk_version: "30",
2805 }
2806 `)
2807 expectLink := func(from, from_variant, to, to_variant string) {
2808 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2809 libFlags := ld.Args["libFlags"]
2810 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2811 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002812 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002813 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002814}
2815
Jooyung Haned124c32021-01-26 11:43:46 +09002816func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002817 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2818 func(variables android.FixtureProductVariables) {
2819 variables.Platform_sdk_codename = proptools.StringPtr("S")
2820 variables.Platform_version_active_codenames = []string{"S"}
2821 },
2822 )
Jooyung Haned124c32021-01-26 11:43:46 +09002823 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2824 apex {
2825 name: "myapex",
2826 key: "myapex.key",
2827 native_shared_libs: ["libfoo"],
2828 min_sdk_version: "S",
2829 }
2830 apex_key {
2831 name: "myapex.key",
2832 public_key: "testkey.avbpubkey",
2833 private_key: "testkey.pem",
2834 }
2835 cc_library {
2836 name: "libfoo",
2837 shared_libs: ["libbar"],
2838 apex_available: ["myapex"],
2839 min_sdk_version: "29",
2840 }
2841 cc_library {
2842 name: "libbar",
2843 apex_available: ["myapex"],
2844 }
2845 `, withSAsActiveCodeNames)
2846}
2847
2848func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002849 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2850 variables.Platform_sdk_codename = proptools.StringPtr("S")
2851 variables.Platform_version_active_codenames = []string{"S", "T"}
2852 })
Colin Cross1c460562021-02-16 17:55:47 -08002853 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002854 apex {
2855 name: "myapex",
2856 key: "myapex.key",
2857 native_shared_libs: ["libfoo"],
2858 min_sdk_version: "S",
2859 }
2860 apex_key {
2861 name: "myapex.key",
2862 public_key: "testkey.avbpubkey",
2863 private_key: "testkey.pem",
2864 }
2865 cc_library {
2866 name: "libfoo",
2867 shared_libs: ["libbar"],
2868 apex_available: ["myapex"],
2869 min_sdk_version: "S",
2870 }
2871 cc_library {
2872 name: "libbar",
2873 stubs: {
2874 symbol_file: "libbar.map.txt",
2875 versions: ["30", "S", "T"],
2876 },
2877 }
2878 `, withSAsActiveCodeNames)
2879
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002880 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002881 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2882 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002883 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002884}
2885
Jiyong Park7c2ee712018-12-07 00:42:25 +09002886func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002887 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002888 apex {
2889 name: "myapex",
2890 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002891 native_shared_libs: ["mylib"],
Jooyung Han4ed512b2023-08-11 16:30:04 +09002892 binaries: ["mybin", "mybin.rust"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002893 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002894 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002895 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002896 }
2897
2898 apex_key {
2899 name: "myapex.key",
2900 public_key: "testkey.avbpubkey",
2901 private_key: "testkey.pem",
2902 }
2903
2904 prebuilt_etc {
2905 name: "myetc",
2906 src: "myprebuilt",
2907 sub_dir: "foo/bar",
2908 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002909
2910 cc_library {
2911 name: "mylib",
2912 srcs: ["mylib.cpp"],
2913 relative_install_path: "foo/bar",
2914 system_shared_libs: [],
2915 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002916 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002917 }
2918
2919 cc_binary {
2920 name: "mybin",
2921 srcs: ["mylib.cpp"],
2922 relative_install_path: "foo/bar",
2923 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002924 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002925 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002926 }
Jooyung Han4ed512b2023-08-11 16:30:04 +09002927
2928 rust_binary {
2929 name: "mybin.rust",
2930 srcs: ["foo.rs"],
2931 relative_install_path: "rust_subdir",
2932 apex_available: [ "myapex" ],
2933 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002934 `)
2935
Jooyung Hana0503a52023-08-23 13:12:50 +09002936 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002937 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002938
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002939 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002940 ensureContains(t, cmd, "/etc ")
2941 ensureContains(t, cmd, "/etc/foo ")
2942 ensureContains(t, cmd, "/etc/foo/bar ")
2943 ensureContains(t, cmd, "/lib64 ")
2944 ensureContains(t, cmd, "/lib64/foo ")
2945 ensureContains(t, cmd, "/lib64/foo/bar ")
2946 ensureContains(t, cmd, "/lib ")
2947 ensureContains(t, cmd, "/lib/foo ")
2948 ensureContains(t, cmd, "/lib/foo/bar ")
2949 ensureContains(t, cmd, "/bin ")
2950 ensureContains(t, cmd, "/bin/foo ")
2951 ensureContains(t, cmd, "/bin/foo/bar ")
Jooyung Han4ed512b2023-08-11 16:30:04 +09002952 ensureContains(t, cmd, "/bin/rust_subdir ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002953}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002954
Jooyung Han35155c42020-02-06 17:33:20 +09002955func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002956 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002957 apex {
2958 name: "myapex",
2959 key: "myapex.key",
2960 multilib: {
2961 both: {
2962 native_shared_libs: ["mylib"],
2963 binaries: ["mybin"],
2964 },
2965 },
2966 compile_multilib: "both",
2967 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002968 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002969 }
2970
2971 apex_key {
2972 name: "myapex.key",
2973 public_key: "testkey.avbpubkey",
2974 private_key: "testkey.pem",
2975 }
2976
2977 cc_library {
2978 name: "mylib",
2979 relative_install_path: "foo/bar",
2980 system_shared_libs: [],
2981 stl: "none",
2982 apex_available: [ "myapex" ],
2983 native_bridge_supported: true,
2984 }
2985
2986 cc_binary {
2987 name: "mybin",
2988 relative_install_path: "foo/bar",
2989 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002990 stl: "none",
2991 apex_available: [ "myapex" ],
2992 native_bridge_supported: true,
2993 compile_multilib: "both", // default is "first" for binary
2994 multilib: {
2995 lib64: {
2996 suffix: "64",
2997 },
2998 },
2999 }
3000 `, withNativeBridgeEnabled)
Jooyung Hana0503a52023-08-23 13:12:50 +09003001 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han35155c42020-02-06 17:33:20 +09003002 "bin/foo/bar/mybin",
3003 "bin/foo/bar/mybin64",
3004 "bin/arm/foo/bar/mybin",
3005 "bin/arm64/foo/bar/mybin64",
3006 "lib/foo/bar/mylib.so",
3007 "lib/arm/foo/bar/mylib.so",
3008 "lib64/foo/bar/mylib.so",
3009 "lib64/arm64/foo/bar/mylib.so",
3010 })
3011}
3012
Jooyung Han85d61762020-06-24 23:50:26 +09003013func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08003014 result := android.GroupFixturePreparers(
3015 prepareForApexTest,
3016 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
3017 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09003018 apex {
3019 name: "myapex",
3020 key: "myapex.key",
3021 binaries: ["mybin"],
3022 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003023 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09003024 }
3025 apex_key {
3026 name: "myapex.key",
3027 public_key: "testkey.avbpubkey",
3028 private_key: "testkey.pem",
3029 }
3030 cc_binary {
3031 name: "mybin",
3032 vendor: true,
3033 shared_libs: ["libfoo"],
3034 }
3035 cc_library {
3036 name: "libfoo",
3037 proprietary: true,
3038 }
3039 `)
3040
Jooyung Hana0503a52023-08-23 13:12:50 +09003041 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09003042 "bin/mybin",
3043 "lib64/libfoo.so",
3044 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
3045 "lib64/libc++.so",
3046 })
3047
Jooyung Hana0503a52023-08-23 13:12:50 +09003048 apexBundle := result.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossc68db4b2021-11-11 18:59:15 -08003049 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09003050 name := apexBundle.BaseModuleName()
3051 prefix := "TARGET_"
3052 var builder strings.Builder
3053 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08003054 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01003055 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01003056 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003057
Jooyung Hana0503a52023-08-23 13:12:50 +09003058 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003059 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
3060 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09003061}
3062
Justin Yun13decfb2021-03-08 19:25:55 +09003063func TestProductVariant(t *testing.T) {
3064 ctx := testApex(t, `
3065 apex {
3066 name: "myapex",
3067 key: "myapex.key",
3068 updatable: false,
3069 product_specific: true,
3070 binaries: ["foo"],
3071 }
3072
3073 apex_key {
3074 name: "myapex.key",
3075 public_key: "testkey.avbpubkey",
3076 private_key: "testkey.pem",
3077 }
3078
3079 cc_binary {
3080 name: "foo",
3081 product_available: true,
3082 apex_available: ["myapex"],
3083 srcs: ["foo.cpp"],
3084 }
Justin Yunaf1fde42023-09-27 16:22:10 +09003085 `)
Justin Yun13decfb2021-03-08 19:25:55 +09003086
3087 cflags := strings.Fields(
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003088 ctx.ModuleForTests("foo", "android_product_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09003089 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
3090 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
3091 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
3092 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
3093}
3094
Jooyung Han8e5685d2020-09-21 11:02:57 +09003095func TestApex_withPrebuiltFirmware(t *testing.T) {
3096 testCases := []struct {
3097 name string
3098 additionalProp string
3099 }{
3100 {"system apex with prebuilt_firmware", ""},
3101 {"vendor apex with prebuilt_firmware", "vendor: true,"},
3102 }
3103 for _, tc := range testCases {
3104 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003105 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09003106 apex {
3107 name: "myapex",
3108 key: "myapex.key",
3109 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003110 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09003111 `+tc.additionalProp+`
3112 }
3113 apex_key {
3114 name: "myapex.key",
3115 public_key: "testkey.avbpubkey",
3116 private_key: "testkey.pem",
3117 }
3118 prebuilt_firmware {
3119 name: "myfirmware",
3120 src: "myfirmware.bin",
3121 filename_from_src: true,
3122 `+tc.additionalProp+`
3123 }
3124 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09003125 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han8e5685d2020-09-21 11:02:57 +09003126 "etc/firmware/myfirmware.bin",
3127 })
3128 })
3129 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003130}
3131
Jooyung Hanefb184e2020-06-25 17:14:25 +09003132func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003133 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003134 apex {
3135 name: "myapex",
3136 key: "myapex.key",
3137 vendor: true,
3138 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003139 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003140 }
3141
3142 apex_key {
3143 name: "myapex.key",
3144 public_key: "testkey.avbpubkey",
3145 private_key: "testkey.pem",
3146 }
3147
3148 cc_library {
3149 name: "mylib",
3150 vendor_available: true,
3151 }
3152 `)
3153
Jooyung Hana0503a52023-08-23 13:12:50 +09003154 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003155 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003156 name := apexBundle.BaseModuleName()
3157 prefix := "TARGET_"
3158 var builder strings.Builder
3159 data.Custom(&builder, name, prefix, "", data)
3160 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09003161 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 +09003162}
3163
Jooyung Han2ed99d02020-06-24 23:26:26 +09003164func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003165 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003166 apex {
3167 name: "myapex",
3168 key: "myapex.key",
3169 vintf_fragments: ["fragment.xml"],
3170 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003171 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003172 }
3173 apex_key {
3174 name: "myapex.key",
3175 public_key: "testkey.avbpubkey",
3176 private_key: "testkey.pem",
3177 }
3178 cc_binary {
3179 name: "mybin",
3180 }
3181 `)
3182
Jooyung Hana0503a52023-08-23 13:12:50 +09003183 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003184 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003185 name := apexBundle.BaseModuleName()
3186 prefix := "TARGET_"
3187 var builder strings.Builder
3188 data.Custom(&builder, name, prefix, "", data)
3189 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003190 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003191 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003192}
3193
Jiyong Park16e91a02018-12-20 18:18:08 +09003194func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003195 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003196 apex {
3197 name: "myapex",
3198 key: "myapex.key",
3199 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003200 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003201 }
3202
3203 apex_key {
3204 name: "myapex.key",
3205 public_key: "testkey.avbpubkey",
3206 private_key: "testkey.pem",
3207 }
3208
3209 cc_library {
3210 name: "mylib",
3211 srcs: ["mylib.cpp"],
3212 system_shared_libs: [],
3213 stl: "none",
3214 stubs: {
3215 versions: ["1", "2", "3"],
3216 },
Spandan Das20fce2d2023-04-12 17:21:39 +00003217 apex_available: ["myapex"],
Jiyong Park16e91a02018-12-20 18:18:08 +09003218 }
3219
3220 cc_binary {
3221 name: "not_in_apex",
3222 srcs: ["mylib.cpp"],
3223 static_libs: ["mylib"],
3224 static_executable: true,
3225 system_shared_libs: [],
3226 stl: "none",
3227 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003228 `)
3229
Colin Cross7113d202019-11-20 16:39:12 -08003230 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003231
3232 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003233 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003234}
Jiyong Park9335a262018-12-24 11:31:58 +09003235
3236func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003237 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003238 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003239 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003240 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003241 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003242 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003243 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003244 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003245 }
3246
3247 cc_library {
3248 name: "mylib",
3249 srcs: ["mylib.cpp"],
3250 system_shared_libs: [],
3251 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003252 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003253 }
3254
3255 apex_key {
3256 name: "myapex.key",
3257 public_key: "testkey.avbpubkey",
3258 private_key: "testkey.pem",
3259 }
3260
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003261 android_app_certificate {
3262 name: "myapex.certificate",
3263 certificate: "testkey",
3264 }
3265
3266 android_app_certificate {
3267 name: "myapex.certificate.override",
3268 certificate: "testkey.override",
3269 }
3270
Jiyong Park9335a262018-12-24 11:31:58 +09003271 `)
3272
3273 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003274 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003275
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003276 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3277 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003278 "vendor/foo/devkeys/testkey.avbpubkey")
3279 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003280 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3281 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003282 "vendor/foo/devkeys/testkey.pem")
3283 }
3284
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003285 // check the APK certs. It should be overridden to myapex.certificate.override
Jooyung Hana0503a52023-08-23 13:12:50 +09003286 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003287 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003288 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003289 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003290 }
3291}
Jiyong Park58e364a2019-01-19 19:24:06 +09003292
Jooyung Hanf121a652019-12-17 14:30:11 +09003293func TestCertificate(t *testing.T) {
3294 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003295 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003296 apex {
3297 name: "myapex",
3298 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003299 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003300 }
3301 apex_key {
3302 name: "myapex.key",
3303 public_key: "testkey.avbpubkey",
3304 private_key: "testkey.pem",
3305 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003306 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003307 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3308 if actual := rule.Args["certificates"]; actual != expected {
3309 t.Errorf("certificates should be %q, not %q", expected, actual)
3310 }
3311 })
3312 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003313 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003314 apex {
3315 name: "myapex_keytest",
3316 key: "myapex.key",
3317 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003318 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003319 }
3320 apex_key {
3321 name: "myapex.key",
3322 public_key: "testkey.avbpubkey",
3323 private_key: "testkey.pem",
3324 }
3325 android_app_certificate {
3326 name: "myapex.certificate.override",
3327 certificate: "testkey.override",
3328 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003329 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003330 expected := "testkey.override.x509.pem testkey.override.pk8"
3331 if actual := rule.Args["certificates"]; actual != expected {
3332 t.Errorf("certificates should be %q, not %q", expected, actual)
3333 }
3334 })
3335 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003336 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003337 apex {
3338 name: "myapex",
3339 key: "myapex.key",
3340 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003341 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003342 }
3343 apex_key {
3344 name: "myapex.key",
3345 public_key: "testkey.avbpubkey",
3346 private_key: "testkey.pem",
3347 }
3348 android_app_certificate {
3349 name: "myapex.certificate",
3350 certificate: "testkey",
3351 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003352 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003353 expected := "testkey.x509.pem testkey.pk8"
3354 if actual := rule.Args["certificates"]; actual != expected {
3355 t.Errorf("certificates should be %q, not %q", expected, actual)
3356 }
3357 })
3358 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003359 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003360 apex {
3361 name: "myapex_keytest",
3362 key: "myapex.key",
3363 file_contexts: ":myapex-file_contexts",
3364 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003365 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003366 }
3367 apex_key {
3368 name: "myapex.key",
3369 public_key: "testkey.avbpubkey",
3370 private_key: "testkey.pem",
3371 }
3372 android_app_certificate {
3373 name: "myapex.certificate.override",
3374 certificate: "testkey.override",
3375 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003376 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003377 expected := "testkey.override.x509.pem testkey.override.pk8"
3378 if actual := rule.Args["certificates"]; actual != expected {
3379 t.Errorf("certificates should be %q, not %q", expected, actual)
3380 }
3381 })
3382 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003383 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003384 apex {
3385 name: "myapex",
3386 key: "myapex.key",
3387 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003388 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003389 }
3390 apex_key {
3391 name: "myapex.key",
3392 public_key: "testkey.avbpubkey",
3393 private_key: "testkey.pem",
3394 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003395 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003396 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3397 if actual := rule.Args["certificates"]; actual != expected {
3398 t.Errorf("certificates should be %q, not %q", expected, actual)
3399 }
3400 })
3401 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003402 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003403 apex {
3404 name: "myapex_keytest",
3405 key: "myapex.key",
3406 file_contexts: ":myapex-file_contexts",
3407 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003408 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003409 }
3410 apex_key {
3411 name: "myapex.key",
3412 public_key: "testkey.avbpubkey",
3413 private_key: "testkey.pem",
3414 }
3415 android_app_certificate {
3416 name: "myapex.certificate.override",
3417 certificate: "testkey.override",
3418 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003419 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003420 expected := "testkey.override.x509.pem testkey.override.pk8"
3421 if actual := rule.Args["certificates"]; actual != expected {
3422 t.Errorf("certificates should be %q, not %q", expected, actual)
3423 }
3424 })
3425}
3426
Jiyong Park58e364a2019-01-19 19:24:06 +09003427func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003428 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003429 apex {
3430 name: "myapex",
3431 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003432 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003433 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003434 }
3435
3436 apex {
3437 name: "otherapex",
3438 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003439 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003440 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003441 }
3442
3443 apex_key {
3444 name: "myapex.key",
3445 public_key: "testkey.avbpubkey",
3446 private_key: "testkey.pem",
3447 }
3448
3449 cc_library {
3450 name: "mylib",
3451 srcs: ["mylib.cpp"],
3452 system_shared_libs: [],
3453 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003454 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003455 "myapex",
3456 "otherapex",
3457 ],
Jooyung Han24282772020-03-21 23:20:55 +09003458 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003459 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003460 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003461 cc_library {
3462 name: "mylib2",
3463 srcs: ["mylib.cpp"],
3464 system_shared_libs: [],
3465 stl: "none",
3466 apex_available: [
3467 "myapex",
3468 "otherapex",
3469 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003470 static_libs: ["mylib3"],
3471 recovery_available: true,
3472 min_sdk_version: "29",
3473 }
3474 cc_library {
3475 name: "mylib3",
3476 srcs: ["mylib.cpp"],
3477 system_shared_libs: [],
3478 stl: "none",
3479 apex_available: [
3480 "myapex",
3481 "otherapex",
3482 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003483 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003484 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003485 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003486 `)
3487
Jooyung Hanc87a0592020-03-02 17:44:33 +09003488 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003489 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003490 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003491
Vinh Tranf9754732023-01-19 22:41:46 -05003492 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003493 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003494 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003495
Vinh Tranf9754732023-01-19 22:41:46 -05003496 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003497 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003498 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003499
Colin Crossaede88c2020-08-11 12:17:01 -07003500 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3501 // each variant defines additional macros to distinguish which apex variant it is built for
3502
3503 // non-APEX variant does not have __ANDROID_APEX__ defined
3504 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3505 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3506
Vinh Tranf9754732023-01-19 22:41:46 -05003507 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003508 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3509 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003510
Jooyung Hanc87a0592020-03-02 17:44:33 +09003511 // non-APEX variant does not have __ANDROID_APEX__ defined
3512 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3513 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3514
Vinh Tranf9754732023-01-19 22:41:46 -05003515 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003516 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003517 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003518}
Jiyong Park7e636d02019-01-28 16:16:54 +09003519
3520func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003521 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003522 apex {
3523 name: "myapex",
3524 key: "myapex.key",
3525 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003526 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003527 }
3528
3529 apex_key {
3530 name: "myapex.key",
3531 public_key: "testkey.avbpubkey",
3532 private_key: "testkey.pem",
3533 }
3534
3535 cc_library_headers {
3536 name: "mylib_headers",
3537 export_include_dirs: ["my_include"],
3538 system_shared_libs: [],
3539 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003540 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003541 }
3542
3543 cc_library {
3544 name: "mylib",
3545 srcs: ["mylib.cpp"],
3546 system_shared_libs: [],
3547 stl: "none",
3548 header_libs: ["mylib_headers"],
3549 export_header_lib_headers: ["mylib_headers"],
3550 stubs: {
3551 versions: ["1", "2", "3"],
3552 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003553 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003554 }
3555
3556 cc_library {
3557 name: "otherlib",
3558 srcs: ["mylib.cpp"],
3559 system_shared_libs: [],
3560 stl: "none",
3561 shared_libs: ["mylib"],
3562 }
3563 `)
3564
Colin Cross7113d202019-11-20 16:39:12 -08003565 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003566
3567 // Ensure that the include path of the header lib is exported to 'otherlib'
3568 ensureContains(t, cFlags, "-Imy_include")
3569}
Alex Light9670d332019-01-29 18:07:33 -08003570
Jiyong Park7cd10e32020-01-14 09:22:18 +09003571type fileInApex struct {
3572 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003573 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003574 isLink bool
3575}
3576
Jooyung Han1724d582022-12-21 10:17:44 +09003577func (f fileInApex) String() string {
3578 return f.src + ":" + f.path
3579}
3580
3581func (f fileInApex) match(expectation string) bool {
3582 parts := strings.Split(expectation, ":")
3583 if len(parts) == 1 {
3584 match, _ := path.Match(parts[0], f.path)
3585 return match
3586 }
3587 if len(parts) == 2 {
3588 matchSrc, _ := path.Match(parts[0], f.src)
3589 matchDst, _ := path.Match(parts[1], f.path)
3590 return matchSrc && matchDst
3591 }
3592 panic("invalid expected file specification: " + expectation)
3593}
3594
Jooyung Hana57af4a2020-01-23 05:36:59 +00003595func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003596 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003597 module := ctx.ModuleForTests(moduleName, variant)
3598 apexRule := module.MaybeRule("apexRule")
3599 apexDir := "/image.apex/"
Jooyung Han31c470b2019-10-18 16:26:59 +09003600 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003601 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003602 for _, cmd := range strings.Split(copyCmds, "&&") {
3603 cmd = strings.TrimSpace(cmd)
3604 if cmd == "" {
3605 continue
3606 }
3607 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003608 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003609 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003610 switch terms[0] {
3611 case "mkdir":
3612 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003613 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003614 t.Fatal("copyCmds contains invalid cp command", cmd)
3615 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003616 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003617 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003618 isLink = false
3619 case "ln":
3620 if len(terms) != 3 && len(terms) != 4 {
3621 // ln LINK TARGET or ln -s LINK TARGET
3622 t.Fatal("copyCmds contains invalid ln command", cmd)
3623 }
3624 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003625 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003626 isLink = true
3627 default:
3628 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3629 }
3630 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003631 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003632 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003633 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003634 }
Jooyung Han1724d582022-12-21 10:17:44 +09003635 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003636 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003637 }
3638 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003639 return ret
3640}
3641
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003642func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003643 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003644 var failed bool
3645 var surplus []string
3646 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003647 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003648 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003649 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003650 if file.match(expected) {
3651 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003652 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003653 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003654 }
3655 }
Jooyung Han1724d582022-12-21 10:17:44 +09003656 if !matchFound {
3657 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003658 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003659 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003660
Jooyung Han31c470b2019-10-18 16:26:59 +09003661 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003662 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003663 t.Log("surplus files", surplus)
3664 failed = true
3665 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003666
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003667 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003668 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003669 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003670 if !filesMatched[expected] {
3671 missing = append(missing, expected)
3672 }
3673 }
3674 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003675 t.Log("missing files", missing)
3676 failed = true
3677 }
3678 if failed {
3679 t.Fail()
3680 }
3681}
3682
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003683func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3684 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3685}
3686
3687func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
Spandan Das2069c3f2023-12-06 19:40:24 +00003688 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Description("deapex")
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003689 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3690 if deapexer.Output != nil {
3691 outputs = append(outputs, deapexer.Output.String())
3692 }
3693 for _, output := range deapexer.ImplicitOutputs {
3694 outputs = append(outputs, output.String())
3695 }
3696 actualFiles := make([]fileInApex, 0, len(outputs))
3697 for _, output := range outputs {
3698 dir := "/deapexer/"
3699 pos := strings.LastIndex(output, dir)
3700 if pos == -1 {
3701 t.Fatal("Unknown deapexer output ", output)
3702 }
3703 path := output[pos+len(dir):]
3704 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3705 }
3706 assertFileListEquals(t, files, actualFiles)
3707}
3708
Jooyung Han39edb6c2019-11-06 16:53:07 +09003709func vndkLibrariesTxtFiles(vers ...string) (result string) {
3710 for _, v := range vers {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +09003711 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Justin Yund5784122023-10-25 13:25:32 +09003712 result += `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003713 prebuilt_etc {
3714 name: "` + txt + `.libraries.` + v + `.txt",
3715 src: "dummy.txt",
3716 }
3717 `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003718 }
3719 }
3720 return
3721}
3722
Jooyung Han344d5432019-08-23 11:17:39 +09003723func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003724 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003725 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003726 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003727 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003728 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003729 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003730 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003731 }
3732
3733 apex_key {
3734 name: "myapex.key",
3735 public_key: "testkey.avbpubkey",
3736 private_key: "testkey.pem",
3737 }
3738
Jooyung Han31c470b2019-10-18 16:26:59 +09003739 vndk_prebuilt_shared {
3740 name: "libvndk27",
3741 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003742 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003743 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003744 vndk: {
3745 enabled: true,
3746 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003747 target_arch: "arm64",
3748 arch: {
3749 arm: {
3750 srcs: ["libvndk27_arm.so"],
3751 },
3752 arm64: {
3753 srcs: ["libvndk27_arm64.so"],
3754 },
3755 },
Colin Cross2807f002021-03-02 10:15:29 -08003756 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003757 }
3758
3759 vndk_prebuilt_shared {
3760 name: "libvndk27",
3761 version: "27",
3762 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003763 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003764 vndk: {
3765 enabled: true,
3766 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003767 target_arch: "x86_64",
3768 arch: {
3769 x86: {
3770 srcs: ["libvndk27_x86.so"],
3771 },
3772 x86_64: {
3773 srcs: ["libvndk27_x86_64.so"],
3774 },
3775 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003776 }
3777 `+vndkLibrariesTxtFiles("27"),
3778 withFiles(map[string][]byte{
3779 "libvndk27_arm.so": nil,
3780 "libvndk27_arm64.so": nil,
3781 "libvndk27_x86.so": nil,
3782 "libvndk27_x86_64.so": nil,
3783 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003784
Jooyung Hana0503a52023-08-23 13:12:50 +09003785 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003786 "lib/libvndk27_arm.so",
3787 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003788 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003789 })
Jooyung Han344d5432019-08-23 11:17:39 +09003790}
3791
Jooyung Han90eee022019-10-01 20:02:42 +09003792func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003793 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003794 apex_vndk {
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003795 name: "com.android.vndk.v29",
Jooyung Han90eee022019-10-01 20:02:42 +09003796 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003797 file_contexts: ":myapex-file_contexts",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003798 vndk_version: "29",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003799 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003800 }
3801 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003802 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003803 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003804 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003805 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003806 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003807 }
3808 apex_key {
3809 name: "myapex.key",
3810 public_key: "testkey.avbpubkey",
3811 private_key: "testkey.pem",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003812 }`+vndkLibrariesTxtFiles("28", "29"))
Jooyung Han90eee022019-10-01 20:02:42 +09003813
3814 assertApexName := func(expected, moduleName string) {
Jooyung Hana0503a52023-08-23 13:12:50 +09003815 module := ctx.ModuleForTests(moduleName, "android_common")
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003816 apexManifestRule := module.Rule("apexManifestRule")
3817 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003818 }
3819
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003820 assertApexName("com.android.vndk.v29", "com.android.vndk.v29")
Colin Cross2807f002021-03-02 10:15:29 -08003821 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003822}
3823
Jooyung Han344d5432019-08-23 11:17:39 +09003824func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003825 testApexError(t, `module "com.android.vndk.v30" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003826 apex_vndk {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003827 name: "com.android.vndk.v30",
3828 key: "com.android.vndk.v30.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003829 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003830 native_bridge_supported: true,
3831 }
3832
3833 apex_key {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003834 name: "com.android.vndk.v30.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003835 public_key: "testkey.avbpubkey",
3836 private_key: "testkey.pem",
3837 }
3838
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003839 vndk_prebuilt_shared {
Jooyung Han344d5432019-08-23 11:17:39 +09003840 name: "libvndk",
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003841 version: "30",
3842 target_arch: "arm",
Jooyung Han344d5432019-08-23 11:17:39 +09003843 srcs: ["mylib.cpp"],
3844 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003845 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003846 native_bridge_supported: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003847 vndk: {
3848 enabled: true,
3849 },
Jooyung Han344d5432019-08-23 11:17:39 +09003850 }
3851 `)
3852}
3853
Jooyung Han31c470b2019-10-18 16:26:59 +09003854func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003855 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003856 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003857 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003858 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003859 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003860 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003861 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003862 }
3863
3864 apex_key {
3865 name: "myapex.key",
3866 public_key: "testkey.avbpubkey",
3867 private_key: "testkey.pem",
3868 }
3869
3870 vndk_prebuilt_shared {
3871 name: "libvndk27",
3872 version: "27",
3873 target_arch: "arm",
3874 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003875 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003876 vndk: {
3877 enabled: true,
3878 },
3879 arch: {
3880 arm: {
3881 srcs: ["libvndk27.so"],
3882 }
3883 },
3884 }
3885
3886 vndk_prebuilt_shared {
3887 name: "libvndk27",
3888 version: "27",
3889 target_arch: "arm",
3890 binder32bit: true,
3891 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003892 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003893 vndk: {
3894 enabled: true,
3895 },
3896 arch: {
3897 arm: {
3898 srcs: ["libvndk27binder32.so"],
3899 }
3900 },
Colin Cross2807f002021-03-02 10:15:29 -08003901 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003902 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003903 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003904 withFiles(map[string][]byte{
3905 "libvndk27.so": nil,
3906 "libvndk27binder32.so": nil,
3907 }),
3908 withBinder32bit,
3909 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07003910 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09003911 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3912 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003913 },
3914 }),
3915 )
3916
Jooyung Hana0503a52023-08-23 13:12:50 +09003917 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003918 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003919 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003920 })
3921}
3922
Jooyung Hane1633032019-08-01 17:41:43 +09003923func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003924 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003925 apex {
3926 name: "myapex_nodep",
3927 key: "myapex.key",
3928 native_shared_libs: ["lib_nodep"],
3929 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003930 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003931 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003932 }
3933
3934 apex {
3935 name: "myapex_dep",
3936 key: "myapex.key",
3937 native_shared_libs: ["lib_dep"],
3938 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003939 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003940 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003941 }
3942
3943 apex {
3944 name: "myapex_provider",
3945 key: "myapex.key",
3946 native_shared_libs: ["libfoo"],
3947 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003948 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003949 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003950 }
3951
3952 apex {
3953 name: "myapex_selfcontained",
3954 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00003955 native_shared_libs: ["lib_dep_on_bar", "libbar"],
Jooyung Hane1633032019-08-01 17:41:43 +09003956 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003957 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003958 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003959 }
3960
3961 apex_key {
3962 name: "myapex.key",
3963 public_key: "testkey.avbpubkey",
3964 private_key: "testkey.pem",
3965 }
3966
3967 cc_library {
3968 name: "lib_nodep",
3969 srcs: ["mylib.cpp"],
3970 system_shared_libs: [],
3971 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003972 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003973 }
3974
3975 cc_library {
3976 name: "lib_dep",
3977 srcs: ["mylib.cpp"],
3978 shared_libs: ["libfoo"],
3979 system_shared_libs: [],
3980 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003981 apex_available: [
3982 "myapex_dep",
3983 "myapex_provider",
3984 "myapex_selfcontained",
3985 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003986 }
3987
3988 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00003989 name: "lib_dep_on_bar",
3990 srcs: ["mylib.cpp"],
3991 shared_libs: ["libbar"],
3992 system_shared_libs: [],
3993 stl: "none",
3994 apex_available: [
3995 "myapex_selfcontained",
3996 ],
3997 }
3998
3999
4000 cc_library {
Jooyung Hane1633032019-08-01 17:41:43 +09004001 name: "libfoo",
4002 srcs: ["mytest.cpp"],
4003 stubs: {
4004 versions: ["1"],
4005 },
4006 system_shared_libs: [],
4007 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004008 apex_available: [
4009 "myapex_provider",
Spandan Das20fce2d2023-04-12 17:21:39 +00004010 ],
4011 }
4012
4013 cc_library {
4014 name: "libbar",
4015 srcs: ["mytest.cpp"],
4016 stubs: {
4017 versions: ["1"],
4018 },
4019 system_shared_libs: [],
4020 stl: "none",
4021 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004022 "myapex_selfcontained",
4023 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004024 }
Spandan Das20fce2d2023-04-12 17:21:39 +00004025
Jooyung Hane1633032019-08-01 17:41:43 +09004026 `)
4027
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004028 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004029 var provideNativeLibs, requireNativeLibs []string
4030
Jooyung Hana0503a52023-08-23 13:12:50 +09004031 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004032 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4033 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004034 ensureListEmpty(t, provideNativeLibs)
4035 ensureListEmpty(t, requireNativeLibs)
4036
Jooyung Hana0503a52023-08-23 13:12:50 +09004037 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004038 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4039 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004040 ensureListEmpty(t, provideNativeLibs)
4041 ensureListContains(t, requireNativeLibs, "libfoo.so")
4042
Jooyung Hana0503a52023-08-23 13:12:50 +09004043 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004044 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4045 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004046 ensureListContains(t, provideNativeLibs, "libfoo.so")
4047 ensureListEmpty(t, requireNativeLibs)
4048
Jooyung Hana0503a52023-08-23 13:12:50 +09004049 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004050 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4051 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Spandan Das20fce2d2023-04-12 17:21:39 +00004052 ensureListContains(t, provideNativeLibs, "libbar.so")
Jooyung Hane1633032019-08-01 17:41:43 +09004053 ensureListEmpty(t, requireNativeLibs)
4054}
4055
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004056func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4057 ctx := testApex(t, `
4058 apex {
4059 name: "myapex",
4060 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004061 native_shared_libs: ["mylib"],
4062 updatable: false,
4063 }
4064
4065 apex_key {
4066 name: "myapex.key",
4067 public_key: "testkey.avbpubkey",
4068 private_key: "testkey.pem",
4069 }
4070
4071 cc_library {
4072 name: "mylib",
4073 srcs: ["mylib.cpp"],
4074 system_shared_libs: [],
4075 stl: "none",
4076 apex_available: [
4077 "//apex_available:platform",
4078 "myapex",
4079 ],
4080 }
4081 `, android.FixtureMergeEnv(map[string]string{
4082 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4083 }))
4084
Jooyung Hana0503a52023-08-23 13:12:50 +09004085 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004086 apexManifestRule := module.Rule("apexManifestRule")
4087 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4088}
4089
Vinh Tran8f5310f2022-10-07 18:16:47 -04004090func TestCompileMultilibProp(t *testing.T) {
4091 testCases := []struct {
4092 compileMultiLibProp string
4093 containedLibs []string
4094 notContainedLibs []string
4095 }{
4096 {
4097 containedLibs: []string{
4098 "image.apex/lib64/mylib.so",
4099 "image.apex/lib/mylib.so",
4100 },
4101 compileMultiLibProp: `compile_multilib: "both",`,
4102 },
4103 {
4104 containedLibs: []string{"image.apex/lib64/mylib.so"},
4105 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4106 compileMultiLibProp: `compile_multilib: "first",`,
4107 },
4108 {
4109 containedLibs: []string{"image.apex/lib64/mylib.so"},
4110 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4111 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4112 },
4113 {
4114 containedLibs: []string{"image.apex/lib64/mylib.so"},
4115 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4116 compileMultiLibProp: `compile_multilib: "64",`,
4117 },
4118 {
4119 containedLibs: []string{"image.apex/lib/mylib.so"},
4120 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4121 compileMultiLibProp: `compile_multilib: "32",`,
4122 },
4123 }
4124 for _, testCase := range testCases {
4125 ctx := testApex(t, fmt.Sprintf(`
4126 apex {
4127 name: "myapex",
4128 key: "myapex.key",
4129 %s
4130 native_shared_libs: ["mylib"],
4131 updatable: false,
4132 }
4133 apex_key {
4134 name: "myapex.key",
4135 public_key: "testkey.avbpubkey",
4136 private_key: "testkey.pem",
4137 }
4138 cc_library {
4139 name: "mylib",
4140 srcs: ["mylib.cpp"],
4141 apex_available: [
4142 "//apex_available:platform",
4143 "myapex",
4144 ],
4145 }
4146 `, testCase.compileMultiLibProp),
4147 )
Jooyung Hana0503a52023-08-23 13:12:50 +09004148 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Vinh Tran8f5310f2022-10-07 18:16:47 -04004149 apexRule := module.Rule("apexRule")
4150 copyCmds := apexRule.Args["copy_commands"]
4151 for _, containedLib := range testCase.containedLibs {
4152 ensureContains(t, copyCmds, containedLib)
4153 }
4154 for _, notContainedLib := range testCase.notContainedLibs {
4155 ensureNotContains(t, copyCmds, notContainedLib)
4156 }
4157 }
4158}
4159
Alex Light0851b882019-02-07 13:20:53 -08004160func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004161 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004162 apex {
4163 name: "myapex",
4164 key: "myapex.key",
4165 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004166 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004167 }
4168
4169 apex_key {
4170 name: "myapex.key",
4171 public_key: "testkey.avbpubkey",
4172 private_key: "testkey.pem",
4173 }
4174
4175 cc_library {
4176 name: "mylib_common",
4177 srcs: ["mylib.cpp"],
4178 system_shared_libs: [],
4179 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004180 apex_available: [
4181 "//apex_available:platform",
4182 "myapex",
4183 ],
Alex Light0851b882019-02-07 13:20:53 -08004184 }
4185 `)
4186
Jooyung Hana0503a52023-08-23 13:12:50 +09004187 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004188 apexRule := module.Rule("apexRule")
4189 copyCmds := apexRule.Args["copy_commands"]
4190
4191 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4192 t.Log("Apex was a test apex!")
4193 t.Fail()
4194 }
4195 // Ensure that main rule creates an output
4196 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4197
4198 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004199 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004200
4201 // Ensure that both direct and indirect deps are copied into apex
4202 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4203
Colin Cross7113d202019-11-20 16:39:12 -08004204 // Ensure that the platform variant ends with _shared
4205 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004206
Colin Cross56a83212020-09-15 18:30:11 -07004207 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004208 t.Log("Found mylib_common not in any apex!")
4209 t.Fail()
4210 }
4211}
4212
4213func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004214 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004215 apex_test {
4216 name: "myapex",
4217 key: "myapex.key",
4218 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004219 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004220 }
4221
4222 apex_key {
4223 name: "myapex.key",
4224 public_key: "testkey.avbpubkey",
4225 private_key: "testkey.pem",
4226 }
4227
4228 cc_library {
4229 name: "mylib_common_test",
4230 srcs: ["mylib.cpp"],
4231 system_shared_libs: [],
4232 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004233 // TODO: remove //apex_available:platform
4234 apex_available: [
4235 "//apex_available:platform",
4236 "myapex",
4237 ],
Alex Light0851b882019-02-07 13:20:53 -08004238 }
4239 `)
4240
Jooyung Hana0503a52023-08-23 13:12:50 +09004241 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004242 apexRule := module.Rule("apexRule")
4243 copyCmds := apexRule.Args["copy_commands"]
4244
4245 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4246 t.Log("Apex was not a test apex!")
4247 t.Fail()
4248 }
4249 // Ensure that main rule creates an output
4250 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4251
4252 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004253 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004254
4255 // Ensure that both direct and indirect deps are copied into apex
4256 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4257
Colin Cross7113d202019-11-20 16:39:12 -08004258 // Ensure that the platform variant ends with _shared
4259 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004260}
4261
Jooyung Han85707de2023-12-01 14:21:13 +09004262func TestLibzVendorIsntStable(t *testing.T) {
4263 ctx := testApex(t, `
4264 apex {
4265 name: "myapex",
4266 key: "myapex.key",
4267 updatable: false,
4268 binaries: ["mybin"],
4269 }
4270 apex {
4271 name: "myvendorapex",
4272 key: "myapex.key",
4273 file_contexts: "myvendorapex_file_contexts",
4274 vendor: true,
4275 updatable: false,
4276 binaries: ["mybin"],
4277 }
4278 apex_key {
4279 name: "myapex.key",
4280 public_key: "testkey.avbpubkey",
4281 private_key: "testkey.pem",
4282 }
4283 cc_binary {
4284 name: "mybin",
4285 vendor_available: true,
4286 system_shared_libs: [],
4287 stl: "none",
4288 shared_libs: ["libz"],
4289 apex_available: ["//apex_available:anyapex"],
4290 }
4291 cc_library {
4292 name: "libz",
4293 vendor_available: true,
4294 system_shared_libs: [],
4295 stl: "none",
4296 stubs: {
4297 versions: ["28", "30"],
4298 },
4299 target: {
4300 vendor: {
4301 no_stubs: true,
4302 },
4303 },
4304 }
4305 `, withFiles(map[string][]byte{
4306 "myvendorapex_file_contexts": nil,
4307 }))
4308
4309 // libz provides stubs for core variant.
4310 {
4311 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
4312 "bin/mybin",
4313 })
4314 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
4315 android.AssertStringEquals(t, "should require libz", apexManifestRule.Args["requireNativeLibs"], "libz.so")
4316 }
4317 // libz doesn't provide stubs for vendor variant.
4318 {
4319 ensureExactContents(t, ctx, "myvendorapex", "android_common_myvendorapex", []string{
4320 "bin/mybin",
4321 "lib64/libz.so",
4322 })
4323 apexManifestRule := ctx.ModuleForTests("myvendorapex", "android_common_myvendorapex").Rule("apexManifestRule")
4324 android.AssertStringEquals(t, "should not require libz", apexManifestRule.Args["requireNativeLibs"], "")
4325 }
4326}
4327
Alex Light9670d332019-01-29 18:07:33 -08004328func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004329 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004330 apex {
4331 name: "myapex",
4332 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004333 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004334 multilib: {
4335 first: {
4336 native_shared_libs: ["mylib_common"],
4337 }
4338 },
4339 target: {
4340 android: {
4341 multilib: {
4342 first: {
4343 native_shared_libs: ["mylib"],
4344 }
4345 }
4346 },
4347 host: {
4348 multilib: {
4349 first: {
4350 native_shared_libs: ["mylib2"],
4351 }
4352 }
4353 }
4354 }
4355 }
4356
4357 apex_key {
4358 name: "myapex.key",
4359 public_key: "testkey.avbpubkey",
4360 private_key: "testkey.pem",
4361 }
4362
4363 cc_library {
4364 name: "mylib",
4365 srcs: ["mylib.cpp"],
4366 system_shared_libs: [],
4367 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004368 // TODO: remove //apex_available:platform
4369 apex_available: [
4370 "//apex_available:platform",
4371 "myapex",
4372 ],
Alex Light9670d332019-01-29 18:07:33 -08004373 }
4374
4375 cc_library {
4376 name: "mylib_common",
4377 srcs: ["mylib.cpp"],
4378 system_shared_libs: [],
4379 stl: "none",
4380 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004381 // TODO: remove //apex_available:platform
4382 apex_available: [
4383 "//apex_available:platform",
4384 "myapex",
4385 ],
Alex Light9670d332019-01-29 18:07:33 -08004386 }
4387
4388 cc_library {
4389 name: "mylib2",
4390 srcs: ["mylib.cpp"],
4391 system_shared_libs: [],
4392 stl: "none",
4393 compile_multilib: "first",
4394 }
4395 `)
4396
Jooyung Hana0503a52023-08-23 13:12:50 +09004397 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004398 copyCmds := apexRule.Args["copy_commands"]
4399
4400 // Ensure that main rule creates an output
4401 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4402
4403 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004404 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4405 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4406 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004407
4408 // Ensure that both direct and indirect deps are copied into apex
4409 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4410 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4411 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4412
Colin Cross7113d202019-11-20 16:39:12 -08004413 // Ensure that the platform variant ends with _shared
4414 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4415 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4416 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004417}
Jiyong Park04480cf2019-02-06 00:16:29 +09004418
Jiyong Park59140302020-12-14 18:44:04 +09004419func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004420 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004421 apex {
4422 name: "myapex",
4423 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004424 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004425 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004426 arch: {
4427 arm64: {
4428 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004429 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004430 },
4431 x86_64: {
4432 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004433 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004434 },
4435 }
4436 }
4437
4438 apex_key {
4439 name: "myapex.key",
4440 public_key: "testkey.avbpubkey",
4441 private_key: "testkey.pem",
4442 }
4443
4444 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004445 name: "mylib.generic",
4446 srcs: ["mylib.cpp"],
4447 system_shared_libs: [],
4448 stl: "none",
4449 // TODO: remove //apex_available:platform
4450 apex_available: [
4451 "//apex_available:platform",
4452 "myapex",
4453 ],
4454 }
4455
4456 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004457 name: "mylib.arm64",
4458 srcs: ["mylib.cpp"],
4459 system_shared_libs: [],
4460 stl: "none",
4461 // TODO: remove //apex_available:platform
4462 apex_available: [
4463 "//apex_available:platform",
4464 "myapex",
4465 ],
4466 }
4467
4468 cc_library {
4469 name: "mylib.x64",
4470 srcs: ["mylib.cpp"],
4471 system_shared_libs: [],
4472 stl: "none",
4473 // TODO: remove //apex_available:platform
4474 apex_available: [
4475 "//apex_available:platform",
4476 "myapex",
4477 ],
4478 }
4479 `)
4480
Jooyung Hana0503a52023-08-23 13:12:50 +09004481 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park59140302020-12-14 18:44:04 +09004482 copyCmds := apexRule.Args["copy_commands"]
4483
4484 // Ensure that apex variant is created for the direct dep
4485 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004486 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004487 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4488
4489 // Ensure that both direct and indirect deps are copied into apex
4490 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4491 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4492}
4493
Jiyong Park04480cf2019-02-06 00:16:29 +09004494func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004495 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004496 apex {
4497 name: "myapex",
4498 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004499 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004500 updatable: false,
Riya Thakur654461c2024-02-27 07:21:05 +00004501 compile_multilib: "both",
Jiyong Park04480cf2019-02-06 00:16:29 +09004502 }
4503
4504 apex_key {
4505 name: "myapex.key",
4506 public_key: "testkey.avbpubkey",
4507 private_key: "testkey.pem",
4508 }
4509
4510 sh_binary {
4511 name: "myscript",
4512 src: "mylib.cpp",
4513 filename: "myscript.sh",
4514 sub_dir: "script",
4515 }
4516 `)
4517
Jooyung Hana0503a52023-08-23 13:12:50 +09004518 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004519 copyCmds := apexRule.Args["copy_commands"]
4520
4521 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4522}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004523
Jooyung Han91df2082019-11-20 01:49:42 +09004524func TestApexInVariousPartition(t *testing.T) {
4525 testcases := []struct {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004526 propName, partition string
Jooyung Han91df2082019-11-20 01:49:42 +09004527 }{
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004528 {"", "system"},
4529 {"product_specific: true", "product"},
4530 {"soc_specific: true", "vendor"},
4531 {"proprietary: true", "vendor"},
4532 {"vendor: true", "vendor"},
4533 {"system_ext_specific: true", "system_ext"},
Jooyung Han91df2082019-11-20 01:49:42 +09004534 }
4535 for _, tc := range testcases {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004536 t.Run(tc.propName+":"+tc.partition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004537 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004538 apex {
4539 name: "myapex",
4540 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004541 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004542 `+tc.propName+`
4543 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004544
Jooyung Han91df2082019-11-20 01:49:42 +09004545 apex_key {
4546 name: "myapex.key",
4547 public_key: "testkey.avbpubkey",
4548 private_key: "testkey.pem",
4549 }
4550 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004551
Jooyung Hana0503a52023-08-23 13:12:50 +09004552 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004553 expected := "out/soong/target/product/test_device/" + tc.partition + "/apex"
Paul Duffin37ba3442021-03-29 00:21:08 +01004554 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004555 if actual != expected {
4556 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4557 }
Jooyung Han91df2082019-11-20 01:49:42 +09004558 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004559 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004560}
Jiyong Park67882562019-03-21 01:11:21 +09004561
Jooyung Han580eb4f2020-06-24 19:33:06 +09004562func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004563 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004564 apex {
4565 name: "myapex",
4566 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004567 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004568 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004569
Jooyung Han580eb4f2020-06-24 19:33:06 +09004570 apex_key {
4571 name: "myapex.key",
4572 public_key: "testkey.avbpubkey",
4573 private_key: "testkey.pem",
4574 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004575 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09004576 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004577 rule := module.Output("file_contexts")
4578 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4579}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004580
Jooyung Han580eb4f2020-06-24 19:33:06 +09004581func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004582 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004583 apex {
4584 name: "myapex",
4585 key: "myapex.key",
4586 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004587 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004588 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004589
Jooyung Han580eb4f2020-06-24 19:33:06 +09004590 apex_key {
4591 name: "myapex.key",
4592 public_key: "testkey.avbpubkey",
4593 private_key: "testkey.pem",
4594 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004595 `, withFiles(map[string][]byte{
4596 "my_own_file_contexts": nil,
4597 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004598}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004599
Jooyung Han580eb4f2020-06-24 19:33:06 +09004600func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004601 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004602 apex {
4603 name: "myapex",
4604 key: "myapex.key",
4605 product_specific: true,
4606 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004607 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004608 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004609
Jooyung Han580eb4f2020-06-24 19:33:06 +09004610 apex_key {
4611 name: "myapex.key",
4612 public_key: "testkey.avbpubkey",
4613 private_key: "testkey.pem",
4614 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004615 `)
4616
Colin Cross1c460562021-02-16 17:55:47 -08004617 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004618 apex {
4619 name: "myapex",
4620 key: "myapex.key",
4621 product_specific: true,
4622 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004623 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004624 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004625
Jooyung Han580eb4f2020-06-24 19:33:06 +09004626 apex_key {
4627 name: "myapex.key",
4628 public_key: "testkey.avbpubkey",
4629 private_key: "testkey.pem",
4630 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004631 `, withFiles(map[string][]byte{
4632 "product_specific_file_contexts": nil,
4633 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004634 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004635 rule := module.Output("file_contexts")
4636 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4637}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004638
Jooyung Han580eb4f2020-06-24 19:33:06 +09004639func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004640 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004641 apex {
4642 name: "myapex",
4643 key: "myapex.key",
4644 product_specific: true,
4645 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004646 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004647 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004648
Jooyung Han580eb4f2020-06-24 19:33:06 +09004649 apex_key {
4650 name: "myapex.key",
4651 public_key: "testkey.avbpubkey",
4652 private_key: "testkey.pem",
4653 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004654
Jooyung Han580eb4f2020-06-24 19:33:06 +09004655 filegroup {
4656 name: "my-file-contexts",
4657 srcs: ["product_specific_file_contexts"],
4658 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004659 `, withFiles(map[string][]byte{
4660 "product_specific_file_contexts": nil,
4661 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004662 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004663 rule := module.Output("file_contexts")
4664 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004665}
4666
Jiyong Park67882562019-03-21 01:11:21 +09004667func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004668 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004669 apex_key {
4670 name: "myapex.key",
4671 public_key: ":my.avbpubkey",
4672 private_key: ":my.pem",
4673 product_specific: true,
4674 }
4675
4676 filegroup {
4677 name: "my.avbpubkey",
4678 srcs: ["testkey2.avbpubkey"],
4679 }
4680
4681 filegroup {
4682 name: "my.pem",
4683 srcs: ["testkey2.pem"],
4684 }
4685 `)
4686
4687 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4688 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004689 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004690 if actual_pubkey != expected_pubkey {
4691 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4692 }
4693 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004694 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004695 if actual_privkey != expected_privkey {
4696 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4697 }
4698}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004699
4700func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004701 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004702 prebuilt_apex {
4703 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004704 arch: {
4705 arm64: {
4706 src: "myapex-arm64.apex",
4707 },
4708 arm: {
4709 src: "myapex-arm.apex",
4710 },
4711 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004712 }
4713 `)
4714
Wei Li340ee8e2022-03-18 17:33:24 -07004715 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4716 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004717
Jiyong Parkc95714e2019-03-29 14:23:10 +09004718 expectedInput := "myapex-arm64.apex"
4719 if prebuilt.inputApex.String() != expectedInput {
4720 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4721 }
Wei Li340ee8e2022-03-18 17:33:24 -07004722 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4723 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4724 rule := testingModule.Rule("genProvenanceMetaData")
4725 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4726 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4727 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4728 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08004729
4730 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
4731 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004732}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004733
Paul Duffinc0609c62021-03-01 17:27:16 +00004734func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004735 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004736 prebuilt_apex {
4737 name: "myapex",
4738 }
4739 `)
4740}
4741
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004742func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004743 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004744 prebuilt_apex {
4745 name: "myapex",
4746 src: "myapex-arm.apex",
4747 filename: "notmyapex.apex",
4748 }
4749 `)
4750
Wei Li340ee8e2022-03-18 17:33:24 -07004751 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4752 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004753
4754 expected := "notmyapex.apex"
4755 if p.installFilename != expected {
4756 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4757 }
Wei Li340ee8e2022-03-18 17:33:24 -07004758 rule := testingModule.Rule("genProvenanceMetaData")
4759 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4760 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4761 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4762 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004763}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004764
Samiul Islam7c02e262021-09-08 17:48:28 +01004765func TestApexSetFilenameOverride(t *testing.T) {
4766 testApex(t, `
4767 apex_set {
4768 name: "com.company.android.myapex",
4769 apex_name: "com.android.myapex",
4770 set: "company-myapex.apks",
4771 filename: "com.company.android.myapex.apex"
4772 }
4773 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4774
4775 testApex(t, `
4776 apex_set {
4777 name: "com.company.android.myapex",
4778 apex_name: "com.android.myapex",
4779 set: "company-myapex.apks",
4780 filename: "com.company.android.myapex.capex"
4781 }
4782 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4783
4784 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4785 apex_set {
4786 name: "com.company.android.myapex",
4787 apex_name: "com.android.myapex",
4788 set: "company-myapex.apks",
4789 filename: "some-random-suffix"
4790 }
4791 `)
4792}
4793
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004794func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004795 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004796 prebuilt_apex {
4797 name: "myapex.prebuilt",
4798 src: "myapex-arm.apex",
4799 overrides: [
4800 "myapex",
4801 ],
4802 }
4803 `)
4804
Wei Li340ee8e2022-03-18 17:33:24 -07004805 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4806 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004807
4808 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004809 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004810 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004811 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004812 }
Wei Li340ee8e2022-03-18 17:33:24 -07004813 rule := testingModule.Rule("genProvenanceMetaData")
4814 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4815 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4816 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4817 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004818}
4819
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004820func TestPrebuiltApexName(t *testing.T) {
4821 testApex(t, `
4822 prebuilt_apex {
4823 name: "com.company.android.myapex",
4824 apex_name: "com.android.myapex",
4825 src: "company-myapex-arm.apex",
4826 }
4827 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4828
4829 testApex(t, `
4830 apex_set {
4831 name: "com.company.android.myapex",
4832 apex_name: "com.android.myapex",
4833 set: "company-myapex.apks",
4834 }
4835 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4836}
4837
4838func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4839 _ = android.GroupFixturePreparers(
4840 java.PrepareForTestWithJavaDefaultModules,
4841 PrepareForTestWithApexBuildComponents,
4842 android.FixtureWithRootAndroidBp(`
4843 platform_bootclasspath {
4844 name: "platform-bootclasspath",
4845 fragments: [
4846 {
4847 apex: "com.android.art",
4848 module: "art-bootclasspath-fragment",
4849 },
4850 ],
4851 }
4852
4853 prebuilt_apex {
4854 name: "com.company.android.art",
4855 apex_name: "com.android.art",
4856 src: "com.company.android.art-arm.apex",
4857 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4858 }
4859
4860 prebuilt_bootclasspath_fragment {
4861 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004862 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004863 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004864 hidden_api: {
4865 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4866 metadata: "my-bootclasspath-fragment/metadata.csv",
4867 index: "my-bootclasspath-fragment/index.csv",
4868 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4869 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4870 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004871 }
4872
4873 java_import {
4874 name: "core-oj",
4875 jars: ["prebuilt.jar"],
4876 }
4877 `),
4878 ).RunTest(t)
4879}
4880
Spandan Das59a4a2b2024-01-09 21:35:56 +00004881// A minimal context object for use with DexJarBuildPath
4882type moduleErrorfTestCtx struct {
4883}
4884
4885func (ctx moduleErrorfTestCtx) ModuleErrorf(format string, args ...interface{}) {
4886}
4887
Paul Duffin092153d2021-01-26 11:42:39 +00004888// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4889// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004890func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004891 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004892
Paul Duffin89886cb2021-02-05 16:44:03 +00004893 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004894 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004895 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004896 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +00004897 dexJarBuildPath := p.DexJarBuildPath(moduleErrorfTestCtx{}).PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004898 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004899 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
Spandan Das3576e762024-01-03 18:57:03 +00004900 ".intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
Jeongik Chad5fe8782021-07-08 01:13:11 +09004901 android.NormalizePathForTesting(dexJarBuildPath))
4902 }
4903
4904 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004905 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004906 // Make sure the import has been given the correct path to the dex jar.
4907 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4908 dexJarBuildPath := p.DexJarInstallPath()
4909 stem := android.RemoveOptionalPrebuiltPrefix(name)
4910 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4911 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4912 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004913 }
4914
Paul Duffin39853512021-02-26 11:09:39 +00004915 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004916 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004917 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004918 android.AssertArrayString(t, "Check if there is no source variant",
4919 []string{"android_common"},
4920 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004921 }
4922
4923 t.Run("prebuilt only", func(t *testing.T) {
4924 bp := `
4925 prebuilt_apex {
4926 name: "myapex",
4927 arch: {
4928 arm64: {
4929 src: "myapex-arm64.apex",
4930 },
4931 arm: {
4932 src: "myapex-arm.apex",
4933 },
4934 },
Paul Duffin39853512021-02-26 11:09:39 +00004935 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004936 }
4937
4938 java_import {
4939 name: "libfoo",
4940 jars: ["libfoo.jar"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00004941 sdk_version: "core_current",
Paul Duffin064b70c2020-11-02 17:32:38 +00004942 }
Paul Duffin39853512021-02-26 11:09:39 +00004943
4944 java_sdk_library_import {
4945 name: "libbar",
4946 public: {
4947 jars: ["libbar.jar"],
4948 },
4949 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004950 `
4951
4952 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4953 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4954
Spandan Das3576e762024-01-03 18:57:03 +00004955 deapexerName := deapexerModuleName("prebuilt_myapex")
4956 android.AssertStringEquals(t, "APEX module name from deapexer name", "prebuilt_myapex", apexModuleName(deapexerName))
Martin Stjernholm44825602021-09-17 01:44:12 +01004957
Paul Duffinf6932af2021-02-26 18:21:56 +00004958 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004959 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004960 rule := deapexer.Rule("deapexer")
4961 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4962 t.Errorf("expected: %q, found: %q", expected, actual)
4963 }
4964
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004965 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004966 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004967 rule = prebuiltApex.Rule("android/soong/android.Cp")
4968 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4969 t.Errorf("expected: %q, found: %q", expected, actual)
4970 }
4971
Paul Duffin89886cb2021-02-05 16:44:03 +00004972 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004973 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004974
4975 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004976 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004977 })
4978
4979 t.Run("prebuilt with source preferred", func(t *testing.T) {
4980
4981 bp := `
Jihoon Kang85bc1932024-07-01 17:04:46 +00004982 apex {
4983 name: "myapex",
4984 key: "myapex.key",
4985 updatable: false,
4986 java_libs: [
4987 "libfoo",
4988 "libbar",
4989 ],
4990 }
4991
4992 apex_key {
4993 name: "myapex.key",
4994 public_key: "testkey.avbpubkey",
4995 private_key: "testkey.pem",
4996 }
4997
Paul Duffin064b70c2020-11-02 17:32:38 +00004998 prebuilt_apex {
4999 name: "myapex",
5000 arch: {
5001 arm64: {
5002 src: "myapex-arm64.apex",
5003 },
5004 arm: {
5005 src: "myapex-arm.apex",
5006 },
5007 },
Paul Duffin39853512021-02-26 11:09:39 +00005008 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005009 }
5010
5011 java_import {
5012 name: "libfoo",
5013 jars: ["libfoo.jar"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005014 apex_available: [
5015 "myapex",
5016 ],
5017 compile_dex: true,
5018 sdk_version: "core_current",
Paul Duffin064b70c2020-11-02 17:32:38 +00005019 }
5020
5021 java_library {
5022 name: "libfoo",
Jihoon Kang85bc1932024-07-01 17:04:46 +00005023 srcs: ["foo/bar/MyClass.java"],
5024 apex_available: [
5025 "myapex",
5026 ],
5027 compile_dex: true,
5028 sdk_version: "core_current",
Paul Duffin064b70c2020-11-02 17:32:38 +00005029 }
Paul Duffin39853512021-02-26 11:09:39 +00005030
5031 java_sdk_library_import {
5032 name: "libbar",
5033 public: {
5034 jars: ["libbar.jar"],
5035 },
Jihoon Kang85bc1932024-07-01 17:04:46 +00005036 apex_available: [
5037 "myapex",
5038 ],
5039 compile_dex: true,
Paul Duffin39853512021-02-26 11:09:39 +00005040 }
5041
5042 java_sdk_library {
5043 name: "libbar",
5044 srcs: ["foo/bar/MyClass.java"],
5045 unsafe_ignore_missing_latest_api: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00005046 apex_available: [
5047 "myapex",
5048 ],
5049 compile_dex: true,
5050 sdk_version: "core_current",
Paul Duffin39853512021-02-26 11:09:39 +00005051 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005052 `
5053
5054 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5055 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5056
Paul Duffin89886cb2021-02-05 16:44:03 +00005057 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005058 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005059
5060 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005061 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005062 })
5063
5064 t.Run("prebuilt preferred with source", func(t *testing.T) {
5065 bp := `
5066 prebuilt_apex {
5067 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005068 arch: {
5069 arm64: {
5070 src: "myapex-arm64.apex",
5071 },
5072 arm: {
5073 src: "myapex-arm.apex",
5074 },
5075 },
Paul Duffin39853512021-02-26 11:09:39 +00005076 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005077 }
5078
5079 java_import {
5080 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005081 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005082 jars: ["libfoo.jar"],
5083 }
5084
5085 java_library {
5086 name: "libfoo",
Jihoon Kang85bc1932024-07-01 17:04:46 +00005087 sdk_version: "core_current",
Paul Duffin064b70c2020-11-02 17:32:38 +00005088 }
Paul Duffin39853512021-02-26 11:09:39 +00005089
5090 java_sdk_library_import {
5091 name: "libbar",
5092 prefer: true,
5093 public: {
5094 jars: ["libbar.jar"],
5095 },
5096 }
5097
5098 java_sdk_library {
5099 name: "libbar",
5100 srcs: ["foo/bar/MyClass.java"],
5101 unsafe_ignore_missing_latest_api: true,
5102 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005103 `
5104
5105 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5106 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5107
Paul Duffin89886cb2021-02-05 16:44:03 +00005108 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005109 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005110 ensureNoSourceVariant(t, ctx, "libfoo")
5111
5112 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005113 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005114 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005115 })
5116}
5117
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005118func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005119 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005120 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005121 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5122 // is disabled.
5123 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
Spandan Das81fe4d12024-05-15 18:43:47 +00005124
5125 // Make sure that we have atleast one platform library so that we can check the monolithic hiddenapi
5126 // file creation.
5127 java.FixtureConfigureBootJars("platform:foo"),
5128 android.FixtureModifyMockFS(func(fs android.MockFS) {
5129 fs["platform/Android.bp"] = []byte(`
5130 java_library {
5131 name: "foo",
5132 srcs: ["Test.java"],
5133 compile_dex: true,
5134 }
5135 `)
5136 fs["platform/Test.java"] = nil
5137 }),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005138 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005139
Paul Duffin37856732021-02-26 14:24:15 +00005140 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5141 t.Helper()
Jiakai Zhangc6879f32023-11-06 16:31:19 +00005142 s := ctx.ModuleForTests("dex_bootjars", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005143 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005144 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005145 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005146 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005147 foundLibfooJar = true
5148 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005149 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005150 }
5151 }
5152 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005153 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005154 }
5155 }
5156
Paul Duffin40a3f652021-07-19 13:11:24 +01005157 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005158 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005159 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005160 var rule android.TestingBuildParams
5161
5162 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5163 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005164 }
5165
Paul Duffin40a3f652021-07-19 13:11:24 +01005166 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5167 t.Helper()
5168 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5169 var rule android.TestingBuildParams
5170
5171 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5172 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5173 }
5174
Paul Duffin89f570a2021-06-16 01:42:33 +01005175 fragment := java.ApexVariantReference{
5176 Apex: proptools.StringPtr("myapex"),
5177 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5178 }
5179
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005180 t.Run("prebuilt only", func(t *testing.T) {
5181 bp := `
5182 prebuilt_apex {
5183 name: "myapex",
5184 arch: {
5185 arm64: {
5186 src: "myapex-arm64.apex",
5187 },
5188 arm: {
5189 src: "myapex-arm.apex",
5190 },
5191 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005192 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5193 }
5194
5195 prebuilt_bootclasspath_fragment {
5196 name: "my-bootclasspath-fragment",
5197 contents: ["libfoo", "libbar"],
5198 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005199 hidden_api: {
5200 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5201 metadata: "my-bootclasspath-fragment/metadata.csv",
5202 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005203 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5204 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5205 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005206 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005207 }
5208
5209 java_import {
5210 name: "libfoo",
5211 jars: ["libfoo.jar"],
5212 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005213 permitted_packages: ["foo"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005214 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005215 }
Paul Duffin37856732021-02-26 14:24:15 +00005216
5217 java_sdk_library_import {
5218 name: "libbar",
5219 public: {
5220 jars: ["libbar.jar"],
5221 },
5222 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005223 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005224 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005225 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005226 `
5227
Paul Duffin89f570a2021-06-16 01:42:33 +01005228 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005229 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5230 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005231
Paul Duffin537ea3d2021-05-14 10:38:00 +01005232 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005233 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005234 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005235 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005236 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005237 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 +01005238 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005239 })
5240
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005241 t.Run("apex_set only", func(t *testing.T) {
5242 bp := `
5243 apex_set {
5244 name: "myapex",
5245 set: "myapex.apks",
Liz Kammer2dc72442023-04-20 10:10:48 -04005246 exported_java_libs: ["myjavalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005247 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
Liz Kammer2dc72442023-04-20 10:10:48 -04005248 exported_systemserverclasspath_fragments: ["my-systemserverclasspath-fragment"],
5249 }
5250
5251 java_import {
5252 name: "myjavalib",
5253 jars: ["myjavalib.jar"],
5254 apex_available: ["myapex"],
5255 permitted_packages: ["javalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005256 }
5257
5258 prebuilt_bootclasspath_fragment {
5259 name: "my-bootclasspath-fragment",
5260 contents: ["libfoo", "libbar"],
5261 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005262 hidden_api: {
5263 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5264 metadata: "my-bootclasspath-fragment/metadata.csv",
5265 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005266 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5267 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5268 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005269 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005270 }
5271
Liz Kammer2dc72442023-04-20 10:10:48 -04005272 prebuilt_systemserverclasspath_fragment {
5273 name: "my-systemserverclasspath-fragment",
5274 contents: ["libbaz"],
5275 apex_available: ["myapex"],
5276 }
5277
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005278 java_import {
5279 name: "libfoo",
5280 jars: ["libfoo.jar"],
5281 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005282 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005283 }
5284
5285 java_sdk_library_import {
5286 name: "libbar",
5287 public: {
5288 jars: ["libbar.jar"],
5289 },
5290 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005291 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005292 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005293 }
Liz Kammer2dc72442023-04-20 10:10:48 -04005294
5295 java_sdk_library_import {
5296 name: "libbaz",
5297 public: {
5298 jars: ["libbaz.jar"],
5299 },
5300 apex_available: ["myapex"],
5301 shared_library: false,
5302 permitted_packages: ["baz"],
5303 }
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005304 `
5305
Paul Duffin89f570a2021-06-16 01:42:33 +01005306 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005307 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5308 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005309
Paul Duffin537ea3d2021-05-14 10:38:00 +01005310 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005311 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005312 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005313 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005314 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005315 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 +01005316 `)
Liz Kammer2dc72442023-04-20 10:10:48 -04005317
5318 myApex := ctx.ModuleForTests("myapex", "android_common_myapex").Module()
5319
5320 overrideNames := []string{
Spandan Dasa8e2d612024-07-26 19:24:27 +00005321 "",
Liz Kammer2dc72442023-04-20 10:10:48 -04005322 "myjavalib.myapex",
5323 "libfoo.myapex",
5324 "libbar.myapex",
5325 "libbaz.myapex",
5326 }
5327 mkEntries := android.AndroidMkEntriesForTest(t, ctx, myApex)
5328 for i, e := range mkEntries {
5329 g := e.OverrideName
5330 if w := overrideNames[i]; w != g {
5331 t.Errorf("Expected override name %q, got %q", w, g)
5332 }
5333 }
5334
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005335 })
5336
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005337 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5338 bp := `
5339 prebuilt_apex {
5340 name: "myapex",
5341 arch: {
5342 arm64: {
5343 src: "myapex-arm64.apex",
5344 },
5345 arm: {
5346 src: "myapex-arm.apex",
5347 },
5348 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005349 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5350 }
5351
5352 prebuilt_bootclasspath_fragment {
5353 name: "my-bootclasspath-fragment",
5354 contents: ["libfoo", "libbar"],
5355 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005356 hidden_api: {
5357 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5358 metadata: "my-bootclasspath-fragment/metadata.csv",
5359 index: "my-bootclasspath-fragment/index.csv",
5360 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5361 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5362 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005363 }
5364
5365 java_import {
5366 name: "libfoo",
5367 jars: ["libfoo.jar"],
5368 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005369 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005370 }
5371
5372 java_library {
5373 name: "libfoo",
5374 srcs: ["foo/bar/MyClass.java"],
5375 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005376 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005377 }
Paul Duffin37856732021-02-26 14:24:15 +00005378
5379 java_sdk_library_import {
5380 name: "libbar",
5381 public: {
5382 jars: ["libbar.jar"],
5383 },
5384 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005385 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005386 }
5387
5388 java_sdk_library {
5389 name: "libbar",
5390 srcs: ["foo/bar/MyClass.java"],
5391 unsafe_ignore_missing_latest_api: true,
5392 apex_available: ["myapex"],
5393 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005394 `
5395
5396 // In this test the source (java_library) libfoo is active since the
5397 // prebuilt (java_import) defaults to prefer:false. However the
5398 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5399 // find the dex boot jar in it. We either need to disable the source libfoo
5400 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005401 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005402 // dexbootjar check is skipped if AllowMissingDependencies is true
5403 preparerAllowMissingDeps := android.GroupFixturePreparers(
5404 preparer,
5405 android.PrepareForTestWithAllowMissingDependencies,
5406 )
5407 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005408 })
5409
5410 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5411 bp := `
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005412 apex {
5413 name: "myapex",
5414 key: "myapex.key",
5415 updatable: false,
5416 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5417 }
5418
5419 apex_key {
5420 name: "myapex.key",
5421 public_key: "testkey.avbpubkey",
5422 private_key: "testkey.pem",
5423 }
5424
5425 bootclasspath_fragment {
5426 name: "my-bootclasspath-fragment",
5427 contents: ["libfoo", "libbar"],
5428 apex_available: ["myapex"],
5429 hidden_api: {
5430 split_packages: ["*"],
5431 },
5432 }
5433
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005434 prebuilt_apex {
5435 name: "myapex",
5436 arch: {
5437 arm64: {
5438 src: "myapex-arm64.apex",
5439 },
5440 arm: {
5441 src: "myapex-arm.apex",
5442 },
5443 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005444 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5445 }
5446
5447 prebuilt_bootclasspath_fragment {
5448 name: "my-bootclasspath-fragment",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005449 prefer: true,
Paul Duffin89f570a2021-06-16 01:42:33 +01005450 contents: ["libfoo", "libbar"],
5451 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005452 hidden_api: {
5453 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5454 metadata: "my-bootclasspath-fragment/metadata.csv",
5455 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005456 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5457 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5458 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005459 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005460 }
5461
5462 java_import {
5463 name: "libfoo",
5464 prefer: true,
5465 jars: ["libfoo.jar"],
5466 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005467 permitted_packages: ["foo"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005468 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005469 }
5470
5471 java_library {
5472 name: "libfoo",
5473 srcs: ["foo/bar/MyClass.java"],
5474 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005475 installable: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00005476 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005477 }
Paul Duffin37856732021-02-26 14:24:15 +00005478
5479 java_sdk_library_import {
5480 name: "libbar",
5481 prefer: true,
5482 public: {
5483 jars: ["libbar.jar"],
5484 },
5485 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005486 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005487 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005488 }
5489
5490 java_sdk_library {
5491 name: "libbar",
5492 srcs: ["foo/bar/MyClass.java"],
5493 unsafe_ignore_missing_latest_api: true,
5494 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005495 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005496 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005497 `
5498
Paul Duffin89f570a2021-06-16 01:42:33 +01005499 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005500 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5501 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005502
Paul Duffin537ea3d2021-05-14 10:38:00 +01005503 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005504 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005505 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005506 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005507 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005508 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 +01005509 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005510 })
5511
5512 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5513 bp := `
5514 apex {
5515 name: "myapex",
5516 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005517 updatable: false,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005518 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005519 }
5520
5521 apex_key {
5522 name: "myapex.key",
5523 public_key: "testkey.avbpubkey",
5524 private_key: "testkey.pem",
5525 }
5526
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005527 bootclasspath_fragment {
5528 name: "my-bootclasspath-fragment",
5529 contents: ["libfoo", "libbar"],
5530 apex_available: ["myapex"],
5531 hidden_api: {
5532 split_packages: ["*"],
5533 },
5534 }
5535
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005536 prebuilt_apex {
5537 name: "myapex",
5538 arch: {
5539 arm64: {
5540 src: "myapex-arm64.apex",
5541 },
5542 arm: {
5543 src: "myapex-arm.apex",
5544 },
5545 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005546 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5547 }
5548
5549 prebuilt_bootclasspath_fragment {
5550 name: "my-bootclasspath-fragment",
5551 contents: ["libfoo", "libbar"],
5552 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005553 hidden_api: {
5554 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5555 metadata: "my-bootclasspath-fragment/metadata.csv",
5556 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005557 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5558 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5559 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005560 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005561 }
5562
5563 java_import {
5564 name: "libfoo",
5565 jars: ["libfoo.jar"],
5566 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005567 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005568 }
5569
5570 java_library {
5571 name: "libfoo",
5572 srcs: ["foo/bar/MyClass.java"],
5573 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005574 permitted_packages: ["foo"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005575 installable: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00005576 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005577 }
Paul Duffin37856732021-02-26 14:24:15 +00005578
5579 java_sdk_library_import {
5580 name: "libbar",
5581 public: {
5582 jars: ["libbar.jar"],
5583 },
5584 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005585 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005586 }
5587
5588 java_sdk_library {
5589 name: "libbar",
5590 srcs: ["foo/bar/MyClass.java"],
5591 unsafe_ignore_missing_latest_api: true,
5592 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005593 permitted_packages: ["bar"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005594 compile_dex: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00005595 sdk_version: "core_current",
Paul Duffin37856732021-02-26 14:24:15 +00005596 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005597 `
5598
Paul Duffin89f570a2021-06-16 01:42:33 +01005599 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Jiakai Zhangc6879f32023-11-06 16:31:19 +00005600 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/hiddenapi-modular/encoded/libfoo.jar")
5601 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/hiddenapi-modular/encoded/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005602
Paul Duffin537ea3d2021-05-14 10:38:00 +01005603 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005604 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005605 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
5606 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005607 out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/modular-hiddenapi/index.csv
5608 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 +01005609 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005610 })
5611
5612 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5613 bp := `
5614 apex {
5615 name: "myapex",
5616 enabled: false,
5617 key: "myapex.key",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005618 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005619 }
5620
5621 apex_key {
5622 name: "myapex.key",
5623 public_key: "testkey.avbpubkey",
5624 private_key: "testkey.pem",
5625 }
5626
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005627 bootclasspath_fragment {
5628 name: "my-bootclasspath-fragment",
5629 enabled: false,
5630 contents: ["libfoo", "libbar"],
5631 apex_available: ["myapex"],
5632 hidden_api: {
5633 split_packages: ["*"],
5634 },
5635 }
5636
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005637 prebuilt_apex {
5638 name: "myapex",
5639 arch: {
5640 arm64: {
5641 src: "myapex-arm64.apex",
5642 },
5643 arm: {
5644 src: "myapex-arm.apex",
5645 },
5646 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005647 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5648 }
5649
5650 prebuilt_bootclasspath_fragment {
5651 name: "my-bootclasspath-fragment",
5652 contents: ["libfoo", "libbar"],
5653 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005654 hidden_api: {
5655 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5656 metadata: "my-bootclasspath-fragment/metadata.csv",
5657 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005658 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5659 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5660 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005661 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005662 }
5663
5664 java_import {
5665 name: "libfoo",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005666 jars: ["libfoo.jar"],
5667 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005668 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005669 }
5670
5671 java_library {
5672 name: "libfoo",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005673 enabled: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005674 srcs: ["foo/bar/MyClass.java"],
5675 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005676 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005677 }
Paul Duffin37856732021-02-26 14:24:15 +00005678
5679 java_sdk_library_import {
5680 name: "libbar",
Paul Duffin37856732021-02-26 14:24:15 +00005681 public: {
5682 jars: ["libbar.jar"],
5683 },
5684 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005685 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005686 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005687 }
5688
5689 java_sdk_library {
5690 name: "libbar",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005691 enabled: false,
Paul Duffin37856732021-02-26 14:24:15 +00005692 srcs: ["foo/bar/MyClass.java"],
5693 unsafe_ignore_missing_latest_api: true,
5694 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005695 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005696 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005697 `
Cole Fausta963b942024-04-11 17:43:00 -07005698 // This test disables libbar, which causes the ComponentDepsMutator to add
5699 // deps on libbar.stubs and other sub-modules that don't exist. We can
5700 // enable AllowMissingDependencies to work around that, but enabling that
5701 // causes extra checks for missing source files to dex_bootjars, so add those
5702 // to the mock fs as well.
5703 preparer2 := android.GroupFixturePreparers(
5704 preparer,
5705 android.PrepareForTestWithAllowMissingDependencies,
5706 android.FixtureMergeMockFs(map[string][]byte{
5707 "build/soong/scripts/check_boot_jars/package_allowed_list.txt": nil,
5708 "frameworks/base/config/boot-profile.txt": nil,
5709 }),
5710 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005711
Cole Fausta963b942024-04-11 17:43:00 -07005712 ctx := testDexpreoptWithApexes(t, bp, "", preparer2, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005713 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5714 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005715
Paul Duffin537ea3d2021-05-14 10:38:00 +01005716 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005717 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005718 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005719 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005720 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005721 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 +01005722 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005723 })
Spandan Das3a392012024-01-17 18:26:27 +00005724
Spandan Dasf2c10572024-02-27 04:49:52 +00005725 t.Run("Co-existing unflagged apexes should create a duplicate module error", func(t *testing.T) {
Spandan Das3a392012024-01-17 18:26:27 +00005726 bp := `
5727 // Source
5728 apex {
5729 name: "myapex",
5730 enabled: false,
5731 key: "myapex.key",
5732 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5733 }
5734
5735 apex_key {
5736 name: "myapex.key",
5737 public_key: "testkey.avbpubkey",
5738 private_key: "testkey.pem",
5739 }
5740
5741 // Prebuilt
5742 prebuilt_apex {
5743 name: "myapex.v1",
5744 source_apex_name: "myapex",
5745 arch: {
5746 arm64: {
5747 src: "myapex-arm64.apex",
5748 },
5749 arm: {
5750 src: "myapex-arm.apex",
5751 },
5752 },
5753 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5754 prefer: true,
5755 }
5756 prebuilt_apex {
5757 name: "myapex.v2",
5758 source_apex_name: "myapex",
5759 arch: {
5760 arm64: {
5761 src: "myapex-arm64.apex",
5762 },
5763 arm: {
5764 src: "myapex-arm.apex",
5765 },
5766 },
5767 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5768 prefer: true,
5769 }
5770
5771 prebuilt_bootclasspath_fragment {
5772 name: "my-bootclasspath-fragment",
5773 contents: ["libfoo", "libbar"],
5774 apex_available: ["myapex"],
5775 hidden_api: {
5776 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5777 metadata: "my-bootclasspath-fragment/metadata.csv",
5778 index: "my-bootclasspath-fragment/index.csv",
5779 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5780 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5781 },
5782 prefer: true,
5783 }
5784
5785 java_import {
5786 name: "libfoo",
5787 jars: ["libfoo.jar"],
5788 apex_available: ["myapex"],
5789 prefer: true,
5790 }
5791 java_import {
5792 name: "libbar",
5793 jars: ["libbar.jar"],
5794 apex_available: ["myapex"],
5795 prefer: true,
5796 }
5797 `
5798
Spandan Dasf2c10572024-02-27 04:49:52 +00005799 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 +00005800 })
5801
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005802}
5803
Roland Levillain630846d2019-06-26 12:48:34 +01005804func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005805 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005806 apex_test {
5807 name: "myapex",
5808 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005809 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005810 tests: [
5811 "mytest",
5812 ],
5813 }
5814
5815 apex_key {
5816 name: "myapex.key",
5817 public_key: "testkey.avbpubkey",
5818 private_key: "testkey.pem",
5819 }
5820
Liz Kammer1c14a212020-05-12 15:26:55 -07005821 filegroup {
5822 name: "fg",
5823 srcs: [
5824 "baz",
5825 "bar/baz"
5826 ],
5827 }
5828
Roland Levillain630846d2019-06-26 12:48:34 +01005829 cc_test {
5830 name: "mytest",
5831 gtest: false,
5832 srcs: ["mytest.cpp"],
5833 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005834 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005835 system_shared_libs: [],
5836 static_executable: true,
5837 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005838 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005839 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005840
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005841 cc_library {
5842 name: "mylib",
5843 srcs: ["mylib.cpp"],
5844 system_shared_libs: [],
5845 stl: "none",
5846 }
5847
Liz Kammer5bd365f2020-05-27 15:15:11 -07005848 filegroup {
5849 name: "fg2",
5850 srcs: [
5851 "testdata/baz"
5852 ],
5853 }
Roland Levillain630846d2019-06-26 12:48:34 +01005854 `)
5855
Jooyung Hana0503a52023-08-23 13:12:50 +09005856 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005857 copyCmds := apexRule.Args["copy_commands"]
5858
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005859 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005860 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005861 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005862
Liz Kammer1c14a212020-05-12 15:26:55 -07005863 //Ensure that test data are copied into apex.
5864 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5865 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5866
Roland Levillainf89cd092019-07-29 16:22:59 +01005867 // Ensure the module is correctly translated.
Jooyung Hana0503a52023-08-23 13:12:50 +09005868 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005869 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005870 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005871 prefix := "TARGET_"
5872 var builder strings.Builder
5873 data.Custom(&builder, name, prefix, "", data)
5874 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005875 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005876 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005877}
5878
Jooyung Hand48f3c32019-08-23 11:18:57 +09005879func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5880 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5881 apex {
5882 name: "myapex",
5883 key: "myapex.key",
5884 native_shared_libs: ["libfoo"],
5885 }
5886
5887 apex_key {
5888 name: "myapex.key",
5889 public_key: "testkey.avbpubkey",
5890 private_key: "testkey.pem",
5891 }
5892
5893 cc_library {
5894 name: "libfoo",
5895 stl: "none",
5896 system_shared_libs: [],
5897 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005898 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005899 }
5900 `)
5901 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5902 apex {
5903 name: "myapex",
5904 key: "myapex.key",
5905 java_libs: ["myjar"],
5906 }
5907
5908 apex_key {
5909 name: "myapex.key",
5910 public_key: "testkey.avbpubkey",
5911 private_key: "testkey.pem",
5912 }
5913
5914 java_library {
5915 name: "myjar",
5916 srcs: ["foo/bar/MyClass.java"],
5917 sdk_version: "none",
5918 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005919 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005920 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005921 }
5922 `)
5923}
5924
Bill Peckhama41a6962021-01-11 10:58:54 -08005925func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005926 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005927 apex {
5928 name: "myapex",
5929 key: "myapex.key",
5930 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005931 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005932 }
5933
5934 apex_key {
5935 name: "myapex.key",
5936 public_key: "testkey.avbpubkey",
5937 private_key: "testkey.pem",
5938 }
5939
5940 java_import {
5941 name: "myjavaimport",
5942 apex_available: ["myapex"],
5943 jars: ["my.jar"],
5944 compile_dex: true,
5945 }
5946 `)
5947
Jooyung Hana0503a52023-08-23 13:12:50 +09005948 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Bill Peckhama41a6962021-01-11 10:58:54 -08005949 apexRule := module.Rule("apexRule")
5950 copyCmds := apexRule.Args["copy_commands"]
5951 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5952}
5953
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005954func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005955 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005956 apex {
5957 name: "myapex",
5958 key: "myapex.key",
5959 apps: [
5960 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005961 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005962 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005963 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005964 }
5965
5966 apex_key {
5967 name: "myapex.key",
5968 public_key: "testkey.avbpubkey",
5969 private_key: "testkey.pem",
5970 }
5971
5972 android_app {
5973 name: "AppFoo",
5974 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005975 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005976 system_modules: "none",
Jiyong Park970c5242024-05-17 22:58:54 +00005977 use_embedded_native_libs: true,
Jiyong Park8be103b2019-11-08 15:53:48 +09005978 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005979 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005980 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005981 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005982
5983 android_app {
5984 name: "AppFooPriv",
5985 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005986 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005987 system_modules: "none",
5988 privileged: true,
Sam Delmerico15809f82023-05-15 17:21:47 -04005989 privapp_allowlist: "privapp_allowlist_com.android.AppFooPriv.xml",
Colin Cross094cde42020-02-15 10:38:00 -08005990 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005991 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005992 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005993
5994 cc_library_shared {
5995 name: "libjni",
5996 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005997 shared_libs: ["libfoo"],
5998 stl: "none",
5999 system_shared_libs: [],
6000 apex_available: [ "myapex" ],
6001 sdk_version: "current",
6002 }
6003
6004 cc_library_shared {
6005 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09006006 stl: "none",
6007 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09006008 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08006009 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09006010 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006011 `)
6012
Jooyung Hana0503a52023-08-23 13:12:50 +09006013 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006014 apexRule := module.Rule("apexRule")
6015 copyCmds := apexRule.Args["copy_commands"]
6016
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006017 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
6018 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Andrei Onea580636b2022-08-17 16:53:46 +00006019 ensureContains(t, copyCmds, "image.apex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Jiyong Park52cd06f2019-11-11 10:14:32 +09006020
Colin Crossaede88c2020-08-11 12:17:01 -07006021 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006022 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09006023 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006024 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09006025 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006026 // JNI libraries including transitive deps are
6027 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01006028 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006029 // ... embedded inside APK (jnilibs.zip)
6030 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
6031 // ... and not directly inside the APEX
6032 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
6033 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04006034
6035 apexBundle := module.Module().(*apexBundle)
6036 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
6037 var builder strings.Builder
6038 data.Custom(&builder, apexBundle.Name(), "TARGET_", "", data)
6039 androidMk := builder.String()
6040 ensureContains(t, androidMk, "LOCAL_MODULE := AppFooPriv.myapex")
6041 ensureContains(t, androidMk, "LOCAL_MODULE := AppFoo.myapex")
6042 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFooPriv.apk")
6043 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFoo.apk")
6044 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := \\S+AppFooPriv.apk")
6045 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 +01006046}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006047
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006048func TestApexWithAppImportBuildId(t *testing.T) {
6049 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
6050 for _, id := range invalidBuildIds {
6051 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
6052 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6053 variables.BuildId = proptools.StringPtr(id)
6054 })
6055 testApexError(t, message, `apex {
6056 name: "myapex",
6057 key: "myapex.key",
6058 apps: ["AppFooPrebuilt"],
6059 updatable: false,
6060 }
6061
6062 apex_key {
6063 name: "myapex.key",
6064 public_key: "testkey.avbpubkey",
6065 private_key: "testkey.pem",
6066 }
6067
6068 android_app_import {
6069 name: "AppFooPrebuilt",
6070 apk: "PrebuiltAppFoo.apk",
6071 presigned: true,
6072 apex_available: ["myapex"],
6073 }
6074 `, fixture)
6075 }
6076}
6077
Dario Frenicde2a032019-10-27 00:29:22 +01006078func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006079 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01006080 apex {
6081 name: "myapex",
6082 key: "myapex.key",
6083 apps: [
6084 "AppFooPrebuilt",
6085 "AppFooPrivPrebuilt",
6086 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006087 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01006088 }
6089
6090 apex_key {
6091 name: "myapex.key",
6092 public_key: "testkey.avbpubkey",
6093 private_key: "testkey.pem",
6094 }
6095
6096 android_app_import {
6097 name: "AppFooPrebuilt",
6098 apk: "PrebuiltAppFoo.apk",
6099 presigned: true,
6100 dex_preopt: {
6101 enabled: false,
6102 },
Jiyong Park592a6a42020-04-21 22:34:28 +09006103 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006104 }
6105
6106 android_app_import {
6107 name: "AppFooPrivPrebuilt",
6108 apk: "PrebuiltAppFooPriv.apk",
6109 privileged: true,
6110 presigned: true,
6111 dex_preopt: {
6112 enabled: false,
6113 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006114 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006115 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006116 }
6117 `)
6118
Jooyung Hana0503a52023-08-23 13:12:50 +09006119 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Frenicde2a032019-10-27 00:29:22 +01006120 apexRule := module.Rule("apexRule")
6121 copyCmds := apexRule.Args["copy_commands"]
6122
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006123 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6124 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006125}
6126
6127func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006128 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006129 apex {
6130 name: "myapex",
6131 key: "myapex.key",
6132 apps: [
6133 "AppFoo",
6134 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006135 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006136 }
6137
6138 apex_key {
6139 name: "myapex.key",
6140 public_key: "testkey.avbpubkey",
6141 private_key: "testkey.pem",
6142 }
6143
6144 android_app {
6145 name: "AppFoo",
6146 srcs: ["foo/bar/MyClass.java"],
6147 sdk_version: "none",
6148 system_modules: "none",
6149 apex_available: [ "myapex" ],
6150 }
6151
6152 android_app_import {
6153 name: "AppFoo",
6154 apk: "AppFooPrebuilt.apk",
6155 filename: "AppFooPrebuilt.apk",
6156 presigned: true,
6157 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006158 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006159 }
6160 `, withFiles(map[string][]byte{
6161 "AppFooPrebuilt.apk": nil,
6162 }))
6163
Jooyung Hana0503a52023-08-23 13:12:50 +09006164 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006165 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006166 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006167}
6168
Dario Freni6f3937c2019-12-20 22:58:03 +00006169func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006170 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006171 apex {
6172 name: "myapex",
6173 key: "myapex.key",
6174 apps: [
6175 "TesterHelpAppFoo",
6176 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006177 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006178 }
6179
6180 apex_key {
6181 name: "myapex.key",
6182 public_key: "testkey.avbpubkey",
6183 private_key: "testkey.pem",
6184 }
6185
6186 android_test_helper_app {
6187 name: "TesterHelpAppFoo",
6188 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006189 apex_available: [ "myapex" ],
Jihoon Kang85bc1932024-07-01 17:04:46 +00006190 sdk_version: "test_current",
Dario Freni6f3937c2019-12-20 22:58:03 +00006191 }
6192
6193 `)
6194
Jooyung Hana0503a52023-08-23 13:12:50 +09006195 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Freni6f3937c2019-12-20 22:58:03 +00006196 apexRule := module.Rule("apexRule")
6197 copyCmds := apexRule.Args["copy_commands"]
6198
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006199 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006200}
6201
Jooyung Han18020ea2019-11-13 10:50:48 +09006202func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6203 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006204 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006205 apex {
6206 name: "myapex",
6207 key: "myapex.key",
6208 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006209 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006210 }
6211
6212 apex_key {
6213 name: "myapex.key",
6214 public_key: "testkey.avbpubkey",
6215 private_key: "testkey.pem",
6216 }
6217
6218 apex {
6219 name: "otherapex",
6220 key: "myapex.key",
6221 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006222 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006223 }
6224
6225 cc_defaults {
6226 name: "libfoo-defaults",
6227 apex_available: ["otherapex"],
6228 }
6229
6230 cc_library {
6231 name: "libfoo",
6232 defaults: ["libfoo-defaults"],
6233 stl: "none",
6234 system_shared_libs: [],
6235 }`)
6236}
6237
Paul Duffine52e66f2020-03-30 17:54:29 +01006238func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006239 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006240 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006241 apex {
6242 name: "myapex",
6243 key: "myapex.key",
6244 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006245 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006246 }
6247
6248 apex_key {
6249 name: "myapex.key",
6250 public_key: "testkey.avbpubkey",
6251 private_key: "testkey.pem",
6252 }
6253
6254 apex {
6255 name: "otherapex",
6256 key: "otherapex.key",
6257 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006258 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006259 }
6260
6261 apex_key {
6262 name: "otherapex.key",
6263 public_key: "testkey.avbpubkey",
6264 private_key: "testkey.pem",
6265 }
6266
6267 cc_library {
6268 name: "libfoo",
6269 stl: "none",
6270 system_shared_libs: [],
6271 apex_available: ["otherapex"],
6272 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006273}
Jiyong Park127b40b2019-09-30 16:04:35 +09006274
Paul Duffine52e66f2020-03-30 17:54:29 +01006275func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006276 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006277 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006278.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006279.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006280.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006281.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006282.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006283.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006284 apex {
6285 name: "myapex",
6286 key: "myapex.key",
6287 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006288 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006289 }
6290
6291 apex_key {
6292 name: "myapex.key",
6293 public_key: "testkey.avbpubkey",
6294 private_key: "testkey.pem",
6295 }
6296
Jiyong Park127b40b2019-09-30 16:04:35 +09006297 cc_library {
6298 name: "libfoo",
6299 stl: "none",
6300 shared_libs: ["libbar"],
6301 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006302 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006303 }
6304
6305 cc_library {
6306 name: "libbar",
6307 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006308 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006309 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006310 apex_available: ["myapex"],
6311 }
6312
6313 cc_library {
6314 name: "libbaz",
6315 stl: "none",
6316 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006317 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006318}
Jiyong Park127b40b2019-09-30 16:04:35 +09006319
Liz Kammer5f108fa2023-05-11 14:33:17 -04006320func TestApexAvailable_IndirectStaticDep(t *testing.T) {
6321 testApex(t, `
6322 apex {
6323 name: "myapex",
6324 key: "myapex.key",
6325 native_shared_libs: ["libfoo"],
6326 updatable: false,
6327 }
6328
6329 apex_key {
6330 name: "myapex.key",
6331 public_key: "testkey.avbpubkey",
6332 private_key: "testkey.pem",
6333 }
6334
6335 cc_library {
6336 name: "libfoo",
6337 stl: "none",
6338 static_libs: ["libbar"],
6339 system_shared_libs: [],
6340 apex_available: ["myapex"],
6341 }
6342
6343 cc_library {
6344 name: "libbar",
6345 stl: "none",
6346 shared_libs: ["libbaz"],
6347 system_shared_libs: [],
6348 apex_available: ["myapex"],
6349 }
6350
6351 cc_library {
6352 name: "libbaz",
6353 stl: "none",
6354 system_shared_libs: [],
6355 }`)
6356
6357 testApexError(t, `requires "libbar" that doesn't list the APEX under 'apex_available'.`, `
6358 apex {
6359 name: "myapex",
6360 key: "myapex.key",
6361 native_shared_libs: ["libfoo"],
6362 updatable: false,
6363 }
6364
6365 apex_key {
6366 name: "myapex.key",
6367 public_key: "testkey.avbpubkey",
6368 private_key: "testkey.pem",
6369 }
6370
6371 cc_library {
6372 name: "libfoo",
6373 stl: "none",
6374 static_libs: ["libbar"],
6375 system_shared_libs: [],
6376 apex_available: ["myapex"],
6377 }
6378
6379 cc_library {
6380 name: "libbar",
6381 stl: "none",
6382 system_shared_libs: [],
6383 }`)
6384}
6385
Paul Duffine52e66f2020-03-30 17:54:29 +01006386func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006387 testApexError(t, "\"otherapex\" is not a valid module name", `
6388 apex {
6389 name: "myapex",
6390 key: "myapex.key",
6391 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006392 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006393 }
6394
6395 apex_key {
6396 name: "myapex.key",
6397 public_key: "testkey.avbpubkey",
6398 private_key: "testkey.pem",
6399 }
6400
6401 cc_library {
6402 name: "libfoo",
6403 stl: "none",
6404 system_shared_libs: [],
6405 apex_available: ["otherapex"],
6406 }`)
6407
Paul Duffine52e66f2020-03-30 17:54:29 +01006408 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006409 apex {
6410 name: "myapex",
6411 key: "myapex.key",
6412 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006413 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006414 }
6415
6416 apex_key {
6417 name: "myapex.key",
6418 public_key: "testkey.avbpubkey",
6419 private_key: "testkey.pem",
6420 }
6421
6422 cc_library {
6423 name: "libfoo",
6424 stl: "none",
6425 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006426 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006427 apex_available: ["myapex"],
6428 }
6429
6430 cc_library {
6431 name: "libbar",
6432 stl: "none",
6433 system_shared_libs: [],
6434 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006435 }
6436
6437 cc_library {
6438 name: "libbaz",
6439 stl: "none",
6440 system_shared_libs: [],
6441 stubs: {
6442 versions: ["10", "20", "30"],
6443 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006444 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006445}
Jiyong Park127b40b2019-09-30 16:04:35 +09006446
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006447func TestApexAvailable_ApexAvailableNameWithVersionCodeError(t *testing.T) {
6448 t.Run("negative variant_version produces error", func(t *testing.T) {
6449 testApexError(t, "expected an integer between 0-9; got -1", `
6450 apex {
6451 name: "myapex",
6452 key: "myapex.key",
6453 apex_available_name: "com.android.foo",
6454 variant_version: "-1",
6455 updatable: false,
6456 }
6457 apex_key {
6458 name: "myapex.key",
6459 public_key: "testkey.avbpubkey",
6460 private_key: "testkey.pem",
6461 }
6462 `)
6463 })
6464
6465 t.Run("variant_version greater than 9 produces error", func(t *testing.T) {
6466 testApexError(t, "expected an integer between 0-9; got 10", `
6467 apex {
6468 name: "myapex",
6469 key: "myapex.key",
6470 apex_available_name: "com.android.foo",
6471 variant_version: "10",
6472 updatable: false,
6473 }
6474 apex_key {
6475 name: "myapex.key",
6476 public_key: "testkey.avbpubkey",
6477 private_key: "testkey.pem",
6478 }
6479 `)
6480 })
6481}
6482
6483func TestApexAvailable_ApexAvailableNameWithVersionCode(t *testing.T) {
6484 context := android.GroupFixturePreparers(
6485 android.PrepareForIntegrationTestWithAndroid,
6486 PrepareForTestWithApexBuildComponents,
6487 android.FixtureMergeMockFs(android.MockFS{
6488 "system/sepolicy/apex/foo-file_contexts": nil,
6489 "system/sepolicy/apex/bar-file_contexts": nil,
6490 }),
6491 )
6492 result := context.RunTestWithBp(t, `
6493 apex {
6494 name: "foo",
6495 key: "myapex.key",
6496 apex_available_name: "com.android.foo",
6497 variant_version: "0",
6498 updatable: false,
6499 }
6500 apex {
6501 name: "bar",
6502 key: "myapex.key",
6503 apex_available_name: "com.android.foo",
6504 variant_version: "3",
6505 updatable: false,
6506 }
6507 apex_key {
6508 name: "myapex.key",
6509 public_key: "testkey.avbpubkey",
6510 private_key: "testkey.pem",
6511 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006512 override_apex {
6513 name: "myoverrideapex",
6514 base: "bar",
6515 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006516 `)
6517
Jooyung Hana0503a52023-08-23 13:12:50 +09006518 fooManifestRule := result.ModuleForTests("foo", "android_common_foo").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006519 fooExpectedDefaultVersion := android.DefaultUpdatableModuleVersion
6520 fooActualDefaultVersion := fooManifestRule.Args["default_version"]
6521 if fooActualDefaultVersion != fooExpectedDefaultVersion {
6522 t.Errorf("expected to find defaultVersion %q; got %q", fooExpectedDefaultVersion, fooActualDefaultVersion)
6523 }
6524
Jooyung Hana0503a52023-08-23 13:12:50 +09006525 barManifestRule := result.ModuleForTests("bar", "android_common_bar").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006526 defaultVersionInt, _ := strconv.Atoi(android.DefaultUpdatableModuleVersion)
6527 barExpectedDefaultVersion := fmt.Sprint(defaultVersionInt + 3)
6528 barActualDefaultVersion := barManifestRule.Args["default_version"]
6529 if barActualDefaultVersion != barExpectedDefaultVersion {
6530 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6531 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006532
Spandan Das50801e22024-05-13 18:29:45 +00006533 overrideBarManifestRule := result.ModuleForTests("bar", "android_common_myoverrideapex_myoverrideapex").Rule("apexManifestRule")
Sam Delmerico419f9a32023-07-21 12:00:13 -04006534 overrideBarActualDefaultVersion := overrideBarManifestRule.Args["default_version"]
6535 if overrideBarActualDefaultVersion != barExpectedDefaultVersion {
6536 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6537 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006538}
6539
Sam Delmericoca816532023-06-02 14:09:50 -04006540func TestApexAvailable_ApexAvailableName(t *testing.T) {
6541 t.Run("using name of apex that sets apex_available_name is not allowed", func(t *testing.T) {
6542 testApexError(t, "Consider adding \"myapex\" to 'apex_available' property of \"AppFoo\"", `
6543 apex {
6544 name: "myapex_sminus",
6545 key: "myapex.key",
6546 apps: ["AppFoo"],
6547 apex_available_name: "myapex",
6548 updatable: false,
6549 }
6550 apex {
6551 name: "myapex",
6552 key: "myapex.key",
6553 apps: ["AppFoo"],
6554 updatable: false,
6555 }
6556 apex_key {
6557 name: "myapex.key",
6558 public_key: "testkey.avbpubkey",
6559 private_key: "testkey.pem",
6560 }
6561 android_app {
6562 name: "AppFoo",
6563 srcs: ["foo/bar/MyClass.java"],
6564 sdk_version: "none",
6565 system_modules: "none",
6566 apex_available: [ "myapex_sminus" ],
6567 }`,
6568 android.FixtureMergeMockFs(android.MockFS{
6569 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6570 }),
6571 )
6572 })
6573
6574 t.Run("apex_available_name allows module to be used in two different apexes", func(t *testing.T) {
6575 testApex(t, `
6576 apex {
6577 name: "myapex_sminus",
6578 key: "myapex.key",
6579 apps: ["AppFoo"],
6580 apex_available_name: "myapex",
6581 updatable: false,
6582 }
6583 apex {
6584 name: "myapex",
6585 key: "myapex.key",
6586 apps: ["AppFoo"],
6587 updatable: false,
6588 }
6589 apex_key {
6590 name: "myapex.key",
6591 public_key: "testkey.avbpubkey",
6592 private_key: "testkey.pem",
6593 }
6594 android_app {
6595 name: "AppFoo",
6596 srcs: ["foo/bar/MyClass.java"],
6597 sdk_version: "none",
6598 system_modules: "none",
6599 apex_available: [ "myapex" ],
6600 }`,
6601 android.FixtureMergeMockFs(android.MockFS{
6602 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6603 }),
6604 )
6605 })
6606
6607 t.Run("override_apexes work with apex_available_name", func(t *testing.T) {
6608 testApex(t, `
6609 override_apex {
6610 name: "myoverrideapex_sminus",
6611 base: "myapex_sminus",
6612 key: "myapex.key",
6613 apps: ["AppFooOverride"],
6614 }
6615 override_apex {
6616 name: "myoverrideapex",
6617 base: "myapex",
6618 key: "myapex.key",
6619 apps: ["AppFooOverride"],
6620 }
6621 apex {
6622 name: "myapex_sminus",
6623 key: "myapex.key",
6624 apps: ["AppFoo"],
6625 apex_available_name: "myapex",
6626 updatable: false,
6627 }
6628 apex {
6629 name: "myapex",
6630 key: "myapex.key",
6631 apps: ["AppFoo"],
6632 updatable: false,
6633 }
6634 apex_key {
6635 name: "myapex.key",
6636 public_key: "testkey.avbpubkey",
6637 private_key: "testkey.pem",
6638 }
6639 android_app {
6640 name: "AppFooOverride",
6641 srcs: ["foo/bar/MyClass.java"],
6642 sdk_version: "none",
6643 system_modules: "none",
6644 apex_available: [ "myapex" ],
6645 }
6646 android_app {
6647 name: "AppFoo",
6648 srcs: ["foo/bar/MyClass.java"],
6649 sdk_version: "none",
6650 system_modules: "none",
6651 apex_available: [ "myapex" ],
6652 }`,
6653 android.FixtureMergeMockFs(android.MockFS{
6654 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6655 }),
6656 )
6657 })
6658}
6659
6660func TestApexAvailable_ApexAvailableNameWithOverrides(t *testing.T) {
6661 context := android.GroupFixturePreparers(
6662 android.PrepareForIntegrationTestWithAndroid,
6663 PrepareForTestWithApexBuildComponents,
6664 java.PrepareForTestWithDexpreopt,
6665 android.FixtureMergeMockFs(android.MockFS{
6666 "system/sepolicy/apex/myapex-file_contexts": nil,
6667 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6668 }),
6669 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6670 variables.BuildId = proptools.StringPtr("buildid")
6671 }),
6672 )
6673 context.RunTestWithBp(t, `
6674 override_apex {
6675 name: "myoverrideapex_sminus",
6676 base: "myapex_sminus",
6677 }
6678 override_apex {
6679 name: "myoverrideapex",
6680 base: "myapex",
6681 }
6682 apex {
6683 name: "myapex",
6684 key: "myapex.key",
6685 apps: ["AppFoo"],
6686 updatable: false,
6687 }
6688 apex {
6689 name: "myapex_sminus",
6690 apex_available_name: "myapex",
6691 key: "myapex.key",
6692 apps: ["AppFoo_sminus"],
6693 updatable: false,
6694 }
6695 apex_key {
6696 name: "myapex.key",
6697 public_key: "testkey.avbpubkey",
6698 private_key: "testkey.pem",
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_app {
6708 name: "AppFoo_sminus",
6709 srcs: ["foo/bar/MyClass.java"],
6710 sdk_version: "none",
6711 min_sdk_version: "29",
6712 system_modules: "none",
6713 apex_available: [ "myapex" ],
6714 }`)
6715}
6716
Jiyong Park89e850a2020-04-07 16:37:39 +09006717func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006718 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006719 apex {
6720 name: "myapex",
6721 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006722 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006723 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006724 }
6725
6726 apex_key {
6727 name: "myapex.key",
6728 public_key: "testkey.avbpubkey",
6729 private_key: "testkey.pem",
6730 }
6731
6732 cc_library {
6733 name: "libfoo",
6734 stl: "none",
6735 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006736 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006737 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006738 }
6739
6740 cc_library {
6741 name: "libfoo2",
6742 stl: "none",
6743 system_shared_libs: [],
6744 shared_libs: ["libbaz"],
6745 apex_available: ["//apex_available:platform"],
6746 }
6747
6748 cc_library {
6749 name: "libbar",
6750 stl: "none",
6751 system_shared_libs: [],
6752 apex_available: ["myapex"],
6753 }
6754
6755 cc_library {
6756 name: "libbaz",
6757 stl: "none",
6758 system_shared_libs: [],
6759 apex_available: ["myapex"],
6760 stubs: {
6761 versions: ["1"],
6762 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006763 }`)
6764
Jiyong Park89e850a2020-04-07 16:37:39 +09006765 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6766 // because it depends on libbar which isn't available to platform
6767 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6768 if libfoo.NotAvailableForPlatform() != true {
6769 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6770 }
6771
6772 // libfoo2 however can be available to platform because it depends on libbaz which provides
6773 // stubs
6774 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6775 if libfoo2.NotAvailableForPlatform() == true {
6776 t.Errorf("%q should be available to platform", libfoo2.String())
6777 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006778}
Jiyong Parka90ca002019-10-07 15:47:24 +09006779
Paul Duffine52e66f2020-03-30 17:54:29 +01006780func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006781 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006782 apex {
6783 name: "myapex",
6784 key: "myapex.key",
6785 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006786 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006787 }
6788
6789 apex_key {
6790 name: "myapex.key",
6791 public_key: "testkey.avbpubkey",
6792 private_key: "testkey.pem",
6793 }
6794
6795 cc_library {
6796 name: "libfoo",
6797 stl: "none",
6798 system_shared_libs: [],
6799 apex_available: ["myapex"],
6800 static: {
6801 apex_available: ["//apex_available:platform"],
6802 },
6803 }`)
6804
Jiyong Park89e850a2020-04-07 16:37:39 +09006805 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6806 if libfooShared.NotAvailableForPlatform() != true {
6807 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6808 }
6809 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6810 if libfooStatic.NotAvailableForPlatform() != false {
6811 t.Errorf("%q should be available to platform", libfooStatic.String())
6812 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006813}
6814
Jooyung Han9a419e22024-08-16 17:14:21 +09006815func TestApexAvailable_PrefixMatch(t *testing.T) {
6816
6817 for _, tc := range []struct {
6818 name string
6819 apexAvailable string
6820 expectedError string
6821 }{
6822 {
6823 name: "prefix matches correctly",
6824 apexAvailable: "com.foo.*",
6825 },
6826 {
6827 name: "prefix doesn't match",
6828 apexAvailable: "com.bar.*",
6829 expectedError: `Consider .* "com.foo\.\*"`,
6830 },
6831 {
6832 name: "short prefix",
6833 apexAvailable: "com.*",
6834 expectedError: "requires two or more components",
6835 },
6836 {
6837 name: "wildcard not in the end",
6838 apexAvailable: "com.*.foo",
6839 expectedError: "should end with .*",
6840 },
6841 {
6842 name: "wildcard in the middle",
6843 apexAvailable: "com.foo*.*",
6844 expectedError: "not allowed in the middle",
6845 },
6846 {
6847 name: "hint with prefix pattern",
6848 apexAvailable: "//apex_available:platform",
6849 expectedError: "Consider adding \"com.foo.bar\" or \"com.foo.*\"",
6850 },
6851 } {
6852 t.Run(tc.name, func(t *testing.T) {
6853 errorHandler := android.FixtureExpectsNoErrors
6854 if tc.expectedError != "" {
6855 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(tc.expectedError)
6856 }
6857 context := android.GroupFixturePreparers(
6858 prepareForApexTest,
6859 android.FixtureMergeMockFs(android.MockFS{
6860 "system/sepolicy/apex/com.foo.bar-file_contexts": nil,
6861 }),
6862 ).ExtendWithErrorHandler(errorHandler)
6863
6864 context.RunTestWithBp(t, `
6865 apex {
6866 name: "com.foo.bar",
6867 key: "myapex.key",
6868 native_shared_libs: ["libfoo"],
6869 updatable: false,
6870 }
6871
6872 apex_key {
6873 name: "myapex.key",
6874 public_key: "testkey.avbpubkey",
6875 private_key: "testkey.pem",
6876 }
6877
6878 cc_library {
6879 name: "libfoo",
6880 stl: "none",
6881 system_shared_libs: [],
6882 apex_available: ["`+tc.apexAvailable+`"],
6883 }`)
6884 })
6885 }
6886 testApexError(t, `Consider adding "com.foo" to`, `
6887 apex {
6888 name: "com.foo", // too short for a partner apex
6889 key: "myapex.key",
6890 native_shared_libs: ["libfoo"],
6891 updatable: false,
6892 }
6893
6894 apex_key {
6895 name: "myapex.key",
6896 public_key: "testkey.avbpubkey",
6897 private_key: "testkey.pem",
6898 }
6899
6900 cc_library {
6901 name: "libfoo",
6902 stl: "none",
6903 system_shared_libs: [],
6904 }
6905 `)
6906}
6907
Jiyong Park5d790c32019-11-15 18:40:32 +09006908func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006909 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006910 apex {
6911 name: "myapex",
6912 key: "myapex.key",
6913 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006914 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006915 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006916 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006917 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006918 }
6919
6920 override_apex {
6921 name: "override_myapex",
6922 base: "myapex",
6923 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006924 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006925 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006926 overrides: ["unknownapex"],
Jesse Melhuishec60e252024-03-29 19:08:20 +00006927 compile_multilib: "first",
6928 multilib: {
6929 lib32: {
6930 native_shared_libs: ["mylib32"],
6931 },
6932 lib64: {
6933 native_shared_libs: ["mylib64"],
6934 },
6935 },
Baligh Uddin004d7172020-02-19 21:29:28 -08006936 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006937 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006938 key: "mynewapex.key",
6939 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006940 }
6941
6942 apex_key {
6943 name: "myapex.key",
6944 public_key: "testkey.avbpubkey",
6945 private_key: "testkey.pem",
6946 }
6947
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006948 apex_key {
6949 name: "mynewapex.key",
6950 public_key: "testkey2.avbpubkey",
6951 private_key: "testkey2.pem",
6952 }
6953
6954 android_app_certificate {
6955 name: "myapex.certificate",
6956 certificate: "testkey",
6957 }
6958
Jiyong Park5d790c32019-11-15 18:40:32 +09006959 android_app {
6960 name: "app",
6961 srcs: ["foo/bar/MyClass.java"],
6962 package_name: "foo",
6963 sdk_version: "none",
6964 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006965 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006966 }
6967
6968 override_android_app {
6969 name: "override_app",
6970 base: "app",
6971 package_name: "bar",
6972 }
markchien7c803b82021-08-26 22:10:06 +08006973
6974 bpf {
6975 name: "bpf",
6976 srcs: ["bpf.c"],
6977 }
6978
6979 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006980 name: "overrideBpf",
6981 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006982 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006983
6984 prebuilt_etc {
6985 name: "myetc",
6986 src: "myprebuilt",
6987 }
6988
6989 prebuilt_etc {
6990 name: "override_myetc",
6991 src: "override_myprebuilt",
6992 }
Jesse Melhuishec60e252024-03-29 19:08:20 +00006993
6994 cc_library {
6995 name: "mylib32",
6996 apex_available: [ "myapex" ],
6997 }
6998
6999 cc_library {
7000 name: "mylib64",
7001 apex_available: [ "myapex" ],
7002 }
Jiyong Park20bacab2020-03-03 11:45:41 +09007003 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09007004
Jooyung Hana0503a52023-08-23 13:12:50 +09007005 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(android.OverridableModule)
Spandan Das50801e22024-05-13 18:29:45 +00007006 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex").Module().(android.OverridableModule)
Jiyong Park317645e2019-12-05 13:20:58 +09007007 if originalVariant.GetOverriddenBy() != "" {
7008 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
7009 }
7010 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
7011 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
7012 }
7013
Spandan Das50801e22024-05-13 18:29:45 +00007014 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex")
Jiyong Park5d790c32019-11-15 18:40:32 +09007015 apexRule := module.Rule("apexRule")
7016 copyCmds := apexRule.Args["copy_commands"]
7017
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007018 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
7019 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007020
markchien7c803b82021-08-26 22:10:06 +08007021 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08007022 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08007023
Daniel Norman5a3ce132021-08-26 15:44:43 -07007024 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
7025 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
7026
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007027 apexBundle := module.Module().(*apexBundle)
7028 name := apexBundle.Name()
7029 if name != "override_myapex" {
7030 t.Errorf("name should be \"override_myapex\", but was %q", name)
7031 }
7032
Baligh Uddin004d7172020-02-19 21:29:28 -08007033 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
7034 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
7035 }
7036
Jiyong Park20bacab2020-03-03 11:45:41 +09007037 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07007038 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07007039 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
7040
7041 signApkRule := module.Rule("signapk")
7042 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09007043
Colin Crossaa255532020-07-03 13:18:24 -07007044 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007045 var builder strings.Builder
7046 data.Custom(&builder, name, "TARGET_", "", data)
7047 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007048 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
7049 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007050 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08007051 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007052 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08007053 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09007054 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007055 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09007056}
7057
Albert Martineefabcf2022-03-21 20:11:16 +00007058func TestMinSdkVersionOverride(t *testing.T) {
7059 // Override from 29 to 31
7060 minSdkOverride31 := "31"
7061 ctx := testApex(t, `
7062 apex {
7063 name: "myapex",
7064 key: "myapex.key",
7065 native_shared_libs: ["mylib"],
7066 updatable: true,
7067 min_sdk_version: "29"
7068 }
7069
7070 override_apex {
7071 name: "override_myapex",
7072 base: "myapex",
7073 logging_parent: "com.foo.bar",
7074 package_name: "test.overridden.package"
7075 }
7076
7077 apex_key {
7078 name: "myapex.key",
7079 public_key: "testkey.avbpubkey",
7080 private_key: "testkey.pem",
7081 }
7082
7083 cc_library {
7084 name: "mylib",
7085 srcs: ["mylib.cpp"],
7086 runtime_libs: ["libbar"],
7087 system_shared_libs: [],
7088 stl: "none",
7089 apex_available: [ "myapex" ],
7090 min_sdk_version: "apex_inherit"
7091 }
7092
7093 cc_library {
7094 name: "libbar",
7095 srcs: ["mylib.cpp"],
7096 system_shared_libs: [],
7097 stl: "none",
7098 apex_available: [ "myapex" ],
7099 min_sdk_version: "apex_inherit"
7100 }
7101
7102 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
7103
Jooyung Hana0503a52023-08-23 13:12:50 +09007104 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007105 copyCmds := apexRule.Args["copy_commands"]
7106
7107 // Ensure that direct non-stubs dep is always included
7108 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7109
7110 // Ensure that runtime_libs dep in included
7111 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7112
7113 // Ensure libraries target overridden min_sdk_version value
7114 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7115}
7116
7117func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
7118 // Attempt to override from 31 to 29, should be a NOOP
7119 minSdkOverride29 := "29"
7120 ctx := testApex(t, `
7121 apex {
7122 name: "myapex",
7123 key: "myapex.key",
7124 native_shared_libs: ["mylib"],
7125 updatable: true,
7126 min_sdk_version: "31"
7127 }
7128
7129 override_apex {
7130 name: "override_myapex",
7131 base: "myapex",
7132 logging_parent: "com.foo.bar",
7133 package_name: "test.overridden.package"
7134 }
7135
7136 apex_key {
7137 name: "myapex.key",
7138 public_key: "testkey.avbpubkey",
7139 private_key: "testkey.pem",
7140 }
7141
7142 cc_library {
7143 name: "mylib",
7144 srcs: ["mylib.cpp"],
7145 runtime_libs: ["libbar"],
7146 system_shared_libs: [],
7147 stl: "none",
7148 apex_available: [ "myapex" ],
7149 min_sdk_version: "apex_inherit"
7150 }
7151
7152 cc_library {
7153 name: "libbar",
7154 srcs: ["mylib.cpp"],
7155 system_shared_libs: [],
7156 stl: "none",
7157 apex_available: [ "myapex" ],
7158 min_sdk_version: "apex_inherit"
7159 }
7160
7161 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
7162
Jooyung Hana0503a52023-08-23 13:12:50 +09007163 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007164 copyCmds := apexRule.Args["copy_commands"]
7165
7166 // Ensure that direct non-stubs dep is always included
7167 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7168
7169 // Ensure that runtime_libs dep in included
7170 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7171
7172 // Ensure libraries target the original min_sdk_version value rather than the overridden
7173 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7174}
7175
Jooyung Han214bf372019-11-12 13:03:50 +09007176func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007177 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09007178 apex {
7179 name: "myapex",
7180 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007181 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09007182 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09007183 }
7184
7185 apex_key {
7186 name: "myapex.key",
7187 public_key: "testkey.avbpubkey",
7188 private_key: "testkey.pem",
7189 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007190
7191 cc_library {
7192 name: "mylib",
7193 srcs: ["mylib.cpp"],
7194 stl: "libc++",
7195 system_shared_libs: [],
7196 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09007197 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007198 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007199 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09007200
Jooyung Hana0503a52023-08-23 13:12:50 +09007201 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +09007202 args := module.Rule("apexRule").Args
7203 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00007204 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007205
7206 // The copies of the libraries in the apex should have one more dependency than
7207 // the ones outside the apex, namely the unwinder. Ideally we should check
7208 // the dependency names directly here but for some reason the names are blank in
7209 // this test.
7210 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07007211 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007212 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
7213 if len(apexImplicits) != len(nonApexImplicits)+1 {
7214 t.Errorf("%q missing unwinder dep", lib)
7215 }
7216 }
Jooyung Han214bf372019-11-12 13:03:50 +09007217}
7218
Paul Duffine05480a2021-03-08 15:07:14 +00007219var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01007220 "api/current.txt": nil,
7221 "api/removed.txt": nil,
7222 "api/system-current.txt": nil,
7223 "api/system-removed.txt": nil,
7224 "api/test-current.txt": nil,
7225 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01007226
Anton Hanssondff2c782020-12-21 17:10:01 +00007227 "100/public/api/foo.txt": nil,
7228 "100/public/api/foo-removed.txt": nil,
7229 "100/system/api/foo.txt": nil,
7230 "100/system/api/foo-removed.txt": nil,
7231
Paul Duffineedc5d52020-06-12 17:46:39 +01007232 // For java_sdk_library_import
7233 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01007234}
7235
Jooyung Han58f26ab2019-12-18 15:34:32 +09007236func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007237 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09007238 apex {
7239 name: "myapex",
7240 key: "myapex.key",
7241 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007242 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09007243 }
7244
7245 apex_key {
7246 name: "myapex.key",
7247 public_key: "testkey.avbpubkey",
7248 private_key: "testkey.pem",
7249 }
7250
7251 java_sdk_library {
7252 name: "foo",
7253 srcs: ["a.java"],
7254 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007255 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09007256 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007257
7258 prebuilt_apis {
7259 name: "sdk",
7260 api_dirs: ["100"],
7261 }
Paul Duffin9b879592020-05-26 13:21:35 +01007262 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09007263
7264 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007265 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09007266 "javalib/foo.jar",
7267 "etc/permissions/foo.xml",
7268 })
7269 // Permission XML should point to the activated path of impl jar of java_sdk_library
Paul Duffin1816cde2024-04-10 10:58:21 +01007270 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Output("foo.xml")
7271 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7272 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
Jooyung Han58f26ab2019-12-18 15:34:32 +09007273}
7274
Spandan Das3ee19692024-06-19 04:47:40 +00007275func TestJavaSDKLibraryOverrideApexes(t *testing.T) {
7276 ctx := testApex(t, `
7277 override_apex {
7278 name: "mycompanyapex",
7279 base: "myapex",
7280 }
7281 apex {
7282 name: "myapex",
7283 key: "myapex.key",
7284 java_libs: ["foo"],
7285 updatable: false,
7286 }
7287
7288 apex_key {
7289 name: "myapex.key",
7290 public_key: "testkey.avbpubkey",
7291 private_key: "testkey.pem",
7292 }
7293
7294 java_sdk_library {
7295 name: "foo",
7296 srcs: ["a.java"],
7297 api_packages: ["foo"],
7298 apex_available: [ "myapex" ],
7299 }
7300
7301 prebuilt_apis {
7302 name: "sdk",
7303 api_dirs: ["100"],
7304 }
7305 `, withFiles(filesForSdkLibrary))
7306
7307 // Permission XML should point to the activated path of impl jar of java_sdk_library.
7308 // Since override variants (com.mycompany.android.foo) are installed in the same package as the overridden variant
7309 // (com.android.foo), the filepath should not contain override apex name.
7310 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_mycompanyapex").Output("foo.xml")
7311 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7312 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
7313}
7314
Paul Duffin9b879592020-05-26 13:21:35 +01007315func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007316 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007317 apex {
7318 name: "myapex",
7319 key: "myapex.key",
7320 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007321 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007322 }
7323
7324 apex_key {
7325 name: "myapex.key",
7326 public_key: "testkey.avbpubkey",
7327 private_key: "testkey.pem",
7328 }
7329
7330 java_sdk_library {
7331 name: "foo",
7332 srcs: ["a.java"],
7333 api_packages: ["foo"],
7334 apex_available: ["myapex"],
7335 sdk_version: "none",
7336 system_modules: "none",
7337 }
7338
7339 java_library {
7340 name: "bar",
7341 srcs: ["a.java"],
7342 libs: ["foo"],
7343 apex_available: ["myapex"],
7344 sdk_version: "none",
7345 system_modules: "none",
7346 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007347
7348 prebuilt_apis {
7349 name: "sdk",
7350 api_dirs: ["100"],
7351 }
Paul Duffin9b879592020-05-26 13:21:35 +01007352 `, withFiles(filesForSdkLibrary))
7353
7354 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007355 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007356 "javalib/bar.jar",
7357 "javalib/foo.jar",
7358 "etc/permissions/foo.xml",
7359 })
7360
7361 // The bar library should depend on the implementation jar.
7362 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kanga3a05462024-04-05 00:36:44 +00007363 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007364 t.Errorf("expected %q, found %#q", expected, actual)
7365 }
7366}
7367
7368func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007369 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007370 apex {
7371 name: "myapex",
7372 key: "myapex.key",
7373 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007374 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007375 }
7376
7377 apex_key {
7378 name: "myapex.key",
7379 public_key: "testkey.avbpubkey",
7380 private_key: "testkey.pem",
7381 }
7382
7383 java_sdk_library {
7384 name: "foo",
7385 srcs: ["a.java"],
7386 api_packages: ["foo"],
7387 apex_available: ["myapex"],
7388 sdk_version: "none",
7389 system_modules: "none",
7390 }
7391
7392 java_library {
7393 name: "bar",
7394 srcs: ["a.java"],
7395 libs: ["foo"],
7396 sdk_version: "none",
7397 system_modules: "none",
7398 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007399
7400 prebuilt_apis {
7401 name: "sdk",
7402 api_dirs: ["100"],
7403 }
Paul Duffin9b879592020-05-26 13:21:35 +01007404 `, withFiles(filesForSdkLibrary))
7405
7406 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007407 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007408 "javalib/foo.jar",
7409 "etc/permissions/foo.xml",
7410 })
7411
7412 // The bar library should depend on the stubs jar.
7413 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007414 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007415 t.Errorf("expected %q, found %#q", expected, actual)
7416 }
7417}
7418
Paul Duffineedc5d52020-06-12 17:46:39 +01007419func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007420 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00007421 prebuilt_apis {
7422 name: "sdk",
7423 api_dirs: ["100"],
7424 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01007425 withFiles(map[string][]byte{
7426 "apex/a.java": nil,
7427 "apex/apex_manifest.json": nil,
7428 "apex/Android.bp": []byte(`
7429 package {
7430 default_visibility: ["//visibility:private"],
7431 }
7432
7433 apex {
7434 name: "myapex",
7435 key: "myapex.key",
7436 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007437 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007438 }
7439
7440 apex_key {
7441 name: "myapex.key",
7442 public_key: "testkey.avbpubkey",
7443 private_key: "testkey.pem",
7444 }
7445
7446 java_library {
7447 name: "bar",
7448 srcs: ["a.java"],
7449 libs: ["foo"],
7450 apex_available: ["myapex"],
7451 sdk_version: "none",
7452 system_modules: "none",
7453 }
7454`),
7455 "source/a.java": nil,
7456 "source/api/current.txt": nil,
7457 "source/api/removed.txt": nil,
7458 "source/Android.bp": []byte(`
7459 package {
7460 default_visibility: ["//visibility:private"],
7461 }
7462
7463 java_sdk_library {
7464 name: "foo",
7465 visibility: ["//apex"],
7466 srcs: ["a.java"],
7467 api_packages: ["foo"],
7468 apex_available: ["myapex"],
7469 sdk_version: "none",
7470 system_modules: "none",
7471 public: {
7472 enabled: true,
7473 },
7474 }
7475`),
7476 "prebuilt/a.jar": nil,
7477 "prebuilt/Android.bp": []byte(`
7478 package {
7479 default_visibility: ["//visibility:private"],
7480 }
7481
7482 java_sdk_library_import {
7483 name: "foo",
7484 visibility: ["//apex", "//source"],
7485 apex_available: ["myapex"],
7486 prefer: true,
7487 public: {
7488 jars: ["a.jar"],
7489 },
7490 }
7491`),
Anton Hanssondff2c782020-12-21 17:10:01 +00007492 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01007493 )
7494
7495 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007496 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffineedc5d52020-06-12 17:46:39 +01007497 "javalib/bar.jar",
7498 "javalib/foo.jar",
7499 "etc/permissions/foo.xml",
7500 })
7501
7502 // The bar library should depend on the implementation jar.
7503 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kanga3a05462024-04-05 00:36:44 +00007504 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01007505 t.Errorf("expected %q, found %#q", expected, actual)
7506 }
7507}
7508
7509func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
7510 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
7511 apex {
7512 name: "myapex",
7513 key: "myapex.key",
7514 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007515 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007516 }
7517
7518 apex_key {
7519 name: "myapex.key",
7520 public_key: "testkey.avbpubkey",
7521 private_key: "testkey.pem",
7522 }
7523
7524 java_sdk_library_import {
7525 name: "foo",
7526 apex_available: ["myapex"],
7527 prefer: true,
7528 public: {
7529 jars: ["a.jar"],
7530 },
7531 }
7532
7533 `, withFiles(filesForSdkLibrary))
7534}
7535
atrost6e126252020-01-27 17:01:16 +00007536func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007537 result := android.GroupFixturePreparers(
7538 prepareForApexTest,
7539 java.PrepareForTestWithPlatformCompatConfig,
7540 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007541 apex {
7542 name: "myapex",
7543 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007544 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007545 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007546 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007547 }
7548
7549 apex_key {
7550 name: "myapex.key",
7551 public_key: "testkey.avbpubkey",
7552 private_key: "testkey.pem",
7553 }
7554
7555 platform_compat_config {
7556 name: "myjar-platform-compat-config",
7557 src: ":myjar",
7558 }
7559
7560 java_library {
7561 name: "myjar",
7562 srcs: ["foo/bar/MyClass.java"],
7563 sdk_version: "none",
7564 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007565 apex_available: [ "myapex" ],
7566 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007567
7568 // Make sure that a preferred prebuilt does not affect the apex contents.
7569 prebuilt_platform_compat_config {
7570 name: "myjar-platform-compat-config",
7571 metadata: "compat-config/metadata.xml",
7572 prefer: true,
7573 }
atrost6e126252020-01-27 17:01:16 +00007574 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007575 ctx := result.TestContext
Jooyung Hana0503a52023-08-23 13:12:50 +09007576 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
atrost6e126252020-01-27 17:01:16 +00007577 "etc/compatconfig/myjar-platform-compat-config.xml",
7578 "javalib/myjar.jar",
7579 })
7580}
7581
Jooyung Han862c0d62022-12-21 10:15:37 +09007582func TestNoDupeApexFiles(t *testing.T) {
7583 android.GroupFixturePreparers(
7584 android.PrepareForTestWithAndroidBuildComponents,
7585 PrepareForTestWithApexBuildComponents,
7586 prepareForTestWithMyapex,
7587 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7588 ).
7589 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7590 RunTestWithBp(t, `
7591 apex {
7592 name: "myapex",
7593 key: "myapex.key",
7594 prebuilts: ["foo", "bar"],
7595 updatable: false,
7596 }
7597
7598 apex_key {
7599 name: "myapex.key",
7600 public_key: "testkey.avbpubkey",
7601 private_key: "testkey.pem",
7602 }
7603
7604 prebuilt_etc {
7605 name: "foo",
7606 src: "myprebuilt",
7607 filename_from_src: true,
7608 }
7609
7610 prebuilt_etc {
7611 name: "bar",
7612 src: "myprebuilt",
7613 filename_from_src: true,
7614 }
7615 `)
7616}
7617
Jooyung Hana8bd72a2023-11-02 11:56:48 +09007618func TestApexUnwantedTransitiveDeps(t *testing.T) {
7619 bp := `
7620 apex {
7621 name: "myapex",
7622 key: "myapex.key",
7623 native_shared_libs: ["libfoo"],
7624 updatable: false,
7625 unwanted_transitive_deps: ["libbar"],
7626 }
7627
7628 apex_key {
7629 name: "myapex.key",
7630 public_key: "testkey.avbpubkey",
7631 private_key: "testkey.pem",
7632 }
7633
7634 cc_library {
7635 name: "libfoo",
7636 srcs: ["foo.cpp"],
7637 shared_libs: ["libbar"],
7638 apex_available: ["myapex"],
7639 }
7640
7641 cc_library {
7642 name: "libbar",
7643 srcs: ["bar.cpp"],
7644 apex_available: ["myapex"],
7645 }`
7646 ctx := testApex(t, bp)
7647 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
7648 "*/libc++.so",
7649 "*/libfoo.so",
7650 // not libbar.so
7651 })
7652}
7653
Jiyong Park479321d2019-12-16 11:47:12 +09007654func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7655 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7656 apex {
7657 name: "myapex",
7658 key: "myapex.key",
7659 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007660 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007661 }
7662
7663 apex_key {
7664 name: "myapex.key",
7665 public_key: "testkey.avbpubkey",
7666 private_key: "testkey.pem",
7667 }
7668
7669 java_library {
7670 name: "myjar",
7671 srcs: ["foo/bar/MyClass.java"],
7672 sdk_version: "none",
7673 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007674 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007675 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007676 }
7677 `)
7678}
7679
Jiyong Park7afd1072019-12-30 16:56:33 +09007680func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007681 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007682 apex {
7683 name: "myapex",
7684 key: "myapex.key",
7685 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007686 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007687 }
7688
7689 apex_key {
7690 name: "myapex.key",
7691 public_key: "testkey.avbpubkey",
7692 private_key: "testkey.pem",
7693 }
7694
7695 cc_library {
7696 name: "mylib",
7697 srcs: ["mylib.cpp"],
7698 system_shared_libs: [],
7699 stl: "none",
7700 required: ["a", "b"],
7701 host_required: ["c", "d"],
7702 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007703 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007704 }
7705 `)
7706
Jooyung Hana0503a52023-08-23 13:12:50 +09007707 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007708 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007709 name := apexBundle.BaseModuleName()
7710 prefix := "TARGET_"
7711 var builder strings.Builder
7712 data.Custom(&builder, name, prefix, "", data)
7713 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007714 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 a b\n")
Sasha Smundakdcb61292022-12-08 10:41:33 -08007715 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7716 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007717}
7718
Jiyong Park7cd10e32020-01-14 09:22:18 +09007719func TestSymlinksFromApexToSystem(t *testing.T) {
7720 bp := `
7721 apex {
7722 name: "myapex",
7723 key: "myapex.key",
7724 native_shared_libs: ["mylib"],
7725 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007726 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007727 }
7728
Jiyong Park9d677202020-02-19 16:29:35 +09007729 apex {
7730 name: "myapex.updatable",
7731 key: "myapex.key",
7732 native_shared_libs: ["mylib"],
7733 java_libs: ["myjar"],
7734 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007735 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007736 }
7737
Jiyong Park7cd10e32020-01-14 09:22:18 +09007738 apex_key {
7739 name: "myapex.key",
7740 public_key: "testkey.avbpubkey",
7741 private_key: "testkey.pem",
7742 }
7743
7744 cc_library {
7745 name: "mylib",
7746 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007747 shared_libs: [
7748 "myotherlib",
7749 "myotherlib_ext",
7750 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007751 system_shared_libs: [],
7752 stl: "none",
7753 apex_available: [
7754 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007755 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007756 "//apex_available:platform",
7757 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007758 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007759 }
7760
7761 cc_library {
7762 name: "myotherlib",
7763 srcs: ["mylib.cpp"],
7764 system_shared_libs: [],
7765 stl: "none",
7766 apex_available: [
7767 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007768 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007769 "//apex_available:platform",
7770 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007771 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007772 }
7773
Jiyong Parkce243632023-02-17 18:22:25 +09007774 cc_library {
7775 name: "myotherlib_ext",
7776 srcs: ["mylib.cpp"],
7777 system_shared_libs: [],
7778 system_ext_specific: true,
7779 stl: "none",
7780 apex_available: [
7781 "myapex",
7782 "myapex.updatable",
7783 "//apex_available:platform",
7784 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007785 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007786 }
7787
Jiyong Park7cd10e32020-01-14 09:22:18 +09007788 java_library {
7789 name: "myjar",
7790 srcs: ["foo/bar/MyClass.java"],
7791 sdk_version: "none",
7792 system_modules: "none",
Jihoon Kang85bc1932024-07-01 17:04:46 +00007793 static_libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007794 apex_available: [
7795 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007796 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007797 "//apex_available:platform",
7798 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007799 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007800 }
7801
7802 java_library {
7803 name: "myotherjar",
7804 srcs: ["foo/bar/MyClass.java"],
7805 sdk_version: "none",
7806 system_modules: "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
7816 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7817 for _, f := range files {
7818 if f.path == file {
7819 if f.isLink {
7820 t.Errorf("%q is not a real file", file)
7821 }
7822 return
7823 }
7824 }
7825 t.Errorf("%q is not found", file)
7826 }
7827
Jiyong Parkce243632023-02-17 18:22:25 +09007828 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007829 for _, f := range files {
7830 if f.path == file {
7831 if !f.isLink {
7832 t.Errorf("%q is not a symlink", file)
7833 }
Jiyong Parkce243632023-02-17 18:22:25 +09007834 if f.src != target {
7835 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7836 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007837 return
7838 }
7839 }
7840 t.Errorf("%q is not found", file)
7841 }
7842
Jiyong Park9d677202020-02-19 16:29:35 +09007843 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7844 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007845 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana0503a52023-08-23 13:12:50 +09007846 files := getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007847 ensureRealfileExists(t, files, "javalib/myjar.jar")
7848 ensureRealfileExists(t, files, "lib64/mylib.so")
7849 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007850 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007851
Jooyung Hana0503a52023-08-23 13:12:50 +09007852 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007853 ensureRealfileExists(t, files, "javalib/myjar.jar")
7854 ensureRealfileExists(t, files, "lib64/mylib.so")
7855 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007856 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007857
7858 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007859 ctx = testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +09007860 files = getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007861 ensureRealfileExists(t, files, "javalib/myjar.jar")
7862 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007863 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7864 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007865
Jooyung Hana0503a52023-08-23 13:12:50 +09007866 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007867 ensureRealfileExists(t, files, "javalib/myjar.jar")
7868 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007869 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7870 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007871}
7872
Yo Chiange8128052020-07-23 20:09:18 +08007873func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007874 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007875 apex {
7876 name: "myapex",
7877 key: "myapex.key",
7878 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007879 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007880 }
7881
7882 apex_key {
7883 name: "myapex.key",
7884 public_key: "testkey.avbpubkey",
7885 private_key: "testkey.pem",
7886 }
7887
7888 cc_library_shared {
7889 name: "mylib",
7890 srcs: ["mylib.cpp"],
7891 shared_libs: ["myotherlib"],
7892 system_shared_libs: [],
7893 stl: "none",
7894 apex_available: [
7895 "myapex",
7896 "//apex_available:platform",
7897 ],
7898 }
7899
7900 cc_prebuilt_library_shared {
7901 name: "myotherlib",
7902 srcs: ["prebuilt.so"],
7903 system_shared_libs: [],
7904 stl: "none",
7905 apex_available: [
7906 "myapex",
7907 "//apex_available:platform",
7908 ],
7909 }
7910 `)
7911
Jooyung Hana0503a52023-08-23 13:12:50 +09007912 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007913 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007914 var builder strings.Builder
7915 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7916 androidMk := builder.String()
7917 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007918 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007919 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7920 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7921 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007922 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 myotherlib:64\n")
Yo Chiange8128052020-07-23 20:09:18 +08007923}
7924
Jooyung Han643adc42020-02-27 13:50:06 +09007925func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007926 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007927 apex {
7928 name: "myapex",
7929 key: "myapex.key",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007930 binaries: ["mybin"],
7931 jni_libs: ["mylib", "mylib3", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007932 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007933 }
7934
7935 apex_key {
7936 name: "myapex.key",
7937 public_key: "testkey.avbpubkey",
7938 private_key: "testkey.pem",
7939 }
7940
7941 cc_library {
7942 name: "mylib",
7943 srcs: ["mylib.cpp"],
7944 shared_libs: ["mylib2"],
7945 system_shared_libs: [],
7946 stl: "none",
7947 apex_available: [ "myapex" ],
7948 }
7949
7950 cc_library {
7951 name: "mylib2",
7952 srcs: ["mylib.cpp"],
7953 system_shared_libs: [],
7954 stl: "none",
7955 apex_available: [ "myapex" ],
7956 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007957
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007958 // Used as both a JNI library and a regular shared library.
7959 cc_library {
7960 name: "mylib3",
7961 srcs: ["mylib.cpp"],
7962 system_shared_libs: [],
7963 stl: "none",
7964 apex_available: [ "myapex" ],
7965 }
7966
7967 cc_binary {
7968 name: "mybin",
7969 srcs: ["mybin.cpp"],
7970 shared_libs: ["mylib3"],
7971 system_shared_libs: [],
7972 stl: "none",
7973 apex_available: [ "myapex" ],
7974 }
7975
Jiyong Park34d5c332022-02-24 18:02:44 +09007976 rust_ffi_shared {
7977 name: "libfoo.rust",
7978 crate_name: "foo",
7979 srcs: ["foo.rs"],
7980 shared_libs: ["libfoo.shared_from_rust"],
7981 prefer_rlib: true,
7982 apex_available: ["myapex"],
7983 }
7984
7985 cc_library_shared {
7986 name: "libfoo.shared_from_rust",
7987 srcs: ["mylib.cpp"],
7988 system_shared_libs: [],
7989 stl: "none",
7990 stubs: {
7991 versions: ["10", "11", "12"],
7992 },
7993 }
7994
Jooyung Han643adc42020-02-27 13:50:06 +09007995 `)
7996
Jooyung Hana0503a52023-08-23 13:12:50 +09007997 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han643adc42020-02-27 13:50:06 +09007998 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007999 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so mylib3.so")
Jooyung Hana0503a52023-08-23 13:12:50 +09008000 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008001 "bin/mybin",
Jooyung Han643adc42020-02-27 13:50:06 +09008002 "lib64/mylib.so",
8003 "lib64/mylib2.so",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008004 "lib64/mylib3.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09008005 "lib64/libfoo.rust.so",
8006 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
8007 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09008008 })
Jiyong Park34d5c332022-02-24 18:02:44 +09008009
8010 // b/220397949
8011 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09008012}
8013
Jooyung Han49f67012020-04-17 13:43:10 +09008014func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008015 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09008016 apex {
8017 name: "myapex",
8018 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008019 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09008020 }
8021 apex_key {
8022 name: "myapex.key",
8023 public_key: "testkey.avbpubkey",
8024 private_key: "testkey.pem",
8025 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008026 `,
8027 android.FixtureModifyConfig(func(config android.Config) {
8028 delete(config.Targets, android.Android)
8029 config.AndroidCommonTarget = android.Target{}
8030 }),
8031 )
Jooyung Han49f67012020-04-17 13:43:10 +09008032
8033 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
8034 t.Errorf("Expected variants: %v, but got: %v", expected, got)
8035 }
8036}
8037
Jiyong Parkbd159612020-02-28 15:22:21 +09008038func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008039 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09008040 apex {
8041 name: "myapex",
8042 key: "myapex.key",
8043 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008044 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09008045 }
8046
8047 apex_key {
8048 name: "myapex.key",
8049 public_key: "testkey.avbpubkey",
8050 private_key: "testkey.pem",
8051 }
8052
8053 android_app {
8054 name: "AppFoo",
8055 srcs: ["foo/bar/MyClass.java"],
8056 sdk_version: "none",
8057 system_modules: "none",
8058 apex_available: [ "myapex" ],
8059 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09008060 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09008061
Jooyung Hana0503a52023-08-23 13:12:50 +09008062 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07008063 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Jiyong Parkbd159612020-02-28 15:22:21 +09008064
8065 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00008066 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 +09008067}
8068
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008069func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008070 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008071 apex {
8072 name: "myapex",
8073 key: "myapex.key",
8074 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008075 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008076 }
8077
8078 apex_key {
8079 name: "myapex.key",
8080 public_key: "testkey.avbpubkey",
8081 private_key: "testkey.pem",
8082 }
8083
8084 android_app_set {
8085 name: "AppSet",
8086 set: "AppSet.apks",
8087 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09008088 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crosscf371cc2020-11-13 11:48:42 -08008089 bundleConfigRule := mod.Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07008090 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008091 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
8092 s := mod.Rule("apexRule").Args["copy_commands"]
8093 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09008094 if len(copyCmds) != 4 {
8095 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008096 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00008097 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
8098 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09008099 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
8100 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09008101
8102 // Ensure that canned_fs_config has an entry for the app set zip file
8103 generateFsRule := mod.Rule("generateFsConfig")
8104 cmd := generateFsRule.RuleParams.Command
8105 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008106}
8107
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008108func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01008109 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008110 apex_set {
8111 name: "myapex",
8112 filename: "foo_v2.apex",
8113 sanitized: {
8114 none: { set: "myapex.apks", },
8115 hwaddress: { set: "myapex.hwasan.apks", },
8116 },
Paul Duffin24704672021-04-06 16:09:30 +01008117 }
8118 `
8119 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008120
Paul Duffin24704672021-04-06 16:09:30 +01008121 // Check that the extractor produces the correct output file from the correct input file.
Spandan Das3576e762024-01-03 18:57:03 +00008122 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008123
Spandan Das3576e762024-01-03 18:57:03 +00008124 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Paul Duffin24704672021-04-06 16:09:30 +01008125 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008126
Paul Duffin24704672021-04-06 16:09:30 +01008127 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
8128
8129 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01008130 m = ctx.ModuleForTests("myapex", "android_common_myapex")
8131 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01008132
8133 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008134}
8135
Pranav Guptaeba03b02022-09-27 00:27:08 +00008136func TestApexSetApksModuleAssignment(t *testing.T) {
8137 ctx := testApex(t, `
8138 apex_set {
8139 name: "myapex",
8140 set: ":myapex_apks_file",
8141 }
8142
8143 filegroup {
8144 name: "myapex_apks_file",
8145 srcs: ["myapex.apks"],
8146 }
8147 `)
8148
Spandan Das3576e762024-01-03 18:57:03 +00008149 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Pranav Guptaeba03b02022-09-27 00:27:08 +00008150
8151 // Check that the extractor produces the correct apks file from the input module
Spandan Das3576e762024-01-03 18:57:03 +00008152 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.apks"
Pranav Guptaeba03b02022-09-27 00:27:08 +00008153 extractedApex := m.Output(extractorOutput)
8154
8155 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
8156}
8157
Paul Duffin89f570a2021-06-16 01:42:33 +01008158func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00008159 t.Helper()
8160
Paul Duffin55607122021-03-30 23:32:51 +01008161 fs := android.MockFS{
8162 "a.java": nil,
8163 "a.jar": nil,
8164 "apex_manifest.json": nil,
8165 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00008166 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00008167 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
8168 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
8169 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00008170 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008171 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008172
Paul Duffin55607122021-03-30 23:32:51 +01008173 errorHandler := android.FixtureExpectsNoErrors
8174 if errmsg != "" {
8175 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008176 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008177
Paul Duffin55607122021-03-30 23:32:51 +01008178 result := android.GroupFixturePreparers(
8179 cc.PrepareForTestWithCcDefaultModules,
8180 java.PrepareForTestWithHiddenApiBuildComponents,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008181 java.PrepareForTestWithDexpreopt,
Paul Duffin55607122021-03-30 23:32:51 +01008182 java.PrepareForTestWithJavaSdkLibraryFiles,
8183 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01008184 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01008185 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01008186 android.FixtureModifyMockFS(func(fs android.MockFS) {
8187 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
8188 insert := ""
8189 for _, fragment := range fragments {
8190 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
8191 }
8192 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
8193 platform_bootclasspath {
8194 name: "platform-bootclasspath",
8195 fragments: [
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008196 {apex: "com.android.art", module: "art-bootclasspath-fragment"},
Paul Duffin89f570a2021-06-16 01:42:33 +01008197 %s
8198 ],
8199 }
8200 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01008201 }
Paul Duffin89f570a2021-06-16 01:42:33 +01008202 }),
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008203 // Dexpreopt for boot jars requires the ART boot image profile.
8204 java.PrepareApexBootJarModule("com.android.art", "core-oj"),
8205 dexpreopt.FixtureSetArtBootJars("com.android.art:core-oj"),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00008206 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01008207 ).
8208 ExtendWithErrorHandler(errorHandler).
8209 RunTestWithBp(t, bp)
8210
8211 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008212}
8213
Paul Duffin5556c5f2022-06-09 17:32:21 +00008214func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008215 preparers := android.GroupFixturePreparers(
8216 java.PrepareForTestWithJavaDefaultModules,
Spandan Das5be63332023-12-13 00:06:32 +00008217 prepareForTestWithBootclasspathFragment,
8218 dexpreopt.FixtureSetTestOnlyArtBootImageJars("com.android.art:libfoo"),
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008219 PrepareForTestWithApexBuildComponents,
8220 ).
8221 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
Spandan Das3576e762024-01-03 18:57:03 +00008222 "Multiple installable prebuilt APEXes provide ambiguous deapexers: prebuilt_com.android.art and prebuilt_com.mycompany.android.art"))
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008223
8224 bpBase := `
8225 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008226 name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008227 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008228 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008229 set: "myapex.apks",
8230 }
8231
8232 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008233 name: "com.mycompany.android.art",
8234 apex_name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008235 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008236 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008237 set: "company-myapex.apks",
8238 }
8239
8240 prebuilt_bootclasspath_fragment {
Spandan Das5be63332023-12-13 00:06:32 +00008241 name: "art-bootclasspath-fragment",
8242 apex_available: ["com.android.art"],
Spandan Dasfae468e2023-12-12 23:23:53 +00008243 hidden_api: {
8244 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8245 metadata: "my-bootclasspath-fragment/metadata.csv",
8246 index: "my-bootclasspath-fragment/index.csv",
8247 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
8248 all_flags: "my-bootclasspath-fragment/all-flags.csv",
8249 },
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008250 %s
8251 }
8252 `
8253
8254 t.Run("java_import", func(t *testing.T) {
8255 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8256 java_import {
8257 name: "libfoo",
8258 jars: ["libfoo.jar"],
Spandan Das5be63332023-12-13 00:06:32 +00008259 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008260 }
8261 `)
8262 })
8263
8264 t.Run("java_sdk_library_import", func(t *testing.T) {
8265 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8266 java_sdk_library_import {
8267 name: "libfoo",
8268 public: {
8269 jars: ["libbar.jar"],
8270 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008271 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008272 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008273 }
8274 `)
8275 })
8276
8277 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8278 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8279 image_name: "art",
8280 contents: ["libfoo"],
8281 `)+`
8282 java_sdk_library_import {
8283 name: "libfoo",
8284 public: {
8285 jars: ["libbar.jar"],
8286 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008287 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008288 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008289 }
8290 `)
8291 })
8292}
8293
Paul Duffin5556c5f2022-06-09 17:32:21 +00008294func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
8295 preparers := android.GroupFixturePreparers(
8296 java.PrepareForTestWithJavaDefaultModules,
8297 PrepareForTestWithApexBuildComponents,
8298 )
8299
Spandan Das59a4a2b2024-01-09 21:35:56 +00008300 errCtx := moduleErrorfTestCtx{}
8301
Paul Duffin5556c5f2022-06-09 17:32:21 +00008302 bpBase := `
8303 apex_set {
8304 name: "com.android.myapex",
8305 installable: true,
8306 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8307 set: "myapex.apks",
8308 }
8309
8310 apex_set {
8311 name: "com.android.myapex_compressed",
8312 apex_name: "com.android.myapex",
8313 installable: true,
8314 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8315 set: "myapex_compressed.apks",
8316 }
8317
8318 prebuilt_bootclasspath_fragment {
8319 name: "my-bootclasspath-fragment",
8320 apex_available: [
8321 "com.android.myapex",
8322 "com.android.myapex_compressed",
8323 ],
8324 hidden_api: {
8325 annotation_flags: "annotation-flags.csv",
8326 metadata: "metadata.csv",
8327 index: "index.csv",
8328 signature_patterns: "signature_patterns.csv",
8329 },
8330 %s
8331 }
8332 `
8333
8334 t.Run("java_import", func(t *testing.T) {
8335 result := preparers.RunTestWithBp(t,
8336 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8337 java_import {
8338 name: "libfoo",
8339 jars: ["libfoo.jar"],
8340 apex_available: [
8341 "com.android.myapex",
8342 "com.android.myapex_compressed",
8343 ],
8344 }
8345 `)
8346
8347 module := result.Module("libfoo", "android_common_com.android.myapex")
8348 usesLibraryDep := module.(java.UsesLibraryDependency)
8349 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008350 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008351 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008352 })
8353
8354 t.Run("java_sdk_library_import", func(t *testing.T) {
8355 result := preparers.RunTestWithBp(t,
8356 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8357 java_sdk_library_import {
8358 name: "libfoo",
8359 public: {
8360 jars: ["libbar.jar"],
8361 },
8362 apex_available: [
8363 "com.android.myapex",
8364 "com.android.myapex_compressed",
8365 ],
8366 compile_dex: true,
8367 }
8368 `)
8369
8370 module := result.Module("libfoo", "android_common_com.android.myapex")
8371 usesLibraryDep := module.(java.UsesLibraryDependency)
8372 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008373 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008374 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008375 })
8376
8377 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8378 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8379 image_name: "art",
8380 contents: ["libfoo"],
8381 `)+`
8382 java_sdk_library_import {
8383 name: "libfoo",
8384 public: {
8385 jars: ["libbar.jar"],
8386 },
8387 apex_available: [
8388 "com.android.myapex",
8389 "com.android.myapex_compressed",
8390 ],
8391 compile_dex: true,
8392 }
8393 `)
8394 })
8395}
8396
Jooyung Han548640b2020-04-27 12:10:30 +09008397func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
8398 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8399 apex {
8400 name: "myapex",
8401 key: "myapex.key",
8402 updatable: true,
8403 }
8404
8405 apex_key {
8406 name: "myapex.key",
8407 public_key: "testkey.avbpubkey",
8408 private_key: "testkey.pem",
8409 }
8410 `)
8411}
8412
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008413func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
8414 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8415 apex {
8416 name: "myapex",
8417 key: "myapex.key",
8418 }
8419
8420 apex_key {
8421 name: "myapex.key",
8422 public_key: "testkey.avbpubkey",
8423 private_key: "testkey.pem",
8424 }
8425 `)
8426}
8427
satayevb98371c2021-06-15 16:49:50 +01008428func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
8429 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
8430 apex {
8431 name: "myapex",
8432 key: "myapex.key",
8433 systemserverclasspath_fragments: [
8434 "mysystemserverclasspathfragment",
8435 ],
8436 min_sdk_version: "29",
8437 updatable: true,
8438 }
8439
8440 apex_key {
8441 name: "myapex.key",
8442 public_key: "testkey.avbpubkey",
8443 private_key: "testkey.pem",
8444 }
8445
8446 java_library {
8447 name: "foo",
8448 srcs: ["b.java"],
8449 min_sdk_version: "29",
8450 installable: true,
8451 apex_available: [
8452 "myapex",
8453 ],
Jihoon Kang85bc1932024-07-01 17:04:46 +00008454 sdk_version: "current",
satayevb98371c2021-06-15 16:49:50 +01008455 }
8456
8457 systemserverclasspath_fragment {
8458 name: "mysystemserverclasspathfragment",
8459 generate_classpaths_proto: false,
8460 contents: [
8461 "foo",
8462 ],
8463 apex_available: [
8464 "myapex",
8465 ],
8466 }
satayevabcd5972021-08-06 17:49:46 +01008467 `,
8468 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8469 )
satayevb98371c2021-06-15 16:49:50 +01008470}
8471
Paul Duffin064b70c2020-11-02 17:32:38 +00008472func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008473 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008474 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008475 fragment := java.ApexVariantReference{
8476 Apex: proptools.StringPtr("myapex"),
8477 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8478 }
8479
Paul Duffin064b70c2020-11-02 17:32:38 +00008480 testDexpreoptWithApexes(t, `
8481 prebuilt_apex {
8482 name: "myapex" ,
8483 arch: {
8484 arm64: {
8485 src: "myapex-arm64.apex",
8486 },
8487 arm: {
8488 src: "myapex-arm.apex",
8489 },
8490 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008491 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8492 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008493
Paul Duffin89f570a2021-06-16 01:42:33 +01008494 prebuilt_bootclasspath_fragment {
8495 name: "my-bootclasspath-fragment",
8496 contents: ["libfoo"],
8497 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008498 hidden_api: {
8499 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8500 metadata: "my-bootclasspath-fragment/metadata.csv",
8501 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008502 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8503 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8504 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008505 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008506 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008507
Paul Duffin89f570a2021-06-16 01:42:33 +01008508 java_import {
8509 name: "libfoo",
8510 jars: ["libfoo.jar"],
8511 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008512 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008513 }
8514 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008515 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008516}
8517
Spandan Dasf14e2542021-11-12 00:01:37 +00008518func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008519 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008520 bp += `
8521 apex_key {
8522 name: "myapex.key",
8523 public_key: "testkey.avbpubkey",
8524 private_key: "testkey.pem",
8525 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008526 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008527 "lib1/src/A.java": nil,
8528 "lib2/src/B.java": nil,
8529 "system/sepolicy/apex/myapex-file_contexts": nil,
8530 }
8531
Paul Duffin45338f02021-03-30 23:07:52 +01008532 errorHandler := android.FixtureExpectsNoErrors
8533 if errmsg != "" {
8534 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008535 }
Colin Crossae8600b2020-10-29 17:09:13 -07008536
Paul Duffin45338f02021-03-30 23:07:52 +01008537 android.GroupFixturePreparers(
8538 android.PrepareForTestWithAndroidBuildComponents,
8539 java.PrepareForTestWithJavaBuildComponents,
8540 PrepareForTestWithApexBuildComponents,
8541 android.PrepareForTestWithNeverallowRules(rules),
8542 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008543 apexBootJars := make([]string, 0, len(bootJars))
8544 for _, apexBootJar := range bootJars {
8545 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008546 }
satayevd604b212021-07-21 14:23:52 +01008547 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008548 }),
8549 fs.AddToFixture(),
8550 ).
8551 ExtendWithErrorHandler(errorHandler).
8552 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008553}
8554
8555func TestApexPermittedPackagesRules(t *testing.T) {
8556 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008557 name string
8558 expectedError string
8559 bp string
8560 bootJars []string
8561 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008562 }{
8563
8564 {
8565 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8566 expectedError: "",
8567 bp: `
8568 java_library {
8569 name: "bcp_lib1",
8570 srcs: ["lib1/src/*.java"],
8571 permitted_packages: ["foo.bar"],
8572 apex_available: ["myapex"],
8573 sdk_version: "none",
8574 system_modules: "none",
8575 }
8576 java_library {
8577 name: "nonbcp_lib2",
8578 srcs: ["lib2/src/*.java"],
8579 apex_available: ["myapex"],
8580 permitted_packages: ["a.b"],
8581 sdk_version: "none",
8582 system_modules: "none",
8583 }
8584 apex {
8585 name: "myapex",
8586 key: "myapex.key",
8587 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008588 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008589 }`,
8590 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008591 bcpPermittedPackages: map[string][]string{
8592 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008593 "foo.bar",
8594 },
8595 },
8596 },
8597 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008598 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008599 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 +01008600 bp: `
8601 java_library {
8602 name: "bcp_lib1",
8603 srcs: ["lib1/src/*.java"],
8604 apex_available: ["myapex"],
8605 permitted_packages: ["foo.bar"],
8606 sdk_version: "none",
8607 system_modules: "none",
8608 }
8609 java_library {
8610 name: "bcp_lib2",
8611 srcs: ["lib2/src/*.java"],
8612 apex_available: ["myapex"],
8613 permitted_packages: ["foo.bar", "bar.baz"],
8614 sdk_version: "none",
8615 system_modules: "none",
8616 }
8617 apex {
8618 name: "myapex",
8619 key: "myapex.key",
8620 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008621 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008622 }
8623 `,
8624 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008625 bcpPermittedPackages: map[string][]string{
8626 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008627 "foo.bar",
8628 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008629 "bcp_lib2": []string{
8630 "foo.bar",
8631 },
8632 },
8633 },
8634 {
8635 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8636 expectedError: "",
8637 bp: `
8638 java_library {
8639 name: "bcp_lib_restricted",
8640 srcs: ["lib1/src/*.java"],
8641 apex_available: ["myapex"],
8642 permitted_packages: ["foo.bar"],
8643 sdk_version: "none",
8644 min_sdk_version: "29",
8645 system_modules: "none",
8646 }
8647 java_library {
8648 name: "bcp_lib_unrestricted",
8649 srcs: ["lib2/src/*.java"],
8650 apex_available: ["myapex"],
8651 permitted_packages: ["foo.bar", "bar.baz"],
8652 sdk_version: "none",
8653 min_sdk_version: "29",
8654 system_modules: "none",
8655 }
8656 apex {
8657 name: "myapex",
8658 key: "myapex.key",
8659 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8660 updatable: true,
8661 min_sdk_version: "29",
8662 }
8663 `,
8664 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8665 bcpPermittedPackages: map[string][]string{
8666 "bcp_lib1_non_updateable": []string{
8667 "foo.bar",
8668 },
8669 // 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 +01008670 },
8671 },
8672 }
8673 for _, tc := range testcases {
8674 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008675 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8676 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008677 })
8678 }
8679}
8680
Jiyong Park62304bb2020-04-13 16:19:48 +09008681func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008682 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008683 apex {
8684 name: "myapex",
8685 key: "myapex.key",
8686 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008687 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008688 }
8689
8690 apex_key {
8691 name: "myapex.key",
8692 public_key: "testkey.avbpubkey",
8693 private_key: "testkey.pem",
8694 }
8695
8696 cc_library {
8697 name: "mylib",
8698 srcs: ["mylib.cpp"],
8699 system_shared_libs: [],
8700 stl: "none",
8701 stubs: {
8702 versions: ["1"],
8703 },
8704 apex_available: ["myapex"],
8705 }
8706
8707 cc_library {
8708 name: "myprivlib",
8709 srcs: ["mylib.cpp"],
8710 system_shared_libs: [],
8711 stl: "none",
8712 apex_available: ["myapex"],
8713 }
8714
8715
8716 cc_test {
8717 name: "mytest",
8718 gtest: false,
8719 srcs: ["mylib.cpp"],
8720 system_shared_libs: [],
8721 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008722 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008723 test_for: ["myapex"]
8724 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008725
8726 cc_library {
8727 name: "mytestlib",
8728 srcs: ["mylib.cpp"],
8729 system_shared_libs: [],
8730 shared_libs: ["mylib", "myprivlib"],
8731 stl: "none",
8732 test_for: ["myapex"],
8733 }
8734
8735 cc_benchmark {
8736 name: "mybench",
8737 srcs: ["mylib.cpp"],
8738 system_shared_libs: [],
8739 shared_libs: ["mylib", "myprivlib"],
8740 stl: "none",
8741 test_for: ["myapex"],
8742 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008743 `)
8744
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008745 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008746 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008747 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8748 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8749 }
8750
8751 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008752 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008753 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8754 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8755 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8756}
Jiyong Park46a512f2020-12-04 18:02:13 +09008757
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008758func TestIndirectTestFor(t *testing.T) {
8759 ctx := testApex(t, `
8760 apex {
8761 name: "myapex",
8762 key: "myapex.key",
8763 native_shared_libs: ["mylib", "myprivlib"],
8764 updatable: false,
8765 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008766
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008767 apex_key {
8768 name: "myapex.key",
8769 public_key: "testkey.avbpubkey",
8770 private_key: "testkey.pem",
8771 }
8772
8773 cc_library {
8774 name: "mylib",
8775 srcs: ["mylib.cpp"],
8776 system_shared_libs: [],
8777 stl: "none",
8778 stubs: {
8779 versions: ["1"],
8780 },
8781 apex_available: ["myapex"],
8782 }
8783
8784 cc_library {
8785 name: "myprivlib",
8786 srcs: ["mylib.cpp"],
8787 system_shared_libs: [],
8788 stl: "none",
8789 shared_libs: ["mylib"],
8790 apex_available: ["myapex"],
8791 }
8792
8793 cc_library {
8794 name: "mytestlib",
8795 srcs: ["mylib.cpp"],
8796 system_shared_libs: [],
8797 shared_libs: ["myprivlib"],
8798 stl: "none",
8799 test_for: ["myapex"],
8800 }
8801 `)
8802
8803 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008804 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008805 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8806 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8807 }
8808
8809 // The platform variant of mytestlib links to the platform variant of the
8810 // internal myprivlib.
8811 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8812
8813 // The platform variant of myprivlib links to the platform variant of mylib
8814 // and bypasses its stubs.
8815 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 +09008816}
8817
Martin Stjernholmec009002021-03-27 15:18:31 +00008818func TestTestForForLibInOtherApex(t *testing.T) {
8819 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8820 _ = testApex(t, `
8821 apex {
8822 name: "com.android.art",
8823 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008824 native_shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008825 updatable: false,
8826 }
8827
8828 apex {
8829 name: "com.android.art.debug",
8830 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008831 native_shared_libs: ["libnativebridge", "libnativebrdige_test"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008832 updatable: false,
8833 }
8834
8835 apex_key {
8836 name: "myapex.key",
8837 public_key: "testkey.avbpubkey",
8838 private_key: "testkey.pem",
8839 }
8840
8841 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008842 name: "libnativebridge",
8843 srcs: ["libnativebridge.cpp"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008844 system_shared_libs: [],
8845 stl: "none",
8846 stubs: {
8847 versions: ["1"],
8848 },
8849 apex_available: ["com.android.art", "com.android.art.debug"],
8850 }
8851
8852 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008853 name: "libnativebrdige_test",
Martin Stjernholmec009002021-03-27 15:18:31 +00008854 srcs: ["mylib.cpp"],
8855 system_shared_libs: [],
Spandan Das20fce2d2023-04-12 17:21:39 +00008856 shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008857 stl: "none",
8858 apex_available: ["com.android.art.debug"],
8859 test_for: ["com.android.art"],
8860 }
8861 `,
8862 android.MockFS{
8863 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8864 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8865 }.AddToFixture())
8866}
8867
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008868// TODO(jungjw): Move this to proptools
8869func intPtr(i int) *int {
8870 return &i
8871}
8872
8873func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008874 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008875 apex_set {
8876 name: "myapex",
8877 set: "myapex.apks",
8878 filename: "foo_v2.apex",
8879 overrides: ["foo"],
8880 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008881 `,
8882 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8883 variables.Platform_sdk_version = intPtr(30)
8884 }),
8885 android.FixtureModifyConfig(func(config android.Config) {
8886 config.Targets[android.Android] = []android.Target{
8887 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8888 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8889 }
8890 }),
8891 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008892
Spandan Das3576e762024-01-03 18:57:03 +00008893 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008894
8895 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008896 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008897 actual := extractedApex.Args["abis"]
8898 expected := "ARMEABI_V7A,ARM64_V8A"
8899 if actual != expected {
8900 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8901 }
8902 actual = extractedApex.Args["sdk-version"]
8903 expected = "30"
8904 if actual != expected {
8905 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8906 }
8907
Paul Duffin6717d882021-06-15 19:09:41 +01008908 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008909 a := m.Module().(*ApexSet)
8910 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008911 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008912 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8913 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8914 }
8915}
8916
Anton Hansson805e0a52022-11-25 14:06:46 +00008917func TestApexSet_NativeBridge(t *testing.T) {
8918 ctx := testApex(t, `
8919 apex_set {
8920 name: "myapex",
8921 set: "myapex.apks",
8922 filename: "foo_v2.apex",
8923 overrides: ["foo"],
8924 }
8925 `,
8926 android.FixtureModifyConfig(func(config android.Config) {
8927 config.Targets[android.Android] = []android.Target{
8928 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8929 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8930 }
8931 }),
8932 )
8933
Spandan Das3576e762024-01-03 18:57:03 +00008934 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Anton Hansson805e0a52022-11-25 14:06:46 +00008935
8936 // Check extract_apks tool parameters. No native bridge arch expected
8937 extractedApex := m.Output("extracted/myapex.apks")
8938 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8939}
8940
Jiyong Park7d95a512020-05-10 15:16:24 +09008941func TestNoStaticLinkingToStubsLib(t *testing.T) {
8942 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8943 apex {
8944 name: "myapex",
8945 key: "myapex.key",
8946 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008947 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008948 }
8949
8950 apex_key {
8951 name: "myapex.key",
8952 public_key: "testkey.avbpubkey",
8953 private_key: "testkey.pem",
8954 }
8955
8956 cc_library {
8957 name: "mylib",
8958 srcs: ["mylib.cpp"],
8959 static_libs: ["otherlib"],
8960 system_shared_libs: [],
8961 stl: "none",
8962 apex_available: [ "myapex" ],
8963 }
8964
8965 cc_library {
8966 name: "otherlib",
8967 srcs: ["mylib.cpp"],
8968 system_shared_libs: [],
8969 stl: "none",
8970 stubs: {
8971 versions: ["1", "2", "3"],
8972 },
8973 apex_available: [ "myapex" ],
8974 }
8975 `)
8976}
8977
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008978func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008979 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008980 apex {
8981 name: "myapex",
8982 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008983 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008984 custom_sign_tool: "sign_myapex",
8985 }
8986
8987 apex_key {
8988 name: "myapex.key",
8989 public_key: "testkey.avbpubkey",
8990 private_key: "testkey.pem",
8991 }
8992 `)
8993
Jooyung Han286957d2023-10-30 16:17:56 +09008994 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07008995 content := android.ContentFromFileRuleForTests(t, ctx, myapex.Output("apexkeys.txt"))
Jooyung Haneec1b3f2023-06-20 16:25:59 +09008996 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 +09008997}
8998
8999func TestApexKeysTxtOverrides(t *testing.T) {
9000 ctx := testApex(t, `
9001 apex {
9002 name: "myapex",
9003 key: "myapex.key",
9004 updatable: false,
9005 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09009006 }
9007
9008 apex_key {
9009 name: "myapex.key",
9010 public_key: "testkey.avbpubkey",
9011 private_key: "testkey.pem",
9012 }
9013
9014 prebuilt_apex {
9015 name: "myapex",
9016 prefer: true,
9017 arch: {
9018 arm64: {
9019 src: "myapex-arm64.apex",
9020 },
9021 arm: {
9022 src: "myapex-arm.apex",
9023 },
9024 },
9025 }
9026
9027 apex_set {
9028 name: "myapex_set",
9029 set: "myapex.apks",
9030 filename: "myapex_set.apex",
9031 overrides: ["myapex"],
9032 }
9033 `)
9034
Colin Crossf61d03d2023-11-02 16:56:39 -07009035 content := android.ContentFromFileRuleForTests(t, ctx,
9036 ctx.ModuleForTests("myapex", "android_common_myapex").Output("apexkeys.txt"))
Jooyung Han286957d2023-10-30 16:17:56 +09009037 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 -07009038 content = android.ContentFromFileRuleForTests(t, ctx,
9039 ctx.ModuleForTests("myapex_set", "android_common_myapex_set").Output("apexkeys.txt"))
Jiyong Park8d6c51e2020-06-12 17:26:31 +09009040 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 +09009041}
9042
Jooyung Han938b5932020-06-20 12:47:47 +09009043func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009044 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09009045 apex {
9046 name: "myapex",
9047 key: "myapex.key",
9048 apps: ["app"],
9049 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009050 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09009051 }
9052
9053 apex_key {
9054 name: "myapex.key",
9055 public_key: "testkey.avbpubkey",
9056 private_key: "testkey.pem",
9057 }
9058
9059 android_app {
9060 name: "app",
9061 srcs: ["foo/bar/MyClass.java"],
9062 package_name: "foo",
9063 sdk_version: "none",
9064 system_modules: "none",
9065 apex_available: [ "myapex" ],
9066 }
9067 `, withFiles(map[string][]byte{
9068 "sub/Android.bp": []byte(`
9069 override_apex {
9070 name: "override_myapex",
9071 base: "myapex",
9072 apps: ["override_app"],
9073 allowed_files: ":allowed",
9074 }
9075 // Overridable "path" property should be referenced indirectly
9076 filegroup {
9077 name: "allowed",
9078 srcs: ["allowed.txt"],
9079 }
9080 override_android_app {
9081 name: "override_app",
9082 base: "app",
9083 package_name: "bar",
9084 }
9085 `),
9086 }))
9087
Jooyung Hana0503a52023-08-23 13:12:50 +09009088 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09009089 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
9090 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
9091 }
9092
Spandan Das50801e22024-05-13 18:29:45 +00009093 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09009094 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
9095 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
9096 }
9097}
9098
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009099func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009100 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009101 apex {
9102 name: "myapex",
9103 key: "myapex.key",
9104 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009105 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009106 }
9107
9108 apex_key {
9109 name: "myapex.key",
9110 public_key: "testkey.avbpubkey",
9111 private_key: "testkey.pem",
9112 }
9113
9114 cc_library {
9115 name: "mylib",
9116 srcs: ["mylib.cpp"],
9117 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07009118 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009119 },
9120 apex_available: ["myapex"],
9121 }
9122
9123 cc_prebuilt_library_shared {
9124 name: "mylib",
9125 prefer: false,
9126 srcs: ["prebuilt.so"],
9127 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07009128 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009129 },
9130 apex_available: ["myapex"],
9131 }
9132 `)
9133}
9134
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009135func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009136 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009137 apex {
9138 name: "myapex",
9139 key: "myapex.key",
9140 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009141 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009142 }
9143 apex_key {
9144 name: "myapex.key",
9145 public_key: "testkey.avbpubkey",
9146 private_key: "testkey.pem",
9147 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00009148 `,
9149 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9150 variables.CompressedApex = proptools.BoolPtr(true)
9151 }),
9152 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009153
Jooyung Hana0503a52023-08-23 13:12:50 +09009154 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("compressRule")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009155 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
9156
Jooyung Hana0503a52023-08-23 13:12:50 +09009157 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex").Description("sign compressedApex")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009158 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
9159
9160 // Make sure output of bundle is .capex
Jooyung Hana0503a52023-08-23 13:12:50 +09009161 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009162 ensureContains(t, ab.outputFile.String(), "myapex.capex")
9163
9164 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07009165 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009166 var builder strings.Builder
9167 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9168 androidMk := builder.String()
9169 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
9170}
9171
Jooyung Han26ec8482024-07-31 15:04:05 +09009172func TestApexSet_ShouldRespectCompressedApexFlag(t *testing.T) {
9173 for _, compressionEnabled := range []bool{true, false} {
9174 t.Run(fmt.Sprintf("compressionEnabled=%v", compressionEnabled), func(t *testing.T) {
9175 ctx := testApex(t, `
9176 apex_set {
9177 name: "com.company.android.myapex",
9178 apex_name: "com.android.myapex",
9179 set: "company-myapex.apks",
9180 }
9181 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9182 variables.CompressedApex = proptools.BoolPtr(compressionEnabled)
9183 }),
9184 )
9185
9186 build := ctx.ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex").Output("com.company.android.myapex.apex")
9187 if compressionEnabled {
9188 ensureEquals(t, build.Rule.String(), "android/soong/android.Cp")
9189 } else {
9190 ensureEquals(t, build.Rule.String(), "android/apex.decompressApex")
9191 }
9192 })
9193 }
9194}
9195
Martin Stjernholm2856c662020-12-02 15:03:42 +00009196func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009197 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00009198 apex {
9199 name: "myapex",
9200 key: "myapex.key",
9201 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009202 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00009203 }
9204
9205 apex_key {
9206 name: "myapex.key",
9207 public_key: "testkey.avbpubkey",
9208 private_key: "testkey.pem",
9209 }
9210
9211 cc_library {
9212 name: "mylib",
9213 srcs: ["mylib.cpp"],
9214 apex_available: ["myapex"],
9215 shared_libs: ["otherlib"],
9216 system_shared_libs: [],
9217 }
9218
9219 cc_library {
9220 name: "otherlib",
9221 srcs: ["mylib.cpp"],
9222 stubs: {
9223 versions: ["current"],
9224 },
9225 }
9226
9227 cc_prebuilt_library_shared {
9228 name: "otherlib",
9229 prefer: true,
9230 srcs: ["prebuilt.so"],
9231 stubs: {
9232 versions: ["current"],
9233 },
9234 }
9235 `)
9236
Jooyung Hana0503a52023-08-23 13:12:50 +09009237 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07009238 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00009239 var builder strings.Builder
9240 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9241 androidMk := builder.String()
9242
9243 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
9244 // a thing there.
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009245 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 otherlib\n")
Martin Stjernholm2856c662020-12-02 15:03:42 +00009246}
9247
Jiyong Parke3867542020-12-03 17:28:25 +09009248func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009249 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09009250 apex {
9251 name: "myapex",
9252 key: "myapex.key",
9253 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009254 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09009255 }
9256
9257 apex_key {
9258 name: "myapex.key",
9259 public_key: "testkey.avbpubkey",
9260 private_key: "testkey.pem",
9261 }
9262
9263 cc_library {
9264 name: "mylib",
9265 srcs: ["mylib.cpp"],
9266 system_shared_libs: [],
9267 stl: "none",
9268 apex_available: ["myapex"],
9269 shared_libs: ["mylib2"],
9270 target: {
9271 apex: {
9272 exclude_shared_libs: ["mylib2"],
9273 },
9274 },
9275 }
9276
9277 cc_library {
9278 name: "mylib2",
9279 srcs: ["mylib.cpp"],
9280 system_shared_libs: [],
9281 stl: "none",
9282 }
9283 `)
9284
9285 // Check if mylib is linked to mylib2 for the non-apex target
9286 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
9287 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
9288
9289 // Make sure that the link doesn't occur for the apex target
9290 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
9291 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
9292
9293 // It shouldn't appear in the copy cmd as well.
Jooyung Hana0503a52023-08-23 13:12:50 +09009294 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule").Args["copy_commands"]
Jiyong Parke3867542020-12-03 17:28:25 +09009295 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
9296}
9297
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009298func TestPrebuiltStubLibDep(t *testing.T) {
9299 bpBase := `
9300 apex {
9301 name: "myapex",
9302 key: "myapex.key",
9303 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009304 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009305 }
9306 apex_key {
9307 name: "myapex.key",
9308 public_key: "testkey.avbpubkey",
9309 private_key: "testkey.pem",
9310 }
9311 cc_library {
9312 name: "mylib",
9313 srcs: ["mylib.cpp"],
9314 apex_available: ["myapex"],
9315 shared_libs: ["stublib"],
9316 system_shared_libs: [],
9317 }
9318 apex {
9319 name: "otherapex",
9320 enabled: %s,
9321 key: "myapex.key",
9322 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009323 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009324 }
9325 `
9326
9327 stublibSourceBp := `
9328 cc_library {
9329 name: "stublib",
9330 srcs: ["mylib.cpp"],
9331 apex_available: ["otherapex"],
9332 system_shared_libs: [],
9333 stl: "none",
9334 stubs: {
9335 versions: ["1"],
9336 },
9337 }
9338 `
9339
9340 stublibPrebuiltBp := `
9341 cc_prebuilt_library_shared {
9342 name: "stublib",
9343 srcs: ["prebuilt.so"],
9344 apex_available: ["otherapex"],
9345 stubs: {
9346 versions: ["1"],
9347 },
9348 %s
9349 }
9350 `
9351
9352 tests := []struct {
9353 name string
9354 stublibBp string
9355 usePrebuilt bool
9356 modNames []string // Modules to collect AndroidMkEntries for
9357 otherApexEnabled []string
9358 }{
9359 {
9360 name: "only_source",
9361 stublibBp: stublibSourceBp,
9362 usePrebuilt: false,
9363 modNames: []string{"stublib"},
9364 otherApexEnabled: []string{"true", "false"},
9365 },
9366 {
9367 name: "source_preferred",
9368 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9369 usePrebuilt: false,
9370 modNames: []string{"stublib", "prebuilt_stublib"},
9371 otherApexEnabled: []string{"true", "false"},
9372 },
9373 {
9374 name: "prebuilt_preferred",
9375 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9376 usePrebuilt: true,
9377 modNames: []string{"stublib", "prebuilt_stublib"},
9378 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9379 },
9380 {
9381 name: "only_prebuilt",
9382 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9383 usePrebuilt: true,
9384 modNames: []string{"stublib"},
9385 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9386 },
9387 }
9388
9389 for _, test := range tests {
9390 t.Run(test.name, func(t *testing.T) {
9391 for _, otherApexEnabled := range test.otherApexEnabled {
9392 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009393 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009394
9395 type modAndMkEntries struct {
9396 mod *cc.Module
9397 mkEntries android.AndroidMkEntries
9398 }
9399 entries := []*modAndMkEntries{}
9400
9401 // Gather shared lib modules that are installable
9402 for _, modName := range test.modNames {
9403 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9404 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9405 continue
9406 }
9407 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Cole Fausta963b942024-04-11 17:43:00 -07009408 if !mod.Enabled(android.PanickingConfigAndErrorContext(ctx)) || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009409 continue
9410 }
Colin Crossaa255532020-07-03 13:18:24 -07009411 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009412 if ent.Disabled {
9413 continue
9414 }
9415 entries = append(entries, &modAndMkEntries{
9416 mod: mod,
9417 mkEntries: ent,
9418 })
9419 }
9420 }
9421 }
9422
9423 var entry *modAndMkEntries = nil
9424 for _, ent := range entries {
9425 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9426 if entry != nil {
9427 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9428 } else {
9429 entry = ent
9430 }
9431 }
9432 }
9433
9434 if entry == nil {
9435 t.Errorf("AndroidMk entry for \"stublib\" missing")
9436 } else {
9437 isPrebuilt := entry.mod.Prebuilt() != nil
9438 if isPrebuilt != test.usePrebuilt {
9439 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9440 }
9441 if !entry.mod.IsStubs() {
9442 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9443 }
9444 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9445 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9446 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009447 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009448 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009449 if !android.InList(expected, cflags) {
9450 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9451 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009452 }
9453 })
9454 }
9455 })
9456 }
9457}
9458
Colin Crossc33e5212021-05-25 18:16:02 -07009459func TestApexJavaCoverage(t *testing.T) {
9460 bp := `
9461 apex {
9462 name: "myapex",
9463 key: "myapex.key",
9464 java_libs: ["mylib"],
9465 bootclasspath_fragments: ["mybootclasspathfragment"],
9466 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9467 updatable: false,
9468 }
9469
9470 apex_key {
9471 name: "myapex.key",
9472 public_key: "testkey.avbpubkey",
9473 private_key: "testkey.pem",
9474 }
9475
9476 java_library {
9477 name: "mylib",
9478 srcs: ["mylib.java"],
9479 apex_available: ["myapex"],
9480 compile_dex: true,
9481 }
9482
9483 bootclasspath_fragment {
9484 name: "mybootclasspathfragment",
9485 contents: ["mybootclasspathlib"],
9486 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009487 hidden_api: {
9488 split_packages: ["*"],
9489 },
Colin Crossc33e5212021-05-25 18:16:02 -07009490 }
9491
9492 java_library {
9493 name: "mybootclasspathlib",
9494 srcs: ["mybootclasspathlib.java"],
9495 apex_available: ["myapex"],
9496 compile_dex: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00009497 sdk_version: "current",
Colin Crossc33e5212021-05-25 18:16:02 -07009498 }
9499
9500 systemserverclasspath_fragment {
9501 name: "mysystemserverclasspathfragment",
9502 contents: ["mysystemserverclasspathlib"],
9503 apex_available: ["myapex"],
9504 }
9505
9506 java_library {
9507 name: "mysystemserverclasspathlib",
9508 srcs: ["mysystemserverclasspathlib.java"],
9509 apex_available: ["myapex"],
9510 compile_dex: true,
9511 }
9512 `
9513
9514 result := android.GroupFixturePreparers(
9515 PrepareForTestWithApexBuildComponents,
9516 prepareForTestWithMyapex,
9517 java.PrepareForTestWithJavaDefaultModules,
9518 android.PrepareForTestWithAndroidBuildComponents,
9519 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009520 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9521 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009522 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009523 ).RunTest(t)
9524
9525 // Make sure jacoco ran on both mylib and mybootclasspathlib
9526 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9527 t.Errorf("Failed to find jacoco rule for mylib")
9528 }
9529 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9530 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9531 }
9532 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9533 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9534 }
9535}
9536
Jiyong Park192600a2021-08-03 07:52:17 +00009537func TestProhibitStaticExecutable(t *testing.T) {
9538 testApexError(t, `executable mybin is static`, `
9539 apex {
9540 name: "myapex",
9541 key: "myapex.key",
9542 binaries: ["mybin"],
9543 min_sdk_version: "29",
9544 }
9545
9546 apex_key {
9547 name: "myapex.key",
9548 public_key: "testkey.avbpubkey",
9549 private_key: "testkey.pem",
9550 }
9551
9552 cc_binary {
9553 name: "mybin",
9554 srcs: ["mylib.cpp"],
9555 relative_install_path: "foo/bar",
9556 static_executable: true,
9557 system_shared_libs: [],
9558 stl: "none",
9559 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009560 min_sdk_version: "29",
9561 }
9562 `)
9563
9564 testApexError(t, `executable mybin.rust is static`, `
9565 apex {
9566 name: "myapex",
9567 key: "myapex.key",
9568 binaries: ["mybin.rust"],
9569 min_sdk_version: "29",
9570 }
9571
9572 apex_key {
9573 name: "myapex.key",
9574 public_key: "testkey.avbpubkey",
9575 private_key: "testkey.pem",
9576 }
9577
9578 rust_binary {
9579 name: "mybin.rust",
9580 srcs: ["foo.rs"],
9581 static_executable: true,
9582 apex_available: ["myapex"],
9583 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009584 }
9585 `)
9586}
9587
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009588func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9589 ctx := testApex(t, `
9590 apex {
9591 name: "myapex",
9592 key: "myapex.key",
9593 updatable: false,
9594 java_libs: ["foo"],
9595 }
9596
9597 apex_key {
9598 name: "myapex.key",
9599 public_key: "testkey.avbpubkey",
9600 private_key: "testkey.pem",
9601 }
9602
9603 java_library {
9604 name: "foo",
9605 srcs: ["foo.java"],
9606 apex_available: ["myapex"],
9607 installable: true,
9608 }
9609 `,
9610 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9611 )
9612
Jooyung Hana0503a52023-08-23 13:12:50 +09009613 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009614 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9615 var builder strings.Builder
9616 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9617 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009618 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 +00009619}
9620
9621func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9622 ctx := testApex(t, `
9623 prebuilt_apex {
9624 name: "myapex",
9625 arch: {
9626 arm64: {
9627 src: "myapex-arm64.apex",
9628 },
9629 arm: {
9630 src: "myapex-arm.apex",
9631 },
9632 },
9633 exported_java_libs: ["foo"],
9634 }
9635
9636 java_import {
9637 name: "foo",
9638 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009639 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009640 }
9641 `,
9642 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9643 )
9644
9645 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9646 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9647 mainModuleEntries := entriesList[0]
9648 android.AssertArrayString(t,
9649 "LOCAL_REQUIRED_MODULES",
9650 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9651 []string{
9652 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9653 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9654 })
9655}
9656
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009657func TestAndroidMk_RequiredModules(t *testing.T) {
9658 ctx := testApex(t, `
9659 apex {
9660 name: "myapex",
9661 key: "myapex.key",
9662 updatable: false,
9663 java_libs: ["foo"],
9664 required: ["otherapex"],
9665 }
9666
9667 apex {
9668 name: "otherapex",
9669 key: "myapex.key",
9670 updatable: false,
9671 java_libs: ["foo"],
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009672 }
9673
9674 apex_key {
9675 name: "myapex.key",
9676 public_key: "testkey.avbpubkey",
9677 private_key: "testkey.pem",
9678 }
9679
9680 java_library {
9681 name: "foo",
9682 srcs: ["foo.java"],
9683 apex_available: ["myapex", "otherapex"],
9684 installable: true,
9685 }
9686 `)
9687
Jooyung Hana0503a52023-08-23 13:12:50 +09009688 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009689 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9690 var builder strings.Builder
9691 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9692 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009693 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009694}
9695
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009696func TestAndroidMk_RequiredDeps(t *testing.T) {
9697 ctx := testApex(t, `
9698 apex {
9699 name: "myapex",
9700 key: "myapex.key",
9701 updatable: false,
9702 }
9703
9704 apex_key {
9705 name: "myapex.key",
9706 public_key: "testkey.avbpubkey",
9707 private_key: "testkey.pem",
9708 }
9709 `)
9710
Jooyung Hana0503a52023-08-23 13:12:50 +09009711 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009712 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009713 data := android.AndroidMkDataForTest(t, ctx, bundle)
9714 var builder strings.Builder
9715 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9716 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009717 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009718}
9719
Jooyung Hana6d36672022-02-24 13:58:07 +09009720func TestApexOutputFileProducer(t *testing.T) {
9721 for _, tc := range []struct {
9722 name string
9723 ref string
9724 expected_data []string
9725 }{
9726 {
9727 name: "test_using_output",
9728 ref: ":myapex",
Jooyung Hana0503a52023-08-23 13:12:50 +09009729 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.capex:myapex.capex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009730 },
9731 {
9732 name: "test_using_apex",
9733 ref: ":myapex{.apex}",
Jooyung Hana0503a52023-08-23 13:12:50 +09009734 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.apex:myapex.apex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009735 },
9736 } {
9737 t.Run(tc.name, func(t *testing.T) {
9738 ctx := testApex(t, `
9739 apex {
9740 name: "myapex",
9741 key: "myapex.key",
9742 compressible: true,
9743 updatable: false,
9744 }
9745
9746 apex_key {
9747 name: "myapex.key",
9748 public_key: "testkey.avbpubkey",
9749 private_key: "testkey.pem",
9750 }
9751
9752 java_test {
9753 name: "`+tc.name+`",
9754 srcs: ["a.java"],
9755 data: ["`+tc.ref+`"],
9756 }
9757 `,
9758 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9759 variables.CompressedApex = proptools.BoolPtr(true)
9760 }))
9761 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9762 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9763 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9764 })
9765 }
9766}
9767
satayev758968a2021-12-06 11:42:40 +00009768func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9769 preparer := android.GroupFixturePreparers(
9770 PrepareForTestWithApexBuildComponents,
9771 prepareForTestWithMyapex,
9772 java.PrepareForTestWithJavaSdkLibraryFiles,
9773 java.PrepareForTestWithJavaDefaultModules,
9774 android.PrepareForTestWithAndroidBuildComponents,
9775 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9776 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9777 )
9778
9779 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9780 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9781 preparer.RunTestWithBp(t, `
9782 apex {
9783 name: "myapex",
9784 key: "myapex.key",
9785 bootclasspath_fragments: ["mybootclasspathfragment"],
9786 min_sdk_version: "30",
9787 updatable: false,
9788 }
9789
9790 apex_key {
9791 name: "myapex.key",
9792 public_key: "testkey.avbpubkey",
9793 private_key: "testkey.pem",
9794 }
9795
9796 bootclasspath_fragment {
9797 name: "mybootclasspathfragment",
9798 contents: ["mybootclasspathlib"],
9799 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009800 hidden_api: {
9801 split_packages: ["*"],
9802 },
satayev758968a2021-12-06 11:42:40 +00009803 }
9804
9805 java_sdk_library {
9806 name: "mybootclasspathlib",
9807 srcs: ["mybootclasspathlib.java"],
9808 apex_available: ["myapex"],
9809 compile_dex: true,
9810 unsafe_ignore_missing_latest_api: true,
9811 min_sdk_version: "31",
9812 static_libs: ["util"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00009813 sdk_version: "core_current",
satayev758968a2021-12-06 11:42:40 +00009814 }
9815
9816 java_library {
9817 name: "util",
9818 srcs: ["a.java"],
9819 apex_available: ["myapex"],
9820 min_sdk_version: "31",
9821 static_libs: ["another_util"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00009822 sdk_version: "core_current",
satayev758968a2021-12-06 11:42:40 +00009823 }
9824
9825 java_library {
9826 name: "another_util",
9827 srcs: ["a.java"],
9828 min_sdk_version: "31",
9829 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00009830 sdk_version: "core_current",
satayev758968a2021-12-06 11:42:40 +00009831 }
9832 `)
9833 })
9834
9835 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9836 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9837 preparer.RunTestWithBp(t, `
9838 apex {
9839 name: "myapex",
9840 key: "myapex.key",
9841 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9842 min_sdk_version: "30",
9843 updatable: false,
9844 }
9845
9846 apex_key {
9847 name: "myapex.key",
9848 public_key: "testkey.avbpubkey",
9849 private_key: "testkey.pem",
9850 }
9851
9852 systemserverclasspath_fragment {
9853 name: "mysystemserverclasspathfragment",
9854 contents: ["mysystemserverclasspathlib"],
9855 apex_available: ["myapex"],
9856 }
9857
9858 java_sdk_library {
9859 name: "mysystemserverclasspathlib",
9860 srcs: ["mysystemserverclasspathlib.java"],
9861 apex_available: ["myapex"],
9862 compile_dex: true,
9863 min_sdk_version: "32",
9864 unsafe_ignore_missing_latest_api: true,
9865 static_libs: ["util"],
9866 }
9867
9868 java_library {
9869 name: "util",
9870 srcs: ["a.java"],
9871 apex_available: ["myapex"],
9872 min_sdk_version: "31",
9873 static_libs: ["another_util"],
9874 }
9875
9876 java_library {
9877 name: "another_util",
9878 srcs: ["a.java"],
9879 min_sdk_version: "31",
9880 apex_available: ["myapex"],
9881 }
9882 `)
9883 })
9884
9885 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
Jihoon Kang85bc1932024-07-01 17:04:46 +00009886 preparer.
satayev758968a2021-12-06 11:42:40 +00009887 RunTestWithBp(t, `
9888 apex {
9889 name: "myapex",
9890 key: "myapex.key",
9891 bootclasspath_fragments: ["mybootclasspathfragment"],
9892 min_sdk_version: "30",
9893 updatable: false,
9894 }
9895
9896 apex_key {
9897 name: "myapex.key",
9898 public_key: "testkey.avbpubkey",
9899 private_key: "testkey.pem",
9900 }
9901
9902 bootclasspath_fragment {
9903 name: "mybootclasspathfragment",
9904 contents: ["mybootclasspathlib"],
9905 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009906 hidden_api: {
9907 split_packages: ["*"],
9908 },
satayev758968a2021-12-06 11:42:40 +00009909 }
9910
9911 java_sdk_library {
9912 name: "mybootclasspathlib",
9913 srcs: ["mybootclasspathlib.java"],
9914 apex_available: ["myapex"],
9915 compile_dex: true,
9916 unsafe_ignore_missing_latest_api: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00009917 sdk_version: "current",
9918 min_sdk_version: "30",
satayev758968a2021-12-06 11:42:40 +00009919 }
9920 `)
9921 })
9922
9923 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9924 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9925 RunTestWithBp(t, `
9926 apex {
9927 name: "myapex",
9928 key: "myapex.key",
9929 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9930 min_sdk_version: "30",
9931 updatable: false,
9932 }
9933
9934 apex_key {
9935 name: "myapex.key",
9936 public_key: "testkey.avbpubkey",
9937 private_key: "testkey.pem",
9938 }
9939
9940 systemserverclasspath_fragment {
9941 name: "mysystemserverclasspathfragment",
9942 contents: ["mysystemserverclasspathlib"],
9943 apex_available: ["myapex"],
9944 }
9945
9946 java_sdk_library {
9947 name: "mysystemserverclasspathlib",
9948 srcs: ["mysystemserverclasspathlib.java"],
9949 apex_available: ["myapex"],
9950 compile_dex: true,
9951 unsafe_ignore_missing_latest_api: true,
9952 }
9953 `)
9954 })
9955}
9956
Jiakai Zhang6decef92022-01-12 17:56:19 +00009957// Verifies that the APEX depends on all the Make modules in the list.
9958func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9959 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9960 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009961 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009962 }
9963}
9964
9965// Verifies that the APEX does not depend on any of the Make modules in the list.
9966func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9967 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9968 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009969 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009970 }
9971}
9972
Cole Faust24e25c02024-01-19 14:12:17 -08009973func TestApexStrictUpdtabilityLint(t *testing.T) {
9974 bpTemplate := `
9975 apex {
9976 name: "myapex",
9977 key: "myapex.key",
9978 java_libs: ["myjavalib"],
9979 updatable: %v,
9980 min_sdk_version: "29",
9981 }
9982 apex_key {
9983 name: "myapex.key",
9984 }
9985 java_library {
9986 name: "myjavalib",
9987 srcs: ["MyClass.java"],
9988 apex_available: [ "myapex" ],
9989 lint: {
9990 strict_updatability_linting: %v,
9991 %s
9992 },
9993 sdk_version: "current",
9994 min_sdk_version: "29",
9995 }
9996 `
9997 fs := android.MockFS{
9998 "lint-baseline.xml": nil,
9999 }
10000
10001 testCases := []struct {
10002 testCaseName string
10003 apexUpdatable bool
10004 javaStrictUpdtabilityLint bool
10005 lintFileExists bool
10006 disallowedFlagExpected bool
10007 }{
10008 {
10009 testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
10010 apexUpdatable: true,
10011 javaStrictUpdtabilityLint: true,
10012 lintFileExists: false,
10013 disallowedFlagExpected: false,
10014 },
10015 {
10016 testCaseName: "non-updatable apex respects strict_updatability of javalib",
10017 apexUpdatable: false,
10018 javaStrictUpdtabilityLint: false,
10019 lintFileExists: true,
10020 disallowedFlagExpected: false,
10021 },
10022 {
10023 testCaseName: "non-updatable apex respects strict updatability of javalib",
10024 apexUpdatable: false,
10025 javaStrictUpdtabilityLint: true,
10026 lintFileExists: true,
10027 disallowedFlagExpected: true,
10028 },
10029 {
10030 testCaseName: "updatable apex sets strict updatability of javalib to true",
10031 apexUpdatable: true,
10032 javaStrictUpdtabilityLint: false, // will be set to true by mutator
10033 lintFileExists: true,
10034 disallowedFlagExpected: true,
10035 },
10036 }
10037
10038 for _, testCase := range testCases {
10039 fixtures := []android.FixturePreparer{}
10040 baselineProperty := ""
10041 if testCase.lintFileExists {
10042 fixtures = append(fixtures, fs.AddToFixture())
10043 baselineProperty = "baseline_filename: \"lint-baseline.xml\""
10044 }
10045 bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint, baselineProperty)
10046
10047 result := testApex(t, bp, fixtures...)
10048 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
10049 sboxProto := android.RuleBuilderSboxProtoForTests(t, result, myjavalib.Output("lint.sbox.textproto"))
10050 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
10051
10052 if disallowedFlagActual != testCase.disallowedFlagExpected {
10053 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
10054 }
10055 }
10056}
10057
10058func TestUpdatabilityLintSkipLibcore(t *testing.T) {
10059 bp := `
10060 apex {
10061 name: "myapex",
10062 key: "myapex.key",
10063 java_libs: ["myjavalib"],
10064 updatable: true,
10065 min_sdk_version: "29",
10066 }
10067 apex_key {
10068 name: "myapex.key",
10069 }
10070 java_library {
10071 name: "myjavalib",
10072 srcs: ["MyClass.java"],
10073 apex_available: [ "myapex" ],
10074 sdk_version: "current",
10075 min_sdk_version: "29",
10076 lint: {
10077 baseline_filename: "lint-baseline.xml",
10078 }
10079 }
10080 `
10081
10082 testCases := []struct {
10083 testCaseName string
10084 moduleDirectory string
10085 disallowedFlagExpected bool
10086 }{
10087 {
10088 testCaseName: "lintable module defined outside libcore",
10089 moduleDirectory: "",
10090 disallowedFlagExpected: true,
10091 },
10092 {
10093 testCaseName: "lintable module defined in libcore root directory",
10094 moduleDirectory: "libcore/",
10095 disallowedFlagExpected: false,
10096 },
10097 {
10098 testCaseName: "lintable module defined in libcore child directory",
10099 moduleDirectory: "libcore/childdir/",
10100 disallowedFlagExpected: true,
10101 },
10102 }
10103
10104 for _, testCase := range testCases {
10105 lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
10106 bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
10107 result := testApex(t, "", lintFileCreator, bpFileCreator)
10108 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
10109 sboxProto := android.RuleBuilderSboxProtoForTests(t, result, myjavalib.Output("lint.sbox.textproto"))
10110 cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
10111 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
10112
10113 if disallowedFlagActual != testCase.disallowedFlagExpected {
10114 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
10115 }
10116 }
10117}
10118
10119// checks transtive deps of an apex coming from bootclasspath_fragment
10120func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
10121 bp := `
10122 apex {
10123 name: "myapex",
10124 key: "myapex.key",
10125 bootclasspath_fragments: ["mybootclasspathfragment"],
10126 updatable: true,
10127 min_sdk_version: "29",
10128 }
10129 apex_key {
10130 name: "myapex.key",
10131 }
10132 bootclasspath_fragment {
10133 name: "mybootclasspathfragment",
10134 contents: ["myjavalib"],
10135 apex_available: ["myapex"],
10136 hidden_api: {
10137 split_packages: ["*"],
10138 },
10139 }
10140 java_library {
10141 name: "myjavalib",
10142 srcs: ["MyClass.java"],
10143 apex_available: [ "myapex" ],
10144 sdk_version: "current",
10145 min_sdk_version: "29",
10146 compile_dex: true,
10147 lint: {
10148 baseline_filename: "lint-baseline.xml",
10149 }
10150 }
10151 `
10152 fs := android.MockFS{
10153 "lint-baseline.xml": nil,
10154 }
10155
10156 result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
10157 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
10158 sboxProto := android.RuleBuilderSboxProtoForTests(t, result, myjavalib.Output("lint.sbox.textproto"))
10159 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
10160 t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
10161 }
10162}
Spandan Das66773252022-01-15 00:23:18 +000010163
Jihoon Kang784c0052024-06-25 22:15:39 +000010164func TestApexLintBcpFragmentSdkLibDeps(t *testing.T) {
10165 bp := `
10166 apex {
10167 name: "myapex",
10168 key: "myapex.key",
10169 bootclasspath_fragments: ["mybootclasspathfragment"],
10170 min_sdk_version: "29",
Jihoon Kang85bc1932024-07-01 17:04:46 +000010171 java_libs: [
10172 "jacocoagent",
10173 ],
Jihoon Kang784c0052024-06-25 22:15:39 +000010174 }
10175 apex_key {
10176 name: "myapex.key",
10177 }
10178 bootclasspath_fragment {
10179 name: "mybootclasspathfragment",
10180 contents: ["foo"],
10181 apex_available: ["myapex"],
10182 hidden_api: {
10183 split_packages: ["*"],
10184 },
10185 }
10186 java_sdk_library {
10187 name: "foo",
10188 srcs: ["MyClass.java"],
10189 apex_available: [ "myapex" ],
10190 sdk_version: "current",
10191 min_sdk_version: "29",
10192 compile_dex: true,
10193 }
10194 `
10195 fs := android.MockFS{
10196 "lint-baseline.xml": nil,
10197 }
10198
10199 result := android.GroupFixturePreparers(
10200 prepareForApexTest,
10201 java.PrepareForTestWithJavaSdkLibraryFiles,
10202 java.PrepareForTestWithJacocoInstrumentation,
10203 java.FixtureWithLastReleaseApis("foo"),
Jihoon Kang784c0052024-06-25 22:15:39 +000010204 android.FixtureMergeMockFs(fs),
10205 ).RunTestWithBp(t, bp)
10206
10207 myapex := result.ModuleForTests("myapex", "android_common_myapex")
10208 lintReportInputs := strings.Join(myapex.Output("lint-report-xml.zip").Inputs.Strings(), " ")
10209 android.AssertStringDoesContain(t,
10210 "myapex lint report expected to contain that of the sdk library impl lib as an input",
10211 lintReportInputs, "foo.impl")
10212}
10213
Spandan Das42e89502022-05-06 22:12:55 +000010214// updatable apexes should propagate updatable=true to its apps
10215func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
10216 bp := `
10217 apex {
10218 name: "myapex",
10219 key: "myapex.key",
10220 updatable: %v,
10221 apps: [
10222 "myapp",
10223 ],
10224 min_sdk_version: "30",
10225 }
10226 apex_key {
10227 name: "myapex.key",
10228 }
10229 android_app {
10230 name: "myapp",
10231 updatable: %v,
10232 apex_available: [
10233 "myapex",
10234 ],
10235 sdk_version: "current",
10236 min_sdk_version: "30",
10237 }
10238 `
10239 testCases := []struct {
10240 name string
10241 apex_is_updatable_bp bool
10242 app_is_updatable_bp bool
10243 app_is_updatable_expected bool
10244 }{
10245 {
10246 name: "Non-updatable apex respects updatable property of non-updatable app",
10247 apex_is_updatable_bp: false,
10248 app_is_updatable_bp: false,
10249 app_is_updatable_expected: false,
10250 },
10251 {
10252 name: "Non-updatable apex respects updatable property of updatable app",
10253 apex_is_updatable_bp: false,
10254 app_is_updatable_bp: true,
10255 app_is_updatable_expected: true,
10256 },
10257 {
10258 name: "Updatable apex respects updatable property of updatable app",
10259 apex_is_updatable_bp: true,
10260 app_is_updatable_bp: true,
10261 app_is_updatable_expected: true,
10262 },
10263 {
10264 name: "Updatable apex sets updatable=true on non-updatable app",
10265 apex_is_updatable_bp: true,
10266 app_is_updatable_bp: false,
10267 app_is_updatable_expected: true,
10268 },
10269 }
10270 for _, testCase := range testCases {
10271 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
10272 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
10273 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
10274 }
10275}
10276
Dennis Shend4f5d932023-01-31 20:27:21 +000010277func TestTrimmedApex(t *testing.T) {
10278 bp := `
10279 apex {
10280 name: "myapex",
10281 key: "myapex.key",
10282 native_shared_libs: ["libfoo","libbaz"],
10283 min_sdk_version: "29",
10284 trim_against: "mydcla",
10285 }
10286 apex {
10287 name: "mydcla",
10288 key: "myapex.key",
10289 native_shared_libs: ["libfoo","libbar"],
10290 min_sdk_version: "29",
10291 file_contexts: ":myapex-file_contexts",
10292 dynamic_common_lib_apex: true,
10293 }
10294 apex_key {
10295 name: "myapex.key",
10296 }
10297 cc_library {
10298 name: "libfoo",
10299 shared_libs: ["libc"],
10300 apex_available: ["myapex","mydcla"],
10301 min_sdk_version: "29",
10302 }
10303 cc_library {
10304 name: "libbar",
10305 shared_libs: ["libc"],
10306 apex_available: ["myapex","mydcla"],
10307 min_sdk_version: "29",
10308 }
10309 cc_library {
10310 name: "libbaz",
10311 shared_libs: ["libc"],
10312 apex_available: ["myapex","mydcla"],
10313 min_sdk_version: "29",
10314 }
Dennis Shend4f5d932023-01-31 20:27:21 +000010315 `
10316 ctx := testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +090010317 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dennis Shend4f5d932023-01-31 20:27:21 +000010318 apexRule := module.MaybeRule("apexRule")
10319 if apexRule.Rule == nil {
10320 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10321 }
10322
10323 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
Jooyung Hana0503a52023-08-23 13:12:50 +090010324 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("TrimmedApexRule")
Dennis Shend4f5d932023-01-31 20:27:21 +000010325 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10326 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10327 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10328 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10329}
Jingwen Chendea7a642023-03-28 11:30:50 +000010330
10331func TestCannedFsConfig(t *testing.T) {
10332 ctx := testApex(t, `
10333 apex {
10334 name: "myapex",
10335 key: "myapex.key",
10336 updatable: false,
10337 }
10338
10339 apex_key {
10340 name: "myapex.key",
10341 public_key: "testkey.avbpubkey",
10342 private_key: "testkey.pem",
10343 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010344 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010345 generateFsRule := mod.Rule("generateFsConfig")
10346 cmd := generateFsRule.RuleParams.Command
10347
10348 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10349}
10350
10351func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10352 ctx := testApex(t, `
10353 apex {
10354 name: "myapex",
10355 key: "myapex.key",
10356 canned_fs_config: "my_config",
10357 updatable: false,
10358 }
10359
10360 apex_key {
10361 name: "myapex.key",
10362 public_key: "testkey.avbpubkey",
10363 private_key: "testkey.pem",
10364 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010365 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010366 generateFsRule := mod.Rule("generateFsConfig")
10367 cmd := generateFsRule.RuleParams.Command
10368
10369 // Ensure that canned_fs_config has "cat my_config" at the end
10370 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10371}
Spandan Das20fce2d2023-04-12 17:21:39 +000010372
10373func TestStubLibrariesMultipleApexViolation(t *testing.T) {
10374 testCases := []struct {
10375 desc string
10376 hasStubs bool
10377 apexAvailable string
10378 expectedError string
10379 }{
10380 {
10381 desc: "non-stub library can have multiple apex_available",
10382 hasStubs: false,
10383 apexAvailable: `["myapex", "otherapex"]`,
10384 },
10385 {
10386 desc: "stub library should not be available to anyapex",
10387 hasStubs: true,
10388 apexAvailable: `["//apex_available:anyapex"]`,
10389 expectedError: "Stub libraries should have a single apex_available.*anyapex",
10390 },
10391 {
10392 desc: "stub library should not be available to multiple apexes",
10393 hasStubs: true,
10394 apexAvailable: `["myapex", "otherapex"]`,
10395 expectedError: "Stub libraries should have a single apex_available.*myapex.*otherapex",
10396 },
10397 {
10398 desc: "stub library can be available to a core apex and a test apex",
10399 hasStubs: true,
10400 apexAvailable: `["myapex", "test_myapex"]`,
10401 },
10402 }
10403 bpTemplate := `
10404 cc_library {
10405 name: "libfoo",
10406 %v
10407 apex_available: %v,
10408 }
10409 apex {
10410 name: "myapex",
10411 key: "apex.key",
10412 updatable: false,
10413 native_shared_libs: ["libfoo"],
10414 }
10415 apex {
10416 name: "otherapex",
10417 key: "apex.key",
10418 updatable: false,
10419 }
10420 apex_test {
10421 name: "test_myapex",
10422 key: "apex.key",
10423 updatable: false,
10424 native_shared_libs: ["libfoo"],
10425 }
10426 apex_key {
10427 name: "apex.key",
10428 }
10429 `
10430 for _, tc := range testCases {
10431 stubs := ""
10432 if tc.hasStubs {
10433 stubs = `stubs: {symbol_file: "libfoo.map.txt"},`
10434 }
10435 bp := fmt.Sprintf(bpTemplate, stubs, tc.apexAvailable)
10436 mockFsFixturePreparer := android.FixtureModifyMockFS(func(fs android.MockFS) {
10437 fs["system/sepolicy/apex/test_myapex-file_contexts"] = nil
10438 })
10439 if tc.expectedError == "" {
10440 testApex(t, bp, mockFsFixturePreparer)
10441 } else {
10442 testApexError(t, tc.expectedError, bp, mockFsFixturePreparer)
10443 }
10444 }
10445}
Colin Crossbd3a16b2023-04-25 11:30:51 -070010446
10447func TestFileSystemShouldSkipApexLibraries(t *testing.T) {
10448 context := android.GroupFixturePreparers(
10449 android.PrepareForIntegrationTestWithAndroid,
10450 cc.PrepareForIntegrationTestWithCc,
10451 PrepareForTestWithApexBuildComponents,
10452 prepareForTestWithMyapex,
10453 filesystem.PrepareForTestWithFilesystemBuildComponents,
10454 )
10455 result := context.RunTestWithBp(t, `
10456 android_system_image {
10457 name: "myfilesystem",
10458 deps: [
10459 "libfoo",
10460 ],
10461 linker_config_src: "linker.config.json",
10462 }
10463
10464 cc_library {
10465 name: "libfoo",
10466 shared_libs: [
10467 "libbar",
10468 ],
10469 stl: "none",
10470 }
10471
10472 cc_library {
10473 name: "libbar",
10474 stl: "none",
10475 apex_available: ["myapex"],
10476 }
10477
10478 apex {
10479 name: "myapex",
10480 native_shared_libs: ["libbar"],
10481 key: "myapex.key",
10482 updatable: false,
10483 }
10484
10485 apex_key {
10486 name: "myapex.key",
10487 public_key: "testkey.avbpubkey",
10488 private_key: "testkey.pem",
10489 }
10490 `)
10491
Cole Faust3b806d32024-03-11 15:15:03 -070010492 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits
Colin Crossbd3a16b2023-04-25 11:30:51 -070010493 android.AssertStringListDoesNotContain(t, "filesystem should not have libbar",
10494 inputs.Strings(),
10495 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
10496}
Yu Liueae7b362023-11-16 17:05:47 -080010497
10498var apex_default_bp = `
10499 apex_key {
10500 name: "myapex.key",
10501 public_key: "testkey.avbpubkey",
10502 private_key: "testkey.pem",
10503 }
10504
10505 filegroup {
10506 name: "myapex.manifest",
10507 srcs: ["apex_manifest.json"],
10508 }
10509
10510 filegroup {
10511 name: "myapex.androidmanifest",
10512 srcs: ["AndroidManifest.xml"],
10513 }
10514`
10515
10516func TestAconfigFilesJavaDeps(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 java_libs: [
10524 "my_java_library_foo",
10525 "my_java_library_bar",
10526 ],
10527 updatable: false,
10528 }
10529
10530 java_library {
10531 name: "my_java_library_foo",
10532 srcs: ["foo/bar/MyClass.java"],
10533 sdk_version: "none",
10534 system_modules: "none",
10535 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010536 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010537 "myapex",
10538 ],
10539 }
10540
10541 java_library {
10542 name: "my_java_library_bar",
10543 srcs: ["foo/bar/MyClass.java"],
10544 sdk_version: "none",
10545 system_modules: "none",
10546 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010547 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010548 "myapex",
10549 ],
10550 }
10551
10552 aconfig_declarations {
10553 name: "my_aconfig_declarations_foo",
10554 package: "com.example.package",
10555 container: "myapex",
10556 srcs: ["foo.aconfig"],
10557 }
10558
10559 java_aconfig_library {
10560 name: "my_java_aconfig_library_foo",
10561 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010562 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010563 "myapex",
10564 ],
10565 }
10566
10567 aconfig_declarations {
10568 name: "my_aconfig_declarations_bar",
10569 package: "com.example.package",
10570 container: "myapex",
10571 srcs: ["bar.aconfig"],
10572 }
10573
10574 java_aconfig_library {
10575 name: "my_java_aconfig_library_bar",
10576 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010577 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010578 "myapex",
10579 ],
10580 }
10581 `)
10582
10583 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10584 s := mod.Rule("apexRule").Args["copy_commands"]
10585 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jooyung Hana3fddf42024-09-03 13:22:21 +090010586 if len(copyCmds) != 12 {
10587 t.Fatalf("Expected 12 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010588 }
10589
Jooyung Hana3fddf42024-09-03 13:22:21 +090010590 ensureListContainsMatch(t, copyCmds, "^cp -f .*/aconfig_flags.pb .*/image.apex/etc/aconfig_flags.pb")
10591 ensureListContainsMatch(t, copyCmds, "^cp -f .*/package.map .*/image.apex/etc/package.map")
10592 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.map .*/image.apex/etc/flag.map")
10593 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.val .*/image.apex/etc/flag.val")
Yu Liueae7b362023-11-16 17:05:47 -080010594
Yu Liubba555e2024-02-17 00:36:42 +000010595 inputs := []string{
10596 "my_aconfig_declarations_foo/intermediate.pb",
10597 "my_aconfig_declarations_bar/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010598 }
Yu Liubba555e2024-02-17 00:36:42 +000010599 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10600 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10601 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10602 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010603}
10604
10605func TestAconfigFilesJavaAndCcDeps(t *testing.T) {
10606 ctx := testApex(t, apex_default_bp+`
10607 apex {
10608 name: "myapex",
10609 manifest: ":myapex.manifest",
10610 androidManifest: ":myapex.androidmanifest",
10611 key: "myapex.key",
10612 java_libs: [
10613 "my_java_library_foo",
10614 ],
10615 native_shared_libs: [
10616 "my_cc_library_bar",
10617 ],
10618 binaries: [
10619 "my_cc_binary_baz",
10620 ],
10621 updatable: false,
10622 }
10623
10624 java_library {
10625 name: "my_java_library_foo",
10626 srcs: ["foo/bar/MyClass.java"],
10627 sdk_version: "none",
10628 system_modules: "none",
10629 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010630 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010631 "myapex",
10632 ],
10633 }
10634
10635 cc_library {
10636 name: "my_cc_library_bar",
10637 srcs: ["foo/bar/MyClass.cc"],
Yu Liucec0e412023-11-30 16:45:50 -080010638 static_libs: [
10639 "my_cc_aconfig_library_bar",
10640 "my_cc_aconfig_library_baz",
10641 ],
Yu Liueae7b362023-11-16 17:05:47 -080010642 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010643 "myapex",
10644 ],
10645 }
10646
10647 cc_binary {
10648 name: "my_cc_binary_baz",
10649 srcs: ["foo/bar/MyClass.cc"],
10650 static_libs: ["my_cc_aconfig_library_baz"],
Yu Liueae7b362023-11-16 17:05:47 -080010651 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010652 "myapex",
10653 ],
10654 }
10655
10656 aconfig_declarations {
10657 name: "my_aconfig_declarations_foo",
10658 package: "com.example.package",
10659 container: "myapex",
10660 srcs: ["foo.aconfig"],
10661 }
10662
10663 java_aconfig_library {
10664 name: "my_java_aconfig_library_foo",
10665 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010666 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010667 "myapex",
10668 ],
10669 }
10670
10671 aconfig_declarations {
10672 name: "my_aconfig_declarations_bar",
10673 package: "com.example.package",
10674 container: "myapex",
10675 srcs: ["bar.aconfig"],
10676 }
10677
10678 cc_aconfig_library {
10679 name: "my_cc_aconfig_library_bar",
10680 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010681 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010682 "myapex",
10683 ],
10684 }
10685
10686 aconfig_declarations {
10687 name: "my_aconfig_declarations_baz",
10688 package: "com.example.package",
10689 container: "myapex",
10690 srcs: ["baz.aconfig"],
10691 }
10692
10693 cc_aconfig_library {
10694 name: "my_cc_aconfig_library_baz",
10695 aconfig_declarations: "my_aconfig_declarations_baz",
Yu Liueae7b362023-11-16 17:05:47 -080010696 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010697 "myapex",
10698 ],
10699 }
10700
10701 cc_library {
10702 name: "server_configurable_flags",
10703 srcs: ["server_configurable_flags.cc"],
10704 }
Ted Bauerf0f18592024-04-23 18:25:26 +000010705 cc_library {
10706 name: "libbase",
10707 srcs: ["libbase.cc"],
Ted Bauer1e96f8c2024-04-25 19:50:01 +000010708 apex_available: [
10709 "myapex",
10710 ],
Ted Bauerf0f18592024-04-23 18:25:26 +000010711 }
10712 cc_library {
10713 name: "libaconfig_storage_read_api_cc",
10714 srcs: ["libaconfig_storage_read_api_cc.cc"],
10715 }
Yu Liueae7b362023-11-16 17:05:47 -080010716 `)
10717
10718 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10719 s := mod.Rule("apexRule").Args["copy_commands"]
10720 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jooyung Hana3fddf42024-09-03 13:22:21 +090010721 if len(copyCmds) != 16 {
10722 t.Fatalf("Expected 16 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010723 }
10724
Jooyung Hana3fddf42024-09-03 13:22:21 +090010725 ensureListContainsMatch(t, copyCmds, "^cp -f .*/aconfig_flags.pb .*/image.apex/etc/aconfig_flags.pb")
10726 ensureListContainsMatch(t, copyCmds, "^cp -f .*/package.map .*/image.apex/etc/package.map")
10727 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.map .*/image.apex/etc/flag.map")
10728 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.val .*/image.apex/etc/flag.val")
Yu Liueae7b362023-11-16 17:05:47 -080010729
Yu Liubba555e2024-02-17 00:36:42 +000010730 inputs := []string{
10731 "my_aconfig_declarations_foo/intermediate.pb",
10732 "my_cc_library_bar/android_arm64_armv8-a_shared_apex10000/myapex/aconfig_merged.pb",
10733 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010734 }
Yu Liubba555e2024-02-17 00:36:42 +000010735 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10736 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10737 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10738 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010739}
10740
Yu Liucec0e412023-11-30 16:45:50 -080010741func TestAconfigFilesRustDeps(t *testing.T) {
10742 ctx := testApex(t, apex_default_bp+`
10743 apex {
10744 name: "myapex",
10745 manifest: ":myapex.manifest",
10746 androidManifest: ":myapex.androidmanifest",
10747 key: "myapex.key",
10748 native_shared_libs: [
10749 "libmy_rust_library",
10750 ],
10751 binaries: [
10752 "my_rust_binary",
10753 ],
10754 rust_dyn_libs: [
10755 "libmy_rust_dylib",
10756 ],
10757 updatable: false,
10758 }
10759
10760 rust_library {
10761 name: "libflags_rust", // test mock
10762 crate_name: "flags_rust",
10763 srcs: ["lib.rs"],
10764 apex_available: [
10765 "myapex",
10766 ],
10767 }
10768
10769 rust_library {
10770 name: "liblazy_static", // test mock
10771 crate_name: "lazy_static",
10772 srcs: ["src/lib.rs"],
10773 apex_available: [
10774 "myapex",
10775 ],
10776 }
10777
Ted Bauer02d475c2024-03-27 20:56:26 +000010778 rust_library {
10779 name: "libaconfig_storage_read_api", // test mock
10780 crate_name: "aconfig_storage_read_api",
10781 srcs: ["src/lib.rs"],
10782 apex_available: [
10783 "myapex",
10784 ],
10785 }
10786
Ted Bauer6ef40db2024-03-29 14:04:10 +000010787 rust_library {
10788 name: "liblogger", // test mock
10789 crate_name: "logger",
10790 srcs: ["src/lib.rs"],
10791 apex_available: [
10792 "myapex",
10793 ],
10794 }
10795
10796 rust_library {
10797 name: "liblog_rust", // test mock
10798 crate_name: "log_rust",
10799 srcs: ["src/lib.rs"],
10800 apex_available: [
10801 "myapex",
10802 ],
10803 }
10804
Yu Liucec0e412023-11-30 16:45:50 -080010805 rust_ffi_shared {
10806 name: "libmy_rust_library",
10807 srcs: ["src/lib.rs"],
10808 rustlibs: ["libmy_rust_aconfig_library_foo"],
10809 crate_name: "my_rust_library",
10810 apex_available: [
10811 "myapex",
10812 ],
10813 }
10814
10815 rust_library_dylib {
10816 name: "libmy_rust_dylib",
10817 srcs: ["foo/bar/MyClass.rs"],
10818 rustlibs: ["libmy_rust_aconfig_library_bar"],
10819 crate_name: "my_rust_dylib",
10820 apex_available: [
10821 "myapex",
10822 ],
10823 }
10824
10825 rust_binary {
10826 name: "my_rust_binary",
10827 srcs: ["foo/bar/MyClass.rs"],
10828 rustlibs: [
10829 "libmy_rust_aconfig_library_baz",
10830 "libmy_rust_dylib",
10831 ],
10832 apex_available: [
10833 "myapex",
10834 ],
10835 }
10836
10837 aconfig_declarations {
10838 name: "my_aconfig_declarations_foo",
10839 package: "com.example.package",
10840 container: "myapex",
10841 srcs: ["foo.aconfig"],
10842 }
10843
10844 aconfig_declarations {
10845 name: "my_aconfig_declarations_bar",
10846 package: "com.example.package",
10847 container: "myapex",
10848 srcs: ["bar.aconfig"],
10849 }
10850
10851 aconfig_declarations {
10852 name: "my_aconfig_declarations_baz",
10853 package: "com.example.package",
10854 container: "myapex",
10855 srcs: ["baz.aconfig"],
10856 }
10857
10858 rust_aconfig_library {
10859 name: "libmy_rust_aconfig_library_foo",
10860 aconfig_declarations: "my_aconfig_declarations_foo",
10861 crate_name: "my_rust_aconfig_library_foo",
10862 apex_available: [
10863 "myapex",
10864 ],
10865 }
10866
10867 rust_aconfig_library {
10868 name: "libmy_rust_aconfig_library_bar",
10869 aconfig_declarations: "my_aconfig_declarations_bar",
10870 crate_name: "my_rust_aconfig_library_bar",
10871 apex_available: [
10872 "myapex",
10873 ],
10874 }
10875
10876 rust_aconfig_library {
10877 name: "libmy_rust_aconfig_library_baz",
10878 aconfig_declarations: "my_aconfig_declarations_baz",
10879 crate_name: "my_rust_aconfig_library_baz",
10880 apex_available: [
10881 "myapex",
10882 ],
10883 }
10884 `)
10885
10886 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10887 s := mod.Rule("apexRule").Args["copy_commands"]
10888 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jooyung Hana3fddf42024-09-03 13:22:21 +090010889 if len(copyCmds) != 36 {
10890 t.Fatalf("Expected 36 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liucec0e412023-11-30 16:45:50 -080010891 }
10892
Jooyung Hana3fddf42024-09-03 13:22:21 +090010893 ensureListContainsMatch(t, copyCmds, "^cp -f .*/aconfig_flags.pb .*/image.apex/etc/aconfig_flags.pb")
10894 ensureListContainsMatch(t, copyCmds, "^cp -f .*/package.map .*/image.apex/etc/package.map")
10895 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.map .*/image.apex/etc/flag.map")
10896 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.val .*/image.apex/etc/flag.val")
Yu Liucec0e412023-11-30 16:45:50 -080010897
Yu Liubba555e2024-02-17 00:36:42 +000010898 inputs := []string{
10899 "my_aconfig_declarations_foo/intermediate.pb",
Yu Liuab31c822024-02-28 22:21:31 +000010900 "my_aconfig_declarations_bar/intermediate.pb",
10901 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liubba555e2024-02-17 00:36:42 +000010902 "my_rust_binary/android_arm64_armv8-a_apex10000/myapex/aconfig_merged.pb",
10903 }
10904 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10905 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10906 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10907 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
10908}
10909
10910func VerifyAconfigRule(t *testing.T, mod *android.TestingModule, desc string, inputs []string, output string, container string, file_type string) {
10911 aconfigRule := mod.Description(desc)
10912 s := " " + aconfigRule.Args["cache_files"]
Yu Liucec0e412023-11-30 16:45:50 -080010913 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
Yu Liubba555e2024-02-17 00:36:42 +000010914 if len(aconfigArgs) != len(inputs) {
10915 t.Fatalf("Expected %d commands, got %d in:\n%s", len(inputs), len(aconfigArgs), s)
Yu Liucec0e412023-11-30 16:45:50 -080010916 }
Yu Liucec0e412023-11-30 16:45:50 -080010917
Yu Liubba555e2024-02-17 00:36:42 +000010918 ensureEquals(t, container, aconfigRule.Args["container"])
10919 ensureEquals(t, file_type, aconfigRule.Args["file_type"])
10920
10921 buildParams := aconfigRule.BuildParams
10922 for _, input := range inputs {
10923 android.EnsureListContainsSuffix(t, aconfigArgs, input)
10924 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), input)
Yu Liucec0e412023-11-30 16:45:50 -080010925 }
Yu Liubba555e2024-02-17 00:36:42 +000010926
10927 ensureContains(t, buildParams.Output.String(), output)
Yu Liucec0e412023-11-30 16:45:50 -080010928}
10929
Yu Liueae7b362023-11-16 17:05:47 -080010930func TestAconfigFilesOnlyMatchCurrentApex(t *testing.T) {
10931 ctx := testApex(t, apex_default_bp+`
10932 apex {
10933 name: "myapex",
10934 manifest: ":myapex.manifest",
10935 androidManifest: ":myapex.androidmanifest",
10936 key: "myapex.key",
10937 java_libs: [
10938 "my_java_library_foo",
10939 "other_java_library_bar",
10940 ],
10941 updatable: false,
10942 }
10943
10944 java_library {
10945 name: "my_java_library_foo",
10946 srcs: ["foo/bar/MyClass.java"],
10947 sdk_version: "none",
10948 system_modules: "none",
10949 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010950 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010951 "myapex",
10952 ],
10953 }
10954
10955 java_library {
10956 name: "other_java_library_bar",
10957 srcs: ["foo/bar/MyClass.java"],
10958 sdk_version: "none",
10959 system_modules: "none",
10960 static_libs: ["other_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010961 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010962 "myapex",
10963 ],
10964 }
10965
10966 aconfig_declarations {
10967 name: "my_aconfig_declarations_foo",
10968 package: "com.example.package",
10969 container: "myapex",
10970 srcs: ["foo.aconfig"],
10971 }
10972
10973 java_aconfig_library {
10974 name: "my_java_aconfig_library_foo",
10975 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010976 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010977 "myapex",
10978 ],
10979 }
10980
10981 aconfig_declarations {
10982 name: "other_aconfig_declarations_bar",
10983 package: "com.example.package",
10984 container: "otherapex",
10985 srcs: ["bar.aconfig"],
10986 }
10987
10988 java_aconfig_library {
10989 name: "other_java_aconfig_library_bar",
10990 aconfig_declarations: "other_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010991 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010992 "myapex",
10993 ],
10994 }
10995 `)
10996
10997 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10998 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
10999 s := " " + combineAconfigRule.Args["cache_files"]
11000 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11001 if len(aconfigArgs) != 1 {
11002 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
11003 }
11004 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11005
11006 buildParams := combineAconfigRule.BuildParams
11007 if len(buildParams.Inputs) != 1 {
11008 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
11009 }
11010 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11011 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11012}
11013
11014func TestAconfigFilesRemoveDuplicates(t *testing.T) {
11015 ctx := testApex(t, apex_default_bp+`
11016 apex {
11017 name: "myapex",
11018 manifest: ":myapex.manifest",
11019 androidManifest: ":myapex.androidmanifest",
11020 key: "myapex.key",
11021 java_libs: [
11022 "my_java_library_foo",
11023 "my_java_library_bar",
11024 ],
11025 updatable: false,
11026 }
11027
11028 java_library {
11029 name: "my_java_library_foo",
11030 srcs: ["foo/bar/MyClass.java"],
11031 sdk_version: "none",
11032 system_modules: "none",
11033 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080011034 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011035 "myapex",
11036 ],
11037 }
11038
11039 java_library {
11040 name: "my_java_library_bar",
11041 srcs: ["foo/bar/MyClass.java"],
11042 sdk_version: "none",
11043 system_modules: "none",
11044 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080011045 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011046 "myapex",
11047 ],
11048 }
11049
11050 aconfig_declarations {
11051 name: "my_aconfig_declarations_foo",
11052 package: "com.example.package",
11053 container: "myapex",
11054 srcs: ["foo.aconfig"],
11055 }
11056
11057 java_aconfig_library {
11058 name: "my_java_aconfig_library_foo",
11059 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011060 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011061 "myapex",
11062 ],
11063 }
11064
11065 java_aconfig_library {
11066 name: "my_java_aconfig_library_bar",
11067 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011068 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011069 "myapex",
11070 ],
11071 }
11072 `)
11073
11074 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11075 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11076 s := " " + combineAconfigRule.Args["cache_files"]
11077 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11078 if len(aconfigArgs) != 1 {
11079 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
11080 }
11081 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11082
11083 buildParams := combineAconfigRule.BuildParams
11084 if len(buildParams.Inputs) != 1 {
11085 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
11086 }
11087 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11088 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11089}
Spandan Das5be63332023-12-13 00:06:32 +000011090
11091// Test that the boot jars come from the _selected_ apex prebuilt
11092// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11093func TestBootDexJarsMultipleApexPrebuilts(t *testing.T) {
11094 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
11095 t.Helper()
11096 s := ctx.ModuleForTests("dex_bootjars", "android_common")
11097 foundLibfooJar := false
11098 base := stem + ".jar"
11099 for _, output := range s.AllOutputs() {
11100 if filepath.Base(output) == base {
11101 foundLibfooJar = true
11102 buildRule := s.Output(output)
11103 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
11104 }
11105 }
11106 if !foundLibfooJar {
11107 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
11108 }
11109 }
11110
Spandan Das64c9e0c2023-12-20 20:13:34 +000011111 // Check that the boot jars of the selected apex are run through boot_jars_package_check
11112 // This validates that the jars on the bootclasspath do not contain packages outside an allowlist
11113 checkBootJarsPackageCheck := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11114 platformBcp := ctx.ModuleForTests("platform-bootclasspath", "android_common")
11115 bootJarsCheckRule := platformBcp.Rule("boot_jars_package_check")
11116 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)
11117 }
11118
11119 // Check that the boot jars used to generate the monolithic hiddenapi flags come from the selected apex
11120 checkBootJarsForMonolithicHiddenapi := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11121 monolithicHiddenapiFlagsCmd := ctx.ModuleForTests("platform-bootclasspath", "android_common").Output("out/soong/hiddenapi/hiddenapi-stub-flags.txt").RuleParams.Command
11122 android.AssertStringMatches(t, "Could not find the correct boot dex jar in monolithic hiddenapi flags generation command", monolithicHiddenapiFlagsCmd, "--boot-dex="+expectedBootJar)
11123 }
11124
Spandan Das5be63332023-12-13 00:06:32 +000011125 bp := `
11126 // Source APEX.
11127
11128 java_library {
11129 name: "framework-foo",
11130 srcs: ["foo.java"],
11131 installable: true,
11132 apex_available: [
11133 "com.android.foo",
11134 ],
11135 }
11136
11137 bootclasspath_fragment {
11138 name: "foo-bootclasspath-fragment",
11139 contents: ["framework-foo"],
11140 apex_available: [
11141 "com.android.foo",
11142 ],
11143 hidden_api: {
11144 split_packages: ["*"],
11145 },
11146 }
11147
11148 apex_key {
11149 name: "com.android.foo.key",
11150 public_key: "com.android.foo.avbpubkey",
11151 private_key: "com.android.foo.pem",
11152 }
11153
11154 apex {
11155 name: "com.android.foo",
11156 key: "com.android.foo.key",
11157 bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11158 updatable: false,
11159 }
11160
11161 // Prebuilt APEX.
11162
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011163 java_sdk_library_import {
Spandan Das5be63332023-12-13 00:06:32 +000011164 name: "framework-foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011165 public: {
11166 jars: ["foo.jar"],
11167 },
Spandan Das5be63332023-12-13 00:06:32 +000011168 apex_available: ["com.android.foo"],
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011169 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +000011170 }
11171
11172 prebuilt_bootclasspath_fragment {
11173 name: "foo-bootclasspath-fragment",
11174 contents: ["framework-foo"],
11175 hidden_api: {
11176 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
11177 metadata: "my-bootclasspath-fragment/metadata.csv",
11178 index: "my-bootclasspath-fragment/index.csv",
11179 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
11180 all_flags: "my-bootclasspath-fragment/all-flags.csv",
11181 },
11182 apex_available: [
11183 "com.android.foo",
11184 ],
11185 }
11186
11187 prebuilt_apex {
11188 name: "com.android.foo",
11189 apex_name: "com.android.foo",
11190 src: "com.android.foo-arm.apex",
11191 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11192 }
11193
11194 // Another Prebuilt ART APEX
11195 prebuilt_apex {
11196 name: "com.android.foo.v2",
11197 apex_name: "com.android.foo", // Used to determine the API domain
11198 src: "com.android.foo-arm.apex",
11199 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11200 }
11201
11202 // APEX contribution modules
11203
11204 apex_contributions {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011205 name: "foo.source.contributions",
Spandan Das5be63332023-12-13 00:06:32 +000011206 api_domain: "com.android.foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011207 contents: ["com.android.foo"],
11208 }
11209
11210 apex_contributions {
11211 name: "foo.prebuilt.contributions",
11212 api_domain: "com.android.foo",
11213 contents: ["prebuilt_com.android.foo"],
11214 }
11215
11216 apex_contributions {
11217 name: "foo.prebuilt.v2.contributions",
11218 api_domain: "com.android.foo",
11219 contents: ["com.android.foo.v2"], // prebuilt_ prefix is missing because of prebuilt_rename mutator
Spandan Das5be63332023-12-13 00:06:32 +000011220 }
11221 `
11222
11223 testCases := []struct {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011224 desc string
11225 selectedApexContributions string
11226 expectedBootJar string
Spandan Das5be63332023-12-13 00:06:32 +000011227 }{
11228 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011229 desc: "Source apex com.android.foo is selected, bootjar should come from source java library",
11230 selectedApexContributions: "foo.source.contributions",
11231 expectedBootJar: "out/soong/.intermediates/foo-bootclasspath-fragment/android_common_apex10000/hiddenapi-modular/encoded/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011232 },
11233 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011234 desc: "Prebuilt apex prebuilt_com.android.foo is selected, profile should come from .prof deapexed from the prebuilt",
11235 selectedApexContributions: "foo.prebuilt.contributions",
11236 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011237 },
11238 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011239 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected, profile should come from .prof deapexed from the prebuilt",
11240 selectedApexContributions: "foo.prebuilt.v2.contributions",
11241 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.v2.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011242 },
11243 }
11244
11245 fragment := java.ApexVariantReference{
11246 Apex: proptools.StringPtr("com.android.foo"),
11247 Module: proptools.StringPtr("foo-bootclasspath-fragment"),
11248 }
11249
11250 for _, tc := range testCases {
11251 preparer := android.GroupFixturePreparers(
11252 java.FixtureConfigureApexBootJars("com.android.foo:framework-foo"),
11253 android.FixtureMergeMockFs(map[string][]byte{
11254 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11255 }),
Spandan Das81fe4d12024-05-15 18:43:47 +000011256 // Make sure that we have atleast one platform library so that we can check the monolithic hiddenapi
11257 // file creation.
11258 java.FixtureConfigureBootJars("platform:foo"),
11259 android.FixtureModifyMockFS(func(fs android.MockFS) {
11260 fs["platform/Android.bp"] = []byte(`
11261 java_library {
11262 name: "foo",
11263 srcs: ["Test.java"],
11264 compile_dex: true,
11265 }
11266 `)
11267 fs["platform/Test.java"] = nil
11268 }),
11269
Colin Crossa66b4632024-08-08 15:50:47 -070011270 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das5be63332023-12-13 00:06:32 +000011271 )
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011272 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das5be63332023-12-13 00:06:32 +000011273 checkBootDexJarPath(t, ctx, "framework-foo", tc.expectedBootJar)
Spandan Das64c9e0c2023-12-20 20:13:34 +000011274 checkBootJarsPackageCheck(t, ctx, tc.expectedBootJar)
11275 checkBootJarsForMonolithicHiddenapi(t, ctx, tc.expectedBootJar)
Spandan Das5be63332023-12-13 00:06:32 +000011276 }
11277}
Spandan Das3576e762024-01-03 18:57:03 +000011278
11279// Test that product packaging installs the selected mainline module (either source or a specific prebuilt)
11280// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11281func TestInstallationRulesForMultipleApexPrebuilts(t *testing.T) {
Spandan Das3576e762024-01-03 18:57:03 +000011282 // for a mainline module family, check that only the flagged soong module is visible to make
11283 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleName string, hiddenModuleNames []string) {
11284 variation := func(moduleName string) string {
11285 ret := "android_common_com.android.foo"
11286 if moduleName == "com.google.android.foo" {
Spandan Das50801e22024-05-13 18:29:45 +000011287 ret = "android_common_com.google.android.foo_com.google.android.foo"
Spandan Das3576e762024-01-03 18:57:03 +000011288 }
11289 return ret
11290 }
11291
11292 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11293 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11294
11295 for _, hiddenModuleName := range hiddenModuleNames {
11296 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11297 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11298
11299 }
11300 }
11301
11302 bp := `
11303 apex_key {
11304 name: "com.android.foo.key",
11305 public_key: "com.android.foo.avbpubkey",
11306 private_key: "com.android.foo.pem",
11307 }
11308
11309 // AOSP source apex
11310 apex {
11311 name: "com.android.foo",
11312 key: "com.android.foo.key",
11313 updatable: false,
11314 }
11315
11316 // Google source apex
11317 override_apex {
11318 name: "com.google.android.foo",
11319 base: "com.android.foo",
11320 key: "com.android.foo.key",
11321 }
11322
11323 // Prebuilt Google APEX.
11324
11325 prebuilt_apex {
11326 name: "com.google.android.foo",
11327 apex_name: "com.android.foo",
11328 src: "com.android.foo-arm.apex",
11329 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11330 }
11331
11332 // Another Prebuilt Google APEX
11333 prebuilt_apex {
11334 name: "com.google.android.foo.v2",
11335 apex_name: "com.android.foo",
Spandan Dasa8e2d612024-07-26 19:24:27 +000011336 source_apex_name: "com.google.android.foo",
Spandan Das3576e762024-01-03 18:57:03 +000011337 src: "com.android.foo-arm.apex",
11338 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11339 }
11340
11341 // APEX contribution modules
11342
11343 apex_contributions {
11344 name: "foo.source.contributions",
11345 api_domain: "com.android.foo",
11346 contents: ["com.google.android.foo"],
11347 }
11348
11349 apex_contributions {
11350 name: "foo.prebuilt.contributions",
11351 api_domain: "com.android.foo",
11352 contents: ["prebuilt_com.google.android.foo"],
11353 }
11354
11355 apex_contributions {
11356 name: "foo.prebuilt.v2.contributions",
11357 api_domain: "com.android.foo",
11358 contents: ["prebuilt_com.google.android.foo.v2"],
11359 }
11360
11361 // This is an incompatible module because it selects multiple versions of the same mainline module
11362 apex_contributions {
11363 name: "foo.prebuilt.duplicate.contributions",
11364 api_domain: "com.android.foo",
11365 contents: [
11366 "prebuilt_com.google.android.foo",
11367 "prebuilt_com.google.android.foo.v2",
11368 ],
11369 }
11370 `
11371
11372 testCases := []struct {
11373 desc string
11374 selectedApexContributions string
11375 expectedVisibleModuleName string
11376 expectedHiddenModuleNames []string
11377 expectedError string
11378 }{
11379 {
11380 desc: "Source apex is selected, prebuilts should be hidden from make",
11381 selectedApexContributions: "foo.source.contributions",
11382 expectedVisibleModuleName: "com.google.android.foo",
11383 expectedHiddenModuleNames: []string{"prebuilt_com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11384 },
11385 {
11386 desc: "Prebuilt apex prebuilt_com.android.foo is selected, source and the other prebuilt should be hidden from make",
11387 selectedApexContributions: "foo.prebuilt.contributions",
11388 expectedVisibleModuleName: "prebuilt_com.google.android.foo",
11389 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11390 },
11391 {
11392 desc: "Prebuilt apex prebuilt_com.android.fooi.v2 is selected, source and the other prebuilt should be hidden from make",
11393 selectedApexContributions: "foo.prebuilt.v2.contributions",
11394 expectedVisibleModuleName: "prebuilt_com.google.android.foo.v2",
11395 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo"},
11396 },
11397 {
11398 desc: "Multiple versions of a prebuilt apex is selected in the same release config",
11399 selectedApexContributions: "foo.prebuilt.duplicate.contributions",
11400 expectedError: "Found duplicate variations of the same module in apex_contributions: prebuilt_com.google.android.foo and prebuilt_com.google.android.foo.v2",
11401 },
11402 }
11403
11404 for _, tc := range testCases {
11405 preparer := android.GroupFixturePreparers(
11406 android.FixtureMergeMockFs(map[string][]byte{
11407 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11408 }),
Colin Crossa66b4632024-08-08 15:50:47 -070011409 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das3576e762024-01-03 18:57:03 +000011410 )
11411 if tc.expectedError != "" {
11412 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(tc.expectedError))
11413 testApex(t, bp, preparer)
11414 return
11415 }
11416 ctx := testApex(t, bp, preparer)
11417
Spandan Das3576e762024-01-03 18:57:03 +000011418 // Check that
11419 // 1. The contents of the selected apex_contributions are visible to make
11420 // 2. The rest of the apexes in the mainline module family (source or other prebuilt) is hidden from make
11421 checkHideFromMake(t, ctx, tc.expectedVisibleModuleName, tc.expectedHiddenModuleNames)
11422 }
11423}
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011424
Spandan Das85bd4622024-08-01 00:51:20 +000011425// Test that product packaging installs the selected mainline module in workspaces withtout source mainline module
11426func TestInstallationRulesForMultipleApexPrebuiltsWithoutSource(t *testing.T) {
11427 // for a mainline module family, check that only the flagged soong module is visible to make
11428 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleNames []string, hiddenModuleNames []string) {
11429 variation := func(moduleName string) string {
11430 ret := "android_common_com.android.adservices"
11431 if moduleName == "com.google.android.foo" {
11432 ret = "android_common_com.google.android.foo_com.google.android.foo"
11433 }
11434 return ret
11435 }
11436
11437 for _, visibleModuleName := range visibleModuleNames {
11438 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11439 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11440 }
11441
11442 for _, hiddenModuleName := range hiddenModuleNames {
11443 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11444 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11445
11446 }
11447 }
11448
11449 bp := `
11450 apex_key {
11451 name: "com.android.adservices.key",
11452 public_key: "com.android.adservices.avbpubkey",
11453 private_key: "com.android.adservices.pem",
11454 }
11455
11456 // AOSP source apex
11457 apex {
11458 name: "com.android.adservices",
11459 key: "com.android.adservices.key",
11460 updatable: false,
11461 }
11462
11463 // Prebuilt Google APEX.
11464
11465 prebuilt_apex {
11466 name: "com.google.android.adservices",
11467 apex_name: "com.android.adservices",
11468 src: "com.android.foo-arm.apex",
11469 }
11470
11471 // Another Prebuilt Google APEX
11472 prebuilt_apex {
11473 name: "com.google.android.adservices.v2",
11474 apex_name: "com.android.adservices",
11475 src: "com.android.foo-arm.apex",
11476 }
11477
11478 // APEX contribution modules
11479
11480
11481 apex_contributions {
11482 name: "adservices.prebuilt.contributions",
11483 api_domain: "com.android.adservices",
11484 contents: ["prebuilt_com.google.android.adservices"],
11485 }
11486
11487 apex_contributions {
11488 name: "adservices.prebuilt.v2.contributions",
11489 api_domain: "com.android.adservices",
11490 contents: ["prebuilt_com.google.android.adservices.v2"],
11491 }
11492 `
11493
11494 testCases := []struct {
11495 desc string
11496 selectedApexContributions string
11497 expectedVisibleModuleNames []string
11498 expectedHiddenModuleNames []string
11499 }{
11500 {
11501 desc: "No apex contributions selected, source aosp apex should be visible, and mainline prebuilts should be hidden",
11502 selectedApexContributions: "",
11503 expectedVisibleModuleNames: []string{"com.android.adservices"},
11504 expectedHiddenModuleNames: []string{"com.google.android.adservices", "com.google.android.adservices.v2"},
11505 },
11506 {
11507 desc: "Prebuilt apex prebuilt_com.android.foo is selected",
11508 selectedApexContributions: "adservices.prebuilt.contributions",
11509 expectedVisibleModuleNames: []string{"com.android.adservices", "com.google.android.adservices"},
11510 expectedHiddenModuleNames: []string{"com.google.android.adservices.v2"},
11511 },
11512 {
11513 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected",
11514 selectedApexContributions: "adservices.prebuilt.v2.contributions",
11515 expectedVisibleModuleNames: []string{"com.android.adservices", "com.google.android.adservices.v2"},
11516 expectedHiddenModuleNames: []string{"com.google.android.adservices"},
11517 },
11518 }
11519
11520 for _, tc := range testCases {
11521 preparer := android.GroupFixturePreparers(
11522 android.FixtureMergeMockFs(map[string][]byte{
11523 "system/sepolicy/apex/com.android.adservices-file_contexts": nil,
11524 }),
Colin Crossa66b4632024-08-08 15:50:47 -070011525 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das85bd4622024-08-01 00:51:20 +000011526 )
11527 ctx := testApex(t, bp, preparer)
11528
11529 checkHideFromMake(t, ctx, tc.expectedVisibleModuleNames, tc.expectedHiddenModuleNames)
11530 }
11531}
11532
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011533func TestAconfifDeclarationsValidation(t *testing.T) {
11534 aconfigDeclarationLibraryString := func(moduleNames []string) (ret string) {
11535 for _, moduleName := range moduleNames {
11536 ret += fmt.Sprintf(`
11537 aconfig_declarations {
11538 name: "%[1]s",
11539 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +000011540 container: "system",
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011541 srcs: [
11542 "%[1]s.aconfig",
11543 ],
11544 }
11545 java_aconfig_library {
11546 name: "%[1]s-lib",
11547 aconfig_declarations: "%[1]s",
11548 }
11549 `, moduleName)
11550 }
11551 return ret
11552 }
11553
11554 result := android.GroupFixturePreparers(
11555 prepareForApexTest,
11556 java.PrepareForTestWithJavaSdkLibraryFiles,
11557 java.FixtureWithLastReleaseApis("foo"),
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011558 ).RunTestWithBp(t, `
11559 java_library {
11560 name: "baz-java-lib",
11561 static_libs: [
11562 "baz-lib",
11563 ],
11564 }
11565 filegroup {
11566 name: "qux-filegroup",
11567 srcs: [
11568 ":qux-lib{.generated_srcjars}",
11569 ],
11570 }
11571 filegroup {
11572 name: "qux-another-filegroup",
11573 srcs: [
11574 ":qux-filegroup",
11575 ],
11576 }
11577 java_library {
11578 name: "quux-java-lib",
11579 srcs: [
11580 "a.java",
11581 ],
11582 libs: [
11583 "quux-lib",
11584 ],
11585 }
11586 java_sdk_library {
11587 name: "foo",
11588 srcs: [
11589 ":qux-another-filegroup",
11590 ],
11591 api_packages: ["foo"],
11592 system: {
11593 enabled: true,
11594 },
11595 module_lib: {
11596 enabled: true,
11597 },
11598 test: {
11599 enabled: true,
11600 },
11601 static_libs: [
11602 "bar-lib",
11603 ],
11604 libs: [
11605 "baz-java-lib",
11606 "quux-java-lib",
11607 ],
11608 aconfig_declarations: [
11609 "bar",
11610 ],
11611 }
11612 `+aconfigDeclarationLibraryString([]string{"bar", "baz", "qux", "quux"}))
11613
11614 m := result.ModuleForTests("foo.stubs.source", "android_common")
11615 outDir := "out/soong/.intermediates"
11616
11617 // Arguments passed to aconfig to retrieve the state of the flags defined in the
11618 // textproto files
11619 aconfigFlagArgs := m.Output("released-flagged-apis-exportable.txt").Args["flags_path"]
11620
11621 // "bar-lib" is a static_lib of "foo" and is passed to metalava as classpath. Thus the
11622 // cache file provided by the associated aconfig_declarations module "bar" should be passed
11623 // to aconfig.
11624 android.AssertStringDoesContain(t, "cache file of a java_aconfig_library static_lib "+
11625 "passed as an input",
11626 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "bar"))
11627
11628 // "baz-java-lib", which statically depends on "baz-lib", is a lib of "foo" and is passed
11629 // to metalava as classpath. Thus the cache file provided by the associated
11630 // aconfig_declarations module "baz" should be passed to aconfig.
11631 android.AssertStringDoesContain(t, "cache file of a lib that statically depends on "+
11632 "java_aconfig_library passed as an input",
11633 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "baz"))
11634
11635 // "qux-lib" is passed to metalava as src via the filegroup, thus the cache file provided by
11636 // the associated aconfig_declarations module "qux" should be passed to aconfig.
11637 android.AssertStringDoesContain(t, "cache file of srcs java_aconfig_library passed as an "+
11638 "input",
11639 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "qux"))
11640
11641 // "quux-java-lib" is a lib of "foo" and is passed to metalava as classpath, but does not
11642 // statically depend on "quux-lib". Therefore, the cache file provided by the associated
11643 // aconfig_declarations module "quux" should not be passed to aconfig.
11644 android.AssertStringDoesNotContain(t, "cache file of a lib that does not statically "+
11645 "depend on java_aconfig_library not passed as an input",
11646 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "quux"))
11647}
Cole Faust7c991b42024-05-15 11:17:55 -070011648
11649func TestMultiplePrebuiltsWithSameBase(t *testing.T) {
11650 ctx := testApex(t, `
11651 apex {
11652 name: "myapex",
11653 key: "myapex.key",
11654 prebuilts: ["myetc", "myetc2"],
11655 min_sdk_version: "29",
11656 }
11657 apex_key {
11658 name: "myapex.key",
11659 public_key: "testkey.avbpubkey",
11660 private_key: "testkey.pem",
11661 }
11662
11663 prebuilt_etc {
11664 name: "myetc",
11665 src: "myprebuilt",
11666 filename: "myfilename",
11667 }
11668 prebuilt_etc {
11669 name: "myetc2",
11670 sub_dir: "mysubdir",
11671 src: "myprebuilt",
11672 filename: "myfilename",
11673 }
11674 `, withFiles(android.MockFS{
11675 "packages/modules/common/build/allowed_deps.txt": nil,
11676 }))
11677
11678 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
11679 data := android.AndroidMkDataForTest(t, ctx, ab)
11680 var builder strings.Builder
11681 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
11682 androidMk := builder.String()
11683
11684 android.AssertStringDoesContain(t, "not found", androidMk, "LOCAL_MODULE := etc_myfilename.myapex")
11685 android.AssertStringDoesContain(t, "not found", androidMk, "LOCAL_MODULE := etc_mysubdir_myfilename.myapex")
11686}
Spandan Das50801e22024-05-13 18:29:45 +000011687
11688func TestApexMinSdkVersionOverride(t *testing.T) {
11689 checkMinSdkVersion := func(t *testing.T, module android.TestingModule, expectedMinSdkVersion string) {
11690 args := module.Rule("apexRule").Args
11691 optFlags := args["opt_flags"]
11692 if !strings.Contains(optFlags, "--min_sdk_version "+expectedMinSdkVersion) {
11693 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", module.Module(), expectedMinSdkVersion, optFlags)
11694 }
11695 }
11696
11697 checkHasDep := func(t *testing.T, ctx *android.TestContext, m android.Module, wantDep android.Module) {
11698 t.Helper()
11699 found := false
11700 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
11701 if dep == wantDep {
11702 found = true
11703 }
11704 })
11705 if !found {
11706 t.Errorf("Could not find a dependency from %v to %v\n", m, wantDep)
11707 }
11708 }
11709
11710 ctx := testApex(t, `
11711 apex {
11712 name: "com.android.apex30",
11713 min_sdk_version: "30",
11714 key: "apex30.key",
11715 java_libs: ["javalib"],
11716 }
11717
11718 java_library {
11719 name: "javalib",
11720 srcs: ["A.java"],
11721 apex_available: ["com.android.apex30"],
11722 min_sdk_version: "30",
11723 sdk_version: "current",
11724 }
11725
11726 override_apex {
11727 name: "com.mycompany.android.apex30",
11728 base: "com.android.apex30",
11729 }
11730
11731 override_apex {
11732 name: "com.mycompany.android.apex31",
11733 base: "com.android.apex30",
11734 min_sdk_version: "31",
11735 }
11736
11737 apex_key {
11738 name: "apex30.key",
11739 public_key: "testkey.avbpubkey",
11740 private_key: "testkey.pem",
11741 }
11742
11743 `, android.FixtureMergeMockFs(android.MockFS{
11744 "system/sepolicy/apex/com.android.apex30-file_contexts": nil,
11745 }),
11746 )
11747
11748 baseModule := ctx.ModuleForTests("com.android.apex30", "android_common_com.android.apex30")
11749 checkMinSdkVersion(t, baseModule, "30")
11750
11751 // Override module, but uses same min_sdk_version
11752 overridingModuleSameMinSdkVersion := ctx.ModuleForTests("com.android.apex30", "android_common_com.mycompany.android.apex30_com.mycompany.android.apex30")
11753 javalibApex30Variant := ctx.ModuleForTests("javalib", "android_common_apex30")
11754 checkMinSdkVersion(t, overridingModuleSameMinSdkVersion, "30")
11755 checkHasDep(t, ctx, overridingModuleSameMinSdkVersion.Module(), javalibApex30Variant.Module())
11756
11757 // Override module, uses different min_sdk_version
11758 overridingModuleDifferentMinSdkVersion := ctx.ModuleForTests("com.android.apex30", "android_common_com.mycompany.android.apex31_com.mycompany.android.apex31")
11759 javalibApex31Variant := ctx.ModuleForTests("javalib", "android_common_apex31")
11760 checkMinSdkVersion(t, overridingModuleDifferentMinSdkVersion, "31")
11761 checkHasDep(t, ctx, overridingModuleDifferentMinSdkVersion.Module(), javalibApex31Variant.Module())
11762}
Spandan Das0b28fa02024-05-28 23:40:17 +000011763
11764func TestOverrideApexWithPrebuiltApexPreferred(t *testing.T) {
11765 context := android.GroupFixturePreparers(
11766 android.PrepareForIntegrationTestWithAndroid,
11767 PrepareForTestWithApexBuildComponents,
11768 android.FixtureMergeMockFs(android.MockFS{
11769 "system/sepolicy/apex/foo-file_contexts": nil,
11770 }),
11771 )
11772 res := context.RunTestWithBp(t, `
11773 apex {
11774 name: "foo",
11775 key: "myapex.key",
11776 apex_available_name: "com.android.foo",
11777 variant_version: "0",
11778 updatable: false,
11779 }
11780 apex_key {
11781 name: "myapex.key",
11782 public_key: "testkey.avbpubkey",
11783 private_key: "testkey.pem",
11784 }
11785 prebuilt_apex {
11786 name: "foo",
11787 src: "foo.apex",
11788 prefer: true,
11789 }
11790 override_apex {
11791 name: "myoverrideapex",
11792 base: "foo",
11793 }
11794 `)
11795
11796 java.CheckModuleHasDependency(t, res.TestContext, "myoverrideapex", "android_common_myoverrideapex_myoverrideapex", "foo")
11797}
Spandan Dasca1d63e2024-07-01 22:53:49 +000011798
11799func TestUpdatableApexMinSdkVersionCurrent(t *testing.T) {
11800 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`, `
11801 apex {
11802 name: "myapex",
11803 key: "myapex.key",
11804 updatable: true,
11805 min_sdk_version: "current",
11806 }
11807
11808 apex_key {
11809 name: "myapex.key",
11810 public_key: "testkey.avbpubkey",
11811 private_key: "testkey.pem",
11812 }
11813 `)
11814}
Spandan Das2f68f192024-07-22 19:25:50 +000011815
11816func TestPrebuiltStubNoinstall(t *testing.T) {
11817 testFunc := func(t *testing.T, expectLibfooOnSystemLib bool, fs android.MockFS) {
11818 result := android.GroupFixturePreparers(
11819 prepareForApexTest,
11820 android.PrepareForTestWithAndroidMk,
11821 android.PrepareForTestWithMakevars,
11822 android.FixtureMergeMockFs(fs),
11823 ).RunTest(t)
11824
11825 ldRule := result.ModuleForTests("installedlib", "android_arm64_armv8-a_shared").Rule("ld")
Spandan Das357ffcc2024-07-24 18:07:48 +000011826 android.AssertStringDoesContain(t, "", ldRule.Args["libFlags"], "android_arm64_armv8-a_shared_current/libfoo.so")
Spandan Das2f68f192024-07-22 19:25:50 +000011827
11828 installRules := result.InstallMakeRulesForTesting(t)
11829
11830 var installedlibRule *android.InstallMakeRule
11831 for i, rule := range installRules {
11832 if rule.Target == "out/target/product/test_device/system/lib/installedlib.so" {
11833 if installedlibRule != nil {
11834 t.Errorf("Duplicate install rules for %s", rule.Target)
11835 }
11836 installedlibRule = &installRules[i]
11837 }
11838 }
11839 if installedlibRule == nil {
11840 t.Errorf("No install rule found for installedlib")
11841 return
11842 }
11843
11844 if expectLibfooOnSystemLib {
11845 android.AssertStringListContains(t,
11846 "installedlib doesn't have install dependency on libfoo impl",
11847 installedlibRule.OrderOnlyDeps,
11848 "out/target/product/test_device/system/lib/libfoo.so")
11849 } else {
11850 android.AssertStringListDoesNotContain(t,
11851 "installedlib has install dependency on libfoo stub",
11852 installedlibRule.Deps,
11853 "out/target/product/test_device/system/lib/libfoo.so")
11854 android.AssertStringListDoesNotContain(t,
11855 "installedlib has order-only install dependency on libfoo stub",
11856 installedlibRule.OrderOnlyDeps,
11857 "out/target/product/test_device/system/lib/libfoo.so")
11858 }
11859 }
11860
11861 prebuiltLibfooBp := []byte(`
11862 cc_prebuilt_library {
11863 name: "libfoo",
11864 prefer: true,
11865 srcs: ["libfoo.so"],
11866 stubs: {
11867 versions: ["1"],
11868 },
11869 apex_available: ["apexfoo"],
11870 }
11871 `)
11872
11873 apexfooBp := []byte(`
11874 apex {
11875 name: "apexfoo",
11876 key: "apexfoo.key",
11877 native_shared_libs: ["libfoo"],
11878 updatable: false,
11879 compile_multilib: "both",
11880 }
11881 apex_key {
11882 name: "apexfoo.key",
11883 public_key: "testkey.avbpubkey",
11884 private_key: "testkey.pem",
11885 }
11886 `)
11887
11888 installedlibBp := []byte(`
11889 cc_library {
11890 name: "installedlib",
11891 shared_libs: ["libfoo"],
11892 }
11893 `)
11894
11895 t.Run("prebuilt stub (without source): no install", func(t *testing.T) {
11896 testFunc(
11897 t,
11898 /*expectLibfooOnSystemLib=*/ false,
11899 android.MockFS{
11900 "prebuilts/module_sdk/art/current/Android.bp": prebuiltLibfooBp,
11901 "apexfoo/Android.bp": apexfooBp,
11902 "system/sepolicy/apex/apexfoo-file_contexts": nil,
11903 "Android.bp": installedlibBp,
11904 },
11905 )
11906 })
11907
11908 disabledSourceLibfooBp := []byte(`
11909 cc_library {
11910 name: "libfoo",
11911 enabled: false,
11912 stubs: {
11913 versions: ["1"],
11914 },
11915 apex_available: ["apexfoo"],
11916 }
11917 `)
11918
11919 t.Run("prebuilt stub (with disabled source): no install", func(t *testing.T) {
11920 testFunc(
11921 t,
11922 /*expectLibfooOnSystemLib=*/ false,
11923 android.MockFS{
11924 "prebuilts/module_sdk/art/current/Android.bp": prebuiltLibfooBp,
11925 "impl/Android.bp": disabledSourceLibfooBp,
11926 "apexfoo/Android.bp": apexfooBp,
11927 "system/sepolicy/apex/apexfoo-file_contexts": nil,
11928 "Android.bp": installedlibBp,
11929 },
11930 )
11931 })
11932}
Jihoon Kange246bb72024-09-18 22:26:22 +000011933
11934func TestSdkLibraryTransitiveClassLoaderContext(t *testing.T) {
11935 // This test case tests that listing the impl lib instead of the top level java_sdk_library
11936 // in libs of android_app and java_library does not lead to class loader context device/host
11937 // path mismatch errors.
11938 android.GroupFixturePreparers(
11939 prepareForApexTest,
11940 android.PrepareForIntegrationTestWithAndroid,
11941 PrepareForTestWithApexBuildComponents,
11942 android.FixtureModifyEnv(func(env map[string]string) {
11943 env["DISABLE_CONTAINER_CHECK"] = "true"
11944 }),
11945 withFiles(filesForSdkLibrary),
11946 android.FixtureMergeMockFs(android.MockFS{
11947 "system/sepolicy/apex/com.android.foo30-file_contexts": nil,
11948 }),
11949 ).RunTestWithBp(t, `
11950 apex {
11951 name: "com.android.foo30",
11952 key: "myapex.key",
11953 updatable: true,
11954 bootclasspath_fragments: [
11955 "foo-bootclasspath-fragment",
11956 ],
11957 java_libs: [
11958 "bar",
11959 ],
11960 apps: [
11961 "bar-app",
11962 ],
11963 min_sdk_version: "30",
11964 }
11965 apex_key {
11966 name: "myapex.key",
11967 public_key: "testkey.avbpubkey",
11968 private_key: "testkey.pem",
11969 }
11970 bootclasspath_fragment {
11971 name: "foo-bootclasspath-fragment",
11972 contents: [
11973 "framework-foo",
11974 ],
11975 apex_available: [
11976 "com.android.foo30",
11977 ],
11978 hidden_api: {
11979 split_packages: ["*"]
11980 },
11981 }
11982
11983 java_sdk_library {
11984 name: "framework-foo",
11985 srcs: [
11986 "A.java"
11987 ],
11988 unsafe_ignore_missing_latest_api: true,
11989 apex_available: [
11990 "com.android.foo30",
11991 ],
11992 compile_dex: true,
11993 sdk_version: "core_current",
11994 shared_library: false,
11995 }
11996
11997 java_library {
11998 name: "bar",
11999 srcs: [
12000 "A.java"
12001 ],
12002 libs: [
12003 "framework-foo.impl",
12004 ],
12005 apex_available: [
12006 "com.android.foo30",
12007 ],
12008 sdk_version: "core_current",
12009 }
12010
12011 java_library {
12012 name: "baz",
12013 srcs: [
12014 "A.java"
12015 ],
12016 libs: [
12017 "bar",
12018 ],
12019 sdk_version: "core_current",
12020 }
12021
12022 android_app {
12023 name: "bar-app",
12024 srcs: [
12025 "A.java"
12026 ],
12027 libs: [
12028 "baz",
12029 "framework-foo.impl",
12030 ],
12031 apex_available: [
12032 "com.android.foo30",
12033 ],
12034 sdk_version: "core_current",
12035 min_sdk_version: "30",
12036 manifest: "AndroidManifest.xml",
12037 }
12038 `)
12039}