blob: c3a6e692660a50fe7b279593cfec8dfe066e1d8f [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jooyung Han39edb6c2019-11-06 16:53:07 +090019 "path"
Paul Duffin37856732021-02-26 14:24:15 +000020 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010022 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090023 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090024 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090025 "strings"
26 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090027
Yu Liueae7b362023-11-16 17:05:47 -080028 "android/soong/aconfig/codegen"
Jooyung Han20348752023-12-05 15:23:56 +090029
Kiyoung Kim487689e2022-07-26 09:48:22 +090030 "github.com/google/blueprint"
Jiyong Parkda6eb592018-12-19 17:12:36 +090031 "github.com/google/blueprint/proptools"
32
33 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080034 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090035 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000036 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070037 prebuilt_etc "android/soong/etc"
Colin Crossbd3a16b2023-04-25 11:30:51 -070038 "android/soong/filesystem"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090039 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090040 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070041 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090042)
43
Jooyung Hand3639552019-08-09 12:57:43 +090044// names returns name list from white space separated string
45func names(s string) (ns []string) {
46 for _, n := range strings.Split(s, " ") {
47 if len(n) > 0 {
48 ns = append(ns, n)
49 }
50 }
51 return
52}
53
Paul Duffin40b62572021-03-20 11:39:01 +000054func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090055 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010056 android.GroupFixturePreparers(
57 prepareForApexTest,
58 android.GroupFixturePreparers(preparers...),
59 ).
Paul Duffine05480a2021-03-08 15:07:14 +000060 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000061 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090062}
63
Paul Duffin40b62572021-03-20 11:39:01 +000064func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090065 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010066
67 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000068 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010069 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000070 }
Paul Duffin284165a2021-03-29 01:50:31 +010071
72 result := android.GroupFixturePreparers(
73 prepareForApexTest,
74 android.GroupFixturePreparers(preparers...),
75 optionalBpPreparer,
76 ).RunTest(t)
77
Paul Duffine05480a2021-03-08 15:07:14 +000078 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090079}
80
Paul Duffin810f33d2021-03-09 14:12:32 +000081func withFiles(files android.MockFS) android.FixturePreparer {
82 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090083}
84
Paul Duffin810f33d2021-03-09 14:12:32 +000085func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
86 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090087 for k, v := range targets {
88 config.Targets[k] = v
89 }
Paul Duffin810f33d2021-03-09 14:12:32 +000090 })
Jooyung Han344d5432019-08-23 11:17:39 +090091}
92
Jooyung Han35155c42020-02-06 17:33:20 +090093// withNativeBridgeTargets sets configuration with targets including:
94// - X86_64 (primary)
95// - X86 (secondary)
96// - Arm64 on X86_64 (native bridge)
97// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000098var withNativeBridgeEnabled = android.FixtureModifyConfig(
99 func(config android.Config) {
100 config.Targets[android.Android] = []android.Target{
101 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
102 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
103 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
104 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
105 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
106 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
107 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
108 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
109 }
110 },
111)
112
113func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
114 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
115 variables.ManifestPackageNameOverrides = specs
116 })
Jooyung Han35155c42020-02-06 17:33:20 +0900117}
118
Albert Martineefabcf2022-03-21 20:11:16 +0000119func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
120 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
121 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
122 })
123}
124
Paul Duffin810f33d2021-03-09 14:12:32 +0000125var withBinder32bit = android.FixtureModifyProductVariables(
126 func(variables android.FixtureProductVariables) {
127 variables.Binder32bit = proptools.BoolPtr(true)
128 },
129)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900130
Paul Duffin810f33d2021-03-09 14:12:32 +0000131var withUnbundledBuild = android.FixtureModifyProductVariables(
132 func(variables android.FixtureProductVariables) {
133 variables.Unbundled_build = proptools.BoolPtr(true)
134 },
135)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900136
Paul Duffin284165a2021-03-29 01:50:31 +0100137// Legacy preparer used for running tests within the apex package.
138//
139// This includes everything that was needed to run any test in the apex package prior to the
140// introduction of the test fixtures. Tests that are being converted to use fixtures directly
141// rather than through the testApex...() methods should avoid using this and instead use the
142// various preparers directly, using android.GroupFixturePreparers(...) to group them when
143// necessary.
144//
145// deprecated
146var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000147 // General preparers in alphabetical order as test infrastructure will enforce correct
148 // registration order.
149 android.PrepareForTestWithAndroidBuildComponents,
150 bpf.PrepareForTestWithBpf,
151 cc.PrepareForTestWithCcBuildComponents,
Jiakai Zhangb95998b2023-05-11 16:39:27 +0100152 java.PrepareForTestWithDexpreopt,
Paul Duffin37aad602021-03-08 09:47:16 +0000153 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
154 rust.PrepareForTestWithRustDefaultModules,
155 sh.PrepareForTestWithShBuildComponents,
Yu Liueae7b362023-11-16 17:05:47 -0800156 codegen.PrepareForTestWithAconfigBuildComponents,
Paul Duffin37aad602021-03-08 09:47:16 +0000157
158 PrepareForTestWithApexBuildComponents,
159
160 // Additional apex test specific preparers.
161 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
162 filegroup {
163 name: "myapex-file_contexts",
164 srcs: [
165 "apex/myapex-file_contexts",
166 ],
167 }
168 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000169 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000170 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000171 "a.java": nil,
172 "PrebuiltAppFoo.apk": nil,
173 "PrebuiltAppFooPriv.apk": nil,
174 "apex_manifest.json": nil,
175 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000176 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
177 "system/sepolicy/apex/myapex2-file_contexts": nil,
178 "system/sepolicy/apex/otherapex-file_contexts": nil,
179 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
180 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700181 "mylib.cpp": nil,
182 "mytest.cpp": nil,
183 "mytest1.cpp": nil,
184 "mytest2.cpp": nil,
185 "mytest3.cpp": nil,
186 "myprebuilt": nil,
187 "my_include": nil,
188 "foo/bar/MyClass.java": nil,
189 "prebuilt.jar": nil,
190 "prebuilt.so": nil,
191 "vendor/foo/devkeys/test.x509.pem": nil,
192 "vendor/foo/devkeys/test.pk8": nil,
193 "testkey.x509.pem": nil,
194 "testkey.pk8": nil,
195 "testkey.override.x509.pem": nil,
196 "testkey.override.pk8": nil,
197 "vendor/foo/devkeys/testkey.avbpubkey": nil,
198 "vendor/foo/devkeys/testkey.pem": nil,
199 "NOTICE": nil,
200 "custom_notice": nil,
201 "custom_notice_for_static_lib": nil,
202 "testkey2.avbpubkey": nil,
203 "testkey2.pem": nil,
204 "myapex-arm64.apex": nil,
205 "myapex-arm.apex": nil,
206 "myapex.apks": nil,
207 "frameworks/base/api/current.txt": nil,
208 "framework/aidl/a.aidl": nil,
209 "dummy.txt": nil,
210 "baz": nil,
211 "bar/baz": nil,
212 "testdata/baz": nil,
213 "AppSet.apks": nil,
214 "foo.rs": nil,
215 "libfoo.jar": nil,
216 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000217 },
218 ),
219
220 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
Paul Duffin37aad602021-03-08 09:47:16 +0000221 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
222 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
223 variables.Platform_sdk_codename = proptools.StringPtr("Q")
224 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000225 // "Tiramisu" needs to be in the next line for compatibility with soong code,
226 // not because of these tests specifically (it's not used by the tests)
227 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000228 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000229 }),
230)
231
Paul Duffin52bfaa42021-03-23 23:40:12 +0000232var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
233 "system/sepolicy/apex/myapex-file_contexts": nil,
234})
235
Jooyung Han643adc42020-02-27 13:50:06 +0900236// ensure that 'result' equals 'expected'
237func ensureEquals(t *testing.T, result string, expected string) {
238 t.Helper()
239 if result != expected {
240 t.Errorf("%q != %q", expected, result)
241 }
242}
243
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244// ensure that 'result' contains 'expected'
245func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900246 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247 if !strings.Contains(result, expected) {
248 t.Errorf("%q is not found in %q", expected, result)
249 }
250}
251
Liz Kammer5bd365f2020-05-27 15:15:11 -0700252// ensure that 'result' contains 'expected' exactly one time
253func ensureContainsOnce(t *testing.T, result string, expected string) {
254 t.Helper()
255 count := strings.Count(result, expected)
256 if count != 1 {
257 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
258 }
259}
260
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261// ensures that 'result' does not contain 'notExpected'
262func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900263 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900264 if strings.Contains(result, notExpected) {
265 t.Errorf("%q is found in %q", notExpected, result)
266 }
267}
268
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700269func ensureMatches(t *testing.T, result string, expectedRex string) {
Jooyung Hana3fddf42024-09-03 13:22:21 +0900270 t.Helper()
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700271 ok, err := regexp.MatchString(expectedRex, result)
272 if err != nil {
273 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
274 return
275 }
276 if !ok {
277 t.Errorf("%s does not match regular expession %s", result, expectedRex)
278 }
279}
280
Jooyung Hana3fddf42024-09-03 13:22:21 +0900281func ensureListContainsMatch(t *testing.T, result []string, expectedRex string) {
282 t.Helper()
283 p := regexp.MustCompile(expectedRex)
284 if android.IndexListPred(func(s string) bool { return p.MatchString(s) }, result) == -1 {
285 t.Errorf("%q is not found in %v", expectedRex, result)
286 }
287}
288
Jiyong Park25fc6a92018-11-18 18:02:45 +0900289func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900290 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900291 if !android.InList(expected, result) {
292 t.Errorf("%q is not found in %v", expected, result)
293 }
294}
295
296func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900297 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900298 if android.InList(notExpected, result) {
299 t.Errorf("%q is found in %v", notExpected, result)
300 }
301}
302
Jooyung Hane1633032019-08-01 17:41:43 +0900303func ensureListEmpty(t *testing.T, result []string) {
304 t.Helper()
305 if len(result) > 0 {
306 t.Errorf("%q is expected to be empty", result)
307 }
308}
309
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000310func ensureListNotEmpty(t *testing.T, result []string) {
311 t.Helper()
312 if len(result) == 0 {
313 t.Errorf("%q is expected to be not empty", result)
314 }
315}
316
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317// Minimal test
318func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800319 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900320 apex_defaults {
321 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900322 manifest: ":myapex.manifest",
323 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900324 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900325 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900326 native_shared_libs: [
327 "mylib",
328 "libfoo.ffi",
329 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900330 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800331 multilib: {
332 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900333 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800334 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900335 },
Jiyong Park77acec62020-06-01 21:39:15 +0900336 java_libs: [
337 "myjar",
338 "myjar_dex",
339 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000340 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900341 }
342
Jiyong Park30ca9372019-02-07 16:27:23 +0900343 apex {
344 name: "myapex",
345 defaults: ["myapex-defaults"],
346 }
347
Jiyong Park25fc6a92018-11-18 18:02:45 +0900348 apex_key {
349 name: "myapex.key",
350 public_key: "testkey.avbpubkey",
351 private_key: "testkey.pem",
352 }
353
Jiyong Park809bb722019-02-13 21:33:49 +0900354 filegroup {
355 name: "myapex.manifest",
356 srcs: ["apex_manifest.json"],
357 }
358
359 filegroup {
360 name: "myapex.androidmanifest",
361 srcs: ["AndroidManifest.xml"],
362 }
363
Jiyong Park25fc6a92018-11-18 18:02:45 +0900364 cc_library {
365 name: "mylib",
366 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900367 shared_libs: [
368 "mylib2",
369 "libbar.ffi",
370 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900371 system_shared_libs: [],
372 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000373 // TODO: remove //apex_available:platform
374 apex_available: [
375 "//apex_available:platform",
376 "myapex",
377 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900378 }
379
Alex Light3d673592019-01-18 14:37:31 -0800380 cc_binary {
381 name: "foo",
382 srcs: ["mylib.cpp"],
383 compile_multilib: "both",
384 multilib: {
385 lib32: {
386 suffix: "32",
387 },
388 lib64: {
389 suffix: "64",
390 },
391 },
392 symlinks: ["foo_link_"],
393 symlink_preferred_arch: true,
394 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800395 stl: "none",
Jooyung Han40b79172024-08-16 16:00:33 +0900396 apex_available: [ "myapex" ],
Yifan Hongd22a84a2020-07-28 17:37:46 -0700397 }
398
Jiyong Park99644e92020-11-17 22:21:02 +0900399 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000400 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900401 srcs: ["foo.rs"],
402 rlibs: ["libfoo.rlib.rust"],
Vinh Tran4eeb2a92023-08-14 13:29:30 -0400403 rustlibs: ["libfoo.dylib.rust"],
Jiyong Park99644e92020-11-17 22:21:02 +0900404 apex_available: ["myapex"],
405 }
406
407 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000408 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900409 srcs: ["foo.rs"],
410 crate_name: "foo",
411 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900412 shared_libs: ["libfoo.shared_from_rust"],
413 }
414
415 cc_library_shared {
416 name: "libfoo.shared_from_rust",
417 srcs: ["mylib.cpp"],
418 system_shared_libs: [],
419 stl: "none",
420 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900421 }
422
423 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000424 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900425 srcs: ["foo.rs"],
426 crate_name: "foo",
427 apex_available: ["myapex"],
428 }
429
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900430 rust_ffi_shared {
431 name: "libfoo.ffi",
432 srcs: ["foo.rs"],
433 crate_name: "foo",
434 apex_available: ["myapex"],
435 }
436
437 rust_ffi_shared {
438 name: "libbar.ffi",
439 srcs: ["foo.rs"],
440 crate_name: "bar",
441 apex_available: ["myapex"],
442 }
443
Paul Duffindddd5462020-04-07 15:25:44 +0100444 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900445 name: "mylib2",
446 srcs: ["mylib.cpp"],
447 system_shared_libs: [],
448 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900449 static_libs: ["libstatic"],
450 // TODO: remove //apex_available:platform
451 apex_available: [
452 "//apex_available:platform",
453 "myapex",
454 ],
455 }
456
Paul Duffindddd5462020-04-07 15:25:44 +0100457 cc_prebuilt_library_shared {
458 name: "mylib2",
459 srcs: ["prebuilt.so"],
460 // TODO: remove //apex_available:platform
461 apex_available: [
462 "//apex_available:platform",
463 "myapex",
464 ],
465 }
466
Jiyong Park9918e1a2020-03-17 19:16:40 +0900467 cc_library_static {
468 name: "libstatic",
469 srcs: ["mylib.cpp"],
470 system_shared_libs: [],
471 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000472 // TODO: remove //apex_available:platform
473 apex_available: [
474 "//apex_available:platform",
475 "myapex",
476 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900477 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900478
479 java_library {
480 name: "myjar",
481 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900482 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900483 sdk_version: "none",
484 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900485 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900486 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000487 // TODO: remove //apex_available:platform
488 apex_available: [
489 "//apex_available:platform",
490 "myapex",
491 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900492 }
493
Jiyong Park77acec62020-06-01 21:39:15 +0900494 dex_import {
495 name: "myjar_dex",
496 jars: ["prebuilt.jar"],
497 apex_available: [
498 "//apex_available:platform",
499 "myapex",
500 ],
501 }
502
Jiyong Park7f7766d2019-07-25 22:02:35 +0900503 java_library {
504 name: "myotherjar",
505 srcs: ["foo/bar/MyClass.java"],
506 sdk_version: "none",
507 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900508 // TODO: remove //apex_available:platform
509 apex_available: [
510 "//apex_available:platform",
511 "myapex",
512 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900513 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900514
515 java_library {
516 name: "mysharedjar",
517 srcs: ["foo/bar/MyClass.java"],
518 sdk_version: "none",
519 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900520 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900521 `)
522
Jooyung Hana0503a52023-08-23 13:12:50 +0900523 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900524
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900525 // Make sure that Android.mk is created
Jooyung Hana0503a52023-08-23 13:12:50 +0900526 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700527 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900528 var builder strings.Builder
529 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
530
531 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000532 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900533 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
534
Jiyong Park42cca6c2019-04-01 11:15:50 +0900535 optFlags := apexRule.Args["opt_flags"]
536 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700537 // Ensure that the NOTICE output is being packaged as an asset.
Jooyung Hana0503a52023-08-23 13:12:50 +0900538 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900539
Jiyong Park25fc6a92018-11-18 18:02:45 +0900540 copyCmds := apexRule.Args["copy_commands"]
541
542 // Ensure that main rule creates an output
543 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
544
545 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700546 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
547 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
548 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900549 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900550 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900551
552 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700553 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
554 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
556 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900557 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900558 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900559
560 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800561 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
562 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900563 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900564 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900566 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
567 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900568 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900569 // .. but not for java libs
570 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900571 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800572
Colin Cross7113d202019-11-20 16:39:12 -0800573 // Ensure that the platform variant ends with _shared or _common
574 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
575 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900576 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
577 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900578 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
579
580 // Ensure that dynamic dependency to java libs are not included
581 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800582
583 // Ensure that all symlinks are present.
584 found_foo_link_64 := false
585 found_foo := false
586 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900587 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800588 if strings.HasSuffix(cmd, "bin/foo") {
589 found_foo = true
590 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
591 found_foo_link_64 = true
592 }
593 }
594 }
595 good := found_foo && found_foo_link_64
596 if !good {
597 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
598 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900599
Colin Crossf61d03d2023-11-02 16:56:39 -0700600 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
601 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100602 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100603 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
604 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
605 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100606
Colin Crossf61d03d2023-11-02 16:56:39 -0700607 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
608 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100609 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100610 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
611 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
612 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800613}
614
Jooyung Hanf21c7972019-12-16 22:32:06 +0900615func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800616 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900617 apex_defaults {
618 name: "myapex-defaults",
619 key: "myapex.key",
620 prebuilts: ["myetc"],
621 native_shared_libs: ["mylib"],
622 java_libs: ["myjar"],
623 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900624 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800625 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000626 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900627 }
628
629 prebuilt_etc {
630 name: "myetc",
631 src: "myprebuilt",
632 }
633
634 apex {
635 name: "myapex",
636 defaults: ["myapex-defaults"],
637 }
638
639 apex_key {
640 name: "myapex.key",
641 public_key: "testkey.avbpubkey",
642 private_key: "testkey.pem",
643 }
644
645 cc_library {
646 name: "mylib",
647 system_shared_libs: [],
648 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000649 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900650 }
651
652 java_library {
653 name: "myjar",
654 srcs: ["foo/bar/MyClass.java"],
655 sdk_version: "none",
656 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000657 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900658 }
659
660 android_app {
661 name: "AppFoo",
662 srcs: ["foo/bar/MyClass.java"],
663 sdk_version: "none",
664 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000665 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900666 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900667
668 runtime_resource_overlay {
669 name: "rro",
670 theme: "blue",
671 }
672
markchien2f59ec92020-09-02 16:23:38 +0800673 bpf {
674 name: "bpf",
675 srcs: ["bpf.c", "bpf2.c"],
676 }
677
Ken Chenfad7f9d2021-11-10 22:02:57 +0800678 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800679 name: "netdTest",
680 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800681 sub_dir: "netd",
682 }
683
Jooyung Hanf21c7972019-12-16 22:32:06 +0900684 `)
Jooyung Hana0503a52023-08-23 13:12:50 +0900685 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900686 "etc/myetc",
687 "javalib/myjar.jar",
688 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000689 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900690 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800691 "etc/bpf/bpf.o",
692 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800693 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900694 })
695}
696
Jooyung Han01a3ee22019-11-02 02:52:25 +0900697func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800698 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900699 apex {
700 name: "myapex",
701 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000702 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900703 }
704
705 apex_key {
706 name: "myapex.key",
707 public_key: "testkey.avbpubkey",
708 private_key: "testkey.pem",
709 }
710 `)
711
Jooyung Hana0503a52023-08-23 13:12:50 +0900712 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +0900713 args := module.Rule("apexRule").Args
714 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
715 t.Error("manifest should be apex_manifest.pb, but " + manifest)
716 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900717}
718
Liz Kammer4854a7d2021-05-27 14:28:27 -0400719func TestApexManifestMinSdkVersion(t *testing.T) {
720 ctx := testApex(t, `
721 apex_defaults {
722 name: "my_defaults",
723 key: "myapex.key",
724 product_specific: true,
725 file_contexts: ":my-file-contexts",
726 updatable: false,
727 }
728 apex {
729 name: "myapex_30",
730 min_sdk_version: "30",
731 defaults: ["my_defaults"],
732 }
733
734 apex {
735 name: "myapex_current",
736 min_sdk_version: "current",
737 defaults: ["my_defaults"],
738 }
739
740 apex {
741 name: "myapex_none",
742 defaults: ["my_defaults"],
743 }
744
745 apex_key {
746 name: "myapex.key",
747 public_key: "testkey.avbpubkey",
748 private_key: "testkey.pem",
749 }
750
751 filegroup {
752 name: "my-file-contexts",
753 srcs: ["product_specific_file_contexts"],
754 }
755 `, withFiles(map[string][]byte{
756 "product_specific_file_contexts": nil,
757 }), android.FixtureModifyProductVariables(
758 func(variables android.FixtureProductVariables) {
759 variables.Unbundled_build = proptools.BoolPtr(true)
760 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
761 }), android.FixtureMergeEnv(map[string]string{
762 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
763 }))
764
765 testCases := []struct {
766 module string
767 minSdkVersion string
768 }{
769 {
770 module: "myapex_30",
771 minSdkVersion: "30",
772 },
773 {
774 module: "myapex_current",
775 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
776 },
777 {
778 module: "myapex_none",
779 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
780 },
781 }
782 for _, tc := range testCases {
Jooyung Hana0503a52023-08-23 13:12:50 +0900783 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
Liz Kammer4854a7d2021-05-27 14:28:27 -0400784 args := module.Rule("apexRule").Args
785 optFlags := args["opt_flags"]
786 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
787 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
788 }
789 }
790}
791
Jihoon Kang842b9992024-02-08 01:41:51 +0000792func TestApexWithDessertSha(t *testing.T) {
793 ctx := testApex(t, `
794 apex_defaults {
795 name: "my_defaults",
796 key: "myapex.key",
797 product_specific: true,
798 file_contexts: ":my-file-contexts",
799 updatable: false,
800 }
801 apex {
802 name: "myapex_30",
803 min_sdk_version: "30",
804 defaults: ["my_defaults"],
805 }
806
807 apex {
808 name: "myapex_current",
809 min_sdk_version: "current",
810 defaults: ["my_defaults"],
811 }
812
813 apex {
814 name: "myapex_none",
815 defaults: ["my_defaults"],
816 }
817
818 apex_key {
819 name: "myapex.key",
820 public_key: "testkey.avbpubkey",
821 private_key: "testkey.pem",
822 }
823
824 filegroup {
825 name: "my-file-contexts",
826 srcs: ["product_specific_file_contexts"],
827 }
828 `, withFiles(map[string][]byte{
829 "product_specific_file_contexts": nil,
830 }), android.FixtureModifyProductVariables(
831 func(variables android.FixtureProductVariables) {
832 variables.Unbundled_build = proptools.BoolPtr(true)
833 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
834 }), android.FixtureMergeEnv(map[string]string{
835 "UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA": "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
836 }))
837
838 testCases := []struct {
839 module string
840 minSdkVersion string
841 }{
842 {
843 module: "myapex_30",
844 minSdkVersion: "30",
845 },
846 {
847 module: "myapex_current",
848 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
849 },
850 {
851 module: "myapex_none",
852 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
853 },
854 }
855 for _, tc := range testCases {
856 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
857 args := module.Rule("apexRule").Args
858 optFlags := args["opt_flags"]
859 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
860 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
861 }
862 }
863}
864
Jooyung Hanaf730952023-02-28 14:13:38 +0900865func TestFileContexts(t *testing.T) {
Jooyung Hanbe953902023-05-31 16:42:16 +0900866 for _, vendor := range []bool{true, false} {
Jooyung Hanaf730952023-02-28 14:13:38 +0900867 prop := ""
Jooyung Hanbe953902023-05-31 16:42:16 +0900868 if vendor {
869 prop = "vendor: true,\n"
Jooyung Hanaf730952023-02-28 14:13:38 +0900870 }
871 ctx := testApex(t, `
872 apex {
873 name: "myapex",
874 key: "myapex.key",
Jooyung Hanaf730952023-02-28 14:13:38 +0900875 updatable: false,
Jooyung Hanaf730952023-02-28 14:13:38 +0900876 `+prop+`
877 }
878
879 apex_key {
880 name: "myapex.key",
881 public_key: "testkey.avbpubkey",
882 private_key: "testkey.pem",
883 }
Jooyung Hanbe953902023-05-31 16:42:16 +0900884 `)
Jooyung Hanaf730952023-02-28 14:13:38 +0900885
Jooyung Hana0503a52023-08-23 13:12:50 +0900886 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("file_contexts")
Jooyung Hanbe953902023-05-31 16:42:16 +0900887 if vendor {
888 android.AssertStringDoesContain(t, "should force-label as vendor_apex_metadata_file",
889 rule.RuleParams.Command,
890 "apex_manifest\\\\.pb u:object_r:vendor_apex_metadata_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900891 } else {
Jooyung Hanbe953902023-05-31 16:42:16 +0900892 android.AssertStringDoesContain(t, "should force-label as system_file",
893 rule.RuleParams.Command,
894 "apex_manifest\\\\.pb u:object_r:system_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900895 }
896 }
897}
898
Jiyong Park25fc6a92018-11-18 18:02:45 +0900899func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800900 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900901 apex {
902 name: "myapex",
903 key: "myapex.key",
904 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900905 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000906 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900907 }
908
909 apex_key {
910 name: "myapex.key",
911 public_key: "testkey.avbpubkey",
912 private_key: "testkey.pem",
913 }
914
915 cc_library {
916 name: "mylib",
917 srcs: ["mylib.cpp"],
Spandan Das357ffcc2024-07-24 18:07:48 +0000918 shared_libs: ["mylib2", "mylib3", "my_prebuilt_platform_lib", "my_prebuilt_platform_stub_only_lib"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900919 system_shared_libs: [],
920 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000921 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900922 }
923
924 cc_library {
925 name: "mylib2",
926 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900927 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900928 system_shared_libs: [],
929 stl: "none",
930 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +0000931 symbol_file: "mylib2.map.txt",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900932 versions: ["1", "2", "3"],
933 },
934 }
935
936 cc_library {
937 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900938 srcs: ["mylib.cpp"],
939 shared_libs: ["mylib4"],
940 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900941 stl: "none",
942 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +0000943 symbol_file: "mylib3.map.txt",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900944 versions: ["10", "11", "12"],
945 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000946 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900947 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900948
949 cc_library {
950 name: "mylib4",
951 srcs: ["mylib.cpp"],
952 system_shared_libs: [],
953 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000954 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900955 }
Jiyong Park105dc322021-06-11 17:22:09 +0900956
Spandan Das357ffcc2024-07-24 18:07:48 +0000957 cc_prebuilt_library_shared {
958 name: "my_prebuilt_platform_lib",
959 stubs: {
960 symbol_file: "my_prebuilt_platform_lib.map.txt",
961 versions: ["1", "2", "3"],
962 },
963 srcs: ["foo.so"],
964 }
965
966 // Similar to my_prebuilt_platform_lib, but this library only provides stubs, i.e. srcs is empty
967 cc_prebuilt_library_shared {
968 name: "my_prebuilt_platform_stub_only_lib",
969 stubs: {
970 symbol_file: "my_prebuilt_platform_stub_only_lib.map.txt",
971 versions: ["1", "2", "3"],
972 }
973 }
974
Jiyong Park105dc322021-06-11 17:22:09 +0900975 rust_binary {
976 name: "foo.rust",
977 srcs: ["foo.rs"],
978 shared_libs: ["libfoo.shared_from_rust"],
979 prefer_rlib: true,
980 apex_available: ["myapex"],
981 }
982
983 cc_library_shared {
984 name: "libfoo.shared_from_rust",
985 srcs: ["mylib.cpp"],
986 system_shared_libs: [],
987 stl: "none",
988 stubs: {
989 versions: ["10", "11", "12"],
990 },
991 }
992
Jiyong Park25fc6a92018-11-18 18:02:45 +0900993 `)
994
Jooyung Hana0503a52023-08-23 13:12:50 +0900995 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900996 copyCmds := apexRule.Args["copy_commands"]
997
998 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800999 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001000
1001 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -08001002 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001003
1004 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -08001005 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001006
Colin Crossaede88c2020-08-11 12:17:01 -07001007 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001008
1009 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001010 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001011 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +09001012 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001013
1014 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -07001015 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001016 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -07001017 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +09001018
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -07001019 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
1020 // is replaced by sharing of "cFlags" in cc/builder.go.
1021 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
1022 // module variable representing "cflags". So it was not detected by ensureNotContains.
1023 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
1024 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
1025 // including the original cflags's "-include mylib.h".
1026 //
Jiyong Park64379952018-12-13 18:37:29 +09001027 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -07001028 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1029 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +09001030
Jiyong Park85cc35a2022-07-17 11:30:47 +09001031 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1032 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
1033 // Ensure that genstub for apex-provided lib is invoked with --apex
1034 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +09001035
Jooyung Hana0503a52023-08-23 13:12:50 +09001036 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +09001037 "lib64/mylib.so",
1038 "lib64/mylib3.so",
1039 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +09001040 "bin/foo.rust",
1041 "lib64/libc++.so", // by the implicit dependency from foo.rust
1042 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +09001043 })
Jiyong Park105dc322021-06-11 17:22:09 +09001044
1045 // Ensure that stub dependency from a rust module is not included
1046 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1047 // The rust module is linked to the stub cc library
Colin Cross004bd3f2023-10-02 11:39:17 -07001048 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park105dc322021-06-11 17:22:09 +09001049 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1050 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +09001051
Jooyung Hana0503a52023-08-23 13:12:50 +09001052 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jiyong Park34d5c332022-02-24 18:02:44 +09001053 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Spandan Das357ffcc2024-07-24 18:07:48 +00001054
1055 // Ensure that mylib is linking with the latest version of stubs for my_prebuilt_platform_lib
1056 ensureContains(t, mylibLdFlags, "my_prebuilt_platform_lib/android_arm64_armv8-a_shared_current/my_prebuilt_platform_lib.so")
1057 // ... and not linking to the non-stub (impl) variant of my_prebuilt_platform_lib
1058 ensureNotContains(t, mylibLdFlags, "my_prebuilt_platform_lib/android_arm64_armv8-a_shared/my_prebuilt_platform_lib.so")
1059 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1060 ensureContains(t, ctx.ModuleForTests("my_prebuilt_platform_lib", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
1061
1062 // Ensure that mylib is linking with the latest version of stubs for my_prebuilt_platform_lib
1063 ensureContains(t, mylibLdFlags, "my_prebuilt_platform_stub_only_lib/android_arm64_armv8-a_shared_current/my_prebuilt_platform_stub_only_lib.so")
1064 // ... and not linking to the non-stub (impl) variant of my_prebuilt_platform_lib
1065 ensureNotContains(t, mylibLdFlags, "my_prebuilt_platform_stub_only_lib/android_arm64_armv8-a_shared/my_prebuilt_platform_stub_only_lib.so")
1066 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1067 ensureContains(t, ctx.ModuleForTests("my_prebuilt_platform_stub_only_lib", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001068}
1069
Jooyung Han20348752023-12-05 15:23:56 +09001070func TestApexShouldNotEmbedStubVariant(t *testing.T) {
1071 testApexError(t, `module "myapex" .*: native_shared_libs: "libbar" is a stub`, `
1072 apex {
1073 name: "myapex",
1074 key: "myapex.key",
1075 vendor: true,
1076 updatable: false,
1077 native_shared_libs: ["libbar"], // should not add an LLNDK stub in a vendor apex
1078 }
1079
1080 apex_key {
1081 name: "myapex.key",
1082 public_key: "testkey.avbpubkey",
1083 private_key: "testkey.pem",
1084 }
1085
1086 cc_library {
1087 name: "libbar",
1088 srcs: ["mylib.cpp"],
1089 llndk: {
1090 symbol_file: "libbar.map.txt",
1091 }
1092 }
1093 `)
1094}
1095
Jiyong Park1bc84122021-06-22 20:23:05 +09001096func TestApexCanUsePrivateApis(t *testing.T) {
1097 ctx := testApex(t, `
1098 apex {
1099 name: "myapex",
1100 key: "myapex.key",
1101 native_shared_libs: ["mylib"],
1102 binaries: ["foo.rust"],
1103 updatable: false,
1104 platform_apis: true,
1105 }
1106
1107 apex_key {
1108 name: "myapex.key",
1109 public_key: "testkey.avbpubkey",
1110 private_key: "testkey.pem",
1111 }
1112
1113 cc_library {
1114 name: "mylib",
1115 srcs: ["mylib.cpp"],
1116 shared_libs: ["mylib2"],
1117 system_shared_libs: [],
1118 stl: "none",
1119 apex_available: [ "myapex" ],
1120 }
1121
1122 cc_library {
1123 name: "mylib2",
1124 srcs: ["mylib.cpp"],
1125 cflags: ["-include mylib.h"],
1126 system_shared_libs: [],
1127 stl: "none",
1128 stubs: {
1129 versions: ["1", "2", "3"],
1130 },
1131 }
1132
1133 rust_binary {
1134 name: "foo.rust",
1135 srcs: ["foo.rs"],
1136 shared_libs: ["libfoo.shared_from_rust"],
1137 prefer_rlib: true,
1138 apex_available: ["myapex"],
1139 }
1140
1141 cc_library_shared {
1142 name: "libfoo.shared_from_rust",
1143 srcs: ["mylib.cpp"],
1144 system_shared_libs: [],
1145 stl: "none",
1146 stubs: {
1147 versions: ["10", "11", "12"],
1148 },
1149 }
1150 `)
1151
Jooyung Hana0503a52023-08-23 13:12:50 +09001152 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park1bc84122021-06-22 20:23:05 +09001153 copyCmds := apexRule.Args["copy_commands"]
1154
1155 // Ensure that indirect stubs dep is not included
1156 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1157 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1158
1159 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1160 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001161 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001162 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1163 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Colin Cross004bd3f2023-10-02 11:39:17 -07001164 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001165 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1166 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1167}
1168
Colin Cross7812fd32020-09-25 12:35:10 -07001169func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1170 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001171 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001172 apex {
1173 name: "myapex",
1174 key: "myapex.key",
1175 native_shared_libs: ["mylib", "mylib3"],
1176 min_sdk_version: "29",
1177 }
1178
1179 apex_key {
1180 name: "myapex.key",
1181 public_key: "testkey.avbpubkey",
1182 private_key: "testkey.pem",
1183 }
1184
1185 cc_library {
1186 name: "mylib",
1187 srcs: ["mylib.cpp"],
1188 shared_libs: ["mylib2", "mylib3"],
1189 system_shared_libs: [],
1190 stl: "none",
1191 apex_available: [ "myapex" ],
1192 min_sdk_version: "28",
1193 }
1194
1195 cc_library {
1196 name: "mylib2",
1197 srcs: ["mylib.cpp"],
1198 cflags: ["-include mylib.h"],
1199 system_shared_libs: [],
1200 stl: "none",
1201 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +00001202 symbol_file: "mylib2.map.txt",
Colin Cross7812fd32020-09-25 12:35:10 -07001203 versions: ["28", "29", "30", "current"],
1204 },
1205 min_sdk_version: "28",
1206 }
1207
1208 cc_library {
1209 name: "mylib3",
1210 srcs: ["mylib.cpp"],
1211 shared_libs: ["mylib4"],
1212 system_shared_libs: [],
1213 stl: "none",
1214 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +00001215 symbol_file: "mylib3.map.txt",
Colin Cross7812fd32020-09-25 12:35:10 -07001216 versions: ["28", "29", "30", "current"],
1217 },
1218 apex_available: [ "myapex" ],
1219 min_sdk_version: "28",
1220 }
1221
1222 cc_library {
1223 name: "mylib4",
1224 srcs: ["mylib.cpp"],
1225 system_shared_libs: [],
1226 stl: "none",
1227 apex_available: [ "myapex" ],
1228 min_sdk_version: "28",
1229 }
1230 `)
1231
Jooyung Hana0503a52023-08-23 13:12:50 +09001232 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Colin Cross7812fd32020-09-25 12:35:10 -07001233 copyCmds := apexRule.Args["copy_commands"]
1234
1235 // Ensure that direct non-stubs dep is always included
1236 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1237
1238 // Ensure that indirect stubs dep is not included
1239 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1240
1241 // Ensure that direct stubs dep is included
1242 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1243
1244 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1245
Jiyong Park55549df2021-02-26 23:57:23 +09001246 // Ensure that mylib is linking with the latest version of stub for mylib2
1247 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001248 // ... and not linking to the non-stub (impl) variant of mylib2
1249 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1250
1251 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1252 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1253 // .. and not linking to the stubs variant of mylib3
1254 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1255
1256 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001257 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001258 ensureNotContains(t, mylib2Cflags, "-include ")
1259
Jiyong Park85cc35a2022-07-17 11:30:47 +09001260 // Ensure that genstub is invoked with --systemapi
1261 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001262
Jooyung Hana0503a52023-08-23 13:12:50 +09001263 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Colin Cross7812fd32020-09-25 12:35:10 -07001264 "lib64/mylib.so",
1265 "lib64/mylib3.so",
1266 "lib64/mylib4.so",
1267 })
1268}
1269
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001270func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1271 t.Parallel()
1272 // myapex (Z)
1273 // mylib -----------------.
1274 // |
1275 // otherapex (29) |
1276 // libstub's versions: 29 Z current
1277 // |
1278 // <platform> |
1279 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001280 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001281 apex {
1282 name: "myapex",
1283 key: "myapex.key",
1284 native_shared_libs: ["mylib"],
1285 min_sdk_version: "Z", // non-final
1286 }
1287
1288 cc_library {
1289 name: "mylib",
1290 srcs: ["mylib.cpp"],
1291 shared_libs: ["libstub"],
1292 apex_available: ["myapex"],
1293 min_sdk_version: "Z",
1294 }
1295
1296 apex_key {
1297 name: "myapex.key",
1298 public_key: "testkey.avbpubkey",
1299 private_key: "testkey.pem",
1300 }
1301
1302 apex {
1303 name: "otherapex",
1304 key: "myapex.key",
1305 native_shared_libs: ["libstub"],
1306 min_sdk_version: "29",
1307 }
1308
1309 cc_library {
1310 name: "libstub",
1311 srcs: ["mylib.cpp"],
1312 stubs: {
1313 versions: ["29", "Z", "current"],
1314 },
1315 apex_available: ["otherapex"],
1316 min_sdk_version: "29",
1317 }
1318
1319 // platform module depending on libstub from otherapex should use the latest stub("current")
1320 cc_library {
1321 name: "libplatform",
1322 srcs: ["mylib.cpp"],
1323 shared_libs: ["libstub"],
1324 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001325 `,
1326 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1327 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1328 variables.Platform_sdk_final = proptools.BoolPtr(false)
1329 variables.Platform_version_active_codenames = []string{"Z"}
1330 }),
1331 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001332
Jiyong Park55549df2021-02-26 23:57:23 +09001333 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001334 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001335 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001336 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001337 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001338
1339 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1340 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1341 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1342 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1343 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1344}
1345
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001346func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001347 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001348 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001349 name: "myapex2",
1350 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001351 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001352 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001353 }
1354
1355 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001356 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001357 public_key: "testkey.avbpubkey",
1358 private_key: "testkey.pem",
1359 }
1360
1361 cc_library {
1362 name: "mylib",
1363 srcs: ["mylib.cpp"],
1364 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001365 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001366 system_shared_libs: [],
1367 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001368 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001369 }
1370
1371 cc_library {
1372 name: "libfoo",
1373 srcs: ["mylib.cpp"],
1374 shared_libs: ["libbar"],
1375 system_shared_libs: [],
1376 stl: "none",
1377 stubs: {
1378 versions: ["10", "20", "30"],
1379 },
1380 }
1381
1382 cc_library {
1383 name: "libbar",
1384 srcs: ["mylib.cpp"],
1385 system_shared_libs: [],
1386 stl: "none",
1387 }
1388
Jiyong Park678c8812020-02-07 17:25:49 +09001389 cc_library_static {
1390 name: "libbaz",
1391 srcs: ["mylib.cpp"],
1392 system_shared_libs: [],
1393 stl: "none",
1394 apex_available: [ "myapex2" ],
1395 }
1396
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001397 `)
1398
Jooyung Hana0503a52023-08-23 13:12:50 +09001399 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001400 copyCmds := apexRule.Args["copy_commands"]
1401
1402 // Ensure that direct non-stubs dep is always included
1403 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1404
1405 // Ensure that indirect stubs dep is not included
1406 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1407
1408 // Ensure that dependency of stubs is not included
1409 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1410
Colin Crossaede88c2020-08-11 12:17:01 -07001411 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001412
1413 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001414 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001415 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001416 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001417
Jiyong Park3ff16992019-12-27 14:11:47 +09001418 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001419
1420 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1421 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001422
Colin Crossf61d03d2023-11-02 16:56:39 -07001423 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1424 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001425 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001426
Colin Crossf61d03d2023-11-02 16:56:39 -07001427 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1428 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001429 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001430}
1431
Jooyung Hand3639552019-08-09 12:57:43 +09001432func TestApexWithRuntimeLibsDependency(t *testing.T) {
1433 /*
1434 myapex
1435 |
1436 v (runtime_libs)
1437 mylib ------+------> libfoo [provides stub]
1438 |
1439 `------> libbar
1440 */
Colin Cross1c460562021-02-16 17:55:47 -08001441 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001442 apex {
1443 name: "myapex",
1444 key: "myapex.key",
1445 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001446 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001447 }
1448
1449 apex_key {
1450 name: "myapex.key",
1451 public_key: "testkey.avbpubkey",
1452 private_key: "testkey.pem",
1453 }
1454
1455 cc_library {
1456 name: "mylib",
1457 srcs: ["mylib.cpp"],
Liz Kammer5f108fa2023-05-11 14:33:17 -04001458 static_libs: ["libstatic"],
1459 shared_libs: ["libshared"],
Jooyung Hand3639552019-08-09 12:57:43 +09001460 runtime_libs: ["libfoo", "libbar"],
1461 system_shared_libs: [],
1462 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001463 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001464 }
1465
1466 cc_library {
1467 name: "libfoo",
1468 srcs: ["mylib.cpp"],
1469 system_shared_libs: [],
1470 stl: "none",
1471 stubs: {
1472 versions: ["10", "20", "30"],
1473 },
1474 }
1475
1476 cc_library {
1477 name: "libbar",
1478 srcs: ["mylib.cpp"],
1479 system_shared_libs: [],
1480 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001481 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001482 }
1483
Liz Kammer5f108fa2023-05-11 14:33:17 -04001484 cc_library {
1485 name: "libstatic",
1486 srcs: ["mylib.cpp"],
1487 system_shared_libs: [],
1488 stl: "none",
1489 apex_available: [ "myapex" ],
1490 runtime_libs: ["libstatic_to_runtime"],
1491 }
1492
1493 cc_library {
1494 name: "libshared",
1495 srcs: ["mylib.cpp"],
1496 system_shared_libs: [],
1497 stl: "none",
1498 apex_available: [ "myapex" ],
1499 runtime_libs: ["libshared_to_runtime"],
1500 }
1501
1502 cc_library {
1503 name: "libstatic_to_runtime",
1504 srcs: ["mylib.cpp"],
1505 system_shared_libs: [],
1506 stl: "none",
1507 apex_available: [ "myapex" ],
1508 }
1509
1510 cc_library {
1511 name: "libshared_to_runtime",
1512 srcs: ["mylib.cpp"],
1513 system_shared_libs: [],
1514 stl: "none",
1515 apex_available: [ "myapex" ],
1516 }
Jooyung Hand3639552019-08-09 12:57:43 +09001517 `)
1518
Jooyung Hana0503a52023-08-23 13:12:50 +09001519 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001520 copyCmds := apexRule.Args["copy_commands"]
1521
1522 // Ensure that direct non-stubs dep is always included
1523 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1524
1525 // Ensure that indirect stubs dep is not included
1526 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1527
1528 // Ensure that runtime_libs dep in included
1529 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
Liz Kammer5f108fa2023-05-11 14:33:17 -04001530 ensureContains(t, copyCmds, "image.apex/lib64/libshared.so")
1531 ensureContains(t, copyCmds, "image.apex/lib64/libshared_to_runtime.so")
1532
1533 ensureNotContains(t, copyCmds, "image.apex/lib64/libstatic_to_runtime.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001534
Jooyung Hana0503a52023-08-23 13:12:50 +09001535 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001536 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1537 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001538}
1539
Paul Duffina02cae32021-03-09 01:44:06 +00001540var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1541 cc.PrepareForTestWithCcBuildComponents,
1542 PrepareForTestWithApexBuildComponents,
1543 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001544 apex {
1545 name: "com.android.runtime",
1546 key: "com.android.runtime.key",
1547 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001548 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001549 }
1550
1551 apex_key {
1552 name: "com.android.runtime.key",
1553 public_key: "testkey.avbpubkey",
1554 private_key: "testkey.pem",
1555 }
Paul Duffina02cae32021-03-09 01:44:06 +00001556 `),
1557 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1558)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001559
Paul Duffina02cae32021-03-09 01:44:06 +00001560func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001561 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001562 cc_library {
1563 name: "libc",
1564 no_libcrt: true,
1565 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001566 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001567 stl: "none",
1568 system_shared_libs: [],
1569 stubs: { versions: ["1"] },
1570 apex_available: ["com.android.runtime"],
1571
1572 sanitize: {
1573 hwaddress: true,
1574 }
1575 }
1576
1577 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001578 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001579 no_libcrt: true,
1580 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001581 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001582 stl: "none",
1583 system_shared_libs: [],
1584 srcs: [""],
1585 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001586 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001587
1588 sanitize: {
1589 never: true,
1590 },
Spandan Das4de7b492023-05-05 21:13:01 +00001591 apex_available: [
1592 "//apex_available:anyapex",
1593 "//apex_available:platform",
1594 ],
Paul Duffina02cae32021-03-09 01:44:06 +00001595 } `)
1596 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001597
Jooyung Hana0503a52023-08-23 13:12:50 +09001598 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001599 "lib64/bionic/libc.so",
1600 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1601 })
1602
Colin Cross4c4c1be2022-02-10 11:41:18 -08001603 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001604
1605 installed := hwasan.Description("install libclang_rt.hwasan")
1606 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1607
1608 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1609 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1610 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1611}
1612
1613func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001614 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001615 prepareForTestOfRuntimeApexWithHwasan,
1616 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1617 variables.SanitizeDevice = []string{"hwaddress"}
1618 }),
1619 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001620 cc_library {
1621 name: "libc",
1622 no_libcrt: true,
1623 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001624 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001625 stl: "none",
1626 system_shared_libs: [],
1627 stubs: { versions: ["1"] },
1628 apex_available: ["com.android.runtime"],
1629 }
1630
1631 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001632 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001633 no_libcrt: true,
1634 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001635 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001636 stl: "none",
1637 system_shared_libs: [],
1638 srcs: [""],
1639 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001640 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001641
1642 sanitize: {
1643 never: true,
1644 },
Spandan Das4de7b492023-05-05 21:13:01 +00001645 apex_available: [
1646 "//apex_available:anyapex",
1647 "//apex_available:platform",
1648 ],
Jooyung Han8ce8db92020-05-15 19:05:05 +09001649 }
Paul Duffina02cae32021-03-09 01:44:06 +00001650 `)
1651 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001652
Jooyung Hana0503a52023-08-23 13:12:50 +09001653 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001654 "lib64/bionic/libc.so",
1655 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1656 })
1657
Colin Cross4c4c1be2022-02-10 11:41:18 -08001658 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001659
1660 installed := hwasan.Description("install libclang_rt.hwasan")
1661 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1662
1663 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1664 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1665 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1666}
1667
Jooyung Han61b66e92020-03-21 14:21:46 +00001668func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1669 testcases := []struct {
1670 name string
1671 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001672 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001673 shouldLink string
1674 shouldNotLink []string
1675 }{
1676 {
Jiyong Park55549df2021-02-26 23:57:23 +09001677 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001678 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001679 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001680 shouldLink: "current",
1681 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001682 },
1683 {
Jiyong Park55549df2021-02-26 23:57:23 +09001684 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001685 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001686 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001687 shouldLink: "current",
1688 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001689 },
1690 }
1691 for _, tc := range testcases {
1692 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001693 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001694 apex {
1695 name: "myapex",
1696 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001697 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001698 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001699 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001700 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001701
Jooyung Han61b66e92020-03-21 14:21:46 +00001702 apex_key {
1703 name: "myapex.key",
1704 public_key: "testkey.avbpubkey",
1705 private_key: "testkey.pem",
1706 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001707
Jooyung Han61b66e92020-03-21 14:21:46 +00001708 cc_library {
1709 name: "mylib",
1710 srcs: ["mylib.cpp"],
1711 vendor_available: true,
1712 shared_libs: ["libbar"],
1713 system_shared_libs: [],
1714 stl: "none",
1715 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001716 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001717 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001718
Jooyung Han61b66e92020-03-21 14:21:46 +00001719 cc_library {
1720 name: "libbar",
1721 srcs: ["mylib.cpp"],
1722 system_shared_libs: [],
1723 stl: "none",
1724 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001725 llndk: {
1726 symbol_file: "libbar.map.txt",
1727 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001728 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001729 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001730 withUnbundledBuild,
1731 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001732
Jooyung Han61b66e92020-03-21 14:21:46 +00001733 // Ensure that LLNDK dep is not included
Jooyung Hana0503a52023-08-23 13:12:50 +09001734 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00001735 "lib64/mylib.so",
1736 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001737
Jooyung Han61b66e92020-03-21 14:21:46 +00001738 // Ensure that LLNDK dep is required
Jooyung Hana0503a52023-08-23 13:12:50 +09001739 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han61b66e92020-03-21 14:21:46 +00001740 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1741 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001742
Steven Moreland2c4000c2021-04-27 02:08:49 +00001743 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1744 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001745 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001746 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001747 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001748
Steven Moreland2c4000c2021-04-27 02:08:49 +00001749 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001750 ver := tc.shouldLink
1751 if tc.shouldLink == "current" {
1752 ver = strconv.Itoa(android.FutureApiLevelInt)
1753 }
1754 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001755 })
1756 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001757}
1758
Jiyong Park25fc6a92018-11-18 18:02:45 +09001759func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001760 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001761 apex {
1762 name: "myapex",
1763 key: "myapex.key",
1764 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001765 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001766 }
1767
1768 apex_key {
1769 name: "myapex.key",
1770 public_key: "testkey.avbpubkey",
1771 private_key: "testkey.pem",
1772 }
1773
1774 cc_library {
1775 name: "mylib",
1776 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001777 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001778 shared_libs: ["libdl#27"],
1779 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001780 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001781 }
1782
1783 cc_library_shared {
1784 name: "mylib_shared",
1785 srcs: ["mylib.cpp"],
1786 shared_libs: ["libdl#27"],
1787 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001788 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001789 }
1790
1791 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001792 name: "libBootstrap",
1793 srcs: ["mylib.cpp"],
1794 stl: "none",
1795 bootstrap: true,
1796 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001797 `)
1798
Jooyung Hana0503a52023-08-23 13:12:50 +09001799 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001800 copyCmds := apexRule.Args["copy_commands"]
1801
1802 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001803 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001804 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1805 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001806
1807 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001808 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001809
Colin Crossaede88c2020-08-11 12:17:01 -07001810 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1811 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1812 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001813
1814 // For dependency to libc
1815 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001816 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001817 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001818 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001819 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001820 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1821 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001822
1823 // For dependency to libm
1824 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001825 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001826 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001827 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001828 // ... and is not compiling with the stub
1829 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1830 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1831
1832 // For dependency to libdl
1833 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001834 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001835 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001836 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1837 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001838 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001839 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001840 // ... Cflags from stub is correctly exported to mylib
1841 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1842 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001843
1844 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001845 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1846 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1847 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1848 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001849}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001850
Jooyung Han749dc692020-04-15 11:03:39 +09001851func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001852 // there are three links between liba --> libz.
1853 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001854 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001855 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001856 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001857 apex {
1858 name: "myapex",
1859 key: "myapex.key",
1860 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001861 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001862 }
1863
1864 apex {
1865 name: "otherapex",
1866 key: "myapex.key",
1867 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001868 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001869 }
1870
1871 apex_key {
1872 name: "myapex.key",
1873 public_key: "testkey.avbpubkey",
1874 private_key: "testkey.pem",
1875 }
1876
1877 cc_library {
1878 name: "libx",
1879 shared_libs: ["liba"],
1880 system_shared_libs: [],
1881 stl: "none",
1882 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001883 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001884 }
1885
1886 cc_library {
1887 name: "liby",
1888 shared_libs: ["liba"],
1889 system_shared_libs: [],
1890 stl: "none",
1891 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001892 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001893 }
1894
1895 cc_library {
1896 name: "liba",
1897 shared_libs: ["libz"],
1898 system_shared_libs: [],
1899 stl: "none",
1900 apex_available: [
1901 "//apex_available:anyapex",
1902 "//apex_available:platform",
1903 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001904 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001905 }
1906
1907 cc_library {
1908 name: "libz",
1909 system_shared_libs: [],
1910 stl: "none",
1911 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001912 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001913 },
1914 }
Jooyung Han749dc692020-04-15 11:03:39 +09001915 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001916
1917 expectLink := func(from, from_variant, to, to_variant string) {
1918 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1919 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1920 }
1921 expectNoLink := func(from, from_variant, to, to_variant string) {
1922 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1923 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1924 }
1925 // platform liba is linked to non-stub version
1926 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001927 // liba in myapex is linked to current
1928 expectLink("liba", "shared_apex29", "libz", "shared_current")
1929 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001930 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001931 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001932 // liba in otherapex is linked to current
1933 expectLink("liba", "shared_apex30", "libz", "shared_current")
1934 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001935 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1936 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001937}
1938
Jooyung Hanaed150d2020-04-02 01:41:41 +09001939func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001940 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001941 apex {
1942 name: "myapex",
1943 key: "myapex.key",
1944 native_shared_libs: ["libx"],
1945 min_sdk_version: "R",
1946 }
1947
1948 apex_key {
1949 name: "myapex.key",
1950 public_key: "testkey.avbpubkey",
1951 private_key: "testkey.pem",
1952 }
1953
1954 cc_library {
1955 name: "libx",
1956 shared_libs: ["libz"],
1957 system_shared_libs: [],
1958 stl: "none",
1959 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001960 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001961 }
1962
1963 cc_library {
1964 name: "libz",
1965 system_shared_libs: [],
1966 stl: "none",
1967 stubs: {
1968 versions: ["29", "R"],
1969 },
1970 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001971 `,
1972 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1973 variables.Platform_version_active_codenames = []string{"R"}
1974 }),
1975 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001976
1977 expectLink := func(from, from_variant, to, to_variant string) {
1978 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1979 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1980 }
1981 expectNoLink := func(from, from_variant, to, to_variant string) {
1982 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1983 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1984 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001985 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1986 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001987 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1988 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001989}
1990
Jooyung Han4c4da062021-06-23 10:23:16 +09001991func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1992 testApex(t, `
1993 apex {
1994 name: "myapex",
1995 key: "myapex.key",
1996 java_libs: ["libx"],
1997 min_sdk_version: "S",
1998 }
1999
2000 apex_key {
2001 name: "myapex.key",
2002 public_key: "testkey.avbpubkey",
2003 private_key: "testkey.pem",
2004 }
2005
2006 java_library {
2007 name: "libx",
2008 srcs: ["a.java"],
2009 apex_available: [ "myapex" ],
2010 sdk_version: "current",
2011 min_sdk_version: "S", // should be okay
2012 }
2013 `,
2014 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2015 variables.Platform_version_active_codenames = []string{"S"}
2016 variables.Platform_sdk_codename = proptools.StringPtr("S")
2017 }),
2018 )
2019}
2020
Jooyung Han749dc692020-04-15 11:03:39 +09002021func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002022 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002023 apex {
2024 name: "myapex",
2025 key: "myapex.key",
2026 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002027 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002028 }
2029
2030 apex_key {
2031 name: "myapex.key",
2032 public_key: "testkey.avbpubkey",
2033 private_key: "testkey.pem",
2034 }
2035
2036 cc_library {
2037 name: "libx",
2038 shared_libs: ["libz"],
2039 system_shared_libs: [],
2040 stl: "none",
2041 apex_available: [ "myapex" ],
2042 }
2043
2044 cc_library {
2045 name: "libz",
2046 system_shared_libs: [],
2047 stl: "none",
2048 stubs: {
2049 versions: ["1", "2"],
2050 },
2051 }
2052 `)
2053
2054 expectLink := func(from, from_variant, to, to_variant string) {
2055 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2056 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2057 }
2058 expectNoLink := func(from, from_variant, to, to_variant string) {
2059 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2060 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2061 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002062 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002063 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002064 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07002065 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09002066}
2067
Jooyung Handfc864c2023-03-20 18:19:07 +09002068func TestApexMinSdkVersion_InVendorApex(t *testing.T) {
Jiyong Park5df7bd32021-08-25 16:18:46 +09002069 ctx := testApex(t, `
2070 apex {
2071 name: "myapex",
2072 key: "myapex.key",
2073 native_shared_libs: ["mylib"],
Jooyung Handfc864c2023-03-20 18:19:07 +09002074 updatable: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002075 vendor: true,
2076 min_sdk_version: "29",
2077 }
2078
2079 apex_key {
2080 name: "myapex.key",
2081 public_key: "testkey.avbpubkey",
2082 private_key: "testkey.pem",
2083 }
2084
2085 cc_library {
2086 name: "mylib",
Jooyung Handfc864c2023-03-20 18:19:07 +09002087 srcs: ["mylib.cpp"],
Jiyong Park5df7bd32021-08-25 16:18:46 +09002088 vendor_available: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002089 min_sdk_version: "29",
Jooyung Handfc864c2023-03-20 18:19:07 +09002090 shared_libs: ["libbar"],
2091 }
2092
2093 cc_library {
2094 name: "libbar",
2095 stubs: { versions: ["29", "30"] },
2096 llndk: { symbol_file: "libbar.map.txt" },
Jiyong Park5df7bd32021-08-25 16:18:46 +09002097 }
2098 `)
2099
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002100 vendorVariant := "android_vendor_arm64_armv8-a"
Jiyong Park5df7bd32021-08-25 16:18:46 +09002101
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002102 mylib := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29")
Jooyung Handfc864c2023-03-20 18:19:07 +09002103
2104 // Ensure that mylib links with "current" LLNDK
2105 libFlags := names(mylib.Rule("ld").Args["libFlags"])
Jooyung Han5e8994e2024-03-12 14:12:12 +09002106 ensureListContains(t, libFlags, "out/soong/.intermediates/libbar/"+vendorVariant+"_shared/libbar.so")
Jooyung Handfc864c2023-03-20 18:19:07 +09002107
2108 // Ensure that mylib is targeting 29
2109 ccRule := ctx.ModuleForTests("mylib", vendorVariant+"_static_apex29").Output("obj/mylib.o")
2110 ensureContains(t, ccRule.Args["cFlags"], "-target aarch64-linux-android29")
2111
2112 // Ensure that the correct variant of crtbegin_so is used.
2113 crtBegin := mylib.Rule("ld").Args["crtBegin"]
2114 ensureContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
Jiyong Park5df7bd32021-08-25 16:18:46 +09002115
2116 // Ensure that the crtbegin_so used by the APEX is targeting 29
2117 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
2118 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
2119}
2120
Jooyung Han4495f842023-04-25 16:39:59 +09002121func TestTrackAllowedDeps(t *testing.T) {
2122 ctx := testApex(t, `
2123 apex {
2124 name: "myapex",
2125 key: "myapex.key",
2126 updatable: true,
2127 native_shared_libs: [
2128 "mylib",
2129 "yourlib",
2130 ],
2131 min_sdk_version: "29",
2132 }
2133
2134 apex {
2135 name: "myapex2",
2136 key: "myapex.key",
2137 updatable: false,
2138 native_shared_libs: ["yourlib"],
2139 }
2140
2141 apex_key {
2142 name: "myapex.key",
2143 public_key: "testkey.avbpubkey",
2144 private_key: "testkey.pem",
2145 }
2146
2147 cc_library {
2148 name: "mylib",
2149 srcs: ["mylib.cpp"],
2150 shared_libs: ["libbar"],
2151 min_sdk_version: "29",
2152 apex_available: ["myapex"],
2153 }
2154
2155 cc_library {
2156 name: "libbar",
2157 stubs: { versions: ["29", "30"] },
2158 }
2159
2160 cc_library {
2161 name: "yourlib",
2162 srcs: ["mylib.cpp"],
2163 min_sdk_version: "29",
2164 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2165 }
2166 `, withFiles(android.MockFS{
2167 "packages/modules/common/build/allowed_deps.txt": nil,
2168 }))
2169
2170 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2171 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2172 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002173 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002174 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002175 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002176
Jooyung Hana0503a52023-08-23 13:12:50 +09002177 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07002178 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2179 myapex.Output("depsinfo/flatlist.txt")), "\n")
Jooyung Han4495f842023-04-25 16:39:59 +09002180 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2181 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2182 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2183 flatlist, "mylib:(minSdkVersion:29)")
2184 android.AssertStringListContains(t, "track platform-available lib",
2185 flatlist, "yourlib(minSdkVersion:29)")
2186}
2187
Nouby Mohamed3413f302024-09-04 22:19:51 +00002188func TestTrackCustomAllowedDepsInvalidDefaultTxt(t *testing.T) {
2189 ctx := testApex(t, `
2190 apex {
2191 name: "myapex",
2192 key: "myapex.key",
2193 updatable: true,
2194 native_shared_libs: [
2195 "mylib",
2196 "yourlib",
2197 ],
2198 min_sdk_version: "29",
2199 }
2200
2201 apex {
2202 name: "myapex2",
2203 key: "myapex.key",
2204 updatable: false,
2205 native_shared_libs: ["yourlib"],
2206 }
2207
2208 apex_key {
2209 name: "myapex.key",
2210 public_key: "testkey.avbpubkey",
2211 private_key: "testkey.pem",
2212 }
2213
2214 cc_library {
2215 name: "mylib",
2216 srcs: ["mylib.cpp"],
2217 shared_libs: ["libbar"],
2218 min_sdk_version: "29",
2219 apex_available: ["myapex"],
2220 }
2221
2222 cc_library {
2223 name: "libbar",
2224 stubs: { versions: ["29", "30"] },
2225 }
2226
2227 cc_library {
2228 name: "yourlib",
2229 srcs: ["mylib.cpp"],
2230 min_sdk_version: "29",
2231 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2232 }
2233 `, withFiles(android.MockFS{
2234 "packages/modules/common/build/custom_allowed_deps.txt": nil,
2235 }),
2236 android.FixtureModifyProductVariables(
2237 func(variables android.FixtureProductVariables) {
2238 variables.ExtraAllowedDepsTxt = proptools.StringPtr("packages/modules/common/build/custom_allowed_deps.txt")
2239 },
2240 ))
2241
2242 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2243 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2244 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
2245 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
2246 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
2247 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
2248
2249 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
2250 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2251 myapex.Output("depsinfo/flatlist.txt")), "\n")
2252 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2253 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2254 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2255 flatlist, "mylib:(minSdkVersion:29)")
2256 android.AssertStringListContains(t, "track platform-available lib",
2257 flatlist, "yourlib(minSdkVersion:29)")
2258}
2259
2260func TestTrackCustomAllowedDepsWithDefaultTxt(t *testing.T) {
2261 ctx := testApex(t, `
2262 apex {
2263 name: "myapex",
2264 key: "myapex.key",
2265 updatable: true,
2266 native_shared_libs: [
2267 "mylib",
2268 "yourlib",
2269 ],
2270 min_sdk_version: "29",
2271 }
2272
2273 apex {
2274 name: "myapex2",
2275 key: "myapex.key",
2276 updatable: false,
2277 native_shared_libs: ["yourlib"],
2278 }
2279
2280 apex_key {
2281 name: "myapex.key",
2282 public_key: "testkey.avbpubkey",
2283 private_key: "testkey.pem",
2284 }
2285
2286 cc_library {
2287 name: "mylib",
2288 srcs: ["mylib.cpp"],
2289 shared_libs: ["libbar"],
2290 min_sdk_version: "29",
2291 apex_available: ["myapex"],
2292 }
2293
2294 cc_library {
2295 name: "libbar",
2296 stubs: { versions: ["29", "30"] },
2297 }
2298
2299 cc_library {
2300 name: "yourlib",
2301 srcs: ["mylib.cpp"],
2302 min_sdk_version: "29",
2303 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2304 }
2305 `, withFiles(android.MockFS{
2306 "packages/modules/common/build/custom_allowed_deps.txt": nil,
2307 "packages/modules/common/build/allowed_deps.txt": nil,
2308 }),
2309 android.FixtureModifyProductVariables(
2310 func(variables android.FixtureProductVariables) {
2311 variables.ExtraAllowedDepsTxt = proptools.StringPtr("packages/modules/common/build/custom_allowed_deps.txt")
2312 },
2313 ))
2314
2315 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2316 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2317 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
2318 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
2319 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
2320 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
2321
2322 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
2323 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2324 myapex.Output("depsinfo/flatlist.txt")), "\n")
2325 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2326 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2327 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2328 flatlist, "mylib:(minSdkVersion:29)")
2329 android.AssertStringListContains(t, "track platform-available lib",
2330 flatlist, "yourlib(minSdkVersion:29)")
2331}
2332
Jooyung Han4495f842023-04-25 16:39:59 +09002333func TestTrackAllowedDeps_SkipWithoutAllowedDepsTxt(t *testing.T) {
2334 ctx := testApex(t, `
2335 apex {
2336 name: "myapex",
2337 key: "myapex.key",
2338 updatable: true,
2339 min_sdk_version: "29",
2340 }
2341
2342 apex_key {
2343 name: "myapex.key",
2344 public_key: "testkey.avbpubkey",
2345 private_key: "testkey.pem",
2346 }
2347 `)
2348 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2349 if nil != depsinfo.MaybeRule("generateApexDepsInfoFilesRule").Output {
2350 t.Error("apex_depsinfo_singleton shouldn't run when allowed_deps.txt doesn't exist")
2351 }
2352}
2353
Jooyung Han03b51852020-02-26 22:45:42 +09002354func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002355 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002356 apex {
2357 name: "myapex",
2358 key: "myapex.key",
2359 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002360 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002361 }
2362
2363 apex_key {
2364 name: "myapex.key",
2365 public_key: "testkey.avbpubkey",
2366 private_key: "testkey.pem",
2367 }
2368
2369 cc_library {
2370 name: "libx",
2371 system_shared_libs: [],
2372 stl: "none",
2373 apex_available: [ "myapex" ],
2374 stubs: {
2375 versions: ["1", "2"],
2376 },
2377 }
2378
2379 cc_library {
2380 name: "libz",
2381 shared_libs: ["libx"],
2382 system_shared_libs: [],
2383 stl: "none",
2384 }
2385 `)
2386
2387 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002388 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002389 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2390 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2391 }
2392 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002393 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002394 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2395 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2396 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002397 expectLink("libz", "shared", "libx", "shared_current")
2398 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002399 expectNoLink("libz", "shared", "libz", "shared_1")
2400 expectNoLink("libz", "shared", "libz", "shared")
2401}
2402
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002403var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2404 func(variables android.FixtureProductVariables) {
2405 variables.SanitizeDevice = []string{"hwaddress"}
2406 },
2407)
2408
Jooyung Han75568392020-03-20 04:29:24 +09002409func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002410 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002411 apex {
2412 name: "myapex",
2413 key: "myapex.key",
2414 native_shared_libs: ["libx"],
2415 min_sdk_version: "29",
2416 }
2417
2418 apex_key {
2419 name: "myapex.key",
2420 public_key: "testkey.avbpubkey",
2421 private_key: "testkey.pem",
2422 }
2423
2424 cc_library {
2425 name: "libx",
2426 shared_libs: ["libbar"],
2427 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002428 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002429 }
2430
2431 cc_library {
2432 name: "libbar",
2433 stubs: {
2434 versions: ["29", "30"],
2435 },
2436 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002437 `,
2438 prepareForTestWithSantitizeHwaddress,
2439 )
Jooyung Han03b51852020-02-26 22:45:42 +09002440 expectLink := func(from, from_variant, to, to_variant string) {
2441 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2442 libFlags := ld.Args["libFlags"]
2443 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2444 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002445 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002446}
2447
Jooyung Han75568392020-03-20 04:29:24 +09002448func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002449 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002450 apex {
2451 name: "myapex",
2452 key: "myapex.key",
2453 native_shared_libs: ["libx"],
2454 min_sdk_version: "29",
2455 }
2456
2457 apex_key {
2458 name: "myapex.key",
2459 public_key: "testkey.avbpubkey",
2460 private_key: "testkey.pem",
2461 }
2462
2463 cc_library {
2464 name: "libx",
2465 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002466 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002467 }
Jooyung Han75568392020-03-20 04:29:24 +09002468 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002469
2470 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002471 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002472 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002473 // note that platform variant is not.
2474 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002475 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002476}
2477
Jooyung Han749dc692020-04-15 11:03:39 +09002478func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2479 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002480 apex {
2481 name: "myapex",
2482 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002483 native_shared_libs: ["mylib"],
2484 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002485 }
2486
2487 apex_key {
2488 name: "myapex.key",
2489 public_key: "testkey.avbpubkey",
2490 private_key: "testkey.pem",
2491 }
Jooyung Han749dc692020-04-15 11:03:39 +09002492
2493 cc_library {
2494 name: "mylib",
2495 srcs: ["mylib.cpp"],
2496 system_shared_libs: [],
2497 stl: "none",
2498 apex_available: [
2499 "myapex",
2500 ],
2501 min_sdk_version: "30",
2502 }
2503 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002504
2505 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2506 apex {
2507 name: "myapex",
2508 key: "myapex.key",
2509 native_shared_libs: ["libfoo.ffi"],
2510 min_sdk_version: "29",
2511 }
2512
2513 apex_key {
2514 name: "myapex.key",
2515 public_key: "testkey.avbpubkey",
2516 private_key: "testkey.pem",
2517 }
2518
2519 rust_ffi_shared {
2520 name: "libfoo.ffi",
2521 srcs: ["foo.rs"],
2522 crate_name: "foo",
2523 apex_available: [
2524 "myapex",
2525 ],
2526 min_sdk_version: "30",
2527 }
2528 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002529
2530 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2531 apex {
2532 name: "myapex",
2533 key: "myapex.key",
2534 java_libs: ["libfoo"],
2535 min_sdk_version: "29",
2536 }
2537
2538 apex_key {
2539 name: "myapex.key",
2540 public_key: "testkey.avbpubkey",
2541 private_key: "testkey.pem",
2542 }
2543
2544 java_import {
2545 name: "libfoo",
2546 jars: ["libfoo.jar"],
2547 apex_available: [
2548 "myapex",
2549 ],
2550 min_sdk_version: "30",
2551 }
2552 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002553
2554 // Skip check for modules compiling against core API surface
2555 testApex(t, `
2556 apex {
2557 name: "myapex",
2558 key: "myapex.key",
2559 java_libs: ["libfoo"],
2560 min_sdk_version: "29",
2561 }
2562
2563 apex_key {
2564 name: "myapex.key",
2565 public_key: "testkey.avbpubkey",
2566 private_key: "testkey.pem",
2567 }
2568
2569 java_library {
2570 name: "libfoo",
2571 srcs: ["Foo.java"],
2572 apex_available: [
2573 "myapex",
2574 ],
2575 // Compile against core API surface
2576 sdk_version: "core_current",
2577 min_sdk_version: "30",
2578 }
2579 `)
2580
Jooyung Han749dc692020-04-15 11:03:39 +09002581}
2582
2583func TestApexMinSdkVersion_Okay(t *testing.T) {
2584 testApex(t, `
2585 apex {
2586 name: "myapex",
2587 key: "myapex.key",
2588 native_shared_libs: ["libfoo"],
2589 java_libs: ["libbar"],
2590 min_sdk_version: "29",
2591 }
2592
2593 apex_key {
2594 name: "myapex.key",
2595 public_key: "testkey.avbpubkey",
2596 private_key: "testkey.pem",
2597 }
2598
2599 cc_library {
2600 name: "libfoo",
2601 srcs: ["mylib.cpp"],
2602 shared_libs: ["libfoo_dep"],
2603 apex_available: ["myapex"],
2604 min_sdk_version: "29",
2605 }
2606
2607 cc_library {
2608 name: "libfoo_dep",
2609 srcs: ["mylib.cpp"],
2610 apex_available: ["myapex"],
2611 min_sdk_version: "29",
2612 }
2613
2614 java_library {
2615 name: "libbar",
2616 sdk_version: "current",
2617 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002618 static_libs: [
2619 "libbar_dep",
2620 "libbar_import_dep",
2621 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002622 apex_available: ["myapex"],
2623 min_sdk_version: "29",
2624 }
2625
2626 java_library {
2627 name: "libbar_dep",
2628 sdk_version: "current",
2629 srcs: ["a.java"],
2630 apex_available: ["myapex"],
2631 min_sdk_version: "29",
2632 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002633
2634 java_import {
2635 name: "libbar_import_dep",
2636 jars: ["libbar.jar"],
2637 apex_available: ["myapex"],
2638 min_sdk_version: "29",
2639 }
Jooyung Han03b51852020-02-26 22:45:42 +09002640 `)
2641}
2642
Colin Cross8ca61c12022-10-06 21:00:14 -07002643func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2644 // Tests that an apex dependency with min_sdk_version higher than the
2645 // min_sdk_version of the apex is allowed as long as the dependency's
2646 // min_sdk_version is less than or equal to the api level that the
2647 // architecture was introduced in. In this case, arm64 didn't exist
2648 // until api level 21, so the arm64 code will never need to run on
2649 // an api level 20 device, even if other architectures of the apex
2650 // will.
2651 testApex(t, `
2652 apex {
2653 name: "myapex",
2654 key: "myapex.key",
2655 native_shared_libs: ["libfoo"],
2656 min_sdk_version: "20",
2657 }
2658
2659 apex_key {
2660 name: "myapex.key",
2661 public_key: "testkey.avbpubkey",
2662 private_key: "testkey.pem",
2663 }
2664
2665 cc_library {
2666 name: "libfoo",
2667 srcs: ["mylib.cpp"],
2668 apex_available: ["myapex"],
2669 min_sdk_version: "21",
2670 stl: "none",
2671 }
2672 `)
2673}
2674
Artur Satayev8cf899a2020-04-15 17:29:42 +01002675func TestJavaStableSdkVersion(t *testing.T) {
2676 testCases := []struct {
2677 name string
2678 expectedError string
2679 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002680 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002681 }{
2682 {
2683 name: "Non-updatable apex with non-stable dep",
2684 bp: `
2685 apex {
2686 name: "myapex",
2687 java_libs: ["myjar"],
2688 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002689 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002690 }
2691 apex_key {
2692 name: "myapex.key",
2693 public_key: "testkey.avbpubkey",
2694 private_key: "testkey.pem",
2695 }
2696 java_library {
2697 name: "myjar",
2698 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002699 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002700 apex_available: ["myapex"],
2701 }
2702 `,
2703 },
2704 {
2705 name: "Updatable apex with stable dep",
2706 bp: `
2707 apex {
2708 name: "myapex",
2709 java_libs: ["myjar"],
2710 key: "myapex.key",
2711 updatable: true,
2712 min_sdk_version: "29",
2713 }
2714 apex_key {
2715 name: "myapex.key",
2716 public_key: "testkey.avbpubkey",
2717 private_key: "testkey.pem",
2718 }
2719 java_library {
2720 name: "myjar",
2721 srcs: ["foo/bar/MyClass.java"],
2722 sdk_version: "current",
2723 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002724 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002725 }
2726 `,
2727 },
2728 {
2729 name: "Updatable apex with non-stable dep",
2730 expectedError: "cannot depend on \"myjar\"",
2731 bp: `
2732 apex {
2733 name: "myapex",
2734 java_libs: ["myjar"],
2735 key: "myapex.key",
2736 updatable: true,
2737 }
2738 apex_key {
2739 name: "myapex.key",
2740 public_key: "testkey.avbpubkey",
2741 private_key: "testkey.pem",
2742 }
2743 java_library {
2744 name: "myjar",
2745 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002746 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002747 apex_available: ["myapex"],
2748 }
2749 `,
2750 },
2751 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002752 name: "Updatable apex with non-stable legacy core platform dep",
2753 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2754 bp: `
2755 apex {
2756 name: "myapex",
2757 java_libs: ["myjar-uses-legacy"],
2758 key: "myapex.key",
2759 updatable: true,
2760 }
2761 apex_key {
2762 name: "myapex.key",
2763 public_key: "testkey.avbpubkey",
2764 private_key: "testkey.pem",
2765 }
2766 java_library {
2767 name: "myjar-uses-legacy",
2768 srcs: ["foo/bar/MyClass.java"],
2769 sdk_version: "core_platform",
2770 apex_available: ["myapex"],
2771 }
2772 `,
2773 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2774 },
2775 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002776 name: "Updatable apex with non-stable transitive dep",
2777 // This is not actually detecting that the transitive dependency is unstable, rather it is
2778 // detecting that the transitive dependency is building against a wider API surface than the
2779 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002780 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002781 bp: `
2782 apex {
2783 name: "myapex",
2784 java_libs: ["myjar"],
2785 key: "myapex.key",
2786 updatable: true,
2787 }
2788 apex_key {
2789 name: "myapex.key",
2790 public_key: "testkey.avbpubkey",
2791 private_key: "testkey.pem",
2792 }
2793 java_library {
2794 name: "myjar",
2795 srcs: ["foo/bar/MyClass.java"],
2796 sdk_version: "current",
2797 apex_available: ["myapex"],
2798 static_libs: ["transitive-jar"],
2799 }
2800 java_library {
2801 name: "transitive-jar",
2802 srcs: ["foo/bar/MyClass.java"],
2803 sdk_version: "core_platform",
2804 apex_available: ["myapex"],
2805 }
2806 `,
2807 },
2808 }
2809
2810 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002811 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2812 continue
2813 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002814 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002815 errorHandler := android.FixtureExpectsNoErrors
2816 if test.expectedError != "" {
2817 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002818 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002819 android.GroupFixturePreparers(
2820 java.PrepareForTestWithJavaDefaultModules,
2821 PrepareForTestWithApexBuildComponents,
2822 prepareForTestWithMyapex,
2823 android.OptionalFixturePreparer(test.preparer),
2824 ).
2825 ExtendWithErrorHandler(errorHandler).
2826 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002827 })
2828 }
2829}
2830
Jooyung Han749dc692020-04-15 11:03:39 +09002831func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2832 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2833 apex {
2834 name: "myapex",
2835 key: "myapex.key",
2836 native_shared_libs: ["mylib"],
2837 min_sdk_version: "29",
2838 }
2839
2840 apex_key {
2841 name: "myapex.key",
2842 public_key: "testkey.avbpubkey",
2843 private_key: "testkey.pem",
2844 }
2845
2846 cc_library {
2847 name: "mylib",
2848 srcs: ["mylib.cpp"],
2849 shared_libs: ["mylib2"],
2850 system_shared_libs: [],
2851 stl: "none",
2852 apex_available: [
2853 "myapex",
2854 ],
2855 min_sdk_version: "29",
2856 }
2857
2858 // indirect part of the apex
2859 cc_library {
2860 name: "mylib2",
2861 srcs: ["mylib.cpp"],
2862 system_shared_libs: [],
2863 stl: "none",
2864 apex_available: [
2865 "myapex",
2866 ],
2867 min_sdk_version: "30",
2868 }
2869 `)
2870}
2871
2872func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2873 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2874 apex {
2875 name: "myapex",
2876 key: "myapex.key",
2877 apps: ["AppFoo"],
2878 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002879 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002880 }
2881
2882 apex_key {
2883 name: "myapex.key",
2884 public_key: "testkey.avbpubkey",
2885 private_key: "testkey.pem",
2886 }
2887
2888 android_app {
2889 name: "AppFoo",
2890 srcs: ["foo/bar/MyClass.java"],
2891 sdk_version: "current",
2892 min_sdk_version: "29",
2893 system_modules: "none",
2894 stl: "none",
2895 static_libs: ["bar"],
2896 apex_available: [ "myapex" ],
2897 }
2898
2899 java_library {
2900 name: "bar",
2901 sdk_version: "current",
2902 srcs: ["a.java"],
2903 apex_available: [ "myapex" ],
2904 }
2905 `)
2906}
2907
2908func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002909 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002910 apex {
2911 name: "myapex",
2912 key: "myapex.key",
2913 native_shared_libs: ["mylib"],
2914 min_sdk_version: "29",
2915 }
2916
2917 apex_key {
2918 name: "myapex.key",
2919 public_key: "testkey.avbpubkey",
2920 private_key: "testkey.pem",
2921 }
2922
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002923 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002924 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2925 cc_library {
2926 name: "mylib",
2927 srcs: ["mylib.cpp"],
2928 shared_libs: ["mylib2"],
2929 system_shared_libs: [],
2930 stl: "none",
2931 apex_available: ["myapex", "otherapex"],
2932 min_sdk_version: "29",
2933 }
2934
2935 cc_library {
2936 name: "mylib2",
2937 srcs: ["mylib.cpp"],
2938 system_shared_libs: [],
2939 stl: "none",
2940 apex_available: ["otherapex"],
2941 stubs: { versions: ["29", "30"] },
2942 min_sdk_version: "30",
2943 }
2944
2945 apex {
2946 name: "otherapex",
2947 key: "myapex.key",
2948 native_shared_libs: ["mylib", "mylib2"],
2949 min_sdk_version: "30",
2950 }
2951 `)
2952 expectLink := func(from, from_variant, to, to_variant string) {
2953 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2954 libFlags := ld.Args["libFlags"]
2955 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2956 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002957 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002958 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002959}
2960
Jooyung Haned124c32021-01-26 11:43:46 +09002961func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002962 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2963 func(variables android.FixtureProductVariables) {
2964 variables.Platform_sdk_codename = proptools.StringPtr("S")
2965 variables.Platform_version_active_codenames = []string{"S"}
2966 },
2967 )
Jooyung Haned124c32021-01-26 11:43:46 +09002968 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2969 apex {
2970 name: "myapex",
2971 key: "myapex.key",
2972 native_shared_libs: ["libfoo"],
2973 min_sdk_version: "S",
2974 }
2975 apex_key {
2976 name: "myapex.key",
2977 public_key: "testkey.avbpubkey",
2978 private_key: "testkey.pem",
2979 }
2980 cc_library {
2981 name: "libfoo",
2982 shared_libs: ["libbar"],
2983 apex_available: ["myapex"],
2984 min_sdk_version: "29",
2985 }
2986 cc_library {
2987 name: "libbar",
2988 apex_available: ["myapex"],
2989 }
2990 `, withSAsActiveCodeNames)
2991}
2992
2993func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002994 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2995 variables.Platform_sdk_codename = proptools.StringPtr("S")
2996 variables.Platform_version_active_codenames = []string{"S", "T"}
2997 })
Colin Cross1c460562021-02-16 17:55:47 -08002998 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002999 apex {
3000 name: "myapex",
3001 key: "myapex.key",
3002 native_shared_libs: ["libfoo"],
3003 min_sdk_version: "S",
3004 }
3005 apex_key {
3006 name: "myapex.key",
3007 public_key: "testkey.avbpubkey",
3008 private_key: "testkey.pem",
3009 }
3010 cc_library {
3011 name: "libfoo",
3012 shared_libs: ["libbar"],
3013 apex_available: ["myapex"],
3014 min_sdk_version: "S",
3015 }
3016 cc_library {
3017 name: "libbar",
3018 stubs: {
3019 symbol_file: "libbar.map.txt",
3020 versions: ["30", "S", "T"],
3021 },
3022 }
3023 `, withSAsActiveCodeNames)
3024
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003025 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09003026 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
3027 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003028 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09003029}
3030
Jiyong Park7c2ee712018-12-07 00:42:25 +09003031func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003032 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09003033 apex {
3034 name: "myapex",
3035 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003036 native_shared_libs: ["mylib"],
Jooyung Han4ed512b2023-08-11 16:30:04 +09003037 binaries: ["mybin", "mybin.rust"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09003038 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003039 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003040 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09003041 }
3042
3043 apex_key {
3044 name: "myapex.key",
3045 public_key: "testkey.avbpubkey",
3046 private_key: "testkey.pem",
3047 }
3048
3049 prebuilt_etc {
3050 name: "myetc",
3051 src: "myprebuilt",
3052 sub_dir: "foo/bar",
3053 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003054
3055 cc_library {
3056 name: "mylib",
3057 srcs: ["mylib.cpp"],
3058 relative_install_path: "foo/bar",
3059 system_shared_libs: [],
3060 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003061 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003062 }
3063
3064 cc_binary {
3065 name: "mybin",
3066 srcs: ["mylib.cpp"],
3067 relative_install_path: "foo/bar",
3068 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003069 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003070 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003071 }
Jooyung Han4ed512b2023-08-11 16:30:04 +09003072
3073 rust_binary {
3074 name: "mybin.rust",
3075 srcs: ["foo.rs"],
3076 relative_install_path: "rust_subdir",
3077 apex_available: [ "myapex" ],
3078 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09003079 `)
3080
Jooyung Hana0503a52023-08-23 13:12:50 +09003081 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09003082 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09003083
Jiyong Parkb7c24df2019-02-01 12:03:59 +09003084 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09003085 ensureContains(t, cmd, "/etc ")
3086 ensureContains(t, cmd, "/etc/foo ")
3087 ensureContains(t, cmd, "/etc/foo/bar ")
3088 ensureContains(t, cmd, "/lib64 ")
3089 ensureContains(t, cmd, "/lib64/foo ")
3090 ensureContains(t, cmd, "/lib64/foo/bar ")
3091 ensureContains(t, cmd, "/lib ")
3092 ensureContains(t, cmd, "/lib/foo ")
3093 ensureContains(t, cmd, "/lib/foo/bar ")
3094 ensureContains(t, cmd, "/bin ")
3095 ensureContains(t, cmd, "/bin/foo ")
3096 ensureContains(t, cmd, "/bin/foo/bar ")
Jooyung Han4ed512b2023-08-11 16:30:04 +09003097 ensureContains(t, cmd, "/bin/rust_subdir ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09003098}
Jiyong Parkda6eb592018-12-19 17:12:36 +09003099
Jooyung Han35155c42020-02-06 17:33:20 +09003100func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003101 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09003102 apex {
3103 name: "myapex",
3104 key: "myapex.key",
3105 multilib: {
3106 both: {
3107 native_shared_libs: ["mylib"],
3108 binaries: ["mybin"],
3109 },
3110 },
3111 compile_multilib: "both",
3112 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003113 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09003114 }
3115
3116 apex_key {
3117 name: "myapex.key",
3118 public_key: "testkey.avbpubkey",
3119 private_key: "testkey.pem",
3120 }
3121
3122 cc_library {
3123 name: "mylib",
3124 relative_install_path: "foo/bar",
3125 system_shared_libs: [],
3126 stl: "none",
3127 apex_available: [ "myapex" ],
3128 native_bridge_supported: true,
3129 }
3130
3131 cc_binary {
3132 name: "mybin",
3133 relative_install_path: "foo/bar",
3134 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09003135 stl: "none",
3136 apex_available: [ "myapex" ],
3137 native_bridge_supported: true,
3138 compile_multilib: "both", // default is "first" for binary
3139 multilib: {
3140 lib64: {
3141 suffix: "64",
3142 },
3143 },
3144 }
3145 `, withNativeBridgeEnabled)
Jooyung Hana0503a52023-08-23 13:12:50 +09003146 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han35155c42020-02-06 17:33:20 +09003147 "bin/foo/bar/mybin",
3148 "bin/foo/bar/mybin64",
3149 "bin/arm/foo/bar/mybin",
3150 "bin/arm64/foo/bar/mybin64",
3151 "lib/foo/bar/mylib.so",
3152 "lib/arm/foo/bar/mylib.so",
3153 "lib64/foo/bar/mylib.so",
3154 "lib64/arm64/foo/bar/mylib.so",
3155 })
3156}
3157
Jooyung Han85d61762020-06-24 23:50:26 +09003158func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08003159 result := android.GroupFixturePreparers(
3160 prepareForApexTest,
3161 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
3162 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09003163 apex {
3164 name: "myapex",
3165 key: "myapex.key",
3166 binaries: ["mybin"],
3167 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003168 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09003169 }
3170 apex_key {
3171 name: "myapex.key",
3172 public_key: "testkey.avbpubkey",
3173 private_key: "testkey.pem",
3174 }
3175 cc_binary {
3176 name: "mybin",
3177 vendor: true,
3178 shared_libs: ["libfoo"],
3179 }
3180 cc_library {
3181 name: "libfoo",
3182 proprietary: true,
3183 }
3184 `)
3185
Jooyung Hana0503a52023-08-23 13:12:50 +09003186 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09003187 "bin/mybin",
3188 "lib64/libfoo.so",
3189 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
3190 "lib64/libc++.so",
3191 })
3192
Jooyung Hana0503a52023-08-23 13:12:50 +09003193 apexBundle := result.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossc68db4b2021-11-11 18:59:15 -08003194 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09003195 name := apexBundle.BaseModuleName()
3196 prefix := "TARGET_"
3197 var builder strings.Builder
3198 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08003199 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01003200 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01003201 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003202
Jooyung Hana0503a52023-08-23 13:12:50 +09003203 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003204 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
3205 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09003206}
3207
Justin Yun13decfb2021-03-08 19:25:55 +09003208func TestProductVariant(t *testing.T) {
3209 ctx := testApex(t, `
3210 apex {
3211 name: "myapex",
3212 key: "myapex.key",
3213 updatable: false,
3214 product_specific: true,
3215 binaries: ["foo"],
3216 }
3217
3218 apex_key {
3219 name: "myapex.key",
3220 public_key: "testkey.avbpubkey",
3221 private_key: "testkey.pem",
3222 }
3223
3224 cc_binary {
3225 name: "foo",
3226 product_available: true,
3227 apex_available: ["myapex"],
3228 srcs: ["foo.cpp"],
3229 }
Justin Yunaf1fde42023-09-27 16:22:10 +09003230 `)
Justin Yun13decfb2021-03-08 19:25:55 +09003231
3232 cflags := strings.Fields(
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003233 ctx.ModuleForTests("foo", "android_product_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09003234 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
3235 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
3236 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
3237 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
3238}
3239
Jooyung Han8e5685d2020-09-21 11:02:57 +09003240func TestApex_withPrebuiltFirmware(t *testing.T) {
3241 testCases := []struct {
3242 name string
3243 additionalProp string
3244 }{
3245 {"system apex with prebuilt_firmware", ""},
3246 {"vendor apex with prebuilt_firmware", "vendor: true,"},
3247 }
3248 for _, tc := range testCases {
3249 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003250 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09003251 apex {
3252 name: "myapex",
3253 key: "myapex.key",
3254 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003255 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09003256 `+tc.additionalProp+`
3257 }
3258 apex_key {
3259 name: "myapex.key",
3260 public_key: "testkey.avbpubkey",
3261 private_key: "testkey.pem",
3262 }
3263 prebuilt_firmware {
3264 name: "myfirmware",
3265 src: "myfirmware.bin",
3266 filename_from_src: true,
3267 `+tc.additionalProp+`
3268 }
3269 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09003270 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han8e5685d2020-09-21 11:02:57 +09003271 "etc/firmware/myfirmware.bin",
3272 })
3273 })
3274 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003275}
3276
Jooyung Hanefb184e2020-06-25 17:14:25 +09003277func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003278 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003279 apex {
3280 name: "myapex",
3281 key: "myapex.key",
3282 vendor: true,
3283 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003284 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003285 }
3286
3287 apex_key {
3288 name: "myapex.key",
3289 public_key: "testkey.avbpubkey",
3290 private_key: "testkey.pem",
3291 }
3292
3293 cc_library {
3294 name: "mylib",
3295 vendor_available: true,
3296 }
3297 `)
3298
Jooyung Hana0503a52023-08-23 13:12:50 +09003299 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003300 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003301 name := apexBundle.BaseModuleName()
3302 prefix := "TARGET_"
3303 var builder strings.Builder
3304 data.Custom(&builder, name, prefix, "", data)
3305 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09003306 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++.vendor.myapex:64 mylib.vendor.myapex:64 libc.vendor libm.vendor libdl.vendor\n")
Jooyung Hanefb184e2020-06-25 17:14:25 +09003307}
3308
Jooyung Han2ed99d02020-06-24 23:26:26 +09003309func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003310 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003311 apex {
3312 name: "myapex",
3313 key: "myapex.key",
3314 vintf_fragments: ["fragment.xml"],
3315 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003316 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003317 }
3318 apex_key {
3319 name: "myapex.key",
3320 public_key: "testkey.avbpubkey",
3321 private_key: "testkey.pem",
3322 }
3323 cc_binary {
3324 name: "mybin",
3325 }
3326 `)
3327
Jooyung Hana0503a52023-08-23 13:12:50 +09003328 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003329 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003330 name := apexBundle.BaseModuleName()
3331 prefix := "TARGET_"
3332 var builder strings.Builder
3333 data.Custom(&builder, name, prefix, "", data)
3334 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003335 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003336 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003337}
3338
Jiyong Park16e91a02018-12-20 18:18:08 +09003339func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003340 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003341 apex {
3342 name: "myapex",
3343 key: "myapex.key",
3344 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003345 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003346 }
3347
3348 apex_key {
3349 name: "myapex.key",
3350 public_key: "testkey.avbpubkey",
3351 private_key: "testkey.pem",
3352 }
3353
3354 cc_library {
3355 name: "mylib",
3356 srcs: ["mylib.cpp"],
3357 system_shared_libs: [],
3358 stl: "none",
3359 stubs: {
3360 versions: ["1", "2", "3"],
3361 },
Spandan Das20fce2d2023-04-12 17:21:39 +00003362 apex_available: ["myapex"],
Jiyong Park16e91a02018-12-20 18:18:08 +09003363 }
3364
3365 cc_binary {
3366 name: "not_in_apex",
3367 srcs: ["mylib.cpp"],
3368 static_libs: ["mylib"],
3369 static_executable: true,
3370 system_shared_libs: [],
3371 stl: "none",
3372 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003373 `)
3374
Colin Cross7113d202019-11-20 16:39:12 -08003375 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003376
3377 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003378 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003379}
Jiyong Park9335a262018-12-24 11:31:58 +09003380
3381func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003382 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003383 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003384 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003385 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003386 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003387 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003388 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003389 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003390 }
3391
3392 cc_library {
3393 name: "mylib",
3394 srcs: ["mylib.cpp"],
3395 system_shared_libs: [],
3396 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003397 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003398 }
3399
3400 apex_key {
3401 name: "myapex.key",
3402 public_key: "testkey.avbpubkey",
3403 private_key: "testkey.pem",
3404 }
3405
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003406 android_app_certificate {
3407 name: "myapex.certificate",
3408 certificate: "testkey",
3409 }
3410
3411 android_app_certificate {
3412 name: "myapex.certificate.override",
3413 certificate: "testkey.override",
3414 }
3415
Jiyong Park9335a262018-12-24 11:31:58 +09003416 `)
3417
3418 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003419 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003420
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003421 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3422 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003423 "vendor/foo/devkeys/testkey.avbpubkey")
3424 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003425 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3426 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003427 "vendor/foo/devkeys/testkey.pem")
3428 }
3429
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003430 // check the APK certs. It should be overridden to myapex.certificate.override
Jooyung Hana0503a52023-08-23 13:12:50 +09003431 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003432 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003433 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003434 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003435 }
3436}
Jiyong Park58e364a2019-01-19 19:24:06 +09003437
Jooyung Hanf121a652019-12-17 14:30:11 +09003438func TestCertificate(t *testing.T) {
3439 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003440 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003441 apex {
3442 name: "myapex",
3443 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003444 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003445 }
3446 apex_key {
3447 name: "myapex.key",
3448 public_key: "testkey.avbpubkey",
3449 private_key: "testkey.pem",
3450 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003451 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003452 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3453 if actual := rule.Args["certificates"]; actual != expected {
3454 t.Errorf("certificates should be %q, not %q", expected, actual)
3455 }
3456 })
3457 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003458 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003459 apex {
3460 name: "myapex_keytest",
3461 key: "myapex.key",
3462 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003463 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003464 }
3465 apex_key {
3466 name: "myapex.key",
3467 public_key: "testkey.avbpubkey",
3468 private_key: "testkey.pem",
3469 }
3470 android_app_certificate {
3471 name: "myapex.certificate.override",
3472 certificate: "testkey.override",
3473 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003474 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003475 expected := "testkey.override.x509.pem testkey.override.pk8"
3476 if actual := rule.Args["certificates"]; actual != expected {
3477 t.Errorf("certificates should be %q, not %q", expected, actual)
3478 }
3479 })
3480 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003481 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003482 apex {
3483 name: "myapex",
3484 key: "myapex.key",
3485 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003486 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003487 }
3488 apex_key {
3489 name: "myapex.key",
3490 public_key: "testkey.avbpubkey",
3491 private_key: "testkey.pem",
3492 }
3493 android_app_certificate {
3494 name: "myapex.certificate",
3495 certificate: "testkey",
3496 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003497 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003498 expected := "testkey.x509.pem testkey.pk8"
3499 if actual := rule.Args["certificates"]; actual != expected {
3500 t.Errorf("certificates should be %q, not %q", expected, actual)
3501 }
3502 })
3503 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003504 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003505 apex {
3506 name: "myapex_keytest",
3507 key: "myapex.key",
3508 file_contexts: ":myapex-file_contexts",
3509 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003510 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003511 }
3512 apex_key {
3513 name: "myapex.key",
3514 public_key: "testkey.avbpubkey",
3515 private_key: "testkey.pem",
3516 }
3517 android_app_certificate {
3518 name: "myapex.certificate.override",
3519 certificate: "testkey.override",
3520 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003521 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003522 expected := "testkey.override.x509.pem testkey.override.pk8"
3523 if actual := rule.Args["certificates"]; actual != expected {
3524 t.Errorf("certificates should be %q, not %q", expected, actual)
3525 }
3526 })
3527 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003528 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003529 apex {
3530 name: "myapex",
3531 key: "myapex.key",
3532 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003533 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003534 }
3535 apex_key {
3536 name: "myapex.key",
3537 public_key: "testkey.avbpubkey",
3538 private_key: "testkey.pem",
3539 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003540 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003541 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3542 if actual := rule.Args["certificates"]; actual != expected {
3543 t.Errorf("certificates should be %q, not %q", expected, actual)
3544 }
3545 })
3546 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003547 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003548 apex {
3549 name: "myapex_keytest",
3550 key: "myapex.key",
3551 file_contexts: ":myapex-file_contexts",
3552 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003553 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003554 }
3555 apex_key {
3556 name: "myapex.key",
3557 public_key: "testkey.avbpubkey",
3558 private_key: "testkey.pem",
3559 }
3560 android_app_certificate {
3561 name: "myapex.certificate.override",
3562 certificate: "testkey.override",
3563 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003564 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003565 expected := "testkey.override.x509.pem testkey.override.pk8"
3566 if actual := rule.Args["certificates"]; actual != expected {
3567 t.Errorf("certificates should be %q, not %q", expected, actual)
3568 }
3569 })
3570}
3571
Jiyong Park58e364a2019-01-19 19:24:06 +09003572func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003573 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003574 apex {
3575 name: "myapex",
3576 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003577 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003578 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003579 }
3580
3581 apex {
3582 name: "otherapex",
3583 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003584 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003585 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003586 }
3587
3588 apex_key {
3589 name: "myapex.key",
3590 public_key: "testkey.avbpubkey",
3591 private_key: "testkey.pem",
3592 }
3593
3594 cc_library {
3595 name: "mylib",
3596 srcs: ["mylib.cpp"],
3597 system_shared_libs: [],
3598 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003599 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003600 "myapex",
3601 "otherapex",
3602 ],
Jooyung Han24282772020-03-21 23:20:55 +09003603 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003604 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003605 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003606 cc_library {
3607 name: "mylib2",
3608 srcs: ["mylib.cpp"],
3609 system_shared_libs: [],
3610 stl: "none",
3611 apex_available: [
3612 "myapex",
3613 "otherapex",
3614 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003615 static_libs: ["mylib3"],
3616 recovery_available: true,
3617 min_sdk_version: "29",
3618 }
3619 cc_library {
3620 name: "mylib3",
3621 srcs: ["mylib.cpp"],
3622 system_shared_libs: [],
3623 stl: "none",
3624 apex_available: [
3625 "myapex",
3626 "otherapex",
3627 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003628 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003629 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003630 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003631 `)
3632
Jooyung Hanc87a0592020-03-02 17:44:33 +09003633 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003634 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003635 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003636
Vinh Tranf9754732023-01-19 22:41:46 -05003637 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003638 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003639 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003640
Vinh Tranf9754732023-01-19 22:41:46 -05003641 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003642 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003643 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003644
Colin Crossaede88c2020-08-11 12:17:01 -07003645 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3646 // each variant defines additional macros to distinguish which apex variant it is built for
3647
3648 // non-APEX variant does not have __ANDROID_APEX__ defined
3649 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3650 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3651
Vinh Tranf9754732023-01-19 22:41:46 -05003652 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003653 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3654 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003655
Jooyung Hanc87a0592020-03-02 17:44:33 +09003656 // non-APEX variant does not have __ANDROID_APEX__ defined
3657 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3658 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3659
Vinh Tranf9754732023-01-19 22:41:46 -05003660 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003661 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003662 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003663}
Jiyong Park7e636d02019-01-28 16:16:54 +09003664
3665func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003666 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003667 apex {
3668 name: "myapex",
3669 key: "myapex.key",
3670 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003671 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003672 }
3673
3674 apex_key {
3675 name: "myapex.key",
3676 public_key: "testkey.avbpubkey",
3677 private_key: "testkey.pem",
3678 }
3679
3680 cc_library_headers {
3681 name: "mylib_headers",
3682 export_include_dirs: ["my_include"],
3683 system_shared_libs: [],
3684 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003685 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003686 }
3687
3688 cc_library {
3689 name: "mylib",
3690 srcs: ["mylib.cpp"],
3691 system_shared_libs: [],
3692 stl: "none",
3693 header_libs: ["mylib_headers"],
3694 export_header_lib_headers: ["mylib_headers"],
3695 stubs: {
3696 versions: ["1", "2", "3"],
3697 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003698 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003699 }
3700
3701 cc_library {
3702 name: "otherlib",
3703 srcs: ["mylib.cpp"],
3704 system_shared_libs: [],
3705 stl: "none",
3706 shared_libs: ["mylib"],
3707 }
3708 `)
3709
Colin Cross7113d202019-11-20 16:39:12 -08003710 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003711
3712 // Ensure that the include path of the header lib is exported to 'otherlib'
3713 ensureContains(t, cFlags, "-Imy_include")
3714}
Alex Light9670d332019-01-29 18:07:33 -08003715
Jiyong Park7cd10e32020-01-14 09:22:18 +09003716type fileInApex struct {
3717 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003718 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003719 isLink bool
3720}
3721
Jooyung Han1724d582022-12-21 10:17:44 +09003722func (f fileInApex) String() string {
3723 return f.src + ":" + f.path
3724}
3725
3726func (f fileInApex) match(expectation string) bool {
3727 parts := strings.Split(expectation, ":")
3728 if len(parts) == 1 {
3729 match, _ := path.Match(parts[0], f.path)
3730 return match
3731 }
3732 if len(parts) == 2 {
3733 matchSrc, _ := path.Match(parts[0], f.src)
3734 matchDst, _ := path.Match(parts[1], f.path)
3735 return matchSrc && matchDst
3736 }
3737 panic("invalid expected file specification: " + expectation)
3738}
3739
Jooyung Hana57af4a2020-01-23 05:36:59 +00003740func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003741 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003742 module := ctx.ModuleForTests(moduleName, variant)
3743 apexRule := module.MaybeRule("apexRule")
3744 apexDir := "/image.apex/"
Jooyung Han31c470b2019-10-18 16:26:59 +09003745 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003746 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003747 for _, cmd := range strings.Split(copyCmds, "&&") {
3748 cmd = strings.TrimSpace(cmd)
3749 if cmd == "" {
3750 continue
3751 }
3752 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003753 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003754 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003755 switch terms[0] {
3756 case "mkdir":
3757 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003758 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003759 t.Fatal("copyCmds contains invalid cp command", cmd)
3760 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003761 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003762 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003763 isLink = false
3764 case "ln":
3765 if len(terms) != 3 && len(terms) != 4 {
3766 // ln LINK TARGET or ln -s LINK TARGET
3767 t.Fatal("copyCmds contains invalid ln command", cmd)
3768 }
3769 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003770 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003771 isLink = true
3772 default:
3773 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3774 }
3775 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003776 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003777 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003778 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003779 }
Jooyung Han1724d582022-12-21 10:17:44 +09003780 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003781 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003782 }
3783 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003784 return ret
3785}
3786
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003787func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003788 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003789 var failed bool
3790 var surplus []string
3791 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003792 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003793 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003794 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003795 if file.match(expected) {
3796 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003797 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003798 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003799 }
3800 }
Jooyung Han1724d582022-12-21 10:17:44 +09003801 if !matchFound {
3802 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003803 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003804 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003805
Jooyung Han31c470b2019-10-18 16:26:59 +09003806 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003807 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003808 t.Log("surplus files", surplus)
3809 failed = true
3810 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003811
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003812 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003813 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003814 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003815 if !filesMatched[expected] {
3816 missing = append(missing, expected)
3817 }
3818 }
3819 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003820 t.Log("missing files", missing)
3821 failed = true
3822 }
3823 if failed {
3824 t.Fail()
3825 }
3826}
3827
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003828func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3829 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3830}
3831
3832func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
Spandan Das52c01a12024-09-20 01:09:48 +00003833 deapexer := ctx.ModuleForTests(moduleName, variant).Description("deapex")
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003834 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3835 if deapexer.Output != nil {
3836 outputs = append(outputs, deapexer.Output.String())
3837 }
3838 for _, output := range deapexer.ImplicitOutputs {
3839 outputs = append(outputs, output.String())
3840 }
3841 actualFiles := make([]fileInApex, 0, len(outputs))
3842 for _, output := range outputs {
3843 dir := "/deapexer/"
3844 pos := strings.LastIndex(output, dir)
3845 if pos == -1 {
3846 t.Fatal("Unknown deapexer output ", output)
3847 }
3848 path := output[pos+len(dir):]
3849 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3850 }
3851 assertFileListEquals(t, files, actualFiles)
3852}
3853
Jooyung Han39edb6c2019-11-06 16:53:07 +09003854func vndkLibrariesTxtFiles(vers ...string) (result string) {
3855 for _, v := range vers {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +09003856 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Justin Yund5784122023-10-25 13:25:32 +09003857 result += `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003858 prebuilt_etc {
3859 name: "` + txt + `.libraries.` + v + `.txt",
3860 src: "dummy.txt",
3861 }
3862 `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003863 }
3864 }
3865 return
3866}
3867
Jooyung Han344d5432019-08-23 11:17:39 +09003868func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003869 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003870 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003871 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003872 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003873 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003874 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003875 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003876 }
3877
3878 apex_key {
3879 name: "myapex.key",
3880 public_key: "testkey.avbpubkey",
3881 private_key: "testkey.pem",
3882 }
3883
Jooyung Han31c470b2019-10-18 16:26:59 +09003884 vndk_prebuilt_shared {
3885 name: "libvndk27",
3886 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003887 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003888 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003889 vndk: {
3890 enabled: true,
3891 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003892 target_arch: "arm64",
3893 arch: {
3894 arm: {
3895 srcs: ["libvndk27_arm.so"],
3896 },
3897 arm64: {
3898 srcs: ["libvndk27_arm64.so"],
3899 },
3900 },
Colin Cross2807f002021-03-02 10:15:29 -08003901 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003902 }
3903
3904 vndk_prebuilt_shared {
3905 name: "libvndk27",
3906 version: "27",
3907 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003908 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003909 vndk: {
3910 enabled: true,
3911 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003912 target_arch: "x86_64",
3913 arch: {
3914 x86: {
3915 srcs: ["libvndk27_x86.so"],
3916 },
3917 x86_64: {
3918 srcs: ["libvndk27_x86_64.so"],
3919 },
3920 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003921 }
3922 `+vndkLibrariesTxtFiles("27"),
3923 withFiles(map[string][]byte{
3924 "libvndk27_arm.so": nil,
3925 "libvndk27_arm64.so": nil,
3926 "libvndk27_x86.so": nil,
3927 "libvndk27_x86_64.so": nil,
3928 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003929
Jooyung Hana0503a52023-08-23 13:12:50 +09003930 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003931 "lib/libvndk27_arm.so",
3932 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003933 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003934 })
Jooyung Han344d5432019-08-23 11:17:39 +09003935}
3936
Jooyung Han90eee022019-10-01 20:02:42 +09003937func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003938 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003939 apex_vndk {
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003940 name: "com.android.vndk.v29",
Jooyung Han90eee022019-10-01 20:02:42 +09003941 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003942 file_contexts: ":myapex-file_contexts",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003943 vndk_version: "29",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003944 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003945 }
3946 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003947 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003948 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003949 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003950 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003951 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003952 }
3953 apex_key {
3954 name: "myapex.key",
3955 public_key: "testkey.avbpubkey",
3956 private_key: "testkey.pem",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003957 }`+vndkLibrariesTxtFiles("28", "29"))
Jooyung Han90eee022019-10-01 20:02:42 +09003958
3959 assertApexName := func(expected, moduleName string) {
Jooyung Hana0503a52023-08-23 13:12:50 +09003960 module := ctx.ModuleForTests(moduleName, "android_common")
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003961 apexManifestRule := module.Rule("apexManifestRule")
3962 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003963 }
3964
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003965 assertApexName("com.android.vndk.v29", "com.android.vndk.v29")
Colin Cross2807f002021-03-02 10:15:29 -08003966 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003967}
3968
Jooyung Han344d5432019-08-23 11:17:39 +09003969func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003970 testApexError(t, `module "com.android.vndk.v30" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003971 apex_vndk {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003972 name: "com.android.vndk.v30",
3973 key: "com.android.vndk.v30.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003974 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003975 native_bridge_supported: true,
3976 }
3977
3978 apex_key {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003979 name: "com.android.vndk.v30.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003980 public_key: "testkey.avbpubkey",
3981 private_key: "testkey.pem",
3982 }
3983
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003984 vndk_prebuilt_shared {
Jooyung Han344d5432019-08-23 11:17:39 +09003985 name: "libvndk",
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003986 version: "30",
3987 target_arch: "arm",
Jooyung Han344d5432019-08-23 11:17:39 +09003988 srcs: ["mylib.cpp"],
3989 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003990 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003991 native_bridge_supported: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003992 vndk: {
3993 enabled: true,
3994 },
Jooyung Han344d5432019-08-23 11:17:39 +09003995 }
3996 `)
3997}
3998
Jooyung Han31c470b2019-10-18 16:26:59 +09003999func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004000 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09004001 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004002 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09004003 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004004 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09004005 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004006 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09004007 }
4008
4009 apex_key {
4010 name: "myapex.key",
4011 public_key: "testkey.avbpubkey",
4012 private_key: "testkey.pem",
4013 }
4014
4015 vndk_prebuilt_shared {
4016 name: "libvndk27",
4017 version: "27",
4018 target_arch: "arm",
4019 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004020 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004021 vndk: {
4022 enabled: true,
4023 },
4024 arch: {
4025 arm: {
4026 srcs: ["libvndk27.so"],
4027 }
4028 },
4029 }
4030
4031 vndk_prebuilt_shared {
4032 name: "libvndk27",
4033 version: "27",
4034 target_arch: "arm",
4035 binder32bit: true,
4036 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004037 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004038 vndk: {
4039 enabled: true,
4040 },
4041 arch: {
4042 arm: {
4043 srcs: ["libvndk27binder32.so"],
4044 }
4045 },
Colin Cross2807f002021-03-02 10:15:29 -08004046 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09004047 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09004048 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09004049 withFiles(map[string][]byte{
4050 "libvndk27.so": nil,
4051 "libvndk27binder32.so": nil,
4052 }),
4053 withBinder32bit,
4054 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07004055 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09004056 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
4057 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09004058 },
4059 }),
4060 )
4061
Jooyung Hana0503a52023-08-23 13:12:50 +09004062 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09004063 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004064 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09004065 })
4066}
4067
Jooyung Hane1633032019-08-01 17:41:43 +09004068func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004069 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09004070 apex {
4071 name: "myapex_nodep",
4072 key: "myapex.key",
4073 native_shared_libs: ["lib_nodep"],
4074 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004075 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004076 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004077 }
4078
4079 apex {
4080 name: "myapex_dep",
4081 key: "myapex.key",
4082 native_shared_libs: ["lib_dep"],
4083 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004084 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004085 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004086 }
4087
4088 apex {
4089 name: "myapex_provider",
4090 key: "myapex.key",
4091 native_shared_libs: ["libfoo"],
4092 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004093 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004094 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004095 }
4096
4097 apex {
4098 name: "myapex_selfcontained",
4099 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00004100 native_shared_libs: ["lib_dep_on_bar", "libbar"],
Jooyung Hane1633032019-08-01 17:41:43 +09004101 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004102 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004103 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004104 }
4105
4106 apex_key {
4107 name: "myapex.key",
4108 public_key: "testkey.avbpubkey",
4109 private_key: "testkey.pem",
4110 }
4111
4112 cc_library {
4113 name: "lib_nodep",
4114 srcs: ["mylib.cpp"],
4115 system_shared_libs: [],
4116 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004117 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004118 }
4119
4120 cc_library {
4121 name: "lib_dep",
4122 srcs: ["mylib.cpp"],
4123 shared_libs: ["libfoo"],
4124 system_shared_libs: [],
4125 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004126 apex_available: [
4127 "myapex_dep",
4128 "myapex_provider",
4129 "myapex_selfcontained",
4130 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004131 }
4132
4133 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00004134 name: "lib_dep_on_bar",
4135 srcs: ["mylib.cpp"],
4136 shared_libs: ["libbar"],
4137 system_shared_libs: [],
4138 stl: "none",
4139 apex_available: [
4140 "myapex_selfcontained",
4141 ],
4142 }
4143
4144
4145 cc_library {
Jooyung Hane1633032019-08-01 17:41:43 +09004146 name: "libfoo",
4147 srcs: ["mytest.cpp"],
4148 stubs: {
4149 versions: ["1"],
4150 },
4151 system_shared_libs: [],
4152 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004153 apex_available: [
4154 "myapex_provider",
Spandan Das20fce2d2023-04-12 17:21:39 +00004155 ],
4156 }
4157
4158 cc_library {
4159 name: "libbar",
4160 srcs: ["mytest.cpp"],
4161 stubs: {
4162 versions: ["1"],
4163 },
4164 system_shared_libs: [],
4165 stl: "none",
4166 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004167 "myapex_selfcontained",
4168 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004169 }
Spandan Das20fce2d2023-04-12 17:21:39 +00004170
Jooyung Hane1633032019-08-01 17:41:43 +09004171 `)
4172
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004173 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004174 var provideNativeLibs, requireNativeLibs []string
4175
Jooyung Hana0503a52023-08-23 13:12:50 +09004176 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004177 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4178 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004179 ensureListEmpty(t, provideNativeLibs)
4180 ensureListEmpty(t, requireNativeLibs)
4181
Jooyung Hana0503a52023-08-23 13:12:50 +09004182 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004183 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4184 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004185 ensureListEmpty(t, provideNativeLibs)
4186 ensureListContains(t, requireNativeLibs, "libfoo.so")
4187
Jooyung Hana0503a52023-08-23 13:12:50 +09004188 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004189 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4190 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004191 ensureListContains(t, provideNativeLibs, "libfoo.so")
4192 ensureListEmpty(t, requireNativeLibs)
4193
Jooyung Hana0503a52023-08-23 13:12:50 +09004194 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004195 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4196 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Spandan Das20fce2d2023-04-12 17:21:39 +00004197 ensureListContains(t, provideNativeLibs, "libbar.so")
Jooyung Hane1633032019-08-01 17:41:43 +09004198 ensureListEmpty(t, requireNativeLibs)
4199}
4200
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004201func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4202 ctx := testApex(t, `
4203 apex {
4204 name: "myapex",
4205 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004206 native_shared_libs: ["mylib"],
4207 updatable: false,
4208 }
4209
4210 apex_key {
4211 name: "myapex.key",
4212 public_key: "testkey.avbpubkey",
4213 private_key: "testkey.pem",
4214 }
4215
4216 cc_library {
4217 name: "mylib",
4218 srcs: ["mylib.cpp"],
4219 system_shared_libs: [],
4220 stl: "none",
4221 apex_available: [
4222 "//apex_available:platform",
4223 "myapex",
4224 ],
4225 }
4226 `, android.FixtureMergeEnv(map[string]string{
4227 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4228 }))
4229
Jooyung Hana0503a52023-08-23 13:12:50 +09004230 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004231 apexManifestRule := module.Rule("apexManifestRule")
4232 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4233}
4234
Vinh Tran8f5310f2022-10-07 18:16:47 -04004235func TestCompileMultilibProp(t *testing.T) {
4236 testCases := []struct {
4237 compileMultiLibProp string
4238 containedLibs []string
4239 notContainedLibs []string
4240 }{
4241 {
4242 containedLibs: []string{
4243 "image.apex/lib64/mylib.so",
4244 "image.apex/lib/mylib.so",
4245 },
4246 compileMultiLibProp: `compile_multilib: "both",`,
4247 },
4248 {
4249 containedLibs: []string{"image.apex/lib64/mylib.so"},
4250 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4251 compileMultiLibProp: `compile_multilib: "first",`,
4252 },
4253 {
4254 containedLibs: []string{"image.apex/lib64/mylib.so"},
4255 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4256 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4257 },
4258 {
4259 containedLibs: []string{"image.apex/lib64/mylib.so"},
4260 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4261 compileMultiLibProp: `compile_multilib: "64",`,
4262 },
4263 {
4264 containedLibs: []string{"image.apex/lib/mylib.so"},
4265 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4266 compileMultiLibProp: `compile_multilib: "32",`,
4267 },
4268 }
4269 for _, testCase := range testCases {
4270 ctx := testApex(t, fmt.Sprintf(`
4271 apex {
4272 name: "myapex",
4273 key: "myapex.key",
4274 %s
4275 native_shared_libs: ["mylib"],
4276 updatable: false,
4277 }
4278 apex_key {
4279 name: "myapex.key",
4280 public_key: "testkey.avbpubkey",
4281 private_key: "testkey.pem",
4282 }
4283 cc_library {
4284 name: "mylib",
4285 srcs: ["mylib.cpp"],
4286 apex_available: [
4287 "//apex_available:platform",
4288 "myapex",
4289 ],
4290 }
4291 `, testCase.compileMultiLibProp),
4292 )
Jooyung Hana0503a52023-08-23 13:12:50 +09004293 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Vinh Tran8f5310f2022-10-07 18:16:47 -04004294 apexRule := module.Rule("apexRule")
4295 copyCmds := apexRule.Args["copy_commands"]
4296 for _, containedLib := range testCase.containedLibs {
4297 ensureContains(t, copyCmds, containedLib)
4298 }
4299 for _, notContainedLib := range testCase.notContainedLibs {
4300 ensureNotContains(t, copyCmds, notContainedLib)
4301 }
4302 }
4303}
4304
Alex Light0851b882019-02-07 13:20:53 -08004305func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004306 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004307 apex {
4308 name: "myapex",
4309 key: "myapex.key",
4310 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004311 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004312 }
4313
4314 apex_key {
4315 name: "myapex.key",
4316 public_key: "testkey.avbpubkey",
4317 private_key: "testkey.pem",
4318 }
4319
4320 cc_library {
4321 name: "mylib_common",
4322 srcs: ["mylib.cpp"],
4323 system_shared_libs: [],
4324 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004325 apex_available: [
4326 "//apex_available:platform",
4327 "myapex",
4328 ],
Alex Light0851b882019-02-07 13:20:53 -08004329 }
4330 `)
4331
Jooyung Hana0503a52023-08-23 13:12:50 +09004332 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004333 apexRule := module.Rule("apexRule")
4334 copyCmds := apexRule.Args["copy_commands"]
4335
4336 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4337 t.Log("Apex was a test apex!")
4338 t.Fail()
4339 }
4340 // Ensure that main rule creates an output
4341 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4342
4343 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004344 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004345
4346 // Ensure that both direct and indirect deps are copied into apex
4347 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4348
Colin Cross7113d202019-11-20 16:39:12 -08004349 // Ensure that the platform variant ends with _shared
4350 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004351
Colin Cross56a83212020-09-15 18:30:11 -07004352 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004353 t.Log("Found mylib_common not in any apex!")
4354 t.Fail()
4355 }
4356}
4357
4358func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004359 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004360 apex_test {
4361 name: "myapex",
4362 key: "myapex.key",
4363 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004364 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004365 }
4366
4367 apex_key {
4368 name: "myapex.key",
4369 public_key: "testkey.avbpubkey",
4370 private_key: "testkey.pem",
4371 }
4372
4373 cc_library {
4374 name: "mylib_common_test",
4375 srcs: ["mylib.cpp"],
4376 system_shared_libs: [],
4377 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004378 // TODO: remove //apex_available:platform
4379 apex_available: [
4380 "//apex_available:platform",
4381 "myapex",
4382 ],
Alex Light0851b882019-02-07 13:20:53 -08004383 }
4384 `)
4385
Jooyung Hana0503a52023-08-23 13:12:50 +09004386 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004387 apexRule := module.Rule("apexRule")
4388 copyCmds := apexRule.Args["copy_commands"]
4389
4390 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4391 t.Log("Apex was not a test apex!")
4392 t.Fail()
4393 }
4394 // Ensure that main rule creates an output
4395 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4396
4397 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004398 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004399
4400 // Ensure that both direct and indirect deps are copied into apex
4401 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4402
Colin Cross7113d202019-11-20 16:39:12 -08004403 // Ensure that the platform variant ends with _shared
4404 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004405}
4406
Jooyung Han85707de2023-12-01 14:21:13 +09004407func TestLibzVendorIsntStable(t *testing.T) {
4408 ctx := testApex(t, `
4409 apex {
4410 name: "myapex",
4411 key: "myapex.key",
4412 updatable: false,
4413 binaries: ["mybin"],
4414 }
4415 apex {
4416 name: "myvendorapex",
4417 key: "myapex.key",
4418 file_contexts: "myvendorapex_file_contexts",
4419 vendor: true,
4420 updatable: false,
4421 binaries: ["mybin"],
4422 }
4423 apex_key {
4424 name: "myapex.key",
4425 public_key: "testkey.avbpubkey",
4426 private_key: "testkey.pem",
4427 }
4428 cc_binary {
4429 name: "mybin",
4430 vendor_available: true,
4431 system_shared_libs: [],
4432 stl: "none",
4433 shared_libs: ["libz"],
4434 apex_available: ["//apex_available:anyapex"],
4435 }
4436 cc_library {
4437 name: "libz",
4438 vendor_available: true,
4439 system_shared_libs: [],
4440 stl: "none",
4441 stubs: {
4442 versions: ["28", "30"],
4443 },
4444 target: {
4445 vendor: {
4446 no_stubs: true,
4447 },
4448 },
4449 }
4450 `, withFiles(map[string][]byte{
4451 "myvendorapex_file_contexts": nil,
4452 }))
4453
4454 // libz provides stubs for core variant.
4455 {
4456 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
4457 "bin/mybin",
4458 })
4459 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
4460 android.AssertStringEquals(t, "should require libz", apexManifestRule.Args["requireNativeLibs"], "libz.so")
4461 }
4462 // libz doesn't provide stubs for vendor variant.
4463 {
4464 ensureExactContents(t, ctx, "myvendorapex", "android_common_myvendorapex", []string{
4465 "bin/mybin",
4466 "lib64/libz.so",
4467 })
4468 apexManifestRule := ctx.ModuleForTests("myvendorapex", "android_common_myvendorapex").Rule("apexManifestRule")
4469 android.AssertStringEquals(t, "should not require libz", apexManifestRule.Args["requireNativeLibs"], "")
4470 }
4471}
4472
Alex Light9670d332019-01-29 18:07:33 -08004473func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004474 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004475 apex {
4476 name: "myapex",
4477 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004478 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004479 multilib: {
4480 first: {
4481 native_shared_libs: ["mylib_common"],
4482 }
4483 },
4484 target: {
4485 android: {
4486 multilib: {
4487 first: {
4488 native_shared_libs: ["mylib"],
4489 }
4490 }
4491 },
4492 host: {
4493 multilib: {
4494 first: {
4495 native_shared_libs: ["mylib2"],
4496 }
4497 }
4498 }
4499 }
4500 }
4501
4502 apex_key {
4503 name: "myapex.key",
4504 public_key: "testkey.avbpubkey",
4505 private_key: "testkey.pem",
4506 }
4507
4508 cc_library {
4509 name: "mylib",
4510 srcs: ["mylib.cpp"],
4511 system_shared_libs: [],
4512 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004513 // TODO: remove //apex_available:platform
4514 apex_available: [
4515 "//apex_available:platform",
4516 "myapex",
4517 ],
Alex Light9670d332019-01-29 18:07:33 -08004518 }
4519
4520 cc_library {
4521 name: "mylib_common",
4522 srcs: ["mylib.cpp"],
4523 system_shared_libs: [],
4524 stl: "none",
4525 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004526 // TODO: remove //apex_available:platform
4527 apex_available: [
4528 "//apex_available:platform",
4529 "myapex",
4530 ],
Alex Light9670d332019-01-29 18:07:33 -08004531 }
4532
4533 cc_library {
4534 name: "mylib2",
4535 srcs: ["mylib.cpp"],
4536 system_shared_libs: [],
4537 stl: "none",
4538 compile_multilib: "first",
4539 }
4540 `)
4541
Jooyung Hana0503a52023-08-23 13:12:50 +09004542 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004543 copyCmds := apexRule.Args["copy_commands"]
4544
4545 // Ensure that main rule creates an output
4546 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4547
4548 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004549 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4550 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4551 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004552
4553 // Ensure that both direct and indirect deps are copied into apex
4554 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4555 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4556 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4557
Colin Cross7113d202019-11-20 16:39:12 -08004558 // Ensure that the platform variant ends with _shared
4559 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4560 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4561 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004562}
Jiyong Park04480cf2019-02-06 00:16:29 +09004563
Jiyong Park59140302020-12-14 18:44:04 +09004564func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004565 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004566 apex {
4567 name: "myapex",
4568 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004569 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004570 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004571 arch: {
4572 arm64: {
4573 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004574 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004575 },
4576 x86_64: {
4577 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004578 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004579 },
4580 }
4581 }
4582
4583 apex_key {
4584 name: "myapex.key",
4585 public_key: "testkey.avbpubkey",
4586 private_key: "testkey.pem",
4587 }
4588
4589 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004590 name: "mylib.generic",
4591 srcs: ["mylib.cpp"],
4592 system_shared_libs: [],
4593 stl: "none",
4594 // TODO: remove //apex_available:platform
4595 apex_available: [
4596 "//apex_available:platform",
4597 "myapex",
4598 ],
4599 }
4600
4601 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004602 name: "mylib.arm64",
4603 srcs: ["mylib.cpp"],
4604 system_shared_libs: [],
4605 stl: "none",
4606 // TODO: remove //apex_available:platform
4607 apex_available: [
4608 "//apex_available:platform",
4609 "myapex",
4610 ],
4611 }
4612
4613 cc_library {
4614 name: "mylib.x64",
4615 srcs: ["mylib.cpp"],
4616 system_shared_libs: [],
4617 stl: "none",
4618 // TODO: remove //apex_available:platform
4619 apex_available: [
4620 "//apex_available:platform",
4621 "myapex",
4622 ],
4623 }
4624 `)
4625
Jooyung Hana0503a52023-08-23 13:12:50 +09004626 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park59140302020-12-14 18:44:04 +09004627 copyCmds := apexRule.Args["copy_commands"]
4628
4629 // Ensure that apex variant is created for the direct dep
4630 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004631 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004632 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4633
4634 // Ensure that both direct and indirect deps are copied into apex
4635 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4636 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4637}
4638
Jiyong Park04480cf2019-02-06 00:16:29 +09004639func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004640 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004641 apex {
4642 name: "myapex",
4643 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004644 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004645 updatable: false,
Riya Thakur654461c2024-02-27 07:21:05 +00004646 compile_multilib: "both",
Jiyong Park04480cf2019-02-06 00:16:29 +09004647 }
4648
4649 apex_key {
4650 name: "myapex.key",
4651 public_key: "testkey.avbpubkey",
4652 private_key: "testkey.pem",
4653 }
4654
4655 sh_binary {
4656 name: "myscript",
4657 src: "mylib.cpp",
4658 filename: "myscript.sh",
4659 sub_dir: "script",
4660 }
4661 `)
4662
Jooyung Hana0503a52023-08-23 13:12:50 +09004663 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004664 copyCmds := apexRule.Args["copy_commands"]
4665
4666 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4667}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004668
Jooyung Han91df2082019-11-20 01:49:42 +09004669func TestApexInVariousPartition(t *testing.T) {
4670 testcases := []struct {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004671 propName, partition string
Jooyung Han91df2082019-11-20 01:49:42 +09004672 }{
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004673 {"", "system"},
4674 {"product_specific: true", "product"},
4675 {"soc_specific: true", "vendor"},
4676 {"proprietary: true", "vendor"},
4677 {"vendor: true", "vendor"},
4678 {"system_ext_specific: true", "system_ext"},
Jooyung Han91df2082019-11-20 01:49:42 +09004679 }
4680 for _, tc := range testcases {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004681 t.Run(tc.propName+":"+tc.partition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004682 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004683 apex {
4684 name: "myapex",
4685 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004686 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004687 `+tc.propName+`
4688 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004689
Jooyung Han91df2082019-11-20 01:49:42 +09004690 apex_key {
4691 name: "myapex.key",
4692 public_key: "testkey.avbpubkey",
4693 private_key: "testkey.pem",
4694 }
4695 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004696
Jooyung Hana0503a52023-08-23 13:12:50 +09004697 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004698 expected := "out/soong/target/product/test_device/" + tc.partition + "/apex"
Paul Duffin37ba3442021-03-29 00:21:08 +01004699 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004700 if actual != expected {
4701 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4702 }
Jooyung Han91df2082019-11-20 01:49:42 +09004703 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004704 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004705}
Jiyong Park67882562019-03-21 01:11:21 +09004706
Jooyung Han580eb4f2020-06-24 19:33:06 +09004707func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004708 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004709 apex {
4710 name: "myapex",
4711 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004712 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004713 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004714
Jooyung Han580eb4f2020-06-24 19:33:06 +09004715 apex_key {
4716 name: "myapex.key",
4717 public_key: "testkey.avbpubkey",
4718 private_key: "testkey.pem",
4719 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004720 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09004721 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004722 rule := module.Output("file_contexts")
4723 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4724}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004725
Jooyung Han580eb4f2020-06-24 19:33:06 +09004726func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004727 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004728 apex {
4729 name: "myapex",
4730 key: "myapex.key",
4731 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004732 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004733 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004734
Jooyung Han580eb4f2020-06-24 19:33:06 +09004735 apex_key {
4736 name: "myapex.key",
4737 public_key: "testkey.avbpubkey",
4738 private_key: "testkey.pem",
4739 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004740 `, withFiles(map[string][]byte{
4741 "my_own_file_contexts": nil,
4742 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004743}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004744
Jooyung Han580eb4f2020-06-24 19:33:06 +09004745func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004746 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004747 apex {
4748 name: "myapex",
4749 key: "myapex.key",
4750 product_specific: true,
4751 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004752 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004753 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004754
Jooyung Han580eb4f2020-06-24 19:33:06 +09004755 apex_key {
4756 name: "myapex.key",
4757 public_key: "testkey.avbpubkey",
4758 private_key: "testkey.pem",
4759 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004760 `)
4761
Colin Cross1c460562021-02-16 17:55:47 -08004762 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004763 apex {
4764 name: "myapex",
4765 key: "myapex.key",
4766 product_specific: true,
4767 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004768 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004769 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004770
Jooyung Han580eb4f2020-06-24 19:33:06 +09004771 apex_key {
4772 name: "myapex.key",
4773 public_key: "testkey.avbpubkey",
4774 private_key: "testkey.pem",
4775 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004776 `, withFiles(map[string][]byte{
4777 "product_specific_file_contexts": nil,
4778 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004779 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004780 rule := module.Output("file_contexts")
4781 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4782}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004783
Jooyung Han580eb4f2020-06-24 19:33:06 +09004784func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004785 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004786 apex {
4787 name: "myapex",
4788 key: "myapex.key",
4789 product_specific: true,
4790 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004791 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004792 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004793
Jooyung Han580eb4f2020-06-24 19:33:06 +09004794 apex_key {
4795 name: "myapex.key",
4796 public_key: "testkey.avbpubkey",
4797 private_key: "testkey.pem",
4798 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004799
Jooyung Han580eb4f2020-06-24 19:33:06 +09004800 filegroup {
4801 name: "my-file-contexts",
4802 srcs: ["product_specific_file_contexts"],
4803 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004804 `, withFiles(map[string][]byte{
4805 "product_specific_file_contexts": nil,
4806 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004807 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004808 rule := module.Output("file_contexts")
4809 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004810}
4811
Jiyong Park67882562019-03-21 01:11:21 +09004812func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004813 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004814 apex_key {
4815 name: "myapex.key",
4816 public_key: ":my.avbpubkey",
4817 private_key: ":my.pem",
4818 product_specific: true,
4819 }
4820
4821 filegroup {
4822 name: "my.avbpubkey",
4823 srcs: ["testkey2.avbpubkey"],
4824 }
4825
4826 filegroup {
4827 name: "my.pem",
4828 srcs: ["testkey2.pem"],
4829 }
4830 `)
4831
4832 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4833 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004834 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004835 if actual_pubkey != expected_pubkey {
4836 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4837 }
4838 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004839 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004840 if actual_privkey != expected_privkey {
4841 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4842 }
4843}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004844
4845func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004846 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004847 prebuilt_apex {
4848 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004849 arch: {
4850 arm64: {
4851 src: "myapex-arm64.apex",
4852 },
4853 arm: {
4854 src: "myapex-arm.apex",
4855 },
4856 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004857 }
4858 `)
4859
Wei Li340ee8e2022-03-18 17:33:24 -07004860 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4861 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004862
Jiyong Parkc95714e2019-03-29 14:23:10 +09004863 expectedInput := "myapex-arm64.apex"
4864 if prebuilt.inputApex.String() != expectedInput {
4865 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4866 }
Wei Li340ee8e2022-03-18 17:33:24 -07004867 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4868 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4869 rule := testingModule.Rule("genProvenanceMetaData")
4870 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4871 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4872 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4873 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08004874
4875 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
4876 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004877}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004878
Paul Duffinc0609c62021-03-01 17:27:16 +00004879func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004880 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004881 prebuilt_apex {
4882 name: "myapex",
4883 }
4884 `)
4885}
4886
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004887func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004888 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004889 prebuilt_apex {
4890 name: "myapex",
4891 src: "myapex-arm.apex",
4892 filename: "notmyapex.apex",
4893 }
4894 `)
4895
Wei Li340ee8e2022-03-18 17:33:24 -07004896 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4897 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004898
4899 expected := "notmyapex.apex"
4900 if p.installFilename != expected {
4901 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4902 }
Wei Li340ee8e2022-03-18 17:33:24 -07004903 rule := testingModule.Rule("genProvenanceMetaData")
4904 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4905 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4906 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4907 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004908}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004909
Samiul Islam7c02e262021-09-08 17:48:28 +01004910func TestApexSetFilenameOverride(t *testing.T) {
4911 testApex(t, `
4912 apex_set {
4913 name: "com.company.android.myapex",
4914 apex_name: "com.android.myapex",
4915 set: "company-myapex.apks",
4916 filename: "com.company.android.myapex.apex"
4917 }
4918 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4919
4920 testApex(t, `
4921 apex_set {
4922 name: "com.company.android.myapex",
4923 apex_name: "com.android.myapex",
4924 set: "company-myapex.apks",
4925 filename: "com.company.android.myapex.capex"
4926 }
4927 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4928
4929 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4930 apex_set {
4931 name: "com.company.android.myapex",
4932 apex_name: "com.android.myapex",
4933 set: "company-myapex.apks",
4934 filename: "some-random-suffix"
4935 }
4936 `)
4937}
4938
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004939func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004940 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004941 prebuilt_apex {
4942 name: "myapex.prebuilt",
4943 src: "myapex-arm.apex",
4944 overrides: [
4945 "myapex",
4946 ],
4947 }
4948 `)
4949
Wei Li340ee8e2022-03-18 17:33:24 -07004950 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4951 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004952
4953 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004954 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004955 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004956 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004957 }
Wei Li340ee8e2022-03-18 17:33:24 -07004958 rule := testingModule.Rule("genProvenanceMetaData")
4959 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4960 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4961 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4962 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004963}
4964
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004965func TestPrebuiltApexName(t *testing.T) {
4966 testApex(t, `
4967 prebuilt_apex {
4968 name: "com.company.android.myapex",
4969 apex_name: "com.android.myapex",
4970 src: "company-myapex-arm.apex",
4971 }
4972 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4973
4974 testApex(t, `
4975 apex_set {
4976 name: "com.company.android.myapex",
4977 apex_name: "com.android.myapex",
4978 set: "company-myapex.apks",
4979 }
4980 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4981}
4982
4983func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4984 _ = android.GroupFixturePreparers(
4985 java.PrepareForTestWithJavaDefaultModules,
4986 PrepareForTestWithApexBuildComponents,
4987 android.FixtureWithRootAndroidBp(`
4988 platform_bootclasspath {
4989 name: "platform-bootclasspath",
4990 fragments: [
4991 {
4992 apex: "com.android.art",
4993 module: "art-bootclasspath-fragment",
4994 },
4995 ],
4996 }
4997
4998 prebuilt_apex {
4999 name: "com.company.android.art",
5000 apex_name: "com.android.art",
5001 src: "com.company.android.art-arm.apex",
5002 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
5003 }
5004
5005 prebuilt_bootclasspath_fragment {
5006 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01005007 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01005008 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01005009 hidden_api: {
5010 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5011 metadata: "my-bootclasspath-fragment/metadata.csv",
5012 index: "my-bootclasspath-fragment/index.csv",
5013 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5014 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5015 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01005016 }
5017
5018 java_import {
5019 name: "core-oj",
5020 jars: ["prebuilt.jar"],
5021 }
5022 `),
5023 ).RunTest(t)
5024}
5025
Spandan Das59a4a2b2024-01-09 21:35:56 +00005026// A minimal context object for use with DexJarBuildPath
5027type moduleErrorfTestCtx struct {
5028}
5029
5030func (ctx moduleErrorfTestCtx) ModuleErrorf(format string, args ...interface{}) {
5031}
5032
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005033func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005034 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005035 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005036 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5037 // is disabled.
5038 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
Spandan Das81fe4d12024-05-15 18:43:47 +00005039
5040 // Make sure that we have atleast one platform library so that we can check the monolithic hiddenapi
5041 // file creation.
5042 java.FixtureConfigureBootJars("platform:foo"),
5043 android.FixtureModifyMockFS(func(fs android.MockFS) {
5044 fs["platform/Android.bp"] = []byte(`
5045 java_library {
5046 name: "foo",
5047 srcs: ["Test.java"],
5048 compile_dex: true,
5049 }
5050 `)
5051 fs["platform/Test.java"] = nil
5052 }),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005053 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005054
Paul Duffin40a3f652021-07-19 13:11:24 +01005055 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005056 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005057 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005058 var rule android.TestingBuildParams
5059
5060 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5061 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005062 }
5063
Paul Duffin40a3f652021-07-19 13:11:24 +01005064 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5065 t.Helper()
5066 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5067 var rule android.TestingBuildParams
5068
5069 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5070 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5071 }
5072
Paul Duffin89f570a2021-06-16 01:42:33 +01005073 fragment := java.ApexVariantReference{
5074 Apex: proptools.StringPtr("myapex"),
5075 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5076 }
5077
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005078 t.Run("prebuilt only", func(t *testing.T) {
5079 bp := `
5080 prebuilt_apex {
5081 name: "myapex",
5082 arch: {
5083 arm64: {
5084 src: "myapex-arm64.apex",
5085 },
5086 arm: {
5087 src: "myapex-arm.apex",
5088 },
5089 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005090 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5091 }
5092
5093 prebuilt_bootclasspath_fragment {
5094 name: "my-bootclasspath-fragment",
5095 contents: ["libfoo", "libbar"],
5096 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005097 hidden_api: {
5098 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5099 metadata: "my-bootclasspath-fragment/metadata.csv",
5100 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005101 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5102 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5103 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005104 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005105 }
5106
Spandan Das52c01a12024-09-20 01:09:48 +00005107 java_sdk_library_import {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005108 name: "libfoo",
Spandan Das52c01a12024-09-20 01:09:48 +00005109 public: {
5110 jars: ["libfoo.jar"],
5111 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005112 apex_available: ["myapex"],
Spandan Das52c01a12024-09-20 01:09:48 +00005113 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005114 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005115 }
Paul Duffin37856732021-02-26 14:24:15 +00005116
5117 java_sdk_library_import {
5118 name: "libbar",
5119 public: {
5120 jars: ["libbar.jar"],
5121 },
5122 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005123 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005124 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005125 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005126 `
5127
Paul Duffin89f570a2021-06-16 01:42:33 +01005128 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005129
Paul Duffin537ea3d2021-05-14 10:38:00 +01005130 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005131 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005132 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005133 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005134 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005135 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005136 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005137 })
5138
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005139 t.Run("apex_set only", func(t *testing.T) {
5140 bp := `
5141 apex_set {
5142 name: "myapex",
5143 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005144 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
Liz Kammer2dc72442023-04-20 10:10:48 -04005145 exported_systemserverclasspath_fragments: ["my-systemserverclasspath-fragment"],
5146 }
5147
Paul Duffin89f570a2021-06-16 01:42:33 +01005148 prebuilt_bootclasspath_fragment {
5149 name: "my-bootclasspath-fragment",
5150 contents: ["libfoo", "libbar"],
5151 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005152 hidden_api: {
5153 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5154 metadata: "my-bootclasspath-fragment/metadata.csv",
5155 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005156 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5157 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5158 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005159 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005160 }
5161
Liz Kammer2dc72442023-04-20 10:10:48 -04005162 prebuilt_systemserverclasspath_fragment {
5163 name: "my-systemserverclasspath-fragment",
5164 contents: ["libbaz"],
5165 apex_available: ["myapex"],
5166 }
5167
Spandan Das52c01a12024-09-20 01:09:48 +00005168 java_sdk_library_import {
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005169 name: "libfoo",
Spandan Das52c01a12024-09-20 01:09:48 +00005170 public: {
5171 jars: ["libfoo.jar"],
5172 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005173 apex_available: ["myapex"],
Spandan Das52c01a12024-09-20 01:09:48 +00005174 shared_library: false,
5175 permitted_packages: ["libfoo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005176 }
5177
Spandan Das52c01a12024-09-20 01:09:48 +00005178
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005179 java_sdk_library_import {
5180 name: "libbar",
5181 public: {
5182 jars: ["libbar.jar"],
5183 },
5184 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005185 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005186 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005187 }
Liz Kammer2dc72442023-04-20 10:10:48 -04005188
5189 java_sdk_library_import {
5190 name: "libbaz",
5191 public: {
5192 jars: ["libbaz.jar"],
5193 },
5194 apex_available: ["myapex"],
5195 shared_library: false,
5196 permitted_packages: ["baz"],
5197 }
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005198 `
5199
Paul Duffin89f570a2021-06-16 01:42:33 +01005200 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005201
Paul Duffin537ea3d2021-05-14 10:38:00 +01005202 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005203 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005204 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005205 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005206 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005207 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005208 `)
Liz Kammer2dc72442023-04-20 10:10:48 -04005209
5210 myApex := ctx.ModuleForTests("myapex", "android_common_myapex").Module()
5211
5212 overrideNames := []string{
Spandan Dasa8e2d612024-07-26 19:24:27 +00005213 "",
Liz Kammer2dc72442023-04-20 10:10:48 -04005214 "myjavalib.myapex",
5215 "libfoo.myapex",
5216 "libbar.myapex",
5217 "libbaz.myapex",
5218 }
5219 mkEntries := android.AndroidMkEntriesForTest(t, ctx, myApex)
5220 for i, e := range mkEntries {
5221 g := e.OverrideName
5222 if w := overrideNames[i]; w != g {
5223 t.Errorf("Expected override name %q, got %q", w, g)
5224 }
5225 }
5226
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005227 })
5228
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005229 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5230 bp := `
5231 prebuilt_apex {
5232 name: "myapex",
5233 arch: {
5234 arm64: {
5235 src: "myapex-arm64.apex",
5236 },
5237 arm: {
5238 src: "myapex-arm.apex",
5239 },
5240 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005241 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5242 }
5243
5244 prebuilt_bootclasspath_fragment {
5245 name: "my-bootclasspath-fragment",
5246 contents: ["libfoo", "libbar"],
5247 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005248 hidden_api: {
5249 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5250 metadata: "my-bootclasspath-fragment/metadata.csv",
5251 index: "my-bootclasspath-fragment/index.csv",
5252 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5253 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5254 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005255 }
5256
5257 java_import {
5258 name: "libfoo",
5259 jars: ["libfoo.jar"],
5260 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005261 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005262 }
5263
5264 java_library {
5265 name: "libfoo",
5266 srcs: ["foo/bar/MyClass.java"],
5267 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005268 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005269 }
Paul Duffin37856732021-02-26 14:24:15 +00005270
5271 java_sdk_library_import {
5272 name: "libbar",
5273 public: {
5274 jars: ["libbar.jar"],
5275 },
5276 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005277 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005278 }
5279
5280 java_sdk_library {
5281 name: "libbar",
5282 srcs: ["foo/bar/MyClass.java"],
5283 unsafe_ignore_missing_latest_api: true,
5284 apex_available: ["myapex"],
5285 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005286 `
5287
5288 // In this test the source (java_library) libfoo is active since the
5289 // prebuilt (java_import) defaults to prefer:false. However the
5290 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5291 // find the dex boot jar in it. We either need to disable the source libfoo
5292 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005293 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005294 // dexbootjar check is skipped if AllowMissingDependencies is true
5295 preparerAllowMissingDeps := android.GroupFixturePreparers(
5296 preparer,
5297 android.PrepareForTestWithAllowMissingDependencies,
5298 )
5299 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005300 })
5301
5302 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5303 bp := `
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005304 apex {
5305 name: "myapex",
5306 key: "myapex.key",
5307 updatable: false,
5308 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5309 }
5310
5311 apex_key {
5312 name: "myapex.key",
5313 public_key: "testkey.avbpubkey",
5314 private_key: "testkey.pem",
5315 }
5316
5317 bootclasspath_fragment {
5318 name: "my-bootclasspath-fragment",
5319 contents: ["libfoo", "libbar"],
5320 apex_available: ["myapex"],
5321 hidden_api: {
5322 split_packages: ["*"],
5323 },
5324 }
5325
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005326 prebuilt_apex {
5327 name: "myapex",
5328 arch: {
5329 arm64: {
5330 src: "myapex-arm64.apex",
5331 },
5332 arm: {
5333 src: "myapex-arm.apex",
5334 },
5335 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005336 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5337 }
5338
5339 prebuilt_bootclasspath_fragment {
5340 name: "my-bootclasspath-fragment",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005341 prefer: true,
Paul Duffin89f570a2021-06-16 01:42:33 +01005342 contents: ["libfoo", "libbar"],
5343 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005344 hidden_api: {
5345 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5346 metadata: "my-bootclasspath-fragment/metadata.csv",
5347 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005348 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5349 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5350 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005351 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005352 }
5353
Spandan Das52c01a12024-09-20 01:09:48 +00005354 java_sdk_library_import {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005355 name: "libfoo",
5356 prefer: true,
Spandan Das52c01a12024-09-20 01:09:48 +00005357 public: {
5358 jars: ["libfoo.jar"],
5359 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005360 apex_available: ["myapex"],
Spandan Das52c01a12024-09-20 01:09:48 +00005361 shared_library: false,
5362 permitted_packages: ["libfoo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005363 }
5364
5365 java_library {
5366 name: "libfoo",
5367 srcs: ["foo/bar/MyClass.java"],
5368 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005369 installable: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00005370 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005371 }
Paul Duffin37856732021-02-26 14:24:15 +00005372
5373 java_sdk_library_import {
5374 name: "libbar",
5375 prefer: true,
5376 public: {
5377 jars: ["libbar.jar"],
5378 },
5379 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005380 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005381 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005382 }
5383
5384 java_sdk_library {
5385 name: "libbar",
5386 srcs: ["foo/bar/MyClass.java"],
5387 unsafe_ignore_missing_latest_api: true,
5388 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005389 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005390 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005391 `
5392
Paul Duffin89f570a2021-06-16 01:42:33 +01005393 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005394
Paul Duffin537ea3d2021-05-14 10:38:00 +01005395 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005396 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005397 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005398 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005399 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005400 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005401 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005402 })
5403
5404 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5405 bp := `
5406 apex {
5407 name: "myapex",
5408 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005409 updatable: false,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005410 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005411 }
5412
5413 apex_key {
5414 name: "myapex.key",
5415 public_key: "testkey.avbpubkey",
5416 private_key: "testkey.pem",
5417 }
5418
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005419 bootclasspath_fragment {
5420 name: "my-bootclasspath-fragment",
5421 contents: ["libfoo", "libbar"],
5422 apex_available: ["myapex"],
5423 hidden_api: {
5424 split_packages: ["*"],
5425 },
5426 }
5427
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005428 prebuilt_apex {
5429 name: "myapex",
5430 arch: {
5431 arm64: {
5432 src: "myapex-arm64.apex",
5433 },
5434 arm: {
5435 src: "myapex-arm.apex",
5436 },
5437 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005438 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5439 }
5440
5441 prebuilt_bootclasspath_fragment {
5442 name: "my-bootclasspath-fragment",
5443 contents: ["libfoo", "libbar"],
5444 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005445 hidden_api: {
5446 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5447 metadata: "my-bootclasspath-fragment/metadata.csv",
5448 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005449 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5450 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5451 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005452 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005453 }
5454
5455 java_import {
5456 name: "libfoo",
5457 jars: ["libfoo.jar"],
5458 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00005459 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005460 }
5461
5462 java_library {
5463 name: "libfoo",
5464 srcs: ["foo/bar/MyClass.java"],
5465 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005466 permitted_packages: ["foo"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005467 installable: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00005468 sdk_version: "core_current",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005469 }
Paul Duffin37856732021-02-26 14:24:15 +00005470
5471 java_sdk_library_import {
5472 name: "libbar",
5473 public: {
5474 jars: ["libbar.jar"],
5475 },
5476 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005477 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005478 }
5479
5480 java_sdk_library {
5481 name: "libbar",
5482 srcs: ["foo/bar/MyClass.java"],
5483 unsafe_ignore_missing_latest_api: true,
5484 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005485 permitted_packages: ["bar"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005486 compile_dex: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00005487 sdk_version: "core_current",
Paul Duffin37856732021-02-26 14:24:15 +00005488 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005489 `
5490
Paul Duffin89f570a2021-06-16 01:42:33 +01005491 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005492
Paul Duffin537ea3d2021-05-14 10:38:00 +01005493 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005494 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005495 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
5496 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005497 out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/modular-hiddenapi/index.csv
5498 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005499 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005500 })
5501
5502 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5503 bp := `
5504 apex {
5505 name: "myapex",
5506 enabled: false,
5507 key: "myapex.key",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005508 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005509 }
5510
5511 apex_key {
5512 name: "myapex.key",
5513 public_key: "testkey.avbpubkey",
5514 private_key: "testkey.pem",
5515 }
5516
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005517 bootclasspath_fragment {
5518 name: "my-bootclasspath-fragment",
5519 enabled: false,
5520 contents: ["libfoo", "libbar"],
5521 apex_available: ["myapex"],
5522 hidden_api: {
5523 split_packages: ["*"],
5524 },
5525 }
5526
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005527 prebuilt_apex {
5528 name: "myapex",
5529 arch: {
5530 arm64: {
5531 src: "myapex-arm64.apex",
5532 },
5533 arm: {
5534 src: "myapex-arm.apex",
5535 },
5536 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005537 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5538 }
5539
5540 prebuilt_bootclasspath_fragment {
5541 name: "my-bootclasspath-fragment",
5542 contents: ["libfoo", "libbar"],
5543 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005544 hidden_api: {
5545 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5546 metadata: "my-bootclasspath-fragment/metadata.csv",
5547 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005548 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5549 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5550 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005551 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005552 }
5553
5554 java_import {
5555 name: "libfoo",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005556 jars: ["libfoo.jar"],
5557 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005558 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005559 }
5560
5561 java_library {
5562 name: "libfoo",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005563 enabled: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005564 srcs: ["foo/bar/MyClass.java"],
5565 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005566 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005567 }
Paul Duffin37856732021-02-26 14:24:15 +00005568
5569 java_sdk_library_import {
5570 name: "libbar",
Paul Duffin37856732021-02-26 14:24:15 +00005571 public: {
5572 jars: ["libbar.jar"],
5573 },
5574 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005575 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005576 permitted_packages: ["bar"],
Jihoon Kanga6d0aa82024-09-24 00:34:49 +00005577 prefer: true,
Paul Duffin37856732021-02-26 14:24:15 +00005578 }
5579
5580 java_sdk_library {
5581 name: "libbar",
5582 srcs: ["foo/bar/MyClass.java"],
5583 unsafe_ignore_missing_latest_api: true,
5584 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005585 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005586 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005587 `
Cole Fausta963b942024-04-11 17:43:00 -07005588 // This test disables libbar, which causes the ComponentDepsMutator to add
5589 // deps on libbar.stubs and other sub-modules that don't exist. We can
5590 // enable AllowMissingDependencies to work around that, but enabling that
5591 // causes extra checks for missing source files to dex_bootjars, so add those
5592 // to the mock fs as well.
5593 preparer2 := android.GroupFixturePreparers(
5594 preparer,
5595 android.PrepareForTestWithAllowMissingDependencies,
5596 android.FixtureMergeMockFs(map[string][]byte{
5597 "build/soong/scripts/check_boot_jars/package_allowed_list.txt": nil,
Nouby Mohamed3413f302024-09-04 22:19:51 +00005598 "frameworks/base/boot/boot-profile.txt": nil,
Cole Fausta963b942024-04-11 17:43:00 -07005599 }),
5600 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005601
Cole Fausta963b942024-04-11 17:43:00 -07005602 ctx := testDexpreoptWithApexes(t, bp, "", preparer2, fragment)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005603
Paul Duffin537ea3d2021-05-14 10:38:00 +01005604 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005605 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005606 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005607 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005608 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005609 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005610 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005611 })
Spandan Das3a392012024-01-17 18:26:27 +00005612
Spandan Dasf2c10572024-02-27 04:49:52 +00005613 t.Run("Co-existing unflagged apexes should create a duplicate module error", func(t *testing.T) {
Spandan Das3a392012024-01-17 18:26:27 +00005614 bp := `
5615 // Source
5616 apex {
5617 name: "myapex",
5618 enabled: false,
5619 key: "myapex.key",
5620 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5621 }
5622
5623 apex_key {
5624 name: "myapex.key",
5625 public_key: "testkey.avbpubkey",
5626 private_key: "testkey.pem",
5627 }
5628
5629 // Prebuilt
5630 prebuilt_apex {
5631 name: "myapex.v1",
5632 source_apex_name: "myapex",
5633 arch: {
5634 arm64: {
5635 src: "myapex-arm64.apex",
5636 },
5637 arm: {
5638 src: "myapex-arm.apex",
5639 },
5640 },
5641 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5642 prefer: true,
5643 }
5644 prebuilt_apex {
5645 name: "myapex.v2",
5646 source_apex_name: "myapex",
5647 arch: {
5648 arm64: {
5649 src: "myapex-arm64.apex",
5650 },
5651 arm: {
5652 src: "myapex-arm.apex",
5653 },
5654 },
5655 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5656 prefer: true,
5657 }
5658
5659 prebuilt_bootclasspath_fragment {
5660 name: "my-bootclasspath-fragment",
5661 contents: ["libfoo", "libbar"],
5662 apex_available: ["myapex"],
5663 hidden_api: {
5664 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5665 metadata: "my-bootclasspath-fragment/metadata.csv",
5666 index: "my-bootclasspath-fragment/index.csv",
5667 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5668 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5669 },
5670 prefer: true,
5671 }
5672
5673 java_import {
5674 name: "libfoo",
5675 jars: ["libfoo.jar"],
5676 apex_available: ["myapex"],
5677 prefer: true,
5678 }
5679 java_import {
5680 name: "libbar",
5681 jars: ["libbar.jar"],
5682 apex_available: ["myapex"],
5683 prefer: true,
5684 }
5685 `
5686
Spandan Dasf2c10572024-02-27 04:49:52 +00005687 testDexpreoptWithApexes(t, bp, "Multiple prebuilt modules prebuilt_myapex.v1 and prebuilt_myapex.v2 have been marked as preferred for this source module", preparer, fragment)
Spandan Das3a392012024-01-17 18:26:27 +00005688 })
5689
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005690}
5691
Roland Levillain630846d2019-06-26 12:48:34 +01005692func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005693 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005694 apex_test {
5695 name: "myapex",
5696 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005697 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005698 tests: [
5699 "mytest",
5700 ],
5701 }
5702
5703 apex_key {
5704 name: "myapex.key",
5705 public_key: "testkey.avbpubkey",
5706 private_key: "testkey.pem",
5707 }
5708
Liz Kammer1c14a212020-05-12 15:26:55 -07005709 filegroup {
5710 name: "fg",
5711 srcs: [
5712 "baz",
5713 "bar/baz"
5714 ],
5715 }
5716
Roland Levillain630846d2019-06-26 12:48:34 +01005717 cc_test {
5718 name: "mytest",
5719 gtest: false,
5720 srcs: ["mytest.cpp"],
5721 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005722 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005723 system_shared_libs: [],
5724 static_executable: true,
5725 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005726 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005727 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005728
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005729 cc_library {
5730 name: "mylib",
5731 srcs: ["mylib.cpp"],
5732 system_shared_libs: [],
5733 stl: "none",
5734 }
5735
Liz Kammer5bd365f2020-05-27 15:15:11 -07005736 filegroup {
5737 name: "fg2",
5738 srcs: [
5739 "testdata/baz"
5740 ],
5741 }
Roland Levillain630846d2019-06-26 12:48:34 +01005742 `)
5743
Jooyung Hana0503a52023-08-23 13:12:50 +09005744 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005745 copyCmds := apexRule.Args["copy_commands"]
5746
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005747 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005748 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005749 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005750
Liz Kammer1c14a212020-05-12 15:26:55 -07005751 //Ensure that test data are copied into apex.
5752 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5753 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5754
Roland Levillainf89cd092019-07-29 16:22:59 +01005755 // Ensure the module is correctly translated.
Jooyung Hana0503a52023-08-23 13:12:50 +09005756 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005757 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005758 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005759 prefix := "TARGET_"
5760 var builder strings.Builder
5761 data.Custom(&builder, name, prefix, "", data)
5762 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005763 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005764 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005765}
5766
Jooyung Hand48f3c32019-08-23 11:18:57 +09005767func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5768 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5769 apex {
5770 name: "myapex",
5771 key: "myapex.key",
5772 native_shared_libs: ["libfoo"],
5773 }
5774
5775 apex_key {
5776 name: "myapex.key",
5777 public_key: "testkey.avbpubkey",
5778 private_key: "testkey.pem",
5779 }
5780
5781 cc_library {
5782 name: "libfoo",
5783 stl: "none",
5784 system_shared_libs: [],
5785 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005786 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005787 }
5788 `)
5789 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5790 apex {
5791 name: "myapex",
5792 key: "myapex.key",
5793 java_libs: ["myjar"],
5794 }
5795
5796 apex_key {
5797 name: "myapex.key",
5798 public_key: "testkey.avbpubkey",
5799 private_key: "testkey.pem",
5800 }
5801
5802 java_library {
5803 name: "myjar",
5804 srcs: ["foo/bar/MyClass.java"],
5805 sdk_version: "none",
5806 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005807 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005808 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005809 }
5810 `)
5811}
5812
Bill Peckhama41a6962021-01-11 10:58:54 -08005813func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005814 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005815 apex {
5816 name: "myapex",
5817 key: "myapex.key",
5818 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005819 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005820 }
5821
5822 apex_key {
5823 name: "myapex.key",
5824 public_key: "testkey.avbpubkey",
5825 private_key: "testkey.pem",
5826 }
5827
5828 java_import {
5829 name: "myjavaimport",
5830 apex_available: ["myapex"],
5831 jars: ["my.jar"],
5832 compile_dex: true,
5833 }
5834 `)
5835
Jooyung Hana0503a52023-08-23 13:12:50 +09005836 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Bill Peckhama41a6962021-01-11 10:58:54 -08005837 apexRule := module.Rule("apexRule")
5838 copyCmds := apexRule.Args["copy_commands"]
5839 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5840}
5841
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005842func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005843 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005844 apex {
5845 name: "myapex",
5846 key: "myapex.key",
5847 apps: [
5848 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005849 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005850 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005851 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005852 }
5853
5854 apex_key {
5855 name: "myapex.key",
5856 public_key: "testkey.avbpubkey",
5857 private_key: "testkey.pem",
5858 }
5859
5860 android_app {
5861 name: "AppFoo",
5862 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005863 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005864 system_modules: "none",
Jiyong Park970c5242024-05-17 22:58:54 +00005865 use_embedded_native_libs: true,
Jiyong Park8be103b2019-11-08 15:53:48 +09005866 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005867 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005868 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005869 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005870
5871 android_app {
5872 name: "AppFooPriv",
5873 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005874 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005875 system_modules: "none",
5876 privileged: true,
Sam Delmerico15809f82023-05-15 17:21:47 -04005877 privapp_allowlist: "privapp_allowlist_com.android.AppFooPriv.xml",
Colin Cross094cde42020-02-15 10:38:00 -08005878 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005879 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005880 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005881
5882 cc_library_shared {
5883 name: "libjni",
5884 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005885 shared_libs: ["libfoo"],
5886 stl: "none",
5887 system_shared_libs: [],
5888 apex_available: [ "myapex" ],
5889 sdk_version: "current",
5890 }
5891
5892 cc_library_shared {
5893 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005894 stl: "none",
5895 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005896 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005897 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005898 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005899 `)
5900
Jooyung Hana0503a52023-08-23 13:12:50 +09005901 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005902 apexRule := module.Rule("apexRule")
5903 copyCmds := apexRule.Args["copy_commands"]
5904
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005905 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5906 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Andrei Onea580636b2022-08-17 16:53:46 +00005907 ensureContains(t, copyCmds, "image.apex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005908
Colin Crossaede88c2020-08-11 12:17:01 -07005909 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005910 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005911 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005912 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005913 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005914 // JNI libraries including transitive deps are
5915 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005916 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005917 // ... embedded inside APK (jnilibs.zip)
5918 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5919 // ... and not directly inside the APEX
5920 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5921 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04005922
5923 apexBundle := module.Module().(*apexBundle)
5924 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
5925 var builder strings.Builder
5926 data.Custom(&builder, apexBundle.Name(), "TARGET_", "", data)
5927 androidMk := builder.String()
5928 ensureContains(t, androidMk, "LOCAL_MODULE := AppFooPriv.myapex")
5929 ensureContains(t, androidMk, "LOCAL_MODULE := AppFoo.myapex")
5930 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFooPriv.apk")
5931 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFoo.apk")
5932 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := \\S+AppFooPriv.apk")
5933 ensureContains(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := privapp_allowlist_com.android.AppFooPriv.xml:$(PRODUCT_OUT)/apex/myapex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Dario Frenicde2a032019-10-27 00:29:22 +01005934}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005935
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005936func TestApexWithAppImportBuildId(t *testing.T) {
5937 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5938 for _, id := range invalidBuildIds {
5939 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5940 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5941 variables.BuildId = proptools.StringPtr(id)
5942 })
5943 testApexError(t, message, `apex {
5944 name: "myapex",
5945 key: "myapex.key",
5946 apps: ["AppFooPrebuilt"],
5947 updatable: false,
5948 }
5949
5950 apex_key {
5951 name: "myapex.key",
5952 public_key: "testkey.avbpubkey",
5953 private_key: "testkey.pem",
5954 }
5955
5956 android_app_import {
5957 name: "AppFooPrebuilt",
5958 apk: "PrebuiltAppFoo.apk",
5959 presigned: true,
5960 apex_available: ["myapex"],
5961 }
5962 `, fixture)
5963 }
5964}
5965
Dario Frenicde2a032019-10-27 00:29:22 +01005966func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005967 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005968 apex {
5969 name: "myapex",
5970 key: "myapex.key",
5971 apps: [
5972 "AppFooPrebuilt",
5973 "AppFooPrivPrebuilt",
5974 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005975 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005976 }
5977
5978 apex_key {
5979 name: "myapex.key",
5980 public_key: "testkey.avbpubkey",
5981 private_key: "testkey.pem",
5982 }
5983
5984 android_app_import {
5985 name: "AppFooPrebuilt",
5986 apk: "PrebuiltAppFoo.apk",
5987 presigned: true,
5988 dex_preopt: {
5989 enabled: false,
5990 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005991 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005992 }
5993
5994 android_app_import {
5995 name: "AppFooPrivPrebuilt",
5996 apk: "PrebuiltAppFooPriv.apk",
5997 privileged: true,
5998 presigned: true,
5999 dex_preopt: {
6000 enabled: false,
6001 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006002 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006003 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006004 }
6005 `)
6006
Jooyung Hana0503a52023-08-23 13:12:50 +09006007 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Frenicde2a032019-10-27 00:29:22 +01006008 apexRule := module.Rule("apexRule")
6009 copyCmds := apexRule.Args["copy_commands"]
6010
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006011 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6012 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006013}
6014
6015func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006016 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006017 apex {
6018 name: "myapex",
6019 key: "myapex.key",
6020 apps: [
6021 "AppFoo",
6022 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006023 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006024 }
6025
6026 apex_key {
6027 name: "myapex.key",
6028 public_key: "testkey.avbpubkey",
6029 private_key: "testkey.pem",
6030 }
6031
6032 android_app {
6033 name: "AppFoo",
6034 srcs: ["foo/bar/MyClass.java"],
6035 sdk_version: "none",
6036 system_modules: "none",
6037 apex_available: [ "myapex" ],
6038 }
6039
6040 android_app_import {
6041 name: "AppFoo",
6042 apk: "AppFooPrebuilt.apk",
6043 filename: "AppFooPrebuilt.apk",
6044 presigned: true,
6045 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006046 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006047 }
6048 `, withFiles(map[string][]byte{
6049 "AppFooPrebuilt.apk": nil,
6050 }))
6051
Jooyung Hana0503a52023-08-23 13:12:50 +09006052 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006053 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006054 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006055}
6056
Dario Freni6f3937c2019-12-20 22:58:03 +00006057func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006058 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006059 apex {
6060 name: "myapex",
6061 key: "myapex.key",
6062 apps: [
6063 "TesterHelpAppFoo",
6064 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006065 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006066 }
6067
6068 apex_key {
6069 name: "myapex.key",
6070 public_key: "testkey.avbpubkey",
6071 private_key: "testkey.pem",
6072 }
6073
6074 android_test_helper_app {
6075 name: "TesterHelpAppFoo",
6076 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006077 apex_available: [ "myapex" ],
Jihoon Kang85bc1932024-07-01 17:04:46 +00006078 sdk_version: "test_current",
Dario Freni6f3937c2019-12-20 22:58:03 +00006079 }
6080
6081 `)
6082
Jooyung Hana0503a52023-08-23 13:12:50 +09006083 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Freni6f3937c2019-12-20 22:58:03 +00006084 apexRule := module.Rule("apexRule")
6085 copyCmds := apexRule.Args["copy_commands"]
6086
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006087 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006088}
6089
Jooyung Han18020ea2019-11-13 10:50:48 +09006090func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6091 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006092 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006093 apex {
6094 name: "myapex",
6095 key: "myapex.key",
6096 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006097 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006098 }
6099
6100 apex_key {
6101 name: "myapex.key",
6102 public_key: "testkey.avbpubkey",
6103 private_key: "testkey.pem",
6104 }
6105
6106 apex {
6107 name: "otherapex",
6108 key: "myapex.key",
6109 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006110 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006111 }
6112
6113 cc_defaults {
6114 name: "libfoo-defaults",
6115 apex_available: ["otherapex"],
6116 }
6117
6118 cc_library {
6119 name: "libfoo",
6120 defaults: ["libfoo-defaults"],
6121 stl: "none",
6122 system_shared_libs: [],
6123 }`)
6124}
6125
Paul Duffine52e66f2020-03-30 17:54:29 +01006126func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006127 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006128 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006129 apex {
6130 name: "myapex",
6131 key: "myapex.key",
6132 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006133 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006134 }
6135
6136 apex_key {
6137 name: "myapex.key",
6138 public_key: "testkey.avbpubkey",
6139 private_key: "testkey.pem",
6140 }
6141
6142 apex {
6143 name: "otherapex",
6144 key: "otherapex.key",
6145 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006146 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006147 }
6148
6149 apex_key {
6150 name: "otherapex.key",
6151 public_key: "testkey.avbpubkey",
6152 private_key: "testkey.pem",
6153 }
6154
6155 cc_library {
6156 name: "libfoo",
6157 stl: "none",
6158 system_shared_libs: [],
6159 apex_available: ["otherapex"],
6160 }`)
Chan Wang490a6f92024-09-23 11:52:00 +00006161
6162 // 'apex_available' check is bypassed for /product apex with a specific prefix.
6163 // TODO: b/352818241 - Remove below two cases after APEX availability is enforced for /product APEXes.
6164 testApex(t, `
6165 apex {
6166 name: "com.sdv.myapex",
6167 key: "myapex.key",
6168 native_shared_libs: ["libfoo"],
6169 updatable: false,
6170 product_specific: true,
6171 }
6172
6173 apex_key {
6174 name: "myapex.key",
6175 public_key: "testkey.avbpubkey",
6176 private_key: "testkey.pem",
6177 }
6178
6179 apex {
6180 name: "com.any.otherapex",
6181 key: "otherapex.key",
6182 native_shared_libs: ["libfoo"],
6183 updatable: false,
6184 }
6185
6186 apex_key {
6187 name: "otherapex.key",
6188 public_key: "testkey.avbpubkey",
6189 private_key: "testkey.pem",
6190 }
6191
6192 cc_library {
6193 name: "libfoo",
6194 stl: "none",
6195 system_shared_libs: [],
6196 apex_available: ["com.any.otherapex"],
6197 product_specific: true,
6198 }`,
6199 android.FixtureMergeMockFs(android.MockFS{
6200 "system/sepolicy/apex/com.sdv.myapex-file_contexts": nil,
6201 "system/sepolicy/apex/com.any.otherapex-file_contexts": nil,
6202 }))
6203
6204 // 'apex_available' check is not bypassed for non-product apex with a specific prefix.
6205 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
6206 apex {
6207 name: "com.sdv.myapex",
6208 key: "myapex.key",
6209 native_shared_libs: ["libfoo"],
6210 updatable: false,
6211 }
6212
6213 apex_key {
6214 name: "myapex.key",
6215 public_key: "testkey.avbpubkey",
6216 private_key: "testkey.pem",
6217 }
6218
6219 apex {
6220 name: "com.any.otherapex",
6221 key: "otherapex.key",
6222 native_shared_libs: ["libfoo"],
6223 updatable: false,
6224 }
6225
6226 apex_key {
6227 name: "otherapex.key",
6228 public_key: "testkey.avbpubkey",
6229 private_key: "testkey.pem",
6230 }
6231
6232 cc_library {
6233 name: "libfoo",
6234 stl: "none",
6235 system_shared_libs: [],
6236 apex_available: ["com.any.otherapex"],
6237 }`,
6238 android.FixtureMergeMockFs(android.MockFS{
6239 "system/sepolicy/apex/com.sdv.myapex-file_contexts": nil,
6240 "system/sepolicy/apex/com.any.otherapex-file_contexts": nil,
6241 }))
Paul Duffine52e66f2020-03-30 17:54:29 +01006242}
Jiyong Park127b40b2019-09-30 16:04:35 +09006243
Paul Duffine52e66f2020-03-30 17:54:29 +01006244func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006245 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006246 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006247.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006248.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006249.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006250.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006251.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006252.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006253 apex {
6254 name: "myapex",
6255 key: "myapex.key",
6256 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006257 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006258 }
6259
6260 apex_key {
6261 name: "myapex.key",
6262 public_key: "testkey.avbpubkey",
6263 private_key: "testkey.pem",
6264 }
6265
Jiyong Park127b40b2019-09-30 16:04:35 +09006266 cc_library {
6267 name: "libfoo",
6268 stl: "none",
6269 shared_libs: ["libbar"],
6270 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006271 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006272 }
6273
6274 cc_library {
6275 name: "libbar",
6276 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006277 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006278 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006279 apex_available: ["myapex"],
6280 }
6281
6282 cc_library {
6283 name: "libbaz",
6284 stl: "none",
6285 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006286 }`)
Chan Wang490a6f92024-09-23 11:52:00 +00006287
6288 // 'apex_available' check is bypassed for /product apex with a specific prefix.
6289 // TODO: b/352818241 - Remove below two cases after APEX availability is enforced for /product APEXes.
6290 testApex(t, `
6291 apex {
6292 name: "com.sdv.myapex",
6293 key: "myapex.key",
6294 native_shared_libs: ["libfoo"],
6295 updatable: false,
6296 product_specific: true,
6297 }
6298
6299 apex_key {
6300 name: "myapex.key",
6301 public_key: "testkey.avbpubkey",
6302 private_key: "testkey.pem",
6303 }
6304
6305 cc_library {
6306 name: "libfoo",
6307 stl: "none",
6308 shared_libs: ["libbar"],
6309 system_shared_libs: [],
6310 apex_available: ["com.sdv.myapex"],
6311 product_specific: true,
6312 }
6313
6314 cc_library {
6315 name: "libbar",
6316 stl: "none",
6317 shared_libs: ["libbaz"],
6318 system_shared_libs: [],
6319 apex_available: ["com.sdv.myapex"],
6320 product_specific: true,
6321 }
6322
6323 cc_library {
6324 name: "libbaz",
6325 stl: "none",
6326 system_shared_libs: [],
6327 product_specific: true,
6328 }`,
6329 android.FixtureMergeMockFs(android.MockFS{
6330 "system/sepolicy/apex/com.sdv.myapex-file_contexts": nil,
6331 }))
6332
6333 // 'apex_available' check is not bypassed for non-product apex with a specific prefix.
6334 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.`, `
6335 apex {
6336 name: "com.sdv.myapex",
6337 key: "myapex.key",
6338 native_shared_libs: ["libfoo"],
6339 updatable: false,
6340 }
6341
6342 apex_key {
6343 name: "myapex.key",
6344 public_key: "testkey.avbpubkey",
6345 private_key: "testkey.pem",
6346 }
6347
6348 cc_library {
6349 name: "libfoo",
6350 stl: "none",
6351 shared_libs: ["libbar"],
6352 system_shared_libs: [],
6353 apex_available: ["com.sdv.myapex"],
6354 }
6355
6356 cc_library {
6357 name: "libbar",
6358 stl: "none",
6359 shared_libs: ["libbaz"],
6360 system_shared_libs: [],
6361 apex_available: ["com.sdv.myapex"],
6362 }
6363
6364 cc_library {
6365 name: "libbaz",
6366 stl: "none",
6367 system_shared_libs: [],
6368 }`,
6369 android.FixtureMergeMockFs(android.MockFS{
6370 "system/sepolicy/apex/com.sdv.myapex-file_contexts": nil,
6371 }))
Paul Duffine52e66f2020-03-30 17:54:29 +01006372}
Jiyong Park127b40b2019-09-30 16:04:35 +09006373
Liz Kammer5f108fa2023-05-11 14:33:17 -04006374func TestApexAvailable_IndirectStaticDep(t *testing.T) {
6375 testApex(t, `
6376 apex {
6377 name: "myapex",
6378 key: "myapex.key",
6379 native_shared_libs: ["libfoo"],
6380 updatable: false,
6381 }
6382
6383 apex_key {
6384 name: "myapex.key",
6385 public_key: "testkey.avbpubkey",
6386 private_key: "testkey.pem",
6387 }
6388
6389 cc_library {
6390 name: "libfoo",
6391 stl: "none",
6392 static_libs: ["libbar"],
6393 system_shared_libs: [],
6394 apex_available: ["myapex"],
6395 }
6396
6397 cc_library {
6398 name: "libbar",
6399 stl: "none",
6400 shared_libs: ["libbaz"],
6401 system_shared_libs: [],
6402 apex_available: ["myapex"],
6403 }
6404
6405 cc_library {
6406 name: "libbaz",
6407 stl: "none",
6408 system_shared_libs: [],
6409 }`)
6410
6411 testApexError(t, `requires "libbar" that doesn't list the APEX under 'apex_available'.`, `
6412 apex {
6413 name: "myapex",
6414 key: "myapex.key",
6415 native_shared_libs: ["libfoo"],
6416 updatable: false,
6417 }
6418
6419 apex_key {
6420 name: "myapex.key",
6421 public_key: "testkey.avbpubkey",
6422 private_key: "testkey.pem",
6423 }
6424
6425 cc_library {
6426 name: "libfoo",
6427 stl: "none",
6428 static_libs: ["libbar"],
6429 system_shared_libs: [],
6430 apex_available: ["myapex"],
6431 }
6432
6433 cc_library {
6434 name: "libbar",
6435 stl: "none",
6436 system_shared_libs: [],
6437 }`)
6438}
6439
Paul Duffine52e66f2020-03-30 17:54:29 +01006440func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006441 testApexError(t, "\"otherapex\" is not a valid module name", `
6442 apex {
6443 name: "myapex",
6444 key: "myapex.key",
6445 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006446 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006447 }
6448
6449 apex_key {
6450 name: "myapex.key",
6451 public_key: "testkey.avbpubkey",
6452 private_key: "testkey.pem",
6453 }
6454
6455 cc_library {
6456 name: "libfoo",
6457 stl: "none",
6458 system_shared_libs: [],
6459 apex_available: ["otherapex"],
6460 }`)
6461
Paul Duffine52e66f2020-03-30 17:54:29 +01006462 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006463 apex {
6464 name: "myapex",
6465 key: "myapex.key",
6466 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006467 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006468 }
6469
6470 apex_key {
6471 name: "myapex.key",
6472 public_key: "testkey.avbpubkey",
6473 private_key: "testkey.pem",
6474 }
6475
6476 cc_library {
6477 name: "libfoo",
6478 stl: "none",
6479 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006480 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006481 apex_available: ["myapex"],
6482 }
6483
6484 cc_library {
6485 name: "libbar",
6486 stl: "none",
6487 system_shared_libs: [],
6488 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006489 }
6490
6491 cc_library {
6492 name: "libbaz",
6493 stl: "none",
6494 system_shared_libs: [],
6495 stubs: {
6496 versions: ["10", "20", "30"],
6497 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006498 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006499}
Jiyong Park127b40b2019-09-30 16:04:35 +09006500
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006501func TestApexAvailable_ApexAvailableNameWithVersionCodeError(t *testing.T) {
6502 t.Run("negative variant_version produces error", func(t *testing.T) {
6503 testApexError(t, "expected an integer between 0-9; got -1", `
6504 apex {
6505 name: "myapex",
6506 key: "myapex.key",
6507 apex_available_name: "com.android.foo",
6508 variant_version: "-1",
6509 updatable: false,
6510 }
6511 apex_key {
6512 name: "myapex.key",
6513 public_key: "testkey.avbpubkey",
6514 private_key: "testkey.pem",
6515 }
6516 `)
6517 })
6518
6519 t.Run("variant_version greater than 9 produces error", func(t *testing.T) {
6520 testApexError(t, "expected an integer between 0-9; got 10", `
6521 apex {
6522 name: "myapex",
6523 key: "myapex.key",
6524 apex_available_name: "com.android.foo",
6525 variant_version: "10",
6526 updatable: false,
6527 }
6528 apex_key {
6529 name: "myapex.key",
6530 public_key: "testkey.avbpubkey",
6531 private_key: "testkey.pem",
6532 }
6533 `)
6534 })
6535}
6536
6537func TestApexAvailable_ApexAvailableNameWithVersionCode(t *testing.T) {
6538 context := android.GroupFixturePreparers(
6539 android.PrepareForIntegrationTestWithAndroid,
6540 PrepareForTestWithApexBuildComponents,
6541 android.FixtureMergeMockFs(android.MockFS{
6542 "system/sepolicy/apex/foo-file_contexts": nil,
6543 "system/sepolicy/apex/bar-file_contexts": nil,
6544 }),
6545 )
6546 result := context.RunTestWithBp(t, `
6547 apex {
6548 name: "foo",
6549 key: "myapex.key",
6550 apex_available_name: "com.android.foo",
6551 variant_version: "0",
6552 updatable: false,
6553 }
6554 apex {
6555 name: "bar",
6556 key: "myapex.key",
6557 apex_available_name: "com.android.foo",
6558 variant_version: "3",
6559 updatable: false,
6560 }
6561 apex_key {
6562 name: "myapex.key",
6563 public_key: "testkey.avbpubkey",
6564 private_key: "testkey.pem",
6565 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006566 override_apex {
6567 name: "myoverrideapex",
6568 base: "bar",
6569 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006570 `)
6571
Jooyung Hana0503a52023-08-23 13:12:50 +09006572 fooManifestRule := result.ModuleForTests("foo", "android_common_foo").Rule("apexManifestRule")
Alyssa Ketpreechasawat3a6eced2024-08-22 15:09:16 +00006573 fooExpectedDefaultVersion := testDefaultUpdatableModuleVersion
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006574 fooActualDefaultVersion := fooManifestRule.Args["default_version"]
6575 if fooActualDefaultVersion != fooExpectedDefaultVersion {
6576 t.Errorf("expected to find defaultVersion %q; got %q", fooExpectedDefaultVersion, fooActualDefaultVersion)
6577 }
6578
Jooyung Hana0503a52023-08-23 13:12:50 +09006579 barManifestRule := result.ModuleForTests("bar", "android_common_bar").Rule("apexManifestRule")
Alyssa Ketpreechasawat3a6eced2024-08-22 15:09:16 +00006580 defaultVersionInt, _ := strconv.Atoi(testDefaultUpdatableModuleVersion)
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006581 barExpectedDefaultVersion := fmt.Sprint(defaultVersionInt + 3)
6582 barActualDefaultVersion := barManifestRule.Args["default_version"]
6583 if barActualDefaultVersion != barExpectedDefaultVersion {
6584 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6585 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006586
Spandan Das50801e22024-05-13 18:29:45 +00006587 overrideBarManifestRule := result.ModuleForTests("bar", "android_common_myoverrideapex_myoverrideapex").Rule("apexManifestRule")
Sam Delmerico419f9a32023-07-21 12:00:13 -04006588 overrideBarActualDefaultVersion := overrideBarManifestRule.Args["default_version"]
6589 if overrideBarActualDefaultVersion != barExpectedDefaultVersion {
6590 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6591 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006592}
6593
Sam Delmericoca816532023-06-02 14:09:50 -04006594func TestApexAvailable_ApexAvailableName(t *testing.T) {
6595 t.Run("using name of apex that sets apex_available_name is not allowed", func(t *testing.T) {
6596 testApexError(t, "Consider adding \"myapex\" to 'apex_available' property of \"AppFoo\"", `
6597 apex {
6598 name: "myapex_sminus",
6599 key: "myapex.key",
6600 apps: ["AppFoo"],
6601 apex_available_name: "myapex",
6602 updatable: false,
6603 }
6604 apex {
6605 name: "myapex",
6606 key: "myapex.key",
6607 apps: ["AppFoo"],
6608 updatable: false,
6609 }
6610 apex_key {
6611 name: "myapex.key",
6612 public_key: "testkey.avbpubkey",
6613 private_key: "testkey.pem",
6614 }
6615 android_app {
6616 name: "AppFoo",
6617 srcs: ["foo/bar/MyClass.java"],
6618 sdk_version: "none",
6619 system_modules: "none",
6620 apex_available: [ "myapex_sminus" ],
6621 }`,
6622 android.FixtureMergeMockFs(android.MockFS{
6623 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6624 }),
6625 )
6626 })
6627
6628 t.Run("apex_available_name allows module to be used in two different apexes", func(t *testing.T) {
6629 testApex(t, `
6630 apex {
6631 name: "myapex_sminus",
6632 key: "myapex.key",
6633 apps: ["AppFoo"],
6634 apex_available_name: "myapex",
6635 updatable: false,
6636 }
6637 apex {
6638 name: "myapex",
6639 key: "myapex.key",
6640 apps: ["AppFoo"],
6641 updatable: false,
6642 }
6643 apex_key {
6644 name: "myapex.key",
6645 public_key: "testkey.avbpubkey",
6646 private_key: "testkey.pem",
6647 }
6648 android_app {
6649 name: "AppFoo",
6650 srcs: ["foo/bar/MyClass.java"],
6651 sdk_version: "none",
6652 system_modules: "none",
6653 apex_available: [ "myapex" ],
6654 }`,
6655 android.FixtureMergeMockFs(android.MockFS{
6656 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6657 }),
6658 )
6659 })
6660
6661 t.Run("override_apexes work with apex_available_name", func(t *testing.T) {
6662 testApex(t, `
6663 override_apex {
6664 name: "myoverrideapex_sminus",
6665 base: "myapex_sminus",
6666 key: "myapex.key",
6667 apps: ["AppFooOverride"],
6668 }
6669 override_apex {
6670 name: "myoverrideapex",
6671 base: "myapex",
6672 key: "myapex.key",
6673 apps: ["AppFooOverride"],
6674 }
6675 apex {
6676 name: "myapex_sminus",
6677 key: "myapex.key",
6678 apps: ["AppFoo"],
6679 apex_available_name: "myapex",
6680 updatable: false,
6681 }
6682 apex {
6683 name: "myapex",
6684 key: "myapex.key",
6685 apps: ["AppFoo"],
6686 updatable: false,
6687 }
6688 apex_key {
6689 name: "myapex.key",
6690 public_key: "testkey.avbpubkey",
6691 private_key: "testkey.pem",
6692 }
6693 android_app {
6694 name: "AppFooOverride",
6695 srcs: ["foo/bar/MyClass.java"],
6696 sdk_version: "none",
6697 system_modules: "none",
6698 apex_available: [ "myapex" ],
6699 }
6700 android_app {
6701 name: "AppFoo",
6702 srcs: ["foo/bar/MyClass.java"],
6703 sdk_version: "none",
6704 system_modules: "none",
6705 apex_available: [ "myapex" ],
6706 }`,
6707 android.FixtureMergeMockFs(android.MockFS{
6708 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6709 }),
6710 )
6711 })
6712}
6713
6714func TestApexAvailable_ApexAvailableNameWithOverrides(t *testing.T) {
6715 context := android.GroupFixturePreparers(
6716 android.PrepareForIntegrationTestWithAndroid,
6717 PrepareForTestWithApexBuildComponents,
6718 java.PrepareForTestWithDexpreopt,
6719 android.FixtureMergeMockFs(android.MockFS{
6720 "system/sepolicy/apex/myapex-file_contexts": nil,
6721 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6722 }),
6723 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6724 variables.BuildId = proptools.StringPtr("buildid")
6725 }),
6726 )
6727 context.RunTestWithBp(t, `
6728 override_apex {
6729 name: "myoverrideapex_sminus",
6730 base: "myapex_sminus",
6731 }
6732 override_apex {
6733 name: "myoverrideapex",
6734 base: "myapex",
6735 }
6736 apex {
6737 name: "myapex",
6738 key: "myapex.key",
6739 apps: ["AppFoo"],
6740 updatable: false,
6741 }
6742 apex {
6743 name: "myapex_sminus",
6744 apex_available_name: "myapex",
6745 key: "myapex.key",
6746 apps: ["AppFoo_sminus"],
6747 updatable: false,
6748 }
6749 apex_key {
6750 name: "myapex.key",
6751 public_key: "testkey.avbpubkey",
6752 private_key: "testkey.pem",
6753 }
6754 android_app {
6755 name: "AppFoo",
6756 srcs: ["foo/bar/MyClass.java"],
6757 sdk_version: "none",
6758 system_modules: "none",
6759 apex_available: [ "myapex" ],
6760 }
6761 android_app {
6762 name: "AppFoo_sminus",
6763 srcs: ["foo/bar/MyClass.java"],
6764 sdk_version: "none",
6765 min_sdk_version: "29",
6766 system_modules: "none",
6767 apex_available: [ "myapex" ],
6768 }`)
6769}
6770
Jiyong Park89e850a2020-04-07 16:37:39 +09006771func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006772 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006773 apex {
6774 name: "myapex",
6775 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006776 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006777 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006778 }
6779
6780 apex_key {
6781 name: "myapex.key",
6782 public_key: "testkey.avbpubkey",
6783 private_key: "testkey.pem",
6784 }
6785
6786 cc_library {
6787 name: "libfoo",
6788 stl: "none",
6789 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006790 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006791 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006792 }
6793
6794 cc_library {
6795 name: "libfoo2",
6796 stl: "none",
6797 system_shared_libs: [],
6798 shared_libs: ["libbaz"],
6799 apex_available: ["//apex_available:platform"],
6800 }
6801
6802 cc_library {
6803 name: "libbar",
6804 stl: "none",
6805 system_shared_libs: [],
6806 apex_available: ["myapex"],
6807 }
6808
6809 cc_library {
6810 name: "libbaz",
6811 stl: "none",
6812 system_shared_libs: [],
6813 apex_available: ["myapex"],
6814 stubs: {
6815 versions: ["1"],
6816 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006817 }`)
6818
Jiyong Park89e850a2020-04-07 16:37:39 +09006819 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6820 // because it depends on libbar which isn't available to platform
6821 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6822 if libfoo.NotAvailableForPlatform() != true {
6823 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6824 }
6825
6826 // libfoo2 however can be available to platform because it depends on libbaz which provides
6827 // stubs
6828 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6829 if libfoo2.NotAvailableForPlatform() == true {
6830 t.Errorf("%q should be available to platform", libfoo2.String())
6831 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006832}
Jiyong Parka90ca002019-10-07 15:47:24 +09006833
Paul Duffine52e66f2020-03-30 17:54:29 +01006834func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006835 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006836 apex {
6837 name: "myapex",
6838 key: "myapex.key",
6839 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006840 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006841 }
6842
6843 apex_key {
6844 name: "myapex.key",
6845 public_key: "testkey.avbpubkey",
6846 private_key: "testkey.pem",
6847 }
6848
6849 cc_library {
6850 name: "libfoo",
6851 stl: "none",
6852 system_shared_libs: [],
6853 apex_available: ["myapex"],
6854 static: {
6855 apex_available: ["//apex_available:platform"],
6856 },
6857 }`)
6858
Jiyong Park89e850a2020-04-07 16:37:39 +09006859 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6860 if libfooShared.NotAvailableForPlatform() != true {
6861 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6862 }
6863 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6864 if libfooStatic.NotAvailableForPlatform() != false {
6865 t.Errorf("%q should be available to platform", libfooStatic.String())
6866 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006867}
6868
Jooyung Han9a419e22024-08-16 17:14:21 +09006869func TestApexAvailable_PrefixMatch(t *testing.T) {
6870
6871 for _, tc := range []struct {
6872 name string
6873 apexAvailable string
6874 expectedError string
6875 }{
6876 {
6877 name: "prefix matches correctly",
6878 apexAvailable: "com.foo.*",
6879 },
6880 {
6881 name: "prefix doesn't match",
6882 apexAvailable: "com.bar.*",
6883 expectedError: `Consider .* "com.foo\.\*"`,
6884 },
6885 {
6886 name: "short prefix",
6887 apexAvailable: "com.*",
6888 expectedError: "requires two or more components",
6889 },
6890 {
6891 name: "wildcard not in the end",
6892 apexAvailable: "com.*.foo",
6893 expectedError: "should end with .*",
6894 },
6895 {
6896 name: "wildcard in the middle",
6897 apexAvailable: "com.foo*.*",
6898 expectedError: "not allowed in the middle",
6899 },
6900 {
6901 name: "hint with prefix pattern",
6902 apexAvailable: "//apex_available:platform",
6903 expectedError: "Consider adding \"com.foo.bar\" or \"com.foo.*\"",
6904 },
6905 } {
6906 t.Run(tc.name, func(t *testing.T) {
6907 errorHandler := android.FixtureExpectsNoErrors
6908 if tc.expectedError != "" {
6909 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(tc.expectedError)
6910 }
6911 context := android.GroupFixturePreparers(
6912 prepareForApexTest,
6913 android.FixtureMergeMockFs(android.MockFS{
6914 "system/sepolicy/apex/com.foo.bar-file_contexts": nil,
6915 }),
6916 ).ExtendWithErrorHandler(errorHandler)
6917
6918 context.RunTestWithBp(t, `
6919 apex {
6920 name: "com.foo.bar",
6921 key: "myapex.key",
6922 native_shared_libs: ["libfoo"],
6923 updatable: false,
6924 }
6925
6926 apex_key {
6927 name: "myapex.key",
6928 public_key: "testkey.avbpubkey",
6929 private_key: "testkey.pem",
6930 }
6931
6932 cc_library {
6933 name: "libfoo",
6934 stl: "none",
6935 system_shared_libs: [],
6936 apex_available: ["`+tc.apexAvailable+`"],
6937 }`)
6938 })
6939 }
6940 testApexError(t, `Consider adding "com.foo" to`, `
6941 apex {
6942 name: "com.foo", // too short for a partner apex
6943 key: "myapex.key",
6944 native_shared_libs: ["libfoo"],
6945 updatable: false,
6946 }
6947
6948 apex_key {
6949 name: "myapex.key",
6950 public_key: "testkey.avbpubkey",
6951 private_key: "testkey.pem",
6952 }
6953
6954 cc_library {
6955 name: "libfoo",
6956 stl: "none",
6957 system_shared_libs: [],
6958 }
6959 `)
6960}
6961
Jiyong Park5d790c32019-11-15 18:40:32 +09006962func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006963 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006964 apex {
6965 name: "myapex",
6966 key: "myapex.key",
6967 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006968 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006969 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006970 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006971 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006972 }
6973
6974 override_apex {
6975 name: "override_myapex",
6976 base: "myapex",
6977 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006978 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006979 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006980 overrides: ["unknownapex"],
Jesse Melhuishec60e252024-03-29 19:08:20 +00006981 compile_multilib: "first",
6982 multilib: {
6983 lib32: {
6984 native_shared_libs: ["mylib32"],
6985 },
6986 lib64: {
6987 native_shared_libs: ["mylib64"],
6988 },
6989 },
Baligh Uddin004d7172020-02-19 21:29:28 -08006990 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006991 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006992 key: "mynewapex.key",
6993 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006994 }
6995
6996 apex_key {
6997 name: "myapex.key",
6998 public_key: "testkey.avbpubkey",
6999 private_key: "testkey.pem",
7000 }
7001
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07007002 apex_key {
7003 name: "mynewapex.key",
7004 public_key: "testkey2.avbpubkey",
7005 private_key: "testkey2.pem",
7006 }
7007
7008 android_app_certificate {
7009 name: "myapex.certificate",
7010 certificate: "testkey",
7011 }
7012
Jiyong Park5d790c32019-11-15 18:40:32 +09007013 android_app {
7014 name: "app",
7015 srcs: ["foo/bar/MyClass.java"],
7016 package_name: "foo",
7017 sdk_version: "none",
7018 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007019 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09007020 }
7021
7022 override_android_app {
7023 name: "override_app",
7024 base: "app",
7025 package_name: "bar",
7026 }
markchien7c803b82021-08-26 22:10:06 +08007027
7028 bpf {
7029 name: "bpf",
7030 srcs: ["bpf.c"],
7031 }
7032
7033 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08007034 name: "overrideBpf",
7035 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08007036 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07007037
7038 prebuilt_etc {
7039 name: "myetc",
7040 src: "myprebuilt",
7041 }
7042
7043 prebuilt_etc {
7044 name: "override_myetc",
7045 src: "override_myprebuilt",
7046 }
Jesse Melhuishec60e252024-03-29 19:08:20 +00007047
7048 cc_library {
7049 name: "mylib32",
7050 apex_available: [ "myapex" ],
7051 }
7052
7053 cc_library {
7054 name: "mylib64",
7055 apex_available: [ "myapex" ],
7056 }
Jiyong Park20bacab2020-03-03 11:45:41 +09007057 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09007058
Jooyung Hana0503a52023-08-23 13:12:50 +09007059 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(android.OverridableModule)
Spandan Das50801e22024-05-13 18:29:45 +00007060 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex").Module().(android.OverridableModule)
Jiyong Park317645e2019-12-05 13:20:58 +09007061 if originalVariant.GetOverriddenBy() != "" {
7062 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
7063 }
7064 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
7065 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
7066 }
7067
Spandan Das50801e22024-05-13 18:29:45 +00007068 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex")
Jiyong Park5d790c32019-11-15 18:40:32 +09007069 apexRule := module.Rule("apexRule")
7070 copyCmds := apexRule.Args["copy_commands"]
7071
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007072 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
7073 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007074
markchien7c803b82021-08-26 22:10:06 +08007075 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08007076 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08007077
Daniel Norman5a3ce132021-08-26 15:44:43 -07007078 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
7079 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
7080
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007081 apexBundle := module.Module().(*apexBundle)
7082 name := apexBundle.Name()
7083 if name != "override_myapex" {
7084 t.Errorf("name should be \"override_myapex\", but was %q", name)
7085 }
7086
Baligh Uddin004d7172020-02-19 21:29:28 -08007087 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
7088 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
7089 }
7090
Jiyong Park20bacab2020-03-03 11:45:41 +09007091 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07007092 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07007093 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
7094
7095 signApkRule := module.Rule("signapk")
7096 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09007097
Colin Crossaa255532020-07-03 13:18:24 -07007098 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007099 var builder strings.Builder
7100 data.Custom(&builder, name, "TARGET_", "", data)
7101 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007102 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
7103 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007104 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08007105 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007106 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08007107 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09007108 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007109 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09007110}
7111
Albert Martineefabcf2022-03-21 20:11:16 +00007112func TestMinSdkVersionOverride(t *testing.T) {
7113 // Override from 29 to 31
7114 minSdkOverride31 := "31"
7115 ctx := testApex(t, `
7116 apex {
7117 name: "myapex",
7118 key: "myapex.key",
7119 native_shared_libs: ["mylib"],
7120 updatable: true,
7121 min_sdk_version: "29"
7122 }
7123
7124 override_apex {
7125 name: "override_myapex",
7126 base: "myapex",
7127 logging_parent: "com.foo.bar",
7128 package_name: "test.overridden.package"
7129 }
7130
7131 apex_key {
7132 name: "myapex.key",
7133 public_key: "testkey.avbpubkey",
7134 private_key: "testkey.pem",
7135 }
7136
7137 cc_library {
7138 name: "mylib",
7139 srcs: ["mylib.cpp"],
7140 runtime_libs: ["libbar"],
7141 system_shared_libs: [],
7142 stl: "none",
7143 apex_available: [ "myapex" ],
7144 min_sdk_version: "apex_inherit"
7145 }
7146
7147 cc_library {
7148 name: "libbar",
7149 srcs: ["mylib.cpp"],
7150 system_shared_libs: [],
7151 stl: "none",
7152 apex_available: [ "myapex" ],
7153 min_sdk_version: "apex_inherit"
7154 }
7155
7156 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
7157
Jooyung Hana0503a52023-08-23 13:12:50 +09007158 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007159 copyCmds := apexRule.Args["copy_commands"]
7160
7161 // Ensure that direct non-stubs dep is always included
7162 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7163
7164 // Ensure that runtime_libs dep in included
7165 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7166
7167 // Ensure libraries target overridden min_sdk_version value
7168 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7169}
7170
7171func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
7172 // Attempt to override from 31 to 29, should be a NOOP
7173 minSdkOverride29 := "29"
7174 ctx := testApex(t, `
7175 apex {
7176 name: "myapex",
7177 key: "myapex.key",
7178 native_shared_libs: ["mylib"],
7179 updatable: true,
7180 min_sdk_version: "31"
7181 }
7182
7183 override_apex {
7184 name: "override_myapex",
7185 base: "myapex",
7186 logging_parent: "com.foo.bar",
7187 package_name: "test.overridden.package"
7188 }
7189
7190 apex_key {
7191 name: "myapex.key",
7192 public_key: "testkey.avbpubkey",
7193 private_key: "testkey.pem",
7194 }
7195
7196 cc_library {
7197 name: "mylib",
7198 srcs: ["mylib.cpp"],
7199 runtime_libs: ["libbar"],
7200 system_shared_libs: [],
7201 stl: "none",
7202 apex_available: [ "myapex" ],
7203 min_sdk_version: "apex_inherit"
7204 }
7205
7206 cc_library {
7207 name: "libbar",
7208 srcs: ["mylib.cpp"],
7209 system_shared_libs: [],
7210 stl: "none",
7211 apex_available: [ "myapex" ],
7212 min_sdk_version: "apex_inherit"
7213 }
7214
7215 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
7216
Jooyung Hana0503a52023-08-23 13:12:50 +09007217 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007218 copyCmds := apexRule.Args["copy_commands"]
7219
7220 // Ensure that direct non-stubs dep is always included
7221 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7222
7223 // Ensure that runtime_libs dep in included
7224 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7225
7226 // Ensure libraries target the original min_sdk_version value rather than the overridden
7227 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7228}
7229
Jooyung Han214bf372019-11-12 13:03:50 +09007230func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007231 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09007232 apex {
7233 name: "myapex",
7234 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007235 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09007236 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09007237 }
7238
7239 apex_key {
7240 name: "myapex.key",
7241 public_key: "testkey.avbpubkey",
7242 private_key: "testkey.pem",
7243 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007244
7245 cc_library {
7246 name: "mylib",
7247 srcs: ["mylib.cpp"],
7248 stl: "libc++",
7249 system_shared_libs: [],
7250 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09007251 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007252 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007253 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09007254
Jooyung Hana0503a52023-08-23 13:12:50 +09007255 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +09007256 args := module.Rule("apexRule").Args
7257 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007258
7259 // The copies of the libraries in the apex should have one more dependency than
7260 // the ones outside the apex, namely the unwinder. Ideally we should check
7261 // the dependency names directly here but for some reason the names are blank in
7262 // this test.
7263 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07007264 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007265 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
7266 if len(apexImplicits) != len(nonApexImplicits)+1 {
7267 t.Errorf("%q missing unwinder dep", lib)
7268 }
7269 }
Jooyung Han214bf372019-11-12 13:03:50 +09007270}
7271
Paul Duffine05480a2021-03-08 15:07:14 +00007272var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01007273 "api/current.txt": nil,
7274 "api/removed.txt": nil,
7275 "api/system-current.txt": nil,
7276 "api/system-removed.txt": nil,
7277 "api/test-current.txt": nil,
7278 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01007279
Anton Hanssondff2c782020-12-21 17:10:01 +00007280 "100/public/api/foo.txt": nil,
7281 "100/public/api/foo-removed.txt": nil,
7282 "100/system/api/foo.txt": nil,
7283 "100/system/api/foo-removed.txt": nil,
7284
Paul Duffineedc5d52020-06-12 17:46:39 +01007285 // For java_sdk_library_import
7286 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01007287}
7288
Jooyung Han58f26ab2019-12-18 15:34:32 +09007289func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007290 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09007291 apex {
7292 name: "myapex",
7293 key: "myapex.key",
7294 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007295 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09007296 }
7297
7298 apex_key {
7299 name: "myapex.key",
7300 public_key: "testkey.avbpubkey",
7301 private_key: "testkey.pem",
7302 }
7303
7304 java_sdk_library {
7305 name: "foo",
7306 srcs: ["a.java"],
7307 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007308 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09007309 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007310
7311 prebuilt_apis {
7312 name: "sdk",
7313 api_dirs: ["100"],
7314 }
Paul Duffin9b879592020-05-26 13:21:35 +01007315 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09007316
7317 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007318 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09007319 "javalib/foo.jar",
7320 "etc/permissions/foo.xml",
7321 })
7322 // Permission XML should point to the activated path of impl jar of java_sdk_library
Paul Duffin1816cde2024-04-10 10:58:21 +01007323 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Output("foo.xml")
7324 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7325 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
Jooyung Han58f26ab2019-12-18 15:34:32 +09007326}
7327
Spandan Das3ee19692024-06-19 04:47:40 +00007328func TestJavaSDKLibraryOverrideApexes(t *testing.T) {
7329 ctx := testApex(t, `
7330 override_apex {
7331 name: "mycompanyapex",
7332 base: "myapex",
7333 }
7334 apex {
7335 name: "myapex",
7336 key: "myapex.key",
7337 java_libs: ["foo"],
7338 updatable: false,
7339 }
7340
7341 apex_key {
7342 name: "myapex.key",
7343 public_key: "testkey.avbpubkey",
7344 private_key: "testkey.pem",
7345 }
7346
7347 java_sdk_library {
7348 name: "foo",
7349 srcs: ["a.java"],
7350 api_packages: ["foo"],
7351 apex_available: [ "myapex" ],
7352 }
7353
7354 prebuilt_apis {
7355 name: "sdk",
7356 api_dirs: ["100"],
7357 }
7358 `, withFiles(filesForSdkLibrary))
7359
7360 // Permission XML should point to the activated path of impl jar of java_sdk_library.
7361 // Since override variants (com.mycompany.android.foo) are installed in the same package as the overridden variant
7362 // (com.android.foo), the filepath should not contain override apex name.
7363 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_mycompanyapex").Output("foo.xml")
7364 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7365 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
7366}
7367
Paul Duffin9b879592020-05-26 13:21:35 +01007368func TestJavaSDKLibrary_WithinApex(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", "bar"],
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"],
Jihoon Kang28c96572024-09-11 23:44:44 +00007395 libs: ["foo.impl"],
Paul Duffin9b879592020-05-26 13:21:35 +01007396 apex_available: ["myapex"],
7397 sdk_version: "none",
7398 system_modules: "none",
7399 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007400
7401 prebuilt_apis {
7402 name: "sdk",
7403 api_dirs: ["100"],
7404 }
Paul Duffin9b879592020-05-26 13:21:35 +01007405 `, withFiles(filesForSdkLibrary))
7406
7407 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007408 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007409 "javalib/bar.jar",
7410 "javalib/foo.jar",
7411 "etc/permissions/foo.xml",
7412 })
7413
7414 // The bar library should depend on the implementation jar.
7415 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kanga3a05462024-04-05 00:36:44 +00007416 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007417 t.Errorf("expected %q, found %#q", expected, actual)
7418 }
7419}
7420
7421func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007422 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007423 apex {
7424 name: "myapex",
7425 key: "myapex.key",
7426 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007427 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007428 }
7429
7430 apex_key {
7431 name: "myapex.key",
7432 public_key: "testkey.avbpubkey",
7433 private_key: "testkey.pem",
7434 }
7435
7436 java_sdk_library {
7437 name: "foo",
7438 srcs: ["a.java"],
7439 api_packages: ["foo"],
7440 apex_available: ["myapex"],
7441 sdk_version: "none",
7442 system_modules: "none",
7443 }
7444
7445 java_library {
7446 name: "bar",
7447 srcs: ["a.java"],
Jihoon Kang28c96572024-09-11 23:44:44 +00007448 libs: ["foo.stubs"],
Paul Duffin9b879592020-05-26 13:21:35 +01007449 sdk_version: "none",
7450 system_modules: "none",
7451 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007452
7453 prebuilt_apis {
7454 name: "sdk",
7455 api_dirs: ["100"],
7456 }
Paul Duffin9b879592020-05-26 13:21:35 +01007457 `, withFiles(filesForSdkLibrary))
7458
7459 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007460 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007461 "javalib/foo.jar",
7462 "etc/permissions/foo.xml",
7463 })
7464
7465 // The bar library should depend on the stubs jar.
7466 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007467 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007468 t.Errorf("expected %q, found %#q", expected, actual)
7469 }
7470}
7471
Paul Duffineedc5d52020-06-12 17:46:39 +01007472func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007473 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00007474 prebuilt_apis {
7475 name: "sdk",
7476 api_dirs: ["100"],
7477 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01007478 withFiles(map[string][]byte{
7479 "apex/a.java": nil,
7480 "apex/apex_manifest.json": nil,
7481 "apex/Android.bp": []byte(`
7482 package {
7483 default_visibility: ["//visibility:private"],
7484 }
7485
7486 apex {
7487 name: "myapex",
7488 key: "myapex.key",
7489 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007490 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007491 }
7492
7493 apex_key {
7494 name: "myapex.key",
7495 public_key: "testkey.avbpubkey",
7496 private_key: "testkey.pem",
7497 }
7498
7499 java_library {
7500 name: "bar",
7501 srcs: ["a.java"],
Jihoon Kang28c96572024-09-11 23:44:44 +00007502 libs: ["foo.impl"],
Paul Duffineedc5d52020-06-12 17:46:39 +01007503 apex_available: ["myapex"],
7504 sdk_version: "none",
7505 system_modules: "none",
7506 }
7507`),
7508 "source/a.java": nil,
7509 "source/api/current.txt": nil,
7510 "source/api/removed.txt": nil,
7511 "source/Android.bp": []byte(`
7512 package {
7513 default_visibility: ["//visibility:private"],
7514 }
7515
7516 java_sdk_library {
7517 name: "foo",
7518 visibility: ["//apex"],
7519 srcs: ["a.java"],
7520 api_packages: ["foo"],
7521 apex_available: ["myapex"],
7522 sdk_version: "none",
7523 system_modules: "none",
7524 public: {
7525 enabled: true,
7526 },
7527 }
7528`),
7529 "prebuilt/a.jar": nil,
7530 "prebuilt/Android.bp": []byte(`
7531 package {
7532 default_visibility: ["//visibility:private"],
7533 }
7534
7535 java_sdk_library_import {
7536 name: "foo",
7537 visibility: ["//apex", "//source"],
7538 apex_available: ["myapex"],
7539 prefer: true,
7540 public: {
7541 jars: ["a.jar"],
7542 },
7543 }
7544`),
Anton Hanssondff2c782020-12-21 17:10:01 +00007545 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01007546 )
7547
7548 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007549 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffineedc5d52020-06-12 17:46:39 +01007550 "javalib/bar.jar",
7551 "javalib/foo.jar",
7552 "etc/permissions/foo.xml",
7553 })
7554
7555 // The bar library should depend on the implementation jar.
7556 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kanga3a05462024-04-05 00:36:44 +00007557 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01007558 t.Errorf("expected %q, found %#q", expected, actual)
7559 }
7560}
7561
7562func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
7563 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
7564 apex {
7565 name: "myapex",
7566 key: "myapex.key",
7567 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007568 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007569 }
7570
7571 apex_key {
7572 name: "myapex.key",
7573 public_key: "testkey.avbpubkey",
7574 private_key: "testkey.pem",
7575 }
7576
7577 java_sdk_library_import {
7578 name: "foo",
7579 apex_available: ["myapex"],
7580 prefer: true,
7581 public: {
7582 jars: ["a.jar"],
7583 },
7584 }
7585
7586 `, withFiles(filesForSdkLibrary))
7587}
7588
atrost6e126252020-01-27 17:01:16 +00007589func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007590 result := android.GroupFixturePreparers(
7591 prepareForApexTest,
7592 java.PrepareForTestWithPlatformCompatConfig,
7593 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007594 apex {
7595 name: "myapex",
7596 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007597 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007598 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007599 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007600 }
7601
7602 apex_key {
7603 name: "myapex.key",
7604 public_key: "testkey.avbpubkey",
7605 private_key: "testkey.pem",
7606 }
7607
7608 platform_compat_config {
7609 name: "myjar-platform-compat-config",
7610 src: ":myjar",
7611 }
7612
7613 java_library {
7614 name: "myjar",
7615 srcs: ["foo/bar/MyClass.java"],
7616 sdk_version: "none",
7617 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007618 apex_available: [ "myapex" ],
7619 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007620
7621 // Make sure that a preferred prebuilt does not affect the apex contents.
7622 prebuilt_platform_compat_config {
7623 name: "myjar-platform-compat-config",
7624 metadata: "compat-config/metadata.xml",
7625 prefer: true,
7626 }
atrost6e126252020-01-27 17:01:16 +00007627 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007628 ctx := result.TestContext
Jooyung Hana0503a52023-08-23 13:12:50 +09007629 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
atrost6e126252020-01-27 17:01:16 +00007630 "etc/compatconfig/myjar-platform-compat-config.xml",
7631 "javalib/myjar.jar",
7632 })
7633}
7634
Jooyung Han862c0d62022-12-21 10:15:37 +09007635func TestNoDupeApexFiles(t *testing.T) {
7636 android.GroupFixturePreparers(
7637 android.PrepareForTestWithAndroidBuildComponents,
7638 PrepareForTestWithApexBuildComponents,
7639 prepareForTestWithMyapex,
7640 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7641 ).
7642 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7643 RunTestWithBp(t, `
7644 apex {
7645 name: "myapex",
7646 key: "myapex.key",
7647 prebuilts: ["foo", "bar"],
7648 updatable: false,
7649 }
7650
7651 apex_key {
7652 name: "myapex.key",
7653 public_key: "testkey.avbpubkey",
7654 private_key: "testkey.pem",
7655 }
7656
7657 prebuilt_etc {
7658 name: "foo",
7659 src: "myprebuilt",
7660 filename_from_src: true,
7661 }
7662
7663 prebuilt_etc {
7664 name: "bar",
7665 src: "myprebuilt",
7666 filename_from_src: true,
7667 }
7668 `)
7669}
7670
Jooyung Hana8bd72a2023-11-02 11:56:48 +09007671func TestApexUnwantedTransitiveDeps(t *testing.T) {
7672 bp := `
7673 apex {
7674 name: "myapex",
7675 key: "myapex.key",
7676 native_shared_libs: ["libfoo"],
7677 updatable: false,
7678 unwanted_transitive_deps: ["libbar"],
7679 }
7680
7681 apex_key {
7682 name: "myapex.key",
7683 public_key: "testkey.avbpubkey",
7684 private_key: "testkey.pem",
7685 }
7686
7687 cc_library {
7688 name: "libfoo",
7689 srcs: ["foo.cpp"],
7690 shared_libs: ["libbar"],
7691 apex_available: ["myapex"],
7692 }
7693
7694 cc_library {
7695 name: "libbar",
7696 srcs: ["bar.cpp"],
7697 apex_available: ["myapex"],
7698 }`
7699 ctx := testApex(t, bp)
7700 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
7701 "*/libc++.so",
7702 "*/libfoo.so",
7703 // not libbar.so
7704 })
7705}
7706
Jiyong Park479321d2019-12-16 11:47:12 +09007707func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7708 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7709 apex {
7710 name: "myapex",
7711 key: "myapex.key",
7712 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007713 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007714 }
7715
7716 apex_key {
7717 name: "myapex.key",
7718 public_key: "testkey.avbpubkey",
7719 private_key: "testkey.pem",
7720 }
7721
7722 java_library {
7723 name: "myjar",
7724 srcs: ["foo/bar/MyClass.java"],
7725 sdk_version: "none",
7726 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007727 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007728 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007729 }
7730 `)
7731}
7732
Jiyong Park7afd1072019-12-30 16:56:33 +09007733func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007734 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007735 apex {
7736 name: "myapex",
7737 key: "myapex.key",
7738 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007739 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007740 }
7741
7742 apex_key {
7743 name: "myapex.key",
7744 public_key: "testkey.avbpubkey",
7745 private_key: "testkey.pem",
7746 }
7747
7748 cc_library {
7749 name: "mylib",
7750 srcs: ["mylib.cpp"],
7751 system_shared_libs: [],
7752 stl: "none",
7753 required: ["a", "b"],
7754 host_required: ["c", "d"],
7755 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007756 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007757 }
7758 `)
7759
Jooyung Hana0503a52023-08-23 13:12:50 +09007760 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007761 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007762 name := apexBundle.BaseModuleName()
7763 prefix := "TARGET_"
7764 var builder strings.Builder
7765 data.Custom(&builder, name, prefix, "", data)
7766 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007767 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 a b\n")
Sasha Smundakdcb61292022-12-08 10:41:33 -08007768 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7769 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007770}
7771
Jiyong Park7cd10e32020-01-14 09:22:18 +09007772func TestSymlinksFromApexToSystem(t *testing.T) {
7773 bp := `
7774 apex {
7775 name: "myapex",
7776 key: "myapex.key",
7777 native_shared_libs: ["mylib"],
7778 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007779 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007780 }
7781
Jiyong Park9d677202020-02-19 16:29:35 +09007782 apex {
7783 name: "myapex.updatable",
7784 key: "myapex.key",
7785 native_shared_libs: ["mylib"],
7786 java_libs: ["myjar"],
7787 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007788 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007789 }
7790
Jiyong Park7cd10e32020-01-14 09:22:18 +09007791 apex_key {
7792 name: "myapex.key",
7793 public_key: "testkey.avbpubkey",
7794 private_key: "testkey.pem",
7795 }
7796
7797 cc_library {
7798 name: "mylib",
7799 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007800 shared_libs: [
7801 "myotherlib",
7802 "myotherlib_ext",
7803 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007804 system_shared_libs: [],
7805 stl: "none",
7806 apex_available: [
7807 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007808 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007809 "//apex_available:platform",
7810 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007811 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007812 }
7813
7814 cc_library {
7815 name: "myotherlib",
7816 srcs: ["mylib.cpp"],
7817 system_shared_libs: [],
7818 stl: "none",
7819 apex_available: [
7820 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007821 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007822 "//apex_available:platform",
7823 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007824 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007825 }
7826
Jiyong Parkce243632023-02-17 18:22:25 +09007827 cc_library {
7828 name: "myotherlib_ext",
7829 srcs: ["mylib.cpp"],
7830 system_shared_libs: [],
7831 system_ext_specific: true,
7832 stl: "none",
7833 apex_available: [
7834 "myapex",
7835 "myapex.updatable",
7836 "//apex_available:platform",
7837 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007838 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007839 }
7840
Jiyong Park7cd10e32020-01-14 09:22:18 +09007841 java_library {
7842 name: "myjar",
7843 srcs: ["foo/bar/MyClass.java"],
7844 sdk_version: "none",
7845 system_modules: "none",
Jihoon Kang85bc1932024-07-01 17:04:46 +00007846 static_libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007847 apex_available: [
7848 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007849 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007850 "//apex_available:platform",
7851 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007852 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007853 }
7854
7855 java_library {
7856 name: "myotherjar",
7857 srcs: ["foo/bar/MyClass.java"],
7858 sdk_version: "none",
7859 system_modules: "none",
7860 apex_available: [
7861 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007862 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007863 "//apex_available:platform",
7864 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007865 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007866 }
7867 `
7868
7869 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7870 for _, f := range files {
7871 if f.path == file {
7872 if f.isLink {
7873 t.Errorf("%q is not a real file", file)
7874 }
7875 return
7876 }
7877 }
7878 t.Errorf("%q is not found", file)
7879 }
7880
Jiyong Parkce243632023-02-17 18:22:25 +09007881 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007882 for _, f := range files {
7883 if f.path == file {
7884 if !f.isLink {
7885 t.Errorf("%q is not a symlink", file)
7886 }
Jiyong Parkce243632023-02-17 18:22:25 +09007887 if f.src != target {
7888 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7889 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007890 return
7891 }
7892 }
7893 t.Errorf("%q is not found", file)
7894 }
7895
Jiyong Park9d677202020-02-19 16:29:35 +09007896 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7897 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007898 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana0503a52023-08-23 13:12:50 +09007899 files := getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007900 ensureRealfileExists(t, files, "javalib/myjar.jar")
7901 ensureRealfileExists(t, files, "lib64/mylib.so")
7902 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007903 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007904
Jooyung Hana0503a52023-08-23 13:12:50 +09007905 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007906 ensureRealfileExists(t, files, "javalib/myjar.jar")
7907 ensureRealfileExists(t, files, "lib64/mylib.so")
7908 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007909 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007910
7911 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007912 ctx = testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +09007913 files = getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007914 ensureRealfileExists(t, files, "javalib/myjar.jar")
7915 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007916 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7917 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007918
Jooyung Hana0503a52023-08-23 13:12:50 +09007919 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007920 ensureRealfileExists(t, files, "javalib/myjar.jar")
7921 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007922 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7923 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007924}
7925
Yo Chiange8128052020-07-23 20:09:18 +08007926func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007927 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007928 apex {
7929 name: "myapex",
7930 key: "myapex.key",
7931 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007932 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007933 }
7934
7935 apex_key {
7936 name: "myapex.key",
7937 public_key: "testkey.avbpubkey",
7938 private_key: "testkey.pem",
7939 }
7940
7941 cc_library_shared {
7942 name: "mylib",
7943 srcs: ["mylib.cpp"],
7944 shared_libs: ["myotherlib"],
7945 system_shared_libs: [],
7946 stl: "none",
7947 apex_available: [
7948 "myapex",
7949 "//apex_available:platform",
7950 ],
7951 }
7952
7953 cc_prebuilt_library_shared {
7954 name: "myotherlib",
7955 srcs: ["prebuilt.so"],
7956 system_shared_libs: [],
7957 stl: "none",
7958 apex_available: [
7959 "myapex",
7960 "//apex_available:platform",
7961 ],
7962 }
7963 `)
7964
Jooyung Hana0503a52023-08-23 13:12:50 +09007965 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007966 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007967 var builder strings.Builder
7968 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7969 androidMk := builder.String()
7970 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007971 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007972 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7973 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7974 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007975 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 myotherlib:64\n")
Yo Chiange8128052020-07-23 20:09:18 +08007976}
7977
Jooyung Han643adc42020-02-27 13:50:06 +09007978func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007979 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007980 apex {
7981 name: "myapex",
7982 key: "myapex.key",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007983 binaries: ["mybin"],
7984 jni_libs: ["mylib", "mylib3", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007985 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007986 }
7987
7988 apex_key {
7989 name: "myapex.key",
7990 public_key: "testkey.avbpubkey",
7991 private_key: "testkey.pem",
7992 }
7993
7994 cc_library {
7995 name: "mylib",
7996 srcs: ["mylib.cpp"],
7997 shared_libs: ["mylib2"],
7998 system_shared_libs: [],
7999 stl: "none",
8000 apex_available: [ "myapex" ],
8001 }
8002
8003 cc_library {
8004 name: "mylib2",
8005 srcs: ["mylib.cpp"],
8006 system_shared_libs: [],
8007 stl: "none",
8008 apex_available: [ "myapex" ],
8009 }
Jiyong Park34d5c332022-02-24 18:02:44 +09008010
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008011 // Used as both a JNI library and a regular shared library.
8012 cc_library {
8013 name: "mylib3",
8014 srcs: ["mylib.cpp"],
8015 system_shared_libs: [],
8016 stl: "none",
8017 apex_available: [ "myapex" ],
8018 }
8019
8020 cc_binary {
8021 name: "mybin",
8022 srcs: ["mybin.cpp"],
8023 shared_libs: ["mylib3"],
8024 system_shared_libs: [],
8025 stl: "none",
8026 apex_available: [ "myapex" ],
8027 }
8028
Jiyong Park34d5c332022-02-24 18:02:44 +09008029 rust_ffi_shared {
8030 name: "libfoo.rust",
8031 crate_name: "foo",
8032 srcs: ["foo.rs"],
8033 shared_libs: ["libfoo.shared_from_rust"],
8034 prefer_rlib: true,
8035 apex_available: ["myapex"],
8036 }
8037
8038 cc_library_shared {
8039 name: "libfoo.shared_from_rust",
8040 srcs: ["mylib.cpp"],
8041 system_shared_libs: [],
8042 stl: "none",
8043 stubs: {
8044 versions: ["10", "11", "12"],
8045 },
8046 }
8047
Jooyung Han643adc42020-02-27 13:50:06 +09008048 `)
8049
Jooyung Hana0503a52023-08-23 13:12:50 +09008050 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han643adc42020-02-27 13:50:06 +09008051 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008052 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so mylib3.so")
Jooyung Hana0503a52023-08-23 13:12:50 +09008053 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008054 "bin/mybin",
Jooyung Han643adc42020-02-27 13:50:06 +09008055 "lib64/mylib.so",
8056 "lib64/mylib2.so",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008057 "lib64/mylib3.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09008058 "lib64/libfoo.rust.so",
8059 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
8060 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09008061 })
Jiyong Park34d5c332022-02-24 18:02:44 +09008062
8063 // b/220397949
8064 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09008065}
8066
Jooyung Han49f67012020-04-17 13:43:10 +09008067func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008068 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09008069 apex {
8070 name: "myapex",
8071 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008072 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09008073 }
8074 apex_key {
8075 name: "myapex.key",
8076 public_key: "testkey.avbpubkey",
8077 private_key: "testkey.pem",
8078 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008079 `,
8080 android.FixtureModifyConfig(func(config android.Config) {
8081 delete(config.Targets, android.Android)
8082 config.AndroidCommonTarget = android.Target{}
8083 }),
8084 )
Jooyung Han49f67012020-04-17 13:43:10 +09008085
8086 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
8087 t.Errorf("Expected variants: %v, but got: %v", expected, got)
8088 }
8089}
8090
Jiyong Parkbd159612020-02-28 15:22:21 +09008091func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008092 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09008093 apex {
8094 name: "myapex",
8095 key: "myapex.key",
8096 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008097 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09008098 }
8099
8100 apex_key {
8101 name: "myapex.key",
8102 public_key: "testkey.avbpubkey",
8103 private_key: "testkey.pem",
8104 }
8105
8106 android_app {
8107 name: "AppFoo",
8108 srcs: ["foo/bar/MyClass.java"],
8109 sdk_version: "none",
8110 system_modules: "none",
8111 apex_available: [ "myapex" ],
8112 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09008113 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09008114
Jooyung Hana0503a52023-08-23 13:12:50 +09008115 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07008116 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Jiyong Parkbd159612020-02-28 15:22:21 +09008117
8118 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00008119 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 +09008120}
8121
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008122func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008123 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008124 apex {
8125 name: "myapex",
8126 key: "myapex.key",
8127 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008128 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008129 }
8130
8131 apex_key {
8132 name: "myapex.key",
8133 public_key: "testkey.avbpubkey",
8134 private_key: "testkey.pem",
8135 }
8136
8137 android_app_set {
8138 name: "AppSet",
8139 set: "AppSet.apks",
8140 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09008141 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crosscf371cc2020-11-13 11:48:42 -08008142 bundleConfigRule := mod.Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07008143 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008144 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
8145 s := mod.Rule("apexRule").Args["copy_commands"]
8146 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09008147 if len(copyCmds) != 4 {
8148 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008149 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00008150 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
8151 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09008152 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
8153 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09008154
8155 // Ensure that canned_fs_config has an entry for the app set zip file
8156 generateFsRule := mod.Rule("generateFsConfig")
8157 cmd := generateFsRule.RuleParams.Command
8158 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008159}
8160
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008161func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01008162 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008163 apex_set {
8164 name: "myapex",
8165 filename: "foo_v2.apex",
8166 sanitized: {
8167 none: { set: "myapex.apks", },
8168 hwaddress: { set: "myapex.hwasan.apks", },
8169 },
Paul Duffin24704672021-04-06 16:09:30 +01008170 }
8171 `
8172 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008173
Paul Duffin24704672021-04-06 16:09:30 +01008174 // Check that the extractor produces the correct output file from the correct input file.
Spandan Das9d6e2092024-09-21 02:50:00 +00008175 extractorOutput := "out/soong/.intermediates/myapex/android_common_myapex/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008176
Spandan Das9d6e2092024-09-21 02:50:00 +00008177 m := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin24704672021-04-06 16:09:30 +01008178 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008179
Paul Duffin24704672021-04-06 16:09:30 +01008180 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
8181
8182 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01008183 m = ctx.ModuleForTests("myapex", "android_common_myapex")
8184 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01008185
8186 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008187}
8188
Pranav Guptaeba03b02022-09-27 00:27:08 +00008189func TestApexSetApksModuleAssignment(t *testing.T) {
8190 ctx := testApex(t, `
8191 apex_set {
8192 name: "myapex",
8193 set: ":myapex_apks_file",
8194 }
8195
8196 filegroup {
8197 name: "myapex_apks_file",
8198 srcs: ["myapex.apks"],
8199 }
8200 `)
8201
Spandan Das9d6e2092024-09-21 02:50:00 +00008202 m := ctx.ModuleForTests("myapex", "android_common_myapex")
Pranav Guptaeba03b02022-09-27 00:27:08 +00008203
8204 // Check that the extractor produces the correct apks file from the input module
Spandan Das9d6e2092024-09-21 02:50:00 +00008205 extractorOutput := "out/soong/.intermediates/myapex/android_common_myapex/extracted/myapex.apks"
Pranav Guptaeba03b02022-09-27 00:27:08 +00008206 extractedApex := m.Output(extractorOutput)
8207
8208 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
8209}
8210
Paul Duffin89f570a2021-06-16 01:42:33 +01008211func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00008212 t.Helper()
8213
Paul Duffin55607122021-03-30 23:32:51 +01008214 fs := android.MockFS{
8215 "a.java": nil,
8216 "a.jar": nil,
8217 "apex_manifest.json": nil,
8218 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00008219 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00008220 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
8221 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
8222 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00008223 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008224 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008225
Paul Duffin55607122021-03-30 23:32:51 +01008226 errorHandler := android.FixtureExpectsNoErrors
8227 if errmsg != "" {
8228 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008229 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008230
Paul Duffin55607122021-03-30 23:32:51 +01008231 result := android.GroupFixturePreparers(
8232 cc.PrepareForTestWithCcDefaultModules,
8233 java.PrepareForTestWithHiddenApiBuildComponents,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008234 java.PrepareForTestWithDexpreopt,
Paul Duffin55607122021-03-30 23:32:51 +01008235 java.PrepareForTestWithJavaSdkLibraryFiles,
8236 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01008237 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01008238 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01008239 android.FixtureModifyMockFS(func(fs android.MockFS) {
8240 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
8241 insert := ""
8242 for _, fragment := range fragments {
8243 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
8244 }
8245 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
8246 platform_bootclasspath {
8247 name: "platform-bootclasspath",
8248 fragments: [
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008249 {apex: "com.android.art", module: "art-bootclasspath-fragment"},
Paul Duffin89f570a2021-06-16 01:42:33 +01008250 %s
8251 ],
8252 }
8253 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01008254 }
Paul Duffin89f570a2021-06-16 01:42:33 +01008255 }),
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008256 // Dexpreopt for boot jars requires the ART boot image profile.
8257 java.PrepareApexBootJarModule("com.android.art", "core-oj"),
8258 dexpreopt.FixtureSetArtBootJars("com.android.art:core-oj"),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00008259 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01008260 ).
8261 ExtendWithErrorHandler(errorHandler).
8262 RunTestWithBp(t, bp)
8263
8264 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008265}
8266
Jooyung Han548640b2020-04-27 12:10:30 +09008267func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
8268 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8269 apex {
8270 name: "myapex",
8271 key: "myapex.key",
8272 updatable: true,
8273 }
8274
8275 apex_key {
8276 name: "myapex.key",
8277 public_key: "testkey.avbpubkey",
8278 private_key: "testkey.pem",
8279 }
8280 `)
8281}
8282
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008283func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
8284 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8285 apex {
8286 name: "myapex",
8287 key: "myapex.key",
8288 }
8289
8290 apex_key {
8291 name: "myapex.key",
8292 public_key: "testkey.avbpubkey",
8293 private_key: "testkey.pem",
8294 }
8295 `)
8296}
8297
satayevb98371c2021-06-15 16:49:50 +01008298func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
8299 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
8300 apex {
8301 name: "myapex",
8302 key: "myapex.key",
8303 systemserverclasspath_fragments: [
8304 "mysystemserverclasspathfragment",
8305 ],
8306 min_sdk_version: "29",
8307 updatable: true,
8308 }
8309
8310 apex_key {
8311 name: "myapex.key",
8312 public_key: "testkey.avbpubkey",
8313 private_key: "testkey.pem",
8314 }
8315
8316 java_library {
8317 name: "foo",
8318 srcs: ["b.java"],
8319 min_sdk_version: "29",
8320 installable: true,
8321 apex_available: [
8322 "myapex",
8323 ],
Jihoon Kang85bc1932024-07-01 17:04:46 +00008324 sdk_version: "current",
satayevb98371c2021-06-15 16:49:50 +01008325 }
8326
8327 systemserverclasspath_fragment {
8328 name: "mysystemserverclasspathfragment",
8329 generate_classpaths_proto: false,
8330 contents: [
8331 "foo",
8332 ],
8333 apex_available: [
8334 "myapex",
8335 ],
8336 }
satayevabcd5972021-08-06 17:49:46 +01008337 `,
8338 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8339 )
satayevb98371c2021-06-15 16:49:50 +01008340}
8341
Paul Duffin064b70c2020-11-02 17:32:38 +00008342func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008343 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008344 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008345 fragment := java.ApexVariantReference{
8346 Apex: proptools.StringPtr("myapex"),
8347 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8348 }
8349
Paul Duffin064b70c2020-11-02 17:32:38 +00008350 testDexpreoptWithApexes(t, `
8351 prebuilt_apex {
8352 name: "myapex" ,
8353 arch: {
8354 arm64: {
8355 src: "myapex-arm64.apex",
8356 },
8357 arm: {
8358 src: "myapex-arm.apex",
8359 },
8360 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008361 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8362 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008363
Paul Duffin89f570a2021-06-16 01:42:33 +01008364 prebuilt_bootclasspath_fragment {
8365 name: "my-bootclasspath-fragment",
8366 contents: ["libfoo"],
8367 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008368 hidden_api: {
8369 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8370 metadata: "my-bootclasspath-fragment/metadata.csv",
8371 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008372 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8373 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8374 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008375 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008376 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008377
Spandan Das52c01a12024-09-20 01:09:48 +00008378 java_sdk_library_import {
8379 name: "libfoo",
8380 prefer: true,
8381 public: {
Paul Duffin89f570a2021-06-16 01:42:33 +01008382 jars: ["libfoo.jar"],
Spandan Das52c01a12024-09-20 01:09:48 +00008383 },
8384 apex_available: ["myapex"],
8385 shared_library: false,
8386 permitted_packages: ["libfoo"],
8387 }
Paul Duffin89f570a2021-06-16 01:42:33 +01008388 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008389 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008390}
8391
Spandan Dasf14e2542021-11-12 00:01:37 +00008392func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008393 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008394 bp += `
8395 apex_key {
8396 name: "myapex.key",
8397 public_key: "testkey.avbpubkey",
8398 private_key: "testkey.pem",
8399 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008400 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008401 "lib1/src/A.java": nil,
8402 "lib2/src/B.java": nil,
8403 "system/sepolicy/apex/myapex-file_contexts": nil,
8404 }
8405
Paul Duffin45338f02021-03-30 23:07:52 +01008406 errorHandler := android.FixtureExpectsNoErrors
8407 if errmsg != "" {
8408 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008409 }
Colin Crossae8600b2020-10-29 17:09:13 -07008410
Paul Duffin45338f02021-03-30 23:07:52 +01008411 android.GroupFixturePreparers(
8412 android.PrepareForTestWithAndroidBuildComponents,
8413 java.PrepareForTestWithJavaBuildComponents,
8414 PrepareForTestWithApexBuildComponents,
8415 android.PrepareForTestWithNeverallowRules(rules),
8416 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008417 apexBootJars := make([]string, 0, len(bootJars))
8418 for _, apexBootJar := range bootJars {
8419 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008420 }
satayevd604b212021-07-21 14:23:52 +01008421 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008422 }),
8423 fs.AddToFixture(),
8424 ).
8425 ExtendWithErrorHandler(errorHandler).
8426 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008427}
8428
8429func TestApexPermittedPackagesRules(t *testing.T) {
8430 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008431 name string
8432 expectedError string
8433 bp string
8434 bootJars []string
8435 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008436 }{
8437
8438 {
8439 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8440 expectedError: "",
8441 bp: `
8442 java_library {
8443 name: "bcp_lib1",
8444 srcs: ["lib1/src/*.java"],
8445 permitted_packages: ["foo.bar"],
8446 apex_available: ["myapex"],
8447 sdk_version: "none",
8448 system_modules: "none",
8449 }
8450 java_library {
8451 name: "nonbcp_lib2",
8452 srcs: ["lib2/src/*.java"],
8453 apex_available: ["myapex"],
8454 permitted_packages: ["a.b"],
8455 sdk_version: "none",
8456 system_modules: "none",
8457 }
8458 apex {
8459 name: "myapex",
8460 key: "myapex.key",
8461 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008462 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008463 }`,
8464 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008465 bcpPermittedPackages: map[string][]string{
8466 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008467 "foo.bar",
8468 },
8469 },
8470 },
8471 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008472 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008473 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 +01008474 bp: `
8475 java_library {
8476 name: "bcp_lib1",
8477 srcs: ["lib1/src/*.java"],
8478 apex_available: ["myapex"],
8479 permitted_packages: ["foo.bar"],
8480 sdk_version: "none",
8481 system_modules: "none",
8482 }
8483 java_library {
8484 name: "bcp_lib2",
8485 srcs: ["lib2/src/*.java"],
8486 apex_available: ["myapex"],
8487 permitted_packages: ["foo.bar", "bar.baz"],
8488 sdk_version: "none",
8489 system_modules: "none",
8490 }
8491 apex {
8492 name: "myapex",
8493 key: "myapex.key",
8494 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008495 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008496 }
8497 `,
8498 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008499 bcpPermittedPackages: map[string][]string{
8500 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008501 "foo.bar",
8502 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008503 "bcp_lib2": []string{
8504 "foo.bar",
8505 },
8506 },
8507 },
8508 {
8509 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8510 expectedError: "",
8511 bp: `
8512 java_library {
8513 name: "bcp_lib_restricted",
8514 srcs: ["lib1/src/*.java"],
8515 apex_available: ["myapex"],
8516 permitted_packages: ["foo.bar"],
8517 sdk_version: "none",
8518 min_sdk_version: "29",
8519 system_modules: "none",
8520 }
8521 java_library {
8522 name: "bcp_lib_unrestricted",
8523 srcs: ["lib2/src/*.java"],
8524 apex_available: ["myapex"],
8525 permitted_packages: ["foo.bar", "bar.baz"],
8526 sdk_version: "none",
8527 min_sdk_version: "29",
8528 system_modules: "none",
8529 }
8530 apex {
8531 name: "myapex",
8532 key: "myapex.key",
8533 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8534 updatable: true,
8535 min_sdk_version: "29",
8536 }
8537 `,
8538 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8539 bcpPermittedPackages: map[string][]string{
8540 "bcp_lib1_non_updateable": []string{
8541 "foo.bar",
8542 },
8543 // 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 +01008544 },
8545 },
8546 }
8547 for _, tc := range testcases {
8548 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008549 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8550 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008551 })
8552 }
8553}
8554
Jiyong Park62304bb2020-04-13 16:19:48 +09008555func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008556 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008557 apex {
8558 name: "myapex",
8559 key: "myapex.key",
8560 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008561 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008562 }
8563
8564 apex_key {
8565 name: "myapex.key",
8566 public_key: "testkey.avbpubkey",
8567 private_key: "testkey.pem",
8568 }
8569
8570 cc_library {
8571 name: "mylib",
8572 srcs: ["mylib.cpp"],
8573 system_shared_libs: [],
8574 stl: "none",
8575 stubs: {
8576 versions: ["1"],
8577 },
8578 apex_available: ["myapex"],
8579 }
8580
8581 cc_library {
8582 name: "myprivlib",
8583 srcs: ["mylib.cpp"],
8584 system_shared_libs: [],
8585 stl: "none",
8586 apex_available: ["myapex"],
8587 }
8588
8589
8590 cc_test {
8591 name: "mytest",
8592 gtest: false,
8593 srcs: ["mylib.cpp"],
8594 system_shared_libs: [],
8595 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008596 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008597 test_for: ["myapex"]
8598 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008599
8600 cc_library {
8601 name: "mytestlib",
8602 srcs: ["mylib.cpp"],
8603 system_shared_libs: [],
8604 shared_libs: ["mylib", "myprivlib"],
8605 stl: "none",
8606 test_for: ["myapex"],
8607 }
8608
8609 cc_benchmark {
8610 name: "mybench",
8611 srcs: ["mylib.cpp"],
8612 system_shared_libs: [],
8613 shared_libs: ["mylib", "myprivlib"],
8614 stl: "none",
8615 test_for: ["myapex"],
8616 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008617 `)
8618
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008619 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008620 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008621 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8622 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8623 }
8624
8625 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008626 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008627 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8628 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8629 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8630}
Jiyong Park46a512f2020-12-04 18:02:13 +09008631
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008632func TestIndirectTestFor(t *testing.T) {
8633 ctx := testApex(t, `
8634 apex {
8635 name: "myapex",
8636 key: "myapex.key",
8637 native_shared_libs: ["mylib", "myprivlib"],
8638 updatable: false,
8639 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008640
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008641 apex_key {
8642 name: "myapex.key",
8643 public_key: "testkey.avbpubkey",
8644 private_key: "testkey.pem",
8645 }
8646
8647 cc_library {
8648 name: "mylib",
8649 srcs: ["mylib.cpp"],
8650 system_shared_libs: [],
8651 stl: "none",
8652 stubs: {
8653 versions: ["1"],
8654 },
8655 apex_available: ["myapex"],
8656 }
8657
8658 cc_library {
8659 name: "myprivlib",
8660 srcs: ["mylib.cpp"],
8661 system_shared_libs: [],
8662 stl: "none",
8663 shared_libs: ["mylib"],
8664 apex_available: ["myapex"],
8665 }
8666
8667 cc_library {
8668 name: "mytestlib",
8669 srcs: ["mylib.cpp"],
8670 system_shared_libs: [],
8671 shared_libs: ["myprivlib"],
8672 stl: "none",
8673 test_for: ["myapex"],
8674 }
8675 `)
8676
8677 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008678 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008679 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8680 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8681 }
8682
8683 // The platform variant of mytestlib links to the platform variant of the
8684 // internal myprivlib.
8685 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8686
8687 // The platform variant of myprivlib links to the platform variant of mylib
8688 // and bypasses its stubs.
8689 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 +09008690}
8691
Martin Stjernholmec009002021-03-27 15:18:31 +00008692func TestTestForForLibInOtherApex(t *testing.T) {
8693 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8694 _ = testApex(t, `
8695 apex {
8696 name: "com.android.art",
8697 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008698 native_shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008699 updatable: false,
8700 }
8701
8702 apex {
8703 name: "com.android.art.debug",
8704 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008705 native_shared_libs: ["libnativebridge", "libnativebrdige_test"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008706 updatable: false,
8707 }
8708
8709 apex_key {
8710 name: "myapex.key",
8711 public_key: "testkey.avbpubkey",
8712 private_key: "testkey.pem",
8713 }
8714
8715 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008716 name: "libnativebridge",
8717 srcs: ["libnativebridge.cpp"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008718 system_shared_libs: [],
8719 stl: "none",
8720 stubs: {
8721 versions: ["1"],
8722 },
8723 apex_available: ["com.android.art", "com.android.art.debug"],
8724 }
8725
8726 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008727 name: "libnativebrdige_test",
Martin Stjernholmec009002021-03-27 15:18:31 +00008728 srcs: ["mylib.cpp"],
8729 system_shared_libs: [],
Spandan Das20fce2d2023-04-12 17:21:39 +00008730 shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008731 stl: "none",
8732 apex_available: ["com.android.art.debug"],
8733 test_for: ["com.android.art"],
8734 }
8735 `,
8736 android.MockFS{
8737 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8738 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8739 }.AddToFixture())
8740}
8741
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008742// TODO(jungjw): Move this to proptools
8743func intPtr(i int) *int {
8744 return &i
8745}
8746
8747func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008748 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008749 apex_set {
8750 name: "myapex",
8751 set: "myapex.apks",
8752 filename: "foo_v2.apex",
8753 overrides: ["foo"],
8754 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008755 `,
8756 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8757 variables.Platform_sdk_version = intPtr(30)
8758 }),
8759 android.FixtureModifyConfig(func(config android.Config) {
8760 config.Targets[android.Android] = []android.Target{
8761 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8762 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8763 }
8764 }),
8765 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008766
Spandan Das9d6e2092024-09-21 02:50:00 +00008767 m := ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008768
8769 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008770 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008771 actual := extractedApex.Args["abis"]
8772 expected := "ARMEABI_V7A,ARM64_V8A"
8773 if actual != expected {
8774 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8775 }
8776 actual = extractedApex.Args["sdk-version"]
8777 expected = "30"
8778 if actual != expected {
8779 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8780 }
8781
Paul Duffin6717d882021-06-15 19:09:41 +01008782 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008783 a := m.Module().(*ApexSet)
8784 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008785 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008786 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8787 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8788 }
8789}
8790
Anton Hansson805e0a52022-11-25 14:06:46 +00008791func TestApexSet_NativeBridge(t *testing.T) {
8792 ctx := testApex(t, `
8793 apex_set {
8794 name: "myapex",
8795 set: "myapex.apks",
8796 filename: "foo_v2.apex",
8797 overrides: ["foo"],
8798 }
8799 `,
8800 android.FixtureModifyConfig(func(config android.Config) {
8801 config.Targets[android.Android] = []android.Target{
8802 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8803 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8804 }
8805 }),
8806 )
8807
Spandan Das9d6e2092024-09-21 02:50:00 +00008808 m := ctx.ModuleForTests("myapex", "android_common_myapex")
Anton Hansson805e0a52022-11-25 14:06:46 +00008809
8810 // Check extract_apks tool parameters. No native bridge arch expected
8811 extractedApex := m.Output("extracted/myapex.apks")
8812 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8813}
8814
Jiyong Park7d95a512020-05-10 15:16:24 +09008815func TestNoStaticLinkingToStubsLib(t *testing.T) {
8816 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8817 apex {
8818 name: "myapex",
8819 key: "myapex.key",
8820 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008821 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008822 }
8823
8824 apex_key {
8825 name: "myapex.key",
8826 public_key: "testkey.avbpubkey",
8827 private_key: "testkey.pem",
8828 }
8829
8830 cc_library {
8831 name: "mylib",
8832 srcs: ["mylib.cpp"],
8833 static_libs: ["otherlib"],
8834 system_shared_libs: [],
8835 stl: "none",
8836 apex_available: [ "myapex" ],
8837 }
8838
8839 cc_library {
8840 name: "otherlib",
8841 srcs: ["mylib.cpp"],
8842 system_shared_libs: [],
8843 stl: "none",
8844 stubs: {
8845 versions: ["1", "2", "3"],
8846 },
8847 apex_available: [ "myapex" ],
8848 }
8849 `)
8850}
8851
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008852func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008853 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008854 apex {
8855 name: "myapex",
8856 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008857 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008858 custom_sign_tool: "sign_myapex",
8859 }
8860
8861 apex_key {
8862 name: "myapex.key",
8863 public_key: "testkey.avbpubkey",
8864 private_key: "testkey.pem",
8865 }
8866 `)
8867
Jooyung Han286957d2023-10-30 16:17:56 +09008868 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07008869 content := android.ContentFromFileRuleForTests(t, ctx, myapex.Output("apexkeys.txt"))
Jooyung Haneec1b3f2023-06-20 16:25:59 +09008870 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 +09008871}
8872
8873func TestApexKeysTxtOverrides(t *testing.T) {
8874 ctx := testApex(t, `
8875 apex {
8876 name: "myapex",
8877 key: "myapex.key",
8878 updatable: false,
8879 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008880 }
8881
8882 apex_key {
8883 name: "myapex.key",
8884 public_key: "testkey.avbpubkey",
8885 private_key: "testkey.pem",
8886 }
8887
8888 prebuilt_apex {
8889 name: "myapex",
8890 prefer: true,
8891 arch: {
8892 arm64: {
8893 src: "myapex-arm64.apex",
8894 },
8895 arm: {
8896 src: "myapex-arm.apex",
8897 },
8898 },
8899 }
8900
8901 apex_set {
8902 name: "myapex_set",
8903 set: "myapex.apks",
8904 filename: "myapex_set.apex",
8905 overrides: ["myapex"],
8906 }
8907 `)
8908
Colin Crossf61d03d2023-11-02 16:56:39 -07008909 content := android.ContentFromFileRuleForTests(t, ctx,
8910 ctx.ModuleForTests("myapex", "android_common_myapex").Output("apexkeys.txt"))
Jooyung Han286957d2023-10-30 16:17:56 +09008911 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 -07008912 content = android.ContentFromFileRuleForTests(t, ctx,
8913 ctx.ModuleForTests("myapex_set", "android_common_myapex_set").Output("apexkeys.txt"))
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008914 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 +09008915}
8916
Jooyung Han938b5932020-06-20 12:47:47 +09008917func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008918 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008919 apex {
8920 name: "myapex",
8921 key: "myapex.key",
8922 apps: ["app"],
8923 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008924 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008925 }
8926
8927 apex_key {
8928 name: "myapex.key",
8929 public_key: "testkey.avbpubkey",
8930 private_key: "testkey.pem",
8931 }
8932
8933 android_app {
8934 name: "app",
8935 srcs: ["foo/bar/MyClass.java"],
8936 package_name: "foo",
8937 sdk_version: "none",
8938 system_modules: "none",
8939 apex_available: [ "myapex" ],
8940 }
8941 `, withFiles(map[string][]byte{
8942 "sub/Android.bp": []byte(`
8943 override_apex {
8944 name: "override_myapex",
8945 base: "myapex",
8946 apps: ["override_app"],
8947 allowed_files: ":allowed",
8948 }
8949 // Overridable "path" property should be referenced indirectly
8950 filegroup {
8951 name: "allowed",
8952 srcs: ["allowed.txt"],
8953 }
8954 override_android_app {
8955 name: "override_app",
8956 base: "app",
8957 package_name: "bar",
8958 }
8959 `),
8960 }))
8961
Jooyung Hana0503a52023-08-23 13:12:50 +09008962 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008963 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8964 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8965 }
8966
Spandan Das50801e22024-05-13 18:29:45 +00008967 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008968 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8969 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8970 }
8971}
8972
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008973func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008974 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008975 apex {
8976 name: "myapex",
8977 key: "myapex.key",
8978 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008979 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008980 }
8981
8982 apex_key {
8983 name: "myapex.key",
8984 public_key: "testkey.avbpubkey",
8985 private_key: "testkey.pem",
8986 }
8987
8988 cc_library {
8989 name: "mylib",
8990 srcs: ["mylib.cpp"],
8991 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008992 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008993 },
8994 apex_available: ["myapex"],
8995 }
8996
8997 cc_prebuilt_library_shared {
8998 name: "mylib",
8999 prefer: false,
9000 srcs: ["prebuilt.so"],
9001 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07009002 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009003 },
9004 apex_available: ["myapex"],
9005 }
9006 `)
9007}
9008
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009009func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009010 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009011 apex {
9012 name: "myapex",
9013 key: "myapex.key",
9014 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009015 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009016 }
9017 apex_key {
9018 name: "myapex.key",
9019 public_key: "testkey.avbpubkey",
9020 private_key: "testkey.pem",
9021 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00009022 `,
9023 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9024 variables.CompressedApex = proptools.BoolPtr(true)
9025 }),
9026 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009027
Jooyung Hana0503a52023-08-23 13:12:50 +09009028 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("compressRule")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009029 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
9030
Jooyung Hana0503a52023-08-23 13:12:50 +09009031 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex").Description("sign compressedApex")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009032 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
9033
9034 // Make sure output of bundle is .capex
Jooyung Hana0503a52023-08-23 13:12:50 +09009035 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009036 ensureContains(t, ab.outputFile.String(), "myapex.capex")
9037
9038 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07009039 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009040 var builder strings.Builder
9041 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9042 androidMk := builder.String()
9043 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
9044}
9045
Jooyung Han26ec8482024-07-31 15:04:05 +09009046func TestApexSet_ShouldRespectCompressedApexFlag(t *testing.T) {
9047 for _, compressionEnabled := range []bool{true, false} {
9048 t.Run(fmt.Sprintf("compressionEnabled=%v", compressionEnabled), func(t *testing.T) {
9049 ctx := testApex(t, `
9050 apex_set {
9051 name: "com.company.android.myapex",
9052 apex_name: "com.android.myapex",
9053 set: "company-myapex.apks",
9054 }
9055 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9056 variables.CompressedApex = proptools.BoolPtr(compressionEnabled)
9057 }),
9058 )
9059
9060 build := ctx.ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex").Output("com.company.android.myapex.apex")
9061 if compressionEnabled {
9062 ensureEquals(t, build.Rule.String(), "android/soong/android.Cp")
9063 } else {
9064 ensureEquals(t, build.Rule.String(), "android/apex.decompressApex")
9065 }
9066 })
9067 }
9068}
9069
Martin Stjernholm2856c662020-12-02 15:03:42 +00009070func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009071 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00009072 apex {
9073 name: "myapex",
9074 key: "myapex.key",
9075 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009076 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00009077 }
9078
9079 apex_key {
9080 name: "myapex.key",
9081 public_key: "testkey.avbpubkey",
9082 private_key: "testkey.pem",
9083 }
9084
9085 cc_library {
9086 name: "mylib",
9087 srcs: ["mylib.cpp"],
9088 apex_available: ["myapex"],
9089 shared_libs: ["otherlib"],
9090 system_shared_libs: [],
9091 }
9092
9093 cc_library {
9094 name: "otherlib",
9095 srcs: ["mylib.cpp"],
9096 stubs: {
9097 versions: ["current"],
9098 },
9099 }
9100
9101 cc_prebuilt_library_shared {
9102 name: "otherlib",
9103 prefer: true,
9104 srcs: ["prebuilt.so"],
9105 stubs: {
9106 versions: ["current"],
9107 },
9108 }
9109 `)
9110
Jooyung Hana0503a52023-08-23 13:12:50 +09009111 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07009112 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00009113 var builder strings.Builder
9114 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9115 androidMk := builder.String()
9116
9117 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
9118 // a thing there.
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009119 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 otherlib\n")
Martin Stjernholm2856c662020-12-02 15:03:42 +00009120}
9121
Jiyong Parke3867542020-12-03 17:28:25 +09009122func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009123 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09009124 apex {
9125 name: "myapex",
9126 key: "myapex.key",
9127 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009128 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09009129 }
9130
9131 apex_key {
9132 name: "myapex.key",
9133 public_key: "testkey.avbpubkey",
9134 private_key: "testkey.pem",
9135 }
9136
9137 cc_library {
9138 name: "mylib",
9139 srcs: ["mylib.cpp"],
9140 system_shared_libs: [],
9141 stl: "none",
9142 apex_available: ["myapex"],
9143 shared_libs: ["mylib2"],
9144 target: {
9145 apex: {
9146 exclude_shared_libs: ["mylib2"],
9147 },
9148 },
9149 }
9150
9151 cc_library {
9152 name: "mylib2",
9153 srcs: ["mylib.cpp"],
9154 system_shared_libs: [],
9155 stl: "none",
9156 }
9157 `)
9158
9159 // Check if mylib is linked to mylib2 for the non-apex target
9160 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
9161 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
9162
9163 // Make sure that the link doesn't occur for the apex target
9164 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
9165 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
9166
9167 // It shouldn't appear in the copy cmd as well.
Jooyung Hana0503a52023-08-23 13:12:50 +09009168 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule").Args["copy_commands"]
Jiyong Parke3867542020-12-03 17:28:25 +09009169 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
9170}
9171
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009172func TestPrebuiltStubLibDep(t *testing.T) {
9173 bpBase := `
9174 apex {
9175 name: "myapex",
9176 key: "myapex.key",
9177 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009178 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009179 }
9180 apex_key {
9181 name: "myapex.key",
9182 public_key: "testkey.avbpubkey",
9183 private_key: "testkey.pem",
9184 }
9185 cc_library {
9186 name: "mylib",
9187 srcs: ["mylib.cpp"],
9188 apex_available: ["myapex"],
9189 shared_libs: ["stublib"],
9190 system_shared_libs: [],
9191 }
9192 apex {
9193 name: "otherapex",
9194 enabled: %s,
9195 key: "myapex.key",
9196 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009197 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009198 }
9199 `
9200
9201 stublibSourceBp := `
9202 cc_library {
9203 name: "stublib",
9204 srcs: ["mylib.cpp"],
9205 apex_available: ["otherapex"],
9206 system_shared_libs: [],
9207 stl: "none",
9208 stubs: {
9209 versions: ["1"],
9210 },
9211 }
9212 `
9213
9214 stublibPrebuiltBp := `
9215 cc_prebuilt_library_shared {
9216 name: "stublib",
9217 srcs: ["prebuilt.so"],
9218 apex_available: ["otherapex"],
9219 stubs: {
9220 versions: ["1"],
9221 },
9222 %s
9223 }
9224 `
9225
9226 tests := []struct {
9227 name string
9228 stublibBp string
9229 usePrebuilt bool
9230 modNames []string // Modules to collect AndroidMkEntries for
9231 otherApexEnabled []string
9232 }{
9233 {
9234 name: "only_source",
9235 stublibBp: stublibSourceBp,
9236 usePrebuilt: false,
9237 modNames: []string{"stublib"},
9238 otherApexEnabled: []string{"true", "false"},
9239 },
9240 {
9241 name: "source_preferred",
9242 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9243 usePrebuilt: false,
9244 modNames: []string{"stublib", "prebuilt_stublib"},
9245 otherApexEnabled: []string{"true", "false"},
9246 },
9247 {
9248 name: "prebuilt_preferred",
9249 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9250 usePrebuilt: true,
9251 modNames: []string{"stublib", "prebuilt_stublib"},
9252 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9253 },
9254 {
9255 name: "only_prebuilt",
9256 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9257 usePrebuilt: true,
9258 modNames: []string{"stublib"},
9259 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9260 },
9261 }
9262
9263 for _, test := range tests {
9264 t.Run(test.name, func(t *testing.T) {
9265 for _, otherApexEnabled := range test.otherApexEnabled {
9266 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009267 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009268
9269 type modAndMkEntries struct {
9270 mod *cc.Module
9271 mkEntries android.AndroidMkEntries
9272 }
9273 entries := []*modAndMkEntries{}
9274
9275 // Gather shared lib modules that are installable
9276 for _, modName := range test.modNames {
9277 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9278 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9279 continue
9280 }
9281 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Cole Fausta963b942024-04-11 17:43:00 -07009282 if !mod.Enabled(android.PanickingConfigAndErrorContext(ctx)) || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009283 continue
9284 }
Colin Crossaa255532020-07-03 13:18:24 -07009285 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009286 if ent.Disabled {
9287 continue
9288 }
9289 entries = append(entries, &modAndMkEntries{
9290 mod: mod,
9291 mkEntries: ent,
9292 })
9293 }
9294 }
9295 }
9296
9297 var entry *modAndMkEntries = nil
9298 for _, ent := range entries {
9299 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9300 if entry != nil {
9301 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9302 } else {
9303 entry = ent
9304 }
9305 }
9306 }
9307
9308 if entry == nil {
9309 t.Errorf("AndroidMk entry for \"stublib\" missing")
9310 } else {
9311 isPrebuilt := entry.mod.Prebuilt() != nil
9312 if isPrebuilt != test.usePrebuilt {
9313 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9314 }
9315 if !entry.mod.IsStubs() {
9316 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9317 }
9318 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9319 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9320 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009321 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009322 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009323 if !android.InList(expected, cflags) {
9324 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9325 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009326 }
9327 })
9328 }
9329 })
9330 }
9331}
9332
Colin Crossc33e5212021-05-25 18:16:02 -07009333func TestApexJavaCoverage(t *testing.T) {
9334 bp := `
9335 apex {
9336 name: "myapex",
9337 key: "myapex.key",
9338 java_libs: ["mylib"],
9339 bootclasspath_fragments: ["mybootclasspathfragment"],
9340 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9341 updatable: false,
9342 }
9343
9344 apex_key {
9345 name: "myapex.key",
9346 public_key: "testkey.avbpubkey",
9347 private_key: "testkey.pem",
9348 }
9349
9350 java_library {
9351 name: "mylib",
9352 srcs: ["mylib.java"],
9353 apex_available: ["myapex"],
9354 compile_dex: true,
9355 }
9356
9357 bootclasspath_fragment {
9358 name: "mybootclasspathfragment",
9359 contents: ["mybootclasspathlib"],
9360 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009361 hidden_api: {
9362 split_packages: ["*"],
9363 },
Colin Crossc33e5212021-05-25 18:16:02 -07009364 }
9365
9366 java_library {
9367 name: "mybootclasspathlib",
9368 srcs: ["mybootclasspathlib.java"],
9369 apex_available: ["myapex"],
9370 compile_dex: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00009371 sdk_version: "current",
Colin Crossc33e5212021-05-25 18:16:02 -07009372 }
9373
9374 systemserverclasspath_fragment {
9375 name: "mysystemserverclasspathfragment",
9376 contents: ["mysystemserverclasspathlib"],
9377 apex_available: ["myapex"],
9378 }
9379
9380 java_library {
9381 name: "mysystemserverclasspathlib",
9382 srcs: ["mysystemserverclasspathlib.java"],
9383 apex_available: ["myapex"],
9384 compile_dex: true,
9385 }
9386 `
9387
9388 result := android.GroupFixturePreparers(
9389 PrepareForTestWithApexBuildComponents,
9390 prepareForTestWithMyapex,
9391 java.PrepareForTestWithJavaDefaultModules,
9392 android.PrepareForTestWithAndroidBuildComponents,
9393 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009394 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9395 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009396 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009397 ).RunTest(t)
9398
9399 // Make sure jacoco ran on both mylib and mybootclasspathlib
9400 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9401 t.Errorf("Failed to find jacoco rule for mylib")
9402 }
9403 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9404 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9405 }
9406 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9407 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9408 }
9409}
9410
Jiyong Park192600a2021-08-03 07:52:17 +00009411func TestProhibitStaticExecutable(t *testing.T) {
9412 testApexError(t, `executable mybin is static`, `
9413 apex {
9414 name: "myapex",
9415 key: "myapex.key",
9416 binaries: ["mybin"],
9417 min_sdk_version: "29",
9418 }
9419
9420 apex_key {
9421 name: "myapex.key",
9422 public_key: "testkey.avbpubkey",
9423 private_key: "testkey.pem",
9424 }
9425
9426 cc_binary {
9427 name: "mybin",
9428 srcs: ["mylib.cpp"],
9429 relative_install_path: "foo/bar",
9430 static_executable: true,
9431 system_shared_libs: [],
9432 stl: "none",
9433 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009434 min_sdk_version: "29",
9435 }
9436 `)
9437
9438 testApexError(t, `executable mybin.rust is static`, `
9439 apex {
9440 name: "myapex",
9441 key: "myapex.key",
9442 binaries: ["mybin.rust"],
9443 min_sdk_version: "29",
9444 }
9445
9446 apex_key {
9447 name: "myapex.key",
9448 public_key: "testkey.avbpubkey",
9449 private_key: "testkey.pem",
9450 }
9451
9452 rust_binary {
9453 name: "mybin.rust",
9454 srcs: ["foo.rs"],
9455 static_executable: true,
9456 apex_available: ["myapex"],
9457 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009458 }
9459 `)
9460}
9461
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009462func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9463 ctx := testApex(t, `
9464 apex {
9465 name: "myapex",
9466 key: "myapex.key",
9467 updatable: false,
9468 java_libs: ["foo"],
9469 }
9470
9471 apex_key {
9472 name: "myapex.key",
9473 public_key: "testkey.avbpubkey",
9474 private_key: "testkey.pem",
9475 }
9476
9477 java_library {
9478 name: "foo",
9479 srcs: ["foo.java"],
9480 apex_available: ["myapex"],
9481 installable: true,
9482 }
9483 `,
9484 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9485 )
9486
Jooyung Hana0503a52023-08-23 13:12:50 +09009487 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009488 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9489 var builder strings.Builder
9490 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9491 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009492 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 +00009493}
9494
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009495func TestAndroidMk_RequiredModules(t *testing.T) {
9496 ctx := testApex(t, `
9497 apex {
9498 name: "myapex",
9499 key: "myapex.key",
9500 updatable: false,
9501 java_libs: ["foo"],
9502 required: ["otherapex"],
9503 }
9504
9505 apex {
9506 name: "otherapex",
9507 key: "myapex.key",
9508 updatable: false,
9509 java_libs: ["foo"],
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009510 }
9511
9512 apex_key {
9513 name: "myapex.key",
9514 public_key: "testkey.avbpubkey",
9515 private_key: "testkey.pem",
9516 }
9517
9518 java_library {
9519 name: "foo",
9520 srcs: ["foo.java"],
9521 apex_available: ["myapex", "otherapex"],
9522 installable: true,
9523 }
9524 `)
9525
Jooyung Hana0503a52023-08-23 13:12:50 +09009526 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009527 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9528 var builder strings.Builder
9529 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9530 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009531 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009532}
9533
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009534func TestAndroidMk_RequiredDeps(t *testing.T) {
9535 ctx := testApex(t, `
9536 apex {
9537 name: "myapex",
9538 key: "myapex.key",
9539 updatable: false,
9540 }
9541
9542 apex_key {
9543 name: "myapex.key",
9544 public_key: "testkey.avbpubkey",
9545 private_key: "testkey.pem",
9546 }
9547 `)
9548
Jooyung Hana0503a52023-08-23 13:12:50 +09009549 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009550 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009551 data := android.AndroidMkDataForTest(t, ctx, bundle)
9552 var builder strings.Builder
9553 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9554 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009555 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009556}
9557
Jooyung Hana6d36672022-02-24 13:58:07 +09009558func TestApexOutputFileProducer(t *testing.T) {
9559 for _, tc := range []struct {
9560 name string
9561 ref string
9562 expected_data []string
9563 }{
9564 {
9565 name: "test_using_output",
9566 ref: ":myapex",
Jooyung Hana0503a52023-08-23 13:12:50 +09009567 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.capex:myapex.capex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009568 },
9569 {
9570 name: "test_using_apex",
9571 ref: ":myapex{.apex}",
Jooyung Hana0503a52023-08-23 13:12:50 +09009572 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.apex:myapex.apex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009573 },
9574 } {
9575 t.Run(tc.name, func(t *testing.T) {
9576 ctx := testApex(t, `
9577 apex {
9578 name: "myapex",
9579 key: "myapex.key",
9580 compressible: true,
9581 updatable: false,
9582 }
9583
9584 apex_key {
9585 name: "myapex.key",
9586 public_key: "testkey.avbpubkey",
9587 private_key: "testkey.pem",
9588 }
9589
9590 java_test {
9591 name: "`+tc.name+`",
9592 srcs: ["a.java"],
9593 data: ["`+tc.ref+`"],
9594 }
9595 `,
9596 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9597 variables.CompressedApex = proptools.BoolPtr(true)
9598 }))
9599 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9600 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9601 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9602 })
9603 }
9604}
9605
satayev758968a2021-12-06 11:42:40 +00009606func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9607 preparer := android.GroupFixturePreparers(
9608 PrepareForTestWithApexBuildComponents,
9609 prepareForTestWithMyapex,
9610 java.PrepareForTestWithJavaSdkLibraryFiles,
9611 java.PrepareForTestWithJavaDefaultModules,
9612 android.PrepareForTestWithAndroidBuildComponents,
9613 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9614 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9615 )
9616
9617 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9618 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9619 preparer.RunTestWithBp(t, `
9620 apex {
9621 name: "myapex",
9622 key: "myapex.key",
9623 bootclasspath_fragments: ["mybootclasspathfragment"],
9624 min_sdk_version: "30",
9625 updatable: false,
9626 }
9627
9628 apex_key {
9629 name: "myapex.key",
9630 public_key: "testkey.avbpubkey",
9631 private_key: "testkey.pem",
9632 }
9633
9634 bootclasspath_fragment {
9635 name: "mybootclasspathfragment",
9636 contents: ["mybootclasspathlib"],
9637 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009638 hidden_api: {
9639 split_packages: ["*"],
9640 },
satayev758968a2021-12-06 11:42:40 +00009641 }
9642
9643 java_sdk_library {
9644 name: "mybootclasspathlib",
9645 srcs: ["mybootclasspathlib.java"],
9646 apex_available: ["myapex"],
9647 compile_dex: true,
9648 unsafe_ignore_missing_latest_api: true,
9649 min_sdk_version: "31",
9650 static_libs: ["util"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00009651 sdk_version: "core_current",
satayev758968a2021-12-06 11:42:40 +00009652 }
9653
9654 java_library {
9655 name: "util",
9656 srcs: ["a.java"],
9657 apex_available: ["myapex"],
9658 min_sdk_version: "31",
9659 static_libs: ["another_util"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00009660 sdk_version: "core_current",
satayev758968a2021-12-06 11:42:40 +00009661 }
9662
9663 java_library {
9664 name: "another_util",
9665 srcs: ["a.java"],
9666 min_sdk_version: "31",
9667 apex_available: ["myapex"],
Jihoon Kang85bc1932024-07-01 17:04:46 +00009668 sdk_version: "core_current",
satayev758968a2021-12-06 11:42:40 +00009669 }
9670 `)
9671 })
9672
9673 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9674 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9675 preparer.RunTestWithBp(t, `
9676 apex {
9677 name: "myapex",
9678 key: "myapex.key",
9679 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9680 min_sdk_version: "30",
9681 updatable: false,
9682 }
9683
9684 apex_key {
9685 name: "myapex.key",
9686 public_key: "testkey.avbpubkey",
9687 private_key: "testkey.pem",
9688 }
9689
9690 systemserverclasspath_fragment {
9691 name: "mysystemserverclasspathfragment",
9692 contents: ["mysystemserverclasspathlib"],
9693 apex_available: ["myapex"],
9694 }
9695
9696 java_sdk_library {
9697 name: "mysystemserverclasspathlib",
9698 srcs: ["mysystemserverclasspathlib.java"],
9699 apex_available: ["myapex"],
9700 compile_dex: true,
9701 min_sdk_version: "32",
9702 unsafe_ignore_missing_latest_api: true,
9703 static_libs: ["util"],
9704 }
9705
9706 java_library {
9707 name: "util",
9708 srcs: ["a.java"],
9709 apex_available: ["myapex"],
9710 min_sdk_version: "31",
9711 static_libs: ["another_util"],
9712 }
9713
9714 java_library {
9715 name: "another_util",
9716 srcs: ["a.java"],
9717 min_sdk_version: "31",
9718 apex_available: ["myapex"],
9719 }
9720 `)
9721 })
9722
9723 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
Jihoon Kang85bc1932024-07-01 17:04:46 +00009724 preparer.
satayev758968a2021-12-06 11:42:40 +00009725 RunTestWithBp(t, `
9726 apex {
9727 name: "myapex",
9728 key: "myapex.key",
9729 bootclasspath_fragments: ["mybootclasspathfragment"],
9730 min_sdk_version: "30",
9731 updatable: false,
9732 }
9733
9734 apex_key {
9735 name: "myapex.key",
9736 public_key: "testkey.avbpubkey",
9737 private_key: "testkey.pem",
9738 }
9739
9740 bootclasspath_fragment {
9741 name: "mybootclasspathfragment",
9742 contents: ["mybootclasspathlib"],
9743 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009744 hidden_api: {
9745 split_packages: ["*"],
9746 },
satayev758968a2021-12-06 11:42:40 +00009747 }
9748
9749 java_sdk_library {
9750 name: "mybootclasspathlib",
9751 srcs: ["mybootclasspathlib.java"],
9752 apex_available: ["myapex"],
9753 compile_dex: true,
9754 unsafe_ignore_missing_latest_api: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +00009755 sdk_version: "current",
9756 min_sdk_version: "30",
satayev758968a2021-12-06 11:42:40 +00009757 }
9758 `)
9759 })
9760
9761 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9762 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9763 RunTestWithBp(t, `
9764 apex {
9765 name: "myapex",
9766 key: "myapex.key",
9767 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9768 min_sdk_version: "30",
9769 updatable: false,
9770 }
9771
9772 apex_key {
9773 name: "myapex.key",
9774 public_key: "testkey.avbpubkey",
9775 private_key: "testkey.pem",
9776 }
9777
9778 systemserverclasspath_fragment {
9779 name: "mysystemserverclasspathfragment",
9780 contents: ["mysystemserverclasspathlib"],
9781 apex_available: ["myapex"],
9782 }
9783
9784 java_sdk_library {
9785 name: "mysystemserverclasspathlib",
9786 srcs: ["mysystemserverclasspathlib.java"],
9787 apex_available: ["myapex"],
9788 compile_dex: true,
9789 unsafe_ignore_missing_latest_api: true,
9790 }
9791 `)
9792 })
9793}
9794
Jiakai Zhang6decef92022-01-12 17:56:19 +00009795// Verifies that the APEX depends on all the Make modules in the list.
9796func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9797 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9798 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009799 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009800 }
9801}
9802
9803// Verifies that the APEX does not depend on any of the Make modules in the list.
9804func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9805 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9806 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009807 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009808 }
9809}
9810
Cole Faust24e25c02024-01-19 14:12:17 -08009811func TestApexStrictUpdtabilityLint(t *testing.T) {
9812 bpTemplate := `
9813 apex {
9814 name: "myapex",
9815 key: "myapex.key",
9816 java_libs: ["myjavalib"],
9817 updatable: %v,
9818 min_sdk_version: "29",
9819 }
9820 apex_key {
9821 name: "myapex.key",
9822 }
9823 java_library {
9824 name: "myjavalib",
9825 srcs: ["MyClass.java"],
9826 apex_available: [ "myapex" ],
9827 lint: {
9828 strict_updatability_linting: %v,
9829 %s
9830 },
9831 sdk_version: "current",
9832 min_sdk_version: "29",
9833 }
9834 `
9835 fs := android.MockFS{
9836 "lint-baseline.xml": nil,
9837 }
9838
9839 testCases := []struct {
Colin Cross87427352024-09-25 15:41:19 -07009840 testCaseName string
9841 apexUpdatable bool
9842 javaStrictUpdtabilityLint bool
9843 lintFileExists bool
9844 disallowedFlagExpectedOnApex bool
9845 disallowedFlagExpectedOnJavalib bool
Cole Faust24e25c02024-01-19 14:12:17 -08009846 }{
9847 {
Colin Cross87427352024-09-25 15:41:19 -07009848 testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9849 apexUpdatable: true,
9850 javaStrictUpdtabilityLint: true,
9851 lintFileExists: false,
9852 disallowedFlagExpectedOnApex: false,
9853 disallowedFlagExpectedOnJavalib: false,
Cole Faust24e25c02024-01-19 14:12:17 -08009854 },
9855 {
Colin Cross87427352024-09-25 15:41:19 -07009856 testCaseName: "non-updatable apex respects strict_updatability of javalib",
9857 apexUpdatable: false,
9858 javaStrictUpdtabilityLint: false,
9859 lintFileExists: true,
9860 disallowedFlagExpectedOnApex: false,
9861 disallowedFlagExpectedOnJavalib: false,
Cole Faust24e25c02024-01-19 14:12:17 -08009862 },
9863 {
Colin Cross87427352024-09-25 15:41:19 -07009864 testCaseName: "non-updatable apex respects strict updatability of javalib",
9865 apexUpdatable: false,
9866 javaStrictUpdtabilityLint: true,
9867 lintFileExists: true,
9868 disallowedFlagExpectedOnApex: false,
9869 disallowedFlagExpectedOnJavalib: true,
Cole Faust24e25c02024-01-19 14:12:17 -08009870 },
9871 {
Colin Cross87427352024-09-25 15:41:19 -07009872 testCaseName: "updatable apex checks strict updatability of javalib",
9873 apexUpdatable: true,
9874 javaStrictUpdtabilityLint: false,
9875 lintFileExists: true,
9876 disallowedFlagExpectedOnApex: true,
9877 disallowedFlagExpectedOnJavalib: false,
Cole Faust24e25c02024-01-19 14:12:17 -08009878 },
9879 }
9880
9881 for _, testCase := range testCases {
Colin Cross87427352024-09-25 15:41:19 -07009882 t.Run(testCase.testCaseName, func(t *testing.T) {
9883 fixtures := []android.FixturePreparer{}
9884 baselineProperty := ""
9885 if testCase.lintFileExists {
9886 fixtures = append(fixtures, fs.AddToFixture())
9887 baselineProperty = "baseline_filename: \"lint-baseline.xml\""
9888 }
9889 bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint, baselineProperty)
Cole Faust24e25c02024-01-19 14:12:17 -08009890
Colin Cross87427352024-09-25 15:41:19 -07009891 result := testApex(t, bp, fixtures...)
Cole Faust24e25c02024-01-19 14:12:17 -08009892
Colin Cross87427352024-09-25 15:41:19 -07009893 checkModule := func(m android.TestingBuildParams, name string, expectStrictUpdatability bool) {
9894 if expectStrictUpdatability {
9895 if m.Rule == nil {
9896 t.Errorf("expected strict updatability check rule on %s", name)
9897 } else {
9898 android.AssertStringDoesContain(t, fmt.Sprintf("strict updatability check rule for %s", name),
9899 m.RuleParams.Command, "--disallowed_issues NewApi")
9900 android.AssertStringListContains(t, fmt.Sprintf("strict updatability check baselines for %s", name),
9901 m.Inputs.Strings(), "lint-baseline.xml")
9902 }
9903 } else {
9904 if m.Rule != nil {
9905 t.Errorf("expected no strict updatability check rule on %s", name)
9906 }
9907 }
9908 }
9909
9910 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9911 apex := result.ModuleForTests("myapex", "android_common_myapex")
9912 apexStrictUpdatabilityCheck := apex.MaybeOutput("lint_strict_updatability_check.stamp")
9913 javalibStrictUpdatabilityCheck := myjavalib.MaybeOutput("lint_strict_updatability_check.stamp")
9914
9915 checkModule(apexStrictUpdatabilityCheck, "myapex", testCase.disallowedFlagExpectedOnApex)
9916 checkModule(javalibStrictUpdatabilityCheck, "myjavalib", testCase.disallowedFlagExpectedOnJavalib)
9917 })
Cole Faust24e25c02024-01-19 14:12:17 -08009918 }
9919}
9920
Cole Faust24e25c02024-01-19 14:12:17 -08009921// checks transtive deps of an apex coming from bootclasspath_fragment
9922func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9923 bp := `
9924 apex {
9925 name: "myapex",
9926 key: "myapex.key",
9927 bootclasspath_fragments: ["mybootclasspathfragment"],
9928 updatable: true,
9929 min_sdk_version: "29",
9930 }
9931 apex_key {
9932 name: "myapex.key",
9933 }
9934 bootclasspath_fragment {
9935 name: "mybootclasspathfragment",
9936 contents: ["myjavalib"],
9937 apex_available: ["myapex"],
9938 hidden_api: {
9939 split_packages: ["*"],
9940 },
9941 }
9942 java_library {
9943 name: "myjavalib",
9944 srcs: ["MyClass.java"],
9945 apex_available: [ "myapex" ],
9946 sdk_version: "current",
9947 min_sdk_version: "29",
9948 compile_dex: true,
9949 lint: {
9950 baseline_filename: "lint-baseline.xml",
9951 }
9952 }
9953 `
9954 fs := android.MockFS{
9955 "lint-baseline.xml": nil,
9956 }
9957
9958 result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
Colin Cross87427352024-09-25 15:41:19 -07009959 apex := result.ModuleForTests("myapex", "android_common_myapex")
9960 apexStrictUpdatabilityCheck := apex.Output("lint_strict_updatability_check.stamp")
9961 android.AssertStringDoesContain(t, "strict updatability check rule for myapex",
9962 apexStrictUpdatabilityCheck.RuleParams.Command, "--disallowed_issues NewApi")
9963 android.AssertStringListContains(t, "strict updatability check baselines for myapex",
9964 apexStrictUpdatabilityCheck.Inputs.Strings(), "lint-baseline.xml")
Cole Faust24e25c02024-01-19 14:12:17 -08009965}
Spandan Das66773252022-01-15 00:23:18 +00009966
Jihoon Kang784c0052024-06-25 22:15:39 +00009967func TestApexLintBcpFragmentSdkLibDeps(t *testing.T) {
9968 bp := `
9969 apex {
9970 name: "myapex",
9971 key: "myapex.key",
9972 bootclasspath_fragments: ["mybootclasspathfragment"],
9973 min_sdk_version: "29",
Jihoon Kang85bc1932024-07-01 17:04:46 +00009974 java_libs: [
9975 "jacocoagent",
9976 ],
Jihoon Kang784c0052024-06-25 22:15:39 +00009977 }
9978 apex_key {
9979 name: "myapex.key",
9980 }
9981 bootclasspath_fragment {
9982 name: "mybootclasspathfragment",
9983 contents: ["foo"],
9984 apex_available: ["myapex"],
9985 hidden_api: {
9986 split_packages: ["*"],
9987 },
9988 }
9989 java_sdk_library {
9990 name: "foo",
9991 srcs: ["MyClass.java"],
9992 apex_available: [ "myapex" ],
9993 sdk_version: "current",
9994 min_sdk_version: "29",
9995 compile_dex: true,
9996 }
9997 `
9998 fs := android.MockFS{
9999 "lint-baseline.xml": nil,
10000 }
10001
10002 result := android.GroupFixturePreparers(
10003 prepareForApexTest,
10004 java.PrepareForTestWithJavaSdkLibraryFiles,
10005 java.PrepareForTestWithJacocoInstrumentation,
10006 java.FixtureWithLastReleaseApis("foo"),
Jihoon Kang784c0052024-06-25 22:15:39 +000010007 android.FixtureMergeMockFs(fs),
10008 ).RunTestWithBp(t, bp)
10009
10010 myapex := result.ModuleForTests("myapex", "android_common_myapex")
10011 lintReportInputs := strings.Join(myapex.Output("lint-report-xml.zip").Inputs.Strings(), " ")
10012 android.AssertStringDoesContain(t,
10013 "myapex lint report expected to contain that of the sdk library impl lib as an input",
10014 lintReportInputs, "foo.impl")
10015}
10016
Spandan Das42e89502022-05-06 22:12:55 +000010017// updatable apexes should propagate updatable=true to its apps
10018func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
10019 bp := `
10020 apex {
10021 name: "myapex",
10022 key: "myapex.key",
10023 updatable: %v,
10024 apps: [
10025 "myapp",
10026 ],
10027 min_sdk_version: "30",
10028 }
10029 apex_key {
10030 name: "myapex.key",
10031 }
10032 android_app {
10033 name: "myapp",
10034 updatable: %v,
10035 apex_available: [
10036 "myapex",
10037 ],
10038 sdk_version: "current",
10039 min_sdk_version: "30",
10040 }
10041 `
10042 testCases := []struct {
10043 name string
10044 apex_is_updatable_bp bool
10045 app_is_updatable_bp bool
10046 app_is_updatable_expected bool
10047 }{
10048 {
10049 name: "Non-updatable apex respects updatable property of non-updatable app",
10050 apex_is_updatable_bp: false,
10051 app_is_updatable_bp: false,
10052 app_is_updatable_expected: false,
10053 },
10054 {
10055 name: "Non-updatable apex respects updatable property of updatable app",
10056 apex_is_updatable_bp: false,
10057 app_is_updatable_bp: true,
10058 app_is_updatable_expected: true,
10059 },
10060 {
10061 name: "Updatable apex respects updatable property of updatable app",
10062 apex_is_updatable_bp: true,
10063 app_is_updatable_bp: true,
10064 app_is_updatable_expected: true,
10065 },
10066 {
10067 name: "Updatable apex sets updatable=true on non-updatable app",
10068 apex_is_updatable_bp: true,
10069 app_is_updatable_bp: false,
10070 app_is_updatable_expected: true,
10071 },
10072 }
10073 for _, testCase := range testCases {
10074 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
10075 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
10076 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
10077 }
10078}
10079
Dennis Shend4f5d932023-01-31 20:27:21 +000010080func TestTrimmedApex(t *testing.T) {
10081 bp := `
10082 apex {
10083 name: "myapex",
10084 key: "myapex.key",
10085 native_shared_libs: ["libfoo","libbaz"],
10086 min_sdk_version: "29",
10087 trim_against: "mydcla",
10088 }
10089 apex {
10090 name: "mydcla",
10091 key: "myapex.key",
10092 native_shared_libs: ["libfoo","libbar"],
10093 min_sdk_version: "29",
10094 file_contexts: ":myapex-file_contexts",
10095 dynamic_common_lib_apex: true,
10096 }
10097 apex_key {
10098 name: "myapex.key",
10099 }
10100 cc_library {
10101 name: "libfoo",
10102 shared_libs: ["libc"],
10103 apex_available: ["myapex","mydcla"],
10104 min_sdk_version: "29",
10105 }
10106 cc_library {
10107 name: "libbar",
10108 shared_libs: ["libc"],
10109 apex_available: ["myapex","mydcla"],
10110 min_sdk_version: "29",
10111 }
10112 cc_library {
10113 name: "libbaz",
10114 shared_libs: ["libc"],
10115 apex_available: ["myapex","mydcla"],
10116 min_sdk_version: "29",
10117 }
Dennis Shend4f5d932023-01-31 20:27:21 +000010118 `
10119 ctx := testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +090010120 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dennis Shend4f5d932023-01-31 20:27:21 +000010121 apexRule := module.MaybeRule("apexRule")
10122 if apexRule.Rule == nil {
10123 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10124 }
10125
10126 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
Jooyung Hana0503a52023-08-23 13:12:50 +090010127 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("TrimmedApexRule")
Dennis Shend4f5d932023-01-31 20:27:21 +000010128 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10129 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10130 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10131 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10132}
Jingwen Chendea7a642023-03-28 11:30:50 +000010133
10134func TestCannedFsConfig(t *testing.T) {
10135 ctx := testApex(t, `
10136 apex {
10137 name: "myapex",
10138 key: "myapex.key",
10139 updatable: false,
10140 }
10141
10142 apex_key {
10143 name: "myapex.key",
10144 public_key: "testkey.avbpubkey",
10145 private_key: "testkey.pem",
10146 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010147 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010148 generateFsRule := mod.Rule("generateFsConfig")
10149 cmd := generateFsRule.RuleParams.Command
10150
10151 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10152}
10153
10154func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10155 ctx := testApex(t, `
10156 apex {
10157 name: "myapex",
10158 key: "myapex.key",
10159 canned_fs_config: "my_config",
10160 updatable: false,
10161 }
10162
10163 apex_key {
10164 name: "myapex.key",
10165 public_key: "testkey.avbpubkey",
10166 private_key: "testkey.pem",
10167 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010168 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010169 generateFsRule := mod.Rule("generateFsConfig")
10170 cmd := generateFsRule.RuleParams.Command
10171
10172 // Ensure that canned_fs_config has "cat my_config" at the end
10173 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10174}
Spandan Das20fce2d2023-04-12 17:21:39 +000010175
10176func TestStubLibrariesMultipleApexViolation(t *testing.T) {
10177 testCases := []struct {
10178 desc string
10179 hasStubs bool
10180 apexAvailable string
10181 expectedError string
10182 }{
10183 {
10184 desc: "non-stub library can have multiple apex_available",
10185 hasStubs: false,
10186 apexAvailable: `["myapex", "otherapex"]`,
10187 },
10188 {
10189 desc: "stub library should not be available to anyapex",
10190 hasStubs: true,
10191 apexAvailable: `["//apex_available:anyapex"]`,
10192 expectedError: "Stub libraries should have a single apex_available.*anyapex",
10193 },
10194 {
10195 desc: "stub library should not be available to multiple apexes",
10196 hasStubs: true,
10197 apexAvailable: `["myapex", "otherapex"]`,
10198 expectedError: "Stub libraries should have a single apex_available.*myapex.*otherapex",
10199 },
10200 {
10201 desc: "stub library can be available to a core apex and a test apex",
10202 hasStubs: true,
10203 apexAvailable: `["myapex", "test_myapex"]`,
10204 },
10205 }
10206 bpTemplate := `
10207 cc_library {
10208 name: "libfoo",
10209 %v
10210 apex_available: %v,
10211 }
10212 apex {
10213 name: "myapex",
10214 key: "apex.key",
10215 updatable: false,
10216 native_shared_libs: ["libfoo"],
10217 }
10218 apex {
10219 name: "otherapex",
10220 key: "apex.key",
10221 updatable: false,
10222 }
10223 apex_test {
10224 name: "test_myapex",
10225 key: "apex.key",
10226 updatable: false,
10227 native_shared_libs: ["libfoo"],
10228 }
10229 apex_key {
10230 name: "apex.key",
10231 }
10232 `
10233 for _, tc := range testCases {
10234 stubs := ""
10235 if tc.hasStubs {
10236 stubs = `stubs: {symbol_file: "libfoo.map.txt"},`
10237 }
10238 bp := fmt.Sprintf(bpTemplate, stubs, tc.apexAvailable)
10239 mockFsFixturePreparer := android.FixtureModifyMockFS(func(fs android.MockFS) {
10240 fs["system/sepolicy/apex/test_myapex-file_contexts"] = nil
10241 })
10242 if tc.expectedError == "" {
10243 testApex(t, bp, mockFsFixturePreparer)
10244 } else {
10245 testApexError(t, tc.expectedError, bp, mockFsFixturePreparer)
10246 }
10247 }
10248}
Colin Crossbd3a16b2023-04-25 11:30:51 -070010249
10250func TestFileSystemShouldSkipApexLibraries(t *testing.T) {
10251 context := android.GroupFixturePreparers(
10252 android.PrepareForIntegrationTestWithAndroid,
10253 cc.PrepareForIntegrationTestWithCc,
10254 PrepareForTestWithApexBuildComponents,
10255 prepareForTestWithMyapex,
10256 filesystem.PrepareForTestWithFilesystemBuildComponents,
10257 )
10258 result := context.RunTestWithBp(t, `
10259 android_system_image {
10260 name: "myfilesystem",
10261 deps: [
10262 "libfoo",
10263 ],
10264 linker_config_src: "linker.config.json",
10265 }
10266
10267 cc_library {
10268 name: "libfoo",
10269 shared_libs: [
10270 "libbar",
10271 ],
10272 stl: "none",
10273 }
10274
10275 cc_library {
10276 name: "libbar",
10277 stl: "none",
10278 apex_available: ["myapex"],
10279 }
10280
10281 apex {
10282 name: "myapex",
10283 native_shared_libs: ["libbar"],
10284 key: "myapex.key",
10285 updatable: false,
10286 }
10287
10288 apex_key {
10289 name: "myapex.key",
10290 public_key: "testkey.avbpubkey",
10291 private_key: "testkey.pem",
10292 }
10293 `)
10294
Cole Faust3b806d32024-03-11 15:15:03 -070010295 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits
Colin Crossbd3a16b2023-04-25 11:30:51 -070010296 android.AssertStringListDoesNotContain(t, "filesystem should not have libbar",
10297 inputs.Strings(),
10298 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
10299}
Yu Liueae7b362023-11-16 17:05:47 -080010300
10301var apex_default_bp = `
10302 apex_key {
10303 name: "myapex.key",
10304 public_key: "testkey.avbpubkey",
10305 private_key: "testkey.pem",
10306 }
10307
10308 filegroup {
10309 name: "myapex.manifest",
10310 srcs: ["apex_manifest.json"],
10311 }
10312
10313 filegroup {
10314 name: "myapex.androidmanifest",
10315 srcs: ["AndroidManifest.xml"],
10316 }
10317`
10318
10319func TestAconfigFilesJavaDeps(t *testing.T) {
10320 ctx := testApex(t, apex_default_bp+`
10321 apex {
10322 name: "myapex",
10323 manifest: ":myapex.manifest",
10324 androidManifest: ":myapex.androidmanifest",
10325 key: "myapex.key",
10326 java_libs: [
10327 "my_java_library_foo",
10328 "my_java_library_bar",
10329 ],
10330 updatable: false,
10331 }
10332
10333 java_library {
10334 name: "my_java_library_foo",
10335 srcs: ["foo/bar/MyClass.java"],
10336 sdk_version: "none",
10337 system_modules: "none",
10338 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010339 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010340 "myapex",
10341 ],
10342 }
10343
10344 java_library {
10345 name: "my_java_library_bar",
10346 srcs: ["foo/bar/MyClass.java"],
10347 sdk_version: "none",
10348 system_modules: "none",
10349 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010350 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010351 "myapex",
10352 ],
10353 }
10354
10355 aconfig_declarations {
10356 name: "my_aconfig_declarations_foo",
10357 package: "com.example.package",
10358 container: "myapex",
10359 srcs: ["foo.aconfig"],
10360 }
10361
10362 java_aconfig_library {
10363 name: "my_java_aconfig_library_foo",
10364 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010365 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010366 "myapex",
10367 ],
10368 }
10369
10370 aconfig_declarations {
10371 name: "my_aconfig_declarations_bar",
10372 package: "com.example.package",
10373 container: "myapex",
10374 srcs: ["bar.aconfig"],
10375 }
10376
10377 java_aconfig_library {
10378 name: "my_java_aconfig_library_bar",
10379 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010380 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010381 "myapex",
10382 ],
10383 }
10384 `)
10385
10386 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10387 s := mod.Rule("apexRule").Args["copy_commands"]
10388 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Dennis Shen343d09e2024-09-23 17:19:15 +000010389 if len(copyCmds) != 14 {
10390 t.Fatalf("Expected 14 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010391 }
10392
Jooyung Hana3fddf42024-09-03 13:22:21 +090010393 ensureListContainsMatch(t, copyCmds, "^cp -f .*/aconfig_flags.pb .*/image.apex/etc/aconfig_flags.pb")
10394 ensureListContainsMatch(t, copyCmds, "^cp -f .*/package.map .*/image.apex/etc/package.map")
10395 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.map .*/image.apex/etc/flag.map")
10396 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.val .*/image.apex/etc/flag.val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010397 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.info.*/image.apex/etc/flag.info")
Yu Liueae7b362023-11-16 17:05:47 -080010398
Yu Liubba555e2024-02-17 00:36:42 +000010399 inputs := []string{
10400 "my_aconfig_declarations_foo/intermediate.pb",
10401 "my_aconfig_declarations_bar/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010402 }
Yu Liubba555e2024-02-17 00:36:42 +000010403 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10404 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10405 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10406 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010407 VerifyAconfigRule(t, &mod, "create_aconfig_flag_info_file", inputs, "android_common_myapex/flag.info", "myapex", "flag_info")
Yu Liueae7b362023-11-16 17:05:47 -080010408}
10409
10410func TestAconfigFilesJavaAndCcDeps(t *testing.T) {
10411 ctx := testApex(t, apex_default_bp+`
10412 apex {
10413 name: "myapex",
10414 manifest: ":myapex.manifest",
10415 androidManifest: ":myapex.androidmanifest",
10416 key: "myapex.key",
10417 java_libs: [
10418 "my_java_library_foo",
10419 ],
10420 native_shared_libs: [
10421 "my_cc_library_bar",
10422 ],
10423 binaries: [
10424 "my_cc_binary_baz",
10425 ],
10426 updatable: false,
10427 }
10428
10429 java_library {
10430 name: "my_java_library_foo",
10431 srcs: ["foo/bar/MyClass.java"],
10432 sdk_version: "none",
10433 system_modules: "none",
10434 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010435 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010436 "myapex",
10437 ],
10438 }
10439
10440 cc_library {
10441 name: "my_cc_library_bar",
10442 srcs: ["foo/bar/MyClass.cc"],
Yu Liucec0e412023-11-30 16:45:50 -080010443 static_libs: [
10444 "my_cc_aconfig_library_bar",
10445 "my_cc_aconfig_library_baz",
10446 ],
Yu Liueae7b362023-11-16 17:05:47 -080010447 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010448 "myapex",
10449 ],
10450 }
10451
10452 cc_binary {
10453 name: "my_cc_binary_baz",
10454 srcs: ["foo/bar/MyClass.cc"],
10455 static_libs: ["my_cc_aconfig_library_baz"],
Yu Liueae7b362023-11-16 17:05:47 -080010456 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010457 "myapex",
10458 ],
10459 }
10460
10461 aconfig_declarations {
10462 name: "my_aconfig_declarations_foo",
10463 package: "com.example.package",
10464 container: "myapex",
10465 srcs: ["foo.aconfig"],
10466 }
10467
10468 java_aconfig_library {
10469 name: "my_java_aconfig_library_foo",
10470 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010471 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010472 "myapex",
10473 ],
10474 }
10475
10476 aconfig_declarations {
10477 name: "my_aconfig_declarations_bar",
10478 package: "com.example.package",
10479 container: "myapex",
10480 srcs: ["bar.aconfig"],
10481 }
10482
10483 cc_aconfig_library {
10484 name: "my_cc_aconfig_library_bar",
10485 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010486 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010487 "myapex",
10488 ],
10489 }
10490
10491 aconfig_declarations {
10492 name: "my_aconfig_declarations_baz",
10493 package: "com.example.package",
10494 container: "myapex",
10495 srcs: ["baz.aconfig"],
10496 }
10497
10498 cc_aconfig_library {
10499 name: "my_cc_aconfig_library_baz",
10500 aconfig_declarations: "my_aconfig_declarations_baz",
Yu Liueae7b362023-11-16 17:05:47 -080010501 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010502 "myapex",
10503 ],
10504 }
10505
10506 cc_library {
10507 name: "server_configurable_flags",
10508 srcs: ["server_configurable_flags.cc"],
10509 }
Ted Bauerf0f18592024-04-23 18:25:26 +000010510 cc_library {
10511 name: "libbase",
10512 srcs: ["libbase.cc"],
Ted Bauer1e96f8c2024-04-25 19:50:01 +000010513 apex_available: [
10514 "myapex",
10515 ],
Ted Bauerf0f18592024-04-23 18:25:26 +000010516 }
10517 cc_library {
10518 name: "libaconfig_storage_read_api_cc",
10519 srcs: ["libaconfig_storage_read_api_cc.cc"],
10520 }
Yu Liueae7b362023-11-16 17:05:47 -080010521 `)
10522
10523 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10524 s := mod.Rule("apexRule").Args["copy_commands"]
10525 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Dennis Shen343d09e2024-09-23 17:19:15 +000010526 if len(copyCmds) != 18 {
10527 t.Fatalf("Expected 18 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010528 }
10529
Jooyung Hana3fddf42024-09-03 13:22:21 +090010530 ensureListContainsMatch(t, copyCmds, "^cp -f .*/aconfig_flags.pb .*/image.apex/etc/aconfig_flags.pb")
10531 ensureListContainsMatch(t, copyCmds, "^cp -f .*/package.map .*/image.apex/etc/package.map")
10532 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.map .*/image.apex/etc/flag.map")
10533 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.val .*/image.apex/etc/flag.val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010534 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.info .*/image.apex/etc/flag.info")
Yu Liueae7b362023-11-16 17:05:47 -080010535
Yu Liubba555e2024-02-17 00:36:42 +000010536 inputs := []string{
10537 "my_aconfig_declarations_foo/intermediate.pb",
10538 "my_cc_library_bar/android_arm64_armv8-a_shared_apex10000/myapex/aconfig_merged.pb",
10539 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010540 }
Yu Liubba555e2024-02-17 00:36:42 +000010541 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10542 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10543 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10544 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010545 VerifyAconfigRule(t, &mod, "create_aconfig_flag_info_file", inputs, "android_common_myapex/flag.info", "myapex", "flag_info")
Yu Liueae7b362023-11-16 17:05:47 -080010546}
10547
Yu Liucec0e412023-11-30 16:45:50 -080010548func TestAconfigFilesRustDeps(t *testing.T) {
10549 ctx := testApex(t, apex_default_bp+`
10550 apex {
10551 name: "myapex",
10552 manifest: ":myapex.manifest",
10553 androidManifest: ":myapex.androidmanifest",
10554 key: "myapex.key",
10555 native_shared_libs: [
10556 "libmy_rust_library",
10557 ],
10558 binaries: [
10559 "my_rust_binary",
10560 ],
10561 rust_dyn_libs: [
10562 "libmy_rust_dylib",
10563 ],
10564 updatable: false,
10565 }
10566
10567 rust_library {
10568 name: "libflags_rust", // test mock
10569 crate_name: "flags_rust",
10570 srcs: ["lib.rs"],
10571 apex_available: [
10572 "myapex",
10573 ],
10574 }
10575
10576 rust_library {
10577 name: "liblazy_static", // test mock
10578 crate_name: "lazy_static",
10579 srcs: ["src/lib.rs"],
10580 apex_available: [
10581 "myapex",
10582 ],
10583 }
10584
Ted Bauer02d475c2024-03-27 20:56:26 +000010585 rust_library {
10586 name: "libaconfig_storage_read_api", // test mock
10587 crate_name: "aconfig_storage_read_api",
10588 srcs: ["src/lib.rs"],
10589 apex_available: [
10590 "myapex",
10591 ],
10592 }
10593
Ted Bauer6ef40db2024-03-29 14:04:10 +000010594 rust_library {
10595 name: "liblogger", // test mock
10596 crate_name: "logger",
10597 srcs: ["src/lib.rs"],
10598 apex_available: [
10599 "myapex",
10600 ],
10601 }
10602
10603 rust_library {
10604 name: "liblog_rust", // test mock
10605 crate_name: "log_rust",
10606 srcs: ["src/lib.rs"],
10607 apex_available: [
10608 "myapex",
10609 ],
10610 }
10611
Yu Liucec0e412023-11-30 16:45:50 -080010612 rust_ffi_shared {
10613 name: "libmy_rust_library",
10614 srcs: ["src/lib.rs"],
10615 rustlibs: ["libmy_rust_aconfig_library_foo"],
10616 crate_name: "my_rust_library",
10617 apex_available: [
10618 "myapex",
10619 ],
10620 }
10621
10622 rust_library_dylib {
10623 name: "libmy_rust_dylib",
10624 srcs: ["foo/bar/MyClass.rs"],
10625 rustlibs: ["libmy_rust_aconfig_library_bar"],
10626 crate_name: "my_rust_dylib",
10627 apex_available: [
10628 "myapex",
10629 ],
10630 }
10631
10632 rust_binary {
10633 name: "my_rust_binary",
10634 srcs: ["foo/bar/MyClass.rs"],
10635 rustlibs: [
10636 "libmy_rust_aconfig_library_baz",
10637 "libmy_rust_dylib",
10638 ],
10639 apex_available: [
10640 "myapex",
10641 ],
10642 }
10643
10644 aconfig_declarations {
10645 name: "my_aconfig_declarations_foo",
10646 package: "com.example.package",
10647 container: "myapex",
10648 srcs: ["foo.aconfig"],
10649 }
10650
10651 aconfig_declarations {
10652 name: "my_aconfig_declarations_bar",
10653 package: "com.example.package",
10654 container: "myapex",
10655 srcs: ["bar.aconfig"],
10656 }
10657
10658 aconfig_declarations {
10659 name: "my_aconfig_declarations_baz",
10660 package: "com.example.package",
10661 container: "myapex",
10662 srcs: ["baz.aconfig"],
10663 }
10664
10665 rust_aconfig_library {
10666 name: "libmy_rust_aconfig_library_foo",
10667 aconfig_declarations: "my_aconfig_declarations_foo",
10668 crate_name: "my_rust_aconfig_library_foo",
10669 apex_available: [
10670 "myapex",
10671 ],
10672 }
10673
10674 rust_aconfig_library {
10675 name: "libmy_rust_aconfig_library_bar",
10676 aconfig_declarations: "my_aconfig_declarations_bar",
10677 crate_name: "my_rust_aconfig_library_bar",
10678 apex_available: [
10679 "myapex",
10680 ],
10681 }
10682
10683 rust_aconfig_library {
10684 name: "libmy_rust_aconfig_library_baz",
10685 aconfig_declarations: "my_aconfig_declarations_baz",
10686 crate_name: "my_rust_aconfig_library_baz",
10687 apex_available: [
10688 "myapex",
10689 ],
10690 }
10691 `)
10692
10693 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10694 s := mod.Rule("apexRule").Args["copy_commands"]
10695 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Dennis Shen343d09e2024-09-23 17:19:15 +000010696 if len(copyCmds) != 38 {
10697 t.Fatalf("Expected 38 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liucec0e412023-11-30 16:45:50 -080010698 }
10699
Jooyung Hana3fddf42024-09-03 13:22:21 +090010700 ensureListContainsMatch(t, copyCmds, "^cp -f .*/aconfig_flags.pb .*/image.apex/etc/aconfig_flags.pb")
10701 ensureListContainsMatch(t, copyCmds, "^cp -f .*/package.map .*/image.apex/etc/package.map")
10702 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.map .*/image.apex/etc/flag.map")
10703 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.val .*/image.apex/etc/flag.val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010704 ensureListContainsMatch(t, copyCmds, "^cp -f .*/flag.info .*/image.apex/etc/flag.info")
Yu Liucec0e412023-11-30 16:45:50 -080010705
Yu Liubba555e2024-02-17 00:36:42 +000010706 inputs := []string{
10707 "my_aconfig_declarations_foo/intermediate.pb",
Yu Liuab31c822024-02-28 22:21:31 +000010708 "my_aconfig_declarations_bar/intermediate.pb",
10709 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liubba555e2024-02-17 00:36:42 +000010710 "my_rust_binary/android_arm64_armv8-a_apex10000/myapex/aconfig_merged.pb",
10711 }
10712 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10713 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10714 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10715 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Dennis Shen343d09e2024-09-23 17:19:15 +000010716 VerifyAconfigRule(t, &mod, "create_aconfig_flag_info_file", inputs, "android_common_myapex/flag.info", "myapex", "flag_info")
Yu Liubba555e2024-02-17 00:36:42 +000010717}
10718
10719func VerifyAconfigRule(t *testing.T, mod *android.TestingModule, desc string, inputs []string, output string, container string, file_type string) {
10720 aconfigRule := mod.Description(desc)
10721 s := " " + aconfigRule.Args["cache_files"]
Yu Liucec0e412023-11-30 16:45:50 -080010722 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
Yu Liubba555e2024-02-17 00:36:42 +000010723 if len(aconfigArgs) != len(inputs) {
10724 t.Fatalf("Expected %d commands, got %d in:\n%s", len(inputs), len(aconfigArgs), s)
Yu Liucec0e412023-11-30 16:45:50 -080010725 }
Yu Liucec0e412023-11-30 16:45:50 -080010726
Yu Liubba555e2024-02-17 00:36:42 +000010727 ensureEquals(t, container, aconfigRule.Args["container"])
10728 ensureEquals(t, file_type, aconfigRule.Args["file_type"])
10729
10730 buildParams := aconfigRule.BuildParams
10731 for _, input := range inputs {
10732 android.EnsureListContainsSuffix(t, aconfigArgs, input)
10733 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), input)
Yu Liucec0e412023-11-30 16:45:50 -080010734 }
Yu Liubba555e2024-02-17 00:36:42 +000010735
10736 ensureContains(t, buildParams.Output.String(), output)
Yu Liucec0e412023-11-30 16:45:50 -080010737}
10738
Yu Liueae7b362023-11-16 17:05:47 -080010739func TestAconfigFilesOnlyMatchCurrentApex(t *testing.T) {
10740 ctx := testApex(t, apex_default_bp+`
10741 apex {
10742 name: "myapex",
10743 manifest: ":myapex.manifest",
10744 androidManifest: ":myapex.androidmanifest",
10745 key: "myapex.key",
10746 java_libs: [
10747 "my_java_library_foo",
10748 "other_java_library_bar",
10749 ],
10750 updatable: false,
10751 }
10752
10753 java_library {
10754 name: "my_java_library_foo",
10755 srcs: ["foo/bar/MyClass.java"],
10756 sdk_version: "none",
10757 system_modules: "none",
10758 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010759 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010760 "myapex",
10761 ],
10762 }
10763
10764 java_library {
10765 name: "other_java_library_bar",
10766 srcs: ["foo/bar/MyClass.java"],
10767 sdk_version: "none",
10768 system_modules: "none",
10769 static_libs: ["other_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010770 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010771 "myapex",
10772 ],
10773 }
10774
10775 aconfig_declarations {
10776 name: "my_aconfig_declarations_foo",
10777 package: "com.example.package",
10778 container: "myapex",
10779 srcs: ["foo.aconfig"],
10780 }
10781
10782 java_aconfig_library {
10783 name: "my_java_aconfig_library_foo",
10784 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010785 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010786 "myapex",
10787 ],
10788 }
10789
10790 aconfig_declarations {
10791 name: "other_aconfig_declarations_bar",
10792 package: "com.example.package",
10793 container: "otherapex",
10794 srcs: ["bar.aconfig"],
10795 }
10796
10797 java_aconfig_library {
10798 name: "other_java_aconfig_library_bar",
10799 aconfig_declarations: "other_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010800 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010801 "myapex",
10802 ],
10803 }
10804 `)
10805
10806 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10807 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
10808 s := " " + combineAconfigRule.Args["cache_files"]
10809 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
10810 if len(aconfigArgs) != 1 {
10811 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
10812 }
10813 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
10814
10815 buildParams := combineAconfigRule.BuildParams
10816 if len(buildParams.Inputs) != 1 {
10817 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
10818 }
10819 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
10820 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
10821}
10822
10823func TestAconfigFilesRemoveDuplicates(t *testing.T) {
10824 ctx := testApex(t, apex_default_bp+`
10825 apex {
10826 name: "myapex",
10827 manifest: ":myapex.manifest",
10828 androidManifest: ":myapex.androidmanifest",
10829 key: "myapex.key",
10830 java_libs: [
10831 "my_java_library_foo",
10832 "my_java_library_bar",
10833 ],
10834 updatable: false,
10835 }
10836
10837 java_library {
10838 name: "my_java_library_foo",
10839 srcs: ["foo/bar/MyClass.java"],
10840 sdk_version: "none",
10841 system_modules: "none",
10842 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010843 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010844 "myapex",
10845 ],
10846 }
10847
10848 java_library {
10849 name: "my_java_library_bar",
10850 srcs: ["foo/bar/MyClass.java"],
10851 sdk_version: "none",
10852 system_modules: "none",
10853 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010854 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010855 "myapex",
10856 ],
10857 }
10858
10859 aconfig_declarations {
10860 name: "my_aconfig_declarations_foo",
10861 package: "com.example.package",
10862 container: "myapex",
10863 srcs: ["foo.aconfig"],
10864 }
10865
10866 java_aconfig_library {
10867 name: "my_java_aconfig_library_foo",
10868 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010869 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010870 "myapex",
10871 ],
10872 }
10873
10874 java_aconfig_library {
10875 name: "my_java_aconfig_library_bar",
10876 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010877 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010878 "myapex",
10879 ],
10880 }
10881 `)
10882
10883 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10884 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
10885 s := " " + combineAconfigRule.Args["cache_files"]
10886 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
10887 if len(aconfigArgs) != 1 {
10888 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
10889 }
10890 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
10891
10892 buildParams := combineAconfigRule.BuildParams
10893 if len(buildParams.Inputs) != 1 {
10894 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
10895 }
10896 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
10897 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
10898}
Spandan Das5be63332023-12-13 00:06:32 +000010899
10900// Test that the boot jars come from the _selected_ apex prebuilt
10901// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
10902func TestBootDexJarsMultipleApexPrebuilts(t *testing.T) {
10903 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
10904 t.Helper()
10905 s := ctx.ModuleForTests("dex_bootjars", "android_common")
10906 foundLibfooJar := false
10907 base := stem + ".jar"
10908 for _, output := range s.AllOutputs() {
10909 if filepath.Base(output) == base {
10910 foundLibfooJar = true
10911 buildRule := s.Output(output)
10912 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
10913 }
10914 }
10915 if !foundLibfooJar {
10916 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
10917 }
10918 }
10919
Spandan Das64c9e0c2023-12-20 20:13:34 +000010920 // Check that the boot jars of the selected apex are run through boot_jars_package_check
10921 // This validates that the jars on the bootclasspath do not contain packages outside an allowlist
10922 checkBootJarsPackageCheck := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
10923 platformBcp := ctx.ModuleForTests("platform-bootclasspath", "android_common")
10924 bootJarsCheckRule := platformBcp.Rule("boot_jars_package_check")
10925 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)
10926 }
10927
10928 // Check that the boot jars used to generate the monolithic hiddenapi flags come from the selected apex
10929 checkBootJarsForMonolithicHiddenapi := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
10930 monolithicHiddenapiFlagsCmd := ctx.ModuleForTests("platform-bootclasspath", "android_common").Output("out/soong/hiddenapi/hiddenapi-stub-flags.txt").RuleParams.Command
10931 android.AssertStringMatches(t, "Could not find the correct boot dex jar in monolithic hiddenapi flags generation command", monolithicHiddenapiFlagsCmd, "--boot-dex="+expectedBootJar)
10932 }
10933
Spandan Das5be63332023-12-13 00:06:32 +000010934 bp := `
10935 // Source APEX.
10936
10937 java_library {
10938 name: "framework-foo",
10939 srcs: ["foo.java"],
10940 installable: true,
10941 apex_available: [
10942 "com.android.foo",
10943 ],
10944 }
10945
10946 bootclasspath_fragment {
10947 name: "foo-bootclasspath-fragment",
10948 contents: ["framework-foo"],
10949 apex_available: [
10950 "com.android.foo",
10951 ],
10952 hidden_api: {
10953 split_packages: ["*"],
10954 },
10955 }
10956
10957 apex_key {
10958 name: "com.android.foo.key",
10959 public_key: "com.android.foo.avbpubkey",
10960 private_key: "com.android.foo.pem",
10961 }
10962
10963 apex {
10964 name: "com.android.foo",
10965 key: "com.android.foo.key",
10966 bootclasspath_fragments: ["foo-bootclasspath-fragment"],
10967 updatable: false,
10968 }
10969
10970 // Prebuilt APEX.
10971
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000010972 java_sdk_library_import {
Spandan Das5be63332023-12-13 00:06:32 +000010973 name: "framework-foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000010974 public: {
10975 jars: ["foo.jar"],
10976 },
Spandan Das5be63332023-12-13 00:06:32 +000010977 apex_available: ["com.android.foo"],
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000010978 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +000010979 }
10980
10981 prebuilt_bootclasspath_fragment {
10982 name: "foo-bootclasspath-fragment",
10983 contents: ["framework-foo"],
10984 hidden_api: {
10985 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
10986 metadata: "my-bootclasspath-fragment/metadata.csv",
10987 index: "my-bootclasspath-fragment/index.csv",
10988 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
10989 all_flags: "my-bootclasspath-fragment/all-flags.csv",
10990 },
10991 apex_available: [
10992 "com.android.foo",
10993 ],
10994 }
10995
10996 prebuilt_apex {
10997 name: "com.android.foo",
10998 apex_name: "com.android.foo",
10999 src: "com.android.foo-arm.apex",
11000 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11001 }
11002
11003 // Another Prebuilt ART APEX
11004 prebuilt_apex {
11005 name: "com.android.foo.v2",
11006 apex_name: "com.android.foo", // Used to determine the API domain
11007 src: "com.android.foo-arm.apex",
11008 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11009 }
11010
11011 // APEX contribution modules
11012
11013 apex_contributions {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011014 name: "foo.source.contributions",
Spandan Das5be63332023-12-13 00:06:32 +000011015 api_domain: "com.android.foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011016 contents: ["com.android.foo"],
11017 }
11018
11019 apex_contributions {
11020 name: "foo.prebuilt.contributions",
11021 api_domain: "com.android.foo",
11022 contents: ["prebuilt_com.android.foo"],
11023 }
11024
11025 apex_contributions {
11026 name: "foo.prebuilt.v2.contributions",
11027 api_domain: "com.android.foo",
11028 contents: ["com.android.foo.v2"], // prebuilt_ prefix is missing because of prebuilt_rename mutator
Spandan Das5be63332023-12-13 00:06:32 +000011029 }
11030 `
11031
11032 testCases := []struct {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011033 desc string
11034 selectedApexContributions string
11035 expectedBootJar string
Spandan Das5be63332023-12-13 00:06:32 +000011036 }{
11037 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011038 desc: "Source apex com.android.foo is selected, bootjar should come from source java library",
11039 selectedApexContributions: "foo.source.contributions",
11040 expectedBootJar: "out/soong/.intermediates/foo-bootclasspath-fragment/android_common_apex10000/hiddenapi-modular/encoded/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011041 },
11042 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011043 desc: "Prebuilt apex prebuilt_com.android.foo is selected, profile should come from .prof deapexed from the prebuilt",
11044 selectedApexContributions: "foo.prebuilt.contributions",
Spandan Das52c01a12024-09-20 01:09:48 +000011045 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo/android_common_com.android.foo/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011046 },
11047 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011048 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected, profile should come from .prof deapexed from the prebuilt",
11049 selectedApexContributions: "foo.prebuilt.v2.contributions",
Spandan Das52c01a12024-09-20 01:09:48 +000011050 expectedBootJar: "out/soong/.intermediates/com.android.foo.v2/android_common_com.android.foo/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011051 },
11052 }
11053
11054 fragment := java.ApexVariantReference{
11055 Apex: proptools.StringPtr("com.android.foo"),
11056 Module: proptools.StringPtr("foo-bootclasspath-fragment"),
11057 }
11058
11059 for _, tc := range testCases {
11060 preparer := android.GroupFixturePreparers(
11061 java.FixtureConfigureApexBootJars("com.android.foo:framework-foo"),
11062 android.FixtureMergeMockFs(map[string][]byte{
11063 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11064 }),
Spandan Das81fe4d12024-05-15 18:43:47 +000011065 // Make sure that we have atleast one platform library so that we can check the monolithic hiddenapi
11066 // file creation.
11067 java.FixtureConfigureBootJars("platform:foo"),
11068 android.FixtureModifyMockFS(func(fs android.MockFS) {
11069 fs["platform/Android.bp"] = []byte(`
11070 java_library {
11071 name: "foo",
11072 srcs: ["Test.java"],
11073 compile_dex: true,
11074 }
11075 `)
11076 fs["platform/Test.java"] = nil
11077 }),
11078
Colin Crossa66b4632024-08-08 15:50:47 -070011079 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das5be63332023-12-13 00:06:32 +000011080 )
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011081 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das5be63332023-12-13 00:06:32 +000011082 checkBootDexJarPath(t, ctx, "framework-foo", tc.expectedBootJar)
Spandan Das64c9e0c2023-12-20 20:13:34 +000011083 checkBootJarsPackageCheck(t, ctx, tc.expectedBootJar)
11084 checkBootJarsForMonolithicHiddenapi(t, ctx, tc.expectedBootJar)
Spandan Das5be63332023-12-13 00:06:32 +000011085 }
11086}
Spandan Das3576e762024-01-03 18:57:03 +000011087
11088// Test that product packaging installs the selected mainline module (either source or a specific prebuilt)
11089// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11090func TestInstallationRulesForMultipleApexPrebuilts(t *testing.T) {
Spandan Das3576e762024-01-03 18:57:03 +000011091 // for a mainline module family, check that only the flagged soong module is visible to make
11092 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleName string, hiddenModuleNames []string) {
11093 variation := func(moduleName string) string {
11094 ret := "android_common_com.android.foo"
11095 if moduleName == "com.google.android.foo" {
Spandan Das50801e22024-05-13 18:29:45 +000011096 ret = "android_common_com.google.android.foo_com.google.android.foo"
Spandan Das3576e762024-01-03 18:57:03 +000011097 }
11098 return ret
11099 }
11100
11101 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11102 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11103
11104 for _, hiddenModuleName := range hiddenModuleNames {
11105 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11106 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11107
11108 }
11109 }
11110
11111 bp := `
11112 apex_key {
11113 name: "com.android.foo.key",
11114 public_key: "com.android.foo.avbpubkey",
11115 private_key: "com.android.foo.pem",
11116 }
11117
11118 // AOSP source apex
11119 apex {
11120 name: "com.android.foo",
11121 key: "com.android.foo.key",
11122 updatable: false,
11123 }
11124
11125 // Google source apex
11126 override_apex {
11127 name: "com.google.android.foo",
11128 base: "com.android.foo",
11129 key: "com.android.foo.key",
11130 }
11131
11132 // Prebuilt Google APEX.
11133
11134 prebuilt_apex {
11135 name: "com.google.android.foo",
11136 apex_name: "com.android.foo",
11137 src: "com.android.foo-arm.apex",
11138 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11139 }
11140
11141 // Another Prebuilt Google APEX
11142 prebuilt_apex {
11143 name: "com.google.android.foo.v2",
11144 apex_name: "com.android.foo",
Spandan Dasa8e2d612024-07-26 19:24:27 +000011145 source_apex_name: "com.google.android.foo",
Spandan Das3576e762024-01-03 18:57:03 +000011146 src: "com.android.foo-arm.apex",
11147 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11148 }
11149
11150 // APEX contribution modules
11151
11152 apex_contributions {
11153 name: "foo.source.contributions",
11154 api_domain: "com.android.foo",
11155 contents: ["com.google.android.foo"],
11156 }
11157
11158 apex_contributions {
11159 name: "foo.prebuilt.contributions",
11160 api_domain: "com.android.foo",
11161 contents: ["prebuilt_com.google.android.foo"],
11162 }
11163
11164 apex_contributions {
11165 name: "foo.prebuilt.v2.contributions",
11166 api_domain: "com.android.foo",
11167 contents: ["prebuilt_com.google.android.foo.v2"],
11168 }
11169
11170 // This is an incompatible module because it selects multiple versions of the same mainline module
11171 apex_contributions {
11172 name: "foo.prebuilt.duplicate.contributions",
11173 api_domain: "com.android.foo",
11174 contents: [
11175 "prebuilt_com.google.android.foo",
11176 "prebuilt_com.google.android.foo.v2",
11177 ],
11178 }
11179 `
11180
11181 testCases := []struct {
11182 desc string
11183 selectedApexContributions string
11184 expectedVisibleModuleName string
11185 expectedHiddenModuleNames []string
11186 expectedError string
11187 }{
11188 {
11189 desc: "Source apex is selected, prebuilts should be hidden from make",
11190 selectedApexContributions: "foo.source.contributions",
11191 expectedVisibleModuleName: "com.google.android.foo",
11192 expectedHiddenModuleNames: []string{"prebuilt_com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11193 },
11194 {
11195 desc: "Prebuilt apex prebuilt_com.android.foo is selected, source and the other prebuilt should be hidden from make",
11196 selectedApexContributions: "foo.prebuilt.contributions",
11197 expectedVisibleModuleName: "prebuilt_com.google.android.foo",
11198 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11199 },
11200 {
11201 desc: "Prebuilt apex prebuilt_com.android.fooi.v2 is selected, source and the other prebuilt should be hidden from make",
11202 selectedApexContributions: "foo.prebuilt.v2.contributions",
11203 expectedVisibleModuleName: "prebuilt_com.google.android.foo.v2",
11204 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo"},
11205 },
11206 {
11207 desc: "Multiple versions of a prebuilt apex is selected in the same release config",
11208 selectedApexContributions: "foo.prebuilt.duplicate.contributions",
11209 expectedError: "Found duplicate variations of the same module in apex_contributions: prebuilt_com.google.android.foo and prebuilt_com.google.android.foo.v2",
11210 },
11211 }
11212
11213 for _, tc := range testCases {
11214 preparer := android.GroupFixturePreparers(
11215 android.FixtureMergeMockFs(map[string][]byte{
11216 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11217 }),
Colin Crossa66b4632024-08-08 15:50:47 -070011218 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das3576e762024-01-03 18:57:03 +000011219 )
11220 if tc.expectedError != "" {
11221 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(tc.expectedError))
11222 testApex(t, bp, preparer)
11223 return
11224 }
11225 ctx := testApex(t, bp, preparer)
11226
Spandan Das3576e762024-01-03 18:57:03 +000011227 // Check that
11228 // 1. The contents of the selected apex_contributions are visible to make
11229 // 2. The rest of the apexes in the mainline module family (source or other prebuilt) is hidden from make
11230 checkHideFromMake(t, ctx, tc.expectedVisibleModuleName, tc.expectedHiddenModuleNames)
11231 }
11232}
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011233
Spandan Das85bd4622024-08-01 00:51:20 +000011234// Test that product packaging installs the selected mainline module in workspaces withtout source mainline module
11235func TestInstallationRulesForMultipleApexPrebuiltsWithoutSource(t *testing.T) {
11236 // for a mainline module family, check that only the flagged soong module is visible to make
11237 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleNames []string, hiddenModuleNames []string) {
11238 variation := func(moduleName string) string {
11239 ret := "android_common_com.android.adservices"
11240 if moduleName == "com.google.android.foo" {
11241 ret = "android_common_com.google.android.foo_com.google.android.foo"
11242 }
11243 return ret
11244 }
11245
11246 for _, visibleModuleName := range visibleModuleNames {
11247 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11248 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11249 }
11250
11251 for _, hiddenModuleName := range hiddenModuleNames {
11252 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11253 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11254
11255 }
11256 }
11257
11258 bp := `
11259 apex_key {
11260 name: "com.android.adservices.key",
11261 public_key: "com.android.adservices.avbpubkey",
11262 private_key: "com.android.adservices.pem",
11263 }
11264
11265 // AOSP source apex
11266 apex {
11267 name: "com.android.adservices",
11268 key: "com.android.adservices.key",
11269 updatable: false,
11270 }
11271
11272 // Prebuilt Google APEX.
11273
11274 prebuilt_apex {
11275 name: "com.google.android.adservices",
11276 apex_name: "com.android.adservices",
11277 src: "com.android.foo-arm.apex",
11278 }
11279
11280 // Another Prebuilt Google APEX
11281 prebuilt_apex {
11282 name: "com.google.android.adservices.v2",
11283 apex_name: "com.android.adservices",
11284 src: "com.android.foo-arm.apex",
11285 }
11286
11287 // APEX contribution modules
11288
11289
11290 apex_contributions {
11291 name: "adservices.prebuilt.contributions",
11292 api_domain: "com.android.adservices",
11293 contents: ["prebuilt_com.google.android.adservices"],
11294 }
11295
11296 apex_contributions {
11297 name: "adservices.prebuilt.v2.contributions",
11298 api_domain: "com.android.adservices",
11299 contents: ["prebuilt_com.google.android.adservices.v2"],
11300 }
11301 `
11302
11303 testCases := []struct {
11304 desc string
11305 selectedApexContributions string
11306 expectedVisibleModuleNames []string
11307 expectedHiddenModuleNames []string
11308 }{
11309 {
11310 desc: "No apex contributions selected, source aosp apex should be visible, and mainline prebuilts should be hidden",
11311 selectedApexContributions: "",
11312 expectedVisibleModuleNames: []string{"com.android.adservices"},
11313 expectedHiddenModuleNames: []string{"com.google.android.adservices", "com.google.android.adservices.v2"},
11314 },
11315 {
11316 desc: "Prebuilt apex prebuilt_com.android.foo is selected",
11317 selectedApexContributions: "adservices.prebuilt.contributions",
11318 expectedVisibleModuleNames: []string{"com.android.adservices", "com.google.android.adservices"},
11319 expectedHiddenModuleNames: []string{"com.google.android.adservices.v2"},
11320 },
11321 {
11322 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected",
11323 selectedApexContributions: "adservices.prebuilt.v2.contributions",
11324 expectedVisibleModuleNames: []string{"com.android.adservices", "com.google.android.adservices.v2"},
11325 expectedHiddenModuleNames: []string{"com.google.android.adservices"},
11326 },
11327 }
11328
11329 for _, tc := range testCases {
11330 preparer := android.GroupFixturePreparers(
11331 android.FixtureMergeMockFs(map[string][]byte{
11332 "system/sepolicy/apex/com.android.adservices-file_contexts": nil,
11333 }),
Colin Crossa66b4632024-08-08 15:50:47 -070011334 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das85bd4622024-08-01 00:51:20 +000011335 )
11336 ctx := testApex(t, bp, preparer)
11337
11338 checkHideFromMake(t, ctx, tc.expectedVisibleModuleNames, tc.expectedHiddenModuleNames)
11339 }
11340}
11341
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011342func TestAconfifDeclarationsValidation(t *testing.T) {
11343 aconfigDeclarationLibraryString := func(moduleNames []string) (ret string) {
11344 for _, moduleName := range moduleNames {
11345 ret += fmt.Sprintf(`
11346 aconfig_declarations {
11347 name: "%[1]s",
11348 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +000011349 container: "system",
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011350 srcs: [
11351 "%[1]s.aconfig",
11352 ],
11353 }
11354 java_aconfig_library {
11355 name: "%[1]s-lib",
11356 aconfig_declarations: "%[1]s",
11357 }
11358 `, moduleName)
11359 }
11360 return ret
11361 }
11362
11363 result := android.GroupFixturePreparers(
11364 prepareForApexTest,
11365 java.PrepareForTestWithJavaSdkLibraryFiles,
11366 java.FixtureWithLastReleaseApis("foo"),
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011367 ).RunTestWithBp(t, `
11368 java_library {
11369 name: "baz-java-lib",
11370 static_libs: [
11371 "baz-lib",
11372 ],
11373 }
11374 filegroup {
11375 name: "qux-filegroup",
11376 srcs: [
11377 ":qux-lib{.generated_srcjars}",
11378 ],
11379 }
11380 filegroup {
11381 name: "qux-another-filegroup",
11382 srcs: [
11383 ":qux-filegroup",
11384 ],
11385 }
11386 java_library {
11387 name: "quux-java-lib",
11388 srcs: [
11389 "a.java",
11390 ],
11391 libs: [
11392 "quux-lib",
11393 ],
11394 }
11395 java_sdk_library {
11396 name: "foo",
11397 srcs: [
11398 ":qux-another-filegroup",
11399 ],
11400 api_packages: ["foo"],
11401 system: {
11402 enabled: true,
11403 },
11404 module_lib: {
11405 enabled: true,
11406 },
11407 test: {
11408 enabled: true,
11409 },
11410 static_libs: [
11411 "bar-lib",
11412 ],
11413 libs: [
11414 "baz-java-lib",
11415 "quux-java-lib",
11416 ],
11417 aconfig_declarations: [
11418 "bar",
11419 ],
11420 }
11421 `+aconfigDeclarationLibraryString([]string{"bar", "baz", "qux", "quux"}))
11422
11423 m := result.ModuleForTests("foo.stubs.source", "android_common")
11424 outDir := "out/soong/.intermediates"
11425
11426 // Arguments passed to aconfig to retrieve the state of the flags defined in the
11427 // textproto files
11428 aconfigFlagArgs := m.Output("released-flagged-apis-exportable.txt").Args["flags_path"]
11429
11430 // "bar-lib" is a static_lib of "foo" and is passed to metalava as classpath. Thus the
11431 // cache file provided by the associated aconfig_declarations module "bar" should be passed
11432 // to aconfig.
11433 android.AssertStringDoesContain(t, "cache file of a java_aconfig_library static_lib "+
11434 "passed as an input",
11435 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "bar"))
11436
11437 // "baz-java-lib", which statically depends on "baz-lib", is a lib of "foo" and is passed
11438 // to metalava as classpath. Thus the cache file provided by the associated
11439 // aconfig_declarations module "baz" should be passed to aconfig.
11440 android.AssertStringDoesContain(t, "cache file of a lib that statically depends on "+
11441 "java_aconfig_library passed as an input",
11442 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "baz"))
11443
11444 // "qux-lib" is passed to metalava as src via the filegroup, thus the cache file provided by
11445 // the associated aconfig_declarations module "qux" should be passed to aconfig.
11446 android.AssertStringDoesContain(t, "cache file of srcs java_aconfig_library passed as an "+
11447 "input",
11448 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "qux"))
11449
11450 // "quux-java-lib" is a lib of "foo" and is passed to metalava as classpath, but does not
11451 // statically depend on "quux-lib". Therefore, the cache file provided by the associated
11452 // aconfig_declarations module "quux" should not be passed to aconfig.
11453 android.AssertStringDoesNotContain(t, "cache file of a lib that does not statically "+
11454 "depend on java_aconfig_library not passed as an input",
11455 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "quux"))
11456}
Cole Faust7c991b42024-05-15 11:17:55 -070011457
11458func TestMultiplePrebuiltsWithSameBase(t *testing.T) {
11459 ctx := testApex(t, `
11460 apex {
11461 name: "myapex",
11462 key: "myapex.key",
11463 prebuilts: ["myetc", "myetc2"],
11464 min_sdk_version: "29",
11465 }
11466 apex_key {
11467 name: "myapex.key",
11468 public_key: "testkey.avbpubkey",
11469 private_key: "testkey.pem",
11470 }
11471
11472 prebuilt_etc {
11473 name: "myetc",
11474 src: "myprebuilt",
11475 filename: "myfilename",
11476 }
11477 prebuilt_etc {
11478 name: "myetc2",
11479 sub_dir: "mysubdir",
11480 src: "myprebuilt",
11481 filename: "myfilename",
11482 }
11483 `, withFiles(android.MockFS{
11484 "packages/modules/common/build/allowed_deps.txt": nil,
11485 }))
11486
11487 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
11488 data := android.AndroidMkDataForTest(t, ctx, ab)
11489 var builder strings.Builder
11490 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
11491 androidMk := builder.String()
11492
11493 android.AssertStringDoesContain(t, "not found", androidMk, "LOCAL_MODULE := etc_myfilename.myapex")
11494 android.AssertStringDoesContain(t, "not found", androidMk, "LOCAL_MODULE := etc_mysubdir_myfilename.myapex")
11495}
Spandan Das50801e22024-05-13 18:29:45 +000011496
11497func TestApexMinSdkVersionOverride(t *testing.T) {
11498 checkMinSdkVersion := func(t *testing.T, module android.TestingModule, expectedMinSdkVersion string) {
11499 args := module.Rule("apexRule").Args
11500 optFlags := args["opt_flags"]
11501 if !strings.Contains(optFlags, "--min_sdk_version "+expectedMinSdkVersion) {
11502 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", module.Module(), expectedMinSdkVersion, optFlags)
11503 }
11504 }
11505
11506 checkHasDep := func(t *testing.T, ctx *android.TestContext, m android.Module, wantDep android.Module) {
11507 t.Helper()
11508 found := false
11509 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
11510 if dep == wantDep {
11511 found = true
11512 }
11513 })
11514 if !found {
11515 t.Errorf("Could not find a dependency from %v to %v\n", m, wantDep)
11516 }
11517 }
11518
11519 ctx := testApex(t, `
11520 apex {
11521 name: "com.android.apex30",
11522 min_sdk_version: "30",
11523 key: "apex30.key",
11524 java_libs: ["javalib"],
11525 }
11526
11527 java_library {
11528 name: "javalib",
11529 srcs: ["A.java"],
11530 apex_available: ["com.android.apex30"],
11531 min_sdk_version: "30",
11532 sdk_version: "current",
11533 }
11534
11535 override_apex {
11536 name: "com.mycompany.android.apex30",
11537 base: "com.android.apex30",
11538 }
11539
11540 override_apex {
11541 name: "com.mycompany.android.apex31",
11542 base: "com.android.apex30",
11543 min_sdk_version: "31",
11544 }
11545
11546 apex_key {
11547 name: "apex30.key",
11548 public_key: "testkey.avbpubkey",
11549 private_key: "testkey.pem",
11550 }
11551
11552 `, android.FixtureMergeMockFs(android.MockFS{
11553 "system/sepolicy/apex/com.android.apex30-file_contexts": nil,
11554 }),
11555 )
11556
11557 baseModule := ctx.ModuleForTests("com.android.apex30", "android_common_com.android.apex30")
11558 checkMinSdkVersion(t, baseModule, "30")
11559
11560 // Override module, but uses same min_sdk_version
11561 overridingModuleSameMinSdkVersion := ctx.ModuleForTests("com.android.apex30", "android_common_com.mycompany.android.apex30_com.mycompany.android.apex30")
11562 javalibApex30Variant := ctx.ModuleForTests("javalib", "android_common_apex30")
11563 checkMinSdkVersion(t, overridingModuleSameMinSdkVersion, "30")
11564 checkHasDep(t, ctx, overridingModuleSameMinSdkVersion.Module(), javalibApex30Variant.Module())
11565
11566 // Override module, uses different min_sdk_version
11567 overridingModuleDifferentMinSdkVersion := ctx.ModuleForTests("com.android.apex30", "android_common_com.mycompany.android.apex31_com.mycompany.android.apex31")
11568 javalibApex31Variant := ctx.ModuleForTests("javalib", "android_common_apex31")
11569 checkMinSdkVersion(t, overridingModuleDifferentMinSdkVersion, "31")
11570 checkHasDep(t, ctx, overridingModuleDifferentMinSdkVersion.Module(), javalibApex31Variant.Module())
11571}
Spandan Das0b28fa02024-05-28 23:40:17 +000011572
11573func TestOverrideApexWithPrebuiltApexPreferred(t *testing.T) {
11574 context := android.GroupFixturePreparers(
11575 android.PrepareForIntegrationTestWithAndroid,
11576 PrepareForTestWithApexBuildComponents,
11577 android.FixtureMergeMockFs(android.MockFS{
11578 "system/sepolicy/apex/foo-file_contexts": nil,
11579 }),
11580 )
11581 res := context.RunTestWithBp(t, `
11582 apex {
11583 name: "foo",
11584 key: "myapex.key",
11585 apex_available_name: "com.android.foo",
11586 variant_version: "0",
11587 updatable: false,
11588 }
11589 apex_key {
11590 name: "myapex.key",
11591 public_key: "testkey.avbpubkey",
11592 private_key: "testkey.pem",
11593 }
11594 prebuilt_apex {
11595 name: "foo",
11596 src: "foo.apex",
11597 prefer: true,
11598 }
11599 override_apex {
11600 name: "myoverrideapex",
11601 base: "foo",
11602 }
11603 `)
11604
11605 java.CheckModuleHasDependency(t, res.TestContext, "myoverrideapex", "android_common_myoverrideapex_myoverrideapex", "foo")
11606}
Spandan Dasca1d63e2024-07-01 22:53:49 +000011607
11608func TestUpdatableApexMinSdkVersionCurrent(t *testing.T) {
11609 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`, `
11610 apex {
11611 name: "myapex",
11612 key: "myapex.key",
11613 updatable: true,
11614 min_sdk_version: "current",
11615 }
11616
11617 apex_key {
11618 name: "myapex.key",
11619 public_key: "testkey.avbpubkey",
11620 private_key: "testkey.pem",
11621 }
11622 `)
11623}
Spandan Das2f68f192024-07-22 19:25:50 +000011624
11625func TestPrebuiltStubNoinstall(t *testing.T) {
11626 testFunc := func(t *testing.T, expectLibfooOnSystemLib bool, fs android.MockFS) {
11627 result := android.GroupFixturePreparers(
11628 prepareForApexTest,
11629 android.PrepareForTestWithAndroidMk,
11630 android.PrepareForTestWithMakevars,
11631 android.FixtureMergeMockFs(fs),
11632 ).RunTest(t)
11633
11634 ldRule := result.ModuleForTests("installedlib", "android_arm64_armv8-a_shared").Rule("ld")
Spandan Das357ffcc2024-07-24 18:07:48 +000011635 android.AssertStringDoesContain(t, "", ldRule.Args["libFlags"], "android_arm64_armv8-a_shared_current/libfoo.so")
Spandan Das2f68f192024-07-22 19:25:50 +000011636
11637 installRules := result.InstallMakeRulesForTesting(t)
11638
11639 var installedlibRule *android.InstallMakeRule
11640 for i, rule := range installRules {
11641 if rule.Target == "out/target/product/test_device/system/lib/installedlib.so" {
11642 if installedlibRule != nil {
11643 t.Errorf("Duplicate install rules for %s", rule.Target)
11644 }
11645 installedlibRule = &installRules[i]
11646 }
11647 }
11648 if installedlibRule == nil {
11649 t.Errorf("No install rule found for installedlib")
11650 return
11651 }
11652
11653 if expectLibfooOnSystemLib {
11654 android.AssertStringListContains(t,
11655 "installedlib doesn't have install dependency on libfoo impl",
11656 installedlibRule.OrderOnlyDeps,
11657 "out/target/product/test_device/system/lib/libfoo.so")
11658 } else {
11659 android.AssertStringListDoesNotContain(t,
11660 "installedlib has install dependency on libfoo stub",
11661 installedlibRule.Deps,
11662 "out/target/product/test_device/system/lib/libfoo.so")
11663 android.AssertStringListDoesNotContain(t,
11664 "installedlib has order-only install dependency on libfoo stub",
11665 installedlibRule.OrderOnlyDeps,
11666 "out/target/product/test_device/system/lib/libfoo.so")
11667 }
11668 }
11669
11670 prebuiltLibfooBp := []byte(`
11671 cc_prebuilt_library {
11672 name: "libfoo",
11673 prefer: true,
11674 srcs: ["libfoo.so"],
11675 stubs: {
11676 versions: ["1"],
11677 },
11678 apex_available: ["apexfoo"],
11679 }
11680 `)
11681
11682 apexfooBp := []byte(`
11683 apex {
11684 name: "apexfoo",
11685 key: "apexfoo.key",
11686 native_shared_libs: ["libfoo"],
11687 updatable: false,
11688 compile_multilib: "both",
11689 }
11690 apex_key {
11691 name: "apexfoo.key",
11692 public_key: "testkey.avbpubkey",
11693 private_key: "testkey.pem",
11694 }
11695 `)
11696
11697 installedlibBp := []byte(`
11698 cc_library {
11699 name: "installedlib",
11700 shared_libs: ["libfoo"],
11701 }
11702 `)
11703
11704 t.Run("prebuilt stub (without source): no install", func(t *testing.T) {
11705 testFunc(
11706 t,
11707 /*expectLibfooOnSystemLib=*/ false,
11708 android.MockFS{
11709 "prebuilts/module_sdk/art/current/Android.bp": prebuiltLibfooBp,
11710 "apexfoo/Android.bp": apexfooBp,
11711 "system/sepolicy/apex/apexfoo-file_contexts": nil,
11712 "Android.bp": installedlibBp,
11713 },
11714 )
11715 })
11716
11717 disabledSourceLibfooBp := []byte(`
11718 cc_library {
11719 name: "libfoo",
11720 enabled: false,
11721 stubs: {
11722 versions: ["1"],
11723 },
11724 apex_available: ["apexfoo"],
11725 }
11726 `)
11727
11728 t.Run("prebuilt stub (with disabled source): no install", func(t *testing.T) {
11729 testFunc(
11730 t,
11731 /*expectLibfooOnSystemLib=*/ false,
11732 android.MockFS{
11733 "prebuilts/module_sdk/art/current/Android.bp": prebuiltLibfooBp,
11734 "impl/Android.bp": disabledSourceLibfooBp,
11735 "apexfoo/Android.bp": apexfooBp,
11736 "system/sepolicy/apex/apexfoo-file_contexts": nil,
11737 "Android.bp": installedlibBp,
11738 },
11739 )
11740 })
11741}
Jihoon Kange246bb72024-09-18 22:26:22 +000011742
11743func TestSdkLibraryTransitiveClassLoaderContext(t *testing.T) {
11744 // This test case tests that listing the impl lib instead of the top level java_sdk_library
11745 // in libs of android_app and java_library does not lead to class loader context device/host
11746 // path mismatch errors.
11747 android.GroupFixturePreparers(
11748 prepareForApexTest,
11749 android.PrepareForIntegrationTestWithAndroid,
11750 PrepareForTestWithApexBuildComponents,
11751 android.FixtureModifyEnv(func(env map[string]string) {
11752 env["DISABLE_CONTAINER_CHECK"] = "true"
11753 }),
11754 withFiles(filesForSdkLibrary),
11755 android.FixtureMergeMockFs(android.MockFS{
11756 "system/sepolicy/apex/com.android.foo30-file_contexts": nil,
11757 }),
11758 ).RunTestWithBp(t, `
11759 apex {
11760 name: "com.android.foo30",
11761 key: "myapex.key",
11762 updatable: true,
11763 bootclasspath_fragments: [
11764 "foo-bootclasspath-fragment",
11765 ],
11766 java_libs: [
11767 "bar",
11768 ],
11769 apps: [
11770 "bar-app",
11771 ],
11772 min_sdk_version: "30",
11773 }
11774 apex_key {
11775 name: "myapex.key",
11776 public_key: "testkey.avbpubkey",
11777 private_key: "testkey.pem",
11778 }
11779 bootclasspath_fragment {
11780 name: "foo-bootclasspath-fragment",
11781 contents: [
11782 "framework-foo",
11783 ],
11784 apex_available: [
11785 "com.android.foo30",
11786 ],
11787 hidden_api: {
11788 split_packages: ["*"]
11789 },
11790 }
11791
11792 java_sdk_library {
11793 name: "framework-foo",
11794 srcs: [
11795 "A.java"
11796 ],
11797 unsafe_ignore_missing_latest_api: true,
11798 apex_available: [
11799 "com.android.foo30",
11800 ],
11801 compile_dex: true,
11802 sdk_version: "core_current",
11803 shared_library: false,
11804 }
11805
11806 java_library {
11807 name: "bar",
11808 srcs: [
11809 "A.java"
11810 ],
11811 libs: [
11812 "framework-foo.impl",
11813 ],
11814 apex_available: [
11815 "com.android.foo30",
11816 ],
11817 sdk_version: "core_current",
11818 }
11819
11820 java_library {
11821 name: "baz",
11822 srcs: [
11823 "A.java"
11824 ],
11825 libs: [
11826 "bar",
11827 ],
11828 sdk_version: "core_current",
11829 }
11830
11831 android_app {
11832 name: "bar-app",
11833 srcs: [
11834 "A.java"
11835 ],
11836 libs: [
11837 "baz",
11838 "framework-foo.impl",
11839 ],
11840 apex_available: [
11841 "com.android.foo30",
11842 ],
11843 sdk_version: "core_current",
11844 min_sdk_version: "30",
11845 manifest: "AndroidManifest.xml",
11846 }
11847 `)
11848}