blob: c37c7d0c7941dcac9ed02b44462d394338c48f11 [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",
Jooyung Han40b79172024-08-16 16:00:33 +0900387 apex_available: [ "myapex" ],
Yifan Hongd22a84a2020-07-28 17:37:46 -0700388 }
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
Paul Duffindddd5462020-04-07 15:25:44 +0100435 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900436 name: "mylib2",
437 srcs: ["mylib.cpp"],
438 system_shared_libs: [],
439 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900440 static_libs: ["libstatic"],
441 // TODO: remove //apex_available:platform
442 apex_available: [
443 "//apex_available:platform",
444 "myapex",
445 ],
446 }
447
Paul Duffindddd5462020-04-07 15:25:44 +0100448 cc_prebuilt_library_shared {
449 name: "mylib2",
450 srcs: ["prebuilt.so"],
451 // TODO: remove //apex_available:platform
452 apex_available: [
453 "//apex_available:platform",
454 "myapex",
455 ],
456 }
457
Jiyong Park9918e1a2020-03-17 19:16:40 +0900458 cc_library_static {
459 name: "libstatic",
460 srcs: ["mylib.cpp"],
461 system_shared_libs: [],
462 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000463 // TODO: remove //apex_available:platform
464 apex_available: [
465 "//apex_available:platform",
466 "myapex",
467 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900468 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900469
470 java_library {
471 name: "myjar",
472 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900473 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900474 sdk_version: "none",
475 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900476 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900477 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000478 // TODO: remove //apex_available:platform
479 apex_available: [
480 "//apex_available:platform",
481 "myapex",
482 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900483 }
484
Jiyong Park77acec62020-06-01 21:39:15 +0900485 dex_import {
486 name: "myjar_dex",
487 jars: ["prebuilt.jar"],
488 apex_available: [
489 "//apex_available:platform",
490 "myapex",
491 ],
492 }
493
Jiyong Park7f7766d2019-07-25 22:02:35 +0900494 java_library {
495 name: "myotherjar",
496 srcs: ["foo/bar/MyClass.java"],
497 sdk_version: "none",
498 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900499 // TODO: remove //apex_available:platform
500 apex_available: [
501 "//apex_available:platform",
502 "myapex",
503 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900504 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900505
506 java_library {
507 name: "mysharedjar",
508 srcs: ["foo/bar/MyClass.java"],
509 sdk_version: "none",
510 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900511 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900512 `)
513
Jooyung Hana0503a52023-08-23 13:12:50 +0900514 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900515
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900516 // Make sure that Android.mk is created
Jooyung Hana0503a52023-08-23 13:12:50 +0900517 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700518 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900519 var builder strings.Builder
520 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
521
522 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000523 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900524 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
525
Jiyong Park42cca6c2019-04-01 11:15:50 +0900526 optFlags := apexRule.Args["opt_flags"]
527 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700528 // Ensure that the NOTICE output is being packaged as an asset.
Jooyung Hana0503a52023-08-23 13:12:50 +0900529 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900530
Jiyong Park25fc6a92018-11-18 18:02:45 +0900531 copyCmds := apexRule.Args["copy_commands"]
532
533 // Ensure that main rule creates an output
534 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
535
536 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700537 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
538 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
539 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900540 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900541 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900542
543 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700544 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
545 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900546 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
547 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900548 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900549 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900550
551 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800552 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
553 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900554 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900555 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900556 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900557 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
558 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900559 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900560 // .. but not for java libs
561 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900562 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800563
Colin Cross7113d202019-11-20 16:39:12 -0800564 // Ensure that the platform variant ends with _shared or _common
565 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
566 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900567 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
568 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900569 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
570
571 // Ensure that dynamic dependency to java libs are not included
572 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800573
574 // Ensure that all symlinks are present.
575 found_foo_link_64 := false
576 found_foo := false
577 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900578 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800579 if strings.HasSuffix(cmd, "bin/foo") {
580 found_foo = true
581 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
582 found_foo_link_64 = true
583 }
584 }
585 }
586 good := found_foo && found_foo_link_64
587 if !good {
588 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
589 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900590
Colin Crossf61d03d2023-11-02 16:56:39 -0700591 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
592 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100593 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100594 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
595 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
596 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100597
Colin Crossf61d03d2023-11-02 16:56:39 -0700598 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
599 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100600 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100601 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
602 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
603 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800604}
605
Jooyung Hanf21c7972019-12-16 22:32:06 +0900606func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800607 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900608 apex_defaults {
609 name: "myapex-defaults",
610 key: "myapex.key",
611 prebuilts: ["myetc"],
612 native_shared_libs: ["mylib"],
613 java_libs: ["myjar"],
614 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900615 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800616 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000617 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900618 }
619
620 prebuilt_etc {
621 name: "myetc",
622 src: "myprebuilt",
623 }
624
625 apex {
626 name: "myapex",
627 defaults: ["myapex-defaults"],
628 }
629
630 apex_key {
631 name: "myapex.key",
632 public_key: "testkey.avbpubkey",
633 private_key: "testkey.pem",
634 }
635
636 cc_library {
637 name: "mylib",
638 system_shared_libs: [],
639 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000640 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900641 }
642
643 java_library {
644 name: "myjar",
645 srcs: ["foo/bar/MyClass.java"],
646 sdk_version: "none",
647 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000648 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900649 }
650
651 android_app {
652 name: "AppFoo",
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 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900658
659 runtime_resource_overlay {
660 name: "rro",
661 theme: "blue",
662 }
663
markchien2f59ec92020-09-02 16:23:38 +0800664 bpf {
665 name: "bpf",
666 srcs: ["bpf.c", "bpf2.c"],
667 }
668
Ken Chenfad7f9d2021-11-10 22:02:57 +0800669 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800670 name: "netdTest",
671 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800672 sub_dir: "netd",
673 }
674
Jooyung Hanf21c7972019-12-16 22:32:06 +0900675 `)
Jooyung Hana0503a52023-08-23 13:12:50 +0900676 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900677 "etc/myetc",
678 "javalib/myjar.jar",
679 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000680 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900681 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800682 "etc/bpf/bpf.o",
683 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800684 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900685 })
686}
687
Jooyung Han01a3ee22019-11-02 02:52:25 +0900688func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800689 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900690 apex {
691 name: "myapex",
692 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000693 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900694 }
695
696 apex_key {
697 name: "myapex.key",
698 public_key: "testkey.avbpubkey",
699 private_key: "testkey.pem",
700 }
701 `)
702
Jooyung Hana0503a52023-08-23 13:12:50 +0900703 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +0900704 args := module.Rule("apexRule").Args
705 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
706 t.Error("manifest should be apex_manifest.pb, but " + manifest)
707 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900708}
709
Liz Kammer4854a7d2021-05-27 14:28:27 -0400710func TestApexManifestMinSdkVersion(t *testing.T) {
711 ctx := testApex(t, `
712 apex_defaults {
713 name: "my_defaults",
714 key: "myapex.key",
715 product_specific: true,
716 file_contexts: ":my-file-contexts",
717 updatable: false,
718 }
719 apex {
720 name: "myapex_30",
721 min_sdk_version: "30",
722 defaults: ["my_defaults"],
723 }
724
725 apex {
726 name: "myapex_current",
727 min_sdk_version: "current",
728 defaults: ["my_defaults"],
729 }
730
731 apex {
732 name: "myapex_none",
733 defaults: ["my_defaults"],
734 }
735
736 apex_key {
737 name: "myapex.key",
738 public_key: "testkey.avbpubkey",
739 private_key: "testkey.pem",
740 }
741
742 filegroup {
743 name: "my-file-contexts",
744 srcs: ["product_specific_file_contexts"],
745 }
746 `, withFiles(map[string][]byte{
747 "product_specific_file_contexts": nil,
748 }), android.FixtureModifyProductVariables(
749 func(variables android.FixtureProductVariables) {
750 variables.Unbundled_build = proptools.BoolPtr(true)
751 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
752 }), android.FixtureMergeEnv(map[string]string{
753 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
754 }))
755
756 testCases := []struct {
757 module string
758 minSdkVersion string
759 }{
760 {
761 module: "myapex_30",
762 minSdkVersion: "30",
763 },
764 {
765 module: "myapex_current",
766 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
767 },
768 {
769 module: "myapex_none",
770 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
771 },
772 }
773 for _, tc := range testCases {
Jooyung Hana0503a52023-08-23 13:12:50 +0900774 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
Liz Kammer4854a7d2021-05-27 14:28:27 -0400775 args := module.Rule("apexRule").Args
776 optFlags := args["opt_flags"]
777 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
778 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
779 }
780 }
781}
782
Jihoon Kang842b9992024-02-08 01:41:51 +0000783func TestApexWithDessertSha(t *testing.T) {
784 ctx := testApex(t, `
785 apex_defaults {
786 name: "my_defaults",
787 key: "myapex.key",
788 product_specific: true,
789 file_contexts: ":my-file-contexts",
790 updatable: false,
791 }
792 apex {
793 name: "myapex_30",
794 min_sdk_version: "30",
795 defaults: ["my_defaults"],
796 }
797
798 apex {
799 name: "myapex_current",
800 min_sdk_version: "current",
801 defaults: ["my_defaults"],
802 }
803
804 apex {
805 name: "myapex_none",
806 defaults: ["my_defaults"],
807 }
808
809 apex_key {
810 name: "myapex.key",
811 public_key: "testkey.avbpubkey",
812 private_key: "testkey.pem",
813 }
814
815 filegroup {
816 name: "my-file-contexts",
817 srcs: ["product_specific_file_contexts"],
818 }
819 `, withFiles(map[string][]byte{
820 "product_specific_file_contexts": nil,
821 }), android.FixtureModifyProductVariables(
822 func(variables android.FixtureProductVariables) {
823 variables.Unbundled_build = proptools.BoolPtr(true)
824 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
825 }), android.FixtureMergeEnv(map[string]string{
826 "UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA": "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
827 }))
828
829 testCases := []struct {
830 module string
831 minSdkVersion string
832 }{
833 {
834 module: "myapex_30",
835 minSdkVersion: "30",
836 },
837 {
838 module: "myapex_current",
839 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
840 },
841 {
842 module: "myapex_none",
843 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
844 },
845 }
846 for _, tc := range testCases {
847 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
848 args := module.Rule("apexRule").Args
849 optFlags := args["opt_flags"]
850 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
851 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
852 }
853 }
854}
855
Jooyung Hanaf730952023-02-28 14:13:38 +0900856func TestFileContexts(t *testing.T) {
Jooyung Hanbe953902023-05-31 16:42:16 +0900857 for _, vendor := range []bool{true, false} {
Jooyung Hanaf730952023-02-28 14:13:38 +0900858 prop := ""
Jooyung Hanbe953902023-05-31 16:42:16 +0900859 if vendor {
860 prop = "vendor: true,\n"
Jooyung Hanaf730952023-02-28 14:13:38 +0900861 }
862 ctx := testApex(t, `
863 apex {
864 name: "myapex",
865 key: "myapex.key",
Jooyung Hanaf730952023-02-28 14:13:38 +0900866 updatable: false,
Jooyung Hanaf730952023-02-28 14:13:38 +0900867 `+prop+`
868 }
869
870 apex_key {
871 name: "myapex.key",
872 public_key: "testkey.avbpubkey",
873 private_key: "testkey.pem",
874 }
Jooyung Hanbe953902023-05-31 16:42:16 +0900875 `)
Jooyung Hanaf730952023-02-28 14:13:38 +0900876
Jooyung Hana0503a52023-08-23 13:12:50 +0900877 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("file_contexts")
Jooyung Hanbe953902023-05-31 16:42:16 +0900878 if vendor {
879 android.AssertStringDoesContain(t, "should force-label as vendor_apex_metadata_file",
880 rule.RuleParams.Command,
881 "apex_manifest\\\\.pb u:object_r:vendor_apex_metadata_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900882 } else {
Jooyung Hanbe953902023-05-31 16:42:16 +0900883 android.AssertStringDoesContain(t, "should force-label as system_file",
884 rule.RuleParams.Command,
885 "apex_manifest\\\\.pb u:object_r:system_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900886 }
887 }
888}
889
Jiyong Park25fc6a92018-11-18 18:02:45 +0900890func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800891 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900892 apex {
893 name: "myapex",
894 key: "myapex.key",
895 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900896 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000897 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900898 }
899
900 apex_key {
901 name: "myapex.key",
902 public_key: "testkey.avbpubkey",
903 private_key: "testkey.pem",
904 }
905
906 cc_library {
907 name: "mylib",
908 srcs: ["mylib.cpp"],
909 shared_libs: ["mylib2", "mylib3"],
910 system_shared_libs: [],
911 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000912 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900913 }
914
915 cc_library {
916 name: "mylib2",
917 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900918 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900919 system_shared_libs: [],
920 stl: "none",
921 stubs: {
922 versions: ["1", "2", "3"],
923 },
924 }
925
926 cc_library {
927 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900928 srcs: ["mylib.cpp"],
929 shared_libs: ["mylib4"],
930 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900931 stl: "none",
932 stubs: {
933 versions: ["10", "11", "12"],
934 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000935 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900936 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900937
938 cc_library {
939 name: "mylib4",
940 srcs: ["mylib.cpp"],
941 system_shared_libs: [],
942 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000943 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900944 }
Jiyong Park105dc322021-06-11 17:22:09 +0900945
946 rust_binary {
947 name: "foo.rust",
948 srcs: ["foo.rs"],
949 shared_libs: ["libfoo.shared_from_rust"],
950 prefer_rlib: true,
951 apex_available: ["myapex"],
952 }
953
954 cc_library_shared {
955 name: "libfoo.shared_from_rust",
956 srcs: ["mylib.cpp"],
957 system_shared_libs: [],
958 stl: "none",
959 stubs: {
960 versions: ["10", "11", "12"],
961 },
962 }
963
Jiyong Park25fc6a92018-11-18 18:02:45 +0900964 `)
965
Jooyung Hana0503a52023-08-23 13:12:50 +0900966 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900967 copyCmds := apexRule.Args["copy_commands"]
968
969 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800970 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900971
972 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800973 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900974
975 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800976 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900977
Colin Crossaede88c2020-08-11 12:17:01 -0700978 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900979
980 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900981 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900982 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900983 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900984
985 // 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 -0700986 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900987 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700988 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900989
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700990 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
991 // is replaced by sharing of "cFlags" in cc/builder.go.
992 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
993 // module variable representing "cflags". So it was not detected by ensureNotContains.
994 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
995 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
996 // including the original cflags's "-include mylib.h".
997 //
Jiyong Park64379952018-12-13 18:37:29 +0900998 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700999 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1000 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +09001001
Jiyong Park85cc35a2022-07-17 11:30:47 +09001002 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1003 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
1004 // Ensure that genstub for apex-provided lib is invoked with --apex
1005 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +09001006
Jooyung Hana0503a52023-08-23 13:12:50 +09001007 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +09001008 "lib64/mylib.so",
1009 "lib64/mylib3.so",
1010 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +09001011 "bin/foo.rust",
1012 "lib64/libc++.so", // by the implicit dependency from foo.rust
1013 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +09001014 })
Jiyong Park105dc322021-06-11 17:22:09 +09001015
1016 // Ensure that stub dependency from a rust module is not included
1017 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1018 // The rust module is linked to the stub cc library
Colin Cross004bd3f2023-10-02 11:39:17 -07001019 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park105dc322021-06-11 17:22:09 +09001020 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1021 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +09001022
Jooyung Hana0503a52023-08-23 13:12:50 +09001023 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jiyong Park34d5c332022-02-24 18:02:44 +09001024 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001025}
1026
Jooyung Han20348752023-12-05 15:23:56 +09001027func TestApexShouldNotEmbedStubVariant(t *testing.T) {
1028 testApexError(t, `module "myapex" .*: native_shared_libs: "libbar" is a stub`, `
1029 apex {
1030 name: "myapex",
1031 key: "myapex.key",
1032 vendor: true,
1033 updatable: false,
1034 native_shared_libs: ["libbar"], // should not add an LLNDK stub in a vendor apex
1035 }
1036
1037 apex_key {
1038 name: "myapex.key",
1039 public_key: "testkey.avbpubkey",
1040 private_key: "testkey.pem",
1041 }
1042
1043 cc_library {
1044 name: "libbar",
1045 srcs: ["mylib.cpp"],
1046 llndk: {
1047 symbol_file: "libbar.map.txt",
1048 }
1049 }
1050 `)
1051}
1052
Jiyong Park1bc84122021-06-22 20:23:05 +09001053func TestApexCanUsePrivateApis(t *testing.T) {
1054 ctx := testApex(t, `
1055 apex {
1056 name: "myapex",
1057 key: "myapex.key",
1058 native_shared_libs: ["mylib"],
1059 binaries: ["foo.rust"],
1060 updatable: false,
1061 platform_apis: true,
1062 }
1063
1064 apex_key {
1065 name: "myapex.key",
1066 public_key: "testkey.avbpubkey",
1067 private_key: "testkey.pem",
1068 }
1069
1070 cc_library {
1071 name: "mylib",
1072 srcs: ["mylib.cpp"],
1073 shared_libs: ["mylib2"],
1074 system_shared_libs: [],
1075 stl: "none",
1076 apex_available: [ "myapex" ],
1077 }
1078
1079 cc_library {
1080 name: "mylib2",
1081 srcs: ["mylib.cpp"],
1082 cflags: ["-include mylib.h"],
1083 system_shared_libs: [],
1084 stl: "none",
1085 stubs: {
1086 versions: ["1", "2", "3"],
1087 },
1088 }
1089
1090 rust_binary {
1091 name: "foo.rust",
1092 srcs: ["foo.rs"],
1093 shared_libs: ["libfoo.shared_from_rust"],
1094 prefer_rlib: true,
1095 apex_available: ["myapex"],
1096 }
1097
1098 cc_library_shared {
1099 name: "libfoo.shared_from_rust",
1100 srcs: ["mylib.cpp"],
1101 system_shared_libs: [],
1102 stl: "none",
1103 stubs: {
1104 versions: ["10", "11", "12"],
1105 },
1106 }
1107 `)
1108
Jooyung Hana0503a52023-08-23 13:12:50 +09001109 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park1bc84122021-06-22 20:23:05 +09001110 copyCmds := apexRule.Args["copy_commands"]
1111
1112 // Ensure that indirect stubs dep is not included
1113 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1114 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1115
1116 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1117 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001118 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001119 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1120 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Colin Cross004bd3f2023-10-02 11:39:17 -07001121 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001122 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1123 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1124}
1125
Colin Cross7812fd32020-09-25 12:35:10 -07001126func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1127 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001128 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001129 apex {
1130 name: "myapex",
1131 key: "myapex.key",
1132 native_shared_libs: ["mylib", "mylib3"],
1133 min_sdk_version: "29",
1134 }
1135
1136 apex_key {
1137 name: "myapex.key",
1138 public_key: "testkey.avbpubkey",
1139 private_key: "testkey.pem",
1140 }
1141
1142 cc_library {
1143 name: "mylib",
1144 srcs: ["mylib.cpp"],
1145 shared_libs: ["mylib2", "mylib3"],
1146 system_shared_libs: [],
1147 stl: "none",
1148 apex_available: [ "myapex" ],
1149 min_sdk_version: "28",
1150 }
1151
1152 cc_library {
1153 name: "mylib2",
1154 srcs: ["mylib.cpp"],
1155 cflags: ["-include mylib.h"],
1156 system_shared_libs: [],
1157 stl: "none",
1158 stubs: {
1159 versions: ["28", "29", "30", "current"],
1160 },
1161 min_sdk_version: "28",
1162 }
1163
1164 cc_library {
1165 name: "mylib3",
1166 srcs: ["mylib.cpp"],
1167 shared_libs: ["mylib4"],
1168 system_shared_libs: [],
1169 stl: "none",
1170 stubs: {
1171 versions: ["28", "29", "30", "current"],
1172 },
1173 apex_available: [ "myapex" ],
1174 min_sdk_version: "28",
1175 }
1176
1177 cc_library {
1178 name: "mylib4",
1179 srcs: ["mylib.cpp"],
1180 system_shared_libs: [],
1181 stl: "none",
1182 apex_available: [ "myapex" ],
1183 min_sdk_version: "28",
1184 }
1185 `)
1186
Jooyung Hana0503a52023-08-23 13:12:50 +09001187 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Colin Cross7812fd32020-09-25 12:35:10 -07001188 copyCmds := apexRule.Args["copy_commands"]
1189
1190 // Ensure that direct non-stubs dep is always included
1191 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1192
1193 // Ensure that indirect stubs dep is not included
1194 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1195
1196 // Ensure that direct stubs dep is included
1197 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1198
1199 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1200
Jiyong Park55549df2021-02-26 23:57:23 +09001201 // Ensure that mylib is linking with the latest version of stub for mylib2
1202 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001203 // ... and not linking to the non-stub (impl) variant of mylib2
1204 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1205
1206 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1207 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1208 // .. and not linking to the stubs variant of mylib3
1209 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1210
1211 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001212 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001213 ensureNotContains(t, mylib2Cflags, "-include ")
1214
Jiyong Park85cc35a2022-07-17 11:30:47 +09001215 // Ensure that genstub is invoked with --systemapi
1216 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001217
Jooyung Hana0503a52023-08-23 13:12:50 +09001218 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Colin Cross7812fd32020-09-25 12:35:10 -07001219 "lib64/mylib.so",
1220 "lib64/mylib3.so",
1221 "lib64/mylib4.so",
1222 })
1223}
1224
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001225func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1226 t.Parallel()
1227 // myapex (Z)
1228 // mylib -----------------.
1229 // |
1230 // otherapex (29) |
1231 // libstub's versions: 29 Z current
1232 // |
1233 // <platform> |
1234 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001235 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001236 apex {
1237 name: "myapex",
1238 key: "myapex.key",
1239 native_shared_libs: ["mylib"],
1240 min_sdk_version: "Z", // non-final
1241 }
1242
1243 cc_library {
1244 name: "mylib",
1245 srcs: ["mylib.cpp"],
1246 shared_libs: ["libstub"],
1247 apex_available: ["myapex"],
1248 min_sdk_version: "Z",
1249 }
1250
1251 apex_key {
1252 name: "myapex.key",
1253 public_key: "testkey.avbpubkey",
1254 private_key: "testkey.pem",
1255 }
1256
1257 apex {
1258 name: "otherapex",
1259 key: "myapex.key",
1260 native_shared_libs: ["libstub"],
1261 min_sdk_version: "29",
1262 }
1263
1264 cc_library {
1265 name: "libstub",
1266 srcs: ["mylib.cpp"],
1267 stubs: {
1268 versions: ["29", "Z", "current"],
1269 },
1270 apex_available: ["otherapex"],
1271 min_sdk_version: "29",
1272 }
1273
1274 // platform module depending on libstub from otherapex should use the latest stub("current")
1275 cc_library {
1276 name: "libplatform",
1277 srcs: ["mylib.cpp"],
1278 shared_libs: ["libstub"],
1279 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001280 `,
1281 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1282 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1283 variables.Platform_sdk_final = proptools.BoolPtr(false)
1284 variables.Platform_version_active_codenames = []string{"Z"}
1285 }),
1286 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001287
Jiyong Park55549df2021-02-26 23:57:23 +09001288 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001289 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001290 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001291 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001292 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001293
1294 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1295 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1296 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1297 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1298 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1299}
1300
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001301func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001302 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001303 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001304 name: "myapex2",
1305 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001306 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001307 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001308 }
1309
1310 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001311 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001312 public_key: "testkey.avbpubkey",
1313 private_key: "testkey.pem",
1314 }
1315
1316 cc_library {
1317 name: "mylib",
1318 srcs: ["mylib.cpp"],
1319 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001320 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001321 system_shared_libs: [],
1322 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001323 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001324 }
1325
1326 cc_library {
1327 name: "libfoo",
1328 srcs: ["mylib.cpp"],
1329 shared_libs: ["libbar"],
1330 system_shared_libs: [],
1331 stl: "none",
1332 stubs: {
1333 versions: ["10", "20", "30"],
1334 },
1335 }
1336
1337 cc_library {
1338 name: "libbar",
1339 srcs: ["mylib.cpp"],
1340 system_shared_libs: [],
1341 stl: "none",
1342 }
1343
Jiyong Park678c8812020-02-07 17:25:49 +09001344 cc_library_static {
1345 name: "libbaz",
1346 srcs: ["mylib.cpp"],
1347 system_shared_libs: [],
1348 stl: "none",
1349 apex_available: [ "myapex2" ],
1350 }
1351
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001352 `)
1353
Jooyung Hana0503a52023-08-23 13:12:50 +09001354 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001355 copyCmds := apexRule.Args["copy_commands"]
1356
1357 // Ensure that direct non-stubs dep is always included
1358 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1359
1360 // Ensure that indirect stubs dep is not included
1361 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1362
1363 // Ensure that dependency of stubs is not included
1364 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1365
Colin Crossaede88c2020-08-11 12:17:01 -07001366 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001367
1368 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001369 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001370 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001371 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001372
Jiyong Park3ff16992019-12-27 14:11:47 +09001373 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001374
1375 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1376 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001377
Colin Crossf61d03d2023-11-02 16:56:39 -07001378 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1379 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001380 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001381
Colin Crossf61d03d2023-11-02 16:56:39 -07001382 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1383 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001384 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001385}
1386
Jooyung Hand3639552019-08-09 12:57:43 +09001387func TestApexWithRuntimeLibsDependency(t *testing.T) {
1388 /*
1389 myapex
1390 |
1391 v (runtime_libs)
1392 mylib ------+------> libfoo [provides stub]
1393 |
1394 `------> libbar
1395 */
Colin Cross1c460562021-02-16 17:55:47 -08001396 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001397 apex {
1398 name: "myapex",
1399 key: "myapex.key",
1400 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001401 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001402 }
1403
1404 apex_key {
1405 name: "myapex.key",
1406 public_key: "testkey.avbpubkey",
1407 private_key: "testkey.pem",
1408 }
1409
1410 cc_library {
1411 name: "mylib",
1412 srcs: ["mylib.cpp"],
Liz Kammer5f108fa2023-05-11 14:33:17 -04001413 static_libs: ["libstatic"],
1414 shared_libs: ["libshared"],
Jooyung Hand3639552019-08-09 12:57:43 +09001415 runtime_libs: ["libfoo", "libbar"],
1416 system_shared_libs: [],
1417 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001418 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001419 }
1420
1421 cc_library {
1422 name: "libfoo",
1423 srcs: ["mylib.cpp"],
1424 system_shared_libs: [],
1425 stl: "none",
1426 stubs: {
1427 versions: ["10", "20", "30"],
1428 },
1429 }
1430
1431 cc_library {
1432 name: "libbar",
1433 srcs: ["mylib.cpp"],
1434 system_shared_libs: [],
1435 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001436 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001437 }
1438
Liz Kammer5f108fa2023-05-11 14:33:17 -04001439 cc_library {
1440 name: "libstatic",
1441 srcs: ["mylib.cpp"],
1442 system_shared_libs: [],
1443 stl: "none",
1444 apex_available: [ "myapex" ],
1445 runtime_libs: ["libstatic_to_runtime"],
1446 }
1447
1448 cc_library {
1449 name: "libshared",
1450 srcs: ["mylib.cpp"],
1451 system_shared_libs: [],
1452 stl: "none",
1453 apex_available: [ "myapex" ],
1454 runtime_libs: ["libshared_to_runtime"],
1455 }
1456
1457 cc_library {
1458 name: "libstatic_to_runtime",
1459 srcs: ["mylib.cpp"],
1460 system_shared_libs: [],
1461 stl: "none",
1462 apex_available: [ "myapex" ],
1463 }
1464
1465 cc_library {
1466 name: "libshared_to_runtime",
1467 srcs: ["mylib.cpp"],
1468 system_shared_libs: [],
1469 stl: "none",
1470 apex_available: [ "myapex" ],
1471 }
Jooyung Hand3639552019-08-09 12:57:43 +09001472 `)
1473
Jooyung Hana0503a52023-08-23 13:12:50 +09001474 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001475 copyCmds := apexRule.Args["copy_commands"]
1476
1477 // Ensure that direct non-stubs dep is always included
1478 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1479
1480 // Ensure that indirect stubs dep is not included
1481 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1482
1483 // Ensure that runtime_libs dep in included
1484 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
Liz Kammer5f108fa2023-05-11 14:33:17 -04001485 ensureContains(t, copyCmds, "image.apex/lib64/libshared.so")
1486 ensureContains(t, copyCmds, "image.apex/lib64/libshared_to_runtime.so")
1487
1488 ensureNotContains(t, copyCmds, "image.apex/lib64/libstatic_to_runtime.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001489
Jooyung Hana0503a52023-08-23 13:12:50 +09001490 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001491 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1492 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001493}
1494
Paul Duffina02cae32021-03-09 01:44:06 +00001495var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1496 cc.PrepareForTestWithCcBuildComponents,
1497 PrepareForTestWithApexBuildComponents,
1498 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001499 apex {
1500 name: "com.android.runtime",
1501 key: "com.android.runtime.key",
1502 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001503 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001504 }
1505
1506 apex_key {
1507 name: "com.android.runtime.key",
1508 public_key: "testkey.avbpubkey",
1509 private_key: "testkey.pem",
1510 }
Paul Duffina02cae32021-03-09 01:44:06 +00001511 `),
1512 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1513)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001514
Paul Duffina02cae32021-03-09 01:44:06 +00001515func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001516 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001517 cc_library {
1518 name: "libc",
1519 no_libcrt: true,
1520 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001521 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001522 stl: "none",
1523 system_shared_libs: [],
1524 stubs: { versions: ["1"] },
1525 apex_available: ["com.android.runtime"],
1526
1527 sanitize: {
1528 hwaddress: true,
1529 }
1530 }
1531
1532 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001533 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001534 no_libcrt: true,
1535 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001536 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001537 stl: "none",
1538 system_shared_libs: [],
1539 srcs: [""],
1540 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001541 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001542
1543 sanitize: {
1544 never: true,
1545 },
Spandan Das4de7b492023-05-05 21:13:01 +00001546 apex_available: [
1547 "//apex_available:anyapex",
1548 "//apex_available:platform",
1549 ],
Paul Duffina02cae32021-03-09 01:44:06 +00001550 } `)
1551 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001552
Jooyung Hana0503a52023-08-23 13:12:50 +09001553 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001554 "lib64/bionic/libc.so",
1555 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1556 })
1557
Colin Cross4c4c1be2022-02-10 11:41:18 -08001558 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001559
1560 installed := hwasan.Description("install libclang_rt.hwasan")
1561 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1562
1563 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1564 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1565 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1566}
1567
1568func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001569 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001570 prepareForTestOfRuntimeApexWithHwasan,
1571 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1572 variables.SanitizeDevice = []string{"hwaddress"}
1573 }),
1574 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001575 cc_library {
1576 name: "libc",
1577 no_libcrt: true,
1578 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001579 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001580 stl: "none",
1581 system_shared_libs: [],
1582 stubs: { versions: ["1"] },
1583 apex_available: ["com.android.runtime"],
1584 }
1585
1586 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001587 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001588 no_libcrt: true,
1589 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001590 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001591 stl: "none",
1592 system_shared_libs: [],
1593 srcs: [""],
1594 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001595 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001596
1597 sanitize: {
1598 never: true,
1599 },
Spandan Das4de7b492023-05-05 21:13:01 +00001600 apex_available: [
1601 "//apex_available:anyapex",
1602 "//apex_available:platform",
1603 ],
Jooyung Han8ce8db92020-05-15 19:05:05 +09001604 }
Paul Duffina02cae32021-03-09 01:44:06 +00001605 `)
1606 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001607
Jooyung Hana0503a52023-08-23 13:12:50 +09001608 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001609 "lib64/bionic/libc.so",
1610 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1611 })
1612
Colin Cross4c4c1be2022-02-10 11:41:18 -08001613 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001614
1615 installed := hwasan.Description("install libclang_rt.hwasan")
1616 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1617
1618 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1619 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1620 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1621}
1622
Jooyung Han61b66e92020-03-21 14:21:46 +00001623func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1624 testcases := []struct {
1625 name string
1626 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001627 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001628 shouldLink string
1629 shouldNotLink []string
1630 }{
1631 {
Jiyong Park55549df2021-02-26 23:57:23 +09001632 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001633 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001634 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001635 shouldLink: "current",
1636 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001637 },
1638 {
Jiyong Park55549df2021-02-26 23:57:23 +09001639 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001640 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001641 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001642 shouldLink: "current",
1643 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001644 },
1645 }
1646 for _, tc := range testcases {
1647 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001648 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001649 apex {
1650 name: "myapex",
1651 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001652 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001653 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001654 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001655 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001656
Jooyung Han61b66e92020-03-21 14:21:46 +00001657 apex_key {
1658 name: "myapex.key",
1659 public_key: "testkey.avbpubkey",
1660 private_key: "testkey.pem",
1661 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001662
Jooyung Han61b66e92020-03-21 14:21:46 +00001663 cc_library {
1664 name: "mylib",
1665 srcs: ["mylib.cpp"],
1666 vendor_available: true,
1667 shared_libs: ["libbar"],
1668 system_shared_libs: [],
1669 stl: "none",
1670 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001671 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001672 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001673
Jooyung Han61b66e92020-03-21 14:21:46 +00001674 cc_library {
1675 name: "libbar",
1676 srcs: ["mylib.cpp"],
1677 system_shared_libs: [],
1678 stl: "none",
1679 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001680 llndk: {
1681 symbol_file: "libbar.map.txt",
1682 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001683 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001684 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001685 withUnbundledBuild,
1686 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001687
Jooyung Han61b66e92020-03-21 14:21:46 +00001688 // Ensure that LLNDK dep is not included
Jooyung Hana0503a52023-08-23 13:12:50 +09001689 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00001690 "lib64/mylib.so",
1691 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001692
Jooyung Han61b66e92020-03-21 14:21:46 +00001693 // Ensure that LLNDK dep is required
Jooyung Hana0503a52023-08-23 13:12:50 +09001694 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han61b66e92020-03-21 14:21:46 +00001695 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1696 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001697
Steven Moreland2c4000c2021-04-27 02:08:49 +00001698 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1699 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001700 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001701 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001702 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001703
Steven Moreland2c4000c2021-04-27 02:08:49 +00001704 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001705 ver := tc.shouldLink
1706 if tc.shouldLink == "current" {
1707 ver = strconv.Itoa(android.FutureApiLevelInt)
1708 }
1709 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001710 })
1711 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001712}
1713
Jiyong Park25fc6a92018-11-18 18:02:45 +09001714func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001715 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001716 apex {
1717 name: "myapex",
1718 key: "myapex.key",
1719 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001720 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001721 }
1722
1723 apex_key {
1724 name: "myapex.key",
1725 public_key: "testkey.avbpubkey",
1726 private_key: "testkey.pem",
1727 }
1728
1729 cc_library {
1730 name: "mylib",
1731 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001732 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001733 shared_libs: ["libdl#27"],
1734 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001735 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001736 }
1737
1738 cc_library_shared {
1739 name: "mylib_shared",
1740 srcs: ["mylib.cpp"],
1741 shared_libs: ["libdl#27"],
1742 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001743 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001744 }
1745
1746 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001747 name: "libBootstrap",
1748 srcs: ["mylib.cpp"],
1749 stl: "none",
1750 bootstrap: true,
1751 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001752 `)
1753
Jooyung Hana0503a52023-08-23 13:12:50 +09001754 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001755 copyCmds := apexRule.Args["copy_commands"]
1756
1757 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001758 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001759 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1760 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001761
1762 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001763 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001764
Colin Crossaede88c2020-08-11 12:17:01 -07001765 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1766 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1767 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001768
1769 // For dependency to libc
1770 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001771 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001772 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001773 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001774 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001775 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1776 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001777
1778 // For dependency to libm
1779 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001780 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001781 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001782 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001783 // ... and is not compiling with the stub
1784 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1785 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1786
1787 // For dependency to libdl
1788 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001789 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001790 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001791 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1792 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001793 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001794 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001795 // ... Cflags from stub is correctly exported to mylib
1796 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1797 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001798
1799 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001800 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1801 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1802 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1803 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001804}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001805
Jooyung Han749dc692020-04-15 11:03:39 +09001806func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001807 // there are three links between liba --> libz.
1808 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001809 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001810 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001811 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001812 apex {
1813 name: "myapex",
1814 key: "myapex.key",
1815 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001816 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001817 }
1818
1819 apex {
1820 name: "otherapex",
1821 key: "myapex.key",
1822 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001823 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001824 }
1825
1826 apex_key {
1827 name: "myapex.key",
1828 public_key: "testkey.avbpubkey",
1829 private_key: "testkey.pem",
1830 }
1831
1832 cc_library {
1833 name: "libx",
1834 shared_libs: ["liba"],
1835 system_shared_libs: [],
1836 stl: "none",
1837 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001838 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001839 }
1840
1841 cc_library {
1842 name: "liby",
1843 shared_libs: ["liba"],
1844 system_shared_libs: [],
1845 stl: "none",
1846 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001847 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001848 }
1849
1850 cc_library {
1851 name: "liba",
1852 shared_libs: ["libz"],
1853 system_shared_libs: [],
1854 stl: "none",
1855 apex_available: [
1856 "//apex_available:anyapex",
1857 "//apex_available:platform",
1858 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001859 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001860 }
1861
1862 cc_library {
1863 name: "libz",
1864 system_shared_libs: [],
1865 stl: "none",
1866 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001867 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001868 },
1869 }
Jooyung Han749dc692020-04-15 11:03:39 +09001870 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001871
1872 expectLink := func(from, from_variant, to, to_variant string) {
1873 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1874 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1875 }
1876 expectNoLink := func(from, from_variant, to, to_variant string) {
1877 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1878 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1879 }
1880 // platform liba is linked to non-stub version
1881 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001882 // liba in myapex is linked to current
1883 expectLink("liba", "shared_apex29", "libz", "shared_current")
1884 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001885 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001886 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001887 // liba in otherapex is linked to current
1888 expectLink("liba", "shared_apex30", "libz", "shared_current")
1889 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001890 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1891 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001892}
1893
Jooyung Hanaed150d2020-04-02 01:41:41 +09001894func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001895 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001896 apex {
1897 name: "myapex",
1898 key: "myapex.key",
1899 native_shared_libs: ["libx"],
1900 min_sdk_version: "R",
1901 }
1902
1903 apex_key {
1904 name: "myapex.key",
1905 public_key: "testkey.avbpubkey",
1906 private_key: "testkey.pem",
1907 }
1908
1909 cc_library {
1910 name: "libx",
1911 shared_libs: ["libz"],
1912 system_shared_libs: [],
1913 stl: "none",
1914 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001915 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001916 }
1917
1918 cc_library {
1919 name: "libz",
1920 system_shared_libs: [],
1921 stl: "none",
1922 stubs: {
1923 versions: ["29", "R"],
1924 },
1925 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001926 `,
1927 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1928 variables.Platform_version_active_codenames = []string{"R"}
1929 }),
1930 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001931
1932 expectLink := func(from, from_variant, to, to_variant string) {
1933 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1934 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1935 }
1936 expectNoLink := func(from, from_variant, to, to_variant string) {
1937 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1938 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1939 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001940 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1941 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001942 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1943 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001944}
1945
Jooyung Han4c4da062021-06-23 10:23:16 +09001946func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1947 testApex(t, `
1948 apex {
1949 name: "myapex",
1950 key: "myapex.key",
1951 java_libs: ["libx"],
1952 min_sdk_version: "S",
1953 }
1954
1955 apex_key {
1956 name: "myapex.key",
1957 public_key: "testkey.avbpubkey",
1958 private_key: "testkey.pem",
1959 }
1960
1961 java_library {
1962 name: "libx",
1963 srcs: ["a.java"],
1964 apex_available: [ "myapex" ],
1965 sdk_version: "current",
1966 min_sdk_version: "S", // should be okay
1967 }
1968 `,
1969 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1970 variables.Platform_version_active_codenames = []string{"S"}
1971 variables.Platform_sdk_codename = proptools.StringPtr("S")
1972 }),
1973 )
1974}
1975
Jooyung Han749dc692020-04-15 11:03:39 +09001976func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001977 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001978 apex {
1979 name: "myapex",
1980 key: "myapex.key",
1981 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001982 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001983 }
1984
1985 apex_key {
1986 name: "myapex.key",
1987 public_key: "testkey.avbpubkey",
1988 private_key: "testkey.pem",
1989 }
1990
1991 cc_library {
1992 name: "libx",
1993 shared_libs: ["libz"],
1994 system_shared_libs: [],
1995 stl: "none",
1996 apex_available: [ "myapex" ],
1997 }
1998
1999 cc_library {
2000 name: "libz",
2001 system_shared_libs: [],
2002 stl: "none",
2003 stubs: {
2004 versions: ["1", "2"],
2005 },
2006 }
2007 `)
2008
2009 expectLink := func(from, from_variant, to, to_variant string) {
2010 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2011 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2012 }
2013 expectNoLink := func(from, from_variant, to, to_variant string) {
2014 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2015 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2016 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002017 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002018 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002019 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07002020 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09002021}
2022
Jooyung Handfc864c2023-03-20 18:19:07 +09002023func TestApexMinSdkVersion_InVendorApex(t *testing.T) {
Jiyong Park5df7bd32021-08-25 16:18:46 +09002024 ctx := testApex(t, `
2025 apex {
2026 name: "myapex",
2027 key: "myapex.key",
2028 native_shared_libs: ["mylib"],
Jooyung Handfc864c2023-03-20 18:19:07 +09002029 updatable: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002030 vendor: true,
2031 min_sdk_version: "29",
2032 }
2033
2034 apex_key {
2035 name: "myapex.key",
2036 public_key: "testkey.avbpubkey",
2037 private_key: "testkey.pem",
2038 }
2039
2040 cc_library {
2041 name: "mylib",
Jooyung Handfc864c2023-03-20 18:19:07 +09002042 srcs: ["mylib.cpp"],
Jiyong Park5df7bd32021-08-25 16:18:46 +09002043 vendor_available: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002044 min_sdk_version: "29",
Jooyung Handfc864c2023-03-20 18:19:07 +09002045 shared_libs: ["libbar"],
2046 }
2047
2048 cc_library {
2049 name: "libbar",
2050 stubs: { versions: ["29", "30"] },
2051 llndk: { symbol_file: "libbar.map.txt" },
Jiyong Park5df7bd32021-08-25 16:18:46 +09002052 }
2053 `)
2054
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002055 vendorVariant := "android_vendor_arm64_armv8-a"
Jiyong Park5df7bd32021-08-25 16:18:46 +09002056
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002057 mylib := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29")
Jooyung Handfc864c2023-03-20 18:19:07 +09002058
2059 // Ensure that mylib links with "current" LLNDK
2060 libFlags := names(mylib.Rule("ld").Args["libFlags"])
Jooyung Han5e8994e2024-03-12 14:12:12 +09002061 ensureListContains(t, libFlags, "out/soong/.intermediates/libbar/"+vendorVariant+"_shared/libbar.so")
Jooyung Handfc864c2023-03-20 18:19:07 +09002062
2063 // Ensure that mylib is targeting 29
2064 ccRule := ctx.ModuleForTests("mylib", vendorVariant+"_static_apex29").Output("obj/mylib.o")
2065 ensureContains(t, ccRule.Args["cFlags"], "-target aarch64-linux-android29")
2066
2067 // Ensure that the correct variant of crtbegin_so is used.
2068 crtBegin := mylib.Rule("ld").Args["crtBegin"]
2069 ensureContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
Jiyong Park5df7bd32021-08-25 16:18:46 +09002070
2071 // Ensure that the crtbegin_so used by the APEX is targeting 29
2072 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
2073 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
2074}
2075
Jooyung Han4495f842023-04-25 16:39:59 +09002076func TestTrackAllowedDeps(t *testing.T) {
2077 ctx := testApex(t, `
2078 apex {
2079 name: "myapex",
2080 key: "myapex.key",
2081 updatable: true,
2082 native_shared_libs: [
2083 "mylib",
2084 "yourlib",
2085 ],
2086 min_sdk_version: "29",
2087 }
2088
2089 apex {
2090 name: "myapex2",
2091 key: "myapex.key",
2092 updatable: false,
2093 native_shared_libs: ["yourlib"],
2094 }
2095
2096 apex_key {
2097 name: "myapex.key",
2098 public_key: "testkey.avbpubkey",
2099 private_key: "testkey.pem",
2100 }
2101
2102 cc_library {
2103 name: "mylib",
2104 srcs: ["mylib.cpp"],
2105 shared_libs: ["libbar"],
2106 min_sdk_version: "29",
2107 apex_available: ["myapex"],
2108 }
2109
2110 cc_library {
2111 name: "libbar",
2112 stubs: { versions: ["29", "30"] },
2113 }
2114
2115 cc_library {
2116 name: "yourlib",
2117 srcs: ["mylib.cpp"],
2118 min_sdk_version: "29",
2119 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2120 }
2121 `, withFiles(android.MockFS{
2122 "packages/modules/common/build/allowed_deps.txt": nil,
2123 }))
2124
2125 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2126 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2127 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002128 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002129 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002130 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002131
Jooyung Hana0503a52023-08-23 13:12:50 +09002132 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07002133 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2134 myapex.Output("depsinfo/flatlist.txt")), "\n")
Jooyung Han4495f842023-04-25 16:39:59 +09002135 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2136 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2137 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2138 flatlist, "mylib:(minSdkVersion:29)")
2139 android.AssertStringListContains(t, "track platform-available lib",
2140 flatlist, "yourlib(minSdkVersion:29)")
2141}
2142
2143func TestTrackAllowedDeps_SkipWithoutAllowedDepsTxt(t *testing.T) {
2144 ctx := testApex(t, `
2145 apex {
2146 name: "myapex",
2147 key: "myapex.key",
2148 updatable: true,
2149 min_sdk_version: "29",
2150 }
2151
2152 apex_key {
2153 name: "myapex.key",
2154 public_key: "testkey.avbpubkey",
2155 private_key: "testkey.pem",
2156 }
2157 `)
2158 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2159 if nil != depsinfo.MaybeRule("generateApexDepsInfoFilesRule").Output {
2160 t.Error("apex_depsinfo_singleton shouldn't run when allowed_deps.txt doesn't exist")
2161 }
2162}
2163
Jooyung Han03b51852020-02-26 22:45:42 +09002164func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002165 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002166 apex {
2167 name: "myapex",
2168 key: "myapex.key",
2169 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002170 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002171 }
2172
2173 apex_key {
2174 name: "myapex.key",
2175 public_key: "testkey.avbpubkey",
2176 private_key: "testkey.pem",
2177 }
2178
2179 cc_library {
2180 name: "libx",
2181 system_shared_libs: [],
2182 stl: "none",
2183 apex_available: [ "myapex" ],
2184 stubs: {
2185 versions: ["1", "2"],
2186 },
2187 }
2188
2189 cc_library {
2190 name: "libz",
2191 shared_libs: ["libx"],
2192 system_shared_libs: [],
2193 stl: "none",
2194 }
2195 `)
2196
2197 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002198 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002199 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2200 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2201 }
2202 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002203 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002204 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2205 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2206 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002207 expectLink("libz", "shared", "libx", "shared_current")
2208 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002209 expectNoLink("libz", "shared", "libz", "shared_1")
2210 expectNoLink("libz", "shared", "libz", "shared")
2211}
2212
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002213var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2214 func(variables android.FixtureProductVariables) {
2215 variables.SanitizeDevice = []string{"hwaddress"}
2216 },
2217)
2218
Jooyung Han75568392020-03-20 04:29:24 +09002219func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002220 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002221 apex {
2222 name: "myapex",
2223 key: "myapex.key",
2224 native_shared_libs: ["libx"],
2225 min_sdk_version: "29",
2226 }
2227
2228 apex_key {
2229 name: "myapex.key",
2230 public_key: "testkey.avbpubkey",
2231 private_key: "testkey.pem",
2232 }
2233
2234 cc_library {
2235 name: "libx",
2236 shared_libs: ["libbar"],
2237 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002238 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002239 }
2240
2241 cc_library {
2242 name: "libbar",
2243 stubs: {
2244 versions: ["29", "30"],
2245 },
2246 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002247 `,
2248 prepareForTestWithSantitizeHwaddress,
2249 )
Jooyung Han03b51852020-02-26 22:45:42 +09002250 expectLink := func(from, from_variant, to, to_variant string) {
2251 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2252 libFlags := ld.Args["libFlags"]
2253 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2254 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002255 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002256}
2257
Jooyung Han75568392020-03-20 04:29:24 +09002258func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002259 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002260 apex {
2261 name: "myapex",
2262 key: "myapex.key",
2263 native_shared_libs: ["libx"],
2264 min_sdk_version: "29",
2265 }
2266
2267 apex_key {
2268 name: "myapex.key",
2269 public_key: "testkey.avbpubkey",
2270 private_key: "testkey.pem",
2271 }
2272
2273 cc_library {
2274 name: "libx",
2275 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002276 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002277 }
Jooyung Han75568392020-03-20 04:29:24 +09002278 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002279
2280 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002281 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002282 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002283 // note that platform variant is not.
2284 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002285 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002286}
2287
Jooyung Han749dc692020-04-15 11:03:39 +09002288func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2289 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002290 apex {
2291 name: "myapex",
2292 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002293 native_shared_libs: ["mylib"],
2294 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002295 }
2296
2297 apex_key {
2298 name: "myapex.key",
2299 public_key: "testkey.avbpubkey",
2300 private_key: "testkey.pem",
2301 }
Jooyung Han749dc692020-04-15 11:03:39 +09002302
2303 cc_library {
2304 name: "mylib",
2305 srcs: ["mylib.cpp"],
2306 system_shared_libs: [],
2307 stl: "none",
2308 apex_available: [
2309 "myapex",
2310 ],
2311 min_sdk_version: "30",
2312 }
2313 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002314
2315 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2316 apex {
2317 name: "myapex",
2318 key: "myapex.key",
2319 native_shared_libs: ["libfoo.ffi"],
2320 min_sdk_version: "29",
2321 }
2322
2323 apex_key {
2324 name: "myapex.key",
2325 public_key: "testkey.avbpubkey",
2326 private_key: "testkey.pem",
2327 }
2328
2329 rust_ffi_shared {
2330 name: "libfoo.ffi",
2331 srcs: ["foo.rs"],
2332 crate_name: "foo",
2333 apex_available: [
2334 "myapex",
2335 ],
2336 min_sdk_version: "30",
2337 }
2338 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002339
2340 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2341 apex {
2342 name: "myapex",
2343 key: "myapex.key",
2344 java_libs: ["libfoo"],
2345 min_sdk_version: "29",
2346 }
2347
2348 apex_key {
2349 name: "myapex.key",
2350 public_key: "testkey.avbpubkey",
2351 private_key: "testkey.pem",
2352 }
2353
2354 java_import {
2355 name: "libfoo",
2356 jars: ["libfoo.jar"],
2357 apex_available: [
2358 "myapex",
2359 ],
2360 min_sdk_version: "30",
2361 }
2362 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002363
2364 // Skip check for modules compiling against core API surface
2365 testApex(t, `
2366 apex {
2367 name: "myapex",
2368 key: "myapex.key",
2369 java_libs: ["libfoo"],
2370 min_sdk_version: "29",
2371 }
2372
2373 apex_key {
2374 name: "myapex.key",
2375 public_key: "testkey.avbpubkey",
2376 private_key: "testkey.pem",
2377 }
2378
2379 java_library {
2380 name: "libfoo",
2381 srcs: ["Foo.java"],
2382 apex_available: [
2383 "myapex",
2384 ],
2385 // Compile against core API surface
2386 sdk_version: "core_current",
2387 min_sdk_version: "30",
2388 }
2389 `)
2390
Jooyung Han749dc692020-04-15 11:03:39 +09002391}
2392
2393func TestApexMinSdkVersion_Okay(t *testing.T) {
2394 testApex(t, `
2395 apex {
2396 name: "myapex",
2397 key: "myapex.key",
2398 native_shared_libs: ["libfoo"],
2399 java_libs: ["libbar"],
2400 min_sdk_version: "29",
2401 }
2402
2403 apex_key {
2404 name: "myapex.key",
2405 public_key: "testkey.avbpubkey",
2406 private_key: "testkey.pem",
2407 }
2408
2409 cc_library {
2410 name: "libfoo",
2411 srcs: ["mylib.cpp"],
2412 shared_libs: ["libfoo_dep"],
2413 apex_available: ["myapex"],
2414 min_sdk_version: "29",
2415 }
2416
2417 cc_library {
2418 name: "libfoo_dep",
2419 srcs: ["mylib.cpp"],
2420 apex_available: ["myapex"],
2421 min_sdk_version: "29",
2422 }
2423
2424 java_library {
2425 name: "libbar",
2426 sdk_version: "current",
2427 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002428 static_libs: [
2429 "libbar_dep",
2430 "libbar_import_dep",
2431 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002432 apex_available: ["myapex"],
2433 min_sdk_version: "29",
2434 }
2435
2436 java_library {
2437 name: "libbar_dep",
2438 sdk_version: "current",
2439 srcs: ["a.java"],
2440 apex_available: ["myapex"],
2441 min_sdk_version: "29",
2442 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002443
2444 java_import {
2445 name: "libbar_import_dep",
2446 jars: ["libbar.jar"],
2447 apex_available: ["myapex"],
2448 min_sdk_version: "29",
2449 }
Jooyung Han03b51852020-02-26 22:45:42 +09002450 `)
2451}
2452
Colin Cross8ca61c12022-10-06 21:00:14 -07002453func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2454 // Tests that an apex dependency with min_sdk_version higher than the
2455 // min_sdk_version of the apex is allowed as long as the dependency's
2456 // min_sdk_version is less than or equal to the api level that the
2457 // architecture was introduced in. In this case, arm64 didn't exist
2458 // until api level 21, so the arm64 code will never need to run on
2459 // an api level 20 device, even if other architectures of the apex
2460 // will.
2461 testApex(t, `
2462 apex {
2463 name: "myapex",
2464 key: "myapex.key",
2465 native_shared_libs: ["libfoo"],
2466 min_sdk_version: "20",
2467 }
2468
2469 apex_key {
2470 name: "myapex.key",
2471 public_key: "testkey.avbpubkey",
2472 private_key: "testkey.pem",
2473 }
2474
2475 cc_library {
2476 name: "libfoo",
2477 srcs: ["mylib.cpp"],
2478 apex_available: ["myapex"],
2479 min_sdk_version: "21",
2480 stl: "none",
2481 }
2482 `)
2483}
2484
Artur Satayev8cf899a2020-04-15 17:29:42 +01002485func TestJavaStableSdkVersion(t *testing.T) {
2486 testCases := []struct {
2487 name string
2488 expectedError string
2489 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002490 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002491 }{
2492 {
2493 name: "Non-updatable apex with non-stable dep",
2494 bp: `
2495 apex {
2496 name: "myapex",
2497 java_libs: ["myjar"],
2498 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002499 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002500 }
2501 apex_key {
2502 name: "myapex.key",
2503 public_key: "testkey.avbpubkey",
2504 private_key: "testkey.pem",
2505 }
2506 java_library {
2507 name: "myjar",
2508 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002509 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002510 apex_available: ["myapex"],
2511 }
2512 `,
2513 },
2514 {
2515 name: "Updatable apex with stable dep",
2516 bp: `
2517 apex {
2518 name: "myapex",
2519 java_libs: ["myjar"],
2520 key: "myapex.key",
2521 updatable: true,
2522 min_sdk_version: "29",
2523 }
2524 apex_key {
2525 name: "myapex.key",
2526 public_key: "testkey.avbpubkey",
2527 private_key: "testkey.pem",
2528 }
2529 java_library {
2530 name: "myjar",
2531 srcs: ["foo/bar/MyClass.java"],
2532 sdk_version: "current",
2533 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002534 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002535 }
2536 `,
2537 },
2538 {
2539 name: "Updatable apex with non-stable dep",
2540 expectedError: "cannot depend on \"myjar\"",
2541 bp: `
2542 apex {
2543 name: "myapex",
2544 java_libs: ["myjar"],
2545 key: "myapex.key",
2546 updatable: true,
2547 }
2548 apex_key {
2549 name: "myapex.key",
2550 public_key: "testkey.avbpubkey",
2551 private_key: "testkey.pem",
2552 }
2553 java_library {
2554 name: "myjar",
2555 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002556 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002557 apex_available: ["myapex"],
2558 }
2559 `,
2560 },
2561 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002562 name: "Updatable apex with non-stable legacy core platform dep",
2563 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2564 bp: `
2565 apex {
2566 name: "myapex",
2567 java_libs: ["myjar-uses-legacy"],
2568 key: "myapex.key",
2569 updatable: true,
2570 }
2571 apex_key {
2572 name: "myapex.key",
2573 public_key: "testkey.avbpubkey",
2574 private_key: "testkey.pem",
2575 }
2576 java_library {
2577 name: "myjar-uses-legacy",
2578 srcs: ["foo/bar/MyClass.java"],
2579 sdk_version: "core_platform",
2580 apex_available: ["myapex"],
2581 }
2582 `,
2583 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2584 },
2585 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002586 name: "Updatable apex with non-stable transitive dep",
2587 // This is not actually detecting that the transitive dependency is unstable, rather it is
2588 // detecting that the transitive dependency is building against a wider API surface than the
2589 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002590 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002591 bp: `
2592 apex {
2593 name: "myapex",
2594 java_libs: ["myjar"],
2595 key: "myapex.key",
2596 updatable: true,
2597 }
2598 apex_key {
2599 name: "myapex.key",
2600 public_key: "testkey.avbpubkey",
2601 private_key: "testkey.pem",
2602 }
2603 java_library {
2604 name: "myjar",
2605 srcs: ["foo/bar/MyClass.java"],
2606 sdk_version: "current",
2607 apex_available: ["myapex"],
2608 static_libs: ["transitive-jar"],
2609 }
2610 java_library {
2611 name: "transitive-jar",
2612 srcs: ["foo/bar/MyClass.java"],
2613 sdk_version: "core_platform",
2614 apex_available: ["myapex"],
2615 }
2616 `,
2617 },
2618 }
2619
2620 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002621 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2622 continue
2623 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002624 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002625 errorHandler := android.FixtureExpectsNoErrors
2626 if test.expectedError != "" {
2627 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002628 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002629 android.GroupFixturePreparers(
2630 java.PrepareForTestWithJavaDefaultModules,
2631 PrepareForTestWithApexBuildComponents,
2632 prepareForTestWithMyapex,
2633 android.OptionalFixturePreparer(test.preparer),
2634 ).
2635 ExtendWithErrorHandler(errorHandler).
2636 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002637 })
2638 }
2639}
2640
Jooyung Han749dc692020-04-15 11:03:39 +09002641func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2642 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2643 apex {
2644 name: "myapex",
2645 key: "myapex.key",
2646 native_shared_libs: ["mylib"],
2647 min_sdk_version: "29",
2648 }
2649
2650 apex_key {
2651 name: "myapex.key",
2652 public_key: "testkey.avbpubkey",
2653 private_key: "testkey.pem",
2654 }
2655
2656 cc_library {
2657 name: "mylib",
2658 srcs: ["mylib.cpp"],
2659 shared_libs: ["mylib2"],
2660 system_shared_libs: [],
2661 stl: "none",
2662 apex_available: [
2663 "myapex",
2664 ],
2665 min_sdk_version: "29",
2666 }
2667
2668 // indirect part of the apex
2669 cc_library {
2670 name: "mylib2",
2671 srcs: ["mylib.cpp"],
2672 system_shared_libs: [],
2673 stl: "none",
2674 apex_available: [
2675 "myapex",
2676 ],
2677 min_sdk_version: "30",
2678 }
2679 `)
2680}
2681
2682func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2683 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2684 apex {
2685 name: "myapex",
2686 key: "myapex.key",
2687 apps: ["AppFoo"],
2688 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002689 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002690 }
2691
2692 apex_key {
2693 name: "myapex.key",
2694 public_key: "testkey.avbpubkey",
2695 private_key: "testkey.pem",
2696 }
2697
2698 android_app {
2699 name: "AppFoo",
2700 srcs: ["foo/bar/MyClass.java"],
2701 sdk_version: "current",
2702 min_sdk_version: "29",
2703 system_modules: "none",
2704 stl: "none",
2705 static_libs: ["bar"],
2706 apex_available: [ "myapex" ],
2707 }
2708
2709 java_library {
2710 name: "bar",
2711 sdk_version: "current",
2712 srcs: ["a.java"],
2713 apex_available: [ "myapex" ],
2714 }
2715 `)
2716}
2717
2718func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002719 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002720 apex {
2721 name: "myapex",
2722 key: "myapex.key",
2723 native_shared_libs: ["mylib"],
2724 min_sdk_version: "29",
2725 }
2726
2727 apex_key {
2728 name: "myapex.key",
2729 public_key: "testkey.avbpubkey",
2730 private_key: "testkey.pem",
2731 }
2732
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002733 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002734 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2735 cc_library {
2736 name: "mylib",
2737 srcs: ["mylib.cpp"],
2738 shared_libs: ["mylib2"],
2739 system_shared_libs: [],
2740 stl: "none",
2741 apex_available: ["myapex", "otherapex"],
2742 min_sdk_version: "29",
2743 }
2744
2745 cc_library {
2746 name: "mylib2",
2747 srcs: ["mylib.cpp"],
2748 system_shared_libs: [],
2749 stl: "none",
2750 apex_available: ["otherapex"],
2751 stubs: { versions: ["29", "30"] },
2752 min_sdk_version: "30",
2753 }
2754
2755 apex {
2756 name: "otherapex",
2757 key: "myapex.key",
2758 native_shared_libs: ["mylib", "mylib2"],
2759 min_sdk_version: "30",
2760 }
2761 `)
2762 expectLink := func(from, from_variant, to, to_variant string) {
2763 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2764 libFlags := ld.Args["libFlags"]
2765 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2766 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002767 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002768 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002769}
2770
Jooyung Haned124c32021-01-26 11:43:46 +09002771func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002772 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2773 func(variables android.FixtureProductVariables) {
2774 variables.Platform_sdk_codename = proptools.StringPtr("S")
2775 variables.Platform_version_active_codenames = []string{"S"}
2776 },
2777 )
Jooyung Haned124c32021-01-26 11:43:46 +09002778 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2779 apex {
2780 name: "myapex",
2781 key: "myapex.key",
2782 native_shared_libs: ["libfoo"],
2783 min_sdk_version: "S",
2784 }
2785 apex_key {
2786 name: "myapex.key",
2787 public_key: "testkey.avbpubkey",
2788 private_key: "testkey.pem",
2789 }
2790 cc_library {
2791 name: "libfoo",
2792 shared_libs: ["libbar"],
2793 apex_available: ["myapex"],
2794 min_sdk_version: "29",
2795 }
2796 cc_library {
2797 name: "libbar",
2798 apex_available: ["myapex"],
2799 }
2800 `, withSAsActiveCodeNames)
2801}
2802
2803func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002804 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2805 variables.Platform_sdk_codename = proptools.StringPtr("S")
2806 variables.Platform_version_active_codenames = []string{"S", "T"}
2807 })
Colin Cross1c460562021-02-16 17:55:47 -08002808 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002809 apex {
2810 name: "myapex",
2811 key: "myapex.key",
2812 native_shared_libs: ["libfoo"],
2813 min_sdk_version: "S",
2814 }
2815 apex_key {
2816 name: "myapex.key",
2817 public_key: "testkey.avbpubkey",
2818 private_key: "testkey.pem",
2819 }
2820 cc_library {
2821 name: "libfoo",
2822 shared_libs: ["libbar"],
2823 apex_available: ["myapex"],
2824 min_sdk_version: "S",
2825 }
2826 cc_library {
2827 name: "libbar",
2828 stubs: {
2829 symbol_file: "libbar.map.txt",
2830 versions: ["30", "S", "T"],
2831 },
2832 }
2833 `, withSAsActiveCodeNames)
2834
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002835 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002836 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2837 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002838 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002839}
2840
Jiyong Park7c2ee712018-12-07 00:42:25 +09002841func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002842 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002843 apex {
2844 name: "myapex",
2845 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002846 native_shared_libs: ["mylib"],
Jooyung Han4ed512b2023-08-11 16:30:04 +09002847 binaries: ["mybin", "mybin.rust"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002848 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002849 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002850 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002851 }
2852
2853 apex_key {
2854 name: "myapex.key",
2855 public_key: "testkey.avbpubkey",
2856 private_key: "testkey.pem",
2857 }
2858
2859 prebuilt_etc {
2860 name: "myetc",
2861 src: "myprebuilt",
2862 sub_dir: "foo/bar",
2863 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002864
2865 cc_library {
2866 name: "mylib",
2867 srcs: ["mylib.cpp"],
2868 relative_install_path: "foo/bar",
2869 system_shared_libs: [],
2870 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002871 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002872 }
2873
2874 cc_binary {
2875 name: "mybin",
2876 srcs: ["mylib.cpp"],
2877 relative_install_path: "foo/bar",
2878 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002879 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002880 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002881 }
Jooyung Han4ed512b2023-08-11 16:30:04 +09002882
2883 rust_binary {
2884 name: "mybin.rust",
2885 srcs: ["foo.rs"],
2886 relative_install_path: "rust_subdir",
2887 apex_available: [ "myapex" ],
2888 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002889 `)
2890
Jooyung Hana0503a52023-08-23 13:12:50 +09002891 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002892 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002893
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002894 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002895 ensureContains(t, cmd, "/etc ")
2896 ensureContains(t, cmd, "/etc/foo ")
2897 ensureContains(t, cmd, "/etc/foo/bar ")
2898 ensureContains(t, cmd, "/lib64 ")
2899 ensureContains(t, cmd, "/lib64/foo ")
2900 ensureContains(t, cmd, "/lib64/foo/bar ")
2901 ensureContains(t, cmd, "/lib ")
2902 ensureContains(t, cmd, "/lib/foo ")
2903 ensureContains(t, cmd, "/lib/foo/bar ")
2904 ensureContains(t, cmd, "/bin ")
2905 ensureContains(t, cmd, "/bin/foo ")
2906 ensureContains(t, cmd, "/bin/foo/bar ")
Jooyung Han4ed512b2023-08-11 16:30:04 +09002907 ensureContains(t, cmd, "/bin/rust_subdir ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002908}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002909
Jooyung Han35155c42020-02-06 17:33:20 +09002910func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002911 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002912 apex {
2913 name: "myapex",
2914 key: "myapex.key",
2915 multilib: {
2916 both: {
2917 native_shared_libs: ["mylib"],
2918 binaries: ["mybin"],
2919 },
2920 },
2921 compile_multilib: "both",
2922 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002923 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002924 }
2925
2926 apex_key {
2927 name: "myapex.key",
2928 public_key: "testkey.avbpubkey",
2929 private_key: "testkey.pem",
2930 }
2931
2932 cc_library {
2933 name: "mylib",
2934 relative_install_path: "foo/bar",
2935 system_shared_libs: [],
2936 stl: "none",
2937 apex_available: [ "myapex" ],
2938 native_bridge_supported: true,
2939 }
2940
2941 cc_binary {
2942 name: "mybin",
2943 relative_install_path: "foo/bar",
2944 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002945 stl: "none",
2946 apex_available: [ "myapex" ],
2947 native_bridge_supported: true,
2948 compile_multilib: "both", // default is "first" for binary
2949 multilib: {
2950 lib64: {
2951 suffix: "64",
2952 },
2953 },
2954 }
2955 `, withNativeBridgeEnabled)
Jooyung Hana0503a52023-08-23 13:12:50 +09002956 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han35155c42020-02-06 17:33:20 +09002957 "bin/foo/bar/mybin",
2958 "bin/foo/bar/mybin64",
2959 "bin/arm/foo/bar/mybin",
2960 "bin/arm64/foo/bar/mybin64",
2961 "lib/foo/bar/mylib.so",
2962 "lib/arm/foo/bar/mylib.so",
2963 "lib64/foo/bar/mylib.so",
2964 "lib64/arm64/foo/bar/mylib.so",
2965 })
2966}
2967
Jooyung Han85d61762020-06-24 23:50:26 +09002968func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002969 result := android.GroupFixturePreparers(
2970 prepareForApexTest,
2971 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2972 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002973 apex {
2974 name: "myapex",
2975 key: "myapex.key",
2976 binaries: ["mybin"],
2977 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002978 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002979 }
2980 apex_key {
2981 name: "myapex.key",
2982 public_key: "testkey.avbpubkey",
2983 private_key: "testkey.pem",
2984 }
2985 cc_binary {
2986 name: "mybin",
2987 vendor: true,
2988 shared_libs: ["libfoo"],
2989 }
2990 cc_library {
2991 name: "libfoo",
2992 proprietary: true,
2993 }
2994 `)
2995
Jooyung Hana0503a52023-08-23 13:12:50 +09002996 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002997 "bin/mybin",
2998 "lib64/libfoo.so",
2999 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
3000 "lib64/libc++.so",
3001 })
3002
Jooyung Hana0503a52023-08-23 13:12:50 +09003003 apexBundle := result.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossc68db4b2021-11-11 18:59:15 -08003004 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09003005 name := apexBundle.BaseModuleName()
3006 prefix := "TARGET_"
3007 var builder strings.Builder
3008 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08003009 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01003010 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01003011 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003012
Jooyung Hana0503a52023-08-23 13:12:50 +09003013 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003014 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
3015 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09003016}
3017
Justin Yun13decfb2021-03-08 19:25:55 +09003018func TestProductVariant(t *testing.T) {
3019 ctx := testApex(t, `
3020 apex {
3021 name: "myapex",
3022 key: "myapex.key",
3023 updatable: false,
3024 product_specific: true,
3025 binaries: ["foo"],
3026 }
3027
3028 apex_key {
3029 name: "myapex.key",
3030 public_key: "testkey.avbpubkey",
3031 private_key: "testkey.pem",
3032 }
3033
3034 cc_binary {
3035 name: "foo",
3036 product_available: true,
3037 apex_available: ["myapex"],
3038 srcs: ["foo.cpp"],
3039 }
Justin Yunaf1fde42023-09-27 16:22:10 +09003040 `)
Justin Yun13decfb2021-03-08 19:25:55 +09003041
3042 cflags := strings.Fields(
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003043 ctx.ModuleForTests("foo", "android_product_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09003044 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
3045 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
3046 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
3047 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
3048}
3049
Jooyung Han8e5685d2020-09-21 11:02:57 +09003050func TestApex_withPrebuiltFirmware(t *testing.T) {
3051 testCases := []struct {
3052 name string
3053 additionalProp string
3054 }{
3055 {"system apex with prebuilt_firmware", ""},
3056 {"vendor apex with prebuilt_firmware", "vendor: true,"},
3057 }
3058 for _, tc := range testCases {
3059 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003060 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09003061 apex {
3062 name: "myapex",
3063 key: "myapex.key",
3064 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003065 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09003066 `+tc.additionalProp+`
3067 }
3068 apex_key {
3069 name: "myapex.key",
3070 public_key: "testkey.avbpubkey",
3071 private_key: "testkey.pem",
3072 }
3073 prebuilt_firmware {
3074 name: "myfirmware",
3075 src: "myfirmware.bin",
3076 filename_from_src: true,
3077 `+tc.additionalProp+`
3078 }
3079 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09003080 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han8e5685d2020-09-21 11:02:57 +09003081 "etc/firmware/myfirmware.bin",
3082 })
3083 })
3084 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003085}
3086
Jooyung Hanefb184e2020-06-25 17:14:25 +09003087func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003088 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003089 apex {
3090 name: "myapex",
3091 key: "myapex.key",
3092 vendor: true,
3093 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003094 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003095 }
3096
3097 apex_key {
3098 name: "myapex.key",
3099 public_key: "testkey.avbpubkey",
3100 private_key: "testkey.pem",
3101 }
3102
3103 cc_library {
3104 name: "mylib",
3105 vendor_available: true,
3106 }
3107 `)
3108
Jooyung Hana0503a52023-08-23 13:12:50 +09003109 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003110 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003111 name := apexBundle.BaseModuleName()
3112 prefix := "TARGET_"
3113 var builder strings.Builder
3114 data.Custom(&builder, name, prefix, "", data)
3115 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09003116 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 +09003117}
3118
Jooyung Han2ed99d02020-06-24 23:26:26 +09003119func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003120 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003121 apex {
3122 name: "myapex",
3123 key: "myapex.key",
3124 vintf_fragments: ["fragment.xml"],
3125 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003126 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003127 }
3128 apex_key {
3129 name: "myapex.key",
3130 public_key: "testkey.avbpubkey",
3131 private_key: "testkey.pem",
3132 }
3133 cc_binary {
3134 name: "mybin",
3135 }
3136 `)
3137
Jooyung Hana0503a52023-08-23 13:12:50 +09003138 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003139 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003140 name := apexBundle.BaseModuleName()
3141 prefix := "TARGET_"
3142 var builder strings.Builder
3143 data.Custom(&builder, name, prefix, "", data)
3144 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003145 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003146 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003147}
3148
Jiyong Park16e91a02018-12-20 18:18:08 +09003149func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003150 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003151 apex {
3152 name: "myapex",
3153 key: "myapex.key",
3154 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003155 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003156 }
3157
3158 apex_key {
3159 name: "myapex.key",
3160 public_key: "testkey.avbpubkey",
3161 private_key: "testkey.pem",
3162 }
3163
3164 cc_library {
3165 name: "mylib",
3166 srcs: ["mylib.cpp"],
3167 system_shared_libs: [],
3168 stl: "none",
3169 stubs: {
3170 versions: ["1", "2", "3"],
3171 },
Spandan Das20fce2d2023-04-12 17:21:39 +00003172 apex_available: ["myapex"],
Jiyong Park16e91a02018-12-20 18:18:08 +09003173 }
3174
3175 cc_binary {
3176 name: "not_in_apex",
3177 srcs: ["mylib.cpp"],
3178 static_libs: ["mylib"],
3179 static_executable: true,
3180 system_shared_libs: [],
3181 stl: "none",
3182 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003183 `)
3184
Colin Cross7113d202019-11-20 16:39:12 -08003185 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003186
3187 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003188 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003189}
Jiyong Park9335a262018-12-24 11:31:58 +09003190
3191func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003192 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003193 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003194 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003195 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003196 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003197 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003198 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003199 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003200 }
3201
3202 cc_library {
3203 name: "mylib",
3204 srcs: ["mylib.cpp"],
3205 system_shared_libs: [],
3206 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003207 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003208 }
3209
3210 apex_key {
3211 name: "myapex.key",
3212 public_key: "testkey.avbpubkey",
3213 private_key: "testkey.pem",
3214 }
3215
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003216 android_app_certificate {
3217 name: "myapex.certificate",
3218 certificate: "testkey",
3219 }
3220
3221 android_app_certificate {
3222 name: "myapex.certificate.override",
3223 certificate: "testkey.override",
3224 }
3225
Jiyong Park9335a262018-12-24 11:31:58 +09003226 `)
3227
3228 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003229 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003230
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003231 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3232 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003233 "vendor/foo/devkeys/testkey.avbpubkey")
3234 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003235 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3236 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003237 "vendor/foo/devkeys/testkey.pem")
3238 }
3239
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003240 // check the APK certs. It should be overridden to myapex.certificate.override
Jooyung Hana0503a52023-08-23 13:12:50 +09003241 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003242 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003243 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003244 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003245 }
3246}
Jiyong Park58e364a2019-01-19 19:24:06 +09003247
Jooyung Hanf121a652019-12-17 14:30:11 +09003248func TestCertificate(t *testing.T) {
3249 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003250 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003251 apex {
3252 name: "myapex",
3253 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003254 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003255 }
3256 apex_key {
3257 name: "myapex.key",
3258 public_key: "testkey.avbpubkey",
3259 private_key: "testkey.pem",
3260 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003261 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003262 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3263 if actual := rule.Args["certificates"]; actual != expected {
3264 t.Errorf("certificates should be %q, not %q", expected, actual)
3265 }
3266 })
3267 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003268 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003269 apex {
3270 name: "myapex_keytest",
3271 key: "myapex.key",
3272 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003273 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003274 }
3275 apex_key {
3276 name: "myapex.key",
3277 public_key: "testkey.avbpubkey",
3278 private_key: "testkey.pem",
3279 }
3280 android_app_certificate {
3281 name: "myapex.certificate.override",
3282 certificate: "testkey.override",
3283 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003284 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003285 expected := "testkey.override.x509.pem testkey.override.pk8"
3286 if actual := rule.Args["certificates"]; actual != expected {
3287 t.Errorf("certificates should be %q, not %q", expected, actual)
3288 }
3289 })
3290 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003291 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003292 apex {
3293 name: "myapex",
3294 key: "myapex.key",
3295 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003296 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003297 }
3298 apex_key {
3299 name: "myapex.key",
3300 public_key: "testkey.avbpubkey",
3301 private_key: "testkey.pem",
3302 }
3303 android_app_certificate {
3304 name: "myapex.certificate",
3305 certificate: "testkey",
3306 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003307 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003308 expected := "testkey.x509.pem testkey.pk8"
3309 if actual := rule.Args["certificates"]; actual != expected {
3310 t.Errorf("certificates should be %q, not %q", expected, actual)
3311 }
3312 })
3313 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003314 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003315 apex {
3316 name: "myapex_keytest",
3317 key: "myapex.key",
3318 file_contexts: ":myapex-file_contexts",
3319 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003320 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003321 }
3322 apex_key {
3323 name: "myapex.key",
3324 public_key: "testkey.avbpubkey",
3325 private_key: "testkey.pem",
3326 }
3327 android_app_certificate {
3328 name: "myapex.certificate.override",
3329 certificate: "testkey.override",
3330 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003331 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003332 expected := "testkey.override.x509.pem testkey.override.pk8"
3333 if actual := rule.Args["certificates"]; actual != expected {
3334 t.Errorf("certificates should be %q, not %q", expected, actual)
3335 }
3336 })
3337 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003338 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003339 apex {
3340 name: "myapex",
3341 key: "myapex.key",
3342 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003343 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003344 }
3345 apex_key {
3346 name: "myapex.key",
3347 public_key: "testkey.avbpubkey",
3348 private_key: "testkey.pem",
3349 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003350 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003351 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3352 if actual := rule.Args["certificates"]; actual != expected {
3353 t.Errorf("certificates should be %q, not %q", expected, actual)
3354 }
3355 })
3356 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003357 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003358 apex {
3359 name: "myapex_keytest",
3360 key: "myapex.key",
3361 file_contexts: ":myapex-file_contexts",
3362 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003363 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003364 }
3365 apex_key {
3366 name: "myapex.key",
3367 public_key: "testkey.avbpubkey",
3368 private_key: "testkey.pem",
3369 }
3370 android_app_certificate {
3371 name: "myapex.certificate.override",
3372 certificate: "testkey.override",
3373 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003374 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003375 expected := "testkey.override.x509.pem testkey.override.pk8"
3376 if actual := rule.Args["certificates"]; actual != expected {
3377 t.Errorf("certificates should be %q, not %q", expected, actual)
3378 }
3379 })
3380}
3381
Jiyong Park58e364a2019-01-19 19:24:06 +09003382func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003383 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003384 apex {
3385 name: "myapex",
3386 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003387 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003388 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003389 }
3390
3391 apex {
3392 name: "otherapex",
3393 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003394 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003395 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003396 }
3397
3398 apex_key {
3399 name: "myapex.key",
3400 public_key: "testkey.avbpubkey",
3401 private_key: "testkey.pem",
3402 }
3403
3404 cc_library {
3405 name: "mylib",
3406 srcs: ["mylib.cpp"],
3407 system_shared_libs: [],
3408 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003409 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003410 "myapex",
3411 "otherapex",
3412 ],
Jooyung Han24282772020-03-21 23:20:55 +09003413 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003414 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003415 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003416 cc_library {
3417 name: "mylib2",
3418 srcs: ["mylib.cpp"],
3419 system_shared_libs: [],
3420 stl: "none",
3421 apex_available: [
3422 "myapex",
3423 "otherapex",
3424 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003425 static_libs: ["mylib3"],
3426 recovery_available: true,
3427 min_sdk_version: "29",
3428 }
3429 cc_library {
3430 name: "mylib3",
3431 srcs: ["mylib.cpp"],
3432 system_shared_libs: [],
3433 stl: "none",
3434 apex_available: [
3435 "myapex",
3436 "otherapex",
3437 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003438 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003439 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003440 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003441 `)
3442
Jooyung Hanc87a0592020-03-02 17:44:33 +09003443 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003444 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003445 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003446
Vinh Tranf9754732023-01-19 22:41:46 -05003447 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003448 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003449 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003450
Vinh Tranf9754732023-01-19 22:41:46 -05003451 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003452 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003453 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003454
Colin Crossaede88c2020-08-11 12:17:01 -07003455 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3456 // each variant defines additional macros to distinguish which apex variant it is built for
3457
3458 // non-APEX variant does not have __ANDROID_APEX__ defined
3459 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3460 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3461
Vinh Tranf9754732023-01-19 22:41:46 -05003462 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003463 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3464 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003465
Jooyung Hanc87a0592020-03-02 17:44:33 +09003466 // non-APEX variant does not have __ANDROID_APEX__ defined
3467 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3468 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3469
Vinh Tranf9754732023-01-19 22:41:46 -05003470 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003471 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003472 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003473}
Jiyong Park7e636d02019-01-28 16:16:54 +09003474
3475func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003476 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003477 apex {
3478 name: "myapex",
3479 key: "myapex.key",
3480 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003481 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003482 }
3483
3484 apex_key {
3485 name: "myapex.key",
3486 public_key: "testkey.avbpubkey",
3487 private_key: "testkey.pem",
3488 }
3489
3490 cc_library_headers {
3491 name: "mylib_headers",
3492 export_include_dirs: ["my_include"],
3493 system_shared_libs: [],
3494 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003495 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003496 }
3497
3498 cc_library {
3499 name: "mylib",
3500 srcs: ["mylib.cpp"],
3501 system_shared_libs: [],
3502 stl: "none",
3503 header_libs: ["mylib_headers"],
3504 export_header_lib_headers: ["mylib_headers"],
3505 stubs: {
3506 versions: ["1", "2", "3"],
3507 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003508 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003509 }
3510
3511 cc_library {
3512 name: "otherlib",
3513 srcs: ["mylib.cpp"],
3514 system_shared_libs: [],
3515 stl: "none",
3516 shared_libs: ["mylib"],
3517 }
3518 `)
3519
Colin Cross7113d202019-11-20 16:39:12 -08003520 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003521
3522 // Ensure that the include path of the header lib is exported to 'otherlib'
3523 ensureContains(t, cFlags, "-Imy_include")
3524}
Alex Light9670d332019-01-29 18:07:33 -08003525
Jiyong Park7cd10e32020-01-14 09:22:18 +09003526type fileInApex struct {
3527 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003528 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003529 isLink bool
3530}
3531
Jooyung Han1724d582022-12-21 10:17:44 +09003532func (f fileInApex) String() string {
3533 return f.src + ":" + f.path
3534}
3535
3536func (f fileInApex) match(expectation string) bool {
3537 parts := strings.Split(expectation, ":")
3538 if len(parts) == 1 {
3539 match, _ := path.Match(parts[0], f.path)
3540 return match
3541 }
3542 if len(parts) == 2 {
3543 matchSrc, _ := path.Match(parts[0], f.src)
3544 matchDst, _ := path.Match(parts[1], f.path)
3545 return matchSrc && matchDst
3546 }
3547 panic("invalid expected file specification: " + expectation)
3548}
3549
Jooyung Hana57af4a2020-01-23 05:36:59 +00003550func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003551 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003552 module := ctx.ModuleForTests(moduleName, variant)
3553 apexRule := module.MaybeRule("apexRule")
3554 apexDir := "/image.apex/"
Jooyung Han31c470b2019-10-18 16:26:59 +09003555 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003556 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003557 for _, cmd := range strings.Split(copyCmds, "&&") {
3558 cmd = strings.TrimSpace(cmd)
3559 if cmd == "" {
3560 continue
3561 }
3562 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003563 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003564 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003565 switch terms[0] {
3566 case "mkdir":
3567 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003568 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003569 t.Fatal("copyCmds contains invalid cp command", cmd)
3570 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003571 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003572 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003573 isLink = false
3574 case "ln":
3575 if len(terms) != 3 && len(terms) != 4 {
3576 // ln LINK TARGET or ln -s LINK TARGET
3577 t.Fatal("copyCmds contains invalid ln command", cmd)
3578 }
3579 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003580 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003581 isLink = true
3582 default:
3583 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3584 }
3585 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003586 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003587 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003588 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003589 }
Jooyung Han1724d582022-12-21 10:17:44 +09003590 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003591 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003592 }
3593 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003594 return ret
3595}
3596
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003597func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003598 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003599 var failed bool
3600 var surplus []string
3601 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003602 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003603 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003604 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003605 if file.match(expected) {
3606 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003607 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003608 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003609 }
3610 }
Jooyung Han1724d582022-12-21 10:17:44 +09003611 if !matchFound {
3612 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003613 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003614 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003615
Jooyung Han31c470b2019-10-18 16:26:59 +09003616 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003617 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003618 t.Log("surplus files", surplus)
3619 failed = true
3620 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003621
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003622 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003623 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003624 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003625 if !filesMatched[expected] {
3626 missing = append(missing, expected)
3627 }
3628 }
3629 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003630 t.Log("missing files", missing)
3631 failed = true
3632 }
3633 if failed {
3634 t.Fail()
3635 }
3636}
3637
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003638func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3639 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3640}
3641
3642func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
Spandan Das2069c3f2023-12-06 19:40:24 +00003643 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Description("deapex")
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003644 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3645 if deapexer.Output != nil {
3646 outputs = append(outputs, deapexer.Output.String())
3647 }
3648 for _, output := range deapexer.ImplicitOutputs {
3649 outputs = append(outputs, output.String())
3650 }
3651 actualFiles := make([]fileInApex, 0, len(outputs))
3652 for _, output := range outputs {
3653 dir := "/deapexer/"
3654 pos := strings.LastIndex(output, dir)
3655 if pos == -1 {
3656 t.Fatal("Unknown deapexer output ", output)
3657 }
3658 path := output[pos+len(dir):]
3659 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3660 }
3661 assertFileListEquals(t, files, actualFiles)
3662}
3663
Jooyung Han39edb6c2019-11-06 16:53:07 +09003664func vndkLibrariesTxtFiles(vers ...string) (result string) {
3665 for _, v := range vers {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +09003666 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Justin Yund5784122023-10-25 13:25:32 +09003667 result += `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003668 prebuilt_etc {
3669 name: "` + txt + `.libraries.` + v + `.txt",
3670 src: "dummy.txt",
3671 }
3672 `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003673 }
3674 }
3675 return
3676}
3677
Jooyung Han344d5432019-08-23 11:17:39 +09003678func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003679 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003680 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003681 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003682 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003683 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003684 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003685 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003686 }
3687
3688 apex_key {
3689 name: "myapex.key",
3690 public_key: "testkey.avbpubkey",
3691 private_key: "testkey.pem",
3692 }
3693
Jooyung Han31c470b2019-10-18 16:26:59 +09003694 vndk_prebuilt_shared {
3695 name: "libvndk27",
3696 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003697 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003698 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003699 vndk: {
3700 enabled: true,
3701 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003702 target_arch: "arm64",
3703 arch: {
3704 arm: {
3705 srcs: ["libvndk27_arm.so"],
3706 },
3707 arm64: {
3708 srcs: ["libvndk27_arm64.so"],
3709 },
3710 },
Colin Cross2807f002021-03-02 10:15:29 -08003711 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003712 }
3713
3714 vndk_prebuilt_shared {
3715 name: "libvndk27",
3716 version: "27",
3717 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003718 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003719 vndk: {
3720 enabled: true,
3721 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003722 target_arch: "x86_64",
3723 arch: {
3724 x86: {
3725 srcs: ["libvndk27_x86.so"],
3726 },
3727 x86_64: {
3728 srcs: ["libvndk27_x86_64.so"],
3729 },
3730 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003731 }
3732 `+vndkLibrariesTxtFiles("27"),
3733 withFiles(map[string][]byte{
3734 "libvndk27_arm.so": nil,
3735 "libvndk27_arm64.so": nil,
3736 "libvndk27_x86.so": nil,
3737 "libvndk27_x86_64.so": nil,
3738 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003739
Jooyung Hana0503a52023-08-23 13:12:50 +09003740 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003741 "lib/libvndk27_arm.so",
3742 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003743 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003744 })
Jooyung Han344d5432019-08-23 11:17:39 +09003745}
3746
Jooyung Han90eee022019-10-01 20:02:42 +09003747func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003748 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003749 apex_vndk {
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003750 name: "com.android.vndk.v29",
Jooyung Han90eee022019-10-01 20:02:42 +09003751 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003752 file_contexts: ":myapex-file_contexts",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003753 vndk_version: "29",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003754 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003755 }
3756 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003757 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003758 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003759 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003760 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003761 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003762 }
3763 apex_key {
3764 name: "myapex.key",
3765 public_key: "testkey.avbpubkey",
3766 private_key: "testkey.pem",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003767 }`+vndkLibrariesTxtFiles("28", "29"))
Jooyung Han90eee022019-10-01 20:02:42 +09003768
3769 assertApexName := func(expected, moduleName string) {
Jooyung Hana0503a52023-08-23 13:12:50 +09003770 module := ctx.ModuleForTests(moduleName, "android_common")
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003771 apexManifestRule := module.Rule("apexManifestRule")
3772 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003773 }
3774
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003775 assertApexName("com.android.vndk.v29", "com.android.vndk.v29")
Colin Cross2807f002021-03-02 10:15:29 -08003776 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003777}
3778
Jooyung Han344d5432019-08-23 11:17:39 +09003779func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003780 testApexError(t, `module "com.android.vndk.v30" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003781 apex_vndk {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003782 name: "com.android.vndk.v30",
3783 key: "com.android.vndk.v30.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003784 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003785 native_bridge_supported: true,
3786 }
3787
3788 apex_key {
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003789 name: "com.android.vndk.v30.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003790 public_key: "testkey.avbpubkey",
3791 private_key: "testkey.pem",
3792 }
3793
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003794 vndk_prebuilt_shared {
Jooyung Han344d5432019-08-23 11:17:39 +09003795 name: "libvndk",
Kiyoung Kim9f26fcf2024-05-27 17:25:52 +09003796 version: "30",
3797 target_arch: "arm",
Jooyung Han344d5432019-08-23 11:17:39 +09003798 srcs: ["mylib.cpp"],
3799 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003800 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003801 native_bridge_supported: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003802 vndk: {
3803 enabled: true,
3804 },
Jooyung Han344d5432019-08-23 11:17:39 +09003805 }
3806 `)
3807}
3808
Jooyung Han31c470b2019-10-18 16:26:59 +09003809func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003810 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003811 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003812 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003813 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003814 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003815 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003816 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003817 }
3818
3819 apex_key {
3820 name: "myapex.key",
3821 public_key: "testkey.avbpubkey",
3822 private_key: "testkey.pem",
3823 }
3824
3825 vndk_prebuilt_shared {
3826 name: "libvndk27",
3827 version: "27",
3828 target_arch: "arm",
3829 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003830 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003831 vndk: {
3832 enabled: true,
3833 },
3834 arch: {
3835 arm: {
3836 srcs: ["libvndk27.so"],
3837 }
3838 },
3839 }
3840
3841 vndk_prebuilt_shared {
3842 name: "libvndk27",
3843 version: "27",
3844 target_arch: "arm",
3845 binder32bit: true,
3846 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003847 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003848 vndk: {
3849 enabled: true,
3850 },
3851 arch: {
3852 arm: {
3853 srcs: ["libvndk27binder32.so"],
3854 }
3855 },
Colin Cross2807f002021-03-02 10:15:29 -08003856 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003857 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003858 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003859 withFiles(map[string][]byte{
3860 "libvndk27.so": nil,
3861 "libvndk27binder32.so": nil,
3862 }),
3863 withBinder32bit,
3864 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07003865 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09003866 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3867 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003868 },
3869 }),
3870 )
3871
Jooyung Hana0503a52023-08-23 13:12:50 +09003872 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003873 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003874 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003875 })
3876}
3877
Jooyung Hane1633032019-08-01 17:41:43 +09003878func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003879 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003880 apex {
3881 name: "myapex_nodep",
3882 key: "myapex.key",
3883 native_shared_libs: ["lib_nodep"],
3884 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003885 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003886 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003887 }
3888
3889 apex {
3890 name: "myapex_dep",
3891 key: "myapex.key",
3892 native_shared_libs: ["lib_dep"],
3893 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003894 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003895 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003896 }
3897
3898 apex {
3899 name: "myapex_provider",
3900 key: "myapex.key",
3901 native_shared_libs: ["libfoo"],
3902 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003903 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003904 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003905 }
3906
3907 apex {
3908 name: "myapex_selfcontained",
3909 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00003910 native_shared_libs: ["lib_dep_on_bar", "libbar"],
Jooyung Hane1633032019-08-01 17:41:43 +09003911 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003912 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003913 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003914 }
3915
3916 apex_key {
3917 name: "myapex.key",
3918 public_key: "testkey.avbpubkey",
3919 private_key: "testkey.pem",
3920 }
3921
3922 cc_library {
3923 name: "lib_nodep",
3924 srcs: ["mylib.cpp"],
3925 system_shared_libs: [],
3926 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003927 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003928 }
3929
3930 cc_library {
3931 name: "lib_dep",
3932 srcs: ["mylib.cpp"],
3933 shared_libs: ["libfoo"],
3934 system_shared_libs: [],
3935 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003936 apex_available: [
3937 "myapex_dep",
3938 "myapex_provider",
3939 "myapex_selfcontained",
3940 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003941 }
3942
3943 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00003944 name: "lib_dep_on_bar",
3945 srcs: ["mylib.cpp"],
3946 shared_libs: ["libbar"],
3947 system_shared_libs: [],
3948 stl: "none",
3949 apex_available: [
3950 "myapex_selfcontained",
3951 ],
3952 }
3953
3954
3955 cc_library {
Jooyung Hane1633032019-08-01 17:41:43 +09003956 name: "libfoo",
3957 srcs: ["mytest.cpp"],
3958 stubs: {
3959 versions: ["1"],
3960 },
3961 system_shared_libs: [],
3962 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003963 apex_available: [
3964 "myapex_provider",
Spandan Das20fce2d2023-04-12 17:21:39 +00003965 ],
3966 }
3967
3968 cc_library {
3969 name: "libbar",
3970 srcs: ["mytest.cpp"],
3971 stubs: {
3972 versions: ["1"],
3973 },
3974 system_shared_libs: [],
3975 stl: "none",
3976 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003977 "myapex_selfcontained",
3978 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003979 }
Spandan Das20fce2d2023-04-12 17:21:39 +00003980
Jooyung Hane1633032019-08-01 17:41:43 +09003981 `)
3982
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003983 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003984 var provideNativeLibs, requireNativeLibs []string
3985
Jooyung Hana0503a52023-08-23 13:12:50 +09003986 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003987 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3988 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003989 ensureListEmpty(t, provideNativeLibs)
3990 ensureListEmpty(t, requireNativeLibs)
3991
Jooyung Hana0503a52023-08-23 13:12:50 +09003992 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003993 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3994 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003995 ensureListEmpty(t, provideNativeLibs)
3996 ensureListContains(t, requireNativeLibs, "libfoo.so")
3997
Jooyung Hana0503a52023-08-23 13:12:50 +09003998 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003999 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4000 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004001 ensureListContains(t, provideNativeLibs, "libfoo.so")
4002 ensureListEmpty(t, requireNativeLibs)
4003
Jooyung Hana0503a52023-08-23 13:12:50 +09004004 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004005 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4006 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Spandan Das20fce2d2023-04-12 17:21:39 +00004007 ensureListContains(t, provideNativeLibs, "libbar.so")
Jooyung Hane1633032019-08-01 17:41:43 +09004008 ensureListEmpty(t, requireNativeLibs)
4009}
4010
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004011func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4012 ctx := testApex(t, `
4013 apex {
4014 name: "myapex",
4015 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004016 native_shared_libs: ["mylib"],
4017 updatable: false,
4018 }
4019
4020 apex_key {
4021 name: "myapex.key",
4022 public_key: "testkey.avbpubkey",
4023 private_key: "testkey.pem",
4024 }
4025
4026 cc_library {
4027 name: "mylib",
4028 srcs: ["mylib.cpp"],
4029 system_shared_libs: [],
4030 stl: "none",
4031 apex_available: [
4032 "//apex_available:platform",
4033 "myapex",
4034 ],
4035 }
4036 `, android.FixtureMergeEnv(map[string]string{
4037 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4038 }))
4039
Jooyung Hana0503a52023-08-23 13:12:50 +09004040 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004041 apexManifestRule := module.Rule("apexManifestRule")
4042 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4043}
4044
Vinh Tran8f5310f2022-10-07 18:16:47 -04004045func TestCompileMultilibProp(t *testing.T) {
4046 testCases := []struct {
4047 compileMultiLibProp string
4048 containedLibs []string
4049 notContainedLibs []string
4050 }{
4051 {
4052 containedLibs: []string{
4053 "image.apex/lib64/mylib.so",
4054 "image.apex/lib/mylib.so",
4055 },
4056 compileMultiLibProp: `compile_multilib: "both",`,
4057 },
4058 {
4059 containedLibs: []string{"image.apex/lib64/mylib.so"},
4060 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4061 compileMultiLibProp: `compile_multilib: "first",`,
4062 },
4063 {
4064 containedLibs: []string{"image.apex/lib64/mylib.so"},
4065 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4066 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4067 },
4068 {
4069 containedLibs: []string{"image.apex/lib64/mylib.so"},
4070 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4071 compileMultiLibProp: `compile_multilib: "64",`,
4072 },
4073 {
4074 containedLibs: []string{"image.apex/lib/mylib.so"},
4075 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4076 compileMultiLibProp: `compile_multilib: "32",`,
4077 },
4078 }
4079 for _, testCase := range testCases {
4080 ctx := testApex(t, fmt.Sprintf(`
4081 apex {
4082 name: "myapex",
4083 key: "myapex.key",
4084 %s
4085 native_shared_libs: ["mylib"],
4086 updatable: false,
4087 }
4088 apex_key {
4089 name: "myapex.key",
4090 public_key: "testkey.avbpubkey",
4091 private_key: "testkey.pem",
4092 }
4093 cc_library {
4094 name: "mylib",
4095 srcs: ["mylib.cpp"],
4096 apex_available: [
4097 "//apex_available:platform",
4098 "myapex",
4099 ],
4100 }
4101 `, testCase.compileMultiLibProp),
4102 )
Jooyung Hana0503a52023-08-23 13:12:50 +09004103 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Vinh Tran8f5310f2022-10-07 18:16:47 -04004104 apexRule := module.Rule("apexRule")
4105 copyCmds := apexRule.Args["copy_commands"]
4106 for _, containedLib := range testCase.containedLibs {
4107 ensureContains(t, copyCmds, containedLib)
4108 }
4109 for _, notContainedLib := range testCase.notContainedLibs {
4110 ensureNotContains(t, copyCmds, notContainedLib)
4111 }
4112 }
4113}
4114
Alex Light0851b882019-02-07 13:20:53 -08004115func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004116 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004117 apex {
4118 name: "myapex",
4119 key: "myapex.key",
4120 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004121 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004122 }
4123
4124 apex_key {
4125 name: "myapex.key",
4126 public_key: "testkey.avbpubkey",
4127 private_key: "testkey.pem",
4128 }
4129
4130 cc_library {
4131 name: "mylib_common",
4132 srcs: ["mylib.cpp"],
4133 system_shared_libs: [],
4134 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004135 apex_available: [
4136 "//apex_available:platform",
4137 "myapex",
4138 ],
Alex Light0851b882019-02-07 13:20:53 -08004139 }
4140 `)
4141
Jooyung Hana0503a52023-08-23 13:12:50 +09004142 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004143 apexRule := module.Rule("apexRule")
4144 copyCmds := apexRule.Args["copy_commands"]
4145
4146 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4147 t.Log("Apex was a test apex!")
4148 t.Fail()
4149 }
4150 // Ensure that main rule creates an output
4151 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4152
4153 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004154 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004155
4156 // Ensure that both direct and indirect deps are copied into apex
4157 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4158
Colin Cross7113d202019-11-20 16:39:12 -08004159 // Ensure that the platform variant ends with _shared
4160 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004161
Colin Cross56a83212020-09-15 18:30:11 -07004162 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004163 t.Log("Found mylib_common not in any apex!")
4164 t.Fail()
4165 }
4166}
4167
4168func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004169 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004170 apex_test {
4171 name: "myapex",
4172 key: "myapex.key",
4173 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004174 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004175 }
4176
4177 apex_key {
4178 name: "myapex.key",
4179 public_key: "testkey.avbpubkey",
4180 private_key: "testkey.pem",
4181 }
4182
4183 cc_library {
4184 name: "mylib_common_test",
4185 srcs: ["mylib.cpp"],
4186 system_shared_libs: [],
4187 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004188 // TODO: remove //apex_available:platform
4189 apex_available: [
4190 "//apex_available:platform",
4191 "myapex",
4192 ],
Alex Light0851b882019-02-07 13:20:53 -08004193 }
4194 `)
4195
Jooyung Hana0503a52023-08-23 13:12:50 +09004196 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004197 apexRule := module.Rule("apexRule")
4198 copyCmds := apexRule.Args["copy_commands"]
4199
4200 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4201 t.Log("Apex was not a test apex!")
4202 t.Fail()
4203 }
4204 // Ensure that main rule creates an output
4205 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4206
4207 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004208 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004209
4210 // Ensure that both direct and indirect deps are copied into apex
4211 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4212
Colin Cross7113d202019-11-20 16:39:12 -08004213 // Ensure that the platform variant ends with _shared
4214 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004215}
4216
Jooyung Han85707de2023-12-01 14:21:13 +09004217func TestLibzVendorIsntStable(t *testing.T) {
4218 ctx := testApex(t, `
4219 apex {
4220 name: "myapex",
4221 key: "myapex.key",
4222 updatable: false,
4223 binaries: ["mybin"],
4224 }
4225 apex {
4226 name: "myvendorapex",
4227 key: "myapex.key",
4228 file_contexts: "myvendorapex_file_contexts",
4229 vendor: true,
4230 updatable: false,
4231 binaries: ["mybin"],
4232 }
4233 apex_key {
4234 name: "myapex.key",
4235 public_key: "testkey.avbpubkey",
4236 private_key: "testkey.pem",
4237 }
4238 cc_binary {
4239 name: "mybin",
4240 vendor_available: true,
4241 system_shared_libs: [],
4242 stl: "none",
4243 shared_libs: ["libz"],
4244 apex_available: ["//apex_available:anyapex"],
4245 }
4246 cc_library {
4247 name: "libz",
4248 vendor_available: true,
4249 system_shared_libs: [],
4250 stl: "none",
4251 stubs: {
4252 versions: ["28", "30"],
4253 },
4254 target: {
4255 vendor: {
4256 no_stubs: true,
4257 },
4258 },
4259 }
4260 `, withFiles(map[string][]byte{
4261 "myvendorapex_file_contexts": nil,
4262 }))
4263
4264 // libz provides stubs for core variant.
4265 {
4266 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
4267 "bin/mybin",
4268 })
4269 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
4270 android.AssertStringEquals(t, "should require libz", apexManifestRule.Args["requireNativeLibs"], "libz.so")
4271 }
4272 // libz doesn't provide stubs for vendor variant.
4273 {
4274 ensureExactContents(t, ctx, "myvendorapex", "android_common_myvendorapex", []string{
4275 "bin/mybin",
4276 "lib64/libz.so",
4277 })
4278 apexManifestRule := ctx.ModuleForTests("myvendorapex", "android_common_myvendorapex").Rule("apexManifestRule")
4279 android.AssertStringEquals(t, "should not require libz", apexManifestRule.Args["requireNativeLibs"], "")
4280 }
4281}
4282
Alex Light9670d332019-01-29 18:07:33 -08004283func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004284 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004285 apex {
4286 name: "myapex",
4287 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004288 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004289 multilib: {
4290 first: {
4291 native_shared_libs: ["mylib_common"],
4292 }
4293 },
4294 target: {
4295 android: {
4296 multilib: {
4297 first: {
4298 native_shared_libs: ["mylib"],
4299 }
4300 }
4301 },
4302 host: {
4303 multilib: {
4304 first: {
4305 native_shared_libs: ["mylib2"],
4306 }
4307 }
4308 }
4309 }
4310 }
4311
4312 apex_key {
4313 name: "myapex.key",
4314 public_key: "testkey.avbpubkey",
4315 private_key: "testkey.pem",
4316 }
4317
4318 cc_library {
4319 name: "mylib",
4320 srcs: ["mylib.cpp"],
4321 system_shared_libs: [],
4322 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004323 // TODO: remove //apex_available:platform
4324 apex_available: [
4325 "//apex_available:platform",
4326 "myapex",
4327 ],
Alex Light9670d332019-01-29 18:07:33 -08004328 }
4329
4330 cc_library {
4331 name: "mylib_common",
4332 srcs: ["mylib.cpp"],
4333 system_shared_libs: [],
4334 stl: "none",
4335 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004336 // TODO: remove //apex_available:platform
4337 apex_available: [
4338 "//apex_available:platform",
4339 "myapex",
4340 ],
Alex Light9670d332019-01-29 18:07:33 -08004341 }
4342
4343 cc_library {
4344 name: "mylib2",
4345 srcs: ["mylib.cpp"],
4346 system_shared_libs: [],
4347 stl: "none",
4348 compile_multilib: "first",
4349 }
4350 `)
4351
Jooyung Hana0503a52023-08-23 13:12:50 +09004352 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004353 copyCmds := apexRule.Args["copy_commands"]
4354
4355 // Ensure that main rule creates an output
4356 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4357
4358 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004359 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4360 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4361 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004362
4363 // Ensure that both direct and indirect deps are copied into apex
4364 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4365 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4366 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4367
Colin Cross7113d202019-11-20 16:39:12 -08004368 // Ensure that the platform variant ends with _shared
4369 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4370 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4371 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004372}
Jiyong Park04480cf2019-02-06 00:16:29 +09004373
Jiyong Park59140302020-12-14 18:44:04 +09004374func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004375 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004376 apex {
4377 name: "myapex",
4378 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004379 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004380 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004381 arch: {
4382 arm64: {
4383 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004384 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004385 },
4386 x86_64: {
4387 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004388 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004389 },
4390 }
4391 }
4392
4393 apex_key {
4394 name: "myapex.key",
4395 public_key: "testkey.avbpubkey",
4396 private_key: "testkey.pem",
4397 }
4398
4399 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004400 name: "mylib.generic",
4401 srcs: ["mylib.cpp"],
4402 system_shared_libs: [],
4403 stl: "none",
4404 // TODO: remove //apex_available:platform
4405 apex_available: [
4406 "//apex_available:platform",
4407 "myapex",
4408 ],
4409 }
4410
4411 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004412 name: "mylib.arm64",
4413 srcs: ["mylib.cpp"],
4414 system_shared_libs: [],
4415 stl: "none",
4416 // TODO: remove //apex_available:platform
4417 apex_available: [
4418 "//apex_available:platform",
4419 "myapex",
4420 ],
4421 }
4422
4423 cc_library {
4424 name: "mylib.x64",
4425 srcs: ["mylib.cpp"],
4426 system_shared_libs: [],
4427 stl: "none",
4428 // TODO: remove //apex_available:platform
4429 apex_available: [
4430 "//apex_available:platform",
4431 "myapex",
4432 ],
4433 }
4434 `)
4435
Jooyung Hana0503a52023-08-23 13:12:50 +09004436 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park59140302020-12-14 18:44:04 +09004437 copyCmds := apexRule.Args["copy_commands"]
4438
4439 // Ensure that apex variant is created for the direct dep
4440 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004441 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004442 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4443
4444 // Ensure that both direct and indirect deps are copied into apex
4445 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4446 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4447}
4448
Jiyong Park04480cf2019-02-06 00:16:29 +09004449func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004450 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004451 apex {
4452 name: "myapex",
4453 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004454 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004455 updatable: false,
Riya Thakur654461c2024-02-27 07:21:05 +00004456 compile_multilib: "both",
Jiyong Park04480cf2019-02-06 00:16:29 +09004457 }
4458
4459 apex_key {
4460 name: "myapex.key",
4461 public_key: "testkey.avbpubkey",
4462 private_key: "testkey.pem",
4463 }
4464
4465 sh_binary {
4466 name: "myscript",
4467 src: "mylib.cpp",
4468 filename: "myscript.sh",
4469 sub_dir: "script",
4470 }
4471 `)
4472
Jooyung Hana0503a52023-08-23 13:12:50 +09004473 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004474 copyCmds := apexRule.Args["copy_commands"]
4475
4476 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4477}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004478
Jooyung Han91df2082019-11-20 01:49:42 +09004479func TestApexInVariousPartition(t *testing.T) {
4480 testcases := []struct {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004481 propName, partition string
Jooyung Han91df2082019-11-20 01:49:42 +09004482 }{
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004483 {"", "system"},
4484 {"product_specific: true", "product"},
4485 {"soc_specific: true", "vendor"},
4486 {"proprietary: true", "vendor"},
4487 {"vendor: true", "vendor"},
4488 {"system_ext_specific: true", "system_ext"},
Jooyung Han91df2082019-11-20 01:49:42 +09004489 }
4490 for _, tc := range testcases {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004491 t.Run(tc.propName+":"+tc.partition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004492 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004493 apex {
4494 name: "myapex",
4495 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004496 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004497 `+tc.propName+`
4498 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004499
Jooyung Han91df2082019-11-20 01:49:42 +09004500 apex_key {
4501 name: "myapex.key",
4502 public_key: "testkey.avbpubkey",
4503 private_key: "testkey.pem",
4504 }
4505 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004506
Jooyung Hana0503a52023-08-23 13:12:50 +09004507 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004508 expected := "out/soong/target/product/test_device/" + tc.partition + "/apex"
Paul Duffin37ba3442021-03-29 00:21:08 +01004509 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004510 if actual != expected {
4511 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4512 }
Jooyung Han91df2082019-11-20 01:49:42 +09004513 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004514 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004515}
Jiyong Park67882562019-03-21 01:11:21 +09004516
Jooyung Han580eb4f2020-06-24 19:33:06 +09004517func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004518 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004519 apex {
4520 name: "myapex",
4521 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004522 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004523 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004524
Jooyung Han580eb4f2020-06-24 19:33:06 +09004525 apex_key {
4526 name: "myapex.key",
4527 public_key: "testkey.avbpubkey",
4528 private_key: "testkey.pem",
4529 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004530 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09004531 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004532 rule := module.Output("file_contexts")
4533 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4534}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004535
Jooyung Han580eb4f2020-06-24 19:33:06 +09004536func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004537 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004538 apex {
4539 name: "myapex",
4540 key: "myapex.key",
4541 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004542 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004543 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004544
Jooyung Han580eb4f2020-06-24 19:33:06 +09004545 apex_key {
4546 name: "myapex.key",
4547 public_key: "testkey.avbpubkey",
4548 private_key: "testkey.pem",
4549 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004550 `, withFiles(map[string][]byte{
4551 "my_own_file_contexts": nil,
4552 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004553}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004554
Jooyung Han580eb4f2020-06-24 19:33:06 +09004555func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004556 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004557 apex {
4558 name: "myapex",
4559 key: "myapex.key",
4560 product_specific: true,
4561 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004562 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004563 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004564
Jooyung Han580eb4f2020-06-24 19:33:06 +09004565 apex_key {
4566 name: "myapex.key",
4567 public_key: "testkey.avbpubkey",
4568 private_key: "testkey.pem",
4569 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004570 `)
4571
Colin Cross1c460562021-02-16 17:55:47 -08004572 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004573 apex {
4574 name: "myapex",
4575 key: "myapex.key",
4576 product_specific: true,
4577 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004578 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004579 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004580
Jooyung Han580eb4f2020-06-24 19:33:06 +09004581 apex_key {
4582 name: "myapex.key",
4583 public_key: "testkey.avbpubkey",
4584 private_key: "testkey.pem",
4585 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004586 `, withFiles(map[string][]byte{
4587 "product_specific_file_contexts": nil,
4588 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004589 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004590 rule := module.Output("file_contexts")
4591 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4592}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004593
Jooyung Han580eb4f2020-06-24 19:33:06 +09004594func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004595 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004596 apex {
4597 name: "myapex",
4598 key: "myapex.key",
4599 product_specific: true,
4600 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004601 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004602 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004603
Jooyung Han580eb4f2020-06-24 19:33:06 +09004604 apex_key {
4605 name: "myapex.key",
4606 public_key: "testkey.avbpubkey",
4607 private_key: "testkey.pem",
4608 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004609
Jooyung Han580eb4f2020-06-24 19:33:06 +09004610 filegroup {
4611 name: "my-file-contexts",
4612 srcs: ["product_specific_file_contexts"],
4613 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004614 `, withFiles(map[string][]byte{
4615 "product_specific_file_contexts": nil,
4616 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004617 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004618 rule := module.Output("file_contexts")
4619 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004620}
4621
Jiyong Park67882562019-03-21 01:11:21 +09004622func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004623 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004624 apex_key {
4625 name: "myapex.key",
4626 public_key: ":my.avbpubkey",
4627 private_key: ":my.pem",
4628 product_specific: true,
4629 }
4630
4631 filegroup {
4632 name: "my.avbpubkey",
4633 srcs: ["testkey2.avbpubkey"],
4634 }
4635
4636 filegroup {
4637 name: "my.pem",
4638 srcs: ["testkey2.pem"],
4639 }
4640 `)
4641
4642 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4643 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004644 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004645 if actual_pubkey != expected_pubkey {
4646 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4647 }
4648 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004649 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004650 if actual_privkey != expected_privkey {
4651 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4652 }
4653}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004654
4655func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004656 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004657 prebuilt_apex {
4658 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004659 arch: {
4660 arm64: {
4661 src: "myapex-arm64.apex",
4662 },
4663 arm: {
4664 src: "myapex-arm.apex",
4665 },
4666 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004667 }
4668 `)
4669
Wei Li340ee8e2022-03-18 17:33:24 -07004670 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4671 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004672
Jiyong Parkc95714e2019-03-29 14:23:10 +09004673 expectedInput := "myapex-arm64.apex"
4674 if prebuilt.inputApex.String() != expectedInput {
4675 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4676 }
Wei Li340ee8e2022-03-18 17:33:24 -07004677 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4678 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4679 rule := testingModule.Rule("genProvenanceMetaData")
4680 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4681 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4682 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4683 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08004684
4685 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
4686 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004687}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004688
Paul Duffinc0609c62021-03-01 17:27:16 +00004689func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004690 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004691 prebuilt_apex {
4692 name: "myapex",
4693 }
4694 `)
4695}
4696
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004697func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004698 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004699 prebuilt_apex {
4700 name: "myapex",
4701 src: "myapex-arm.apex",
4702 filename: "notmyapex.apex",
4703 }
4704 `)
4705
Wei Li340ee8e2022-03-18 17:33:24 -07004706 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4707 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004708
4709 expected := "notmyapex.apex"
4710 if p.installFilename != expected {
4711 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4712 }
Wei Li340ee8e2022-03-18 17:33:24 -07004713 rule := testingModule.Rule("genProvenanceMetaData")
4714 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4715 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4716 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4717 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004718}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004719
Samiul Islam7c02e262021-09-08 17:48:28 +01004720func TestApexSetFilenameOverride(t *testing.T) {
4721 testApex(t, `
4722 apex_set {
4723 name: "com.company.android.myapex",
4724 apex_name: "com.android.myapex",
4725 set: "company-myapex.apks",
4726 filename: "com.company.android.myapex.apex"
4727 }
4728 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4729
4730 testApex(t, `
4731 apex_set {
4732 name: "com.company.android.myapex",
4733 apex_name: "com.android.myapex",
4734 set: "company-myapex.apks",
4735 filename: "com.company.android.myapex.capex"
4736 }
4737 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4738
4739 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4740 apex_set {
4741 name: "com.company.android.myapex",
4742 apex_name: "com.android.myapex",
4743 set: "company-myapex.apks",
4744 filename: "some-random-suffix"
4745 }
4746 `)
4747}
4748
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004749func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004750 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004751 prebuilt_apex {
4752 name: "myapex.prebuilt",
4753 src: "myapex-arm.apex",
4754 overrides: [
4755 "myapex",
4756 ],
4757 }
4758 `)
4759
Wei Li340ee8e2022-03-18 17:33:24 -07004760 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4761 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004762
4763 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004764 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004765 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004766 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004767 }
Wei Li340ee8e2022-03-18 17:33:24 -07004768 rule := testingModule.Rule("genProvenanceMetaData")
4769 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4770 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4771 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4772 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004773}
4774
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004775func TestPrebuiltApexName(t *testing.T) {
4776 testApex(t, `
4777 prebuilt_apex {
4778 name: "com.company.android.myapex",
4779 apex_name: "com.android.myapex",
4780 src: "company-myapex-arm.apex",
4781 }
4782 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4783
4784 testApex(t, `
4785 apex_set {
4786 name: "com.company.android.myapex",
4787 apex_name: "com.android.myapex",
4788 set: "company-myapex.apks",
4789 }
4790 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4791}
4792
4793func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4794 _ = android.GroupFixturePreparers(
4795 java.PrepareForTestWithJavaDefaultModules,
4796 PrepareForTestWithApexBuildComponents,
4797 android.FixtureWithRootAndroidBp(`
4798 platform_bootclasspath {
4799 name: "platform-bootclasspath",
4800 fragments: [
4801 {
4802 apex: "com.android.art",
4803 module: "art-bootclasspath-fragment",
4804 },
4805 ],
4806 }
4807
4808 prebuilt_apex {
4809 name: "com.company.android.art",
4810 apex_name: "com.android.art",
4811 src: "com.company.android.art-arm.apex",
4812 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4813 }
4814
4815 prebuilt_bootclasspath_fragment {
4816 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004817 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004818 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004819 hidden_api: {
4820 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4821 metadata: "my-bootclasspath-fragment/metadata.csv",
4822 index: "my-bootclasspath-fragment/index.csv",
4823 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4824 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4825 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004826 }
4827
4828 java_import {
4829 name: "core-oj",
4830 jars: ["prebuilt.jar"],
4831 }
4832 `),
4833 ).RunTest(t)
4834}
4835
Spandan Das59a4a2b2024-01-09 21:35:56 +00004836// A minimal context object for use with DexJarBuildPath
4837type moduleErrorfTestCtx struct {
4838}
4839
4840func (ctx moduleErrorfTestCtx) ModuleErrorf(format string, args ...interface{}) {
4841}
4842
Paul Duffin092153d2021-01-26 11:42:39 +00004843// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4844// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004845func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004846 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004847
Paul Duffin89886cb2021-02-05 16:44:03 +00004848 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004849 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004850 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004851 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +00004852 dexJarBuildPath := p.DexJarBuildPath(moduleErrorfTestCtx{}).PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004853 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004854 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
Spandan Das3576e762024-01-03 18:57:03 +00004855 ".intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
Jeongik Chad5fe8782021-07-08 01:13:11 +09004856 android.NormalizePathForTesting(dexJarBuildPath))
4857 }
4858
4859 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004860 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004861 // Make sure the import has been given the correct path to the dex jar.
4862 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4863 dexJarBuildPath := p.DexJarInstallPath()
4864 stem := android.RemoveOptionalPrebuiltPrefix(name)
4865 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4866 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4867 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004868 }
4869
Paul Duffin39853512021-02-26 11:09:39 +00004870 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004871 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004872 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004873 android.AssertArrayString(t, "Check if there is no source variant",
4874 []string{"android_common"},
4875 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004876 }
4877
4878 t.Run("prebuilt only", func(t *testing.T) {
4879 bp := `
4880 prebuilt_apex {
4881 name: "myapex",
4882 arch: {
4883 arm64: {
4884 src: "myapex-arm64.apex",
4885 },
4886 arm: {
4887 src: "myapex-arm.apex",
4888 },
4889 },
Paul Duffin39853512021-02-26 11:09:39 +00004890 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004891 }
4892
4893 java_import {
4894 name: "libfoo",
4895 jars: ["libfoo.jar"],
4896 }
Paul Duffin39853512021-02-26 11:09:39 +00004897
4898 java_sdk_library_import {
4899 name: "libbar",
4900 public: {
4901 jars: ["libbar.jar"],
4902 },
4903 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004904 `
4905
4906 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4907 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4908
Spandan Das3576e762024-01-03 18:57:03 +00004909 deapexerName := deapexerModuleName("prebuilt_myapex")
4910 android.AssertStringEquals(t, "APEX module name from deapexer name", "prebuilt_myapex", apexModuleName(deapexerName))
Martin Stjernholm44825602021-09-17 01:44:12 +01004911
Paul Duffinf6932af2021-02-26 18:21:56 +00004912 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004913 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004914 rule := deapexer.Rule("deapexer")
4915 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4916 t.Errorf("expected: %q, found: %q", expected, actual)
4917 }
4918
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004919 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004920 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004921 rule = prebuiltApex.Rule("android/soong/android.Cp")
4922 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4923 t.Errorf("expected: %q, found: %q", expected, actual)
4924 }
4925
Paul Duffin89886cb2021-02-05 16:44:03 +00004926 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004927 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004928
4929 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004930 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004931 })
4932
4933 t.Run("prebuilt with source preferred", func(t *testing.T) {
4934
4935 bp := `
4936 prebuilt_apex {
4937 name: "myapex",
4938 arch: {
4939 arm64: {
4940 src: "myapex-arm64.apex",
4941 },
4942 arm: {
4943 src: "myapex-arm.apex",
4944 },
4945 },
Paul Duffin39853512021-02-26 11:09:39 +00004946 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004947 }
4948
4949 java_import {
4950 name: "libfoo",
4951 jars: ["libfoo.jar"],
4952 }
4953
4954 java_library {
4955 name: "libfoo",
4956 }
Paul Duffin39853512021-02-26 11:09:39 +00004957
4958 java_sdk_library_import {
4959 name: "libbar",
4960 public: {
4961 jars: ["libbar.jar"],
4962 },
4963 }
4964
4965 java_sdk_library {
4966 name: "libbar",
4967 srcs: ["foo/bar/MyClass.java"],
4968 unsafe_ignore_missing_latest_api: true,
4969 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004970 `
4971
4972 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4973 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4974
Paul Duffin89886cb2021-02-05 16:44:03 +00004975 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004976 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004977 ensureNoSourceVariant(t, ctx, "libfoo")
4978
4979 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004980 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004981 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004982 })
4983
4984 t.Run("prebuilt preferred with source", func(t *testing.T) {
4985 bp := `
4986 prebuilt_apex {
4987 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004988 arch: {
4989 arm64: {
4990 src: "myapex-arm64.apex",
4991 },
4992 arm: {
4993 src: "myapex-arm.apex",
4994 },
4995 },
Paul Duffin39853512021-02-26 11:09:39 +00004996 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004997 }
4998
4999 java_import {
5000 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005001 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005002 jars: ["libfoo.jar"],
5003 }
5004
5005 java_library {
5006 name: "libfoo",
5007 }
Paul Duffin39853512021-02-26 11:09:39 +00005008
5009 java_sdk_library_import {
5010 name: "libbar",
5011 prefer: true,
5012 public: {
5013 jars: ["libbar.jar"],
5014 },
5015 }
5016
5017 java_sdk_library {
5018 name: "libbar",
5019 srcs: ["foo/bar/MyClass.java"],
5020 unsafe_ignore_missing_latest_api: true,
5021 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005022 `
5023
5024 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5025 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5026
Paul Duffin89886cb2021-02-05 16:44:03 +00005027 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005028 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005029 ensureNoSourceVariant(t, ctx, "libfoo")
5030
5031 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005032 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005033 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005034 })
5035}
5036
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005037func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005038 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005039 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005040 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5041 // is disabled.
5042 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
Spandan Das81fe4d12024-05-15 18:43:47 +00005043
5044 // Make sure that we have atleast one platform library so that we can check the monolithic hiddenapi
5045 // file creation.
5046 java.FixtureConfigureBootJars("platform:foo"),
5047 android.FixtureModifyMockFS(func(fs android.MockFS) {
5048 fs["platform/Android.bp"] = []byte(`
5049 java_library {
5050 name: "foo",
5051 srcs: ["Test.java"],
5052 compile_dex: true,
5053 }
5054 `)
5055 fs["platform/Test.java"] = nil
5056 }),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005057 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005058
Paul Duffin37856732021-02-26 14:24:15 +00005059 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5060 t.Helper()
Jiakai Zhangc6879f32023-11-06 16:31:19 +00005061 s := ctx.ModuleForTests("dex_bootjars", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005062 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005063 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005064 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005065 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005066 foundLibfooJar = true
5067 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005068 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005069 }
5070 }
5071 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005072 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 +00005073 }
5074 }
5075
Paul Duffin40a3f652021-07-19 13:11:24 +01005076 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005077 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005078 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005079 var rule android.TestingBuildParams
5080
5081 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5082 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005083 }
5084
Paul Duffin40a3f652021-07-19 13:11:24 +01005085 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5086 t.Helper()
5087 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5088 var rule android.TestingBuildParams
5089
5090 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5091 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5092 }
5093
Paul Duffin89f570a2021-06-16 01:42:33 +01005094 fragment := java.ApexVariantReference{
5095 Apex: proptools.StringPtr("myapex"),
5096 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5097 }
5098
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005099 t.Run("prebuilt only", func(t *testing.T) {
5100 bp := `
5101 prebuilt_apex {
5102 name: "myapex",
5103 arch: {
5104 arm64: {
5105 src: "myapex-arm64.apex",
5106 },
5107 arm: {
5108 src: "myapex-arm.apex",
5109 },
5110 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005111 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5112 }
5113
5114 prebuilt_bootclasspath_fragment {
5115 name: "my-bootclasspath-fragment",
5116 contents: ["libfoo", "libbar"],
5117 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005118 hidden_api: {
5119 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5120 metadata: "my-bootclasspath-fragment/metadata.csv",
5121 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005122 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5123 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5124 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005125 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005126 }
5127
5128 java_import {
5129 name: "libfoo",
5130 jars: ["libfoo.jar"],
5131 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005132 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005133 }
Paul Duffin37856732021-02-26 14:24:15 +00005134
5135 java_sdk_library_import {
5136 name: "libbar",
5137 public: {
5138 jars: ["libbar.jar"],
5139 },
5140 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005141 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005142 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005143 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005144 `
5145
Paul Duffin89f570a2021-06-16 01:42:33 +01005146 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005147 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5148 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005149
Paul Duffin537ea3d2021-05-14 10:38:00 +01005150 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005151 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005152 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005153 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005154 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005155 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 +01005156 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005157 })
5158
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005159 t.Run("apex_set only", func(t *testing.T) {
5160 bp := `
5161 apex_set {
5162 name: "myapex",
5163 set: "myapex.apks",
Liz Kammer2dc72442023-04-20 10:10:48 -04005164 exported_java_libs: ["myjavalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005165 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
Liz Kammer2dc72442023-04-20 10:10:48 -04005166 exported_systemserverclasspath_fragments: ["my-systemserverclasspath-fragment"],
5167 }
5168
5169 java_import {
5170 name: "myjavalib",
5171 jars: ["myjavalib.jar"],
5172 apex_available: ["myapex"],
5173 permitted_packages: ["javalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005174 }
5175
5176 prebuilt_bootclasspath_fragment {
5177 name: "my-bootclasspath-fragment",
5178 contents: ["libfoo", "libbar"],
5179 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005180 hidden_api: {
5181 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5182 metadata: "my-bootclasspath-fragment/metadata.csv",
5183 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005184 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5185 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5186 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005187 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005188 }
5189
Liz Kammer2dc72442023-04-20 10:10:48 -04005190 prebuilt_systemserverclasspath_fragment {
5191 name: "my-systemserverclasspath-fragment",
5192 contents: ["libbaz"],
5193 apex_available: ["myapex"],
5194 }
5195
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005196 java_import {
5197 name: "libfoo",
5198 jars: ["libfoo.jar"],
5199 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005200 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005201 }
5202
5203 java_sdk_library_import {
5204 name: "libbar",
5205 public: {
5206 jars: ["libbar.jar"],
5207 },
5208 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005209 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005210 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005211 }
Liz Kammer2dc72442023-04-20 10:10:48 -04005212
5213 java_sdk_library_import {
5214 name: "libbaz",
5215 public: {
5216 jars: ["libbaz.jar"],
5217 },
5218 apex_available: ["myapex"],
5219 shared_library: false,
5220 permitted_packages: ["baz"],
5221 }
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005222 `
5223
Paul Duffin89f570a2021-06-16 01:42:33 +01005224 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005225 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5226 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005227
Paul Duffin537ea3d2021-05-14 10:38:00 +01005228 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005229 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005230 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005231 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005232 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005233 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 +01005234 `)
Liz Kammer2dc72442023-04-20 10:10:48 -04005235
5236 myApex := ctx.ModuleForTests("myapex", "android_common_myapex").Module()
5237
5238 overrideNames := []string{
Spandan Dasa8e2d612024-07-26 19:24:27 +00005239 "",
Liz Kammer2dc72442023-04-20 10:10:48 -04005240 "myjavalib.myapex",
5241 "libfoo.myapex",
5242 "libbar.myapex",
5243 "libbaz.myapex",
5244 }
5245 mkEntries := android.AndroidMkEntriesForTest(t, ctx, myApex)
5246 for i, e := range mkEntries {
5247 g := e.OverrideName
5248 if w := overrideNames[i]; w != g {
5249 t.Errorf("Expected override name %q, got %q", w, g)
5250 }
5251 }
5252
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005253 })
5254
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005255 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5256 bp := `
5257 prebuilt_apex {
5258 name: "myapex",
5259 arch: {
5260 arm64: {
5261 src: "myapex-arm64.apex",
5262 },
5263 arm: {
5264 src: "myapex-arm.apex",
5265 },
5266 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005267 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5268 }
5269
5270 prebuilt_bootclasspath_fragment {
5271 name: "my-bootclasspath-fragment",
5272 contents: ["libfoo", "libbar"],
5273 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005274 hidden_api: {
5275 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5276 metadata: "my-bootclasspath-fragment/metadata.csv",
5277 index: "my-bootclasspath-fragment/index.csv",
5278 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5279 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5280 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005281 }
5282
5283 java_import {
5284 name: "libfoo",
5285 jars: ["libfoo.jar"],
5286 apex_available: ["myapex"],
5287 }
5288
5289 java_library {
5290 name: "libfoo",
5291 srcs: ["foo/bar/MyClass.java"],
5292 apex_available: ["myapex"],
5293 }
Paul Duffin37856732021-02-26 14:24:15 +00005294
5295 java_sdk_library_import {
5296 name: "libbar",
5297 public: {
5298 jars: ["libbar.jar"],
5299 },
5300 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005301 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005302 }
5303
5304 java_sdk_library {
5305 name: "libbar",
5306 srcs: ["foo/bar/MyClass.java"],
5307 unsafe_ignore_missing_latest_api: true,
5308 apex_available: ["myapex"],
5309 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005310 `
5311
5312 // In this test the source (java_library) libfoo is active since the
5313 // prebuilt (java_import) defaults to prefer:false. However the
5314 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5315 // find the dex boot jar in it. We either need to disable the source libfoo
5316 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005317 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005318 // dexbootjar check is skipped if AllowMissingDependencies is true
5319 preparerAllowMissingDeps := android.GroupFixturePreparers(
5320 preparer,
5321 android.PrepareForTestWithAllowMissingDependencies,
5322 )
5323 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005324 })
5325
5326 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5327 bp := `
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005328 apex {
5329 name: "myapex",
5330 key: "myapex.key",
5331 updatable: false,
5332 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5333 }
5334
5335 apex_key {
5336 name: "myapex.key",
5337 public_key: "testkey.avbpubkey",
5338 private_key: "testkey.pem",
5339 }
5340
5341 bootclasspath_fragment {
5342 name: "my-bootclasspath-fragment",
5343 contents: ["libfoo", "libbar"],
5344 apex_available: ["myapex"],
5345 hidden_api: {
5346 split_packages: ["*"],
5347 },
5348 }
5349
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005350 prebuilt_apex {
5351 name: "myapex",
5352 arch: {
5353 arm64: {
5354 src: "myapex-arm64.apex",
5355 },
5356 arm: {
5357 src: "myapex-arm.apex",
5358 },
5359 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005360 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5361 }
5362
5363 prebuilt_bootclasspath_fragment {
5364 name: "my-bootclasspath-fragment",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005365 prefer: true,
Paul Duffin89f570a2021-06-16 01:42:33 +01005366 contents: ["libfoo", "libbar"],
5367 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005368 hidden_api: {
5369 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5370 metadata: "my-bootclasspath-fragment/metadata.csv",
5371 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005372 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5373 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5374 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005375 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005376 }
5377
5378 java_import {
5379 name: "libfoo",
5380 prefer: true,
5381 jars: ["libfoo.jar"],
5382 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005383 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005384 }
5385
5386 java_library {
5387 name: "libfoo",
5388 srcs: ["foo/bar/MyClass.java"],
5389 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005390 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005391 }
Paul Duffin37856732021-02-26 14:24:15 +00005392
5393 java_sdk_library_import {
5394 name: "libbar",
5395 prefer: true,
5396 public: {
5397 jars: ["libbar.jar"],
5398 },
5399 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005400 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005401 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005402 }
5403
5404 java_sdk_library {
5405 name: "libbar",
5406 srcs: ["foo/bar/MyClass.java"],
5407 unsafe_ignore_missing_latest_api: true,
5408 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005409 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005410 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005411 `
5412
Paul Duffin89f570a2021-06-16 01:42:33 +01005413 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005414 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5415 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005416
Paul Duffin537ea3d2021-05-14 10:38:00 +01005417 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005418 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005419 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005420 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005421 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005422 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 +01005423 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005424 })
5425
5426 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5427 bp := `
5428 apex {
5429 name: "myapex",
5430 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005431 updatable: false,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005432 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005433 }
5434
5435 apex_key {
5436 name: "myapex.key",
5437 public_key: "testkey.avbpubkey",
5438 private_key: "testkey.pem",
5439 }
5440
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005441 bootclasspath_fragment {
5442 name: "my-bootclasspath-fragment",
5443 contents: ["libfoo", "libbar"],
5444 apex_available: ["myapex"],
5445 hidden_api: {
5446 split_packages: ["*"],
5447 },
5448 }
5449
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005450 prebuilt_apex {
5451 name: "myapex",
5452 arch: {
5453 arm64: {
5454 src: "myapex-arm64.apex",
5455 },
5456 arm: {
5457 src: "myapex-arm.apex",
5458 },
5459 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005460 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5461 }
5462
5463 prebuilt_bootclasspath_fragment {
5464 name: "my-bootclasspath-fragment",
5465 contents: ["libfoo", "libbar"],
5466 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005467 hidden_api: {
5468 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5469 metadata: "my-bootclasspath-fragment/metadata.csv",
5470 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005471 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5472 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5473 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005474 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005475 }
5476
5477 java_import {
5478 name: "libfoo",
5479 jars: ["libfoo.jar"],
5480 apex_available: ["myapex"],
5481 }
5482
5483 java_library {
5484 name: "libfoo",
5485 srcs: ["foo/bar/MyClass.java"],
5486 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005487 permitted_packages: ["foo"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005488 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005489 }
Paul Duffin37856732021-02-26 14:24:15 +00005490
5491 java_sdk_library_import {
5492 name: "libbar",
5493 public: {
5494 jars: ["libbar.jar"],
5495 },
5496 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005497 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005498 }
5499
5500 java_sdk_library {
5501 name: "libbar",
5502 srcs: ["foo/bar/MyClass.java"],
5503 unsafe_ignore_missing_latest_api: true,
5504 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005505 permitted_packages: ["bar"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005506 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005507 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005508 `
5509
Paul Duffin89f570a2021-06-16 01:42:33 +01005510 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Jiakai Zhangc6879f32023-11-06 16:31:19 +00005511 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/hiddenapi-modular/encoded/libfoo.jar")
5512 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 +00005513
Paul Duffin537ea3d2021-05-14 10:38:00 +01005514 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005515 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005516 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
5517 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005518 out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/modular-hiddenapi/index.csv
5519 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 +01005520 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005521 })
5522
5523 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5524 bp := `
5525 apex {
5526 name: "myapex",
5527 enabled: false,
5528 key: "myapex.key",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005529 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005530 }
5531
5532 apex_key {
5533 name: "myapex.key",
5534 public_key: "testkey.avbpubkey",
5535 private_key: "testkey.pem",
5536 }
5537
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005538 bootclasspath_fragment {
5539 name: "my-bootclasspath-fragment",
5540 enabled: false,
5541 contents: ["libfoo", "libbar"],
5542 apex_available: ["myapex"],
5543 hidden_api: {
5544 split_packages: ["*"],
5545 },
5546 }
5547
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005548 prebuilt_apex {
5549 name: "myapex",
5550 arch: {
5551 arm64: {
5552 src: "myapex-arm64.apex",
5553 },
5554 arm: {
5555 src: "myapex-arm.apex",
5556 },
5557 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005558 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5559 }
5560
5561 prebuilt_bootclasspath_fragment {
5562 name: "my-bootclasspath-fragment",
5563 contents: ["libfoo", "libbar"],
5564 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005565 hidden_api: {
5566 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5567 metadata: "my-bootclasspath-fragment/metadata.csv",
5568 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005569 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5570 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5571 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005572 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005573 }
5574
5575 java_import {
5576 name: "libfoo",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005577 jars: ["libfoo.jar"],
5578 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005579 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005580 }
5581
5582 java_library {
5583 name: "libfoo",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005584 enabled: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005585 srcs: ["foo/bar/MyClass.java"],
5586 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005587 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005588 }
Paul Duffin37856732021-02-26 14:24:15 +00005589
5590 java_sdk_library_import {
5591 name: "libbar",
Paul Duffin37856732021-02-26 14:24:15 +00005592 public: {
5593 jars: ["libbar.jar"],
5594 },
5595 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005596 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005597 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005598 }
5599
5600 java_sdk_library {
5601 name: "libbar",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005602 enabled: false,
Paul Duffin37856732021-02-26 14:24:15 +00005603 srcs: ["foo/bar/MyClass.java"],
5604 unsafe_ignore_missing_latest_api: true,
5605 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005606 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005607 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005608 `
Cole Fausta963b942024-04-11 17:43:00 -07005609 // This test disables libbar, which causes the ComponentDepsMutator to add
5610 // deps on libbar.stubs and other sub-modules that don't exist. We can
5611 // enable AllowMissingDependencies to work around that, but enabling that
5612 // causes extra checks for missing source files to dex_bootjars, so add those
5613 // to the mock fs as well.
5614 preparer2 := android.GroupFixturePreparers(
5615 preparer,
5616 android.PrepareForTestWithAllowMissingDependencies,
5617 android.FixtureMergeMockFs(map[string][]byte{
5618 "build/soong/scripts/check_boot_jars/package_allowed_list.txt": nil,
5619 "frameworks/base/config/boot-profile.txt": nil,
5620 }),
5621 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005622
Cole Fausta963b942024-04-11 17:43:00 -07005623 ctx := testDexpreoptWithApexes(t, bp, "", preparer2, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005624 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5625 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005626
Paul Duffin537ea3d2021-05-14 10:38:00 +01005627 // Verify the correct module jars contribute to the hiddenapi index file.
Spandan Das81fe4d12024-05-15 18:43:47 +00005628 checkHiddenAPIIndexFromClassesInputs(t, ctx, `out/soong/.intermediates/platform/foo/android_common/javac/foo.jar`)
Paul Duffin40a3f652021-07-19 13:11:24 +01005629 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005630 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005631 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005632 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 +01005633 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005634 })
Spandan Das3a392012024-01-17 18:26:27 +00005635
Spandan Dasf2c10572024-02-27 04:49:52 +00005636 t.Run("Co-existing unflagged apexes should create a duplicate module error", func(t *testing.T) {
Spandan Das3a392012024-01-17 18:26:27 +00005637 bp := `
5638 // Source
5639 apex {
5640 name: "myapex",
5641 enabled: false,
5642 key: "myapex.key",
5643 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5644 }
5645
5646 apex_key {
5647 name: "myapex.key",
5648 public_key: "testkey.avbpubkey",
5649 private_key: "testkey.pem",
5650 }
5651
5652 // Prebuilt
5653 prebuilt_apex {
5654 name: "myapex.v1",
5655 source_apex_name: "myapex",
5656 arch: {
5657 arm64: {
5658 src: "myapex-arm64.apex",
5659 },
5660 arm: {
5661 src: "myapex-arm.apex",
5662 },
5663 },
5664 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5665 prefer: true,
5666 }
5667 prebuilt_apex {
5668 name: "myapex.v2",
5669 source_apex_name: "myapex",
5670 arch: {
5671 arm64: {
5672 src: "myapex-arm64.apex",
5673 },
5674 arm: {
5675 src: "myapex-arm.apex",
5676 },
5677 },
5678 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5679 prefer: true,
5680 }
5681
5682 prebuilt_bootclasspath_fragment {
5683 name: "my-bootclasspath-fragment",
5684 contents: ["libfoo", "libbar"],
5685 apex_available: ["myapex"],
5686 hidden_api: {
5687 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5688 metadata: "my-bootclasspath-fragment/metadata.csv",
5689 index: "my-bootclasspath-fragment/index.csv",
5690 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5691 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5692 },
5693 prefer: true,
5694 }
5695
5696 java_import {
5697 name: "libfoo",
5698 jars: ["libfoo.jar"],
5699 apex_available: ["myapex"],
5700 prefer: true,
5701 }
5702 java_import {
5703 name: "libbar",
5704 jars: ["libbar.jar"],
5705 apex_available: ["myapex"],
5706 prefer: true,
5707 }
5708 `
5709
Spandan Dasf2c10572024-02-27 04:49:52 +00005710 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 +00005711 })
5712
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005713}
5714
Roland Levillain630846d2019-06-26 12:48:34 +01005715func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005716 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005717 apex_test {
5718 name: "myapex",
5719 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005720 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005721 tests: [
5722 "mytest",
5723 ],
5724 }
5725
5726 apex_key {
5727 name: "myapex.key",
5728 public_key: "testkey.avbpubkey",
5729 private_key: "testkey.pem",
5730 }
5731
Liz Kammer1c14a212020-05-12 15:26:55 -07005732 filegroup {
5733 name: "fg",
5734 srcs: [
5735 "baz",
5736 "bar/baz"
5737 ],
5738 }
5739
Roland Levillain630846d2019-06-26 12:48:34 +01005740 cc_test {
5741 name: "mytest",
5742 gtest: false,
5743 srcs: ["mytest.cpp"],
5744 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005745 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005746 system_shared_libs: [],
5747 static_executable: true,
5748 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005749 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005750 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005751
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005752 cc_library {
5753 name: "mylib",
5754 srcs: ["mylib.cpp"],
5755 system_shared_libs: [],
5756 stl: "none",
5757 }
5758
Liz Kammer5bd365f2020-05-27 15:15:11 -07005759 filegroup {
5760 name: "fg2",
5761 srcs: [
5762 "testdata/baz"
5763 ],
5764 }
Roland Levillain630846d2019-06-26 12:48:34 +01005765 `)
5766
Jooyung Hana0503a52023-08-23 13:12:50 +09005767 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005768 copyCmds := apexRule.Args["copy_commands"]
5769
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005770 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005771 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005772 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005773
Liz Kammer1c14a212020-05-12 15:26:55 -07005774 //Ensure that test data are copied into apex.
5775 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5776 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5777
Roland Levillainf89cd092019-07-29 16:22:59 +01005778 // Ensure the module is correctly translated.
Jooyung Hana0503a52023-08-23 13:12:50 +09005779 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005780 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005781 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005782 prefix := "TARGET_"
5783 var builder strings.Builder
5784 data.Custom(&builder, name, prefix, "", data)
5785 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005786 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005787 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005788}
5789
Jooyung Hand48f3c32019-08-23 11:18:57 +09005790func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5791 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5792 apex {
5793 name: "myapex",
5794 key: "myapex.key",
5795 native_shared_libs: ["libfoo"],
5796 }
5797
5798 apex_key {
5799 name: "myapex.key",
5800 public_key: "testkey.avbpubkey",
5801 private_key: "testkey.pem",
5802 }
5803
5804 cc_library {
5805 name: "libfoo",
5806 stl: "none",
5807 system_shared_libs: [],
5808 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005809 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005810 }
5811 `)
5812 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5813 apex {
5814 name: "myapex",
5815 key: "myapex.key",
5816 java_libs: ["myjar"],
5817 }
5818
5819 apex_key {
5820 name: "myapex.key",
5821 public_key: "testkey.avbpubkey",
5822 private_key: "testkey.pem",
5823 }
5824
5825 java_library {
5826 name: "myjar",
5827 srcs: ["foo/bar/MyClass.java"],
5828 sdk_version: "none",
5829 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005830 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005831 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005832 }
5833 `)
5834}
5835
Bill Peckhama41a6962021-01-11 10:58:54 -08005836func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005837 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005838 apex {
5839 name: "myapex",
5840 key: "myapex.key",
5841 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005842 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005843 }
5844
5845 apex_key {
5846 name: "myapex.key",
5847 public_key: "testkey.avbpubkey",
5848 private_key: "testkey.pem",
5849 }
5850
5851 java_import {
5852 name: "myjavaimport",
5853 apex_available: ["myapex"],
5854 jars: ["my.jar"],
5855 compile_dex: true,
5856 }
5857 `)
5858
Jooyung Hana0503a52023-08-23 13:12:50 +09005859 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Bill Peckhama41a6962021-01-11 10:58:54 -08005860 apexRule := module.Rule("apexRule")
5861 copyCmds := apexRule.Args["copy_commands"]
5862 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5863}
5864
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005865func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005866 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005867 apex {
5868 name: "myapex",
5869 key: "myapex.key",
5870 apps: [
5871 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005872 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005873 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005874 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005875 }
5876
5877 apex_key {
5878 name: "myapex.key",
5879 public_key: "testkey.avbpubkey",
5880 private_key: "testkey.pem",
5881 }
5882
5883 android_app {
5884 name: "AppFoo",
5885 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005886 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005887 system_modules: "none",
Jiyong Park970c5242024-05-17 22:58:54 +00005888 use_embedded_native_libs: true,
Jiyong Park8be103b2019-11-08 15:53:48 +09005889 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005890 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005891 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005892 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005893
5894 android_app {
5895 name: "AppFooPriv",
5896 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005897 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005898 system_modules: "none",
5899 privileged: true,
Sam Delmerico15809f82023-05-15 17:21:47 -04005900 privapp_allowlist: "privapp_allowlist_com.android.AppFooPriv.xml",
Colin Cross094cde42020-02-15 10:38:00 -08005901 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005902 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005903 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005904
5905 cc_library_shared {
5906 name: "libjni",
5907 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005908 shared_libs: ["libfoo"],
5909 stl: "none",
5910 system_shared_libs: [],
5911 apex_available: [ "myapex" ],
5912 sdk_version: "current",
5913 }
5914
5915 cc_library_shared {
5916 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005917 stl: "none",
5918 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005919 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005920 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005921 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005922 `)
5923
Jooyung Hana0503a52023-08-23 13:12:50 +09005924 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005925 apexRule := module.Rule("apexRule")
5926 copyCmds := apexRule.Args["copy_commands"]
5927
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005928 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5929 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Andrei Onea580636b2022-08-17 16:53:46 +00005930 ensureContains(t, copyCmds, "image.apex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005931
Colin Crossaede88c2020-08-11 12:17:01 -07005932 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005933 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005934 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005935 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005936 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005937 // JNI libraries including transitive deps are
5938 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005939 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005940 // ... embedded inside APK (jnilibs.zip)
5941 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5942 // ... and not directly inside the APEX
5943 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5944 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04005945
5946 apexBundle := module.Module().(*apexBundle)
5947 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
5948 var builder strings.Builder
5949 data.Custom(&builder, apexBundle.Name(), "TARGET_", "", data)
5950 androidMk := builder.String()
5951 ensureContains(t, androidMk, "LOCAL_MODULE := AppFooPriv.myapex")
5952 ensureContains(t, androidMk, "LOCAL_MODULE := AppFoo.myapex")
5953 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFooPriv.apk")
5954 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFoo.apk")
5955 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := \\S+AppFooPriv.apk")
5956 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 +01005957}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005958
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005959func TestApexWithAppImportBuildId(t *testing.T) {
5960 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5961 for _, id := range invalidBuildIds {
5962 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5963 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5964 variables.BuildId = proptools.StringPtr(id)
5965 })
5966 testApexError(t, message, `apex {
5967 name: "myapex",
5968 key: "myapex.key",
5969 apps: ["AppFooPrebuilt"],
5970 updatable: false,
5971 }
5972
5973 apex_key {
5974 name: "myapex.key",
5975 public_key: "testkey.avbpubkey",
5976 private_key: "testkey.pem",
5977 }
5978
5979 android_app_import {
5980 name: "AppFooPrebuilt",
5981 apk: "PrebuiltAppFoo.apk",
5982 presigned: true,
5983 apex_available: ["myapex"],
5984 }
5985 `, fixture)
5986 }
5987}
5988
Dario Frenicde2a032019-10-27 00:29:22 +01005989func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005990 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005991 apex {
5992 name: "myapex",
5993 key: "myapex.key",
5994 apps: [
5995 "AppFooPrebuilt",
5996 "AppFooPrivPrebuilt",
5997 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005998 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005999 }
6000
6001 apex_key {
6002 name: "myapex.key",
6003 public_key: "testkey.avbpubkey",
6004 private_key: "testkey.pem",
6005 }
6006
6007 android_app_import {
6008 name: "AppFooPrebuilt",
6009 apk: "PrebuiltAppFoo.apk",
6010 presigned: true,
6011 dex_preopt: {
6012 enabled: false,
6013 },
Jiyong Park592a6a42020-04-21 22:34:28 +09006014 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006015 }
6016
6017 android_app_import {
6018 name: "AppFooPrivPrebuilt",
6019 apk: "PrebuiltAppFooPriv.apk",
6020 privileged: true,
6021 presigned: true,
6022 dex_preopt: {
6023 enabled: false,
6024 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006025 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006026 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006027 }
6028 `)
6029
Jooyung Hana0503a52023-08-23 13:12:50 +09006030 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Frenicde2a032019-10-27 00:29:22 +01006031 apexRule := module.Rule("apexRule")
6032 copyCmds := apexRule.Args["copy_commands"]
6033
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006034 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6035 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006036}
6037
6038func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006039 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006040 apex {
6041 name: "myapex",
6042 key: "myapex.key",
6043 apps: [
6044 "AppFoo",
6045 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006046 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006047 }
6048
6049 apex_key {
6050 name: "myapex.key",
6051 public_key: "testkey.avbpubkey",
6052 private_key: "testkey.pem",
6053 }
6054
6055 android_app {
6056 name: "AppFoo",
6057 srcs: ["foo/bar/MyClass.java"],
6058 sdk_version: "none",
6059 system_modules: "none",
6060 apex_available: [ "myapex" ],
6061 }
6062
6063 android_app_import {
6064 name: "AppFoo",
6065 apk: "AppFooPrebuilt.apk",
6066 filename: "AppFooPrebuilt.apk",
6067 presigned: true,
6068 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006069 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006070 }
6071 `, withFiles(map[string][]byte{
6072 "AppFooPrebuilt.apk": nil,
6073 }))
6074
Jooyung Hana0503a52023-08-23 13:12:50 +09006075 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006076 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006077 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006078}
6079
Dario Freni6f3937c2019-12-20 22:58:03 +00006080func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006081 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006082 apex {
6083 name: "myapex",
6084 key: "myapex.key",
6085 apps: [
6086 "TesterHelpAppFoo",
6087 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006088 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006089 }
6090
6091 apex_key {
6092 name: "myapex.key",
6093 public_key: "testkey.avbpubkey",
6094 private_key: "testkey.pem",
6095 }
6096
6097 android_test_helper_app {
6098 name: "TesterHelpAppFoo",
6099 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006100 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006101 }
6102
6103 `)
6104
Jooyung Hana0503a52023-08-23 13:12:50 +09006105 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Freni6f3937c2019-12-20 22:58:03 +00006106 apexRule := module.Rule("apexRule")
6107 copyCmds := apexRule.Args["copy_commands"]
6108
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006109 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006110}
6111
Jooyung Han18020ea2019-11-13 10:50:48 +09006112func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6113 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006114 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006115 apex {
6116 name: "myapex",
6117 key: "myapex.key",
6118 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006119 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006120 }
6121
6122 apex_key {
6123 name: "myapex.key",
6124 public_key: "testkey.avbpubkey",
6125 private_key: "testkey.pem",
6126 }
6127
6128 apex {
6129 name: "otherapex",
6130 key: "myapex.key",
6131 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006132 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006133 }
6134
6135 cc_defaults {
6136 name: "libfoo-defaults",
6137 apex_available: ["otherapex"],
6138 }
6139
6140 cc_library {
6141 name: "libfoo",
6142 defaults: ["libfoo-defaults"],
6143 stl: "none",
6144 system_shared_libs: [],
6145 }`)
6146}
6147
Paul Duffine52e66f2020-03-30 17:54:29 +01006148func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006149 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006150 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006151 apex {
6152 name: "myapex",
6153 key: "myapex.key",
6154 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006155 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006156 }
6157
6158 apex_key {
6159 name: "myapex.key",
6160 public_key: "testkey.avbpubkey",
6161 private_key: "testkey.pem",
6162 }
6163
6164 apex {
6165 name: "otherapex",
6166 key: "otherapex.key",
6167 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006168 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006169 }
6170
6171 apex_key {
6172 name: "otherapex.key",
6173 public_key: "testkey.avbpubkey",
6174 private_key: "testkey.pem",
6175 }
6176
6177 cc_library {
6178 name: "libfoo",
6179 stl: "none",
6180 system_shared_libs: [],
6181 apex_available: ["otherapex"],
6182 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006183}
Jiyong Park127b40b2019-09-30 16:04:35 +09006184
Paul Duffine52e66f2020-03-30 17:54:29 +01006185func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006186 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006187 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006188.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006189.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006190.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006191.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006192.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006193.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006194 apex {
6195 name: "myapex",
6196 key: "myapex.key",
6197 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006198 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006199 }
6200
6201 apex_key {
6202 name: "myapex.key",
6203 public_key: "testkey.avbpubkey",
6204 private_key: "testkey.pem",
6205 }
6206
Jiyong Park127b40b2019-09-30 16:04:35 +09006207 cc_library {
6208 name: "libfoo",
6209 stl: "none",
6210 shared_libs: ["libbar"],
6211 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006212 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006213 }
6214
6215 cc_library {
6216 name: "libbar",
6217 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006218 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006219 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006220 apex_available: ["myapex"],
6221 }
6222
6223 cc_library {
6224 name: "libbaz",
6225 stl: "none",
6226 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006227 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006228}
Jiyong Park127b40b2019-09-30 16:04:35 +09006229
Liz Kammer5f108fa2023-05-11 14:33:17 -04006230func TestApexAvailable_IndirectStaticDep(t *testing.T) {
6231 testApex(t, `
6232 apex {
6233 name: "myapex",
6234 key: "myapex.key",
6235 native_shared_libs: ["libfoo"],
6236 updatable: false,
6237 }
6238
6239 apex_key {
6240 name: "myapex.key",
6241 public_key: "testkey.avbpubkey",
6242 private_key: "testkey.pem",
6243 }
6244
6245 cc_library {
6246 name: "libfoo",
6247 stl: "none",
6248 static_libs: ["libbar"],
6249 system_shared_libs: [],
6250 apex_available: ["myapex"],
6251 }
6252
6253 cc_library {
6254 name: "libbar",
6255 stl: "none",
6256 shared_libs: ["libbaz"],
6257 system_shared_libs: [],
6258 apex_available: ["myapex"],
6259 }
6260
6261 cc_library {
6262 name: "libbaz",
6263 stl: "none",
6264 system_shared_libs: [],
6265 }`)
6266
6267 testApexError(t, `requires "libbar" that doesn't list the APEX under 'apex_available'.`, `
6268 apex {
6269 name: "myapex",
6270 key: "myapex.key",
6271 native_shared_libs: ["libfoo"],
6272 updatable: false,
6273 }
6274
6275 apex_key {
6276 name: "myapex.key",
6277 public_key: "testkey.avbpubkey",
6278 private_key: "testkey.pem",
6279 }
6280
6281 cc_library {
6282 name: "libfoo",
6283 stl: "none",
6284 static_libs: ["libbar"],
6285 system_shared_libs: [],
6286 apex_available: ["myapex"],
6287 }
6288
6289 cc_library {
6290 name: "libbar",
6291 stl: "none",
6292 system_shared_libs: [],
6293 }`)
6294}
6295
Paul Duffine52e66f2020-03-30 17:54:29 +01006296func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006297 testApexError(t, "\"otherapex\" is not a valid module name", `
6298 apex {
6299 name: "myapex",
6300 key: "myapex.key",
6301 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006302 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006303 }
6304
6305 apex_key {
6306 name: "myapex.key",
6307 public_key: "testkey.avbpubkey",
6308 private_key: "testkey.pem",
6309 }
6310
6311 cc_library {
6312 name: "libfoo",
6313 stl: "none",
6314 system_shared_libs: [],
6315 apex_available: ["otherapex"],
6316 }`)
6317
Paul Duffine52e66f2020-03-30 17:54:29 +01006318 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006319 apex {
6320 name: "myapex",
6321 key: "myapex.key",
6322 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006323 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006324 }
6325
6326 apex_key {
6327 name: "myapex.key",
6328 public_key: "testkey.avbpubkey",
6329 private_key: "testkey.pem",
6330 }
6331
6332 cc_library {
6333 name: "libfoo",
6334 stl: "none",
6335 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006336 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006337 apex_available: ["myapex"],
6338 }
6339
6340 cc_library {
6341 name: "libbar",
6342 stl: "none",
6343 system_shared_libs: [],
6344 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006345 }
6346
6347 cc_library {
6348 name: "libbaz",
6349 stl: "none",
6350 system_shared_libs: [],
6351 stubs: {
6352 versions: ["10", "20", "30"],
6353 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006354 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006355}
Jiyong Park127b40b2019-09-30 16:04:35 +09006356
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006357func TestApexAvailable_ApexAvailableNameWithVersionCodeError(t *testing.T) {
6358 t.Run("negative variant_version produces error", func(t *testing.T) {
6359 testApexError(t, "expected an integer between 0-9; got -1", `
6360 apex {
6361 name: "myapex",
6362 key: "myapex.key",
6363 apex_available_name: "com.android.foo",
6364 variant_version: "-1",
6365 updatable: false,
6366 }
6367 apex_key {
6368 name: "myapex.key",
6369 public_key: "testkey.avbpubkey",
6370 private_key: "testkey.pem",
6371 }
6372 `)
6373 })
6374
6375 t.Run("variant_version greater than 9 produces error", func(t *testing.T) {
6376 testApexError(t, "expected an integer between 0-9; got 10", `
6377 apex {
6378 name: "myapex",
6379 key: "myapex.key",
6380 apex_available_name: "com.android.foo",
6381 variant_version: "10",
6382 updatable: false,
6383 }
6384 apex_key {
6385 name: "myapex.key",
6386 public_key: "testkey.avbpubkey",
6387 private_key: "testkey.pem",
6388 }
6389 `)
6390 })
6391}
6392
6393func TestApexAvailable_ApexAvailableNameWithVersionCode(t *testing.T) {
6394 context := android.GroupFixturePreparers(
6395 android.PrepareForIntegrationTestWithAndroid,
6396 PrepareForTestWithApexBuildComponents,
6397 android.FixtureMergeMockFs(android.MockFS{
6398 "system/sepolicy/apex/foo-file_contexts": nil,
6399 "system/sepolicy/apex/bar-file_contexts": nil,
6400 }),
6401 )
6402 result := context.RunTestWithBp(t, `
6403 apex {
6404 name: "foo",
6405 key: "myapex.key",
6406 apex_available_name: "com.android.foo",
6407 variant_version: "0",
6408 updatable: false,
6409 }
6410 apex {
6411 name: "bar",
6412 key: "myapex.key",
6413 apex_available_name: "com.android.foo",
6414 variant_version: "3",
6415 updatable: false,
6416 }
6417 apex_key {
6418 name: "myapex.key",
6419 public_key: "testkey.avbpubkey",
6420 private_key: "testkey.pem",
6421 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006422 override_apex {
6423 name: "myoverrideapex",
6424 base: "bar",
6425 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006426 `)
6427
Jooyung Hana0503a52023-08-23 13:12:50 +09006428 fooManifestRule := result.ModuleForTests("foo", "android_common_foo").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006429 fooExpectedDefaultVersion := android.DefaultUpdatableModuleVersion
6430 fooActualDefaultVersion := fooManifestRule.Args["default_version"]
6431 if fooActualDefaultVersion != fooExpectedDefaultVersion {
6432 t.Errorf("expected to find defaultVersion %q; got %q", fooExpectedDefaultVersion, fooActualDefaultVersion)
6433 }
6434
Jooyung Hana0503a52023-08-23 13:12:50 +09006435 barManifestRule := result.ModuleForTests("bar", "android_common_bar").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006436 defaultVersionInt, _ := strconv.Atoi(android.DefaultUpdatableModuleVersion)
6437 barExpectedDefaultVersion := fmt.Sprint(defaultVersionInt + 3)
6438 barActualDefaultVersion := barManifestRule.Args["default_version"]
6439 if barActualDefaultVersion != barExpectedDefaultVersion {
6440 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6441 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006442
Spandan Das50801e22024-05-13 18:29:45 +00006443 overrideBarManifestRule := result.ModuleForTests("bar", "android_common_myoverrideapex_myoverrideapex").Rule("apexManifestRule")
Sam Delmerico419f9a32023-07-21 12:00:13 -04006444 overrideBarActualDefaultVersion := overrideBarManifestRule.Args["default_version"]
6445 if overrideBarActualDefaultVersion != barExpectedDefaultVersion {
6446 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6447 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006448}
6449
Sam Delmericoca816532023-06-02 14:09:50 -04006450func TestApexAvailable_ApexAvailableName(t *testing.T) {
6451 t.Run("using name of apex that sets apex_available_name is not allowed", func(t *testing.T) {
6452 testApexError(t, "Consider adding \"myapex\" to 'apex_available' property of \"AppFoo\"", `
6453 apex {
6454 name: "myapex_sminus",
6455 key: "myapex.key",
6456 apps: ["AppFoo"],
6457 apex_available_name: "myapex",
6458 updatable: false,
6459 }
6460 apex {
6461 name: "myapex",
6462 key: "myapex.key",
6463 apps: ["AppFoo"],
6464 updatable: false,
6465 }
6466 apex_key {
6467 name: "myapex.key",
6468 public_key: "testkey.avbpubkey",
6469 private_key: "testkey.pem",
6470 }
6471 android_app {
6472 name: "AppFoo",
6473 srcs: ["foo/bar/MyClass.java"],
6474 sdk_version: "none",
6475 system_modules: "none",
6476 apex_available: [ "myapex_sminus" ],
6477 }`,
6478 android.FixtureMergeMockFs(android.MockFS{
6479 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6480 }),
6481 )
6482 })
6483
6484 t.Run("apex_available_name allows module to be used in two different apexes", func(t *testing.T) {
6485 testApex(t, `
6486 apex {
6487 name: "myapex_sminus",
6488 key: "myapex.key",
6489 apps: ["AppFoo"],
6490 apex_available_name: "myapex",
6491 updatable: false,
6492 }
6493 apex {
6494 name: "myapex",
6495 key: "myapex.key",
6496 apps: ["AppFoo"],
6497 updatable: false,
6498 }
6499 apex_key {
6500 name: "myapex.key",
6501 public_key: "testkey.avbpubkey",
6502 private_key: "testkey.pem",
6503 }
6504 android_app {
6505 name: "AppFoo",
6506 srcs: ["foo/bar/MyClass.java"],
6507 sdk_version: "none",
6508 system_modules: "none",
6509 apex_available: [ "myapex" ],
6510 }`,
6511 android.FixtureMergeMockFs(android.MockFS{
6512 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6513 }),
6514 )
6515 })
6516
6517 t.Run("override_apexes work with apex_available_name", func(t *testing.T) {
6518 testApex(t, `
6519 override_apex {
6520 name: "myoverrideapex_sminus",
6521 base: "myapex_sminus",
6522 key: "myapex.key",
6523 apps: ["AppFooOverride"],
6524 }
6525 override_apex {
6526 name: "myoverrideapex",
6527 base: "myapex",
6528 key: "myapex.key",
6529 apps: ["AppFooOverride"],
6530 }
6531 apex {
6532 name: "myapex_sminus",
6533 key: "myapex.key",
6534 apps: ["AppFoo"],
6535 apex_available_name: "myapex",
6536 updatable: false,
6537 }
6538 apex {
6539 name: "myapex",
6540 key: "myapex.key",
6541 apps: ["AppFoo"],
6542 updatable: false,
6543 }
6544 apex_key {
6545 name: "myapex.key",
6546 public_key: "testkey.avbpubkey",
6547 private_key: "testkey.pem",
6548 }
6549 android_app {
6550 name: "AppFooOverride",
6551 srcs: ["foo/bar/MyClass.java"],
6552 sdk_version: "none",
6553 system_modules: "none",
6554 apex_available: [ "myapex" ],
6555 }
6556 android_app {
6557 name: "AppFoo",
6558 srcs: ["foo/bar/MyClass.java"],
6559 sdk_version: "none",
6560 system_modules: "none",
6561 apex_available: [ "myapex" ],
6562 }`,
6563 android.FixtureMergeMockFs(android.MockFS{
6564 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6565 }),
6566 )
6567 })
6568}
6569
6570func TestApexAvailable_ApexAvailableNameWithOverrides(t *testing.T) {
6571 context := android.GroupFixturePreparers(
6572 android.PrepareForIntegrationTestWithAndroid,
6573 PrepareForTestWithApexBuildComponents,
6574 java.PrepareForTestWithDexpreopt,
6575 android.FixtureMergeMockFs(android.MockFS{
6576 "system/sepolicy/apex/myapex-file_contexts": nil,
6577 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6578 }),
6579 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6580 variables.BuildId = proptools.StringPtr("buildid")
6581 }),
6582 )
6583 context.RunTestWithBp(t, `
6584 override_apex {
6585 name: "myoverrideapex_sminus",
6586 base: "myapex_sminus",
6587 }
6588 override_apex {
6589 name: "myoverrideapex",
6590 base: "myapex",
6591 }
6592 apex {
6593 name: "myapex",
6594 key: "myapex.key",
6595 apps: ["AppFoo"],
6596 updatable: false,
6597 }
6598 apex {
6599 name: "myapex_sminus",
6600 apex_available_name: "myapex",
6601 key: "myapex.key",
6602 apps: ["AppFoo_sminus"],
6603 updatable: false,
6604 }
6605 apex_key {
6606 name: "myapex.key",
6607 public_key: "testkey.avbpubkey",
6608 private_key: "testkey.pem",
6609 }
6610 android_app {
6611 name: "AppFoo",
6612 srcs: ["foo/bar/MyClass.java"],
6613 sdk_version: "none",
6614 system_modules: "none",
6615 apex_available: [ "myapex" ],
6616 }
6617 android_app {
6618 name: "AppFoo_sminus",
6619 srcs: ["foo/bar/MyClass.java"],
6620 sdk_version: "none",
6621 min_sdk_version: "29",
6622 system_modules: "none",
6623 apex_available: [ "myapex" ],
6624 }`)
6625}
6626
Jiyong Park89e850a2020-04-07 16:37:39 +09006627func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006628 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006629 apex {
6630 name: "myapex",
6631 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006632 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006633 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006634 }
6635
6636 apex_key {
6637 name: "myapex.key",
6638 public_key: "testkey.avbpubkey",
6639 private_key: "testkey.pem",
6640 }
6641
6642 cc_library {
6643 name: "libfoo",
6644 stl: "none",
6645 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006646 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006647 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006648 }
6649
6650 cc_library {
6651 name: "libfoo2",
6652 stl: "none",
6653 system_shared_libs: [],
6654 shared_libs: ["libbaz"],
6655 apex_available: ["//apex_available:platform"],
6656 }
6657
6658 cc_library {
6659 name: "libbar",
6660 stl: "none",
6661 system_shared_libs: [],
6662 apex_available: ["myapex"],
6663 }
6664
6665 cc_library {
6666 name: "libbaz",
6667 stl: "none",
6668 system_shared_libs: [],
6669 apex_available: ["myapex"],
6670 stubs: {
6671 versions: ["1"],
6672 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006673 }`)
6674
Jiyong Park89e850a2020-04-07 16:37:39 +09006675 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6676 // because it depends on libbar which isn't available to platform
6677 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6678 if libfoo.NotAvailableForPlatform() != true {
6679 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6680 }
6681
6682 // libfoo2 however can be available to platform because it depends on libbaz which provides
6683 // stubs
6684 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6685 if libfoo2.NotAvailableForPlatform() == true {
6686 t.Errorf("%q should be available to platform", libfoo2.String())
6687 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006688}
Jiyong Parka90ca002019-10-07 15:47:24 +09006689
Paul Duffine52e66f2020-03-30 17:54:29 +01006690func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006691 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006692 apex {
6693 name: "myapex",
6694 key: "myapex.key",
6695 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006696 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006697 }
6698
6699 apex_key {
6700 name: "myapex.key",
6701 public_key: "testkey.avbpubkey",
6702 private_key: "testkey.pem",
6703 }
6704
6705 cc_library {
6706 name: "libfoo",
6707 stl: "none",
6708 system_shared_libs: [],
6709 apex_available: ["myapex"],
6710 static: {
6711 apex_available: ["//apex_available:platform"],
6712 },
6713 }`)
6714
Jiyong Park89e850a2020-04-07 16:37:39 +09006715 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6716 if libfooShared.NotAvailableForPlatform() != true {
6717 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6718 }
6719 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6720 if libfooStatic.NotAvailableForPlatform() != false {
6721 t.Errorf("%q should be available to platform", libfooStatic.String())
6722 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006723}
6724
Jiyong Park5d790c32019-11-15 18:40:32 +09006725func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006726 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006727 apex {
6728 name: "myapex",
6729 key: "myapex.key",
6730 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006731 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006732 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006733 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006734 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006735 }
6736
6737 override_apex {
6738 name: "override_myapex",
6739 base: "myapex",
6740 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006741 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006742 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006743 overrides: ["unknownapex"],
Jesse Melhuishec60e252024-03-29 19:08:20 +00006744 compile_multilib: "first",
6745 multilib: {
6746 lib32: {
6747 native_shared_libs: ["mylib32"],
6748 },
6749 lib64: {
6750 native_shared_libs: ["mylib64"],
6751 },
6752 },
Baligh Uddin004d7172020-02-19 21:29:28 -08006753 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006754 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006755 key: "mynewapex.key",
6756 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006757 }
6758
6759 apex_key {
6760 name: "myapex.key",
6761 public_key: "testkey.avbpubkey",
6762 private_key: "testkey.pem",
6763 }
6764
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006765 apex_key {
6766 name: "mynewapex.key",
6767 public_key: "testkey2.avbpubkey",
6768 private_key: "testkey2.pem",
6769 }
6770
6771 android_app_certificate {
6772 name: "myapex.certificate",
6773 certificate: "testkey",
6774 }
6775
Jiyong Park5d790c32019-11-15 18:40:32 +09006776 android_app {
6777 name: "app",
6778 srcs: ["foo/bar/MyClass.java"],
6779 package_name: "foo",
6780 sdk_version: "none",
6781 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006782 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006783 }
6784
6785 override_android_app {
6786 name: "override_app",
6787 base: "app",
6788 package_name: "bar",
6789 }
markchien7c803b82021-08-26 22:10:06 +08006790
6791 bpf {
6792 name: "bpf",
6793 srcs: ["bpf.c"],
6794 }
6795
6796 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006797 name: "overrideBpf",
6798 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006799 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006800
6801 prebuilt_etc {
6802 name: "myetc",
6803 src: "myprebuilt",
6804 }
6805
6806 prebuilt_etc {
6807 name: "override_myetc",
6808 src: "override_myprebuilt",
6809 }
Jesse Melhuishec60e252024-03-29 19:08:20 +00006810
6811 cc_library {
6812 name: "mylib32",
6813 apex_available: [ "myapex" ],
6814 }
6815
6816 cc_library {
6817 name: "mylib64",
6818 apex_available: [ "myapex" ],
6819 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006820 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006821
Jooyung Hana0503a52023-08-23 13:12:50 +09006822 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(android.OverridableModule)
Spandan Das50801e22024-05-13 18:29:45 +00006823 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex").Module().(android.OverridableModule)
Jiyong Park317645e2019-12-05 13:20:58 +09006824 if originalVariant.GetOverriddenBy() != "" {
6825 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6826 }
6827 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6828 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6829 }
6830
Spandan Das50801e22024-05-13 18:29:45 +00006831 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006832 apexRule := module.Rule("apexRule")
6833 copyCmds := apexRule.Args["copy_commands"]
6834
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006835 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6836 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006837
markchien7c803b82021-08-26 22:10:06 +08006838 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006839 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006840
Daniel Norman5a3ce132021-08-26 15:44:43 -07006841 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6842 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6843
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006844 apexBundle := module.Module().(*apexBundle)
6845 name := apexBundle.Name()
6846 if name != "override_myapex" {
6847 t.Errorf("name should be \"override_myapex\", but was %q", name)
6848 }
6849
Baligh Uddin004d7172020-02-19 21:29:28 -08006850 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6851 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6852 }
6853
Jiyong Park20bacab2020-03-03 11:45:41 +09006854 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006855 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006856 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6857
6858 signApkRule := module.Rule("signapk")
6859 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006860
Colin Crossaa255532020-07-03 13:18:24 -07006861 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006862 var builder strings.Builder
6863 data.Custom(&builder, name, "TARGET_", "", data)
6864 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006865 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6866 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006867 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006868 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006869 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006870 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006871 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006872 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006873}
6874
Albert Martineefabcf2022-03-21 20:11:16 +00006875func TestMinSdkVersionOverride(t *testing.T) {
6876 // Override from 29 to 31
6877 minSdkOverride31 := "31"
6878 ctx := testApex(t, `
6879 apex {
6880 name: "myapex",
6881 key: "myapex.key",
6882 native_shared_libs: ["mylib"],
6883 updatable: true,
6884 min_sdk_version: "29"
6885 }
6886
6887 override_apex {
6888 name: "override_myapex",
6889 base: "myapex",
6890 logging_parent: "com.foo.bar",
6891 package_name: "test.overridden.package"
6892 }
6893
6894 apex_key {
6895 name: "myapex.key",
6896 public_key: "testkey.avbpubkey",
6897 private_key: "testkey.pem",
6898 }
6899
6900 cc_library {
6901 name: "mylib",
6902 srcs: ["mylib.cpp"],
6903 runtime_libs: ["libbar"],
6904 system_shared_libs: [],
6905 stl: "none",
6906 apex_available: [ "myapex" ],
6907 min_sdk_version: "apex_inherit"
6908 }
6909
6910 cc_library {
6911 name: "libbar",
6912 srcs: ["mylib.cpp"],
6913 system_shared_libs: [],
6914 stl: "none",
6915 apex_available: [ "myapex" ],
6916 min_sdk_version: "apex_inherit"
6917 }
6918
6919 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6920
Jooyung Hana0503a52023-08-23 13:12:50 +09006921 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00006922 copyCmds := apexRule.Args["copy_commands"]
6923
6924 // Ensure that direct non-stubs dep is always included
6925 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6926
6927 // Ensure that runtime_libs dep in included
6928 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6929
6930 // Ensure libraries target overridden min_sdk_version value
6931 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6932}
6933
6934func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6935 // Attempt to override from 31 to 29, should be a NOOP
6936 minSdkOverride29 := "29"
6937 ctx := testApex(t, `
6938 apex {
6939 name: "myapex",
6940 key: "myapex.key",
6941 native_shared_libs: ["mylib"],
6942 updatable: true,
6943 min_sdk_version: "31"
6944 }
6945
6946 override_apex {
6947 name: "override_myapex",
6948 base: "myapex",
6949 logging_parent: "com.foo.bar",
6950 package_name: "test.overridden.package"
6951 }
6952
6953 apex_key {
6954 name: "myapex.key",
6955 public_key: "testkey.avbpubkey",
6956 private_key: "testkey.pem",
6957 }
6958
6959 cc_library {
6960 name: "mylib",
6961 srcs: ["mylib.cpp"],
6962 runtime_libs: ["libbar"],
6963 system_shared_libs: [],
6964 stl: "none",
6965 apex_available: [ "myapex" ],
6966 min_sdk_version: "apex_inherit"
6967 }
6968
6969 cc_library {
6970 name: "libbar",
6971 srcs: ["mylib.cpp"],
6972 system_shared_libs: [],
6973 stl: "none",
6974 apex_available: [ "myapex" ],
6975 min_sdk_version: "apex_inherit"
6976 }
6977
6978 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6979
Jooyung Hana0503a52023-08-23 13:12:50 +09006980 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00006981 copyCmds := apexRule.Args["copy_commands"]
6982
6983 // Ensure that direct non-stubs dep is always included
6984 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6985
6986 // Ensure that runtime_libs dep in included
6987 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6988
6989 // Ensure libraries target the original min_sdk_version value rather than the overridden
6990 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6991}
6992
Jooyung Han214bf372019-11-12 13:03:50 +09006993func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006994 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006995 apex {
6996 name: "myapex",
6997 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006998 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006999 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09007000 }
7001
7002 apex_key {
7003 name: "myapex.key",
7004 public_key: "testkey.avbpubkey",
7005 private_key: "testkey.pem",
7006 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007007
7008 cc_library {
7009 name: "mylib",
7010 srcs: ["mylib.cpp"],
7011 stl: "libc++",
7012 system_shared_libs: [],
7013 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09007014 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007015 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007016 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09007017
Jooyung Hana0503a52023-08-23 13:12:50 +09007018 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +09007019 args := module.Rule("apexRule").Args
7020 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00007021 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007022
7023 // The copies of the libraries in the apex should have one more dependency than
7024 // the ones outside the apex, namely the unwinder. Ideally we should check
7025 // the dependency names directly here but for some reason the names are blank in
7026 // this test.
7027 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07007028 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007029 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
7030 if len(apexImplicits) != len(nonApexImplicits)+1 {
7031 t.Errorf("%q missing unwinder dep", lib)
7032 }
7033 }
Jooyung Han214bf372019-11-12 13:03:50 +09007034}
7035
Paul Duffine05480a2021-03-08 15:07:14 +00007036var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01007037 "api/current.txt": nil,
7038 "api/removed.txt": nil,
7039 "api/system-current.txt": nil,
7040 "api/system-removed.txt": nil,
7041 "api/test-current.txt": nil,
7042 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01007043
Anton Hanssondff2c782020-12-21 17:10:01 +00007044 "100/public/api/foo.txt": nil,
7045 "100/public/api/foo-removed.txt": nil,
7046 "100/system/api/foo.txt": nil,
7047 "100/system/api/foo-removed.txt": nil,
7048
Paul Duffineedc5d52020-06-12 17:46:39 +01007049 // For java_sdk_library_import
7050 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01007051}
7052
Jooyung Han58f26ab2019-12-18 15:34:32 +09007053func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007054 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09007055 apex {
7056 name: "myapex",
7057 key: "myapex.key",
7058 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007059 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09007060 }
7061
7062 apex_key {
7063 name: "myapex.key",
7064 public_key: "testkey.avbpubkey",
7065 private_key: "testkey.pem",
7066 }
7067
7068 java_sdk_library {
7069 name: "foo",
7070 srcs: ["a.java"],
7071 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007072 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09007073 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007074
7075 prebuilt_apis {
7076 name: "sdk",
7077 api_dirs: ["100"],
7078 }
Paul Duffin9b879592020-05-26 13:21:35 +01007079 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09007080
7081 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007082 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09007083 "javalib/foo.jar",
7084 "etc/permissions/foo.xml",
7085 })
7086 // Permission XML should point to the activated path of impl jar of java_sdk_library
Paul Duffin1816cde2024-04-10 10:58:21 +01007087 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Output("foo.xml")
7088 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7089 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
Jooyung Han58f26ab2019-12-18 15:34:32 +09007090}
7091
Spandan Das3ee19692024-06-19 04:47:40 +00007092func TestJavaSDKLibraryOverrideApexes(t *testing.T) {
7093 ctx := testApex(t, `
7094 override_apex {
7095 name: "mycompanyapex",
7096 base: "myapex",
7097 }
7098 apex {
7099 name: "myapex",
7100 key: "myapex.key",
7101 java_libs: ["foo"],
7102 updatable: false,
7103 }
7104
7105 apex_key {
7106 name: "myapex.key",
7107 public_key: "testkey.avbpubkey",
7108 private_key: "testkey.pem",
7109 }
7110
7111 java_sdk_library {
7112 name: "foo",
7113 srcs: ["a.java"],
7114 api_packages: ["foo"],
7115 apex_available: [ "myapex" ],
7116 }
7117
7118 prebuilt_apis {
7119 name: "sdk",
7120 api_dirs: ["100"],
7121 }
7122 `, withFiles(filesForSdkLibrary))
7123
7124 // Permission XML should point to the activated path of impl jar of java_sdk_library.
7125 // Since override variants (com.mycompany.android.foo) are installed in the same package as the overridden variant
7126 // (com.android.foo), the filepath should not contain override apex name.
7127 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_mycompanyapex").Output("foo.xml")
7128 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7129 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
7130}
7131
Paul Duffin9b879592020-05-26 13:21:35 +01007132func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007133 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007134 apex {
7135 name: "myapex",
7136 key: "myapex.key",
7137 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007138 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007139 }
7140
7141 apex_key {
7142 name: "myapex.key",
7143 public_key: "testkey.avbpubkey",
7144 private_key: "testkey.pem",
7145 }
7146
7147 java_sdk_library {
7148 name: "foo",
7149 srcs: ["a.java"],
7150 api_packages: ["foo"],
7151 apex_available: ["myapex"],
7152 sdk_version: "none",
7153 system_modules: "none",
7154 }
7155
7156 java_library {
7157 name: "bar",
7158 srcs: ["a.java"],
7159 libs: ["foo"],
7160 apex_available: ["myapex"],
7161 sdk_version: "none",
7162 system_modules: "none",
7163 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007164
7165 prebuilt_apis {
7166 name: "sdk",
7167 api_dirs: ["100"],
7168 }
Paul Duffin9b879592020-05-26 13:21:35 +01007169 `, withFiles(filesForSdkLibrary))
7170
7171 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007172 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007173 "javalib/bar.jar",
7174 "javalib/foo.jar",
7175 "etc/permissions/foo.xml",
7176 })
7177
7178 // The bar library should depend on the implementation jar.
7179 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kanga3a05462024-04-05 00:36:44 +00007180 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007181 t.Errorf("expected %q, found %#q", expected, actual)
7182 }
7183}
7184
7185func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007186 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007187 apex {
7188 name: "myapex",
7189 key: "myapex.key",
7190 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007191 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007192 }
7193
7194 apex_key {
7195 name: "myapex.key",
7196 public_key: "testkey.avbpubkey",
7197 private_key: "testkey.pem",
7198 }
7199
7200 java_sdk_library {
7201 name: "foo",
7202 srcs: ["a.java"],
7203 api_packages: ["foo"],
7204 apex_available: ["myapex"],
7205 sdk_version: "none",
7206 system_modules: "none",
7207 }
7208
7209 java_library {
7210 name: "bar",
7211 srcs: ["a.java"],
7212 libs: ["foo"],
7213 sdk_version: "none",
7214 system_modules: "none",
7215 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007216
7217 prebuilt_apis {
7218 name: "sdk",
7219 api_dirs: ["100"],
7220 }
Paul Duffin9b879592020-05-26 13:21:35 +01007221 `, withFiles(filesForSdkLibrary))
7222
7223 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007224 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007225 "javalib/foo.jar",
7226 "etc/permissions/foo.xml",
7227 })
7228
7229 // The bar library should depend on the stubs jar.
7230 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007231 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007232 t.Errorf("expected %q, found %#q", expected, actual)
7233 }
7234}
7235
Paul Duffineedc5d52020-06-12 17:46:39 +01007236func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007237 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00007238 prebuilt_apis {
7239 name: "sdk",
7240 api_dirs: ["100"],
7241 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01007242 withFiles(map[string][]byte{
7243 "apex/a.java": nil,
7244 "apex/apex_manifest.json": nil,
7245 "apex/Android.bp": []byte(`
7246 package {
7247 default_visibility: ["//visibility:private"],
7248 }
7249
7250 apex {
7251 name: "myapex",
7252 key: "myapex.key",
7253 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007254 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007255 }
7256
7257 apex_key {
7258 name: "myapex.key",
7259 public_key: "testkey.avbpubkey",
7260 private_key: "testkey.pem",
7261 }
7262
7263 java_library {
7264 name: "bar",
7265 srcs: ["a.java"],
7266 libs: ["foo"],
7267 apex_available: ["myapex"],
7268 sdk_version: "none",
7269 system_modules: "none",
7270 }
7271`),
7272 "source/a.java": nil,
7273 "source/api/current.txt": nil,
7274 "source/api/removed.txt": nil,
7275 "source/Android.bp": []byte(`
7276 package {
7277 default_visibility: ["//visibility:private"],
7278 }
7279
7280 java_sdk_library {
7281 name: "foo",
7282 visibility: ["//apex"],
7283 srcs: ["a.java"],
7284 api_packages: ["foo"],
7285 apex_available: ["myapex"],
7286 sdk_version: "none",
7287 system_modules: "none",
7288 public: {
7289 enabled: true,
7290 },
7291 }
7292`),
7293 "prebuilt/a.jar": nil,
7294 "prebuilt/Android.bp": []byte(`
7295 package {
7296 default_visibility: ["//visibility:private"],
7297 }
7298
7299 java_sdk_library_import {
7300 name: "foo",
7301 visibility: ["//apex", "//source"],
7302 apex_available: ["myapex"],
7303 prefer: true,
7304 public: {
7305 jars: ["a.jar"],
7306 },
7307 }
7308`),
Anton Hanssondff2c782020-12-21 17:10:01 +00007309 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01007310 )
7311
7312 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007313 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffineedc5d52020-06-12 17:46:39 +01007314 "javalib/bar.jar",
7315 "javalib/foo.jar",
7316 "etc/permissions/foo.xml",
7317 })
7318
7319 // The bar library should depend on the implementation jar.
7320 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kanga3a05462024-04-05 00:36:44 +00007321 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01007322 t.Errorf("expected %q, found %#q", expected, actual)
7323 }
7324}
7325
7326func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
7327 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
7328 apex {
7329 name: "myapex",
7330 key: "myapex.key",
7331 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007332 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007333 }
7334
7335 apex_key {
7336 name: "myapex.key",
7337 public_key: "testkey.avbpubkey",
7338 private_key: "testkey.pem",
7339 }
7340
7341 java_sdk_library_import {
7342 name: "foo",
7343 apex_available: ["myapex"],
7344 prefer: true,
7345 public: {
7346 jars: ["a.jar"],
7347 },
7348 }
7349
7350 `, withFiles(filesForSdkLibrary))
7351}
7352
atrost6e126252020-01-27 17:01:16 +00007353func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007354 result := android.GroupFixturePreparers(
7355 prepareForApexTest,
7356 java.PrepareForTestWithPlatformCompatConfig,
7357 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007358 apex {
7359 name: "myapex",
7360 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007361 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007362 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007363 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007364 }
7365
7366 apex_key {
7367 name: "myapex.key",
7368 public_key: "testkey.avbpubkey",
7369 private_key: "testkey.pem",
7370 }
7371
7372 platform_compat_config {
7373 name: "myjar-platform-compat-config",
7374 src: ":myjar",
7375 }
7376
7377 java_library {
7378 name: "myjar",
7379 srcs: ["foo/bar/MyClass.java"],
7380 sdk_version: "none",
7381 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007382 apex_available: [ "myapex" ],
7383 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007384
7385 // Make sure that a preferred prebuilt does not affect the apex contents.
7386 prebuilt_platform_compat_config {
7387 name: "myjar-platform-compat-config",
7388 metadata: "compat-config/metadata.xml",
7389 prefer: true,
7390 }
atrost6e126252020-01-27 17:01:16 +00007391 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007392 ctx := result.TestContext
Jooyung Hana0503a52023-08-23 13:12:50 +09007393 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
atrost6e126252020-01-27 17:01:16 +00007394 "etc/compatconfig/myjar-platform-compat-config.xml",
7395 "javalib/myjar.jar",
7396 })
7397}
7398
Jooyung Han862c0d62022-12-21 10:15:37 +09007399func TestNoDupeApexFiles(t *testing.T) {
7400 android.GroupFixturePreparers(
7401 android.PrepareForTestWithAndroidBuildComponents,
7402 PrepareForTestWithApexBuildComponents,
7403 prepareForTestWithMyapex,
7404 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7405 ).
7406 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7407 RunTestWithBp(t, `
7408 apex {
7409 name: "myapex",
7410 key: "myapex.key",
7411 prebuilts: ["foo", "bar"],
7412 updatable: false,
7413 }
7414
7415 apex_key {
7416 name: "myapex.key",
7417 public_key: "testkey.avbpubkey",
7418 private_key: "testkey.pem",
7419 }
7420
7421 prebuilt_etc {
7422 name: "foo",
7423 src: "myprebuilt",
7424 filename_from_src: true,
7425 }
7426
7427 prebuilt_etc {
7428 name: "bar",
7429 src: "myprebuilt",
7430 filename_from_src: true,
7431 }
7432 `)
7433}
7434
Jooyung Hana8bd72a2023-11-02 11:56:48 +09007435func TestApexUnwantedTransitiveDeps(t *testing.T) {
7436 bp := `
7437 apex {
7438 name: "myapex",
7439 key: "myapex.key",
7440 native_shared_libs: ["libfoo"],
7441 updatable: false,
7442 unwanted_transitive_deps: ["libbar"],
7443 }
7444
7445 apex_key {
7446 name: "myapex.key",
7447 public_key: "testkey.avbpubkey",
7448 private_key: "testkey.pem",
7449 }
7450
7451 cc_library {
7452 name: "libfoo",
7453 srcs: ["foo.cpp"],
7454 shared_libs: ["libbar"],
7455 apex_available: ["myapex"],
7456 }
7457
7458 cc_library {
7459 name: "libbar",
7460 srcs: ["bar.cpp"],
7461 apex_available: ["myapex"],
7462 }`
7463 ctx := testApex(t, bp)
7464 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
7465 "*/libc++.so",
7466 "*/libfoo.so",
7467 // not libbar.so
7468 })
7469}
7470
Jiyong Park479321d2019-12-16 11:47:12 +09007471func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7472 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7473 apex {
7474 name: "myapex",
7475 key: "myapex.key",
7476 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007477 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007478 }
7479
7480 apex_key {
7481 name: "myapex.key",
7482 public_key: "testkey.avbpubkey",
7483 private_key: "testkey.pem",
7484 }
7485
7486 java_library {
7487 name: "myjar",
7488 srcs: ["foo/bar/MyClass.java"],
7489 sdk_version: "none",
7490 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007491 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007492 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007493 }
7494 `)
7495}
7496
Jiyong Park7afd1072019-12-30 16:56:33 +09007497func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007498 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007499 apex {
7500 name: "myapex",
7501 key: "myapex.key",
7502 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007503 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007504 }
7505
7506 apex_key {
7507 name: "myapex.key",
7508 public_key: "testkey.avbpubkey",
7509 private_key: "testkey.pem",
7510 }
7511
7512 cc_library {
7513 name: "mylib",
7514 srcs: ["mylib.cpp"],
7515 system_shared_libs: [],
7516 stl: "none",
7517 required: ["a", "b"],
7518 host_required: ["c", "d"],
7519 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007520 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007521 }
7522 `)
7523
Jooyung Hana0503a52023-08-23 13:12:50 +09007524 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007525 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007526 name := apexBundle.BaseModuleName()
7527 prefix := "TARGET_"
7528 var builder strings.Builder
7529 data.Custom(&builder, name, prefix, "", data)
7530 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007531 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 a b\n")
Sasha Smundakdcb61292022-12-08 10:41:33 -08007532 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7533 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007534}
7535
Jiyong Park7cd10e32020-01-14 09:22:18 +09007536func TestSymlinksFromApexToSystem(t *testing.T) {
7537 bp := `
7538 apex {
7539 name: "myapex",
7540 key: "myapex.key",
7541 native_shared_libs: ["mylib"],
7542 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007543 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007544 }
7545
Jiyong Park9d677202020-02-19 16:29:35 +09007546 apex {
7547 name: "myapex.updatable",
7548 key: "myapex.key",
7549 native_shared_libs: ["mylib"],
7550 java_libs: ["myjar"],
7551 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007552 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007553 }
7554
Jiyong Park7cd10e32020-01-14 09:22:18 +09007555 apex_key {
7556 name: "myapex.key",
7557 public_key: "testkey.avbpubkey",
7558 private_key: "testkey.pem",
7559 }
7560
7561 cc_library {
7562 name: "mylib",
7563 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007564 shared_libs: [
7565 "myotherlib",
7566 "myotherlib_ext",
7567 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007568 system_shared_libs: [],
7569 stl: "none",
7570 apex_available: [
7571 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007572 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007573 "//apex_available:platform",
7574 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007575 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007576 }
7577
7578 cc_library {
7579 name: "myotherlib",
7580 srcs: ["mylib.cpp"],
7581 system_shared_libs: [],
7582 stl: "none",
7583 apex_available: [
7584 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007585 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007586 "//apex_available:platform",
7587 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007588 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007589 }
7590
Jiyong Parkce243632023-02-17 18:22:25 +09007591 cc_library {
7592 name: "myotherlib_ext",
7593 srcs: ["mylib.cpp"],
7594 system_shared_libs: [],
7595 system_ext_specific: true,
7596 stl: "none",
7597 apex_available: [
7598 "myapex",
7599 "myapex.updatable",
7600 "//apex_available:platform",
7601 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007602 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007603 }
7604
Jiyong Park7cd10e32020-01-14 09:22:18 +09007605 java_library {
7606 name: "myjar",
7607 srcs: ["foo/bar/MyClass.java"],
7608 sdk_version: "none",
7609 system_modules: "none",
7610 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007611 apex_available: [
7612 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007613 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007614 "//apex_available:platform",
7615 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007616 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007617 }
7618
7619 java_library {
7620 name: "myotherjar",
7621 srcs: ["foo/bar/MyClass.java"],
7622 sdk_version: "none",
7623 system_modules: "none",
7624 apex_available: [
7625 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007626 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007627 "//apex_available:platform",
7628 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007629 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007630 }
7631 `
7632
7633 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7634 for _, f := range files {
7635 if f.path == file {
7636 if f.isLink {
7637 t.Errorf("%q is not a real file", file)
7638 }
7639 return
7640 }
7641 }
7642 t.Errorf("%q is not found", file)
7643 }
7644
Jiyong Parkce243632023-02-17 18:22:25 +09007645 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007646 for _, f := range files {
7647 if f.path == file {
7648 if !f.isLink {
7649 t.Errorf("%q is not a symlink", file)
7650 }
Jiyong Parkce243632023-02-17 18:22:25 +09007651 if f.src != target {
7652 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7653 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007654 return
7655 }
7656 }
7657 t.Errorf("%q is not found", file)
7658 }
7659
Jiyong Park9d677202020-02-19 16:29:35 +09007660 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7661 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007662 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana0503a52023-08-23 13:12:50 +09007663 files := getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007664 ensureRealfileExists(t, files, "javalib/myjar.jar")
7665 ensureRealfileExists(t, files, "lib64/mylib.so")
7666 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007667 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007668
Jooyung Hana0503a52023-08-23 13:12:50 +09007669 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007670 ensureRealfileExists(t, files, "javalib/myjar.jar")
7671 ensureRealfileExists(t, files, "lib64/mylib.so")
7672 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007673 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007674
7675 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007676 ctx = testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +09007677 files = getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007678 ensureRealfileExists(t, files, "javalib/myjar.jar")
7679 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007680 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7681 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007682
Jooyung Hana0503a52023-08-23 13:12:50 +09007683 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007684 ensureRealfileExists(t, files, "javalib/myjar.jar")
7685 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007686 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7687 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007688}
7689
Yo Chiange8128052020-07-23 20:09:18 +08007690func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007691 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007692 apex {
7693 name: "myapex",
7694 key: "myapex.key",
7695 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007696 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007697 }
7698
7699 apex_key {
7700 name: "myapex.key",
7701 public_key: "testkey.avbpubkey",
7702 private_key: "testkey.pem",
7703 }
7704
7705 cc_library_shared {
7706 name: "mylib",
7707 srcs: ["mylib.cpp"],
7708 shared_libs: ["myotherlib"],
7709 system_shared_libs: [],
7710 stl: "none",
7711 apex_available: [
7712 "myapex",
7713 "//apex_available:platform",
7714 ],
7715 }
7716
7717 cc_prebuilt_library_shared {
7718 name: "myotherlib",
7719 srcs: ["prebuilt.so"],
7720 system_shared_libs: [],
7721 stl: "none",
7722 apex_available: [
7723 "myapex",
7724 "//apex_available:platform",
7725 ],
7726 }
7727 `)
7728
Jooyung Hana0503a52023-08-23 13:12:50 +09007729 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007730 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007731 var builder strings.Builder
7732 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7733 androidMk := builder.String()
7734 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007735 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007736 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7737 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7738 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007739 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 myotherlib:64\n")
Yo Chiange8128052020-07-23 20:09:18 +08007740}
7741
Jooyung Han643adc42020-02-27 13:50:06 +09007742func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007743 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007744 apex {
7745 name: "myapex",
7746 key: "myapex.key",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007747 binaries: ["mybin"],
7748 jni_libs: ["mylib", "mylib3", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007749 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007750 }
7751
7752 apex_key {
7753 name: "myapex.key",
7754 public_key: "testkey.avbpubkey",
7755 private_key: "testkey.pem",
7756 }
7757
7758 cc_library {
7759 name: "mylib",
7760 srcs: ["mylib.cpp"],
7761 shared_libs: ["mylib2"],
7762 system_shared_libs: [],
7763 stl: "none",
7764 apex_available: [ "myapex" ],
7765 }
7766
7767 cc_library {
7768 name: "mylib2",
7769 srcs: ["mylib.cpp"],
7770 system_shared_libs: [],
7771 stl: "none",
7772 apex_available: [ "myapex" ],
7773 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007774
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007775 // Used as both a JNI library and a regular shared library.
7776 cc_library {
7777 name: "mylib3",
7778 srcs: ["mylib.cpp"],
7779 system_shared_libs: [],
7780 stl: "none",
7781 apex_available: [ "myapex" ],
7782 }
7783
7784 cc_binary {
7785 name: "mybin",
7786 srcs: ["mybin.cpp"],
7787 shared_libs: ["mylib3"],
7788 system_shared_libs: [],
7789 stl: "none",
7790 apex_available: [ "myapex" ],
7791 }
7792
Jiyong Park34d5c332022-02-24 18:02:44 +09007793 rust_ffi_shared {
7794 name: "libfoo.rust",
7795 crate_name: "foo",
7796 srcs: ["foo.rs"],
7797 shared_libs: ["libfoo.shared_from_rust"],
7798 prefer_rlib: true,
7799 apex_available: ["myapex"],
7800 }
7801
7802 cc_library_shared {
7803 name: "libfoo.shared_from_rust",
7804 srcs: ["mylib.cpp"],
7805 system_shared_libs: [],
7806 stl: "none",
7807 stubs: {
7808 versions: ["10", "11", "12"],
7809 },
7810 }
7811
Jooyung Han643adc42020-02-27 13:50:06 +09007812 `)
7813
Jooyung Hana0503a52023-08-23 13:12:50 +09007814 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han643adc42020-02-27 13:50:06 +09007815 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007816 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so mylib3.so")
Jooyung Hana0503a52023-08-23 13:12:50 +09007817 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007818 "bin/mybin",
Jooyung Han643adc42020-02-27 13:50:06 +09007819 "lib64/mylib.so",
7820 "lib64/mylib2.so",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007821 "lib64/mylib3.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007822 "lib64/libfoo.rust.so",
7823 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7824 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007825 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007826
7827 // b/220397949
7828 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007829}
7830
Jooyung Han49f67012020-04-17 13:43:10 +09007831func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007832 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007833 apex {
7834 name: "myapex",
7835 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007836 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007837 }
7838 apex_key {
7839 name: "myapex.key",
7840 public_key: "testkey.avbpubkey",
7841 private_key: "testkey.pem",
7842 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007843 `,
7844 android.FixtureModifyConfig(func(config android.Config) {
7845 delete(config.Targets, android.Android)
7846 config.AndroidCommonTarget = android.Target{}
7847 }),
7848 )
Jooyung Han49f67012020-04-17 13:43:10 +09007849
7850 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7851 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7852 }
7853}
7854
Jiyong Parkbd159612020-02-28 15:22:21 +09007855func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007856 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007857 apex {
7858 name: "myapex",
7859 key: "myapex.key",
7860 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007861 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007862 }
7863
7864 apex_key {
7865 name: "myapex.key",
7866 public_key: "testkey.avbpubkey",
7867 private_key: "testkey.pem",
7868 }
7869
7870 android_app {
7871 name: "AppFoo",
7872 srcs: ["foo/bar/MyClass.java"],
7873 sdk_version: "none",
7874 system_modules: "none",
7875 apex_available: [ "myapex" ],
7876 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007877 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007878
Jooyung Hana0503a52023-08-23 13:12:50 +09007879 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07007880 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Jiyong Parkbd159612020-02-28 15:22:21 +09007881
7882 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007883 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 +09007884}
7885
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007886func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007887 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007888 apex {
7889 name: "myapex",
7890 key: "myapex.key",
7891 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007892 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007893 }
7894
7895 apex_key {
7896 name: "myapex.key",
7897 public_key: "testkey.avbpubkey",
7898 private_key: "testkey.pem",
7899 }
7900
7901 android_app_set {
7902 name: "AppSet",
7903 set: "AppSet.apks",
7904 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09007905 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crosscf371cc2020-11-13 11:48:42 -08007906 bundleConfigRule := mod.Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07007907 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007908 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7909 s := mod.Rule("apexRule").Args["copy_commands"]
7910 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007911 if len(copyCmds) != 4 {
7912 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007913 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007914 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7915 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007916 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7917 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007918
7919 // Ensure that canned_fs_config has an entry for the app set zip file
7920 generateFsRule := mod.Rule("generateFsConfig")
7921 cmd := generateFsRule.RuleParams.Command
7922 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007923}
7924
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007925func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007926 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007927 apex_set {
7928 name: "myapex",
7929 filename: "foo_v2.apex",
7930 sanitized: {
7931 none: { set: "myapex.apks", },
7932 hwaddress: { set: "myapex.hwasan.apks", },
7933 },
Paul Duffin24704672021-04-06 16:09:30 +01007934 }
7935 `
7936 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007937
Paul Duffin24704672021-04-06 16:09:30 +01007938 // Check that the extractor produces the correct output file from the correct input file.
Spandan Das3576e762024-01-03 18:57:03 +00007939 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007940
Spandan Das3576e762024-01-03 18:57:03 +00007941 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Paul Duffin24704672021-04-06 16:09:30 +01007942 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007943
Paul Duffin24704672021-04-06 16:09:30 +01007944 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7945
7946 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007947 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7948 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007949
7950 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007951}
7952
Pranav Guptaeba03b02022-09-27 00:27:08 +00007953func TestApexSetApksModuleAssignment(t *testing.T) {
7954 ctx := testApex(t, `
7955 apex_set {
7956 name: "myapex",
7957 set: ":myapex_apks_file",
7958 }
7959
7960 filegroup {
7961 name: "myapex_apks_file",
7962 srcs: ["myapex.apks"],
7963 }
7964 `)
7965
Spandan Das3576e762024-01-03 18:57:03 +00007966 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Pranav Guptaeba03b02022-09-27 00:27:08 +00007967
7968 // Check that the extractor produces the correct apks file from the input module
Spandan Das3576e762024-01-03 18:57:03 +00007969 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.apks"
Pranav Guptaeba03b02022-09-27 00:27:08 +00007970 extractedApex := m.Output(extractorOutput)
7971
7972 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7973}
7974
Paul Duffin89f570a2021-06-16 01:42:33 +01007975func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007976 t.Helper()
7977
Paul Duffin55607122021-03-30 23:32:51 +01007978 fs := android.MockFS{
7979 "a.java": nil,
7980 "a.jar": nil,
7981 "apex_manifest.json": nil,
7982 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007983 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007984 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7985 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7986 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007987 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007988 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007989
Paul Duffin55607122021-03-30 23:32:51 +01007990 errorHandler := android.FixtureExpectsNoErrors
7991 if errmsg != "" {
7992 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007993 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007994
Paul Duffin55607122021-03-30 23:32:51 +01007995 result := android.GroupFixturePreparers(
7996 cc.PrepareForTestWithCcDefaultModules,
7997 java.PrepareForTestWithHiddenApiBuildComponents,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01007998 java.PrepareForTestWithDexpreopt,
Paul Duffin55607122021-03-30 23:32:51 +01007999 java.PrepareForTestWithJavaSdkLibraryFiles,
8000 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01008001 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01008002 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01008003 android.FixtureModifyMockFS(func(fs android.MockFS) {
8004 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
8005 insert := ""
8006 for _, fragment := range fragments {
8007 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
8008 }
8009 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
8010 platform_bootclasspath {
8011 name: "platform-bootclasspath",
8012 fragments: [
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008013 {apex: "com.android.art", module: "art-bootclasspath-fragment"},
Paul Duffin89f570a2021-06-16 01:42:33 +01008014 %s
8015 ],
8016 }
8017 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01008018 }
Paul Duffin89f570a2021-06-16 01:42:33 +01008019 }),
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008020 // Dexpreopt for boot jars requires the ART boot image profile.
8021 java.PrepareApexBootJarModule("com.android.art", "core-oj"),
8022 dexpreopt.FixtureSetArtBootJars("com.android.art:core-oj"),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00008023 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01008024 ).
8025 ExtendWithErrorHandler(errorHandler).
8026 RunTestWithBp(t, bp)
8027
8028 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008029}
8030
Paul Duffin5556c5f2022-06-09 17:32:21 +00008031func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008032 preparers := android.GroupFixturePreparers(
8033 java.PrepareForTestWithJavaDefaultModules,
Spandan Das5be63332023-12-13 00:06:32 +00008034 prepareForTestWithBootclasspathFragment,
8035 dexpreopt.FixtureSetTestOnlyArtBootImageJars("com.android.art:libfoo"),
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008036 PrepareForTestWithApexBuildComponents,
8037 ).
8038 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
Spandan Das3576e762024-01-03 18:57:03 +00008039 "Multiple installable prebuilt APEXes provide ambiguous deapexers: prebuilt_com.android.art and prebuilt_com.mycompany.android.art"))
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008040
8041 bpBase := `
8042 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008043 name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008044 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008045 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008046 set: "myapex.apks",
8047 }
8048
8049 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008050 name: "com.mycompany.android.art",
8051 apex_name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008052 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008053 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008054 set: "company-myapex.apks",
8055 }
8056
8057 prebuilt_bootclasspath_fragment {
Spandan Das5be63332023-12-13 00:06:32 +00008058 name: "art-bootclasspath-fragment",
8059 apex_available: ["com.android.art"],
Spandan Dasfae468e2023-12-12 23:23:53 +00008060 hidden_api: {
8061 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8062 metadata: "my-bootclasspath-fragment/metadata.csv",
8063 index: "my-bootclasspath-fragment/index.csv",
8064 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
8065 all_flags: "my-bootclasspath-fragment/all-flags.csv",
8066 },
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008067 %s
8068 }
8069 `
8070
8071 t.Run("java_import", func(t *testing.T) {
8072 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8073 java_import {
8074 name: "libfoo",
8075 jars: ["libfoo.jar"],
Spandan Das5be63332023-12-13 00:06:32 +00008076 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008077 }
8078 `)
8079 })
8080
8081 t.Run("java_sdk_library_import", func(t *testing.T) {
8082 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8083 java_sdk_library_import {
8084 name: "libfoo",
8085 public: {
8086 jars: ["libbar.jar"],
8087 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008088 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008089 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008090 }
8091 `)
8092 })
8093
8094 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8095 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8096 image_name: "art",
8097 contents: ["libfoo"],
8098 `)+`
8099 java_sdk_library_import {
8100 name: "libfoo",
8101 public: {
8102 jars: ["libbar.jar"],
8103 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008104 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008105 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008106 }
8107 `)
8108 })
8109}
8110
Paul Duffin5556c5f2022-06-09 17:32:21 +00008111func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
8112 preparers := android.GroupFixturePreparers(
8113 java.PrepareForTestWithJavaDefaultModules,
8114 PrepareForTestWithApexBuildComponents,
8115 )
8116
Spandan Das59a4a2b2024-01-09 21:35:56 +00008117 errCtx := moduleErrorfTestCtx{}
8118
Paul Duffin5556c5f2022-06-09 17:32:21 +00008119 bpBase := `
8120 apex_set {
8121 name: "com.android.myapex",
8122 installable: true,
8123 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8124 set: "myapex.apks",
8125 }
8126
8127 apex_set {
8128 name: "com.android.myapex_compressed",
8129 apex_name: "com.android.myapex",
8130 installable: true,
8131 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8132 set: "myapex_compressed.apks",
8133 }
8134
8135 prebuilt_bootclasspath_fragment {
8136 name: "my-bootclasspath-fragment",
8137 apex_available: [
8138 "com.android.myapex",
8139 "com.android.myapex_compressed",
8140 ],
8141 hidden_api: {
8142 annotation_flags: "annotation-flags.csv",
8143 metadata: "metadata.csv",
8144 index: "index.csv",
8145 signature_patterns: "signature_patterns.csv",
8146 },
8147 %s
8148 }
8149 `
8150
8151 t.Run("java_import", func(t *testing.T) {
8152 result := preparers.RunTestWithBp(t,
8153 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8154 java_import {
8155 name: "libfoo",
8156 jars: ["libfoo.jar"],
8157 apex_available: [
8158 "com.android.myapex",
8159 "com.android.myapex_compressed",
8160 ],
8161 }
8162 `)
8163
8164 module := result.Module("libfoo", "android_common_com.android.myapex")
8165 usesLibraryDep := module.(java.UsesLibraryDependency)
8166 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008167 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008168 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008169 })
8170
8171 t.Run("java_sdk_library_import", func(t *testing.T) {
8172 result := preparers.RunTestWithBp(t,
8173 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8174 java_sdk_library_import {
8175 name: "libfoo",
8176 public: {
8177 jars: ["libbar.jar"],
8178 },
8179 apex_available: [
8180 "com.android.myapex",
8181 "com.android.myapex_compressed",
8182 ],
8183 compile_dex: true,
8184 }
8185 `)
8186
8187 module := result.Module("libfoo", "android_common_com.android.myapex")
8188 usesLibraryDep := module.(java.UsesLibraryDependency)
8189 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008190 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008191 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008192 })
8193
8194 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8195 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8196 image_name: "art",
8197 contents: ["libfoo"],
8198 `)+`
8199 java_sdk_library_import {
8200 name: "libfoo",
8201 public: {
8202 jars: ["libbar.jar"],
8203 },
8204 apex_available: [
8205 "com.android.myapex",
8206 "com.android.myapex_compressed",
8207 ],
8208 compile_dex: true,
8209 }
8210 `)
8211 })
8212}
8213
Jooyung Han548640b2020-04-27 12:10:30 +09008214func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
8215 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8216 apex {
8217 name: "myapex",
8218 key: "myapex.key",
8219 updatable: true,
8220 }
8221
8222 apex_key {
8223 name: "myapex.key",
8224 public_key: "testkey.avbpubkey",
8225 private_key: "testkey.pem",
8226 }
8227 `)
8228}
8229
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008230func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
8231 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8232 apex {
8233 name: "myapex",
8234 key: "myapex.key",
8235 }
8236
8237 apex_key {
8238 name: "myapex.key",
8239 public_key: "testkey.avbpubkey",
8240 private_key: "testkey.pem",
8241 }
8242 `)
8243}
8244
satayevb98371c2021-06-15 16:49:50 +01008245func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
8246 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
8247 apex {
8248 name: "myapex",
8249 key: "myapex.key",
8250 systemserverclasspath_fragments: [
8251 "mysystemserverclasspathfragment",
8252 ],
8253 min_sdk_version: "29",
8254 updatable: true,
8255 }
8256
8257 apex_key {
8258 name: "myapex.key",
8259 public_key: "testkey.avbpubkey",
8260 private_key: "testkey.pem",
8261 }
8262
8263 java_library {
8264 name: "foo",
8265 srcs: ["b.java"],
8266 min_sdk_version: "29",
8267 installable: true,
8268 apex_available: [
8269 "myapex",
8270 ],
8271 }
8272
8273 systemserverclasspath_fragment {
8274 name: "mysystemserverclasspathfragment",
8275 generate_classpaths_proto: false,
8276 contents: [
8277 "foo",
8278 ],
8279 apex_available: [
8280 "myapex",
8281 ],
8282 }
satayevabcd5972021-08-06 17:49:46 +01008283 `,
8284 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8285 )
satayevb98371c2021-06-15 16:49:50 +01008286}
8287
Paul Duffin064b70c2020-11-02 17:32:38 +00008288func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008289 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008290 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008291 fragment := java.ApexVariantReference{
8292 Apex: proptools.StringPtr("myapex"),
8293 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8294 }
8295
Paul Duffin064b70c2020-11-02 17:32:38 +00008296 testDexpreoptWithApexes(t, `
8297 prebuilt_apex {
8298 name: "myapex" ,
8299 arch: {
8300 arm64: {
8301 src: "myapex-arm64.apex",
8302 },
8303 arm: {
8304 src: "myapex-arm.apex",
8305 },
8306 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008307 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8308 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008309
Paul Duffin89f570a2021-06-16 01:42:33 +01008310 prebuilt_bootclasspath_fragment {
8311 name: "my-bootclasspath-fragment",
8312 contents: ["libfoo"],
8313 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008314 hidden_api: {
8315 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8316 metadata: "my-bootclasspath-fragment/metadata.csv",
8317 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008318 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8319 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8320 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008321 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008322 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008323
Paul Duffin89f570a2021-06-16 01:42:33 +01008324 java_import {
8325 name: "libfoo",
8326 jars: ["libfoo.jar"],
8327 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008328 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008329 }
8330 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008331 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008332}
8333
Spandan Dasf14e2542021-11-12 00:01:37 +00008334func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008335 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008336 bp += `
8337 apex_key {
8338 name: "myapex.key",
8339 public_key: "testkey.avbpubkey",
8340 private_key: "testkey.pem",
8341 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008342 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008343 "lib1/src/A.java": nil,
8344 "lib2/src/B.java": nil,
8345 "system/sepolicy/apex/myapex-file_contexts": nil,
8346 }
8347
Paul Duffin45338f02021-03-30 23:07:52 +01008348 errorHandler := android.FixtureExpectsNoErrors
8349 if errmsg != "" {
8350 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008351 }
Colin Crossae8600b2020-10-29 17:09:13 -07008352
Paul Duffin45338f02021-03-30 23:07:52 +01008353 android.GroupFixturePreparers(
8354 android.PrepareForTestWithAndroidBuildComponents,
8355 java.PrepareForTestWithJavaBuildComponents,
8356 PrepareForTestWithApexBuildComponents,
8357 android.PrepareForTestWithNeverallowRules(rules),
8358 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008359 apexBootJars := make([]string, 0, len(bootJars))
8360 for _, apexBootJar := range bootJars {
8361 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008362 }
satayevd604b212021-07-21 14:23:52 +01008363 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008364 }),
8365 fs.AddToFixture(),
8366 ).
8367 ExtendWithErrorHandler(errorHandler).
8368 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008369}
8370
8371func TestApexPermittedPackagesRules(t *testing.T) {
8372 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008373 name string
8374 expectedError string
8375 bp string
8376 bootJars []string
8377 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008378 }{
8379
8380 {
8381 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8382 expectedError: "",
8383 bp: `
8384 java_library {
8385 name: "bcp_lib1",
8386 srcs: ["lib1/src/*.java"],
8387 permitted_packages: ["foo.bar"],
8388 apex_available: ["myapex"],
8389 sdk_version: "none",
8390 system_modules: "none",
8391 }
8392 java_library {
8393 name: "nonbcp_lib2",
8394 srcs: ["lib2/src/*.java"],
8395 apex_available: ["myapex"],
8396 permitted_packages: ["a.b"],
8397 sdk_version: "none",
8398 system_modules: "none",
8399 }
8400 apex {
8401 name: "myapex",
8402 key: "myapex.key",
8403 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008404 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008405 }`,
8406 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008407 bcpPermittedPackages: map[string][]string{
8408 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008409 "foo.bar",
8410 },
8411 },
8412 },
8413 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008414 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008415 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 +01008416 bp: `
8417 java_library {
8418 name: "bcp_lib1",
8419 srcs: ["lib1/src/*.java"],
8420 apex_available: ["myapex"],
8421 permitted_packages: ["foo.bar"],
8422 sdk_version: "none",
8423 system_modules: "none",
8424 }
8425 java_library {
8426 name: "bcp_lib2",
8427 srcs: ["lib2/src/*.java"],
8428 apex_available: ["myapex"],
8429 permitted_packages: ["foo.bar", "bar.baz"],
8430 sdk_version: "none",
8431 system_modules: "none",
8432 }
8433 apex {
8434 name: "myapex",
8435 key: "myapex.key",
8436 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008437 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008438 }
8439 `,
8440 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008441 bcpPermittedPackages: map[string][]string{
8442 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008443 "foo.bar",
8444 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008445 "bcp_lib2": []string{
8446 "foo.bar",
8447 },
8448 },
8449 },
8450 {
8451 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8452 expectedError: "",
8453 bp: `
8454 java_library {
8455 name: "bcp_lib_restricted",
8456 srcs: ["lib1/src/*.java"],
8457 apex_available: ["myapex"],
8458 permitted_packages: ["foo.bar"],
8459 sdk_version: "none",
8460 min_sdk_version: "29",
8461 system_modules: "none",
8462 }
8463 java_library {
8464 name: "bcp_lib_unrestricted",
8465 srcs: ["lib2/src/*.java"],
8466 apex_available: ["myapex"],
8467 permitted_packages: ["foo.bar", "bar.baz"],
8468 sdk_version: "none",
8469 min_sdk_version: "29",
8470 system_modules: "none",
8471 }
8472 apex {
8473 name: "myapex",
8474 key: "myapex.key",
8475 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8476 updatable: true,
8477 min_sdk_version: "29",
8478 }
8479 `,
8480 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8481 bcpPermittedPackages: map[string][]string{
8482 "bcp_lib1_non_updateable": []string{
8483 "foo.bar",
8484 },
8485 // 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 +01008486 },
8487 },
8488 }
8489 for _, tc := range testcases {
8490 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008491 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8492 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008493 })
8494 }
8495}
8496
Jiyong Park62304bb2020-04-13 16:19:48 +09008497func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008498 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008499 apex {
8500 name: "myapex",
8501 key: "myapex.key",
8502 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008503 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008504 }
8505
8506 apex_key {
8507 name: "myapex.key",
8508 public_key: "testkey.avbpubkey",
8509 private_key: "testkey.pem",
8510 }
8511
8512 cc_library {
8513 name: "mylib",
8514 srcs: ["mylib.cpp"],
8515 system_shared_libs: [],
8516 stl: "none",
8517 stubs: {
8518 versions: ["1"],
8519 },
8520 apex_available: ["myapex"],
8521 }
8522
8523 cc_library {
8524 name: "myprivlib",
8525 srcs: ["mylib.cpp"],
8526 system_shared_libs: [],
8527 stl: "none",
8528 apex_available: ["myapex"],
8529 }
8530
8531
8532 cc_test {
8533 name: "mytest",
8534 gtest: false,
8535 srcs: ["mylib.cpp"],
8536 system_shared_libs: [],
8537 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008538 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008539 test_for: ["myapex"]
8540 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008541
8542 cc_library {
8543 name: "mytestlib",
8544 srcs: ["mylib.cpp"],
8545 system_shared_libs: [],
8546 shared_libs: ["mylib", "myprivlib"],
8547 stl: "none",
8548 test_for: ["myapex"],
8549 }
8550
8551 cc_benchmark {
8552 name: "mybench",
8553 srcs: ["mylib.cpp"],
8554 system_shared_libs: [],
8555 shared_libs: ["mylib", "myprivlib"],
8556 stl: "none",
8557 test_for: ["myapex"],
8558 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008559 `)
8560
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008561 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008562 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008563 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8564 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8565 }
8566
8567 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008568 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008569 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8570 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8571 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8572}
Jiyong Park46a512f2020-12-04 18:02:13 +09008573
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008574func TestIndirectTestFor(t *testing.T) {
8575 ctx := testApex(t, `
8576 apex {
8577 name: "myapex",
8578 key: "myapex.key",
8579 native_shared_libs: ["mylib", "myprivlib"],
8580 updatable: false,
8581 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008582
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008583 apex_key {
8584 name: "myapex.key",
8585 public_key: "testkey.avbpubkey",
8586 private_key: "testkey.pem",
8587 }
8588
8589 cc_library {
8590 name: "mylib",
8591 srcs: ["mylib.cpp"],
8592 system_shared_libs: [],
8593 stl: "none",
8594 stubs: {
8595 versions: ["1"],
8596 },
8597 apex_available: ["myapex"],
8598 }
8599
8600 cc_library {
8601 name: "myprivlib",
8602 srcs: ["mylib.cpp"],
8603 system_shared_libs: [],
8604 stl: "none",
8605 shared_libs: ["mylib"],
8606 apex_available: ["myapex"],
8607 }
8608
8609 cc_library {
8610 name: "mytestlib",
8611 srcs: ["mylib.cpp"],
8612 system_shared_libs: [],
8613 shared_libs: ["myprivlib"],
8614 stl: "none",
8615 test_for: ["myapex"],
8616 }
8617 `)
8618
8619 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008620 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008621 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8622 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8623 }
8624
8625 // The platform variant of mytestlib links to the platform variant of the
8626 // internal myprivlib.
8627 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8628
8629 // The platform variant of myprivlib links to the platform variant of mylib
8630 // and bypasses its stubs.
8631 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 +09008632}
8633
Martin Stjernholmec009002021-03-27 15:18:31 +00008634func TestTestForForLibInOtherApex(t *testing.T) {
8635 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8636 _ = testApex(t, `
8637 apex {
8638 name: "com.android.art",
8639 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008640 native_shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008641 updatable: false,
8642 }
8643
8644 apex {
8645 name: "com.android.art.debug",
8646 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008647 native_shared_libs: ["libnativebridge", "libnativebrdige_test"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008648 updatable: false,
8649 }
8650
8651 apex_key {
8652 name: "myapex.key",
8653 public_key: "testkey.avbpubkey",
8654 private_key: "testkey.pem",
8655 }
8656
8657 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008658 name: "libnativebridge",
8659 srcs: ["libnativebridge.cpp"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008660 system_shared_libs: [],
8661 stl: "none",
8662 stubs: {
8663 versions: ["1"],
8664 },
8665 apex_available: ["com.android.art", "com.android.art.debug"],
8666 }
8667
8668 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008669 name: "libnativebrdige_test",
Martin Stjernholmec009002021-03-27 15:18:31 +00008670 srcs: ["mylib.cpp"],
8671 system_shared_libs: [],
Spandan Das20fce2d2023-04-12 17:21:39 +00008672 shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008673 stl: "none",
8674 apex_available: ["com.android.art.debug"],
8675 test_for: ["com.android.art"],
8676 }
8677 `,
8678 android.MockFS{
8679 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8680 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8681 }.AddToFixture())
8682}
8683
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008684// TODO(jungjw): Move this to proptools
8685func intPtr(i int) *int {
8686 return &i
8687}
8688
8689func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008690 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008691 apex_set {
8692 name: "myapex",
8693 set: "myapex.apks",
8694 filename: "foo_v2.apex",
8695 overrides: ["foo"],
8696 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008697 `,
8698 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8699 variables.Platform_sdk_version = intPtr(30)
8700 }),
8701 android.FixtureModifyConfig(func(config android.Config) {
8702 config.Targets[android.Android] = []android.Target{
8703 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8704 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8705 }
8706 }),
8707 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008708
Spandan Das3576e762024-01-03 18:57:03 +00008709 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008710
8711 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008712 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008713 actual := extractedApex.Args["abis"]
8714 expected := "ARMEABI_V7A,ARM64_V8A"
8715 if actual != expected {
8716 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8717 }
8718 actual = extractedApex.Args["sdk-version"]
8719 expected = "30"
8720 if actual != expected {
8721 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8722 }
8723
Paul Duffin6717d882021-06-15 19:09:41 +01008724 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008725 a := m.Module().(*ApexSet)
8726 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008727 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008728 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8729 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8730 }
8731}
8732
Anton Hansson805e0a52022-11-25 14:06:46 +00008733func TestApexSet_NativeBridge(t *testing.T) {
8734 ctx := testApex(t, `
8735 apex_set {
8736 name: "myapex",
8737 set: "myapex.apks",
8738 filename: "foo_v2.apex",
8739 overrides: ["foo"],
8740 }
8741 `,
8742 android.FixtureModifyConfig(func(config android.Config) {
8743 config.Targets[android.Android] = []android.Target{
8744 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8745 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8746 }
8747 }),
8748 )
8749
Spandan Das3576e762024-01-03 18:57:03 +00008750 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Anton Hansson805e0a52022-11-25 14:06:46 +00008751
8752 // Check extract_apks tool parameters. No native bridge arch expected
8753 extractedApex := m.Output("extracted/myapex.apks")
8754 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8755}
8756
Jiyong Park7d95a512020-05-10 15:16:24 +09008757func TestNoStaticLinkingToStubsLib(t *testing.T) {
8758 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8759 apex {
8760 name: "myapex",
8761 key: "myapex.key",
8762 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008763 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008764 }
8765
8766 apex_key {
8767 name: "myapex.key",
8768 public_key: "testkey.avbpubkey",
8769 private_key: "testkey.pem",
8770 }
8771
8772 cc_library {
8773 name: "mylib",
8774 srcs: ["mylib.cpp"],
8775 static_libs: ["otherlib"],
8776 system_shared_libs: [],
8777 stl: "none",
8778 apex_available: [ "myapex" ],
8779 }
8780
8781 cc_library {
8782 name: "otherlib",
8783 srcs: ["mylib.cpp"],
8784 system_shared_libs: [],
8785 stl: "none",
8786 stubs: {
8787 versions: ["1", "2", "3"],
8788 },
8789 apex_available: [ "myapex" ],
8790 }
8791 `)
8792}
8793
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008794func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008795 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008796 apex {
8797 name: "myapex",
8798 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008799 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008800 custom_sign_tool: "sign_myapex",
8801 }
8802
8803 apex_key {
8804 name: "myapex.key",
8805 public_key: "testkey.avbpubkey",
8806 private_key: "testkey.pem",
8807 }
8808 `)
8809
Jooyung Han286957d2023-10-30 16:17:56 +09008810 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07008811 content := android.ContentFromFileRuleForTests(t, ctx, myapex.Output("apexkeys.txt"))
Jooyung Haneec1b3f2023-06-20 16:25:59 +09008812 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 +09008813}
8814
8815func TestApexKeysTxtOverrides(t *testing.T) {
8816 ctx := testApex(t, `
8817 apex {
8818 name: "myapex",
8819 key: "myapex.key",
8820 updatable: false,
8821 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008822 }
8823
8824 apex_key {
8825 name: "myapex.key",
8826 public_key: "testkey.avbpubkey",
8827 private_key: "testkey.pem",
8828 }
8829
8830 prebuilt_apex {
8831 name: "myapex",
8832 prefer: true,
8833 arch: {
8834 arm64: {
8835 src: "myapex-arm64.apex",
8836 },
8837 arm: {
8838 src: "myapex-arm.apex",
8839 },
8840 },
8841 }
8842
8843 apex_set {
8844 name: "myapex_set",
8845 set: "myapex.apks",
8846 filename: "myapex_set.apex",
8847 overrides: ["myapex"],
8848 }
8849 `)
8850
Colin Crossf61d03d2023-11-02 16:56:39 -07008851 content := android.ContentFromFileRuleForTests(t, ctx,
8852 ctx.ModuleForTests("myapex", "android_common_myapex").Output("apexkeys.txt"))
Jooyung Han286957d2023-10-30 16:17:56 +09008853 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 -07008854 content = android.ContentFromFileRuleForTests(t, ctx,
8855 ctx.ModuleForTests("myapex_set", "android_common_myapex_set").Output("apexkeys.txt"))
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008856 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 +09008857}
8858
Jooyung Han938b5932020-06-20 12:47:47 +09008859func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008860 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008861 apex {
8862 name: "myapex",
8863 key: "myapex.key",
8864 apps: ["app"],
8865 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008866 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008867 }
8868
8869 apex_key {
8870 name: "myapex.key",
8871 public_key: "testkey.avbpubkey",
8872 private_key: "testkey.pem",
8873 }
8874
8875 android_app {
8876 name: "app",
8877 srcs: ["foo/bar/MyClass.java"],
8878 package_name: "foo",
8879 sdk_version: "none",
8880 system_modules: "none",
8881 apex_available: [ "myapex" ],
8882 }
8883 `, withFiles(map[string][]byte{
8884 "sub/Android.bp": []byte(`
8885 override_apex {
8886 name: "override_myapex",
8887 base: "myapex",
8888 apps: ["override_app"],
8889 allowed_files: ":allowed",
8890 }
8891 // Overridable "path" property should be referenced indirectly
8892 filegroup {
8893 name: "allowed",
8894 srcs: ["allowed.txt"],
8895 }
8896 override_android_app {
8897 name: "override_app",
8898 base: "app",
8899 package_name: "bar",
8900 }
8901 `),
8902 }))
8903
Jooyung Hana0503a52023-08-23 13:12:50 +09008904 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008905 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8906 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8907 }
8908
Spandan Das50801e22024-05-13 18:29:45 +00008909 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_override_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008910 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8911 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8912 }
8913}
8914
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008915func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008916 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008917 apex {
8918 name: "myapex",
8919 key: "myapex.key",
8920 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008921 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008922 }
8923
8924 apex_key {
8925 name: "myapex.key",
8926 public_key: "testkey.avbpubkey",
8927 private_key: "testkey.pem",
8928 }
8929
8930 cc_library {
8931 name: "mylib",
8932 srcs: ["mylib.cpp"],
8933 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008934 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008935 },
8936 apex_available: ["myapex"],
8937 }
8938
8939 cc_prebuilt_library_shared {
8940 name: "mylib",
8941 prefer: false,
8942 srcs: ["prebuilt.so"],
8943 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008944 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008945 },
8946 apex_available: ["myapex"],
8947 }
8948 `)
8949}
8950
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008951func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008952 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008953 apex {
8954 name: "myapex",
8955 key: "myapex.key",
8956 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008957 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008958 }
8959 apex_key {
8960 name: "myapex.key",
8961 public_key: "testkey.avbpubkey",
8962 private_key: "testkey.pem",
8963 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008964 `,
8965 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8966 variables.CompressedApex = proptools.BoolPtr(true)
8967 }),
8968 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008969
Jooyung Hana0503a52023-08-23 13:12:50 +09008970 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("compressRule")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008971 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8972
Jooyung Hana0503a52023-08-23 13:12:50 +09008973 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex").Description("sign compressedApex")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008974 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8975
8976 // Make sure output of bundle is .capex
Jooyung Hana0503a52023-08-23 13:12:50 +09008977 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008978 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8979
8980 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008981 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008982 var builder strings.Builder
8983 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8984 androidMk := builder.String()
8985 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8986}
8987
Jooyung Han26ec8482024-07-31 15:04:05 +09008988func TestApexSet_ShouldRespectCompressedApexFlag(t *testing.T) {
8989 for _, compressionEnabled := range []bool{true, false} {
8990 t.Run(fmt.Sprintf("compressionEnabled=%v", compressionEnabled), func(t *testing.T) {
8991 ctx := testApex(t, `
8992 apex_set {
8993 name: "com.company.android.myapex",
8994 apex_name: "com.android.myapex",
8995 set: "company-myapex.apks",
8996 }
8997 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8998 variables.CompressedApex = proptools.BoolPtr(compressionEnabled)
8999 }),
9000 )
9001
9002 build := ctx.ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex").Output("com.company.android.myapex.apex")
9003 if compressionEnabled {
9004 ensureEquals(t, build.Rule.String(), "android/soong/android.Cp")
9005 } else {
9006 ensureEquals(t, build.Rule.String(), "android/apex.decompressApex")
9007 }
9008 })
9009 }
9010}
9011
Martin Stjernholm2856c662020-12-02 15:03:42 +00009012func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009013 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00009014 apex {
9015 name: "myapex",
9016 key: "myapex.key",
9017 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009018 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00009019 }
9020
9021 apex_key {
9022 name: "myapex.key",
9023 public_key: "testkey.avbpubkey",
9024 private_key: "testkey.pem",
9025 }
9026
9027 cc_library {
9028 name: "mylib",
9029 srcs: ["mylib.cpp"],
9030 apex_available: ["myapex"],
9031 shared_libs: ["otherlib"],
9032 system_shared_libs: [],
9033 }
9034
9035 cc_library {
9036 name: "otherlib",
9037 srcs: ["mylib.cpp"],
9038 stubs: {
9039 versions: ["current"],
9040 },
9041 }
9042
9043 cc_prebuilt_library_shared {
9044 name: "otherlib",
9045 prefer: true,
9046 srcs: ["prebuilt.so"],
9047 stubs: {
9048 versions: ["current"],
9049 },
9050 }
9051 `)
9052
Jooyung Hana0503a52023-08-23 13:12:50 +09009053 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07009054 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00009055 var builder strings.Builder
9056 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9057 androidMk := builder.String()
9058
9059 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
9060 // a thing there.
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009061 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 otherlib\n")
Martin Stjernholm2856c662020-12-02 15:03:42 +00009062}
9063
Jiyong Parke3867542020-12-03 17:28:25 +09009064func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009065 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09009066 apex {
9067 name: "myapex",
9068 key: "myapex.key",
9069 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009070 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09009071 }
9072
9073 apex_key {
9074 name: "myapex.key",
9075 public_key: "testkey.avbpubkey",
9076 private_key: "testkey.pem",
9077 }
9078
9079 cc_library {
9080 name: "mylib",
9081 srcs: ["mylib.cpp"],
9082 system_shared_libs: [],
9083 stl: "none",
9084 apex_available: ["myapex"],
9085 shared_libs: ["mylib2"],
9086 target: {
9087 apex: {
9088 exclude_shared_libs: ["mylib2"],
9089 },
9090 },
9091 }
9092
9093 cc_library {
9094 name: "mylib2",
9095 srcs: ["mylib.cpp"],
9096 system_shared_libs: [],
9097 stl: "none",
9098 }
9099 `)
9100
9101 // Check if mylib is linked to mylib2 for the non-apex target
9102 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
9103 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
9104
9105 // Make sure that the link doesn't occur for the apex target
9106 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
9107 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
9108
9109 // It shouldn't appear in the copy cmd as well.
Jooyung Hana0503a52023-08-23 13:12:50 +09009110 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule").Args["copy_commands"]
Jiyong Parke3867542020-12-03 17:28:25 +09009111 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
9112}
9113
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009114func TestPrebuiltStubLibDep(t *testing.T) {
9115 bpBase := `
9116 apex {
9117 name: "myapex",
9118 key: "myapex.key",
9119 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009120 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009121 }
9122 apex_key {
9123 name: "myapex.key",
9124 public_key: "testkey.avbpubkey",
9125 private_key: "testkey.pem",
9126 }
9127 cc_library {
9128 name: "mylib",
9129 srcs: ["mylib.cpp"],
9130 apex_available: ["myapex"],
9131 shared_libs: ["stublib"],
9132 system_shared_libs: [],
9133 }
9134 apex {
9135 name: "otherapex",
9136 enabled: %s,
9137 key: "myapex.key",
9138 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009139 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009140 }
9141 `
9142
9143 stublibSourceBp := `
9144 cc_library {
9145 name: "stublib",
9146 srcs: ["mylib.cpp"],
9147 apex_available: ["otherapex"],
9148 system_shared_libs: [],
9149 stl: "none",
9150 stubs: {
9151 versions: ["1"],
9152 },
9153 }
9154 `
9155
9156 stublibPrebuiltBp := `
9157 cc_prebuilt_library_shared {
9158 name: "stublib",
9159 srcs: ["prebuilt.so"],
9160 apex_available: ["otherapex"],
9161 stubs: {
9162 versions: ["1"],
9163 },
9164 %s
9165 }
9166 `
9167
9168 tests := []struct {
9169 name string
9170 stublibBp string
9171 usePrebuilt bool
9172 modNames []string // Modules to collect AndroidMkEntries for
9173 otherApexEnabled []string
9174 }{
9175 {
9176 name: "only_source",
9177 stublibBp: stublibSourceBp,
9178 usePrebuilt: false,
9179 modNames: []string{"stublib"},
9180 otherApexEnabled: []string{"true", "false"},
9181 },
9182 {
9183 name: "source_preferred",
9184 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9185 usePrebuilt: false,
9186 modNames: []string{"stublib", "prebuilt_stublib"},
9187 otherApexEnabled: []string{"true", "false"},
9188 },
9189 {
9190 name: "prebuilt_preferred",
9191 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9192 usePrebuilt: true,
9193 modNames: []string{"stublib", "prebuilt_stublib"},
9194 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9195 },
9196 {
9197 name: "only_prebuilt",
9198 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9199 usePrebuilt: true,
9200 modNames: []string{"stublib"},
9201 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9202 },
9203 }
9204
9205 for _, test := range tests {
9206 t.Run(test.name, func(t *testing.T) {
9207 for _, otherApexEnabled := range test.otherApexEnabled {
9208 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009209 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009210
9211 type modAndMkEntries struct {
9212 mod *cc.Module
9213 mkEntries android.AndroidMkEntries
9214 }
9215 entries := []*modAndMkEntries{}
9216
9217 // Gather shared lib modules that are installable
9218 for _, modName := range test.modNames {
9219 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9220 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9221 continue
9222 }
9223 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Cole Fausta963b942024-04-11 17:43:00 -07009224 if !mod.Enabled(android.PanickingConfigAndErrorContext(ctx)) || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009225 continue
9226 }
Colin Crossaa255532020-07-03 13:18:24 -07009227 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009228 if ent.Disabled {
9229 continue
9230 }
9231 entries = append(entries, &modAndMkEntries{
9232 mod: mod,
9233 mkEntries: ent,
9234 })
9235 }
9236 }
9237 }
9238
9239 var entry *modAndMkEntries = nil
9240 for _, ent := range entries {
9241 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9242 if entry != nil {
9243 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9244 } else {
9245 entry = ent
9246 }
9247 }
9248 }
9249
9250 if entry == nil {
9251 t.Errorf("AndroidMk entry for \"stublib\" missing")
9252 } else {
9253 isPrebuilt := entry.mod.Prebuilt() != nil
9254 if isPrebuilt != test.usePrebuilt {
9255 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9256 }
9257 if !entry.mod.IsStubs() {
9258 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9259 }
9260 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9261 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9262 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009263 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009264 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009265 if !android.InList(expected, cflags) {
9266 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9267 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009268 }
9269 })
9270 }
9271 })
9272 }
9273}
9274
Colin Crossc33e5212021-05-25 18:16:02 -07009275func TestApexJavaCoverage(t *testing.T) {
9276 bp := `
9277 apex {
9278 name: "myapex",
9279 key: "myapex.key",
9280 java_libs: ["mylib"],
9281 bootclasspath_fragments: ["mybootclasspathfragment"],
9282 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9283 updatable: false,
9284 }
9285
9286 apex_key {
9287 name: "myapex.key",
9288 public_key: "testkey.avbpubkey",
9289 private_key: "testkey.pem",
9290 }
9291
9292 java_library {
9293 name: "mylib",
9294 srcs: ["mylib.java"],
9295 apex_available: ["myapex"],
9296 compile_dex: true,
9297 }
9298
9299 bootclasspath_fragment {
9300 name: "mybootclasspathfragment",
9301 contents: ["mybootclasspathlib"],
9302 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009303 hidden_api: {
9304 split_packages: ["*"],
9305 },
Colin Crossc33e5212021-05-25 18:16:02 -07009306 }
9307
9308 java_library {
9309 name: "mybootclasspathlib",
9310 srcs: ["mybootclasspathlib.java"],
9311 apex_available: ["myapex"],
9312 compile_dex: true,
9313 }
9314
9315 systemserverclasspath_fragment {
9316 name: "mysystemserverclasspathfragment",
9317 contents: ["mysystemserverclasspathlib"],
9318 apex_available: ["myapex"],
9319 }
9320
9321 java_library {
9322 name: "mysystemserverclasspathlib",
9323 srcs: ["mysystemserverclasspathlib.java"],
9324 apex_available: ["myapex"],
9325 compile_dex: true,
9326 }
9327 `
9328
9329 result := android.GroupFixturePreparers(
9330 PrepareForTestWithApexBuildComponents,
9331 prepareForTestWithMyapex,
9332 java.PrepareForTestWithJavaDefaultModules,
9333 android.PrepareForTestWithAndroidBuildComponents,
9334 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009335 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9336 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009337 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009338 ).RunTest(t)
9339
9340 // Make sure jacoco ran on both mylib and mybootclasspathlib
9341 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9342 t.Errorf("Failed to find jacoco rule for mylib")
9343 }
9344 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9345 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9346 }
9347 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9348 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9349 }
9350}
9351
Jiyong Park192600a2021-08-03 07:52:17 +00009352func TestProhibitStaticExecutable(t *testing.T) {
9353 testApexError(t, `executable mybin is static`, `
9354 apex {
9355 name: "myapex",
9356 key: "myapex.key",
9357 binaries: ["mybin"],
9358 min_sdk_version: "29",
9359 }
9360
9361 apex_key {
9362 name: "myapex.key",
9363 public_key: "testkey.avbpubkey",
9364 private_key: "testkey.pem",
9365 }
9366
9367 cc_binary {
9368 name: "mybin",
9369 srcs: ["mylib.cpp"],
9370 relative_install_path: "foo/bar",
9371 static_executable: true,
9372 system_shared_libs: [],
9373 stl: "none",
9374 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009375 min_sdk_version: "29",
9376 }
9377 `)
9378
9379 testApexError(t, `executable mybin.rust is static`, `
9380 apex {
9381 name: "myapex",
9382 key: "myapex.key",
9383 binaries: ["mybin.rust"],
9384 min_sdk_version: "29",
9385 }
9386
9387 apex_key {
9388 name: "myapex.key",
9389 public_key: "testkey.avbpubkey",
9390 private_key: "testkey.pem",
9391 }
9392
9393 rust_binary {
9394 name: "mybin.rust",
9395 srcs: ["foo.rs"],
9396 static_executable: true,
9397 apex_available: ["myapex"],
9398 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009399 }
9400 `)
9401}
9402
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009403func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9404 ctx := testApex(t, `
9405 apex {
9406 name: "myapex",
9407 key: "myapex.key",
9408 updatable: false,
9409 java_libs: ["foo"],
9410 }
9411
9412 apex_key {
9413 name: "myapex.key",
9414 public_key: "testkey.avbpubkey",
9415 private_key: "testkey.pem",
9416 }
9417
9418 java_library {
9419 name: "foo",
9420 srcs: ["foo.java"],
9421 apex_available: ["myapex"],
9422 installable: true,
9423 }
9424 `,
9425 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9426 )
9427
Jooyung Hana0503a52023-08-23 13:12:50 +09009428 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009429 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9430 var builder strings.Builder
9431 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9432 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009433 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 +00009434}
9435
9436func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9437 ctx := testApex(t, `
9438 prebuilt_apex {
9439 name: "myapex",
9440 arch: {
9441 arm64: {
9442 src: "myapex-arm64.apex",
9443 },
9444 arm: {
9445 src: "myapex-arm.apex",
9446 },
9447 },
9448 exported_java_libs: ["foo"],
9449 }
9450
9451 java_import {
9452 name: "foo",
9453 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009454 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009455 }
9456 `,
9457 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9458 )
9459
9460 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9461 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9462 mainModuleEntries := entriesList[0]
9463 android.AssertArrayString(t,
9464 "LOCAL_REQUIRED_MODULES",
9465 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9466 []string{
9467 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9468 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9469 })
9470}
9471
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009472func TestAndroidMk_RequiredModules(t *testing.T) {
9473 ctx := testApex(t, `
9474 apex {
9475 name: "myapex",
9476 key: "myapex.key",
9477 updatable: false,
9478 java_libs: ["foo"],
9479 required: ["otherapex"],
9480 }
9481
9482 apex {
9483 name: "otherapex",
9484 key: "myapex.key",
9485 updatable: false,
9486 java_libs: ["foo"],
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009487 }
9488
9489 apex_key {
9490 name: "myapex.key",
9491 public_key: "testkey.avbpubkey",
9492 private_key: "testkey.pem",
9493 }
9494
9495 java_library {
9496 name: "foo",
9497 srcs: ["foo.java"],
9498 apex_available: ["myapex", "otherapex"],
9499 installable: true,
9500 }
9501 `)
9502
Jooyung Hana0503a52023-08-23 13:12:50 +09009503 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009504 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9505 var builder strings.Builder
9506 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9507 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009508 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009509}
9510
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009511func TestAndroidMk_RequiredDeps(t *testing.T) {
9512 ctx := testApex(t, `
9513 apex {
9514 name: "myapex",
9515 key: "myapex.key",
9516 updatable: false,
9517 }
9518
9519 apex_key {
9520 name: "myapex.key",
9521 public_key: "testkey.avbpubkey",
9522 private_key: "testkey.pem",
9523 }
9524 `)
9525
Jooyung Hana0503a52023-08-23 13:12:50 +09009526 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009527 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009528 data := android.AndroidMkDataForTest(t, ctx, bundle)
9529 var builder strings.Builder
9530 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9531 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009532 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009533}
9534
Jooyung Hana6d36672022-02-24 13:58:07 +09009535func TestApexOutputFileProducer(t *testing.T) {
9536 for _, tc := range []struct {
9537 name string
9538 ref string
9539 expected_data []string
9540 }{
9541 {
9542 name: "test_using_output",
9543 ref: ":myapex",
Jooyung Hana0503a52023-08-23 13:12:50 +09009544 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.capex:myapex.capex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009545 },
9546 {
9547 name: "test_using_apex",
9548 ref: ":myapex{.apex}",
Jooyung Hana0503a52023-08-23 13:12:50 +09009549 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.apex:myapex.apex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009550 },
9551 } {
9552 t.Run(tc.name, func(t *testing.T) {
9553 ctx := testApex(t, `
9554 apex {
9555 name: "myapex",
9556 key: "myapex.key",
9557 compressible: true,
9558 updatable: false,
9559 }
9560
9561 apex_key {
9562 name: "myapex.key",
9563 public_key: "testkey.avbpubkey",
9564 private_key: "testkey.pem",
9565 }
9566
9567 java_test {
9568 name: "`+tc.name+`",
9569 srcs: ["a.java"],
9570 data: ["`+tc.ref+`"],
9571 }
9572 `,
9573 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9574 variables.CompressedApex = proptools.BoolPtr(true)
9575 }))
9576 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9577 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9578 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9579 })
9580 }
9581}
9582
satayev758968a2021-12-06 11:42:40 +00009583func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9584 preparer := android.GroupFixturePreparers(
9585 PrepareForTestWithApexBuildComponents,
9586 prepareForTestWithMyapex,
9587 java.PrepareForTestWithJavaSdkLibraryFiles,
9588 java.PrepareForTestWithJavaDefaultModules,
9589 android.PrepareForTestWithAndroidBuildComponents,
9590 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9591 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9592 )
9593
9594 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9595 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9596 preparer.RunTestWithBp(t, `
9597 apex {
9598 name: "myapex",
9599 key: "myapex.key",
9600 bootclasspath_fragments: ["mybootclasspathfragment"],
9601 min_sdk_version: "30",
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 bootclasspath_fragment {
9612 name: "mybootclasspathfragment",
9613 contents: ["mybootclasspathlib"],
9614 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009615 hidden_api: {
9616 split_packages: ["*"],
9617 },
satayev758968a2021-12-06 11:42:40 +00009618 }
9619
9620 java_sdk_library {
9621 name: "mybootclasspathlib",
9622 srcs: ["mybootclasspathlib.java"],
9623 apex_available: ["myapex"],
9624 compile_dex: true,
9625 unsafe_ignore_missing_latest_api: true,
9626 min_sdk_version: "31",
9627 static_libs: ["util"],
9628 }
9629
9630 java_library {
9631 name: "util",
9632 srcs: ["a.java"],
9633 apex_available: ["myapex"],
9634 min_sdk_version: "31",
9635 static_libs: ["another_util"],
9636 }
9637
9638 java_library {
9639 name: "another_util",
9640 srcs: ["a.java"],
9641 min_sdk_version: "31",
9642 apex_available: ["myapex"],
9643 }
9644 `)
9645 })
9646
9647 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9648 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9649 preparer.RunTestWithBp(t, `
9650 apex {
9651 name: "myapex",
9652 key: "myapex.key",
9653 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9654 min_sdk_version: "30",
9655 updatable: false,
9656 }
9657
9658 apex_key {
9659 name: "myapex.key",
9660 public_key: "testkey.avbpubkey",
9661 private_key: "testkey.pem",
9662 }
9663
9664 systemserverclasspath_fragment {
9665 name: "mysystemserverclasspathfragment",
9666 contents: ["mysystemserverclasspathlib"],
9667 apex_available: ["myapex"],
9668 }
9669
9670 java_sdk_library {
9671 name: "mysystemserverclasspathlib",
9672 srcs: ["mysystemserverclasspathlib.java"],
9673 apex_available: ["myapex"],
9674 compile_dex: true,
9675 min_sdk_version: "32",
9676 unsafe_ignore_missing_latest_api: true,
9677 static_libs: ["util"],
9678 }
9679
9680 java_library {
9681 name: "util",
9682 srcs: ["a.java"],
9683 apex_available: ["myapex"],
9684 min_sdk_version: "31",
9685 static_libs: ["another_util"],
9686 }
9687
9688 java_library {
9689 name: "another_util",
9690 srcs: ["a.java"],
9691 min_sdk_version: "31",
9692 apex_available: ["myapex"],
9693 }
9694 `)
9695 })
9696
9697 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9698 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9699 RunTestWithBp(t, `
9700 apex {
9701 name: "myapex",
9702 key: "myapex.key",
9703 bootclasspath_fragments: ["mybootclasspathfragment"],
9704 min_sdk_version: "30",
9705 updatable: false,
9706 }
9707
9708 apex_key {
9709 name: "myapex.key",
9710 public_key: "testkey.avbpubkey",
9711 private_key: "testkey.pem",
9712 }
9713
9714 bootclasspath_fragment {
9715 name: "mybootclasspathfragment",
9716 contents: ["mybootclasspathlib"],
9717 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009718 hidden_api: {
9719 split_packages: ["*"],
9720 },
satayev758968a2021-12-06 11:42:40 +00009721 }
9722
9723 java_sdk_library {
9724 name: "mybootclasspathlib",
9725 srcs: ["mybootclasspathlib.java"],
9726 apex_available: ["myapex"],
9727 compile_dex: true,
9728 unsafe_ignore_missing_latest_api: true,
9729 }
9730 `)
9731 })
9732
9733 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9734 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9735 RunTestWithBp(t, `
9736 apex {
9737 name: "myapex",
9738 key: "myapex.key",
9739 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9740 min_sdk_version: "30",
9741 updatable: false,
9742 }
9743
9744 apex_key {
9745 name: "myapex.key",
9746 public_key: "testkey.avbpubkey",
9747 private_key: "testkey.pem",
9748 }
9749
9750 systemserverclasspath_fragment {
9751 name: "mysystemserverclasspathfragment",
9752 contents: ["mysystemserverclasspathlib"],
9753 apex_available: ["myapex"],
9754 }
9755
9756 java_sdk_library {
9757 name: "mysystemserverclasspathlib",
9758 srcs: ["mysystemserverclasspathlib.java"],
9759 apex_available: ["myapex"],
9760 compile_dex: true,
9761 unsafe_ignore_missing_latest_api: true,
9762 }
9763 `)
9764 })
9765}
9766
Jiakai Zhang6decef92022-01-12 17:56:19 +00009767// Verifies that the APEX depends on all the Make modules in the list.
9768func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9769 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9770 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009771 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009772 }
9773}
9774
9775// Verifies that the APEX does not depend on any of the Make modules in the list.
9776func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9777 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9778 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009779 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009780 }
9781}
9782
Cole Faust24e25c02024-01-19 14:12:17 -08009783func TestApexStrictUpdtabilityLint(t *testing.T) {
9784 bpTemplate := `
9785 apex {
9786 name: "myapex",
9787 key: "myapex.key",
9788 java_libs: ["myjavalib"],
9789 updatable: %v,
9790 min_sdk_version: "29",
9791 }
9792 apex_key {
9793 name: "myapex.key",
9794 }
9795 java_library {
9796 name: "myjavalib",
9797 srcs: ["MyClass.java"],
9798 apex_available: [ "myapex" ],
9799 lint: {
9800 strict_updatability_linting: %v,
9801 %s
9802 },
9803 sdk_version: "current",
9804 min_sdk_version: "29",
9805 }
9806 `
9807 fs := android.MockFS{
9808 "lint-baseline.xml": nil,
9809 }
9810
9811 testCases := []struct {
9812 testCaseName string
9813 apexUpdatable bool
9814 javaStrictUpdtabilityLint bool
9815 lintFileExists bool
9816 disallowedFlagExpected bool
9817 }{
9818 {
9819 testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9820 apexUpdatable: true,
9821 javaStrictUpdtabilityLint: true,
9822 lintFileExists: false,
9823 disallowedFlagExpected: false,
9824 },
9825 {
9826 testCaseName: "non-updatable apex respects strict_updatability of javalib",
9827 apexUpdatable: false,
9828 javaStrictUpdtabilityLint: false,
9829 lintFileExists: true,
9830 disallowedFlagExpected: false,
9831 },
9832 {
9833 testCaseName: "non-updatable apex respects strict updatability of javalib",
9834 apexUpdatable: false,
9835 javaStrictUpdtabilityLint: true,
9836 lintFileExists: true,
9837 disallowedFlagExpected: true,
9838 },
9839 {
9840 testCaseName: "updatable apex sets strict updatability of javalib to true",
9841 apexUpdatable: true,
9842 javaStrictUpdtabilityLint: false, // will be set to true by mutator
9843 lintFileExists: true,
9844 disallowedFlagExpected: true,
9845 },
9846 }
9847
9848 for _, testCase := range testCases {
9849 fixtures := []android.FixturePreparer{}
9850 baselineProperty := ""
9851 if testCase.lintFileExists {
9852 fixtures = append(fixtures, fs.AddToFixture())
9853 baselineProperty = "baseline_filename: \"lint-baseline.xml\""
9854 }
9855 bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint, baselineProperty)
9856
9857 result := testApex(t, bp, fixtures...)
9858 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9859 sboxProto := android.RuleBuilderSboxProtoForTests(t, result, myjavalib.Output("lint.sbox.textproto"))
9860 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9861
9862 if disallowedFlagActual != testCase.disallowedFlagExpected {
9863 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9864 }
9865 }
9866}
9867
9868func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9869 bp := `
9870 apex {
9871 name: "myapex",
9872 key: "myapex.key",
9873 java_libs: ["myjavalib"],
9874 updatable: true,
9875 min_sdk_version: "29",
9876 }
9877 apex_key {
9878 name: "myapex.key",
9879 }
9880 java_library {
9881 name: "myjavalib",
9882 srcs: ["MyClass.java"],
9883 apex_available: [ "myapex" ],
9884 sdk_version: "current",
9885 min_sdk_version: "29",
9886 lint: {
9887 baseline_filename: "lint-baseline.xml",
9888 }
9889 }
9890 `
9891
9892 testCases := []struct {
9893 testCaseName string
9894 moduleDirectory string
9895 disallowedFlagExpected bool
9896 }{
9897 {
9898 testCaseName: "lintable module defined outside libcore",
9899 moduleDirectory: "",
9900 disallowedFlagExpected: true,
9901 },
9902 {
9903 testCaseName: "lintable module defined in libcore root directory",
9904 moduleDirectory: "libcore/",
9905 disallowedFlagExpected: false,
9906 },
9907 {
9908 testCaseName: "lintable module defined in libcore child directory",
9909 moduleDirectory: "libcore/childdir/",
9910 disallowedFlagExpected: true,
9911 },
9912 }
9913
9914 for _, testCase := range testCases {
9915 lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9916 bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9917 result := testApex(t, "", lintFileCreator, bpFileCreator)
9918 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9919 sboxProto := android.RuleBuilderSboxProtoForTests(t, result, myjavalib.Output("lint.sbox.textproto"))
9920 cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9921 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9922
9923 if disallowedFlagActual != testCase.disallowedFlagExpected {
9924 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9925 }
9926 }
9927}
9928
9929// checks transtive deps of an apex coming from bootclasspath_fragment
9930func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9931 bp := `
9932 apex {
9933 name: "myapex",
9934 key: "myapex.key",
9935 bootclasspath_fragments: ["mybootclasspathfragment"],
9936 updatable: true,
9937 min_sdk_version: "29",
9938 }
9939 apex_key {
9940 name: "myapex.key",
9941 }
9942 bootclasspath_fragment {
9943 name: "mybootclasspathfragment",
9944 contents: ["myjavalib"],
9945 apex_available: ["myapex"],
9946 hidden_api: {
9947 split_packages: ["*"],
9948 },
9949 }
9950 java_library {
9951 name: "myjavalib",
9952 srcs: ["MyClass.java"],
9953 apex_available: [ "myapex" ],
9954 sdk_version: "current",
9955 min_sdk_version: "29",
9956 compile_dex: true,
9957 lint: {
9958 baseline_filename: "lint-baseline.xml",
9959 }
9960 }
9961 `
9962 fs := android.MockFS{
9963 "lint-baseline.xml": nil,
9964 }
9965
9966 result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9967 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9968 sboxProto := android.RuleBuilderSboxProtoForTests(t, result, myjavalib.Output("lint.sbox.textproto"))
9969 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9970 t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9971 }
9972}
Spandan Das66773252022-01-15 00:23:18 +00009973
Jihoon Kang784c0052024-06-25 22:15:39 +00009974func TestApexLintBcpFragmentSdkLibDeps(t *testing.T) {
9975 bp := `
9976 apex {
9977 name: "myapex",
9978 key: "myapex.key",
9979 bootclasspath_fragments: ["mybootclasspathfragment"],
9980 min_sdk_version: "29",
9981 }
9982 apex_key {
9983 name: "myapex.key",
9984 }
9985 bootclasspath_fragment {
9986 name: "mybootclasspathfragment",
9987 contents: ["foo"],
9988 apex_available: ["myapex"],
9989 hidden_api: {
9990 split_packages: ["*"],
9991 },
9992 }
9993 java_sdk_library {
9994 name: "foo",
9995 srcs: ["MyClass.java"],
9996 apex_available: [ "myapex" ],
9997 sdk_version: "current",
9998 min_sdk_version: "29",
9999 compile_dex: true,
10000 }
10001 `
10002 fs := android.MockFS{
10003 "lint-baseline.xml": nil,
10004 }
10005
10006 result := android.GroupFixturePreparers(
10007 prepareForApexTest,
10008 java.PrepareForTestWithJavaSdkLibraryFiles,
10009 java.PrepareForTestWithJacocoInstrumentation,
10010 java.FixtureWithLastReleaseApis("foo"),
Jihoon Kang784c0052024-06-25 22:15:39 +000010011 android.FixtureMergeMockFs(fs),
10012 ).RunTestWithBp(t, bp)
10013
10014 myapex := result.ModuleForTests("myapex", "android_common_myapex")
10015 lintReportInputs := strings.Join(myapex.Output("lint-report-xml.zip").Inputs.Strings(), " ")
10016 android.AssertStringDoesContain(t,
10017 "myapex lint report expected to contain that of the sdk library impl lib as an input",
10018 lintReportInputs, "foo.impl")
10019}
10020
Spandan Das42e89502022-05-06 22:12:55 +000010021// updatable apexes should propagate updatable=true to its apps
10022func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
10023 bp := `
10024 apex {
10025 name: "myapex",
10026 key: "myapex.key",
10027 updatable: %v,
10028 apps: [
10029 "myapp",
10030 ],
10031 min_sdk_version: "30",
10032 }
10033 apex_key {
10034 name: "myapex.key",
10035 }
10036 android_app {
10037 name: "myapp",
10038 updatable: %v,
10039 apex_available: [
10040 "myapex",
10041 ],
10042 sdk_version: "current",
10043 min_sdk_version: "30",
10044 }
10045 `
10046 testCases := []struct {
10047 name string
10048 apex_is_updatable_bp bool
10049 app_is_updatable_bp bool
10050 app_is_updatable_expected bool
10051 }{
10052 {
10053 name: "Non-updatable apex respects updatable property of non-updatable app",
10054 apex_is_updatable_bp: false,
10055 app_is_updatable_bp: false,
10056 app_is_updatable_expected: false,
10057 },
10058 {
10059 name: "Non-updatable apex respects updatable property of updatable app",
10060 apex_is_updatable_bp: false,
10061 app_is_updatable_bp: true,
10062 app_is_updatable_expected: true,
10063 },
10064 {
10065 name: "Updatable apex respects updatable property of updatable app",
10066 apex_is_updatable_bp: true,
10067 app_is_updatable_bp: true,
10068 app_is_updatable_expected: true,
10069 },
10070 {
10071 name: "Updatable apex sets updatable=true on non-updatable app",
10072 apex_is_updatable_bp: true,
10073 app_is_updatable_bp: false,
10074 app_is_updatable_expected: true,
10075 },
10076 }
10077 for _, testCase := range testCases {
10078 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
10079 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
10080 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
10081 }
10082}
10083
Kiyoung Kim487689e2022-07-26 09:48:22 +090010084func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10085 bp := `
10086 apex {
10087 name: "myapex",
10088 key: "myapex.key",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010089 native_shared_libs: ["libbaz"],
10090 binaries: ["binfoo"],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010091 min_sdk_version: "29",
10092 }
10093 apex_key {
10094 name: "myapex.key",
10095 }
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010096 cc_binary {
10097 name: "binfoo",
10098 shared_libs: ["libbar", "libbaz", "libqux",],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010099 apex_available: ["myapex"],
10100 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010101 recovery_available: false,
10102 }
10103 cc_library {
10104 name: "libbar",
10105 srcs: ["libbar.cc"],
10106 stubs: {
10107 symbol_file: "libbar.map.txt",
10108 versions: [
10109 "29",
10110 ],
10111 },
10112 }
10113 cc_library {
10114 name: "libbaz",
10115 srcs: ["libbaz.cc"],
10116 apex_available: ["myapex"],
10117 min_sdk_version: "29",
10118 stubs: {
10119 symbol_file: "libbaz.map.txt",
10120 versions: [
10121 "29",
10122 ],
10123 },
Kiyoung Kim487689e2022-07-26 09:48:22 +090010124 }
10125 cc_api_library {
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010126 name: "libbar",
10127 src: "libbar_stub.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010128 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010129 variants: ["apex.29"],
10130 }
10131 cc_api_variant {
10132 name: "libbar",
10133 variant: "apex",
10134 version: "29",
10135 src: "libbar_apex_29.so",
10136 }
10137 cc_api_library {
10138 name: "libbaz",
10139 src: "libbaz_stub.so",
10140 min_sdk_version: "29",
10141 variants: ["apex.29"],
10142 }
10143 cc_api_variant {
10144 name: "libbaz",
10145 variant: "apex",
10146 version: "29",
10147 src: "libbaz_apex_29.so",
10148 }
10149 cc_api_library {
10150 name: "libqux",
10151 src: "libqux_stub.so",
10152 min_sdk_version: "29",
10153 variants: ["apex.29"],
10154 }
10155 cc_api_variant {
10156 name: "libqux",
10157 variant: "apex",
10158 version: "29",
10159 src: "libqux_apex_29.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010160 }
10161 api_imports {
10162 name: "api_imports",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010163 apex_shared_libs: [
10164 "libbar",
10165 "libbaz",
10166 "libqux",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010167 ],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010168 }
10169 `
10170 result := testApex(t, bp)
10171
10172 hasDep := func(m android.Module, wantDep android.Module) bool {
10173 t.Helper()
10174 var found bool
10175 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10176 if dep == wantDep {
10177 found = true
10178 }
10179 })
10180 return found
10181 }
10182
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010183 // Library defines stubs and cc_api_library should be used with cc_api_library
10184 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Module()
10185 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10186 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
Kiyoung Kim487689e2022-07-26 09:48:22 +090010187
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010188 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10189 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
Kiyoung Kim487689e2022-07-26 09:48:22 +090010190
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010191 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Rule("ld").Args["libFlags"]
10192 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10193 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10194 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
10195
10196 // Library defined in the same APEX should be linked with original definition instead of cc_api_library
10197 libbazApexVariant := result.ModuleForTests("libbaz", "android_arm64_armv8-a_shared_apex29").Module()
10198 libbazApiImportCoreVariant := result.ModuleForTests("libbaz.apiimport", "android_arm64_armv8-a_shared").Module()
10199 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even from same APEX", true, hasDep(binfooApexVariant, libbazApiImportCoreVariant))
10200 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbazApexVariant))
10201
10202 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbaz.so")
10203 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbaz.apiimport.so")
10204 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbaz.apex.29.apiimport.so")
10205
10206 // cc_api_library defined without original library should be linked with cc_api_library
10207 libquxApiImportApexVariant := result.ModuleForTests("libqux.apiimport", "android_arm64_armv8-a_shared").Module()
10208 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even original library definition does not exist", true, hasDep(binfooApexVariant, libquxApiImportApexVariant))
10209 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libqux.apex.29.apiimport.so")
10210}
10211
10212func TestPlatformBinaryBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10213 bp := `
10214 apex {
10215 name: "myapex",
10216 key: "myapex.key",
10217 native_shared_libs: ["libbar"],
10218 min_sdk_version: "29",
10219 }
10220 apex_key {
10221 name: "myapex.key",
10222 }
10223 cc_binary {
10224 name: "binfoo",
10225 shared_libs: ["libbar"],
10226 recovery_available: false,
10227 }
10228 cc_library {
10229 name: "libbar",
10230 srcs: ["libbar.cc"],
10231 apex_available: ["myapex"],
10232 min_sdk_version: "29",
10233 stubs: {
10234 symbol_file: "libbar.map.txt",
10235 versions: [
10236 "29",
10237 ],
10238 },
10239 }
10240 cc_api_library {
10241 name: "libbar",
10242 src: "libbar_stub.so",
10243 variants: ["apex.29"],
10244 }
10245 cc_api_variant {
10246 name: "libbar",
10247 variant: "apex",
10248 version: "29",
10249 src: "libbar_apex_29.so",
10250 }
10251 api_imports {
10252 name: "api_imports",
10253 apex_shared_libs: [
10254 "libbar",
10255 ],
10256 }
10257 `
10258
10259 result := testApex(t, bp)
10260
10261 hasDep := func(m android.Module, wantDep android.Module) bool {
10262 t.Helper()
10263 var found bool
10264 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10265 if dep == wantDep {
10266 found = true
10267 }
10268 })
10269 return found
10270 }
10271
10272 // Library defines stubs and cc_api_library should be used with cc_api_library
10273 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Module()
10274 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10275 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
10276
10277 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10278 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
10279
10280 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
10281 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10282 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10283 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
Kiyoung Kim487689e2022-07-26 09:48:22 +090010284}
Dennis Shend4f5d932023-01-31 20:27:21 +000010285
10286func TestTrimmedApex(t *testing.T) {
10287 bp := `
10288 apex {
10289 name: "myapex",
10290 key: "myapex.key",
10291 native_shared_libs: ["libfoo","libbaz"],
10292 min_sdk_version: "29",
10293 trim_against: "mydcla",
10294 }
10295 apex {
10296 name: "mydcla",
10297 key: "myapex.key",
10298 native_shared_libs: ["libfoo","libbar"],
10299 min_sdk_version: "29",
10300 file_contexts: ":myapex-file_contexts",
10301 dynamic_common_lib_apex: true,
10302 }
10303 apex_key {
10304 name: "myapex.key",
10305 }
10306 cc_library {
10307 name: "libfoo",
10308 shared_libs: ["libc"],
10309 apex_available: ["myapex","mydcla"],
10310 min_sdk_version: "29",
10311 }
10312 cc_library {
10313 name: "libbar",
10314 shared_libs: ["libc"],
10315 apex_available: ["myapex","mydcla"],
10316 min_sdk_version: "29",
10317 }
10318 cc_library {
10319 name: "libbaz",
10320 shared_libs: ["libc"],
10321 apex_available: ["myapex","mydcla"],
10322 min_sdk_version: "29",
10323 }
10324 cc_api_library {
10325 name: "libc",
10326 src: "libc.so",
10327 min_sdk_version: "29",
10328 recovery_available: true,
Ivan Lozanoadd122a2023-07-13 11:01:41 -040010329 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +090010330 product_available: true,
Dennis Shend4f5d932023-01-31 20:27:21 +000010331 }
10332 api_imports {
10333 name: "api_imports",
10334 shared_libs: [
10335 "libc",
10336 ],
10337 header_libs: [],
10338 }
10339 `
10340 ctx := testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +090010341 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dennis Shend4f5d932023-01-31 20:27:21 +000010342 apexRule := module.MaybeRule("apexRule")
10343 if apexRule.Rule == nil {
10344 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10345 }
10346
10347 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
Jooyung Hana0503a52023-08-23 13:12:50 +090010348 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("TrimmedApexRule")
Dennis Shend4f5d932023-01-31 20:27:21 +000010349 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10350 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10351 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10352 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10353}
Jingwen Chendea7a642023-03-28 11:30:50 +000010354
10355func TestCannedFsConfig(t *testing.T) {
10356 ctx := testApex(t, `
10357 apex {
10358 name: "myapex",
10359 key: "myapex.key",
10360 updatable: false,
10361 }
10362
10363 apex_key {
10364 name: "myapex.key",
10365 public_key: "testkey.avbpubkey",
10366 private_key: "testkey.pem",
10367 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010368 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010369 generateFsRule := mod.Rule("generateFsConfig")
10370 cmd := generateFsRule.RuleParams.Command
10371
10372 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10373}
10374
10375func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10376 ctx := testApex(t, `
10377 apex {
10378 name: "myapex",
10379 key: "myapex.key",
10380 canned_fs_config: "my_config",
10381 updatable: false,
10382 }
10383
10384 apex_key {
10385 name: "myapex.key",
10386 public_key: "testkey.avbpubkey",
10387 private_key: "testkey.pem",
10388 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010389 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010390 generateFsRule := mod.Rule("generateFsConfig")
10391 cmd := generateFsRule.RuleParams.Command
10392
10393 // Ensure that canned_fs_config has "cat my_config" at the end
10394 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10395}
Spandan Das20fce2d2023-04-12 17:21:39 +000010396
10397func TestStubLibrariesMultipleApexViolation(t *testing.T) {
10398 testCases := []struct {
10399 desc string
10400 hasStubs bool
10401 apexAvailable string
10402 expectedError string
10403 }{
10404 {
10405 desc: "non-stub library can have multiple apex_available",
10406 hasStubs: false,
10407 apexAvailable: `["myapex", "otherapex"]`,
10408 },
10409 {
10410 desc: "stub library should not be available to anyapex",
10411 hasStubs: true,
10412 apexAvailable: `["//apex_available:anyapex"]`,
10413 expectedError: "Stub libraries should have a single apex_available.*anyapex",
10414 },
10415 {
10416 desc: "stub library should not be available to multiple apexes",
10417 hasStubs: true,
10418 apexAvailable: `["myapex", "otherapex"]`,
10419 expectedError: "Stub libraries should have a single apex_available.*myapex.*otherapex",
10420 },
10421 {
10422 desc: "stub library can be available to a core apex and a test apex",
10423 hasStubs: true,
10424 apexAvailable: `["myapex", "test_myapex"]`,
10425 },
10426 }
10427 bpTemplate := `
10428 cc_library {
10429 name: "libfoo",
10430 %v
10431 apex_available: %v,
10432 }
10433 apex {
10434 name: "myapex",
10435 key: "apex.key",
10436 updatable: false,
10437 native_shared_libs: ["libfoo"],
10438 }
10439 apex {
10440 name: "otherapex",
10441 key: "apex.key",
10442 updatable: false,
10443 }
10444 apex_test {
10445 name: "test_myapex",
10446 key: "apex.key",
10447 updatable: false,
10448 native_shared_libs: ["libfoo"],
10449 }
10450 apex_key {
10451 name: "apex.key",
10452 }
10453 `
10454 for _, tc := range testCases {
10455 stubs := ""
10456 if tc.hasStubs {
10457 stubs = `stubs: {symbol_file: "libfoo.map.txt"},`
10458 }
10459 bp := fmt.Sprintf(bpTemplate, stubs, tc.apexAvailable)
10460 mockFsFixturePreparer := android.FixtureModifyMockFS(func(fs android.MockFS) {
10461 fs["system/sepolicy/apex/test_myapex-file_contexts"] = nil
10462 })
10463 if tc.expectedError == "" {
10464 testApex(t, bp, mockFsFixturePreparer)
10465 } else {
10466 testApexError(t, tc.expectedError, bp, mockFsFixturePreparer)
10467 }
10468 }
10469}
Colin Crossbd3a16b2023-04-25 11:30:51 -070010470
10471func TestFileSystemShouldSkipApexLibraries(t *testing.T) {
10472 context := android.GroupFixturePreparers(
10473 android.PrepareForIntegrationTestWithAndroid,
10474 cc.PrepareForIntegrationTestWithCc,
10475 PrepareForTestWithApexBuildComponents,
10476 prepareForTestWithMyapex,
10477 filesystem.PrepareForTestWithFilesystemBuildComponents,
10478 )
10479 result := context.RunTestWithBp(t, `
10480 android_system_image {
10481 name: "myfilesystem",
10482 deps: [
10483 "libfoo",
10484 ],
10485 linker_config_src: "linker.config.json",
10486 }
10487
10488 cc_library {
10489 name: "libfoo",
10490 shared_libs: [
10491 "libbar",
10492 ],
10493 stl: "none",
10494 }
10495
10496 cc_library {
10497 name: "libbar",
10498 stl: "none",
10499 apex_available: ["myapex"],
10500 }
10501
10502 apex {
10503 name: "myapex",
10504 native_shared_libs: ["libbar"],
10505 key: "myapex.key",
10506 updatable: false,
10507 }
10508
10509 apex_key {
10510 name: "myapex.key",
10511 public_key: "testkey.avbpubkey",
10512 private_key: "testkey.pem",
10513 }
10514 `)
10515
Cole Faust3b806d32024-03-11 15:15:03 -070010516 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits
Colin Crossbd3a16b2023-04-25 11:30:51 -070010517 android.AssertStringListDoesNotContain(t, "filesystem should not have libbar",
10518 inputs.Strings(),
10519 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
10520}
Yu Liueae7b362023-11-16 17:05:47 -080010521
10522var apex_default_bp = `
10523 apex_key {
10524 name: "myapex.key",
10525 public_key: "testkey.avbpubkey",
10526 private_key: "testkey.pem",
10527 }
10528
10529 filegroup {
10530 name: "myapex.manifest",
10531 srcs: ["apex_manifest.json"],
10532 }
10533
10534 filegroup {
10535 name: "myapex.androidmanifest",
10536 srcs: ["AndroidManifest.xml"],
10537 }
10538`
10539
10540func TestAconfigFilesJavaDeps(t *testing.T) {
10541 ctx := testApex(t, apex_default_bp+`
10542 apex {
10543 name: "myapex",
10544 manifest: ":myapex.manifest",
10545 androidManifest: ":myapex.androidmanifest",
10546 key: "myapex.key",
10547 java_libs: [
10548 "my_java_library_foo",
10549 "my_java_library_bar",
10550 ],
10551 updatable: false,
10552 }
10553
10554 java_library {
10555 name: "my_java_library_foo",
10556 srcs: ["foo/bar/MyClass.java"],
10557 sdk_version: "none",
10558 system_modules: "none",
10559 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010560 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010561 "myapex",
10562 ],
10563 }
10564
10565 java_library {
10566 name: "my_java_library_bar",
10567 srcs: ["foo/bar/MyClass.java"],
10568 sdk_version: "none",
10569 system_modules: "none",
10570 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010571 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010572 "myapex",
10573 ],
10574 }
10575
10576 aconfig_declarations {
10577 name: "my_aconfig_declarations_foo",
10578 package: "com.example.package",
10579 container: "myapex",
10580 srcs: ["foo.aconfig"],
10581 }
10582
10583 java_aconfig_library {
10584 name: "my_java_aconfig_library_foo",
10585 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010586 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010587 "myapex",
10588 ],
10589 }
10590
10591 aconfig_declarations {
10592 name: "my_aconfig_declarations_bar",
10593 package: "com.example.package",
10594 container: "myapex",
10595 srcs: ["bar.aconfig"],
10596 }
10597
10598 java_aconfig_library {
10599 name: "my_java_aconfig_library_bar",
10600 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010601 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010602 "myapex",
10603 ],
10604 }
10605 `)
10606
10607 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10608 s := mod.Rule("apexRule").Args["copy_commands"]
10609 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Yu Liubba555e2024-02-17 00:36:42 +000010610 if len(copyCmds) != 8 {
Yu Liueae7b362023-11-16 17:05:47 -080010611 t.Fatalf("Expected 5 commands, got %d in:\n%s", len(copyCmds), s)
10612 }
10613
Yu Liuab31c822024-02-28 22:21:31 +000010614 ensureMatches(t, copyCmds[4], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10615 ensureMatches(t, copyCmds[5], "^cp -f .*/package.map .*/image.apex/etc$")
10616 ensureMatches(t, copyCmds[6], "^cp -f .*/flag.map .*/image.apex/etc$")
10617 ensureMatches(t, copyCmds[7], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liueae7b362023-11-16 17:05:47 -080010618
Yu Liubba555e2024-02-17 00:36:42 +000010619 inputs := []string{
10620 "my_aconfig_declarations_foo/intermediate.pb",
10621 "my_aconfig_declarations_bar/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010622 }
Yu Liubba555e2024-02-17 00:36:42 +000010623 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10624 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10625 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10626 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010627}
10628
10629func TestAconfigFilesJavaAndCcDeps(t *testing.T) {
10630 ctx := testApex(t, apex_default_bp+`
10631 apex {
10632 name: "myapex",
10633 manifest: ":myapex.manifest",
10634 androidManifest: ":myapex.androidmanifest",
10635 key: "myapex.key",
10636 java_libs: [
10637 "my_java_library_foo",
10638 ],
10639 native_shared_libs: [
10640 "my_cc_library_bar",
10641 ],
10642 binaries: [
10643 "my_cc_binary_baz",
10644 ],
10645 updatable: false,
10646 }
10647
10648 java_library {
10649 name: "my_java_library_foo",
10650 srcs: ["foo/bar/MyClass.java"],
10651 sdk_version: "none",
10652 system_modules: "none",
10653 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010654 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010655 "myapex",
10656 ],
10657 }
10658
10659 cc_library {
10660 name: "my_cc_library_bar",
10661 srcs: ["foo/bar/MyClass.cc"],
Yu Liucec0e412023-11-30 16:45:50 -080010662 static_libs: [
10663 "my_cc_aconfig_library_bar",
10664 "my_cc_aconfig_library_baz",
10665 ],
Yu Liueae7b362023-11-16 17:05:47 -080010666 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010667 "myapex",
10668 ],
10669 }
10670
10671 cc_binary {
10672 name: "my_cc_binary_baz",
10673 srcs: ["foo/bar/MyClass.cc"],
10674 static_libs: ["my_cc_aconfig_library_baz"],
Yu Liueae7b362023-11-16 17:05:47 -080010675 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010676 "myapex",
10677 ],
10678 }
10679
10680 aconfig_declarations {
10681 name: "my_aconfig_declarations_foo",
10682 package: "com.example.package",
10683 container: "myapex",
10684 srcs: ["foo.aconfig"],
10685 }
10686
10687 java_aconfig_library {
10688 name: "my_java_aconfig_library_foo",
10689 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010690 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010691 "myapex",
10692 ],
10693 }
10694
10695 aconfig_declarations {
10696 name: "my_aconfig_declarations_bar",
10697 package: "com.example.package",
10698 container: "myapex",
10699 srcs: ["bar.aconfig"],
10700 }
10701
10702 cc_aconfig_library {
10703 name: "my_cc_aconfig_library_bar",
10704 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010705 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010706 "myapex",
10707 ],
10708 }
10709
10710 aconfig_declarations {
10711 name: "my_aconfig_declarations_baz",
10712 package: "com.example.package",
10713 container: "myapex",
10714 srcs: ["baz.aconfig"],
10715 }
10716
10717 cc_aconfig_library {
10718 name: "my_cc_aconfig_library_baz",
10719 aconfig_declarations: "my_aconfig_declarations_baz",
Yu Liueae7b362023-11-16 17:05:47 -080010720 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010721 "myapex",
10722 ],
10723 }
10724
10725 cc_library {
10726 name: "server_configurable_flags",
10727 srcs: ["server_configurable_flags.cc"],
10728 }
Ted Bauerf0f18592024-04-23 18:25:26 +000010729 cc_library {
10730 name: "libbase",
10731 srcs: ["libbase.cc"],
Ted Bauer1e96f8c2024-04-25 19:50:01 +000010732 apex_available: [
10733 "myapex",
10734 ],
Ted Bauerf0f18592024-04-23 18:25:26 +000010735 }
10736 cc_library {
10737 name: "libaconfig_storage_read_api_cc",
10738 srcs: ["libaconfig_storage_read_api_cc.cc"],
10739 }
Yu Liueae7b362023-11-16 17:05:47 -080010740 `)
10741
10742 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10743 s := mod.Rule("apexRule").Args["copy_commands"]
10744 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Yu Liubba555e2024-02-17 00:36:42 +000010745 if len(copyCmds) != 12 {
10746 t.Fatalf("Expected 12 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010747 }
10748
Yu Liuab31c822024-02-28 22:21:31 +000010749 ensureMatches(t, copyCmds[8], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10750 ensureMatches(t, copyCmds[9], "^cp -f .*/package.map .*/image.apex/etc$")
10751 ensureMatches(t, copyCmds[10], "^cp -f .*/flag.map .*/image.apex/etc$")
10752 ensureMatches(t, copyCmds[11], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liueae7b362023-11-16 17:05:47 -080010753
Yu Liubba555e2024-02-17 00:36:42 +000010754 inputs := []string{
10755 "my_aconfig_declarations_foo/intermediate.pb",
10756 "my_cc_library_bar/android_arm64_armv8-a_shared_apex10000/myapex/aconfig_merged.pb",
10757 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010758 }
Yu Liubba555e2024-02-17 00:36:42 +000010759 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10760 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10761 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10762 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010763}
10764
Yu Liucec0e412023-11-30 16:45:50 -080010765func TestAconfigFilesRustDeps(t *testing.T) {
10766 ctx := testApex(t, apex_default_bp+`
10767 apex {
10768 name: "myapex",
10769 manifest: ":myapex.manifest",
10770 androidManifest: ":myapex.androidmanifest",
10771 key: "myapex.key",
10772 native_shared_libs: [
10773 "libmy_rust_library",
10774 ],
10775 binaries: [
10776 "my_rust_binary",
10777 ],
10778 rust_dyn_libs: [
10779 "libmy_rust_dylib",
10780 ],
10781 updatable: false,
10782 }
10783
10784 rust_library {
10785 name: "libflags_rust", // test mock
10786 crate_name: "flags_rust",
10787 srcs: ["lib.rs"],
10788 apex_available: [
10789 "myapex",
10790 ],
10791 }
10792
10793 rust_library {
10794 name: "liblazy_static", // test mock
10795 crate_name: "lazy_static",
10796 srcs: ["src/lib.rs"],
10797 apex_available: [
10798 "myapex",
10799 ],
10800 }
10801
Ted Bauer02d475c2024-03-27 20:56:26 +000010802 rust_library {
10803 name: "libaconfig_storage_read_api", // test mock
10804 crate_name: "aconfig_storage_read_api",
10805 srcs: ["src/lib.rs"],
10806 apex_available: [
10807 "myapex",
10808 ],
10809 }
10810
Ted Bauer6ef40db2024-03-29 14:04:10 +000010811 rust_library {
10812 name: "liblogger", // test mock
10813 crate_name: "logger",
10814 srcs: ["src/lib.rs"],
10815 apex_available: [
10816 "myapex",
10817 ],
10818 }
10819
10820 rust_library {
10821 name: "liblog_rust", // test mock
10822 crate_name: "log_rust",
10823 srcs: ["src/lib.rs"],
10824 apex_available: [
10825 "myapex",
10826 ],
10827 }
10828
Yu Liucec0e412023-11-30 16:45:50 -080010829 rust_ffi_shared {
10830 name: "libmy_rust_library",
10831 srcs: ["src/lib.rs"],
10832 rustlibs: ["libmy_rust_aconfig_library_foo"],
10833 crate_name: "my_rust_library",
10834 apex_available: [
10835 "myapex",
10836 ],
10837 }
10838
10839 rust_library_dylib {
10840 name: "libmy_rust_dylib",
10841 srcs: ["foo/bar/MyClass.rs"],
10842 rustlibs: ["libmy_rust_aconfig_library_bar"],
10843 crate_name: "my_rust_dylib",
10844 apex_available: [
10845 "myapex",
10846 ],
10847 }
10848
10849 rust_binary {
10850 name: "my_rust_binary",
10851 srcs: ["foo/bar/MyClass.rs"],
10852 rustlibs: [
10853 "libmy_rust_aconfig_library_baz",
10854 "libmy_rust_dylib",
10855 ],
10856 apex_available: [
10857 "myapex",
10858 ],
10859 }
10860
10861 aconfig_declarations {
10862 name: "my_aconfig_declarations_foo",
10863 package: "com.example.package",
10864 container: "myapex",
10865 srcs: ["foo.aconfig"],
10866 }
10867
10868 aconfig_declarations {
10869 name: "my_aconfig_declarations_bar",
10870 package: "com.example.package",
10871 container: "myapex",
10872 srcs: ["bar.aconfig"],
10873 }
10874
10875 aconfig_declarations {
10876 name: "my_aconfig_declarations_baz",
10877 package: "com.example.package",
10878 container: "myapex",
10879 srcs: ["baz.aconfig"],
10880 }
10881
10882 rust_aconfig_library {
10883 name: "libmy_rust_aconfig_library_foo",
10884 aconfig_declarations: "my_aconfig_declarations_foo",
10885 crate_name: "my_rust_aconfig_library_foo",
10886 apex_available: [
10887 "myapex",
10888 ],
10889 }
10890
10891 rust_aconfig_library {
10892 name: "libmy_rust_aconfig_library_bar",
10893 aconfig_declarations: "my_aconfig_declarations_bar",
10894 crate_name: "my_rust_aconfig_library_bar",
10895 apex_available: [
10896 "myapex",
10897 ],
10898 }
10899
10900 rust_aconfig_library {
10901 name: "libmy_rust_aconfig_library_baz",
10902 aconfig_declarations: "my_aconfig_declarations_baz",
10903 crate_name: "my_rust_aconfig_library_baz",
10904 apex_available: [
10905 "myapex",
10906 ],
10907 }
10908 `)
10909
10910 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10911 s := mod.Rule("apexRule").Args["copy_commands"]
10912 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Ted Bauer6ef40db2024-03-29 14:04:10 +000010913 if len(copyCmds) != 32 {
Ted Bauer02d475c2024-03-27 20:56:26 +000010914 t.Fatalf("Expected 28 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liucec0e412023-11-30 16:45:50 -080010915 }
10916
Ted Bauer6ef40db2024-03-29 14:04:10 +000010917 ensureMatches(t, copyCmds[28], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10918 ensureMatches(t, copyCmds[29], "^cp -f .*/package.map .*/image.apex/etc$")
10919 ensureMatches(t, copyCmds[30], "^cp -f .*/flag.map .*/image.apex/etc$")
10920 ensureMatches(t, copyCmds[31], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liucec0e412023-11-30 16:45:50 -080010921
Yu Liubba555e2024-02-17 00:36:42 +000010922 inputs := []string{
10923 "my_aconfig_declarations_foo/intermediate.pb",
Yu Liuab31c822024-02-28 22:21:31 +000010924 "my_aconfig_declarations_bar/intermediate.pb",
10925 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liubba555e2024-02-17 00:36:42 +000010926 "my_rust_binary/android_arm64_armv8-a_apex10000/myapex/aconfig_merged.pb",
10927 }
10928 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10929 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10930 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10931 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
10932}
10933
10934func VerifyAconfigRule(t *testing.T, mod *android.TestingModule, desc string, inputs []string, output string, container string, file_type string) {
10935 aconfigRule := mod.Description(desc)
10936 s := " " + aconfigRule.Args["cache_files"]
Yu Liucec0e412023-11-30 16:45:50 -080010937 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
Yu Liubba555e2024-02-17 00:36:42 +000010938 if len(aconfigArgs) != len(inputs) {
10939 t.Fatalf("Expected %d commands, got %d in:\n%s", len(inputs), len(aconfigArgs), s)
Yu Liucec0e412023-11-30 16:45:50 -080010940 }
Yu Liucec0e412023-11-30 16:45:50 -080010941
Yu Liubba555e2024-02-17 00:36:42 +000010942 ensureEquals(t, container, aconfigRule.Args["container"])
10943 ensureEquals(t, file_type, aconfigRule.Args["file_type"])
10944
10945 buildParams := aconfigRule.BuildParams
10946 for _, input := range inputs {
10947 android.EnsureListContainsSuffix(t, aconfigArgs, input)
10948 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), input)
Yu Liucec0e412023-11-30 16:45:50 -080010949 }
Yu Liubba555e2024-02-17 00:36:42 +000010950
10951 ensureContains(t, buildParams.Output.String(), output)
Yu Liucec0e412023-11-30 16:45:50 -080010952}
10953
Yu Liueae7b362023-11-16 17:05:47 -080010954func TestAconfigFilesOnlyMatchCurrentApex(t *testing.T) {
10955 ctx := testApex(t, apex_default_bp+`
10956 apex {
10957 name: "myapex",
10958 manifest: ":myapex.manifest",
10959 androidManifest: ":myapex.androidmanifest",
10960 key: "myapex.key",
10961 java_libs: [
10962 "my_java_library_foo",
10963 "other_java_library_bar",
10964 ],
10965 updatable: false,
10966 }
10967
10968 java_library {
10969 name: "my_java_library_foo",
10970 srcs: ["foo/bar/MyClass.java"],
10971 sdk_version: "none",
10972 system_modules: "none",
10973 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010974 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010975 "myapex",
10976 ],
10977 }
10978
10979 java_library {
10980 name: "other_java_library_bar",
10981 srcs: ["foo/bar/MyClass.java"],
10982 sdk_version: "none",
10983 system_modules: "none",
10984 static_libs: ["other_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010985 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010986 "myapex",
10987 ],
10988 }
10989
10990 aconfig_declarations {
10991 name: "my_aconfig_declarations_foo",
10992 package: "com.example.package",
10993 container: "myapex",
10994 srcs: ["foo.aconfig"],
10995 }
10996
10997 java_aconfig_library {
10998 name: "my_java_aconfig_library_foo",
10999 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011000 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011001 "myapex",
11002 ],
11003 }
11004
11005 aconfig_declarations {
11006 name: "other_aconfig_declarations_bar",
11007 package: "com.example.package",
11008 container: "otherapex",
11009 srcs: ["bar.aconfig"],
11010 }
11011
11012 java_aconfig_library {
11013 name: "other_java_aconfig_library_bar",
11014 aconfig_declarations: "other_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080011015 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011016 "myapex",
11017 ],
11018 }
11019 `)
11020
11021 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11022 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11023 s := " " + combineAconfigRule.Args["cache_files"]
11024 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11025 if len(aconfigArgs) != 1 {
11026 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
11027 }
11028 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11029
11030 buildParams := combineAconfigRule.BuildParams
11031 if len(buildParams.Inputs) != 1 {
11032 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
11033 }
11034 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11035 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11036}
11037
11038func TestAconfigFilesRemoveDuplicates(t *testing.T) {
11039 ctx := testApex(t, apex_default_bp+`
11040 apex {
11041 name: "myapex",
11042 manifest: ":myapex.manifest",
11043 androidManifest: ":myapex.androidmanifest",
11044 key: "myapex.key",
11045 java_libs: [
11046 "my_java_library_foo",
11047 "my_java_library_bar",
11048 ],
11049 updatable: false,
11050 }
11051
11052 java_library {
11053 name: "my_java_library_foo",
11054 srcs: ["foo/bar/MyClass.java"],
11055 sdk_version: "none",
11056 system_modules: "none",
11057 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080011058 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011059 "myapex",
11060 ],
11061 }
11062
11063 java_library {
11064 name: "my_java_library_bar",
11065 srcs: ["foo/bar/MyClass.java"],
11066 sdk_version: "none",
11067 system_modules: "none",
11068 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080011069 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011070 "myapex",
11071 ],
11072 }
11073
11074 aconfig_declarations {
11075 name: "my_aconfig_declarations_foo",
11076 package: "com.example.package",
11077 container: "myapex",
11078 srcs: ["foo.aconfig"],
11079 }
11080
11081 java_aconfig_library {
11082 name: "my_java_aconfig_library_foo",
11083 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011084 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011085 "myapex",
11086 ],
11087 }
11088
11089 java_aconfig_library {
11090 name: "my_java_aconfig_library_bar",
11091 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011092 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011093 "myapex",
11094 ],
11095 }
11096 `)
11097
11098 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11099 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11100 s := " " + combineAconfigRule.Args["cache_files"]
11101 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11102 if len(aconfigArgs) != 1 {
11103 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
11104 }
11105 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11106
11107 buildParams := combineAconfigRule.BuildParams
11108 if len(buildParams.Inputs) != 1 {
11109 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
11110 }
11111 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11112 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11113}
Spandan Das5be63332023-12-13 00:06:32 +000011114
11115// Test that the boot jars come from the _selected_ apex prebuilt
11116// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11117func TestBootDexJarsMultipleApexPrebuilts(t *testing.T) {
11118 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
11119 t.Helper()
11120 s := ctx.ModuleForTests("dex_bootjars", "android_common")
11121 foundLibfooJar := false
11122 base := stem + ".jar"
11123 for _, output := range s.AllOutputs() {
11124 if filepath.Base(output) == base {
11125 foundLibfooJar = true
11126 buildRule := s.Output(output)
11127 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
11128 }
11129 }
11130 if !foundLibfooJar {
11131 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
11132 }
11133 }
11134
Spandan Das64c9e0c2023-12-20 20:13:34 +000011135 // Check that the boot jars of the selected apex are run through boot_jars_package_check
11136 // This validates that the jars on the bootclasspath do not contain packages outside an allowlist
11137 checkBootJarsPackageCheck := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11138 platformBcp := ctx.ModuleForTests("platform-bootclasspath", "android_common")
11139 bootJarsCheckRule := platformBcp.Rule("boot_jars_package_check")
11140 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)
11141 }
11142
11143 // Check that the boot jars used to generate the monolithic hiddenapi flags come from the selected apex
11144 checkBootJarsForMonolithicHiddenapi := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11145 monolithicHiddenapiFlagsCmd := ctx.ModuleForTests("platform-bootclasspath", "android_common").Output("out/soong/hiddenapi/hiddenapi-stub-flags.txt").RuleParams.Command
11146 android.AssertStringMatches(t, "Could not find the correct boot dex jar in monolithic hiddenapi flags generation command", monolithicHiddenapiFlagsCmd, "--boot-dex="+expectedBootJar)
11147 }
11148
Spandan Das5be63332023-12-13 00:06:32 +000011149 bp := `
11150 // Source APEX.
11151
11152 java_library {
11153 name: "framework-foo",
11154 srcs: ["foo.java"],
11155 installable: true,
11156 apex_available: [
11157 "com.android.foo",
11158 ],
11159 }
11160
11161 bootclasspath_fragment {
11162 name: "foo-bootclasspath-fragment",
11163 contents: ["framework-foo"],
11164 apex_available: [
11165 "com.android.foo",
11166 ],
11167 hidden_api: {
11168 split_packages: ["*"],
11169 },
11170 }
11171
11172 apex_key {
11173 name: "com.android.foo.key",
11174 public_key: "com.android.foo.avbpubkey",
11175 private_key: "com.android.foo.pem",
11176 }
11177
11178 apex {
11179 name: "com.android.foo",
11180 key: "com.android.foo.key",
11181 bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11182 updatable: false,
11183 }
11184
11185 // Prebuilt APEX.
11186
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011187 java_sdk_library_import {
Spandan Das5be63332023-12-13 00:06:32 +000011188 name: "framework-foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011189 public: {
11190 jars: ["foo.jar"],
11191 },
Spandan Das5be63332023-12-13 00:06:32 +000011192 apex_available: ["com.android.foo"],
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011193 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +000011194 }
11195
11196 prebuilt_bootclasspath_fragment {
11197 name: "foo-bootclasspath-fragment",
11198 contents: ["framework-foo"],
11199 hidden_api: {
11200 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
11201 metadata: "my-bootclasspath-fragment/metadata.csv",
11202 index: "my-bootclasspath-fragment/index.csv",
11203 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
11204 all_flags: "my-bootclasspath-fragment/all-flags.csv",
11205 },
11206 apex_available: [
11207 "com.android.foo",
11208 ],
11209 }
11210
11211 prebuilt_apex {
11212 name: "com.android.foo",
11213 apex_name: "com.android.foo",
11214 src: "com.android.foo-arm.apex",
11215 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11216 }
11217
11218 // Another Prebuilt ART APEX
11219 prebuilt_apex {
11220 name: "com.android.foo.v2",
11221 apex_name: "com.android.foo", // Used to determine the API domain
11222 src: "com.android.foo-arm.apex",
11223 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11224 }
11225
11226 // APEX contribution modules
11227
11228 apex_contributions {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011229 name: "foo.source.contributions",
Spandan Das5be63332023-12-13 00:06:32 +000011230 api_domain: "com.android.foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011231 contents: ["com.android.foo"],
11232 }
11233
11234 apex_contributions {
11235 name: "foo.prebuilt.contributions",
11236 api_domain: "com.android.foo",
11237 contents: ["prebuilt_com.android.foo"],
11238 }
11239
11240 apex_contributions {
11241 name: "foo.prebuilt.v2.contributions",
11242 api_domain: "com.android.foo",
11243 contents: ["com.android.foo.v2"], // prebuilt_ prefix is missing because of prebuilt_rename mutator
Spandan Das5be63332023-12-13 00:06:32 +000011244 }
11245 `
11246
11247 testCases := []struct {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011248 desc string
11249 selectedApexContributions string
11250 expectedBootJar string
Spandan Das5be63332023-12-13 00:06:32 +000011251 }{
11252 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011253 desc: "Source apex com.android.foo is selected, bootjar should come from source java library",
11254 selectedApexContributions: "foo.source.contributions",
11255 expectedBootJar: "out/soong/.intermediates/foo-bootclasspath-fragment/android_common_apex10000/hiddenapi-modular/encoded/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011256 },
11257 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011258 desc: "Prebuilt apex prebuilt_com.android.foo is selected, profile should come from .prof deapexed from the prebuilt",
11259 selectedApexContributions: "foo.prebuilt.contributions",
11260 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011261 },
11262 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011263 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected, profile should come from .prof deapexed from the prebuilt",
11264 selectedApexContributions: "foo.prebuilt.v2.contributions",
11265 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.v2.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011266 },
11267 }
11268
11269 fragment := java.ApexVariantReference{
11270 Apex: proptools.StringPtr("com.android.foo"),
11271 Module: proptools.StringPtr("foo-bootclasspath-fragment"),
11272 }
11273
11274 for _, tc := range testCases {
11275 preparer := android.GroupFixturePreparers(
11276 java.FixtureConfigureApexBootJars("com.android.foo:framework-foo"),
11277 android.FixtureMergeMockFs(map[string][]byte{
11278 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11279 }),
Spandan Das81fe4d12024-05-15 18:43:47 +000011280 // Make sure that we have atleast one platform library so that we can check the monolithic hiddenapi
11281 // file creation.
11282 java.FixtureConfigureBootJars("platform:foo"),
11283 android.FixtureModifyMockFS(func(fs android.MockFS) {
11284 fs["platform/Android.bp"] = []byte(`
11285 java_library {
11286 name: "foo",
11287 srcs: ["Test.java"],
11288 compile_dex: true,
11289 }
11290 `)
11291 fs["platform/Test.java"] = nil
11292 }),
11293
Colin Crossa66b4632024-08-08 15:50:47 -070011294 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das5be63332023-12-13 00:06:32 +000011295 )
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011296 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das5be63332023-12-13 00:06:32 +000011297 checkBootDexJarPath(t, ctx, "framework-foo", tc.expectedBootJar)
Spandan Das64c9e0c2023-12-20 20:13:34 +000011298 checkBootJarsPackageCheck(t, ctx, tc.expectedBootJar)
11299 checkBootJarsForMonolithicHiddenapi(t, ctx, tc.expectedBootJar)
Spandan Das5be63332023-12-13 00:06:32 +000011300 }
11301}
Spandan Das3576e762024-01-03 18:57:03 +000011302
11303// Test that product packaging installs the selected mainline module (either source or a specific prebuilt)
11304// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11305func TestInstallationRulesForMultipleApexPrebuilts(t *testing.T) {
Spandan Das3576e762024-01-03 18:57:03 +000011306 // for a mainline module family, check that only the flagged soong module is visible to make
11307 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleName string, hiddenModuleNames []string) {
11308 variation := func(moduleName string) string {
11309 ret := "android_common_com.android.foo"
11310 if moduleName == "com.google.android.foo" {
Spandan Das50801e22024-05-13 18:29:45 +000011311 ret = "android_common_com.google.android.foo_com.google.android.foo"
Spandan Das3576e762024-01-03 18:57:03 +000011312 }
11313 return ret
11314 }
11315
11316 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11317 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11318
11319 for _, hiddenModuleName := range hiddenModuleNames {
11320 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11321 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11322
11323 }
11324 }
11325
11326 bp := `
11327 apex_key {
11328 name: "com.android.foo.key",
11329 public_key: "com.android.foo.avbpubkey",
11330 private_key: "com.android.foo.pem",
11331 }
11332
11333 // AOSP source apex
11334 apex {
11335 name: "com.android.foo",
11336 key: "com.android.foo.key",
11337 updatable: false,
11338 }
11339
11340 // Google source apex
11341 override_apex {
11342 name: "com.google.android.foo",
11343 base: "com.android.foo",
11344 key: "com.android.foo.key",
11345 }
11346
11347 // Prebuilt Google APEX.
11348
11349 prebuilt_apex {
11350 name: "com.google.android.foo",
11351 apex_name: "com.android.foo",
11352 src: "com.android.foo-arm.apex",
11353 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11354 }
11355
11356 // Another Prebuilt Google APEX
11357 prebuilt_apex {
11358 name: "com.google.android.foo.v2",
11359 apex_name: "com.android.foo",
Spandan Dasa8e2d612024-07-26 19:24:27 +000011360 source_apex_name: "com.google.android.foo",
Spandan Das3576e762024-01-03 18:57:03 +000011361 src: "com.android.foo-arm.apex",
11362 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11363 }
11364
11365 // APEX contribution modules
11366
11367 apex_contributions {
11368 name: "foo.source.contributions",
11369 api_domain: "com.android.foo",
11370 contents: ["com.google.android.foo"],
11371 }
11372
11373 apex_contributions {
11374 name: "foo.prebuilt.contributions",
11375 api_domain: "com.android.foo",
11376 contents: ["prebuilt_com.google.android.foo"],
11377 }
11378
11379 apex_contributions {
11380 name: "foo.prebuilt.v2.contributions",
11381 api_domain: "com.android.foo",
11382 contents: ["prebuilt_com.google.android.foo.v2"],
11383 }
11384
11385 // This is an incompatible module because it selects multiple versions of the same mainline module
11386 apex_contributions {
11387 name: "foo.prebuilt.duplicate.contributions",
11388 api_domain: "com.android.foo",
11389 contents: [
11390 "prebuilt_com.google.android.foo",
11391 "prebuilt_com.google.android.foo.v2",
11392 ],
11393 }
11394 `
11395
11396 testCases := []struct {
11397 desc string
11398 selectedApexContributions string
11399 expectedVisibleModuleName string
11400 expectedHiddenModuleNames []string
11401 expectedError string
11402 }{
11403 {
11404 desc: "Source apex is selected, prebuilts should be hidden from make",
11405 selectedApexContributions: "foo.source.contributions",
11406 expectedVisibleModuleName: "com.google.android.foo",
11407 expectedHiddenModuleNames: []string{"prebuilt_com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11408 },
11409 {
11410 desc: "Prebuilt apex prebuilt_com.android.foo is selected, source and the other prebuilt should be hidden from make",
11411 selectedApexContributions: "foo.prebuilt.contributions",
11412 expectedVisibleModuleName: "prebuilt_com.google.android.foo",
11413 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11414 },
11415 {
11416 desc: "Prebuilt apex prebuilt_com.android.fooi.v2 is selected, source and the other prebuilt should be hidden from make",
11417 selectedApexContributions: "foo.prebuilt.v2.contributions",
11418 expectedVisibleModuleName: "prebuilt_com.google.android.foo.v2",
11419 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo"},
11420 },
11421 {
11422 desc: "Multiple versions of a prebuilt apex is selected in the same release config",
11423 selectedApexContributions: "foo.prebuilt.duplicate.contributions",
11424 expectedError: "Found duplicate variations of the same module in apex_contributions: prebuilt_com.google.android.foo and prebuilt_com.google.android.foo.v2",
11425 },
11426 }
11427
11428 for _, tc := range testCases {
11429 preparer := android.GroupFixturePreparers(
11430 android.FixtureMergeMockFs(map[string][]byte{
11431 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11432 }),
Colin Crossa66b4632024-08-08 15:50:47 -070011433 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das3576e762024-01-03 18:57:03 +000011434 )
11435 if tc.expectedError != "" {
11436 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(tc.expectedError))
11437 testApex(t, bp, preparer)
11438 return
11439 }
11440 ctx := testApex(t, bp, preparer)
11441
Spandan Das3576e762024-01-03 18:57:03 +000011442 // Check that
11443 // 1. The contents of the selected apex_contributions are visible to make
11444 // 2. The rest of the apexes in the mainline module family (source or other prebuilt) is hidden from make
11445 checkHideFromMake(t, ctx, tc.expectedVisibleModuleName, tc.expectedHiddenModuleNames)
11446 }
11447}
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011448
Spandan Das85bd4622024-08-01 00:51:20 +000011449// Test that product packaging installs the selected mainline module in workspaces withtout source mainline module
11450func TestInstallationRulesForMultipleApexPrebuiltsWithoutSource(t *testing.T) {
11451 // for a mainline module family, check that only the flagged soong module is visible to make
11452 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleNames []string, hiddenModuleNames []string) {
11453 variation := func(moduleName string) string {
11454 ret := "android_common_com.android.adservices"
11455 if moduleName == "com.google.android.foo" {
11456 ret = "android_common_com.google.android.foo_com.google.android.foo"
11457 }
11458 return ret
11459 }
11460
11461 for _, visibleModuleName := range visibleModuleNames {
11462 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11463 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11464 }
11465
11466 for _, hiddenModuleName := range hiddenModuleNames {
11467 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11468 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11469
11470 }
11471 }
11472
11473 bp := `
11474 apex_key {
11475 name: "com.android.adservices.key",
11476 public_key: "com.android.adservices.avbpubkey",
11477 private_key: "com.android.adservices.pem",
11478 }
11479
11480 // AOSP source apex
11481 apex {
11482 name: "com.android.adservices",
11483 key: "com.android.adservices.key",
11484 updatable: false,
11485 }
11486
11487 // Prebuilt Google APEX.
11488
11489 prebuilt_apex {
11490 name: "com.google.android.adservices",
11491 apex_name: "com.android.adservices",
11492 src: "com.android.foo-arm.apex",
11493 }
11494
11495 // Another Prebuilt Google APEX
11496 prebuilt_apex {
11497 name: "com.google.android.adservices.v2",
11498 apex_name: "com.android.adservices",
11499 src: "com.android.foo-arm.apex",
11500 }
11501
11502 // APEX contribution modules
11503
11504
11505 apex_contributions {
11506 name: "adservices.prebuilt.contributions",
11507 api_domain: "com.android.adservices",
11508 contents: ["prebuilt_com.google.android.adservices"],
11509 }
11510
11511 apex_contributions {
11512 name: "adservices.prebuilt.v2.contributions",
11513 api_domain: "com.android.adservices",
11514 contents: ["prebuilt_com.google.android.adservices.v2"],
11515 }
11516 `
11517
11518 testCases := []struct {
11519 desc string
11520 selectedApexContributions string
11521 expectedVisibleModuleNames []string
11522 expectedHiddenModuleNames []string
11523 }{
11524 {
11525 desc: "No apex contributions selected, source aosp apex should be visible, and mainline prebuilts should be hidden",
11526 selectedApexContributions: "",
11527 expectedVisibleModuleNames: []string{"com.android.adservices"},
11528 expectedHiddenModuleNames: []string{"com.google.android.adservices", "com.google.android.adservices.v2"},
11529 },
11530 {
11531 desc: "Prebuilt apex prebuilt_com.android.foo is selected",
11532 selectedApexContributions: "adservices.prebuilt.contributions",
11533 expectedVisibleModuleNames: []string{"com.android.adservices", "com.google.android.adservices"},
11534 expectedHiddenModuleNames: []string{"com.google.android.adservices.v2"},
11535 },
11536 {
11537 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected",
11538 selectedApexContributions: "adservices.prebuilt.v2.contributions",
11539 expectedVisibleModuleNames: []string{"com.android.adservices", "com.google.android.adservices.v2"},
11540 expectedHiddenModuleNames: []string{"com.google.android.adservices"},
11541 },
11542 }
11543
11544 for _, tc := range testCases {
11545 preparer := android.GroupFixturePreparers(
11546 android.FixtureMergeMockFs(map[string][]byte{
11547 "system/sepolicy/apex/com.android.adservices-file_contexts": nil,
11548 }),
Colin Crossa66b4632024-08-08 15:50:47 -070011549 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
Spandan Das85bd4622024-08-01 00:51:20 +000011550 )
11551 ctx := testApex(t, bp, preparer)
11552
11553 checkHideFromMake(t, ctx, tc.expectedVisibleModuleNames, tc.expectedHiddenModuleNames)
11554 }
11555}
11556
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011557func TestAconfifDeclarationsValidation(t *testing.T) {
11558 aconfigDeclarationLibraryString := func(moduleNames []string) (ret string) {
11559 for _, moduleName := range moduleNames {
11560 ret += fmt.Sprintf(`
11561 aconfig_declarations {
11562 name: "%[1]s",
11563 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +000011564 container: "system",
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011565 srcs: [
11566 "%[1]s.aconfig",
11567 ],
11568 }
11569 java_aconfig_library {
11570 name: "%[1]s-lib",
11571 aconfig_declarations: "%[1]s",
11572 }
11573 `, moduleName)
11574 }
11575 return ret
11576 }
11577
11578 result := android.GroupFixturePreparers(
11579 prepareForApexTest,
11580 java.PrepareForTestWithJavaSdkLibraryFiles,
11581 java.FixtureWithLastReleaseApis("foo"),
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011582 ).RunTestWithBp(t, `
11583 java_library {
11584 name: "baz-java-lib",
11585 static_libs: [
11586 "baz-lib",
11587 ],
11588 }
11589 filegroup {
11590 name: "qux-filegroup",
11591 srcs: [
11592 ":qux-lib{.generated_srcjars}",
11593 ],
11594 }
11595 filegroup {
11596 name: "qux-another-filegroup",
11597 srcs: [
11598 ":qux-filegroup",
11599 ],
11600 }
11601 java_library {
11602 name: "quux-java-lib",
11603 srcs: [
11604 "a.java",
11605 ],
11606 libs: [
11607 "quux-lib",
11608 ],
11609 }
11610 java_sdk_library {
11611 name: "foo",
11612 srcs: [
11613 ":qux-another-filegroup",
11614 ],
11615 api_packages: ["foo"],
11616 system: {
11617 enabled: true,
11618 },
11619 module_lib: {
11620 enabled: true,
11621 },
11622 test: {
11623 enabled: true,
11624 },
11625 static_libs: [
11626 "bar-lib",
11627 ],
11628 libs: [
11629 "baz-java-lib",
11630 "quux-java-lib",
11631 ],
11632 aconfig_declarations: [
11633 "bar",
11634 ],
11635 }
11636 `+aconfigDeclarationLibraryString([]string{"bar", "baz", "qux", "quux"}))
11637
11638 m := result.ModuleForTests("foo.stubs.source", "android_common")
11639 outDir := "out/soong/.intermediates"
11640
11641 // Arguments passed to aconfig to retrieve the state of the flags defined in the
11642 // textproto files
11643 aconfigFlagArgs := m.Output("released-flagged-apis-exportable.txt").Args["flags_path"]
11644
11645 // "bar-lib" is a static_lib of "foo" and is passed to metalava as classpath. Thus the
11646 // cache file provided by the associated aconfig_declarations module "bar" should be passed
11647 // to aconfig.
11648 android.AssertStringDoesContain(t, "cache file of a java_aconfig_library static_lib "+
11649 "passed as an input",
11650 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "bar"))
11651
11652 // "baz-java-lib", which statically depends on "baz-lib", is a lib of "foo" and is passed
11653 // to metalava as classpath. Thus the cache file provided by the associated
11654 // aconfig_declarations module "baz" should be passed to aconfig.
11655 android.AssertStringDoesContain(t, "cache file of a lib that statically depends on "+
11656 "java_aconfig_library passed as an input",
11657 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "baz"))
11658
11659 // "qux-lib" is passed to metalava as src via the filegroup, thus the cache file provided by
11660 // the associated aconfig_declarations module "qux" should be passed to aconfig.
11661 android.AssertStringDoesContain(t, "cache file of srcs java_aconfig_library passed as an "+
11662 "input",
11663 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "qux"))
11664
11665 // "quux-java-lib" is a lib of "foo" and is passed to metalava as classpath, but does not
11666 // statically depend on "quux-lib". Therefore, the cache file provided by the associated
11667 // aconfig_declarations module "quux" should not be passed to aconfig.
11668 android.AssertStringDoesNotContain(t, "cache file of a lib that does not statically "+
11669 "depend on java_aconfig_library not passed as an input",
11670 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "quux"))
11671}
Cole Faust7c991b42024-05-15 11:17:55 -070011672
11673func TestMultiplePrebuiltsWithSameBase(t *testing.T) {
11674 ctx := testApex(t, `
11675 apex {
11676 name: "myapex",
11677 key: "myapex.key",
11678 prebuilts: ["myetc", "myetc2"],
11679 min_sdk_version: "29",
11680 }
11681 apex_key {
11682 name: "myapex.key",
11683 public_key: "testkey.avbpubkey",
11684 private_key: "testkey.pem",
11685 }
11686
11687 prebuilt_etc {
11688 name: "myetc",
11689 src: "myprebuilt",
11690 filename: "myfilename",
11691 }
11692 prebuilt_etc {
11693 name: "myetc2",
11694 sub_dir: "mysubdir",
11695 src: "myprebuilt",
11696 filename: "myfilename",
11697 }
11698 `, withFiles(android.MockFS{
11699 "packages/modules/common/build/allowed_deps.txt": nil,
11700 }))
11701
11702 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
11703 data := android.AndroidMkDataForTest(t, ctx, ab)
11704 var builder strings.Builder
11705 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
11706 androidMk := builder.String()
11707
11708 android.AssertStringDoesContain(t, "not found", androidMk, "LOCAL_MODULE := etc_myfilename.myapex")
11709 android.AssertStringDoesContain(t, "not found", androidMk, "LOCAL_MODULE := etc_mysubdir_myfilename.myapex")
11710}
Spandan Das50801e22024-05-13 18:29:45 +000011711
11712func TestApexMinSdkVersionOverride(t *testing.T) {
11713 checkMinSdkVersion := func(t *testing.T, module android.TestingModule, expectedMinSdkVersion string) {
11714 args := module.Rule("apexRule").Args
11715 optFlags := args["opt_flags"]
11716 if !strings.Contains(optFlags, "--min_sdk_version "+expectedMinSdkVersion) {
11717 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", module.Module(), expectedMinSdkVersion, optFlags)
11718 }
11719 }
11720
11721 checkHasDep := func(t *testing.T, ctx *android.TestContext, m android.Module, wantDep android.Module) {
11722 t.Helper()
11723 found := false
11724 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
11725 if dep == wantDep {
11726 found = true
11727 }
11728 })
11729 if !found {
11730 t.Errorf("Could not find a dependency from %v to %v\n", m, wantDep)
11731 }
11732 }
11733
11734 ctx := testApex(t, `
11735 apex {
11736 name: "com.android.apex30",
11737 min_sdk_version: "30",
11738 key: "apex30.key",
11739 java_libs: ["javalib"],
11740 }
11741
11742 java_library {
11743 name: "javalib",
11744 srcs: ["A.java"],
11745 apex_available: ["com.android.apex30"],
11746 min_sdk_version: "30",
11747 sdk_version: "current",
11748 }
11749
11750 override_apex {
11751 name: "com.mycompany.android.apex30",
11752 base: "com.android.apex30",
11753 }
11754
11755 override_apex {
11756 name: "com.mycompany.android.apex31",
11757 base: "com.android.apex30",
11758 min_sdk_version: "31",
11759 }
11760
11761 apex_key {
11762 name: "apex30.key",
11763 public_key: "testkey.avbpubkey",
11764 private_key: "testkey.pem",
11765 }
11766
11767 `, android.FixtureMergeMockFs(android.MockFS{
11768 "system/sepolicy/apex/com.android.apex30-file_contexts": nil,
11769 }),
11770 )
11771
11772 baseModule := ctx.ModuleForTests("com.android.apex30", "android_common_com.android.apex30")
11773 checkMinSdkVersion(t, baseModule, "30")
11774
11775 // Override module, but uses same min_sdk_version
11776 overridingModuleSameMinSdkVersion := ctx.ModuleForTests("com.android.apex30", "android_common_com.mycompany.android.apex30_com.mycompany.android.apex30")
11777 javalibApex30Variant := ctx.ModuleForTests("javalib", "android_common_apex30")
11778 checkMinSdkVersion(t, overridingModuleSameMinSdkVersion, "30")
11779 checkHasDep(t, ctx, overridingModuleSameMinSdkVersion.Module(), javalibApex30Variant.Module())
11780
11781 // Override module, uses different min_sdk_version
11782 overridingModuleDifferentMinSdkVersion := ctx.ModuleForTests("com.android.apex30", "android_common_com.mycompany.android.apex31_com.mycompany.android.apex31")
11783 javalibApex31Variant := ctx.ModuleForTests("javalib", "android_common_apex31")
11784 checkMinSdkVersion(t, overridingModuleDifferentMinSdkVersion, "31")
11785 checkHasDep(t, ctx, overridingModuleDifferentMinSdkVersion.Module(), javalibApex31Variant.Module())
11786}
Spandan Das0b28fa02024-05-28 23:40:17 +000011787
11788func TestOverrideApexWithPrebuiltApexPreferred(t *testing.T) {
11789 context := android.GroupFixturePreparers(
11790 android.PrepareForIntegrationTestWithAndroid,
11791 PrepareForTestWithApexBuildComponents,
11792 android.FixtureMergeMockFs(android.MockFS{
11793 "system/sepolicy/apex/foo-file_contexts": nil,
11794 }),
11795 )
11796 res := context.RunTestWithBp(t, `
11797 apex {
11798 name: "foo",
11799 key: "myapex.key",
11800 apex_available_name: "com.android.foo",
11801 variant_version: "0",
11802 updatable: false,
11803 }
11804 apex_key {
11805 name: "myapex.key",
11806 public_key: "testkey.avbpubkey",
11807 private_key: "testkey.pem",
11808 }
11809 prebuilt_apex {
11810 name: "foo",
11811 src: "foo.apex",
11812 prefer: true,
11813 }
11814 override_apex {
11815 name: "myoverrideapex",
11816 base: "foo",
11817 }
11818 `)
11819
11820 java.CheckModuleHasDependency(t, res.TestContext, "myoverrideapex", "android_common_myoverrideapex_myoverrideapex", "foo")
11821}
Spandan Dasca1d63e2024-07-01 22:53:49 +000011822
11823func TestUpdatableApexMinSdkVersionCurrent(t *testing.T) {
11824 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`, `
11825 apex {
11826 name: "myapex",
11827 key: "myapex.key",
11828 updatable: true,
11829 min_sdk_version: "current",
11830 }
11831
11832 apex_key {
11833 name: "myapex.key",
11834 public_key: "testkey.avbpubkey",
11835 private_key: "testkey.pem",
11836 }
11837 `)
11838}
Spandan Das2f68f192024-07-22 19:25:50 +000011839
11840func TestPrebuiltStubNoinstall(t *testing.T) {
11841 testFunc := func(t *testing.T, expectLibfooOnSystemLib bool, fs android.MockFS) {
11842 result := android.GroupFixturePreparers(
11843 prepareForApexTest,
11844 android.PrepareForTestWithAndroidMk,
11845 android.PrepareForTestWithMakevars,
11846 android.FixtureMergeMockFs(fs),
11847 ).RunTest(t)
11848
11849 ldRule := result.ModuleForTests("installedlib", "android_arm64_armv8-a_shared").Rule("ld")
11850 android.AssertStringDoesContain(t, "", ldRule.Args["libFlags"], "android_arm64_armv8-a_shared/libfoo.so")
11851
11852 installRules := result.InstallMakeRulesForTesting(t)
11853
11854 var installedlibRule *android.InstallMakeRule
11855 for i, rule := range installRules {
11856 if rule.Target == "out/target/product/test_device/system/lib/installedlib.so" {
11857 if installedlibRule != nil {
11858 t.Errorf("Duplicate install rules for %s", rule.Target)
11859 }
11860 installedlibRule = &installRules[i]
11861 }
11862 }
11863 if installedlibRule == nil {
11864 t.Errorf("No install rule found for installedlib")
11865 return
11866 }
11867
11868 if expectLibfooOnSystemLib {
11869 android.AssertStringListContains(t,
11870 "installedlib doesn't have install dependency on libfoo impl",
11871 installedlibRule.OrderOnlyDeps,
11872 "out/target/product/test_device/system/lib/libfoo.so")
11873 } else {
11874 android.AssertStringListDoesNotContain(t,
11875 "installedlib has install dependency on libfoo stub",
11876 installedlibRule.Deps,
11877 "out/target/product/test_device/system/lib/libfoo.so")
11878 android.AssertStringListDoesNotContain(t,
11879 "installedlib has order-only install dependency on libfoo stub",
11880 installedlibRule.OrderOnlyDeps,
11881 "out/target/product/test_device/system/lib/libfoo.so")
11882 }
11883 }
11884
11885 prebuiltLibfooBp := []byte(`
11886 cc_prebuilt_library {
11887 name: "libfoo",
11888 prefer: true,
11889 srcs: ["libfoo.so"],
11890 stubs: {
11891 versions: ["1"],
11892 },
11893 apex_available: ["apexfoo"],
11894 }
11895 `)
11896
11897 apexfooBp := []byte(`
11898 apex {
11899 name: "apexfoo",
11900 key: "apexfoo.key",
11901 native_shared_libs: ["libfoo"],
11902 updatable: false,
11903 compile_multilib: "both",
11904 }
11905 apex_key {
11906 name: "apexfoo.key",
11907 public_key: "testkey.avbpubkey",
11908 private_key: "testkey.pem",
11909 }
11910 `)
11911
11912 installedlibBp := []byte(`
11913 cc_library {
11914 name: "installedlib",
11915 shared_libs: ["libfoo"],
11916 }
11917 `)
11918
11919 t.Run("prebuilt stub (without source): no install", func(t *testing.T) {
11920 testFunc(
11921 t,
11922 /*expectLibfooOnSystemLib=*/ false,
11923 android.MockFS{
11924 "prebuilts/module_sdk/art/current/Android.bp": prebuiltLibfooBp,
11925 "apexfoo/Android.bp": apexfooBp,
11926 "system/sepolicy/apex/apexfoo-file_contexts": nil,
11927 "Android.bp": installedlibBp,
11928 },
11929 )
11930 })
11931
11932 disabledSourceLibfooBp := []byte(`
11933 cc_library {
11934 name: "libfoo",
11935 enabled: false,
11936 stubs: {
11937 versions: ["1"],
11938 },
11939 apex_available: ["apexfoo"],
11940 }
11941 `)
11942
11943 t.Run("prebuilt stub (with disabled source): no install", func(t *testing.T) {
11944 testFunc(
11945 t,
11946 /*expectLibfooOnSystemLib=*/ false,
11947 android.MockFS{
11948 "prebuilts/module_sdk/art/current/Android.bp": prebuiltLibfooBp,
11949 "impl/Android.bp": disabledSourceLibfooBp,
11950 "apexfoo/Android.bp": apexfooBp,
11951 "system/sepolicy/apex/apexfoo-file_contexts": nil,
11952 "Android.bp": installedlibBp,
11953 },
11954 )
11955 })
11956}