blob: 1e75948af876493bf530c1ac19d6f5986f8c5c63 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jooyung Han39edb6c2019-11-06 16:53:07 +090019 "path"
Paul Duffin37856732021-02-26 14:24:15 +000020 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010022 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090023 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090024 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090025 "strings"
26 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090027
Yu Liueae7b362023-11-16 17:05:47 -080028 "android/soong/aconfig/codegen"
Jooyung Han20348752023-12-05 15:23:56 +090029
Kiyoung Kim487689e2022-07-26 09:48:22 +090030 "github.com/google/blueprint"
Jiyong Parkda6eb592018-12-19 17:12:36 +090031 "github.com/google/blueprint/proptools"
32
33 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080034 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090035 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000036 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070037 prebuilt_etc "android/soong/etc"
Colin Crossbd3a16b2023-04-25 11:30:51 -070038 "android/soong/filesystem"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090039 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090040 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070041 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090042)
43
Jooyung Hand3639552019-08-09 12:57:43 +090044// names returns name list from white space separated string
45func names(s string) (ns []string) {
46 for _, n := range strings.Split(s, " ") {
47 if len(n) > 0 {
48 ns = append(ns, n)
49 }
50 }
51 return
52}
53
Paul Duffin40b62572021-03-20 11:39:01 +000054func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090055 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010056 android.GroupFixturePreparers(
57 prepareForApexTest,
58 android.GroupFixturePreparers(preparers...),
59 ).
Paul Duffine05480a2021-03-08 15:07:14 +000060 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000061 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090062}
63
Paul Duffin40b62572021-03-20 11:39:01 +000064func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090065 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010066
67 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000068 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010069 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000070 }
Paul Duffin284165a2021-03-29 01:50:31 +010071
72 result := android.GroupFixturePreparers(
73 prepareForApexTest,
74 android.GroupFixturePreparers(preparers...),
75 optionalBpPreparer,
76 ).RunTest(t)
77
Paul Duffine05480a2021-03-08 15:07:14 +000078 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090079}
80
Paul Duffin810f33d2021-03-09 14:12:32 +000081func withFiles(files android.MockFS) android.FixturePreparer {
82 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090083}
84
Paul Duffin810f33d2021-03-09 14:12:32 +000085func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
86 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090087 for k, v := range targets {
88 config.Targets[k] = v
89 }
Paul Duffin810f33d2021-03-09 14:12:32 +000090 })
Jooyung Han344d5432019-08-23 11:17:39 +090091}
92
Jooyung Han35155c42020-02-06 17:33:20 +090093// withNativeBridgeTargets sets configuration with targets including:
94// - X86_64 (primary)
95// - X86 (secondary)
96// - Arm64 on X86_64 (native bridge)
97// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000098var withNativeBridgeEnabled = android.FixtureModifyConfig(
99 func(config android.Config) {
100 config.Targets[android.Android] = []android.Target{
101 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
102 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
103 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
104 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
105 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
106 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
107 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
108 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
109 }
110 },
111)
112
113func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
114 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
115 variables.ManifestPackageNameOverrides = specs
116 })
Jooyung Han35155c42020-02-06 17:33:20 +0900117}
118
Albert Martineefabcf2022-03-21 20:11:16 +0000119func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
120 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
121 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
122 })
123}
124
Paul Duffin810f33d2021-03-09 14:12:32 +0000125var withBinder32bit = android.FixtureModifyProductVariables(
126 func(variables android.FixtureProductVariables) {
127 variables.Binder32bit = proptools.BoolPtr(true)
128 },
129)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900130
Paul Duffin810f33d2021-03-09 14:12:32 +0000131var withUnbundledBuild = android.FixtureModifyProductVariables(
132 func(variables android.FixtureProductVariables) {
133 variables.Unbundled_build = proptools.BoolPtr(true)
134 },
135)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900136
Paul Duffin284165a2021-03-29 01:50:31 +0100137// Legacy preparer used for running tests within the apex package.
138//
139// This includes everything that was needed to run any test in the apex package prior to the
140// introduction of the test fixtures. Tests that are being converted to use fixtures directly
141// rather than through the testApex...() methods should avoid using this and instead use the
142// various preparers directly, using android.GroupFixturePreparers(...) to group them when
143// necessary.
144//
145// deprecated
146var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000147 // General preparers in alphabetical order as test infrastructure will enforce correct
148 // registration order.
149 android.PrepareForTestWithAndroidBuildComponents,
150 bpf.PrepareForTestWithBpf,
151 cc.PrepareForTestWithCcBuildComponents,
Jiakai Zhangb95998b2023-05-11 16:39:27 +0100152 java.PrepareForTestWithDexpreopt,
Paul Duffin37aad602021-03-08 09:47:16 +0000153 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
154 rust.PrepareForTestWithRustDefaultModules,
155 sh.PrepareForTestWithShBuildComponents,
Yu Liueae7b362023-11-16 17:05:47 -0800156 codegen.PrepareForTestWithAconfigBuildComponents,
Paul Duffin37aad602021-03-08 09:47:16 +0000157
158 PrepareForTestWithApexBuildComponents,
159
160 // Additional apex test specific preparers.
161 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
162 filegroup {
163 name: "myapex-file_contexts",
164 srcs: [
165 "apex/myapex-file_contexts",
166 ],
167 }
168 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000169 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000170 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000171 "a.java": nil,
172 "PrebuiltAppFoo.apk": nil,
173 "PrebuiltAppFooPriv.apk": nil,
174 "apex_manifest.json": nil,
175 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000176 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
177 "system/sepolicy/apex/myapex2-file_contexts": nil,
178 "system/sepolicy/apex/otherapex-file_contexts": nil,
179 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
180 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700181 "mylib.cpp": nil,
182 "mytest.cpp": nil,
183 "mytest1.cpp": nil,
184 "mytest2.cpp": nil,
185 "mytest3.cpp": nil,
186 "myprebuilt": nil,
187 "my_include": nil,
188 "foo/bar/MyClass.java": nil,
189 "prebuilt.jar": nil,
190 "prebuilt.so": nil,
191 "vendor/foo/devkeys/test.x509.pem": nil,
192 "vendor/foo/devkeys/test.pk8": nil,
193 "testkey.x509.pem": nil,
194 "testkey.pk8": nil,
195 "testkey.override.x509.pem": nil,
196 "testkey.override.pk8": nil,
197 "vendor/foo/devkeys/testkey.avbpubkey": nil,
198 "vendor/foo/devkeys/testkey.pem": nil,
199 "NOTICE": nil,
200 "custom_notice": nil,
201 "custom_notice_for_static_lib": nil,
202 "testkey2.avbpubkey": nil,
203 "testkey2.pem": nil,
204 "myapex-arm64.apex": nil,
205 "myapex-arm.apex": nil,
206 "myapex.apks": nil,
207 "frameworks/base/api/current.txt": nil,
208 "framework/aidl/a.aidl": nil,
209 "dummy.txt": nil,
210 "baz": nil,
211 "bar/baz": nil,
212 "testdata/baz": nil,
213 "AppSet.apks": nil,
214 "foo.rs": nil,
215 "libfoo.jar": nil,
216 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000217 },
218 ),
219
220 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
Paul Duffin37aad602021-03-08 09:47:16 +0000221 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
222 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
223 variables.Platform_sdk_codename = proptools.StringPtr("Q")
224 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000225 // "Tiramisu" needs to be in the next line for compatibility with soong code,
226 // not because of these tests specifically (it's not used by the tests)
227 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000228 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000229 }),
230)
231
Paul Duffin52bfaa42021-03-23 23:40:12 +0000232var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
233 "system/sepolicy/apex/myapex-file_contexts": nil,
234})
235
Jooyung Han643adc42020-02-27 13:50:06 +0900236// ensure that 'result' equals 'expected'
237func ensureEquals(t *testing.T, result string, expected string) {
238 t.Helper()
239 if result != expected {
240 t.Errorf("%q != %q", expected, result)
241 }
242}
243
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244// ensure that 'result' contains 'expected'
245func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900246 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247 if !strings.Contains(result, expected) {
248 t.Errorf("%q is not found in %q", expected, result)
249 }
250}
251
Liz Kammer5bd365f2020-05-27 15:15:11 -0700252// ensure that 'result' contains 'expected' exactly one time
253func ensureContainsOnce(t *testing.T, result string, expected string) {
254 t.Helper()
255 count := strings.Count(result, expected)
256 if count != 1 {
257 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
258 }
259}
260
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261// ensures that 'result' does not contain 'notExpected'
262func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900263 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900264 if strings.Contains(result, notExpected) {
265 t.Errorf("%q is found in %q", notExpected, result)
266 }
267}
268
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700269func ensureMatches(t *testing.T, result string, expectedRex string) {
270 ok, err := regexp.MatchString(expectedRex, result)
271 if err != nil {
272 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
273 return
274 }
275 if !ok {
276 t.Errorf("%s does not match regular expession %s", result, expectedRex)
277 }
278}
279
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900281 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900282 if !android.InList(expected, result) {
283 t.Errorf("%q is not found in %v", expected, result)
284 }
285}
286
287func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900288 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900289 if android.InList(notExpected, result) {
290 t.Errorf("%q is found in %v", notExpected, result)
291 }
292}
293
Jooyung Hane1633032019-08-01 17:41:43 +0900294func ensureListEmpty(t *testing.T, result []string) {
295 t.Helper()
296 if len(result) > 0 {
297 t.Errorf("%q is expected to be empty", result)
298 }
299}
300
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000301func ensureListNotEmpty(t *testing.T, result []string) {
302 t.Helper()
303 if len(result) == 0 {
304 t.Errorf("%q is expected to be not empty", result)
305 }
306}
307
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308// Minimal test
309func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800310 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900311 apex_defaults {
312 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900313 manifest: ":myapex.manifest",
314 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900316 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900317 native_shared_libs: [
318 "mylib",
319 "libfoo.ffi",
320 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900321 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800322 multilib: {
323 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900324 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800325 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900326 },
Jiyong Park77acec62020-06-01 21:39:15 +0900327 java_libs: [
328 "myjar",
329 "myjar_dex",
330 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000331 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900332 }
333
Jiyong Park30ca9372019-02-07 16:27:23 +0900334 apex {
335 name: "myapex",
336 defaults: ["myapex-defaults"],
337 }
338
Jiyong Park25fc6a92018-11-18 18:02:45 +0900339 apex_key {
340 name: "myapex.key",
341 public_key: "testkey.avbpubkey",
342 private_key: "testkey.pem",
343 }
344
Jiyong Park809bb722019-02-13 21:33:49 +0900345 filegroup {
346 name: "myapex.manifest",
347 srcs: ["apex_manifest.json"],
348 }
349
350 filegroup {
351 name: "myapex.androidmanifest",
352 srcs: ["AndroidManifest.xml"],
353 }
354
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355 cc_library {
356 name: "mylib",
357 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900358 shared_libs: [
359 "mylib2",
360 "libbar.ffi",
361 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362 system_shared_libs: [],
363 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000364 // TODO: remove //apex_available:platform
365 apex_available: [
366 "//apex_available:platform",
367 "myapex",
368 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900369 }
370
Alex Light3d673592019-01-18 14:37:31 -0800371 cc_binary {
372 name: "foo",
373 srcs: ["mylib.cpp"],
374 compile_multilib: "both",
375 multilib: {
376 lib32: {
377 suffix: "32",
378 },
379 lib64: {
380 suffix: "64",
381 },
382 },
383 symlinks: ["foo_link_"],
384 symlink_preferred_arch: true,
385 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800386 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700387 apex_available: [ "myapex", "com.android.gki.*" ],
388 }
389
Jiyong Park99644e92020-11-17 22:21:02 +0900390 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000391 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900392 srcs: ["foo.rs"],
393 rlibs: ["libfoo.rlib.rust"],
Vinh Tran4eeb2a92023-08-14 13:29:30 -0400394 rustlibs: ["libfoo.dylib.rust"],
Jiyong Park99644e92020-11-17 22:21:02 +0900395 apex_available: ["myapex"],
396 }
397
398 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000399 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900400 srcs: ["foo.rs"],
401 crate_name: "foo",
402 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900403 shared_libs: ["libfoo.shared_from_rust"],
404 }
405
406 cc_library_shared {
407 name: "libfoo.shared_from_rust",
408 srcs: ["mylib.cpp"],
409 system_shared_libs: [],
410 stl: "none",
411 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900412 }
413
414 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000415 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900416 srcs: ["foo.rs"],
417 crate_name: "foo",
418 apex_available: ["myapex"],
419 }
420
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900421 rust_ffi_shared {
422 name: "libfoo.ffi",
423 srcs: ["foo.rs"],
424 crate_name: "foo",
425 apex_available: ["myapex"],
426 }
427
428 rust_ffi_shared {
429 name: "libbar.ffi",
430 srcs: ["foo.rs"],
431 crate_name: "bar",
432 apex_available: ["myapex"],
433 }
434
Yifan Hongd22a84a2020-07-28 17:37:46 -0700435 apex {
436 name: "com.android.gki.fake",
437 binaries: ["foo"],
438 key: "myapex.key",
439 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000440 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800441 }
442
Paul Duffindddd5462020-04-07 15:25:44 +0100443 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900444 name: "mylib2",
445 srcs: ["mylib.cpp"],
446 system_shared_libs: [],
447 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900448 static_libs: ["libstatic"],
449 // TODO: remove //apex_available:platform
450 apex_available: [
451 "//apex_available:platform",
452 "myapex",
453 ],
454 }
455
Paul Duffindddd5462020-04-07 15:25:44 +0100456 cc_prebuilt_library_shared {
457 name: "mylib2",
458 srcs: ["prebuilt.so"],
459 // TODO: remove //apex_available:platform
460 apex_available: [
461 "//apex_available:platform",
462 "myapex",
463 ],
464 }
465
Jiyong Park9918e1a2020-03-17 19:16:40 +0900466 cc_library_static {
467 name: "libstatic",
468 srcs: ["mylib.cpp"],
469 system_shared_libs: [],
470 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000471 // TODO: remove //apex_available:platform
472 apex_available: [
473 "//apex_available:platform",
474 "myapex",
475 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900476 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900477
478 java_library {
479 name: "myjar",
480 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900481 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 sdk_version: "none",
483 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900485 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000486 // TODO: remove //apex_available:platform
487 apex_available: [
488 "//apex_available:platform",
489 "myapex",
490 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900491 }
492
Jiyong Park77acec62020-06-01 21:39:15 +0900493 dex_import {
494 name: "myjar_dex",
495 jars: ["prebuilt.jar"],
496 apex_available: [
497 "//apex_available:platform",
498 "myapex",
499 ],
500 }
501
Jiyong Park7f7766d2019-07-25 22:02:35 +0900502 java_library {
503 name: "myotherjar",
504 srcs: ["foo/bar/MyClass.java"],
505 sdk_version: "none",
506 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900507 // TODO: remove //apex_available:platform
508 apex_available: [
509 "//apex_available:platform",
510 "myapex",
511 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900512 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900513
514 java_library {
515 name: "mysharedjar",
516 srcs: ["foo/bar/MyClass.java"],
517 sdk_version: "none",
518 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900519 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900520 `)
521
Jooyung Hana0503a52023-08-23 13:12:50 +0900522 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900523
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900524 // Make sure that Android.mk is created
Jooyung Hana0503a52023-08-23 13:12:50 +0900525 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700526 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900527 var builder strings.Builder
528 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
529
530 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000531 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900532 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
533
Jiyong Park42cca6c2019-04-01 11:15:50 +0900534 optFlags := apexRule.Args["opt_flags"]
535 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700536 // Ensure that the NOTICE output is being packaged as an asset.
Jooyung Hana0503a52023-08-23 13:12:50 +0900537 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900538
Jiyong Park25fc6a92018-11-18 18:02:45 +0900539 copyCmds := apexRule.Args["copy_commands"]
540
541 // Ensure that main rule creates an output
542 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
543
544 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700545 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
546 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
547 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900548 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900549 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900550
551 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700552 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
553 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900554 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900556 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900557 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900558
559 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800560 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
561 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900562 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900563 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900564 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
566 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900567 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900568 // .. but not for java libs
569 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900570 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800571
Colin Cross7113d202019-11-20 16:39:12 -0800572 // Ensure that the platform variant ends with _shared or _common
573 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
574 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900575 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
576 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900577 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
578
579 // Ensure that dynamic dependency to java libs are not included
580 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800581
582 // Ensure that all symlinks are present.
583 found_foo_link_64 := false
584 found_foo := false
585 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900586 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800587 if strings.HasSuffix(cmd, "bin/foo") {
588 found_foo = true
589 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
590 found_foo_link_64 = true
591 }
592 }
593 }
594 good := found_foo && found_foo_link_64
595 if !good {
596 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
597 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900598
Colin Crossf61d03d2023-11-02 16:56:39 -0700599 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
600 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100601 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100602 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
603 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
604 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100605
Colin Crossf61d03d2023-11-02 16:56:39 -0700606 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
607 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100608 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100609 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
610 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
611 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800612}
613
Jooyung Hanf21c7972019-12-16 22:32:06 +0900614func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800615 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900616 apex_defaults {
617 name: "myapex-defaults",
618 key: "myapex.key",
619 prebuilts: ["myetc"],
620 native_shared_libs: ["mylib"],
621 java_libs: ["myjar"],
622 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900623 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800624 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000625 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900626 }
627
628 prebuilt_etc {
629 name: "myetc",
630 src: "myprebuilt",
631 }
632
633 apex {
634 name: "myapex",
635 defaults: ["myapex-defaults"],
636 }
637
638 apex_key {
639 name: "myapex.key",
640 public_key: "testkey.avbpubkey",
641 private_key: "testkey.pem",
642 }
643
644 cc_library {
645 name: "mylib",
646 system_shared_libs: [],
647 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000648 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900649 }
650
651 java_library {
652 name: "myjar",
653 srcs: ["foo/bar/MyClass.java"],
654 sdk_version: "none",
655 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000656 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900657 }
658
659 android_app {
660 name: "AppFoo",
661 srcs: ["foo/bar/MyClass.java"],
662 sdk_version: "none",
663 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000664 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900665 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900666
667 runtime_resource_overlay {
668 name: "rro",
669 theme: "blue",
670 }
671
markchien2f59ec92020-09-02 16:23:38 +0800672 bpf {
673 name: "bpf",
674 srcs: ["bpf.c", "bpf2.c"],
675 }
676
Ken Chenfad7f9d2021-11-10 22:02:57 +0800677 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800678 name: "netdTest",
679 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800680 sub_dir: "netd",
681 }
682
Jooyung Hanf21c7972019-12-16 22:32:06 +0900683 `)
Jooyung Hana0503a52023-08-23 13:12:50 +0900684 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900685 "etc/myetc",
686 "javalib/myjar.jar",
687 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000688 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900689 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800690 "etc/bpf/bpf.o",
691 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800692 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900693 })
694}
695
Jooyung Han01a3ee22019-11-02 02:52:25 +0900696func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800697 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900698 apex {
699 name: "myapex",
700 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000701 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900702 }
703
704 apex_key {
705 name: "myapex.key",
706 public_key: "testkey.avbpubkey",
707 private_key: "testkey.pem",
708 }
709 `)
710
Jooyung Hana0503a52023-08-23 13:12:50 +0900711 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +0900712 args := module.Rule("apexRule").Args
713 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
714 t.Error("manifest should be apex_manifest.pb, but " + manifest)
715 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900716}
717
Liz Kammer4854a7d2021-05-27 14:28:27 -0400718func TestApexManifestMinSdkVersion(t *testing.T) {
719 ctx := testApex(t, `
720 apex_defaults {
721 name: "my_defaults",
722 key: "myapex.key",
723 product_specific: true,
724 file_contexts: ":my-file-contexts",
725 updatable: false,
726 }
727 apex {
728 name: "myapex_30",
729 min_sdk_version: "30",
730 defaults: ["my_defaults"],
731 }
732
733 apex {
734 name: "myapex_current",
735 min_sdk_version: "current",
736 defaults: ["my_defaults"],
737 }
738
739 apex {
740 name: "myapex_none",
741 defaults: ["my_defaults"],
742 }
743
744 apex_key {
745 name: "myapex.key",
746 public_key: "testkey.avbpubkey",
747 private_key: "testkey.pem",
748 }
749
750 filegroup {
751 name: "my-file-contexts",
752 srcs: ["product_specific_file_contexts"],
753 }
754 `, withFiles(map[string][]byte{
755 "product_specific_file_contexts": nil,
756 }), android.FixtureModifyProductVariables(
757 func(variables android.FixtureProductVariables) {
758 variables.Unbundled_build = proptools.BoolPtr(true)
759 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
760 }), android.FixtureMergeEnv(map[string]string{
761 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
762 }))
763
764 testCases := []struct {
765 module string
766 minSdkVersion string
767 }{
768 {
769 module: "myapex_30",
770 minSdkVersion: "30",
771 },
772 {
773 module: "myapex_current",
774 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
775 },
776 {
777 module: "myapex_none",
778 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
779 },
780 }
781 for _, tc := range testCases {
Jooyung Hana0503a52023-08-23 13:12:50 +0900782 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
Liz Kammer4854a7d2021-05-27 14:28:27 -0400783 args := module.Rule("apexRule").Args
784 optFlags := args["opt_flags"]
785 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
786 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
787 }
788 }
789}
790
Jihoon Kang842b9992024-02-08 01:41:51 +0000791func TestApexWithDessertSha(t *testing.T) {
792 ctx := testApex(t, `
793 apex_defaults {
794 name: "my_defaults",
795 key: "myapex.key",
796 product_specific: true,
797 file_contexts: ":my-file-contexts",
798 updatable: false,
799 }
800 apex {
801 name: "myapex_30",
802 min_sdk_version: "30",
803 defaults: ["my_defaults"],
804 }
805
806 apex {
807 name: "myapex_current",
808 min_sdk_version: "current",
809 defaults: ["my_defaults"],
810 }
811
812 apex {
813 name: "myapex_none",
814 defaults: ["my_defaults"],
815 }
816
817 apex_key {
818 name: "myapex.key",
819 public_key: "testkey.avbpubkey",
820 private_key: "testkey.pem",
821 }
822
823 filegroup {
824 name: "my-file-contexts",
825 srcs: ["product_specific_file_contexts"],
826 }
827 `, withFiles(map[string][]byte{
828 "product_specific_file_contexts": nil,
829 }), android.FixtureModifyProductVariables(
830 func(variables android.FixtureProductVariables) {
831 variables.Unbundled_build = proptools.BoolPtr(true)
832 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
833 }), android.FixtureMergeEnv(map[string]string{
834 "UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA": "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
835 }))
836
837 testCases := []struct {
838 module string
839 minSdkVersion string
840 }{
841 {
842 module: "myapex_30",
843 minSdkVersion: "30",
844 },
845 {
846 module: "myapex_current",
847 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
848 },
849 {
850 module: "myapex_none",
851 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
852 },
853 }
854 for _, tc := range testCases {
855 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
856 args := module.Rule("apexRule").Args
857 optFlags := args["opt_flags"]
858 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
859 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
860 }
861 }
862}
863
Jooyung Hanaf730952023-02-28 14:13:38 +0900864func TestFileContexts(t *testing.T) {
Jooyung Hanbe953902023-05-31 16:42:16 +0900865 for _, vendor := range []bool{true, false} {
Jooyung Hanaf730952023-02-28 14:13:38 +0900866 prop := ""
Jooyung Hanbe953902023-05-31 16:42:16 +0900867 if vendor {
868 prop = "vendor: true,\n"
Jooyung Hanaf730952023-02-28 14:13:38 +0900869 }
870 ctx := testApex(t, `
871 apex {
872 name: "myapex",
873 key: "myapex.key",
Jooyung Hanaf730952023-02-28 14:13:38 +0900874 updatable: false,
Jooyung Hanaf730952023-02-28 14:13:38 +0900875 `+prop+`
876 }
877
878 apex_key {
879 name: "myapex.key",
880 public_key: "testkey.avbpubkey",
881 private_key: "testkey.pem",
882 }
Jooyung Hanbe953902023-05-31 16:42:16 +0900883 `)
Jooyung Hanaf730952023-02-28 14:13:38 +0900884
Jooyung Hana0503a52023-08-23 13:12:50 +0900885 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("file_contexts")
Jooyung Hanbe953902023-05-31 16:42:16 +0900886 if vendor {
887 android.AssertStringDoesContain(t, "should force-label as vendor_apex_metadata_file",
888 rule.RuleParams.Command,
889 "apex_manifest\\\\.pb u:object_r:vendor_apex_metadata_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900890 } else {
Jooyung Hanbe953902023-05-31 16:42:16 +0900891 android.AssertStringDoesContain(t, "should force-label as system_file",
892 rule.RuleParams.Command,
893 "apex_manifest\\\\.pb u:object_r:system_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900894 }
895 }
896}
897
Jiyong Park25fc6a92018-11-18 18:02:45 +0900898func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800899 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900900 apex {
901 name: "myapex",
902 key: "myapex.key",
903 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900904 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000905 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900906 }
907
908 apex_key {
909 name: "myapex.key",
910 public_key: "testkey.avbpubkey",
911 private_key: "testkey.pem",
912 }
913
914 cc_library {
915 name: "mylib",
916 srcs: ["mylib.cpp"],
917 shared_libs: ["mylib2", "mylib3"],
918 system_shared_libs: [],
919 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000920 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900921 }
922
923 cc_library {
924 name: "mylib2",
925 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900926 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927 system_shared_libs: [],
928 stl: "none",
929 stubs: {
930 versions: ["1", "2", "3"],
931 },
932 }
933
934 cc_library {
935 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900936 srcs: ["mylib.cpp"],
937 shared_libs: ["mylib4"],
938 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900939 stl: "none",
940 stubs: {
941 versions: ["10", "11", "12"],
942 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000943 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900944 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900945
946 cc_library {
947 name: "mylib4",
948 srcs: ["mylib.cpp"],
949 system_shared_libs: [],
950 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000951 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900952 }
Jiyong Park105dc322021-06-11 17:22:09 +0900953
954 rust_binary {
955 name: "foo.rust",
956 srcs: ["foo.rs"],
957 shared_libs: ["libfoo.shared_from_rust"],
958 prefer_rlib: true,
959 apex_available: ["myapex"],
960 }
961
962 cc_library_shared {
963 name: "libfoo.shared_from_rust",
964 srcs: ["mylib.cpp"],
965 system_shared_libs: [],
966 stl: "none",
967 stubs: {
968 versions: ["10", "11", "12"],
969 },
970 }
971
Jiyong Park25fc6a92018-11-18 18:02:45 +0900972 `)
973
Jooyung Hana0503a52023-08-23 13:12:50 +0900974 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900975 copyCmds := apexRule.Args["copy_commands"]
976
977 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800978 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900979
980 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800981 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900982
983 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800984 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900985
Colin Crossaede88c2020-08-11 12:17:01 -0700986 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900987
988 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900989 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900990 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900991 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900992
993 // 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 -0700994 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900995 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700996 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900997
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700998 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
999 // is replaced by sharing of "cFlags" in cc/builder.go.
1000 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
1001 // module variable representing "cflags". So it was not detected by ensureNotContains.
1002 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
1003 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
1004 // including the original cflags's "-include mylib.h".
1005 //
Jiyong Park64379952018-12-13 18:37:29 +09001006 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -07001007 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1008 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +09001009
Jiyong Park85cc35a2022-07-17 11:30:47 +09001010 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1011 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
1012 // Ensure that genstub for apex-provided lib is invoked with --apex
1013 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +09001014
Jooyung Hana0503a52023-08-23 13:12:50 +09001015 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +09001016 "lib64/mylib.so",
1017 "lib64/mylib3.so",
1018 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +09001019 "bin/foo.rust",
1020 "lib64/libc++.so", // by the implicit dependency from foo.rust
1021 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +09001022 })
Jiyong Park105dc322021-06-11 17:22:09 +09001023
1024 // Ensure that stub dependency from a rust module is not included
1025 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1026 // The rust module is linked to the stub cc library
Colin Cross004bd3f2023-10-02 11:39:17 -07001027 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park105dc322021-06-11 17:22:09 +09001028 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1029 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +09001030
Jooyung Hana0503a52023-08-23 13:12:50 +09001031 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jiyong Park34d5c332022-02-24 18:02:44 +09001032 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001033}
1034
Jooyung Han20348752023-12-05 15:23:56 +09001035func TestApexShouldNotEmbedStubVariant(t *testing.T) {
1036 testApexError(t, `module "myapex" .*: native_shared_libs: "libbar" is a stub`, `
1037 apex {
1038 name: "myapex",
1039 key: "myapex.key",
1040 vendor: true,
1041 updatable: false,
1042 native_shared_libs: ["libbar"], // should not add an LLNDK stub in a vendor apex
1043 }
1044
1045 apex_key {
1046 name: "myapex.key",
1047 public_key: "testkey.avbpubkey",
1048 private_key: "testkey.pem",
1049 }
1050
1051 cc_library {
1052 name: "libbar",
1053 srcs: ["mylib.cpp"],
1054 llndk: {
1055 symbol_file: "libbar.map.txt",
1056 }
1057 }
1058 `)
1059}
1060
Jiyong Park1bc84122021-06-22 20:23:05 +09001061func TestApexCanUsePrivateApis(t *testing.T) {
1062 ctx := testApex(t, `
1063 apex {
1064 name: "myapex",
1065 key: "myapex.key",
1066 native_shared_libs: ["mylib"],
1067 binaries: ["foo.rust"],
1068 updatable: false,
1069 platform_apis: true,
1070 }
1071
1072 apex_key {
1073 name: "myapex.key",
1074 public_key: "testkey.avbpubkey",
1075 private_key: "testkey.pem",
1076 }
1077
1078 cc_library {
1079 name: "mylib",
1080 srcs: ["mylib.cpp"],
1081 shared_libs: ["mylib2"],
1082 system_shared_libs: [],
1083 stl: "none",
1084 apex_available: [ "myapex" ],
1085 }
1086
1087 cc_library {
1088 name: "mylib2",
1089 srcs: ["mylib.cpp"],
1090 cflags: ["-include mylib.h"],
1091 system_shared_libs: [],
1092 stl: "none",
1093 stubs: {
1094 versions: ["1", "2", "3"],
1095 },
1096 }
1097
1098 rust_binary {
1099 name: "foo.rust",
1100 srcs: ["foo.rs"],
1101 shared_libs: ["libfoo.shared_from_rust"],
1102 prefer_rlib: true,
1103 apex_available: ["myapex"],
1104 }
1105
1106 cc_library_shared {
1107 name: "libfoo.shared_from_rust",
1108 srcs: ["mylib.cpp"],
1109 system_shared_libs: [],
1110 stl: "none",
1111 stubs: {
1112 versions: ["10", "11", "12"],
1113 },
1114 }
1115 `)
1116
Jooyung Hana0503a52023-08-23 13:12:50 +09001117 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park1bc84122021-06-22 20:23:05 +09001118 copyCmds := apexRule.Args["copy_commands"]
1119
1120 // Ensure that indirect stubs dep is not included
1121 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1122 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1123
1124 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1125 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001126 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001127 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1128 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Colin Cross004bd3f2023-10-02 11:39:17 -07001129 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001130 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1131 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1132}
1133
Colin Cross7812fd32020-09-25 12:35:10 -07001134func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1135 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001136 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001137 apex {
1138 name: "myapex",
1139 key: "myapex.key",
1140 native_shared_libs: ["mylib", "mylib3"],
1141 min_sdk_version: "29",
1142 }
1143
1144 apex_key {
1145 name: "myapex.key",
1146 public_key: "testkey.avbpubkey",
1147 private_key: "testkey.pem",
1148 }
1149
1150 cc_library {
1151 name: "mylib",
1152 srcs: ["mylib.cpp"],
1153 shared_libs: ["mylib2", "mylib3"],
1154 system_shared_libs: [],
1155 stl: "none",
1156 apex_available: [ "myapex" ],
1157 min_sdk_version: "28",
1158 }
1159
1160 cc_library {
1161 name: "mylib2",
1162 srcs: ["mylib.cpp"],
1163 cflags: ["-include mylib.h"],
1164 system_shared_libs: [],
1165 stl: "none",
1166 stubs: {
1167 versions: ["28", "29", "30", "current"],
1168 },
1169 min_sdk_version: "28",
1170 }
1171
1172 cc_library {
1173 name: "mylib3",
1174 srcs: ["mylib.cpp"],
1175 shared_libs: ["mylib4"],
1176 system_shared_libs: [],
1177 stl: "none",
1178 stubs: {
1179 versions: ["28", "29", "30", "current"],
1180 },
1181 apex_available: [ "myapex" ],
1182 min_sdk_version: "28",
1183 }
1184
1185 cc_library {
1186 name: "mylib4",
1187 srcs: ["mylib.cpp"],
1188 system_shared_libs: [],
1189 stl: "none",
1190 apex_available: [ "myapex" ],
1191 min_sdk_version: "28",
1192 }
1193 `)
1194
Jooyung Hana0503a52023-08-23 13:12:50 +09001195 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Colin Cross7812fd32020-09-25 12:35:10 -07001196 copyCmds := apexRule.Args["copy_commands"]
1197
1198 // Ensure that direct non-stubs dep is always included
1199 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1200
1201 // Ensure that indirect stubs dep is not included
1202 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1203
1204 // Ensure that direct stubs dep is included
1205 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1206
1207 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1208
Jiyong Park55549df2021-02-26 23:57:23 +09001209 // Ensure that mylib is linking with the latest version of stub for mylib2
1210 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001211 // ... and not linking to the non-stub (impl) variant of mylib2
1212 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1213
1214 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1215 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1216 // .. and not linking to the stubs variant of mylib3
1217 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1218
1219 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001220 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001221 ensureNotContains(t, mylib2Cflags, "-include ")
1222
Jiyong Park85cc35a2022-07-17 11:30:47 +09001223 // Ensure that genstub is invoked with --systemapi
1224 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001225
Jooyung Hana0503a52023-08-23 13:12:50 +09001226 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Colin Cross7812fd32020-09-25 12:35:10 -07001227 "lib64/mylib.so",
1228 "lib64/mylib3.so",
1229 "lib64/mylib4.so",
1230 })
1231}
1232
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001233func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1234 t.Parallel()
1235 // myapex (Z)
1236 // mylib -----------------.
1237 // |
1238 // otherapex (29) |
1239 // libstub's versions: 29 Z current
1240 // |
1241 // <platform> |
1242 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001243 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001244 apex {
1245 name: "myapex",
1246 key: "myapex.key",
1247 native_shared_libs: ["mylib"],
1248 min_sdk_version: "Z", // non-final
1249 }
1250
1251 cc_library {
1252 name: "mylib",
1253 srcs: ["mylib.cpp"],
1254 shared_libs: ["libstub"],
1255 apex_available: ["myapex"],
1256 min_sdk_version: "Z",
1257 }
1258
1259 apex_key {
1260 name: "myapex.key",
1261 public_key: "testkey.avbpubkey",
1262 private_key: "testkey.pem",
1263 }
1264
1265 apex {
1266 name: "otherapex",
1267 key: "myapex.key",
1268 native_shared_libs: ["libstub"],
1269 min_sdk_version: "29",
1270 }
1271
1272 cc_library {
1273 name: "libstub",
1274 srcs: ["mylib.cpp"],
1275 stubs: {
1276 versions: ["29", "Z", "current"],
1277 },
1278 apex_available: ["otherapex"],
1279 min_sdk_version: "29",
1280 }
1281
1282 // platform module depending on libstub from otherapex should use the latest stub("current")
1283 cc_library {
1284 name: "libplatform",
1285 srcs: ["mylib.cpp"],
1286 shared_libs: ["libstub"],
1287 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001288 `,
1289 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1290 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1291 variables.Platform_sdk_final = proptools.BoolPtr(false)
1292 variables.Platform_version_active_codenames = []string{"Z"}
1293 }),
1294 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001295
Jiyong Park55549df2021-02-26 23:57:23 +09001296 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001297 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001298 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001299 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001300 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001301
1302 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1303 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1304 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1305 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1306 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1307}
1308
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001309func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001310 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001311 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001312 name: "myapex2",
1313 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001314 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001315 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001316 }
1317
1318 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001319 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001320 public_key: "testkey.avbpubkey",
1321 private_key: "testkey.pem",
1322 }
1323
1324 cc_library {
1325 name: "mylib",
1326 srcs: ["mylib.cpp"],
1327 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001328 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001329 system_shared_libs: [],
1330 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001331 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001332 }
1333
1334 cc_library {
1335 name: "libfoo",
1336 srcs: ["mylib.cpp"],
1337 shared_libs: ["libbar"],
1338 system_shared_libs: [],
1339 stl: "none",
1340 stubs: {
1341 versions: ["10", "20", "30"],
1342 },
1343 }
1344
1345 cc_library {
1346 name: "libbar",
1347 srcs: ["mylib.cpp"],
1348 system_shared_libs: [],
1349 stl: "none",
1350 }
1351
Jiyong Park678c8812020-02-07 17:25:49 +09001352 cc_library_static {
1353 name: "libbaz",
1354 srcs: ["mylib.cpp"],
1355 system_shared_libs: [],
1356 stl: "none",
1357 apex_available: [ "myapex2" ],
1358 }
1359
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001360 `)
1361
Jooyung Hana0503a52023-08-23 13:12:50 +09001362 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001363 copyCmds := apexRule.Args["copy_commands"]
1364
1365 // Ensure that direct non-stubs dep is always included
1366 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1367
1368 // Ensure that indirect stubs dep is not included
1369 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1370
1371 // Ensure that dependency of stubs is not included
1372 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1373
Colin Crossaede88c2020-08-11 12:17:01 -07001374 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001375
1376 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001377 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001378 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001379 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001380
Jiyong Park3ff16992019-12-27 14:11:47 +09001381 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001382
1383 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1384 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001385
Colin Crossf61d03d2023-11-02 16:56:39 -07001386 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1387 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001388 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001389
Colin Crossf61d03d2023-11-02 16:56:39 -07001390 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1391 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001392 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001393}
1394
Jooyung Hand3639552019-08-09 12:57:43 +09001395func TestApexWithRuntimeLibsDependency(t *testing.T) {
1396 /*
1397 myapex
1398 |
1399 v (runtime_libs)
1400 mylib ------+------> libfoo [provides stub]
1401 |
1402 `------> libbar
1403 */
Colin Cross1c460562021-02-16 17:55:47 -08001404 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001405 apex {
1406 name: "myapex",
1407 key: "myapex.key",
1408 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001409 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001410 }
1411
1412 apex_key {
1413 name: "myapex.key",
1414 public_key: "testkey.avbpubkey",
1415 private_key: "testkey.pem",
1416 }
1417
1418 cc_library {
1419 name: "mylib",
1420 srcs: ["mylib.cpp"],
Liz Kammer5f108fa2023-05-11 14:33:17 -04001421 static_libs: ["libstatic"],
1422 shared_libs: ["libshared"],
Jooyung Hand3639552019-08-09 12:57:43 +09001423 runtime_libs: ["libfoo", "libbar"],
1424 system_shared_libs: [],
1425 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001426 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001427 }
1428
1429 cc_library {
1430 name: "libfoo",
1431 srcs: ["mylib.cpp"],
1432 system_shared_libs: [],
1433 stl: "none",
1434 stubs: {
1435 versions: ["10", "20", "30"],
1436 },
1437 }
1438
1439 cc_library {
1440 name: "libbar",
1441 srcs: ["mylib.cpp"],
1442 system_shared_libs: [],
1443 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001444 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001445 }
1446
Liz Kammer5f108fa2023-05-11 14:33:17 -04001447 cc_library {
1448 name: "libstatic",
1449 srcs: ["mylib.cpp"],
1450 system_shared_libs: [],
1451 stl: "none",
1452 apex_available: [ "myapex" ],
1453 runtime_libs: ["libstatic_to_runtime"],
1454 }
1455
1456 cc_library {
1457 name: "libshared",
1458 srcs: ["mylib.cpp"],
1459 system_shared_libs: [],
1460 stl: "none",
1461 apex_available: [ "myapex" ],
1462 runtime_libs: ["libshared_to_runtime"],
1463 }
1464
1465 cc_library {
1466 name: "libstatic_to_runtime",
1467 srcs: ["mylib.cpp"],
1468 system_shared_libs: [],
1469 stl: "none",
1470 apex_available: [ "myapex" ],
1471 }
1472
1473 cc_library {
1474 name: "libshared_to_runtime",
1475 srcs: ["mylib.cpp"],
1476 system_shared_libs: [],
1477 stl: "none",
1478 apex_available: [ "myapex" ],
1479 }
Jooyung Hand3639552019-08-09 12:57:43 +09001480 `)
1481
Jooyung Hana0503a52023-08-23 13:12:50 +09001482 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001483 copyCmds := apexRule.Args["copy_commands"]
1484
1485 // Ensure that direct non-stubs dep is always included
1486 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1487
1488 // Ensure that indirect stubs dep is not included
1489 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1490
1491 // Ensure that runtime_libs dep in included
1492 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
Liz Kammer5f108fa2023-05-11 14:33:17 -04001493 ensureContains(t, copyCmds, "image.apex/lib64/libshared.so")
1494 ensureContains(t, copyCmds, "image.apex/lib64/libshared_to_runtime.so")
1495
1496 ensureNotContains(t, copyCmds, "image.apex/lib64/libstatic_to_runtime.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001497
Jooyung Hana0503a52023-08-23 13:12:50 +09001498 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001499 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1500 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001501}
1502
Paul Duffina02cae32021-03-09 01:44:06 +00001503var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1504 cc.PrepareForTestWithCcBuildComponents,
1505 PrepareForTestWithApexBuildComponents,
1506 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001507 apex {
1508 name: "com.android.runtime",
1509 key: "com.android.runtime.key",
1510 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001511 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001512 }
1513
1514 apex_key {
1515 name: "com.android.runtime.key",
1516 public_key: "testkey.avbpubkey",
1517 private_key: "testkey.pem",
1518 }
Paul Duffina02cae32021-03-09 01:44:06 +00001519 `),
1520 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1521)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001522
Paul Duffina02cae32021-03-09 01:44:06 +00001523func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001524 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001525 cc_library {
1526 name: "libc",
1527 no_libcrt: true,
1528 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001529 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001530 stl: "none",
1531 system_shared_libs: [],
1532 stubs: { versions: ["1"] },
1533 apex_available: ["com.android.runtime"],
1534
1535 sanitize: {
1536 hwaddress: true,
1537 }
1538 }
1539
1540 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001541 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001542 no_libcrt: true,
1543 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001544 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001545 stl: "none",
1546 system_shared_libs: [],
1547 srcs: [""],
1548 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001549 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001550
1551 sanitize: {
1552 never: true,
1553 },
Spandan Das4de7b492023-05-05 21:13:01 +00001554 apex_available: [
1555 "//apex_available:anyapex",
1556 "//apex_available:platform",
1557 ],
Paul Duffina02cae32021-03-09 01:44:06 +00001558 } `)
1559 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001560
Jooyung Hana0503a52023-08-23 13:12:50 +09001561 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001562 "lib64/bionic/libc.so",
1563 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1564 })
1565
Colin Cross4c4c1be2022-02-10 11:41:18 -08001566 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001567
1568 installed := hwasan.Description("install libclang_rt.hwasan")
1569 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1570
1571 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1572 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1573 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1574}
1575
1576func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001577 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001578 prepareForTestOfRuntimeApexWithHwasan,
1579 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1580 variables.SanitizeDevice = []string{"hwaddress"}
1581 }),
1582 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001583 cc_library {
1584 name: "libc",
1585 no_libcrt: true,
1586 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001587 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001588 stl: "none",
1589 system_shared_libs: [],
1590 stubs: { versions: ["1"] },
1591 apex_available: ["com.android.runtime"],
1592 }
1593
1594 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001595 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001596 no_libcrt: true,
1597 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001598 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001599 stl: "none",
1600 system_shared_libs: [],
1601 srcs: [""],
1602 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001603 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001604
1605 sanitize: {
1606 never: true,
1607 },
Spandan Das4de7b492023-05-05 21:13:01 +00001608 apex_available: [
1609 "//apex_available:anyapex",
1610 "//apex_available:platform",
1611 ],
Jooyung Han8ce8db92020-05-15 19:05:05 +09001612 }
Paul Duffina02cae32021-03-09 01:44:06 +00001613 `)
1614 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001615
Jooyung Hana0503a52023-08-23 13:12:50 +09001616 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001617 "lib64/bionic/libc.so",
1618 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1619 })
1620
Colin Cross4c4c1be2022-02-10 11:41:18 -08001621 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001622
1623 installed := hwasan.Description("install libclang_rt.hwasan")
1624 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1625
1626 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1627 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1628 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1629}
1630
Jooyung Han61b66e92020-03-21 14:21:46 +00001631func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1632 testcases := []struct {
1633 name string
1634 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001635 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001636 shouldLink string
1637 shouldNotLink []string
1638 }{
1639 {
Jiyong Park55549df2021-02-26 23:57:23 +09001640 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001641 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001642 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001643 shouldLink: "current",
1644 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001645 },
1646 {
Jiyong Park55549df2021-02-26 23:57:23 +09001647 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001648 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001649 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001650 shouldLink: "current",
1651 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001652 },
1653 }
1654 for _, tc := range testcases {
1655 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001656 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001657 apex {
1658 name: "myapex",
1659 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001660 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001661 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001662 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001663 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001664
Jooyung Han61b66e92020-03-21 14:21:46 +00001665 apex_key {
1666 name: "myapex.key",
1667 public_key: "testkey.avbpubkey",
1668 private_key: "testkey.pem",
1669 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001670
Jooyung Han61b66e92020-03-21 14:21:46 +00001671 cc_library {
1672 name: "mylib",
1673 srcs: ["mylib.cpp"],
1674 vendor_available: true,
1675 shared_libs: ["libbar"],
1676 system_shared_libs: [],
1677 stl: "none",
1678 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001679 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001680 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001681
Jooyung Han61b66e92020-03-21 14:21:46 +00001682 cc_library {
1683 name: "libbar",
1684 srcs: ["mylib.cpp"],
1685 system_shared_libs: [],
1686 stl: "none",
1687 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001688 llndk: {
1689 symbol_file: "libbar.map.txt",
1690 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001691 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001692 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001693 withUnbundledBuild,
1694 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001695
Jooyung Han61b66e92020-03-21 14:21:46 +00001696 // Ensure that LLNDK dep is not included
Jooyung Hana0503a52023-08-23 13:12:50 +09001697 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00001698 "lib64/mylib.so",
1699 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001700
Jooyung Han61b66e92020-03-21 14:21:46 +00001701 // Ensure that LLNDK dep is required
Jooyung Hana0503a52023-08-23 13:12:50 +09001702 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han61b66e92020-03-21 14:21:46 +00001703 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1704 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001705
Steven Moreland2c4000c2021-04-27 02:08:49 +00001706 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1707 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001708 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001709 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001710 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001711
Steven Moreland2c4000c2021-04-27 02:08:49 +00001712 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001713 ver := tc.shouldLink
1714 if tc.shouldLink == "current" {
1715 ver = strconv.Itoa(android.FutureApiLevelInt)
1716 }
1717 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001718 })
1719 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001720}
1721
Jiyong Park25fc6a92018-11-18 18:02:45 +09001722func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001723 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001724 apex {
1725 name: "myapex",
1726 key: "myapex.key",
1727 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001728 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001729 }
1730
1731 apex_key {
1732 name: "myapex.key",
1733 public_key: "testkey.avbpubkey",
1734 private_key: "testkey.pem",
1735 }
1736
1737 cc_library {
1738 name: "mylib",
1739 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001740 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001741 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_shared {
1747 name: "mylib_shared",
1748 srcs: ["mylib.cpp"],
1749 shared_libs: ["libdl#27"],
1750 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001751 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001752 }
1753
1754 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001755 name: "libBootstrap",
1756 srcs: ["mylib.cpp"],
1757 stl: "none",
1758 bootstrap: true,
1759 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001760 `)
1761
Jooyung Hana0503a52023-08-23 13:12:50 +09001762 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001763 copyCmds := apexRule.Args["copy_commands"]
1764
1765 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001766 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001767 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1768 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001769
1770 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001771 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001772
Colin Crossaede88c2020-08-11 12:17:01 -07001773 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1774 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1775 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001776
1777 // For dependency to libc
1778 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001779 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001780 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001781 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001782 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001783 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1784 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001785
1786 // For dependency to libm
1787 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001788 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001789 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001790 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001791 // ... and is not compiling with the stub
1792 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1793 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1794
1795 // For dependency to libdl
1796 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001797 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001798 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001799 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1800 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001801 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001802 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001803 // ... Cflags from stub is correctly exported to mylib
1804 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1805 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001806
1807 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001808 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1809 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1810 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1811 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001812}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001813
Jooyung Han749dc692020-04-15 11:03:39 +09001814func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001815 // there are three links between liba --> libz.
1816 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001817 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001818 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001819 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001820 apex {
1821 name: "myapex",
1822 key: "myapex.key",
1823 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001824 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001825 }
1826
1827 apex {
1828 name: "otherapex",
1829 key: "myapex.key",
1830 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001831 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001832 }
1833
1834 apex_key {
1835 name: "myapex.key",
1836 public_key: "testkey.avbpubkey",
1837 private_key: "testkey.pem",
1838 }
1839
1840 cc_library {
1841 name: "libx",
1842 shared_libs: ["liba"],
1843 system_shared_libs: [],
1844 stl: "none",
1845 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001846 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001847 }
1848
1849 cc_library {
1850 name: "liby",
1851 shared_libs: ["liba"],
1852 system_shared_libs: [],
1853 stl: "none",
1854 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001855 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001856 }
1857
1858 cc_library {
1859 name: "liba",
1860 shared_libs: ["libz"],
1861 system_shared_libs: [],
1862 stl: "none",
1863 apex_available: [
1864 "//apex_available:anyapex",
1865 "//apex_available:platform",
1866 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001867 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001868 }
1869
1870 cc_library {
1871 name: "libz",
1872 system_shared_libs: [],
1873 stl: "none",
1874 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001875 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001876 },
1877 }
Jooyung Han749dc692020-04-15 11:03:39 +09001878 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001879
1880 expectLink := func(from, from_variant, to, to_variant string) {
1881 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1882 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1883 }
1884 expectNoLink := func(from, from_variant, to, to_variant string) {
1885 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1886 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1887 }
1888 // platform liba is linked to non-stub version
1889 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001890 // liba in myapex is linked to current
1891 expectLink("liba", "shared_apex29", "libz", "shared_current")
1892 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001893 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001894 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001895 // liba in otherapex is linked to current
1896 expectLink("liba", "shared_apex30", "libz", "shared_current")
1897 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001898 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1899 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001900}
1901
Jooyung Hanaed150d2020-04-02 01:41:41 +09001902func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001903 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001904 apex {
1905 name: "myapex",
1906 key: "myapex.key",
1907 native_shared_libs: ["libx"],
1908 min_sdk_version: "R",
1909 }
1910
1911 apex_key {
1912 name: "myapex.key",
1913 public_key: "testkey.avbpubkey",
1914 private_key: "testkey.pem",
1915 }
1916
1917 cc_library {
1918 name: "libx",
1919 shared_libs: ["libz"],
1920 system_shared_libs: [],
1921 stl: "none",
1922 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001923 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001924 }
1925
1926 cc_library {
1927 name: "libz",
1928 system_shared_libs: [],
1929 stl: "none",
1930 stubs: {
1931 versions: ["29", "R"],
1932 },
1933 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001934 `,
1935 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1936 variables.Platform_version_active_codenames = []string{"R"}
1937 }),
1938 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001939
1940 expectLink := func(from, from_variant, to, to_variant string) {
1941 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1942 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1943 }
1944 expectNoLink := func(from, from_variant, to, to_variant string) {
1945 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1946 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1947 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001948 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1949 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001950 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1951 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001952}
1953
Jooyung Han4c4da062021-06-23 10:23:16 +09001954func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1955 testApex(t, `
1956 apex {
1957 name: "myapex",
1958 key: "myapex.key",
1959 java_libs: ["libx"],
1960 min_sdk_version: "S",
1961 }
1962
1963 apex_key {
1964 name: "myapex.key",
1965 public_key: "testkey.avbpubkey",
1966 private_key: "testkey.pem",
1967 }
1968
1969 java_library {
1970 name: "libx",
1971 srcs: ["a.java"],
1972 apex_available: [ "myapex" ],
1973 sdk_version: "current",
1974 min_sdk_version: "S", // should be okay
1975 }
1976 `,
1977 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1978 variables.Platform_version_active_codenames = []string{"S"}
1979 variables.Platform_sdk_codename = proptools.StringPtr("S")
1980 }),
1981 )
1982}
1983
Jooyung Han749dc692020-04-15 11:03:39 +09001984func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001985 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001986 apex {
1987 name: "myapex",
1988 key: "myapex.key",
1989 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001990 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001991 }
1992
1993 apex_key {
1994 name: "myapex.key",
1995 public_key: "testkey.avbpubkey",
1996 private_key: "testkey.pem",
1997 }
1998
1999 cc_library {
2000 name: "libx",
2001 shared_libs: ["libz"],
2002 system_shared_libs: [],
2003 stl: "none",
2004 apex_available: [ "myapex" ],
2005 }
2006
2007 cc_library {
2008 name: "libz",
2009 system_shared_libs: [],
2010 stl: "none",
2011 stubs: {
2012 versions: ["1", "2"],
2013 },
2014 }
2015 `)
2016
2017 expectLink := func(from, from_variant, to, to_variant string) {
2018 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2019 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2020 }
2021 expectNoLink := func(from, from_variant, to, to_variant string) {
2022 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2023 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2024 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002025 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002026 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002027 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07002028 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09002029}
2030
Jooyung Handfc864c2023-03-20 18:19:07 +09002031func TestApexMinSdkVersion_InVendorApex(t *testing.T) {
Jiyong Park5df7bd32021-08-25 16:18:46 +09002032 ctx := testApex(t, `
2033 apex {
2034 name: "myapex",
2035 key: "myapex.key",
2036 native_shared_libs: ["mylib"],
Jooyung Handfc864c2023-03-20 18:19:07 +09002037 updatable: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002038 vendor: true,
2039 min_sdk_version: "29",
2040 }
2041
2042 apex_key {
2043 name: "myapex.key",
2044 public_key: "testkey.avbpubkey",
2045 private_key: "testkey.pem",
2046 }
2047
2048 cc_library {
2049 name: "mylib",
Jooyung Handfc864c2023-03-20 18:19:07 +09002050 srcs: ["mylib.cpp"],
Jiyong Park5df7bd32021-08-25 16:18:46 +09002051 vendor_available: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002052 min_sdk_version: "29",
Jooyung Handfc864c2023-03-20 18:19:07 +09002053 shared_libs: ["libbar"],
2054 }
2055
2056 cc_library {
2057 name: "libbar",
2058 stubs: { versions: ["29", "30"] },
2059 llndk: { symbol_file: "libbar.map.txt" },
Jiyong Park5df7bd32021-08-25 16:18:46 +09002060 }
2061 `)
2062
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002063 vendorVariant := "android_vendor_arm64_armv8-a"
Jiyong Park5df7bd32021-08-25 16:18:46 +09002064
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002065 mylib := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29")
Jooyung Handfc864c2023-03-20 18:19:07 +09002066
2067 // Ensure that mylib links with "current" LLNDK
2068 libFlags := names(mylib.Rule("ld").Args["libFlags"])
Jooyung Han5e8994e2024-03-12 14:12:12 +09002069 ensureListContains(t, libFlags, "out/soong/.intermediates/libbar/"+vendorVariant+"_shared/libbar.so")
Jooyung Handfc864c2023-03-20 18:19:07 +09002070
2071 // Ensure that mylib is targeting 29
2072 ccRule := ctx.ModuleForTests("mylib", vendorVariant+"_static_apex29").Output("obj/mylib.o")
2073 ensureContains(t, ccRule.Args["cFlags"], "-target aarch64-linux-android29")
2074
2075 // Ensure that the correct variant of crtbegin_so is used.
2076 crtBegin := mylib.Rule("ld").Args["crtBegin"]
2077 ensureContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
Jiyong Park5df7bd32021-08-25 16:18:46 +09002078
2079 // Ensure that the crtbegin_so used by the APEX is targeting 29
2080 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
2081 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
2082}
2083
Jooyung Han4495f842023-04-25 16:39:59 +09002084func TestTrackAllowedDeps(t *testing.T) {
2085 ctx := testApex(t, `
2086 apex {
2087 name: "myapex",
2088 key: "myapex.key",
2089 updatable: true,
2090 native_shared_libs: [
2091 "mylib",
2092 "yourlib",
2093 ],
2094 min_sdk_version: "29",
2095 }
2096
2097 apex {
2098 name: "myapex2",
2099 key: "myapex.key",
2100 updatable: false,
2101 native_shared_libs: ["yourlib"],
2102 }
2103
2104 apex_key {
2105 name: "myapex.key",
2106 public_key: "testkey.avbpubkey",
2107 private_key: "testkey.pem",
2108 }
2109
2110 cc_library {
2111 name: "mylib",
2112 srcs: ["mylib.cpp"],
2113 shared_libs: ["libbar"],
2114 min_sdk_version: "29",
2115 apex_available: ["myapex"],
2116 }
2117
2118 cc_library {
2119 name: "libbar",
2120 stubs: { versions: ["29", "30"] },
2121 }
2122
2123 cc_library {
2124 name: "yourlib",
2125 srcs: ["mylib.cpp"],
2126 min_sdk_version: "29",
2127 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2128 }
2129 `, withFiles(android.MockFS{
2130 "packages/modules/common/build/allowed_deps.txt": nil,
2131 }))
2132
2133 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2134 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2135 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002136 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002137 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002138 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002139
Jooyung Hana0503a52023-08-23 13:12:50 +09002140 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07002141 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2142 myapex.Output("depsinfo/flatlist.txt")), "\n")
Jooyung Han4495f842023-04-25 16:39:59 +09002143 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2144 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2145 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2146 flatlist, "mylib:(minSdkVersion:29)")
2147 android.AssertStringListContains(t, "track platform-available lib",
2148 flatlist, "yourlib(minSdkVersion:29)")
2149}
2150
2151func TestTrackAllowedDeps_SkipWithoutAllowedDepsTxt(t *testing.T) {
2152 ctx := testApex(t, `
2153 apex {
2154 name: "myapex",
2155 key: "myapex.key",
2156 updatable: true,
2157 min_sdk_version: "29",
2158 }
2159
2160 apex_key {
2161 name: "myapex.key",
2162 public_key: "testkey.avbpubkey",
2163 private_key: "testkey.pem",
2164 }
2165 `)
2166 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2167 if nil != depsinfo.MaybeRule("generateApexDepsInfoFilesRule").Output {
2168 t.Error("apex_depsinfo_singleton shouldn't run when allowed_deps.txt doesn't exist")
2169 }
2170}
2171
Jooyung Han03b51852020-02-26 22:45:42 +09002172func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002173 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002174 apex {
2175 name: "myapex",
2176 key: "myapex.key",
2177 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002178 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002179 }
2180
2181 apex_key {
2182 name: "myapex.key",
2183 public_key: "testkey.avbpubkey",
2184 private_key: "testkey.pem",
2185 }
2186
2187 cc_library {
2188 name: "libx",
2189 system_shared_libs: [],
2190 stl: "none",
2191 apex_available: [ "myapex" ],
2192 stubs: {
2193 versions: ["1", "2"],
2194 },
2195 }
2196
2197 cc_library {
2198 name: "libz",
2199 shared_libs: ["libx"],
2200 system_shared_libs: [],
2201 stl: "none",
2202 }
2203 `)
2204
2205 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002206 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002207 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2208 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2209 }
2210 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002211 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002212 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2213 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2214 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002215 expectLink("libz", "shared", "libx", "shared_current")
2216 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002217 expectNoLink("libz", "shared", "libz", "shared_1")
2218 expectNoLink("libz", "shared", "libz", "shared")
2219}
2220
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002221var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2222 func(variables android.FixtureProductVariables) {
2223 variables.SanitizeDevice = []string{"hwaddress"}
2224 },
2225)
2226
Jooyung Han75568392020-03-20 04:29:24 +09002227func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002228 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002229 apex {
2230 name: "myapex",
2231 key: "myapex.key",
2232 native_shared_libs: ["libx"],
2233 min_sdk_version: "29",
2234 }
2235
2236 apex_key {
2237 name: "myapex.key",
2238 public_key: "testkey.avbpubkey",
2239 private_key: "testkey.pem",
2240 }
2241
2242 cc_library {
2243 name: "libx",
2244 shared_libs: ["libbar"],
2245 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002246 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002247 }
2248
2249 cc_library {
2250 name: "libbar",
2251 stubs: {
2252 versions: ["29", "30"],
2253 },
2254 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002255 `,
2256 prepareForTestWithSantitizeHwaddress,
2257 )
Jooyung Han03b51852020-02-26 22:45:42 +09002258 expectLink := func(from, from_variant, to, to_variant string) {
2259 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2260 libFlags := ld.Args["libFlags"]
2261 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2262 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002263 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002264}
2265
Jooyung Han75568392020-03-20 04:29:24 +09002266func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002267 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002268 apex {
2269 name: "myapex",
2270 key: "myapex.key",
2271 native_shared_libs: ["libx"],
2272 min_sdk_version: "29",
2273 }
2274
2275 apex_key {
2276 name: "myapex.key",
2277 public_key: "testkey.avbpubkey",
2278 private_key: "testkey.pem",
2279 }
2280
2281 cc_library {
2282 name: "libx",
2283 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002284 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002285 }
Jooyung Han75568392020-03-20 04:29:24 +09002286 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002287
2288 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002289 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002290 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002291 // note that platform variant is not.
2292 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002293 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002294}
2295
Jooyung Han749dc692020-04-15 11:03:39 +09002296func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2297 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002298 apex {
2299 name: "myapex",
2300 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002301 native_shared_libs: ["mylib"],
2302 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002303 }
2304
2305 apex_key {
2306 name: "myapex.key",
2307 public_key: "testkey.avbpubkey",
2308 private_key: "testkey.pem",
2309 }
Jooyung Han749dc692020-04-15 11:03:39 +09002310
2311 cc_library {
2312 name: "mylib",
2313 srcs: ["mylib.cpp"],
2314 system_shared_libs: [],
2315 stl: "none",
2316 apex_available: [
2317 "myapex",
2318 ],
2319 min_sdk_version: "30",
2320 }
2321 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002322
2323 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2324 apex {
2325 name: "myapex",
2326 key: "myapex.key",
2327 native_shared_libs: ["libfoo.ffi"],
2328 min_sdk_version: "29",
2329 }
2330
2331 apex_key {
2332 name: "myapex.key",
2333 public_key: "testkey.avbpubkey",
2334 private_key: "testkey.pem",
2335 }
2336
2337 rust_ffi_shared {
2338 name: "libfoo.ffi",
2339 srcs: ["foo.rs"],
2340 crate_name: "foo",
2341 apex_available: [
2342 "myapex",
2343 ],
2344 min_sdk_version: "30",
2345 }
2346 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002347
2348 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2349 apex {
2350 name: "myapex",
2351 key: "myapex.key",
2352 java_libs: ["libfoo"],
2353 min_sdk_version: "29",
2354 }
2355
2356 apex_key {
2357 name: "myapex.key",
2358 public_key: "testkey.avbpubkey",
2359 private_key: "testkey.pem",
2360 }
2361
2362 java_import {
2363 name: "libfoo",
2364 jars: ["libfoo.jar"],
2365 apex_available: [
2366 "myapex",
2367 ],
2368 min_sdk_version: "30",
2369 }
2370 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002371
2372 // Skip check for modules compiling against core API surface
2373 testApex(t, `
2374 apex {
2375 name: "myapex",
2376 key: "myapex.key",
2377 java_libs: ["libfoo"],
2378 min_sdk_version: "29",
2379 }
2380
2381 apex_key {
2382 name: "myapex.key",
2383 public_key: "testkey.avbpubkey",
2384 private_key: "testkey.pem",
2385 }
2386
2387 java_library {
2388 name: "libfoo",
2389 srcs: ["Foo.java"],
2390 apex_available: [
2391 "myapex",
2392 ],
2393 // Compile against core API surface
2394 sdk_version: "core_current",
2395 min_sdk_version: "30",
2396 }
2397 `)
2398
Jooyung Han749dc692020-04-15 11:03:39 +09002399}
2400
2401func TestApexMinSdkVersion_Okay(t *testing.T) {
2402 testApex(t, `
2403 apex {
2404 name: "myapex",
2405 key: "myapex.key",
2406 native_shared_libs: ["libfoo"],
2407 java_libs: ["libbar"],
2408 min_sdk_version: "29",
2409 }
2410
2411 apex_key {
2412 name: "myapex.key",
2413 public_key: "testkey.avbpubkey",
2414 private_key: "testkey.pem",
2415 }
2416
2417 cc_library {
2418 name: "libfoo",
2419 srcs: ["mylib.cpp"],
2420 shared_libs: ["libfoo_dep"],
2421 apex_available: ["myapex"],
2422 min_sdk_version: "29",
2423 }
2424
2425 cc_library {
2426 name: "libfoo_dep",
2427 srcs: ["mylib.cpp"],
2428 apex_available: ["myapex"],
2429 min_sdk_version: "29",
2430 }
2431
2432 java_library {
2433 name: "libbar",
2434 sdk_version: "current",
2435 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002436 static_libs: [
2437 "libbar_dep",
2438 "libbar_import_dep",
2439 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002440 apex_available: ["myapex"],
2441 min_sdk_version: "29",
2442 }
2443
2444 java_library {
2445 name: "libbar_dep",
2446 sdk_version: "current",
2447 srcs: ["a.java"],
2448 apex_available: ["myapex"],
2449 min_sdk_version: "29",
2450 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002451
2452 java_import {
2453 name: "libbar_import_dep",
2454 jars: ["libbar.jar"],
2455 apex_available: ["myapex"],
2456 min_sdk_version: "29",
2457 }
Jooyung Han03b51852020-02-26 22:45:42 +09002458 `)
2459}
2460
Colin Cross8ca61c12022-10-06 21:00:14 -07002461func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2462 // Tests that an apex dependency with min_sdk_version higher than the
2463 // min_sdk_version of the apex is allowed as long as the dependency's
2464 // min_sdk_version is less than or equal to the api level that the
2465 // architecture was introduced in. In this case, arm64 didn't exist
2466 // until api level 21, so the arm64 code will never need to run on
2467 // an api level 20 device, even if other architectures of the apex
2468 // will.
2469 testApex(t, `
2470 apex {
2471 name: "myapex",
2472 key: "myapex.key",
2473 native_shared_libs: ["libfoo"],
2474 min_sdk_version: "20",
2475 }
2476
2477 apex_key {
2478 name: "myapex.key",
2479 public_key: "testkey.avbpubkey",
2480 private_key: "testkey.pem",
2481 }
2482
2483 cc_library {
2484 name: "libfoo",
2485 srcs: ["mylib.cpp"],
2486 apex_available: ["myapex"],
2487 min_sdk_version: "21",
2488 stl: "none",
2489 }
2490 `)
2491}
2492
Artur Satayev8cf899a2020-04-15 17:29:42 +01002493func TestJavaStableSdkVersion(t *testing.T) {
2494 testCases := []struct {
2495 name string
2496 expectedError string
2497 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002498 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002499 }{
2500 {
2501 name: "Non-updatable apex with non-stable dep",
2502 bp: `
2503 apex {
2504 name: "myapex",
2505 java_libs: ["myjar"],
2506 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002507 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002508 }
2509 apex_key {
2510 name: "myapex.key",
2511 public_key: "testkey.avbpubkey",
2512 private_key: "testkey.pem",
2513 }
2514 java_library {
2515 name: "myjar",
2516 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002517 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002518 apex_available: ["myapex"],
2519 }
2520 `,
2521 },
2522 {
2523 name: "Updatable apex with stable dep",
2524 bp: `
2525 apex {
2526 name: "myapex",
2527 java_libs: ["myjar"],
2528 key: "myapex.key",
2529 updatable: true,
2530 min_sdk_version: "29",
2531 }
2532 apex_key {
2533 name: "myapex.key",
2534 public_key: "testkey.avbpubkey",
2535 private_key: "testkey.pem",
2536 }
2537 java_library {
2538 name: "myjar",
2539 srcs: ["foo/bar/MyClass.java"],
2540 sdk_version: "current",
2541 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002542 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002543 }
2544 `,
2545 },
2546 {
2547 name: "Updatable apex with non-stable dep",
2548 expectedError: "cannot depend on \"myjar\"",
2549 bp: `
2550 apex {
2551 name: "myapex",
2552 java_libs: ["myjar"],
2553 key: "myapex.key",
2554 updatable: true,
2555 }
2556 apex_key {
2557 name: "myapex.key",
2558 public_key: "testkey.avbpubkey",
2559 private_key: "testkey.pem",
2560 }
2561 java_library {
2562 name: "myjar",
2563 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002564 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002565 apex_available: ["myapex"],
2566 }
2567 `,
2568 },
2569 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002570 name: "Updatable apex with non-stable legacy core platform dep",
2571 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2572 bp: `
2573 apex {
2574 name: "myapex",
2575 java_libs: ["myjar-uses-legacy"],
2576 key: "myapex.key",
2577 updatable: true,
2578 }
2579 apex_key {
2580 name: "myapex.key",
2581 public_key: "testkey.avbpubkey",
2582 private_key: "testkey.pem",
2583 }
2584 java_library {
2585 name: "myjar-uses-legacy",
2586 srcs: ["foo/bar/MyClass.java"],
2587 sdk_version: "core_platform",
2588 apex_available: ["myapex"],
2589 }
2590 `,
2591 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2592 },
2593 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002594 name: "Updatable apex with non-stable transitive dep",
2595 // This is not actually detecting that the transitive dependency is unstable, rather it is
2596 // detecting that the transitive dependency is building against a wider API surface than the
2597 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002598 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002599 bp: `
2600 apex {
2601 name: "myapex",
2602 java_libs: ["myjar"],
2603 key: "myapex.key",
2604 updatable: true,
2605 }
2606 apex_key {
2607 name: "myapex.key",
2608 public_key: "testkey.avbpubkey",
2609 private_key: "testkey.pem",
2610 }
2611 java_library {
2612 name: "myjar",
2613 srcs: ["foo/bar/MyClass.java"],
2614 sdk_version: "current",
2615 apex_available: ["myapex"],
2616 static_libs: ["transitive-jar"],
2617 }
2618 java_library {
2619 name: "transitive-jar",
2620 srcs: ["foo/bar/MyClass.java"],
2621 sdk_version: "core_platform",
2622 apex_available: ["myapex"],
2623 }
2624 `,
2625 },
2626 }
2627
2628 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002629 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2630 continue
2631 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002632 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002633 errorHandler := android.FixtureExpectsNoErrors
2634 if test.expectedError != "" {
2635 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002636 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002637 android.GroupFixturePreparers(
2638 java.PrepareForTestWithJavaDefaultModules,
2639 PrepareForTestWithApexBuildComponents,
2640 prepareForTestWithMyapex,
2641 android.OptionalFixturePreparer(test.preparer),
2642 ).
2643 ExtendWithErrorHandler(errorHandler).
2644 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002645 })
2646 }
2647}
2648
Jooyung Han749dc692020-04-15 11:03:39 +09002649func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2650 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2651 apex {
2652 name: "myapex",
2653 key: "myapex.key",
2654 native_shared_libs: ["mylib"],
2655 min_sdk_version: "29",
2656 }
2657
2658 apex_key {
2659 name: "myapex.key",
2660 public_key: "testkey.avbpubkey",
2661 private_key: "testkey.pem",
2662 }
2663
2664 cc_library {
2665 name: "mylib",
2666 srcs: ["mylib.cpp"],
2667 shared_libs: ["mylib2"],
2668 system_shared_libs: [],
2669 stl: "none",
2670 apex_available: [
2671 "myapex",
2672 ],
2673 min_sdk_version: "29",
2674 }
2675
2676 // indirect part of the apex
2677 cc_library {
2678 name: "mylib2",
2679 srcs: ["mylib.cpp"],
2680 system_shared_libs: [],
2681 stl: "none",
2682 apex_available: [
2683 "myapex",
2684 ],
2685 min_sdk_version: "30",
2686 }
2687 `)
2688}
2689
2690func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2691 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2692 apex {
2693 name: "myapex",
2694 key: "myapex.key",
2695 apps: ["AppFoo"],
2696 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002697 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002698 }
2699
2700 apex_key {
2701 name: "myapex.key",
2702 public_key: "testkey.avbpubkey",
2703 private_key: "testkey.pem",
2704 }
2705
2706 android_app {
2707 name: "AppFoo",
2708 srcs: ["foo/bar/MyClass.java"],
2709 sdk_version: "current",
2710 min_sdk_version: "29",
2711 system_modules: "none",
2712 stl: "none",
2713 static_libs: ["bar"],
2714 apex_available: [ "myapex" ],
2715 }
2716
2717 java_library {
2718 name: "bar",
2719 sdk_version: "current",
2720 srcs: ["a.java"],
2721 apex_available: [ "myapex" ],
2722 }
2723 `)
2724}
2725
2726func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002727 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002728 apex {
2729 name: "myapex",
2730 key: "myapex.key",
2731 native_shared_libs: ["mylib"],
2732 min_sdk_version: "29",
2733 }
2734
2735 apex_key {
2736 name: "myapex.key",
2737 public_key: "testkey.avbpubkey",
2738 private_key: "testkey.pem",
2739 }
2740
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002741 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002742 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2743 cc_library {
2744 name: "mylib",
2745 srcs: ["mylib.cpp"],
2746 shared_libs: ["mylib2"],
2747 system_shared_libs: [],
2748 stl: "none",
2749 apex_available: ["myapex", "otherapex"],
2750 min_sdk_version: "29",
2751 }
2752
2753 cc_library {
2754 name: "mylib2",
2755 srcs: ["mylib.cpp"],
2756 system_shared_libs: [],
2757 stl: "none",
2758 apex_available: ["otherapex"],
2759 stubs: { versions: ["29", "30"] },
2760 min_sdk_version: "30",
2761 }
2762
2763 apex {
2764 name: "otherapex",
2765 key: "myapex.key",
2766 native_shared_libs: ["mylib", "mylib2"],
2767 min_sdk_version: "30",
2768 }
2769 `)
2770 expectLink := func(from, from_variant, to, to_variant string) {
2771 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2772 libFlags := ld.Args["libFlags"]
2773 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2774 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002775 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002776 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002777}
2778
Jooyung Haned124c32021-01-26 11:43:46 +09002779func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002780 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2781 func(variables android.FixtureProductVariables) {
2782 variables.Platform_sdk_codename = proptools.StringPtr("S")
2783 variables.Platform_version_active_codenames = []string{"S"}
2784 },
2785 )
Jooyung Haned124c32021-01-26 11:43:46 +09002786 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2787 apex {
2788 name: "myapex",
2789 key: "myapex.key",
2790 native_shared_libs: ["libfoo"],
2791 min_sdk_version: "S",
2792 }
2793 apex_key {
2794 name: "myapex.key",
2795 public_key: "testkey.avbpubkey",
2796 private_key: "testkey.pem",
2797 }
2798 cc_library {
2799 name: "libfoo",
2800 shared_libs: ["libbar"],
2801 apex_available: ["myapex"],
2802 min_sdk_version: "29",
2803 }
2804 cc_library {
2805 name: "libbar",
2806 apex_available: ["myapex"],
2807 }
2808 `, withSAsActiveCodeNames)
2809}
2810
2811func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002812 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2813 variables.Platform_sdk_codename = proptools.StringPtr("S")
2814 variables.Platform_version_active_codenames = []string{"S", "T"}
2815 })
Colin Cross1c460562021-02-16 17:55:47 -08002816 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002817 apex {
2818 name: "myapex",
2819 key: "myapex.key",
2820 native_shared_libs: ["libfoo"],
2821 min_sdk_version: "S",
2822 }
2823 apex_key {
2824 name: "myapex.key",
2825 public_key: "testkey.avbpubkey",
2826 private_key: "testkey.pem",
2827 }
2828 cc_library {
2829 name: "libfoo",
2830 shared_libs: ["libbar"],
2831 apex_available: ["myapex"],
2832 min_sdk_version: "S",
2833 }
2834 cc_library {
2835 name: "libbar",
2836 stubs: {
2837 symbol_file: "libbar.map.txt",
2838 versions: ["30", "S", "T"],
2839 },
2840 }
2841 `, withSAsActiveCodeNames)
2842
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002843 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002844 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2845 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002846 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002847}
2848
Jiyong Park7c2ee712018-12-07 00:42:25 +09002849func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002850 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002851 apex {
2852 name: "myapex",
2853 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002854 native_shared_libs: ["mylib"],
Jooyung Han4ed512b2023-08-11 16:30:04 +09002855 binaries: ["mybin", "mybin.rust"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002856 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002857 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002858 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002859 }
2860
2861 apex_key {
2862 name: "myapex.key",
2863 public_key: "testkey.avbpubkey",
2864 private_key: "testkey.pem",
2865 }
2866
2867 prebuilt_etc {
2868 name: "myetc",
2869 src: "myprebuilt",
2870 sub_dir: "foo/bar",
2871 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002872
2873 cc_library {
2874 name: "mylib",
2875 srcs: ["mylib.cpp"],
2876 relative_install_path: "foo/bar",
2877 system_shared_libs: [],
2878 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002879 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002880 }
2881
2882 cc_binary {
2883 name: "mybin",
2884 srcs: ["mylib.cpp"],
2885 relative_install_path: "foo/bar",
2886 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002887 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002888 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002889 }
Jooyung Han4ed512b2023-08-11 16:30:04 +09002890
2891 rust_binary {
2892 name: "mybin.rust",
2893 srcs: ["foo.rs"],
2894 relative_install_path: "rust_subdir",
2895 apex_available: [ "myapex" ],
2896 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002897 `)
2898
Jooyung Hana0503a52023-08-23 13:12:50 +09002899 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002900 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002901
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002902 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002903 ensureContains(t, cmd, "/etc ")
2904 ensureContains(t, cmd, "/etc/foo ")
2905 ensureContains(t, cmd, "/etc/foo/bar ")
2906 ensureContains(t, cmd, "/lib64 ")
2907 ensureContains(t, cmd, "/lib64/foo ")
2908 ensureContains(t, cmd, "/lib64/foo/bar ")
2909 ensureContains(t, cmd, "/lib ")
2910 ensureContains(t, cmd, "/lib/foo ")
2911 ensureContains(t, cmd, "/lib/foo/bar ")
2912 ensureContains(t, cmd, "/bin ")
2913 ensureContains(t, cmd, "/bin/foo ")
2914 ensureContains(t, cmd, "/bin/foo/bar ")
Jooyung Han4ed512b2023-08-11 16:30:04 +09002915 ensureContains(t, cmd, "/bin/rust_subdir ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002916}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002917
Jooyung Han35155c42020-02-06 17:33:20 +09002918func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002919 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002920 apex {
2921 name: "myapex",
2922 key: "myapex.key",
2923 multilib: {
2924 both: {
2925 native_shared_libs: ["mylib"],
2926 binaries: ["mybin"],
2927 },
2928 },
2929 compile_multilib: "both",
2930 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002931 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002932 }
2933
2934 apex_key {
2935 name: "myapex.key",
2936 public_key: "testkey.avbpubkey",
2937 private_key: "testkey.pem",
2938 }
2939
2940 cc_library {
2941 name: "mylib",
2942 relative_install_path: "foo/bar",
2943 system_shared_libs: [],
2944 stl: "none",
2945 apex_available: [ "myapex" ],
2946 native_bridge_supported: true,
2947 }
2948
2949 cc_binary {
2950 name: "mybin",
2951 relative_install_path: "foo/bar",
2952 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002953 stl: "none",
2954 apex_available: [ "myapex" ],
2955 native_bridge_supported: true,
2956 compile_multilib: "both", // default is "first" for binary
2957 multilib: {
2958 lib64: {
2959 suffix: "64",
2960 },
2961 },
2962 }
2963 `, withNativeBridgeEnabled)
Jooyung Hana0503a52023-08-23 13:12:50 +09002964 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han35155c42020-02-06 17:33:20 +09002965 "bin/foo/bar/mybin",
2966 "bin/foo/bar/mybin64",
2967 "bin/arm/foo/bar/mybin",
2968 "bin/arm64/foo/bar/mybin64",
2969 "lib/foo/bar/mylib.so",
2970 "lib/arm/foo/bar/mylib.so",
2971 "lib64/foo/bar/mylib.so",
2972 "lib64/arm64/foo/bar/mylib.so",
2973 })
2974}
2975
Jooyung Han85d61762020-06-24 23:50:26 +09002976func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002977 result := android.GroupFixturePreparers(
2978 prepareForApexTest,
2979 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2980 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002981 apex {
2982 name: "myapex",
2983 key: "myapex.key",
2984 binaries: ["mybin"],
2985 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002986 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002987 }
2988 apex_key {
2989 name: "myapex.key",
2990 public_key: "testkey.avbpubkey",
2991 private_key: "testkey.pem",
2992 }
2993 cc_binary {
2994 name: "mybin",
2995 vendor: true,
2996 shared_libs: ["libfoo"],
2997 }
2998 cc_library {
2999 name: "libfoo",
3000 proprietary: true,
3001 }
3002 `)
3003
Jooyung Hana0503a52023-08-23 13:12:50 +09003004 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09003005 "bin/mybin",
3006 "lib64/libfoo.so",
3007 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
3008 "lib64/libc++.so",
3009 })
3010
Jooyung Hana0503a52023-08-23 13:12:50 +09003011 apexBundle := result.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossc68db4b2021-11-11 18:59:15 -08003012 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09003013 name := apexBundle.BaseModuleName()
3014 prefix := "TARGET_"
3015 var builder strings.Builder
3016 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08003017 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01003018 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01003019 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003020
Jooyung Hana0503a52023-08-23 13:12:50 +09003021 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003022 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
3023 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09003024}
3025
Justin Yun13decfb2021-03-08 19:25:55 +09003026func TestProductVariant(t *testing.T) {
3027 ctx := testApex(t, `
3028 apex {
3029 name: "myapex",
3030 key: "myapex.key",
3031 updatable: false,
3032 product_specific: true,
3033 binaries: ["foo"],
3034 }
3035
3036 apex_key {
3037 name: "myapex.key",
3038 public_key: "testkey.avbpubkey",
3039 private_key: "testkey.pem",
3040 }
3041
3042 cc_binary {
3043 name: "foo",
3044 product_available: true,
3045 apex_available: ["myapex"],
3046 srcs: ["foo.cpp"],
3047 }
Justin Yunaf1fde42023-09-27 16:22:10 +09003048 `)
Justin Yun13decfb2021-03-08 19:25:55 +09003049
3050 cflags := strings.Fields(
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003051 ctx.ModuleForTests("foo", "android_product_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09003052 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
3053 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
3054 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
3055 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
3056}
3057
Jooyung Han8e5685d2020-09-21 11:02:57 +09003058func TestApex_withPrebuiltFirmware(t *testing.T) {
3059 testCases := []struct {
3060 name string
3061 additionalProp string
3062 }{
3063 {"system apex with prebuilt_firmware", ""},
3064 {"vendor apex with prebuilt_firmware", "vendor: true,"},
3065 }
3066 for _, tc := range testCases {
3067 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003068 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09003069 apex {
3070 name: "myapex",
3071 key: "myapex.key",
3072 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003073 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09003074 `+tc.additionalProp+`
3075 }
3076 apex_key {
3077 name: "myapex.key",
3078 public_key: "testkey.avbpubkey",
3079 private_key: "testkey.pem",
3080 }
3081 prebuilt_firmware {
3082 name: "myfirmware",
3083 src: "myfirmware.bin",
3084 filename_from_src: true,
3085 `+tc.additionalProp+`
3086 }
3087 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09003088 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han8e5685d2020-09-21 11:02:57 +09003089 "etc/firmware/myfirmware.bin",
3090 })
3091 })
3092 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003093}
3094
Jooyung Hanefb184e2020-06-25 17:14:25 +09003095func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003096 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003097 apex {
3098 name: "myapex",
3099 key: "myapex.key",
3100 vendor: true,
3101 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003102 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003103 }
3104
3105 apex_key {
3106 name: "myapex.key",
3107 public_key: "testkey.avbpubkey",
3108 private_key: "testkey.pem",
3109 }
3110
3111 cc_library {
3112 name: "mylib",
3113 vendor_available: true,
3114 }
3115 `)
3116
Jooyung Hana0503a52023-08-23 13:12:50 +09003117 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003118 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003119 name := apexBundle.BaseModuleName()
3120 prefix := "TARGET_"
3121 var builder strings.Builder
3122 data.Custom(&builder, name, prefix, "", data)
3123 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09003124 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 +09003125}
3126
Jooyung Han2ed99d02020-06-24 23:26:26 +09003127func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003128 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003129 apex {
3130 name: "myapex",
3131 key: "myapex.key",
3132 vintf_fragments: ["fragment.xml"],
3133 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003134 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003135 }
3136 apex_key {
3137 name: "myapex.key",
3138 public_key: "testkey.avbpubkey",
3139 private_key: "testkey.pem",
3140 }
3141 cc_binary {
3142 name: "mybin",
3143 }
3144 `)
3145
Jooyung Hana0503a52023-08-23 13:12:50 +09003146 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003147 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003148 name := apexBundle.BaseModuleName()
3149 prefix := "TARGET_"
3150 var builder strings.Builder
3151 data.Custom(&builder, name, prefix, "", data)
3152 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003153 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003154 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003155}
3156
Jiyong Park16e91a02018-12-20 18:18:08 +09003157func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003158 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003159 apex {
3160 name: "myapex",
3161 key: "myapex.key",
3162 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003163 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003164 }
3165
3166 apex_key {
3167 name: "myapex.key",
3168 public_key: "testkey.avbpubkey",
3169 private_key: "testkey.pem",
3170 }
3171
3172 cc_library {
3173 name: "mylib",
3174 srcs: ["mylib.cpp"],
3175 system_shared_libs: [],
3176 stl: "none",
3177 stubs: {
3178 versions: ["1", "2", "3"],
3179 },
Spandan Das20fce2d2023-04-12 17:21:39 +00003180 apex_available: ["myapex"],
Jiyong Park16e91a02018-12-20 18:18:08 +09003181 }
3182
3183 cc_binary {
3184 name: "not_in_apex",
3185 srcs: ["mylib.cpp"],
3186 static_libs: ["mylib"],
3187 static_executable: true,
3188 system_shared_libs: [],
3189 stl: "none",
3190 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003191 `)
3192
Colin Cross7113d202019-11-20 16:39:12 -08003193 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003194
3195 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003196 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003197}
Jiyong Park9335a262018-12-24 11:31:58 +09003198
3199func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003200 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003201 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003202 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003203 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003204 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003205 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003206 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003207 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003208 }
3209
3210 cc_library {
3211 name: "mylib",
3212 srcs: ["mylib.cpp"],
3213 system_shared_libs: [],
3214 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003215 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003216 }
3217
3218 apex_key {
3219 name: "myapex.key",
3220 public_key: "testkey.avbpubkey",
3221 private_key: "testkey.pem",
3222 }
3223
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003224 android_app_certificate {
3225 name: "myapex.certificate",
3226 certificate: "testkey",
3227 }
3228
3229 android_app_certificate {
3230 name: "myapex.certificate.override",
3231 certificate: "testkey.override",
3232 }
3233
Jiyong Park9335a262018-12-24 11:31:58 +09003234 `)
3235
3236 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003237 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003238
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003239 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3240 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003241 "vendor/foo/devkeys/testkey.avbpubkey")
3242 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003243 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3244 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003245 "vendor/foo/devkeys/testkey.pem")
3246 }
3247
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003248 // check the APK certs. It should be overridden to myapex.certificate.override
Jooyung Hana0503a52023-08-23 13:12:50 +09003249 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003250 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003251 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003252 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003253 }
3254}
Jiyong Park58e364a2019-01-19 19:24:06 +09003255
Jooyung Hanf121a652019-12-17 14:30:11 +09003256func TestCertificate(t *testing.T) {
3257 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003258 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003259 apex {
3260 name: "myapex",
3261 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003262 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003263 }
3264 apex_key {
3265 name: "myapex.key",
3266 public_key: "testkey.avbpubkey",
3267 private_key: "testkey.pem",
3268 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003269 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003270 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3271 if actual := rule.Args["certificates"]; actual != expected {
3272 t.Errorf("certificates should be %q, not %q", expected, actual)
3273 }
3274 })
3275 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003276 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003277 apex {
3278 name: "myapex_keytest",
3279 key: "myapex.key",
3280 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003281 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003282 }
3283 apex_key {
3284 name: "myapex.key",
3285 public_key: "testkey.avbpubkey",
3286 private_key: "testkey.pem",
3287 }
3288 android_app_certificate {
3289 name: "myapex.certificate.override",
3290 certificate: "testkey.override",
3291 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003292 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003293 expected := "testkey.override.x509.pem testkey.override.pk8"
3294 if actual := rule.Args["certificates"]; actual != expected {
3295 t.Errorf("certificates should be %q, not %q", expected, actual)
3296 }
3297 })
3298 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003299 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003300 apex {
3301 name: "myapex",
3302 key: "myapex.key",
3303 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003304 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003305 }
3306 apex_key {
3307 name: "myapex.key",
3308 public_key: "testkey.avbpubkey",
3309 private_key: "testkey.pem",
3310 }
3311 android_app_certificate {
3312 name: "myapex.certificate",
3313 certificate: "testkey",
3314 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003315 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003316 expected := "testkey.x509.pem testkey.pk8"
3317 if actual := rule.Args["certificates"]; actual != expected {
3318 t.Errorf("certificates should be %q, not %q", expected, actual)
3319 }
3320 })
3321 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003322 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003323 apex {
3324 name: "myapex_keytest",
3325 key: "myapex.key",
3326 file_contexts: ":myapex-file_contexts",
3327 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003328 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003329 }
3330 apex_key {
3331 name: "myapex.key",
3332 public_key: "testkey.avbpubkey",
3333 private_key: "testkey.pem",
3334 }
3335 android_app_certificate {
3336 name: "myapex.certificate.override",
3337 certificate: "testkey.override",
3338 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003339 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003340 expected := "testkey.override.x509.pem testkey.override.pk8"
3341 if actual := rule.Args["certificates"]; actual != expected {
3342 t.Errorf("certificates should be %q, not %q", expected, actual)
3343 }
3344 })
3345 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003346 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003347 apex {
3348 name: "myapex",
3349 key: "myapex.key",
3350 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003351 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003352 }
3353 apex_key {
3354 name: "myapex.key",
3355 public_key: "testkey.avbpubkey",
3356 private_key: "testkey.pem",
3357 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003358 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003359 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3360 if actual := rule.Args["certificates"]; actual != expected {
3361 t.Errorf("certificates should be %q, not %q", expected, actual)
3362 }
3363 })
3364 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003365 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003366 apex {
3367 name: "myapex_keytest",
3368 key: "myapex.key",
3369 file_contexts: ":myapex-file_contexts",
3370 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003371 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003372 }
3373 apex_key {
3374 name: "myapex.key",
3375 public_key: "testkey.avbpubkey",
3376 private_key: "testkey.pem",
3377 }
3378 android_app_certificate {
3379 name: "myapex.certificate.override",
3380 certificate: "testkey.override",
3381 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003382 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003383 expected := "testkey.override.x509.pem testkey.override.pk8"
3384 if actual := rule.Args["certificates"]; actual != expected {
3385 t.Errorf("certificates should be %q, not %q", expected, actual)
3386 }
3387 })
3388}
3389
Jiyong Park58e364a2019-01-19 19:24:06 +09003390func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003391 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003392 apex {
3393 name: "myapex",
3394 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003395 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003396 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003397 }
3398
3399 apex {
3400 name: "otherapex",
3401 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003402 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003403 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003404 }
3405
3406 apex_key {
3407 name: "myapex.key",
3408 public_key: "testkey.avbpubkey",
3409 private_key: "testkey.pem",
3410 }
3411
3412 cc_library {
3413 name: "mylib",
3414 srcs: ["mylib.cpp"],
3415 system_shared_libs: [],
3416 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003417 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003418 "myapex",
3419 "otherapex",
3420 ],
Jooyung Han24282772020-03-21 23:20:55 +09003421 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003422 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003423 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003424 cc_library {
3425 name: "mylib2",
3426 srcs: ["mylib.cpp"],
3427 system_shared_libs: [],
3428 stl: "none",
3429 apex_available: [
3430 "myapex",
3431 "otherapex",
3432 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003433 static_libs: ["mylib3"],
3434 recovery_available: true,
3435 min_sdk_version: "29",
3436 }
3437 cc_library {
3438 name: "mylib3",
3439 srcs: ["mylib.cpp"],
3440 system_shared_libs: [],
3441 stl: "none",
3442 apex_available: [
3443 "myapex",
3444 "otherapex",
3445 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003446 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003447 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003448 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003449 `)
3450
Jooyung Hanc87a0592020-03-02 17:44:33 +09003451 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003452 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003453 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003454
Vinh Tranf9754732023-01-19 22:41:46 -05003455 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003456 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003457 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003458
Vinh Tranf9754732023-01-19 22:41:46 -05003459 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003460 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003461 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003462
Colin Crossaede88c2020-08-11 12:17:01 -07003463 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3464 // each variant defines additional macros to distinguish which apex variant it is built for
3465
3466 // non-APEX variant does not have __ANDROID_APEX__ defined
3467 mylibCFlags = ctx.ModuleForTests("mylib3", "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("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3472 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003473
Jooyung Hanc87a0592020-03-02 17:44:33 +09003474 // non-APEX variant does not have __ANDROID_APEX__ defined
3475 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3476 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3477
Vinh Tranf9754732023-01-19 22:41:46 -05003478 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003479 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003480 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003481}
Jiyong Park7e636d02019-01-28 16:16:54 +09003482
3483func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003484 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003485 apex {
3486 name: "myapex",
3487 key: "myapex.key",
3488 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003489 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003490 }
3491
3492 apex_key {
3493 name: "myapex.key",
3494 public_key: "testkey.avbpubkey",
3495 private_key: "testkey.pem",
3496 }
3497
3498 cc_library_headers {
3499 name: "mylib_headers",
3500 export_include_dirs: ["my_include"],
3501 system_shared_libs: [],
3502 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003503 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003504 }
3505
3506 cc_library {
3507 name: "mylib",
3508 srcs: ["mylib.cpp"],
3509 system_shared_libs: [],
3510 stl: "none",
3511 header_libs: ["mylib_headers"],
3512 export_header_lib_headers: ["mylib_headers"],
3513 stubs: {
3514 versions: ["1", "2", "3"],
3515 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003516 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003517 }
3518
3519 cc_library {
3520 name: "otherlib",
3521 srcs: ["mylib.cpp"],
3522 system_shared_libs: [],
3523 stl: "none",
3524 shared_libs: ["mylib"],
3525 }
3526 `)
3527
Colin Cross7113d202019-11-20 16:39:12 -08003528 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003529
3530 // Ensure that the include path of the header lib is exported to 'otherlib'
3531 ensureContains(t, cFlags, "-Imy_include")
3532}
Alex Light9670d332019-01-29 18:07:33 -08003533
Jiyong Park7cd10e32020-01-14 09:22:18 +09003534type fileInApex struct {
3535 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003536 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003537 isLink bool
3538}
3539
Jooyung Han1724d582022-12-21 10:17:44 +09003540func (f fileInApex) String() string {
3541 return f.src + ":" + f.path
3542}
3543
3544func (f fileInApex) match(expectation string) bool {
3545 parts := strings.Split(expectation, ":")
3546 if len(parts) == 1 {
3547 match, _ := path.Match(parts[0], f.path)
3548 return match
3549 }
3550 if len(parts) == 2 {
3551 matchSrc, _ := path.Match(parts[0], f.src)
3552 matchDst, _ := path.Match(parts[1], f.path)
3553 return matchSrc && matchDst
3554 }
3555 panic("invalid expected file specification: " + expectation)
3556}
3557
Jooyung Hana57af4a2020-01-23 05:36:59 +00003558func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003559 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003560 module := ctx.ModuleForTests(moduleName, variant)
3561 apexRule := module.MaybeRule("apexRule")
3562 apexDir := "/image.apex/"
Jooyung Han31c470b2019-10-18 16:26:59 +09003563 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003564 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003565 for _, cmd := range strings.Split(copyCmds, "&&") {
3566 cmd = strings.TrimSpace(cmd)
3567 if cmd == "" {
3568 continue
3569 }
3570 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003571 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003572 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003573 switch terms[0] {
3574 case "mkdir":
3575 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003576 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003577 t.Fatal("copyCmds contains invalid cp command", cmd)
3578 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003579 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 = false
3582 case "ln":
3583 if len(terms) != 3 && len(terms) != 4 {
3584 // ln LINK TARGET or ln -s LINK TARGET
3585 t.Fatal("copyCmds contains invalid ln command", cmd)
3586 }
3587 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003588 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003589 isLink = true
3590 default:
3591 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3592 }
3593 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003594 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003595 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003596 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003597 }
Jooyung Han1724d582022-12-21 10:17:44 +09003598 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003599 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003600 }
3601 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003602 return ret
3603}
3604
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003605func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003606 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003607 var failed bool
3608 var surplus []string
3609 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003610 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003611 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003612 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003613 if file.match(expected) {
3614 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003615 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003616 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003617 }
3618 }
Jooyung Han1724d582022-12-21 10:17:44 +09003619 if !matchFound {
3620 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003621 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003622 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003623
Jooyung Han31c470b2019-10-18 16:26:59 +09003624 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003625 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003626 t.Log("surplus files", surplus)
3627 failed = true
3628 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003629
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003630 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003631 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003632 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003633 if !filesMatched[expected] {
3634 missing = append(missing, expected)
3635 }
3636 }
3637 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003638 t.Log("missing files", missing)
3639 failed = true
3640 }
3641 if failed {
3642 t.Fail()
3643 }
3644}
3645
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003646func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3647 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3648}
3649
3650func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
Spandan Das2069c3f2023-12-06 19:40:24 +00003651 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Description("deapex")
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003652 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3653 if deapexer.Output != nil {
3654 outputs = append(outputs, deapexer.Output.String())
3655 }
3656 for _, output := range deapexer.ImplicitOutputs {
3657 outputs = append(outputs, output.String())
3658 }
3659 actualFiles := make([]fileInApex, 0, len(outputs))
3660 for _, output := range outputs {
3661 dir := "/deapexer/"
3662 pos := strings.LastIndex(output, dir)
3663 if pos == -1 {
3664 t.Fatal("Unknown deapexer output ", output)
3665 }
3666 path := output[pos+len(dir):]
3667 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3668 }
3669 assertFileListEquals(t, files, actualFiles)
3670}
3671
Jooyung Han39edb6c2019-11-06 16:53:07 +09003672func vndkLibrariesTxtFiles(vers ...string) (result string) {
3673 for _, v := range vers {
3674 if v == "current" {
Justin Yund5784122023-10-25 13:25:32 +09003675 for _, txt := range []string{"vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003676 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003677 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003678 name: "` + txt + `.libraries.txt",
Justin Yund5784122023-10-25 13:25:32 +09003679 insert_vndk_version: true,
Jooyung Han39edb6c2019-11-06 16:53:07 +09003680 }
3681 `
3682 }
Justin Yund5784122023-10-25 13:25:32 +09003683 result += `
3684 llndk_libraries_txt {
3685 name: "llndk.libraries.txt",
3686 }
3687 llndk_libraries_txt_for_apex {
3688 name: "llndk.libraries.txt.apex",
3689 stem: "llndk.libraries.txt",
3690 insert_vndk_version: true,
3691 }
3692 `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003693 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003694 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003695 result += `
3696 prebuilt_etc {
3697 name: "` + txt + `.libraries.` + v + `.txt",
3698 src: "dummy.txt",
3699 }
3700 `
3701 }
3702 }
3703 }
3704 return
3705}
3706
Jooyung Han344d5432019-08-23 11:17:39 +09003707func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003708 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003709 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003710 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003711 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003712 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003713 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003714 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003715 }
3716
3717 apex_key {
3718 name: "myapex.key",
3719 public_key: "testkey.avbpubkey",
3720 private_key: "testkey.pem",
3721 }
3722
Jooyung Han31c470b2019-10-18 16:26:59 +09003723 vndk_prebuilt_shared {
3724 name: "libvndk27",
3725 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003726 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003727 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003728 vndk: {
3729 enabled: true,
3730 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003731 target_arch: "arm64",
3732 arch: {
3733 arm: {
3734 srcs: ["libvndk27_arm.so"],
3735 },
3736 arm64: {
3737 srcs: ["libvndk27_arm64.so"],
3738 },
3739 },
Colin Cross2807f002021-03-02 10:15:29 -08003740 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003741 }
3742
3743 vndk_prebuilt_shared {
3744 name: "libvndk27",
3745 version: "27",
3746 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003747 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003748 vndk: {
3749 enabled: true,
3750 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003751 target_arch: "x86_64",
3752 arch: {
3753 x86: {
3754 srcs: ["libvndk27_x86.so"],
3755 },
3756 x86_64: {
3757 srcs: ["libvndk27_x86_64.so"],
3758 },
3759 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003760 }
3761 `+vndkLibrariesTxtFiles("27"),
3762 withFiles(map[string][]byte{
3763 "libvndk27_arm.so": nil,
3764 "libvndk27_arm64.so": nil,
3765 "libvndk27_x86.so": nil,
3766 "libvndk27_x86_64.so": nil,
3767 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003768
Jooyung Hana0503a52023-08-23 13:12:50 +09003769 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003770 "lib/libvndk27_arm.so",
3771 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003772 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003773 })
Jooyung Han344d5432019-08-23 11:17:39 +09003774}
3775
Jooyung Han90eee022019-10-01 20:02:42 +09003776func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003777 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003778 apex_vndk {
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003779 name: "com.android.vndk.v29",
Jooyung Han90eee022019-10-01 20:02:42 +09003780 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003781 file_contexts: ":myapex-file_contexts",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003782 vndk_version: "29",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003783 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003784 }
3785 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003786 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003787 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003788 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003789 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003790 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003791 }
3792 apex_key {
3793 name: "myapex.key",
3794 public_key: "testkey.avbpubkey",
3795 private_key: "testkey.pem",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003796 }`+vndkLibrariesTxtFiles("28", "29"))
Jooyung Han90eee022019-10-01 20:02:42 +09003797
3798 assertApexName := func(expected, moduleName string) {
Jooyung Hana0503a52023-08-23 13:12:50 +09003799 module := ctx.ModuleForTests(moduleName, "android_common")
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003800 apexManifestRule := module.Rule("apexManifestRule")
3801 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003802 }
3803
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003804 assertApexName("com.android.vndk.v29", "com.android.vndk.v29")
Colin Cross2807f002021-03-02 10:15:29 -08003805 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003806}
3807
Jooyung Han344d5432019-08-23 11:17:39 +09003808func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003809 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003810 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003811 name: "com.android.vndk.current",
3812 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003813 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003814 native_bridge_supported: true,
3815 }
3816
3817 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003818 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003819 public_key: "testkey.avbpubkey",
3820 private_key: "testkey.pem",
3821 }
3822
3823 cc_library {
3824 name: "libvndk",
3825 srcs: ["mylib.cpp"],
3826 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003827 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003828 native_bridge_supported: true,
3829 host_supported: true,
3830 vndk: {
3831 enabled: true,
3832 },
3833 system_shared_libs: [],
3834 stl: "none",
3835 }
3836 `)
3837}
3838
Jooyung Han31c470b2019-10-18 16:26:59 +09003839func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003840 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003841 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003842 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003843 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003844 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003845 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003846 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003847 }
3848
3849 apex_key {
3850 name: "myapex.key",
3851 public_key: "testkey.avbpubkey",
3852 private_key: "testkey.pem",
3853 }
3854
3855 vndk_prebuilt_shared {
3856 name: "libvndk27",
3857 version: "27",
3858 target_arch: "arm",
3859 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003860 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003861 vndk: {
3862 enabled: true,
3863 },
3864 arch: {
3865 arm: {
3866 srcs: ["libvndk27.so"],
3867 }
3868 },
3869 }
3870
3871 vndk_prebuilt_shared {
3872 name: "libvndk27",
3873 version: "27",
3874 target_arch: "arm",
3875 binder32bit: true,
3876 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003877 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003878 vndk: {
3879 enabled: true,
3880 },
3881 arch: {
3882 arm: {
3883 srcs: ["libvndk27binder32.so"],
3884 }
3885 },
Colin Cross2807f002021-03-02 10:15:29 -08003886 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003887 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003888 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003889 withFiles(map[string][]byte{
3890 "libvndk27.so": nil,
3891 "libvndk27binder32.so": nil,
3892 }),
3893 withBinder32bit,
3894 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07003895 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09003896 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3897 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003898 },
3899 }),
3900 )
3901
Jooyung Hana0503a52023-08-23 13:12:50 +09003902 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003903 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003904 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003905 })
3906}
3907
Jooyung Hane1633032019-08-01 17:41:43 +09003908func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003909 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003910 apex {
3911 name: "myapex_nodep",
3912 key: "myapex.key",
3913 native_shared_libs: ["lib_nodep"],
3914 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003915 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003916 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003917 }
3918
3919 apex {
3920 name: "myapex_dep",
3921 key: "myapex.key",
3922 native_shared_libs: ["lib_dep"],
3923 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003924 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003925 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003926 }
3927
3928 apex {
3929 name: "myapex_provider",
3930 key: "myapex.key",
3931 native_shared_libs: ["libfoo"],
3932 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003933 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003934 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003935 }
3936
3937 apex {
3938 name: "myapex_selfcontained",
3939 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00003940 native_shared_libs: ["lib_dep_on_bar", "libbar"],
Jooyung Hane1633032019-08-01 17:41:43 +09003941 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003942 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003943 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003944 }
3945
3946 apex_key {
3947 name: "myapex.key",
3948 public_key: "testkey.avbpubkey",
3949 private_key: "testkey.pem",
3950 }
3951
3952 cc_library {
3953 name: "lib_nodep",
3954 srcs: ["mylib.cpp"],
3955 system_shared_libs: [],
3956 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003957 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003958 }
3959
3960 cc_library {
3961 name: "lib_dep",
3962 srcs: ["mylib.cpp"],
3963 shared_libs: ["libfoo"],
3964 system_shared_libs: [],
3965 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003966 apex_available: [
3967 "myapex_dep",
3968 "myapex_provider",
3969 "myapex_selfcontained",
3970 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003971 }
3972
3973 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00003974 name: "lib_dep_on_bar",
3975 srcs: ["mylib.cpp"],
3976 shared_libs: ["libbar"],
3977 system_shared_libs: [],
3978 stl: "none",
3979 apex_available: [
3980 "myapex_selfcontained",
3981 ],
3982 }
3983
3984
3985 cc_library {
Jooyung Hane1633032019-08-01 17:41:43 +09003986 name: "libfoo",
3987 srcs: ["mytest.cpp"],
3988 stubs: {
3989 versions: ["1"],
3990 },
3991 system_shared_libs: [],
3992 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003993 apex_available: [
3994 "myapex_provider",
Spandan Das20fce2d2023-04-12 17:21:39 +00003995 ],
3996 }
3997
3998 cc_library {
3999 name: "libbar",
4000 srcs: ["mytest.cpp"],
4001 stubs: {
4002 versions: ["1"],
4003 },
4004 system_shared_libs: [],
4005 stl: "none",
4006 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004007 "myapex_selfcontained",
4008 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004009 }
Spandan Das20fce2d2023-04-12 17:21:39 +00004010
Jooyung Hane1633032019-08-01 17:41:43 +09004011 `)
4012
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004013 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004014 var provideNativeLibs, requireNativeLibs []string
4015
Jooyung Hana0503a52023-08-23 13:12:50 +09004016 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004017 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4018 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004019 ensureListEmpty(t, provideNativeLibs)
4020 ensureListEmpty(t, requireNativeLibs)
4021
Jooyung Hana0503a52023-08-23 13:12:50 +09004022 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004023 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4024 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004025 ensureListEmpty(t, provideNativeLibs)
4026 ensureListContains(t, requireNativeLibs, "libfoo.so")
4027
Jooyung Hana0503a52023-08-23 13:12:50 +09004028 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004029 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4030 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004031 ensureListContains(t, provideNativeLibs, "libfoo.so")
4032 ensureListEmpty(t, requireNativeLibs)
4033
Jooyung Hana0503a52023-08-23 13:12:50 +09004034 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004035 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4036 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Spandan Das20fce2d2023-04-12 17:21:39 +00004037 ensureListContains(t, provideNativeLibs, "libbar.so")
Jooyung Hane1633032019-08-01 17:41:43 +09004038 ensureListEmpty(t, requireNativeLibs)
4039}
4040
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004041func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4042 ctx := testApex(t, `
4043 apex {
4044 name: "myapex",
4045 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004046 native_shared_libs: ["mylib"],
4047 updatable: false,
4048 }
4049
4050 apex_key {
4051 name: "myapex.key",
4052 public_key: "testkey.avbpubkey",
4053 private_key: "testkey.pem",
4054 }
4055
4056 cc_library {
4057 name: "mylib",
4058 srcs: ["mylib.cpp"],
4059 system_shared_libs: [],
4060 stl: "none",
4061 apex_available: [
4062 "//apex_available:platform",
4063 "myapex",
4064 ],
4065 }
4066 `, android.FixtureMergeEnv(map[string]string{
4067 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4068 }))
4069
Jooyung Hana0503a52023-08-23 13:12:50 +09004070 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004071 apexManifestRule := module.Rule("apexManifestRule")
4072 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4073}
4074
Vinh Tran8f5310f2022-10-07 18:16:47 -04004075func TestCompileMultilibProp(t *testing.T) {
4076 testCases := []struct {
4077 compileMultiLibProp string
4078 containedLibs []string
4079 notContainedLibs []string
4080 }{
4081 {
4082 containedLibs: []string{
4083 "image.apex/lib64/mylib.so",
4084 "image.apex/lib/mylib.so",
4085 },
4086 compileMultiLibProp: `compile_multilib: "both",`,
4087 },
4088 {
4089 containedLibs: []string{"image.apex/lib64/mylib.so"},
4090 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4091 compileMultiLibProp: `compile_multilib: "first",`,
4092 },
4093 {
4094 containedLibs: []string{"image.apex/lib64/mylib.so"},
4095 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4096 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4097 },
4098 {
4099 containedLibs: []string{"image.apex/lib64/mylib.so"},
4100 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4101 compileMultiLibProp: `compile_multilib: "64",`,
4102 },
4103 {
4104 containedLibs: []string{"image.apex/lib/mylib.so"},
4105 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4106 compileMultiLibProp: `compile_multilib: "32",`,
4107 },
4108 }
4109 for _, testCase := range testCases {
4110 ctx := testApex(t, fmt.Sprintf(`
4111 apex {
4112 name: "myapex",
4113 key: "myapex.key",
4114 %s
4115 native_shared_libs: ["mylib"],
4116 updatable: false,
4117 }
4118 apex_key {
4119 name: "myapex.key",
4120 public_key: "testkey.avbpubkey",
4121 private_key: "testkey.pem",
4122 }
4123 cc_library {
4124 name: "mylib",
4125 srcs: ["mylib.cpp"],
4126 apex_available: [
4127 "//apex_available:platform",
4128 "myapex",
4129 ],
4130 }
4131 `, testCase.compileMultiLibProp),
4132 )
Jooyung Hana0503a52023-08-23 13:12:50 +09004133 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Vinh Tran8f5310f2022-10-07 18:16:47 -04004134 apexRule := module.Rule("apexRule")
4135 copyCmds := apexRule.Args["copy_commands"]
4136 for _, containedLib := range testCase.containedLibs {
4137 ensureContains(t, copyCmds, containedLib)
4138 }
4139 for _, notContainedLib := range testCase.notContainedLibs {
4140 ensureNotContains(t, copyCmds, notContainedLib)
4141 }
4142 }
4143}
4144
Alex Light0851b882019-02-07 13:20:53 -08004145func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004146 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004147 apex {
4148 name: "myapex",
4149 key: "myapex.key",
4150 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004151 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004152 }
4153
4154 apex_key {
4155 name: "myapex.key",
4156 public_key: "testkey.avbpubkey",
4157 private_key: "testkey.pem",
4158 }
4159
4160 cc_library {
4161 name: "mylib_common",
4162 srcs: ["mylib.cpp"],
4163 system_shared_libs: [],
4164 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004165 apex_available: [
4166 "//apex_available:platform",
4167 "myapex",
4168 ],
Alex Light0851b882019-02-07 13:20:53 -08004169 }
4170 `)
4171
Jooyung Hana0503a52023-08-23 13:12:50 +09004172 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004173 apexRule := module.Rule("apexRule")
4174 copyCmds := apexRule.Args["copy_commands"]
4175
4176 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4177 t.Log("Apex was a test apex!")
4178 t.Fail()
4179 }
4180 // Ensure that main rule creates an output
4181 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4182
4183 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004184 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004185
4186 // Ensure that both direct and indirect deps are copied into apex
4187 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4188
Colin Cross7113d202019-11-20 16:39:12 -08004189 // Ensure that the platform variant ends with _shared
4190 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004191
Colin Cross56a83212020-09-15 18:30:11 -07004192 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004193 t.Log("Found mylib_common not in any apex!")
4194 t.Fail()
4195 }
4196}
4197
4198func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004199 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004200 apex_test {
4201 name: "myapex",
4202 key: "myapex.key",
4203 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004204 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004205 }
4206
4207 apex_key {
4208 name: "myapex.key",
4209 public_key: "testkey.avbpubkey",
4210 private_key: "testkey.pem",
4211 }
4212
4213 cc_library {
4214 name: "mylib_common_test",
4215 srcs: ["mylib.cpp"],
4216 system_shared_libs: [],
4217 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004218 // TODO: remove //apex_available:platform
4219 apex_available: [
4220 "//apex_available:platform",
4221 "myapex",
4222 ],
Alex Light0851b882019-02-07 13:20:53 -08004223 }
4224 `)
4225
Jooyung Hana0503a52023-08-23 13:12:50 +09004226 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004227 apexRule := module.Rule("apexRule")
4228 copyCmds := apexRule.Args["copy_commands"]
4229
4230 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4231 t.Log("Apex was not a test apex!")
4232 t.Fail()
4233 }
4234 // Ensure that main rule creates an output
4235 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4236
4237 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004238 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004239
4240 // Ensure that both direct and indirect deps are copied into apex
4241 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4242
Colin Cross7113d202019-11-20 16:39:12 -08004243 // Ensure that the platform variant ends with _shared
4244 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004245}
4246
Jooyung Han85707de2023-12-01 14:21:13 +09004247func TestLibzVendorIsntStable(t *testing.T) {
4248 ctx := testApex(t, `
4249 apex {
4250 name: "myapex",
4251 key: "myapex.key",
4252 updatable: false,
4253 binaries: ["mybin"],
4254 }
4255 apex {
4256 name: "myvendorapex",
4257 key: "myapex.key",
4258 file_contexts: "myvendorapex_file_contexts",
4259 vendor: true,
4260 updatable: false,
4261 binaries: ["mybin"],
4262 }
4263 apex_key {
4264 name: "myapex.key",
4265 public_key: "testkey.avbpubkey",
4266 private_key: "testkey.pem",
4267 }
4268 cc_binary {
4269 name: "mybin",
4270 vendor_available: true,
4271 system_shared_libs: [],
4272 stl: "none",
4273 shared_libs: ["libz"],
4274 apex_available: ["//apex_available:anyapex"],
4275 }
4276 cc_library {
4277 name: "libz",
4278 vendor_available: true,
4279 system_shared_libs: [],
4280 stl: "none",
4281 stubs: {
4282 versions: ["28", "30"],
4283 },
4284 target: {
4285 vendor: {
4286 no_stubs: true,
4287 },
4288 },
4289 }
4290 `, withFiles(map[string][]byte{
4291 "myvendorapex_file_contexts": nil,
4292 }))
4293
4294 // libz provides stubs for core variant.
4295 {
4296 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
4297 "bin/mybin",
4298 })
4299 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
4300 android.AssertStringEquals(t, "should require libz", apexManifestRule.Args["requireNativeLibs"], "libz.so")
4301 }
4302 // libz doesn't provide stubs for vendor variant.
4303 {
4304 ensureExactContents(t, ctx, "myvendorapex", "android_common_myvendorapex", []string{
4305 "bin/mybin",
4306 "lib64/libz.so",
4307 })
4308 apexManifestRule := ctx.ModuleForTests("myvendorapex", "android_common_myvendorapex").Rule("apexManifestRule")
4309 android.AssertStringEquals(t, "should not require libz", apexManifestRule.Args["requireNativeLibs"], "")
4310 }
4311}
4312
Alex Light9670d332019-01-29 18:07:33 -08004313func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004314 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004315 apex {
4316 name: "myapex",
4317 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004318 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004319 multilib: {
4320 first: {
4321 native_shared_libs: ["mylib_common"],
4322 }
4323 },
4324 target: {
4325 android: {
4326 multilib: {
4327 first: {
4328 native_shared_libs: ["mylib"],
4329 }
4330 }
4331 },
4332 host: {
4333 multilib: {
4334 first: {
4335 native_shared_libs: ["mylib2"],
4336 }
4337 }
4338 }
4339 }
4340 }
4341
4342 apex_key {
4343 name: "myapex.key",
4344 public_key: "testkey.avbpubkey",
4345 private_key: "testkey.pem",
4346 }
4347
4348 cc_library {
4349 name: "mylib",
4350 srcs: ["mylib.cpp"],
4351 system_shared_libs: [],
4352 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004353 // TODO: remove //apex_available:platform
4354 apex_available: [
4355 "//apex_available:platform",
4356 "myapex",
4357 ],
Alex Light9670d332019-01-29 18:07:33 -08004358 }
4359
4360 cc_library {
4361 name: "mylib_common",
4362 srcs: ["mylib.cpp"],
4363 system_shared_libs: [],
4364 stl: "none",
4365 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004366 // TODO: remove //apex_available:platform
4367 apex_available: [
4368 "//apex_available:platform",
4369 "myapex",
4370 ],
Alex Light9670d332019-01-29 18:07:33 -08004371 }
4372
4373 cc_library {
4374 name: "mylib2",
4375 srcs: ["mylib.cpp"],
4376 system_shared_libs: [],
4377 stl: "none",
4378 compile_multilib: "first",
4379 }
4380 `)
4381
Jooyung Hana0503a52023-08-23 13:12:50 +09004382 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004383 copyCmds := apexRule.Args["copy_commands"]
4384
4385 // Ensure that main rule creates an output
4386 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4387
4388 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004389 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4390 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4391 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004392
4393 // Ensure that both direct and indirect deps are copied into apex
4394 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4395 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4396 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4397
Colin Cross7113d202019-11-20 16:39:12 -08004398 // Ensure that the platform variant ends with _shared
4399 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4400 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4401 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004402}
Jiyong Park04480cf2019-02-06 00:16:29 +09004403
Jiyong Park59140302020-12-14 18:44:04 +09004404func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004405 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004406 apex {
4407 name: "myapex",
4408 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004409 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004410 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004411 arch: {
4412 arm64: {
4413 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004414 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004415 },
4416 x86_64: {
4417 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004418 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004419 },
4420 }
4421 }
4422
4423 apex_key {
4424 name: "myapex.key",
4425 public_key: "testkey.avbpubkey",
4426 private_key: "testkey.pem",
4427 }
4428
4429 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004430 name: "mylib.generic",
4431 srcs: ["mylib.cpp"],
4432 system_shared_libs: [],
4433 stl: "none",
4434 // TODO: remove //apex_available:platform
4435 apex_available: [
4436 "//apex_available:platform",
4437 "myapex",
4438 ],
4439 }
4440
4441 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004442 name: "mylib.arm64",
4443 srcs: ["mylib.cpp"],
4444 system_shared_libs: [],
4445 stl: "none",
4446 // TODO: remove //apex_available:platform
4447 apex_available: [
4448 "//apex_available:platform",
4449 "myapex",
4450 ],
4451 }
4452
4453 cc_library {
4454 name: "mylib.x64",
4455 srcs: ["mylib.cpp"],
4456 system_shared_libs: [],
4457 stl: "none",
4458 // TODO: remove //apex_available:platform
4459 apex_available: [
4460 "//apex_available:platform",
4461 "myapex",
4462 ],
4463 }
4464 `)
4465
Jooyung Hana0503a52023-08-23 13:12:50 +09004466 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park59140302020-12-14 18:44:04 +09004467 copyCmds := apexRule.Args["copy_commands"]
4468
4469 // Ensure that apex variant is created for the direct dep
4470 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004471 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004472 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4473
4474 // Ensure that both direct and indirect deps are copied into apex
4475 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4476 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4477}
4478
Jiyong Park04480cf2019-02-06 00:16:29 +09004479func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004480 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004481 apex {
4482 name: "myapex",
4483 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004484 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004485 updatable: false,
Riya Thakur654461c2024-02-27 07:21:05 +00004486 compile_multilib: "both",
Jiyong Park04480cf2019-02-06 00:16:29 +09004487 }
4488
4489 apex_key {
4490 name: "myapex.key",
4491 public_key: "testkey.avbpubkey",
4492 private_key: "testkey.pem",
4493 }
4494
4495 sh_binary {
4496 name: "myscript",
4497 src: "mylib.cpp",
4498 filename: "myscript.sh",
4499 sub_dir: "script",
4500 }
4501 `)
4502
Jooyung Hana0503a52023-08-23 13:12:50 +09004503 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004504 copyCmds := apexRule.Args["copy_commands"]
4505
4506 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4507}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004508
Jooyung Han91df2082019-11-20 01:49:42 +09004509func TestApexInVariousPartition(t *testing.T) {
4510 testcases := []struct {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004511 propName, partition string
Jooyung Han91df2082019-11-20 01:49:42 +09004512 }{
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004513 {"", "system"},
4514 {"product_specific: true", "product"},
4515 {"soc_specific: true", "vendor"},
4516 {"proprietary: true", "vendor"},
4517 {"vendor: true", "vendor"},
4518 {"system_ext_specific: true", "system_ext"},
Jooyung Han91df2082019-11-20 01:49:42 +09004519 }
4520 for _, tc := range testcases {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004521 t.Run(tc.propName+":"+tc.partition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004522 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004523 apex {
4524 name: "myapex",
4525 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004526 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004527 `+tc.propName+`
4528 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004529
Jooyung Han91df2082019-11-20 01:49:42 +09004530 apex_key {
4531 name: "myapex.key",
4532 public_key: "testkey.avbpubkey",
4533 private_key: "testkey.pem",
4534 }
4535 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004536
Jooyung Hana0503a52023-08-23 13:12:50 +09004537 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004538 expected := "out/soong/target/product/test_device/" + tc.partition + "/apex"
Paul Duffin37ba3442021-03-29 00:21:08 +01004539 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004540 if actual != expected {
4541 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4542 }
Jooyung Han91df2082019-11-20 01:49:42 +09004543 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004544 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004545}
Jiyong Park67882562019-03-21 01:11:21 +09004546
Jooyung Han580eb4f2020-06-24 19:33:06 +09004547func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004548 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004549 apex {
4550 name: "myapex",
4551 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004552 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004553 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004554
Jooyung Han580eb4f2020-06-24 19:33:06 +09004555 apex_key {
4556 name: "myapex.key",
4557 public_key: "testkey.avbpubkey",
4558 private_key: "testkey.pem",
4559 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004560 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09004561 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004562 rule := module.Output("file_contexts")
4563 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4564}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004565
Jooyung Han580eb4f2020-06-24 19:33:06 +09004566func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004567 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004568 apex {
4569 name: "myapex",
4570 key: "myapex.key",
4571 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004572 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004573 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004574
Jooyung Han580eb4f2020-06-24 19:33:06 +09004575 apex_key {
4576 name: "myapex.key",
4577 public_key: "testkey.avbpubkey",
4578 private_key: "testkey.pem",
4579 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004580 `, withFiles(map[string][]byte{
4581 "my_own_file_contexts": nil,
4582 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004583}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004584
Jooyung Han580eb4f2020-06-24 19:33:06 +09004585func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004586 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004587 apex {
4588 name: "myapex",
4589 key: "myapex.key",
4590 product_specific: true,
4591 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004592 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004593 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004594
Jooyung Han580eb4f2020-06-24 19:33:06 +09004595 apex_key {
4596 name: "myapex.key",
4597 public_key: "testkey.avbpubkey",
4598 private_key: "testkey.pem",
4599 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004600 `)
4601
Colin Cross1c460562021-02-16 17:55:47 -08004602 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004603 apex {
4604 name: "myapex",
4605 key: "myapex.key",
4606 product_specific: true,
4607 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004608 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004609 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004610
Jooyung Han580eb4f2020-06-24 19:33:06 +09004611 apex_key {
4612 name: "myapex.key",
4613 public_key: "testkey.avbpubkey",
4614 private_key: "testkey.pem",
4615 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004616 `, withFiles(map[string][]byte{
4617 "product_specific_file_contexts": nil,
4618 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004619 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004620 rule := module.Output("file_contexts")
4621 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4622}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004623
Jooyung Han580eb4f2020-06-24 19:33:06 +09004624func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004625 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004626 apex {
4627 name: "myapex",
4628 key: "myapex.key",
4629 product_specific: true,
4630 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004631 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004632 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004633
Jooyung Han580eb4f2020-06-24 19:33:06 +09004634 apex_key {
4635 name: "myapex.key",
4636 public_key: "testkey.avbpubkey",
4637 private_key: "testkey.pem",
4638 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004639
Jooyung Han580eb4f2020-06-24 19:33:06 +09004640 filegroup {
4641 name: "my-file-contexts",
4642 srcs: ["product_specific_file_contexts"],
4643 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004644 `, withFiles(map[string][]byte{
4645 "product_specific_file_contexts": nil,
4646 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004647 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004648 rule := module.Output("file_contexts")
4649 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004650}
4651
Jiyong Park67882562019-03-21 01:11:21 +09004652func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004653 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004654 apex_key {
4655 name: "myapex.key",
4656 public_key: ":my.avbpubkey",
4657 private_key: ":my.pem",
4658 product_specific: true,
4659 }
4660
4661 filegroup {
4662 name: "my.avbpubkey",
4663 srcs: ["testkey2.avbpubkey"],
4664 }
4665
4666 filegroup {
4667 name: "my.pem",
4668 srcs: ["testkey2.pem"],
4669 }
4670 `)
4671
4672 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4673 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004674 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004675 if actual_pubkey != expected_pubkey {
4676 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4677 }
4678 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004679 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004680 if actual_privkey != expected_privkey {
4681 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4682 }
4683}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004684
4685func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004686 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004687 prebuilt_apex {
4688 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004689 arch: {
4690 arm64: {
4691 src: "myapex-arm64.apex",
4692 },
4693 arm: {
4694 src: "myapex-arm.apex",
4695 },
4696 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004697 }
4698 `)
4699
Wei Li340ee8e2022-03-18 17:33:24 -07004700 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4701 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004702
Jiyong Parkc95714e2019-03-29 14:23:10 +09004703 expectedInput := "myapex-arm64.apex"
4704 if prebuilt.inputApex.String() != expectedInput {
4705 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4706 }
Wei Li340ee8e2022-03-18 17:33:24 -07004707 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4708 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4709 rule := testingModule.Rule("genProvenanceMetaData")
4710 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4711 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4712 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4713 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08004714
4715 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
4716 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004717}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004718
Paul Duffinc0609c62021-03-01 17:27:16 +00004719func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004720 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004721 prebuilt_apex {
4722 name: "myapex",
4723 }
4724 `)
4725}
4726
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004727func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004728 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004729 prebuilt_apex {
4730 name: "myapex",
4731 src: "myapex-arm.apex",
4732 filename: "notmyapex.apex",
4733 }
4734 `)
4735
Wei Li340ee8e2022-03-18 17:33:24 -07004736 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4737 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004738
4739 expected := "notmyapex.apex"
4740 if p.installFilename != expected {
4741 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4742 }
Wei Li340ee8e2022-03-18 17:33:24 -07004743 rule := testingModule.Rule("genProvenanceMetaData")
4744 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4745 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4746 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4747 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004748}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004749
Samiul Islam7c02e262021-09-08 17:48:28 +01004750func TestApexSetFilenameOverride(t *testing.T) {
4751 testApex(t, `
4752 apex_set {
4753 name: "com.company.android.myapex",
4754 apex_name: "com.android.myapex",
4755 set: "company-myapex.apks",
4756 filename: "com.company.android.myapex.apex"
4757 }
4758 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4759
4760 testApex(t, `
4761 apex_set {
4762 name: "com.company.android.myapex",
4763 apex_name: "com.android.myapex",
4764 set: "company-myapex.apks",
4765 filename: "com.company.android.myapex.capex"
4766 }
4767 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4768
4769 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4770 apex_set {
4771 name: "com.company.android.myapex",
4772 apex_name: "com.android.myapex",
4773 set: "company-myapex.apks",
4774 filename: "some-random-suffix"
4775 }
4776 `)
4777}
4778
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004779func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004780 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004781 prebuilt_apex {
4782 name: "myapex.prebuilt",
4783 src: "myapex-arm.apex",
4784 overrides: [
4785 "myapex",
4786 ],
4787 }
4788 `)
4789
Wei Li340ee8e2022-03-18 17:33:24 -07004790 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4791 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004792
4793 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004794 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004795 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004796 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004797 }
Wei Li340ee8e2022-03-18 17:33:24 -07004798 rule := testingModule.Rule("genProvenanceMetaData")
4799 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4800 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4801 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4802 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004803}
4804
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004805func TestPrebuiltApexName(t *testing.T) {
4806 testApex(t, `
4807 prebuilt_apex {
4808 name: "com.company.android.myapex",
4809 apex_name: "com.android.myapex",
4810 src: "company-myapex-arm.apex",
4811 }
4812 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4813
4814 testApex(t, `
4815 apex_set {
4816 name: "com.company.android.myapex",
4817 apex_name: "com.android.myapex",
4818 set: "company-myapex.apks",
4819 }
4820 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4821}
4822
4823func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4824 _ = android.GroupFixturePreparers(
4825 java.PrepareForTestWithJavaDefaultModules,
4826 PrepareForTestWithApexBuildComponents,
4827 android.FixtureWithRootAndroidBp(`
4828 platform_bootclasspath {
4829 name: "platform-bootclasspath",
4830 fragments: [
4831 {
4832 apex: "com.android.art",
4833 module: "art-bootclasspath-fragment",
4834 },
4835 ],
4836 }
4837
4838 prebuilt_apex {
4839 name: "com.company.android.art",
4840 apex_name: "com.android.art",
4841 src: "com.company.android.art-arm.apex",
4842 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4843 }
4844
4845 prebuilt_bootclasspath_fragment {
4846 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004847 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004848 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004849 hidden_api: {
4850 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4851 metadata: "my-bootclasspath-fragment/metadata.csv",
4852 index: "my-bootclasspath-fragment/index.csv",
4853 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4854 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4855 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004856 }
4857
4858 java_import {
4859 name: "core-oj",
4860 jars: ["prebuilt.jar"],
4861 }
4862 `),
4863 ).RunTest(t)
4864}
4865
Spandan Das59a4a2b2024-01-09 21:35:56 +00004866// A minimal context object for use with DexJarBuildPath
4867type moduleErrorfTestCtx struct {
4868}
4869
4870func (ctx moduleErrorfTestCtx) ModuleErrorf(format string, args ...interface{}) {
4871}
4872
Paul Duffin092153d2021-01-26 11:42:39 +00004873// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4874// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004875func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004876 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004877
Paul Duffin89886cb2021-02-05 16:44:03 +00004878 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004879 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004880 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004881 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +00004882 dexJarBuildPath := p.DexJarBuildPath(moduleErrorfTestCtx{}).PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004883 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004884 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
Spandan Das3576e762024-01-03 18:57:03 +00004885 ".intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
Jeongik Chad5fe8782021-07-08 01:13:11 +09004886 android.NormalizePathForTesting(dexJarBuildPath))
4887 }
4888
4889 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004890 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004891 // Make sure the import has been given the correct path to the dex jar.
4892 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4893 dexJarBuildPath := p.DexJarInstallPath()
4894 stem := android.RemoveOptionalPrebuiltPrefix(name)
4895 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4896 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4897 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004898 }
4899
Paul Duffin39853512021-02-26 11:09:39 +00004900 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004901 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004902 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004903 android.AssertArrayString(t, "Check if there is no source variant",
4904 []string{"android_common"},
4905 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004906 }
4907
4908 t.Run("prebuilt only", func(t *testing.T) {
4909 bp := `
4910 prebuilt_apex {
4911 name: "myapex",
4912 arch: {
4913 arm64: {
4914 src: "myapex-arm64.apex",
4915 },
4916 arm: {
4917 src: "myapex-arm.apex",
4918 },
4919 },
Paul Duffin39853512021-02-26 11:09:39 +00004920 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004921 }
4922
4923 java_import {
4924 name: "libfoo",
4925 jars: ["libfoo.jar"],
4926 }
Paul Duffin39853512021-02-26 11:09:39 +00004927
4928 java_sdk_library_import {
4929 name: "libbar",
4930 public: {
4931 jars: ["libbar.jar"],
4932 },
4933 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004934 `
4935
4936 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4937 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4938
Spandan Das3576e762024-01-03 18:57:03 +00004939 deapexerName := deapexerModuleName("prebuilt_myapex")
4940 android.AssertStringEquals(t, "APEX module name from deapexer name", "prebuilt_myapex", apexModuleName(deapexerName))
Martin Stjernholm44825602021-09-17 01:44:12 +01004941
Paul Duffinf6932af2021-02-26 18:21:56 +00004942 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004943 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004944 rule := deapexer.Rule("deapexer")
4945 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4946 t.Errorf("expected: %q, found: %q", expected, actual)
4947 }
4948
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004949 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004950 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004951 rule = prebuiltApex.Rule("android/soong/android.Cp")
4952 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4953 t.Errorf("expected: %q, found: %q", expected, actual)
4954 }
4955
Paul Duffin89886cb2021-02-05 16:44:03 +00004956 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004957 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004958
4959 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004960 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004961 })
4962
4963 t.Run("prebuilt with source preferred", func(t *testing.T) {
4964
4965 bp := `
4966 prebuilt_apex {
4967 name: "myapex",
4968 arch: {
4969 arm64: {
4970 src: "myapex-arm64.apex",
4971 },
4972 arm: {
4973 src: "myapex-arm.apex",
4974 },
4975 },
Paul Duffin39853512021-02-26 11:09:39 +00004976 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004977 }
4978
4979 java_import {
4980 name: "libfoo",
4981 jars: ["libfoo.jar"],
4982 }
4983
4984 java_library {
4985 name: "libfoo",
4986 }
Paul Duffin39853512021-02-26 11:09:39 +00004987
4988 java_sdk_library_import {
4989 name: "libbar",
4990 public: {
4991 jars: ["libbar.jar"],
4992 },
4993 }
4994
4995 java_sdk_library {
4996 name: "libbar",
4997 srcs: ["foo/bar/MyClass.java"],
4998 unsafe_ignore_missing_latest_api: true,
4999 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005000 `
5001
5002 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5003 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5004
Paul Duffin89886cb2021-02-05 16:44:03 +00005005 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005006 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005007 ensureNoSourceVariant(t, ctx, "libfoo")
5008
5009 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005010 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005011 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005012 })
5013
5014 t.Run("prebuilt preferred with source", func(t *testing.T) {
5015 bp := `
5016 prebuilt_apex {
5017 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005018 arch: {
5019 arm64: {
5020 src: "myapex-arm64.apex",
5021 },
5022 arm: {
5023 src: "myapex-arm.apex",
5024 },
5025 },
Paul Duffin39853512021-02-26 11:09:39 +00005026 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005027 }
5028
5029 java_import {
5030 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005031 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005032 jars: ["libfoo.jar"],
5033 }
5034
5035 java_library {
5036 name: "libfoo",
5037 }
Paul Duffin39853512021-02-26 11:09:39 +00005038
5039 java_sdk_library_import {
5040 name: "libbar",
5041 prefer: true,
5042 public: {
5043 jars: ["libbar.jar"],
5044 },
5045 }
5046
5047 java_sdk_library {
5048 name: "libbar",
5049 srcs: ["foo/bar/MyClass.java"],
5050 unsafe_ignore_missing_latest_api: true,
5051 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005052 `
5053
5054 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5055 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5056
Paul Duffin89886cb2021-02-05 16:44:03 +00005057 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005058 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005059 ensureNoSourceVariant(t, ctx, "libfoo")
5060
5061 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005062 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005063 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005064 })
5065}
5066
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005067func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005068 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005069 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005070 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5071 // is disabled.
5072 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
5073 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005074
Paul Duffin37856732021-02-26 14:24:15 +00005075 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5076 t.Helper()
Jiakai Zhangc6879f32023-11-06 16:31:19 +00005077 s := ctx.ModuleForTests("dex_bootjars", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005078 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005079 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005080 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005081 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005082 foundLibfooJar = true
5083 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005084 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005085 }
5086 }
5087 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005088 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 +00005089 }
5090 }
5091
Paul Duffin40a3f652021-07-19 13:11:24 +01005092 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005093 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005094 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005095 var rule android.TestingBuildParams
5096
5097 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5098 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005099 }
5100
Paul Duffin40a3f652021-07-19 13:11:24 +01005101 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5102 t.Helper()
5103 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5104 var rule android.TestingBuildParams
5105
5106 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5107 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5108 }
5109
Paul Duffin89f570a2021-06-16 01:42:33 +01005110 fragment := java.ApexVariantReference{
5111 Apex: proptools.StringPtr("myapex"),
5112 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5113 }
5114
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005115 t.Run("prebuilt only", func(t *testing.T) {
5116 bp := `
5117 prebuilt_apex {
5118 name: "myapex",
5119 arch: {
5120 arm64: {
5121 src: "myapex-arm64.apex",
5122 },
5123 arm: {
5124 src: "myapex-arm.apex",
5125 },
5126 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005127 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5128 }
5129
5130 prebuilt_bootclasspath_fragment {
5131 name: "my-bootclasspath-fragment",
5132 contents: ["libfoo", "libbar"],
5133 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005134 hidden_api: {
5135 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5136 metadata: "my-bootclasspath-fragment/metadata.csv",
5137 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005138 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5139 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5140 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005141 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005142 }
5143
5144 java_import {
5145 name: "libfoo",
5146 jars: ["libfoo.jar"],
5147 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005148 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005149 }
Paul Duffin37856732021-02-26 14:24:15 +00005150
5151 java_sdk_library_import {
5152 name: "libbar",
5153 public: {
5154 jars: ["libbar.jar"],
5155 },
5156 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005157 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005158 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005159 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005160 `
5161
Paul Duffin89f570a2021-06-16 01:42:33 +01005162 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005163 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5164 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005165
Paul Duffin537ea3d2021-05-14 10:38:00 +01005166 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005167 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005168 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005169 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005170 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005171 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 +01005172 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005173 })
5174
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005175 t.Run("apex_set only", func(t *testing.T) {
5176 bp := `
5177 apex_set {
5178 name: "myapex",
5179 set: "myapex.apks",
Liz Kammer2dc72442023-04-20 10:10:48 -04005180 exported_java_libs: ["myjavalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005181 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
Liz Kammer2dc72442023-04-20 10:10:48 -04005182 exported_systemserverclasspath_fragments: ["my-systemserverclasspath-fragment"],
5183 }
5184
5185 java_import {
5186 name: "myjavalib",
5187 jars: ["myjavalib.jar"],
5188 apex_available: ["myapex"],
5189 permitted_packages: ["javalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005190 }
5191
5192 prebuilt_bootclasspath_fragment {
5193 name: "my-bootclasspath-fragment",
5194 contents: ["libfoo", "libbar"],
5195 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005196 hidden_api: {
5197 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5198 metadata: "my-bootclasspath-fragment/metadata.csv",
5199 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005200 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5201 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5202 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005203 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005204 }
5205
Liz Kammer2dc72442023-04-20 10:10:48 -04005206 prebuilt_systemserverclasspath_fragment {
5207 name: "my-systemserverclasspath-fragment",
5208 contents: ["libbaz"],
5209 apex_available: ["myapex"],
5210 }
5211
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005212 java_import {
5213 name: "libfoo",
5214 jars: ["libfoo.jar"],
5215 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005216 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005217 }
5218
5219 java_sdk_library_import {
5220 name: "libbar",
5221 public: {
5222 jars: ["libbar.jar"],
5223 },
5224 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005225 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005226 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005227 }
Liz Kammer2dc72442023-04-20 10:10:48 -04005228
5229 java_sdk_library_import {
5230 name: "libbaz",
5231 public: {
5232 jars: ["libbaz.jar"],
5233 },
5234 apex_available: ["myapex"],
5235 shared_library: false,
5236 permitted_packages: ["baz"],
5237 }
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005238 `
5239
Paul Duffin89f570a2021-06-16 01:42:33 +01005240 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005241 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5242 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005243
Paul Duffin537ea3d2021-05-14 10:38:00 +01005244 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005245 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005246 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005247 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005248 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005249 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 +01005250 `)
Liz Kammer2dc72442023-04-20 10:10:48 -04005251
5252 myApex := ctx.ModuleForTests("myapex", "android_common_myapex").Module()
5253
5254 overrideNames := []string{
Spandan Das3576e762024-01-03 18:57:03 +00005255 "myapex",
Liz Kammer2dc72442023-04-20 10:10:48 -04005256 "myjavalib.myapex",
5257 "libfoo.myapex",
5258 "libbar.myapex",
5259 "libbaz.myapex",
5260 }
5261 mkEntries := android.AndroidMkEntriesForTest(t, ctx, myApex)
5262 for i, e := range mkEntries {
5263 g := e.OverrideName
5264 if w := overrideNames[i]; w != g {
5265 t.Errorf("Expected override name %q, got %q", w, g)
5266 }
5267 }
5268
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005269 })
5270
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005271 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5272 bp := `
5273 prebuilt_apex {
5274 name: "myapex",
5275 arch: {
5276 arm64: {
5277 src: "myapex-arm64.apex",
5278 },
5279 arm: {
5280 src: "myapex-arm.apex",
5281 },
5282 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005283 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5284 }
5285
5286 prebuilt_bootclasspath_fragment {
5287 name: "my-bootclasspath-fragment",
5288 contents: ["libfoo", "libbar"],
5289 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005290 hidden_api: {
5291 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5292 metadata: "my-bootclasspath-fragment/metadata.csv",
5293 index: "my-bootclasspath-fragment/index.csv",
5294 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5295 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5296 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005297 }
5298
5299 java_import {
5300 name: "libfoo",
5301 jars: ["libfoo.jar"],
5302 apex_available: ["myapex"],
5303 }
5304
5305 java_library {
5306 name: "libfoo",
5307 srcs: ["foo/bar/MyClass.java"],
5308 apex_available: ["myapex"],
5309 }
Paul Duffin37856732021-02-26 14:24:15 +00005310
5311 java_sdk_library_import {
5312 name: "libbar",
5313 public: {
5314 jars: ["libbar.jar"],
5315 },
5316 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005317 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005318 }
5319
5320 java_sdk_library {
5321 name: "libbar",
5322 srcs: ["foo/bar/MyClass.java"],
5323 unsafe_ignore_missing_latest_api: true,
5324 apex_available: ["myapex"],
5325 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005326 `
5327
5328 // In this test the source (java_library) libfoo is active since the
5329 // prebuilt (java_import) defaults to prefer:false. However the
5330 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5331 // find the dex boot jar in it. We either need to disable the source libfoo
5332 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005333 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005334 // dexbootjar check is skipped if AllowMissingDependencies is true
5335 preparerAllowMissingDeps := android.GroupFixturePreparers(
5336 preparer,
5337 android.PrepareForTestWithAllowMissingDependencies,
5338 )
5339 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005340 })
5341
5342 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5343 bp := `
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005344 apex {
5345 name: "myapex",
5346 key: "myapex.key",
5347 updatable: false,
5348 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5349 }
5350
5351 apex_key {
5352 name: "myapex.key",
5353 public_key: "testkey.avbpubkey",
5354 private_key: "testkey.pem",
5355 }
5356
5357 bootclasspath_fragment {
5358 name: "my-bootclasspath-fragment",
5359 contents: ["libfoo", "libbar"],
5360 apex_available: ["myapex"],
5361 hidden_api: {
5362 split_packages: ["*"],
5363 },
5364 }
5365
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005366 prebuilt_apex {
5367 name: "myapex",
5368 arch: {
5369 arm64: {
5370 src: "myapex-arm64.apex",
5371 },
5372 arm: {
5373 src: "myapex-arm.apex",
5374 },
5375 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005376 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5377 }
5378
5379 prebuilt_bootclasspath_fragment {
5380 name: "my-bootclasspath-fragment",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005381 prefer: true,
Paul Duffin89f570a2021-06-16 01:42:33 +01005382 contents: ["libfoo", "libbar"],
5383 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005384 hidden_api: {
5385 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5386 metadata: "my-bootclasspath-fragment/metadata.csv",
5387 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005388 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5389 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5390 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005391 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005392 }
5393
5394 java_import {
5395 name: "libfoo",
5396 prefer: true,
5397 jars: ["libfoo.jar"],
5398 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005399 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005400 }
5401
5402 java_library {
5403 name: "libfoo",
5404 srcs: ["foo/bar/MyClass.java"],
5405 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005406 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005407 }
Paul Duffin37856732021-02-26 14:24:15 +00005408
5409 java_sdk_library_import {
5410 name: "libbar",
5411 prefer: true,
5412 public: {
5413 jars: ["libbar.jar"],
5414 },
5415 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005416 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005417 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005418 }
5419
5420 java_sdk_library {
5421 name: "libbar",
5422 srcs: ["foo/bar/MyClass.java"],
5423 unsafe_ignore_missing_latest_api: true,
5424 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005425 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005426 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005427 `
5428
Paul Duffin89f570a2021-06-16 01:42:33 +01005429 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005430 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5431 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005432
Paul Duffin537ea3d2021-05-14 10:38:00 +01005433 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005434 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005435 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005436 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005437 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005438 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 +01005439 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005440 })
5441
5442 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5443 bp := `
5444 apex {
5445 name: "myapex",
5446 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005447 updatable: false,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005448 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005449 }
5450
5451 apex_key {
5452 name: "myapex.key",
5453 public_key: "testkey.avbpubkey",
5454 private_key: "testkey.pem",
5455 }
5456
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005457 bootclasspath_fragment {
5458 name: "my-bootclasspath-fragment",
5459 contents: ["libfoo", "libbar"],
5460 apex_available: ["myapex"],
5461 hidden_api: {
5462 split_packages: ["*"],
5463 },
5464 }
5465
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005466 prebuilt_apex {
5467 name: "myapex",
5468 arch: {
5469 arm64: {
5470 src: "myapex-arm64.apex",
5471 },
5472 arm: {
5473 src: "myapex-arm.apex",
5474 },
5475 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005476 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5477 }
5478
5479 prebuilt_bootclasspath_fragment {
5480 name: "my-bootclasspath-fragment",
5481 contents: ["libfoo", "libbar"],
5482 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005483 hidden_api: {
5484 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5485 metadata: "my-bootclasspath-fragment/metadata.csv",
5486 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005487 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5488 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5489 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005490 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005491 }
5492
5493 java_import {
5494 name: "libfoo",
5495 jars: ["libfoo.jar"],
5496 apex_available: ["myapex"],
5497 }
5498
5499 java_library {
5500 name: "libfoo",
5501 srcs: ["foo/bar/MyClass.java"],
5502 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005503 permitted_packages: ["foo"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005504 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005505 }
Paul Duffin37856732021-02-26 14:24:15 +00005506
5507 java_sdk_library_import {
5508 name: "libbar",
5509 public: {
5510 jars: ["libbar.jar"],
5511 },
5512 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005513 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005514 }
5515
5516 java_sdk_library {
5517 name: "libbar",
5518 srcs: ["foo/bar/MyClass.java"],
5519 unsafe_ignore_missing_latest_api: true,
5520 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005521 permitted_packages: ["bar"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005522 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005523 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005524 `
5525
Paul Duffin89f570a2021-06-16 01:42:33 +01005526 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Jiakai Zhangc6879f32023-11-06 16:31:19 +00005527 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/hiddenapi-modular/encoded/libfoo.jar")
5528 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 +00005529
Paul Duffin537ea3d2021-05-14 10:38:00 +01005530 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005531 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005532 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
5533 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005534 out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/modular-hiddenapi/index.csv
5535 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 +01005536 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005537 })
5538
5539 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5540 bp := `
5541 apex {
5542 name: "myapex",
5543 enabled: false,
5544 key: "myapex.key",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005545 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005546 }
5547
5548 apex_key {
5549 name: "myapex.key",
5550 public_key: "testkey.avbpubkey",
5551 private_key: "testkey.pem",
5552 }
5553
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005554 bootclasspath_fragment {
5555 name: "my-bootclasspath-fragment",
5556 enabled: false,
5557 contents: ["libfoo", "libbar"],
5558 apex_available: ["myapex"],
5559 hidden_api: {
5560 split_packages: ["*"],
5561 },
5562 }
5563
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005564 prebuilt_apex {
5565 name: "myapex",
5566 arch: {
5567 arm64: {
5568 src: "myapex-arm64.apex",
5569 },
5570 arm: {
5571 src: "myapex-arm.apex",
5572 },
5573 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005574 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5575 }
5576
5577 prebuilt_bootclasspath_fragment {
5578 name: "my-bootclasspath-fragment",
5579 contents: ["libfoo", "libbar"],
5580 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005581 hidden_api: {
5582 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5583 metadata: "my-bootclasspath-fragment/metadata.csv",
5584 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005585 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5586 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5587 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005588 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005589 }
5590
5591 java_import {
5592 name: "libfoo",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005593 jars: ["libfoo.jar"],
5594 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005595 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005596 }
5597
5598 java_library {
5599 name: "libfoo",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005600 enabled: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005601 srcs: ["foo/bar/MyClass.java"],
5602 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005603 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005604 }
Paul Duffin37856732021-02-26 14:24:15 +00005605
5606 java_sdk_library_import {
5607 name: "libbar",
Paul Duffin37856732021-02-26 14:24:15 +00005608 public: {
5609 jars: ["libbar.jar"],
5610 },
5611 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005612 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005613 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005614 }
5615
5616 java_sdk_library {
5617 name: "libbar",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005618 enabled: false,
Paul Duffin37856732021-02-26 14:24:15 +00005619 srcs: ["foo/bar/MyClass.java"],
5620 unsafe_ignore_missing_latest_api: true,
5621 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005622 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005623 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005624 `
5625
Paul Duffin89f570a2021-06-16 01:42:33 +01005626 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005627 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5628 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005629
Paul Duffin537ea3d2021-05-14 10:38:00 +01005630 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005631 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005632 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005633 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005634 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005635 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 +01005636 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005637 })
Spandan Das3a392012024-01-17 18:26:27 +00005638
Spandan Dasf2c10572024-02-27 04:49:52 +00005639 t.Run("Co-existing unflagged apexes should create a duplicate module error", func(t *testing.T) {
Spandan Das3a392012024-01-17 18:26:27 +00005640 bp := `
5641 // Source
5642 apex {
5643 name: "myapex",
5644 enabled: false,
5645 key: "myapex.key",
5646 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5647 }
5648
5649 apex_key {
5650 name: "myapex.key",
5651 public_key: "testkey.avbpubkey",
5652 private_key: "testkey.pem",
5653 }
5654
5655 // Prebuilt
5656 prebuilt_apex {
5657 name: "myapex.v1",
5658 source_apex_name: "myapex",
5659 arch: {
5660 arm64: {
5661 src: "myapex-arm64.apex",
5662 },
5663 arm: {
5664 src: "myapex-arm.apex",
5665 },
5666 },
5667 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5668 prefer: true,
5669 }
5670 prebuilt_apex {
5671 name: "myapex.v2",
5672 source_apex_name: "myapex",
5673 arch: {
5674 arm64: {
5675 src: "myapex-arm64.apex",
5676 },
5677 arm: {
5678 src: "myapex-arm.apex",
5679 },
5680 },
5681 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5682 prefer: true,
5683 }
5684
5685 prebuilt_bootclasspath_fragment {
5686 name: "my-bootclasspath-fragment",
5687 contents: ["libfoo", "libbar"],
5688 apex_available: ["myapex"],
5689 hidden_api: {
5690 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5691 metadata: "my-bootclasspath-fragment/metadata.csv",
5692 index: "my-bootclasspath-fragment/index.csv",
5693 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5694 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5695 },
5696 prefer: true,
5697 }
5698
5699 java_import {
5700 name: "libfoo",
5701 jars: ["libfoo.jar"],
5702 apex_available: ["myapex"],
5703 prefer: true,
5704 }
5705 java_import {
5706 name: "libbar",
5707 jars: ["libbar.jar"],
5708 apex_available: ["myapex"],
5709 prefer: true,
5710 }
5711 `
5712
Spandan Dasf2c10572024-02-27 04:49:52 +00005713 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 +00005714 })
5715
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005716}
5717
Roland Levillain630846d2019-06-26 12:48:34 +01005718func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005719 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005720 apex_test {
5721 name: "myapex",
5722 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005723 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005724 tests: [
5725 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005726 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005727 ],
5728 }
5729
5730 apex_key {
5731 name: "myapex.key",
5732 public_key: "testkey.avbpubkey",
5733 private_key: "testkey.pem",
5734 }
5735
Liz Kammer1c14a212020-05-12 15:26:55 -07005736 filegroup {
5737 name: "fg",
5738 srcs: [
5739 "baz",
5740 "bar/baz"
5741 ],
5742 }
5743
Roland Levillain630846d2019-06-26 12:48:34 +01005744 cc_test {
5745 name: "mytest",
5746 gtest: false,
5747 srcs: ["mytest.cpp"],
5748 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005749 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005750 system_shared_libs: [],
5751 static_executable: true,
5752 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005753 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005754 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005755
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005756 cc_library {
5757 name: "mylib",
5758 srcs: ["mylib.cpp"],
5759 system_shared_libs: [],
5760 stl: "none",
5761 }
5762
Liz Kammer5bd365f2020-05-27 15:15:11 -07005763 filegroup {
5764 name: "fg2",
5765 srcs: [
5766 "testdata/baz"
5767 ],
5768 }
5769
Roland Levillain9b5fde92019-06-28 15:41:19 +01005770 cc_test {
5771 name: "mytests",
5772 gtest: false,
5773 srcs: [
5774 "mytest1.cpp",
5775 "mytest2.cpp",
5776 "mytest3.cpp",
5777 ],
5778 test_per_src: true,
5779 relative_install_path: "test",
5780 system_shared_libs: [],
5781 static_executable: true,
5782 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005783 data: [
5784 ":fg",
5785 ":fg2",
5786 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005787 }
Roland Levillain630846d2019-06-26 12:48:34 +01005788 `)
5789
Jooyung Hana0503a52023-08-23 13:12:50 +09005790 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005791 copyCmds := apexRule.Args["copy_commands"]
5792
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005793 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005794 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005795 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005796
Liz Kammer1c14a212020-05-12 15:26:55 -07005797 //Ensure that test data are copied into apex.
5798 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5799 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5800
Roland Levillain9b5fde92019-06-28 15:41:19 +01005801 // Ensure that test deps built with `test_per_src` are copied into apex.
5802 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5803 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5804 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005805
5806 // Ensure the module is correctly translated.
Jooyung Hana0503a52023-08-23 13:12:50 +09005807 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005808 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005809 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005810 prefix := "TARGET_"
5811 var builder strings.Builder
5812 data.Custom(&builder, name, prefix, "", data)
5813 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005814 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5815 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5816 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5817 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005818 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005819}
5820
Jooyung Hand48f3c32019-08-23 11:18:57 +09005821func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5822 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5823 apex {
5824 name: "myapex",
5825 key: "myapex.key",
5826 native_shared_libs: ["libfoo"],
5827 }
5828
5829 apex_key {
5830 name: "myapex.key",
5831 public_key: "testkey.avbpubkey",
5832 private_key: "testkey.pem",
5833 }
5834
5835 cc_library {
5836 name: "libfoo",
5837 stl: "none",
5838 system_shared_libs: [],
5839 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005840 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005841 }
5842 `)
5843 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5844 apex {
5845 name: "myapex",
5846 key: "myapex.key",
5847 java_libs: ["myjar"],
5848 }
5849
5850 apex_key {
5851 name: "myapex.key",
5852 public_key: "testkey.avbpubkey",
5853 private_key: "testkey.pem",
5854 }
5855
5856 java_library {
5857 name: "myjar",
5858 srcs: ["foo/bar/MyClass.java"],
5859 sdk_version: "none",
5860 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005861 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005862 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005863 }
5864 `)
5865}
5866
Bill Peckhama41a6962021-01-11 10:58:54 -08005867func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005868 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005869 apex {
5870 name: "myapex",
5871 key: "myapex.key",
5872 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005873 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005874 }
5875
5876 apex_key {
5877 name: "myapex.key",
5878 public_key: "testkey.avbpubkey",
5879 private_key: "testkey.pem",
5880 }
5881
5882 java_import {
5883 name: "myjavaimport",
5884 apex_available: ["myapex"],
5885 jars: ["my.jar"],
5886 compile_dex: true,
5887 }
5888 `)
5889
Jooyung Hana0503a52023-08-23 13:12:50 +09005890 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Bill Peckhama41a6962021-01-11 10:58:54 -08005891 apexRule := module.Rule("apexRule")
5892 copyCmds := apexRule.Args["copy_commands"]
5893 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5894}
5895
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005896func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005897 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005898 apex {
5899 name: "myapex",
5900 key: "myapex.key",
5901 apps: [
5902 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005903 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005904 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005905 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005906 }
5907
5908 apex_key {
5909 name: "myapex.key",
5910 public_key: "testkey.avbpubkey",
5911 private_key: "testkey.pem",
5912 }
5913
5914 android_app {
5915 name: "AppFoo",
5916 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005917 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005918 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005919 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005920 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005921 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005922 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005923
5924 android_app {
5925 name: "AppFooPriv",
5926 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005927 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005928 system_modules: "none",
5929 privileged: true,
Sam Delmerico15809f82023-05-15 17:21:47 -04005930 privapp_allowlist: "privapp_allowlist_com.android.AppFooPriv.xml",
Colin Cross094cde42020-02-15 10:38:00 -08005931 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005932 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005933 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005934
5935 cc_library_shared {
5936 name: "libjni",
5937 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005938 shared_libs: ["libfoo"],
5939 stl: "none",
5940 system_shared_libs: [],
5941 apex_available: [ "myapex" ],
5942 sdk_version: "current",
5943 }
5944
5945 cc_library_shared {
5946 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005947 stl: "none",
5948 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005949 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005950 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005951 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005952 `)
5953
Jooyung Hana0503a52023-08-23 13:12:50 +09005954 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005955 apexRule := module.Rule("apexRule")
5956 copyCmds := apexRule.Args["copy_commands"]
5957
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005958 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5959 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Andrei Onea580636b2022-08-17 16:53:46 +00005960 ensureContains(t, copyCmds, "image.apex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005961
Colin Crossaede88c2020-08-11 12:17:01 -07005962 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005963 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005964 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005965 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005966 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005967 // JNI libraries including transitive deps are
5968 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005969 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005970 // ... embedded inside APK (jnilibs.zip)
5971 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5972 // ... and not directly inside the APEX
5973 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5974 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04005975
5976 apexBundle := module.Module().(*apexBundle)
5977 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
5978 var builder strings.Builder
5979 data.Custom(&builder, apexBundle.Name(), "TARGET_", "", data)
5980 androidMk := builder.String()
5981 ensureContains(t, androidMk, "LOCAL_MODULE := AppFooPriv.myapex")
5982 ensureContains(t, androidMk, "LOCAL_MODULE := AppFoo.myapex")
5983 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFooPriv.apk")
5984 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFoo.apk")
5985 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := \\S+AppFooPriv.apk")
5986 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 +01005987}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005988
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005989func TestApexWithAppImportBuildId(t *testing.T) {
5990 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5991 for _, id := range invalidBuildIds {
5992 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5993 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5994 variables.BuildId = proptools.StringPtr(id)
5995 })
5996 testApexError(t, message, `apex {
5997 name: "myapex",
5998 key: "myapex.key",
5999 apps: ["AppFooPrebuilt"],
6000 updatable: false,
6001 }
6002
6003 apex_key {
6004 name: "myapex.key",
6005 public_key: "testkey.avbpubkey",
6006 private_key: "testkey.pem",
6007 }
6008
6009 android_app_import {
6010 name: "AppFooPrebuilt",
6011 apk: "PrebuiltAppFoo.apk",
6012 presigned: true,
6013 apex_available: ["myapex"],
6014 }
6015 `, fixture)
6016 }
6017}
6018
Dario Frenicde2a032019-10-27 00:29:22 +01006019func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006020 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01006021 apex {
6022 name: "myapex",
6023 key: "myapex.key",
6024 apps: [
6025 "AppFooPrebuilt",
6026 "AppFooPrivPrebuilt",
6027 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006028 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01006029 }
6030
6031 apex_key {
6032 name: "myapex.key",
6033 public_key: "testkey.avbpubkey",
6034 private_key: "testkey.pem",
6035 }
6036
6037 android_app_import {
6038 name: "AppFooPrebuilt",
6039 apk: "PrebuiltAppFoo.apk",
6040 presigned: true,
6041 dex_preopt: {
6042 enabled: false,
6043 },
Jiyong Park592a6a42020-04-21 22:34:28 +09006044 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006045 }
6046
6047 android_app_import {
6048 name: "AppFooPrivPrebuilt",
6049 apk: "PrebuiltAppFooPriv.apk",
6050 privileged: true,
6051 presigned: true,
6052 dex_preopt: {
6053 enabled: false,
6054 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006055 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006056 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006057 }
6058 `)
6059
Jooyung Hana0503a52023-08-23 13:12:50 +09006060 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Frenicde2a032019-10-27 00:29:22 +01006061 apexRule := module.Rule("apexRule")
6062 copyCmds := apexRule.Args["copy_commands"]
6063
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006064 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6065 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006066}
6067
6068func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006069 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006070 apex {
6071 name: "myapex",
6072 key: "myapex.key",
6073 apps: [
6074 "AppFoo",
6075 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006076 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006077 }
6078
6079 apex_key {
6080 name: "myapex.key",
6081 public_key: "testkey.avbpubkey",
6082 private_key: "testkey.pem",
6083 }
6084
6085 android_app {
6086 name: "AppFoo",
6087 srcs: ["foo/bar/MyClass.java"],
6088 sdk_version: "none",
6089 system_modules: "none",
6090 apex_available: [ "myapex" ],
6091 }
6092
6093 android_app_import {
6094 name: "AppFoo",
6095 apk: "AppFooPrebuilt.apk",
6096 filename: "AppFooPrebuilt.apk",
6097 presigned: true,
6098 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006099 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006100 }
6101 `, withFiles(map[string][]byte{
6102 "AppFooPrebuilt.apk": nil,
6103 }))
6104
Jooyung Hana0503a52023-08-23 13:12:50 +09006105 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006106 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006107 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006108}
6109
Dario Freni6f3937c2019-12-20 22:58:03 +00006110func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006111 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006112 apex {
6113 name: "myapex",
6114 key: "myapex.key",
6115 apps: [
6116 "TesterHelpAppFoo",
6117 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006118 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006119 }
6120
6121 apex_key {
6122 name: "myapex.key",
6123 public_key: "testkey.avbpubkey",
6124 private_key: "testkey.pem",
6125 }
6126
6127 android_test_helper_app {
6128 name: "TesterHelpAppFoo",
6129 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006130 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006131 }
6132
6133 `)
6134
Jooyung Hana0503a52023-08-23 13:12:50 +09006135 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Freni6f3937c2019-12-20 22:58:03 +00006136 apexRule := module.Rule("apexRule")
6137 copyCmds := apexRule.Args["copy_commands"]
6138
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006139 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006140}
6141
Jooyung Han18020ea2019-11-13 10:50:48 +09006142func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6143 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006144 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006145 apex {
6146 name: "myapex",
6147 key: "myapex.key",
6148 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006149 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006150 }
6151
6152 apex_key {
6153 name: "myapex.key",
6154 public_key: "testkey.avbpubkey",
6155 private_key: "testkey.pem",
6156 }
6157
6158 apex {
6159 name: "otherapex",
6160 key: "myapex.key",
6161 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006162 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006163 }
6164
6165 cc_defaults {
6166 name: "libfoo-defaults",
6167 apex_available: ["otherapex"],
6168 }
6169
6170 cc_library {
6171 name: "libfoo",
6172 defaults: ["libfoo-defaults"],
6173 stl: "none",
6174 system_shared_libs: [],
6175 }`)
6176}
6177
Paul Duffine52e66f2020-03-30 17:54:29 +01006178func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006179 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006180 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006181 apex {
6182 name: "myapex",
6183 key: "myapex.key",
6184 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006185 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006186 }
6187
6188 apex_key {
6189 name: "myapex.key",
6190 public_key: "testkey.avbpubkey",
6191 private_key: "testkey.pem",
6192 }
6193
6194 apex {
6195 name: "otherapex",
6196 key: "otherapex.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: "otherapex.key",
6203 public_key: "testkey.avbpubkey",
6204 private_key: "testkey.pem",
6205 }
6206
6207 cc_library {
6208 name: "libfoo",
6209 stl: "none",
6210 system_shared_libs: [],
6211 apex_available: ["otherapex"],
6212 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006213}
Jiyong Park127b40b2019-09-30 16:04:35 +09006214
Paul Duffine52e66f2020-03-30 17:54:29 +01006215func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006216 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006217 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006218.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006219.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006220.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006221.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006222.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006223.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006224 apex {
6225 name: "myapex",
6226 key: "myapex.key",
6227 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006228 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006229 }
6230
6231 apex_key {
6232 name: "myapex.key",
6233 public_key: "testkey.avbpubkey",
6234 private_key: "testkey.pem",
6235 }
6236
Jiyong Park127b40b2019-09-30 16:04:35 +09006237 cc_library {
6238 name: "libfoo",
6239 stl: "none",
6240 shared_libs: ["libbar"],
6241 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006242 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006243 }
6244
6245 cc_library {
6246 name: "libbar",
6247 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006248 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006249 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006250 apex_available: ["myapex"],
6251 }
6252
6253 cc_library {
6254 name: "libbaz",
6255 stl: "none",
6256 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006257 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006258}
Jiyong Park127b40b2019-09-30 16:04:35 +09006259
Liz Kammer5f108fa2023-05-11 14:33:17 -04006260func TestApexAvailable_IndirectStaticDep(t *testing.T) {
6261 testApex(t, `
6262 apex {
6263 name: "myapex",
6264 key: "myapex.key",
6265 native_shared_libs: ["libfoo"],
6266 updatable: false,
6267 }
6268
6269 apex_key {
6270 name: "myapex.key",
6271 public_key: "testkey.avbpubkey",
6272 private_key: "testkey.pem",
6273 }
6274
6275 cc_library {
6276 name: "libfoo",
6277 stl: "none",
6278 static_libs: ["libbar"],
6279 system_shared_libs: [],
6280 apex_available: ["myapex"],
6281 }
6282
6283 cc_library {
6284 name: "libbar",
6285 stl: "none",
6286 shared_libs: ["libbaz"],
6287 system_shared_libs: [],
6288 apex_available: ["myapex"],
6289 }
6290
6291 cc_library {
6292 name: "libbaz",
6293 stl: "none",
6294 system_shared_libs: [],
6295 }`)
6296
6297 testApexError(t, `requires "libbar" that doesn't list the APEX under 'apex_available'.`, `
6298 apex {
6299 name: "myapex",
6300 key: "myapex.key",
6301 native_shared_libs: ["libfoo"],
6302 updatable: false,
6303 }
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 static_libs: ["libbar"],
6315 system_shared_libs: [],
6316 apex_available: ["myapex"],
6317 }
6318
6319 cc_library {
6320 name: "libbar",
6321 stl: "none",
6322 system_shared_libs: [],
6323 }`)
6324}
6325
Paul Duffine52e66f2020-03-30 17:54:29 +01006326func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006327 testApexError(t, "\"otherapex\" is not a valid module name", `
6328 apex {
6329 name: "myapex",
6330 key: "myapex.key",
6331 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006332 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006333 }
6334
6335 apex_key {
6336 name: "myapex.key",
6337 public_key: "testkey.avbpubkey",
6338 private_key: "testkey.pem",
6339 }
6340
6341 cc_library {
6342 name: "libfoo",
6343 stl: "none",
6344 system_shared_libs: [],
6345 apex_available: ["otherapex"],
6346 }`)
6347
Paul Duffine52e66f2020-03-30 17:54:29 +01006348 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006349 apex {
6350 name: "myapex",
6351 key: "myapex.key",
6352 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006353 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006354 }
6355
6356 apex_key {
6357 name: "myapex.key",
6358 public_key: "testkey.avbpubkey",
6359 private_key: "testkey.pem",
6360 }
6361
6362 cc_library {
6363 name: "libfoo",
6364 stl: "none",
6365 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006366 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006367 apex_available: ["myapex"],
6368 }
6369
6370 cc_library {
6371 name: "libbar",
6372 stl: "none",
6373 system_shared_libs: [],
6374 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006375 }
6376
6377 cc_library {
6378 name: "libbaz",
6379 stl: "none",
6380 system_shared_libs: [],
6381 stubs: {
6382 versions: ["10", "20", "30"],
6383 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006384 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006385}
Jiyong Park127b40b2019-09-30 16:04:35 +09006386
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006387func TestApexAvailable_ApexAvailableNameWithVersionCodeError(t *testing.T) {
6388 t.Run("negative variant_version produces error", func(t *testing.T) {
6389 testApexError(t, "expected an integer between 0-9; got -1", `
6390 apex {
6391 name: "myapex",
6392 key: "myapex.key",
6393 apex_available_name: "com.android.foo",
6394 variant_version: "-1",
6395 updatable: false,
6396 }
6397 apex_key {
6398 name: "myapex.key",
6399 public_key: "testkey.avbpubkey",
6400 private_key: "testkey.pem",
6401 }
6402 `)
6403 })
6404
6405 t.Run("variant_version greater than 9 produces error", func(t *testing.T) {
6406 testApexError(t, "expected an integer between 0-9; got 10", `
6407 apex {
6408 name: "myapex",
6409 key: "myapex.key",
6410 apex_available_name: "com.android.foo",
6411 variant_version: "10",
6412 updatable: false,
6413 }
6414 apex_key {
6415 name: "myapex.key",
6416 public_key: "testkey.avbpubkey",
6417 private_key: "testkey.pem",
6418 }
6419 `)
6420 })
6421}
6422
6423func TestApexAvailable_ApexAvailableNameWithVersionCode(t *testing.T) {
6424 context := android.GroupFixturePreparers(
6425 android.PrepareForIntegrationTestWithAndroid,
6426 PrepareForTestWithApexBuildComponents,
6427 android.FixtureMergeMockFs(android.MockFS{
6428 "system/sepolicy/apex/foo-file_contexts": nil,
6429 "system/sepolicy/apex/bar-file_contexts": nil,
6430 }),
6431 )
6432 result := context.RunTestWithBp(t, `
6433 apex {
6434 name: "foo",
6435 key: "myapex.key",
6436 apex_available_name: "com.android.foo",
6437 variant_version: "0",
6438 updatable: false,
6439 }
6440 apex {
6441 name: "bar",
6442 key: "myapex.key",
6443 apex_available_name: "com.android.foo",
6444 variant_version: "3",
6445 updatable: false,
6446 }
6447 apex_key {
6448 name: "myapex.key",
6449 public_key: "testkey.avbpubkey",
6450 private_key: "testkey.pem",
6451 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006452 override_apex {
6453 name: "myoverrideapex",
6454 base: "bar",
6455 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006456 `)
6457
Jooyung Hana0503a52023-08-23 13:12:50 +09006458 fooManifestRule := result.ModuleForTests("foo", "android_common_foo").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006459 fooExpectedDefaultVersion := android.DefaultUpdatableModuleVersion
6460 fooActualDefaultVersion := fooManifestRule.Args["default_version"]
6461 if fooActualDefaultVersion != fooExpectedDefaultVersion {
6462 t.Errorf("expected to find defaultVersion %q; got %q", fooExpectedDefaultVersion, fooActualDefaultVersion)
6463 }
6464
Jooyung Hana0503a52023-08-23 13:12:50 +09006465 barManifestRule := result.ModuleForTests("bar", "android_common_bar").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006466 defaultVersionInt, _ := strconv.Atoi(android.DefaultUpdatableModuleVersion)
6467 barExpectedDefaultVersion := fmt.Sprint(defaultVersionInt + 3)
6468 barActualDefaultVersion := barManifestRule.Args["default_version"]
6469 if barActualDefaultVersion != barExpectedDefaultVersion {
6470 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6471 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006472
Jooyung Hana0503a52023-08-23 13:12:50 +09006473 overrideBarManifestRule := result.ModuleForTests("bar", "android_common_myoverrideapex_bar").Rule("apexManifestRule")
Sam Delmerico419f9a32023-07-21 12:00:13 -04006474 overrideBarActualDefaultVersion := overrideBarManifestRule.Args["default_version"]
6475 if overrideBarActualDefaultVersion != barExpectedDefaultVersion {
6476 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6477 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006478}
6479
Sam Delmericoca816532023-06-02 14:09:50 -04006480func TestApexAvailable_ApexAvailableName(t *testing.T) {
6481 t.Run("using name of apex that sets apex_available_name is not allowed", func(t *testing.T) {
6482 testApexError(t, "Consider adding \"myapex\" to 'apex_available' property of \"AppFoo\"", `
6483 apex {
6484 name: "myapex_sminus",
6485 key: "myapex.key",
6486 apps: ["AppFoo"],
6487 apex_available_name: "myapex",
6488 updatable: false,
6489 }
6490 apex {
6491 name: "myapex",
6492 key: "myapex.key",
6493 apps: ["AppFoo"],
6494 updatable: false,
6495 }
6496 apex_key {
6497 name: "myapex.key",
6498 public_key: "testkey.avbpubkey",
6499 private_key: "testkey.pem",
6500 }
6501 android_app {
6502 name: "AppFoo",
6503 srcs: ["foo/bar/MyClass.java"],
6504 sdk_version: "none",
6505 system_modules: "none",
6506 apex_available: [ "myapex_sminus" ],
6507 }`,
6508 android.FixtureMergeMockFs(android.MockFS{
6509 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6510 }),
6511 )
6512 })
6513
6514 t.Run("apex_available_name allows module to be used in two different apexes", func(t *testing.T) {
6515 testApex(t, `
6516 apex {
6517 name: "myapex_sminus",
6518 key: "myapex.key",
6519 apps: ["AppFoo"],
6520 apex_available_name: "myapex",
6521 updatable: false,
6522 }
6523 apex {
6524 name: "myapex",
6525 key: "myapex.key",
6526 apps: ["AppFoo"],
6527 updatable: false,
6528 }
6529 apex_key {
6530 name: "myapex.key",
6531 public_key: "testkey.avbpubkey",
6532 private_key: "testkey.pem",
6533 }
6534 android_app {
6535 name: "AppFoo",
6536 srcs: ["foo/bar/MyClass.java"],
6537 sdk_version: "none",
6538 system_modules: "none",
6539 apex_available: [ "myapex" ],
6540 }`,
6541 android.FixtureMergeMockFs(android.MockFS{
6542 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6543 }),
6544 )
6545 })
6546
6547 t.Run("override_apexes work with apex_available_name", func(t *testing.T) {
6548 testApex(t, `
6549 override_apex {
6550 name: "myoverrideapex_sminus",
6551 base: "myapex_sminus",
6552 key: "myapex.key",
6553 apps: ["AppFooOverride"],
6554 }
6555 override_apex {
6556 name: "myoverrideapex",
6557 base: "myapex",
6558 key: "myapex.key",
6559 apps: ["AppFooOverride"],
6560 }
6561 apex {
6562 name: "myapex_sminus",
6563 key: "myapex.key",
6564 apps: ["AppFoo"],
6565 apex_available_name: "myapex",
6566 updatable: false,
6567 }
6568 apex {
6569 name: "myapex",
6570 key: "myapex.key",
6571 apps: ["AppFoo"],
6572 updatable: false,
6573 }
6574 apex_key {
6575 name: "myapex.key",
6576 public_key: "testkey.avbpubkey",
6577 private_key: "testkey.pem",
6578 }
6579 android_app {
6580 name: "AppFooOverride",
6581 srcs: ["foo/bar/MyClass.java"],
6582 sdk_version: "none",
6583 system_modules: "none",
6584 apex_available: [ "myapex" ],
6585 }
6586 android_app {
6587 name: "AppFoo",
6588 srcs: ["foo/bar/MyClass.java"],
6589 sdk_version: "none",
6590 system_modules: "none",
6591 apex_available: [ "myapex" ],
6592 }`,
6593 android.FixtureMergeMockFs(android.MockFS{
6594 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6595 }),
6596 )
6597 })
6598}
6599
6600func TestApexAvailable_ApexAvailableNameWithOverrides(t *testing.T) {
6601 context := android.GroupFixturePreparers(
6602 android.PrepareForIntegrationTestWithAndroid,
6603 PrepareForTestWithApexBuildComponents,
6604 java.PrepareForTestWithDexpreopt,
6605 android.FixtureMergeMockFs(android.MockFS{
6606 "system/sepolicy/apex/myapex-file_contexts": nil,
6607 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6608 }),
6609 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6610 variables.BuildId = proptools.StringPtr("buildid")
6611 }),
6612 )
6613 context.RunTestWithBp(t, `
6614 override_apex {
6615 name: "myoverrideapex_sminus",
6616 base: "myapex_sminus",
6617 }
6618 override_apex {
6619 name: "myoverrideapex",
6620 base: "myapex",
6621 }
6622 apex {
6623 name: "myapex",
6624 key: "myapex.key",
6625 apps: ["AppFoo"],
6626 updatable: false,
6627 }
6628 apex {
6629 name: "myapex_sminus",
6630 apex_available_name: "myapex",
6631 key: "myapex.key",
6632 apps: ["AppFoo_sminus"],
6633 updatable: false,
6634 }
6635 apex_key {
6636 name: "myapex.key",
6637 public_key: "testkey.avbpubkey",
6638 private_key: "testkey.pem",
6639 }
6640 android_app {
6641 name: "AppFoo",
6642 srcs: ["foo/bar/MyClass.java"],
6643 sdk_version: "none",
6644 system_modules: "none",
6645 apex_available: [ "myapex" ],
6646 }
6647 android_app {
6648 name: "AppFoo_sminus",
6649 srcs: ["foo/bar/MyClass.java"],
6650 sdk_version: "none",
6651 min_sdk_version: "29",
6652 system_modules: "none",
6653 apex_available: [ "myapex" ],
6654 }`)
6655}
6656
Jiyong Park89e850a2020-04-07 16:37:39 +09006657func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006658 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006659 apex {
6660 name: "myapex",
6661 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006662 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006663 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006664 }
6665
6666 apex_key {
6667 name: "myapex.key",
6668 public_key: "testkey.avbpubkey",
6669 private_key: "testkey.pem",
6670 }
6671
6672 cc_library {
6673 name: "libfoo",
6674 stl: "none",
6675 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006676 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006677 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006678 }
6679
6680 cc_library {
6681 name: "libfoo2",
6682 stl: "none",
6683 system_shared_libs: [],
6684 shared_libs: ["libbaz"],
6685 apex_available: ["//apex_available:platform"],
6686 }
6687
6688 cc_library {
6689 name: "libbar",
6690 stl: "none",
6691 system_shared_libs: [],
6692 apex_available: ["myapex"],
6693 }
6694
6695 cc_library {
6696 name: "libbaz",
6697 stl: "none",
6698 system_shared_libs: [],
6699 apex_available: ["myapex"],
6700 stubs: {
6701 versions: ["1"],
6702 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006703 }`)
6704
Jiyong Park89e850a2020-04-07 16:37:39 +09006705 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6706 // because it depends on libbar which isn't available to platform
6707 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6708 if libfoo.NotAvailableForPlatform() != true {
6709 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6710 }
6711
6712 // libfoo2 however can be available to platform because it depends on libbaz which provides
6713 // stubs
6714 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6715 if libfoo2.NotAvailableForPlatform() == true {
6716 t.Errorf("%q should be available to platform", libfoo2.String())
6717 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006718}
Jiyong Parka90ca002019-10-07 15:47:24 +09006719
Paul Duffine52e66f2020-03-30 17:54:29 +01006720func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006721 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006722 apex {
6723 name: "myapex",
6724 key: "myapex.key",
6725 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006726 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006727 }
6728
6729 apex_key {
6730 name: "myapex.key",
6731 public_key: "testkey.avbpubkey",
6732 private_key: "testkey.pem",
6733 }
6734
6735 cc_library {
6736 name: "libfoo",
6737 stl: "none",
6738 system_shared_libs: [],
6739 apex_available: ["myapex"],
6740 static: {
6741 apex_available: ["//apex_available:platform"],
6742 },
6743 }`)
6744
Jiyong Park89e850a2020-04-07 16:37:39 +09006745 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6746 if libfooShared.NotAvailableForPlatform() != true {
6747 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6748 }
6749 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6750 if libfooStatic.NotAvailableForPlatform() != false {
6751 t.Errorf("%q should be available to platform", libfooStatic.String())
6752 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006753}
6754
Jiyong Park5d790c32019-11-15 18:40:32 +09006755func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006756 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006757 apex {
6758 name: "myapex",
6759 key: "myapex.key",
6760 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006761 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006762 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006763 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006764 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006765 }
6766
6767 override_apex {
6768 name: "override_myapex",
6769 base: "myapex",
6770 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006771 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006772 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006773 overrides: ["unknownapex"],
Jesse Melhuishec60e252024-03-29 19:08:20 +00006774 compile_multilib: "first",
6775 multilib: {
6776 lib32: {
6777 native_shared_libs: ["mylib32"],
6778 },
6779 lib64: {
6780 native_shared_libs: ["mylib64"],
6781 },
6782 },
Baligh Uddin004d7172020-02-19 21:29:28 -08006783 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006784 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006785 key: "mynewapex.key",
6786 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006787 }
6788
6789 apex_key {
6790 name: "myapex.key",
6791 public_key: "testkey.avbpubkey",
6792 private_key: "testkey.pem",
6793 }
6794
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006795 apex_key {
6796 name: "mynewapex.key",
6797 public_key: "testkey2.avbpubkey",
6798 private_key: "testkey2.pem",
6799 }
6800
6801 android_app_certificate {
6802 name: "myapex.certificate",
6803 certificate: "testkey",
6804 }
6805
Jiyong Park5d790c32019-11-15 18:40:32 +09006806 android_app {
6807 name: "app",
6808 srcs: ["foo/bar/MyClass.java"],
6809 package_name: "foo",
6810 sdk_version: "none",
6811 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006812 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006813 }
6814
6815 override_android_app {
6816 name: "override_app",
6817 base: "app",
6818 package_name: "bar",
6819 }
markchien7c803b82021-08-26 22:10:06 +08006820
6821 bpf {
6822 name: "bpf",
6823 srcs: ["bpf.c"],
6824 }
6825
6826 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006827 name: "overrideBpf",
6828 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006829 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006830
6831 prebuilt_etc {
6832 name: "myetc",
6833 src: "myprebuilt",
6834 }
6835
6836 prebuilt_etc {
6837 name: "override_myetc",
6838 src: "override_myprebuilt",
6839 }
Jesse Melhuishec60e252024-03-29 19:08:20 +00006840
6841 cc_library {
6842 name: "mylib32",
6843 apex_available: [ "myapex" ],
6844 }
6845
6846 cc_library {
6847 name: "mylib64",
6848 apex_available: [ "myapex" ],
6849 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006850 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006851
Jooyung Hana0503a52023-08-23 13:12:50 +09006852 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(android.OverridableModule)
6853 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex").Module().(android.OverridableModule)
Jiyong Park317645e2019-12-05 13:20:58 +09006854 if originalVariant.GetOverriddenBy() != "" {
6855 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6856 }
6857 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6858 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6859 }
6860
Jooyung Hana0503a52023-08-23 13:12:50 +09006861 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006862 apexRule := module.Rule("apexRule")
6863 copyCmds := apexRule.Args["copy_commands"]
6864
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006865 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6866 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006867
markchien7c803b82021-08-26 22:10:06 +08006868 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006869 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006870
Daniel Norman5a3ce132021-08-26 15:44:43 -07006871 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6872 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6873
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006874 apexBundle := module.Module().(*apexBundle)
6875 name := apexBundle.Name()
6876 if name != "override_myapex" {
6877 t.Errorf("name should be \"override_myapex\", but was %q", name)
6878 }
6879
Baligh Uddin004d7172020-02-19 21:29:28 -08006880 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6881 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6882 }
6883
Jiyong Park20bacab2020-03-03 11:45:41 +09006884 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006885 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006886 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6887
6888 signApkRule := module.Rule("signapk")
6889 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006890
Colin Crossaa255532020-07-03 13:18:24 -07006891 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006892 var builder strings.Builder
6893 data.Custom(&builder, name, "TARGET_", "", data)
6894 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006895 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6896 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006897 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006898 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006899 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006900 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006901 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006902 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006903}
6904
Albert Martineefabcf2022-03-21 20:11:16 +00006905func TestMinSdkVersionOverride(t *testing.T) {
6906 // Override from 29 to 31
6907 minSdkOverride31 := "31"
6908 ctx := testApex(t, `
6909 apex {
6910 name: "myapex",
6911 key: "myapex.key",
6912 native_shared_libs: ["mylib"],
6913 updatable: true,
6914 min_sdk_version: "29"
6915 }
6916
6917 override_apex {
6918 name: "override_myapex",
6919 base: "myapex",
6920 logging_parent: "com.foo.bar",
6921 package_name: "test.overridden.package"
6922 }
6923
6924 apex_key {
6925 name: "myapex.key",
6926 public_key: "testkey.avbpubkey",
6927 private_key: "testkey.pem",
6928 }
6929
6930 cc_library {
6931 name: "mylib",
6932 srcs: ["mylib.cpp"],
6933 runtime_libs: ["libbar"],
6934 system_shared_libs: [],
6935 stl: "none",
6936 apex_available: [ "myapex" ],
6937 min_sdk_version: "apex_inherit"
6938 }
6939
6940 cc_library {
6941 name: "libbar",
6942 srcs: ["mylib.cpp"],
6943 system_shared_libs: [],
6944 stl: "none",
6945 apex_available: [ "myapex" ],
6946 min_sdk_version: "apex_inherit"
6947 }
6948
6949 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6950
Jooyung Hana0503a52023-08-23 13:12:50 +09006951 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00006952 copyCmds := apexRule.Args["copy_commands"]
6953
6954 // Ensure that direct non-stubs dep is always included
6955 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6956
6957 // Ensure that runtime_libs dep in included
6958 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6959
6960 // Ensure libraries target overridden min_sdk_version value
6961 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6962}
6963
6964func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6965 // Attempt to override from 31 to 29, should be a NOOP
6966 minSdkOverride29 := "29"
6967 ctx := testApex(t, `
6968 apex {
6969 name: "myapex",
6970 key: "myapex.key",
6971 native_shared_libs: ["mylib"],
6972 updatable: true,
6973 min_sdk_version: "31"
6974 }
6975
6976 override_apex {
6977 name: "override_myapex",
6978 base: "myapex",
6979 logging_parent: "com.foo.bar",
6980 package_name: "test.overridden.package"
6981 }
6982
6983 apex_key {
6984 name: "myapex.key",
6985 public_key: "testkey.avbpubkey",
6986 private_key: "testkey.pem",
6987 }
6988
6989 cc_library {
6990 name: "mylib",
6991 srcs: ["mylib.cpp"],
6992 runtime_libs: ["libbar"],
6993 system_shared_libs: [],
6994 stl: "none",
6995 apex_available: [ "myapex" ],
6996 min_sdk_version: "apex_inherit"
6997 }
6998
6999 cc_library {
7000 name: "libbar",
7001 srcs: ["mylib.cpp"],
7002 system_shared_libs: [],
7003 stl: "none",
7004 apex_available: [ "myapex" ],
7005 min_sdk_version: "apex_inherit"
7006 }
7007
7008 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
7009
Jooyung Hana0503a52023-08-23 13:12:50 +09007010 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007011 copyCmds := apexRule.Args["copy_commands"]
7012
7013 // Ensure that direct non-stubs dep is always included
7014 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7015
7016 // Ensure that runtime_libs dep in included
7017 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7018
7019 // Ensure libraries target the original min_sdk_version value rather than the overridden
7020 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7021}
7022
Jooyung Han214bf372019-11-12 13:03:50 +09007023func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007024 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09007025 apex {
7026 name: "myapex",
7027 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007028 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09007029 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09007030 }
7031
7032 apex_key {
7033 name: "myapex.key",
7034 public_key: "testkey.avbpubkey",
7035 private_key: "testkey.pem",
7036 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007037
7038 cc_library {
7039 name: "mylib",
7040 srcs: ["mylib.cpp"],
7041 stl: "libc++",
7042 system_shared_libs: [],
7043 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09007044 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007045 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007046 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09007047
Jooyung Hana0503a52023-08-23 13:12:50 +09007048 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +09007049 args := module.Rule("apexRule").Args
7050 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00007051 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007052
7053 // The copies of the libraries in the apex should have one more dependency than
7054 // the ones outside the apex, namely the unwinder. Ideally we should check
7055 // the dependency names directly here but for some reason the names are blank in
7056 // this test.
7057 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07007058 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007059 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
7060 if len(apexImplicits) != len(nonApexImplicits)+1 {
7061 t.Errorf("%q missing unwinder dep", lib)
7062 }
7063 }
Jooyung Han214bf372019-11-12 13:03:50 +09007064}
7065
Paul Duffine05480a2021-03-08 15:07:14 +00007066var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01007067 "api/current.txt": nil,
7068 "api/removed.txt": nil,
7069 "api/system-current.txt": nil,
7070 "api/system-removed.txt": nil,
7071 "api/test-current.txt": nil,
7072 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01007073
Anton Hanssondff2c782020-12-21 17:10:01 +00007074 "100/public/api/foo.txt": nil,
7075 "100/public/api/foo-removed.txt": nil,
7076 "100/system/api/foo.txt": nil,
7077 "100/system/api/foo-removed.txt": nil,
7078
Paul Duffineedc5d52020-06-12 17:46:39 +01007079 // For java_sdk_library_import
7080 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01007081}
7082
Jooyung Han58f26ab2019-12-18 15:34:32 +09007083func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007084 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09007085 apex {
7086 name: "myapex",
7087 key: "myapex.key",
7088 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007089 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09007090 }
7091
7092 apex_key {
7093 name: "myapex.key",
7094 public_key: "testkey.avbpubkey",
7095 private_key: "testkey.pem",
7096 }
7097
7098 java_sdk_library {
7099 name: "foo",
7100 srcs: ["a.java"],
7101 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007102 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09007103 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007104
7105 prebuilt_apis {
7106 name: "sdk",
7107 api_dirs: ["100"],
7108 }
Paul Duffin9b879592020-05-26 13:21:35 +01007109 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09007110
7111 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007112 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09007113 "javalib/foo.jar",
7114 "etc/permissions/foo.xml",
7115 })
7116 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09007117 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00007118 ensureMatches(t, sdkLibrary.RuleParams.Command, `<library\\n\s+name=\\\"foo\\\"\\n\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09007119}
7120
Paul Duffin9b879592020-05-26 13:21:35 +01007121func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007122 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007123 apex {
7124 name: "myapex",
7125 key: "myapex.key",
7126 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007127 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007128 }
7129
7130 apex_key {
7131 name: "myapex.key",
7132 public_key: "testkey.avbpubkey",
7133 private_key: "testkey.pem",
7134 }
7135
7136 java_sdk_library {
7137 name: "foo",
7138 srcs: ["a.java"],
7139 api_packages: ["foo"],
7140 apex_available: ["myapex"],
7141 sdk_version: "none",
7142 system_modules: "none",
7143 }
7144
7145 java_library {
7146 name: "bar",
7147 srcs: ["a.java"],
7148 libs: ["foo"],
7149 apex_available: ["myapex"],
7150 sdk_version: "none",
7151 system_modules: "none",
7152 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007153
7154 prebuilt_apis {
7155 name: "sdk",
7156 api_dirs: ["100"],
7157 }
Paul Duffin9b879592020-05-26 13:21:35 +01007158 `, withFiles(filesForSdkLibrary))
7159
7160 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007161 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007162 "javalib/bar.jar",
7163 "javalib/foo.jar",
7164 "etc/permissions/foo.xml",
7165 })
7166
7167 // The bar library should depend on the implementation jar.
7168 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kang8479dea2024-04-04 01:19:05 +00007169 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007170 t.Errorf("expected %q, found %#q", expected, actual)
7171 }
7172}
7173
7174func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007175 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007176 apex {
7177 name: "myapex",
7178 key: "myapex.key",
7179 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007180 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007181 }
7182
7183 apex_key {
7184 name: "myapex.key",
7185 public_key: "testkey.avbpubkey",
7186 private_key: "testkey.pem",
7187 }
7188
7189 java_sdk_library {
7190 name: "foo",
7191 srcs: ["a.java"],
7192 api_packages: ["foo"],
7193 apex_available: ["myapex"],
7194 sdk_version: "none",
7195 system_modules: "none",
7196 }
7197
7198 java_library {
7199 name: "bar",
7200 srcs: ["a.java"],
7201 libs: ["foo"],
7202 sdk_version: "none",
7203 system_modules: "none",
7204 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007205
7206 prebuilt_apis {
7207 name: "sdk",
7208 api_dirs: ["100"],
7209 }
Paul Duffin9b879592020-05-26 13:21:35 +01007210 `, withFiles(filesForSdkLibrary))
7211
7212 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007213 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007214 "javalib/foo.jar",
7215 "etc/permissions/foo.xml",
7216 })
7217
7218 // The bar library should depend on the stubs jar.
7219 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007220 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007221 t.Errorf("expected %q, found %#q", expected, actual)
7222 }
7223}
7224
Paul Duffineedc5d52020-06-12 17:46:39 +01007225func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007226 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00007227 prebuilt_apis {
7228 name: "sdk",
7229 api_dirs: ["100"],
7230 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01007231 withFiles(map[string][]byte{
7232 "apex/a.java": nil,
7233 "apex/apex_manifest.json": nil,
7234 "apex/Android.bp": []byte(`
7235 package {
7236 default_visibility: ["//visibility:private"],
7237 }
7238
7239 apex {
7240 name: "myapex",
7241 key: "myapex.key",
7242 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007243 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007244 }
7245
7246 apex_key {
7247 name: "myapex.key",
7248 public_key: "testkey.avbpubkey",
7249 private_key: "testkey.pem",
7250 }
7251
7252 java_library {
7253 name: "bar",
7254 srcs: ["a.java"],
7255 libs: ["foo"],
7256 apex_available: ["myapex"],
7257 sdk_version: "none",
7258 system_modules: "none",
7259 }
7260`),
7261 "source/a.java": nil,
7262 "source/api/current.txt": nil,
7263 "source/api/removed.txt": nil,
7264 "source/Android.bp": []byte(`
7265 package {
7266 default_visibility: ["//visibility:private"],
7267 }
7268
7269 java_sdk_library {
7270 name: "foo",
7271 visibility: ["//apex"],
7272 srcs: ["a.java"],
7273 api_packages: ["foo"],
7274 apex_available: ["myapex"],
7275 sdk_version: "none",
7276 system_modules: "none",
7277 public: {
7278 enabled: true,
7279 },
7280 }
7281`),
7282 "prebuilt/a.jar": nil,
7283 "prebuilt/Android.bp": []byte(`
7284 package {
7285 default_visibility: ["//visibility:private"],
7286 }
7287
7288 java_sdk_library_import {
7289 name: "foo",
7290 visibility: ["//apex", "//source"],
7291 apex_available: ["myapex"],
7292 prefer: true,
7293 public: {
7294 jars: ["a.jar"],
7295 },
7296 }
7297`),
Anton Hanssondff2c782020-12-21 17:10:01 +00007298 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01007299 )
7300
7301 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007302 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffineedc5d52020-06-12 17:46:39 +01007303 "javalib/bar.jar",
7304 "javalib/foo.jar",
7305 "etc/permissions/foo.xml",
7306 })
7307
7308 // The bar library should depend on the implementation jar.
7309 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007310 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01007311 t.Errorf("expected %q, found %#q", expected, actual)
7312 }
7313}
7314
7315func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
7316 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
7317 apex {
7318 name: "myapex",
7319 key: "myapex.key",
7320 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007321 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007322 }
7323
7324 apex_key {
7325 name: "myapex.key",
7326 public_key: "testkey.avbpubkey",
7327 private_key: "testkey.pem",
7328 }
7329
7330 java_sdk_library_import {
7331 name: "foo",
7332 apex_available: ["myapex"],
7333 prefer: true,
7334 public: {
7335 jars: ["a.jar"],
7336 },
7337 }
7338
7339 `, withFiles(filesForSdkLibrary))
7340}
7341
atrost6e126252020-01-27 17:01:16 +00007342func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007343 result := android.GroupFixturePreparers(
7344 prepareForApexTest,
7345 java.PrepareForTestWithPlatformCompatConfig,
7346 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007347 apex {
7348 name: "myapex",
7349 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007350 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007351 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007352 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007353 }
7354
7355 apex_key {
7356 name: "myapex.key",
7357 public_key: "testkey.avbpubkey",
7358 private_key: "testkey.pem",
7359 }
7360
7361 platform_compat_config {
7362 name: "myjar-platform-compat-config",
7363 src: ":myjar",
7364 }
7365
7366 java_library {
7367 name: "myjar",
7368 srcs: ["foo/bar/MyClass.java"],
7369 sdk_version: "none",
7370 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007371 apex_available: [ "myapex" ],
7372 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007373
7374 // Make sure that a preferred prebuilt does not affect the apex contents.
7375 prebuilt_platform_compat_config {
7376 name: "myjar-platform-compat-config",
7377 metadata: "compat-config/metadata.xml",
7378 prefer: true,
7379 }
atrost6e126252020-01-27 17:01:16 +00007380 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007381 ctx := result.TestContext
Jooyung Hana0503a52023-08-23 13:12:50 +09007382 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
atrost6e126252020-01-27 17:01:16 +00007383 "etc/compatconfig/myjar-platform-compat-config.xml",
7384 "javalib/myjar.jar",
7385 })
7386}
7387
Jooyung Han862c0d62022-12-21 10:15:37 +09007388func TestNoDupeApexFiles(t *testing.T) {
7389 android.GroupFixturePreparers(
7390 android.PrepareForTestWithAndroidBuildComponents,
7391 PrepareForTestWithApexBuildComponents,
7392 prepareForTestWithMyapex,
7393 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7394 ).
7395 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7396 RunTestWithBp(t, `
7397 apex {
7398 name: "myapex",
7399 key: "myapex.key",
7400 prebuilts: ["foo", "bar"],
7401 updatable: false,
7402 }
7403
7404 apex_key {
7405 name: "myapex.key",
7406 public_key: "testkey.avbpubkey",
7407 private_key: "testkey.pem",
7408 }
7409
7410 prebuilt_etc {
7411 name: "foo",
7412 src: "myprebuilt",
7413 filename_from_src: true,
7414 }
7415
7416 prebuilt_etc {
7417 name: "bar",
7418 src: "myprebuilt",
7419 filename_from_src: true,
7420 }
7421 `)
7422}
7423
Jooyung Hana8bd72a2023-11-02 11:56:48 +09007424func TestApexUnwantedTransitiveDeps(t *testing.T) {
7425 bp := `
7426 apex {
7427 name: "myapex",
7428 key: "myapex.key",
7429 native_shared_libs: ["libfoo"],
7430 updatable: false,
7431 unwanted_transitive_deps: ["libbar"],
7432 }
7433
7434 apex_key {
7435 name: "myapex.key",
7436 public_key: "testkey.avbpubkey",
7437 private_key: "testkey.pem",
7438 }
7439
7440 cc_library {
7441 name: "libfoo",
7442 srcs: ["foo.cpp"],
7443 shared_libs: ["libbar"],
7444 apex_available: ["myapex"],
7445 }
7446
7447 cc_library {
7448 name: "libbar",
7449 srcs: ["bar.cpp"],
7450 apex_available: ["myapex"],
7451 }`
7452 ctx := testApex(t, bp)
7453 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
7454 "*/libc++.so",
7455 "*/libfoo.so",
7456 // not libbar.so
7457 })
7458}
7459
Jiyong Park479321d2019-12-16 11:47:12 +09007460func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7461 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7462 apex {
7463 name: "myapex",
7464 key: "myapex.key",
7465 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007466 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007467 }
7468
7469 apex_key {
7470 name: "myapex.key",
7471 public_key: "testkey.avbpubkey",
7472 private_key: "testkey.pem",
7473 }
7474
7475 java_library {
7476 name: "myjar",
7477 srcs: ["foo/bar/MyClass.java"],
7478 sdk_version: "none",
7479 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007480 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007481 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007482 }
7483 `)
7484}
7485
Jiyong Park7afd1072019-12-30 16:56:33 +09007486func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007487 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007488 apex {
7489 name: "myapex",
7490 key: "myapex.key",
7491 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007492 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007493 }
7494
7495 apex_key {
7496 name: "myapex.key",
7497 public_key: "testkey.avbpubkey",
7498 private_key: "testkey.pem",
7499 }
7500
7501 cc_library {
7502 name: "mylib",
7503 srcs: ["mylib.cpp"],
7504 system_shared_libs: [],
7505 stl: "none",
7506 required: ["a", "b"],
7507 host_required: ["c", "d"],
7508 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007509 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007510 }
7511 `)
7512
Jooyung Hana0503a52023-08-23 13:12:50 +09007513 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007514 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007515 name := apexBundle.BaseModuleName()
7516 prefix := "TARGET_"
7517 var builder strings.Builder
7518 data.Custom(&builder, name, prefix, "", data)
7519 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007520 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 a b\n")
Sasha Smundakdcb61292022-12-08 10:41:33 -08007521 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7522 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007523}
7524
Jiyong Park7cd10e32020-01-14 09:22:18 +09007525func TestSymlinksFromApexToSystem(t *testing.T) {
7526 bp := `
7527 apex {
7528 name: "myapex",
7529 key: "myapex.key",
7530 native_shared_libs: ["mylib"],
7531 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007532 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007533 }
7534
Jiyong Park9d677202020-02-19 16:29:35 +09007535 apex {
7536 name: "myapex.updatable",
7537 key: "myapex.key",
7538 native_shared_libs: ["mylib"],
7539 java_libs: ["myjar"],
7540 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007541 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007542 }
7543
Jiyong Park7cd10e32020-01-14 09:22:18 +09007544 apex_key {
7545 name: "myapex.key",
7546 public_key: "testkey.avbpubkey",
7547 private_key: "testkey.pem",
7548 }
7549
7550 cc_library {
7551 name: "mylib",
7552 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007553 shared_libs: [
7554 "myotherlib",
7555 "myotherlib_ext",
7556 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007557 system_shared_libs: [],
7558 stl: "none",
7559 apex_available: [
7560 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007561 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007562 "//apex_available:platform",
7563 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007564 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007565 }
7566
7567 cc_library {
7568 name: "myotherlib",
7569 srcs: ["mylib.cpp"],
7570 system_shared_libs: [],
7571 stl: "none",
7572 apex_available: [
7573 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007574 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007575 "//apex_available:platform",
7576 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007577 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007578 }
7579
Jiyong Parkce243632023-02-17 18:22:25 +09007580 cc_library {
7581 name: "myotherlib_ext",
7582 srcs: ["mylib.cpp"],
7583 system_shared_libs: [],
7584 system_ext_specific: true,
7585 stl: "none",
7586 apex_available: [
7587 "myapex",
7588 "myapex.updatable",
7589 "//apex_available:platform",
7590 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007591 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007592 }
7593
Jiyong Park7cd10e32020-01-14 09:22:18 +09007594 java_library {
7595 name: "myjar",
7596 srcs: ["foo/bar/MyClass.java"],
7597 sdk_version: "none",
7598 system_modules: "none",
7599 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007600 apex_available: [
7601 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007602 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007603 "//apex_available:platform",
7604 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007605 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007606 }
7607
7608 java_library {
7609 name: "myotherjar",
7610 srcs: ["foo/bar/MyClass.java"],
7611 sdk_version: "none",
7612 system_modules: "none",
7613 apex_available: [
7614 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007615 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007616 "//apex_available:platform",
7617 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007618 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007619 }
7620 `
7621
7622 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7623 for _, f := range files {
7624 if f.path == file {
7625 if f.isLink {
7626 t.Errorf("%q is not a real file", file)
7627 }
7628 return
7629 }
7630 }
7631 t.Errorf("%q is not found", file)
7632 }
7633
Jiyong Parkce243632023-02-17 18:22:25 +09007634 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007635 for _, f := range files {
7636 if f.path == file {
7637 if !f.isLink {
7638 t.Errorf("%q is not a symlink", file)
7639 }
Jiyong Parkce243632023-02-17 18:22:25 +09007640 if f.src != target {
7641 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7642 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007643 return
7644 }
7645 }
7646 t.Errorf("%q is not found", file)
7647 }
7648
Jiyong Park9d677202020-02-19 16:29:35 +09007649 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7650 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007651 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana0503a52023-08-23 13:12:50 +09007652 files := getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007653 ensureRealfileExists(t, files, "javalib/myjar.jar")
7654 ensureRealfileExists(t, files, "lib64/mylib.so")
7655 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007656 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007657
Jooyung Hana0503a52023-08-23 13:12:50 +09007658 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007659 ensureRealfileExists(t, files, "javalib/myjar.jar")
7660 ensureRealfileExists(t, files, "lib64/mylib.so")
7661 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007662 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007663
7664 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007665 ctx = testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +09007666 files = getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007667 ensureRealfileExists(t, files, "javalib/myjar.jar")
7668 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007669 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7670 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007671
Jooyung Hana0503a52023-08-23 13:12:50 +09007672 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007673 ensureRealfileExists(t, files, "javalib/myjar.jar")
7674 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007675 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7676 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007677}
7678
Yo Chiange8128052020-07-23 20:09:18 +08007679func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007680 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007681 apex {
7682 name: "myapex",
7683 key: "myapex.key",
7684 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007685 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007686 }
7687
7688 apex_key {
7689 name: "myapex.key",
7690 public_key: "testkey.avbpubkey",
7691 private_key: "testkey.pem",
7692 }
7693
7694 cc_library_shared {
7695 name: "mylib",
7696 srcs: ["mylib.cpp"],
7697 shared_libs: ["myotherlib"],
7698 system_shared_libs: [],
7699 stl: "none",
7700 apex_available: [
7701 "myapex",
7702 "//apex_available:platform",
7703 ],
7704 }
7705
7706 cc_prebuilt_library_shared {
7707 name: "myotherlib",
7708 srcs: ["prebuilt.so"],
7709 system_shared_libs: [],
7710 stl: "none",
7711 apex_available: [
7712 "myapex",
7713 "//apex_available:platform",
7714 ],
7715 }
7716 `)
7717
Jooyung Hana0503a52023-08-23 13:12:50 +09007718 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007719 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007720 var builder strings.Builder
7721 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7722 androidMk := builder.String()
7723 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007724 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007725 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7726 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7727 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007728 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 myotherlib:64\n")
Yo Chiange8128052020-07-23 20:09:18 +08007729}
7730
Jooyung Han643adc42020-02-27 13:50:06 +09007731func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007732 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007733 apex {
7734 name: "myapex",
7735 key: "myapex.key",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007736 binaries: ["mybin"],
7737 jni_libs: ["mylib", "mylib3", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007738 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007739 }
7740
7741 apex_key {
7742 name: "myapex.key",
7743 public_key: "testkey.avbpubkey",
7744 private_key: "testkey.pem",
7745 }
7746
7747 cc_library {
7748 name: "mylib",
7749 srcs: ["mylib.cpp"],
7750 shared_libs: ["mylib2"],
7751 system_shared_libs: [],
7752 stl: "none",
7753 apex_available: [ "myapex" ],
7754 }
7755
7756 cc_library {
7757 name: "mylib2",
7758 srcs: ["mylib.cpp"],
7759 system_shared_libs: [],
7760 stl: "none",
7761 apex_available: [ "myapex" ],
7762 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007763
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007764 // Used as both a JNI library and a regular shared library.
7765 cc_library {
7766 name: "mylib3",
7767 srcs: ["mylib.cpp"],
7768 system_shared_libs: [],
7769 stl: "none",
7770 apex_available: [ "myapex" ],
7771 }
7772
7773 cc_binary {
7774 name: "mybin",
7775 srcs: ["mybin.cpp"],
7776 shared_libs: ["mylib3"],
7777 system_shared_libs: [],
7778 stl: "none",
7779 apex_available: [ "myapex" ],
7780 }
7781
Jiyong Park34d5c332022-02-24 18:02:44 +09007782 rust_ffi_shared {
7783 name: "libfoo.rust",
7784 crate_name: "foo",
7785 srcs: ["foo.rs"],
7786 shared_libs: ["libfoo.shared_from_rust"],
7787 prefer_rlib: true,
7788 apex_available: ["myapex"],
7789 }
7790
7791 cc_library_shared {
7792 name: "libfoo.shared_from_rust",
7793 srcs: ["mylib.cpp"],
7794 system_shared_libs: [],
7795 stl: "none",
7796 stubs: {
7797 versions: ["10", "11", "12"],
7798 },
7799 }
7800
Jooyung Han643adc42020-02-27 13:50:06 +09007801 `)
7802
Jooyung Hana0503a52023-08-23 13:12:50 +09007803 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han643adc42020-02-27 13:50:06 +09007804 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007805 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so mylib3.so")
Jooyung Hana0503a52023-08-23 13:12:50 +09007806 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007807 "bin/mybin",
Jooyung Han643adc42020-02-27 13:50:06 +09007808 "lib64/mylib.so",
7809 "lib64/mylib2.so",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007810 "lib64/mylib3.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007811 "lib64/libfoo.rust.so",
7812 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7813 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007814 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007815
7816 // b/220397949
7817 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007818}
7819
Jooyung Han49f67012020-04-17 13:43:10 +09007820func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007821 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007822 apex {
7823 name: "myapex",
7824 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007825 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007826 }
7827 apex_key {
7828 name: "myapex.key",
7829 public_key: "testkey.avbpubkey",
7830 private_key: "testkey.pem",
7831 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007832 `,
7833 android.FixtureModifyConfig(func(config android.Config) {
7834 delete(config.Targets, android.Android)
7835 config.AndroidCommonTarget = android.Target{}
7836 }),
7837 )
Jooyung Han49f67012020-04-17 13:43:10 +09007838
7839 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7840 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7841 }
7842}
7843
Jiyong Parkbd159612020-02-28 15:22:21 +09007844func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007845 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007846 apex {
7847 name: "myapex",
7848 key: "myapex.key",
7849 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007850 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007851 }
7852
7853 apex_key {
7854 name: "myapex.key",
7855 public_key: "testkey.avbpubkey",
7856 private_key: "testkey.pem",
7857 }
7858
7859 android_app {
7860 name: "AppFoo",
7861 srcs: ["foo/bar/MyClass.java"],
7862 sdk_version: "none",
7863 system_modules: "none",
7864 apex_available: [ "myapex" ],
7865 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007866 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007867
Jooyung Hana0503a52023-08-23 13:12:50 +09007868 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07007869 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Jiyong Parkbd159612020-02-28 15:22:21 +09007870
7871 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007872 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 +09007873}
7874
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007875func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007876 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007877 apex {
7878 name: "myapex",
7879 key: "myapex.key",
7880 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007881 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007882 }
7883
7884 apex_key {
7885 name: "myapex.key",
7886 public_key: "testkey.avbpubkey",
7887 private_key: "testkey.pem",
7888 }
7889
7890 android_app_set {
7891 name: "AppSet",
7892 set: "AppSet.apks",
7893 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09007894 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crosscf371cc2020-11-13 11:48:42 -08007895 bundleConfigRule := mod.Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07007896 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007897 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7898 s := mod.Rule("apexRule").Args["copy_commands"]
7899 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007900 if len(copyCmds) != 4 {
7901 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007902 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007903 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7904 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007905 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7906 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007907
7908 // Ensure that canned_fs_config has an entry for the app set zip file
7909 generateFsRule := mod.Rule("generateFsConfig")
7910 cmd := generateFsRule.RuleParams.Command
7911 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007912}
7913
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007914func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007915 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007916 apex_set {
7917 name: "myapex",
7918 filename: "foo_v2.apex",
7919 sanitized: {
7920 none: { set: "myapex.apks", },
7921 hwaddress: { set: "myapex.hwasan.apks", },
7922 },
Paul Duffin24704672021-04-06 16:09:30 +01007923 }
7924 `
7925 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007926
Paul Duffin24704672021-04-06 16:09:30 +01007927 // Check that the extractor produces the correct output file from the correct input file.
Spandan Das3576e762024-01-03 18:57:03 +00007928 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007929
Spandan Das3576e762024-01-03 18:57:03 +00007930 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Paul Duffin24704672021-04-06 16:09:30 +01007931 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007932
Paul Duffin24704672021-04-06 16:09:30 +01007933 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7934
7935 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007936 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7937 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007938
7939 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007940}
7941
Pranav Guptaeba03b02022-09-27 00:27:08 +00007942func TestApexSetApksModuleAssignment(t *testing.T) {
7943 ctx := testApex(t, `
7944 apex_set {
7945 name: "myapex",
7946 set: ":myapex_apks_file",
7947 }
7948
7949 filegroup {
7950 name: "myapex_apks_file",
7951 srcs: ["myapex.apks"],
7952 }
7953 `)
7954
Spandan Das3576e762024-01-03 18:57:03 +00007955 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Pranav Guptaeba03b02022-09-27 00:27:08 +00007956
7957 // Check that the extractor produces the correct apks file from the input module
Spandan Das3576e762024-01-03 18:57:03 +00007958 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.apks"
Pranav Guptaeba03b02022-09-27 00:27:08 +00007959 extractedApex := m.Output(extractorOutput)
7960
7961 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7962}
7963
Paul Duffin89f570a2021-06-16 01:42:33 +01007964func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007965 t.Helper()
7966
Paul Duffin55607122021-03-30 23:32:51 +01007967 fs := android.MockFS{
7968 "a.java": nil,
7969 "a.jar": nil,
7970 "apex_manifest.json": nil,
7971 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007972 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007973 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7974 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7975 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007976 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007977 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007978
Paul Duffin55607122021-03-30 23:32:51 +01007979 errorHandler := android.FixtureExpectsNoErrors
7980 if errmsg != "" {
7981 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007982 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007983
Paul Duffin55607122021-03-30 23:32:51 +01007984 result := android.GroupFixturePreparers(
7985 cc.PrepareForTestWithCcDefaultModules,
7986 java.PrepareForTestWithHiddenApiBuildComponents,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01007987 java.PrepareForTestWithDexpreopt,
Paul Duffin55607122021-03-30 23:32:51 +01007988 java.PrepareForTestWithJavaSdkLibraryFiles,
7989 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007990 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007991 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007992 android.FixtureModifyMockFS(func(fs android.MockFS) {
7993 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7994 insert := ""
7995 for _, fragment := range fragments {
7996 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7997 }
7998 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7999 platform_bootclasspath {
8000 name: "platform-bootclasspath",
8001 fragments: [
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008002 {apex: "com.android.art", module: "art-bootclasspath-fragment"},
Paul Duffin89f570a2021-06-16 01:42:33 +01008003 %s
8004 ],
8005 }
8006 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01008007 }
Paul Duffin89f570a2021-06-16 01:42:33 +01008008 }),
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008009 // Dexpreopt for boot jars requires the ART boot image profile.
8010 java.PrepareApexBootJarModule("com.android.art", "core-oj"),
8011 dexpreopt.FixtureSetArtBootJars("com.android.art:core-oj"),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00008012 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01008013 ).
8014 ExtendWithErrorHandler(errorHandler).
8015 RunTestWithBp(t, bp)
8016
8017 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008018}
8019
Paul Duffin5556c5f2022-06-09 17:32:21 +00008020func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008021 preparers := android.GroupFixturePreparers(
8022 java.PrepareForTestWithJavaDefaultModules,
Spandan Das5be63332023-12-13 00:06:32 +00008023 prepareForTestWithBootclasspathFragment,
8024 dexpreopt.FixtureSetTestOnlyArtBootImageJars("com.android.art:libfoo"),
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008025 PrepareForTestWithApexBuildComponents,
8026 ).
8027 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
Spandan Das3576e762024-01-03 18:57:03 +00008028 "Multiple installable prebuilt APEXes provide ambiguous deapexers: prebuilt_com.android.art and prebuilt_com.mycompany.android.art"))
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008029
8030 bpBase := `
8031 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008032 name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008033 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008034 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008035 set: "myapex.apks",
8036 }
8037
8038 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008039 name: "com.mycompany.android.art",
8040 apex_name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008041 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008042 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008043 set: "company-myapex.apks",
8044 }
8045
8046 prebuilt_bootclasspath_fragment {
Spandan Das5be63332023-12-13 00:06:32 +00008047 name: "art-bootclasspath-fragment",
8048 apex_available: ["com.android.art"],
Spandan Dasfae468e2023-12-12 23:23:53 +00008049 hidden_api: {
8050 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8051 metadata: "my-bootclasspath-fragment/metadata.csv",
8052 index: "my-bootclasspath-fragment/index.csv",
8053 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
8054 all_flags: "my-bootclasspath-fragment/all-flags.csv",
8055 },
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008056 %s
8057 }
8058 `
8059
8060 t.Run("java_import", func(t *testing.T) {
8061 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8062 java_import {
8063 name: "libfoo",
8064 jars: ["libfoo.jar"],
Spandan Das5be63332023-12-13 00:06:32 +00008065 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008066 }
8067 `)
8068 })
8069
8070 t.Run("java_sdk_library_import", func(t *testing.T) {
8071 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8072 java_sdk_library_import {
8073 name: "libfoo",
8074 public: {
8075 jars: ["libbar.jar"],
8076 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008077 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008078 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008079 }
8080 `)
8081 })
8082
8083 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8084 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8085 image_name: "art",
8086 contents: ["libfoo"],
8087 `)+`
8088 java_sdk_library_import {
8089 name: "libfoo",
8090 public: {
8091 jars: ["libbar.jar"],
8092 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008093 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008094 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008095 }
8096 `)
8097 })
8098}
8099
Paul Duffin5556c5f2022-06-09 17:32:21 +00008100func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
8101 preparers := android.GroupFixturePreparers(
8102 java.PrepareForTestWithJavaDefaultModules,
8103 PrepareForTestWithApexBuildComponents,
8104 )
8105
Spandan Das59a4a2b2024-01-09 21:35:56 +00008106 errCtx := moduleErrorfTestCtx{}
8107
Paul Duffin5556c5f2022-06-09 17:32:21 +00008108 bpBase := `
8109 apex_set {
8110 name: "com.android.myapex",
8111 installable: true,
8112 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8113 set: "myapex.apks",
8114 }
8115
8116 apex_set {
8117 name: "com.android.myapex_compressed",
8118 apex_name: "com.android.myapex",
8119 installable: true,
8120 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8121 set: "myapex_compressed.apks",
8122 }
8123
8124 prebuilt_bootclasspath_fragment {
8125 name: "my-bootclasspath-fragment",
8126 apex_available: [
8127 "com.android.myapex",
8128 "com.android.myapex_compressed",
8129 ],
8130 hidden_api: {
8131 annotation_flags: "annotation-flags.csv",
8132 metadata: "metadata.csv",
8133 index: "index.csv",
8134 signature_patterns: "signature_patterns.csv",
8135 },
8136 %s
8137 }
8138 `
8139
8140 t.Run("java_import", func(t *testing.T) {
8141 result := preparers.RunTestWithBp(t,
8142 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8143 java_import {
8144 name: "libfoo",
8145 jars: ["libfoo.jar"],
8146 apex_available: [
8147 "com.android.myapex",
8148 "com.android.myapex_compressed",
8149 ],
8150 }
8151 `)
8152
8153 module := result.Module("libfoo", "android_common_com.android.myapex")
8154 usesLibraryDep := module.(java.UsesLibraryDependency)
8155 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008156 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008157 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008158 })
8159
8160 t.Run("java_sdk_library_import", func(t *testing.T) {
8161 result := preparers.RunTestWithBp(t,
8162 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8163 java_sdk_library_import {
8164 name: "libfoo",
8165 public: {
8166 jars: ["libbar.jar"],
8167 },
8168 apex_available: [
8169 "com.android.myapex",
8170 "com.android.myapex_compressed",
8171 ],
8172 compile_dex: true,
8173 }
8174 `)
8175
8176 module := result.Module("libfoo", "android_common_com.android.myapex")
8177 usesLibraryDep := module.(java.UsesLibraryDependency)
8178 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008179 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008180 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008181 })
8182
8183 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8184 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8185 image_name: "art",
8186 contents: ["libfoo"],
8187 `)+`
8188 java_sdk_library_import {
8189 name: "libfoo",
8190 public: {
8191 jars: ["libbar.jar"],
8192 },
8193 apex_available: [
8194 "com.android.myapex",
8195 "com.android.myapex_compressed",
8196 ],
8197 compile_dex: true,
8198 }
8199 `)
8200 })
8201}
8202
Jooyung Han548640b2020-04-27 12:10:30 +09008203func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
8204 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8205 apex {
8206 name: "myapex",
8207 key: "myapex.key",
8208 updatable: true,
8209 }
8210
8211 apex_key {
8212 name: "myapex.key",
8213 public_key: "testkey.avbpubkey",
8214 private_key: "testkey.pem",
8215 }
8216 `)
8217}
8218
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008219func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
8220 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8221 apex {
8222 name: "myapex",
8223 key: "myapex.key",
8224 }
8225
8226 apex_key {
8227 name: "myapex.key",
8228 public_key: "testkey.avbpubkey",
8229 private_key: "testkey.pem",
8230 }
8231 `)
8232}
8233
Jooyung Handfc864c2023-03-20 18:19:07 +09008234func Test_use_vndk_as_stable_shouldnt_be_used_for_updatable_vendor_apexes(t *testing.T) {
8235 testApexError(t, `"myapex" .*: use_vndk_as_stable: updatable APEXes can't use external VNDK libs`, `
Daniel Norman69109112021-12-02 12:52:42 -08008236 apex {
8237 name: "myapex",
8238 key: "myapex.key",
8239 updatable: true,
Jooyung Handfc864c2023-03-20 18:19:07 +09008240 use_vndk_as_stable: true,
Daniel Norman69109112021-12-02 12:52:42 -08008241 soc_specific: true,
8242 }
8243
8244 apex_key {
8245 name: "myapex.key",
8246 public_key: "testkey.avbpubkey",
8247 private_key: "testkey.pem",
8248 }
8249 `)
8250}
8251
Jooyung Han02873da2023-03-22 17:41:03 +09008252func Test_use_vndk_as_stable_shouldnt_be_used_with_min_sdk_version(t *testing.T) {
8253 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported when min_sdk_version is set`, `
8254 apex {
8255 name: "myapex",
8256 key: "myapex.key",
8257 updatable: false,
8258 min_sdk_version: "29",
8259 use_vndk_as_stable: true,
8260 vendor: true,
8261 }
8262
8263 apex_key {
8264 name: "myapex.key",
8265 public_key: "testkey.avbpubkey",
8266 private_key: "testkey.pem",
8267 }
8268 `)
8269}
8270
Jooyung Handfc864c2023-03-20 18:19:07 +09008271func Test_use_vndk_as_stable_shouldnt_be_used_for_non_vendor_apexes(t *testing.T) {
8272 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported for system/system_ext APEXes`, `
8273 apex {
8274 name: "myapex",
8275 key: "myapex.key",
8276 updatable: false,
8277 use_vndk_as_stable: true,
8278 }
8279
8280 apex_key {
8281 name: "myapex.key",
8282 public_key: "testkey.avbpubkey",
8283 private_key: "testkey.pem",
8284 }
8285 `)
8286}
8287
satayevb98371c2021-06-15 16:49:50 +01008288func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
8289 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
8290 apex {
8291 name: "myapex",
8292 key: "myapex.key",
8293 systemserverclasspath_fragments: [
8294 "mysystemserverclasspathfragment",
8295 ],
8296 min_sdk_version: "29",
8297 updatable: true,
8298 }
8299
8300 apex_key {
8301 name: "myapex.key",
8302 public_key: "testkey.avbpubkey",
8303 private_key: "testkey.pem",
8304 }
8305
8306 java_library {
8307 name: "foo",
8308 srcs: ["b.java"],
8309 min_sdk_version: "29",
8310 installable: true,
8311 apex_available: [
8312 "myapex",
8313 ],
8314 }
8315
8316 systemserverclasspath_fragment {
8317 name: "mysystemserverclasspathfragment",
8318 generate_classpaths_proto: false,
8319 contents: [
8320 "foo",
8321 ],
8322 apex_available: [
8323 "myapex",
8324 ],
8325 }
satayevabcd5972021-08-06 17:49:46 +01008326 `,
8327 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8328 )
satayevb98371c2021-06-15 16:49:50 +01008329}
8330
Paul Duffin064b70c2020-11-02 17:32:38 +00008331func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008332 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008333 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008334 fragment := java.ApexVariantReference{
8335 Apex: proptools.StringPtr("myapex"),
8336 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8337 }
8338
Paul Duffin064b70c2020-11-02 17:32:38 +00008339 testDexpreoptWithApexes(t, `
8340 prebuilt_apex {
8341 name: "myapex" ,
8342 arch: {
8343 arm64: {
8344 src: "myapex-arm64.apex",
8345 },
8346 arm: {
8347 src: "myapex-arm.apex",
8348 },
8349 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008350 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8351 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008352
Paul Duffin89f570a2021-06-16 01:42:33 +01008353 prebuilt_bootclasspath_fragment {
8354 name: "my-bootclasspath-fragment",
8355 contents: ["libfoo"],
8356 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008357 hidden_api: {
8358 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8359 metadata: "my-bootclasspath-fragment/metadata.csv",
8360 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008361 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8362 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8363 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008364 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008365 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008366
Paul Duffin89f570a2021-06-16 01:42:33 +01008367 java_import {
8368 name: "libfoo",
8369 jars: ["libfoo.jar"],
8370 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008371 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008372 }
8373 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008374 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008375}
8376
Spandan Dasf14e2542021-11-12 00:01:37 +00008377func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008378 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008379 bp += `
8380 apex_key {
8381 name: "myapex.key",
8382 public_key: "testkey.avbpubkey",
8383 private_key: "testkey.pem",
8384 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008385 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008386 "lib1/src/A.java": nil,
8387 "lib2/src/B.java": nil,
8388 "system/sepolicy/apex/myapex-file_contexts": nil,
8389 }
8390
Paul Duffin45338f02021-03-30 23:07:52 +01008391 errorHandler := android.FixtureExpectsNoErrors
8392 if errmsg != "" {
8393 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008394 }
Colin Crossae8600b2020-10-29 17:09:13 -07008395
Paul Duffin45338f02021-03-30 23:07:52 +01008396 android.GroupFixturePreparers(
8397 android.PrepareForTestWithAndroidBuildComponents,
8398 java.PrepareForTestWithJavaBuildComponents,
8399 PrepareForTestWithApexBuildComponents,
8400 android.PrepareForTestWithNeverallowRules(rules),
8401 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008402 apexBootJars := make([]string, 0, len(bootJars))
8403 for _, apexBootJar := range bootJars {
8404 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008405 }
satayevd604b212021-07-21 14:23:52 +01008406 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008407 }),
8408 fs.AddToFixture(),
8409 ).
8410 ExtendWithErrorHandler(errorHandler).
8411 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008412}
8413
8414func TestApexPermittedPackagesRules(t *testing.T) {
8415 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008416 name string
8417 expectedError string
8418 bp string
8419 bootJars []string
8420 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008421 }{
8422
8423 {
8424 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8425 expectedError: "",
8426 bp: `
8427 java_library {
8428 name: "bcp_lib1",
8429 srcs: ["lib1/src/*.java"],
8430 permitted_packages: ["foo.bar"],
8431 apex_available: ["myapex"],
8432 sdk_version: "none",
8433 system_modules: "none",
8434 }
8435 java_library {
8436 name: "nonbcp_lib2",
8437 srcs: ["lib2/src/*.java"],
8438 apex_available: ["myapex"],
8439 permitted_packages: ["a.b"],
8440 sdk_version: "none",
8441 system_modules: "none",
8442 }
8443 apex {
8444 name: "myapex",
8445 key: "myapex.key",
8446 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008447 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008448 }`,
8449 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008450 bcpPermittedPackages: map[string][]string{
8451 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008452 "foo.bar",
8453 },
8454 },
8455 },
8456 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008457 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008458 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 +01008459 bp: `
8460 java_library {
8461 name: "bcp_lib1",
8462 srcs: ["lib1/src/*.java"],
8463 apex_available: ["myapex"],
8464 permitted_packages: ["foo.bar"],
8465 sdk_version: "none",
8466 system_modules: "none",
8467 }
8468 java_library {
8469 name: "bcp_lib2",
8470 srcs: ["lib2/src/*.java"],
8471 apex_available: ["myapex"],
8472 permitted_packages: ["foo.bar", "bar.baz"],
8473 sdk_version: "none",
8474 system_modules: "none",
8475 }
8476 apex {
8477 name: "myapex",
8478 key: "myapex.key",
8479 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008480 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008481 }
8482 `,
8483 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008484 bcpPermittedPackages: map[string][]string{
8485 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008486 "foo.bar",
8487 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008488 "bcp_lib2": []string{
8489 "foo.bar",
8490 },
8491 },
8492 },
8493 {
8494 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8495 expectedError: "",
8496 bp: `
8497 java_library {
8498 name: "bcp_lib_restricted",
8499 srcs: ["lib1/src/*.java"],
8500 apex_available: ["myapex"],
8501 permitted_packages: ["foo.bar"],
8502 sdk_version: "none",
8503 min_sdk_version: "29",
8504 system_modules: "none",
8505 }
8506 java_library {
8507 name: "bcp_lib_unrestricted",
8508 srcs: ["lib2/src/*.java"],
8509 apex_available: ["myapex"],
8510 permitted_packages: ["foo.bar", "bar.baz"],
8511 sdk_version: "none",
8512 min_sdk_version: "29",
8513 system_modules: "none",
8514 }
8515 apex {
8516 name: "myapex",
8517 key: "myapex.key",
8518 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8519 updatable: true,
8520 min_sdk_version: "29",
8521 }
8522 `,
8523 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8524 bcpPermittedPackages: map[string][]string{
8525 "bcp_lib1_non_updateable": []string{
8526 "foo.bar",
8527 },
8528 // 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 +01008529 },
8530 },
8531 }
8532 for _, tc := range testcases {
8533 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008534 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8535 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008536 })
8537 }
8538}
8539
Jiyong Park62304bb2020-04-13 16:19:48 +09008540func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008541 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008542 apex {
8543 name: "myapex",
8544 key: "myapex.key",
8545 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008546 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008547 }
8548
8549 apex_key {
8550 name: "myapex.key",
8551 public_key: "testkey.avbpubkey",
8552 private_key: "testkey.pem",
8553 }
8554
8555 cc_library {
8556 name: "mylib",
8557 srcs: ["mylib.cpp"],
8558 system_shared_libs: [],
8559 stl: "none",
8560 stubs: {
8561 versions: ["1"],
8562 },
8563 apex_available: ["myapex"],
8564 }
8565
8566 cc_library {
8567 name: "myprivlib",
8568 srcs: ["mylib.cpp"],
8569 system_shared_libs: [],
8570 stl: "none",
8571 apex_available: ["myapex"],
8572 }
8573
8574
8575 cc_test {
8576 name: "mytest",
8577 gtest: false,
8578 srcs: ["mylib.cpp"],
8579 system_shared_libs: [],
8580 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008581 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008582 test_for: ["myapex"]
8583 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008584
8585 cc_library {
8586 name: "mytestlib",
8587 srcs: ["mylib.cpp"],
8588 system_shared_libs: [],
8589 shared_libs: ["mylib", "myprivlib"],
8590 stl: "none",
8591 test_for: ["myapex"],
8592 }
8593
8594 cc_benchmark {
8595 name: "mybench",
8596 srcs: ["mylib.cpp"],
8597 system_shared_libs: [],
8598 shared_libs: ["mylib", "myprivlib"],
8599 stl: "none",
8600 test_for: ["myapex"],
8601 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008602 `)
8603
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008604 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008605 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008606 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8607 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8608 }
8609
8610 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008611 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008612 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8613 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8614 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8615}
Jiyong Park46a512f2020-12-04 18:02:13 +09008616
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008617func TestIndirectTestFor(t *testing.T) {
8618 ctx := testApex(t, `
8619 apex {
8620 name: "myapex",
8621 key: "myapex.key",
8622 native_shared_libs: ["mylib", "myprivlib"],
8623 updatable: false,
8624 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008625
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008626 apex_key {
8627 name: "myapex.key",
8628 public_key: "testkey.avbpubkey",
8629 private_key: "testkey.pem",
8630 }
8631
8632 cc_library {
8633 name: "mylib",
8634 srcs: ["mylib.cpp"],
8635 system_shared_libs: [],
8636 stl: "none",
8637 stubs: {
8638 versions: ["1"],
8639 },
8640 apex_available: ["myapex"],
8641 }
8642
8643 cc_library {
8644 name: "myprivlib",
8645 srcs: ["mylib.cpp"],
8646 system_shared_libs: [],
8647 stl: "none",
8648 shared_libs: ["mylib"],
8649 apex_available: ["myapex"],
8650 }
8651
8652 cc_library {
8653 name: "mytestlib",
8654 srcs: ["mylib.cpp"],
8655 system_shared_libs: [],
8656 shared_libs: ["myprivlib"],
8657 stl: "none",
8658 test_for: ["myapex"],
8659 }
8660 `)
8661
8662 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008663 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008664 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8665 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8666 }
8667
8668 // The platform variant of mytestlib links to the platform variant of the
8669 // internal myprivlib.
8670 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8671
8672 // The platform variant of myprivlib links to the platform variant of mylib
8673 // and bypasses its stubs.
8674 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 +09008675}
8676
Martin Stjernholmec009002021-03-27 15:18:31 +00008677func TestTestForForLibInOtherApex(t *testing.T) {
8678 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8679 _ = testApex(t, `
8680 apex {
8681 name: "com.android.art",
8682 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008683 native_shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008684 updatable: false,
8685 }
8686
8687 apex {
8688 name: "com.android.art.debug",
8689 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008690 native_shared_libs: ["libnativebridge", "libnativebrdige_test"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008691 updatable: false,
8692 }
8693
8694 apex_key {
8695 name: "myapex.key",
8696 public_key: "testkey.avbpubkey",
8697 private_key: "testkey.pem",
8698 }
8699
8700 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008701 name: "libnativebridge",
8702 srcs: ["libnativebridge.cpp"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008703 system_shared_libs: [],
8704 stl: "none",
8705 stubs: {
8706 versions: ["1"],
8707 },
8708 apex_available: ["com.android.art", "com.android.art.debug"],
8709 }
8710
8711 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008712 name: "libnativebrdige_test",
Martin Stjernholmec009002021-03-27 15:18:31 +00008713 srcs: ["mylib.cpp"],
8714 system_shared_libs: [],
Spandan Das20fce2d2023-04-12 17:21:39 +00008715 shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008716 stl: "none",
8717 apex_available: ["com.android.art.debug"],
8718 test_for: ["com.android.art"],
8719 }
8720 `,
8721 android.MockFS{
8722 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8723 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8724 }.AddToFixture())
8725}
8726
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008727// TODO(jungjw): Move this to proptools
8728func intPtr(i int) *int {
8729 return &i
8730}
8731
8732func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008733 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008734 apex_set {
8735 name: "myapex",
8736 set: "myapex.apks",
8737 filename: "foo_v2.apex",
8738 overrides: ["foo"],
8739 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008740 `,
8741 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8742 variables.Platform_sdk_version = intPtr(30)
8743 }),
8744 android.FixtureModifyConfig(func(config android.Config) {
8745 config.Targets[android.Android] = []android.Target{
8746 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8747 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8748 }
8749 }),
8750 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008751
Spandan Das3576e762024-01-03 18:57:03 +00008752 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008753
8754 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008755 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008756 actual := extractedApex.Args["abis"]
8757 expected := "ARMEABI_V7A,ARM64_V8A"
8758 if actual != expected {
8759 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8760 }
8761 actual = extractedApex.Args["sdk-version"]
8762 expected = "30"
8763 if actual != expected {
8764 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8765 }
8766
Paul Duffin6717d882021-06-15 19:09:41 +01008767 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008768 a := m.Module().(*ApexSet)
8769 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008770 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008771 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8772 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8773 }
8774}
8775
Anton Hansson805e0a52022-11-25 14:06:46 +00008776func TestApexSet_NativeBridge(t *testing.T) {
8777 ctx := testApex(t, `
8778 apex_set {
8779 name: "myapex",
8780 set: "myapex.apks",
8781 filename: "foo_v2.apex",
8782 overrides: ["foo"],
8783 }
8784 `,
8785 android.FixtureModifyConfig(func(config android.Config) {
8786 config.Targets[android.Android] = []android.Target{
8787 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8788 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8789 }
8790 }),
8791 )
8792
Spandan Das3576e762024-01-03 18:57:03 +00008793 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Anton Hansson805e0a52022-11-25 14:06:46 +00008794
8795 // Check extract_apks tool parameters. No native bridge arch expected
8796 extractedApex := m.Output("extracted/myapex.apks")
8797 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8798}
8799
Jiyong Park7d95a512020-05-10 15:16:24 +09008800func TestNoStaticLinkingToStubsLib(t *testing.T) {
8801 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8802 apex {
8803 name: "myapex",
8804 key: "myapex.key",
8805 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008806 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008807 }
8808
8809 apex_key {
8810 name: "myapex.key",
8811 public_key: "testkey.avbpubkey",
8812 private_key: "testkey.pem",
8813 }
8814
8815 cc_library {
8816 name: "mylib",
8817 srcs: ["mylib.cpp"],
8818 static_libs: ["otherlib"],
8819 system_shared_libs: [],
8820 stl: "none",
8821 apex_available: [ "myapex" ],
8822 }
8823
8824 cc_library {
8825 name: "otherlib",
8826 srcs: ["mylib.cpp"],
8827 system_shared_libs: [],
8828 stl: "none",
8829 stubs: {
8830 versions: ["1", "2", "3"],
8831 },
8832 apex_available: [ "myapex" ],
8833 }
8834 `)
8835}
8836
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008837func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008838 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008839 apex {
8840 name: "myapex",
8841 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008842 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008843 custom_sign_tool: "sign_myapex",
8844 }
8845
8846 apex_key {
8847 name: "myapex.key",
8848 public_key: "testkey.avbpubkey",
8849 private_key: "testkey.pem",
8850 }
8851 `)
8852
Jooyung Han286957d2023-10-30 16:17:56 +09008853 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07008854 content := android.ContentFromFileRuleForTests(t, ctx, myapex.Output("apexkeys.txt"))
Jooyung Haneec1b3f2023-06-20 16:25:59 +09008855 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 +09008856}
8857
8858func TestApexKeysTxtOverrides(t *testing.T) {
8859 ctx := testApex(t, `
8860 apex {
8861 name: "myapex",
8862 key: "myapex.key",
8863 updatable: false,
8864 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008865 }
8866
8867 apex_key {
8868 name: "myapex.key",
8869 public_key: "testkey.avbpubkey",
8870 private_key: "testkey.pem",
8871 }
8872
8873 prebuilt_apex {
8874 name: "myapex",
8875 prefer: true,
8876 arch: {
8877 arm64: {
8878 src: "myapex-arm64.apex",
8879 },
8880 arm: {
8881 src: "myapex-arm.apex",
8882 },
8883 },
8884 }
8885
8886 apex_set {
8887 name: "myapex_set",
8888 set: "myapex.apks",
8889 filename: "myapex_set.apex",
8890 overrides: ["myapex"],
8891 }
8892 `)
8893
Colin Crossf61d03d2023-11-02 16:56:39 -07008894 content := android.ContentFromFileRuleForTests(t, ctx,
8895 ctx.ModuleForTests("myapex", "android_common_myapex").Output("apexkeys.txt"))
Jooyung Han286957d2023-10-30 16:17:56 +09008896 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 -07008897 content = android.ContentFromFileRuleForTests(t, ctx,
8898 ctx.ModuleForTests("myapex_set", "android_common_myapex_set").Output("apexkeys.txt"))
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008899 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 +09008900}
8901
Jooyung Han938b5932020-06-20 12:47:47 +09008902func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008903 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008904 apex {
8905 name: "myapex",
8906 key: "myapex.key",
8907 apps: ["app"],
8908 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008909 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008910 }
8911
8912 apex_key {
8913 name: "myapex.key",
8914 public_key: "testkey.avbpubkey",
8915 private_key: "testkey.pem",
8916 }
8917
8918 android_app {
8919 name: "app",
8920 srcs: ["foo/bar/MyClass.java"],
8921 package_name: "foo",
8922 sdk_version: "none",
8923 system_modules: "none",
8924 apex_available: [ "myapex" ],
8925 }
8926 `, withFiles(map[string][]byte{
8927 "sub/Android.bp": []byte(`
8928 override_apex {
8929 name: "override_myapex",
8930 base: "myapex",
8931 apps: ["override_app"],
8932 allowed_files: ":allowed",
8933 }
8934 // Overridable "path" property should be referenced indirectly
8935 filegroup {
8936 name: "allowed",
8937 srcs: ["allowed.txt"],
8938 }
8939 override_android_app {
8940 name: "override_app",
8941 base: "app",
8942 package_name: "bar",
8943 }
8944 `),
8945 }))
8946
Jooyung Hana0503a52023-08-23 13:12:50 +09008947 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008948 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8949 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8950 }
8951
Jooyung Hana0503a52023-08-23 13:12:50 +09008952 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008953 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8954 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8955 }
8956}
8957
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008958func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008959 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008960 apex {
8961 name: "myapex",
8962 key: "myapex.key",
8963 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008964 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008965 }
8966
8967 apex_key {
8968 name: "myapex.key",
8969 public_key: "testkey.avbpubkey",
8970 private_key: "testkey.pem",
8971 }
8972
8973 cc_library {
8974 name: "mylib",
8975 srcs: ["mylib.cpp"],
8976 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008977 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008978 },
8979 apex_available: ["myapex"],
8980 }
8981
8982 cc_prebuilt_library_shared {
8983 name: "mylib",
8984 prefer: false,
8985 srcs: ["prebuilt.so"],
8986 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008987 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008988 },
8989 apex_available: ["myapex"],
8990 }
8991 `)
8992}
8993
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008994func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008995 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008996 apex {
8997 name: "myapex",
8998 key: "myapex.key",
8999 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009000 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009001 }
9002 apex_key {
9003 name: "myapex.key",
9004 public_key: "testkey.avbpubkey",
9005 private_key: "testkey.pem",
9006 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00009007 `,
9008 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9009 variables.CompressedApex = proptools.BoolPtr(true)
9010 }),
9011 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009012
Jooyung Hana0503a52023-08-23 13:12:50 +09009013 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("compressRule")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009014 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
9015
Jooyung Hana0503a52023-08-23 13:12:50 +09009016 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex").Description("sign compressedApex")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009017 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
9018
9019 // Make sure output of bundle is .capex
Jooyung Hana0503a52023-08-23 13:12:50 +09009020 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009021 ensureContains(t, ab.outputFile.String(), "myapex.capex")
9022
9023 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07009024 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009025 var builder strings.Builder
9026 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9027 androidMk := builder.String()
9028 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
9029}
9030
Martin Stjernholm2856c662020-12-02 15:03:42 +00009031func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009032 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00009033 apex {
9034 name: "myapex",
9035 key: "myapex.key",
9036 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009037 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00009038 }
9039
9040 apex_key {
9041 name: "myapex.key",
9042 public_key: "testkey.avbpubkey",
9043 private_key: "testkey.pem",
9044 }
9045
9046 cc_library {
9047 name: "mylib",
9048 srcs: ["mylib.cpp"],
9049 apex_available: ["myapex"],
9050 shared_libs: ["otherlib"],
9051 system_shared_libs: [],
9052 }
9053
9054 cc_library {
9055 name: "otherlib",
9056 srcs: ["mylib.cpp"],
9057 stubs: {
9058 versions: ["current"],
9059 },
9060 }
9061
9062 cc_prebuilt_library_shared {
9063 name: "otherlib",
9064 prefer: true,
9065 srcs: ["prebuilt.so"],
9066 stubs: {
9067 versions: ["current"],
9068 },
9069 }
9070 `)
9071
Jooyung Hana0503a52023-08-23 13:12:50 +09009072 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07009073 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00009074 var builder strings.Builder
9075 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9076 androidMk := builder.String()
9077
9078 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
9079 // a thing there.
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009080 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 otherlib\n")
Martin Stjernholm2856c662020-12-02 15:03:42 +00009081}
9082
Jiyong Parke3867542020-12-03 17:28:25 +09009083func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009084 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09009085 apex {
9086 name: "myapex",
9087 key: "myapex.key",
9088 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009089 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09009090 }
9091
9092 apex_key {
9093 name: "myapex.key",
9094 public_key: "testkey.avbpubkey",
9095 private_key: "testkey.pem",
9096 }
9097
9098 cc_library {
9099 name: "mylib",
9100 srcs: ["mylib.cpp"],
9101 system_shared_libs: [],
9102 stl: "none",
9103 apex_available: ["myapex"],
9104 shared_libs: ["mylib2"],
9105 target: {
9106 apex: {
9107 exclude_shared_libs: ["mylib2"],
9108 },
9109 },
9110 }
9111
9112 cc_library {
9113 name: "mylib2",
9114 srcs: ["mylib.cpp"],
9115 system_shared_libs: [],
9116 stl: "none",
9117 }
9118 `)
9119
9120 // Check if mylib is linked to mylib2 for the non-apex target
9121 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
9122 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
9123
9124 // Make sure that the link doesn't occur for the apex target
9125 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
9126 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
9127
9128 // It shouldn't appear in the copy cmd as well.
Jooyung Hana0503a52023-08-23 13:12:50 +09009129 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule").Args["copy_commands"]
Jiyong Parke3867542020-12-03 17:28:25 +09009130 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
9131}
9132
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009133func TestPrebuiltStubLibDep(t *testing.T) {
9134 bpBase := `
9135 apex {
9136 name: "myapex",
9137 key: "myapex.key",
9138 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009139 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009140 }
9141 apex_key {
9142 name: "myapex.key",
9143 public_key: "testkey.avbpubkey",
9144 private_key: "testkey.pem",
9145 }
9146 cc_library {
9147 name: "mylib",
9148 srcs: ["mylib.cpp"],
9149 apex_available: ["myapex"],
9150 shared_libs: ["stublib"],
9151 system_shared_libs: [],
9152 }
9153 apex {
9154 name: "otherapex",
9155 enabled: %s,
9156 key: "myapex.key",
9157 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009158 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009159 }
9160 `
9161
9162 stublibSourceBp := `
9163 cc_library {
9164 name: "stublib",
9165 srcs: ["mylib.cpp"],
9166 apex_available: ["otherapex"],
9167 system_shared_libs: [],
9168 stl: "none",
9169 stubs: {
9170 versions: ["1"],
9171 },
9172 }
9173 `
9174
9175 stublibPrebuiltBp := `
9176 cc_prebuilt_library_shared {
9177 name: "stublib",
9178 srcs: ["prebuilt.so"],
9179 apex_available: ["otherapex"],
9180 stubs: {
9181 versions: ["1"],
9182 },
9183 %s
9184 }
9185 `
9186
9187 tests := []struct {
9188 name string
9189 stublibBp string
9190 usePrebuilt bool
9191 modNames []string // Modules to collect AndroidMkEntries for
9192 otherApexEnabled []string
9193 }{
9194 {
9195 name: "only_source",
9196 stublibBp: stublibSourceBp,
9197 usePrebuilt: false,
9198 modNames: []string{"stublib"},
9199 otherApexEnabled: []string{"true", "false"},
9200 },
9201 {
9202 name: "source_preferred",
9203 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9204 usePrebuilt: false,
9205 modNames: []string{"stublib", "prebuilt_stublib"},
9206 otherApexEnabled: []string{"true", "false"},
9207 },
9208 {
9209 name: "prebuilt_preferred",
9210 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9211 usePrebuilt: true,
9212 modNames: []string{"stublib", "prebuilt_stublib"},
9213 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9214 },
9215 {
9216 name: "only_prebuilt",
9217 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9218 usePrebuilt: true,
9219 modNames: []string{"stublib"},
9220 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9221 },
9222 }
9223
9224 for _, test := range tests {
9225 t.Run(test.name, func(t *testing.T) {
9226 for _, otherApexEnabled := range test.otherApexEnabled {
9227 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009228 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009229
9230 type modAndMkEntries struct {
9231 mod *cc.Module
9232 mkEntries android.AndroidMkEntries
9233 }
9234 entries := []*modAndMkEntries{}
9235
9236 // Gather shared lib modules that are installable
9237 for _, modName := range test.modNames {
9238 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9239 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9240 continue
9241 }
9242 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08009243 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009244 continue
9245 }
Colin Crossaa255532020-07-03 13:18:24 -07009246 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009247 if ent.Disabled {
9248 continue
9249 }
9250 entries = append(entries, &modAndMkEntries{
9251 mod: mod,
9252 mkEntries: ent,
9253 })
9254 }
9255 }
9256 }
9257
9258 var entry *modAndMkEntries = nil
9259 for _, ent := range entries {
9260 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9261 if entry != nil {
9262 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9263 } else {
9264 entry = ent
9265 }
9266 }
9267 }
9268
9269 if entry == nil {
9270 t.Errorf("AndroidMk entry for \"stublib\" missing")
9271 } else {
9272 isPrebuilt := entry.mod.Prebuilt() != nil
9273 if isPrebuilt != test.usePrebuilt {
9274 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9275 }
9276 if !entry.mod.IsStubs() {
9277 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9278 }
9279 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9280 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9281 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009282 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009283 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009284 if !android.InList(expected, cflags) {
9285 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9286 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009287 }
9288 })
9289 }
9290 })
9291 }
9292}
9293
Colin Crossc33e5212021-05-25 18:16:02 -07009294func TestApexJavaCoverage(t *testing.T) {
9295 bp := `
9296 apex {
9297 name: "myapex",
9298 key: "myapex.key",
9299 java_libs: ["mylib"],
9300 bootclasspath_fragments: ["mybootclasspathfragment"],
9301 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9302 updatable: false,
9303 }
9304
9305 apex_key {
9306 name: "myapex.key",
9307 public_key: "testkey.avbpubkey",
9308 private_key: "testkey.pem",
9309 }
9310
9311 java_library {
9312 name: "mylib",
9313 srcs: ["mylib.java"],
9314 apex_available: ["myapex"],
9315 compile_dex: true,
9316 }
9317
9318 bootclasspath_fragment {
9319 name: "mybootclasspathfragment",
9320 contents: ["mybootclasspathlib"],
9321 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009322 hidden_api: {
9323 split_packages: ["*"],
9324 },
Colin Crossc33e5212021-05-25 18:16:02 -07009325 }
9326
9327 java_library {
9328 name: "mybootclasspathlib",
9329 srcs: ["mybootclasspathlib.java"],
9330 apex_available: ["myapex"],
9331 compile_dex: true,
9332 }
9333
9334 systemserverclasspath_fragment {
9335 name: "mysystemserverclasspathfragment",
9336 contents: ["mysystemserverclasspathlib"],
9337 apex_available: ["myapex"],
9338 }
9339
9340 java_library {
9341 name: "mysystemserverclasspathlib",
9342 srcs: ["mysystemserverclasspathlib.java"],
9343 apex_available: ["myapex"],
9344 compile_dex: true,
9345 }
9346 `
9347
9348 result := android.GroupFixturePreparers(
9349 PrepareForTestWithApexBuildComponents,
9350 prepareForTestWithMyapex,
9351 java.PrepareForTestWithJavaDefaultModules,
9352 android.PrepareForTestWithAndroidBuildComponents,
9353 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009354 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9355 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009356 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009357 ).RunTest(t)
9358
9359 // Make sure jacoco ran on both mylib and mybootclasspathlib
9360 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9361 t.Errorf("Failed to find jacoco rule for mylib")
9362 }
9363 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9364 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9365 }
9366 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9367 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9368 }
9369}
9370
Jiyong Park192600a2021-08-03 07:52:17 +00009371func TestProhibitStaticExecutable(t *testing.T) {
9372 testApexError(t, `executable mybin is static`, `
9373 apex {
9374 name: "myapex",
9375 key: "myapex.key",
9376 binaries: ["mybin"],
9377 min_sdk_version: "29",
9378 }
9379
9380 apex_key {
9381 name: "myapex.key",
9382 public_key: "testkey.avbpubkey",
9383 private_key: "testkey.pem",
9384 }
9385
9386 cc_binary {
9387 name: "mybin",
9388 srcs: ["mylib.cpp"],
9389 relative_install_path: "foo/bar",
9390 static_executable: true,
9391 system_shared_libs: [],
9392 stl: "none",
9393 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009394 min_sdk_version: "29",
9395 }
9396 `)
9397
9398 testApexError(t, `executable mybin.rust is static`, `
9399 apex {
9400 name: "myapex",
9401 key: "myapex.key",
9402 binaries: ["mybin.rust"],
9403 min_sdk_version: "29",
9404 }
9405
9406 apex_key {
9407 name: "myapex.key",
9408 public_key: "testkey.avbpubkey",
9409 private_key: "testkey.pem",
9410 }
9411
9412 rust_binary {
9413 name: "mybin.rust",
9414 srcs: ["foo.rs"],
9415 static_executable: true,
9416 apex_available: ["myapex"],
9417 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009418 }
9419 `)
9420}
9421
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009422func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9423 ctx := testApex(t, `
9424 apex {
9425 name: "myapex",
9426 key: "myapex.key",
9427 updatable: false,
9428 java_libs: ["foo"],
9429 }
9430
9431 apex_key {
9432 name: "myapex.key",
9433 public_key: "testkey.avbpubkey",
9434 private_key: "testkey.pem",
9435 }
9436
9437 java_library {
9438 name: "foo",
9439 srcs: ["foo.java"],
9440 apex_available: ["myapex"],
9441 installable: true,
9442 }
9443 `,
9444 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9445 )
9446
Jooyung Hana0503a52023-08-23 13:12:50 +09009447 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009448 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9449 var builder strings.Builder
9450 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9451 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009452 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 +00009453}
9454
9455func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9456 ctx := testApex(t, `
9457 prebuilt_apex {
9458 name: "myapex",
9459 arch: {
9460 arm64: {
9461 src: "myapex-arm64.apex",
9462 },
9463 arm: {
9464 src: "myapex-arm.apex",
9465 },
9466 },
9467 exported_java_libs: ["foo"],
9468 }
9469
9470 java_import {
9471 name: "foo",
9472 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009473 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009474 }
9475 `,
9476 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9477 )
9478
9479 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9480 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9481 mainModuleEntries := entriesList[0]
9482 android.AssertArrayString(t,
9483 "LOCAL_REQUIRED_MODULES",
9484 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9485 []string{
9486 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9487 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9488 })
9489}
9490
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009491func TestAndroidMk_RequiredModules(t *testing.T) {
9492 ctx := testApex(t, `
9493 apex {
9494 name: "myapex",
9495 key: "myapex.key",
9496 updatable: false,
9497 java_libs: ["foo"],
9498 required: ["otherapex"],
9499 }
9500
9501 apex {
9502 name: "otherapex",
9503 key: "myapex.key",
9504 updatable: false,
9505 java_libs: ["foo"],
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009506 }
9507
9508 apex_key {
9509 name: "myapex.key",
9510 public_key: "testkey.avbpubkey",
9511 private_key: "testkey.pem",
9512 }
9513
9514 java_library {
9515 name: "foo",
9516 srcs: ["foo.java"],
9517 apex_available: ["myapex", "otherapex"],
9518 installable: true,
9519 }
9520 `)
9521
Jooyung Hana0503a52023-08-23 13:12:50 +09009522 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009523 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9524 var builder strings.Builder
9525 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9526 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009527 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009528}
9529
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009530func TestAndroidMk_RequiredDeps(t *testing.T) {
9531 ctx := testApex(t, `
9532 apex {
9533 name: "myapex",
9534 key: "myapex.key",
9535 updatable: false,
9536 }
9537
9538 apex_key {
9539 name: "myapex.key",
9540 public_key: "testkey.avbpubkey",
9541 private_key: "testkey.pem",
9542 }
9543 `)
9544
Jooyung Hana0503a52023-08-23 13:12:50 +09009545 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009546 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009547 data := android.AndroidMkDataForTest(t, ctx, bundle)
9548 var builder strings.Builder
9549 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9550 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009551 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009552}
9553
Jooyung Hana6d36672022-02-24 13:58:07 +09009554func TestApexOutputFileProducer(t *testing.T) {
9555 for _, tc := range []struct {
9556 name string
9557 ref string
9558 expected_data []string
9559 }{
9560 {
9561 name: "test_using_output",
9562 ref: ":myapex",
Jooyung Hana0503a52023-08-23 13:12:50 +09009563 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.capex:myapex.capex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009564 },
9565 {
9566 name: "test_using_apex",
9567 ref: ":myapex{.apex}",
Jooyung Hana0503a52023-08-23 13:12:50 +09009568 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.apex:myapex.apex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009569 },
9570 } {
9571 t.Run(tc.name, func(t *testing.T) {
9572 ctx := testApex(t, `
9573 apex {
9574 name: "myapex",
9575 key: "myapex.key",
9576 compressible: true,
9577 updatable: false,
9578 }
9579
9580 apex_key {
9581 name: "myapex.key",
9582 public_key: "testkey.avbpubkey",
9583 private_key: "testkey.pem",
9584 }
9585
9586 java_test {
9587 name: "`+tc.name+`",
9588 srcs: ["a.java"],
9589 data: ["`+tc.ref+`"],
9590 }
9591 `,
9592 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9593 variables.CompressedApex = proptools.BoolPtr(true)
9594 }))
9595 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9596 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9597 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9598 })
9599 }
9600}
9601
satayev758968a2021-12-06 11:42:40 +00009602func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9603 preparer := android.GroupFixturePreparers(
9604 PrepareForTestWithApexBuildComponents,
9605 prepareForTestWithMyapex,
9606 java.PrepareForTestWithJavaSdkLibraryFiles,
9607 java.PrepareForTestWithJavaDefaultModules,
9608 android.PrepareForTestWithAndroidBuildComponents,
9609 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9610 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9611 )
9612
9613 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9614 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9615 preparer.RunTestWithBp(t, `
9616 apex {
9617 name: "myapex",
9618 key: "myapex.key",
9619 bootclasspath_fragments: ["mybootclasspathfragment"],
9620 min_sdk_version: "30",
9621 updatable: false,
9622 }
9623
9624 apex_key {
9625 name: "myapex.key",
9626 public_key: "testkey.avbpubkey",
9627 private_key: "testkey.pem",
9628 }
9629
9630 bootclasspath_fragment {
9631 name: "mybootclasspathfragment",
9632 contents: ["mybootclasspathlib"],
9633 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009634 hidden_api: {
9635 split_packages: ["*"],
9636 },
satayev758968a2021-12-06 11:42:40 +00009637 }
9638
9639 java_sdk_library {
9640 name: "mybootclasspathlib",
9641 srcs: ["mybootclasspathlib.java"],
9642 apex_available: ["myapex"],
9643 compile_dex: true,
9644 unsafe_ignore_missing_latest_api: true,
9645 min_sdk_version: "31",
9646 static_libs: ["util"],
9647 }
9648
9649 java_library {
9650 name: "util",
9651 srcs: ["a.java"],
9652 apex_available: ["myapex"],
9653 min_sdk_version: "31",
9654 static_libs: ["another_util"],
9655 }
9656
9657 java_library {
9658 name: "another_util",
9659 srcs: ["a.java"],
9660 min_sdk_version: "31",
9661 apex_available: ["myapex"],
9662 }
9663 `)
9664 })
9665
9666 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9667 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9668 preparer.RunTestWithBp(t, `
9669 apex {
9670 name: "myapex",
9671 key: "myapex.key",
9672 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9673 min_sdk_version: "30",
9674 updatable: false,
9675 }
9676
9677 apex_key {
9678 name: "myapex.key",
9679 public_key: "testkey.avbpubkey",
9680 private_key: "testkey.pem",
9681 }
9682
9683 systemserverclasspath_fragment {
9684 name: "mysystemserverclasspathfragment",
9685 contents: ["mysystemserverclasspathlib"],
9686 apex_available: ["myapex"],
9687 }
9688
9689 java_sdk_library {
9690 name: "mysystemserverclasspathlib",
9691 srcs: ["mysystemserverclasspathlib.java"],
9692 apex_available: ["myapex"],
9693 compile_dex: true,
9694 min_sdk_version: "32",
9695 unsafe_ignore_missing_latest_api: true,
9696 static_libs: ["util"],
9697 }
9698
9699 java_library {
9700 name: "util",
9701 srcs: ["a.java"],
9702 apex_available: ["myapex"],
9703 min_sdk_version: "31",
9704 static_libs: ["another_util"],
9705 }
9706
9707 java_library {
9708 name: "another_util",
9709 srcs: ["a.java"],
9710 min_sdk_version: "31",
9711 apex_available: ["myapex"],
9712 }
9713 `)
9714 })
9715
9716 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9717 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9718 RunTestWithBp(t, `
9719 apex {
9720 name: "myapex",
9721 key: "myapex.key",
9722 bootclasspath_fragments: ["mybootclasspathfragment"],
9723 min_sdk_version: "30",
9724 updatable: false,
9725 }
9726
9727 apex_key {
9728 name: "myapex.key",
9729 public_key: "testkey.avbpubkey",
9730 private_key: "testkey.pem",
9731 }
9732
9733 bootclasspath_fragment {
9734 name: "mybootclasspathfragment",
9735 contents: ["mybootclasspathlib"],
9736 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009737 hidden_api: {
9738 split_packages: ["*"],
9739 },
satayev758968a2021-12-06 11:42:40 +00009740 }
9741
9742 java_sdk_library {
9743 name: "mybootclasspathlib",
9744 srcs: ["mybootclasspathlib.java"],
9745 apex_available: ["myapex"],
9746 compile_dex: true,
9747 unsafe_ignore_missing_latest_api: true,
9748 }
9749 `)
9750 })
9751
9752 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9753 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9754 RunTestWithBp(t, `
9755 apex {
9756 name: "myapex",
9757 key: "myapex.key",
9758 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9759 min_sdk_version: "30",
9760 updatable: false,
9761 }
9762
9763 apex_key {
9764 name: "myapex.key",
9765 public_key: "testkey.avbpubkey",
9766 private_key: "testkey.pem",
9767 }
9768
9769 systemserverclasspath_fragment {
9770 name: "mysystemserverclasspathfragment",
9771 contents: ["mysystemserverclasspathlib"],
9772 apex_available: ["myapex"],
9773 }
9774
9775 java_sdk_library {
9776 name: "mysystemserverclasspathlib",
9777 srcs: ["mysystemserverclasspathlib.java"],
9778 apex_available: ["myapex"],
9779 compile_dex: true,
9780 unsafe_ignore_missing_latest_api: true,
9781 }
9782 `)
9783 })
9784}
9785
Jiakai Zhang6decef92022-01-12 17:56:19 +00009786// Verifies that the APEX depends on all the Make modules in the list.
9787func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9788 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9789 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009790 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009791 }
9792}
9793
9794// Verifies that the APEX does not depend on any of the Make modules in the list.
9795func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9796 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9797 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009798 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009799 }
9800}
9801
Cole Faust1021ccd2023-02-26 21:15:25 -08009802// TODO(b/193460475): Re-enable this test
9803//func TestApexStrictUpdtabilityLint(t *testing.T) {
9804// bpTemplate := `
9805// apex {
9806// name: "myapex",
9807// key: "myapex.key",
9808// java_libs: ["myjavalib"],
9809// updatable: %v,
9810// min_sdk_version: "29",
9811// }
9812// apex_key {
9813// name: "myapex.key",
9814// }
9815// java_library {
9816// name: "myjavalib",
9817// srcs: ["MyClass.java"],
9818// apex_available: [ "myapex" ],
9819// lint: {
9820// strict_updatability_linting: %v,
9821// },
9822// sdk_version: "current",
9823// min_sdk_version: "29",
9824// }
9825// `
9826// fs := android.MockFS{
9827// "lint-baseline.xml": nil,
9828// }
9829//
9830// testCases := []struct {
9831// testCaseName string
9832// apexUpdatable bool
9833// javaStrictUpdtabilityLint bool
9834// lintFileExists bool
9835// disallowedFlagExpected bool
9836// }{
9837// {
9838// testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9839// apexUpdatable: true,
9840// javaStrictUpdtabilityLint: true,
9841// lintFileExists: false,
9842// disallowedFlagExpected: false,
9843// },
9844// {
9845// testCaseName: "non-updatable apex respects strict_updatability of javalib",
9846// apexUpdatable: false,
9847// javaStrictUpdtabilityLint: false,
9848// lintFileExists: true,
9849// disallowedFlagExpected: false,
9850// },
9851// {
9852// testCaseName: "non-updatable apex respects strict updatability of javalib",
9853// apexUpdatable: false,
9854// javaStrictUpdtabilityLint: true,
9855// lintFileExists: true,
9856// disallowedFlagExpected: true,
9857// },
9858// {
9859// testCaseName: "updatable apex sets strict updatability of javalib to true",
9860// apexUpdatable: true,
9861// javaStrictUpdtabilityLint: false, // will be set to true by mutator
9862// lintFileExists: true,
9863// disallowedFlagExpected: true,
9864// },
9865// }
9866//
9867// for _, testCase := range testCases {
9868// bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9869// fixtures := []android.FixturePreparer{}
9870// if testCase.lintFileExists {
9871// fixtures = append(fixtures, fs.AddToFixture())
9872// }
9873//
9874// result := testApex(t, bp, fixtures...)
9875// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9876// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9877// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9878//
9879// if disallowedFlagActual != testCase.disallowedFlagExpected {
9880// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9881// }
9882// }
9883//}
9884//
9885//func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9886// bp := `
9887// apex {
9888// name: "myapex",
9889// key: "myapex.key",
9890// java_libs: ["myjavalib"],
9891// updatable: true,
9892// min_sdk_version: "29",
9893// }
9894// apex_key {
9895// name: "myapex.key",
9896// }
9897// java_library {
9898// name: "myjavalib",
9899// srcs: ["MyClass.java"],
9900// apex_available: [ "myapex" ],
9901// sdk_version: "current",
9902// min_sdk_version: "29",
9903// }
9904// `
9905//
9906// testCases := []struct {
9907// testCaseName string
9908// moduleDirectory string
9909// disallowedFlagExpected bool
9910// }{
9911// {
9912// testCaseName: "lintable module defined outside libcore",
9913// moduleDirectory: "",
9914// disallowedFlagExpected: true,
9915// },
9916// {
9917// testCaseName: "lintable module defined in libcore root directory",
9918// moduleDirectory: "libcore/",
9919// disallowedFlagExpected: false,
9920// },
9921// {
9922// testCaseName: "lintable module defined in libcore child directory",
9923// moduleDirectory: "libcore/childdir/",
9924// disallowedFlagExpected: true,
9925// },
9926// }
9927//
9928// for _, testCase := range testCases {
9929// lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9930// bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9931// result := testApex(t, "", lintFileCreator, bpFileCreator)
9932// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9933// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9934// cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9935// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9936//
9937// if disallowedFlagActual != testCase.disallowedFlagExpected {
9938// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9939// }
9940// }
9941//}
9942//
9943//// checks transtive deps of an apex coming from bootclasspath_fragment
9944//func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9945// bp := `
9946// apex {
9947// name: "myapex",
9948// key: "myapex.key",
9949// bootclasspath_fragments: ["mybootclasspathfragment"],
9950// updatable: true,
9951// min_sdk_version: "29",
9952// }
9953// apex_key {
9954// name: "myapex.key",
9955// }
9956// bootclasspath_fragment {
9957// name: "mybootclasspathfragment",
9958// contents: ["myjavalib"],
9959// apex_available: ["myapex"],
9960// hidden_api: {
9961// split_packages: ["*"],
9962// },
9963// }
9964// java_library {
9965// name: "myjavalib",
9966// srcs: ["MyClass.java"],
9967// apex_available: [ "myapex" ],
9968// sdk_version: "current",
9969// min_sdk_version: "29",
9970// compile_dex: true,
9971// }
9972// `
9973// fs := android.MockFS{
9974// "lint-baseline.xml": nil,
9975// }
9976//
9977// result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9978// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9979// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9980// if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9981// t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9982// }
9983//}
Spandan Das66773252022-01-15 00:23:18 +00009984
Spandan Das42e89502022-05-06 22:12:55 +00009985// updatable apexes should propagate updatable=true to its apps
9986func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9987 bp := `
9988 apex {
9989 name: "myapex",
9990 key: "myapex.key",
9991 updatable: %v,
9992 apps: [
9993 "myapp",
9994 ],
9995 min_sdk_version: "30",
9996 }
9997 apex_key {
9998 name: "myapex.key",
9999 }
10000 android_app {
10001 name: "myapp",
10002 updatable: %v,
10003 apex_available: [
10004 "myapex",
10005 ],
10006 sdk_version: "current",
10007 min_sdk_version: "30",
10008 }
10009 `
10010 testCases := []struct {
10011 name string
10012 apex_is_updatable_bp bool
10013 app_is_updatable_bp bool
10014 app_is_updatable_expected bool
10015 }{
10016 {
10017 name: "Non-updatable apex respects updatable property of non-updatable app",
10018 apex_is_updatable_bp: false,
10019 app_is_updatable_bp: false,
10020 app_is_updatable_expected: false,
10021 },
10022 {
10023 name: "Non-updatable apex respects updatable property of updatable app",
10024 apex_is_updatable_bp: false,
10025 app_is_updatable_bp: true,
10026 app_is_updatable_expected: true,
10027 },
10028 {
10029 name: "Updatable apex respects updatable property of updatable app",
10030 apex_is_updatable_bp: true,
10031 app_is_updatable_bp: true,
10032 app_is_updatable_expected: true,
10033 },
10034 {
10035 name: "Updatable apex sets updatable=true on non-updatable app",
10036 apex_is_updatable_bp: true,
10037 app_is_updatable_bp: false,
10038 app_is_updatable_expected: true,
10039 },
10040 }
10041 for _, testCase := range testCases {
10042 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
10043 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
10044 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
10045 }
10046}
10047
Kiyoung Kim487689e2022-07-26 09:48:22 +090010048func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10049 bp := `
10050 apex {
10051 name: "myapex",
10052 key: "myapex.key",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010053 native_shared_libs: ["libbaz"],
10054 binaries: ["binfoo"],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010055 min_sdk_version: "29",
10056 }
10057 apex_key {
10058 name: "myapex.key",
10059 }
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010060 cc_binary {
10061 name: "binfoo",
10062 shared_libs: ["libbar", "libbaz", "libqux",],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010063 apex_available: ["myapex"],
10064 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010065 recovery_available: false,
10066 }
10067 cc_library {
10068 name: "libbar",
10069 srcs: ["libbar.cc"],
10070 stubs: {
10071 symbol_file: "libbar.map.txt",
10072 versions: [
10073 "29",
10074 ],
10075 },
10076 }
10077 cc_library {
10078 name: "libbaz",
10079 srcs: ["libbaz.cc"],
10080 apex_available: ["myapex"],
10081 min_sdk_version: "29",
10082 stubs: {
10083 symbol_file: "libbaz.map.txt",
10084 versions: [
10085 "29",
10086 ],
10087 },
Kiyoung Kim487689e2022-07-26 09:48:22 +090010088 }
10089 cc_api_library {
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010090 name: "libbar",
10091 src: "libbar_stub.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010092 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010093 variants: ["apex.29"],
10094 }
10095 cc_api_variant {
10096 name: "libbar",
10097 variant: "apex",
10098 version: "29",
10099 src: "libbar_apex_29.so",
10100 }
10101 cc_api_library {
10102 name: "libbaz",
10103 src: "libbaz_stub.so",
10104 min_sdk_version: "29",
10105 variants: ["apex.29"],
10106 }
10107 cc_api_variant {
10108 name: "libbaz",
10109 variant: "apex",
10110 version: "29",
10111 src: "libbaz_apex_29.so",
10112 }
10113 cc_api_library {
10114 name: "libqux",
10115 src: "libqux_stub.so",
10116 min_sdk_version: "29",
10117 variants: ["apex.29"],
10118 }
10119 cc_api_variant {
10120 name: "libqux",
10121 variant: "apex",
10122 version: "29",
10123 src: "libqux_apex_29.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010124 }
10125 api_imports {
10126 name: "api_imports",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010127 apex_shared_libs: [
10128 "libbar",
10129 "libbaz",
10130 "libqux",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010131 ],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010132 }
10133 `
10134 result := testApex(t, bp)
10135
10136 hasDep := func(m android.Module, wantDep android.Module) bool {
10137 t.Helper()
10138 var found bool
10139 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10140 if dep == wantDep {
10141 found = true
10142 }
10143 })
10144 return found
10145 }
10146
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010147 // Library defines stubs and cc_api_library should be used with cc_api_library
10148 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Module()
10149 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10150 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
Kiyoung Kim487689e2022-07-26 09:48:22 +090010151
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010152 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10153 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
Kiyoung Kim487689e2022-07-26 09:48:22 +090010154
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010155 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Rule("ld").Args["libFlags"]
10156 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10157 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10158 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
10159
10160 // Library defined in the same APEX should be linked with original definition instead of cc_api_library
10161 libbazApexVariant := result.ModuleForTests("libbaz", "android_arm64_armv8-a_shared_apex29").Module()
10162 libbazApiImportCoreVariant := result.ModuleForTests("libbaz.apiimport", "android_arm64_armv8-a_shared").Module()
10163 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even from same APEX", true, hasDep(binfooApexVariant, libbazApiImportCoreVariant))
10164 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbazApexVariant))
10165
10166 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbaz.so")
10167 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbaz.apiimport.so")
10168 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbaz.apex.29.apiimport.so")
10169
10170 // cc_api_library defined without original library should be linked with cc_api_library
10171 libquxApiImportApexVariant := result.ModuleForTests("libqux.apiimport", "android_arm64_armv8-a_shared").Module()
10172 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even original library definition does not exist", true, hasDep(binfooApexVariant, libquxApiImportApexVariant))
10173 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libqux.apex.29.apiimport.so")
10174}
10175
10176func TestPlatformBinaryBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10177 bp := `
10178 apex {
10179 name: "myapex",
10180 key: "myapex.key",
10181 native_shared_libs: ["libbar"],
10182 min_sdk_version: "29",
10183 }
10184 apex_key {
10185 name: "myapex.key",
10186 }
10187 cc_binary {
10188 name: "binfoo",
10189 shared_libs: ["libbar"],
10190 recovery_available: false,
10191 }
10192 cc_library {
10193 name: "libbar",
10194 srcs: ["libbar.cc"],
10195 apex_available: ["myapex"],
10196 min_sdk_version: "29",
10197 stubs: {
10198 symbol_file: "libbar.map.txt",
10199 versions: [
10200 "29",
10201 ],
10202 },
10203 }
10204 cc_api_library {
10205 name: "libbar",
10206 src: "libbar_stub.so",
10207 variants: ["apex.29"],
10208 }
10209 cc_api_variant {
10210 name: "libbar",
10211 variant: "apex",
10212 version: "29",
10213 src: "libbar_apex_29.so",
10214 }
10215 api_imports {
10216 name: "api_imports",
10217 apex_shared_libs: [
10218 "libbar",
10219 ],
10220 }
10221 `
10222
10223 result := testApex(t, bp)
10224
10225 hasDep := func(m android.Module, wantDep android.Module) bool {
10226 t.Helper()
10227 var found bool
10228 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10229 if dep == wantDep {
10230 found = true
10231 }
10232 })
10233 return found
10234 }
10235
10236 // Library defines stubs and cc_api_library should be used with cc_api_library
10237 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Module()
10238 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10239 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
10240
10241 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10242 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
10243
10244 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
10245 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10246 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10247 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
Kiyoung Kim487689e2022-07-26 09:48:22 +090010248}
Dennis Shend4f5d932023-01-31 20:27:21 +000010249
10250func TestTrimmedApex(t *testing.T) {
10251 bp := `
10252 apex {
10253 name: "myapex",
10254 key: "myapex.key",
10255 native_shared_libs: ["libfoo","libbaz"],
10256 min_sdk_version: "29",
10257 trim_against: "mydcla",
10258 }
10259 apex {
10260 name: "mydcla",
10261 key: "myapex.key",
10262 native_shared_libs: ["libfoo","libbar"],
10263 min_sdk_version: "29",
10264 file_contexts: ":myapex-file_contexts",
10265 dynamic_common_lib_apex: true,
10266 }
10267 apex_key {
10268 name: "myapex.key",
10269 }
10270 cc_library {
10271 name: "libfoo",
10272 shared_libs: ["libc"],
10273 apex_available: ["myapex","mydcla"],
10274 min_sdk_version: "29",
10275 }
10276 cc_library {
10277 name: "libbar",
10278 shared_libs: ["libc"],
10279 apex_available: ["myapex","mydcla"],
10280 min_sdk_version: "29",
10281 }
10282 cc_library {
10283 name: "libbaz",
10284 shared_libs: ["libc"],
10285 apex_available: ["myapex","mydcla"],
10286 min_sdk_version: "29",
10287 }
10288 cc_api_library {
10289 name: "libc",
10290 src: "libc.so",
10291 min_sdk_version: "29",
10292 recovery_available: true,
Ivan Lozanoadd122a2023-07-13 11:01:41 -040010293 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +090010294 product_available: true,
Dennis Shend4f5d932023-01-31 20:27:21 +000010295 }
10296 api_imports {
10297 name: "api_imports",
10298 shared_libs: [
10299 "libc",
10300 ],
10301 header_libs: [],
10302 }
10303 `
10304 ctx := testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +090010305 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dennis Shend4f5d932023-01-31 20:27:21 +000010306 apexRule := module.MaybeRule("apexRule")
10307 if apexRule.Rule == nil {
10308 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10309 }
10310
10311 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
Jooyung Hana0503a52023-08-23 13:12:50 +090010312 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("TrimmedApexRule")
Dennis Shend4f5d932023-01-31 20:27:21 +000010313 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10314 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10315 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10316 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10317}
Jingwen Chendea7a642023-03-28 11:30:50 +000010318
10319func TestCannedFsConfig(t *testing.T) {
10320 ctx := testApex(t, `
10321 apex {
10322 name: "myapex",
10323 key: "myapex.key",
10324 updatable: false,
10325 }
10326
10327 apex_key {
10328 name: "myapex.key",
10329 public_key: "testkey.avbpubkey",
10330 private_key: "testkey.pem",
10331 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010332 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010333 generateFsRule := mod.Rule("generateFsConfig")
10334 cmd := generateFsRule.RuleParams.Command
10335
10336 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10337}
10338
10339func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10340 ctx := testApex(t, `
10341 apex {
10342 name: "myapex",
10343 key: "myapex.key",
10344 canned_fs_config: "my_config",
10345 updatable: false,
10346 }
10347
10348 apex_key {
10349 name: "myapex.key",
10350 public_key: "testkey.avbpubkey",
10351 private_key: "testkey.pem",
10352 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010353 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010354 generateFsRule := mod.Rule("generateFsConfig")
10355 cmd := generateFsRule.RuleParams.Command
10356
10357 // Ensure that canned_fs_config has "cat my_config" at the end
10358 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10359}
Spandan Das20fce2d2023-04-12 17:21:39 +000010360
10361func TestStubLibrariesMultipleApexViolation(t *testing.T) {
10362 testCases := []struct {
10363 desc string
10364 hasStubs bool
10365 apexAvailable string
10366 expectedError string
10367 }{
10368 {
10369 desc: "non-stub library can have multiple apex_available",
10370 hasStubs: false,
10371 apexAvailable: `["myapex", "otherapex"]`,
10372 },
10373 {
10374 desc: "stub library should not be available to anyapex",
10375 hasStubs: true,
10376 apexAvailable: `["//apex_available:anyapex"]`,
10377 expectedError: "Stub libraries should have a single apex_available.*anyapex",
10378 },
10379 {
10380 desc: "stub library should not be available to multiple apexes",
10381 hasStubs: true,
10382 apexAvailable: `["myapex", "otherapex"]`,
10383 expectedError: "Stub libraries should have a single apex_available.*myapex.*otherapex",
10384 },
10385 {
10386 desc: "stub library can be available to a core apex and a test apex",
10387 hasStubs: true,
10388 apexAvailable: `["myapex", "test_myapex"]`,
10389 },
10390 }
10391 bpTemplate := `
10392 cc_library {
10393 name: "libfoo",
10394 %v
10395 apex_available: %v,
10396 }
10397 apex {
10398 name: "myapex",
10399 key: "apex.key",
10400 updatable: false,
10401 native_shared_libs: ["libfoo"],
10402 }
10403 apex {
10404 name: "otherapex",
10405 key: "apex.key",
10406 updatable: false,
10407 }
10408 apex_test {
10409 name: "test_myapex",
10410 key: "apex.key",
10411 updatable: false,
10412 native_shared_libs: ["libfoo"],
10413 }
10414 apex_key {
10415 name: "apex.key",
10416 }
10417 `
10418 for _, tc := range testCases {
10419 stubs := ""
10420 if tc.hasStubs {
10421 stubs = `stubs: {symbol_file: "libfoo.map.txt"},`
10422 }
10423 bp := fmt.Sprintf(bpTemplate, stubs, tc.apexAvailable)
10424 mockFsFixturePreparer := android.FixtureModifyMockFS(func(fs android.MockFS) {
10425 fs["system/sepolicy/apex/test_myapex-file_contexts"] = nil
10426 })
10427 if tc.expectedError == "" {
10428 testApex(t, bp, mockFsFixturePreparer)
10429 } else {
10430 testApexError(t, tc.expectedError, bp, mockFsFixturePreparer)
10431 }
10432 }
10433}
Colin Crossbd3a16b2023-04-25 11:30:51 -070010434
10435func TestFileSystemShouldSkipApexLibraries(t *testing.T) {
10436 context := android.GroupFixturePreparers(
10437 android.PrepareForIntegrationTestWithAndroid,
10438 cc.PrepareForIntegrationTestWithCc,
10439 PrepareForTestWithApexBuildComponents,
10440 prepareForTestWithMyapex,
10441 filesystem.PrepareForTestWithFilesystemBuildComponents,
10442 )
10443 result := context.RunTestWithBp(t, `
10444 android_system_image {
10445 name: "myfilesystem",
10446 deps: [
10447 "libfoo",
10448 ],
10449 linker_config_src: "linker.config.json",
10450 }
10451
10452 cc_library {
10453 name: "libfoo",
10454 shared_libs: [
10455 "libbar",
10456 ],
10457 stl: "none",
10458 }
10459
10460 cc_library {
10461 name: "libbar",
10462 stl: "none",
10463 apex_available: ["myapex"],
10464 }
10465
10466 apex {
10467 name: "myapex",
10468 native_shared_libs: ["libbar"],
10469 key: "myapex.key",
10470 updatable: false,
10471 }
10472
10473 apex_key {
10474 name: "myapex.key",
10475 public_key: "testkey.avbpubkey",
10476 private_key: "testkey.pem",
10477 }
10478 `)
10479
Cole Faust3b806d32024-03-11 15:15:03 -070010480 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits
Colin Crossbd3a16b2023-04-25 11:30:51 -070010481 android.AssertStringListDoesNotContain(t, "filesystem should not have libbar",
10482 inputs.Strings(),
10483 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
10484}
Yu Liueae7b362023-11-16 17:05:47 -080010485
10486var apex_default_bp = `
10487 apex_key {
10488 name: "myapex.key",
10489 public_key: "testkey.avbpubkey",
10490 private_key: "testkey.pem",
10491 }
10492
10493 filegroup {
10494 name: "myapex.manifest",
10495 srcs: ["apex_manifest.json"],
10496 }
10497
10498 filegroup {
10499 name: "myapex.androidmanifest",
10500 srcs: ["AndroidManifest.xml"],
10501 }
10502`
10503
10504func TestAconfigFilesJavaDeps(t *testing.T) {
10505 ctx := testApex(t, apex_default_bp+`
10506 apex {
10507 name: "myapex",
10508 manifest: ":myapex.manifest",
10509 androidManifest: ":myapex.androidmanifest",
10510 key: "myapex.key",
10511 java_libs: [
10512 "my_java_library_foo",
10513 "my_java_library_bar",
10514 ],
10515 updatable: false,
10516 }
10517
10518 java_library {
10519 name: "my_java_library_foo",
10520 srcs: ["foo/bar/MyClass.java"],
10521 sdk_version: "none",
10522 system_modules: "none",
10523 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010524 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010525 "myapex",
10526 ],
10527 }
10528
10529 java_library {
10530 name: "my_java_library_bar",
10531 srcs: ["foo/bar/MyClass.java"],
10532 sdk_version: "none",
10533 system_modules: "none",
10534 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010535 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010536 "myapex",
10537 ],
10538 }
10539
10540 aconfig_declarations {
10541 name: "my_aconfig_declarations_foo",
10542 package: "com.example.package",
10543 container: "myapex",
10544 srcs: ["foo.aconfig"],
10545 }
10546
10547 java_aconfig_library {
10548 name: "my_java_aconfig_library_foo",
10549 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010550 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010551 "myapex",
10552 ],
10553 }
10554
10555 aconfig_declarations {
10556 name: "my_aconfig_declarations_bar",
10557 package: "com.example.package",
10558 container: "myapex",
10559 srcs: ["bar.aconfig"],
10560 }
10561
10562 java_aconfig_library {
10563 name: "my_java_aconfig_library_bar",
10564 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010565 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010566 "myapex",
10567 ],
10568 }
10569 `)
10570
10571 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10572 s := mod.Rule("apexRule").Args["copy_commands"]
10573 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Yu Liubba555e2024-02-17 00:36:42 +000010574 if len(copyCmds) != 8 {
Yu Liueae7b362023-11-16 17:05:47 -080010575 t.Fatalf("Expected 5 commands, got %d in:\n%s", len(copyCmds), s)
10576 }
10577
Yu Liuab31c822024-02-28 22:21:31 +000010578 ensureMatches(t, copyCmds[4], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10579 ensureMatches(t, copyCmds[5], "^cp -f .*/package.map .*/image.apex/etc$")
10580 ensureMatches(t, copyCmds[6], "^cp -f .*/flag.map .*/image.apex/etc$")
10581 ensureMatches(t, copyCmds[7], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liueae7b362023-11-16 17:05:47 -080010582
Yu Liubba555e2024-02-17 00:36:42 +000010583 inputs := []string{
10584 "my_aconfig_declarations_foo/intermediate.pb",
10585 "my_aconfig_declarations_bar/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010586 }
Yu Liubba555e2024-02-17 00:36:42 +000010587 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10588 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10589 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10590 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010591}
10592
10593func TestAconfigFilesJavaAndCcDeps(t *testing.T) {
10594 ctx := testApex(t, apex_default_bp+`
10595 apex {
10596 name: "myapex",
10597 manifest: ":myapex.manifest",
10598 androidManifest: ":myapex.androidmanifest",
10599 key: "myapex.key",
10600 java_libs: [
10601 "my_java_library_foo",
10602 ],
10603 native_shared_libs: [
10604 "my_cc_library_bar",
10605 ],
10606 binaries: [
10607 "my_cc_binary_baz",
10608 ],
10609 updatable: false,
10610 }
10611
10612 java_library {
10613 name: "my_java_library_foo",
10614 srcs: ["foo/bar/MyClass.java"],
10615 sdk_version: "none",
10616 system_modules: "none",
10617 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010618 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010619 "myapex",
10620 ],
10621 }
10622
10623 cc_library {
10624 name: "my_cc_library_bar",
10625 srcs: ["foo/bar/MyClass.cc"],
Yu Liucec0e412023-11-30 16:45:50 -080010626 static_libs: [
10627 "my_cc_aconfig_library_bar",
10628 "my_cc_aconfig_library_baz",
10629 ],
Yu Liueae7b362023-11-16 17:05:47 -080010630 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010631 "myapex",
10632 ],
10633 }
10634
10635 cc_binary {
10636 name: "my_cc_binary_baz",
10637 srcs: ["foo/bar/MyClass.cc"],
10638 static_libs: ["my_cc_aconfig_library_baz"],
Yu Liueae7b362023-11-16 17:05:47 -080010639 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010640 "myapex",
10641 ],
10642 }
10643
10644 aconfig_declarations {
10645 name: "my_aconfig_declarations_foo",
10646 package: "com.example.package",
10647 container: "myapex",
10648 srcs: ["foo.aconfig"],
10649 }
10650
10651 java_aconfig_library {
10652 name: "my_java_aconfig_library_foo",
10653 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010654 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010655 "myapex",
10656 ],
10657 }
10658
10659 aconfig_declarations {
10660 name: "my_aconfig_declarations_bar",
10661 package: "com.example.package",
10662 container: "myapex",
10663 srcs: ["bar.aconfig"],
10664 }
10665
10666 cc_aconfig_library {
10667 name: "my_cc_aconfig_library_bar",
10668 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010669 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010670 "myapex",
10671 ],
10672 }
10673
10674 aconfig_declarations {
10675 name: "my_aconfig_declarations_baz",
10676 package: "com.example.package",
10677 container: "myapex",
10678 srcs: ["baz.aconfig"],
10679 }
10680
10681 cc_aconfig_library {
10682 name: "my_cc_aconfig_library_baz",
10683 aconfig_declarations: "my_aconfig_declarations_baz",
Yu Liueae7b362023-11-16 17:05:47 -080010684 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010685 "myapex",
10686 ],
10687 }
10688
10689 cc_library {
10690 name: "server_configurable_flags",
10691 srcs: ["server_configurable_flags.cc"],
10692 }
10693 `)
10694
10695 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10696 s := mod.Rule("apexRule").Args["copy_commands"]
10697 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Yu Liubba555e2024-02-17 00:36:42 +000010698 if len(copyCmds) != 12 {
10699 t.Fatalf("Expected 12 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010700 }
10701
Yu Liuab31c822024-02-28 22:21:31 +000010702 ensureMatches(t, copyCmds[8], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10703 ensureMatches(t, copyCmds[9], "^cp -f .*/package.map .*/image.apex/etc$")
10704 ensureMatches(t, copyCmds[10], "^cp -f .*/flag.map .*/image.apex/etc$")
10705 ensureMatches(t, copyCmds[11], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liueae7b362023-11-16 17:05:47 -080010706
Yu Liubba555e2024-02-17 00:36:42 +000010707 inputs := []string{
10708 "my_aconfig_declarations_foo/intermediate.pb",
10709 "my_cc_library_bar/android_arm64_armv8-a_shared_apex10000/myapex/aconfig_merged.pb",
10710 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010711 }
Yu Liubba555e2024-02-17 00:36:42 +000010712 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10713 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10714 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10715 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010716}
10717
Yu Liucec0e412023-11-30 16:45:50 -080010718func TestAconfigFilesRustDeps(t *testing.T) {
10719 ctx := testApex(t, apex_default_bp+`
10720 apex {
10721 name: "myapex",
10722 manifest: ":myapex.manifest",
10723 androidManifest: ":myapex.androidmanifest",
10724 key: "myapex.key",
10725 native_shared_libs: [
10726 "libmy_rust_library",
10727 ],
10728 binaries: [
10729 "my_rust_binary",
10730 ],
10731 rust_dyn_libs: [
10732 "libmy_rust_dylib",
10733 ],
10734 updatable: false,
10735 }
10736
10737 rust_library {
10738 name: "libflags_rust", // test mock
10739 crate_name: "flags_rust",
10740 srcs: ["lib.rs"],
10741 apex_available: [
10742 "myapex",
10743 ],
10744 }
10745
10746 rust_library {
10747 name: "liblazy_static", // test mock
10748 crate_name: "lazy_static",
10749 srcs: ["src/lib.rs"],
10750 apex_available: [
10751 "myapex",
10752 ],
10753 }
10754
Ted Bauer02d475c2024-03-27 20:56:26 +000010755 rust_library {
10756 name: "libaconfig_storage_read_api", // test mock
10757 crate_name: "aconfig_storage_read_api",
10758 srcs: ["src/lib.rs"],
10759 apex_available: [
10760 "myapex",
10761 ],
10762 }
10763
Ted Bauer6ef40db2024-03-29 14:04:10 +000010764 rust_library {
10765 name: "liblogger", // test mock
10766 crate_name: "logger",
10767 srcs: ["src/lib.rs"],
10768 apex_available: [
10769 "myapex",
10770 ],
10771 }
10772
10773 rust_library {
10774 name: "liblog_rust", // test mock
10775 crate_name: "log_rust",
10776 srcs: ["src/lib.rs"],
10777 apex_available: [
10778 "myapex",
10779 ],
10780 }
10781
Yu Liucec0e412023-11-30 16:45:50 -080010782 rust_ffi_shared {
10783 name: "libmy_rust_library",
10784 srcs: ["src/lib.rs"],
10785 rustlibs: ["libmy_rust_aconfig_library_foo"],
10786 crate_name: "my_rust_library",
10787 apex_available: [
10788 "myapex",
10789 ],
10790 }
10791
10792 rust_library_dylib {
10793 name: "libmy_rust_dylib",
10794 srcs: ["foo/bar/MyClass.rs"],
10795 rustlibs: ["libmy_rust_aconfig_library_bar"],
10796 crate_name: "my_rust_dylib",
10797 apex_available: [
10798 "myapex",
10799 ],
10800 }
10801
10802 rust_binary {
10803 name: "my_rust_binary",
10804 srcs: ["foo/bar/MyClass.rs"],
10805 rustlibs: [
10806 "libmy_rust_aconfig_library_baz",
10807 "libmy_rust_dylib",
10808 ],
10809 apex_available: [
10810 "myapex",
10811 ],
10812 }
10813
10814 aconfig_declarations {
10815 name: "my_aconfig_declarations_foo",
10816 package: "com.example.package",
10817 container: "myapex",
10818 srcs: ["foo.aconfig"],
10819 }
10820
10821 aconfig_declarations {
10822 name: "my_aconfig_declarations_bar",
10823 package: "com.example.package",
10824 container: "myapex",
10825 srcs: ["bar.aconfig"],
10826 }
10827
10828 aconfig_declarations {
10829 name: "my_aconfig_declarations_baz",
10830 package: "com.example.package",
10831 container: "myapex",
10832 srcs: ["baz.aconfig"],
10833 }
10834
10835 rust_aconfig_library {
10836 name: "libmy_rust_aconfig_library_foo",
10837 aconfig_declarations: "my_aconfig_declarations_foo",
10838 crate_name: "my_rust_aconfig_library_foo",
10839 apex_available: [
10840 "myapex",
10841 ],
10842 }
10843
10844 rust_aconfig_library {
10845 name: "libmy_rust_aconfig_library_bar",
10846 aconfig_declarations: "my_aconfig_declarations_bar",
10847 crate_name: "my_rust_aconfig_library_bar",
10848 apex_available: [
10849 "myapex",
10850 ],
10851 }
10852
10853 rust_aconfig_library {
10854 name: "libmy_rust_aconfig_library_baz",
10855 aconfig_declarations: "my_aconfig_declarations_baz",
10856 crate_name: "my_rust_aconfig_library_baz",
10857 apex_available: [
10858 "myapex",
10859 ],
10860 }
10861 `)
10862
10863 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10864 s := mod.Rule("apexRule").Args["copy_commands"]
10865 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Ted Bauer6ef40db2024-03-29 14:04:10 +000010866 if len(copyCmds) != 32 {
Ted Bauer02d475c2024-03-27 20:56:26 +000010867 t.Fatalf("Expected 28 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liucec0e412023-11-30 16:45:50 -080010868 }
10869
Ted Bauer6ef40db2024-03-29 14:04:10 +000010870 ensureMatches(t, copyCmds[28], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10871 ensureMatches(t, copyCmds[29], "^cp -f .*/package.map .*/image.apex/etc$")
10872 ensureMatches(t, copyCmds[30], "^cp -f .*/flag.map .*/image.apex/etc$")
10873 ensureMatches(t, copyCmds[31], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liucec0e412023-11-30 16:45:50 -080010874
Yu Liubba555e2024-02-17 00:36:42 +000010875 inputs := []string{
10876 "my_aconfig_declarations_foo/intermediate.pb",
Yu Liuab31c822024-02-28 22:21:31 +000010877 "my_aconfig_declarations_bar/intermediate.pb",
10878 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liubba555e2024-02-17 00:36:42 +000010879 "my_rust_binary/android_arm64_armv8-a_apex10000/myapex/aconfig_merged.pb",
10880 }
10881 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10882 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10883 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10884 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
10885}
10886
10887func VerifyAconfigRule(t *testing.T, mod *android.TestingModule, desc string, inputs []string, output string, container string, file_type string) {
10888 aconfigRule := mod.Description(desc)
10889 s := " " + aconfigRule.Args["cache_files"]
Yu Liucec0e412023-11-30 16:45:50 -080010890 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
Yu Liubba555e2024-02-17 00:36:42 +000010891 if len(aconfigArgs) != len(inputs) {
10892 t.Fatalf("Expected %d commands, got %d in:\n%s", len(inputs), len(aconfigArgs), s)
Yu Liucec0e412023-11-30 16:45:50 -080010893 }
Yu Liucec0e412023-11-30 16:45:50 -080010894
Yu Liubba555e2024-02-17 00:36:42 +000010895 ensureEquals(t, container, aconfigRule.Args["container"])
10896 ensureEquals(t, file_type, aconfigRule.Args["file_type"])
10897
10898 buildParams := aconfigRule.BuildParams
10899 for _, input := range inputs {
10900 android.EnsureListContainsSuffix(t, aconfigArgs, input)
10901 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), input)
Yu Liucec0e412023-11-30 16:45:50 -080010902 }
Yu Liubba555e2024-02-17 00:36:42 +000010903
10904 ensureContains(t, buildParams.Output.String(), output)
Yu Liucec0e412023-11-30 16:45:50 -080010905}
10906
Yu Liueae7b362023-11-16 17:05:47 -080010907func TestAconfigFilesOnlyMatchCurrentApex(t *testing.T) {
10908 ctx := testApex(t, apex_default_bp+`
10909 apex {
10910 name: "myapex",
10911 manifest: ":myapex.manifest",
10912 androidManifest: ":myapex.androidmanifest",
10913 key: "myapex.key",
10914 java_libs: [
10915 "my_java_library_foo",
10916 "other_java_library_bar",
10917 ],
10918 updatable: false,
10919 }
10920
10921 java_library {
10922 name: "my_java_library_foo",
10923 srcs: ["foo/bar/MyClass.java"],
10924 sdk_version: "none",
10925 system_modules: "none",
10926 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010927 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010928 "myapex",
10929 ],
10930 }
10931
10932 java_library {
10933 name: "other_java_library_bar",
10934 srcs: ["foo/bar/MyClass.java"],
10935 sdk_version: "none",
10936 system_modules: "none",
10937 static_libs: ["other_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010938 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010939 "myapex",
10940 ],
10941 }
10942
10943 aconfig_declarations {
10944 name: "my_aconfig_declarations_foo",
10945 package: "com.example.package",
10946 container: "myapex",
10947 srcs: ["foo.aconfig"],
10948 }
10949
10950 java_aconfig_library {
10951 name: "my_java_aconfig_library_foo",
10952 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010953 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010954 "myapex",
10955 ],
10956 }
10957
10958 aconfig_declarations {
10959 name: "other_aconfig_declarations_bar",
10960 package: "com.example.package",
10961 container: "otherapex",
10962 srcs: ["bar.aconfig"],
10963 }
10964
10965 java_aconfig_library {
10966 name: "other_java_aconfig_library_bar",
10967 aconfig_declarations: "other_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010968 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010969 "myapex",
10970 ],
10971 }
10972 `)
10973
10974 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10975 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
10976 s := " " + combineAconfigRule.Args["cache_files"]
10977 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
10978 if len(aconfigArgs) != 1 {
10979 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
10980 }
10981 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
10982
10983 buildParams := combineAconfigRule.BuildParams
10984 if len(buildParams.Inputs) != 1 {
10985 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
10986 }
10987 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
10988 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
10989}
10990
10991func TestAconfigFilesRemoveDuplicates(t *testing.T) {
10992 ctx := testApex(t, apex_default_bp+`
10993 apex {
10994 name: "myapex",
10995 manifest: ":myapex.manifest",
10996 androidManifest: ":myapex.androidmanifest",
10997 key: "myapex.key",
10998 java_libs: [
10999 "my_java_library_foo",
11000 "my_java_library_bar",
11001 ],
11002 updatable: false,
11003 }
11004
11005 java_library {
11006 name: "my_java_library_foo",
11007 srcs: ["foo/bar/MyClass.java"],
11008 sdk_version: "none",
11009 system_modules: "none",
11010 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080011011 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011012 "myapex",
11013 ],
11014 }
11015
11016 java_library {
11017 name: "my_java_library_bar",
11018 srcs: ["foo/bar/MyClass.java"],
11019 sdk_version: "none",
11020 system_modules: "none",
11021 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080011022 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011023 "myapex",
11024 ],
11025 }
11026
11027 aconfig_declarations {
11028 name: "my_aconfig_declarations_foo",
11029 package: "com.example.package",
11030 container: "myapex",
11031 srcs: ["foo.aconfig"],
11032 }
11033
11034 java_aconfig_library {
11035 name: "my_java_aconfig_library_foo",
11036 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011037 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011038 "myapex",
11039 ],
11040 }
11041
11042 java_aconfig_library {
11043 name: "my_java_aconfig_library_bar",
11044 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011045 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011046 "myapex",
11047 ],
11048 }
11049 `)
11050
11051 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11052 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11053 s := " " + combineAconfigRule.Args["cache_files"]
11054 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11055 if len(aconfigArgs) != 1 {
11056 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
11057 }
11058 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11059
11060 buildParams := combineAconfigRule.BuildParams
11061 if len(buildParams.Inputs) != 1 {
11062 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
11063 }
11064 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11065 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11066}
Spandan Das5be63332023-12-13 00:06:32 +000011067
11068// Test that the boot jars come from the _selected_ apex prebuilt
11069// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11070func TestBootDexJarsMultipleApexPrebuilts(t *testing.T) {
11071 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
11072 t.Helper()
11073 s := ctx.ModuleForTests("dex_bootjars", "android_common")
11074 foundLibfooJar := false
11075 base := stem + ".jar"
11076 for _, output := range s.AllOutputs() {
11077 if filepath.Base(output) == base {
11078 foundLibfooJar = true
11079 buildRule := s.Output(output)
11080 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
11081 }
11082 }
11083 if !foundLibfooJar {
11084 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
11085 }
11086 }
11087
Spandan Das64c9e0c2023-12-20 20:13:34 +000011088 // Check that the boot jars of the selected apex are run through boot_jars_package_check
11089 // This validates that the jars on the bootclasspath do not contain packages outside an allowlist
11090 checkBootJarsPackageCheck := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11091 platformBcp := ctx.ModuleForTests("platform-bootclasspath", "android_common")
11092 bootJarsCheckRule := platformBcp.Rule("boot_jars_package_check")
11093 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)
11094 }
11095
11096 // Check that the boot jars used to generate the monolithic hiddenapi flags come from the selected apex
11097 checkBootJarsForMonolithicHiddenapi := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11098 monolithicHiddenapiFlagsCmd := ctx.ModuleForTests("platform-bootclasspath", "android_common").Output("out/soong/hiddenapi/hiddenapi-stub-flags.txt").RuleParams.Command
11099 android.AssertStringMatches(t, "Could not find the correct boot dex jar in monolithic hiddenapi flags generation command", monolithicHiddenapiFlagsCmd, "--boot-dex="+expectedBootJar)
11100 }
11101
Spandan Das5be63332023-12-13 00:06:32 +000011102 bp := `
11103 // Source APEX.
11104
11105 java_library {
11106 name: "framework-foo",
11107 srcs: ["foo.java"],
11108 installable: true,
11109 apex_available: [
11110 "com.android.foo",
11111 ],
11112 }
11113
11114 bootclasspath_fragment {
11115 name: "foo-bootclasspath-fragment",
11116 contents: ["framework-foo"],
11117 apex_available: [
11118 "com.android.foo",
11119 ],
11120 hidden_api: {
11121 split_packages: ["*"],
11122 },
11123 }
11124
11125 apex_key {
11126 name: "com.android.foo.key",
11127 public_key: "com.android.foo.avbpubkey",
11128 private_key: "com.android.foo.pem",
11129 }
11130
11131 apex {
11132 name: "com.android.foo",
11133 key: "com.android.foo.key",
11134 bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11135 updatable: false,
11136 }
11137
11138 // Prebuilt APEX.
11139
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011140 java_sdk_library_import {
Spandan Das5be63332023-12-13 00:06:32 +000011141 name: "framework-foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011142 public: {
11143 jars: ["foo.jar"],
11144 },
Spandan Das5be63332023-12-13 00:06:32 +000011145 apex_available: ["com.android.foo"],
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011146 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +000011147 }
11148
11149 prebuilt_bootclasspath_fragment {
11150 name: "foo-bootclasspath-fragment",
11151 contents: ["framework-foo"],
11152 hidden_api: {
11153 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
11154 metadata: "my-bootclasspath-fragment/metadata.csv",
11155 index: "my-bootclasspath-fragment/index.csv",
11156 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
11157 all_flags: "my-bootclasspath-fragment/all-flags.csv",
11158 },
11159 apex_available: [
11160 "com.android.foo",
11161 ],
11162 }
11163
11164 prebuilt_apex {
11165 name: "com.android.foo",
11166 apex_name: "com.android.foo",
11167 src: "com.android.foo-arm.apex",
11168 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11169 }
11170
11171 // Another Prebuilt ART APEX
11172 prebuilt_apex {
11173 name: "com.android.foo.v2",
11174 apex_name: "com.android.foo", // Used to determine the API domain
11175 src: "com.android.foo-arm.apex",
11176 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11177 }
11178
11179 // APEX contribution modules
11180
11181 apex_contributions {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011182 name: "foo.source.contributions",
Spandan Das5be63332023-12-13 00:06:32 +000011183 api_domain: "com.android.foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011184 contents: ["com.android.foo"],
11185 }
11186
11187 apex_contributions {
11188 name: "foo.prebuilt.contributions",
11189 api_domain: "com.android.foo",
11190 contents: ["prebuilt_com.android.foo"],
11191 }
11192
11193 apex_contributions {
11194 name: "foo.prebuilt.v2.contributions",
11195 api_domain: "com.android.foo",
11196 contents: ["com.android.foo.v2"], // prebuilt_ prefix is missing because of prebuilt_rename mutator
Spandan Das5be63332023-12-13 00:06:32 +000011197 }
11198 `
11199
11200 testCases := []struct {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011201 desc string
11202 selectedApexContributions string
11203 expectedBootJar string
Spandan Das5be63332023-12-13 00:06:32 +000011204 }{
11205 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011206 desc: "Source apex com.android.foo is selected, bootjar should come from source java library",
11207 selectedApexContributions: "foo.source.contributions",
11208 expectedBootJar: "out/soong/.intermediates/foo-bootclasspath-fragment/android_common_apex10000/hiddenapi-modular/encoded/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011209 },
11210 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011211 desc: "Prebuilt apex prebuilt_com.android.foo is selected, profile should come from .prof deapexed from the prebuilt",
11212 selectedApexContributions: "foo.prebuilt.contributions",
11213 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011214 },
11215 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011216 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected, profile should come from .prof deapexed from the prebuilt",
11217 selectedApexContributions: "foo.prebuilt.v2.contributions",
11218 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.v2.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011219 },
11220 }
11221
11222 fragment := java.ApexVariantReference{
11223 Apex: proptools.StringPtr("com.android.foo"),
11224 Module: proptools.StringPtr("foo-bootclasspath-fragment"),
11225 }
11226
11227 for _, tc := range testCases {
11228 preparer := android.GroupFixturePreparers(
11229 java.FixtureConfigureApexBootJars("com.android.foo:framework-foo"),
11230 android.FixtureMergeMockFs(map[string][]byte{
11231 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11232 }),
11233 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
11234 variables.BuildFlags = map[string]string{
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011235 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": tc.selectedApexContributions,
Spandan Das5be63332023-12-13 00:06:32 +000011236 }
11237 }),
11238 )
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011239 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das5be63332023-12-13 00:06:32 +000011240 checkBootDexJarPath(t, ctx, "framework-foo", tc.expectedBootJar)
Spandan Das64c9e0c2023-12-20 20:13:34 +000011241 checkBootJarsPackageCheck(t, ctx, tc.expectedBootJar)
11242 checkBootJarsForMonolithicHiddenapi(t, ctx, tc.expectedBootJar)
Spandan Das5be63332023-12-13 00:06:32 +000011243 }
11244}
Spandan Das3576e762024-01-03 18:57:03 +000011245
11246// Test that product packaging installs the selected mainline module (either source or a specific prebuilt)
11247// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11248func TestInstallationRulesForMultipleApexPrebuilts(t *testing.T) {
11249 // check that the LOCAL_MODULE in the generated mk file matches the name used in PRODUCT_PACKAGES
11250 // Since the name used in PRODUCT_PACKAGES does not contain prebuilt_ prefix, LOCAL_MODULE should not contain any prefix either
11251 checkLocalModuleName := func(t *testing.T, ctx *android.TestContext, soongApexModuleName string, expectedLocalModuleName string) {
11252 // Variations are created based on apex_name
11253 entries := android.AndroidMkEntriesForTest(t, ctx, ctx.ModuleForTests(soongApexModuleName, "android_common_com.android.foo").Module())
11254 android.AssertStringEquals(t, "LOCAL_MODULE of the prebuilt apex must match the name listed in PRODUCT_PACKAGES", expectedLocalModuleName, entries[0].EntryMap["LOCAL_MODULE"][0])
11255 }
11256 // for a mainline module family, check that only the flagged soong module is visible to make
11257 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleName string, hiddenModuleNames []string) {
11258 variation := func(moduleName string) string {
11259 ret := "android_common_com.android.foo"
11260 if moduleName == "com.google.android.foo" {
11261 ret = "android_common_com.google.android.foo_com.android.foo"
11262 }
11263 return ret
11264 }
11265
11266 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11267 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11268
11269 for _, hiddenModuleName := range hiddenModuleNames {
11270 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11271 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11272
11273 }
11274 }
11275
11276 bp := `
11277 apex_key {
11278 name: "com.android.foo.key",
11279 public_key: "com.android.foo.avbpubkey",
11280 private_key: "com.android.foo.pem",
11281 }
11282
11283 // AOSP source apex
11284 apex {
11285 name: "com.android.foo",
11286 key: "com.android.foo.key",
11287 updatable: false,
11288 }
11289
11290 // Google source apex
11291 override_apex {
11292 name: "com.google.android.foo",
11293 base: "com.android.foo",
11294 key: "com.android.foo.key",
11295 }
11296
11297 // Prebuilt Google APEX.
11298
11299 prebuilt_apex {
11300 name: "com.google.android.foo",
11301 apex_name: "com.android.foo",
11302 src: "com.android.foo-arm.apex",
11303 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11304 }
11305
11306 // Another Prebuilt Google APEX
11307 prebuilt_apex {
11308 name: "com.google.android.foo.v2",
11309 apex_name: "com.android.foo",
11310 source_apex_name: "com.google.android.foo", // source_apex_name becomes LOCAL_MODULE in the generated mk file
11311 src: "com.android.foo-arm.apex",
11312 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11313 }
11314
11315 // APEX contribution modules
11316
11317 apex_contributions {
11318 name: "foo.source.contributions",
11319 api_domain: "com.android.foo",
11320 contents: ["com.google.android.foo"],
11321 }
11322
11323 apex_contributions {
11324 name: "foo.prebuilt.contributions",
11325 api_domain: "com.android.foo",
11326 contents: ["prebuilt_com.google.android.foo"],
11327 }
11328
11329 apex_contributions {
11330 name: "foo.prebuilt.v2.contributions",
11331 api_domain: "com.android.foo",
11332 contents: ["prebuilt_com.google.android.foo.v2"],
11333 }
11334
11335 // This is an incompatible module because it selects multiple versions of the same mainline module
11336 apex_contributions {
11337 name: "foo.prebuilt.duplicate.contributions",
11338 api_domain: "com.android.foo",
11339 contents: [
11340 "prebuilt_com.google.android.foo",
11341 "prebuilt_com.google.android.foo.v2",
11342 ],
11343 }
11344 `
11345
11346 testCases := []struct {
11347 desc string
11348 selectedApexContributions string
11349 expectedVisibleModuleName string
11350 expectedHiddenModuleNames []string
11351 expectedError string
11352 }{
11353 {
11354 desc: "Source apex is selected, prebuilts should be hidden from make",
11355 selectedApexContributions: "foo.source.contributions",
11356 expectedVisibleModuleName: "com.google.android.foo",
11357 expectedHiddenModuleNames: []string{"prebuilt_com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11358 },
11359 {
11360 desc: "Prebuilt apex prebuilt_com.android.foo is selected, source and the other prebuilt should be hidden from make",
11361 selectedApexContributions: "foo.prebuilt.contributions",
11362 expectedVisibleModuleName: "prebuilt_com.google.android.foo",
11363 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11364 },
11365 {
11366 desc: "Prebuilt apex prebuilt_com.android.fooi.v2 is selected, source and the other prebuilt should be hidden from make",
11367 selectedApexContributions: "foo.prebuilt.v2.contributions",
11368 expectedVisibleModuleName: "prebuilt_com.google.android.foo.v2",
11369 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo"},
11370 },
11371 {
11372 desc: "Multiple versions of a prebuilt apex is selected in the same release config",
11373 selectedApexContributions: "foo.prebuilt.duplicate.contributions",
11374 expectedError: "Found duplicate variations of the same module in apex_contributions: prebuilt_com.google.android.foo and prebuilt_com.google.android.foo.v2",
11375 },
11376 }
11377
11378 for _, tc := range testCases {
11379 preparer := android.GroupFixturePreparers(
11380 android.FixtureMergeMockFs(map[string][]byte{
11381 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11382 }),
11383 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
11384 variables.BuildFlags = map[string]string{
11385 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": tc.selectedApexContributions,
11386 }
11387 }),
11388 )
11389 if tc.expectedError != "" {
11390 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(tc.expectedError))
11391 testApex(t, bp, preparer)
11392 return
11393 }
11394 ctx := testApex(t, bp, preparer)
11395
11396 // Check that the LOCAL_MODULE of the two prebuilts is com.android.foo
11397 // This ensures that product packaging can pick them for installation if it has been flagged by apex_contributions
11398 checkLocalModuleName(t, ctx, "prebuilt_com.google.android.foo", "com.google.android.foo")
11399 checkLocalModuleName(t, ctx, "prebuilt_com.google.android.foo.v2", "com.google.android.foo")
11400
11401 // Check that
11402 // 1. The contents of the selected apex_contributions are visible to make
11403 // 2. The rest of the apexes in the mainline module family (source or other prebuilt) is hidden from make
11404 checkHideFromMake(t, ctx, tc.expectedVisibleModuleName, tc.expectedHiddenModuleNames)
11405 }
11406}