blob: 041c903334dcbca522c2b87a86019ee4a41122fb [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) {
270 ok, err := regexp.MatchString(expectedRex, result)
271 if err != nil {
272 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
273 return
274 }
275 if !ok {
276 t.Errorf("%s does not match regular expession %s", result, expectedRex)
277 }
278}
279
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900281 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900282 if !android.InList(expected, result) {
283 t.Errorf("%q is not found in %v", expected, result)
284 }
285}
286
287func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900288 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900289 if android.InList(notExpected, result) {
290 t.Errorf("%q is found in %v", notExpected, result)
291 }
292}
293
Jooyung Hane1633032019-08-01 17:41:43 +0900294func ensureListEmpty(t *testing.T, result []string) {
295 t.Helper()
296 if len(result) > 0 {
297 t.Errorf("%q is expected to be empty", result)
298 }
299}
300
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000301func ensureListNotEmpty(t *testing.T, result []string) {
302 t.Helper()
303 if len(result) == 0 {
304 t.Errorf("%q is expected to be not empty", result)
305 }
306}
307
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308// Minimal test
309func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800310 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900311 apex_defaults {
312 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900313 manifest: ":myapex.manifest",
314 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900316 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900317 native_shared_libs: [
318 "mylib",
319 "libfoo.ffi",
320 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900321 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800322 multilib: {
323 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900324 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800325 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900326 },
Jiyong Park77acec62020-06-01 21:39:15 +0900327 java_libs: [
328 "myjar",
329 "myjar_dex",
330 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000331 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900332 }
333
Jiyong Park30ca9372019-02-07 16:27:23 +0900334 apex {
335 name: "myapex",
336 defaults: ["myapex-defaults"],
337 }
338
Jiyong Park25fc6a92018-11-18 18:02:45 +0900339 apex_key {
340 name: "myapex.key",
341 public_key: "testkey.avbpubkey",
342 private_key: "testkey.pem",
343 }
344
Jiyong Park809bb722019-02-13 21:33:49 +0900345 filegroup {
346 name: "myapex.manifest",
347 srcs: ["apex_manifest.json"],
348 }
349
350 filegroup {
351 name: "myapex.androidmanifest",
352 srcs: ["AndroidManifest.xml"],
353 }
354
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355 cc_library {
356 name: "mylib",
357 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900358 shared_libs: [
359 "mylib2",
360 "libbar.ffi",
361 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362 system_shared_libs: [],
363 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000364 // TODO: remove //apex_available:platform
365 apex_available: [
366 "//apex_available:platform",
367 "myapex",
368 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900369 }
370
Alex Light3d673592019-01-18 14:37:31 -0800371 cc_binary {
372 name: "foo",
373 srcs: ["mylib.cpp"],
374 compile_multilib: "both",
375 multilib: {
376 lib32: {
377 suffix: "32",
378 },
379 lib64: {
380 suffix: "64",
381 },
382 },
383 symlinks: ["foo_link_"],
384 symlink_preferred_arch: true,
385 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800386 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700387 apex_available: [ "myapex", "com.android.gki.*" ],
388 }
389
Jiyong Park99644e92020-11-17 22:21:02 +0900390 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000391 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900392 srcs: ["foo.rs"],
393 rlibs: ["libfoo.rlib.rust"],
Vinh Tran4eeb2a92023-08-14 13:29:30 -0400394 rustlibs: ["libfoo.dylib.rust"],
Jiyong Park99644e92020-11-17 22:21:02 +0900395 apex_available: ["myapex"],
396 }
397
398 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000399 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900400 srcs: ["foo.rs"],
401 crate_name: "foo",
402 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900403 shared_libs: ["libfoo.shared_from_rust"],
404 }
405
406 cc_library_shared {
407 name: "libfoo.shared_from_rust",
408 srcs: ["mylib.cpp"],
409 system_shared_libs: [],
410 stl: "none",
411 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900412 }
413
414 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000415 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900416 srcs: ["foo.rs"],
417 crate_name: "foo",
418 apex_available: ["myapex"],
419 }
420
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900421 rust_ffi_shared {
422 name: "libfoo.ffi",
423 srcs: ["foo.rs"],
424 crate_name: "foo",
425 apex_available: ["myapex"],
426 }
427
428 rust_ffi_shared {
429 name: "libbar.ffi",
430 srcs: ["foo.rs"],
431 crate_name: "bar",
432 apex_available: ["myapex"],
433 }
434
Yifan Hongd22a84a2020-07-28 17:37:46 -0700435 apex {
436 name: "com.android.gki.fake",
437 binaries: ["foo"],
438 key: "myapex.key",
439 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000440 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800441 }
442
Paul Duffindddd5462020-04-07 15:25:44 +0100443 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900444 name: "mylib2",
445 srcs: ["mylib.cpp"],
446 system_shared_libs: [],
447 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900448 static_libs: ["libstatic"],
449 // TODO: remove //apex_available:platform
450 apex_available: [
451 "//apex_available:platform",
452 "myapex",
453 ],
454 }
455
Paul Duffindddd5462020-04-07 15:25:44 +0100456 cc_prebuilt_library_shared {
457 name: "mylib2",
458 srcs: ["prebuilt.so"],
459 // TODO: remove //apex_available:platform
460 apex_available: [
461 "//apex_available:platform",
462 "myapex",
463 ],
464 }
465
Jiyong Park9918e1a2020-03-17 19:16:40 +0900466 cc_library_static {
467 name: "libstatic",
468 srcs: ["mylib.cpp"],
469 system_shared_libs: [],
470 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000471 // TODO: remove //apex_available:platform
472 apex_available: [
473 "//apex_available:platform",
474 "myapex",
475 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900476 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900477
478 java_library {
479 name: "myjar",
480 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900481 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 sdk_version: "none",
483 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900485 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000486 // TODO: remove //apex_available:platform
487 apex_available: [
488 "//apex_available:platform",
489 "myapex",
490 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900491 }
492
Jiyong Park77acec62020-06-01 21:39:15 +0900493 dex_import {
494 name: "myjar_dex",
495 jars: ["prebuilt.jar"],
496 apex_available: [
497 "//apex_available:platform",
498 "myapex",
499 ],
500 }
501
Jiyong Park7f7766d2019-07-25 22:02:35 +0900502 java_library {
503 name: "myotherjar",
504 srcs: ["foo/bar/MyClass.java"],
505 sdk_version: "none",
506 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900507 // TODO: remove //apex_available:platform
508 apex_available: [
509 "//apex_available:platform",
510 "myapex",
511 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900512 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900513
514 java_library {
515 name: "mysharedjar",
516 srcs: ["foo/bar/MyClass.java"],
517 sdk_version: "none",
518 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900519 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900520 `)
521
Jooyung Hana0503a52023-08-23 13:12:50 +0900522 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900523
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900524 // Make sure that Android.mk is created
Jooyung Hana0503a52023-08-23 13:12:50 +0900525 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700526 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900527 var builder strings.Builder
528 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
529
530 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000531 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900532 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
533
Jiyong Park42cca6c2019-04-01 11:15:50 +0900534 optFlags := apexRule.Args["opt_flags"]
535 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700536 // Ensure that the NOTICE output is being packaged as an asset.
Jooyung Hana0503a52023-08-23 13:12:50 +0900537 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900538
Jiyong Park25fc6a92018-11-18 18:02:45 +0900539 copyCmds := apexRule.Args["copy_commands"]
540
541 // Ensure that main rule creates an output
542 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
543
544 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700545 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
546 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
547 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900548 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900549 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900550
551 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700552 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
553 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900554 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900556 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900557 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900558
559 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800560 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
561 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900562 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900563 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900564 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
566 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900567 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900568 // .. but not for java libs
569 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900570 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800571
Colin Cross7113d202019-11-20 16:39:12 -0800572 // Ensure that the platform variant ends with _shared or _common
573 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
574 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900575 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
576 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900577 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
578
579 // Ensure that dynamic dependency to java libs are not included
580 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800581
582 // Ensure that all symlinks are present.
583 found_foo_link_64 := false
584 found_foo := false
585 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900586 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800587 if strings.HasSuffix(cmd, "bin/foo") {
588 found_foo = true
589 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
590 found_foo_link_64 = true
591 }
592 }
593 }
594 good := found_foo && found_foo_link_64
595 if !good {
596 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
597 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900598
Colin Crossf61d03d2023-11-02 16:56:39 -0700599 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
600 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100601 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100602 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
603 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
604 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100605
Colin Crossf61d03d2023-11-02 16:56:39 -0700606 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
607 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100608 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100609 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
610 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
611 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800612}
613
Jooyung Hanf21c7972019-12-16 22:32:06 +0900614func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800615 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900616 apex_defaults {
617 name: "myapex-defaults",
618 key: "myapex.key",
619 prebuilts: ["myetc"],
620 native_shared_libs: ["mylib"],
621 java_libs: ["myjar"],
622 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900623 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800624 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000625 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900626 }
627
628 prebuilt_etc {
629 name: "myetc",
630 src: "myprebuilt",
631 }
632
633 apex {
634 name: "myapex",
635 defaults: ["myapex-defaults"],
636 }
637
638 apex_key {
639 name: "myapex.key",
640 public_key: "testkey.avbpubkey",
641 private_key: "testkey.pem",
642 }
643
644 cc_library {
645 name: "mylib",
646 system_shared_libs: [],
647 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000648 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900649 }
650
651 java_library {
652 name: "myjar",
653 srcs: ["foo/bar/MyClass.java"],
654 sdk_version: "none",
655 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000656 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900657 }
658
659 android_app {
660 name: "AppFoo",
661 srcs: ["foo/bar/MyClass.java"],
662 sdk_version: "none",
663 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000664 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900665 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900666
667 runtime_resource_overlay {
668 name: "rro",
669 theme: "blue",
670 }
671
markchien2f59ec92020-09-02 16:23:38 +0800672 bpf {
673 name: "bpf",
674 srcs: ["bpf.c", "bpf2.c"],
675 }
676
Ken Chenfad7f9d2021-11-10 22:02:57 +0800677 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800678 name: "netdTest",
679 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800680 sub_dir: "netd",
681 }
682
Jooyung Hanf21c7972019-12-16 22:32:06 +0900683 `)
Jooyung Hana0503a52023-08-23 13:12:50 +0900684 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900685 "etc/myetc",
686 "javalib/myjar.jar",
687 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000688 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900689 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800690 "etc/bpf/bpf.o",
691 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800692 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900693 })
694}
695
Jooyung Han01a3ee22019-11-02 02:52:25 +0900696func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800697 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900698 apex {
699 name: "myapex",
700 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000701 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900702 }
703
704 apex_key {
705 name: "myapex.key",
706 public_key: "testkey.avbpubkey",
707 private_key: "testkey.pem",
708 }
709 `)
710
Jooyung Hana0503a52023-08-23 13:12:50 +0900711 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +0900712 args := module.Rule("apexRule").Args
713 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
714 t.Error("manifest should be apex_manifest.pb, but " + manifest)
715 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900716}
717
Liz Kammer4854a7d2021-05-27 14:28:27 -0400718func TestApexManifestMinSdkVersion(t *testing.T) {
719 ctx := testApex(t, `
720 apex_defaults {
721 name: "my_defaults",
722 key: "myapex.key",
723 product_specific: true,
724 file_contexts: ":my-file-contexts",
725 updatable: false,
726 }
727 apex {
728 name: "myapex_30",
729 min_sdk_version: "30",
730 defaults: ["my_defaults"],
731 }
732
733 apex {
734 name: "myapex_current",
735 min_sdk_version: "current",
736 defaults: ["my_defaults"],
737 }
738
739 apex {
740 name: "myapex_none",
741 defaults: ["my_defaults"],
742 }
743
744 apex_key {
745 name: "myapex.key",
746 public_key: "testkey.avbpubkey",
747 private_key: "testkey.pem",
748 }
749
750 filegroup {
751 name: "my-file-contexts",
752 srcs: ["product_specific_file_contexts"],
753 }
754 `, withFiles(map[string][]byte{
755 "product_specific_file_contexts": nil,
756 }), android.FixtureModifyProductVariables(
757 func(variables android.FixtureProductVariables) {
758 variables.Unbundled_build = proptools.BoolPtr(true)
759 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
760 }), android.FixtureMergeEnv(map[string]string{
761 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
762 }))
763
764 testCases := []struct {
765 module string
766 minSdkVersion string
767 }{
768 {
769 module: "myapex_30",
770 minSdkVersion: "30",
771 },
772 {
773 module: "myapex_current",
774 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
775 },
776 {
777 module: "myapex_none",
778 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
779 },
780 }
781 for _, tc := range testCases {
Jooyung Hana0503a52023-08-23 13:12:50 +0900782 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
Liz Kammer4854a7d2021-05-27 14:28:27 -0400783 args := module.Rule("apexRule").Args
784 optFlags := args["opt_flags"]
785 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
786 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
787 }
788 }
789}
790
Jihoon Kang842b9992024-02-08 01:41:51 +0000791func TestApexWithDessertSha(t *testing.T) {
792 ctx := testApex(t, `
793 apex_defaults {
794 name: "my_defaults",
795 key: "myapex.key",
796 product_specific: true,
797 file_contexts: ":my-file-contexts",
798 updatable: false,
799 }
800 apex {
801 name: "myapex_30",
802 min_sdk_version: "30",
803 defaults: ["my_defaults"],
804 }
805
806 apex {
807 name: "myapex_current",
808 min_sdk_version: "current",
809 defaults: ["my_defaults"],
810 }
811
812 apex {
813 name: "myapex_none",
814 defaults: ["my_defaults"],
815 }
816
817 apex_key {
818 name: "myapex.key",
819 public_key: "testkey.avbpubkey",
820 private_key: "testkey.pem",
821 }
822
823 filegroup {
824 name: "my-file-contexts",
825 srcs: ["product_specific_file_contexts"],
826 }
827 `, withFiles(map[string][]byte{
828 "product_specific_file_contexts": nil,
829 }), android.FixtureModifyProductVariables(
830 func(variables android.FixtureProductVariables) {
831 variables.Unbundled_build = proptools.BoolPtr(true)
832 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
833 }), android.FixtureMergeEnv(map[string]string{
834 "UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA": "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
835 }))
836
837 testCases := []struct {
838 module string
839 minSdkVersion string
840 }{
841 {
842 module: "myapex_30",
843 minSdkVersion: "30",
844 },
845 {
846 module: "myapex_current",
847 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
848 },
849 {
850 module: "myapex_none",
851 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
852 },
853 }
854 for _, tc := range testCases {
855 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
856 args := module.Rule("apexRule").Args
857 optFlags := args["opt_flags"]
858 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
859 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
860 }
861 }
862}
863
Jooyung Hanaf730952023-02-28 14:13:38 +0900864func TestFileContexts(t *testing.T) {
Jooyung Hanbe953902023-05-31 16:42:16 +0900865 for _, vendor := range []bool{true, false} {
Jooyung Hanaf730952023-02-28 14:13:38 +0900866 prop := ""
Jooyung Hanbe953902023-05-31 16:42:16 +0900867 if vendor {
868 prop = "vendor: true,\n"
Jooyung Hanaf730952023-02-28 14:13:38 +0900869 }
870 ctx := testApex(t, `
871 apex {
872 name: "myapex",
873 key: "myapex.key",
Jooyung Hanaf730952023-02-28 14:13:38 +0900874 updatable: false,
Jooyung Hanaf730952023-02-28 14:13:38 +0900875 `+prop+`
876 }
877
878 apex_key {
879 name: "myapex.key",
880 public_key: "testkey.avbpubkey",
881 private_key: "testkey.pem",
882 }
Jooyung Hanbe953902023-05-31 16:42:16 +0900883 `)
Jooyung Hanaf730952023-02-28 14:13:38 +0900884
Jooyung Hana0503a52023-08-23 13:12:50 +0900885 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("file_contexts")
Jooyung Hanbe953902023-05-31 16:42:16 +0900886 if vendor {
887 android.AssertStringDoesContain(t, "should force-label as vendor_apex_metadata_file",
888 rule.RuleParams.Command,
889 "apex_manifest\\\\.pb u:object_r:vendor_apex_metadata_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900890 } else {
Jooyung Hanbe953902023-05-31 16:42:16 +0900891 android.AssertStringDoesContain(t, "should force-label as system_file",
892 rule.RuleParams.Command,
893 "apex_manifest\\\\.pb u:object_r:system_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900894 }
895 }
896}
897
Jiyong Park25fc6a92018-11-18 18:02:45 +0900898func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800899 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900900 apex {
901 name: "myapex",
902 key: "myapex.key",
903 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900904 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000905 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900906 }
907
908 apex_key {
909 name: "myapex.key",
910 public_key: "testkey.avbpubkey",
911 private_key: "testkey.pem",
912 }
913
914 cc_library {
915 name: "mylib",
916 srcs: ["mylib.cpp"],
Spandan Das357ffcc2024-07-24 18:07:48 +0000917 shared_libs: ["mylib2", "mylib3", "my_prebuilt_platform_lib", "my_prebuilt_platform_stub_only_lib"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900918 system_shared_libs: [],
919 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000920 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900921 }
922
923 cc_library {
924 name: "mylib2",
925 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900926 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927 system_shared_libs: [],
928 stl: "none",
929 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +0000930 symbol_file: "mylib2.map.txt",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900931 versions: ["1", "2", "3"],
932 },
933 }
934
935 cc_library {
936 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900937 srcs: ["mylib.cpp"],
938 shared_libs: ["mylib4"],
939 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900940 stl: "none",
941 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +0000942 symbol_file: "mylib3.map.txt",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900943 versions: ["10", "11", "12"],
944 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000945 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900946 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900947
948 cc_library {
949 name: "mylib4",
950 srcs: ["mylib.cpp"],
951 system_shared_libs: [],
952 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000953 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900954 }
Jiyong Park105dc322021-06-11 17:22:09 +0900955
Spandan Das357ffcc2024-07-24 18:07:48 +0000956 cc_prebuilt_library_shared {
957 name: "my_prebuilt_platform_lib",
958 stubs: {
959 symbol_file: "my_prebuilt_platform_lib.map.txt",
960 versions: ["1", "2", "3"],
961 },
962 srcs: ["foo.so"],
963 }
964
965 // Similar to my_prebuilt_platform_lib, but this library only provides stubs, i.e. srcs is empty
966 cc_prebuilt_library_shared {
967 name: "my_prebuilt_platform_stub_only_lib",
968 stubs: {
969 symbol_file: "my_prebuilt_platform_stub_only_lib.map.txt",
970 versions: ["1", "2", "3"],
971 }
972 }
973
Jiyong Park105dc322021-06-11 17:22:09 +0900974 rust_binary {
975 name: "foo.rust",
976 srcs: ["foo.rs"],
977 shared_libs: ["libfoo.shared_from_rust"],
978 prefer_rlib: true,
979 apex_available: ["myapex"],
980 }
981
982 cc_library_shared {
983 name: "libfoo.shared_from_rust",
984 srcs: ["mylib.cpp"],
985 system_shared_libs: [],
986 stl: "none",
987 stubs: {
988 versions: ["10", "11", "12"],
989 },
990 }
991
Jiyong Park25fc6a92018-11-18 18:02:45 +0900992 `)
993
Jooyung Hana0503a52023-08-23 13:12:50 +0900994 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900995 copyCmds := apexRule.Args["copy_commands"]
996
997 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800998 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900999
1000 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -08001001 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001002
1003 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -08001004 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001005
Colin Crossaede88c2020-08-11 12:17:01 -07001006 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001007
1008 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001009 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +09001011 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001012
1013 // 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 -07001014 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001015 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -07001016 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +09001017
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -07001018 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
1019 // is replaced by sharing of "cFlags" in cc/builder.go.
1020 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
1021 // module variable representing "cflags". So it was not detected by ensureNotContains.
1022 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
1023 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
1024 // including the original cflags's "-include mylib.h".
1025 //
Jiyong Park64379952018-12-13 18:37:29 +09001026 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -07001027 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1028 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +09001029
Jiyong Park85cc35a2022-07-17 11:30:47 +09001030 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1031 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
1032 // Ensure that genstub for apex-provided lib is invoked with --apex
1033 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +09001034
Jooyung Hana0503a52023-08-23 13:12:50 +09001035 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +09001036 "lib64/mylib.so",
1037 "lib64/mylib3.so",
1038 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +09001039 "bin/foo.rust",
1040 "lib64/libc++.so", // by the implicit dependency from foo.rust
1041 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +09001042 })
Jiyong Park105dc322021-06-11 17:22:09 +09001043
1044 // Ensure that stub dependency from a rust module is not included
1045 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1046 // The rust module is linked to the stub cc library
Colin Cross004bd3f2023-10-02 11:39:17 -07001047 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park105dc322021-06-11 17:22:09 +09001048 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1049 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +09001050
Jooyung Hana0503a52023-08-23 13:12:50 +09001051 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jiyong Park34d5c332022-02-24 18:02:44 +09001052 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Spandan Das357ffcc2024-07-24 18:07:48 +00001053
1054 // Ensure that mylib is linking with the latest version of stubs for my_prebuilt_platform_lib
1055 ensureContains(t, mylibLdFlags, "my_prebuilt_platform_lib/android_arm64_armv8-a_shared_current/my_prebuilt_platform_lib.so")
1056 // ... and not linking to the non-stub (impl) variant of my_prebuilt_platform_lib
1057 ensureNotContains(t, mylibLdFlags, "my_prebuilt_platform_lib/android_arm64_armv8-a_shared/my_prebuilt_platform_lib.so")
1058 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1059 ensureContains(t, ctx.ModuleForTests("my_prebuilt_platform_lib", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
1060
1061 // Ensure that mylib is linking with the latest version of stubs for my_prebuilt_platform_lib
1062 ensureContains(t, mylibLdFlags, "my_prebuilt_platform_stub_only_lib/android_arm64_armv8-a_shared_current/my_prebuilt_platform_stub_only_lib.so")
1063 // ... and not linking to the non-stub (impl) variant of my_prebuilt_platform_lib
1064 ensureNotContains(t, mylibLdFlags, "my_prebuilt_platform_stub_only_lib/android_arm64_armv8-a_shared/my_prebuilt_platform_stub_only_lib.so")
1065 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1066 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 +09001067}
1068
Jooyung Han20348752023-12-05 15:23:56 +09001069func TestApexShouldNotEmbedStubVariant(t *testing.T) {
1070 testApexError(t, `module "myapex" .*: native_shared_libs: "libbar" is a stub`, `
1071 apex {
1072 name: "myapex",
1073 key: "myapex.key",
1074 vendor: true,
1075 updatable: false,
1076 native_shared_libs: ["libbar"], // should not add an LLNDK stub in a vendor apex
1077 }
1078
1079 apex_key {
1080 name: "myapex.key",
1081 public_key: "testkey.avbpubkey",
1082 private_key: "testkey.pem",
1083 }
1084
1085 cc_library {
1086 name: "libbar",
1087 srcs: ["mylib.cpp"],
1088 llndk: {
1089 symbol_file: "libbar.map.txt",
1090 }
1091 }
1092 `)
1093}
1094
Jiyong Park1bc84122021-06-22 20:23:05 +09001095func TestApexCanUsePrivateApis(t *testing.T) {
1096 ctx := testApex(t, `
1097 apex {
1098 name: "myapex",
1099 key: "myapex.key",
1100 native_shared_libs: ["mylib"],
1101 binaries: ["foo.rust"],
1102 updatable: false,
1103 platform_apis: true,
1104 }
1105
1106 apex_key {
1107 name: "myapex.key",
1108 public_key: "testkey.avbpubkey",
1109 private_key: "testkey.pem",
1110 }
1111
1112 cc_library {
1113 name: "mylib",
1114 srcs: ["mylib.cpp"],
1115 shared_libs: ["mylib2"],
1116 system_shared_libs: [],
1117 stl: "none",
1118 apex_available: [ "myapex" ],
1119 }
1120
1121 cc_library {
1122 name: "mylib2",
1123 srcs: ["mylib.cpp"],
1124 cflags: ["-include mylib.h"],
1125 system_shared_libs: [],
1126 stl: "none",
1127 stubs: {
1128 versions: ["1", "2", "3"],
1129 },
1130 }
1131
1132 rust_binary {
1133 name: "foo.rust",
1134 srcs: ["foo.rs"],
1135 shared_libs: ["libfoo.shared_from_rust"],
1136 prefer_rlib: true,
1137 apex_available: ["myapex"],
1138 }
1139
1140 cc_library_shared {
1141 name: "libfoo.shared_from_rust",
1142 srcs: ["mylib.cpp"],
1143 system_shared_libs: [],
1144 stl: "none",
1145 stubs: {
1146 versions: ["10", "11", "12"],
1147 },
1148 }
1149 `)
1150
Jooyung Hana0503a52023-08-23 13:12:50 +09001151 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park1bc84122021-06-22 20:23:05 +09001152 copyCmds := apexRule.Args["copy_commands"]
1153
1154 // Ensure that indirect stubs dep is not included
1155 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1156 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1157
1158 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1159 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001160 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001161 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1162 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Colin Cross004bd3f2023-10-02 11:39:17 -07001163 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001164 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1165 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1166}
1167
Colin Cross7812fd32020-09-25 12:35:10 -07001168func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1169 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001170 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001171 apex {
1172 name: "myapex",
1173 key: "myapex.key",
1174 native_shared_libs: ["mylib", "mylib3"],
1175 min_sdk_version: "29",
1176 }
1177
1178 apex_key {
1179 name: "myapex.key",
1180 public_key: "testkey.avbpubkey",
1181 private_key: "testkey.pem",
1182 }
1183
1184 cc_library {
1185 name: "mylib",
1186 srcs: ["mylib.cpp"],
1187 shared_libs: ["mylib2", "mylib3"],
1188 system_shared_libs: [],
1189 stl: "none",
1190 apex_available: [ "myapex" ],
1191 min_sdk_version: "28",
1192 }
1193
1194 cc_library {
1195 name: "mylib2",
1196 srcs: ["mylib.cpp"],
1197 cflags: ["-include mylib.h"],
1198 system_shared_libs: [],
1199 stl: "none",
1200 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +00001201 symbol_file: "mylib2.map.txt",
Colin Cross7812fd32020-09-25 12:35:10 -07001202 versions: ["28", "29", "30", "current"],
1203 },
1204 min_sdk_version: "28",
1205 }
1206
1207 cc_library {
1208 name: "mylib3",
1209 srcs: ["mylib.cpp"],
1210 shared_libs: ["mylib4"],
1211 system_shared_libs: [],
1212 stl: "none",
1213 stubs: {
Spandan Das357ffcc2024-07-24 18:07:48 +00001214 symbol_file: "mylib3.map.txt",
Colin Cross7812fd32020-09-25 12:35:10 -07001215 versions: ["28", "29", "30", "current"],
1216 },
1217 apex_available: [ "myapex" ],
1218 min_sdk_version: "28",
1219 }
1220
1221 cc_library {
1222 name: "mylib4",
1223 srcs: ["mylib.cpp"],
1224 system_shared_libs: [],
1225 stl: "none",
1226 apex_available: [ "myapex" ],
1227 min_sdk_version: "28",
1228 }
1229 `)
1230
Jooyung Hana0503a52023-08-23 13:12:50 +09001231 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Colin Cross7812fd32020-09-25 12:35:10 -07001232 copyCmds := apexRule.Args["copy_commands"]
1233
1234 // Ensure that direct non-stubs dep is always included
1235 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1236
1237 // Ensure that indirect stubs dep is not included
1238 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1239
1240 // Ensure that direct stubs dep is included
1241 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1242
1243 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1244
Jiyong Park55549df2021-02-26 23:57:23 +09001245 // Ensure that mylib is linking with the latest version of stub for mylib2
1246 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001247 // ... and not linking to the non-stub (impl) variant of mylib2
1248 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1249
1250 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1251 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1252 // .. and not linking to the stubs variant of mylib3
1253 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1254
1255 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001256 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001257 ensureNotContains(t, mylib2Cflags, "-include ")
1258
Jiyong Park85cc35a2022-07-17 11:30:47 +09001259 // Ensure that genstub is invoked with --systemapi
1260 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001261
Jooyung Hana0503a52023-08-23 13:12:50 +09001262 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Colin Cross7812fd32020-09-25 12:35:10 -07001263 "lib64/mylib.so",
1264 "lib64/mylib3.so",
1265 "lib64/mylib4.so",
1266 })
1267}
1268
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001269func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1270 t.Parallel()
1271 // myapex (Z)
1272 // mylib -----------------.
1273 // |
1274 // otherapex (29) |
1275 // libstub's versions: 29 Z current
1276 // |
1277 // <platform> |
1278 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001279 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001280 apex {
1281 name: "myapex",
1282 key: "myapex.key",
1283 native_shared_libs: ["mylib"],
1284 min_sdk_version: "Z", // non-final
1285 }
1286
1287 cc_library {
1288 name: "mylib",
1289 srcs: ["mylib.cpp"],
1290 shared_libs: ["libstub"],
1291 apex_available: ["myapex"],
1292 min_sdk_version: "Z",
1293 }
1294
1295 apex_key {
1296 name: "myapex.key",
1297 public_key: "testkey.avbpubkey",
1298 private_key: "testkey.pem",
1299 }
1300
1301 apex {
1302 name: "otherapex",
1303 key: "myapex.key",
1304 native_shared_libs: ["libstub"],
1305 min_sdk_version: "29",
1306 }
1307
1308 cc_library {
1309 name: "libstub",
1310 srcs: ["mylib.cpp"],
1311 stubs: {
1312 versions: ["29", "Z", "current"],
1313 },
1314 apex_available: ["otherapex"],
1315 min_sdk_version: "29",
1316 }
1317
1318 // platform module depending on libstub from otherapex should use the latest stub("current")
1319 cc_library {
1320 name: "libplatform",
1321 srcs: ["mylib.cpp"],
1322 shared_libs: ["libstub"],
1323 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001324 `,
1325 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1326 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1327 variables.Platform_sdk_final = proptools.BoolPtr(false)
1328 variables.Platform_version_active_codenames = []string{"Z"}
1329 }),
1330 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001331
Jiyong Park55549df2021-02-26 23:57:23 +09001332 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001333 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001334 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001335 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001336 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001337
1338 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1339 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1340 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1341 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1342 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1343}
1344
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001345func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001346 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001347 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001348 name: "myapex2",
1349 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001350 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001351 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001352 }
1353
1354 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001355 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001356 public_key: "testkey.avbpubkey",
1357 private_key: "testkey.pem",
1358 }
1359
1360 cc_library {
1361 name: "mylib",
1362 srcs: ["mylib.cpp"],
1363 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001364 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001365 system_shared_libs: [],
1366 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001367 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001368 }
1369
1370 cc_library {
1371 name: "libfoo",
1372 srcs: ["mylib.cpp"],
1373 shared_libs: ["libbar"],
1374 system_shared_libs: [],
1375 stl: "none",
1376 stubs: {
1377 versions: ["10", "20", "30"],
1378 },
1379 }
1380
1381 cc_library {
1382 name: "libbar",
1383 srcs: ["mylib.cpp"],
1384 system_shared_libs: [],
1385 stl: "none",
1386 }
1387
Jiyong Park678c8812020-02-07 17:25:49 +09001388 cc_library_static {
1389 name: "libbaz",
1390 srcs: ["mylib.cpp"],
1391 system_shared_libs: [],
1392 stl: "none",
1393 apex_available: [ "myapex2" ],
1394 }
1395
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001396 `)
1397
Jooyung Hana0503a52023-08-23 13:12:50 +09001398 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001399 copyCmds := apexRule.Args["copy_commands"]
1400
1401 // Ensure that direct non-stubs dep is always included
1402 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1403
1404 // Ensure that indirect stubs dep is not included
1405 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1406
1407 // Ensure that dependency of stubs is not included
1408 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1409
Colin Crossaede88c2020-08-11 12:17:01 -07001410 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001411
1412 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001413 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001414 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001415 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001416
Jiyong Park3ff16992019-12-27 14:11:47 +09001417 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001418
1419 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1420 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001421
Colin Crossf61d03d2023-11-02 16:56:39 -07001422 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1423 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001424 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001425
Colin Crossf61d03d2023-11-02 16:56:39 -07001426 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1427 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001428 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001429}
1430
Jooyung Hand3639552019-08-09 12:57:43 +09001431func TestApexWithRuntimeLibsDependency(t *testing.T) {
1432 /*
1433 myapex
1434 |
1435 v (runtime_libs)
1436 mylib ------+------> libfoo [provides stub]
1437 |
1438 `------> libbar
1439 */
Colin Cross1c460562021-02-16 17:55:47 -08001440 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001441 apex {
1442 name: "myapex",
1443 key: "myapex.key",
1444 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001445 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001446 }
1447
1448 apex_key {
1449 name: "myapex.key",
1450 public_key: "testkey.avbpubkey",
1451 private_key: "testkey.pem",
1452 }
1453
1454 cc_library {
1455 name: "mylib",
1456 srcs: ["mylib.cpp"],
Liz Kammer5f108fa2023-05-11 14:33:17 -04001457 static_libs: ["libstatic"],
1458 shared_libs: ["libshared"],
Jooyung Hand3639552019-08-09 12:57:43 +09001459 runtime_libs: ["libfoo", "libbar"],
1460 system_shared_libs: [],
1461 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001462 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001463 }
1464
1465 cc_library {
1466 name: "libfoo",
1467 srcs: ["mylib.cpp"],
1468 system_shared_libs: [],
1469 stl: "none",
1470 stubs: {
1471 versions: ["10", "20", "30"],
1472 },
1473 }
1474
1475 cc_library {
1476 name: "libbar",
1477 srcs: ["mylib.cpp"],
1478 system_shared_libs: [],
1479 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001480 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001481 }
1482
Liz Kammer5f108fa2023-05-11 14:33:17 -04001483 cc_library {
1484 name: "libstatic",
1485 srcs: ["mylib.cpp"],
1486 system_shared_libs: [],
1487 stl: "none",
1488 apex_available: [ "myapex" ],
1489 runtime_libs: ["libstatic_to_runtime"],
1490 }
1491
1492 cc_library {
1493 name: "libshared",
1494 srcs: ["mylib.cpp"],
1495 system_shared_libs: [],
1496 stl: "none",
1497 apex_available: [ "myapex" ],
1498 runtime_libs: ["libshared_to_runtime"],
1499 }
1500
1501 cc_library {
1502 name: "libstatic_to_runtime",
1503 srcs: ["mylib.cpp"],
1504 system_shared_libs: [],
1505 stl: "none",
1506 apex_available: [ "myapex" ],
1507 }
1508
1509 cc_library {
1510 name: "libshared_to_runtime",
1511 srcs: ["mylib.cpp"],
1512 system_shared_libs: [],
1513 stl: "none",
1514 apex_available: [ "myapex" ],
1515 }
Jooyung Hand3639552019-08-09 12:57:43 +09001516 `)
1517
Jooyung Hana0503a52023-08-23 13:12:50 +09001518 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001519 copyCmds := apexRule.Args["copy_commands"]
1520
1521 // Ensure that direct non-stubs dep is always included
1522 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1523
1524 // Ensure that indirect stubs dep is not included
1525 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1526
1527 // Ensure that runtime_libs dep in included
1528 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
Liz Kammer5f108fa2023-05-11 14:33:17 -04001529 ensureContains(t, copyCmds, "image.apex/lib64/libshared.so")
1530 ensureContains(t, copyCmds, "image.apex/lib64/libshared_to_runtime.so")
1531
1532 ensureNotContains(t, copyCmds, "image.apex/lib64/libstatic_to_runtime.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001533
Jooyung Hana0503a52023-08-23 13:12:50 +09001534 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001535 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1536 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001537}
1538
Paul Duffina02cae32021-03-09 01:44:06 +00001539var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1540 cc.PrepareForTestWithCcBuildComponents,
1541 PrepareForTestWithApexBuildComponents,
1542 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001543 apex {
1544 name: "com.android.runtime",
1545 key: "com.android.runtime.key",
1546 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001547 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001548 }
1549
1550 apex_key {
1551 name: "com.android.runtime.key",
1552 public_key: "testkey.avbpubkey",
1553 private_key: "testkey.pem",
1554 }
Paul Duffina02cae32021-03-09 01:44:06 +00001555 `),
1556 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1557)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001558
Paul Duffina02cae32021-03-09 01:44:06 +00001559func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001560 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001561 cc_library {
1562 name: "libc",
1563 no_libcrt: true,
1564 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001565 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001566 stl: "none",
1567 system_shared_libs: [],
1568 stubs: { versions: ["1"] },
1569 apex_available: ["com.android.runtime"],
1570
1571 sanitize: {
1572 hwaddress: true,
1573 }
1574 }
1575
1576 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001577 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001578 no_libcrt: true,
1579 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001580 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001581 stl: "none",
1582 system_shared_libs: [],
1583 srcs: [""],
1584 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001585 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001586
1587 sanitize: {
1588 never: true,
1589 },
Spandan Das4de7b492023-05-05 21:13:01 +00001590 apex_available: [
1591 "//apex_available:anyapex",
1592 "//apex_available:platform",
1593 ],
Paul Duffina02cae32021-03-09 01:44:06 +00001594 } `)
1595 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001596
Jooyung Hana0503a52023-08-23 13:12:50 +09001597 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001598 "lib64/bionic/libc.so",
1599 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1600 })
1601
Colin Cross4c4c1be2022-02-10 11:41:18 -08001602 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001603
1604 installed := hwasan.Description("install libclang_rt.hwasan")
1605 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1606
1607 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1608 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1609 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1610}
1611
1612func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001613 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001614 prepareForTestOfRuntimeApexWithHwasan,
1615 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1616 variables.SanitizeDevice = []string{"hwaddress"}
1617 }),
1618 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001619 cc_library {
1620 name: "libc",
1621 no_libcrt: true,
1622 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001623 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001624 stl: "none",
1625 system_shared_libs: [],
1626 stubs: { versions: ["1"] },
1627 apex_available: ["com.android.runtime"],
1628 }
1629
1630 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001631 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001632 no_libcrt: true,
1633 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001634 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001635 stl: "none",
1636 system_shared_libs: [],
1637 srcs: [""],
1638 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001639 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001640
1641 sanitize: {
1642 never: true,
1643 },
Spandan Das4de7b492023-05-05 21:13:01 +00001644 apex_available: [
1645 "//apex_available:anyapex",
1646 "//apex_available:platform",
1647 ],
Jooyung Han8ce8db92020-05-15 19:05:05 +09001648 }
Paul Duffina02cae32021-03-09 01:44:06 +00001649 `)
1650 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001651
Jooyung Hana0503a52023-08-23 13:12:50 +09001652 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001653 "lib64/bionic/libc.so",
1654 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1655 })
1656
Colin Cross4c4c1be2022-02-10 11:41:18 -08001657 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001658
1659 installed := hwasan.Description("install libclang_rt.hwasan")
1660 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1661
1662 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1663 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1664 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1665}
1666
Jooyung Han61b66e92020-03-21 14:21:46 +00001667func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1668 testcases := []struct {
1669 name string
1670 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001671 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001672 shouldLink string
1673 shouldNotLink []string
1674 }{
1675 {
Jiyong Park55549df2021-02-26 23:57:23 +09001676 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001677 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001678 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001679 shouldLink: "current",
1680 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001681 },
1682 {
Jiyong Park55549df2021-02-26 23:57:23 +09001683 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001684 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001685 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001686 shouldLink: "current",
1687 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001688 },
1689 }
1690 for _, tc := range testcases {
1691 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001692 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001693 apex {
1694 name: "myapex",
1695 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001696 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001697 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001698 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001699 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001700
Jooyung Han61b66e92020-03-21 14:21:46 +00001701 apex_key {
1702 name: "myapex.key",
1703 public_key: "testkey.avbpubkey",
1704 private_key: "testkey.pem",
1705 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001706
Jooyung Han61b66e92020-03-21 14:21:46 +00001707 cc_library {
1708 name: "mylib",
1709 srcs: ["mylib.cpp"],
1710 vendor_available: true,
1711 shared_libs: ["libbar"],
1712 system_shared_libs: [],
1713 stl: "none",
1714 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001715 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001716 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001717
Jooyung Han61b66e92020-03-21 14:21:46 +00001718 cc_library {
1719 name: "libbar",
1720 srcs: ["mylib.cpp"],
1721 system_shared_libs: [],
1722 stl: "none",
1723 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001724 llndk: {
1725 symbol_file: "libbar.map.txt",
1726 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001727 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001728 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001729 withUnbundledBuild,
1730 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001731
Jooyung Han61b66e92020-03-21 14:21:46 +00001732 // Ensure that LLNDK dep is not included
Jooyung Hana0503a52023-08-23 13:12:50 +09001733 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00001734 "lib64/mylib.so",
1735 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001736
Jooyung Han61b66e92020-03-21 14:21:46 +00001737 // Ensure that LLNDK dep is required
Jooyung Hana0503a52023-08-23 13:12:50 +09001738 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han61b66e92020-03-21 14:21:46 +00001739 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1740 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001741
Steven Moreland2c4000c2021-04-27 02:08:49 +00001742 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1743 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001744 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001745 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001746 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001747
Steven Moreland2c4000c2021-04-27 02:08:49 +00001748 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001749 ver := tc.shouldLink
1750 if tc.shouldLink == "current" {
1751 ver = strconv.Itoa(android.FutureApiLevelInt)
1752 }
1753 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001754 })
1755 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001756}
1757
Jiyong Park25fc6a92018-11-18 18:02:45 +09001758func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001759 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001760 apex {
1761 name: "myapex",
1762 key: "myapex.key",
1763 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001764 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001765 }
1766
1767 apex_key {
1768 name: "myapex.key",
1769 public_key: "testkey.avbpubkey",
1770 private_key: "testkey.pem",
1771 }
1772
1773 cc_library {
1774 name: "mylib",
1775 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001776 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001777 shared_libs: ["libdl#27"],
1778 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001779 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001780 }
1781
1782 cc_library_shared {
1783 name: "mylib_shared",
1784 srcs: ["mylib.cpp"],
1785 shared_libs: ["libdl#27"],
1786 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001787 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001788 }
1789
1790 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001791 name: "libBootstrap",
1792 srcs: ["mylib.cpp"],
1793 stl: "none",
1794 bootstrap: true,
1795 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001796 `)
1797
Jooyung Hana0503a52023-08-23 13:12:50 +09001798 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001799 copyCmds := apexRule.Args["copy_commands"]
1800
1801 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001802 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001803 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1804 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001805
1806 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001807 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001808
Colin Crossaede88c2020-08-11 12:17:01 -07001809 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1810 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1811 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001812
1813 // For dependency to libc
1814 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001815 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001816 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001817 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001818 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001819 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1820 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001821
1822 // For dependency to libm
1823 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001824 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001825 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001826 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001827 // ... and is not compiling with the stub
1828 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1829 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1830
1831 // For dependency to libdl
1832 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001833 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001834 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001835 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1836 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001837 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001838 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001839 // ... Cflags from stub is correctly exported to mylib
1840 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1841 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001842
1843 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001844 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1845 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1846 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1847 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001848}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001849
Jooyung Han749dc692020-04-15 11:03:39 +09001850func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001851 // there are three links between liba --> libz.
1852 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001853 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001854 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001855 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001856 apex {
1857 name: "myapex",
1858 key: "myapex.key",
1859 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001860 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001861 }
1862
1863 apex {
1864 name: "otherapex",
1865 key: "myapex.key",
1866 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001867 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001868 }
1869
1870 apex_key {
1871 name: "myapex.key",
1872 public_key: "testkey.avbpubkey",
1873 private_key: "testkey.pem",
1874 }
1875
1876 cc_library {
1877 name: "libx",
1878 shared_libs: ["liba"],
1879 system_shared_libs: [],
1880 stl: "none",
1881 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001882 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001883 }
1884
1885 cc_library {
1886 name: "liby",
1887 shared_libs: ["liba"],
1888 system_shared_libs: [],
1889 stl: "none",
1890 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001891 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001892 }
1893
1894 cc_library {
1895 name: "liba",
1896 shared_libs: ["libz"],
1897 system_shared_libs: [],
1898 stl: "none",
1899 apex_available: [
1900 "//apex_available:anyapex",
1901 "//apex_available:platform",
1902 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001903 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001904 }
1905
1906 cc_library {
1907 name: "libz",
1908 system_shared_libs: [],
1909 stl: "none",
1910 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001911 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001912 },
1913 }
Jooyung Han749dc692020-04-15 11:03:39 +09001914 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001915
1916 expectLink := func(from, from_variant, to, to_variant string) {
1917 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1918 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1919 }
1920 expectNoLink := func(from, from_variant, to, to_variant string) {
1921 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1922 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1923 }
1924 // platform liba is linked to non-stub version
1925 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001926 // liba in myapex is linked to current
1927 expectLink("liba", "shared_apex29", "libz", "shared_current")
1928 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001929 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001930 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001931 // liba in otherapex is linked to current
1932 expectLink("liba", "shared_apex30", "libz", "shared_current")
1933 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001934 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1935 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001936}
1937
Jooyung Hanaed150d2020-04-02 01:41:41 +09001938func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001939 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001940 apex {
1941 name: "myapex",
1942 key: "myapex.key",
1943 native_shared_libs: ["libx"],
1944 min_sdk_version: "R",
1945 }
1946
1947 apex_key {
1948 name: "myapex.key",
1949 public_key: "testkey.avbpubkey",
1950 private_key: "testkey.pem",
1951 }
1952
1953 cc_library {
1954 name: "libx",
1955 shared_libs: ["libz"],
1956 system_shared_libs: [],
1957 stl: "none",
1958 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001959 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001960 }
1961
1962 cc_library {
1963 name: "libz",
1964 system_shared_libs: [],
1965 stl: "none",
1966 stubs: {
1967 versions: ["29", "R"],
1968 },
1969 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001970 `,
1971 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1972 variables.Platform_version_active_codenames = []string{"R"}
1973 }),
1974 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001975
1976 expectLink := func(from, from_variant, to, to_variant string) {
1977 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1978 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1979 }
1980 expectNoLink := func(from, from_variant, to, to_variant string) {
1981 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1982 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1983 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001984 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1985 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001986 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1987 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001988}
1989
Jooyung Han4c4da062021-06-23 10:23:16 +09001990func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1991 testApex(t, `
1992 apex {
1993 name: "myapex",
1994 key: "myapex.key",
1995 java_libs: ["libx"],
1996 min_sdk_version: "S",
1997 }
1998
1999 apex_key {
2000 name: "myapex.key",
2001 public_key: "testkey.avbpubkey",
2002 private_key: "testkey.pem",
2003 }
2004
2005 java_library {
2006 name: "libx",
2007 srcs: ["a.java"],
2008 apex_available: [ "myapex" ],
2009 sdk_version: "current",
2010 min_sdk_version: "S", // should be okay
2011 }
2012 `,
2013 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2014 variables.Platform_version_active_codenames = []string{"S"}
2015 variables.Platform_sdk_codename = proptools.StringPtr("S")
2016 }),
2017 )
2018}
2019
Jooyung Han749dc692020-04-15 11:03:39 +09002020func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002021 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002022 apex {
2023 name: "myapex",
2024 key: "myapex.key",
2025 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002026 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002027 }
2028
2029 apex_key {
2030 name: "myapex.key",
2031 public_key: "testkey.avbpubkey",
2032 private_key: "testkey.pem",
2033 }
2034
2035 cc_library {
2036 name: "libx",
2037 shared_libs: ["libz"],
2038 system_shared_libs: [],
2039 stl: "none",
2040 apex_available: [ "myapex" ],
2041 }
2042
2043 cc_library {
2044 name: "libz",
2045 system_shared_libs: [],
2046 stl: "none",
2047 stubs: {
2048 versions: ["1", "2"],
2049 },
2050 }
2051 `)
2052
2053 expectLink := func(from, from_variant, to, to_variant string) {
2054 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2055 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2056 }
2057 expectNoLink := func(from, from_variant, to, to_variant string) {
2058 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2059 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2060 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002061 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002062 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002063 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07002064 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09002065}
2066
Jooyung Handfc864c2023-03-20 18:19:07 +09002067func TestApexMinSdkVersion_InVendorApex(t *testing.T) {
Jiyong Park5df7bd32021-08-25 16:18:46 +09002068 ctx := testApex(t, `
2069 apex {
2070 name: "myapex",
2071 key: "myapex.key",
2072 native_shared_libs: ["mylib"],
Jooyung Handfc864c2023-03-20 18:19:07 +09002073 updatable: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002074 vendor: true,
2075 min_sdk_version: "29",
2076 }
2077
2078 apex_key {
2079 name: "myapex.key",
2080 public_key: "testkey.avbpubkey",
2081 private_key: "testkey.pem",
2082 }
2083
2084 cc_library {
2085 name: "mylib",
Jooyung Handfc864c2023-03-20 18:19:07 +09002086 srcs: ["mylib.cpp"],
Jiyong Park5df7bd32021-08-25 16:18:46 +09002087 vendor_available: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002088 min_sdk_version: "29",
Jooyung Handfc864c2023-03-20 18:19:07 +09002089 shared_libs: ["libbar"],
2090 }
2091
2092 cc_library {
2093 name: "libbar",
2094 stubs: { versions: ["29", "30"] },
2095 llndk: { symbol_file: "libbar.map.txt" },
Jiyong Park5df7bd32021-08-25 16:18:46 +09002096 }
2097 `)
2098
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002099 vendorVariant := "android_vendor_arm64_armv8-a"
Jiyong Park5df7bd32021-08-25 16:18:46 +09002100
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002101 mylib := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29")
Jooyung Handfc864c2023-03-20 18:19:07 +09002102
2103 // Ensure that mylib links with "current" LLNDK
2104 libFlags := names(mylib.Rule("ld").Args["libFlags"])
Jooyung Han5e8994e2024-03-12 14:12:12 +09002105 ensureListContains(t, libFlags, "out/soong/.intermediates/libbar/"+vendorVariant+"_shared/libbar.so")
Jooyung Handfc864c2023-03-20 18:19:07 +09002106
2107 // Ensure that mylib is targeting 29
2108 ccRule := ctx.ModuleForTests("mylib", vendorVariant+"_static_apex29").Output("obj/mylib.o")
2109 ensureContains(t, ccRule.Args["cFlags"], "-target aarch64-linux-android29")
2110
2111 // Ensure that the correct variant of crtbegin_so is used.
2112 crtBegin := mylib.Rule("ld").Args["crtBegin"]
2113 ensureContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
Jiyong Park5df7bd32021-08-25 16:18:46 +09002114
2115 // Ensure that the crtbegin_so used by the APEX is targeting 29
2116 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
2117 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
2118}
2119
Jooyung Han4495f842023-04-25 16:39:59 +09002120func TestTrackAllowedDeps(t *testing.T) {
2121 ctx := testApex(t, `
2122 apex {
2123 name: "myapex",
2124 key: "myapex.key",
2125 updatable: true,
2126 native_shared_libs: [
2127 "mylib",
2128 "yourlib",
2129 ],
2130 min_sdk_version: "29",
2131 }
2132
2133 apex {
2134 name: "myapex2",
2135 key: "myapex.key",
2136 updatable: false,
2137 native_shared_libs: ["yourlib"],
2138 }
2139
2140 apex_key {
2141 name: "myapex.key",
2142 public_key: "testkey.avbpubkey",
2143 private_key: "testkey.pem",
2144 }
2145
2146 cc_library {
2147 name: "mylib",
2148 srcs: ["mylib.cpp"],
2149 shared_libs: ["libbar"],
2150 min_sdk_version: "29",
2151 apex_available: ["myapex"],
2152 }
2153
2154 cc_library {
2155 name: "libbar",
2156 stubs: { versions: ["29", "30"] },
2157 }
2158
2159 cc_library {
2160 name: "yourlib",
2161 srcs: ["mylib.cpp"],
2162 min_sdk_version: "29",
2163 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2164 }
2165 `, withFiles(android.MockFS{
2166 "packages/modules/common/build/allowed_deps.txt": nil,
2167 }))
2168
2169 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2170 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2171 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002172 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002173 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002174 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002175
Jooyung Hana0503a52023-08-23 13:12:50 +09002176 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07002177 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2178 myapex.Output("depsinfo/flatlist.txt")), "\n")
Jooyung Han4495f842023-04-25 16:39:59 +09002179 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2180 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2181 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2182 flatlist, "mylib:(minSdkVersion:29)")
2183 android.AssertStringListContains(t, "track platform-available lib",
2184 flatlist, "yourlib(minSdkVersion:29)")
2185}
2186
2187func TestTrackAllowedDeps_SkipWithoutAllowedDepsTxt(t *testing.T) {
2188 ctx := testApex(t, `
2189 apex {
2190 name: "myapex",
2191 key: "myapex.key",
2192 updatable: true,
2193 min_sdk_version: "29",
2194 }
2195
2196 apex_key {
2197 name: "myapex.key",
2198 public_key: "testkey.avbpubkey",
2199 private_key: "testkey.pem",
2200 }
2201 `)
2202 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2203 if nil != depsinfo.MaybeRule("generateApexDepsInfoFilesRule").Output {
2204 t.Error("apex_depsinfo_singleton shouldn't run when allowed_deps.txt doesn't exist")
2205 }
2206}
2207
Jooyung Han03b51852020-02-26 22:45:42 +09002208func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002209 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002210 apex {
2211 name: "myapex",
2212 key: "myapex.key",
2213 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002214 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002215 }
2216
2217 apex_key {
2218 name: "myapex.key",
2219 public_key: "testkey.avbpubkey",
2220 private_key: "testkey.pem",
2221 }
2222
2223 cc_library {
2224 name: "libx",
2225 system_shared_libs: [],
2226 stl: "none",
2227 apex_available: [ "myapex" ],
2228 stubs: {
2229 versions: ["1", "2"],
2230 },
2231 }
2232
2233 cc_library {
2234 name: "libz",
2235 shared_libs: ["libx"],
2236 system_shared_libs: [],
2237 stl: "none",
2238 }
2239 `)
2240
2241 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002242 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002243 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2244 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2245 }
2246 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002247 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002248 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2249 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2250 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002251 expectLink("libz", "shared", "libx", "shared_current")
2252 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002253 expectNoLink("libz", "shared", "libz", "shared_1")
2254 expectNoLink("libz", "shared", "libz", "shared")
2255}
2256
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002257var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2258 func(variables android.FixtureProductVariables) {
2259 variables.SanitizeDevice = []string{"hwaddress"}
2260 },
2261)
2262
Jooyung Han75568392020-03-20 04:29:24 +09002263func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002264 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002265 apex {
2266 name: "myapex",
2267 key: "myapex.key",
2268 native_shared_libs: ["libx"],
2269 min_sdk_version: "29",
2270 }
2271
2272 apex_key {
2273 name: "myapex.key",
2274 public_key: "testkey.avbpubkey",
2275 private_key: "testkey.pem",
2276 }
2277
2278 cc_library {
2279 name: "libx",
2280 shared_libs: ["libbar"],
2281 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002282 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002283 }
2284
2285 cc_library {
2286 name: "libbar",
2287 stubs: {
2288 versions: ["29", "30"],
2289 },
2290 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002291 `,
2292 prepareForTestWithSantitizeHwaddress,
2293 )
Jooyung Han03b51852020-02-26 22:45:42 +09002294 expectLink := func(from, from_variant, to, to_variant string) {
2295 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2296 libFlags := ld.Args["libFlags"]
2297 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2298 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002299 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002300}
2301
Jooyung Han75568392020-03-20 04:29:24 +09002302func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002303 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002304 apex {
2305 name: "myapex",
2306 key: "myapex.key",
2307 native_shared_libs: ["libx"],
2308 min_sdk_version: "29",
2309 }
2310
2311 apex_key {
2312 name: "myapex.key",
2313 public_key: "testkey.avbpubkey",
2314 private_key: "testkey.pem",
2315 }
2316
2317 cc_library {
2318 name: "libx",
2319 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002320 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002321 }
Jooyung Han75568392020-03-20 04:29:24 +09002322 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002323
2324 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002325 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002326 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002327 // note that platform variant is not.
2328 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002329 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002330}
2331
Jooyung Han749dc692020-04-15 11:03:39 +09002332func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2333 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002334 apex {
2335 name: "myapex",
2336 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002337 native_shared_libs: ["mylib"],
2338 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002339 }
2340
2341 apex_key {
2342 name: "myapex.key",
2343 public_key: "testkey.avbpubkey",
2344 private_key: "testkey.pem",
2345 }
Jooyung Han749dc692020-04-15 11:03:39 +09002346
2347 cc_library {
2348 name: "mylib",
2349 srcs: ["mylib.cpp"],
2350 system_shared_libs: [],
2351 stl: "none",
2352 apex_available: [
2353 "myapex",
2354 ],
2355 min_sdk_version: "30",
2356 }
2357 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002358
2359 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2360 apex {
2361 name: "myapex",
2362 key: "myapex.key",
2363 native_shared_libs: ["libfoo.ffi"],
2364 min_sdk_version: "29",
2365 }
2366
2367 apex_key {
2368 name: "myapex.key",
2369 public_key: "testkey.avbpubkey",
2370 private_key: "testkey.pem",
2371 }
2372
2373 rust_ffi_shared {
2374 name: "libfoo.ffi",
2375 srcs: ["foo.rs"],
2376 crate_name: "foo",
2377 apex_available: [
2378 "myapex",
2379 ],
2380 min_sdk_version: "30",
2381 }
2382 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002383
2384 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2385 apex {
2386 name: "myapex",
2387 key: "myapex.key",
2388 java_libs: ["libfoo"],
2389 min_sdk_version: "29",
2390 }
2391
2392 apex_key {
2393 name: "myapex.key",
2394 public_key: "testkey.avbpubkey",
2395 private_key: "testkey.pem",
2396 }
2397
2398 java_import {
2399 name: "libfoo",
2400 jars: ["libfoo.jar"],
2401 apex_available: [
2402 "myapex",
2403 ],
2404 min_sdk_version: "30",
2405 }
2406 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002407
2408 // Skip check for modules compiling against core API surface
2409 testApex(t, `
2410 apex {
2411 name: "myapex",
2412 key: "myapex.key",
2413 java_libs: ["libfoo"],
2414 min_sdk_version: "29",
2415 }
2416
2417 apex_key {
2418 name: "myapex.key",
2419 public_key: "testkey.avbpubkey",
2420 private_key: "testkey.pem",
2421 }
2422
2423 java_library {
2424 name: "libfoo",
2425 srcs: ["Foo.java"],
2426 apex_available: [
2427 "myapex",
2428 ],
2429 // Compile against core API surface
2430 sdk_version: "core_current",
2431 min_sdk_version: "30",
2432 }
2433 `)
2434
Jooyung Han749dc692020-04-15 11:03:39 +09002435}
2436
2437func TestApexMinSdkVersion_Okay(t *testing.T) {
2438 testApex(t, `
2439 apex {
2440 name: "myapex",
2441 key: "myapex.key",
2442 native_shared_libs: ["libfoo"],
2443 java_libs: ["libbar"],
2444 min_sdk_version: "29",
2445 }
2446
2447 apex_key {
2448 name: "myapex.key",
2449 public_key: "testkey.avbpubkey",
2450 private_key: "testkey.pem",
2451 }
2452
2453 cc_library {
2454 name: "libfoo",
2455 srcs: ["mylib.cpp"],
2456 shared_libs: ["libfoo_dep"],
2457 apex_available: ["myapex"],
2458 min_sdk_version: "29",
2459 }
2460
2461 cc_library {
2462 name: "libfoo_dep",
2463 srcs: ["mylib.cpp"],
2464 apex_available: ["myapex"],
2465 min_sdk_version: "29",
2466 }
2467
2468 java_library {
2469 name: "libbar",
2470 sdk_version: "current",
2471 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002472 static_libs: [
2473 "libbar_dep",
2474 "libbar_import_dep",
2475 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002476 apex_available: ["myapex"],
2477 min_sdk_version: "29",
2478 }
2479
2480 java_library {
2481 name: "libbar_dep",
2482 sdk_version: "current",
2483 srcs: ["a.java"],
2484 apex_available: ["myapex"],
2485 min_sdk_version: "29",
2486 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002487
2488 java_import {
2489 name: "libbar_import_dep",
2490 jars: ["libbar.jar"],
2491 apex_available: ["myapex"],
2492 min_sdk_version: "29",
2493 }
Jooyung Han03b51852020-02-26 22:45:42 +09002494 `)
2495}
2496
Colin Cross8ca61c12022-10-06 21:00:14 -07002497func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2498 // Tests that an apex dependency with min_sdk_version higher than the
2499 // min_sdk_version of the apex is allowed as long as the dependency's
2500 // min_sdk_version is less than or equal to the api level that the
2501 // architecture was introduced in. In this case, arm64 didn't exist
2502 // until api level 21, so the arm64 code will never need to run on
2503 // an api level 20 device, even if other architectures of the apex
2504 // will.
2505 testApex(t, `
2506 apex {
2507 name: "myapex",
2508 key: "myapex.key",
2509 native_shared_libs: ["libfoo"],
2510 min_sdk_version: "20",
2511 }
2512
2513 apex_key {
2514 name: "myapex.key",
2515 public_key: "testkey.avbpubkey",
2516 private_key: "testkey.pem",
2517 }
2518
2519 cc_library {
2520 name: "libfoo",
2521 srcs: ["mylib.cpp"],
2522 apex_available: ["myapex"],
2523 min_sdk_version: "21",
2524 stl: "none",
2525 }
2526 `)
2527}
2528
Artur Satayev8cf899a2020-04-15 17:29:42 +01002529func TestJavaStableSdkVersion(t *testing.T) {
2530 testCases := []struct {
2531 name string
2532 expectedError string
2533 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002534 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002535 }{
2536 {
2537 name: "Non-updatable apex with non-stable dep",
2538 bp: `
2539 apex {
2540 name: "myapex",
2541 java_libs: ["myjar"],
2542 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002543 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002544 }
2545 apex_key {
2546 name: "myapex.key",
2547 public_key: "testkey.avbpubkey",
2548 private_key: "testkey.pem",
2549 }
2550 java_library {
2551 name: "myjar",
2552 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002553 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002554 apex_available: ["myapex"],
2555 }
2556 `,
2557 },
2558 {
2559 name: "Updatable apex with stable dep",
2560 bp: `
2561 apex {
2562 name: "myapex",
2563 java_libs: ["myjar"],
2564 key: "myapex.key",
2565 updatable: true,
2566 min_sdk_version: "29",
2567 }
2568 apex_key {
2569 name: "myapex.key",
2570 public_key: "testkey.avbpubkey",
2571 private_key: "testkey.pem",
2572 }
2573 java_library {
2574 name: "myjar",
2575 srcs: ["foo/bar/MyClass.java"],
2576 sdk_version: "current",
2577 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002578 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002579 }
2580 `,
2581 },
2582 {
2583 name: "Updatable apex with non-stable dep",
2584 expectedError: "cannot depend on \"myjar\"",
2585 bp: `
2586 apex {
2587 name: "myapex",
2588 java_libs: ["myjar"],
2589 key: "myapex.key",
2590 updatable: true,
2591 }
2592 apex_key {
2593 name: "myapex.key",
2594 public_key: "testkey.avbpubkey",
2595 private_key: "testkey.pem",
2596 }
2597 java_library {
2598 name: "myjar",
2599 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002600 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002601 apex_available: ["myapex"],
2602 }
2603 `,
2604 },
2605 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002606 name: "Updatable apex with non-stable legacy core platform dep",
2607 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2608 bp: `
2609 apex {
2610 name: "myapex",
2611 java_libs: ["myjar-uses-legacy"],
2612 key: "myapex.key",
2613 updatable: true,
2614 }
2615 apex_key {
2616 name: "myapex.key",
2617 public_key: "testkey.avbpubkey",
2618 private_key: "testkey.pem",
2619 }
2620 java_library {
2621 name: "myjar-uses-legacy",
2622 srcs: ["foo/bar/MyClass.java"],
2623 sdk_version: "core_platform",
2624 apex_available: ["myapex"],
2625 }
2626 `,
2627 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2628 },
2629 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002630 name: "Updatable apex with non-stable transitive dep",
2631 // This is not actually detecting that the transitive dependency is unstable, rather it is
2632 // detecting that the transitive dependency is building against a wider API surface than the
2633 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002634 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002635 bp: `
2636 apex {
2637 name: "myapex",
2638 java_libs: ["myjar"],
2639 key: "myapex.key",
2640 updatable: true,
2641 }
2642 apex_key {
2643 name: "myapex.key",
2644 public_key: "testkey.avbpubkey",
2645 private_key: "testkey.pem",
2646 }
2647 java_library {
2648 name: "myjar",
2649 srcs: ["foo/bar/MyClass.java"],
2650 sdk_version: "current",
2651 apex_available: ["myapex"],
2652 static_libs: ["transitive-jar"],
2653 }
2654 java_library {
2655 name: "transitive-jar",
2656 srcs: ["foo/bar/MyClass.java"],
2657 sdk_version: "core_platform",
2658 apex_available: ["myapex"],
2659 }
2660 `,
2661 },
2662 }
2663
2664 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002665 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2666 continue
2667 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002668 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002669 errorHandler := android.FixtureExpectsNoErrors
2670 if test.expectedError != "" {
2671 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002672 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002673 android.GroupFixturePreparers(
2674 java.PrepareForTestWithJavaDefaultModules,
2675 PrepareForTestWithApexBuildComponents,
2676 prepareForTestWithMyapex,
2677 android.OptionalFixturePreparer(test.preparer),
2678 ).
2679 ExtendWithErrorHandler(errorHandler).
2680 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002681 })
2682 }
2683}
2684
Jooyung Han749dc692020-04-15 11:03:39 +09002685func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2686 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2687 apex {
2688 name: "myapex",
2689 key: "myapex.key",
2690 native_shared_libs: ["mylib"],
2691 min_sdk_version: "29",
2692 }
2693
2694 apex_key {
2695 name: "myapex.key",
2696 public_key: "testkey.avbpubkey",
2697 private_key: "testkey.pem",
2698 }
2699
2700 cc_library {
2701 name: "mylib",
2702 srcs: ["mylib.cpp"],
2703 shared_libs: ["mylib2"],
2704 system_shared_libs: [],
2705 stl: "none",
2706 apex_available: [
2707 "myapex",
2708 ],
2709 min_sdk_version: "29",
2710 }
2711
2712 // indirect part of the apex
2713 cc_library {
2714 name: "mylib2",
2715 srcs: ["mylib.cpp"],
2716 system_shared_libs: [],
2717 stl: "none",
2718 apex_available: [
2719 "myapex",
2720 ],
2721 min_sdk_version: "30",
2722 }
2723 `)
2724}
2725
2726func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2727 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2728 apex {
2729 name: "myapex",
2730 key: "myapex.key",
2731 apps: ["AppFoo"],
2732 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002733 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002734 }
2735
2736 apex_key {
2737 name: "myapex.key",
2738 public_key: "testkey.avbpubkey",
2739 private_key: "testkey.pem",
2740 }
2741
2742 android_app {
2743 name: "AppFoo",
2744 srcs: ["foo/bar/MyClass.java"],
2745 sdk_version: "current",
2746 min_sdk_version: "29",
2747 system_modules: "none",
2748 stl: "none",
2749 static_libs: ["bar"],
2750 apex_available: [ "myapex" ],
2751 }
2752
2753 java_library {
2754 name: "bar",
2755 sdk_version: "current",
2756 srcs: ["a.java"],
2757 apex_available: [ "myapex" ],
2758 }
2759 `)
2760}
2761
2762func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002763 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002764 apex {
2765 name: "myapex",
2766 key: "myapex.key",
2767 native_shared_libs: ["mylib"],
2768 min_sdk_version: "29",
2769 }
2770
2771 apex_key {
2772 name: "myapex.key",
2773 public_key: "testkey.avbpubkey",
2774 private_key: "testkey.pem",
2775 }
2776
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002777 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002778 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2779 cc_library {
2780 name: "mylib",
2781 srcs: ["mylib.cpp"],
2782 shared_libs: ["mylib2"],
2783 system_shared_libs: [],
2784 stl: "none",
2785 apex_available: ["myapex", "otherapex"],
2786 min_sdk_version: "29",
2787 }
2788
2789 cc_library {
2790 name: "mylib2",
2791 srcs: ["mylib.cpp"],
2792 system_shared_libs: [],
2793 stl: "none",
2794 apex_available: ["otherapex"],
2795 stubs: { versions: ["29", "30"] },
2796 min_sdk_version: "30",
2797 }
2798
2799 apex {
2800 name: "otherapex",
2801 key: "myapex.key",
2802 native_shared_libs: ["mylib", "mylib2"],
2803 min_sdk_version: "30",
2804 }
2805 `)
2806 expectLink := func(from, from_variant, to, to_variant string) {
2807 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2808 libFlags := ld.Args["libFlags"]
2809 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2810 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002811 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002812 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002813}
2814
Jooyung Haned124c32021-01-26 11:43:46 +09002815func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002816 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2817 func(variables android.FixtureProductVariables) {
2818 variables.Platform_sdk_codename = proptools.StringPtr("S")
2819 variables.Platform_version_active_codenames = []string{"S"}
2820 },
2821 )
Jooyung Haned124c32021-01-26 11:43:46 +09002822 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2823 apex {
2824 name: "myapex",
2825 key: "myapex.key",
2826 native_shared_libs: ["libfoo"],
2827 min_sdk_version: "S",
2828 }
2829 apex_key {
2830 name: "myapex.key",
2831 public_key: "testkey.avbpubkey",
2832 private_key: "testkey.pem",
2833 }
2834 cc_library {
2835 name: "libfoo",
2836 shared_libs: ["libbar"],
2837 apex_available: ["myapex"],
2838 min_sdk_version: "29",
2839 }
2840 cc_library {
2841 name: "libbar",
2842 apex_available: ["myapex"],
2843 }
2844 `, withSAsActiveCodeNames)
2845}
2846
2847func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002848 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2849 variables.Platform_sdk_codename = proptools.StringPtr("S")
2850 variables.Platform_version_active_codenames = []string{"S", "T"}
2851 })
Colin Cross1c460562021-02-16 17:55:47 -08002852 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002853 apex {
2854 name: "myapex",
2855 key: "myapex.key",
2856 native_shared_libs: ["libfoo"],
2857 min_sdk_version: "S",
2858 }
2859 apex_key {
2860 name: "myapex.key",
2861 public_key: "testkey.avbpubkey",
2862 private_key: "testkey.pem",
2863 }
2864 cc_library {
2865 name: "libfoo",
2866 shared_libs: ["libbar"],
2867 apex_available: ["myapex"],
2868 min_sdk_version: "S",
2869 }
2870 cc_library {
2871 name: "libbar",
2872 stubs: {
2873 symbol_file: "libbar.map.txt",
2874 versions: ["30", "S", "T"],
2875 },
2876 }
2877 `, withSAsActiveCodeNames)
2878
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002879 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002880 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2881 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002882 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002883}
2884
Jiyong Park7c2ee712018-12-07 00:42:25 +09002885func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002886 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002887 apex {
2888 name: "myapex",
2889 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002890 native_shared_libs: ["mylib"],
Jooyung Han4ed512b2023-08-11 16:30:04 +09002891 binaries: ["mybin", "mybin.rust"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002892 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002893 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002894 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002895 }
2896
2897 apex_key {
2898 name: "myapex.key",
2899 public_key: "testkey.avbpubkey",
2900 private_key: "testkey.pem",
2901 }
2902
2903 prebuilt_etc {
2904 name: "myetc",
2905 src: "myprebuilt",
2906 sub_dir: "foo/bar",
2907 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002908
2909 cc_library {
2910 name: "mylib",
2911 srcs: ["mylib.cpp"],
2912 relative_install_path: "foo/bar",
2913 system_shared_libs: [],
2914 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002915 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002916 }
2917
2918 cc_binary {
2919 name: "mybin",
2920 srcs: ["mylib.cpp"],
2921 relative_install_path: "foo/bar",
2922 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002923 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002924 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002925 }
Jooyung Han4ed512b2023-08-11 16:30:04 +09002926
2927 rust_binary {
2928 name: "mybin.rust",
2929 srcs: ["foo.rs"],
2930 relative_install_path: "rust_subdir",
2931 apex_available: [ "myapex" ],
2932 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002933 `)
2934
Jooyung Hana0503a52023-08-23 13:12:50 +09002935 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002936 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002937
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002938 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002939 ensureContains(t, cmd, "/etc ")
2940 ensureContains(t, cmd, "/etc/foo ")
2941 ensureContains(t, cmd, "/etc/foo/bar ")
2942 ensureContains(t, cmd, "/lib64 ")
2943 ensureContains(t, cmd, "/lib64/foo ")
2944 ensureContains(t, cmd, "/lib64/foo/bar ")
2945 ensureContains(t, cmd, "/lib ")
2946 ensureContains(t, cmd, "/lib/foo ")
2947 ensureContains(t, cmd, "/lib/foo/bar ")
2948 ensureContains(t, cmd, "/bin ")
2949 ensureContains(t, cmd, "/bin/foo ")
2950 ensureContains(t, cmd, "/bin/foo/bar ")
Jooyung Han4ed512b2023-08-11 16:30:04 +09002951 ensureContains(t, cmd, "/bin/rust_subdir ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002952}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002953
Jooyung Han35155c42020-02-06 17:33:20 +09002954func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002955 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002956 apex {
2957 name: "myapex",
2958 key: "myapex.key",
2959 multilib: {
2960 both: {
2961 native_shared_libs: ["mylib"],
2962 binaries: ["mybin"],
2963 },
2964 },
2965 compile_multilib: "both",
2966 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002967 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002968 }
2969
2970 apex_key {
2971 name: "myapex.key",
2972 public_key: "testkey.avbpubkey",
2973 private_key: "testkey.pem",
2974 }
2975
2976 cc_library {
2977 name: "mylib",
2978 relative_install_path: "foo/bar",
2979 system_shared_libs: [],
2980 stl: "none",
2981 apex_available: [ "myapex" ],
2982 native_bridge_supported: true,
2983 }
2984
2985 cc_binary {
2986 name: "mybin",
2987 relative_install_path: "foo/bar",
2988 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002989 stl: "none",
2990 apex_available: [ "myapex" ],
2991 native_bridge_supported: true,
2992 compile_multilib: "both", // default is "first" for binary
2993 multilib: {
2994 lib64: {
2995 suffix: "64",
2996 },
2997 },
2998 }
2999 `, withNativeBridgeEnabled)
Jooyung Hana0503a52023-08-23 13:12:50 +09003000 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han35155c42020-02-06 17:33:20 +09003001 "bin/foo/bar/mybin",
3002 "bin/foo/bar/mybin64",
3003 "bin/arm/foo/bar/mybin",
3004 "bin/arm64/foo/bar/mybin64",
3005 "lib/foo/bar/mylib.so",
3006 "lib/arm/foo/bar/mylib.so",
3007 "lib64/foo/bar/mylib.so",
3008 "lib64/arm64/foo/bar/mylib.so",
3009 })
3010}
3011
Jooyung Han85d61762020-06-24 23:50:26 +09003012func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08003013 result := android.GroupFixturePreparers(
3014 prepareForApexTest,
3015 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
3016 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09003017 apex {
3018 name: "myapex",
3019 key: "myapex.key",
3020 binaries: ["mybin"],
3021 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003022 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09003023 }
3024 apex_key {
3025 name: "myapex.key",
3026 public_key: "testkey.avbpubkey",
3027 private_key: "testkey.pem",
3028 }
3029 cc_binary {
3030 name: "mybin",
3031 vendor: true,
3032 shared_libs: ["libfoo"],
3033 }
3034 cc_library {
3035 name: "libfoo",
3036 proprietary: true,
3037 }
3038 `)
3039
Jooyung Hana0503a52023-08-23 13:12:50 +09003040 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09003041 "bin/mybin",
3042 "lib64/libfoo.so",
3043 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
3044 "lib64/libc++.so",
3045 })
3046
Jooyung Hana0503a52023-08-23 13:12:50 +09003047 apexBundle := result.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossc68db4b2021-11-11 18:59:15 -08003048 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09003049 name := apexBundle.BaseModuleName()
3050 prefix := "TARGET_"
3051 var builder strings.Builder
3052 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08003053 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01003054 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01003055 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003056
Jooyung Hana0503a52023-08-23 13:12:50 +09003057 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003058 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
3059 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09003060}
3061
Justin Yun13decfb2021-03-08 19:25:55 +09003062func TestProductVariant(t *testing.T) {
3063 ctx := testApex(t, `
3064 apex {
3065 name: "myapex",
3066 key: "myapex.key",
3067 updatable: false,
3068 product_specific: true,
3069 binaries: ["foo"],
3070 }
3071
3072 apex_key {
3073 name: "myapex.key",
3074 public_key: "testkey.avbpubkey",
3075 private_key: "testkey.pem",
3076 }
3077
3078 cc_binary {
3079 name: "foo",
3080 product_available: true,
3081 apex_available: ["myapex"],
3082 srcs: ["foo.cpp"],
3083 }
Justin Yunaf1fde42023-09-27 16:22:10 +09003084 `)
Justin Yun13decfb2021-03-08 19:25:55 +09003085
3086 cflags := strings.Fields(
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003087 ctx.ModuleForTests("foo", "android_product_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09003088 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
3089 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
3090 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
3091 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
3092}
3093
Jooyung Han8e5685d2020-09-21 11:02:57 +09003094func TestApex_withPrebuiltFirmware(t *testing.T) {
3095 testCases := []struct {
3096 name string
3097 additionalProp string
3098 }{
3099 {"system apex with prebuilt_firmware", ""},
3100 {"vendor apex with prebuilt_firmware", "vendor: true,"},
3101 }
3102 for _, tc := range testCases {
3103 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003104 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09003105 apex {
3106 name: "myapex",
3107 key: "myapex.key",
3108 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003109 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09003110 `+tc.additionalProp+`
3111 }
3112 apex_key {
3113 name: "myapex.key",
3114 public_key: "testkey.avbpubkey",
3115 private_key: "testkey.pem",
3116 }
3117 prebuilt_firmware {
3118 name: "myfirmware",
3119 src: "myfirmware.bin",
3120 filename_from_src: true,
3121 `+tc.additionalProp+`
3122 }
3123 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09003124 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han8e5685d2020-09-21 11:02:57 +09003125 "etc/firmware/myfirmware.bin",
3126 })
3127 })
3128 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003129}
3130
Jooyung Hanefb184e2020-06-25 17:14:25 +09003131func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003132 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003133 apex {
3134 name: "myapex",
3135 key: "myapex.key",
3136 vendor: true,
3137 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003138 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003139 }
3140
3141 apex_key {
3142 name: "myapex.key",
3143 public_key: "testkey.avbpubkey",
3144 private_key: "testkey.pem",
3145 }
3146
3147 cc_library {
3148 name: "mylib",
3149 vendor_available: true,
3150 }
3151 `)
3152
Jooyung Hana0503a52023-08-23 13:12:50 +09003153 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003154 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003155 name := apexBundle.BaseModuleName()
3156 prefix := "TARGET_"
3157 var builder strings.Builder
3158 data.Custom(&builder, name, prefix, "", data)
3159 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09003160 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 +09003161}
3162
Jooyung Han2ed99d02020-06-24 23:26:26 +09003163func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003164 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003165 apex {
3166 name: "myapex",
3167 key: "myapex.key",
3168 vintf_fragments: ["fragment.xml"],
3169 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003170 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003171 }
3172 apex_key {
3173 name: "myapex.key",
3174 public_key: "testkey.avbpubkey",
3175 private_key: "testkey.pem",
3176 }
3177 cc_binary {
3178 name: "mybin",
3179 }
3180 `)
3181
Jooyung Hana0503a52023-08-23 13:12:50 +09003182 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003183 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003184 name := apexBundle.BaseModuleName()
3185 prefix := "TARGET_"
3186 var builder strings.Builder
3187 data.Custom(&builder, name, prefix, "", data)
3188 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003189 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003190 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003191}
3192
Jiyong Park16e91a02018-12-20 18:18:08 +09003193func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003194 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003195 apex {
3196 name: "myapex",
3197 key: "myapex.key",
3198 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003199 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003200 }
3201
3202 apex_key {
3203 name: "myapex.key",
3204 public_key: "testkey.avbpubkey",
3205 private_key: "testkey.pem",
3206 }
3207
3208 cc_library {
3209 name: "mylib",
3210 srcs: ["mylib.cpp"],
3211 system_shared_libs: [],
3212 stl: "none",
3213 stubs: {
3214 versions: ["1", "2", "3"],
3215 },
Spandan Das20fce2d2023-04-12 17:21:39 +00003216 apex_available: ["myapex"],
Jiyong Park16e91a02018-12-20 18:18:08 +09003217 }
3218
3219 cc_binary {
3220 name: "not_in_apex",
3221 srcs: ["mylib.cpp"],
3222 static_libs: ["mylib"],
3223 static_executable: true,
3224 system_shared_libs: [],
3225 stl: "none",
3226 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003227 `)
3228
Colin Cross7113d202019-11-20 16:39:12 -08003229 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003230
3231 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003232 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003233}
Jiyong Park9335a262018-12-24 11:31:58 +09003234
3235func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003236 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003237 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003238 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003239 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003240 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003241 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003242 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003243 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003244 }
3245
3246 cc_library {
3247 name: "mylib",
3248 srcs: ["mylib.cpp"],
3249 system_shared_libs: [],
3250 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003251 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003252 }
3253
3254 apex_key {
3255 name: "myapex.key",
3256 public_key: "testkey.avbpubkey",
3257 private_key: "testkey.pem",
3258 }
3259
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003260 android_app_certificate {
3261 name: "myapex.certificate",
3262 certificate: "testkey",
3263 }
3264
3265 android_app_certificate {
3266 name: "myapex.certificate.override",
3267 certificate: "testkey.override",
3268 }
3269
Jiyong Park9335a262018-12-24 11:31:58 +09003270 `)
3271
3272 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003273 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003274
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003275 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3276 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003277 "vendor/foo/devkeys/testkey.avbpubkey")
3278 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003279 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3280 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003281 "vendor/foo/devkeys/testkey.pem")
3282 }
3283
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003284 // check the APK certs. It should be overridden to myapex.certificate.override
Jooyung Hana0503a52023-08-23 13:12:50 +09003285 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003286 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003287 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003288 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003289 }
3290}
Jiyong Park58e364a2019-01-19 19:24:06 +09003291
Jooyung Hanf121a652019-12-17 14:30:11 +09003292func TestCertificate(t *testing.T) {
3293 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003294 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003295 apex {
3296 name: "myapex",
3297 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003298 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003299 }
3300 apex_key {
3301 name: "myapex.key",
3302 public_key: "testkey.avbpubkey",
3303 private_key: "testkey.pem",
3304 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003305 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003306 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3307 if actual := rule.Args["certificates"]; actual != expected {
3308 t.Errorf("certificates should be %q, not %q", expected, actual)
3309 }
3310 })
3311 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003312 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003313 apex {
3314 name: "myapex_keytest",
3315 key: "myapex.key",
3316 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003317 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003318 }
3319 apex_key {
3320 name: "myapex.key",
3321 public_key: "testkey.avbpubkey",
3322 private_key: "testkey.pem",
3323 }
3324 android_app_certificate {
3325 name: "myapex.certificate.override",
3326 certificate: "testkey.override",
3327 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003328 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003329 expected := "testkey.override.x509.pem testkey.override.pk8"
3330 if actual := rule.Args["certificates"]; actual != expected {
3331 t.Errorf("certificates should be %q, not %q", expected, actual)
3332 }
3333 })
3334 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003335 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003336 apex {
3337 name: "myapex",
3338 key: "myapex.key",
3339 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003340 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003341 }
3342 apex_key {
3343 name: "myapex.key",
3344 public_key: "testkey.avbpubkey",
3345 private_key: "testkey.pem",
3346 }
3347 android_app_certificate {
3348 name: "myapex.certificate",
3349 certificate: "testkey",
3350 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003351 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003352 expected := "testkey.x509.pem testkey.pk8"
3353 if actual := rule.Args["certificates"]; actual != expected {
3354 t.Errorf("certificates should be %q, not %q", expected, actual)
3355 }
3356 })
3357 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003358 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003359 apex {
3360 name: "myapex_keytest",
3361 key: "myapex.key",
3362 file_contexts: ":myapex-file_contexts",
3363 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003364 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003365 }
3366 apex_key {
3367 name: "myapex.key",
3368 public_key: "testkey.avbpubkey",
3369 private_key: "testkey.pem",
3370 }
3371 android_app_certificate {
3372 name: "myapex.certificate.override",
3373 certificate: "testkey.override",
3374 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003375 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003376 expected := "testkey.override.x509.pem testkey.override.pk8"
3377 if actual := rule.Args["certificates"]; actual != expected {
3378 t.Errorf("certificates should be %q, not %q", expected, actual)
3379 }
3380 })
3381 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003382 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003383 apex {
3384 name: "myapex",
3385 key: "myapex.key",
3386 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003387 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003388 }
3389 apex_key {
3390 name: "myapex.key",
3391 public_key: "testkey.avbpubkey",
3392 private_key: "testkey.pem",
3393 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003394 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003395 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3396 if actual := rule.Args["certificates"]; actual != expected {
3397 t.Errorf("certificates should be %q, not %q", expected, actual)
3398 }
3399 })
3400 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003401 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003402 apex {
3403 name: "myapex_keytest",
3404 key: "myapex.key",
3405 file_contexts: ":myapex-file_contexts",
3406 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003407 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003408 }
3409 apex_key {
3410 name: "myapex.key",
3411 public_key: "testkey.avbpubkey",
3412 private_key: "testkey.pem",
3413 }
3414 android_app_certificate {
3415 name: "myapex.certificate.override",
3416 certificate: "testkey.override",
3417 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003418 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003419 expected := "testkey.override.x509.pem testkey.override.pk8"
3420 if actual := rule.Args["certificates"]; actual != expected {
3421 t.Errorf("certificates should be %q, not %q", expected, actual)
3422 }
3423 })
3424}
3425
Jiyong Park58e364a2019-01-19 19:24:06 +09003426func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003427 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003428 apex {
3429 name: "myapex",
3430 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003431 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003432 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003433 }
3434
3435 apex {
3436 name: "otherapex",
3437 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003438 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003439 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003440 }
3441
3442 apex_key {
3443 name: "myapex.key",
3444 public_key: "testkey.avbpubkey",
3445 private_key: "testkey.pem",
3446 }
3447
3448 cc_library {
3449 name: "mylib",
3450 srcs: ["mylib.cpp"],
3451 system_shared_libs: [],
3452 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003453 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003454 "myapex",
3455 "otherapex",
3456 ],
Jooyung Han24282772020-03-21 23:20:55 +09003457 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003458 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003459 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003460 cc_library {
3461 name: "mylib2",
3462 srcs: ["mylib.cpp"],
3463 system_shared_libs: [],
3464 stl: "none",
3465 apex_available: [
3466 "myapex",
3467 "otherapex",
3468 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003469 static_libs: ["mylib3"],
3470 recovery_available: true,
3471 min_sdk_version: "29",
3472 }
3473 cc_library {
3474 name: "mylib3",
3475 srcs: ["mylib.cpp"],
3476 system_shared_libs: [],
3477 stl: "none",
3478 apex_available: [
3479 "myapex",
3480 "otherapex",
3481 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003482 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003483 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003484 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003485 `)
3486
Jooyung Hanc87a0592020-03-02 17:44:33 +09003487 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003488 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003489 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003490
Vinh Tranf9754732023-01-19 22:41:46 -05003491 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003492 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003493 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003494
Vinh Tranf9754732023-01-19 22:41:46 -05003495 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003496 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003497 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003498
Colin Crossaede88c2020-08-11 12:17:01 -07003499 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3500 // each variant defines additional macros to distinguish which apex variant it is built for
3501
3502 // non-APEX variant does not have __ANDROID_APEX__ defined
3503 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3504 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3505
Vinh Tranf9754732023-01-19 22:41:46 -05003506 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003507 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3508 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003509
Jooyung Hanc87a0592020-03-02 17:44:33 +09003510 // non-APEX variant does not have __ANDROID_APEX__ defined
3511 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3512 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3513
Vinh Tranf9754732023-01-19 22:41:46 -05003514 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003515 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003516 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003517}
Jiyong Park7e636d02019-01-28 16:16:54 +09003518
3519func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003520 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003521 apex {
3522 name: "myapex",
3523 key: "myapex.key",
3524 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003525 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003526 }
3527
3528 apex_key {
3529 name: "myapex.key",
3530 public_key: "testkey.avbpubkey",
3531 private_key: "testkey.pem",
3532 }
3533
3534 cc_library_headers {
3535 name: "mylib_headers",
3536 export_include_dirs: ["my_include"],
3537 system_shared_libs: [],
3538 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003539 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003540 }
3541
3542 cc_library {
3543 name: "mylib",
3544 srcs: ["mylib.cpp"],
3545 system_shared_libs: [],
3546 stl: "none",
3547 header_libs: ["mylib_headers"],
3548 export_header_lib_headers: ["mylib_headers"],
3549 stubs: {
3550 versions: ["1", "2", "3"],
3551 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003552 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003553 }
3554
3555 cc_library {
3556 name: "otherlib",
3557 srcs: ["mylib.cpp"],
3558 system_shared_libs: [],
3559 stl: "none",
3560 shared_libs: ["mylib"],
3561 }
3562 `)
3563
Colin Cross7113d202019-11-20 16:39:12 -08003564 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003565
3566 // Ensure that the include path of the header lib is exported to 'otherlib'
3567 ensureContains(t, cFlags, "-Imy_include")
3568}
Alex Light9670d332019-01-29 18:07:33 -08003569
Jiyong Park7cd10e32020-01-14 09:22:18 +09003570type fileInApex struct {
3571 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003572 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003573 isLink bool
3574}
3575
Jooyung Han1724d582022-12-21 10:17:44 +09003576func (f fileInApex) String() string {
3577 return f.src + ":" + f.path
3578}
3579
3580func (f fileInApex) match(expectation string) bool {
3581 parts := strings.Split(expectation, ":")
3582 if len(parts) == 1 {
3583 match, _ := path.Match(parts[0], f.path)
3584 return match
3585 }
3586 if len(parts) == 2 {
3587 matchSrc, _ := path.Match(parts[0], f.src)
3588 matchDst, _ := path.Match(parts[1], f.path)
3589 return matchSrc && matchDst
3590 }
3591 panic("invalid expected file specification: " + expectation)
3592}
3593
Jooyung Hana57af4a2020-01-23 05:36:59 +00003594func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003595 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003596 module := ctx.ModuleForTests(moduleName, variant)
3597 apexRule := module.MaybeRule("apexRule")
3598 apexDir := "/image.apex/"
Jooyung Han31c470b2019-10-18 16:26:59 +09003599 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003600 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003601 for _, cmd := range strings.Split(copyCmds, "&&") {
3602 cmd = strings.TrimSpace(cmd)
3603 if cmd == "" {
3604 continue
3605 }
3606 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003607 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003608 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003609 switch terms[0] {
3610 case "mkdir":
3611 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003612 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003613 t.Fatal("copyCmds contains invalid cp command", cmd)
3614 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003615 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003616 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003617 isLink = false
3618 case "ln":
3619 if len(terms) != 3 && len(terms) != 4 {
3620 // ln LINK TARGET or ln -s LINK TARGET
3621 t.Fatal("copyCmds contains invalid ln command", cmd)
3622 }
3623 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003624 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003625 isLink = true
3626 default:
3627 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3628 }
3629 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003630 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003631 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003632 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003633 }
Jooyung Han1724d582022-12-21 10:17:44 +09003634 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003635 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003636 }
3637 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003638 return ret
3639}
3640
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003641func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003642 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003643 var failed bool
3644 var surplus []string
3645 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003646 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003647 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003648 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003649 if file.match(expected) {
3650 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003651 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003652 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003653 }
3654 }
Jooyung Han1724d582022-12-21 10:17:44 +09003655 if !matchFound {
3656 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003657 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003658 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003659
Jooyung Han31c470b2019-10-18 16:26:59 +09003660 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003661 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003662 t.Log("surplus files", surplus)
3663 failed = true
3664 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003665
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003666 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003667 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003668 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003669 if !filesMatched[expected] {
3670 missing = append(missing, expected)
3671 }
3672 }
3673 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003674 t.Log("missing files", missing)
3675 failed = true
3676 }
3677 if failed {
3678 t.Fail()
3679 }
3680}
3681
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003682func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3683 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3684}
3685
3686func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
Spandan Das2069c3f2023-12-06 19:40:24 +00003687 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Description("deapex")
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003688 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3689 if deapexer.Output != nil {
3690 outputs = append(outputs, deapexer.Output.String())
3691 }
3692 for _, output := range deapexer.ImplicitOutputs {
3693 outputs = append(outputs, output.String())
3694 }
3695 actualFiles := make([]fileInApex, 0, len(outputs))
3696 for _, output := range outputs {
3697 dir := "/deapexer/"
3698 pos := strings.LastIndex(output, dir)
3699 if pos == -1 {
3700 t.Fatal("Unknown deapexer output ", output)
3701 }
3702 path := output[pos+len(dir):]
3703 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3704 }
3705 assertFileListEquals(t, files, actualFiles)
3706}
3707
Jooyung Han39edb6c2019-11-06 16:53:07 +09003708func vndkLibrariesTxtFiles(vers ...string) (result string) {
3709 for _, v := range vers {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +09003710 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Justin Yund5784122023-10-25 13:25:32 +09003711 result += `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003712 prebuilt_etc {
3713 name: "` + txt + `.libraries.` + v + `.txt",
3714 src: "dummy.txt",
3715 }
3716 `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003717 }
3718 }
3719 return
3720}
3721
Jooyung Han344d5432019-08-23 11:17:39 +09003722func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003723 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003724 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003725 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003726 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003727 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003728 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003729 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003730 }
3731
3732 apex_key {
3733 name: "myapex.key",
3734 public_key: "testkey.avbpubkey",
3735 private_key: "testkey.pem",
3736 }
3737
Jooyung Han31c470b2019-10-18 16:26:59 +09003738 vndk_prebuilt_shared {
3739 name: "libvndk27",
3740 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003741 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003742 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003743 vndk: {
3744 enabled: true,
3745 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003746 target_arch: "arm64",
3747 arch: {
3748 arm: {
3749 srcs: ["libvndk27_arm.so"],
3750 },
3751 arm64: {
3752 srcs: ["libvndk27_arm64.so"],
3753 },
3754 },
Colin Cross2807f002021-03-02 10:15:29 -08003755 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003756 }
3757
3758 vndk_prebuilt_shared {
3759 name: "libvndk27",
3760 version: "27",
3761 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003762 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003763 vndk: {
3764 enabled: true,
3765 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003766 target_arch: "x86_64",
3767 arch: {
3768 x86: {
3769 srcs: ["libvndk27_x86.so"],
3770 },
3771 x86_64: {
3772 srcs: ["libvndk27_x86_64.so"],
3773 },
3774 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003775 }
3776 `+vndkLibrariesTxtFiles("27"),
3777 withFiles(map[string][]byte{
3778 "libvndk27_arm.so": nil,
3779 "libvndk27_arm64.so": nil,
3780 "libvndk27_x86.so": nil,
3781 "libvndk27_x86_64.so": nil,
3782 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003783
Jooyung Hana0503a52023-08-23 13:12:50 +09003784 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003785 "lib/libvndk27_arm.so",
3786 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003787 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003788 })
Jooyung Han344d5432019-08-23 11:17:39 +09003789}
3790
Jooyung Han90eee022019-10-01 20:02:42 +09003791func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003792 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003793 apex_vndk {
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003794 name: "com.android.vndk.v29",
Jooyung Han90eee022019-10-01 20:02:42 +09003795 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003796 file_contexts: ":myapex-file_contexts",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003797 vndk_version: "29",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003798 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003799 }
3800 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003801 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003802 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003803 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003804 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003805 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003806 }
3807 apex_key {
3808 name: "myapex.key",
3809 public_key: "testkey.avbpubkey",
3810 private_key: "testkey.pem",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003811 }`+vndkLibrariesTxtFiles("28", "29"))
Jooyung Han90eee022019-10-01 20:02:42 +09003812
3813 assertApexName := func(expected, moduleName string) {
Jooyung Hana0503a52023-08-23 13:12:50 +09003814 module := ctx.ModuleForTests(moduleName, "android_common")
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003815 apexManifestRule := module.Rule("apexManifestRule")
3816 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003817 }
3818
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003819 assertApexName("com.android.vndk.v29", "com.android.vndk.v29")
Colin Cross2807f002021-03-02 10:15:29 -08003820 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003821}
3822
Jooyung Han344d5432019-08-23 11:17:39 +09003823func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003824 testApexError(t, `module "com.android.vndk.v30" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003825 apex_vndk {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003826 name: "com.android.vndk.v30",
3827 key: "com.android.vndk.v30.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003828 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003829 native_bridge_supported: true,
3830 }
3831
3832 apex_key {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003833 name: "com.android.vndk.v30.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003834 public_key: "testkey.avbpubkey",
3835 private_key: "testkey.pem",
3836 }
3837
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003838 vndk_prebuilt_shared {
Jooyung Han344d5432019-08-23 11:17:39 +09003839 name: "libvndk",
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003840 version: "30",
3841 target_arch: "arm",
Jooyung Han344d5432019-08-23 11:17:39 +09003842 srcs: ["mylib.cpp"],
3843 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003844 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003845 native_bridge_supported: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003846 vndk: {
3847 enabled: true,
3848 },
Jooyung Han344d5432019-08-23 11:17:39 +09003849 }
3850 `)
3851}
3852
Jooyung Han31c470b2019-10-18 16:26:59 +09003853func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003854 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003855 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003856 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003857 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003858 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003859 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003860 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003861 }
3862
3863 apex_key {
3864 name: "myapex.key",
3865 public_key: "testkey.avbpubkey",
3866 private_key: "testkey.pem",
3867 }
3868
3869 vndk_prebuilt_shared {
3870 name: "libvndk27",
3871 version: "27",
3872 target_arch: "arm",
3873 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003874 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003875 vndk: {
3876 enabled: true,
3877 },
3878 arch: {
3879 arm: {
3880 srcs: ["libvndk27.so"],
3881 }
3882 },
3883 }
3884
3885 vndk_prebuilt_shared {
3886 name: "libvndk27",
3887 version: "27",
3888 target_arch: "arm",
3889 binder32bit: true,
3890 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003891 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003892 vndk: {
3893 enabled: true,
3894 },
3895 arch: {
3896 arm: {
3897 srcs: ["libvndk27binder32.so"],
3898 }
3899 },
Colin Cross2807f002021-03-02 10:15:29 -08003900 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003901 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003902 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003903 withFiles(map[string][]byte{
3904 "libvndk27.so": nil,
3905 "libvndk27binder32.so": nil,
3906 }),
3907 withBinder32bit,
3908 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07003909 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09003910 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3911 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003912 },
3913 }),
3914 )
3915
Jooyung Hana0503a52023-08-23 13:12:50 +09003916 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003917 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003918 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003919 })
3920}
3921
Jooyung Hane1633032019-08-01 17:41:43 +09003922func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003923 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003924 apex {
3925 name: "myapex_nodep",
3926 key: "myapex.key",
3927 native_shared_libs: ["lib_nodep"],
3928 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003929 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003930 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003931 }
3932
3933 apex {
3934 name: "myapex_dep",
3935 key: "myapex.key",
3936 native_shared_libs: ["lib_dep"],
3937 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003938 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003939 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003940 }
3941
3942 apex {
3943 name: "myapex_provider",
3944 key: "myapex.key",
3945 native_shared_libs: ["libfoo"],
3946 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003947 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003948 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003949 }
3950
3951 apex {
3952 name: "myapex_selfcontained",
3953 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00003954 native_shared_libs: ["lib_dep_on_bar", "libbar"],
Jooyung Hane1633032019-08-01 17:41:43 +09003955 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003956 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003957 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003958 }
3959
3960 apex_key {
3961 name: "myapex.key",
3962 public_key: "testkey.avbpubkey",
3963 private_key: "testkey.pem",
3964 }
3965
3966 cc_library {
3967 name: "lib_nodep",
3968 srcs: ["mylib.cpp"],
3969 system_shared_libs: [],
3970 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003971 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003972 }
3973
3974 cc_library {
3975 name: "lib_dep",
3976 srcs: ["mylib.cpp"],
3977 shared_libs: ["libfoo"],
3978 system_shared_libs: [],
3979 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003980 apex_available: [
3981 "myapex_dep",
3982 "myapex_provider",
3983 "myapex_selfcontained",
3984 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003985 }
3986
3987 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00003988 name: "lib_dep_on_bar",
3989 srcs: ["mylib.cpp"],
3990 shared_libs: ["libbar"],
3991 system_shared_libs: [],
3992 stl: "none",
3993 apex_available: [
3994 "myapex_selfcontained",
3995 ],
3996 }
3997
3998
3999 cc_library {
Jooyung Hane1633032019-08-01 17:41:43 +09004000 name: "libfoo",
4001 srcs: ["mytest.cpp"],
4002 stubs: {
4003 versions: ["1"],
4004 },
4005 system_shared_libs: [],
4006 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004007 apex_available: [
4008 "myapex_provider",
Spandan Das20fce2d2023-04-12 17:21:39 +00004009 ],
4010 }
4011
4012 cc_library {
4013 name: "libbar",
4014 srcs: ["mytest.cpp"],
4015 stubs: {
4016 versions: ["1"],
4017 },
4018 system_shared_libs: [],
4019 stl: "none",
4020 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004021 "myapex_selfcontained",
4022 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004023 }
Spandan Das20fce2d2023-04-12 17:21:39 +00004024
Jooyung Hane1633032019-08-01 17:41:43 +09004025 `)
4026
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004027 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004028 var provideNativeLibs, requireNativeLibs []string
4029
Jooyung Hana0503a52023-08-23 13:12:50 +09004030 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004031 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4032 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004033 ensureListEmpty(t, provideNativeLibs)
4034 ensureListEmpty(t, requireNativeLibs)
4035
Jooyung Hana0503a52023-08-23 13:12:50 +09004036 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004037 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4038 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004039 ensureListEmpty(t, provideNativeLibs)
4040 ensureListContains(t, requireNativeLibs, "libfoo.so")
4041
Jooyung Hana0503a52023-08-23 13:12:50 +09004042 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004043 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4044 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004045 ensureListContains(t, provideNativeLibs, "libfoo.so")
4046 ensureListEmpty(t, requireNativeLibs)
4047
Jooyung Hana0503a52023-08-23 13:12:50 +09004048 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004049 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4050 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Spandan Das20fce2d2023-04-12 17:21:39 +00004051 ensureListContains(t, provideNativeLibs, "libbar.so")
Jooyung Hane1633032019-08-01 17:41:43 +09004052 ensureListEmpty(t, requireNativeLibs)
4053}
4054
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004055func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4056 ctx := testApex(t, `
4057 apex {
4058 name: "myapex",
4059 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004060 native_shared_libs: ["mylib"],
4061 updatable: false,
4062 }
4063
4064 apex_key {
4065 name: "myapex.key",
4066 public_key: "testkey.avbpubkey",
4067 private_key: "testkey.pem",
4068 }
4069
4070 cc_library {
4071 name: "mylib",
4072 srcs: ["mylib.cpp"],
4073 system_shared_libs: [],
4074 stl: "none",
4075 apex_available: [
4076 "//apex_available:platform",
4077 "myapex",
4078 ],
4079 }
4080 `, android.FixtureMergeEnv(map[string]string{
4081 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4082 }))
4083
Jooyung Hana0503a52023-08-23 13:12:50 +09004084 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004085 apexManifestRule := module.Rule("apexManifestRule")
4086 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4087}
4088
Vinh Tran8f5310f2022-10-07 18:16:47 -04004089func TestCompileMultilibProp(t *testing.T) {
4090 testCases := []struct {
4091 compileMultiLibProp string
4092 containedLibs []string
4093 notContainedLibs []string
4094 }{
4095 {
4096 containedLibs: []string{
4097 "image.apex/lib64/mylib.so",
4098 "image.apex/lib/mylib.so",
4099 },
4100 compileMultiLibProp: `compile_multilib: "both",`,
4101 },
4102 {
4103 containedLibs: []string{"image.apex/lib64/mylib.so"},
4104 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4105 compileMultiLibProp: `compile_multilib: "first",`,
4106 },
4107 {
4108 containedLibs: []string{"image.apex/lib64/mylib.so"},
4109 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4110 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4111 },
4112 {
4113 containedLibs: []string{"image.apex/lib64/mylib.so"},
4114 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4115 compileMultiLibProp: `compile_multilib: "64",`,
4116 },
4117 {
4118 containedLibs: []string{"image.apex/lib/mylib.so"},
4119 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4120 compileMultiLibProp: `compile_multilib: "32",`,
4121 },
4122 }
4123 for _, testCase := range testCases {
4124 ctx := testApex(t, fmt.Sprintf(`
4125 apex {
4126 name: "myapex",
4127 key: "myapex.key",
4128 %s
4129 native_shared_libs: ["mylib"],
4130 updatable: false,
4131 }
4132 apex_key {
4133 name: "myapex.key",
4134 public_key: "testkey.avbpubkey",
4135 private_key: "testkey.pem",
4136 }
4137 cc_library {
4138 name: "mylib",
4139 srcs: ["mylib.cpp"],
4140 apex_available: [
4141 "//apex_available:platform",
4142 "myapex",
4143 ],
4144 }
4145 `, testCase.compileMultiLibProp),
4146 )
Jooyung Hana0503a52023-08-23 13:12:50 +09004147 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Vinh Tran8f5310f2022-10-07 18:16:47 -04004148 apexRule := module.Rule("apexRule")
4149 copyCmds := apexRule.Args["copy_commands"]
4150 for _, containedLib := range testCase.containedLibs {
4151 ensureContains(t, copyCmds, containedLib)
4152 }
4153 for _, notContainedLib := range testCase.notContainedLibs {
4154 ensureNotContains(t, copyCmds, notContainedLib)
4155 }
4156 }
4157}
4158
Alex Light0851b882019-02-07 13:20:53 -08004159func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004160 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004161 apex {
4162 name: "myapex",
4163 key: "myapex.key",
4164 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004165 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004166 }
4167
4168 apex_key {
4169 name: "myapex.key",
4170 public_key: "testkey.avbpubkey",
4171 private_key: "testkey.pem",
4172 }
4173
4174 cc_library {
4175 name: "mylib_common",
4176 srcs: ["mylib.cpp"],
4177 system_shared_libs: [],
4178 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004179 apex_available: [
4180 "//apex_available:platform",
4181 "myapex",
4182 ],
Alex Light0851b882019-02-07 13:20:53 -08004183 }
4184 `)
4185
Jooyung Hana0503a52023-08-23 13:12:50 +09004186 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004187 apexRule := module.Rule("apexRule")
4188 copyCmds := apexRule.Args["copy_commands"]
4189
4190 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4191 t.Log("Apex was a test apex!")
4192 t.Fail()
4193 }
4194 // Ensure that main rule creates an output
4195 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4196
4197 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004198 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004199
4200 // Ensure that both direct and indirect deps are copied into apex
4201 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4202
Colin Cross7113d202019-11-20 16:39:12 -08004203 // Ensure that the platform variant ends with _shared
4204 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004205
Colin Cross56a83212020-09-15 18:30:11 -07004206 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004207 t.Log("Found mylib_common not in any apex!")
4208 t.Fail()
4209 }
4210}
4211
4212func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004213 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004214 apex_test {
4215 name: "myapex",
4216 key: "myapex.key",
4217 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004218 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004219 }
4220
4221 apex_key {
4222 name: "myapex.key",
4223 public_key: "testkey.avbpubkey",
4224 private_key: "testkey.pem",
4225 }
4226
4227 cc_library {
4228 name: "mylib_common_test",
4229 srcs: ["mylib.cpp"],
4230 system_shared_libs: [],
4231 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004232 // TODO: remove //apex_available:platform
4233 apex_available: [
4234 "//apex_available:platform",
4235 "myapex",
4236 ],
Alex Light0851b882019-02-07 13:20:53 -08004237 }
4238 `)
4239
Jooyung Hana0503a52023-08-23 13:12:50 +09004240 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004241 apexRule := module.Rule("apexRule")
4242 copyCmds := apexRule.Args["copy_commands"]
4243
4244 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4245 t.Log("Apex was not a test apex!")
4246 t.Fail()
4247 }
4248 // Ensure that main rule creates an output
4249 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4250
4251 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004252 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004253
4254 // Ensure that both direct and indirect deps are copied into apex
4255 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4256
Colin Cross7113d202019-11-20 16:39:12 -08004257 // Ensure that the platform variant ends with _shared
4258 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004259}
4260
Jooyung Han85707de2023-12-01 14:21:13 +09004261func TestLibzVendorIsntStable(t *testing.T) {
4262 ctx := testApex(t, `
4263 apex {
4264 name: "myapex",
4265 key: "myapex.key",
4266 updatable: false,
4267 binaries: ["mybin"],
4268 }
4269 apex {
4270 name: "myvendorapex",
4271 key: "myapex.key",
4272 file_contexts: "myvendorapex_file_contexts",
4273 vendor: true,
4274 updatable: false,
4275 binaries: ["mybin"],
4276 }
4277 apex_key {
4278 name: "myapex.key",
4279 public_key: "testkey.avbpubkey",
4280 private_key: "testkey.pem",
4281 }
4282 cc_binary {
4283 name: "mybin",
4284 vendor_available: true,
4285 system_shared_libs: [],
4286 stl: "none",
4287 shared_libs: ["libz"],
4288 apex_available: ["//apex_available:anyapex"],
4289 }
4290 cc_library {
4291 name: "libz",
4292 vendor_available: true,
4293 system_shared_libs: [],
4294 stl: "none",
4295 stubs: {
4296 versions: ["28", "30"],
4297 },
4298 target: {
4299 vendor: {
4300 no_stubs: true,
4301 },
4302 },
4303 }
4304 `, withFiles(map[string][]byte{
4305 "myvendorapex_file_contexts": nil,
4306 }))
4307
4308 // libz provides stubs for core variant.
4309 {
4310 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
4311 "bin/mybin",
4312 })
4313 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
4314 android.AssertStringEquals(t, "should require libz", apexManifestRule.Args["requireNativeLibs"], "libz.so")
4315 }
4316 // libz doesn't provide stubs for vendor variant.
4317 {
4318 ensureExactContents(t, ctx, "myvendorapex", "android_common_myvendorapex", []string{
4319 "bin/mybin",
4320 "lib64/libz.so",
4321 })
4322 apexManifestRule := ctx.ModuleForTests("myvendorapex", "android_common_myvendorapex").Rule("apexManifestRule")
4323 android.AssertStringEquals(t, "should not require libz", apexManifestRule.Args["requireNativeLibs"], "")
4324 }
4325}
4326
Alex Light9670d332019-01-29 18:07:33 -08004327func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004328 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004329 apex {
4330 name: "myapex",
4331 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004332 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004333 multilib: {
4334 first: {
4335 native_shared_libs: ["mylib_common"],
4336 }
4337 },
4338 target: {
4339 android: {
4340 multilib: {
4341 first: {
4342 native_shared_libs: ["mylib"],
4343 }
4344 }
4345 },
4346 host: {
4347 multilib: {
4348 first: {
4349 native_shared_libs: ["mylib2"],
4350 }
4351 }
4352 }
4353 }
4354 }
4355
4356 apex_key {
4357 name: "myapex.key",
4358 public_key: "testkey.avbpubkey",
4359 private_key: "testkey.pem",
4360 }
4361
4362 cc_library {
4363 name: "mylib",
4364 srcs: ["mylib.cpp"],
4365 system_shared_libs: [],
4366 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004367 // TODO: remove //apex_available:platform
4368 apex_available: [
4369 "//apex_available:platform",
4370 "myapex",
4371 ],
Alex Light9670d332019-01-29 18:07:33 -08004372 }
4373
4374 cc_library {
4375 name: "mylib_common",
4376 srcs: ["mylib.cpp"],
4377 system_shared_libs: [],
4378 stl: "none",
4379 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004380 // TODO: remove //apex_available:platform
4381 apex_available: [
4382 "//apex_available:platform",
4383 "myapex",
4384 ],
Alex Light9670d332019-01-29 18:07:33 -08004385 }
4386
4387 cc_library {
4388 name: "mylib2",
4389 srcs: ["mylib.cpp"],
4390 system_shared_libs: [],
4391 stl: "none",
4392 compile_multilib: "first",
4393 }
4394 `)
4395
Jooyung Hana0503a52023-08-23 13:12:50 +09004396 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004397 copyCmds := apexRule.Args["copy_commands"]
4398
4399 // Ensure that main rule creates an output
4400 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4401
4402 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004403 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4404 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4405 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004406
4407 // Ensure that both direct and indirect deps are copied into apex
4408 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4409 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4410 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4411
Colin Cross7113d202019-11-20 16:39:12 -08004412 // Ensure that the platform variant ends with _shared
4413 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4414 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4415 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004416}
Jiyong Park04480cf2019-02-06 00:16:29 +09004417
Jiyong Park59140302020-12-14 18:44:04 +09004418func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004419 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004420 apex {
4421 name: "myapex",
4422 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004423 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004424 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004425 arch: {
4426 arm64: {
4427 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004428 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004429 },
4430 x86_64: {
4431 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004432 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004433 },
4434 }
4435 }
4436
4437 apex_key {
4438 name: "myapex.key",
4439 public_key: "testkey.avbpubkey",
4440 private_key: "testkey.pem",
4441 }
4442
4443 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004444 name: "mylib.generic",
4445 srcs: ["mylib.cpp"],
4446 system_shared_libs: [],
4447 stl: "none",
4448 // TODO: remove //apex_available:platform
4449 apex_available: [
4450 "//apex_available:platform",
4451 "myapex",
4452 ],
4453 }
4454
4455 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004456 name: "mylib.arm64",
4457 srcs: ["mylib.cpp"],
4458 system_shared_libs: [],
4459 stl: "none",
4460 // TODO: remove //apex_available:platform
4461 apex_available: [
4462 "//apex_available:platform",
4463 "myapex",
4464 ],
4465 }
4466
4467 cc_library {
4468 name: "mylib.x64",
4469 srcs: ["mylib.cpp"],
4470 system_shared_libs: [],
4471 stl: "none",
4472 // TODO: remove //apex_available:platform
4473 apex_available: [
4474 "//apex_available:platform",
4475 "myapex",
4476 ],
4477 }
4478 `)
4479
Jooyung Hana0503a52023-08-23 13:12:50 +09004480 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park59140302020-12-14 18:44:04 +09004481 copyCmds := apexRule.Args["copy_commands"]
4482
4483 // Ensure that apex variant is created for the direct dep
4484 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004485 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004486 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4487
4488 // Ensure that both direct and indirect deps are copied into apex
4489 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4490 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4491}
4492
Jiyong Park04480cf2019-02-06 00:16:29 +09004493func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004494 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004495 apex {
4496 name: "myapex",
4497 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004498 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004499 updatable: false,
Riya Thakur654461c2024-02-27 07:21:05 +00004500 compile_multilib: "both",
Jiyong Park04480cf2019-02-06 00:16:29 +09004501 }
4502
4503 apex_key {
4504 name: "myapex.key",
4505 public_key: "testkey.avbpubkey",
4506 private_key: "testkey.pem",
4507 }
4508
4509 sh_binary {
4510 name: "myscript",
4511 src: "mylib.cpp",
4512 filename: "myscript.sh",
4513 sub_dir: "script",
4514 }
4515 `)
4516
Jooyung Hana0503a52023-08-23 13:12:50 +09004517 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004518 copyCmds := apexRule.Args["copy_commands"]
4519
4520 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4521}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004522
Jooyung Han91df2082019-11-20 01:49:42 +09004523func TestApexInVariousPartition(t *testing.T) {
4524 testcases := []struct {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004525 propName, partition string
Jooyung Han91df2082019-11-20 01:49:42 +09004526 }{
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004527 {"", "system"},
4528 {"product_specific: true", "product"},
4529 {"soc_specific: true", "vendor"},
4530 {"proprietary: true", "vendor"},
4531 {"vendor: true", "vendor"},
4532 {"system_ext_specific: true", "system_ext"},
Jooyung Han91df2082019-11-20 01:49:42 +09004533 }
4534 for _, tc := range testcases {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004535 t.Run(tc.propName+":"+tc.partition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004536 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004537 apex {
4538 name: "myapex",
4539 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004540 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004541 `+tc.propName+`
4542 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004543
Jooyung Han91df2082019-11-20 01:49:42 +09004544 apex_key {
4545 name: "myapex.key",
4546 public_key: "testkey.avbpubkey",
4547 private_key: "testkey.pem",
4548 }
4549 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004550
Jooyung Hana0503a52023-08-23 13:12:50 +09004551 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004552 expected := "out/soong/target/product/test_device/" + tc.partition + "/apex"
Paul Duffin37ba3442021-03-29 00:21:08 +01004553 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004554 if actual != expected {
4555 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4556 }
Jooyung Han91df2082019-11-20 01:49:42 +09004557 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004558 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004559}
Jiyong Park67882562019-03-21 01:11:21 +09004560
Jooyung Han580eb4f2020-06-24 19:33:06 +09004561func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004562 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004563 apex {
4564 name: "myapex",
4565 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004566 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004567 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004568
Jooyung Han580eb4f2020-06-24 19:33:06 +09004569 apex_key {
4570 name: "myapex.key",
4571 public_key: "testkey.avbpubkey",
4572 private_key: "testkey.pem",
4573 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004574 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09004575 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004576 rule := module.Output("file_contexts")
4577 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4578}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004579
Jooyung Han580eb4f2020-06-24 19:33:06 +09004580func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004581 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004582 apex {
4583 name: "myapex",
4584 key: "myapex.key",
4585 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004586 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004587 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004588
Jooyung Han580eb4f2020-06-24 19:33:06 +09004589 apex_key {
4590 name: "myapex.key",
4591 public_key: "testkey.avbpubkey",
4592 private_key: "testkey.pem",
4593 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004594 `, withFiles(map[string][]byte{
4595 "my_own_file_contexts": nil,
4596 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004597}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004598
Jooyung Han580eb4f2020-06-24 19:33:06 +09004599func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004600 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004601 apex {
4602 name: "myapex",
4603 key: "myapex.key",
4604 product_specific: true,
4605 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004606 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004607 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004608
Jooyung Han580eb4f2020-06-24 19:33:06 +09004609 apex_key {
4610 name: "myapex.key",
4611 public_key: "testkey.avbpubkey",
4612 private_key: "testkey.pem",
4613 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004614 `)
4615
Colin Cross1c460562021-02-16 17:55:47 -08004616 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004617 apex {
4618 name: "myapex",
4619 key: "myapex.key",
4620 product_specific: true,
4621 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004622 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004623 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004624
Jooyung Han580eb4f2020-06-24 19:33:06 +09004625 apex_key {
4626 name: "myapex.key",
4627 public_key: "testkey.avbpubkey",
4628 private_key: "testkey.pem",
4629 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004630 `, withFiles(map[string][]byte{
4631 "product_specific_file_contexts": nil,
4632 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004633 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004634 rule := module.Output("file_contexts")
4635 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4636}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004637
Jooyung Han580eb4f2020-06-24 19:33:06 +09004638func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004639 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004640 apex {
4641 name: "myapex",
4642 key: "myapex.key",
4643 product_specific: true,
4644 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004645 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004646 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004647
Jooyung Han580eb4f2020-06-24 19:33:06 +09004648 apex_key {
4649 name: "myapex.key",
4650 public_key: "testkey.avbpubkey",
4651 private_key: "testkey.pem",
4652 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004653
Jooyung Han580eb4f2020-06-24 19:33:06 +09004654 filegroup {
4655 name: "my-file-contexts",
4656 srcs: ["product_specific_file_contexts"],
4657 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004658 `, withFiles(map[string][]byte{
4659 "product_specific_file_contexts": nil,
4660 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004661 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004662 rule := module.Output("file_contexts")
4663 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004664}
4665
Jiyong Park67882562019-03-21 01:11:21 +09004666func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004667 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004668 apex_key {
4669 name: "myapex.key",
4670 public_key: ":my.avbpubkey",
4671 private_key: ":my.pem",
4672 product_specific: true,
4673 }
4674
4675 filegroup {
4676 name: "my.avbpubkey",
4677 srcs: ["testkey2.avbpubkey"],
4678 }
4679
4680 filegroup {
4681 name: "my.pem",
4682 srcs: ["testkey2.pem"],
4683 }
4684 `)
4685
4686 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4687 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004688 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004689 if actual_pubkey != expected_pubkey {
4690 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4691 }
4692 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004693 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004694 if actual_privkey != expected_privkey {
4695 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4696 }
4697}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004698
4699func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004700 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004701 prebuilt_apex {
4702 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004703 arch: {
4704 arm64: {
4705 src: "myapex-arm64.apex",
4706 },
4707 arm: {
4708 src: "myapex-arm.apex",
4709 },
4710 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004711 }
4712 `)
4713
Wei Li340ee8e2022-03-18 17:33:24 -07004714 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4715 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004716
Jiyong Parkc95714e2019-03-29 14:23:10 +09004717 expectedInput := "myapex-arm64.apex"
4718 if prebuilt.inputApex.String() != expectedInput {
4719 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4720 }
Wei Li340ee8e2022-03-18 17:33:24 -07004721 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4722 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4723 rule := testingModule.Rule("genProvenanceMetaData")
4724 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4725 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4726 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4727 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08004728
4729 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
4730 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004731}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004732
Paul Duffinc0609c62021-03-01 17:27:16 +00004733func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004734 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004735 prebuilt_apex {
4736 name: "myapex",
4737 }
4738 `)
4739}
4740
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004741func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004742 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004743 prebuilt_apex {
4744 name: "myapex",
4745 src: "myapex-arm.apex",
4746 filename: "notmyapex.apex",
4747 }
4748 `)
4749
Wei Li340ee8e2022-03-18 17:33:24 -07004750 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4751 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004752
4753 expected := "notmyapex.apex"
4754 if p.installFilename != expected {
4755 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4756 }
Wei Li340ee8e2022-03-18 17:33:24 -07004757 rule := testingModule.Rule("genProvenanceMetaData")
4758 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4759 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4760 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4761 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004762}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004763
Samiul Islam7c02e262021-09-08 17:48:28 +01004764func TestApexSetFilenameOverride(t *testing.T) {
4765 testApex(t, `
4766 apex_set {
4767 name: "com.company.android.myapex",
4768 apex_name: "com.android.myapex",
4769 set: "company-myapex.apks",
4770 filename: "com.company.android.myapex.apex"
4771 }
4772 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4773
4774 testApex(t, `
4775 apex_set {
4776 name: "com.company.android.myapex",
4777 apex_name: "com.android.myapex",
4778 set: "company-myapex.apks",
4779 filename: "com.company.android.myapex.capex"
4780 }
4781 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4782
4783 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4784 apex_set {
4785 name: "com.company.android.myapex",
4786 apex_name: "com.android.myapex",
4787 set: "company-myapex.apks",
4788 filename: "some-random-suffix"
4789 }
4790 `)
4791}
4792
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004793func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004794 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004795 prebuilt_apex {
4796 name: "myapex.prebuilt",
4797 src: "myapex-arm.apex",
4798 overrides: [
4799 "myapex",
4800 ],
4801 }
4802 `)
4803
Wei Li340ee8e2022-03-18 17:33:24 -07004804 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4805 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004806
4807 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004808 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004809 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004810 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004811 }
Wei Li340ee8e2022-03-18 17:33:24 -07004812 rule := testingModule.Rule("genProvenanceMetaData")
4813 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4814 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4815 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4816 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004817}
4818
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004819func TestPrebuiltApexName(t *testing.T) {
4820 testApex(t, `
4821 prebuilt_apex {
4822 name: "com.company.android.myapex",
4823 apex_name: "com.android.myapex",
4824 src: "company-myapex-arm.apex",
4825 }
4826 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4827
4828 testApex(t, `
4829 apex_set {
4830 name: "com.company.android.myapex",
4831 apex_name: "com.android.myapex",
4832 set: "company-myapex.apks",
4833 }
4834 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4835}
4836
4837func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4838 _ = android.GroupFixturePreparers(
4839 java.PrepareForTestWithJavaDefaultModules,
4840 PrepareForTestWithApexBuildComponents,
4841 android.FixtureWithRootAndroidBp(`
4842 platform_bootclasspath {
4843 name: "platform-bootclasspath",
4844 fragments: [
4845 {
4846 apex: "com.android.art",
4847 module: "art-bootclasspath-fragment",
4848 },
4849 ],
4850 }
4851
4852 prebuilt_apex {
4853 name: "com.company.android.art",
4854 apex_name: "com.android.art",
4855 src: "com.company.android.art-arm.apex",
4856 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4857 }
4858
4859 prebuilt_bootclasspath_fragment {
4860 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004861 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004862 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004863 hidden_api: {
4864 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4865 metadata: "my-bootclasspath-fragment/metadata.csv",
4866 index: "my-bootclasspath-fragment/index.csv",
4867 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4868 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4869 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004870 }
4871
4872 java_import {
4873 name: "core-oj",
4874 jars: ["prebuilt.jar"],
4875 }
4876 `),
4877 ).RunTest(t)
4878}
4879
Spandan Das59a4a2b2024-01-09 21:35:56 +00004880// A minimal context object for use with DexJarBuildPath
4881type moduleErrorfTestCtx struct {
4882}
4883
4884func (ctx moduleErrorfTestCtx) ModuleErrorf(format string, args ...interface{}) {
4885}
4886
Paul Duffin092153d2021-01-26 11:42:39 +00004887// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4888// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004889func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004890 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004891
Paul Duffin89886cb2021-02-05 16:44:03 +00004892 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004893 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004894 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004895 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +00004896 dexJarBuildPath := p.DexJarBuildPath(moduleErrorfTestCtx{}).PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004897 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004898 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
Spandan Das3576e762024-01-03 18:57:03 +00004899 ".intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
Jeongik Chad5fe8782021-07-08 01:13:11 +09004900 android.NormalizePathForTesting(dexJarBuildPath))
4901 }
4902
4903 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004904 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004905 // Make sure the import has been given the correct path to the dex jar.
4906 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4907 dexJarBuildPath := p.DexJarInstallPath()
4908 stem := android.RemoveOptionalPrebuiltPrefix(name)
4909 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4910 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4911 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004912 }
4913
Paul Duffin39853512021-02-26 11:09:39 +00004914 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004915 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004916 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004917 android.AssertArrayString(t, "Check if there is no source variant",
4918 []string{"android_common"},
4919 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004920 }
4921
4922 t.Run("prebuilt only", func(t *testing.T) {
4923 bp := `
4924 prebuilt_apex {
4925 name: "myapex",
4926 arch: {
4927 arm64: {
4928 src: "myapex-arm64.apex",
4929 },
4930 arm: {
4931 src: "myapex-arm.apex",
4932 },
4933 },
Paul Duffin39853512021-02-26 11:09:39 +00004934 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004935 }
4936
4937 java_import {
4938 name: "libfoo",
4939 jars: ["libfoo.jar"],
4940 }
Paul Duffin39853512021-02-26 11:09:39 +00004941
4942 java_sdk_library_import {
4943 name: "libbar",
4944 public: {
4945 jars: ["libbar.jar"],
4946 },
4947 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004948 `
4949
4950 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4951 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4952
Spandan Das3576e762024-01-03 18:57:03 +00004953 deapexerName := deapexerModuleName("prebuilt_myapex")
4954 android.AssertStringEquals(t, "APEX module name from deapexer name", "prebuilt_myapex", apexModuleName(deapexerName))
Martin Stjernholm44825602021-09-17 01:44:12 +01004955
Paul Duffinf6932af2021-02-26 18:21:56 +00004956 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004957 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004958 rule := deapexer.Rule("deapexer")
4959 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4960 t.Errorf("expected: %q, found: %q", expected, actual)
4961 }
4962
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004963 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004964 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004965 rule = prebuiltApex.Rule("android/soong/android.Cp")
4966 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4967 t.Errorf("expected: %q, found: %q", expected, actual)
4968 }
4969
Paul Duffin89886cb2021-02-05 16:44:03 +00004970 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004971 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004972
4973 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004974 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004975 })
4976
4977 t.Run("prebuilt with source preferred", func(t *testing.T) {
4978
4979 bp := `
4980 prebuilt_apex {
4981 name: "myapex",
4982 arch: {
4983 arm64: {
4984 src: "myapex-arm64.apex",
4985 },
4986 arm: {
4987 src: "myapex-arm.apex",
4988 },
4989 },
Paul Duffin39853512021-02-26 11:09:39 +00004990 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004991 }
4992
4993 java_import {
4994 name: "libfoo",
4995 jars: ["libfoo.jar"],
4996 }
4997
4998 java_library {
4999 name: "libfoo",
5000 }
Paul Duffin39853512021-02-26 11:09:39 +00005001
5002 java_sdk_library_import {
5003 name: "libbar",
5004 public: {
5005 jars: ["libbar.jar"],
5006 },
5007 }
5008
5009 java_sdk_library {
5010 name: "libbar",
5011 srcs: ["foo/bar/MyClass.java"],
5012 unsafe_ignore_missing_latest_api: true,
5013 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005014 `
5015
5016 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5017 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5018
Paul Duffin89886cb2021-02-05 16:44:03 +00005019 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005020 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005021 ensureNoSourceVariant(t, ctx, "libfoo")
5022
5023 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005024 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005025 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005026 })
5027
5028 t.Run("prebuilt preferred with source", func(t *testing.T) {
5029 bp := `
5030 prebuilt_apex {
5031 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005032 arch: {
5033 arm64: {
5034 src: "myapex-arm64.apex",
5035 },
5036 arm: {
5037 src: "myapex-arm.apex",
5038 },
5039 },
Paul Duffin39853512021-02-26 11:09:39 +00005040 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005041 }
5042
5043 java_import {
5044 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005045 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005046 jars: ["libfoo.jar"],
5047 }
5048
5049 java_library {
5050 name: "libfoo",
5051 }
Paul Duffin39853512021-02-26 11:09:39 +00005052
5053 java_sdk_library_import {
5054 name: "libbar",
5055 prefer: true,
5056 public: {
5057 jars: ["libbar.jar"],
5058 },
5059 }
5060
5061 java_sdk_library {
5062 name: "libbar",
5063 srcs: ["foo/bar/MyClass.java"],
5064 unsafe_ignore_missing_latest_api: true,
5065 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005066 `
5067
5068 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5069 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5070
Paul Duffin89886cb2021-02-05 16:44:03 +00005071 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005072 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005073 ensureNoSourceVariant(t, ctx, "libfoo")
5074
5075 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005076 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005077 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005078 })
5079}
5080
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005081func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005082 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005083 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005084 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5085 // is disabled.
5086 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
Spandan Das81fe4d12024-05-15 18:43:47 +00005087
5088 // Make sure that we have atleast one platform library so that we can check the monolithic hiddenapi
5089 // file creation.
5090 java.FixtureConfigureBootJars("platform:foo"),
5091 android.FixtureModifyMockFS(func(fs android.MockFS) {
5092 fs["platform/Android.bp"] = []byte(`
5093 java_library {
5094 name: "foo",
5095 srcs: ["Test.java"],
5096 compile_dex: true,
5097 }
5098 `)
5099 fs["platform/Test.java"] = nil
5100 }),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005101 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005102
Paul Duffin37856732021-02-26 14:24:15 +00005103 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5104 t.Helper()
Jiakai Zhangc6879f32023-11-06 16:31:19 +00005105 s := ctx.ModuleForTests("dex_bootjars", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005106 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005107 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005108 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005109 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005110 foundLibfooJar = true
5111 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005112 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005113 }
5114 }
5115 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005116 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005117 }
5118 }
5119
Paul Duffin40a3f652021-07-19 13:11:24 +01005120 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005121 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005122 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005123 var rule android.TestingBuildParams
5124
5125 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5126 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005127 }
5128
Paul Duffin40a3f652021-07-19 13:11:24 +01005129 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5130 t.Helper()
5131 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5132 var rule android.TestingBuildParams
5133
5134 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5135 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5136 }
5137
Paul Duffin89f570a2021-06-16 01:42:33 +01005138 fragment := java.ApexVariantReference{
5139 Apex: proptools.StringPtr("myapex"),
5140 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5141 }
5142
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005143 t.Run("prebuilt only", func(t *testing.T) {
5144 bp := `
5145 prebuilt_apex {
5146 name: "myapex",
5147 arch: {
5148 arm64: {
5149 src: "myapex-arm64.apex",
5150 },
5151 arm: {
5152 src: "myapex-arm.apex",
5153 },
5154 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005155 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5156 }
5157
5158 prebuilt_bootclasspath_fragment {
5159 name: "my-bootclasspath-fragment",
5160 contents: ["libfoo", "libbar"],
5161 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005162 hidden_api: {
5163 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5164 metadata: "my-bootclasspath-fragment/metadata.csv",
5165 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005166 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5167 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5168 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005169 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005170 }
5171
5172 java_import {
5173 name: "libfoo",
5174 jars: ["libfoo.jar"],
5175 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005176 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005177 }
Paul Duffin37856732021-02-26 14:24:15 +00005178
5179 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 Duffin37856732021-02-26 14:24:15 +00005187 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005188 `
5189
Paul Duffin89f570a2021-06-16 01:42:33 +01005190 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005191 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5192 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005193
Paul Duffin537ea3d2021-05-14 10:38:00 +01005194 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005195 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005196 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005197 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005198 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005199 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 +01005200 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005201 })
5202
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005203 t.Run("apex_set only", func(t *testing.T) {
5204 bp := `
5205 apex_set {
5206 name: "myapex",
5207 set: "myapex.apks",
Liz Kammer2dc72442023-04-20 10:10:48 -04005208 exported_java_libs: ["myjavalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005209 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
Liz Kammer2dc72442023-04-20 10:10:48 -04005210 exported_systemserverclasspath_fragments: ["my-systemserverclasspath-fragment"],
5211 }
5212
5213 java_import {
5214 name: "myjavalib",
5215 jars: ["myjavalib.jar"],
5216 apex_available: ["myapex"],
5217 permitted_packages: ["javalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005218 }
5219
5220 prebuilt_bootclasspath_fragment {
5221 name: "my-bootclasspath-fragment",
5222 contents: ["libfoo", "libbar"],
5223 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005224 hidden_api: {
5225 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5226 metadata: "my-bootclasspath-fragment/metadata.csv",
5227 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005228 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5229 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5230 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005231 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005232 }
5233
Liz Kammer2dc72442023-04-20 10:10:48 -04005234 prebuilt_systemserverclasspath_fragment {
5235 name: "my-systemserverclasspath-fragment",
5236 contents: ["libbaz"],
5237 apex_available: ["myapex"],
5238 }
5239
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005240 java_import {
5241 name: "libfoo",
5242 jars: ["libfoo.jar"],
5243 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005244 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005245 }
5246
5247 java_sdk_library_import {
5248 name: "libbar",
5249 public: {
5250 jars: ["libbar.jar"],
5251 },
5252 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005253 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005254 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005255 }
Liz Kammer2dc72442023-04-20 10:10:48 -04005256
5257 java_sdk_library_import {
5258 name: "libbaz",
5259 public: {
5260 jars: ["libbaz.jar"],
5261 },
5262 apex_available: ["myapex"],
5263 shared_library: false,
5264 permitted_packages: ["baz"],
5265 }
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005266 `
5267
Paul Duffin89f570a2021-06-16 01:42:33 +01005268 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005269 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5270 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005271
Paul Duffin537ea3d2021-05-14 10:38:00 +01005272 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005273 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005274 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005275 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005276 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005277 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 +01005278 `)
Liz Kammer2dc72442023-04-20 10:10:48 -04005279
5280 myApex := ctx.ModuleForTests("myapex", "android_common_myapex").Module()
5281
5282 overrideNames := []string{
Spandan Dasa8e2d612024-07-26 19:24:27 +00005283 "",
Liz Kammer2dc72442023-04-20 10:10:48 -04005284 "myjavalib.myapex",
5285 "libfoo.myapex",
5286 "libbar.myapex",
5287 "libbaz.myapex",
5288 }
5289 mkEntries := android.AndroidMkEntriesForTest(t, ctx, myApex)
5290 for i, e := range mkEntries {
5291 g := e.OverrideName
5292 if w := overrideNames[i]; w != g {
5293 t.Errorf("Expected override name %q, got %q", w, g)
5294 }
5295 }
5296
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005297 })
5298
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005299 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5300 bp := `
5301 prebuilt_apex {
5302 name: "myapex",
5303 arch: {
5304 arm64: {
5305 src: "myapex-arm64.apex",
5306 },
5307 arm: {
5308 src: "myapex-arm.apex",
5309 },
5310 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005311 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5312 }
5313
5314 prebuilt_bootclasspath_fragment {
5315 name: "my-bootclasspath-fragment",
5316 contents: ["libfoo", "libbar"],
5317 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005318 hidden_api: {
5319 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5320 metadata: "my-bootclasspath-fragment/metadata.csv",
5321 index: "my-bootclasspath-fragment/index.csv",
5322 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5323 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5324 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005325 }
5326
5327 java_import {
5328 name: "libfoo",
5329 jars: ["libfoo.jar"],
5330 apex_available: ["myapex"],
5331 }
5332
5333 java_library {
5334 name: "libfoo",
5335 srcs: ["foo/bar/MyClass.java"],
5336 apex_available: ["myapex"],
5337 }
Paul Duffin37856732021-02-26 14:24:15 +00005338
5339 java_sdk_library_import {
5340 name: "libbar",
5341 public: {
5342 jars: ["libbar.jar"],
5343 },
5344 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005345 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005346 }
5347
5348 java_sdk_library {
5349 name: "libbar",
5350 srcs: ["foo/bar/MyClass.java"],
5351 unsafe_ignore_missing_latest_api: true,
5352 apex_available: ["myapex"],
5353 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005354 `
5355
5356 // In this test the source (java_library) libfoo is active since the
5357 // prebuilt (java_import) defaults to prefer:false. However the
5358 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5359 // find the dex boot jar in it. We either need to disable the source libfoo
5360 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005361 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005362 // dexbootjar check is skipped if AllowMissingDependencies is true
5363 preparerAllowMissingDeps := android.GroupFixturePreparers(
5364 preparer,
5365 android.PrepareForTestWithAllowMissingDependencies,
5366 )
5367 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005368 })
5369
5370 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5371 bp := `
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005372 apex {
5373 name: "myapex",
5374 key: "myapex.key",
5375 updatable: false,
5376 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5377 }
5378
5379 apex_key {
5380 name: "myapex.key",
5381 public_key: "testkey.avbpubkey",
5382 private_key: "testkey.pem",
5383 }
5384
5385 bootclasspath_fragment {
5386 name: "my-bootclasspath-fragment",
5387 contents: ["libfoo", "libbar"],
5388 apex_available: ["myapex"],
5389 hidden_api: {
5390 split_packages: ["*"],
5391 },
5392 }
5393
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005394 prebuilt_apex {
5395 name: "myapex",
5396 arch: {
5397 arm64: {
5398 src: "myapex-arm64.apex",
5399 },
5400 arm: {
5401 src: "myapex-arm.apex",
5402 },
5403 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005404 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5405 }
5406
5407 prebuilt_bootclasspath_fragment {
5408 name: "my-bootclasspath-fragment",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005409 prefer: true,
Paul Duffin89f570a2021-06-16 01:42:33 +01005410 contents: ["libfoo", "libbar"],
5411 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005412 hidden_api: {
5413 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5414 metadata: "my-bootclasspath-fragment/metadata.csv",
5415 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005416 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5417 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5418 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005419 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005420 }
5421
5422 java_import {
5423 name: "libfoo",
5424 prefer: true,
5425 jars: ["libfoo.jar"],
5426 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005427 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005428 }
5429
5430 java_library {
5431 name: "libfoo",
5432 srcs: ["foo/bar/MyClass.java"],
5433 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005434 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005435 }
Paul Duffin37856732021-02-26 14:24:15 +00005436
5437 java_sdk_library_import {
5438 name: "libbar",
5439 prefer: true,
5440 public: {
5441 jars: ["libbar.jar"],
5442 },
5443 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005444 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005445 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005446 }
5447
5448 java_sdk_library {
5449 name: "libbar",
5450 srcs: ["foo/bar/MyClass.java"],
5451 unsafe_ignore_missing_latest_api: true,
5452 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005453 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005454 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005455 `
5456
Paul Duffin89f570a2021-06-16 01:42:33 +01005457 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005458 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5459 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005460
Paul Duffin537ea3d2021-05-14 10:38:00 +01005461 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005462 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005463 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005464 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005465 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005466 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 +01005467 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005468 })
5469
5470 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5471 bp := `
5472 apex {
5473 name: "myapex",
5474 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005475 updatable: false,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005476 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005477 }
5478
5479 apex_key {
5480 name: "myapex.key",
5481 public_key: "testkey.avbpubkey",
5482 private_key: "testkey.pem",
5483 }
5484
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005485 bootclasspath_fragment {
5486 name: "my-bootclasspath-fragment",
5487 contents: ["libfoo", "libbar"],
5488 apex_available: ["myapex"],
5489 hidden_api: {
5490 split_packages: ["*"],
5491 },
5492 }
5493
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005494 prebuilt_apex {
5495 name: "myapex",
5496 arch: {
5497 arm64: {
5498 src: "myapex-arm64.apex",
5499 },
5500 arm: {
5501 src: "myapex-arm.apex",
5502 },
5503 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005504 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5505 }
5506
5507 prebuilt_bootclasspath_fragment {
5508 name: "my-bootclasspath-fragment",
5509 contents: ["libfoo", "libbar"],
5510 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005511 hidden_api: {
5512 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5513 metadata: "my-bootclasspath-fragment/metadata.csv",
5514 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005515 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5516 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5517 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005518 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005519 }
5520
5521 java_import {
5522 name: "libfoo",
5523 jars: ["libfoo.jar"],
5524 apex_available: ["myapex"],
5525 }
5526
5527 java_library {
5528 name: "libfoo",
5529 srcs: ["foo/bar/MyClass.java"],
5530 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005531 permitted_packages: ["foo"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005532 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005533 }
Paul Duffin37856732021-02-26 14:24:15 +00005534
5535 java_sdk_library_import {
5536 name: "libbar",
5537 public: {
5538 jars: ["libbar.jar"],
5539 },
5540 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005541 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005542 }
5543
5544 java_sdk_library {
5545 name: "libbar",
5546 srcs: ["foo/bar/MyClass.java"],
5547 unsafe_ignore_missing_latest_api: true,
5548 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005549 permitted_packages: ["bar"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005550 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005551 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005552 `
5553
Paul Duffin89f570a2021-06-16 01:42:33 +01005554 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Jiakai Zhangc6879f32023-11-06 16:31:19 +00005555 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/hiddenapi-modular/encoded/libfoo.jar")
5556 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/hiddenapi-modular/encoded/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005557
Paul Duffin537ea3d2021-05-14 10:38:00 +01005558 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005559 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005560 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
5561 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005562 out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/modular-hiddenapi/index.csv
5563 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 +01005564 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005565 })
5566
5567 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5568 bp := `
5569 apex {
5570 name: "myapex",
5571 enabled: false,
5572 key: "myapex.key",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005573 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005574 }
5575
5576 apex_key {
5577 name: "myapex.key",
5578 public_key: "testkey.avbpubkey",
5579 private_key: "testkey.pem",
5580 }
5581
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005582 bootclasspath_fragment {
5583 name: "my-bootclasspath-fragment",
5584 enabled: false,
5585 contents: ["libfoo", "libbar"],
5586 apex_available: ["myapex"],
5587 hidden_api: {
5588 split_packages: ["*"],
5589 },
5590 }
5591
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005592 prebuilt_apex {
5593 name: "myapex",
5594 arch: {
5595 arm64: {
5596 src: "myapex-arm64.apex",
5597 },
5598 arm: {
5599 src: "myapex-arm.apex",
5600 },
5601 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005602 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5603 }
5604
5605 prebuilt_bootclasspath_fragment {
5606 name: "my-bootclasspath-fragment",
5607 contents: ["libfoo", "libbar"],
5608 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005609 hidden_api: {
5610 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5611 metadata: "my-bootclasspath-fragment/metadata.csv",
5612 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005613 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5614 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5615 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005616 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005617 }
5618
5619 java_import {
5620 name: "libfoo",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005621 jars: ["libfoo.jar"],
5622 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005623 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005624 }
5625
5626 java_library {
5627 name: "libfoo",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005628 enabled: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005629 srcs: ["foo/bar/MyClass.java"],
5630 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005631 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005632 }
Paul Duffin37856732021-02-26 14:24:15 +00005633
5634 java_sdk_library_import {
5635 name: "libbar",
Paul Duffin37856732021-02-26 14:24:15 +00005636 public: {
5637 jars: ["libbar.jar"],
5638 },
5639 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005640 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005641 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005642 }
5643
5644 java_sdk_library {
5645 name: "libbar",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005646 enabled: false,
Paul Duffin37856732021-02-26 14:24:15 +00005647 srcs: ["foo/bar/MyClass.java"],
5648 unsafe_ignore_missing_latest_api: true,
5649 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005650 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005651 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005652 `
Cole Fausta963b942024-04-11 17:43:00 -07005653 // This test disables libbar, which causes the ComponentDepsMutator to add
5654 // deps on libbar.stubs and other sub-modules that don't exist. We can
5655 // enable AllowMissingDependencies to work around that, but enabling that
5656 // causes extra checks for missing source files to dex_bootjars, so add those
5657 // to the mock fs as well.
5658 preparer2 := android.GroupFixturePreparers(
5659 preparer,
5660 android.PrepareForTestWithAllowMissingDependencies,
5661 android.FixtureMergeMockFs(map[string][]byte{
5662 "build/soong/scripts/check_boot_jars/package_allowed_list.txt": nil,
5663 "frameworks/base/config/boot-profile.txt": nil,
5664 }),
5665 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005666
Cole Fausta963b942024-04-11 17:43:00 -07005667 ctx := testDexpreoptWithApexes(t, bp, "", preparer2, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005668 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5669 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005670
Paul Duffin537ea3d2021-05-14 10:38:00 +01005671 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005672 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005673 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005674 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005675 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005676 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 +01005677 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005678 })
Spandan Das3a392012024-01-17 18:26:27 +00005679
Spandan Dasf2c10572024-02-27 04:49:52 +00005680 t.Run("Co-existing unflagged apexes should create a duplicate module error", func(t *testing.T) {
Spandan Das3a392012024-01-17 18:26:27 +00005681 bp := `
5682 // Source
5683 apex {
5684 name: "myapex",
5685 enabled: false,
5686 key: "myapex.key",
5687 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5688 }
5689
5690 apex_key {
5691 name: "myapex.key",
5692 public_key: "testkey.avbpubkey",
5693 private_key: "testkey.pem",
5694 }
5695
5696 // Prebuilt
5697 prebuilt_apex {
5698 name: "myapex.v1",
5699 source_apex_name: "myapex",
5700 arch: {
5701 arm64: {
5702 src: "myapex-arm64.apex",
5703 },
5704 arm: {
5705 src: "myapex-arm.apex",
5706 },
5707 },
5708 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5709 prefer: true,
5710 }
5711 prebuilt_apex {
5712 name: "myapex.v2",
5713 source_apex_name: "myapex",
5714 arch: {
5715 arm64: {
5716 src: "myapex-arm64.apex",
5717 },
5718 arm: {
5719 src: "myapex-arm.apex",
5720 },
5721 },
5722 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5723 prefer: true,
5724 }
5725
5726 prebuilt_bootclasspath_fragment {
5727 name: "my-bootclasspath-fragment",
5728 contents: ["libfoo", "libbar"],
5729 apex_available: ["myapex"],
5730 hidden_api: {
5731 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5732 metadata: "my-bootclasspath-fragment/metadata.csv",
5733 index: "my-bootclasspath-fragment/index.csv",
5734 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5735 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5736 },
5737 prefer: true,
5738 }
5739
5740 java_import {
5741 name: "libfoo",
5742 jars: ["libfoo.jar"],
5743 apex_available: ["myapex"],
5744 prefer: true,
5745 }
5746 java_import {
5747 name: "libbar",
5748 jars: ["libbar.jar"],
5749 apex_available: ["myapex"],
5750 prefer: true,
5751 }
5752 `
5753
Spandan Dasf2c10572024-02-27 04:49:52 +00005754 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 +00005755 })
5756
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005757}
5758
Roland Levillain630846d2019-06-26 12:48:34 +01005759func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005760 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005761 apex_test {
5762 name: "myapex",
5763 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005764 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005765 tests: [
5766 "mytest",
5767 ],
5768 }
5769
5770 apex_key {
5771 name: "myapex.key",
5772 public_key: "testkey.avbpubkey",
5773 private_key: "testkey.pem",
5774 }
5775
Liz Kammer1c14a212020-05-12 15:26:55 -07005776 filegroup {
5777 name: "fg",
5778 srcs: [
5779 "baz",
5780 "bar/baz"
5781 ],
5782 }
5783
Roland Levillain630846d2019-06-26 12:48:34 +01005784 cc_test {
5785 name: "mytest",
5786 gtest: false,
5787 srcs: ["mytest.cpp"],
5788 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005789 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005790 system_shared_libs: [],
5791 static_executable: true,
5792 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005793 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005794 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005795
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005796 cc_library {
5797 name: "mylib",
5798 srcs: ["mylib.cpp"],
5799 system_shared_libs: [],
5800 stl: "none",
5801 }
5802
Liz Kammer5bd365f2020-05-27 15:15:11 -07005803 filegroup {
5804 name: "fg2",
5805 srcs: [
5806 "testdata/baz"
5807 ],
5808 }
Roland Levillain630846d2019-06-26 12:48:34 +01005809 `)
5810
Jooyung Hana0503a52023-08-23 13:12:50 +09005811 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005812 copyCmds := apexRule.Args["copy_commands"]
5813
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005814 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005815 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005816 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005817
Liz Kammer1c14a212020-05-12 15:26:55 -07005818 //Ensure that test data are copied into apex.
5819 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5820 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5821
Roland Levillainf89cd092019-07-29 16:22:59 +01005822 // Ensure the module is correctly translated.
Jooyung Hana0503a52023-08-23 13:12:50 +09005823 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005824 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005825 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005826 prefix := "TARGET_"
5827 var builder strings.Builder
5828 data.Custom(&builder, name, prefix, "", data)
5829 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005830 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005831 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005832}
5833
Jooyung Hand48f3c32019-08-23 11:18:57 +09005834func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5835 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5836 apex {
5837 name: "myapex",
5838 key: "myapex.key",
5839 native_shared_libs: ["libfoo"],
5840 }
5841
5842 apex_key {
5843 name: "myapex.key",
5844 public_key: "testkey.avbpubkey",
5845 private_key: "testkey.pem",
5846 }
5847
5848 cc_library {
5849 name: "libfoo",
5850 stl: "none",
5851 system_shared_libs: [],
5852 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005853 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005854 }
5855 `)
5856 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5857 apex {
5858 name: "myapex",
5859 key: "myapex.key",
5860 java_libs: ["myjar"],
5861 }
5862
5863 apex_key {
5864 name: "myapex.key",
5865 public_key: "testkey.avbpubkey",
5866 private_key: "testkey.pem",
5867 }
5868
5869 java_library {
5870 name: "myjar",
5871 srcs: ["foo/bar/MyClass.java"],
5872 sdk_version: "none",
5873 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005874 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005875 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005876 }
5877 `)
5878}
5879
Bill Peckhama41a6962021-01-11 10:58:54 -08005880func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005881 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005882 apex {
5883 name: "myapex",
5884 key: "myapex.key",
5885 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005886 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005887 }
5888
5889 apex_key {
5890 name: "myapex.key",
5891 public_key: "testkey.avbpubkey",
5892 private_key: "testkey.pem",
5893 }
5894
5895 java_import {
5896 name: "myjavaimport",
5897 apex_available: ["myapex"],
5898 jars: ["my.jar"],
5899 compile_dex: true,
5900 }
5901 `)
5902
Jooyung Hana0503a52023-08-23 13:12:50 +09005903 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Bill Peckhama41a6962021-01-11 10:58:54 -08005904 apexRule := module.Rule("apexRule")
5905 copyCmds := apexRule.Args["copy_commands"]
5906 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5907}
5908
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005909func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005910 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005911 apex {
5912 name: "myapex",
5913 key: "myapex.key",
5914 apps: [
5915 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005916 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005917 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005918 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005919 }
5920
5921 apex_key {
5922 name: "myapex.key",
5923 public_key: "testkey.avbpubkey",
5924 private_key: "testkey.pem",
5925 }
5926
5927 android_app {
5928 name: "AppFoo",
5929 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005930 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005931 system_modules: "none",
Jiyong Park970c5242024-05-17 22:58:54 +00005932 use_embedded_native_libs: true,
Jiyong Park8be103b2019-11-08 15:53:48 +09005933 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005934 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005935 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005936 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005937
5938 android_app {
5939 name: "AppFooPriv",
5940 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005941 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005942 system_modules: "none",
5943 privileged: true,
Sam Delmerico15809f82023-05-15 17:21:47 -04005944 privapp_allowlist: "privapp_allowlist_com.android.AppFooPriv.xml",
Colin Cross094cde42020-02-15 10:38:00 -08005945 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005946 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005947 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005948
5949 cc_library_shared {
5950 name: "libjni",
5951 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005952 shared_libs: ["libfoo"],
5953 stl: "none",
5954 system_shared_libs: [],
5955 apex_available: [ "myapex" ],
5956 sdk_version: "current",
5957 }
5958
5959 cc_library_shared {
5960 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005961 stl: "none",
5962 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005963 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005964 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005965 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005966 `)
5967
Jooyung Hana0503a52023-08-23 13:12:50 +09005968 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005969 apexRule := module.Rule("apexRule")
5970 copyCmds := apexRule.Args["copy_commands"]
5971
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005972 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5973 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Andrei Onea580636b2022-08-17 16:53:46 +00005974 ensureContains(t, copyCmds, "image.apex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005975
Colin Crossaede88c2020-08-11 12:17:01 -07005976 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005977 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005978 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005979 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005980 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005981 // JNI libraries including transitive deps are
5982 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005983 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005984 // ... embedded inside APK (jnilibs.zip)
5985 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5986 // ... and not directly inside the APEX
5987 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5988 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04005989
5990 apexBundle := module.Module().(*apexBundle)
5991 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
5992 var builder strings.Builder
5993 data.Custom(&builder, apexBundle.Name(), "TARGET_", "", data)
5994 androidMk := builder.String()
5995 ensureContains(t, androidMk, "LOCAL_MODULE := AppFooPriv.myapex")
5996 ensureContains(t, androidMk, "LOCAL_MODULE := AppFoo.myapex")
5997 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFooPriv.apk")
5998 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFoo.apk")
5999 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := \\S+AppFooPriv.apk")
6000 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 +01006001}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006002
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006003func TestApexWithAppImportBuildId(t *testing.T) {
6004 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
6005 for _, id := range invalidBuildIds {
6006 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
6007 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6008 variables.BuildId = proptools.StringPtr(id)
6009 })
6010 testApexError(t, message, `apex {
6011 name: "myapex",
6012 key: "myapex.key",
6013 apps: ["AppFooPrebuilt"],
6014 updatable: false,
6015 }
6016
6017 apex_key {
6018 name: "myapex.key",
6019 public_key: "testkey.avbpubkey",
6020 private_key: "testkey.pem",
6021 }
6022
6023 android_app_import {
6024 name: "AppFooPrebuilt",
6025 apk: "PrebuiltAppFoo.apk",
6026 presigned: true,
6027 apex_available: ["myapex"],
6028 }
6029 `, fixture)
6030 }
6031}
6032
Dario Frenicde2a032019-10-27 00:29:22 +01006033func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006034 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01006035 apex {
6036 name: "myapex",
6037 key: "myapex.key",
6038 apps: [
6039 "AppFooPrebuilt",
6040 "AppFooPrivPrebuilt",
6041 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006042 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01006043 }
6044
6045 apex_key {
6046 name: "myapex.key",
6047 public_key: "testkey.avbpubkey",
6048 private_key: "testkey.pem",
6049 }
6050
6051 android_app_import {
6052 name: "AppFooPrebuilt",
6053 apk: "PrebuiltAppFoo.apk",
6054 presigned: true,
6055 dex_preopt: {
6056 enabled: false,
6057 },
Jiyong Park592a6a42020-04-21 22:34:28 +09006058 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006059 }
6060
6061 android_app_import {
6062 name: "AppFooPrivPrebuilt",
6063 apk: "PrebuiltAppFooPriv.apk",
6064 privileged: true,
6065 presigned: true,
6066 dex_preopt: {
6067 enabled: false,
6068 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006069 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006070 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006071 }
6072 `)
6073
Jooyung Hana0503a52023-08-23 13:12:50 +09006074 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Frenicde2a032019-10-27 00:29:22 +01006075 apexRule := module.Rule("apexRule")
6076 copyCmds := apexRule.Args["copy_commands"]
6077
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006078 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6079 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006080}
6081
6082func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006083 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006084 apex {
6085 name: "myapex",
6086 key: "myapex.key",
6087 apps: [
6088 "AppFoo",
6089 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006090 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006091 }
6092
6093 apex_key {
6094 name: "myapex.key",
6095 public_key: "testkey.avbpubkey",
6096 private_key: "testkey.pem",
6097 }
6098
6099 android_app {
6100 name: "AppFoo",
6101 srcs: ["foo/bar/MyClass.java"],
6102 sdk_version: "none",
6103 system_modules: "none",
6104 apex_available: [ "myapex" ],
6105 }
6106
6107 android_app_import {
6108 name: "AppFoo",
6109 apk: "AppFooPrebuilt.apk",
6110 filename: "AppFooPrebuilt.apk",
6111 presigned: true,
6112 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006113 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006114 }
6115 `, withFiles(map[string][]byte{
6116 "AppFooPrebuilt.apk": nil,
6117 }))
6118
Jooyung Hana0503a52023-08-23 13:12:50 +09006119 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006120 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006121 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006122}
6123
Dario Freni6f3937c2019-12-20 22:58:03 +00006124func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006125 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006126 apex {
6127 name: "myapex",
6128 key: "myapex.key",
6129 apps: [
6130 "TesterHelpAppFoo",
6131 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006132 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006133 }
6134
6135 apex_key {
6136 name: "myapex.key",
6137 public_key: "testkey.avbpubkey",
6138 private_key: "testkey.pem",
6139 }
6140
6141 android_test_helper_app {
6142 name: "TesterHelpAppFoo",
6143 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006144 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006145 }
6146
6147 `)
6148
Jooyung Hana0503a52023-08-23 13:12:50 +09006149 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Freni6f3937c2019-12-20 22:58:03 +00006150 apexRule := module.Rule("apexRule")
6151 copyCmds := apexRule.Args["copy_commands"]
6152
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006153 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006154}
6155
Jooyung Han18020ea2019-11-13 10:50:48 +09006156func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6157 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006158 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006159 apex {
6160 name: "myapex",
6161 key: "myapex.key",
6162 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006163 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006164 }
6165
6166 apex_key {
6167 name: "myapex.key",
6168 public_key: "testkey.avbpubkey",
6169 private_key: "testkey.pem",
6170 }
6171
6172 apex {
6173 name: "otherapex",
6174 key: "myapex.key",
6175 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006176 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006177 }
6178
6179 cc_defaults {
6180 name: "libfoo-defaults",
6181 apex_available: ["otherapex"],
6182 }
6183
6184 cc_library {
6185 name: "libfoo",
6186 defaults: ["libfoo-defaults"],
6187 stl: "none",
6188 system_shared_libs: [],
6189 }`)
6190}
6191
Paul Duffine52e66f2020-03-30 17:54:29 +01006192func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006193 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006194 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006195 apex {
6196 name: "myapex",
6197 key: "myapex.key",
6198 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006199 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006200 }
6201
6202 apex_key {
6203 name: "myapex.key",
6204 public_key: "testkey.avbpubkey",
6205 private_key: "testkey.pem",
6206 }
6207
6208 apex {
6209 name: "otherapex",
6210 key: "otherapex.key",
6211 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006212 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006213 }
6214
6215 apex_key {
6216 name: "otherapex.key",
6217 public_key: "testkey.avbpubkey",
6218 private_key: "testkey.pem",
6219 }
6220
6221 cc_library {
6222 name: "libfoo",
6223 stl: "none",
6224 system_shared_libs: [],
6225 apex_available: ["otherapex"],
6226 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006227}
Jiyong Park127b40b2019-09-30 16:04:35 +09006228
Paul Duffine52e66f2020-03-30 17:54:29 +01006229func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006230 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006231 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006232.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006233.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006234.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006235.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006236.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006237.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006238 apex {
6239 name: "myapex",
6240 key: "myapex.key",
6241 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006242 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006243 }
6244
6245 apex_key {
6246 name: "myapex.key",
6247 public_key: "testkey.avbpubkey",
6248 private_key: "testkey.pem",
6249 }
6250
Jiyong Park127b40b2019-09-30 16:04:35 +09006251 cc_library {
6252 name: "libfoo",
6253 stl: "none",
6254 shared_libs: ["libbar"],
6255 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006256 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006257 }
6258
6259 cc_library {
6260 name: "libbar",
6261 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006262 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006263 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006264 apex_available: ["myapex"],
6265 }
6266
6267 cc_library {
6268 name: "libbaz",
6269 stl: "none",
6270 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006271 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006272}
Jiyong Park127b40b2019-09-30 16:04:35 +09006273
Liz Kammer5f108fa2023-05-11 14:33:17 -04006274func TestApexAvailable_IndirectStaticDep(t *testing.T) {
6275 testApex(t, `
6276 apex {
6277 name: "myapex",
6278 key: "myapex.key",
6279 native_shared_libs: ["libfoo"],
6280 updatable: false,
6281 }
6282
6283 apex_key {
6284 name: "myapex.key",
6285 public_key: "testkey.avbpubkey",
6286 private_key: "testkey.pem",
6287 }
6288
6289 cc_library {
6290 name: "libfoo",
6291 stl: "none",
6292 static_libs: ["libbar"],
6293 system_shared_libs: [],
6294 apex_available: ["myapex"],
6295 }
6296
6297 cc_library {
6298 name: "libbar",
6299 stl: "none",
6300 shared_libs: ["libbaz"],
6301 system_shared_libs: [],
6302 apex_available: ["myapex"],
6303 }
6304
6305 cc_library {
6306 name: "libbaz",
6307 stl: "none",
6308 system_shared_libs: [],
6309 }`)
6310
6311 testApexError(t, `requires "libbar" that doesn't list the APEX under 'apex_available'.`, `
6312 apex {
6313 name: "myapex",
6314 key: "myapex.key",
6315 native_shared_libs: ["libfoo"],
6316 updatable: false,
6317 }
6318
6319 apex_key {
6320 name: "myapex.key",
6321 public_key: "testkey.avbpubkey",
6322 private_key: "testkey.pem",
6323 }
6324
6325 cc_library {
6326 name: "libfoo",
6327 stl: "none",
6328 static_libs: ["libbar"],
6329 system_shared_libs: [],
6330 apex_available: ["myapex"],
6331 }
6332
6333 cc_library {
6334 name: "libbar",
6335 stl: "none",
6336 system_shared_libs: [],
6337 }`)
6338}
6339
Paul Duffine52e66f2020-03-30 17:54:29 +01006340func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006341 testApexError(t, "\"otherapex\" is not a valid module name", `
6342 apex {
6343 name: "myapex",
6344 key: "myapex.key",
6345 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006346 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006347 }
6348
6349 apex_key {
6350 name: "myapex.key",
6351 public_key: "testkey.avbpubkey",
6352 private_key: "testkey.pem",
6353 }
6354
6355 cc_library {
6356 name: "libfoo",
6357 stl: "none",
6358 system_shared_libs: [],
6359 apex_available: ["otherapex"],
6360 }`)
6361
Paul Duffine52e66f2020-03-30 17:54:29 +01006362 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006363 apex {
6364 name: "myapex",
6365 key: "myapex.key",
6366 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006367 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006368 }
6369
6370 apex_key {
6371 name: "myapex.key",
6372 public_key: "testkey.avbpubkey",
6373 private_key: "testkey.pem",
6374 }
6375
6376 cc_library {
6377 name: "libfoo",
6378 stl: "none",
6379 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006380 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006381 apex_available: ["myapex"],
6382 }
6383
6384 cc_library {
6385 name: "libbar",
6386 stl: "none",
6387 system_shared_libs: [],
6388 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006389 }
6390
6391 cc_library {
6392 name: "libbaz",
6393 stl: "none",
6394 system_shared_libs: [],
6395 stubs: {
6396 versions: ["10", "20", "30"],
6397 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006398 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006399}
Jiyong Park127b40b2019-09-30 16:04:35 +09006400
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006401func TestApexAvailable_ApexAvailableNameWithVersionCodeError(t *testing.T) {
6402 t.Run("negative variant_version produces error", func(t *testing.T) {
6403 testApexError(t, "expected an integer between 0-9; got -1", `
6404 apex {
6405 name: "myapex",
6406 key: "myapex.key",
6407 apex_available_name: "com.android.foo",
6408 variant_version: "-1",
6409 updatable: false,
6410 }
6411 apex_key {
6412 name: "myapex.key",
6413 public_key: "testkey.avbpubkey",
6414 private_key: "testkey.pem",
6415 }
6416 `)
6417 })
6418
6419 t.Run("variant_version greater than 9 produces error", func(t *testing.T) {
6420 testApexError(t, "expected an integer between 0-9; got 10", `
6421 apex {
6422 name: "myapex",
6423 key: "myapex.key",
6424 apex_available_name: "com.android.foo",
6425 variant_version: "10",
6426 updatable: false,
6427 }
6428 apex_key {
6429 name: "myapex.key",
6430 public_key: "testkey.avbpubkey",
6431 private_key: "testkey.pem",
6432 }
6433 `)
6434 })
6435}
6436
6437func TestApexAvailable_ApexAvailableNameWithVersionCode(t *testing.T) {
6438 context := android.GroupFixturePreparers(
6439 android.PrepareForIntegrationTestWithAndroid,
6440 PrepareForTestWithApexBuildComponents,
6441 android.FixtureMergeMockFs(android.MockFS{
6442 "system/sepolicy/apex/foo-file_contexts": nil,
6443 "system/sepolicy/apex/bar-file_contexts": nil,
6444 }),
6445 )
6446 result := context.RunTestWithBp(t, `
6447 apex {
6448 name: "foo",
6449 key: "myapex.key",
6450 apex_available_name: "com.android.foo",
6451 variant_version: "0",
6452 updatable: false,
6453 }
6454 apex {
6455 name: "bar",
6456 key: "myapex.key",
6457 apex_available_name: "com.android.foo",
6458 variant_version: "3",
6459 updatable: false,
6460 }
6461 apex_key {
6462 name: "myapex.key",
6463 public_key: "testkey.avbpubkey",
6464 private_key: "testkey.pem",
6465 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006466 override_apex {
6467 name: "myoverrideapex",
6468 base: "bar",
6469 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006470 `)
6471
Jooyung Hana0503a52023-08-23 13:12:50 +09006472 fooManifestRule := result.ModuleForTests("foo", "android_common_foo").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006473 fooExpectedDefaultVersion := android.DefaultUpdatableModuleVersion
6474 fooActualDefaultVersion := fooManifestRule.Args["default_version"]
6475 if fooActualDefaultVersion != fooExpectedDefaultVersion {
6476 t.Errorf("expected to find defaultVersion %q; got %q", fooExpectedDefaultVersion, fooActualDefaultVersion)
6477 }
6478
Jooyung Hana0503a52023-08-23 13:12:50 +09006479 barManifestRule := result.ModuleForTests("bar", "android_common_bar").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006480 defaultVersionInt, _ := strconv.Atoi(android.DefaultUpdatableModuleVersion)
6481 barExpectedDefaultVersion := fmt.Sprint(defaultVersionInt + 3)
6482 barActualDefaultVersion := barManifestRule.Args["default_version"]
6483 if barActualDefaultVersion != barExpectedDefaultVersion {
6484 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6485 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006486
Spandan Das50801e22024-05-13 18:29:45 +00006487 overrideBarManifestRule := result.ModuleForTests("bar", "android_common_myoverrideapex_myoverrideapex").Rule("apexManifestRule")
Sam Delmerico419f9a32023-07-21 12:00:13 -04006488 overrideBarActualDefaultVersion := overrideBarManifestRule.Args["default_version"]
6489 if overrideBarActualDefaultVersion != barExpectedDefaultVersion {
6490 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6491 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006492}
6493
Sam Delmericoca816532023-06-02 14:09:50 -04006494func TestApexAvailable_ApexAvailableName(t *testing.T) {
6495 t.Run("using name of apex that sets apex_available_name is not allowed", func(t *testing.T) {
6496 testApexError(t, "Consider adding \"myapex\" to 'apex_available' property of \"AppFoo\"", `
6497 apex {
6498 name: "myapex_sminus",
6499 key: "myapex.key",
6500 apps: ["AppFoo"],
6501 apex_available_name: "myapex",
6502 updatable: false,
6503 }
6504 apex {
6505 name: "myapex",
6506 key: "myapex.key",
6507 apps: ["AppFoo"],
6508 updatable: false,
6509 }
6510 apex_key {
6511 name: "myapex.key",
6512 public_key: "testkey.avbpubkey",
6513 private_key: "testkey.pem",
6514 }
6515 android_app {
6516 name: "AppFoo",
6517 srcs: ["foo/bar/MyClass.java"],
6518 sdk_version: "none",
6519 system_modules: "none",
6520 apex_available: [ "myapex_sminus" ],
6521 }`,
6522 android.FixtureMergeMockFs(android.MockFS{
6523 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6524 }),
6525 )
6526 })
6527
6528 t.Run("apex_available_name allows module to be used in two different apexes", func(t *testing.T) {
6529 testApex(t, `
6530 apex {
6531 name: "myapex_sminus",
6532 key: "myapex.key",
6533 apps: ["AppFoo"],
6534 apex_available_name: "myapex",
6535 updatable: false,
6536 }
6537 apex {
6538 name: "myapex",
6539 key: "myapex.key",
6540 apps: ["AppFoo"],
6541 updatable: false,
6542 }
6543 apex_key {
6544 name: "myapex.key",
6545 public_key: "testkey.avbpubkey",
6546 private_key: "testkey.pem",
6547 }
6548 android_app {
6549 name: "AppFoo",
6550 srcs: ["foo/bar/MyClass.java"],
6551 sdk_version: "none",
6552 system_modules: "none",
6553 apex_available: [ "myapex" ],
6554 }`,
6555 android.FixtureMergeMockFs(android.MockFS{
6556 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6557 }),
6558 )
6559 })
6560
6561 t.Run("override_apexes work with apex_available_name", func(t *testing.T) {
6562 testApex(t, `
6563 override_apex {
6564 name: "myoverrideapex_sminus",
6565 base: "myapex_sminus",
6566 key: "myapex.key",
6567 apps: ["AppFooOverride"],
6568 }
6569 override_apex {
6570 name: "myoverrideapex",
6571 base: "myapex",
6572 key: "myapex.key",
6573 apps: ["AppFooOverride"],
6574 }
6575 apex {
6576 name: "myapex_sminus",
6577 key: "myapex.key",
6578 apps: ["AppFoo"],
6579 apex_available_name: "myapex",
6580 updatable: false,
6581 }
6582 apex {
6583 name: "myapex",
6584 key: "myapex.key",
6585 apps: ["AppFoo"],
6586 updatable: false,
6587 }
6588 apex_key {
6589 name: "myapex.key",
6590 public_key: "testkey.avbpubkey",
6591 private_key: "testkey.pem",
6592 }
6593 android_app {
6594 name: "AppFooOverride",
6595 srcs: ["foo/bar/MyClass.java"],
6596 sdk_version: "none",
6597 system_modules: "none",
6598 apex_available: [ "myapex" ],
6599 }
6600 android_app {
6601 name: "AppFoo",
6602 srcs: ["foo/bar/MyClass.java"],
6603 sdk_version: "none",
6604 system_modules: "none",
6605 apex_available: [ "myapex" ],
6606 }`,
6607 android.FixtureMergeMockFs(android.MockFS{
6608 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6609 }),
6610 )
6611 })
6612}
6613
6614func TestApexAvailable_ApexAvailableNameWithOverrides(t *testing.T) {
6615 context := android.GroupFixturePreparers(
6616 android.PrepareForIntegrationTestWithAndroid,
6617 PrepareForTestWithApexBuildComponents,
6618 java.PrepareForTestWithDexpreopt,
6619 android.FixtureMergeMockFs(android.MockFS{
6620 "system/sepolicy/apex/myapex-file_contexts": nil,
6621 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6622 }),
6623 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6624 variables.BuildId = proptools.StringPtr("buildid")
6625 }),
6626 )
6627 context.RunTestWithBp(t, `
6628 override_apex {
6629 name: "myoverrideapex_sminus",
6630 base: "myapex_sminus",
6631 }
6632 override_apex {
6633 name: "myoverrideapex",
6634 base: "myapex",
6635 }
6636 apex {
6637 name: "myapex",
6638 key: "myapex.key",
6639 apps: ["AppFoo"],
6640 updatable: false,
6641 }
6642 apex {
6643 name: "myapex_sminus",
6644 apex_available_name: "myapex",
6645 key: "myapex.key",
6646 apps: ["AppFoo_sminus"],
6647 updatable: false,
6648 }
6649 apex_key {
6650 name: "myapex.key",
6651 public_key: "testkey.avbpubkey",
6652 private_key: "testkey.pem",
6653 }
6654 android_app {
6655 name: "AppFoo",
6656 srcs: ["foo/bar/MyClass.java"],
6657 sdk_version: "none",
6658 system_modules: "none",
6659 apex_available: [ "myapex" ],
6660 }
6661 android_app {
6662 name: "AppFoo_sminus",
6663 srcs: ["foo/bar/MyClass.java"],
6664 sdk_version: "none",
6665 min_sdk_version: "29",
6666 system_modules: "none",
6667 apex_available: [ "myapex" ],
6668 }`)
6669}
6670
Jiyong Park89e850a2020-04-07 16:37:39 +09006671func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006672 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006673 apex {
6674 name: "myapex",
6675 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006676 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006677 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006678 }
6679
6680 apex_key {
6681 name: "myapex.key",
6682 public_key: "testkey.avbpubkey",
6683 private_key: "testkey.pem",
6684 }
6685
6686 cc_library {
6687 name: "libfoo",
6688 stl: "none",
6689 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006690 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006691 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006692 }
6693
6694 cc_library {
6695 name: "libfoo2",
6696 stl: "none",
6697 system_shared_libs: [],
6698 shared_libs: ["libbaz"],
6699 apex_available: ["//apex_available:platform"],
6700 }
6701
6702 cc_library {
6703 name: "libbar",
6704 stl: "none",
6705 system_shared_libs: [],
6706 apex_available: ["myapex"],
6707 }
6708
6709 cc_library {
6710 name: "libbaz",
6711 stl: "none",
6712 system_shared_libs: [],
6713 apex_available: ["myapex"],
6714 stubs: {
6715 versions: ["1"],
6716 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006717 }`)
6718
Jiyong Park89e850a2020-04-07 16:37:39 +09006719 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6720 // because it depends on libbar which isn't available to platform
6721 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6722 if libfoo.NotAvailableForPlatform() != true {
6723 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6724 }
6725
6726 // libfoo2 however can be available to platform because it depends on libbaz which provides
6727 // stubs
6728 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6729 if libfoo2.NotAvailableForPlatform() == true {
6730 t.Errorf("%q should be available to platform", libfoo2.String())
6731 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006732}
Jiyong Parka90ca002019-10-07 15:47:24 +09006733
Paul Duffine52e66f2020-03-30 17:54:29 +01006734func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006735 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006736 apex {
6737 name: "myapex",
6738 key: "myapex.key",
6739 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006740 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006741 }
6742
6743 apex_key {
6744 name: "myapex.key",
6745 public_key: "testkey.avbpubkey",
6746 private_key: "testkey.pem",
6747 }
6748
6749 cc_library {
6750 name: "libfoo",
6751 stl: "none",
6752 system_shared_libs: [],
6753 apex_available: ["myapex"],
6754 static: {
6755 apex_available: ["//apex_available:platform"],
6756 },
6757 }`)
6758
Jiyong Park89e850a2020-04-07 16:37:39 +09006759 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6760 if libfooShared.NotAvailableForPlatform() != true {
6761 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6762 }
6763 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6764 if libfooStatic.NotAvailableForPlatform() != false {
6765 t.Errorf("%q should be available to platform", libfooStatic.String())
6766 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006767}
6768
Jiyong Park5d790c32019-11-15 18:40:32 +09006769func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006770 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006771 apex {
6772 name: "myapex",
6773 key: "myapex.key",
6774 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006775 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006776 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006777 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006778 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006779 }
6780
6781 override_apex {
6782 name: "override_myapex",
6783 base: "myapex",
6784 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006785 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006786 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006787 overrides: ["unknownapex"],
Jesse Melhuishec60e252024-03-29 19:08:20 +00006788 compile_multilib: "first",
6789 multilib: {
6790 lib32: {
6791 native_shared_libs: ["mylib32"],
6792 },
6793 lib64: {
6794 native_shared_libs: ["mylib64"],
6795 },
6796 },
Baligh Uddin004d7172020-02-19 21:29:28 -08006797 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006798 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006799 key: "mynewapex.key",
6800 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006801 }
6802
6803 apex_key {
6804 name: "myapex.key",
6805 public_key: "testkey.avbpubkey",
6806 private_key: "testkey.pem",
6807 }
6808
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006809 apex_key {
6810 name: "mynewapex.key",
6811 public_key: "testkey2.avbpubkey",
6812 private_key: "testkey2.pem",
6813 }
6814
6815 android_app_certificate {
6816 name: "myapex.certificate",
6817 certificate: "testkey",
6818 }
6819
Jiyong Park5d790c32019-11-15 18:40:32 +09006820 android_app {
6821 name: "app",
6822 srcs: ["foo/bar/MyClass.java"],
6823 package_name: "foo",
6824 sdk_version: "none",
6825 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006826 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006827 }
6828
6829 override_android_app {
6830 name: "override_app",
6831 base: "app",
6832 package_name: "bar",
6833 }
markchien7c803b82021-08-26 22:10:06 +08006834
6835 bpf {
6836 name: "bpf",
6837 srcs: ["bpf.c"],
6838 }
6839
6840 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006841 name: "overrideBpf",
6842 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006843 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006844
6845 prebuilt_etc {
6846 name: "myetc",
6847 src: "myprebuilt",
6848 }
6849
6850 prebuilt_etc {
6851 name: "override_myetc",
6852 src: "override_myprebuilt",
6853 }
Jesse Melhuishec60e252024-03-29 19:08:20 +00006854
6855 cc_library {
6856 name: "mylib32",
6857 apex_available: [ "myapex" ],
6858 }
6859
6860 cc_library {
6861 name: "mylib64",
6862 apex_available: [ "myapex" ],
6863 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006864 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006865
Jooyung Hana0503a52023-08-23 13:12:50 +09006866 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(android.OverridableModule)
Spandan Das50801e22024-05-13 18:29:45 +00006867 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex").Module().(android.OverridableModule)
Jiyong Park317645e2019-12-05 13:20:58 +09006868 if originalVariant.GetOverriddenBy() != "" {
6869 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6870 }
6871 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6872 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6873 }
6874
Spandan Das50801e22024-05-13 18:29:45 +00006875 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006876 apexRule := module.Rule("apexRule")
6877 copyCmds := apexRule.Args["copy_commands"]
6878
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006879 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6880 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006881
markchien7c803b82021-08-26 22:10:06 +08006882 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006883 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006884
Daniel Norman5a3ce132021-08-26 15:44:43 -07006885 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6886 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6887
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006888 apexBundle := module.Module().(*apexBundle)
6889 name := apexBundle.Name()
6890 if name != "override_myapex" {
6891 t.Errorf("name should be \"override_myapex\", but was %q", name)
6892 }
6893
Baligh Uddin004d7172020-02-19 21:29:28 -08006894 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6895 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6896 }
6897
Jiyong Park20bacab2020-03-03 11:45:41 +09006898 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006899 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006900 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6901
6902 signApkRule := module.Rule("signapk")
6903 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006904
Colin Crossaa255532020-07-03 13:18:24 -07006905 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006906 var builder strings.Builder
6907 data.Custom(&builder, name, "TARGET_", "", data)
6908 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006909 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6910 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006911 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006912 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006913 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006914 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006915 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006916 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006917}
6918
Albert Martineefabcf2022-03-21 20:11:16 +00006919func TestMinSdkVersionOverride(t *testing.T) {
6920 // Override from 29 to 31
6921 minSdkOverride31 := "31"
6922 ctx := testApex(t, `
6923 apex {
6924 name: "myapex",
6925 key: "myapex.key",
6926 native_shared_libs: ["mylib"],
6927 updatable: true,
6928 min_sdk_version: "29"
6929 }
6930
6931 override_apex {
6932 name: "override_myapex",
6933 base: "myapex",
6934 logging_parent: "com.foo.bar",
6935 package_name: "test.overridden.package"
6936 }
6937
6938 apex_key {
6939 name: "myapex.key",
6940 public_key: "testkey.avbpubkey",
6941 private_key: "testkey.pem",
6942 }
6943
6944 cc_library {
6945 name: "mylib",
6946 srcs: ["mylib.cpp"],
6947 runtime_libs: ["libbar"],
6948 system_shared_libs: [],
6949 stl: "none",
6950 apex_available: [ "myapex" ],
6951 min_sdk_version: "apex_inherit"
6952 }
6953
6954 cc_library {
6955 name: "libbar",
6956 srcs: ["mylib.cpp"],
6957 system_shared_libs: [],
6958 stl: "none",
6959 apex_available: [ "myapex" ],
6960 min_sdk_version: "apex_inherit"
6961 }
6962
6963 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6964
Jooyung Hana0503a52023-08-23 13:12:50 +09006965 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00006966 copyCmds := apexRule.Args["copy_commands"]
6967
6968 // Ensure that direct non-stubs dep is always included
6969 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6970
6971 // Ensure that runtime_libs dep in included
6972 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6973
6974 // Ensure libraries target overridden min_sdk_version value
6975 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6976}
6977
6978func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6979 // Attempt to override from 31 to 29, should be a NOOP
6980 minSdkOverride29 := "29"
6981 ctx := testApex(t, `
6982 apex {
6983 name: "myapex",
6984 key: "myapex.key",
6985 native_shared_libs: ["mylib"],
6986 updatable: true,
6987 min_sdk_version: "31"
6988 }
6989
6990 override_apex {
6991 name: "override_myapex",
6992 base: "myapex",
6993 logging_parent: "com.foo.bar",
6994 package_name: "test.overridden.package"
6995 }
6996
6997 apex_key {
6998 name: "myapex.key",
6999 public_key: "testkey.avbpubkey",
7000 private_key: "testkey.pem",
7001 }
7002
7003 cc_library {
7004 name: "mylib",
7005 srcs: ["mylib.cpp"],
7006 runtime_libs: ["libbar"],
7007 system_shared_libs: [],
7008 stl: "none",
7009 apex_available: [ "myapex" ],
7010 min_sdk_version: "apex_inherit"
7011 }
7012
7013 cc_library {
7014 name: "libbar",
7015 srcs: ["mylib.cpp"],
7016 system_shared_libs: [],
7017 stl: "none",
7018 apex_available: [ "myapex" ],
7019 min_sdk_version: "apex_inherit"
7020 }
7021
7022 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
7023
Jooyung Hana0503a52023-08-23 13:12:50 +09007024 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007025 copyCmds := apexRule.Args["copy_commands"]
7026
7027 // Ensure that direct non-stubs dep is always included
7028 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7029
7030 // Ensure that runtime_libs dep in included
7031 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7032
7033 // Ensure libraries target the original min_sdk_version value rather than the overridden
7034 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7035}
7036
Jooyung Han214bf372019-11-12 13:03:50 +09007037func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007038 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09007039 apex {
7040 name: "myapex",
7041 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007042 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09007043 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09007044 }
7045
7046 apex_key {
7047 name: "myapex.key",
7048 public_key: "testkey.avbpubkey",
7049 private_key: "testkey.pem",
7050 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007051
7052 cc_library {
7053 name: "mylib",
7054 srcs: ["mylib.cpp"],
7055 stl: "libc++",
7056 system_shared_libs: [],
7057 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09007058 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007059 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007060 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09007061
Jooyung Hana0503a52023-08-23 13:12:50 +09007062 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +09007063 args := module.Rule("apexRule").Args
7064 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00007065 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007066
7067 // The copies of the libraries in the apex should have one more dependency than
7068 // the ones outside the apex, namely the unwinder. Ideally we should check
7069 // the dependency names directly here but for some reason the names are blank in
7070 // this test.
7071 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07007072 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007073 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
7074 if len(apexImplicits) != len(nonApexImplicits)+1 {
7075 t.Errorf("%q missing unwinder dep", lib)
7076 }
7077 }
Jooyung Han214bf372019-11-12 13:03:50 +09007078}
7079
Paul Duffine05480a2021-03-08 15:07:14 +00007080var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01007081 "api/current.txt": nil,
7082 "api/removed.txt": nil,
7083 "api/system-current.txt": nil,
7084 "api/system-removed.txt": nil,
7085 "api/test-current.txt": nil,
7086 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01007087
Anton Hanssondff2c782020-12-21 17:10:01 +00007088 "100/public/api/foo.txt": nil,
7089 "100/public/api/foo-removed.txt": nil,
7090 "100/system/api/foo.txt": nil,
7091 "100/system/api/foo-removed.txt": nil,
7092
Paul Duffineedc5d52020-06-12 17:46:39 +01007093 // For java_sdk_library_import
7094 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01007095}
7096
Jooyung Han58f26ab2019-12-18 15:34:32 +09007097func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007098 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09007099 apex {
7100 name: "myapex",
7101 key: "myapex.key",
7102 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007103 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09007104 }
7105
7106 apex_key {
7107 name: "myapex.key",
7108 public_key: "testkey.avbpubkey",
7109 private_key: "testkey.pem",
7110 }
7111
7112 java_sdk_library {
7113 name: "foo",
7114 srcs: ["a.java"],
7115 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007116 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09007117 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007118
7119 prebuilt_apis {
7120 name: "sdk",
7121 api_dirs: ["100"],
7122 }
Paul Duffin9b879592020-05-26 13:21:35 +01007123 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09007124
7125 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007126 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09007127 "javalib/foo.jar",
7128 "etc/permissions/foo.xml",
7129 })
7130 // Permission XML should point to the activated path of impl jar of java_sdk_library
Paul Duffin1816cde2024-04-10 10:58:21 +01007131 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Output("foo.xml")
7132 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7133 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
Jooyung Han58f26ab2019-12-18 15:34:32 +09007134}
7135
Spandan Das3ee19692024-06-19 04:47:40 +00007136func TestJavaSDKLibraryOverrideApexes(t *testing.T) {
7137 ctx := testApex(t, `
7138 override_apex {
7139 name: "mycompanyapex",
7140 base: "myapex",
7141 }
7142 apex {
7143 name: "myapex",
7144 key: "myapex.key",
7145 java_libs: ["foo"],
7146 updatable: false,
7147 }
7148
7149 apex_key {
7150 name: "myapex.key",
7151 public_key: "testkey.avbpubkey",
7152 private_key: "testkey.pem",
7153 }
7154
7155 java_sdk_library {
7156 name: "foo",
7157 srcs: ["a.java"],
7158 api_packages: ["foo"],
7159 apex_available: [ "myapex" ],
7160 }
7161
7162 prebuilt_apis {
7163 name: "sdk",
7164 api_dirs: ["100"],
7165 }
7166 `, withFiles(filesForSdkLibrary))
7167
7168 // Permission XML should point to the activated path of impl jar of java_sdk_library.
7169 // Since override variants (com.mycompany.android.foo) are installed in the same package as the overridden variant
7170 // (com.android.foo), the filepath should not contain override apex name.
7171 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_mycompanyapex").Output("foo.xml")
7172 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7173 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
7174}
7175
Paul Duffin9b879592020-05-26 13:21:35 +01007176func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007177 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007178 apex {
7179 name: "myapex",
7180 key: "myapex.key",
7181 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007182 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007183 }
7184
7185 apex_key {
7186 name: "myapex.key",
7187 public_key: "testkey.avbpubkey",
7188 private_key: "testkey.pem",
7189 }
7190
7191 java_sdk_library {
7192 name: "foo",
7193 srcs: ["a.java"],
7194 api_packages: ["foo"],
7195 apex_available: ["myapex"],
7196 sdk_version: "none",
7197 system_modules: "none",
7198 }
7199
7200 java_library {
7201 name: "bar",
7202 srcs: ["a.java"],
7203 libs: ["foo"],
7204 apex_available: ["myapex"],
7205 sdk_version: "none",
7206 system_modules: "none",
7207 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007208
7209 prebuilt_apis {
7210 name: "sdk",
7211 api_dirs: ["100"],
7212 }
Paul Duffin9b879592020-05-26 13:21:35 +01007213 `, withFiles(filesForSdkLibrary))
7214
7215 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007216 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007217 "javalib/bar.jar",
7218 "javalib/foo.jar",
7219 "etc/permissions/foo.xml",
7220 })
7221
7222 // The bar library should depend on the implementation jar.
7223 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kanga3a05462024-04-05 00:36:44 +00007224 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007225 t.Errorf("expected %q, found %#q", expected, actual)
7226 }
7227}
7228
7229func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007230 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007231 apex {
7232 name: "myapex",
7233 key: "myapex.key",
7234 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007235 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007236 }
7237
7238 apex_key {
7239 name: "myapex.key",
7240 public_key: "testkey.avbpubkey",
7241 private_key: "testkey.pem",
7242 }
7243
7244 java_sdk_library {
7245 name: "foo",
7246 srcs: ["a.java"],
7247 api_packages: ["foo"],
7248 apex_available: ["myapex"],
7249 sdk_version: "none",
7250 system_modules: "none",
7251 }
7252
7253 java_library {
7254 name: "bar",
7255 srcs: ["a.java"],
7256 libs: ["foo"],
7257 sdk_version: "none",
7258 system_modules: "none",
7259 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007260
7261 prebuilt_apis {
7262 name: "sdk",
7263 api_dirs: ["100"],
7264 }
Paul Duffin9b879592020-05-26 13:21:35 +01007265 `, withFiles(filesForSdkLibrary))
7266
7267 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007268 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007269 "javalib/foo.jar",
7270 "etc/permissions/foo.xml",
7271 })
7272
7273 // The bar library should depend on the stubs jar.
7274 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007275 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007276 t.Errorf("expected %q, found %#q", expected, actual)
7277 }
7278}
7279
Paul Duffineedc5d52020-06-12 17:46:39 +01007280func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007281 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00007282 prebuilt_apis {
7283 name: "sdk",
7284 api_dirs: ["100"],
7285 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01007286 withFiles(map[string][]byte{
7287 "apex/a.java": nil,
7288 "apex/apex_manifest.json": nil,
7289 "apex/Android.bp": []byte(`
7290 package {
7291 default_visibility: ["//visibility:private"],
7292 }
7293
7294 apex {
7295 name: "myapex",
7296 key: "myapex.key",
7297 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007298 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007299 }
7300
7301 apex_key {
7302 name: "myapex.key",
7303 public_key: "testkey.avbpubkey",
7304 private_key: "testkey.pem",
7305 }
7306
7307 java_library {
7308 name: "bar",
7309 srcs: ["a.java"],
7310 libs: ["foo"],
7311 apex_available: ["myapex"],
7312 sdk_version: "none",
7313 system_modules: "none",
7314 }
7315`),
7316 "source/a.java": nil,
7317 "source/api/current.txt": nil,
7318 "source/api/removed.txt": nil,
7319 "source/Android.bp": []byte(`
7320 package {
7321 default_visibility: ["//visibility:private"],
7322 }
7323
7324 java_sdk_library {
7325 name: "foo",
7326 visibility: ["//apex"],
7327 srcs: ["a.java"],
7328 api_packages: ["foo"],
7329 apex_available: ["myapex"],
7330 sdk_version: "none",
7331 system_modules: "none",
7332 public: {
7333 enabled: true,
7334 },
7335 }
7336`),
7337 "prebuilt/a.jar": nil,
7338 "prebuilt/Android.bp": []byte(`
7339 package {
7340 default_visibility: ["//visibility:private"],
7341 }
7342
7343 java_sdk_library_import {
7344 name: "foo",
7345 visibility: ["//apex", "//source"],
7346 apex_available: ["myapex"],
7347 prefer: true,
7348 public: {
7349 jars: ["a.jar"],
7350 },
7351 }
7352`),
Anton Hanssondff2c782020-12-21 17:10:01 +00007353 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01007354 )
7355
7356 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007357 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffineedc5d52020-06-12 17:46:39 +01007358 "javalib/bar.jar",
7359 "javalib/foo.jar",
7360 "etc/permissions/foo.xml",
7361 })
7362
7363 // The bar library should depend on the implementation jar.
7364 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kanga3a05462024-04-05 00:36:44 +00007365 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01007366 t.Errorf("expected %q, found %#q", expected, actual)
7367 }
7368}
7369
7370func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
7371 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
7372 apex {
7373 name: "myapex",
7374 key: "myapex.key",
7375 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007376 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007377 }
7378
7379 apex_key {
7380 name: "myapex.key",
7381 public_key: "testkey.avbpubkey",
7382 private_key: "testkey.pem",
7383 }
7384
7385 java_sdk_library_import {
7386 name: "foo",
7387 apex_available: ["myapex"],
7388 prefer: true,
7389 public: {
7390 jars: ["a.jar"],
7391 },
7392 }
7393
7394 `, withFiles(filesForSdkLibrary))
7395}
7396
atrost6e126252020-01-27 17:01:16 +00007397func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007398 result := android.GroupFixturePreparers(
7399 prepareForApexTest,
7400 java.PrepareForTestWithPlatformCompatConfig,
7401 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007402 apex {
7403 name: "myapex",
7404 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007405 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007406 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007407 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007408 }
7409
7410 apex_key {
7411 name: "myapex.key",
7412 public_key: "testkey.avbpubkey",
7413 private_key: "testkey.pem",
7414 }
7415
7416 platform_compat_config {
7417 name: "myjar-platform-compat-config",
7418 src: ":myjar",
7419 }
7420
7421 java_library {
7422 name: "myjar",
7423 srcs: ["foo/bar/MyClass.java"],
7424 sdk_version: "none",
7425 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007426 apex_available: [ "myapex" ],
7427 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007428
7429 // Make sure that a preferred prebuilt does not affect the apex contents.
7430 prebuilt_platform_compat_config {
7431 name: "myjar-platform-compat-config",
7432 metadata: "compat-config/metadata.xml",
7433 prefer: true,
7434 }
atrost6e126252020-01-27 17:01:16 +00007435 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007436 ctx := result.TestContext
Jooyung Hana0503a52023-08-23 13:12:50 +09007437 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
atrost6e126252020-01-27 17:01:16 +00007438 "etc/compatconfig/myjar-platform-compat-config.xml",
7439 "javalib/myjar.jar",
7440 })
7441}
7442
Jooyung Han862c0d62022-12-21 10:15:37 +09007443func TestNoDupeApexFiles(t *testing.T) {
7444 android.GroupFixturePreparers(
7445 android.PrepareForTestWithAndroidBuildComponents,
7446 PrepareForTestWithApexBuildComponents,
7447 prepareForTestWithMyapex,
7448 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7449 ).
7450 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7451 RunTestWithBp(t, `
7452 apex {
7453 name: "myapex",
7454 key: "myapex.key",
7455 prebuilts: ["foo", "bar"],
7456 updatable: false,
7457 }
7458
7459 apex_key {
7460 name: "myapex.key",
7461 public_key: "testkey.avbpubkey",
7462 private_key: "testkey.pem",
7463 }
7464
7465 prebuilt_etc {
7466 name: "foo",
7467 src: "myprebuilt",
7468 filename_from_src: true,
7469 }
7470
7471 prebuilt_etc {
7472 name: "bar",
7473 src: "myprebuilt",
7474 filename_from_src: true,
7475 }
7476 `)
7477}
7478
Jooyung Hana8bd72a2023-11-02 11:56:48 +09007479func TestApexUnwantedTransitiveDeps(t *testing.T) {
7480 bp := `
7481 apex {
7482 name: "myapex",
7483 key: "myapex.key",
7484 native_shared_libs: ["libfoo"],
7485 updatable: false,
7486 unwanted_transitive_deps: ["libbar"],
7487 }
7488
7489 apex_key {
7490 name: "myapex.key",
7491 public_key: "testkey.avbpubkey",
7492 private_key: "testkey.pem",
7493 }
7494
7495 cc_library {
7496 name: "libfoo",
7497 srcs: ["foo.cpp"],
7498 shared_libs: ["libbar"],
7499 apex_available: ["myapex"],
7500 }
7501
7502 cc_library {
7503 name: "libbar",
7504 srcs: ["bar.cpp"],
7505 apex_available: ["myapex"],
7506 }`
7507 ctx := testApex(t, bp)
7508 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
7509 "*/libc++.so",
7510 "*/libfoo.so",
7511 // not libbar.so
7512 })
7513}
7514
Jiyong Park479321d2019-12-16 11:47:12 +09007515func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7516 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7517 apex {
7518 name: "myapex",
7519 key: "myapex.key",
7520 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007521 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007522 }
7523
7524 apex_key {
7525 name: "myapex.key",
7526 public_key: "testkey.avbpubkey",
7527 private_key: "testkey.pem",
7528 }
7529
7530 java_library {
7531 name: "myjar",
7532 srcs: ["foo/bar/MyClass.java"],
7533 sdk_version: "none",
7534 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007535 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007536 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007537 }
7538 `)
7539}
7540
Jiyong Park7afd1072019-12-30 16:56:33 +09007541func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007542 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007543 apex {
7544 name: "myapex",
7545 key: "myapex.key",
7546 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007547 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007548 }
7549
7550 apex_key {
7551 name: "myapex.key",
7552 public_key: "testkey.avbpubkey",
7553 private_key: "testkey.pem",
7554 }
7555
7556 cc_library {
7557 name: "mylib",
7558 srcs: ["mylib.cpp"],
7559 system_shared_libs: [],
7560 stl: "none",
7561 required: ["a", "b"],
7562 host_required: ["c", "d"],
7563 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007564 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007565 }
7566 `)
7567
Jooyung Hana0503a52023-08-23 13:12:50 +09007568 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007569 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007570 name := apexBundle.BaseModuleName()
7571 prefix := "TARGET_"
7572 var builder strings.Builder
7573 data.Custom(&builder, name, prefix, "", data)
7574 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007575 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 a b\n")
Sasha Smundakdcb61292022-12-08 10:41:33 -08007576 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7577 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007578}
7579
Jiyong Park7cd10e32020-01-14 09:22:18 +09007580func TestSymlinksFromApexToSystem(t *testing.T) {
7581 bp := `
7582 apex {
7583 name: "myapex",
7584 key: "myapex.key",
7585 native_shared_libs: ["mylib"],
7586 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007587 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007588 }
7589
Jiyong Park9d677202020-02-19 16:29:35 +09007590 apex {
7591 name: "myapex.updatable",
7592 key: "myapex.key",
7593 native_shared_libs: ["mylib"],
7594 java_libs: ["myjar"],
7595 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007596 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007597 }
7598
Jiyong Park7cd10e32020-01-14 09:22:18 +09007599 apex_key {
7600 name: "myapex.key",
7601 public_key: "testkey.avbpubkey",
7602 private_key: "testkey.pem",
7603 }
7604
7605 cc_library {
7606 name: "mylib",
7607 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007608 shared_libs: [
7609 "myotherlib",
7610 "myotherlib_ext",
7611 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007612 system_shared_libs: [],
7613 stl: "none",
7614 apex_available: [
7615 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007616 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007617 "//apex_available:platform",
7618 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007619 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007620 }
7621
7622 cc_library {
7623 name: "myotherlib",
7624 srcs: ["mylib.cpp"],
7625 system_shared_libs: [],
7626 stl: "none",
7627 apex_available: [
7628 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007629 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007630 "//apex_available:platform",
7631 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007632 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007633 }
7634
Jiyong Parkce243632023-02-17 18:22:25 +09007635 cc_library {
7636 name: "myotherlib_ext",
7637 srcs: ["mylib.cpp"],
7638 system_shared_libs: [],
7639 system_ext_specific: true,
7640 stl: "none",
7641 apex_available: [
7642 "myapex",
7643 "myapex.updatable",
7644 "//apex_available:platform",
7645 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007646 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007647 }
7648
Jiyong Park7cd10e32020-01-14 09:22:18 +09007649 java_library {
7650 name: "myjar",
7651 srcs: ["foo/bar/MyClass.java"],
7652 sdk_version: "none",
7653 system_modules: "none",
7654 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007655 apex_available: [
7656 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007657 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007658 "//apex_available:platform",
7659 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007660 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007661 }
7662
7663 java_library {
7664 name: "myotherjar",
7665 srcs: ["foo/bar/MyClass.java"],
7666 sdk_version: "none",
7667 system_modules: "none",
7668 apex_available: [
7669 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007670 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007671 "//apex_available:platform",
7672 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007673 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007674 }
7675 `
7676
7677 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7678 for _, f := range files {
7679 if f.path == file {
7680 if f.isLink {
7681 t.Errorf("%q is not a real file", file)
7682 }
7683 return
7684 }
7685 }
7686 t.Errorf("%q is not found", file)
7687 }
7688
Jiyong Parkce243632023-02-17 18:22:25 +09007689 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007690 for _, f := range files {
7691 if f.path == file {
7692 if !f.isLink {
7693 t.Errorf("%q is not a symlink", file)
7694 }
Jiyong Parkce243632023-02-17 18:22:25 +09007695 if f.src != target {
7696 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7697 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007698 return
7699 }
7700 }
7701 t.Errorf("%q is not found", file)
7702 }
7703
Jiyong Park9d677202020-02-19 16:29:35 +09007704 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7705 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007706 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana0503a52023-08-23 13:12:50 +09007707 files := getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007708 ensureRealfileExists(t, files, "javalib/myjar.jar")
7709 ensureRealfileExists(t, files, "lib64/mylib.so")
7710 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007711 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007712
Jooyung Hana0503a52023-08-23 13:12:50 +09007713 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007714 ensureRealfileExists(t, files, "javalib/myjar.jar")
7715 ensureRealfileExists(t, files, "lib64/mylib.so")
7716 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007717 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007718
7719 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007720 ctx = testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +09007721 files = getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007722 ensureRealfileExists(t, files, "javalib/myjar.jar")
7723 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007724 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7725 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007726
Jooyung Hana0503a52023-08-23 13:12:50 +09007727 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007728 ensureRealfileExists(t, files, "javalib/myjar.jar")
7729 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007730 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7731 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007732}
7733
Yo Chiange8128052020-07-23 20:09:18 +08007734func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007735 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007736 apex {
7737 name: "myapex",
7738 key: "myapex.key",
7739 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007740 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007741 }
7742
7743 apex_key {
7744 name: "myapex.key",
7745 public_key: "testkey.avbpubkey",
7746 private_key: "testkey.pem",
7747 }
7748
7749 cc_library_shared {
7750 name: "mylib",
7751 srcs: ["mylib.cpp"],
7752 shared_libs: ["myotherlib"],
7753 system_shared_libs: [],
7754 stl: "none",
7755 apex_available: [
7756 "myapex",
7757 "//apex_available:platform",
7758 ],
7759 }
7760
7761 cc_prebuilt_library_shared {
7762 name: "myotherlib",
7763 srcs: ["prebuilt.so"],
7764 system_shared_libs: [],
7765 stl: "none",
7766 apex_available: [
7767 "myapex",
7768 "//apex_available:platform",
7769 ],
7770 }
7771 `)
7772
Jooyung Hana0503a52023-08-23 13:12:50 +09007773 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007774 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007775 var builder strings.Builder
7776 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7777 androidMk := builder.String()
7778 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007779 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007780 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7781 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7782 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007783 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 myotherlib:64\n")
Yo Chiange8128052020-07-23 20:09:18 +08007784}
7785
Jooyung Han643adc42020-02-27 13:50:06 +09007786func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007787 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007788 apex {
7789 name: "myapex",
7790 key: "myapex.key",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007791 binaries: ["mybin"],
7792 jni_libs: ["mylib", "mylib3", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007793 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007794 }
7795
7796 apex_key {
7797 name: "myapex.key",
7798 public_key: "testkey.avbpubkey",
7799 private_key: "testkey.pem",
7800 }
7801
7802 cc_library {
7803 name: "mylib",
7804 srcs: ["mylib.cpp"],
7805 shared_libs: ["mylib2"],
7806 system_shared_libs: [],
7807 stl: "none",
7808 apex_available: [ "myapex" ],
7809 }
7810
7811 cc_library {
7812 name: "mylib2",
7813 srcs: ["mylib.cpp"],
7814 system_shared_libs: [],
7815 stl: "none",
7816 apex_available: [ "myapex" ],
7817 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007818
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007819 // Used as both a JNI library and a regular shared library.
7820 cc_library {
7821 name: "mylib3",
7822 srcs: ["mylib.cpp"],
7823 system_shared_libs: [],
7824 stl: "none",
7825 apex_available: [ "myapex" ],
7826 }
7827
7828 cc_binary {
7829 name: "mybin",
7830 srcs: ["mybin.cpp"],
7831 shared_libs: ["mylib3"],
7832 system_shared_libs: [],
7833 stl: "none",
7834 apex_available: [ "myapex" ],
7835 }
7836
Jiyong Park34d5c332022-02-24 18:02:44 +09007837 rust_ffi_shared {
7838 name: "libfoo.rust",
7839 crate_name: "foo",
7840 srcs: ["foo.rs"],
7841 shared_libs: ["libfoo.shared_from_rust"],
7842 prefer_rlib: true,
7843 apex_available: ["myapex"],
7844 }
7845
7846 cc_library_shared {
7847 name: "libfoo.shared_from_rust",
7848 srcs: ["mylib.cpp"],
7849 system_shared_libs: [],
7850 stl: "none",
7851 stubs: {
7852 versions: ["10", "11", "12"],
7853 },
7854 }
7855
Jooyung Han643adc42020-02-27 13:50:06 +09007856 `)
7857
Jooyung Hana0503a52023-08-23 13:12:50 +09007858 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han643adc42020-02-27 13:50:06 +09007859 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007860 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so mylib3.so")
Jooyung Hana0503a52023-08-23 13:12:50 +09007861 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007862 "bin/mybin",
Jooyung Han643adc42020-02-27 13:50:06 +09007863 "lib64/mylib.so",
7864 "lib64/mylib2.so",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007865 "lib64/mylib3.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007866 "lib64/libfoo.rust.so",
7867 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7868 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007869 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007870
7871 // b/220397949
7872 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007873}
7874
Jooyung Han49f67012020-04-17 13:43:10 +09007875func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007876 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007877 apex {
7878 name: "myapex",
7879 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007880 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007881 }
7882 apex_key {
7883 name: "myapex.key",
7884 public_key: "testkey.avbpubkey",
7885 private_key: "testkey.pem",
7886 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007887 `,
7888 android.FixtureModifyConfig(func(config android.Config) {
7889 delete(config.Targets, android.Android)
7890 config.AndroidCommonTarget = android.Target{}
7891 }),
7892 )
Jooyung Han49f67012020-04-17 13:43:10 +09007893
7894 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7895 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7896 }
7897}
7898
Jiyong Parkbd159612020-02-28 15:22:21 +09007899func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007900 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007901 apex {
7902 name: "myapex",
7903 key: "myapex.key",
7904 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007905 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007906 }
7907
7908 apex_key {
7909 name: "myapex.key",
7910 public_key: "testkey.avbpubkey",
7911 private_key: "testkey.pem",
7912 }
7913
7914 android_app {
7915 name: "AppFoo",
7916 srcs: ["foo/bar/MyClass.java"],
7917 sdk_version: "none",
7918 system_modules: "none",
7919 apex_available: [ "myapex" ],
7920 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007921 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007922
Jooyung Hana0503a52023-08-23 13:12:50 +09007923 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07007924 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Jiyong Parkbd159612020-02-28 15:22:21 +09007925
7926 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007927 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 +09007928}
7929
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007930func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007931 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007932 apex {
7933 name: "myapex",
7934 key: "myapex.key",
7935 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007936 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007937 }
7938
7939 apex_key {
7940 name: "myapex.key",
7941 public_key: "testkey.avbpubkey",
7942 private_key: "testkey.pem",
7943 }
7944
7945 android_app_set {
7946 name: "AppSet",
7947 set: "AppSet.apks",
7948 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09007949 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crosscf371cc2020-11-13 11:48:42 -08007950 bundleConfigRule := mod.Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07007951 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007952 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7953 s := mod.Rule("apexRule").Args["copy_commands"]
7954 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007955 if len(copyCmds) != 4 {
7956 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007957 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007958 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7959 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007960 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7961 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007962
7963 // Ensure that canned_fs_config has an entry for the app set zip file
7964 generateFsRule := mod.Rule("generateFsConfig")
7965 cmd := generateFsRule.RuleParams.Command
7966 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007967}
7968
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007969func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007970 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007971 apex_set {
7972 name: "myapex",
7973 filename: "foo_v2.apex",
7974 sanitized: {
7975 none: { set: "myapex.apks", },
7976 hwaddress: { set: "myapex.hwasan.apks", },
7977 },
Paul Duffin24704672021-04-06 16:09:30 +01007978 }
7979 `
7980 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007981
Paul Duffin24704672021-04-06 16:09:30 +01007982 // Check that the extractor produces the correct output file from the correct input file.
Spandan Das3576e762024-01-03 18:57:03 +00007983 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007984
Spandan Das3576e762024-01-03 18:57:03 +00007985 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Paul Duffin24704672021-04-06 16:09:30 +01007986 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007987
Paul Duffin24704672021-04-06 16:09:30 +01007988 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7989
7990 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007991 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7992 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007993
7994 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007995}
7996
Pranav Guptaeba03b02022-09-27 00:27:08 +00007997func TestApexSetApksModuleAssignment(t *testing.T) {
7998 ctx := testApex(t, `
7999 apex_set {
8000 name: "myapex",
8001 set: ":myapex_apks_file",
8002 }
8003
8004 filegroup {
8005 name: "myapex_apks_file",
8006 srcs: ["myapex.apks"],
8007 }
8008 `)
8009
Spandan Das3576e762024-01-03 18:57:03 +00008010 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Pranav Guptaeba03b02022-09-27 00:27:08 +00008011
8012 // Check that the extractor produces the correct apks file from the input module
Spandan Das3576e762024-01-03 18:57:03 +00008013 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.apks"
Pranav Guptaeba03b02022-09-27 00:27:08 +00008014 extractedApex := m.Output(extractorOutput)
8015
8016 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
8017}
8018
Paul Duffin89f570a2021-06-16 01:42:33 +01008019func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00008020 t.Helper()
8021
Paul Duffin55607122021-03-30 23:32:51 +01008022 fs := android.MockFS{
8023 "a.java": nil,
8024 "a.jar": nil,
8025 "apex_manifest.json": nil,
8026 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00008027 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00008028 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
8029 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
8030 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00008031 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008032 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008033
Paul Duffin55607122021-03-30 23:32:51 +01008034 errorHandler := android.FixtureExpectsNoErrors
8035 if errmsg != "" {
8036 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008037 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008038
Paul Duffin55607122021-03-30 23:32:51 +01008039 result := android.GroupFixturePreparers(
8040 cc.PrepareForTestWithCcDefaultModules,
8041 java.PrepareForTestWithHiddenApiBuildComponents,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008042 java.PrepareForTestWithDexpreopt,
Paul Duffin55607122021-03-30 23:32:51 +01008043 java.PrepareForTestWithJavaSdkLibraryFiles,
8044 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01008045 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01008046 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01008047 android.FixtureModifyMockFS(func(fs android.MockFS) {
8048 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
8049 insert := ""
8050 for _, fragment := range fragments {
8051 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
8052 }
8053 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
8054 platform_bootclasspath {
8055 name: "platform-bootclasspath",
8056 fragments: [
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008057 {apex: "com.android.art", module: "art-bootclasspath-fragment"},
Paul Duffin89f570a2021-06-16 01:42:33 +01008058 %s
8059 ],
8060 }
8061 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01008062 }
Paul Duffin89f570a2021-06-16 01:42:33 +01008063 }),
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008064 // Dexpreopt for boot jars requires the ART boot image profile.
8065 java.PrepareApexBootJarModule("com.android.art", "core-oj"),
8066 dexpreopt.FixtureSetArtBootJars("com.android.art:core-oj"),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00008067 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01008068 ).
8069 ExtendWithErrorHandler(errorHandler).
8070 RunTestWithBp(t, bp)
8071
8072 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008073}
8074
Paul Duffin5556c5f2022-06-09 17:32:21 +00008075func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008076 preparers := android.GroupFixturePreparers(
8077 java.PrepareForTestWithJavaDefaultModules,
Spandan Das5be63332023-12-13 00:06:32 +00008078 prepareForTestWithBootclasspathFragment,
8079 dexpreopt.FixtureSetTestOnlyArtBootImageJars("com.android.art:libfoo"),
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008080 PrepareForTestWithApexBuildComponents,
8081 ).
8082 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
Spandan Das3576e762024-01-03 18:57:03 +00008083 "Multiple installable prebuilt APEXes provide ambiguous deapexers: prebuilt_com.android.art and prebuilt_com.mycompany.android.art"))
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008084
8085 bpBase := `
8086 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008087 name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008088 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008089 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008090 set: "myapex.apks",
8091 }
8092
8093 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008094 name: "com.mycompany.android.art",
8095 apex_name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008096 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008097 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008098 set: "company-myapex.apks",
8099 }
8100
8101 prebuilt_bootclasspath_fragment {
Spandan Das5be63332023-12-13 00:06:32 +00008102 name: "art-bootclasspath-fragment",
8103 apex_available: ["com.android.art"],
Spandan Dasfae468e2023-12-12 23:23:53 +00008104 hidden_api: {
8105 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8106 metadata: "my-bootclasspath-fragment/metadata.csv",
8107 index: "my-bootclasspath-fragment/index.csv",
8108 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
8109 all_flags: "my-bootclasspath-fragment/all-flags.csv",
8110 },
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008111 %s
8112 }
8113 `
8114
8115 t.Run("java_import", func(t *testing.T) {
8116 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8117 java_import {
8118 name: "libfoo",
8119 jars: ["libfoo.jar"],
Spandan Das5be63332023-12-13 00:06:32 +00008120 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008121 }
8122 `)
8123 })
8124
8125 t.Run("java_sdk_library_import", func(t *testing.T) {
8126 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8127 java_sdk_library_import {
8128 name: "libfoo",
8129 public: {
8130 jars: ["libbar.jar"],
8131 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008132 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008133 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008134 }
8135 `)
8136 })
8137
8138 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8139 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8140 image_name: "art",
8141 contents: ["libfoo"],
8142 `)+`
8143 java_sdk_library_import {
8144 name: "libfoo",
8145 public: {
8146 jars: ["libbar.jar"],
8147 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008148 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008149 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008150 }
8151 `)
8152 })
8153}
8154
Paul Duffin5556c5f2022-06-09 17:32:21 +00008155func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
8156 preparers := android.GroupFixturePreparers(
8157 java.PrepareForTestWithJavaDefaultModules,
8158 PrepareForTestWithApexBuildComponents,
8159 )
8160
Spandan Das59a4a2b2024-01-09 21:35:56 +00008161 errCtx := moduleErrorfTestCtx{}
8162
Paul Duffin5556c5f2022-06-09 17:32:21 +00008163 bpBase := `
8164 apex_set {
8165 name: "com.android.myapex",
8166 installable: true,
8167 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8168 set: "myapex.apks",
8169 }
8170
8171 apex_set {
8172 name: "com.android.myapex_compressed",
8173 apex_name: "com.android.myapex",
8174 installable: true,
8175 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8176 set: "myapex_compressed.apks",
8177 }
8178
8179 prebuilt_bootclasspath_fragment {
8180 name: "my-bootclasspath-fragment",
8181 apex_available: [
8182 "com.android.myapex",
8183 "com.android.myapex_compressed",
8184 ],
8185 hidden_api: {
8186 annotation_flags: "annotation-flags.csv",
8187 metadata: "metadata.csv",
8188 index: "index.csv",
8189 signature_patterns: "signature_patterns.csv",
8190 },
8191 %s
8192 }
8193 `
8194
8195 t.Run("java_import", func(t *testing.T) {
8196 result := preparers.RunTestWithBp(t,
8197 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8198 java_import {
8199 name: "libfoo",
8200 jars: ["libfoo.jar"],
8201 apex_available: [
8202 "com.android.myapex",
8203 "com.android.myapex_compressed",
8204 ],
8205 }
8206 `)
8207
8208 module := result.Module("libfoo", "android_common_com.android.myapex")
8209 usesLibraryDep := module.(java.UsesLibraryDependency)
8210 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008211 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008212 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008213 })
8214
8215 t.Run("java_sdk_library_import", func(t *testing.T) {
8216 result := preparers.RunTestWithBp(t,
8217 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8218 java_sdk_library_import {
8219 name: "libfoo",
8220 public: {
8221 jars: ["libbar.jar"],
8222 },
8223 apex_available: [
8224 "com.android.myapex",
8225 "com.android.myapex_compressed",
8226 ],
8227 compile_dex: true,
8228 }
8229 `)
8230
8231 module := result.Module("libfoo", "android_common_com.android.myapex")
8232 usesLibraryDep := module.(java.UsesLibraryDependency)
8233 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008234 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008235 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008236 })
8237
8238 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8239 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8240 image_name: "art",
8241 contents: ["libfoo"],
8242 `)+`
8243 java_sdk_library_import {
8244 name: "libfoo",
8245 public: {
8246 jars: ["libbar.jar"],
8247 },
8248 apex_available: [
8249 "com.android.myapex",
8250 "com.android.myapex_compressed",
8251 ],
8252 compile_dex: true,
8253 }
8254 `)
8255 })
8256}
8257
Jooyung Han548640b2020-04-27 12:10:30 +09008258func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
8259 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8260 apex {
8261 name: "myapex",
8262 key: "myapex.key",
8263 updatable: true,
8264 }
8265
8266 apex_key {
8267 name: "myapex.key",
8268 public_key: "testkey.avbpubkey",
8269 private_key: "testkey.pem",
8270 }
8271 `)
8272}
8273
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008274func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
8275 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8276 apex {
8277 name: "myapex",
8278 key: "myapex.key",
8279 }
8280
8281 apex_key {
8282 name: "myapex.key",
8283 public_key: "testkey.avbpubkey",
8284 private_key: "testkey.pem",
8285 }
8286 `)
8287}
8288
satayevb98371c2021-06-15 16:49:50 +01008289func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
8290 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
8291 apex {
8292 name: "myapex",
8293 key: "myapex.key",
8294 systemserverclasspath_fragments: [
8295 "mysystemserverclasspathfragment",
8296 ],
8297 min_sdk_version: "29",
8298 updatable: true,
8299 }
8300
8301 apex_key {
8302 name: "myapex.key",
8303 public_key: "testkey.avbpubkey",
8304 private_key: "testkey.pem",
8305 }
8306
8307 java_library {
8308 name: "foo",
8309 srcs: ["b.java"],
8310 min_sdk_version: "29",
8311 installable: true,
8312 apex_available: [
8313 "myapex",
8314 ],
8315 }
8316
8317 systemserverclasspath_fragment {
8318 name: "mysystemserverclasspathfragment",
8319 generate_classpaths_proto: false,
8320 contents: [
8321 "foo",
8322 ],
8323 apex_available: [
8324 "myapex",
8325 ],
8326 }
satayevabcd5972021-08-06 17:49:46 +01008327 `,
8328 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8329 )
satayevb98371c2021-06-15 16:49:50 +01008330}
8331
Paul Duffin064b70c2020-11-02 17:32:38 +00008332func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008333 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008334 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008335 fragment := java.ApexVariantReference{
8336 Apex: proptools.StringPtr("myapex"),
8337 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8338 }
8339
Paul Duffin064b70c2020-11-02 17:32:38 +00008340 testDexpreoptWithApexes(t, `
8341 prebuilt_apex {
8342 name: "myapex" ,
8343 arch: {
8344 arm64: {
8345 src: "myapex-arm64.apex",
8346 },
8347 arm: {
8348 src: "myapex-arm.apex",
8349 },
8350 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008351 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8352 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008353
Paul Duffin89f570a2021-06-16 01:42:33 +01008354 prebuilt_bootclasspath_fragment {
8355 name: "my-bootclasspath-fragment",
8356 contents: ["libfoo"],
8357 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008358 hidden_api: {
8359 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8360 metadata: "my-bootclasspath-fragment/metadata.csv",
8361 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008362 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8363 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8364 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008365 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008366 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008367
Paul Duffin89f570a2021-06-16 01:42:33 +01008368 java_import {
8369 name: "libfoo",
8370 jars: ["libfoo.jar"],
8371 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008372 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008373 }
8374 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008375 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008376}
8377
Spandan Dasf14e2542021-11-12 00:01:37 +00008378func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008379 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008380 bp += `
8381 apex_key {
8382 name: "myapex.key",
8383 public_key: "testkey.avbpubkey",
8384 private_key: "testkey.pem",
8385 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008386 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008387 "lib1/src/A.java": nil,
8388 "lib2/src/B.java": nil,
8389 "system/sepolicy/apex/myapex-file_contexts": nil,
8390 }
8391
Paul Duffin45338f02021-03-30 23:07:52 +01008392 errorHandler := android.FixtureExpectsNoErrors
8393 if errmsg != "" {
8394 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008395 }
Colin Crossae8600b2020-10-29 17:09:13 -07008396
Paul Duffin45338f02021-03-30 23:07:52 +01008397 android.GroupFixturePreparers(
8398 android.PrepareForTestWithAndroidBuildComponents,
8399 java.PrepareForTestWithJavaBuildComponents,
8400 PrepareForTestWithApexBuildComponents,
8401 android.PrepareForTestWithNeverallowRules(rules),
8402 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008403 apexBootJars := make([]string, 0, len(bootJars))
8404 for _, apexBootJar := range bootJars {
8405 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008406 }
satayevd604b212021-07-21 14:23:52 +01008407 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008408 }),
8409 fs.AddToFixture(),
8410 ).
8411 ExtendWithErrorHandler(errorHandler).
8412 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008413}
8414
8415func TestApexPermittedPackagesRules(t *testing.T) {
8416 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008417 name string
8418 expectedError string
8419 bp string
8420 bootJars []string
8421 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008422 }{
8423
8424 {
8425 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8426 expectedError: "",
8427 bp: `
8428 java_library {
8429 name: "bcp_lib1",
8430 srcs: ["lib1/src/*.java"],
8431 permitted_packages: ["foo.bar"],
8432 apex_available: ["myapex"],
8433 sdk_version: "none",
8434 system_modules: "none",
8435 }
8436 java_library {
8437 name: "nonbcp_lib2",
8438 srcs: ["lib2/src/*.java"],
8439 apex_available: ["myapex"],
8440 permitted_packages: ["a.b"],
8441 sdk_version: "none",
8442 system_modules: "none",
8443 }
8444 apex {
8445 name: "myapex",
8446 key: "myapex.key",
8447 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008448 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008449 }`,
8450 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008451 bcpPermittedPackages: map[string][]string{
8452 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008453 "foo.bar",
8454 },
8455 },
8456 },
8457 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008458 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008459 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 +01008460 bp: `
8461 java_library {
8462 name: "bcp_lib1",
8463 srcs: ["lib1/src/*.java"],
8464 apex_available: ["myapex"],
8465 permitted_packages: ["foo.bar"],
8466 sdk_version: "none",
8467 system_modules: "none",
8468 }
8469 java_library {
8470 name: "bcp_lib2",
8471 srcs: ["lib2/src/*.java"],
8472 apex_available: ["myapex"],
8473 permitted_packages: ["foo.bar", "bar.baz"],
8474 sdk_version: "none",
8475 system_modules: "none",
8476 }
8477 apex {
8478 name: "myapex",
8479 key: "myapex.key",
8480 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008481 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008482 }
8483 `,
8484 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008485 bcpPermittedPackages: map[string][]string{
8486 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008487 "foo.bar",
8488 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008489 "bcp_lib2": []string{
8490 "foo.bar",
8491 },
8492 },
8493 },
8494 {
8495 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8496 expectedError: "",
8497 bp: `
8498 java_library {
8499 name: "bcp_lib_restricted",
8500 srcs: ["lib1/src/*.java"],
8501 apex_available: ["myapex"],
8502 permitted_packages: ["foo.bar"],
8503 sdk_version: "none",
8504 min_sdk_version: "29",
8505 system_modules: "none",
8506 }
8507 java_library {
8508 name: "bcp_lib_unrestricted",
8509 srcs: ["lib2/src/*.java"],
8510 apex_available: ["myapex"],
8511 permitted_packages: ["foo.bar", "bar.baz"],
8512 sdk_version: "none",
8513 min_sdk_version: "29",
8514 system_modules: "none",
8515 }
8516 apex {
8517 name: "myapex",
8518 key: "myapex.key",
8519 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8520 updatable: true,
8521 min_sdk_version: "29",
8522 }
8523 `,
8524 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8525 bcpPermittedPackages: map[string][]string{
8526 "bcp_lib1_non_updateable": []string{
8527 "foo.bar",
8528 },
8529 // 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 +01008530 },
8531 },
8532 }
8533 for _, tc := range testcases {
8534 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008535 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8536 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008537 })
8538 }
8539}
8540
Jiyong Park62304bb2020-04-13 16:19:48 +09008541func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008542 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008543 apex {
8544 name: "myapex",
8545 key: "myapex.key",
8546 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008547 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008548 }
8549
8550 apex_key {
8551 name: "myapex.key",
8552 public_key: "testkey.avbpubkey",
8553 private_key: "testkey.pem",
8554 }
8555
8556 cc_library {
8557 name: "mylib",
8558 srcs: ["mylib.cpp"],
8559 system_shared_libs: [],
8560 stl: "none",
8561 stubs: {
8562 versions: ["1"],
8563 },
8564 apex_available: ["myapex"],
8565 }
8566
8567 cc_library {
8568 name: "myprivlib",
8569 srcs: ["mylib.cpp"],
8570 system_shared_libs: [],
8571 stl: "none",
8572 apex_available: ["myapex"],
8573 }
8574
8575
8576 cc_test {
8577 name: "mytest",
8578 gtest: false,
8579 srcs: ["mylib.cpp"],
8580 system_shared_libs: [],
8581 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008582 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008583 test_for: ["myapex"]
8584 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008585
8586 cc_library {
8587 name: "mytestlib",
8588 srcs: ["mylib.cpp"],
8589 system_shared_libs: [],
8590 shared_libs: ["mylib", "myprivlib"],
8591 stl: "none",
8592 test_for: ["myapex"],
8593 }
8594
8595 cc_benchmark {
8596 name: "mybench",
8597 srcs: ["mylib.cpp"],
8598 system_shared_libs: [],
8599 shared_libs: ["mylib", "myprivlib"],
8600 stl: "none",
8601 test_for: ["myapex"],
8602 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008603 `)
8604
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008605 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008606 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008607 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8608 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8609 }
8610
8611 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008612 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008613 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8614 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8615 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8616}
Jiyong Park46a512f2020-12-04 18:02:13 +09008617
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008618func TestIndirectTestFor(t *testing.T) {
8619 ctx := testApex(t, `
8620 apex {
8621 name: "myapex",
8622 key: "myapex.key",
8623 native_shared_libs: ["mylib", "myprivlib"],
8624 updatable: false,
8625 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008626
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008627 apex_key {
8628 name: "myapex.key",
8629 public_key: "testkey.avbpubkey",
8630 private_key: "testkey.pem",
8631 }
8632
8633 cc_library {
8634 name: "mylib",
8635 srcs: ["mylib.cpp"],
8636 system_shared_libs: [],
8637 stl: "none",
8638 stubs: {
8639 versions: ["1"],
8640 },
8641 apex_available: ["myapex"],
8642 }
8643
8644 cc_library {
8645 name: "myprivlib",
8646 srcs: ["mylib.cpp"],
8647 system_shared_libs: [],
8648 stl: "none",
8649 shared_libs: ["mylib"],
8650 apex_available: ["myapex"],
8651 }
8652
8653 cc_library {
8654 name: "mytestlib",
8655 srcs: ["mylib.cpp"],
8656 system_shared_libs: [],
8657 shared_libs: ["myprivlib"],
8658 stl: "none",
8659 test_for: ["myapex"],
8660 }
8661 `)
8662
8663 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008664 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008665 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8666 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8667 }
8668
8669 // The platform variant of mytestlib links to the platform variant of the
8670 // internal myprivlib.
8671 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8672
8673 // The platform variant of myprivlib links to the platform variant of mylib
8674 // and bypasses its stubs.
8675 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 +09008676}
8677
Martin Stjernholmec009002021-03-27 15:18:31 +00008678func TestTestForForLibInOtherApex(t *testing.T) {
8679 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8680 _ = testApex(t, `
8681 apex {
8682 name: "com.android.art",
8683 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008684 native_shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008685 updatable: false,
8686 }
8687
8688 apex {
8689 name: "com.android.art.debug",
8690 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008691 native_shared_libs: ["libnativebridge", "libnativebrdige_test"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008692 updatable: false,
8693 }
8694
8695 apex_key {
8696 name: "myapex.key",
8697 public_key: "testkey.avbpubkey",
8698 private_key: "testkey.pem",
8699 }
8700
8701 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008702 name: "libnativebridge",
8703 srcs: ["libnativebridge.cpp"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008704 system_shared_libs: [],
8705 stl: "none",
8706 stubs: {
8707 versions: ["1"],
8708 },
8709 apex_available: ["com.android.art", "com.android.art.debug"],
8710 }
8711
8712 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008713 name: "libnativebrdige_test",
Martin Stjernholmec009002021-03-27 15:18:31 +00008714 srcs: ["mylib.cpp"],
8715 system_shared_libs: [],
Spandan Das20fce2d2023-04-12 17:21:39 +00008716 shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008717 stl: "none",
8718 apex_available: ["com.android.art.debug"],
8719 test_for: ["com.android.art"],
8720 }
8721 `,
8722 android.MockFS{
8723 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8724 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8725 }.AddToFixture())
8726}
8727
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008728// TODO(jungjw): Move this to proptools
8729func intPtr(i int) *int {
8730 return &i
8731}
8732
8733func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008734 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008735 apex_set {
8736 name: "myapex",
8737 set: "myapex.apks",
8738 filename: "foo_v2.apex",
8739 overrides: ["foo"],
8740 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008741 `,
8742 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8743 variables.Platform_sdk_version = intPtr(30)
8744 }),
8745 android.FixtureModifyConfig(func(config android.Config) {
8746 config.Targets[android.Android] = []android.Target{
8747 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8748 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8749 }
8750 }),
8751 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008752
Spandan Das3576e762024-01-03 18:57:03 +00008753 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008754
8755 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008756 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008757 actual := extractedApex.Args["abis"]
8758 expected := "ARMEABI_V7A,ARM64_V8A"
8759 if actual != expected {
8760 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8761 }
8762 actual = extractedApex.Args["sdk-version"]
8763 expected = "30"
8764 if actual != expected {
8765 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8766 }
8767
Paul Duffin6717d882021-06-15 19:09:41 +01008768 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008769 a := m.Module().(*ApexSet)
8770 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008771 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008772 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8773 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8774 }
8775}
8776
Anton Hansson805e0a52022-11-25 14:06:46 +00008777func TestApexSet_NativeBridge(t *testing.T) {
8778 ctx := testApex(t, `
8779 apex_set {
8780 name: "myapex",
8781 set: "myapex.apks",
8782 filename: "foo_v2.apex",
8783 overrides: ["foo"],
8784 }
8785 `,
8786 android.FixtureModifyConfig(func(config android.Config) {
8787 config.Targets[android.Android] = []android.Target{
8788 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8789 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8790 }
8791 }),
8792 )
8793
Spandan Das3576e762024-01-03 18:57:03 +00008794 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Anton Hansson805e0a52022-11-25 14:06:46 +00008795
8796 // Check extract_apks tool parameters. No native bridge arch expected
8797 extractedApex := m.Output("extracted/myapex.apks")
8798 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8799}
8800
Jiyong Park7d95a512020-05-10 15:16:24 +09008801func TestNoStaticLinkingToStubsLib(t *testing.T) {
8802 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8803 apex {
8804 name: "myapex",
8805 key: "myapex.key",
8806 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008807 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008808 }
8809
8810 apex_key {
8811 name: "myapex.key",
8812 public_key: "testkey.avbpubkey",
8813 private_key: "testkey.pem",
8814 }
8815
8816 cc_library {
8817 name: "mylib",
8818 srcs: ["mylib.cpp"],
8819 static_libs: ["otherlib"],
8820 system_shared_libs: [],
8821 stl: "none",
8822 apex_available: [ "myapex" ],
8823 }
8824
8825 cc_library {
8826 name: "otherlib",
8827 srcs: ["mylib.cpp"],
8828 system_shared_libs: [],
8829 stl: "none",
8830 stubs: {
8831 versions: ["1", "2", "3"],
8832 },
8833 apex_available: [ "myapex" ],
8834 }
8835 `)
8836}
8837
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008838func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008839 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008840 apex {
8841 name: "myapex",
8842 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008843 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008844 custom_sign_tool: "sign_myapex",
8845 }
8846
8847 apex_key {
8848 name: "myapex.key",
8849 public_key: "testkey.avbpubkey",
8850 private_key: "testkey.pem",
8851 }
8852 `)
8853
Jooyung Han286957d2023-10-30 16:17:56 +09008854 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07008855 content := android.ContentFromFileRuleForTests(t, ctx, myapex.Output("apexkeys.txt"))
Jooyung Haneec1b3f2023-06-20 16:25:59 +09008856 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 +09008857}
8858
8859func TestApexKeysTxtOverrides(t *testing.T) {
8860 ctx := testApex(t, `
8861 apex {
8862 name: "myapex",
8863 key: "myapex.key",
8864 updatable: false,
8865 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008866 }
8867
8868 apex_key {
8869 name: "myapex.key",
8870 public_key: "testkey.avbpubkey",
8871 private_key: "testkey.pem",
8872 }
8873
8874 prebuilt_apex {
8875 name: "myapex",
8876 prefer: true,
8877 arch: {
8878 arm64: {
8879 src: "myapex-arm64.apex",
8880 },
8881 arm: {
8882 src: "myapex-arm.apex",
8883 },
8884 },
8885 }
8886
8887 apex_set {
8888 name: "myapex_set",
8889 set: "myapex.apks",
8890 filename: "myapex_set.apex",
8891 overrides: ["myapex"],
8892 }
8893 `)
8894
Colin Crossf61d03d2023-11-02 16:56:39 -07008895 content := android.ContentFromFileRuleForTests(t, ctx,
8896 ctx.ModuleForTests("myapex", "android_common_myapex").Output("apexkeys.txt"))
Jooyung Han286957d2023-10-30 16:17:56 +09008897 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 -07008898 content = android.ContentFromFileRuleForTests(t, ctx,
8899 ctx.ModuleForTests("myapex_set", "android_common_myapex_set").Output("apexkeys.txt"))
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008900 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 +09008901}
8902
Jooyung Han938b5932020-06-20 12:47:47 +09008903func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008904 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008905 apex {
8906 name: "myapex",
8907 key: "myapex.key",
8908 apps: ["app"],
8909 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008910 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008911 }
8912
8913 apex_key {
8914 name: "myapex.key",
8915 public_key: "testkey.avbpubkey",
8916 private_key: "testkey.pem",
8917 }
8918
8919 android_app {
8920 name: "app",
8921 srcs: ["foo/bar/MyClass.java"],
8922 package_name: "foo",
8923 sdk_version: "none",
8924 system_modules: "none",
8925 apex_available: [ "myapex" ],
8926 }
8927 `, withFiles(map[string][]byte{
8928 "sub/Android.bp": []byte(`
8929 override_apex {
8930 name: "override_myapex",
8931 base: "myapex",
8932 apps: ["override_app"],
8933 allowed_files: ":allowed",
8934 }
8935 // Overridable "path" property should be referenced indirectly
8936 filegroup {
8937 name: "allowed",
8938 srcs: ["allowed.txt"],
8939 }
8940 override_android_app {
8941 name: "override_app",
8942 base: "app",
8943 package_name: "bar",
8944 }
8945 `),
8946 }))
8947
Jooyung Hana0503a52023-08-23 13:12:50 +09008948 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008949 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8950 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8951 }
8952
Spandan Das50801e22024-05-13 18:29:45 +00008953 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008954 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8955 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8956 }
8957}
8958
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008959func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008960 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008961 apex {
8962 name: "myapex",
8963 key: "myapex.key",
8964 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008965 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008966 }
8967
8968 apex_key {
8969 name: "myapex.key",
8970 public_key: "testkey.avbpubkey",
8971 private_key: "testkey.pem",
8972 }
8973
8974 cc_library {
8975 name: "mylib",
8976 srcs: ["mylib.cpp"],
8977 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008978 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008979 },
8980 apex_available: ["myapex"],
8981 }
8982
8983 cc_prebuilt_library_shared {
8984 name: "mylib",
8985 prefer: false,
8986 srcs: ["prebuilt.so"],
8987 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008988 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008989 },
8990 apex_available: ["myapex"],
8991 }
8992 `)
8993}
8994
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008995func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008996 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008997 apex {
8998 name: "myapex",
8999 key: "myapex.key",
9000 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009001 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009002 }
9003 apex_key {
9004 name: "myapex.key",
9005 public_key: "testkey.avbpubkey",
9006 private_key: "testkey.pem",
9007 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00009008 `,
9009 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9010 variables.CompressedApex = proptools.BoolPtr(true)
9011 }),
9012 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009013
Jooyung Hana0503a52023-08-23 13:12:50 +09009014 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("compressRule")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009015 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
9016
Jooyung Hana0503a52023-08-23 13:12:50 +09009017 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex").Description("sign compressedApex")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009018 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
9019
9020 // Make sure output of bundle is .capex
Jooyung Hana0503a52023-08-23 13:12:50 +09009021 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009022 ensureContains(t, ab.outputFile.String(), "myapex.capex")
9023
9024 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07009025 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009026 var builder strings.Builder
9027 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9028 androidMk := builder.String()
9029 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
9030}
9031
Jooyung Han26ec8482024-07-31 15:04:05 +09009032func TestApexSet_ShouldRespectCompressedApexFlag(t *testing.T) {
9033 for _, compressionEnabled := range []bool{true, false} {
9034 t.Run(fmt.Sprintf("compressionEnabled=%v", compressionEnabled), func(t *testing.T) {
9035 ctx := testApex(t, `
9036 apex_set {
9037 name: "com.company.android.myapex",
9038 apex_name: "com.android.myapex",
9039 set: "company-myapex.apks",
9040 }
9041 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9042 variables.CompressedApex = proptools.BoolPtr(compressionEnabled)
9043 }),
9044 )
9045
9046 build := ctx.ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex").Output("com.company.android.myapex.apex")
9047 if compressionEnabled {
9048 ensureEquals(t, build.Rule.String(), "android/soong/android.Cp")
9049 } else {
9050 ensureEquals(t, build.Rule.String(), "android/apex.decompressApex")
9051 }
9052 })
9053 }
9054}
9055
Martin Stjernholm2856c662020-12-02 15:03:42 +00009056func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009057 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00009058 apex {
9059 name: "myapex",
9060 key: "myapex.key",
9061 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009062 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00009063 }
9064
9065 apex_key {
9066 name: "myapex.key",
9067 public_key: "testkey.avbpubkey",
9068 private_key: "testkey.pem",
9069 }
9070
9071 cc_library {
9072 name: "mylib",
9073 srcs: ["mylib.cpp"],
9074 apex_available: ["myapex"],
9075 shared_libs: ["otherlib"],
9076 system_shared_libs: [],
9077 }
9078
9079 cc_library {
9080 name: "otherlib",
9081 srcs: ["mylib.cpp"],
9082 stubs: {
9083 versions: ["current"],
9084 },
9085 }
9086
9087 cc_prebuilt_library_shared {
9088 name: "otherlib",
9089 prefer: true,
9090 srcs: ["prebuilt.so"],
9091 stubs: {
9092 versions: ["current"],
9093 },
9094 }
9095 `)
9096
Jooyung Hana0503a52023-08-23 13:12:50 +09009097 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07009098 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00009099 var builder strings.Builder
9100 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9101 androidMk := builder.String()
9102
9103 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
9104 // a thing there.
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009105 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 otherlib\n")
Martin Stjernholm2856c662020-12-02 15:03:42 +00009106}
9107
Jiyong Parke3867542020-12-03 17:28:25 +09009108func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009109 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09009110 apex {
9111 name: "myapex",
9112 key: "myapex.key",
9113 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009114 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09009115 }
9116
9117 apex_key {
9118 name: "myapex.key",
9119 public_key: "testkey.avbpubkey",
9120 private_key: "testkey.pem",
9121 }
9122
9123 cc_library {
9124 name: "mylib",
9125 srcs: ["mylib.cpp"],
9126 system_shared_libs: [],
9127 stl: "none",
9128 apex_available: ["myapex"],
9129 shared_libs: ["mylib2"],
9130 target: {
9131 apex: {
9132 exclude_shared_libs: ["mylib2"],
9133 },
9134 },
9135 }
9136
9137 cc_library {
9138 name: "mylib2",
9139 srcs: ["mylib.cpp"],
9140 system_shared_libs: [],
9141 stl: "none",
9142 }
9143 `)
9144
9145 // Check if mylib is linked to mylib2 for the non-apex target
9146 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
9147 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
9148
9149 // Make sure that the link doesn't occur for the apex target
9150 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
9151 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
9152
9153 // It shouldn't appear in the copy cmd as well.
Jooyung Hana0503a52023-08-23 13:12:50 +09009154 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule").Args["copy_commands"]
Jiyong Parke3867542020-12-03 17:28:25 +09009155 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
9156}
9157
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009158func TestPrebuiltStubLibDep(t *testing.T) {
9159 bpBase := `
9160 apex {
9161 name: "myapex",
9162 key: "myapex.key",
9163 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009164 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009165 }
9166 apex_key {
9167 name: "myapex.key",
9168 public_key: "testkey.avbpubkey",
9169 private_key: "testkey.pem",
9170 }
9171 cc_library {
9172 name: "mylib",
9173 srcs: ["mylib.cpp"],
9174 apex_available: ["myapex"],
9175 shared_libs: ["stublib"],
9176 system_shared_libs: [],
9177 }
9178 apex {
9179 name: "otherapex",
9180 enabled: %s,
9181 key: "myapex.key",
9182 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009183 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009184 }
9185 `
9186
9187 stublibSourceBp := `
9188 cc_library {
9189 name: "stublib",
9190 srcs: ["mylib.cpp"],
9191 apex_available: ["otherapex"],
9192 system_shared_libs: [],
9193 stl: "none",
9194 stubs: {
9195 versions: ["1"],
9196 },
9197 }
9198 `
9199
9200 stublibPrebuiltBp := `
9201 cc_prebuilt_library_shared {
9202 name: "stublib",
9203 srcs: ["prebuilt.so"],
9204 apex_available: ["otherapex"],
9205 stubs: {
9206 versions: ["1"],
9207 },
9208 %s
9209 }
9210 `
9211
9212 tests := []struct {
9213 name string
9214 stublibBp string
9215 usePrebuilt bool
9216 modNames []string // Modules to collect AndroidMkEntries for
9217 otherApexEnabled []string
9218 }{
9219 {
9220 name: "only_source",
9221 stublibBp: stublibSourceBp,
9222 usePrebuilt: false,
9223 modNames: []string{"stublib"},
9224 otherApexEnabled: []string{"true", "false"},
9225 },
9226 {
9227 name: "source_preferred",
9228 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9229 usePrebuilt: false,
9230 modNames: []string{"stublib", "prebuilt_stublib"},
9231 otherApexEnabled: []string{"true", "false"},
9232 },
9233 {
9234 name: "prebuilt_preferred",
9235 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9236 usePrebuilt: true,
9237 modNames: []string{"stublib", "prebuilt_stublib"},
9238 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9239 },
9240 {
9241 name: "only_prebuilt",
9242 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9243 usePrebuilt: true,
9244 modNames: []string{"stublib"},
9245 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9246 },
9247 }
9248
9249 for _, test := range tests {
9250 t.Run(test.name, func(t *testing.T) {
9251 for _, otherApexEnabled := range test.otherApexEnabled {
9252 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009253 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009254
9255 type modAndMkEntries struct {
9256 mod *cc.Module
9257 mkEntries android.AndroidMkEntries
9258 }
9259 entries := []*modAndMkEntries{}
9260
9261 // Gather shared lib modules that are installable
9262 for _, modName := range test.modNames {
9263 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9264 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9265 continue
9266 }
9267 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Cole Fausta963b942024-04-11 17:43:00 -07009268 if !mod.Enabled(android.PanickingConfigAndErrorContext(ctx)) || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009269 continue
9270 }
Colin Crossaa255532020-07-03 13:18:24 -07009271 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009272 if ent.Disabled {
9273 continue
9274 }
9275 entries = append(entries, &modAndMkEntries{
9276 mod: mod,
9277 mkEntries: ent,
9278 })
9279 }
9280 }
9281 }
9282
9283 var entry *modAndMkEntries = nil
9284 for _, ent := range entries {
9285 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9286 if entry != nil {
9287 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9288 } else {
9289 entry = ent
9290 }
9291 }
9292 }
9293
9294 if entry == nil {
9295 t.Errorf("AndroidMk entry for \"stublib\" missing")
9296 } else {
9297 isPrebuilt := entry.mod.Prebuilt() != nil
9298 if isPrebuilt != test.usePrebuilt {
9299 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9300 }
9301 if !entry.mod.IsStubs() {
9302 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9303 }
9304 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9305 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9306 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009307 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009308 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009309 if !android.InList(expected, cflags) {
9310 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9311 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009312 }
9313 })
9314 }
9315 })
9316 }
9317}
9318
Colin Crossc33e5212021-05-25 18:16:02 -07009319func TestApexJavaCoverage(t *testing.T) {
9320 bp := `
9321 apex {
9322 name: "myapex",
9323 key: "myapex.key",
9324 java_libs: ["mylib"],
9325 bootclasspath_fragments: ["mybootclasspathfragment"],
9326 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9327 updatable: false,
9328 }
9329
9330 apex_key {
9331 name: "myapex.key",
9332 public_key: "testkey.avbpubkey",
9333 private_key: "testkey.pem",
9334 }
9335
9336 java_library {
9337 name: "mylib",
9338 srcs: ["mylib.java"],
9339 apex_available: ["myapex"],
9340 compile_dex: true,
9341 }
9342
9343 bootclasspath_fragment {
9344 name: "mybootclasspathfragment",
9345 contents: ["mybootclasspathlib"],
9346 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009347 hidden_api: {
9348 split_packages: ["*"],
9349 },
Colin Crossc33e5212021-05-25 18:16:02 -07009350 }
9351
9352 java_library {
9353 name: "mybootclasspathlib",
9354 srcs: ["mybootclasspathlib.java"],
9355 apex_available: ["myapex"],
9356 compile_dex: true,
9357 }
9358
9359 systemserverclasspath_fragment {
9360 name: "mysystemserverclasspathfragment",
9361 contents: ["mysystemserverclasspathlib"],
9362 apex_available: ["myapex"],
9363 }
9364
9365 java_library {
9366 name: "mysystemserverclasspathlib",
9367 srcs: ["mysystemserverclasspathlib.java"],
9368 apex_available: ["myapex"],
9369 compile_dex: true,
9370 }
9371 `
9372
9373 result := android.GroupFixturePreparers(
9374 PrepareForTestWithApexBuildComponents,
9375 prepareForTestWithMyapex,
9376 java.PrepareForTestWithJavaDefaultModules,
9377 android.PrepareForTestWithAndroidBuildComponents,
9378 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009379 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9380 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009381 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009382 ).RunTest(t)
9383
9384 // Make sure jacoco ran on both mylib and mybootclasspathlib
9385 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9386 t.Errorf("Failed to find jacoco rule for mylib")
9387 }
9388 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9389 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9390 }
9391 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9392 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9393 }
9394}
9395
Jiyong Park192600a2021-08-03 07:52:17 +00009396func TestProhibitStaticExecutable(t *testing.T) {
9397 testApexError(t, `executable mybin is static`, `
9398 apex {
9399 name: "myapex",
9400 key: "myapex.key",
9401 binaries: ["mybin"],
9402 min_sdk_version: "29",
9403 }
9404
9405 apex_key {
9406 name: "myapex.key",
9407 public_key: "testkey.avbpubkey",
9408 private_key: "testkey.pem",
9409 }
9410
9411 cc_binary {
9412 name: "mybin",
9413 srcs: ["mylib.cpp"],
9414 relative_install_path: "foo/bar",
9415 static_executable: true,
9416 system_shared_libs: [],
9417 stl: "none",
9418 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009419 min_sdk_version: "29",
9420 }
9421 `)
9422
9423 testApexError(t, `executable mybin.rust is static`, `
9424 apex {
9425 name: "myapex",
9426 key: "myapex.key",
9427 binaries: ["mybin.rust"],
9428 min_sdk_version: "29",
9429 }
9430
9431 apex_key {
9432 name: "myapex.key",
9433 public_key: "testkey.avbpubkey",
9434 private_key: "testkey.pem",
9435 }
9436
9437 rust_binary {
9438 name: "mybin.rust",
9439 srcs: ["foo.rs"],
9440 static_executable: true,
9441 apex_available: ["myapex"],
9442 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009443 }
9444 `)
9445}
9446
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009447func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9448 ctx := testApex(t, `
9449 apex {
9450 name: "myapex",
9451 key: "myapex.key",
9452 updatable: false,
9453 java_libs: ["foo"],
9454 }
9455
9456 apex_key {
9457 name: "myapex.key",
9458 public_key: "testkey.avbpubkey",
9459 private_key: "testkey.pem",
9460 }
9461
9462 java_library {
9463 name: "foo",
9464 srcs: ["foo.java"],
9465 apex_available: ["myapex"],
9466 installable: true,
9467 }
9468 `,
9469 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9470 )
9471
Jooyung Hana0503a52023-08-23 13:12:50 +09009472 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009473 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9474 var builder strings.Builder
9475 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9476 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009477 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 +00009478}
9479
9480func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9481 ctx := testApex(t, `
9482 prebuilt_apex {
9483 name: "myapex",
9484 arch: {
9485 arm64: {
9486 src: "myapex-arm64.apex",
9487 },
9488 arm: {
9489 src: "myapex-arm.apex",
9490 },
9491 },
9492 exported_java_libs: ["foo"],
9493 }
9494
9495 java_import {
9496 name: "foo",
9497 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009498 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009499 }
9500 `,
9501 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9502 )
9503
9504 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9505 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9506 mainModuleEntries := entriesList[0]
9507 android.AssertArrayString(t,
9508 "LOCAL_REQUIRED_MODULES",
9509 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9510 []string{
9511 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9512 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9513 })
9514}
9515
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009516func TestAndroidMk_RequiredModules(t *testing.T) {
9517 ctx := testApex(t, `
9518 apex {
9519 name: "myapex",
9520 key: "myapex.key",
9521 updatable: false,
9522 java_libs: ["foo"],
9523 required: ["otherapex"],
9524 }
9525
9526 apex {
9527 name: "otherapex",
9528 key: "myapex.key",
9529 updatable: false,
9530 java_libs: ["foo"],
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009531 }
9532
9533 apex_key {
9534 name: "myapex.key",
9535 public_key: "testkey.avbpubkey",
9536 private_key: "testkey.pem",
9537 }
9538
9539 java_library {
9540 name: "foo",
9541 srcs: ["foo.java"],
9542 apex_available: ["myapex", "otherapex"],
9543 installable: true,
9544 }
9545 `)
9546
Jooyung Hana0503a52023-08-23 13:12:50 +09009547 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009548 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9549 var builder strings.Builder
9550 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9551 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009552 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009553}
9554
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009555func TestAndroidMk_RequiredDeps(t *testing.T) {
9556 ctx := testApex(t, `
9557 apex {
9558 name: "myapex",
9559 key: "myapex.key",
9560 updatable: false,
9561 }
9562
9563 apex_key {
9564 name: "myapex.key",
9565 public_key: "testkey.avbpubkey",
9566 private_key: "testkey.pem",
9567 }
9568 `)
9569
Jooyung Hana0503a52023-08-23 13:12:50 +09009570 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009571 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009572 data := android.AndroidMkDataForTest(t, ctx, bundle)
9573 var builder strings.Builder
9574 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9575 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009576 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009577}
9578
Jooyung Hana6d36672022-02-24 13:58:07 +09009579func TestApexOutputFileProducer(t *testing.T) {
9580 for _, tc := range []struct {
9581 name string
9582 ref string
9583 expected_data []string
9584 }{
9585 {
9586 name: "test_using_output",
9587 ref: ":myapex",
Jooyung Hana0503a52023-08-23 13:12:50 +09009588 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.capex:myapex.capex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009589 },
9590 {
9591 name: "test_using_apex",
9592 ref: ":myapex{.apex}",
Jooyung Hana0503a52023-08-23 13:12:50 +09009593 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.apex:myapex.apex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009594 },
9595 } {
9596 t.Run(tc.name, func(t *testing.T) {
9597 ctx := testApex(t, `
9598 apex {
9599 name: "myapex",
9600 key: "myapex.key",
9601 compressible: true,
9602 updatable: false,
9603 }
9604
9605 apex_key {
9606 name: "myapex.key",
9607 public_key: "testkey.avbpubkey",
9608 private_key: "testkey.pem",
9609 }
9610
9611 java_test {
9612 name: "`+tc.name+`",
9613 srcs: ["a.java"],
9614 data: ["`+tc.ref+`"],
9615 }
9616 `,
9617 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9618 variables.CompressedApex = proptools.BoolPtr(true)
9619 }))
9620 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9621 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9622 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9623 })
9624 }
9625}
9626
satayev758968a2021-12-06 11:42:40 +00009627func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9628 preparer := android.GroupFixturePreparers(
9629 PrepareForTestWithApexBuildComponents,
9630 prepareForTestWithMyapex,
9631 java.PrepareForTestWithJavaSdkLibraryFiles,
9632 java.PrepareForTestWithJavaDefaultModules,
9633 android.PrepareForTestWithAndroidBuildComponents,
9634 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9635 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9636 )
9637
9638 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9639 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9640 preparer.RunTestWithBp(t, `
9641 apex {
9642 name: "myapex",
9643 key: "myapex.key",
9644 bootclasspath_fragments: ["mybootclasspathfragment"],
9645 min_sdk_version: "30",
9646 updatable: false,
9647 }
9648
9649 apex_key {
9650 name: "myapex.key",
9651 public_key: "testkey.avbpubkey",
9652 private_key: "testkey.pem",
9653 }
9654
9655 bootclasspath_fragment {
9656 name: "mybootclasspathfragment",
9657 contents: ["mybootclasspathlib"],
9658 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009659 hidden_api: {
9660 split_packages: ["*"],
9661 },
satayev758968a2021-12-06 11:42:40 +00009662 }
9663
9664 java_sdk_library {
9665 name: "mybootclasspathlib",
9666 srcs: ["mybootclasspathlib.java"],
9667 apex_available: ["myapex"],
9668 compile_dex: true,
9669 unsafe_ignore_missing_latest_api: true,
9670 min_sdk_version: "31",
9671 static_libs: ["util"],
9672 }
9673
9674 java_library {
9675 name: "util",
9676 srcs: ["a.java"],
9677 apex_available: ["myapex"],
9678 min_sdk_version: "31",
9679 static_libs: ["another_util"],
9680 }
9681
9682 java_library {
9683 name: "another_util",
9684 srcs: ["a.java"],
9685 min_sdk_version: "31",
9686 apex_available: ["myapex"],
9687 }
9688 `)
9689 })
9690
9691 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9692 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9693 preparer.RunTestWithBp(t, `
9694 apex {
9695 name: "myapex",
9696 key: "myapex.key",
9697 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9698 min_sdk_version: "30",
9699 updatable: false,
9700 }
9701
9702 apex_key {
9703 name: "myapex.key",
9704 public_key: "testkey.avbpubkey",
9705 private_key: "testkey.pem",
9706 }
9707
9708 systemserverclasspath_fragment {
9709 name: "mysystemserverclasspathfragment",
9710 contents: ["mysystemserverclasspathlib"],
9711 apex_available: ["myapex"],
9712 }
9713
9714 java_sdk_library {
9715 name: "mysystemserverclasspathlib",
9716 srcs: ["mysystemserverclasspathlib.java"],
9717 apex_available: ["myapex"],
9718 compile_dex: true,
9719 min_sdk_version: "32",
9720 unsafe_ignore_missing_latest_api: true,
9721 static_libs: ["util"],
9722 }
9723
9724 java_library {
9725 name: "util",
9726 srcs: ["a.java"],
9727 apex_available: ["myapex"],
9728 min_sdk_version: "31",
9729 static_libs: ["another_util"],
9730 }
9731
9732 java_library {
9733 name: "another_util",
9734 srcs: ["a.java"],
9735 min_sdk_version: "31",
9736 apex_available: ["myapex"],
9737 }
9738 `)
9739 })
9740
9741 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9742 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9743 RunTestWithBp(t, `
9744 apex {
9745 name: "myapex",
9746 key: "myapex.key",
9747 bootclasspath_fragments: ["mybootclasspathfragment"],
9748 min_sdk_version: "30",
9749 updatable: false,
9750 }
9751
9752 apex_key {
9753 name: "myapex.key",
9754 public_key: "testkey.avbpubkey",
9755 private_key: "testkey.pem",
9756 }
9757
9758 bootclasspath_fragment {
9759 name: "mybootclasspathfragment",
9760 contents: ["mybootclasspathlib"],
9761 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009762 hidden_api: {
9763 split_packages: ["*"],
9764 },
satayev758968a2021-12-06 11:42:40 +00009765 }
9766
9767 java_sdk_library {
9768 name: "mybootclasspathlib",
9769 srcs: ["mybootclasspathlib.java"],
9770 apex_available: ["myapex"],
9771 compile_dex: true,
9772 unsafe_ignore_missing_latest_api: true,
9773 }
9774 `)
9775 })
9776
9777 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9778 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9779 RunTestWithBp(t, `
9780 apex {
9781 name: "myapex",
9782 key: "myapex.key",
9783 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9784 min_sdk_version: "30",
9785 updatable: false,
9786 }
9787
9788 apex_key {
9789 name: "myapex.key",
9790 public_key: "testkey.avbpubkey",
9791 private_key: "testkey.pem",
9792 }
9793
9794 systemserverclasspath_fragment {
9795 name: "mysystemserverclasspathfragment",
9796 contents: ["mysystemserverclasspathlib"],
9797 apex_available: ["myapex"],
9798 }
9799
9800 java_sdk_library {
9801 name: "mysystemserverclasspathlib",
9802 srcs: ["mysystemserverclasspathlib.java"],
9803 apex_available: ["myapex"],
9804 compile_dex: true,
9805 unsafe_ignore_missing_latest_api: true,
9806 }
9807 `)
9808 })
9809}
9810
Jiakai Zhang6decef92022-01-12 17:56:19 +00009811// Verifies that the APEX depends on all the Make modules in the list.
9812func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9813 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9814 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009815 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009816 }
9817}
9818
9819// Verifies that the APEX does not depend on any of the Make modules in the list.
9820func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9821 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9822 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009823 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009824 }
9825}
9826
Cole Faust24e25c02024-01-19 14:12:17 -08009827func TestApexStrictUpdtabilityLint(t *testing.T) {
9828 bpTemplate := `
9829 apex {
9830 name: "myapex",
9831 key: "myapex.key",
9832 java_libs: ["myjavalib"],
9833 updatable: %v,
9834 min_sdk_version: "29",
9835 }
9836 apex_key {
9837 name: "myapex.key",
9838 }
9839 java_library {
9840 name: "myjavalib",
9841 srcs: ["MyClass.java"],
9842 apex_available: [ "myapex" ],
9843 lint: {
9844 strict_updatability_linting: %v,
9845 %s
9846 },
9847 sdk_version: "current",
9848 min_sdk_version: "29",
9849 }
9850 `
9851 fs := android.MockFS{
9852 "lint-baseline.xml": nil,
9853 }
9854
9855 testCases := []struct {
9856 testCaseName string
9857 apexUpdatable bool
9858 javaStrictUpdtabilityLint bool
9859 lintFileExists bool
9860 disallowedFlagExpected bool
9861 }{
9862 {
9863 testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9864 apexUpdatable: true,
9865 javaStrictUpdtabilityLint: true,
9866 lintFileExists: false,
9867 disallowedFlagExpected: false,
9868 },
9869 {
9870 testCaseName: "non-updatable apex respects strict_updatability of javalib",
9871 apexUpdatable: false,
9872 javaStrictUpdtabilityLint: false,
9873 lintFileExists: true,
9874 disallowedFlagExpected: false,
9875 },
9876 {
9877 testCaseName: "non-updatable apex respects strict updatability of javalib",
9878 apexUpdatable: false,
9879 javaStrictUpdtabilityLint: true,
9880 lintFileExists: true,
9881 disallowedFlagExpected: true,
9882 },
9883 {
9884 testCaseName: "updatable apex sets strict updatability of javalib to true",
9885 apexUpdatable: true,
9886 javaStrictUpdtabilityLint: false, // will be set to true by mutator
9887 lintFileExists: true,
9888 disallowedFlagExpected: true,
9889 },
9890 }
9891
9892 for _, testCase := range testCases {
9893 fixtures := []android.FixturePreparer{}
9894 baselineProperty := ""
9895 if testCase.lintFileExists {
9896 fixtures = append(fixtures, fs.AddToFixture())
9897 baselineProperty = "baseline_filename: \"lint-baseline.xml\""
9898 }
9899 bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint, baselineProperty)
9900
9901 result := testApex(t, bp, fixtures...)
9902 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9903 sboxProto := android.RuleBuilderSboxProtoForTests(t, result, myjavalib.Output("lint.sbox.textproto"))
9904 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9905
9906 if disallowedFlagActual != testCase.disallowedFlagExpected {
9907 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9908 }
9909 }
9910}
9911
9912func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9913 bp := `
9914 apex {
9915 name: "myapex",
9916 key: "myapex.key",
9917 java_libs: ["myjavalib"],
9918 updatable: true,
9919 min_sdk_version: "29",
9920 }
9921 apex_key {
9922 name: "myapex.key",
9923 }
9924 java_library {
9925 name: "myjavalib",
9926 srcs: ["MyClass.java"],
9927 apex_available: [ "myapex" ],
9928 sdk_version: "current",
9929 min_sdk_version: "29",
9930 lint: {
9931 baseline_filename: "lint-baseline.xml",
9932 }
9933 }
9934 `
9935
9936 testCases := []struct {
9937 testCaseName string
9938 moduleDirectory string
9939 disallowedFlagExpected bool
9940 }{
9941 {
9942 testCaseName: "lintable module defined outside libcore",
9943 moduleDirectory: "",
9944 disallowedFlagExpected: true,
9945 },
9946 {
9947 testCaseName: "lintable module defined in libcore root directory",
9948 moduleDirectory: "libcore/",
9949 disallowedFlagExpected: false,
9950 },
9951 {
9952 testCaseName: "lintable module defined in libcore child directory",
9953 moduleDirectory: "libcore/childdir/",
9954 disallowedFlagExpected: true,
9955 },
9956 }
9957
9958 for _, testCase := range testCases {
9959 lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9960 bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9961 result := testApex(t, "", lintFileCreator, bpFileCreator)
9962 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9963 sboxProto := android.RuleBuilderSboxProtoForTests(t, result, myjavalib.Output("lint.sbox.textproto"))
9964 cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9965 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9966
9967 if disallowedFlagActual != testCase.disallowedFlagExpected {
9968 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9969 }
9970 }
9971}
9972
9973// checks transtive deps of an apex coming from bootclasspath_fragment
9974func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9975 bp := `
9976 apex {
9977 name: "myapex",
9978 key: "myapex.key",
9979 bootclasspath_fragments: ["mybootclasspathfragment"],
9980 updatable: true,
9981 min_sdk_version: "29",
9982 }
9983 apex_key {
9984 name: "myapex.key",
9985 }
9986 bootclasspath_fragment {
9987 name: "mybootclasspathfragment",
9988 contents: ["myjavalib"],
9989 apex_available: ["myapex"],
9990 hidden_api: {
9991 split_packages: ["*"],
9992 },
9993 }
9994 java_library {
9995 name: "myjavalib",
9996 srcs: ["MyClass.java"],
9997 apex_available: [ "myapex" ],
9998 sdk_version: "current",
9999 min_sdk_version: "29",
10000 compile_dex: true,
10001 lint: {
10002 baseline_filename: "lint-baseline.xml",
10003 }
10004 }
10005 `
10006 fs := android.MockFS{
10007 "lint-baseline.xml": nil,
10008 }
10009
10010 result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
10011 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
10012 sboxProto := android.RuleBuilderSboxProtoForTests(t, result, myjavalib.Output("lint.sbox.textproto"))
10013 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
10014 t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
10015 }
10016}
Spandan Das66773252022-01-15 00:23:18 +000010017
Jihoon Kang784c0052024-06-25 22:15:39 +000010018func TestApexLintBcpFragmentSdkLibDeps(t *testing.T) {
10019 bp := `
10020 apex {
10021 name: "myapex",
10022 key: "myapex.key",
10023 bootclasspath_fragments: ["mybootclasspathfragment"],
10024 min_sdk_version: "29",
10025 }
10026 apex_key {
10027 name: "myapex.key",
10028 }
10029 bootclasspath_fragment {
10030 name: "mybootclasspathfragment",
10031 contents: ["foo"],
10032 apex_available: ["myapex"],
10033 hidden_api: {
10034 split_packages: ["*"],
10035 },
10036 }
10037 java_sdk_library {
10038 name: "foo",
10039 srcs: ["MyClass.java"],
10040 apex_available: [ "myapex" ],
10041 sdk_version: "current",
10042 min_sdk_version: "29",
10043 compile_dex: true,
10044 }
10045 `
10046 fs := android.MockFS{
10047 "lint-baseline.xml": nil,
10048 }
10049
10050 result := android.GroupFixturePreparers(
10051 prepareForApexTest,
10052 java.PrepareForTestWithJavaSdkLibraryFiles,
10053 java.PrepareForTestWithJacocoInstrumentation,
10054 java.FixtureWithLastReleaseApis("foo"),
Jihoon Kang784c0052024-06-25 22:15:39 +000010055 android.FixtureMergeMockFs(fs),
10056 ).RunTestWithBp(t, bp)
10057
10058 myapex := result.ModuleForTests("myapex", "android_common_myapex")
10059 lintReportInputs := strings.Join(myapex.Output("lint-report-xml.zip").Inputs.Strings(), " ")
10060 android.AssertStringDoesContain(t,
10061 "myapex lint report expected to contain that of the sdk library impl lib as an input",
10062 lintReportInputs, "foo.impl")
10063}
10064
Spandan Das42e89502022-05-06 22:12:55 +000010065// updatable apexes should propagate updatable=true to its apps
10066func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
10067 bp := `
10068 apex {
10069 name: "myapex",
10070 key: "myapex.key",
10071 updatable: %v,
10072 apps: [
10073 "myapp",
10074 ],
10075 min_sdk_version: "30",
10076 }
10077 apex_key {
10078 name: "myapex.key",
10079 }
10080 android_app {
10081 name: "myapp",
10082 updatable: %v,
10083 apex_available: [
10084 "myapex",
10085 ],
10086 sdk_version: "current",
10087 min_sdk_version: "30",
10088 }
10089 `
10090 testCases := []struct {
10091 name string
10092 apex_is_updatable_bp bool
10093 app_is_updatable_bp bool
10094 app_is_updatable_expected bool
10095 }{
10096 {
10097 name: "Non-updatable apex respects updatable property of non-updatable app",
10098 apex_is_updatable_bp: false,
10099 app_is_updatable_bp: false,
10100 app_is_updatable_expected: false,
10101 },
10102 {
10103 name: "Non-updatable apex respects updatable property of updatable app",
10104 apex_is_updatable_bp: false,
10105 app_is_updatable_bp: true,
10106 app_is_updatable_expected: true,
10107 },
10108 {
10109 name: "Updatable apex respects updatable property of updatable app",
10110 apex_is_updatable_bp: true,
10111 app_is_updatable_bp: true,
10112 app_is_updatable_expected: true,
10113 },
10114 {
10115 name: "Updatable apex sets updatable=true on non-updatable app",
10116 apex_is_updatable_bp: true,
10117 app_is_updatable_bp: false,
10118 app_is_updatable_expected: true,
10119 },
10120 }
10121 for _, testCase := range testCases {
10122 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
10123 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
10124 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
10125 }
10126}
10127
Kiyoung Kim487689e2022-07-26 09:48:22 +090010128func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10129 bp := `
10130 apex {
10131 name: "myapex",
10132 key: "myapex.key",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010133 native_shared_libs: ["libbaz"],
10134 binaries: ["binfoo"],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010135 min_sdk_version: "29",
10136 }
10137 apex_key {
10138 name: "myapex.key",
10139 }
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010140 cc_binary {
10141 name: "binfoo",
10142 shared_libs: ["libbar", "libbaz", "libqux",],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010143 apex_available: ["myapex"],
10144 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010145 recovery_available: false,
10146 }
10147 cc_library {
10148 name: "libbar",
10149 srcs: ["libbar.cc"],
10150 stubs: {
10151 symbol_file: "libbar.map.txt",
10152 versions: [
10153 "29",
10154 ],
10155 },
10156 }
10157 cc_library {
10158 name: "libbaz",
10159 srcs: ["libbaz.cc"],
10160 apex_available: ["myapex"],
10161 min_sdk_version: "29",
10162 stubs: {
10163 symbol_file: "libbaz.map.txt",
10164 versions: [
10165 "29",
10166 ],
10167 },
Kiyoung Kim487689e2022-07-26 09:48:22 +090010168 }
10169 cc_api_library {
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010170 name: "libbar",
10171 src: "libbar_stub.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010172 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010173 variants: ["apex.29"],
10174 }
10175 cc_api_variant {
10176 name: "libbar",
10177 variant: "apex",
10178 version: "29",
10179 src: "libbar_apex_29.so",
10180 }
10181 cc_api_library {
10182 name: "libbaz",
10183 src: "libbaz_stub.so",
10184 min_sdk_version: "29",
10185 variants: ["apex.29"],
10186 }
10187 cc_api_variant {
10188 name: "libbaz",
10189 variant: "apex",
10190 version: "29",
10191 src: "libbaz_apex_29.so",
10192 }
10193 cc_api_library {
10194 name: "libqux",
10195 src: "libqux_stub.so",
10196 min_sdk_version: "29",
10197 variants: ["apex.29"],
10198 }
10199 cc_api_variant {
10200 name: "libqux",
10201 variant: "apex",
10202 version: "29",
10203 src: "libqux_apex_29.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010204 }
10205 api_imports {
10206 name: "api_imports",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010207 apex_shared_libs: [
10208 "libbar",
10209 "libbaz",
10210 "libqux",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010211 ],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010212 }
10213 `
10214 result := testApex(t, bp)
10215
10216 hasDep := func(m android.Module, wantDep android.Module) bool {
10217 t.Helper()
10218 var found bool
10219 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10220 if dep == wantDep {
10221 found = true
10222 }
10223 })
10224 return found
10225 }
10226
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010227 // Library defines stubs and cc_api_library should be used with cc_api_library
10228 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Module()
10229 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10230 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
Kiyoung Kim487689e2022-07-26 09:48:22 +090010231
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010232 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10233 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
Kiyoung Kim487689e2022-07-26 09:48:22 +090010234
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010235 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Rule("ld").Args["libFlags"]
10236 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10237 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10238 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
10239
10240 // Library defined in the same APEX should be linked with original definition instead of cc_api_library
10241 libbazApexVariant := result.ModuleForTests("libbaz", "android_arm64_armv8-a_shared_apex29").Module()
10242 libbazApiImportCoreVariant := result.ModuleForTests("libbaz.apiimport", "android_arm64_armv8-a_shared").Module()
10243 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even from same APEX", true, hasDep(binfooApexVariant, libbazApiImportCoreVariant))
10244 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbazApexVariant))
10245
10246 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbaz.so")
10247 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbaz.apiimport.so")
10248 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbaz.apex.29.apiimport.so")
10249
10250 // cc_api_library defined without original library should be linked with cc_api_library
10251 libquxApiImportApexVariant := result.ModuleForTests("libqux.apiimport", "android_arm64_armv8-a_shared").Module()
10252 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even original library definition does not exist", true, hasDep(binfooApexVariant, libquxApiImportApexVariant))
10253 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libqux.apex.29.apiimport.so")
10254}
10255
10256func TestPlatformBinaryBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10257 bp := `
10258 apex {
10259 name: "myapex",
10260 key: "myapex.key",
10261 native_shared_libs: ["libbar"],
10262 min_sdk_version: "29",
10263 }
10264 apex_key {
10265 name: "myapex.key",
10266 }
10267 cc_binary {
10268 name: "binfoo",
10269 shared_libs: ["libbar"],
10270 recovery_available: false,
10271 }
10272 cc_library {
10273 name: "libbar",
10274 srcs: ["libbar.cc"],
10275 apex_available: ["myapex"],
10276 min_sdk_version: "29",
10277 stubs: {
10278 symbol_file: "libbar.map.txt",
10279 versions: [
10280 "29",
10281 ],
10282 },
10283 }
10284 cc_api_library {
10285 name: "libbar",
10286 src: "libbar_stub.so",
10287 variants: ["apex.29"],
10288 }
10289 cc_api_variant {
10290 name: "libbar",
10291 variant: "apex",
10292 version: "29",
10293 src: "libbar_apex_29.so",
10294 }
10295 api_imports {
10296 name: "api_imports",
10297 apex_shared_libs: [
10298 "libbar",
10299 ],
10300 }
10301 `
10302
10303 result := testApex(t, bp)
10304
10305 hasDep := func(m android.Module, wantDep android.Module) bool {
10306 t.Helper()
10307 var found bool
10308 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10309 if dep == wantDep {
10310 found = true
10311 }
10312 })
10313 return found
10314 }
10315
10316 // Library defines stubs and cc_api_library should be used with cc_api_library
10317 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Module()
10318 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10319 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
10320
10321 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10322 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
10323
10324 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
10325 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10326 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10327 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
Kiyoung Kim487689e2022-07-26 09:48:22 +090010328}
Dennis Shend4f5d932023-01-31 20:27:21 +000010329
10330func TestTrimmedApex(t *testing.T) {
10331 bp := `
10332 apex {
10333 name: "myapex",
10334 key: "myapex.key",
10335 native_shared_libs: ["libfoo","libbaz"],
10336 min_sdk_version: "29",
10337 trim_against: "mydcla",
10338 }
10339 apex {
10340 name: "mydcla",
10341 key: "myapex.key",
10342 native_shared_libs: ["libfoo","libbar"],
10343 min_sdk_version: "29",
10344 file_contexts: ":myapex-file_contexts",
10345 dynamic_common_lib_apex: true,
10346 }
10347 apex_key {
10348 name: "myapex.key",
10349 }
10350 cc_library {
10351 name: "libfoo",
10352 shared_libs: ["libc"],
10353 apex_available: ["myapex","mydcla"],
10354 min_sdk_version: "29",
10355 }
10356 cc_library {
10357 name: "libbar",
10358 shared_libs: ["libc"],
10359 apex_available: ["myapex","mydcla"],
10360 min_sdk_version: "29",
10361 }
10362 cc_library {
10363 name: "libbaz",
10364 shared_libs: ["libc"],
10365 apex_available: ["myapex","mydcla"],
10366 min_sdk_version: "29",
10367 }
10368 cc_api_library {
10369 name: "libc",
10370 src: "libc.so",
10371 min_sdk_version: "29",
10372 recovery_available: true,
Ivan Lozanoadd122a2023-07-13 11:01:41 -040010373 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +090010374 product_available: true,
Dennis Shend4f5d932023-01-31 20:27:21 +000010375 }
10376 api_imports {
10377 name: "api_imports",
10378 shared_libs: [
10379 "libc",
10380 ],
10381 header_libs: [],
10382 }
10383 `
10384 ctx := testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +090010385 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dennis Shend4f5d932023-01-31 20:27:21 +000010386 apexRule := module.MaybeRule("apexRule")
10387 if apexRule.Rule == nil {
10388 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10389 }
10390
10391 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
Jooyung Hana0503a52023-08-23 13:12:50 +090010392 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("TrimmedApexRule")
Dennis Shend4f5d932023-01-31 20:27:21 +000010393 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10394 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10395 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10396 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10397}
Jingwen Chendea7a642023-03-28 11:30:50 +000010398
10399func TestCannedFsConfig(t *testing.T) {
10400 ctx := testApex(t, `
10401 apex {
10402 name: "myapex",
10403 key: "myapex.key",
10404 updatable: false,
10405 }
10406
10407 apex_key {
10408 name: "myapex.key",
10409 public_key: "testkey.avbpubkey",
10410 private_key: "testkey.pem",
10411 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010412 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010413 generateFsRule := mod.Rule("generateFsConfig")
10414 cmd := generateFsRule.RuleParams.Command
10415
10416 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10417}
10418
10419func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10420 ctx := testApex(t, `
10421 apex {
10422 name: "myapex",
10423 key: "myapex.key",
10424 canned_fs_config: "my_config",
10425 updatable: false,
10426 }
10427
10428 apex_key {
10429 name: "myapex.key",
10430 public_key: "testkey.avbpubkey",
10431 private_key: "testkey.pem",
10432 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010433 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010434 generateFsRule := mod.Rule("generateFsConfig")
10435 cmd := generateFsRule.RuleParams.Command
10436
10437 // Ensure that canned_fs_config has "cat my_config" at the end
10438 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10439}
Spandan Das20fce2d2023-04-12 17:21:39 +000010440
10441func TestStubLibrariesMultipleApexViolation(t *testing.T) {
10442 testCases := []struct {
10443 desc string
10444 hasStubs bool
10445 apexAvailable string
10446 expectedError string
10447 }{
10448 {
10449 desc: "non-stub library can have multiple apex_available",
10450 hasStubs: false,
10451 apexAvailable: `["myapex", "otherapex"]`,
10452 },
10453 {
10454 desc: "stub library should not be available to anyapex",
10455 hasStubs: true,
10456 apexAvailable: `["//apex_available:anyapex"]`,
10457 expectedError: "Stub libraries should have a single apex_available.*anyapex",
10458 },
10459 {
10460 desc: "stub library should not be available to multiple apexes",
10461 hasStubs: true,
10462 apexAvailable: `["myapex", "otherapex"]`,
10463 expectedError: "Stub libraries should have a single apex_available.*myapex.*otherapex",
10464 },
10465 {
10466 desc: "stub library can be available to a core apex and a test apex",
10467 hasStubs: true,
10468 apexAvailable: `["myapex", "test_myapex"]`,
10469 },
10470 }
10471 bpTemplate := `
10472 cc_library {
10473 name: "libfoo",
10474 %v
10475 apex_available: %v,
10476 }
10477 apex {
10478 name: "myapex",
10479 key: "apex.key",
10480 updatable: false,
10481 native_shared_libs: ["libfoo"],
10482 }
10483 apex {
10484 name: "otherapex",
10485 key: "apex.key",
10486 updatable: false,
10487 }
10488 apex_test {
10489 name: "test_myapex",
10490 key: "apex.key",
10491 updatable: false,
10492 native_shared_libs: ["libfoo"],
10493 }
10494 apex_key {
10495 name: "apex.key",
10496 }
10497 `
10498 for _, tc := range testCases {
10499 stubs := ""
10500 if tc.hasStubs {
10501 stubs = `stubs: {symbol_file: "libfoo.map.txt"},`
10502 }
10503 bp := fmt.Sprintf(bpTemplate, stubs, tc.apexAvailable)
10504 mockFsFixturePreparer := android.FixtureModifyMockFS(func(fs android.MockFS) {
10505 fs["system/sepolicy/apex/test_myapex-file_contexts"] = nil
10506 })
10507 if tc.expectedError == "" {
10508 testApex(t, bp, mockFsFixturePreparer)
10509 } else {
10510 testApexError(t, tc.expectedError, bp, mockFsFixturePreparer)
10511 }
10512 }
10513}
Colin Crossbd3a16b2023-04-25 11:30:51 -070010514
10515func TestFileSystemShouldSkipApexLibraries(t *testing.T) {
10516 context := android.GroupFixturePreparers(
10517 android.PrepareForIntegrationTestWithAndroid,
10518 cc.PrepareForIntegrationTestWithCc,
10519 PrepareForTestWithApexBuildComponents,
10520 prepareForTestWithMyapex,
10521 filesystem.PrepareForTestWithFilesystemBuildComponents,
10522 )
10523 result := context.RunTestWithBp(t, `
10524 android_system_image {
10525 name: "myfilesystem",
10526 deps: [
10527 "libfoo",
10528 ],
10529 linker_config_src: "linker.config.json",
10530 }
10531
10532 cc_library {
10533 name: "libfoo",
10534 shared_libs: [
10535 "libbar",
10536 ],
10537 stl: "none",
10538 }
10539
10540 cc_library {
10541 name: "libbar",
10542 stl: "none",
10543 apex_available: ["myapex"],
10544 }
10545
10546 apex {
10547 name: "myapex",
10548 native_shared_libs: ["libbar"],
10549 key: "myapex.key",
10550 updatable: false,
10551 }
10552
10553 apex_key {
10554 name: "myapex.key",
10555 public_key: "testkey.avbpubkey",
10556 private_key: "testkey.pem",
10557 }
10558 `)
10559
Cole Faust3b806d32024-03-11 15:15:03 -070010560 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits
Colin Crossbd3a16b2023-04-25 11:30:51 -070010561 android.AssertStringListDoesNotContain(t, "filesystem should not have libbar",
10562 inputs.Strings(),
10563 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
10564}
Yu Liueae7b362023-11-16 17:05:47 -080010565
10566var apex_default_bp = `
10567 apex_key {
10568 name: "myapex.key",
10569 public_key: "testkey.avbpubkey",
10570 private_key: "testkey.pem",
10571 }
10572
10573 filegroup {
10574 name: "myapex.manifest",
10575 srcs: ["apex_manifest.json"],
10576 }
10577
10578 filegroup {
10579 name: "myapex.androidmanifest",
10580 srcs: ["AndroidManifest.xml"],
10581 }
10582`
10583
10584func TestAconfigFilesJavaDeps(t *testing.T) {
10585 ctx := testApex(t, apex_default_bp+`
10586 apex {
10587 name: "myapex",
10588 manifest: ":myapex.manifest",
10589 androidManifest: ":myapex.androidmanifest",
10590 key: "myapex.key",
10591 java_libs: [
10592 "my_java_library_foo",
10593 "my_java_library_bar",
10594 ],
10595 updatable: false,
10596 }
10597
10598 java_library {
10599 name: "my_java_library_foo",
10600 srcs: ["foo/bar/MyClass.java"],
10601 sdk_version: "none",
10602 system_modules: "none",
10603 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010604 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010605 "myapex",
10606 ],
10607 }
10608
10609 java_library {
10610 name: "my_java_library_bar",
10611 srcs: ["foo/bar/MyClass.java"],
10612 sdk_version: "none",
10613 system_modules: "none",
10614 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010615 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010616 "myapex",
10617 ],
10618 }
10619
10620 aconfig_declarations {
10621 name: "my_aconfig_declarations_foo",
10622 package: "com.example.package",
10623 container: "myapex",
10624 srcs: ["foo.aconfig"],
10625 }
10626
10627 java_aconfig_library {
10628 name: "my_java_aconfig_library_foo",
10629 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010630 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010631 "myapex",
10632 ],
10633 }
10634
10635 aconfig_declarations {
10636 name: "my_aconfig_declarations_bar",
10637 package: "com.example.package",
10638 container: "myapex",
10639 srcs: ["bar.aconfig"],
10640 }
10641
10642 java_aconfig_library {
10643 name: "my_java_aconfig_library_bar",
10644 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010645 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010646 "myapex",
10647 ],
10648 }
10649 `)
10650
10651 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10652 s := mod.Rule("apexRule").Args["copy_commands"]
10653 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Yu Liubba555e2024-02-17 00:36:42 +000010654 if len(copyCmds) != 8 {
Yu Liueae7b362023-11-16 17:05:47 -080010655 t.Fatalf("Expected 5 commands, got %d in:\n%s", len(copyCmds), s)
10656 }
10657
Yu Liuab31c822024-02-28 22:21:31 +000010658 ensureMatches(t, copyCmds[4], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10659 ensureMatches(t, copyCmds[5], "^cp -f .*/package.map .*/image.apex/etc$")
10660 ensureMatches(t, copyCmds[6], "^cp -f .*/flag.map .*/image.apex/etc$")
10661 ensureMatches(t, copyCmds[7], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liueae7b362023-11-16 17:05:47 -080010662
Yu Liubba555e2024-02-17 00:36:42 +000010663 inputs := []string{
10664 "my_aconfig_declarations_foo/intermediate.pb",
10665 "my_aconfig_declarations_bar/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010666 }
Yu Liubba555e2024-02-17 00:36:42 +000010667 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10668 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10669 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10670 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010671}
10672
10673func TestAconfigFilesJavaAndCcDeps(t *testing.T) {
10674 ctx := testApex(t, apex_default_bp+`
10675 apex {
10676 name: "myapex",
10677 manifest: ":myapex.manifest",
10678 androidManifest: ":myapex.androidmanifest",
10679 key: "myapex.key",
10680 java_libs: [
10681 "my_java_library_foo",
10682 ],
10683 native_shared_libs: [
10684 "my_cc_library_bar",
10685 ],
10686 binaries: [
10687 "my_cc_binary_baz",
10688 ],
10689 updatable: false,
10690 }
10691
10692 java_library {
10693 name: "my_java_library_foo",
10694 srcs: ["foo/bar/MyClass.java"],
10695 sdk_version: "none",
10696 system_modules: "none",
10697 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010698 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010699 "myapex",
10700 ],
10701 }
10702
10703 cc_library {
10704 name: "my_cc_library_bar",
10705 srcs: ["foo/bar/MyClass.cc"],
Yu Liucec0e412023-11-30 16:45:50 -080010706 static_libs: [
10707 "my_cc_aconfig_library_bar",
10708 "my_cc_aconfig_library_baz",
10709 ],
Yu Liueae7b362023-11-16 17:05:47 -080010710 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010711 "myapex",
10712 ],
10713 }
10714
10715 cc_binary {
10716 name: "my_cc_binary_baz",
10717 srcs: ["foo/bar/MyClass.cc"],
10718 static_libs: ["my_cc_aconfig_library_baz"],
Yu Liueae7b362023-11-16 17:05:47 -080010719 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010720 "myapex",
10721 ],
10722 }
10723
10724 aconfig_declarations {
10725 name: "my_aconfig_declarations_foo",
10726 package: "com.example.package",
10727 container: "myapex",
10728 srcs: ["foo.aconfig"],
10729 }
10730
10731 java_aconfig_library {
10732 name: "my_java_aconfig_library_foo",
10733 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010734 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010735 "myapex",
10736 ],
10737 }
10738
10739 aconfig_declarations {
10740 name: "my_aconfig_declarations_bar",
10741 package: "com.example.package",
10742 container: "myapex",
10743 srcs: ["bar.aconfig"],
10744 }
10745
10746 cc_aconfig_library {
10747 name: "my_cc_aconfig_library_bar",
10748 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010749 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010750 "myapex",
10751 ],
10752 }
10753
10754 aconfig_declarations {
10755 name: "my_aconfig_declarations_baz",
10756 package: "com.example.package",
10757 container: "myapex",
10758 srcs: ["baz.aconfig"],
10759 }
10760
10761 cc_aconfig_library {
10762 name: "my_cc_aconfig_library_baz",
10763 aconfig_declarations: "my_aconfig_declarations_baz",
Yu Liueae7b362023-11-16 17:05:47 -080010764 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010765 "myapex",
10766 ],
10767 }
10768
10769 cc_library {
10770 name: "server_configurable_flags",
10771 srcs: ["server_configurable_flags.cc"],
10772 }
Ted Bauerf0f18592024-04-23 18:25:26 +000010773 cc_library {
10774 name: "libbase",
10775 srcs: ["libbase.cc"],
Ted Bauer1e96f8c2024-04-25 19:50:01 +000010776 apex_available: [
10777 "myapex",
10778 ],
Ted Bauerf0f18592024-04-23 18:25:26 +000010779 }
10780 cc_library {
10781 name: "libaconfig_storage_read_api_cc",
10782 srcs: ["libaconfig_storage_read_api_cc.cc"],
10783 }
Yu Liueae7b362023-11-16 17:05:47 -080010784 `)
10785
10786 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10787 s := mod.Rule("apexRule").Args["copy_commands"]
10788 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Yu Liubba555e2024-02-17 00:36:42 +000010789 if len(copyCmds) != 12 {
10790 t.Fatalf("Expected 12 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010791 }
10792
Yu Liuab31c822024-02-28 22:21:31 +000010793 ensureMatches(t, copyCmds[8], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10794 ensureMatches(t, copyCmds[9], "^cp -f .*/package.map .*/image.apex/etc$")
10795 ensureMatches(t, copyCmds[10], "^cp -f .*/flag.map .*/image.apex/etc$")
10796 ensureMatches(t, copyCmds[11], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liueae7b362023-11-16 17:05:47 -080010797
Yu Liubba555e2024-02-17 00:36:42 +000010798 inputs := []string{
10799 "my_aconfig_declarations_foo/intermediate.pb",
10800 "my_cc_library_bar/android_arm64_armv8-a_shared_apex10000/myapex/aconfig_merged.pb",
10801 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010802 }
Yu Liubba555e2024-02-17 00:36:42 +000010803 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10804 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10805 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10806 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010807}
10808
Yu Liucec0e412023-11-30 16:45:50 -080010809func TestAconfigFilesRustDeps(t *testing.T) {
10810 ctx := testApex(t, apex_default_bp+`
10811 apex {
10812 name: "myapex",
10813 manifest: ":myapex.manifest",
10814 androidManifest: ":myapex.androidmanifest",
10815 key: "myapex.key",
10816 native_shared_libs: [
10817 "libmy_rust_library",
10818 ],
10819 binaries: [
10820 "my_rust_binary",
10821 ],
10822 rust_dyn_libs: [
10823 "libmy_rust_dylib",
10824 ],
10825 updatable: false,
10826 }
10827
10828 rust_library {
10829 name: "libflags_rust", // test mock
10830 crate_name: "flags_rust",
10831 srcs: ["lib.rs"],
10832 apex_available: [
10833 "myapex",
10834 ],
10835 }
10836
10837 rust_library {
10838 name: "liblazy_static", // test mock
10839 crate_name: "lazy_static",
10840 srcs: ["src/lib.rs"],
10841 apex_available: [
10842 "myapex",
10843 ],
10844 }
10845
Ted Bauer02d475c2024-03-27 20:56:26 +000010846 rust_library {
10847 name: "libaconfig_storage_read_api", // test mock
10848 crate_name: "aconfig_storage_read_api",
10849 srcs: ["src/lib.rs"],
10850 apex_available: [
10851 "myapex",
10852 ],
10853 }
10854
Ted Bauer6ef40db2024-03-29 14:04:10 +000010855 rust_library {
10856 name: "liblogger", // test mock
10857 crate_name: "logger",
10858 srcs: ["src/lib.rs"],
10859 apex_available: [
10860 "myapex",
10861 ],
10862 }
10863
10864 rust_library {
10865 name: "liblog_rust", // test mock
10866 crate_name: "log_rust",
10867 srcs: ["src/lib.rs"],
10868 apex_available: [
10869 "myapex",
10870 ],
10871 }
10872
Yu Liucec0e412023-11-30 16:45:50 -080010873 rust_ffi_shared {
10874 name: "libmy_rust_library",
10875 srcs: ["src/lib.rs"],
10876 rustlibs: ["libmy_rust_aconfig_library_foo"],
10877 crate_name: "my_rust_library",
10878 apex_available: [
10879 "myapex",
10880 ],
10881 }
10882
10883 rust_library_dylib {
10884 name: "libmy_rust_dylib",
10885 srcs: ["foo/bar/MyClass.rs"],
10886 rustlibs: ["libmy_rust_aconfig_library_bar"],
10887 crate_name: "my_rust_dylib",
10888 apex_available: [
10889 "myapex",
10890 ],
10891 }
10892
10893 rust_binary {
10894 name: "my_rust_binary",
10895 srcs: ["foo/bar/MyClass.rs"],
10896 rustlibs: [
10897 "libmy_rust_aconfig_library_baz",
10898 "libmy_rust_dylib",
10899 ],
10900 apex_available: [
10901 "myapex",
10902 ],
10903 }
10904
10905 aconfig_declarations {
10906 name: "my_aconfig_declarations_foo",
10907 package: "com.example.package",
10908 container: "myapex",
10909 srcs: ["foo.aconfig"],
10910 }
10911
10912 aconfig_declarations {
10913 name: "my_aconfig_declarations_bar",
10914 package: "com.example.package",
10915 container: "myapex",
10916 srcs: ["bar.aconfig"],
10917 }
10918
10919 aconfig_declarations {
10920 name: "my_aconfig_declarations_baz",
10921 package: "com.example.package",
10922 container: "myapex",
10923 srcs: ["baz.aconfig"],
10924 }
10925
10926 rust_aconfig_library {
10927 name: "libmy_rust_aconfig_library_foo",
10928 aconfig_declarations: "my_aconfig_declarations_foo",
10929 crate_name: "my_rust_aconfig_library_foo",
10930 apex_available: [
10931 "myapex",
10932 ],
10933 }
10934
10935 rust_aconfig_library {
10936 name: "libmy_rust_aconfig_library_bar",
10937 aconfig_declarations: "my_aconfig_declarations_bar",
10938 crate_name: "my_rust_aconfig_library_bar",
10939 apex_available: [
10940 "myapex",
10941 ],
10942 }
10943
10944 rust_aconfig_library {
10945 name: "libmy_rust_aconfig_library_baz",
10946 aconfig_declarations: "my_aconfig_declarations_baz",
10947 crate_name: "my_rust_aconfig_library_baz",
10948 apex_available: [
10949 "myapex",
10950 ],
10951 }
10952 `)
10953
10954 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10955 s := mod.Rule("apexRule").Args["copy_commands"]
10956 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Ted Bauer6ef40db2024-03-29 14:04:10 +000010957 if len(copyCmds) != 32 {
Ted Bauer02d475c2024-03-27 20:56:26 +000010958 t.Fatalf("Expected 28 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liucec0e412023-11-30 16:45:50 -080010959 }
10960
Ted Bauer6ef40db2024-03-29 14:04:10 +000010961 ensureMatches(t, copyCmds[28], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10962 ensureMatches(t, copyCmds[29], "^cp -f .*/package.map .*/image.apex/etc$")
10963 ensureMatches(t, copyCmds[30], "^cp -f .*/flag.map .*/image.apex/etc$")
10964 ensureMatches(t, copyCmds[31], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liucec0e412023-11-30 16:45:50 -080010965
Yu Liubba555e2024-02-17 00:36:42 +000010966 inputs := []string{
10967 "my_aconfig_declarations_foo/intermediate.pb",
Yu Liuab31c822024-02-28 22:21:31 +000010968 "my_aconfig_declarations_bar/intermediate.pb",
10969 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liubba555e2024-02-17 00:36:42 +000010970 "my_rust_binary/android_arm64_armv8-a_apex10000/myapex/aconfig_merged.pb",
10971 }
10972 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10973 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10974 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10975 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
10976}
10977
10978func VerifyAconfigRule(t *testing.T, mod *android.TestingModule, desc string, inputs []string, output string, container string, file_type string) {
10979 aconfigRule := mod.Description(desc)
10980 s := " " + aconfigRule.Args["cache_files"]
Yu Liucec0e412023-11-30 16:45:50 -080010981 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
Yu Liubba555e2024-02-17 00:36:42 +000010982 if len(aconfigArgs) != len(inputs) {
10983 t.Fatalf("Expected %d commands, got %d in:\n%s", len(inputs), len(aconfigArgs), s)
Yu Liucec0e412023-11-30 16:45:50 -080010984 }
Yu Liucec0e412023-11-30 16:45:50 -080010985
Yu Liubba555e2024-02-17 00:36:42 +000010986 ensureEquals(t, container, aconfigRule.Args["container"])
10987 ensureEquals(t, file_type, aconfigRule.Args["file_type"])
10988
10989 buildParams := aconfigRule.BuildParams
10990 for _, input := range inputs {
10991 android.EnsureListContainsSuffix(t, aconfigArgs, input)
10992 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), input)
Yu Liucec0e412023-11-30 16:45:50 -080010993 }
Yu Liubba555e2024-02-17 00:36:42 +000010994
10995 ensureContains(t, buildParams.Output.String(), output)
Yu Liucec0e412023-11-30 16:45:50 -080010996}
10997
Yu Liueae7b362023-11-16 17:05:47 -080010998func TestAconfigFilesOnlyMatchCurrentApex(t *testing.T) {
10999 ctx := testApex(t, apex_default_bp+`
11000 apex {
11001 name: "myapex",
11002 manifest: ":myapex.manifest",
11003 androidManifest: ":myapex.androidmanifest",
11004 key: "myapex.key",
11005 java_libs: [
11006 "my_java_library_foo",
11007 "other_java_library_bar",
11008 ],
11009 updatable: false,
11010 }
11011
11012 java_library {
11013 name: "my_java_library_foo",
11014 srcs: ["foo/bar/MyClass.java"],
11015 sdk_version: "none",
11016 system_modules: "none",
11017 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080011018 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011019 "myapex",
11020 ],
11021 }
11022
11023 java_library {
11024 name: "other_java_library_bar",
11025 srcs: ["foo/bar/MyClass.java"],
11026 sdk_version: "none",
11027 system_modules: "none",
11028 static_libs: ["other_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080011029 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011030 "myapex",
11031 ],
11032 }
11033
11034 aconfig_declarations {
11035 name: "my_aconfig_declarations_foo",
11036 package: "com.example.package",
11037 container: "myapex",
11038 srcs: ["foo.aconfig"],
11039 }
11040
11041 java_aconfig_library {
11042 name: "my_java_aconfig_library_foo",
11043 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011044 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011045 "myapex",
11046 ],
11047 }
11048
11049 aconfig_declarations {
11050 name: "other_aconfig_declarations_bar",
11051 package: "com.example.package",
11052 container: "otherapex",
11053 srcs: ["bar.aconfig"],
11054 }
11055
11056 java_aconfig_library {
11057 name: "other_java_aconfig_library_bar",
11058 aconfig_declarations: "other_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080011059 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011060 "myapex",
11061 ],
11062 }
11063 `)
11064
11065 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11066 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11067 s := " " + combineAconfigRule.Args["cache_files"]
11068 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11069 if len(aconfigArgs) != 1 {
11070 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
11071 }
11072 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11073
11074 buildParams := combineAconfigRule.BuildParams
11075 if len(buildParams.Inputs) != 1 {
11076 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
11077 }
11078 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11079 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11080}
11081
11082func TestAconfigFilesRemoveDuplicates(t *testing.T) {
11083 ctx := testApex(t, apex_default_bp+`
11084 apex {
11085 name: "myapex",
11086 manifest: ":myapex.manifest",
11087 androidManifest: ":myapex.androidmanifest",
11088 key: "myapex.key",
11089 java_libs: [
11090 "my_java_library_foo",
11091 "my_java_library_bar",
11092 ],
11093 updatable: false,
11094 }
11095
11096 java_library {
11097 name: "my_java_library_foo",
11098 srcs: ["foo/bar/MyClass.java"],
11099 sdk_version: "none",
11100 system_modules: "none",
11101 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080011102 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011103 "myapex",
11104 ],
11105 }
11106
11107 java_library {
11108 name: "my_java_library_bar",
11109 srcs: ["foo/bar/MyClass.java"],
11110 sdk_version: "none",
11111 system_modules: "none",
11112 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080011113 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011114 "myapex",
11115 ],
11116 }
11117
11118 aconfig_declarations {
11119 name: "my_aconfig_declarations_foo",
11120 package: "com.example.package",
11121 container: "myapex",
11122 srcs: ["foo.aconfig"],
11123 }
11124
11125 java_aconfig_library {
11126 name: "my_java_aconfig_library_foo",
11127 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011128 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011129 "myapex",
11130 ],
11131 }
11132
11133 java_aconfig_library {
11134 name: "my_java_aconfig_library_bar",
11135 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011136 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011137 "myapex",
11138 ],
11139 }
11140 `)
11141
11142 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11143 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11144 s := " " + combineAconfigRule.Args["cache_files"]
11145 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11146 if len(aconfigArgs) != 1 {
11147 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
11148 }
11149 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11150
11151 buildParams := combineAconfigRule.BuildParams
11152 if len(buildParams.Inputs) != 1 {
11153 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
11154 }
11155 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11156 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11157}
Spandan Das5be63332023-12-13 00:06:32 +000011158
11159// Test that the boot jars come from the _selected_ apex prebuilt
11160// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11161func TestBootDexJarsMultipleApexPrebuilts(t *testing.T) {
11162 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
11163 t.Helper()
11164 s := ctx.ModuleForTests("dex_bootjars", "android_common")
11165 foundLibfooJar := false
11166 base := stem + ".jar"
11167 for _, output := range s.AllOutputs() {
11168 if filepath.Base(output) == base {
11169 foundLibfooJar = true
11170 buildRule := s.Output(output)
11171 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
11172 }
11173 }
11174 if !foundLibfooJar {
11175 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
11176 }
11177 }
11178
Spandan Das64c9e0c2023-12-20 20:13:34 +000011179 // Check that the boot jars of the selected apex are run through boot_jars_package_check
11180 // This validates that the jars on the bootclasspath do not contain packages outside an allowlist
11181 checkBootJarsPackageCheck := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11182 platformBcp := ctx.ModuleForTests("platform-bootclasspath", "android_common")
11183 bootJarsCheckRule := platformBcp.Rule("boot_jars_package_check")
11184 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)
11185 }
11186
11187 // Check that the boot jars used to generate the monolithic hiddenapi flags come from the selected apex
11188 checkBootJarsForMonolithicHiddenapi := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11189 monolithicHiddenapiFlagsCmd := ctx.ModuleForTests("platform-bootclasspath", "android_common").Output("out/soong/hiddenapi/hiddenapi-stub-flags.txt").RuleParams.Command
11190 android.AssertStringMatches(t, "Could not find the correct boot dex jar in monolithic hiddenapi flags generation command", monolithicHiddenapiFlagsCmd, "--boot-dex="+expectedBootJar)
11191 }
11192
Spandan Das5be63332023-12-13 00:06:32 +000011193 bp := `
11194 // Source APEX.
11195
11196 java_library {
11197 name: "framework-foo",
11198 srcs: ["foo.java"],
11199 installable: true,
11200 apex_available: [
11201 "com.android.foo",
11202 ],
11203 }
11204
11205 bootclasspath_fragment {
11206 name: "foo-bootclasspath-fragment",
11207 contents: ["framework-foo"],
11208 apex_available: [
11209 "com.android.foo",
11210 ],
11211 hidden_api: {
11212 split_packages: ["*"],
11213 },
11214 }
11215
11216 apex_key {
11217 name: "com.android.foo.key",
11218 public_key: "com.android.foo.avbpubkey",
11219 private_key: "com.android.foo.pem",
11220 }
11221
11222 apex {
11223 name: "com.android.foo",
11224 key: "com.android.foo.key",
11225 bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11226 updatable: false,
11227 }
11228
11229 // Prebuilt APEX.
11230
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011231 java_sdk_library_import {
Spandan Das5be63332023-12-13 00:06:32 +000011232 name: "framework-foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011233 public: {
11234 jars: ["foo.jar"],
11235 },
Spandan Das5be63332023-12-13 00:06:32 +000011236 apex_available: ["com.android.foo"],
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011237 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +000011238 }
11239
11240 prebuilt_bootclasspath_fragment {
11241 name: "foo-bootclasspath-fragment",
11242 contents: ["framework-foo"],
11243 hidden_api: {
11244 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
11245 metadata: "my-bootclasspath-fragment/metadata.csv",
11246 index: "my-bootclasspath-fragment/index.csv",
11247 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
11248 all_flags: "my-bootclasspath-fragment/all-flags.csv",
11249 },
11250 apex_available: [
11251 "com.android.foo",
11252 ],
11253 }
11254
11255 prebuilt_apex {
11256 name: "com.android.foo",
11257 apex_name: "com.android.foo",
11258 src: "com.android.foo-arm.apex",
11259 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11260 }
11261
11262 // Another Prebuilt ART APEX
11263 prebuilt_apex {
11264 name: "com.android.foo.v2",
11265 apex_name: "com.android.foo", // Used to determine the API domain
11266 src: "com.android.foo-arm.apex",
11267 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11268 }
11269
11270 // APEX contribution modules
11271
11272 apex_contributions {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011273 name: "foo.source.contributions",
Spandan Das5be63332023-12-13 00:06:32 +000011274 api_domain: "com.android.foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011275 contents: ["com.android.foo"],
11276 }
11277
11278 apex_contributions {
11279 name: "foo.prebuilt.contributions",
11280 api_domain: "com.android.foo",
11281 contents: ["prebuilt_com.android.foo"],
11282 }
11283
11284 apex_contributions {
11285 name: "foo.prebuilt.v2.contributions",
11286 api_domain: "com.android.foo",
11287 contents: ["com.android.foo.v2"], // prebuilt_ prefix is missing because of prebuilt_rename mutator
Spandan Das5be63332023-12-13 00:06:32 +000011288 }
11289 `
11290
11291 testCases := []struct {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011292 desc string
11293 selectedApexContributions string
11294 expectedBootJar string
Spandan Das5be63332023-12-13 00:06:32 +000011295 }{
11296 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011297 desc: "Source apex com.android.foo is selected, bootjar should come from source java library",
11298 selectedApexContributions: "foo.source.contributions",
11299 expectedBootJar: "out/soong/.intermediates/foo-bootclasspath-fragment/android_common_apex10000/hiddenapi-modular/encoded/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011300 },
11301 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011302 desc: "Prebuilt apex prebuilt_com.android.foo is selected, profile should come from .prof deapexed from the prebuilt",
11303 selectedApexContributions: "foo.prebuilt.contributions",
11304 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011305 },
11306 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011307 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected, profile should come from .prof deapexed from the prebuilt",
11308 selectedApexContributions: "foo.prebuilt.v2.contributions",
11309 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.v2.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011310 },
11311 }
11312
11313 fragment := java.ApexVariantReference{
11314 Apex: proptools.StringPtr("com.android.foo"),
11315 Module: proptools.StringPtr("foo-bootclasspath-fragment"),
11316 }
11317
11318 for _, tc := range testCases {
11319 preparer := android.GroupFixturePreparers(
11320 java.FixtureConfigureApexBootJars("com.android.foo:framework-foo"),
11321 android.FixtureMergeMockFs(map[string][]byte{
11322 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11323 }),
Spandan Das81fe4d12024-05-15 18:43:47 +000011324 // Make sure that we have atleast one platform library so that we can check the monolithic hiddenapi
11325 // file creation.
11326 java.FixtureConfigureBootJars("platform:foo"),
11327 android.FixtureModifyMockFS(func(fs android.MockFS) {
11328 fs["platform/Android.bp"] = []byte(`
11329 java_library {
11330 name: "foo",
11331 srcs: ["Test.java"],
11332 compile_dex: true,
11333 }
11334 `)
11335 fs["platform/Test.java"] = nil
11336 }),
11337
Colin Crossa66b4632024-08-08 15:50:47 -070011338 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das5be63332023-12-13 00:06:32 +000011339 )
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011340 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das5be63332023-12-13 00:06:32 +000011341 checkBootDexJarPath(t, ctx, "framework-foo", tc.expectedBootJar)
Spandan Das64c9e0c2023-12-20 20:13:34 +000011342 checkBootJarsPackageCheck(t, ctx, tc.expectedBootJar)
11343 checkBootJarsForMonolithicHiddenapi(t, ctx, tc.expectedBootJar)
Spandan Das5be63332023-12-13 00:06:32 +000011344 }
11345}
Spandan Das3576e762024-01-03 18:57:03 +000011346
11347// Test that product packaging installs the selected mainline module (either source or a specific prebuilt)
11348// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11349func TestInstallationRulesForMultipleApexPrebuilts(t *testing.T) {
Spandan Das3576e762024-01-03 18:57:03 +000011350 // for a mainline module family, check that only the flagged soong module is visible to make
11351 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleName string, hiddenModuleNames []string) {
11352 variation := func(moduleName string) string {
11353 ret := "android_common_com.android.foo"
11354 if moduleName == "com.google.android.foo" {
Spandan Das50801e22024-05-13 18:29:45 +000011355 ret = "android_common_com.google.android.foo_com.google.android.foo"
Spandan Das3576e762024-01-03 18:57:03 +000011356 }
11357 return ret
11358 }
11359
11360 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11361 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11362
11363 for _, hiddenModuleName := range hiddenModuleNames {
11364 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11365 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11366
11367 }
11368 }
11369
11370 bp := `
11371 apex_key {
11372 name: "com.android.foo.key",
11373 public_key: "com.android.foo.avbpubkey",
11374 private_key: "com.android.foo.pem",
11375 }
11376
11377 // AOSP source apex
11378 apex {
11379 name: "com.android.foo",
11380 key: "com.android.foo.key",
11381 updatable: false,
11382 }
11383
11384 // Google source apex
11385 override_apex {
11386 name: "com.google.android.foo",
11387 base: "com.android.foo",
11388 key: "com.android.foo.key",
11389 }
11390
11391 // Prebuilt Google APEX.
11392
11393 prebuilt_apex {
11394 name: "com.google.android.foo",
11395 apex_name: "com.android.foo",
11396 src: "com.android.foo-arm.apex",
11397 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11398 }
11399
11400 // Another Prebuilt Google APEX
11401 prebuilt_apex {
11402 name: "com.google.android.foo.v2",
11403 apex_name: "com.android.foo",
Spandan Dasa8e2d612024-07-26 19:24:27 +000011404 source_apex_name: "com.google.android.foo",
Spandan Das3576e762024-01-03 18:57:03 +000011405 src: "com.android.foo-arm.apex",
11406 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11407 }
11408
11409 // APEX contribution modules
11410
11411 apex_contributions {
11412 name: "foo.source.contributions",
11413 api_domain: "com.android.foo",
11414 contents: ["com.google.android.foo"],
11415 }
11416
11417 apex_contributions {
11418 name: "foo.prebuilt.contributions",
11419 api_domain: "com.android.foo",
11420 contents: ["prebuilt_com.google.android.foo"],
11421 }
11422
11423 apex_contributions {
11424 name: "foo.prebuilt.v2.contributions",
11425 api_domain: "com.android.foo",
11426 contents: ["prebuilt_com.google.android.foo.v2"],
11427 }
11428
11429 // This is an incompatible module because it selects multiple versions of the same mainline module
11430 apex_contributions {
11431 name: "foo.prebuilt.duplicate.contributions",
11432 api_domain: "com.android.foo",
11433 contents: [
11434 "prebuilt_com.google.android.foo",
11435 "prebuilt_com.google.android.foo.v2",
11436 ],
11437 }
11438 `
11439
11440 testCases := []struct {
11441 desc string
11442 selectedApexContributions string
11443 expectedVisibleModuleName string
11444 expectedHiddenModuleNames []string
11445 expectedError string
11446 }{
11447 {
11448 desc: "Source apex is selected, prebuilts should be hidden from make",
11449 selectedApexContributions: "foo.source.contributions",
11450 expectedVisibleModuleName: "com.google.android.foo",
11451 expectedHiddenModuleNames: []string{"prebuilt_com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11452 },
11453 {
11454 desc: "Prebuilt apex prebuilt_com.android.foo is selected, source and the other prebuilt should be hidden from make",
11455 selectedApexContributions: "foo.prebuilt.contributions",
11456 expectedVisibleModuleName: "prebuilt_com.google.android.foo",
11457 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11458 },
11459 {
11460 desc: "Prebuilt apex prebuilt_com.android.fooi.v2 is selected, source and the other prebuilt should be hidden from make",
11461 selectedApexContributions: "foo.prebuilt.v2.contributions",
11462 expectedVisibleModuleName: "prebuilt_com.google.android.foo.v2",
11463 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo"},
11464 },
11465 {
11466 desc: "Multiple versions of a prebuilt apex is selected in the same release config",
11467 selectedApexContributions: "foo.prebuilt.duplicate.contributions",
11468 expectedError: "Found duplicate variations of the same module in apex_contributions: prebuilt_com.google.android.foo and prebuilt_com.google.android.foo.v2",
11469 },
11470 }
11471
11472 for _, tc := range testCases {
11473 preparer := android.GroupFixturePreparers(
11474 android.FixtureMergeMockFs(map[string][]byte{
11475 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11476 }),
Colin Crossa66b4632024-08-08 15:50:47 -070011477 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das3576e762024-01-03 18:57:03 +000011478 )
11479 if tc.expectedError != "" {
11480 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(tc.expectedError))
11481 testApex(t, bp, preparer)
11482 return
11483 }
11484 ctx := testApex(t, bp, preparer)
11485
Spandan Das3576e762024-01-03 18:57:03 +000011486 // Check that
11487 // 1. The contents of the selected apex_contributions are visible to make
11488 // 2. The rest of the apexes in the mainline module family (source or other prebuilt) is hidden from make
11489 checkHideFromMake(t, ctx, tc.expectedVisibleModuleName, tc.expectedHiddenModuleNames)
11490 }
11491}
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011492
Spandan Das85bd4622024-08-01 00:51:20 +000011493// Test that product packaging installs the selected mainline module in workspaces withtout source mainline module
11494func TestInstallationRulesForMultipleApexPrebuiltsWithoutSource(t *testing.T) {
11495 // for a mainline module family, check that only the flagged soong module is visible to make
11496 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleNames []string, hiddenModuleNames []string) {
11497 variation := func(moduleName string) string {
11498 ret := "android_common_com.android.adservices"
11499 if moduleName == "com.google.android.foo" {
11500 ret = "android_common_com.google.android.foo_com.google.android.foo"
11501 }
11502 return ret
11503 }
11504
11505 for _, visibleModuleName := range visibleModuleNames {
11506 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11507 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11508 }
11509
11510 for _, hiddenModuleName := range hiddenModuleNames {
11511 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11512 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11513
11514 }
11515 }
11516
11517 bp := `
11518 apex_key {
11519 name: "com.android.adservices.key",
11520 public_key: "com.android.adservices.avbpubkey",
11521 private_key: "com.android.adservices.pem",
11522 }
11523
11524 // AOSP source apex
11525 apex {
11526 name: "com.android.adservices",
11527 key: "com.android.adservices.key",
11528 updatable: false,
11529 }
11530
11531 // Prebuilt Google APEX.
11532
11533 prebuilt_apex {
11534 name: "com.google.android.adservices",
11535 apex_name: "com.android.adservices",
11536 src: "com.android.foo-arm.apex",
11537 }
11538
11539 // Another Prebuilt Google APEX
11540 prebuilt_apex {
11541 name: "com.google.android.adservices.v2",
11542 apex_name: "com.android.adservices",
11543 src: "com.android.foo-arm.apex",
11544 }
11545
11546 // APEX contribution modules
11547
11548
11549 apex_contributions {
11550 name: "adservices.prebuilt.contributions",
11551 api_domain: "com.android.adservices",
11552 contents: ["prebuilt_com.google.android.adservices"],
11553 }
11554
11555 apex_contributions {
11556 name: "adservices.prebuilt.v2.contributions",
11557 api_domain: "com.android.adservices",
11558 contents: ["prebuilt_com.google.android.adservices.v2"],
11559 }
11560 `
11561
11562 testCases := []struct {
11563 desc string
11564 selectedApexContributions string
11565 expectedVisibleModuleNames []string
11566 expectedHiddenModuleNames []string
11567 }{
11568 {
11569 desc: "No apex contributions selected, source aosp apex should be visible, and mainline prebuilts should be hidden",
11570 selectedApexContributions: "",
11571 expectedVisibleModuleNames: []string{"com.android.adservices"},
11572 expectedHiddenModuleNames: []string{"com.google.android.adservices", "com.google.android.adservices.v2"},
11573 },
11574 {
11575 desc: "Prebuilt apex prebuilt_com.android.foo is selected",
11576 selectedApexContributions: "adservices.prebuilt.contributions",
11577 expectedVisibleModuleNames: []string{"com.android.adservices", "com.google.android.adservices"},
11578 expectedHiddenModuleNames: []string{"com.google.android.adservices.v2"},
11579 },
11580 {
11581 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected",
11582 selectedApexContributions: "adservices.prebuilt.v2.contributions",
11583 expectedVisibleModuleNames: []string{"com.android.adservices", "com.google.android.adservices.v2"},
11584 expectedHiddenModuleNames: []string{"com.google.android.adservices"},
11585 },
11586 }
11587
11588 for _, tc := range testCases {
11589 preparer := android.GroupFixturePreparers(
11590 android.FixtureMergeMockFs(map[string][]byte{
11591 "system/sepolicy/apex/com.android.adservices-file_contexts": nil,
11592 }),
Colin Crossa66b4632024-08-08 15:50:47 -070011593 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das85bd4622024-08-01 00:51:20 +000011594 )
11595 ctx := testApex(t, bp, preparer)
11596
11597 checkHideFromMake(t, ctx, tc.expectedVisibleModuleNames, tc.expectedHiddenModuleNames)
11598 }
11599}
11600
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011601func TestAconfifDeclarationsValidation(t *testing.T) {
11602 aconfigDeclarationLibraryString := func(moduleNames []string) (ret string) {
11603 for _, moduleName := range moduleNames {
11604 ret += fmt.Sprintf(`
11605 aconfig_declarations {
11606 name: "%[1]s",
11607 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +000011608 container: "system",
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011609 srcs: [
11610 "%[1]s.aconfig",
11611 ],
11612 }
11613 java_aconfig_library {
11614 name: "%[1]s-lib",
11615 aconfig_declarations: "%[1]s",
11616 }
11617 `, moduleName)
11618 }
11619 return ret
11620 }
11621
11622 result := android.GroupFixturePreparers(
11623 prepareForApexTest,
11624 java.PrepareForTestWithJavaSdkLibraryFiles,
11625 java.FixtureWithLastReleaseApis("foo"),
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011626 ).RunTestWithBp(t, `
11627 java_library {
11628 name: "baz-java-lib",
11629 static_libs: [
11630 "baz-lib",
11631 ],
11632 }
11633 filegroup {
11634 name: "qux-filegroup",
11635 srcs: [
11636 ":qux-lib{.generated_srcjars}",
11637 ],
11638 }
11639 filegroup {
11640 name: "qux-another-filegroup",
11641 srcs: [
11642 ":qux-filegroup",
11643 ],
11644 }
11645 java_library {
11646 name: "quux-java-lib",
11647 srcs: [
11648 "a.java",
11649 ],
11650 libs: [
11651 "quux-lib",
11652 ],
11653 }
11654 java_sdk_library {
11655 name: "foo",
11656 srcs: [
11657 ":qux-another-filegroup",
11658 ],
11659 api_packages: ["foo"],
11660 system: {
11661 enabled: true,
11662 },
11663 module_lib: {
11664 enabled: true,
11665 },
11666 test: {
11667 enabled: true,
11668 },
11669 static_libs: [
11670 "bar-lib",
11671 ],
11672 libs: [
11673 "baz-java-lib",
11674 "quux-java-lib",
11675 ],
11676 aconfig_declarations: [
11677 "bar",
11678 ],
11679 }
11680 `+aconfigDeclarationLibraryString([]string{"bar", "baz", "qux", "quux"}))
11681
11682 m := result.ModuleForTests("foo.stubs.source", "android_common")
11683 outDir := "out/soong/.intermediates"
11684
11685 // Arguments passed to aconfig to retrieve the state of the flags defined in the
11686 // textproto files
11687 aconfigFlagArgs := m.Output("released-flagged-apis-exportable.txt").Args["flags_path"]
11688
11689 // "bar-lib" is a static_lib of "foo" and is passed to metalava as classpath. Thus the
11690 // cache file provided by the associated aconfig_declarations module "bar" should be passed
11691 // to aconfig.
11692 android.AssertStringDoesContain(t, "cache file of a java_aconfig_library static_lib "+
11693 "passed as an input",
11694 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "bar"))
11695
11696 // "baz-java-lib", which statically depends on "baz-lib", is a lib of "foo" and is passed
11697 // to metalava as classpath. Thus the cache file provided by the associated
11698 // aconfig_declarations module "baz" should be passed to aconfig.
11699 android.AssertStringDoesContain(t, "cache file of a lib that statically depends on "+
11700 "java_aconfig_library passed as an input",
11701 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "baz"))
11702
11703 // "qux-lib" is passed to metalava as src via the filegroup, thus the cache file provided by
11704 // the associated aconfig_declarations module "qux" should be passed to aconfig.
11705 android.AssertStringDoesContain(t, "cache file of srcs java_aconfig_library passed as an "+
11706 "input",
11707 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "qux"))
11708
11709 // "quux-java-lib" is a lib of "foo" and is passed to metalava as classpath, but does not
11710 // statically depend on "quux-lib". Therefore, the cache file provided by the associated
11711 // aconfig_declarations module "quux" should not be passed to aconfig.
11712 android.AssertStringDoesNotContain(t, "cache file of a lib that does not statically "+
11713 "depend on java_aconfig_library not passed as an input",
11714 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "quux"))
11715}
Cole Faust7c991b42024-05-15 11:17:55 -070011716
11717func TestMultiplePrebuiltsWithSameBase(t *testing.T) {
11718 ctx := testApex(t, `
11719 apex {
11720 name: "myapex",
11721 key: "myapex.key",
11722 prebuilts: ["myetc", "myetc2"],
11723 min_sdk_version: "29",
11724 }
11725 apex_key {
11726 name: "myapex.key",
11727 public_key: "testkey.avbpubkey",
11728 private_key: "testkey.pem",
11729 }
11730
11731 prebuilt_etc {
11732 name: "myetc",
11733 src: "myprebuilt",
11734 filename: "myfilename",
11735 }
11736 prebuilt_etc {
11737 name: "myetc2",
11738 sub_dir: "mysubdir",
11739 src: "myprebuilt",
11740 filename: "myfilename",
11741 }
11742 `, withFiles(android.MockFS{
11743 "packages/modules/common/build/allowed_deps.txt": nil,
11744 }))
11745
11746 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
11747 data := android.AndroidMkDataForTest(t, ctx, ab)
11748 var builder strings.Builder
11749 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
11750 androidMk := builder.String()
11751
11752 android.AssertStringDoesContain(t, "not found", androidMk, "LOCAL_MODULE := etc_myfilename.myapex")
11753 android.AssertStringDoesContain(t, "not found", androidMk, "LOCAL_MODULE := etc_mysubdir_myfilename.myapex")
11754}
Spandan Das50801e22024-05-13 18:29:45 +000011755
11756func TestApexMinSdkVersionOverride(t *testing.T) {
11757 checkMinSdkVersion := func(t *testing.T, module android.TestingModule, expectedMinSdkVersion string) {
11758 args := module.Rule("apexRule").Args
11759 optFlags := args["opt_flags"]
11760 if !strings.Contains(optFlags, "--min_sdk_version "+expectedMinSdkVersion) {
11761 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", module.Module(), expectedMinSdkVersion, optFlags)
11762 }
11763 }
11764
11765 checkHasDep := func(t *testing.T, ctx *android.TestContext, m android.Module, wantDep android.Module) {
11766 t.Helper()
11767 found := false
11768 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
11769 if dep == wantDep {
11770 found = true
11771 }
11772 })
11773 if !found {
11774 t.Errorf("Could not find a dependency from %v to %v\n", m, wantDep)
11775 }
11776 }
11777
11778 ctx := testApex(t, `
11779 apex {
11780 name: "com.android.apex30",
11781 min_sdk_version: "30",
11782 key: "apex30.key",
11783 java_libs: ["javalib"],
11784 }
11785
11786 java_library {
11787 name: "javalib",
11788 srcs: ["A.java"],
11789 apex_available: ["com.android.apex30"],
11790 min_sdk_version: "30",
11791 sdk_version: "current",
11792 }
11793
11794 override_apex {
11795 name: "com.mycompany.android.apex30",
11796 base: "com.android.apex30",
11797 }
11798
11799 override_apex {
11800 name: "com.mycompany.android.apex31",
11801 base: "com.android.apex30",
11802 min_sdk_version: "31",
11803 }
11804
11805 apex_key {
11806 name: "apex30.key",
11807 public_key: "testkey.avbpubkey",
11808 private_key: "testkey.pem",
11809 }
11810
11811 `, android.FixtureMergeMockFs(android.MockFS{
11812 "system/sepolicy/apex/com.android.apex30-file_contexts": nil,
11813 }),
11814 )
11815
11816 baseModule := ctx.ModuleForTests("com.android.apex30", "android_common_com.android.apex30")
11817 checkMinSdkVersion(t, baseModule, "30")
11818
11819 // Override module, but uses same min_sdk_version
11820 overridingModuleSameMinSdkVersion := ctx.ModuleForTests("com.android.apex30", "android_common_com.mycompany.android.apex30_com.mycompany.android.apex30")
11821 javalibApex30Variant := ctx.ModuleForTests("javalib", "android_common_apex30")
11822 checkMinSdkVersion(t, overridingModuleSameMinSdkVersion, "30")
11823 checkHasDep(t, ctx, overridingModuleSameMinSdkVersion.Module(), javalibApex30Variant.Module())
11824
11825 // Override module, uses different min_sdk_version
11826 overridingModuleDifferentMinSdkVersion := ctx.ModuleForTests("com.android.apex30", "android_common_com.mycompany.android.apex31_com.mycompany.android.apex31")
11827 javalibApex31Variant := ctx.ModuleForTests("javalib", "android_common_apex31")
11828 checkMinSdkVersion(t, overridingModuleDifferentMinSdkVersion, "31")
11829 checkHasDep(t, ctx, overridingModuleDifferentMinSdkVersion.Module(), javalibApex31Variant.Module())
11830}
Spandan Das0b28fa02024-05-28 23:40:17 +000011831
11832func TestOverrideApexWithPrebuiltApexPreferred(t *testing.T) {
11833 context := android.GroupFixturePreparers(
11834 android.PrepareForIntegrationTestWithAndroid,
11835 PrepareForTestWithApexBuildComponents,
11836 android.FixtureMergeMockFs(android.MockFS{
11837 "system/sepolicy/apex/foo-file_contexts": nil,
11838 }),
11839 )
11840 res := context.RunTestWithBp(t, `
11841 apex {
11842 name: "foo",
11843 key: "myapex.key",
11844 apex_available_name: "com.android.foo",
11845 variant_version: "0",
11846 updatable: false,
11847 }
11848 apex_key {
11849 name: "myapex.key",
11850 public_key: "testkey.avbpubkey",
11851 private_key: "testkey.pem",
11852 }
11853 prebuilt_apex {
11854 name: "foo",
11855 src: "foo.apex",
11856 prefer: true,
11857 }
11858 override_apex {
11859 name: "myoverrideapex",
11860 base: "foo",
11861 }
11862 `)
11863
11864 java.CheckModuleHasDependency(t, res.TestContext, "myoverrideapex", "android_common_myoverrideapex_myoverrideapex", "foo")
11865}
Spandan Dasca1d63e2024-07-01 22:53:49 +000011866
11867func TestUpdatableApexMinSdkVersionCurrent(t *testing.T) {
11868 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`, `
11869 apex {
11870 name: "myapex",
11871 key: "myapex.key",
11872 updatable: true,
11873 min_sdk_version: "current",
11874 }
11875
11876 apex_key {
11877 name: "myapex.key",
11878 public_key: "testkey.avbpubkey",
11879 private_key: "testkey.pem",
11880 }
11881 `)
11882}
Spandan Das2f68f192024-07-22 19:25:50 +000011883
11884func TestPrebuiltStubNoinstall(t *testing.T) {
11885 testFunc := func(t *testing.T, expectLibfooOnSystemLib bool, fs android.MockFS) {
11886 result := android.GroupFixturePreparers(
11887 prepareForApexTest,
11888 android.PrepareForTestWithAndroidMk,
11889 android.PrepareForTestWithMakevars,
11890 android.FixtureMergeMockFs(fs),
11891 ).RunTest(t)
11892
11893 ldRule := result.ModuleForTests("installedlib", "android_arm64_armv8-a_shared").Rule("ld")
Spandan Das357ffcc2024-07-24 18:07:48 +000011894 android.AssertStringDoesContain(t, "", ldRule.Args["libFlags"], "android_arm64_armv8-a_shared_current/libfoo.so")
Spandan Das2f68f192024-07-22 19:25:50 +000011895
11896 installRules := result.InstallMakeRulesForTesting(t)
11897
11898 var installedlibRule *android.InstallMakeRule
11899 for i, rule := range installRules {
11900 if rule.Target == "out/target/product/test_device/system/lib/installedlib.so" {
11901 if installedlibRule != nil {
11902 t.Errorf("Duplicate install rules for %s", rule.Target)
11903 }
11904 installedlibRule = &installRules[i]
11905 }
11906 }
11907 if installedlibRule == nil {
11908 t.Errorf("No install rule found for installedlib")
11909 return
11910 }
11911
11912 if expectLibfooOnSystemLib {
11913 android.AssertStringListContains(t,
11914 "installedlib doesn't have install dependency on libfoo impl",
11915 installedlibRule.OrderOnlyDeps,
11916 "out/target/product/test_device/system/lib/libfoo.so")
11917 } else {
11918 android.AssertStringListDoesNotContain(t,
11919 "installedlib has install dependency on libfoo stub",
11920 installedlibRule.Deps,
11921 "out/target/product/test_device/system/lib/libfoo.so")
11922 android.AssertStringListDoesNotContain(t,
11923 "installedlib has order-only install dependency on libfoo stub",
11924 installedlibRule.OrderOnlyDeps,
11925 "out/target/product/test_device/system/lib/libfoo.so")
11926 }
11927 }
11928
11929 prebuiltLibfooBp := []byte(`
11930 cc_prebuilt_library {
11931 name: "libfoo",
11932 prefer: true,
11933 srcs: ["libfoo.so"],
11934 stubs: {
11935 versions: ["1"],
11936 },
11937 apex_available: ["apexfoo"],
11938 }
11939 `)
11940
11941 apexfooBp := []byte(`
11942 apex {
11943 name: "apexfoo",
11944 key: "apexfoo.key",
11945 native_shared_libs: ["libfoo"],
11946 updatable: false,
11947 compile_multilib: "both",
11948 }
11949 apex_key {
11950 name: "apexfoo.key",
11951 public_key: "testkey.avbpubkey",
11952 private_key: "testkey.pem",
11953 }
11954 `)
11955
11956 installedlibBp := []byte(`
11957 cc_library {
11958 name: "installedlib",
11959 shared_libs: ["libfoo"],
11960 }
11961 `)
11962
11963 t.Run("prebuilt stub (without source): no install", func(t *testing.T) {
11964 testFunc(
11965 t,
11966 /*expectLibfooOnSystemLib=*/ false,
11967 android.MockFS{
11968 "prebuilts/module_sdk/art/current/Android.bp": prebuiltLibfooBp,
11969 "apexfoo/Android.bp": apexfooBp,
11970 "system/sepolicy/apex/apexfoo-file_contexts": nil,
11971 "Android.bp": installedlibBp,
11972 },
11973 )
11974 })
11975
11976 disabledSourceLibfooBp := []byte(`
11977 cc_library {
11978 name: "libfoo",
11979 enabled: false,
11980 stubs: {
11981 versions: ["1"],
11982 },
11983 apex_available: ["apexfoo"],
11984 }
11985 `)
11986
11987 t.Run("prebuilt stub (with disabled source): no install", func(t *testing.T) {
11988 testFunc(
11989 t,
11990 /*expectLibfooOnSystemLib=*/ false,
11991 android.MockFS{
11992 "prebuilts/module_sdk/art/current/Android.bp": prebuiltLibfooBp,
11993 "impl/Android.bp": disabledSourceLibfooBp,
11994 "apexfoo/Android.bp": apexfooBp,
11995 "system/sepolicy/apex/apexfoo-file_contexts": nil,
11996 "Android.bp": installedlibBp,
11997 },
11998 )
11999 })
12000}