blob: 187e30bd0954f0c1ced82e95ad897a5667556396 [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 `
Cole Faust0e0d7492024-04-11 17:43:00 -07005625 // This test disables libbar, which causes the ComponentDepsMutator to add
5626 // deps on libbar.stubs and other sub-modules that don't exist. We can
5627 // enable AllowMissingDependencies to work around that, but enabling that
5628 // causes extra checks for missing source files to dex_bootjars, so add those
5629 // to the mock fs as well.
5630 preparer2 := android.GroupFixturePreparers(
5631 preparer,
5632 android.PrepareForTestWithAllowMissingDependencies,
5633 android.FixtureMergeMockFs(map[string][]byte{
5634 "build/soong/scripts/check_boot_jars/package_allowed_list.txt": nil,
5635 "frameworks/base/config/boot-profile.txt": nil,
5636 }),
5637 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005638
Cole Faust0e0d7492024-04-11 17:43:00 -07005639 ctx := testDexpreoptWithApexes(t, bp, "", preparer2, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005640 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5641 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005642
Paul Duffin537ea3d2021-05-14 10:38:00 +01005643 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005644 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005645 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005646 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005647 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005648 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 +01005649 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005650 })
Spandan Das3a392012024-01-17 18:26:27 +00005651
Spandan Dasf2c10572024-02-27 04:49:52 +00005652 t.Run("Co-existing unflagged apexes should create a duplicate module error", func(t *testing.T) {
Spandan Das3a392012024-01-17 18:26:27 +00005653 bp := `
5654 // Source
5655 apex {
5656 name: "myapex",
5657 enabled: false,
5658 key: "myapex.key",
5659 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5660 }
5661
5662 apex_key {
5663 name: "myapex.key",
5664 public_key: "testkey.avbpubkey",
5665 private_key: "testkey.pem",
5666 }
5667
5668 // Prebuilt
5669 prebuilt_apex {
5670 name: "myapex.v1",
5671 source_apex_name: "myapex",
5672 arch: {
5673 arm64: {
5674 src: "myapex-arm64.apex",
5675 },
5676 arm: {
5677 src: "myapex-arm.apex",
5678 },
5679 },
5680 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5681 prefer: true,
5682 }
5683 prebuilt_apex {
5684 name: "myapex.v2",
5685 source_apex_name: "myapex",
5686 arch: {
5687 arm64: {
5688 src: "myapex-arm64.apex",
5689 },
5690 arm: {
5691 src: "myapex-arm.apex",
5692 },
5693 },
5694 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5695 prefer: true,
5696 }
5697
5698 prebuilt_bootclasspath_fragment {
5699 name: "my-bootclasspath-fragment",
5700 contents: ["libfoo", "libbar"],
5701 apex_available: ["myapex"],
5702 hidden_api: {
5703 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5704 metadata: "my-bootclasspath-fragment/metadata.csv",
5705 index: "my-bootclasspath-fragment/index.csv",
5706 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5707 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5708 },
5709 prefer: true,
5710 }
5711
5712 java_import {
5713 name: "libfoo",
5714 jars: ["libfoo.jar"],
5715 apex_available: ["myapex"],
5716 prefer: true,
5717 }
5718 java_import {
5719 name: "libbar",
5720 jars: ["libbar.jar"],
5721 apex_available: ["myapex"],
5722 prefer: true,
5723 }
5724 `
5725
Spandan Dasf2c10572024-02-27 04:49:52 +00005726 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 +00005727 })
5728
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005729}
5730
Roland Levillain630846d2019-06-26 12:48:34 +01005731func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005732 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005733 apex_test {
5734 name: "myapex",
5735 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005736 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005737 tests: [
5738 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005739 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005740 ],
5741 }
5742
5743 apex_key {
5744 name: "myapex.key",
5745 public_key: "testkey.avbpubkey",
5746 private_key: "testkey.pem",
5747 }
5748
Liz Kammer1c14a212020-05-12 15:26:55 -07005749 filegroup {
5750 name: "fg",
5751 srcs: [
5752 "baz",
5753 "bar/baz"
5754 ],
5755 }
5756
Roland Levillain630846d2019-06-26 12:48:34 +01005757 cc_test {
5758 name: "mytest",
5759 gtest: false,
5760 srcs: ["mytest.cpp"],
5761 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005762 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005763 system_shared_libs: [],
5764 static_executable: true,
5765 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005766 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005767 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005768
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005769 cc_library {
5770 name: "mylib",
5771 srcs: ["mylib.cpp"],
5772 system_shared_libs: [],
5773 stl: "none",
5774 }
5775
Liz Kammer5bd365f2020-05-27 15:15:11 -07005776 filegroup {
5777 name: "fg2",
5778 srcs: [
5779 "testdata/baz"
5780 ],
5781 }
5782
Roland Levillain9b5fde92019-06-28 15:41:19 +01005783 cc_test {
5784 name: "mytests",
5785 gtest: false,
5786 srcs: [
5787 "mytest1.cpp",
5788 "mytest2.cpp",
5789 "mytest3.cpp",
5790 ],
5791 test_per_src: true,
5792 relative_install_path: "test",
5793 system_shared_libs: [],
5794 static_executable: true,
5795 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005796 data: [
5797 ":fg",
5798 ":fg2",
5799 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005800 }
Roland Levillain630846d2019-06-26 12:48:34 +01005801 `)
5802
Jooyung Hana0503a52023-08-23 13:12:50 +09005803 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005804 copyCmds := apexRule.Args["copy_commands"]
5805
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005806 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005807 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005808 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005809
Liz Kammer1c14a212020-05-12 15:26:55 -07005810 //Ensure that test data are copied into apex.
5811 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5812 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5813
Roland Levillain9b5fde92019-06-28 15:41:19 +01005814 // Ensure that test deps built with `test_per_src` are copied into apex.
5815 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5816 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5817 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005818
5819 // Ensure the module is correctly translated.
Jooyung Hana0503a52023-08-23 13:12:50 +09005820 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005821 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005822 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005823 prefix := "TARGET_"
5824 var builder strings.Builder
5825 data.Custom(&builder, name, prefix, "", data)
5826 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005827 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5828 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5829 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5830 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005831 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005832}
5833
Jooyung Hand48f3c32019-08-23 11:18:57 +09005834func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5835 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5836 apex {
5837 name: "myapex",
5838 key: "myapex.key",
5839 native_shared_libs: ["libfoo"],
5840 }
5841
5842 apex_key {
5843 name: "myapex.key",
5844 public_key: "testkey.avbpubkey",
5845 private_key: "testkey.pem",
5846 }
5847
5848 cc_library {
5849 name: "libfoo",
5850 stl: "none",
5851 system_shared_libs: [],
5852 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005853 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005854 }
5855 `)
5856 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5857 apex {
5858 name: "myapex",
5859 key: "myapex.key",
5860 java_libs: ["myjar"],
5861 }
5862
5863 apex_key {
5864 name: "myapex.key",
5865 public_key: "testkey.avbpubkey",
5866 private_key: "testkey.pem",
5867 }
5868
5869 java_library {
5870 name: "myjar",
5871 srcs: ["foo/bar/MyClass.java"],
5872 sdk_version: "none",
5873 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005874 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005875 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005876 }
5877 `)
5878}
5879
Bill Peckhama41a6962021-01-11 10:58:54 -08005880func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005881 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005882 apex {
5883 name: "myapex",
5884 key: "myapex.key",
5885 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005886 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005887 }
5888
5889 apex_key {
5890 name: "myapex.key",
5891 public_key: "testkey.avbpubkey",
5892 private_key: "testkey.pem",
5893 }
5894
5895 java_import {
5896 name: "myjavaimport",
5897 apex_available: ["myapex"],
5898 jars: ["my.jar"],
5899 compile_dex: true,
5900 }
5901 `)
5902
Jooyung Hana0503a52023-08-23 13:12:50 +09005903 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Bill Peckhama41a6962021-01-11 10:58:54 -08005904 apexRule := module.Rule("apexRule")
5905 copyCmds := apexRule.Args["copy_commands"]
5906 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5907}
5908
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005909func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005910 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005911 apex {
5912 name: "myapex",
5913 key: "myapex.key",
5914 apps: [
5915 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005916 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005917 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005918 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005919 }
5920
5921 apex_key {
5922 name: "myapex.key",
5923 public_key: "testkey.avbpubkey",
5924 private_key: "testkey.pem",
5925 }
5926
5927 android_app {
5928 name: "AppFoo",
5929 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005930 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005931 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005932 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005933 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005934 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005935 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005936
5937 android_app {
5938 name: "AppFooPriv",
5939 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005940 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005941 system_modules: "none",
5942 privileged: true,
Sam Delmerico15809f82023-05-15 17:21:47 -04005943 privapp_allowlist: "privapp_allowlist_com.android.AppFooPriv.xml",
Colin Cross094cde42020-02-15 10:38:00 -08005944 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005945 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005946 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005947
5948 cc_library_shared {
5949 name: "libjni",
5950 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005951 shared_libs: ["libfoo"],
5952 stl: "none",
5953 system_shared_libs: [],
5954 apex_available: [ "myapex" ],
5955 sdk_version: "current",
5956 }
5957
5958 cc_library_shared {
5959 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005960 stl: "none",
5961 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005962 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005963 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005964 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005965 `)
5966
Jooyung Hana0503a52023-08-23 13:12:50 +09005967 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005968 apexRule := module.Rule("apexRule")
5969 copyCmds := apexRule.Args["copy_commands"]
5970
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005971 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5972 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Andrei Onea580636b2022-08-17 16:53:46 +00005973 ensureContains(t, copyCmds, "image.apex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005974
Colin Crossaede88c2020-08-11 12:17:01 -07005975 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005976 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005977 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005978 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005979 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005980 // JNI libraries including transitive deps are
5981 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005982 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005983 // ... embedded inside APK (jnilibs.zip)
5984 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5985 // ... and not directly inside the APEX
5986 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5987 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04005988
5989 apexBundle := module.Module().(*apexBundle)
5990 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
5991 var builder strings.Builder
5992 data.Custom(&builder, apexBundle.Name(), "TARGET_", "", data)
5993 androidMk := builder.String()
5994 ensureContains(t, androidMk, "LOCAL_MODULE := AppFooPriv.myapex")
5995 ensureContains(t, androidMk, "LOCAL_MODULE := AppFoo.myapex")
5996 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFooPriv.apk")
5997 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFoo.apk")
5998 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := \\S+AppFooPriv.apk")
5999 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 +01006000}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006001
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006002func TestApexWithAppImportBuildId(t *testing.T) {
6003 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
6004 for _, id := range invalidBuildIds {
6005 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
6006 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6007 variables.BuildId = proptools.StringPtr(id)
6008 })
6009 testApexError(t, message, `apex {
6010 name: "myapex",
6011 key: "myapex.key",
6012 apps: ["AppFooPrebuilt"],
6013 updatable: false,
6014 }
6015
6016 apex_key {
6017 name: "myapex.key",
6018 public_key: "testkey.avbpubkey",
6019 private_key: "testkey.pem",
6020 }
6021
6022 android_app_import {
6023 name: "AppFooPrebuilt",
6024 apk: "PrebuiltAppFoo.apk",
6025 presigned: true,
6026 apex_available: ["myapex"],
6027 }
6028 `, fixture)
6029 }
6030}
6031
Dario Frenicde2a032019-10-27 00:29:22 +01006032func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006033 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01006034 apex {
6035 name: "myapex",
6036 key: "myapex.key",
6037 apps: [
6038 "AppFooPrebuilt",
6039 "AppFooPrivPrebuilt",
6040 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006041 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01006042 }
6043
6044 apex_key {
6045 name: "myapex.key",
6046 public_key: "testkey.avbpubkey",
6047 private_key: "testkey.pem",
6048 }
6049
6050 android_app_import {
6051 name: "AppFooPrebuilt",
6052 apk: "PrebuiltAppFoo.apk",
6053 presigned: true,
6054 dex_preopt: {
6055 enabled: false,
6056 },
Jiyong Park592a6a42020-04-21 22:34:28 +09006057 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006058 }
6059
6060 android_app_import {
6061 name: "AppFooPrivPrebuilt",
6062 apk: "PrebuiltAppFooPriv.apk",
6063 privileged: true,
6064 presigned: true,
6065 dex_preopt: {
6066 enabled: false,
6067 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006068 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006069 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006070 }
6071 `)
6072
Jooyung Hana0503a52023-08-23 13:12:50 +09006073 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Frenicde2a032019-10-27 00:29:22 +01006074 apexRule := module.Rule("apexRule")
6075 copyCmds := apexRule.Args["copy_commands"]
6076
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006077 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6078 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006079}
6080
6081func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006082 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006083 apex {
6084 name: "myapex",
6085 key: "myapex.key",
6086 apps: [
6087 "AppFoo",
6088 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006089 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006090 }
6091
6092 apex_key {
6093 name: "myapex.key",
6094 public_key: "testkey.avbpubkey",
6095 private_key: "testkey.pem",
6096 }
6097
6098 android_app {
6099 name: "AppFoo",
6100 srcs: ["foo/bar/MyClass.java"],
6101 sdk_version: "none",
6102 system_modules: "none",
6103 apex_available: [ "myapex" ],
6104 }
6105
6106 android_app_import {
6107 name: "AppFoo",
6108 apk: "AppFooPrebuilt.apk",
6109 filename: "AppFooPrebuilt.apk",
6110 presigned: true,
6111 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006112 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006113 }
6114 `, withFiles(map[string][]byte{
6115 "AppFooPrebuilt.apk": nil,
6116 }))
6117
Jooyung Hana0503a52023-08-23 13:12:50 +09006118 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006119 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006120 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006121}
6122
Dario Freni6f3937c2019-12-20 22:58:03 +00006123func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006124 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006125 apex {
6126 name: "myapex",
6127 key: "myapex.key",
6128 apps: [
6129 "TesterHelpAppFoo",
6130 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006131 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006132 }
6133
6134 apex_key {
6135 name: "myapex.key",
6136 public_key: "testkey.avbpubkey",
6137 private_key: "testkey.pem",
6138 }
6139
6140 android_test_helper_app {
6141 name: "TesterHelpAppFoo",
6142 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006143 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006144 }
6145
6146 `)
6147
Jooyung Hana0503a52023-08-23 13:12:50 +09006148 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Freni6f3937c2019-12-20 22:58:03 +00006149 apexRule := module.Rule("apexRule")
6150 copyCmds := apexRule.Args["copy_commands"]
6151
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006152 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006153}
6154
Jooyung Han18020ea2019-11-13 10:50:48 +09006155func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6156 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006157 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006158 apex {
6159 name: "myapex",
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 apex_key {
6166 name: "myapex.key",
6167 public_key: "testkey.avbpubkey",
6168 private_key: "testkey.pem",
6169 }
6170
6171 apex {
6172 name: "otherapex",
6173 key: "myapex.key",
6174 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006175 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006176 }
6177
6178 cc_defaults {
6179 name: "libfoo-defaults",
6180 apex_available: ["otherapex"],
6181 }
6182
6183 cc_library {
6184 name: "libfoo",
6185 defaults: ["libfoo-defaults"],
6186 stl: "none",
6187 system_shared_libs: [],
6188 }`)
6189}
6190
Paul Duffine52e66f2020-03-30 17:54:29 +01006191func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006192 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006193 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006194 apex {
6195 name: "myapex",
6196 key: "myapex.key",
6197 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006198 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006199 }
6200
6201 apex_key {
6202 name: "myapex.key",
6203 public_key: "testkey.avbpubkey",
6204 private_key: "testkey.pem",
6205 }
6206
6207 apex {
6208 name: "otherapex",
6209 key: "otherapex.key",
6210 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006211 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006212 }
6213
6214 apex_key {
6215 name: "otherapex.key",
6216 public_key: "testkey.avbpubkey",
6217 private_key: "testkey.pem",
6218 }
6219
6220 cc_library {
6221 name: "libfoo",
6222 stl: "none",
6223 system_shared_libs: [],
6224 apex_available: ["otherapex"],
6225 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006226}
Jiyong Park127b40b2019-09-30 16:04:35 +09006227
Paul Duffine52e66f2020-03-30 17:54:29 +01006228func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006229 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006230 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006231.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006232.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006233.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006234.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006235.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006236.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006237 apex {
6238 name: "myapex",
6239 key: "myapex.key",
6240 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006241 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006242 }
6243
6244 apex_key {
6245 name: "myapex.key",
6246 public_key: "testkey.avbpubkey",
6247 private_key: "testkey.pem",
6248 }
6249
Jiyong Park127b40b2019-09-30 16:04:35 +09006250 cc_library {
6251 name: "libfoo",
6252 stl: "none",
6253 shared_libs: ["libbar"],
6254 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006255 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006256 }
6257
6258 cc_library {
6259 name: "libbar",
6260 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006261 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006262 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006263 apex_available: ["myapex"],
6264 }
6265
6266 cc_library {
6267 name: "libbaz",
6268 stl: "none",
6269 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006270 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006271}
Jiyong Park127b40b2019-09-30 16:04:35 +09006272
Liz Kammer5f108fa2023-05-11 14:33:17 -04006273func TestApexAvailable_IndirectStaticDep(t *testing.T) {
6274 testApex(t, `
6275 apex {
6276 name: "myapex",
6277 key: "myapex.key",
6278 native_shared_libs: ["libfoo"],
6279 updatable: false,
6280 }
6281
6282 apex_key {
6283 name: "myapex.key",
6284 public_key: "testkey.avbpubkey",
6285 private_key: "testkey.pem",
6286 }
6287
6288 cc_library {
6289 name: "libfoo",
6290 stl: "none",
6291 static_libs: ["libbar"],
6292 system_shared_libs: [],
6293 apex_available: ["myapex"],
6294 }
6295
6296 cc_library {
6297 name: "libbar",
6298 stl: "none",
6299 shared_libs: ["libbaz"],
6300 system_shared_libs: [],
6301 apex_available: ["myapex"],
6302 }
6303
6304 cc_library {
6305 name: "libbaz",
6306 stl: "none",
6307 system_shared_libs: [],
6308 }`)
6309
6310 testApexError(t, `requires "libbar" that doesn't list the APEX under 'apex_available'.`, `
6311 apex {
6312 name: "myapex",
6313 key: "myapex.key",
6314 native_shared_libs: ["libfoo"],
6315 updatable: false,
6316 }
6317
6318 apex_key {
6319 name: "myapex.key",
6320 public_key: "testkey.avbpubkey",
6321 private_key: "testkey.pem",
6322 }
6323
6324 cc_library {
6325 name: "libfoo",
6326 stl: "none",
6327 static_libs: ["libbar"],
6328 system_shared_libs: [],
6329 apex_available: ["myapex"],
6330 }
6331
6332 cc_library {
6333 name: "libbar",
6334 stl: "none",
6335 system_shared_libs: [],
6336 }`)
6337}
6338
Paul Duffine52e66f2020-03-30 17:54:29 +01006339func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006340 testApexError(t, "\"otherapex\" is not a valid module name", `
6341 apex {
6342 name: "myapex",
6343 key: "myapex.key",
6344 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006345 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006346 }
6347
6348 apex_key {
6349 name: "myapex.key",
6350 public_key: "testkey.avbpubkey",
6351 private_key: "testkey.pem",
6352 }
6353
6354 cc_library {
6355 name: "libfoo",
6356 stl: "none",
6357 system_shared_libs: [],
6358 apex_available: ["otherapex"],
6359 }`)
6360
Paul Duffine52e66f2020-03-30 17:54:29 +01006361 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006362 apex {
6363 name: "myapex",
6364 key: "myapex.key",
6365 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006366 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006367 }
6368
6369 apex_key {
6370 name: "myapex.key",
6371 public_key: "testkey.avbpubkey",
6372 private_key: "testkey.pem",
6373 }
6374
6375 cc_library {
6376 name: "libfoo",
6377 stl: "none",
6378 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006379 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006380 apex_available: ["myapex"],
6381 }
6382
6383 cc_library {
6384 name: "libbar",
6385 stl: "none",
6386 system_shared_libs: [],
6387 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006388 }
6389
6390 cc_library {
6391 name: "libbaz",
6392 stl: "none",
6393 system_shared_libs: [],
6394 stubs: {
6395 versions: ["10", "20", "30"],
6396 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006397 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006398}
Jiyong Park127b40b2019-09-30 16:04:35 +09006399
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006400func TestApexAvailable_ApexAvailableNameWithVersionCodeError(t *testing.T) {
6401 t.Run("negative variant_version produces error", func(t *testing.T) {
6402 testApexError(t, "expected an integer between 0-9; got -1", `
6403 apex {
6404 name: "myapex",
6405 key: "myapex.key",
6406 apex_available_name: "com.android.foo",
6407 variant_version: "-1",
6408 updatable: false,
6409 }
6410 apex_key {
6411 name: "myapex.key",
6412 public_key: "testkey.avbpubkey",
6413 private_key: "testkey.pem",
6414 }
6415 `)
6416 })
6417
6418 t.Run("variant_version greater than 9 produces error", func(t *testing.T) {
6419 testApexError(t, "expected an integer between 0-9; got 10", `
6420 apex {
6421 name: "myapex",
6422 key: "myapex.key",
6423 apex_available_name: "com.android.foo",
6424 variant_version: "10",
6425 updatable: false,
6426 }
6427 apex_key {
6428 name: "myapex.key",
6429 public_key: "testkey.avbpubkey",
6430 private_key: "testkey.pem",
6431 }
6432 `)
6433 })
6434}
6435
6436func TestApexAvailable_ApexAvailableNameWithVersionCode(t *testing.T) {
6437 context := android.GroupFixturePreparers(
6438 android.PrepareForIntegrationTestWithAndroid,
6439 PrepareForTestWithApexBuildComponents,
6440 android.FixtureMergeMockFs(android.MockFS{
6441 "system/sepolicy/apex/foo-file_contexts": nil,
6442 "system/sepolicy/apex/bar-file_contexts": nil,
6443 }),
6444 )
6445 result := context.RunTestWithBp(t, `
6446 apex {
6447 name: "foo",
6448 key: "myapex.key",
6449 apex_available_name: "com.android.foo",
6450 variant_version: "0",
6451 updatable: false,
6452 }
6453 apex {
6454 name: "bar",
6455 key: "myapex.key",
6456 apex_available_name: "com.android.foo",
6457 variant_version: "3",
6458 updatable: false,
6459 }
6460 apex_key {
6461 name: "myapex.key",
6462 public_key: "testkey.avbpubkey",
6463 private_key: "testkey.pem",
6464 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006465 override_apex {
6466 name: "myoverrideapex",
6467 base: "bar",
6468 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006469 `)
6470
Jooyung Hana0503a52023-08-23 13:12:50 +09006471 fooManifestRule := result.ModuleForTests("foo", "android_common_foo").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006472 fooExpectedDefaultVersion := android.DefaultUpdatableModuleVersion
6473 fooActualDefaultVersion := fooManifestRule.Args["default_version"]
6474 if fooActualDefaultVersion != fooExpectedDefaultVersion {
6475 t.Errorf("expected to find defaultVersion %q; got %q", fooExpectedDefaultVersion, fooActualDefaultVersion)
6476 }
6477
Jooyung Hana0503a52023-08-23 13:12:50 +09006478 barManifestRule := result.ModuleForTests("bar", "android_common_bar").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006479 defaultVersionInt, _ := strconv.Atoi(android.DefaultUpdatableModuleVersion)
6480 barExpectedDefaultVersion := fmt.Sprint(defaultVersionInt + 3)
6481 barActualDefaultVersion := barManifestRule.Args["default_version"]
6482 if barActualDefaultVersion != barExpectedDefaultVersion {
6483 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6484 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006485
Jooyung Hana0503a52023-08-23 13:12:50 +09006486 overrideBarManifestRule := result.ModuleForTests("bar", "android_common_myoverrideapex_bar").Rule("apexManifestRule")
Sam Delmerico419f9a32023-07-21 12:00:13 -04006487 overrideBarActualDefaultVersion := overrideBarManifestRule.Args["default_version"]
6488 if overrideBarActualDefaultVersion != barExpectedDefaultVersion {
6489 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6490 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006491}
6492
Sam Delmericoca816532023-06-02 14:09:50 -04006493func TestApexAvailable_ApexAvailableName(t *testing.T) {
6494 t.Run("using name of apex that sets apex_available_name is not allowed", func(t *testing.T) {
6495 testApexError(t, "Consider adding \"myapex\" to 'apex_available' property of \"AppFoo\"", `
6496 apex {
6497 name: "myapex_sminus",
6498 key: "myapex.key",
6499 apps: ["AppFoo"],
6500 apex_available_name: "myapex",
6501 updatable: false,
6502 }
6503 apex {
6504 name: "myapex",
6505 key: "myapex.key",
6506 apps: ["AppFoo"],
6507 updatable: false,
6508 }
6509 apex_key {
6510 name: "myapex.key",
6511 public_key: "testkey.avbpubkey",
6512 private_key: "testkey.pem",
6513 }
6514 android_app {
6515 name: "AppFoo",
6516 srcs: ["foo/bar/MyClass.java"],
6517 sdk_version: "none",
6518 system_modules: "none",
6519 apex_available: [ "myapex_sminus" ],
6520 }`,
6521 android.FixtureMergeMockFs(android.MockFS{
6522 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6523 }),
6524 )
6525 })
6526
6527 t.Run("apex_available_name allows module to be used in two different apexes", func(t *testing.T) {
6528 testApex(t, `
6529 apex {
6530 name: "myapex_sminus",
6531 key: "myapex.key",
6532 apps: ["AppFoo"],
6533 apex_available_name: "myapex",
6534 updatable: false,
6535 }
6536 apex {
6537 name: "myapex",
6538 key: "myapex.key",
6539 apps: ["AppFoo"],
6540 updatable: false,
6541 }
6542 apex_key {
6543 name: "myapex.key",
6544 public_key: "testkey.avbpubkey",
6545 private_key: "testkey.pem",
6546 }
6547 android_app {
6548 name: "AppFoo",
6549 srcs: ["foo/bar/MyClass.java"],
6550 sdk_version: "none",
6551 system_modules: "none",
6552 apex_available: [ "myapex" ],
6553 }`,
6554 android.FixtureMergeMockFs(android.MockFS{
6555 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6556 }),
6557 )
6558 })
6559
6560 t.Run("override_apexes work with apex_available_name", func(t *testing.T) {
6561 testApex(t, `
6562 override_apex {
6563 name: "myoverrideapex_sminus",
6564 base: "myapex_sminus",
6565 key: "myapex.key",
6566 apps: ["AppFooOverride"],
6567 }
6568 override_apex {
6569 name: "myoverrideapex",
6570 base: "myapex",
6571 key: "myapex.key",
6572 apps: ["AppFooOverride"],
6573 }
6574 apex {
6575 name: "myapex_sminus",
6576 key: "myapex.key",
6577 apps: ["AppFoo"],
6578 apex_available_name: "myapex",
6579 updatable: false,
6580 }
6581 apex {
6582 name: "myapex",
6583 key: "myapex.key",
6584 apps: ["AppFoo"],
6585 updatable: false,
6586 }
6587 apex_key {
6588 name: "myapex.key",
6589 public_key: "testkey.avbpubkey",
6590 private_key: "testkey.pem",
6591 }
6592 android_app {
6593 name: "AppFooOverride",
6594 srcs: ["foo/bar/MyClass.java"],
6595 sdk_version: "none",
6596 system_modules: "none",
6597 apex_available: [ "myapex" ],
6598 }
6599 android_app {
6600 name: "AppFoo",
6601 srcs: ["foo/bar/MyClass.java"],
6602 sdk_version: "none",
6603 system_modules: "none",
6604 apex_available: [ "myapex" ],
6605 }`,
6606 android.FixtureMergeMockFs(android.MockFS{
6607 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6608 }),
6609 )
6610 })
6611}
6612
6613func TestApexAvailable_ApexAvailableNameWithOverrides(t *testing.T) {
6614 context := android.GroupFixturePreparers(
6615 android.PrepareForIntegrationTestWithAndroid,
6616 PrepareForTestWithApexBuildComponents,
6617 java.PrepareForTestWithDexpreopt,
6618 android.FixtureMergeMockFs(android.MockFS{
6619 "system/sepolicy/apex/myapex-file_contexts": nil,
6620 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6621 }),
6622 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6623 variables.BuildId = proptools.StringPtr("buildid")
6624 }),
6625 )
6626 context.RunTestWithBp(t, `
6627 override_apex {
6628 name: "myoverrideapex_sminus",
6629 base: "myapex_sminus",
6630 }
6631 override_apex {
6632 name: "myoverrideapex",
6633 base: "myapex",
6634 }
6635 apex {
6636 name: "myapex",
6637 key: "myapex.key",
6638 apps: ["AppFoo"],
6639 updatable: false,
6640 }
6641 apex {
6642 name: "myapex_sminus",
6643 apex_available_name: "myapex",
6644 key: "myapex.key",
6645 apps: ["AppFoo_sminus"],
6646 updatable: false,
6647 }
6648 apex_key {
6649 name: "myapex.key",
6650 public_key: "testkey.avbpubkey",
6651 private_key: "testkey.pem",
6652 }
6653 android_app {
6654 name: "AppFoo",
6655 srcs: ["foo/bar/MyClass.java"],
6656 sdk_version: "none",
6657 system_modules: "none",
6658 apex_available: [ "myapex" ],
6659 }
6660 android_app {
6661 name: "AppFoo_sminus",
6662 srcs: ["foo/bar/MyClass.java"],
6663 sdk_version: "none",
6664 min_sdk_version: "29",
6665 system_modules: "none",
6666 apex_available: [ "myapex" ],
6667 }`)
6668}
6669
Jiyong Park89e850a2020-04-07 16:37:39 +09006670func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006671 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006672 apex {
6673 name: "myapex",
6674 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006675 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006676 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006677 }
6678
6679 apex_key {
6680 name: "myapex.key",
6681 public_key: "testkey.avbpubkey",
6682 private_key: "testkey.pem",
6683 }
6684
6685 cc_library {
6686 name: "libfoo",
6687 stl: "none",
6688 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006689 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006690 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006691 }
6692
6693 cc_library {
6694 name: "libfoo2",
6695 stl: "none",
6696 system_shared_libs: [],
6697 shared_libs: ["libbaz"],
6698 apex_available: ["//apex_available:platform"],
6699 }
6700
6701 cc_library {
6702 name: "libbar",
6703 stl: "none",
6704 system_shared_libs: [],
6705 apex_available: ["myapex"],
6706 }
6707
6708 cc_library {
6709 name: "libbaz",
6710 stl: "none",
6711 system_shared_libs: [],
6712 apex_available: ["myapex"],
6713 stubs: {
6714 versions: ["1"],
6715 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006716 }`)
6717
Jiyong Park89e850a2020-04-07 16:37:39 +09006718 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6719 // because it depends on libbar which isn't available to platform
6720 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6721 if libfoo.NotAvailableForPlatform() != true {
6722 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6723 }
6724
6725 // libfoo2 however can be available to platform because it depends on libbaz which provides
6726 // stubs
6727 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6728 if libfoo2.NotAvailableForPlatform() == true {
6729 t.Errorf("%q should be available to platform", libfoo2.String())
6730 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006731}
Jiyong Parka90ca002019-10-07 15:47:24 +09006732
Paul Duffine52e66f2020-03-30 17:54:29 +01006733func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006734 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006735 apex {
6736 name: "myapex",
6737 key: "myapex.key",
6738 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006739 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006740 }
6741
6742 apex_key {
6743 name: "myapex.key",
6744 public_key: "testkey.avbpubkey",
6745 private_key: "testkey.pem",
6746 }
6747
6748 cc_library {
6749 name: "libfoo",
6750 stl: "none",
6751 system_shared_libs: [],
6752 apex_available: ["myapex"],
6753 static: {
6754 apex_available: ["//apex_available:platform"],
6755 },
6756 }`)
6757
Jiyong Park89e850a2020-04-07 16:37:39 +09006758 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6759 if libfooShared.NotAvailableForPlatform() != true {
6760 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6761 }
6762 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6763 if libfooStatic.NotAvailableForPlatform() != false {
6764 t.Errorf("%q should be available to platform", libfooStatic.String())
6765 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006766}
6767
Jiyong Park5d790c32019-11-15 18:40:32 +09006768func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006769 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006770 apex {
6771 name: "myapex",
6772 key: "myapex.key",
6773 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006774 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006775 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006776 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006777 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006778 }
6779
6780 override_apex {
6781 name: "override_myapex",
6782 base: "myapex",
6783 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006784 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006785 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006786 overrides: ["unknownapex"],
Jesse Melhuishec60e252024-03-29 19:08:20 +00006787 compile_multilib: "first",
6788 multilib: {
6789 lib32: {
6790 native_shared_libs: ["mylib32"],
6791 },
6792 lib64: {
6793 native_shared_libs: ["mylib64"],
6794 },
6795 },
Baligh Uddin004d7172020-02-19 21:29:28 -08006796 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006797 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006798 key: "mynewapex.key",
6799 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006800 }
6801
6802 apex_key {
6803 name: "myapex.key",
6804 public_key: "testkey.avbpubkey",
6805 private_key: "testkey.pem",
6806 }
6807
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006808 apex_key {
6809 name: "mynewapex.key",
6810 public_key: "testkey2.avbpubkey",
6811 private_key: "testkey2.pem",
6812 }
6813
6814 android_app_certificate {
6815 name: "myapex.certificate",
6816 certificate: "testkey",
6817 }
6818
Jiyong Park5d790c32019-11-15 18:40:32 +09006819 android_app {
6820 name: "app",
6821 srcs: ["foo/bar/MyClass.java"],
6822 package_name: "foo",
6823 sdk_version: "none",
6824 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006825 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006826 }
6827
6828 override_android_app {
6829 name: "override_app",
6830 base: "app",
6831 package_name: "bar",
6832 }
markchien7c803b82021-08-26 22:10:06 +08006833
6834 bpf {
6835 name: "bpf",
6836 srcs: ["bpf.c"],
6837 }
6838
6839 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006840 name: "overrideBpf",
6841 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006842 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006843
6844 prebuilt_etc {
6845 name: "myetc",
6846 src: "myprebuilt",
6847 }
6848
6849 prebuilt_etc {
6850 name: "override_myetc",
6851 src: "override_myprebuilt",
6852 }
Jesse Melhuishec60e252024-03-29 19:08:20 +00006853
6854 cc_library {
6855 name: "mylib32",
6856 apex_available: [ "myapex" ],
6857 }
6858
6859 cc_library {
6860 name: "mylib64",
6861 apex_available: [ "myapex" ],
6862 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006863 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006864
Jooyung Hana0503a52023-08-23 13:12:50 +09006865 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(android.OverridableModule)
6866 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex").Module().(android.OverridableModule)
Jiyong Park317645e2019-12-05 13:20:58 +09006867 if originalVariant.GetOverriddenBy() != "" {
6868 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6869 }
6870 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6871 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6872 }
6873
Jooyung Hana0503a52023-08-23 13:12:50 +09006874 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006875 apexRule := module.Rule("apexRule")
6876 copyCmds := apexRule.Args["copy_commands"]
6877
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006878 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6879 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006880
markchien7c803b82021-08-26 22:10:06 +08006881 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006882 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006883
Daniel Norman5a3ce132021-08-26 15:44:43 -07006884 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6885 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6886
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006887 apexBundle := module.Module().(*apexBundle)
6888 name := apexBundle.Name()
6889 if name != "override_myapex" {
6890 t.Errorf("name should be \"override_myapex\", but was %q", name)
6891 }
6892
Baligh Uddin004d7172020-02-19 21:29:28 -08006893 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6894 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6895 }
6896
Jiyong Park20bacab2020-03-03 11:45:41 +09006897 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006898 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006899 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6900
6901 signApkRule := module.Rule("signapk")
6902 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006903
Colin Crossaa255532020-07-03 13:18:24 -07006904 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006905 var builder strings.Builder
6906 data.Custom(&builder, name, "TARGET_", "", data)
6907 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006908 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6909 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006910 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006911 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006912 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006913 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006914 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006915 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006916}
6917
Albert Martineefabcf2022-03-21 20:11:16 +00006918func TestMinSdkVersionOverride(t *testing.T) {
6919 // Override from 29 to 31
6920 minSdkOverride31 := "31"
6921 ctx := testApex(t, `
6922 apex {
6923 name: "myapex",
6924 key: "myapex.key",
6925 native_shared_libs: ["mylib"],
6926 updatable: true,
6927 min_sdk_version: "29"
6928 }
6929
6930 override_apex {
6931 name: "override_myapex",
6932 base: "myapex",
6933 logging_parent: "com.foo.bar",
6934 package_name: "test.overridden.package"
6935 }
6936
6937 apex_key {
6938 name: "myapex.key",
6939 public_key: "testkey.avbpubkey",
6940 private_key: "testkey.pem",
6941 }
6942
6943 cc_library {
6944 name: "mylib",
6945 srcs: ["mylib.cpp"],
6946 runtime_libs: ["libbar"],
6947 system_shared_libs: [],
6948 stl: "none",
6949 apex_available: [ "myapex" ],
6950 min_sdk_version: "apex_inherit"
6951 }
6952
6953 cc_library {
6954 name: "libbar",
6955 srcs: ["mylib.cpp"],
6956 system_shared_libs: [],
6957 stl: "none",
6958 apex_available: [ "myapex" ],
6959 min_sdk_version: "apex_inherit"
6960 }
6961
6962 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6963
Jooyung Hana0503a52023-08-23 13:12:50 +09006964 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00006965 copyCmds := apexRule.Args["copy_commands"]
6966
6967 // Ensure that direct non-stubs dep is always included
6968 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6969
6970 // Ensure that runtime_libs dep in included
6971 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6972
6973 // Ensure libraries target overridden min_sdk_version value
6974 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6975}
6976
6977func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6978 // Attempt to override from 31 to 29, should be a NOOP
6979 minSdkOverride29 := "29"
6980 ctx := testApex(t, `
6981 apex {
6982 name: "myapex",
6983 key: "myapex.key",
6984 native_shared_libs: ["mylib"],
6985 updatable: true,
6986 min_sdk_version: "31"
6987 }
6988
6989 override_apex {
6990 name: "override_myapex",
6991 base: "myapex",
6992 logging_parent: "com.foo.bar",
6993 package_name: "test.overridden.package"
6994 }
6995
6996 apex_key {
6997 name: "myapex.key",
6998 public_key: "testkey.avbpubkey",
6999 private_key: "testkey.pem",
7000 }
7001
7002 cc_library {
7003 name: "mylib",
7004 srcs: ["mylib.cpp"],
7005 runtime_libs: ["libbar"],
7006 system_shared_libs: [],
7007 stl: "none",
7008 apex_available: [ "myapex" ],
7009 min_sdk_version: "apex_inherit"
7010 }
7011
7012 cc_library {
7013 name: "libbar",
7014 srcs: ["mylib.cpp"],
7015 system_shared_libs: [],
7016 stl: "none",
7017 apex_available: [ "myapex" ],
7018 min_sdk_version: "apex_inherit"
7019 }
7020
7021 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
7022
Jooyung Hana0503a52023-08-23 13:12:50 +09007023 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007024 copyCmds := apexRule.Args["copy_commands"]
7025
7026 // Ensure that direct non-stubs dep is always included
7027 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7028
7029 // Ensure that runtime_libs dep in included
7030 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7031
7032 // Ensure libraries target the original min_sdk_version value rather than the overridden
7033 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7034}
7035
Jooyung Han214bf372019-11-12 13:03:50 +09007036func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007037 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09007038 apex {
7039 name: "myapex",
7040 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007041 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09007042 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09007043 }
7044
7045 apex_key {
7046 name: "myapex.key",
7047 public_key: "testkey.avbpubkey",
7048 private_key: "testkey.pem",
7049 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007050
7051 cc_library {
7052 name: "mylib",
7053 srcs: ["mylib.cpp"],
7054 stl: "libc++",
7055 system_shared_libs: [],
7056 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09007057 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007058 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007059 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09007060
Jooyung Hana0503a52023-08-23 13:12:50 +09007061 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +09007062 args := module.Rule("apexRule").Args
7063 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00007064 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007065
7066 // The copies of the libraries in the apex should have one more dependency than
7067 // the ones outside the apex, namely the unwinder. Ideally we should check
7068 // the dependency names directly here but for some reason the names are blank in
7069 // this test.
7070 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07007071 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007072 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
7073 if len(apexImplicits) != len(nonApexImplicits)+1 {
7074 t.Errorf("%q missing unwinder dep", lib)
7075 }
7076 }
Jooyung Han214bf372019-11-12 13:03:50 +09007077}
7078
Paul Duffine05480a2021-03-08 15:07:14 +00007079var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01007080 "api/current.txt": nil,
7081 "api/removed.txt": nil,
7082 "api/system-current.txt": nil,
7083 "api/system-removed.txt": nil,
7084 "api/test-current.txt": nil,
7085 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01007086
Anton Hanssondff2c782020-12-21 17:10:01 +00007087 "100/public/api/foo.txt": nil,
7088 "100/public/api/foo-removed.txt": nil,
7089 "100/system/api/foo.txt": nil,
7090 "100/system/api/foo-removed.txt": nil,
7091
Paul Duffineedc5d52020-06-12 17:46:39 +01007092 // For java_sdk_library_import
7093 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01007094}
7095
Jooyung Han58f26ab2019-12-18 15:34:32 +09007096func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007097 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09007098 apex {
7099 name: "myapex",
7100 key: "myapex.key",
7101 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007102 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09007103 }
7104
7105 apex_key {
7106 name: "myapex.key",
7107 public_key: "testkey.avbpubkey",
7108 private_key: "testkey.pem",
7109 }
7110
7111 java_sdk_library {
7112 name: "foo",
7113 srcs: ["a.java"],
7114 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007115 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09007116 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007117
7118 prebuilt_apis {
7119 name: "sdk",
7120 api_dirs: ["100"],
7121 }
Paul Duffin9b879592020-05-26 13:21:35 +01007122 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09007123
7124 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007125 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09007126 "javalib/foo.jar",
7127 "etc/permissions/foo.xml",
7128 })
7129 // Permission XML should point to the activated path of impl jar of java_sdk_library
Paul Duffin1816cde2024-04-10 10:58:21 +01007130 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Output("foo.xml")
7131 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7132 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
Jooyung Han58f26ab2019-12-18 15:34:32 +09007133}
7134
Paul Duffin9b879592020-05-26 13:21:35 +01007135func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007136 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007137 apex {
7138 name: "myapex",
7139 key: "myapex.key",
7140 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007141 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007142 }
7143
7144 apex_key {
7145 name: "myapex.key",
7146 public_key: "testkey.avbpubkey",
7147 private_key: "testkey.pem",
7148 }
7149
7150 java_sdk_library {
7151 name: "foo",
7152 srcs: ["a.java"],
7153 api_packages: ["foo"],
7154 apex_available: ["myapex"],
7155 sdk_version: "none",
7156 system_modules: "none",
7157 }
7158
7159 java_library {
7160 name: "bar",
7161 srcs: ["a.java"],
7162 libs: ["foo"],
7163 apex_available: ["myapex"],
7164 sdk_version: "none",
7165 system_modules: "none",
7166 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007167
7168 prebuilt_apis {
7169 name: "sdk",
7170 api_dirs: ["100"],
7171 }
Paul Duffin9b879592020-05-26 13:21:35 +01007172 `, withFiles(filesForSdkLibrary))
7173
7174 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007175 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007176 "javalib/bar.jar",
7177 "javalib/foo.jar",
7178 "etc/permissions/foo.xml",
7179 })
7180
7181 // The bar library should depend on the implementation jar.
7182 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kang8479dea2024-04-04 01:19:05 +00007183 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007184 t.Errorf("expected %q, found %#q", expected, actual)
7185 }
7186}
7187
7188func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007189 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007190 apex {
7191 name: "myapex",
7192 key: "myapex.key",
7193 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007194 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007195 }
7196
7197 apex_key {
7198 name: "myapex.key",
7199 public_key: "testkey.avbpubkey",
7200 private_key: "testkey.pem",
7201 }
7202
7203 java_sdk_library {
7204 name: "foo",
7205 srcs: ["a.java"],
7206 api_packages: ["foo"],
7207 apex_available: ["myapex"],
7208 sdk_version: "none",
7209 system_modules: "none",
7210 }
7211
7212 java_library {
7213 name: "bar",
7214 srcs: ["a.java"],
7215 libs: ["foo"],
7216 sdk_version: "none",
7217 system_modules: "none",
7218 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007219
7220 prebuilt_apis {
7221 name: "sdk",
7222 api_dirs: ["100"],
7223 }
Paul Duffin9b879592020-05-26 13:21:35 +01007224 `, withFiles(filesForSdkLibrary))
7225
7226 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007227 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007228 "javalib/foo.jar",
7229 "etc/permissions/foo.xml",
7230 })
7231
7232 // The bar library should depend on the stubs jar.
7233 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007234 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007235 t.Errorf("expected %q, found %#q", expected, actual)
7236 }
7237}
7238
Paul Duffineedc5d52020-06-12 17:46:39 +01007239func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007240 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00007241 prebuilt_apis {
7242 name: "sdk",
7243 api_dirs: ["100"],
7244 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01007245 withFiles(map[string][]byte{
7246 "apex/a.java": nil,
7247 "apex/apex_manifest.json": nil,
7248 "apex/Android.bp": []byte(`
7249 package {
7250 default_visibility: ["//visibility:private"],
7251 }
7252
7253 apex {
7254 name: "myapex",
7255 key: "myapex.key",
7256 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007257 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007258 }
7259
7260 apex_key {
7261 name: "myapex.key",
7262 public_key: "testkey.avbpubkey",
7263 private_key: "testkey.pem",
7264 }
7265
7266 java_library {
7267 name: "bar",
7268 srcs: ["a.java"],
7269 libs: ["foo"],
7270 apex_available: ["myapex"],
7271 sdk_version: "none",
7272 system_modules: "none",
7273 }
7274`),
7275 "source/a.java": nil,
7276 "source/api/current.txt": nil,
7277 "source/api/removed.txt": nil,
7278 "source/Android.bp": []byte(`
7279 package {
7280 default_visibility: ["//visibility:private"],
7281 }
7282
7283 java_sdk_library {
7284 name: "foo",
7285 visibility: ["//apex"],
7286 srcs: ["a.java"],
7287 api_packages: ["foo"],
7288 apex_available: ["myapex"],
7289 sdk_version: "none",
7290 system_modules: "none",
7291 public: {
7292 enabled: true,
7293 },
7294 }
7295`),
7296 "prebuilt/a.jar": nil,
7297 "prebuilt/Android.bp": []byte(`
7298 package {
7299 default_visibility: ["//visibility:private"],
7300 }
7301
7302 java_sdk_library_import {
7303 name: "foo",
7304 visibility: ["//apex", "//source"],
7305 apex_available: ["myapex"],
7306 prefer: true,
7307 public: {
7308 jars: ["a.jar"],
7309 },
7310 }
7311`),
Anton Hanssondff2c782020-12-21 17:10:01 +00007312 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01007313 )
7314
7315 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007316 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffineedc5d52020-06-12 17:46:39 +01007317 "javalib/bar.jar",
7318 "javalib/foo.jar",
7319 "etc/permissions/foo.xml",
7320 })
7321
7322 // The bar library should depend on the implementation jar.
7323 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007324 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01007325 t.Errorf("expected %q, found %#q", expected, actual)
7326 }
7327}
7328
7329func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
7330 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
7331 apex {
7332 name: "myapex",
7333 key: "myapex.key",
7334 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007335 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007336 }
7337
7338 apex_key {
7339 name: "myapex.key",
7340 public_key: "testkey.avbpubkey",
7341 private_key: "testkey.pem",
7342 }
7343
7344 java_sdk_library_import {
7345 name: "foo",
7346 apex_available: ["myapex"],
7347 prefer: true,
7348 public: {
7349 jars: ["a.jar"],
7350 },
7351 }
7352
7353 `, withFiles(filesForSdkLibrary))
7354}
7355
atrost6e126252020-01-27 17:01:16 +00007356func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007357 result := android.GroupFixturePreparers(
7358 prepareForApexTest,
7359 java.PrepareForTestWithPlatformCompatConfig,
7360 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007361 apex {
7362 name: "myapex",
7363 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007364 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007365 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007366 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007367 }
7368
7369 apex_key {
7370 name: "myapex.key",
7371 public_key: "testkey.avbpubkey",
7372 private_key: "testkey.pem",
7373 }
7374
7375 platform_compat_config {
7376 name: "myjar-platform-compat-config",
7377 src: ":myjar",
7378 }
7379
7380 java_library {
7381 name: "myjar",
7382 srcs: ["foo/bar/MyClass.java"],
7383 sdk_version: "none",
7384 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007385 apex_available: [ "myapex" ],
7386 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007387
7388 // Make sure that a preferred prebuilt does not affect the apex contents.
7389 prebuilt_platform_compat_config {
7390 name: "myjar-platform-compat-config",
7391 metadata: "compat-config/metadata.xml",
7392 prefer: true,
7393 }
atrost6e126252020-01-27 17:01:16 +00007394 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007395 ctx := result.TestContext
Jooyung Hana0503a52023-08-23 13:12:50 +09007396 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
atrost6e126252020-01-27 17:01:16 +00007397 "etc/compatconfig/myjar-platform-compat-config.xml",
7398 "javalib/myjar.jar",
7399 })
7400}
7401
Jooyung Han862c0d62022-12-21 10:15:37 +09007402func TestNoDupeApexFiles(t *testing.T) {
7403 android.GroupFixturePreparers(
7404 android.PrepareForTestWithAndroidBuildComponents,
7405 PrepareForTestWithApexBuildComponents,
7406 prepareForTestWithMyapex,
7407 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7408 ).
7409 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7410 RunTestWithBp(t, `
7411 apex {
7412 name: "myapex",
7413 key: "myapex.key",
7414 prebuilts: ["foo", "bar"],
7415 updatable: false,
7416 }
7417
7418 apex_key {
7419 name: "myapex.key",
7420 public_key: "testkey.avbpubkey",
7421 private_key: "testkey.pem",
7422 }
7423
7424 prebuilt_etc {
7425 name: "foo",
7426 src: "myprebuilt",
7427 filename_from_src: true,
7428 }
7429
7430 prebuilt_etc {
7431 name: "bar",
7432 src: "myprebuilt",
7433 filename_from_src: true,
7434 }
7435 `)
7436}
7437
Jooyung Hana8bd72a2023-11-02 11:56:48 +09007438func TestApexUnwantedTransitiveDeps(t *testing.T) {
7439 bp := `
7440 apex {
7441 name: "myapex",
7442 key: "myapex.key",
7443 native_shared_libs: ["libfoo"],
7444 updatable: false,
7445 unwanted_transitive_deps: ["libbar"],
7446 }
7447
7448 apex_key {
7449 name: "myapex.key",
7450 public_key: "testkey.avbpubkey",
7451 private_key: "testkey.pem",
7452 }
7453
7454 cc_library {
7455 name: "libfoo",
7456 srcs: ["foo.cpp"],
7457 shared_libs: ["libbar"],
7458 apex_available: ["myapex"],
7459 }
7460
7461 cc_library {
7462 name: "libbar",
7463 srcs: ["bar.cpp"],
7464 apex_available: ["myapex"],
7465 }`
7466 ctx := testApex(t, bp)
7467 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
7468 "*/libc++.so",
7469 "*/libfoo.so",
7470 // not libbar.so
7471 })
7472}
7473
Jiyong Park479321d2019-12-16 11:47:12 +09007474func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7475 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7476 apex {
7477 name: "myapex",
7478 key: "myapex.key",
7479 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007480 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007481 }
7482
7483 apex_key {
7484 name: "myapex.key",
7485 public_key: "testkey.avbpubkey",
7486 private_key: "testkey.pem",
7487 }
7488
7489 java_library {
7490 name: "myjar",
7491 srcs: ["foo/bar/MyClass.java"],
7492 sdk_version: "none",
7493 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007494 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007495 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007496 }
7497 `)
7498}
7499
Jiyong Park7afd1072019-12-30 16:56:33 +09007500func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007501 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007502 apex {
7503 name: "myapex",
7504 key: "myapex.key",
7505 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007506 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007507 }
7508
7509 apex_key {
7510 name: "myapex.key",
7511 public_key: "testkey.avbpubkey",
7512 private_key: "testkey.pem",
7513 }
7514
7515 cc_library {
7516 name: "mylib",
7517 srcs: ["mylib.cpp"],
7518 system_shared_libs: [],
7519 stl: "none",
7520 required: ["a", "b"],
7521 host_required: ["c", "d"],
7522 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007523 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007524 }
7525 `)
7526
Jooyung Hana0503a52023-08-23 13:12:50 +09007527 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007528 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007529 name := apexBundle.BaseModuleName()
7530 prefix := "TARGET_"
7531 var builder strings.Builder
7532 data.Custom(&builder, name, prefix, "", data)
7533 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007534 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 a b\n")
Sasha Smundakdcb61292022-12-08 10:41:33 -08007535 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7536 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007537}
7538
Jiyong Park7cd10e32020-01-14 09:22:18 +09007539func TestSymlinksFromApexToSystem(t *testing.T) {
7540 bp := `
7541 apex {
7542 name: "myapex",
7543 key: "myapex.key",
7544 native_shared_libs: ["mylib"],
7545 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007546 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007547 }
7548
Jiyong Park9d677202020-02-19 16:29:35 +09007549 apex {
7550 name: "myapex.updatable",
7551 key: "myapex.key",
7552 native_shared_libs: ["mylib"],
7553 java_libs: ["myjar"],
7554 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007555 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007556 }
7557
Jiyong Park7cd10e32020-01-14 09:22:18 +09007558 apex_key {
7559 name: "myapex.key",
7560 public_key: "testkey.avbpubkey",
7561 private_key: "testkey.pem",
7562 }
7563
7564 cc_library {
7565 name: "mylib",
7566 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007567 shared_libs: [
7568 "myotherlib",
7569 "myotherlib_ext",
7570 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007571 system_shared_libs: [],
7572 stl: "none",
7573 apex_available: [
7574 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007575 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007576 "//apex_available:platform",
7577 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007578 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007579 }
7580
7581 cc_library {
7582 name: "myotherlib",
7583 srcs: ["mylib.cpp"],
7584 system_shared_libs: [],
7585 stl: "none",
7586 apex_available: [
7587 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007588 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007589 "//apex_available:platform",
7590 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007591 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007592 }
7593
Jiyong Parkce243632023-02-17 18:22:25 +09007594 cc_library {
7595 name: "myotherlib_ext",
7596 srcs: ["mylib.cpp"],
7597 system_shared_libs: [],
7598 system_ext_specific: true,
7599 stl: "none",
7600 apex_available: [
7601 "myapex",
7602 "myapex.updatable",
7603 "//apex_available:platform",
7604 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007605 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007606 }
7607
Jiyong Park7cd10e32020-01-14 09:22:18 +09007608 java_library {
7609 name: "myjar",
7610 srcs: ["foo/bar/MyClass.java"],
7611 sdk_version: "none",
7612 system_modules: "none",
7613 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007614 apex_available: [
7615 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007616 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007617 "//apex_available:platform",
7618 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007619 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007620 }
7621
7622 java_library {
7623 name: "myotherjar",
7624 srcs: ["foo/bar/MyClass.java"],
7625 sdk_version: "none",
7626 system_modules: "none",
7627 apex_available: [
7628 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007629 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007630 "//apex_available:platform",
7631 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007632 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007633 }
7634 `
7635
7636 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7637 for _, f := range files {
7638 if f.path == file {
7639 if f.isLink {
7640 t.Errorf("%q is not a real file", file)
7641 }
7642 return
7643 }
7644 }
7645 t.Errorf("%q is not found", file)
7646 }
7647
Jiyong Parkce243632023-02-17 18:22:25 +09007648 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007649 for _, f := range files {
7650 if f.path == file {
7651 if !f.isLink {
7652 t.Errorf("%q is not a symlink", file)
7653 }
Jiyong Parkce243632023-02-17 18:22:25 +09007654 if f.src != target {
7655 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7656 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007657 return
7658 }
7659 }
7660 t.Errorf("%q is not found", file)
7661 }
7662
Jiyong Park9d677202020-02-19 16:29:35 +09007663 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7664 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007665 ctx := testApex(t, bp, withUnbundledBuild)
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")
7669 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007670 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +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")
7675 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007676 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007677
7678 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007679 ctx = testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +09007680 files = getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007681 ensureRealfileExists(t, files, "javalib/myjar.jar")
7682 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007683 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7684 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007685
Jooyung Hana0503a52023-08-23 13:12:50 +09007686 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007687 ensureRealfileExists(t, files, "javalib/myjar.jar")
7688 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007689 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7690 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007691}
7692
Yo Chiange8128052020-07-23 20:09:18 +08007693func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007694 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007695 apex {
7696 name: "myapex",
7697 key: "myapex.key",
7698 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007699 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007700 }
7701
7702 apex_key {
7703 name: "myapex.key",
7704 public_key: "testkey.avbpubkey",
7705 private_key: "testkey.pem",
7706 }
7707
7708 cc_library_shared {
7709 name: "mylib",
7710 srcs: ["mylib.cpp"],
7711 shared_libs: ["myotherlib"],
7712 system_shared_libs: [],
7713 stl: "none",
7714 apex_available: [
7715 "myapex",
7716 "//apex_available:platform",
7717 ],
7718 }
7719
7720 cc_prebuilt_library_shared {
7721 name: "myotherlib",
7722 srcs: ["prebuilt.so"],
7723 system_shared_libs: [],
7724 stl: "none",
7725 apex_available: [
7726 "myapex",
7727 "//apex_available:platform",
7728 ],
7729 }
7730 `)
7731
Jooyung Hana0503a52023-08-23 13:12:50 +09007732 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007733 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007734 var builder strings.Builder
7735 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7736 androidMk := builder.String()
7737 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007738 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007739 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7740 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7741 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007742 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 myotherlib:64\n")
Yo Chiange8128052020-07-23 20:09:18 +08007743}
7744
Jooyung Han643adc42020-02-27 13:50:06 +09007745func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007746 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007747 apex {
7748 name: "myapex",
7749 key: "myapex.key",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007750 binaries: ["mybin"],
7751 jni_libs: ["mylib", "mylib3", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007752 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007753 }
7754
7755 apex_key {
7756 name: "myapex.key",
7757 public_key: "testkey.avbpubkey",
7758 private_key: "testkey.pem",
7759 }
7760
7761 cc_library {
7762 name: "mylib",
7763 srcs: ["mylib.cpp"],
7764 shared_libs: ["mylib2"],
7765 system_shared_libs: [],
7766 stl: "none",
7767 apex_available: [ "myapex" ],
7768 }
7769
7770 cc_library {
7771 name: "mylib2",
7772 srcs: ["mylib.cpp"],
7773 system_shared_libs: [],
7774 stl: "none",
7775 apex_available: [ "myapex" ],
7776 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007777
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007778 // Used as both a JNI library and a regular shared library.
7779 cc_library {
7780 name: "mylib3",
7781 srcs: ["mylib.cpp"],
7782 system_shared_libs: [],
7783 stl: "none",
7784 apex_available: [ "myapex" ],
7785 }
7786
7787 cc_binary {
7788 name: "mybin",
7789 srcs: ["mybin.cpp"],
7790 shared_libs: ["mylib3"],
7791 system_shared_libs: [],
7792 stl: "none",
7793 apex_available: [ "myapex" ],
7794 }
7795
Jiyong Park34d5c332022-02-24 18:02:44 +09007796 rust_ffi_shared {
7797 name: "libfoo.rust",
7798 crate_name: "foo",
7799 srcs: ["foo.rs"],
7800 shared_libs: ["libfoo.shared_from_rust"],
7801 prefer_rlib: true,
7802 apex_available: ["myapex"],
7803 }
7804
7805 cc_library_shared {
7806 name: "libfoo.shared_from_rust",
7807 srcs: ["mylib.cpp"],
7808 system_shared_libs: [],
7809 stl: "none",
7810 stubs: {
7811 versions: ["10", "11", "12"],
7812 },
7813 }
7814
Jooyung Han643adc42020-02-27 13:50:06 +09007815 `)
7816
Jooyung Hana0503a52023-08-23 13:12:50 +09007817 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han643adc42020-02-27 13:50:06 +09007818 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007819 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so mylib3.so")
Jooyung Hana0503a52023-08-23 13:12:50 +09007820 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007821 "bin/mybin",
Jooyung Han643adc42020-02-27 13:50:06 +09007822 "lib64/mylib.so",
7823 "lib64/mylib2.so",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007824 "lib64/mylib3.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007825 "lib64/libfoo.rust.so",
7826 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7827 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007828 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007829
7830 // b/220397949
7831 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007832}
7833
Jooyung Han49f67012020-04-17 13:43:10 +09007834func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007835 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007836 apex {
7837 name: "myapex",
7838 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007839 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007840 }
7841 apex_key {
7842 name: "myapex.key",
7843 public_key: "testkey.avbpubkey",
7844 private_key: "testkey.pem",
7845 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007846 `,
7847 android.FixtureModifyConfig(func(config android.Config) {
7848 delete(config.Targets, android.Android)
7849 config.AndroidCommonTarget = android.Target{}
7850 }),
7851 )
Jooyung Han49f67012020-04-17 13:43:10 +09007852
7853 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7854 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7855 }
7856}
7857
Jiyong Parkbd159612020-02-28 15:22:21 +09007858func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007859 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007860 apex {
7861 name: "myapex",
7862 key: "myapex.key",
7863 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007864 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007865 }
7866
7867 apex_key {
7868 name: "myapex.key",
7869 public_key: "testkey.avbpubkey",
7870 private_key: "testkey.pem",
7871 }
7872
7873 android_app {
7874 name: "AppFoo",
7875 srcs: ["foo/bar/MyClass.java"],
7876 sdk_version: "none",
7877 system_modules: "none",
7878 apex_available: [ "myapex" ],
7879 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007880 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007881
Jooyung Hana0503a52023-08-23 13:12:50 +09007882 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07007883 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Jiyong Parkbd159612020-02-28 15:22:21 +09007884
7885 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007886 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 +09007887}
7888
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007889func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007890 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007891 apex {
7892 name: "myapex",
7893 key: "myapex.key",
7894 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007895 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007896 }
7897
7898 apex_key {
7899 name: "myapex.key",
7900 public_key: "testkey.avbpubkey",
7901 private_key: "testkey.pem",
7902 }
7903
7904 android_app_set {
7905 name: "AppSet",
7906 set: "AppSet.apks",
7907 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09007908 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crosscf371cc2020-11-13 11:48:42 -08007909 bundleConfigRule := mod.Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07007910 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007911 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7912 s := mod.Rule("apexRule").Args["copy_commands"]
7913 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007914 if len(copyCmds) != 4 {
7915 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007916 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007917 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7918 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007919 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7920 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007921
7922 // Ensure that canned_fs_config has an entry for the app set zip file
7923 generateFsRule := mod.Rule("generateFsConfig")
7924 cmd := generateFsRule.RuleParams.Command
7925 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007926}
7927
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007928func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007929 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007930 apex_set {
7931 name: "myapex",
7932 filename: "foo_v2.apex",
7933 sanitized: {
7934 none: { set: "myapex.apks", },
7935 hwaddress: { set: "myapex.hwasan.apks", },
7936 },
Paul Duffin24704672021-04-06 16:09:30 +01007937 }
7938 `
7939 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007940
Paul Duffin24704672021-04-06 16:09:30 +01007941 // Check that the extractor produces the correct output file from the correct input file.
Spandan Das3576e762024-01-03 18:57:03 +00007942 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007943
Spandan Das3576e762024-01-03 18:57:03 +00007944 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Paul Duffin24704672021-04-06 16:09:30 +01007945 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007946
Paul Duffin24704672021-04-06 16:09:30 +01007947 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7948
7949 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007950 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7951 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007952
7953 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007954}
7955
Pranav Guptaeba03b02022-09-27 00:27:08 +00007956func TestApexSetApksModuleAssignment(t *testing.T) {
7957 ctx := testApex(t, `
7958 apex_set {
7959 name: "myapex",
7960 set: ":myapex_apks_file",
7961 }
7962
7963 filegroup {
7964 name: "myapex_apks_file",
7965 srcs: ["myapex.apks"],
7966 }
7967 `)
7968
Spandan Das3576e762024-01-03 18:57:03 +00007969 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Pranav Guptaeba03b02022-09-27 00:27:08 +00007970
7971 // Check that the extractor produces the correct apks file from the input module
Spandan Das3576e762024-01-03 18:57:03 +00007972 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.apks"
Pranav Guptaeba03b02022-09-27 00:27:08 +00007973 extractedApex := m.Output(extractorOutput)
7974
7975 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7976}
7977
Paul Duffin89f570a2021-06-16 01:42:33 +01007978func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007979 t.Helper()
7980
Paul Duffin55607122021-03-30 23:32:51 +01007981 fs := android.MockFS{
7982 "a.java": nil,
7983 "a.jar": nil,
7984 "apex_manifest.json": nil,
7985 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007986 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007987 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7988 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7989 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007990 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007991 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007992
Paul Duffin55607122021-03-30 23:32:51 +01007993 errorHandler := android.FixtureExpectsNoErrors
7994 if errmsg != "" {
7995 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007996 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007997
Paul Duffin55607122021-03-30 23:32:51 +01007998 result := android.GroupFixturePreparers(
7999 cc.PrepareForTestWithCcDefaultModules,
8000 java.PrepareForTestWithHiddenApiBuildComponents,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008001 java.PrepareForTestWithDexpreopt,
Paul Duffin55607122021-03-30 23:32:51 +01008002 java.PrepareForTestWithJavaSdkLibraryFiles,
8003 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01008004 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01008005 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01008006 android.FixtureModifyMockFS(func(fs android.MockFS) {
8007 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
8008 insert := ""
8009 for _, fragment := range fragments {
8010 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
8011 }
8012 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
8013 platform_bootclasspath {
8014 name: "platform-bootclasspath",
8015 fragments: [
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008016 {apex: "com.android.art", module: "art-bootclasspath-fragment"},
Paul Duffin89f570a2021-06-16 01:42:33 +01008017 %s
8018 ],
8019 }
8020 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01008021 }
Paul Duffin89f570a2021-06-16 01:42:33 +01008022 }),
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008023 // Dexpreopt for boot jars requires the ART boot image profile.
8024 java.PrepareApexBootJarModule("com.android.art", "core-oj"),
8025 dexpreopt.FixtureSetArtBootJars("com.android.art:core-oj"),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00008026 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01008027 ).
8028 ExtendWithErrorHandler(errorHandler).
8029 RunTestWithBp(t, bp)
8030
8031 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008032}
8033
Paul Duffin5556c5f2022-06-09 17:32:21 +00008034func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008035 preparers := android.GroupFixturePreparers(
8036 java.PrepareForTestWithJavaDefaultModules,
Spandan Das5be63332023-12-13 00:06:32 +00008037 prepareForTestWithBootclasspathFragment,
8038 dexpreopt.FixtureSetTestOnlyArtBootImageJars("com.android.art:libfoo"),
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008039 PrepareForTestWithApexBuildComponents,
8040 ).
8041 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
Spandan Das3576e762024-01-03 18:57:03 +00008042 "Multiple installable prebuilt APEXes provide ambiguous deapexers: prebuilt_com.android.art and prebuilt_com.mycompany.android.art"))
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008043
8044 bpBase := `
8045 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008046 name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008047 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008048 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008049 set: "myapex.apks",
8050 }
8051
8052 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008053 name: "com.mycompany.android.art",
8054 apex_name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008055 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008056 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008057 set: "company-myapex.apks",
8058 }
8059
8060 prebuilt_bootclasspath_fragment {
Spandan Das5be63332023-12-13 00:06:32 +00008061 name: "art-bootclasspath-fragment",
8062 apex_available: ["com.android.art"],
Spandan Dasfae468e2023-12-12 23:23:53 +00008063 hidden_api: {
8064 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8065 metadata: "my-bootclasspath-fragment/metadata.csv",
8066 index: "my-bootclasspath-fragment/index.csv",
8067 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
8068 all_flags: "my-bootclasspath-fragment/all-flags.csv",
8069 },
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008070 %s
8071 }
8072 `
8073
8074 t.Run("java_import", func(t *testing.T) {
8075 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8076 java_import {
8077 name: "libfoo",
8078 jars: ["libfoo.jar"],
Spandan Das5be63332023-12-13 00:06:32 +00008079 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008080 }
8081 `)
8082 })
8083
8084 t.Run("java_sdk_library_import", func(t *testing.T) {
8085 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8086 java_sdk_library_import {
8087 name: "libfoo",
8088 public: {
8089 jars: ["libbar.jar"],
8090 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008091 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008092 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008093 }
8094 `)
8095 })
8096
8097 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8098 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8099 image_name: "art",
8100 contents: ["libfoo"],
8101 `)+`
8102 java_sdk_library_import {
8103 name: "libfoo",
8104 public: {
8105 jars: ["libbar.jar"],
8106 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008107 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008108 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008109 }
8110 `)
8111 })
8112}
8113
Paul Duffin5556c5f2022-06-09 17:32:21 +00008114func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
8115 preparers := android.GroupFixturePreparers(
8116 java.PrepareForTestWithJavaDefaultModules,
8117 PrepareForTestWithApexBuildComponents,
8118 )
8119
Spandan Das59a4a2b2024-01-09 21:35:56 +00008120 errCtx := moduleErrorfTestCtx{}
8121
Paul Duffin5556c5f2022-06-09 17:32:21 +00008122 bpBase := `
8123 apex_set {
8124 name: "com.android.myapex",
8125 installable: true,
8126 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8127 set: "myapex.apks",
8128 }
8129
8130 apex_set {
8131 name: "com.android.myapex_compressed",
8132 apex_name: "com.android.myapex",
8133 installable: true,
8134 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8135 set: "myapex_compressed.apks",
8136 }
8137
8138 prebuilt_bootclasspath_fragment {
8139 name: "my-bootclasspath-fragment",
8140 apex_available: [
8141 "com.android.myapex",
8142 "com.android.myapex_compressed",
8143 ],
8144 hidden_api: {
8145 annotation_flags: "annotation-flags.csv",
8146 metadata: "metadata.csv",
8147 index: "index.csv",
8148 signature_patterns: "signature_patterns.csv",
8149 },
8150 %s
8151 }
8152 `
8153
8154 t.Run("java_import", func(t *testing.T) {
8155 result := preparers.RunTestWithBp(t,
8156 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8157 java_import {
8158 name: "libfoo",
8159 jars: ["libfoo.jar"],
8160 apex_available: [
8161 "com.android.myapex",
8162 "com.android.myapex_compressed",
8163 ],
8164 }
8165 `)
8166
8167 module := result.Module("libfoo", "android_common_com.android.myapex")
8168 usesLibraryDep := module.(java.UsesLibraryDependency)
8169 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008170 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008171 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008172 })
8173
8174 t.Run("java_sdk_library_import", func(t *testing.T) {
8175 result := preparers.RunTestWithBp(t,
8176 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8177 java_sdk_library_import {
8178 name: "libfoo",
8179 public: {
8180 jars: ["libbar.jar"],
8181 },
8182 apex_available: [
8183 "com.android.myapex",
8184 "com.android.myapex_compressed",
8185 ],
8186 compile_dex: true,
8187 }
8188 `)
8189
8190 module := result.Module("libfoo", "android_common_com.android.myapex")
8191 usesLibraryDep := module.(java.UsesLibraryDependency)
8192 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008193 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008194 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008195 })
8196
8197 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8198 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8199 image_name: "art",
8200 contents: ["libfoo"],
8201 `)+`
8202 java_sdk_library_import {
8203 name: "libfoo",
8204 public: {
8205 jars: ["libbar.jar"],
8206 },
8207 apex_available: [
8208 "com.android.myapex",
8209 "com.android.myapex_compressed",
8210 ],
8211 compile_dex: true,
8212 }
8213 `)
8214 })
8215}
8216
Jooyung Han548640b2020-04-27 12:10:30 +09008217func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
8218 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8219 apex {
8220 name: "myapex",
8221 key: "myapex.key",
8222 updatable: true,
8223 }
8224
8225 apex_key {
8226 name: "myapex.key",
8227 public_key: "testkey.avbpubkey",
8228 private_key: "testkey.pem",
8229 }
8230 `)
8231}
8232
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008233func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
8234 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8235 apex {
8236 name: "myapex",
8237 key: "myapex.key",
8238 }
8239
8240 apex_key {
8241 name: "myapex.key",
8242 public_key: "testkey.avbpubkey",
8243 private_key: "testkey.pem",
8244 }
8245 `)
8246}
8247
Jooyung Handfc864c2023-03-20 18:19:07 +09008248func Test_use_vndk_as_stable_shouldnt_be_used_for_updatable_vendor_apexes(t *testing.T) {
8249 testApexError(t, `"myapex" .*: use_vndk_as_stable: updatable APEXes can't use external VNDK libs`, `
Daniel Norman69109112021-12-02 12:52:42 -08008250 apex {
8251 name: "myapex",
8252 key: "myapex.key",
8253 updatable: true,
Jooyung Handfc864c2023-03-20 18:19:07 +09008254 use_vndk_as_stable: true,
Daniel Norman69109112021-12-02 12:52:42 -08008255 soc_specific: true,
8256 }
8257
8258 apex_key {
8259 name: "myapex.key",
8260 public_key: "testkey.avbpubkey",
8261 private_key: "testkey.pem",
8262 }
8263 `)
8264}
8265
Jooyung Han02873da2023-03-22 17:41:03 +09008266func Test_use_vndk_as_stable_shouldnt_be_used_with_min_sdk_version(t *testing.T) {
8267 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported when min_sdk_version is set`, `
8268 apex {
8269 name: "myapex",
8270 key: "myapex.key",
8271 updatable: false,
8272 min_sdk_version: "29",
8273 use_vndk_as_stable: true,
8274 vendor: true,
8275 }
8276
8277 apex_key {
8278 name: "myapex.key",
8279 public_key: "testkey.avbpubkey",
8280 private_key: "testkey.pem",
8281 }
8282 `)
8283}
8284
Jooyung Handfc864c2023-03-20 18:19:07 +09008285func Test_use_vndk_as_stable_shouldnt_be_used_for_non_vendor_apexes(t *testing.T) {
8286 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported for system/system_ext APEXes`, `
8287 apex {
8288 name: "myapex",
8289 key: "myapex.key",
8290 updatable: false,
8291 use_vndk_as_stable: true,
8292 }
8293
8294 apex_key {
8295 name: "myapex.key",
8296 public_key: "testkey.avbpubkey",
8297 private_key: "testkey.pem",
8298 }
8299 `)
8300}
8301
satayevb98371c2021-06-15 16:49:50 +01008302func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
8303 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
8304 apex {
8305 name: "myapex",
8306 key: "myapex.key",
8307 systemserverclasspath_fragments: [
8308 "mysystemserverclasspathfragment",
8309 ],
8310 min_sdk_version: "29",
8311 updatable: true,
8312 }
8313
8314 apex_key {
8315 name: "myapex.key",
8316 public_key: "testkey.avbpubkey",
8317 private_key: "testkey.pem",
8318 }
8319
8320 java_library {
8321 name: "foo",
8322 srcs: ["b.java"],
8323 min_sdk_version: "29",
8324 installable: true,
8325 apex_available: [
8326 "myapex",
8327 ],
8328 }
8329
8330 systemserverclasspath_fragment {
8331 name: "mysystemserverclasspathfragment",
8332 generate_classpaths_proto: false,
8333 contents: [
8334 "foo",
8335 ],
8336 apex_available: [
8337 "myapex",
8338 ],
8339 }
satayevabcd5972021-08-06 17:49:46 +01008340 `,
8341 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8342 )
satayevb98371c2021-06-15 16:49:50 +01008343}
8344
Paul Duffin064b70c2020-11-02 17:32:38 +00008345func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008346 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008347 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008348 fragment := java.ApexVariantReference{
8349 Apex: proptools.StringPtr("myapex"),
8350 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8351 }
8352
Paul Duffin064b70c2020-11-02 17:32:38 +00008353 testDexpreoptWithApexes(t, `
8354 prebuilt_apex {
8355 name: "myapex" ,
8356 arch: {
8357 arm64: {
8358 src: "myapex-arm64.apex",
8359 },
8360 arm: {
8361 src: "myapex-arm.apex",
8362 },
8363 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008364 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8365 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008366
Paul Duffin89f570a2021-06-16 01:42:33 +01008367 prebuilt_bootclasspath_fragment {
8368 name: "my-bootclasspath-fragment",
8369 contents: ["libfoo"],
8370 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008371 hidden_api: {
8372 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8373 metadata: "my-bootclasspath-fragment/metadata.csv",
8374 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008375 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8376 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8377 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008378 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008379 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008380
Paul Duffin89f570a2021-06-16 01:42:33 +01008381 java_import {
8382 name: "libfoo",
8383 jars: ["libfoo.jar"],
8384 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008385 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008386 }
8387 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008388 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008389}
8390
Spandan Dasf14e2542021-11-12 00:01:37 +00008391func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008392 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008393 bp += `
8394 apex_key {
8395 name: "myapex.key",
8396 public_key: "testkey.avbpubkey",
8397 private_key: "testkey.pem",
8398 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008399 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008400 "lib1/src/A.java": nil,
8401 "lib2/src/B.java": nil,
8402 "system/sepolicy/apex/myapex-file_contexts": nil,
8403 }
8404
Paul Duffin45338f02021-03-30 23:07:52 +01008405 errorHandler := android.FixtureExpectsNoErrors
8406 if errmsg != "" {
8407 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008408 }
Colin Crossae8600b2020-10-29 17:09:13 -07008409
Paul Duffin45338f02021-03-30 23:07:52 +01008410 android.GroupFixturePreparers(
8411 android.PrepareForTestWithAndroidBuildComponents,
8412 java.PrepareForTestWithJavaBuildComponents,
8413 PrepareForTestWithApexBuildComponents,
8414 android.PrepareForTestWithNeverallowRules(rules),
8415 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008416 apexBootJars := make([]string, 0, len(bootJars))
8417 for _, apexBootJar := range bootJars {
8418 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008419 }
satayevd604b212021-07-21 14:23:52 +01008420 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008421 }),
8422 fs.AddToFixture(),
8423 ).
8424 ExtendWithErrorHandler(errorHandler).
8425 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008426}
8427
8428func TestApexPermittedPackagesRules(t *testing.T) {
8429 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008430 name string
8431 expectedError string
8432 bp string
8433 bootJars []string
8434 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008435 }{
8436
8437 {
8438 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8439 expectedError: "",
8440 bp: `
8441 java_library {
8442 name: "bcp_lib1",
8443 srcs: ["lib1/src/*.java"],
8444 permitted_packages: ["foo.bar"],
8445 apex_available: ["myapex"],
8446 sdk_version: "none",
8447 system_modules: "none",
8448 }
8449 java_library {
8450 name: "nonbcp_lib2",
8451 srcs: ["lib2/src/*.java"],
8452 apex_available: ["myapex"],
8453 permitted_packages: ["a.b"],
8454 sdk_version: "none",
8455 system_modules: "none",
8456 }
8457 apex {
8458 name: "myapex",
8459 key: "myapex.key",
8460 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008461 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008462 }`,
8463 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008464 bcpPermittedPackages: map[string][]string{
8465 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008466 "foo.bar",
8467 },
8468 },
8469 },
8470 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008471 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008472 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 +01008473 bp: `
8474 java_library {
8475 name: "bcp_lib1",
8476 srcs: ["lib1/src/*.java"],
8477 apex_available: ["myapex"],
8478 permitted_packages: ["foo.bar"],
8479 sdk_version: "none",
8480 system_modules: "none",
8481 }
8482 java_library {
8483 name: "bcp_lib2",
8484 srcs: ["lib2/src/*.java"],
8485 apex_available: ["myapex"],
8486 permitted_packages: ["foo.bar", "bar.baz"],
8487 sdk_version: "none",
8488 system_modules: "none",
8489 }
8490 apex {
8491 name: "myapex",
8492 key: "myapex.key",
8493 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008494 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008495 }
8496 `,
8497 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008498 bcpPermittedPackages: map[string][]string{
8499 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008500 "foo.bar",
8501 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008502 "bcp_lib2": []string{
8503 "foo.bar",
8504 },
8505 },
8506 },
8507 {
8508 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8509 expectedError: "",
8510 bp: `
8511 java_library {
8512 name: "bcp_lib_restricted",
8513 srcs: ["lib1/src/*.java"],
8514 apex_available: ["myapex"],
8515 permitted_packages: ["foo.bar"],
8516 sdk_version: "none",
8517 min_sdk_version: "29",
8518 system_modules: "none",
8519 }
8520 java_library {
8521 name: "bcp_lib_unrestricted",
8522 srcs: ["lib2/src/*.java"],
8523 apex_available: ["myapex"],
8524 permitted_packages: ["foo.bar", "bar.baz"],
8525 sdk_version: "none",
8526 min_sdk_version: "29",
8527 system_modules: "none",
8528 }
8529 apex {
8530 name: "myapex",
8531 key: "myapex.key",
8532 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8533 updatable: true,
8534 min_sdk_version: "29",
8535 }
8536 `,
8537 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8538 bcpPermittedPackages: map[string][]string{
8539 "bcp_lib1_non_updateable": []string{
8540 "foo.bar",
8541 },
8542 // 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 +01008543 },
8544 },
8545 }
8546 for _, tc := range testcases {
8547 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008548 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8549 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008550 })
8551 }
8552}
8553
Jiyong Park62304bb2020-04-13 16:19:48 +09008554func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008555 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008556 apex {
8557 name: "myapex",
8558 key: "myapex.key",
8559 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008560 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008561 }
8562
8563 apex_key {
8564 name: "myapex.key",
8565 public_key: "testkey.avbpubkey",
8566 private_key: "testkey.pem",
8567 }
8568
8569 cc_library {
8570 name: "mylib",
8571 srcs: ["mylib.cpp"],
8572 system_shared_libs: [],
8573 stl: "none",
8574 stubs: {
8575 versions: ["1"],
8576 },
8577 apex_available: ["myapex"],
8578 }
8579
8580 cc_library {
8581 name: "myprivlib",
8582 srcs: ["mylib.cpp"],
8583 system_shared_libs: [],
8584 stl: "none",
8585 apex_available: ["myapex"],
8586 }
8587
8588
8589 cc_test {
8590 name: "mytest",
8591 gtest: false,
8592 srcs: ["mylib.cpp"],
8593 system_shared_libs: [],
8594 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008595 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008596 test_for: ["myapex"]
8597 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008598
8599 cc_library {
8600 name: "mytestlib",
8601 srcs: ["mylib.cpp"],
8602 system_shared_libs: [],
8603 shared_libs: ["mylib", "myprivlib"],
8604 stl: "none",
8605 test_for: ["myapex"],
8606 }
8607
8608 cc_benchmark {
8609 name: "mybench",
8610 srcs: ["mylib.cpp"],
8611 system_shared_libs: [],
8612 shared_libs: ["mylib", "myprivlib"],
8613 stl: "none",
8614 test_for: ["myapex"],
8615 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008616 `)
8617
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008618 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008619 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008620 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8621 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8622 }
8623
8624 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008625 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008626 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8627 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8628 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8629}
Jiyong Park46a512f2020-12-04 18:02:13 +09008630
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008631func TestIndirectTestFor(t *testing.T) {
8632 ctx := testApex(t, `
8633 apex {
8634 name: "myapex",
8635 key: "myapex.key",
8636 native_shared_libs: ["mylib", "myprivlib"],
8637 updatable: false,
8638 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008639
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008640 apex_key {
8641 name: "myapex.key",
8642 public_key: "testkey.avbpubkey",
8643 private_key: "testkey.pem",
8644 }
8645
8646 cc_library {
8647 name: "mylib",
8648 srcs: ["mylib.cpp"],
8649 system_shared_libs: [],
8650 stl: "none",
8651 stubs: {
8652 versions: ["1"],
8653 },
8654 apex_available: ["myapex"],
8655 }
8656
8657 cc_library {
8658 name: "myprivlib",
8659 srcs: ["mylib.cpp"],
8660 system_shared_libs: [],
8661 stl: "none",
8662 shared_libs: ["mylib"],
8663 apex_available: ["myapex"],
8664 }
8665
8666 cc_library {
8667 name: "mytestlib",
8668 srcs: ["mylib.cpp"],
8669 system_shared_libs: [],
8670 shared_libs: ["myprivlib"],
8671 stl: "none",
8672 test_for: ["myapex"],
8673 }
8674 `)
8675
8676 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008677 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008678 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8679 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8680 }
8681
8682 // The platform variant of mytestlib links to the platform variant of the
8683 // internal myprivlib.
8684 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8685
8686 // The platform variant of myprivlib links to the platform variant of mylib
8687 // and bypasses its stubs.
8688 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 +09008689}
8690
Martin Stjernholmec009002021-03-27 15:18:31 +00008691func TestTestForForLibInOtherApex(t *testing.T) {
8692 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8693 _ = testApex(t, `
8694 apex {
8695 name: "com.android.art",
8696 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008697 native_shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008698 updatable: false,
8699 }
8700
8701 apex {
8702 name: "com.android.art.debug",
8703 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008704 native_shared_libs: ["libnativebridge", "libnativebrdige_test"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008705 updatable: false,
8706 }
8707
8708 apex_key {
8709 name: "myapex.key",
8710 public_key: "testkey.avbpubkey",
8711 private_key: "testkey.pem",
8712 }
8713
8714 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008715 name: "libnativebridge",
8716 srcs: ["libnativebridge.cpp"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008717 system_shared_libs: [],
8718 stl: "none",
8719 stubs: {
8720 versions: ["1"],
8721 },
8722 apex_available: ["com.android.art", "com.android.art.debug"],
8723 }
8724
8725 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008726 name: "libnativebrdige_test",
Martin Stjernholmec009002021-03-27 15:18:31 +00008727 srcs: ["mylib.cpp"],
8728 system_shared_libs: [],
Spandan Das20fce2d2023-04-12 17:21:39 +00008729 shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008730 stl: "none",
8731 apex_available: ["com.android.art.debug"],
8732 test_for: ["com.android.art"],
8733 }
8734 `,
8735 android.MockFS{
8736 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8737 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8738 }.AddToFixture())
8739}
8740
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008741// TODO(jungjw): Move this to proptools
8742func intPtr(i int) *int {
8743 return &i
8744}
8745
8746func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008747 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008748 apex_set {
8749 name: "myapex",
8750 set: "myapex.apks",
8751 filename: "foo_v2.apex",
8752 overrides: ["foo"],
8753 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008754 `,
8755 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8756 variables.Platform_sdk_version = intPtr(30)
8757 }),
8758 android.FixtureModifyConfig(func(config android.Config) {
8759 config.Targets[android.Android] = []android.Target{
8760 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8761 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8762 }
8763 }),
8764 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008765
Spandan Das3576e762024-01-03 18:57:03 +00008766 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008767
8768 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008769 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008770 actual := extractedApex.Args["abis"]
8771 expected := "ARMEABI_V7A,ARM64_V8A"
8772 if actual != expected {
8773 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8774 }
8775 actual = extractedApex.Args["sdk-version"]
8776 expected = "30"
8777 if actual != expected {
8778 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8779 }
8780
Paul Duffin6717d882021-06-15 19:09:41 +01008781 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008782 a := m.Module().(*ApexSet)
8783 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008784 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008785 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8786 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8787 }
8788}
8789
Anton Hansson805e0a52022-11-25 14:06:46 +00008790func TestApexSet_NativeBridge(t *testing.T) {
8791 ctx := testApex(t, `
8792 apex_set {
8793 name: "myapex",
8794 set: "myapex.apks",
8795 filename: "foo_v2.apex",
8796 overrides: ["foo"],
8797 }
8798 `,
8799 android.FixtureModifyConfig(func(config android.Config) {
8800 config.Targets[android.Android] = []android.Target{
8801 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8802 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8803 }
8804 }),
8805 )
8806
Spandan Das3576e762024-01-03 18:57:03 +00008807 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Anton Hansson805e0a52022-11-25 14:06:46 +00008808
8809 // Check extract_apks tool parameters. No native bridge arch expected
8810 extractedApex := m.Output("extracted/myapex.apks")
8811 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8812}
8813
Jiyong Park7d95a512020-05-10 15:16:24 +09008814func TestNoStaticLinkingToStubsLib(t *testing.T) {
8815 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8816 apex {
8817 name: "myapex",
8818 key: "myapex.key",
8819 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008820 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008821 }
8822
8823 apex_key {
8824 name: "myapex.key",
8825 public_key: "testkey.avbpubkey",
8826 private_key: "testkey.pem",
8827 }
8828
8829 cc_library {
8830 name: "mylib",
8831 srcs: ["mylib.cpp"],
8832 static_libs: ["otherlib"],
8833 system_shared_libs: [],
8834 stl: "none",
8835 apex_available: [ "myapex" ],
8836 }
8837
8838 cc_library {
8839 name: "otherlib",
8840 srcs: ["mylib.cpp"],
8841 system_shared_libs: [],
8842 stl: "none",
8843 stubs: {
8844 versions: ["1", "2", "3"],
8845 },
8846 apex_available: [ "myapex" ],
8847 }
8848 `)
8849}
8850
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008851func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008852 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008853 apex {
8854 name: "myapex",
8855 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008856 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008857 custom_sign_tool: "sign_myapex",
8858 }
8859
8860 apex_key {
8861 name: "myapex.key",
8862 public_key: "testkey.avbpubkey",
8863 private_key: "testkey.pem",
8864 }
8865 `)
8866
Jooyung Han286957d2023-10-30 16:17:56 +09008867 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07008868 content := android.ContentFromFileRuleForTests(t, ctx, myapex.Output("apexkeys.txt"))
Jooyung Haneec1b3f2023-06-20 16:25:59 +09008869 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 +09008870}
8871
8872func TestApexKeysTxtOverrides(t *testing.T) {
8873 ctx := testApex(t, `
8874 apex {
8875 name: "myapex",
8876 key: "myapex.key",
8877 updatable: false,
8878 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008879 }
8880
8881 apex_key {
8882 name: "myapex.key",
8883 public_key: "testkey.avbpubkey",
8884 private_key: "testkey.pem",
8885 }
8886
8887 prebuilt_apex {
8888 name: "myapex",
8889 prefer: true,
8890 arch: {
8891 arm64: {
8892 src: "myapex-arm64.apex",
8893 },
8894 arm: {
8895 src: "myapex-arm.apex",
8896 },
8897 },
8898 }
8899
8900 apex_set {
8901 name: "myapex_set",
8902 set: "myapex.apks",
8903 filename: "myapex_set.apex",
8904 overrides: ["myapex"],
8905 }
8906 `)
8907
Colin Crossf61d03d2023-11-02 16:56:39 -07008908 content := android.ContentFromFileRuleForTests(t, ctx,
8909 ctx.ModuleForTests("myapex", "android_common_myapex").Output("apexkeys.txt"))
Jooyung Han286957d2023-10-30 16:17:56 +09008910 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 -07008911 content = android.ContentFromFileRuleForTests(t, ctx,
8912 ctx.ModuleForTests("myapex_set", "android_common_myapex_set").Output("apexkeys.txt"))
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008913 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 +09008914}
8915
Jooyung Han938b5932020-06-20 12:47:47 +09008916func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008917 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008918 apex {
8919 name: "myapex",
8920 key: "myapex.key",
8921 apps: ["app"],
8922 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008923 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008924 }
8925
8926 apex_key {
8927 name: "myapex.key",
8928 public_key: "testkey.avbpubkey",
8929 private_key: "testkey.pem",
8930 }
8931
8932 android_app {
8933 name: "app",
8934 srcs: ["foo/bar/MyClass.java"],
8935 package_name: "foo",
8936 sdk_version: "none",
8937 system_modules: "none",
8938 apex_available: [ "myapex" ],
8939 }
8940 `, withFiles(map[string][]byte{
8941 "sub/Android.bp": []byte(`
8942 override_apex {
8943 name: "override_myapex",
8944 base: "myapex",
8945 apps: ["override_app"],
8946 allowed_files: ":allowed",
8947 }
8948 // Overridable "path" property should be referenced indirectly
8949 filegroup {
8950 name: "allowed",
8951 srcs: ["allowed.txt"],
8952 }
8953 override_android_app {
8954 name: "override_app",
8955 base: "app",
8956 package_name: "bar",
8957 }
8958 `),
8959 }))
8960
Jooyung Hana0503a52023-08-23 13:12:50 +09008961 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008962 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8963 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8964 }
8965
Jooyung Hana0503a52023-08-23 13:12:50 +09008966 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008967 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8968 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8969 }
8970}
8971
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008972func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008973 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008974 apex {
8975 name: "myapex",
8976 key: "myapex.key",
8977 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008978 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008979 }
8980
8981 apex_key {
8982 name: "myapex.key",
8983 public_key: "testkey.avbpubkey",
8984 private_key: "testkey.pem",
8985 }
8986
8987 cc_library {
8988 name: "mylib",
8989 srcs: ["mylib.cpp"],
8990 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008991 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008992 },
8993 apex_available: ["myapex"],
8994 }
8995
8996 cc_prebuilt_library_shared {
8997 name: "mylib",
8998 prefer: false,
8999 srcs: ["prebuilt.so"],
9000 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07009001 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009002 },
9003 apex_available: ["myapex"],
9004 }
9005 `)
9006}
9007
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009008func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009009 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009010 apex {
9011 name: "myapex",
9012 key: "myapex.key",
9013 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009014 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009015 }
9016 apex_key {
9017 name: "myapex.key",
9018 public_key: "testkey.avbpubkey",
9019 private_key: "testkey.pem",
9020 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00009021 `,
9022 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9023 variables.CompressedApex = proptools.BoolPtr(true)
9024 }),
9025 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009026
Jooyung Hana0503a52023-08-23 13:12:50 +09009027 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("compressRule")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009028 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
9029
Jooyung Hana0503a52023-08-23 13:12:50 +09009030 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex").Description("sign compressedApex")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009031 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
9032
9033 // Make sure output of bundle is .capex
Jooyung Hana0503a52023-08-23 13:12:50 +09009034 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009035 ensureContains(t, ab.outputFile.String(), "myapex.capex")
9036
9037 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07009038 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009039 var builder strings.Builder
9040 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9041 androidMk := builder.String()
9042 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
9043}
9044
Martin Stjernholm2856c662020-12-02 15:03:42 +00009045func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009046 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00009047 apex {
9048 name: "myapex",
9049 key: "myapex.key",
9050 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009051 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00009052 }
9053
9054 apex_key {
9055 name: "myapex.key",
9056 public_key: "testkey.avbpubkey",
9057 private_key: "testkey.pem",
9058 }
9059
9060 cc_library {
9061 name: "mylib",
9062 srcs: ["mylib.cpp"],
9063 apex_available: ["myapex"],
9064 shared_libs: ["otherlib"],
9065 system_shared_libs: [],
9066 }
9067
9068 cc_library {
9069 name: "otherlib",
9070 srcs: ["mylib.cpp"],
9071 stubs: {
9072 versions: ["current"],
9073 },
9074 }
9075
9076 cc_prebuilt_library_shared {
9077 name: "otherlib",
9078 prefer: true,
9079 srcs: ["prebuilt.so"],
9080 stubs: {
9081 versions: ["current"],
9082 },
9083 }
9084 `)
9085
Jooyung Hana0503a52023-08-23 13:12:50 +09009086 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07009087 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00009088 var builder strings.Builder
9089 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9090 androidMk := builder.String()
9091
9092 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
9093 // a thing there.
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009094 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 otherlib\n")
Martin Stjernholm2856c662020-12-02 15:03:42 +00009095}
9096
Jiyong Parke3867542020-12-03 17:28:25 +09009097func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009098 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09009099 apex {
9100 name: "myapex",
9101 key: "myapex.key",
9102 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009103 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09009104 }
9105
9106 apex_key {
9107 name: "myapex.key",
9108 public_key: "testkey.avbpubkey",
9109 private_key: "testkey.pem",
9110 }
9111
9112 cc_library {
9113 name: "mylib",
9114 srcs: ["mylib.cpp"],
9115 system_shared_libs: [],
9116 stl: "none",
9117 apex_available: ["myapex"],
9118 shared_libs: ["mylib2"],
9119 target: {
9120 apex: {
9121 exclude_shared_libs: ["mylib2"],
9122 },
9123 },
9124 }
9125
9126 cc_library {
9127 name: "mylib2",
9128 srcs: ["mylib.cpp"],
9129 system_shared_libs: [],
9130 stl: "none",
9131 }
9132 `)
9133
9134 // Check if mylib is linked to mylib2 for the non-apex target
9135 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
9136 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
9137
9138 // Make sure that the link doesn't occur for the apex target
9139 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
9140 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
9141
9142 // It shouldn't appear in the copy cmd as well.
Jooyung Hana0503a52023-08-23 13:12:50 +09009143 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule").Args["copy_commands"]
Jiyong Parke3867542020-12-03 17:28:25 +09009144 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
9145}
9146
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009147func TestPrebuiltStubLibDep(t *testing.T) {
9148 bpBase := `
9149 apex {
9150 name: "myapex",
9151 key: "myapex.key",
9152 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009153 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009154 }
9155 apex_key {
9156 name: "myapex.key",
9157 public_key: "testkey.avbpubkey",
9158 private_key: "testkey.pem",
9159 }
9160 cc_library {
9161 name: "mylib",
9162 srcs: ["mylib.cpp"],
9163 apex_available: ["myapex"],
9164 shared_libs: ["stublib"],
9165 system_shared_libs: [],
9166 }
9167 apex {
9168 name: "otherapex",
9169 enabled: %s,
9170 key: "myapex.key",
9171 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009172 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009173 }
9174 `
9175
9176 stublibSourceBp := `
9177 cc_library {
9178 name: "stublib",
9179 srcs: ["mylib.cpp"],
9180 apex_available: ["otherapex"],
9181 system_shared_libs: [],
9182 stl: "none",
9183 stubs: {
9184 versions: ["1"],
9185 },
9186 }
9187 `
9188
9189 stublibPrebuiltBp := `
9190 cc_prebuilt_library_shared {
9191 name: "stublib",
9192 srcs: ["prebuilt.so"],
9193 apex_available: ["otherapex"],
9194 stubs: {
9195 versions: ["1"],
9196 },
9197 %s
9198 }
9199 `
9200
9201 tests := []struct {
9202 name string
9203 stublibBp string
9204 usePrebuilt bool
9205 modNames []string // Modules to collect AndroidMkEntries for
9206 otherApexEnabled []string
9207 }{
9208 {
9209 name: "only_source",
9210 stublibBp: stublibSourceBp,
9211 usePrebuilt: false,
9212 modNames: []string{"stublib"},
9213 otherApexEnabled: []string{"true", "false"},
9214 },
9215 {
9216 name: "source_preferred",
9217 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9218 usePrebuilt: false,
9219 modNames: []string{"stublib", "prebuilt_stublib"},
9220 otherApexEnabled: []string{"true", "false"},
9221 },
9222 {
9223 name: "prebuilt_preferred",
9224 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9225 usePrebuilt: true,
9226 modNames: []string{"stublib", "prebuilt_stublib"},
9227 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9228 },
9229 {
9230 name: "only_prebuilt",
9231 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9232 usePrebuilt: true,
9233 modNames: []string{"stublib"},
9234 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9235 },
9236 }
9237
9238 for _, test := range tests {
9239 t.Run(test.name, func(t *testing.T) {
9240 for _, otherApexEnabled := range test.otherApexEnabled {
9241 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009242 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009243
9244 type modAndMkEntries struct {
9245 mod *cc.Module
9246 mkEntries android.AndroidMkEntries
9247 }
9248 entries := []*modAndMkEntries{}
9249
9250 // Gather shared lib modules that are installable
9251 for _, modName := range test.modNames {
9252 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9253 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9254 continue
9255 }
9256 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Cole Faust0e0d7492024-04-11 17:43:00 -07009257 if !mod.Enabled(android.PanickingConfigAndErrorContext(ctx)) || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009258 continue
9259 }
Colin Crossaa255532020-07-03 13:18:24 -07009260 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009261 if ent.Disabled {
9262 continue
9263 }
9264 entries = append(entries, &modAndMkEntries{
9265 mod: mod,
9266 mkEntries: ent,
9267 })
9268 }
9269 }
9270 }
9271
9272 var entry *modAndMkEntries = nil
9273 for _, ent := range entries {
9274 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9275 if entry != nil {
9276 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9277 } else {
9278 entry = ent
9279 }
9280 }
9281 }
9282
9283 if entry == nil {
9284 t.Errorf("AndroidMk entry for \"stublib\" missing")
9285 } else {
9286 isPrebuilt := entry.mod.Prebuilt() != nil
9287 if isPrebuilt != test.usePrebuilt {
9288 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9289 }
9290 if !entry.mod.IsStubs() {
9291 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9292 }
9293 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9294 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9295 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009296 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009297 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009298 if !android.InList(expected, cflags) {
9299 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9300 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009301 }
9302 })
9303 }
9304 })
9305 }
9306}
9307
Colin Crossc33e5212021-05-25 18:16:02 -07009308func TestApexJavaCoverage(t *testing.T) {
9309 bp := `
9310 apex {
9311 name: "myapex",
9312 key: "myapex.key",
9313 java_libs: ["mylib"],
9314 bootclasspath_fragments: ["mybootclasspathfragment"],
9315 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9316 updatable: false,
9317 }
9318
9319 apex_key {
9320 name: "myapex.key",
9321 public_key: "testkey.avbpubkey",
9322 private_key: "testkey.pem",
9323 }
9324
9325 java_library {
9326 name: "mylib",
9327 srcs: ["mylib.java"],
9328 apex_available: ["myapex"],
9329 compile_dex: true,
9330 }
9331
9332 bootclasspath_fragment {
9333 name: "mybootclasspathfragment",
9334 contents: ["mybootclasspathlib"],
9335 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009336 hidden_api: {
9337 split_packages: ["*"],
9338 },
Colin Crossc33e5212021-05-25 18:16:02 -07009339 }
9340
9341 java_library {
9342 name: "mybootclasspathlib",
9343 srcs: ["mybootclasspathlib.java"],
9344 apex_available: ["myapex"],
9345 compile_dex: true,
9346 }
9347
9348 systemserverclasspath_fragment {
9349 name: "mysystemserverclasspathfragment",
9350 contents: ["mysystemserverclasspathlib"],
9351 apex_available: ["myapex"],
9352 }
9353
9354 java_library {
9355 name: "mysystemserverclasspathlib",
9356 srcs: ["mysystemserverclasspathlib.java"],
9357 apex_available: ["myapex"],
9358 compile_dex: true,
9359 }
9360 `
9361
9362 result := android.GroupFixturePreparers(
9363 PrepareForTestWithApexBuildComponents,
9364 prepareForTestWithMyapex,
9365 java.PrepareForTestWithJavaDefaultModules,
9366 android.PrepareForTestWithAndroidBuildComponents,
9367 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009368 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9369 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009370 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009371 ).RunTest(t)
9372
9373 // Make sure jacoco ran on both mylib and mybootclasspathlib
9374 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9375 t.Errorf("Failed to find jacoco rule for mylib")
9376 }
9377 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9378 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9379 }
9380 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9381 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9382 }
9383}
9384
Jiyong Park192600a2021-08-03 07:52:17 +00009385func TestProhibitStaticExecutable(t *testing.T) {
9386 testApexError(t, `executable mybin is static`, `
9387 apex {
9388 name: "myapex",
9389 key: "myapex.key",
9390 binaries: ["mybin"],
9391 min_sdk_version: "29",
9392 }
9393
9394 apex_key {
9395 name: "myapex.key",
9396 public_key: "testkey.avbpubkey",
9397 private_key: "testkey.pem",
9398 }
9399
9400 cc_binary {
9401 name: "mybin",
9402 srcs: ["mylib.cpp"],
9403 relative_install_path: "foo/bar",
9404 static_executable: true,
9405 system_shared_libs: [],
9406 stl: "none",
9407 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009408 min_sdk_version: "29",
9409 }
9410 `)
9411
9412 testApexError(t, `executable mybin.rust is static`, `
9413 apex {
9414 name: "myapex",
9415 key: "myapex.key",
9416 binaries: ["mybin.rust"],
9417 min_sdk_version: "29",
9418 }
9419
9420 apex_key {
9421 name: "myapex.key",
9422 public_key: "testkey.avbpubkey",
9423 private_key: "testkey.pem",
9424 }
9425
9426 rust_binary {
9427 name: "mybin.rust",
9428 srcs: ["foo.rs"],
9429 static_executable: true,
9430 apex_available: ["myapex"],
9431 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009432 }
9433 `)
9434}
9435
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009436func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9437 ctx := testApex(t, `
9438 apex {
9439 name: "myapex",
9440 key: "myapex.key",
9441 updatable: false,
9442 java_libs: ["foo"],
9443 }
9444
9445 apex_key {
9446 name: "myapex.key",
9447 public_key: "testkey.avbpubkey",
9448 private_key: "testkey.pem",
9449 }
9450
9451 java_library {
9452 name: "foo",
9453 srcs: ["foo.java"],
9454 apex_available: ["myapex"],
9455 installable: true,
9456 }
9457 `,
9458 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9459 )
9460
Jooyung Hana0503a52023-08-23 13:12:50 +09009461 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009462 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9463 var builder strings.Builder
9464 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9465 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009466 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 +00009467}
9468
9469func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9470 ctx := testApex(t, `
9471 prebuilt_apex {
9472 name: "myapex",
9473 arch: {
9474 arm64: {
9475 src: "myapex-arm64.apex",
9476 },
9477 arm: {
9478 src: "myapex-arm.apex",
9479 },
9480 },
9481 exported_java_libs: ["foo"],
9482 }
9483
9484 java_import {
9485 name: "foo",
9486 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009487 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009488 }
9489 `,
9490 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9491 )
9492
9493 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9494 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9495 mainModuleEntries := entriesList[0]
9496 android.AssertArrayString(t,
9497 "LOCAL_REQUIRED_MODULES",
9498 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9499 []string{
9500 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9501 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9502 })
9503}
9504
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009505func TestAndroidMk_RequiredModules(t *testing.T) {
9506 ctx := testApex(t, `
9507 apex {
9508 name: "myapex",
9509 key: "myapex.key",
9510 updatable: false,
9511 java_libs: ["foo"],
9512 required: ["otherapex"],
9513 }
9514
9515 apex {
9516 name: "otherapex",
9517 key: "myapex.key",
9518 updatable: false,
9519 java_libs: ["foo"],
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009520 }
9521
9522 apex_key {
9523 name: "myapex.key",
9524 public_key: "testkey.avbpubkey",
9525 private_key: "testkey.pem",
9526 }
9527
9528 java_library {
9529 name: "foo",
9530 srcs: ["foo.java"],
9531 apex_available: ["myapex", "otherapex"],
9532 installable: true,
9533 }
9534 `)
9535
Jooyung Hana0503a52023-08-23 13:12:50 +09009536 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009537 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9538 var builder strings.Builder
9539 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9540 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009541 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009542}
9543
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009544func TestAndroidMk_RequiredDeps(t *testing.T) {
9545 ctx := testApex(t, `
9546 apex {
9547 name: "myapex",
9548 key: "myapex.key",
9549 updatable: false,
9550 }
9551
9552 apex_key {
9553 name: "myapex.key",
9554 public_key: "testkey.avbpubkey",
9555 private_key: "testkey.pem",
9556 }
9557 `)
9558
Jooyung Hana0503a52023-08-23 13:12:50 +09009559 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009560 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009561 data := android.AndroidMkDataForTest(t, ctx, bundle)
9562 var builder strings.Builder
9563 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9564 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009565 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009566}
9567
Jooyung Hana6d36672022-02-24 13:58:07 +09009568func TestApexOutputFileProducer(t *testing.T) {
9569 for _, tc := range []struct {
9570 name string
9571 ref string
9572 expected_data []string
9573 }{
9574 {
9575 name: "test_using_output",
9576 ref: ":myapex",
Jooyung Hana0503a52023-08-23 13:12:50 +09009577 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.capex:myapex.capex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009578 },
9579 {
9580 name: "test_using_apex",
9581 ref: ":myapex{.apex}",
Jooyung Hana0503a52023-08-23 13:12:50 +09009582 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.apex:myapex.apex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009583 },
9584 } {
9585 t.Run(tc.name, func(t *testing.T) {
9586 ctx := testApex(t, `
9587 apex {
9588 name: "myapex",
9589 key: "myapex.key",
9590 compressible: true,
9591 updatable: false,
9592 }
9593
9594 apex_key {
9595 name: "myapex.key",
9596 public_key: "testkey.avbpubkey",
9597 private_key: "testkey.pem",
9598 }
9599
9600 java_test {
9601 name: "`+tc.name+`",
9602 srcs: ["a.java"],
9603 data: ["`+tc.ref+`"],
9604 }
9605 `,
9606 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9607 variables.CompressedApex = proptools.BoolPtr(true)
9608 }))
9609 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9610 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9611 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9612 })
9613 }
9614}
9615
satayev758968a2021-12-06 11:42:40 +00009616func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9617 preparer := android.GroupFixturePreparers(
9618 PrepareForTestWithApexBuildComponents,
9619 prepareForTestWithMyapex,
9620 java.PrepareForTestWithJavaSdkLibraryFiles,
9621 java.PrepareForTestWithJavaDefaultModules,
9622 android.PrepareForTestWithAndroidBuildComponents,
9623 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9624 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9625 )
9626
9627 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9628 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9629 preparer.RunTestWithBp(t, `
9630 apex {
9631 name: "myapex",
9632 key: "myapex.key",
9633 bootclasspath_fragments: ["mybootclasspathfragment"],
9634 min_sdk_version: "30",
9635 updatable: false,
9636 }
9637
9638 apex_key {
9639 name: "myapex.key",
9640 public_key: "testkey.avbpubkey",
9641 private_key: "testkey.pem",
9642 }
9643
9644 bootclasspath_fragment {
9645 name: "mybootclasspathfragment",
9646 contents: ["mybootclasspathlib"],
9647 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009648 hidden_api: {
9649 split_packages: ["*"],
9650 },
satayev758968a2021-12-06 11:42:40 +00009651 }
9652
9653 java_sdk_library {
9654 name: "mybootclasspathlib",
9655 srcs: ["mybootclasspathlib.java"],
9656 apex_available: ["myapex"],
9657 compile_dex: true,
9658 unsafe_ignore_missing_latest_api: true,
9659 min_sdk_version: "31",
9660 static_libs: ["util"],
9661 }
9662
9663 java_library {
9664 name: "util",
9665 srcs: ["a.java"],
9666 apex_available: ["myapex"],
9667 min_sdk_version: "31",
9668 static_libs: ["another_util"],
9669 }
9670
9671 java_library {
9672 name: "another_util",
9673 srcs: ["a.java"],
9674 min_sdk_version: "31",
9675 apex_available: ["myapex"],
9676 }
9677 `)
9678 })
9679
9680 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9681 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9682 preparer.RunTestWithBp(t, `
9683 apex {
9684 name: "myapex",
9685 key: "myapex.key",
9686 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9687 min_sdk_version: "30",
9688 updatable: false,
9689 }
9690
9691 apex_key {
9692 name: "myapex.key",
9693 public_key: "testkey.avbpubkey",
9694 private_key: "testkey.pem",
9695 }
9696
9697 systemserverclasspath_fragment {
9698 name: "mysystemserverclasspathfragment",
9699 contents: ["mysystemserverclasspathlib"],
9700 apex_available: ["myapex"],
9701 }
9702
9703 java_sdk_library {
9704 name: "mysystemserverclasspathlib",
9705 srcs: ["mysystemserverclasspathlib.java"],
9706 apex_available: ["myapex"],
9707 compile_dex: true,
9708 min_sdk_version: "32",
9709 unsafe_ignore_missing_latest_api: true,
9710 static_libs: ["util"],
9711 }
9712
9713 java_library {
9714 name: "util",
9715 srcs: ["a.java"],
9716 apex_available: ["myapex"],
9717 min_sdk_version: "31",
9718 static_libs: ["another_util"],
9719 }
9720
9721 java_library {
9722 name: "another_util",
9723 srcs: ["a.java"],
9724 min_sdk_version: "31",
9725 apex_available: ["myapex"],
9726 }
9727 `)
9728 })
9729
9730 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9731 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9732 RunTestWithBp(t, `
9733 apex {
9734 name: "myapex",
9735 key: "myapex.key",
9736 bootclasspath_fragments: ["mybootclasspathfragment"],
9737 min_sdk_version: "30",
9738 updatable: false,
9739 }
9740
9741 apex_key {
9742 name: "myapex.key",
9743 public_key: "testkey.avbpubkey",
9744 private_key: "testkey.pem",
9745 }
9746
9747 bootclasspath_fragment {
9748 name: "mybootclasspathfragment",
9749 contents: ["mybootclasspathlib"],
9750 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009751 hidden_api: {
9752 split_packages: ["*"],
9753 },
satayev758968a2021-12-06 11:42:40 +00009754 }
9755
9756 java_sdk_library {
9757 name: "mybootclasspathlib",
9758 srcs: ["mybootclasspathlib.java"],
9759 apex_available: ["myapex"],
9760 compile_dex: true,
9761 unsafe_ignore_missing_latest_api: true,
9762 }
9763 `)
9764 })
9765
9766 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9767 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9768 RunTestWithBp(t, `
9769 apex {
9770 name: "myapex",
9771 key: "myapex.key",
9772 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9773 min_sdk_version: "30",
9774 updatable: false,
9775 }
9776
9777 apex_key {
9778 name: "myapex.key",
9779 public_key: "testkey.avbpubkey",
9780 private_key: "testkey.pem",
9781 }
9782
9783 systemserverclasspath_fragment {
9784 name: "mysystemserverclasspathfragment",
9785 contents: ["mysystemserverclasspathlib"],
9786 apex_available: ["myapex"],
9787 }
9788
9789 java_sdk_library {
9790 name: "mysystemserverclasspathlib",
9791 srcs: ["mysystemserverclasspathlib.java"],
9792 apex_available: ["myapex"],
9793 compile_dex: true,
9794 unsafe_ignore_missing_latest_api: true,
9795 }
9796 `)
9797 })
9798}
9799
Jiakai Zhang6decef92022-01-12 17:56:19 +00009800// Verifies that the APEX depends on all the Make modules in the list.
9801func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9802 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9803 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009804 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009805 }
9806}
9807
9808// Verifies that the APEX does not depend on any of the Make modules in the list.
9809func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9810 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9811 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009812 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009813 }
9814}
9815
Cole Faust1021ccd2023-02-26 21:15:25 -08009816// TODO(b/193460475): Re-enable this test
9817//func TestApexStrictUpdtabilityLint(t *testing.T) {
9818// bpTemplate := `
9819// apex {
9820// name: "myapex",
9821// key: "myapex.key",
9822// java_libs: ["myjavalib"],
9823// updatable: %v,
9824// min_sdk_version: "29",
9825// }
9826// apex_key {
9827// name: "myapex.key",
9828// }
9829// java_library {
9830// name: "myjavalib",
9831// srcs: ["MyClass.java"],
9832// apex_available: [ "myapex" ],
9833// lint: {
9834// strict_updatability_linting: %v,
9835// },
9836// sdk_version: "current",
9837// min_sdk_version: "29",
9838// }
9839// `
9840// fs := android.MockFS{
9841// "lint-baseline.xml": nil,
9842// }
9843//
9844// testCases := []struct {
9845// testCaseName string
9846// apexUpdatable bool
9847// javaStrictUpdtabilityLint bool
9848// lintFileExists bool
9849// disallowedFlagExpected bool
9850// }{
9851// {
9852// testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9853// apexUpdatable: true,
9854// javaStrictUpdtabilityLint: true,
9855// lintFileExists: false,
9856// disallowedFlagExpected: false,
9857// },
9858// {
9859// testCaseName: "non-updatable apex respects strict_updatability of javalib",
9860// apexUpdatable: false,
9861// javaStrictUpdtabilityLint: false,
9862// lintFileExists: true,
9863// disallowedFlagExpected: false,
9864// },
9865// {
9866// testCaseName: "non-updatable apex respects strict updatability of javalib",
9867// apexUpdatable: false,
9868// javaStrictUpdtabilityLint: true,
9869// lintFileExists: true,
9870// disallowedFlagExpected: true,
9871// },
9872// {
9873// testCaseName: "updatable apex sets strict updatability of javalib to true",
9874// apexUpdatable: true,
9875// javaStrictUpdtabilityLint: false, // will be set to true by mutator
9876// lintFileExists: true,
9877// disallowedFlagExpected: true,
9878// },
9879// }
9880//
9881// for _, testCase := range testCases {
9882// bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9883// fixtures := []android.FixturePreparer{}
9884// if testCase.lintFileExists {
9885// fixtures = append(fixtures, fs.AddToFixture())
9886// }
9887//
9888// result := testApex(t, bp, fixtures...)
9889// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9890// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9891// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9892//
9893// if disallowedFlagActual != testCase.disallowedFlagExpected {
9894// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9895// }
9896// }
9897//}
9898//
9899//func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9900// bp := `
9901// apex {
9902// name: "myapex",
9903// key: "myapex.key",
9904// java_libs: ["myjavalib"],
9905// updatable: true,
9906// min_sdk_version: "29",
9907// }
9908// apex_key {
9909// name: "myapex.key",
9910// }
9911// java_library {
9912// name: "myjavalib",
9913// srcs: ["MyClass.java"],
9914// apex_available: [ "myapex" ],
9915// sdk_version: "current",
9916// min_sdk_version: "29",
9917// }
9918// `
9919//
9920// testCases := []struct {
9921// testCaseName string
9922// moduleDirectory string
9923// disallowedFlagExpected bool
9924// }{
9925// {
9926// testCaseName: "lintable module defined outside libcore",
9927// moduleDirectory: "",
9928// disallowedFlagExpected: true,
9929// },
9930// {
9931// testCaseName: "lintable module defined in libcore root directory",
9932// moduleDirectory: "libcore/",
9933// disallowedFlagExpected: false,
9934// },
9935// {
9936// testCaseName: "lintable module defined in libcore child directory",
9937// moduleDirectory: "libcore/childdir/",
9938// disallowedFlagExpected: true,
9939// },
9940// }
9941//
9942// for _, testCase := range testCases {
9943// lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9944// bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9945// result := testApex(t, "", lintFileCreator, bpFileCreator)
9946// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9947// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9948// cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9949// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9950//
9951// if disallowedFlagActual != testCase.disallowedFlagExpected {
9952// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9953// }
9954// }
9955//}
9956//
9957//// checks transtive deps of an apex coming from bootclasspath_fragment
9958//func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9959// bp := `
9960// apex {
9961// name: "myapex",
9962// key: "myapex.key",
9963// bootclasspath_fragments: ["mybootclasspathfragment"],
9964// updatable: true,
9965// min_sdk_version: "29",
9966// }
9967// apex_key {
9968// name: "myapex.key",
9969// }
9970// bootclasspath_fragment {
9971// name: "mybootclasspathfragment",
9972// contents: ["myjavalib"],
9973// apex_available: ["myapex"],
9974// hidden_api: {
9975// split_packages: ["*"],
9976// },
9977// }
9978// java_library {
9979// name: "myjavalib",
9980// srcs: ["MyClass.java"],
9981// apex_available: [ "myapex" ],
9982// sdk_version: "current",
9983// min_sdk_version: "29",
9984// compile_dex: true,
9985// }
9986// `
9987// fs := android.MockFS{
9988// "lint-baseline.xml": nil,
9989// }
9990//
9991// result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9992// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9993// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9994// if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9995// t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9996// }
9997//}
Spandan Das66773252022-01-15 00:23:18 +00009998
Spandan Das42e89502022-05-06 22:12:55 +00009999// updatable apexes should propagate updatable=true to its apps
10000func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
10001 bp := `
10002 apex {
10003 name: "myapex",
10004 key: "myapex.key",
10005 updatable: %v,
10006 apps: [
10007 "myapp",
10008 ],
10009 min_sdk_version: "30",
10010 }
10011 apex_key {
10012 name: "myapex.key",
10013 }
10014 android_app {
10015 name: "myapp",
10016 updatable: %v,
10017 apex_available: [
10018 "myapex",
10019 ],
10020 sdk_version: "current",
10021 min_sdk_version: "30",
10022 }
10023 `
10024 testCases := []struct {
10025 name string
10026 apex_is_updatable_bp bool
10027 app_is_updatable_bp bool
10028 app_is_updatable_expected bool
10029 }{
10030 {
10031 name: "Non-updatable apex respects updatable property of non-updatable app",
10032 apex_is_updatable_bp: false,
10033 app_is_updatable_bp: false,
10034 app_is_updatable_expected: false,
10035 },
10036 {
10037 name: "Non-updatable apex respects updatable property of updatable app",
10038 apex_is_updatable_bp: false,
10039 app_is_updatable_bp: true,
10040 app_is_updatable_expected: true,
10041 },
10042 {
10043 name: "Updatable apex respects updatable property of updatable app",
10044 apex_is_updatable_bp: true,
10045 app_is_updatable_bp: true,
10046 app_is_updatable_expected: true,
10047 },
10048 {
10049 name: "Updatable apex sets updatable=true on non-updatable app",
10050 apex_is_updatable_bp: true,
10051 app_is_updatable_bp: false,
10052 app_is_updatable_expected: true,
10053 },
10054 }
10055 for _, testCase := range testCases {
10056 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
10057 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
10058 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
10059 }
10060}
10061
Kiyoung Kim487689e2022-07-26 09:48:22 +090010062func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10063 bp := `
10064 apex {
10065 name: "myapex",
10066 key: "myapex.key",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010067 native_shared_libs: ["libbaz"],
10068 binaries: ["binfoo"],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010069 min_sdk_version: "29",
10070 }
10071 apex_key {
10072 name: "myapex.key",
10073 }
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010074 cc_binary {
10075 name: "binfoo",
10076 shared_libs: ["libbar", "libbaz", "libqux",],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010077 apex_available: ["myapex"],
10078 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010079 recovery_available: false,
10080 }
10081 cc_library {
10082 name: "libbar",
10083 srcs: ["libbar.cc"],
10084 stubs: {
10085 symbol_file: "libbar.map.txt",
10086 versions: [
10087 "29",
10088 ],
10089 },
10090 }
10091 cc_library {
10092 name: "libbaz",
10093 srcs: ["libbaz.cc"],
10094 apex_available: ["myapex"],
10095 min_sdk_version: "29",
10096 stubs: {
10097 symbol_file: "libbaz.map.txt",
10098 versions: [
10099 "29",
10100 ],
10101 },
Kiyoung Kim487689e2022-07-26 09:48:22 +090010102 }
10103 cc_api_library {
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010104 name: "libbar",
10105 src: "libbar_stub.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010106 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010107 variants: ["apex.29"],
10108 }
10109 cc_api_variant {
10110 name: "libbar",
10111 variant: "apex",
10112 version: "29",
10113 src: "libbar_apex_29.so",
10114 }
10115 cc_api_library {
10116 name: "libbaz",
10117 src: "libbaz_stub.so",
10118 min_sdk_version: "29",
10119 variants: ["apex.29"],
10120 }
10121 cc_api_variant {
10122 name: "libbaz",
10123 variant: "apex",
10124 version: "29",
10125 src: "libbaz_apex_29.so",
10126 }
10127 cc_api_library {
10128 name: "libqux",
10129 src: "libqux_stub.so",
10130 min_sdk_version: "29",
10131 variants: ["apex.29"],
10132 }
10133 cc_api_variant {
10134 name: "libqux",
10135 variant: "apex",
10136 version: "29",
10137 src: "libqux_apex_29.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010138 }
10139 api_imports {
10140 name: "api_imports",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010141 apex_shared_libs: [
10142 "libbar",
10143 "libbaz",
10144 "libqux",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010145 ],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010146 }
10147 `
10148 result := testApex(t, bp)
10149
10150 hasDep := func(m android.Module, wantDep android.Module) bool {
10151 t.Helper()
10152 var found bool
10153 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10154 if dep == wantDep {
10155 found = true
10156 }
10157 })
10158 return found
10159 }
10160
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010161 // Library defines stubs and cc_api_library should be used with cc_api_library
10162 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Module()
10163 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10164 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
Kiyoung Kim487689e2022-07-26 09:48:22 +090010165
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010166 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10167 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
Kiyoung Kim487689e2022-07-26 09:48:22 +090010168
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010169 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Rule("ld").Args["libFlags"]
10170 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10171 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10172 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
10173
10174 // Library defined in the same APEX should be linked with original definition instead of cc_api_library
10175 libbazApexVariant := result.ModuleForTests("libbaz", "android_arm64_armv8-a_shared_apex29").Module()
10176 libbazApiImportCoreVariant := result.ModuleForTests("libbaz.apiimport", "android_arm64_armv8-a_shared").Module()
10177 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even from same APEX", true, hasDep(binfooApexVariant, libbazApiImportCoreVariant))
10178 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbazApexVariant))
10179
10180 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbaz.so")
10181 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbaz.apiimport.so")
10182 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbaz.apex.29.apiimport.so")
10183
10184 // cc_api_library defined without original library should be linked with cc_api_library
10185 libquxApiImportApexVariant := result.ModuleForTests("libqux.apiimport", "android_arm64_armv8-a_shared").Module()
10186 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even original library definition does not exist", true, hasDep(binfooApexVariant, libquxApiImportApexVariant))
10187 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libqux.apex.29.apiimport.so")
10188}
10189
10190func TestPlatformBinaryBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10191 bp := `
10192 apex {
10193 name: "myapex",
10194 key: "myapex.key",
10195 native_shared_libs: ["libbar"],
10196 min_sdk_version: "29",
10197 }
10198 apex_key {
10199 name: "myapex.key",
10200 }
10201 cc_binary {
10202 name: "binfoo",
10203 shared_libs: ["libbar"],
10204 recovery_available: false,
10205 }
10206 cc_library {
10207 name: "libbar",
10208 srcs: ["libbar.cc"],
10209 apex_available: ["myapex"],
10210 min_sdk_version: "29",
10211 stubs: {
10212 symbol_file: "libbar.map.txt",
10213 versions: [
10214 "29",
10215 ],
10216 },
10217 }
10218 cc_api_library {
10219 name: "libbar",
10220 src: "libbar_stub.so",
10221 variants: ["apex.29"],
10222 }
10223 cc_api_variant {
10224 name: "libbar",
10225 variant: "apex",
10226 version: "29",
10227 src: "libbar_apex_29.so",
10228 }
10229 api_imports {
10230 name: "api_imports",
10231 apex_shared_libs: [
10232 "libbar",
10233 ],
10234 }
10235 `
10236
10237 result := testApex(t, bp)
10238
10239 hasDep := func(m android.Module, wantDep android.Module) bool {
10240 t.Helper()
10241 var found bool
10242 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10243 if dep == wantDep {
10244 found = true
10245 }
10246 })
10247 return found
10248 }
10249
10250 // Library defines stubs and cc_api_library should be used with cc_api_library
10251 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Module()
10252 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10253 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
10254
10255 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10256 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
10257
10258 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
10259 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10260 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10261 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
Kiyoung Kim487689e2022-07-26 09:48:22 +090010262}
Dennis Shend4f5d932023-01-31 20:27:21 +000010263
10264func TestTrimmedApex(t *testing.T) {
10265 bp := `
10266 apex {
10267 name: "myapex",
10268 key: "myapex.key",
10269 native_shared_libs: ["libfoo","libbaz"],
10270 min_sdk_version: "29",
10271 trim_against: "mydcla",
10272 }
10273 apex {
10274 name: "mydcla",
10275 key: "myapex.key",
10276 native_shared_libs: ["libfoo","libbar"],
10277 min_sdk_version: "29",
10278 file_contexts: ":myapex-file_contexts",
10279 dynamic_common_lib_apex: true,
10280 }
10281 apex_key {
10282 name: "myapex.key",
10283 }
10284 cc_library {
10285 name: "libfoo",
10286 shared_libs: ["libc"],
10287 apex_available: ["myapex","mydcla"],
10288 min_sdk_version: "29",
10289 }
10290 cc_library {
10291 name: "libbar",
10292 shared_libs: ["libc"],
10293 apex_available: ["myapex","mydcla"],
10294 min_sdk_version: "29",
10295 }
10296 cc_library {
10297 name: "libbaz",
10298 shared_libs: ["libc"],
10299 apex_available: ["myapex","mydcla"],
10300 min_sdk_version: "29",
10301 }
10302 cc_api_library {
10303 name: "libc",
10304 src: "libc.so",
10305 min_sdk_version: "29",
10306 recovery_available: true,
Ivan Lozanoadd122a2023-07-13 11:01:41 -040010307 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +090010308 product_available: true,
Dennis Shend4f5d932023-01-31 20:27:21 +000010309 }
10310 api_imports {
10311 name: "api_imports",
10312 shared_libs: [
10313 "libc",
10314 ],
10315 header_libs: [],
10316 }
10317 `
10318 ctx := testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +090010319 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dennis Shend4f5d932023-01-31 20:27:21 +000010320 apexRule := module.MaybeRule("apexRule")
10321 if apexRule.Rule == nil {
10322 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10323 }
10324
10325 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
Jooyung Hana0503a52023-08-23 13:12:50 +090010326 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("TrimmedApexRule")
Dennis Shend4f5d932023-01-31 20:27:21 +000010327 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10328 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10329 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10330 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10331}
Jingwen Chendea7a642023-03-28 11:30:50 +000010332
10333func TestCannedFsConfig(t *testing.T) {
10334 ctx := testApex(t, `
10335 apex {
10336 name: "myapex",
10337 key: "myapex.key",
10338 updatable: false,
10339 }
10340
10341 apex_key {
10342 name: "myapex.key",
10343 public_key: "testkey.avbpubkey",
10344 private_key: "testkey.pem",
10345 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010346 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010347 generateFsRule := mod.Rule("generateFsConfig")
10348 cmd := generateFsRule.RuleParams.Command
10349
10350 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10351}
10352
10353func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10354 ctx := testApex(t, `
10355 apex {
10356 name: "myapex",
10357 key: "myapex.key",
10358 canned_fs_config: "my_config",
10359 updatable: false,
10360 }
10361
10362 apex_key {
10363 name: "myapex.key",
10364 public_key: "testkey.avbpubkey",
10365 private_key: "testkey.pem",
10366 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010367 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010368 generateFsRule := mod.Rule("generateFsConfig")
10369 cmd := generateFsRule.RuleParams.Command
10370
10371 // Ensure that canned_fs_config has "cat my_config" at the end
10372 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10373}
Spandan Das20fce2d2023-04-12 17:21:39 +000010374
10375func TestStubLibrariesMultipleApexViolation(t *testing.T) {
10376 testCases := []struct {
10377 desc string
10378 hasStubs bool
10379 apexAvailable string
10380 expectedError string
10381 }{
10382 {
10383 desc: "non-stub library can have multiple apex_available",
10384 hasStubs: false,
10385 apexAvailable: `["myapex", "otherapex"]`,
10386 },
10387 {
10388 desc: "stub library should not be available to anyapex",
10389 hasStubs: true,
10390 apexAvailable: `["//apex_available:anyapex"]`,
10391 expectedError: "Stub libraries should have a single apex_available.*anyapex",
10392 },
10393 {
10394 desc: "stub library should not be available to multiple apexes",
10395 hasStubs: true,
10396 apexAvailable: `["myapex", "otherapex"]`,
10397 expectedError: "Stub libraries should have a single apex_available.*myapex.*otherapex",
10398 },
10399 {
10400 desc: "stub library can be available to a core apex and a test apex",
10401 hasStubs: true,
10402 apexAvailable: `["myapex", "test_myapex"]`,
10403 },
10404 }
10405 bpTemplate := `
10406 cc_library {
10407 name: "libfoo",
10408 %v
10409 apex_available: %v,
10410 }
10411 apex {
10412 name: "myapex",
10413 key: "apex.key",
10414 updatable: false,
10415 native_shared_libs: ["libfoo"],
10416 }
10417 apex {
10418 name: "otherapex",
10419 key: "apex.key",
10420 updatable: false,
10421 }
10422 apex_test {
10423 name: "test_myapex",
10424 key: "apex.key",
10425 updatable: false,
10426 native_shared_libs: ["libfoo"],
10427 }
10428 apex_key {
10429 name: "apex.key",
10430 }
10431 `
10432 for _, tc := range testCases {
10433 stubs := ""
10434 if tc.hasStubs {
10435 stubs = `stubs: {symbol_file: "libfoo.map.txt"},`
10436 }
10437 bp := fmt.Sprintf(bpTemplate, stubs, tc.apexAvailable)
10438 mockFsFixturePreparer := android.FixtureModifyMockFS(func(fs android.MockFS) {
10439 fs["system/sepolicy/apex/test_myapex-file_contexts"] = nil
10440 })
10441 if tc.expectedError == "" {
10442 testApex(t, bp, mockFsFixturePreparer)
10443 } else {
10444 testApexError(t, tc.expectedError, bp, mockFsFixturePreparer)
10445 }
10446 }
10447}
Colin Crossbd3a16b2023-04-25 11:30:51 -070010448
10449func TestFileSystemShouldSkipApexLibraries(t *testing.T) {
10450 context := android.GroupFixturePreparers(
10451 android.PrepareForIntegrationTestWithAndroid,
10452 cc.PrepareForIntegrationTestWithCc,
10453 PrepareForTestWithApexBuildComponents,
10454 prepareForTestWithMyapex,
10455 filesystem.PrepareForTestWithFilesystemBuildComponents,
10456 )
10457 result := context.RunTestWithBp(t, `
10458 android_system_image {
10459 name: "myfilesystem",
10460 deps: [
10461 "libfoo",
10462 ],
10463 linker_config_src: "linker.config.json",
10464 }
10465
10466 cc_library {
10467 name: "libfoo",
10468 shared_libs: [
10469 "libbar",
10470 ],
10471 stl: "none",
10472 }
10473
10474 cc_library {
10475 name: "libbar",
10476 stl: "none",
10477 apex_available: ["myapex"],
10478 }
10479
10480 apex {
10481 name: "myapex",
10482 native_shared_libs: ["libbar"],
10483 key: "myapex.key",
10484 updatable: false,
10485 }
10486
10487 apex_key {
10488 name: "myapex.key",
10489 public_key: "testkey.avbpubkey",
10490 private_key: "testkey.pem",
10491 }
10492 `)
10493
Cole Faust3b806d32024-03-11 15:15:03 -070010494 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits
Colin Crossbd3a16b2023-04-25 11:30:51 -070010495 android.AssertStringListDoesNotContain(t, "filesystem should not have libbar",
10496 inputs.Strings(),
10497 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
10498}
Yu Liueae7b362023-11-16 17:05:47 -080010499
10500var apex_default_bp = `
10501 apex_key {
10502 name: "myapex.key",
10503 public_key: "testkey.avbpubkey",
10504 private_key: "testkey.pem",
10505 }
10506
10507 filegroup {
10508 name: "myapex.manifest",
10509 srcs: ["apex_manifest.json"],
10510 }
10511
10512 filegroup {
10513 name: "myapex.androidmanifest",
10514 srcs: ["AndroidManifest.xml"],
10515 }
10516`
10517
10518func TestAconfigFilesJavaDeps(t *testing.T) {
10519 ctx := testApex(t, apex_default_bp+`
10520 apex {
10521 name: "myapex",
10522 manifest: ":myapex.manifest",
10523 androidManifest: ":myapex.androidmanifest",
10524 key: "myapex.key",
10525 java_libs: [
10526 "my_java_library_foo",
10527 "my_java_library_bar",
10528 ],
10529 updatable: false,
10530 }
10531
10532 java_library {
10533 name: "my_java_library_foo",
10534 srcs: ["foo/bar/MyClass.java"],
10535 sdk_version: "none",
10536 system_modules: "none",
10537 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010538 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010539 "myapex",
10540 ],
10541 }
10542
10543 java_library {
10544 name: "my_java_library_bar",
10545 srcs: ["foo/bar/MyClass.java"],
10546 sdk_version: "none",
10547 system_modules: "none",
10548 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010549 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010550 "myapex",
10551 ],
10552 }
10553
10554 aconfig_declarations {
10555 name: "my_aconfig_declarations_foo",
10556 package: "com.example.package",
10557 container: "myapex",
10558 srcs: ["foo.aconfig"],
10559 }
10560
10561 java_aconfig_library {
10562 name: "my_java_aconfig_library_foo",
10563 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010564 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010565 "myapex",
10566 ],
10567 }
10568
10569 aconfig_declarations {
10570 name: "my_aconfig_declarations_bar",
10571 package: "com.example.package",
10572 container: "myapex",
10573 srcs: ["bar.aconfig"],
10574 }
10575
10576 java_aconfig_library {
10577 name: "my_java_aconfig_library_bar",
10578 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010579 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010580 "myapex",
10581 ],
10582 }
10583 `)
10584
10585 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10586 s := mod.Rule("apexRule").Args["copy_commands"]
10587 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Yu Liubba555e2024-02-17 00:36:42 +000010588 if len(copyCmds) != 8 {
Yu Liueae7b362023-11-16 17:05:47 -080010589 t.Fatalf("Expected 5 commands, got %d in:\n%s", len(copyCmds), s)
10590 }
10591
Yu Liuab31c822024-02-28 22:21:31 +000010592 ensureMatches(t, copyCmds[4], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10593 ensureMatches(t, copyCmds[5], "^cp -f .*/package.map .*/image.apex/etc$")
10594 ensureMatches(t, copyCmds[6], "^cp -f .*/flag.map .*/image.apex/etc$")
10595 ensureMatches(t, copyCmds[7], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liueae7b362023-11-16 17:05:47 -080010596
Yu Liubba555e2024-02-17 00:36:42 +000010597 inputs := []string{
10598 "my_aconfig_declarations_foo/intermediate.pb",
10599 "my_aconfig_declarations_bar/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010600 }
Yu Liubba555e2024-02-17 00:36:42 +000010601 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10602 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10603 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10604 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010605}
10606
10607func TestAconfigFilesJavaAndCcDeps(t *testing.T) {
10608 ctx := testApex(t, apex_default_bp+`
10609 apex {
10610 name: "myapex",
10611 manifest: ":myapex.manifest",
10612 androidManifest: ":myapex.androidmanifest",
10613 key: "myapex.key",
10614 java_libs: [
10615 "my_java_library_foo",
10616 ],
10617 native_shared_libs: [
10618 "my_cc_library_bar",
10619 ],
10620 binaries: [
10621 "my_cc_binary_baz",
10622 ],
10623 updatable: false,
10624 }
10625
10626 java_library {
10627 name: "my_java_library_foo",
10628 srcs: ["foo/bar/MyClass.java"],
10629 sdk_version: "none",
10630 system_modules: "none",
10631 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010632 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010633 "myapex",
10634 ],
10635 }
10636
10637 cc_library {
10638 name: "my_cc_library_bar",
10639 srcs: ["foo/bar/MyClass.cc"],
Yu Liucec0e412023-11-30 16:45:50 -080010640 static_libs: [
10641 "my_cc_aconfig_library_bar",
10642 "my_cc_aconfig_library_baz",
10643 ],
Yu Liueae7b362023-11-16 17:05:47 -080010644 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010645 "myapex",
10646 ],
10647 }
10648
10649 cc_binary {
10650 name: "my_cc_binary_baz",
10651 srcs: ["foo/bar/MyClass.cc"],
10652 static_libs: ["my_cc_aconfig_library_baz"],
Yu Liueae7b362023-11-16 17:05:47 -080010653 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010654 "myapex",
10655 ],
10656 }
10657
10658 aconfig_declarations {
10659 name: "my_aconfig_declarations_foo",
10660 package: "com.example.package",
10661 container: "myapex",
10662 srcs: ["foo.aconfig"],
10663 }
10664
10665 java_aconfig_library {
10666 name: "my_java_aconfig_library_foo",
10667 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010668 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010669 "myapex",
10670 ],
10671 }
10672
10673 aconfig_declarations {
10674 name: "my_aconfig_declarations_bar",
10675 package: "com.example.package",
10676 container: "myapex",
10677 srcs: ["bar.aconfig"],
10678 }
10679
10680 cc_aconfig_library {
10681 name: "my_cc_aconfig_library_bar",
10682 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010683 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010684 "myapex",
10685 ],
10686 }
10687
10688 aconfig_declarations {
10689 name: "my_aconfig_declarations_baz",
10690 package: "com.example.package",
10691 container: "myapex",
10692 srcs: ["baz.aconfig"],
10693 }
10694
10695 cc_aconfig_library {
10696 name: "my_cc_aconfig_library_baz",
10697 aconfig_declarations: "my_aconfig_declarations_baz",
Yu Liueae7b362023-11-16 17:05:47 -080010698 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010699 "myapex",
10700 ],
10701 }
10702
10703 cc_library {
10704 name: "server_configurable_flags",
10705 srcs: ["server_configurable_flags.cc"],
10706 }
Ted Bauerf0f18592024-04-23 18:25:26 +000010707 cc_library {
10708 name: "libbase",
10709 srcs: ["libbase.cc"],
Ted Bauer1e96f8c2024-04-25 19:50:01 +000010710 apex_available: [
10711 "myapex",
10712 ],
Ted Bauerf0f18592024-04-23 18:25:26 +000010713 }
10714 cc_library {
10715 name: "libaconfig_storage_read_api_cc",
10716 srcs: ["libaconfig_storage_read_api_cc.cc"],
10717 }
10718 cc_library {
10719 name: "libaconfig_storage_protos_cc",
10720 srcs: ["libaconfig_storage_protos_cc.cc"],
10721 }
Yu Liueae7b362023-11-16 17:05:47 -080010722 `)
10723
10724 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10725 s := mod.Rule("apexRule").Args["copy_commands"]
10726 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Yu Liubba555e2024-02-17 00:36:42 +000010727 if len(copyCmds) != 12 {
10728 t.Fatalf("Expected 12 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010729 }
10730
Yu Liuab31c822024-02-28 22:21:31 +000010731 ensureMatches(t, copyCmds[8], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10732 ensureMatches(t, copyCmds[9], "^cp -f .*/package.map .*/image.apex/etc$")
10733 ensureMatches(t, copyCmds[10], "^cp -f .*/flag.map .*/image.apex/etc$")
10734 ensureMatches(t, copyCmds[11], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liueae7b362023-11-16 17:05:47 -080010735
Yu Liubba555e2024-02-17 00:36:42 +000010736 inputs := []string{
10737 "my_aconfig_declarations_foo/intermediate.pb",
10738 "my_cc_library_bar/android_arm64_armv8-a_shared_apex10000/myapex/aconfig_merged.pb",
10739 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010740 }
Yu Liubba555e2024-02-17 00:36:42 +000010741 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10742 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10743 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10744 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010745}
10746
Yu Liucec0e412023-11-30 16:45:50 -080010747func TestAconfigFilesRustDeps(t *testing.T) {
10748 ctx := testApex(t, apex_default_bp+`
10749 apex {
10750 name: "myapex",
10751 manifest: ":myapex.manifest",
10752 androidManifest: ":myapex.androidmanifest",
10753 key: "myapex.key",
10754 native_shared_libs: [
10755 "libmy_rust_library",
10756 ],
10757 binaries: [
10758 "my_rust_binary",
10759 ],
10760 rust_dyn_libs: [
10761 "libmy_rust_dylib",
10762 ],
10763 updatable: false,
10764 }
10765
10766 rust_library {
10767 name: "libflags_rust", // test mock
10768 crate_name: "flags_rust",
10769 srcs: ["lib.rs"],
10770 apex_available: [
10771 "myapex",
10772 ],
10773 }
10774
10775 rust_library {
10776 name: "liblazy_static", // test mock
10777 crate_name: "lazy_static",
10778 srcs: ["src/lib.rs"],
10779 apex_available: [
10780 "myapex",
10781 ],
10782 }
10783
Ted Bauer02d475c2024-03-27 20:56:26 +000010784 rust_library {
10785 name: "libaconfig_storage_read_api", // test mock
10786 crate_name: "aconfig_storage_read_api",
10787 srcs: ["src/lib.rs"],
10788 apex_available: [
10789 "myapex",
10790 ],
10791 }
10792
Ted Bauer6ef40db2024-03-29 14:04:10 +000010793 rust_library {
10794 name: "liblogger", // test mock
10795 crate_name: "logger",
10796 srcs: ["src/lib.rs"],
10797 apex_available: [
10798 "myapex",
10799 ],
10800 }
10801
10802 rust_library {
10803 name: "liblog_rust", // test mock
10804 crate_name: "log_rust",
10805 srcs: ["src/lib.rs"],
10806 apex_available: [
10807 "myapex",
10808 ],
10809 }
10810
Yu Liucec0e412023-11-30 16:45:50 -080010811 rust_ffi_shared {
10812 name: "libmy_rust_library",
10813 srcs: ["src/lib.rs"],
10814 rustlibs: ["libmy_rust_aconfig_library_foo"],
10815 crate_name: "my_rust_library",
10816 apex_available: [
10817 "myapex",
10818 ],
10819 }
10820
10821 rust_library_dylib {
10822 name: "libmy_rust_dylib",
10823 srcs: ["foo/bar/MyClass.rs"],
10824 rustlibs: ["libmy_rust_aconfig_library_bar"],
10825 crate_name: "my_rust_dylib",
10826 apex_available: [
10827 "myapex",
10828 ],
10829 }
10830
10831 rust_binary {
10832 name: "my_rust_binary",
10833 srcs: ["foo/bar/MyClass.rs"],
10834 rustlibs: [
10835 "libmy_rust_aconfig_library_baz",
10836 "libmy_rust_dylib",
10837 ],
10838 apex_available: [
10839 "myapex",
10840 ],
10841 }
10842
10843 aconfig_declarations {
10844 name: "my_aconfig_declarations_foo",
10845 package: "com.example.package",
10846 container: "myapex",
10847 srcs: ["foo.aconfig"],
10848 }
10849
10850 aconfig_declarations {
10851 name: "my_aconfig_declarations_bar",
10852 package: "com.example.package",
10853 container: "myapex",
10854 srcs: ["bar.aconfig"],
10855 }
10856
10857 aconfig_declarations {
10858 name: "my_aconfig_declarations_baz",
10859 package: "com.example.package",
10860 container: "myapex",
10861 srcs: ["baz.aconfig"],
10862 }
10863
10864 rust_aconfig_library {
10865 name: "libmy_rust_aconfig_library_foo",
10866 aconfig_declarations: "my_aconfig_declarations_foo",
10867 crate_name: "my_rust_aconfig_library_foo",
10868 apex_available: [
10869 "myapex",
10870 ],
10871 }
10872
10873 rust_aconfig_library {
10874 name: "libmy_rust_aconfig_library_bar",
10875 aconfig_declarations: "my_aconfig_declarations_bar",
10876 crate_name: "my_rust_aconfig_library_bar",
10877 apex_available: [
10878 "myapex",
10879 ],
10880 }
10881
10882 rust_aconfig_library {
10883 name: "libmy_rust_aconfig_library_baz",
10884 aconfig_declarations: "my_aconfig_declarations_baz",
10885 crate_name: "my_rust_aconfig_library_baz",
10886 apex_available: [
10887 "myapex",
10888 ],
10889 }
10890 `)
10891
10892 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10893 s := mod.Rule("apexRule").Args["copy_commands"]
10894 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Ted Bauer6ef40db2024-03-29 14:04:10 +000010895 if len(copyCmds) != 32 {
Ted Bauer02d475c2024-03-27 20:56:26 +000010896 t.Fatalf("Expected 28 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liucec0e412023-11-30 16:45:50 -080010897 }
10898
Ted Bauer6ef40db2024-03-29 14:04:10 +000010899 ensureMatches(t, copyCmds[28], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10900 ensureMatches(t, copyCmds[29], "^cp -f .*/package.map .*/image.apex/etc$")
10901 ensureMatches(t, copyCmds[30], "^cp -f .*/flag.map .*/image.apex/etc$")
10902 ensureMatches(t, copyCmds[31], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liucec0e412023-11-30 16:45:50 -080010903
Yu Liubba555e2024-02-17 00:36:42 +000010904 inputs := []string{
10905 "my_aconfig_declarations_foo/intermediate.pb",
Yu Liuab31c822024-02-28 22:21:31 +000010906 "my_aconfig_declarations_bar/intermediate.pb",
10907 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liubba555e2024-02-17 00:36:42 +000010908 "my_rust_binary/android_arm64_armv8-a_apex10000/myapex/aconfig_merged.pb",
10909 }
10910 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10911 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10912 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10913 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
10914}
10915
10916func VerifyAconfigRule(t *testing.T, mod *android.TestingModule, desc string, inputs []string, output string, container string, file_type string) {
10917 aconfigRule := mod.Description(desc)
10918 s := " " + aconfigRule.Args["cache_files"]
Yu Liucec0e412023-11-30 16:45:50 -080010919 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
Yu Liubba555e2024-02-17 00:36:42 +000010920 if len(aconfigArgs) != len(inputs) {
10921 t.Fatalf("Expected %d commands, got %d in:\n%s", len(inputs), len(aconfigArgs), s)
Yu Liucec0e412023-11-30 16:45:50 -080010922 }
Yu Liucec0e412023-11-30 16:45:50 -080010923
Yu Liubba555e2024-02-17 00:36:42 +000010924 ensureEquals(t, container, aconfigRule.Args["container"])
10925 ensureEquals(t, file_type, aconfigRule.Args["file_type"])
10926
10927 buildParams := aconfigRule.BuildParams
10928 for _, input := range inputs {
10929 android.EnsureListContainsSuffix(t, aconfigArgs, input)
10930 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), input)
Yu Liucec0e412023-11-30 16:45:50 -080010931 }
Yu Liubba555e2024-02-17 00:36:42 +000010932
10933 ensureContains(t, buildParams.Output.String(), output)
Yu Liucec0e412023-11-30 16:45:50 -080010934}
10935
Yu Liueae7b362023-11-16 17:05:47 -080010936func TestAconfigFilesOnlyMatchCurrentApex(t *testing.T) {
10937 ctx := testApex(t, apex_default_bp+`
10938 apex {
10939 name: "myapex",
10940 manifest: ":myapex.manifest",
10941 androidManifest: ":myapex.androidmanifest",
10942 key: "myapex.key",
10943 java_libs: [
10944 "my_java_library_foo",
10945 "other_java_library_bar",
10946 ],
10947 updatable: false,
10948 }
10949
10950 java_library {
10951 name: "my_java_library_foo",
10952 srcs: ["foo/bar/MyClass.java"],
10953 sdk_version: "none",
10954 system_modules: "none",
10955 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010956 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010957 "myapex",
10958 ],
10959 }
10960
10961 java_library {
10962 name: "other_java_library_bar",
10963 srcs: ["foo/bar/MyClass.java"],
10964 sdk_version: "none",
10965 system_modules: "none",
10966 static_libs: ["other_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010967 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010968 "myapex",
10969 ],
10970 }
10971
10972 aconfig_declarations {
10973 name: "my_aconfig_declarations_foo",
10974 package: "com.example.package",
10975 container: "myapex",
10976 srcs: ["foo.aconfig"],
10977 }
10978
10979 java_aconfig_library {
10980 name: "my_java_aconfig_library_foo",
10981 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010982 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010983 "myapex",
10984 ],
10985 }
10986
10987 aconfig_declarations {
10988 name: "other_aconfig_declarations_bar",
10989 package: "com.example.package",
10990 container: "otherapex",
10991 srcs: ["bar.aconfig"],
10992 }
10993
10994 java_aconfig_library {
10995 name: "other_java_aconfig_library_bar",
10996 aconfig_declarations: "other_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010997 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010998 "myapex",
10999 ],
11000 }
11001 `)
11002
11003 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11004 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11005 s := " " + combineAconfigRule.Args["cache_files"]
11006 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11007 if len(aconfigArgs) != 1 {
11008 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
11009 }
11010 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11011
11012 buildParams := combineAconfigRule.BuildParams
11013 if len(buildParams.Inputs) != 1 {
11014 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
11015 }
11016 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11017 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11018}
11019
11020func TestAconfigFilesRemoveDuplicates(t *testing.T) {
11021 ctx := testApex(t, apex_default_bp+`
11022 apex {
11023 name: "myapex",
11024 manifest: ":myapex.manifest",
11025 androidManifest: ":myapex.androidmanifest",
11026 key: "myapex.key",
11027 java_libs: [
11028 "my_java_library_foo",
11029 "my_java_library_bar",
11030 ],
11031 updatable: false,
11032 }
11033
11034 java_library {
11035 name: "my_java_library_foo",
11036 srcs: ["foo/bar/MyClass.java"],
11037 sdk_version: "none",
11038 system_modules: "none",
11039 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080011040 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011041 "myapex",
11042 ],
11043 }
11044
11045 java_library {
11046 name: "my_java_library_bar",
11047 srcs: ["foo/bar/MyClass.java"],
11048 sdk_version: "none",
11049 system_modules: "none",
11050 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080011051 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011052 "myapex",
11053 ],
11054 }
11055
11056 aconfig_declarations {
11057 name: "my_aconfig_declarations_foo",
11058 package: "com.example.package",
11059 container: "myapex",
11060 srcs: ["foo.aconfig"],
11061 }
11062
11063 java_aconfig_library {
11064 name: "my_java_aconfig_library_foo",
11065 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011066 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011067 "myapex",
11068 ],
11069 }
11070
11071 java_aconfig_library {
11072 name: "my_java_aconfig_library_bar",
11073 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011074 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011075 "myapex",
11076 ],
11077 }
11078 `)
11079
11080 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11081 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11082 s := " " + combineAconfigRule.Args["cache_files"]
11083 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11084 if len(aconfigArgs) != 1 {
11085 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
11086 }
11087 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11088
11089 buildParams := combineAconfigRule.BuildParams
11090 if len(buildParams.Inputs) != 1 {
11091 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
11092 }
11093 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11094 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11095}
Spandan Das5be63332023-12-13 00:06:32 +000011096
11097// Test that the boot jars come from the _selected_ apex prebuilt
11098// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11099func TestBootDexJarsMultipleApexPrebuilts(t *testing.T) {
11100 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
11101 t.Helper()
11102 s := ctx.ModuleForTests("dex_bootjars", "android_common")
11103 foundLibfooJar := false
11104 base := stem + ".jar"
11105 for _, output := range s.AllOutputs() {
11106 if filepath.Base(output) == base {
11107 foundLibfooJar = true
11108 buildRule := s.Output(output)
11109 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
11110 }
11111 }
11112 if !foundLibfooJar {
11113 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
11114 }
11115 }
11116
Spandan Das64c9e0c2023-12-20 20:13:34 +000011117 // Check that the boot jars of the selected apex are run through boot_jars_package_check
11118 // This validates that the jars on the bootclasspath do not contain packages outside an allowlist
11119 checkBootJarsPackageCheck := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11120 platformBcp := ctx.ModuleForTests("platform-bootclasspath", "android_common")
11121 bootJarsCheckRule := platformBcp.Rule("boot_jars_package_check")
11122 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)
11123 }
11124
11125 // Check that the boot jars used to generate the monolithic hiddenapi flags come from the selected apex
11126 checkBootJarsForMonolithicHiddenapi := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11127 monolithicHiddenapiFlagsCmd := ctx.ModuleForTests("platform-bootclasspath", "android_common").Output("out/soong/hiddenapi/hiddenapi-stub-flags.txt").RuleParams.Command
11128 android.AssertStringMatches(t, "Could not find the correct boot dex jar in monolithic hiddenapi flags generation command", monolithicHiddenapiFlagsCmd, "--boot-dex="+expectedBootJar)
11129 }
11130
Spandan Das5be63332023-12-13 00:06:32 +000011131 bp := `
11132 // Source APEX.
11133
11134 java_library {
11135 name: "framework-foo",
11136 srcs: ["foo.java"],
11137 installable: true,
11138 apex_available: [
11139 "com.android.foo",
11140 ],
11141 }
11142
11143 bootclasspath_fragment {
11144 name: "foo-bootclasspath-fragment",
11145 contents: ["framework-foo"],
11146 apex_available: [
11147 "com.android.foo",
11148 ],
11149 hidden_api: {
11150 split_packages: ["*"],
11151 },
11152 }
11153
11154 apex_key {
11155 name: "com.android.foo.key",
11156 public_key: "com.android.foo.avbpubkey",
11157 private_key: "com.android.foo.pem",
11158 }
11159
11160 apex {
11161 name: "com.android.foo",
11162 key: "com.android.foo.key",
11163 bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11164 updatable: false,
11165 }
11166
11167 // Prebuilt APEX.
11168
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011169 java_sdk_library_import {
Spandan Das5be63332023-12-13 00:06:32 +000011170 name: "framework-foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011171 public: {
11172 jars: ["foo.jar"],
11173 },
Spandan Das5be63332023-12-13 00:06:32 +000011174 apex_available: ["com.android.foo"],
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011175 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +000011176 }
11177
11178 prebuilt_bootclasspath_fragment {
11179 name: "foo-bootclasspath-fragment",
11180 contents: ["framework-foo"],
11181 hidden_api: {
11182 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
11183 metadata: "my-bootclasspath-fragment/metadata.csv",
11184 index: "my-bootclasspath-fragment/index.csv",
11185 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
11186 all_flags: "my-bootclasspath-fragment/all-flags.csv",
11187 },
11188 apex_available: [
11189 "com.android.foo",
11190 ],
11191 }
11192
11193 prebuilt_apex {
11194 name: "com.android.foo",
11195 apex_name: "com.android.foo",
11196 src: "com.android.foo-arm.apex",
11197 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11198 }
11199
11200 // Another Prebuilt ART APEX
11201 prebuilt_apex {
11202 name: "com.android.foo.v2",
11203 apex_name: "com.android.foo", // Used to determine the API domain
11204 src: "com.android.foo-arm.apex",
11205 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11206 }
11207
11208 // APEX contribution modules
11209
11210 apex_contributions {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011211 name: "foo.source.contributions",
Spandan Das5be63332023-12-13 00:06:32 +000011212 api_domain: "com.android.foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011213 contents: ["com.android.foo"],
11214 }
11215
11216 apex_contributions {
11217 name: "foo.prebuilt.contributions",
11218 api_domain: "com.android.foo",
11219 contents: ["prebuilt_com.android.foo"],
11220 }
11221
11222 apex_contributions {
11223 name: "foo.prebuilt.v2.contributions",
11224 api_domain: "com.android.foo",
11225 contents: ["com.android.foo.v2"], // prebuilt_ prefix is missing because of prebuilt_rename mutator
Spandan Das5be63332023-12-13 00:06:32 +000011226 }
11227 `
11228
11229 testCases := []struct {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011230 desc string
11231 selectedApexContributions string
11232 expectedBootJar string
Spandan Das5be63332023-12-13 00:06:32 +000011233 }{
11234 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011235 desc: "Source apex com.android.foo is selected, bootjar should come from source java library",
11236 selectedApexContributions: "foo.source.contributions",
11237 expectedBootJar: "out/soong/.intermediates/foo-bootclasspath-fragment/android_common_apex10000/hiddenapi-modular/encoded/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011238 },
11239 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011240 desc: "Prebuilt apex prebuilt_com.android.foo is selected, profile should come from .prof deapexed from the prebuilt",
11241 selectedApexContributions: "foo.prebuilt.contributions",
11242 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011243 },
11244 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011245 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected, profile should come from .prof deapexed from the prebuilt",
11246 selectedApexContributions: "foo.prebuilt.v2.contributions",
11247 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.v2.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011248 },
11249 }
11250
11251 fragment := java.ApexVariantReference{
11252 Apex: proptools.StringPtr("com.android.foo"),
11253 Module: proptools.StringPtr("foo-bootclasspath-fragment"),
11254 }
11255
11256 for _, tc := range testCases {
11257 preparer := android.GroupFixturePreparers(
11258 java.FixtureConfigureApexBootJars("com.android.foo:framework-foo"),
11259 android.FixtureMergeMockFs(map[string][]byte{
11260 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11261 }),
11262 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
11263 variables.BuildFlags = map[string]string{
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011264 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": tc.selectedApexContributions,
Spandan Das5be63332023-12-13 00:06:32 +000011265 }
11266 }),
11267 )
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011268 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das5be63332023-12-13 00:06:32 +000011269 checkBootDexJarPath(t, ctx, "framework-foo", tc.expectedBootJar)
Spandan Das64c9e0c2023-12-20 20:13:34 +000011270 checkBootJarsPackageCheck(t, ctx, tc.expectedBootJar)
11271 checkBootJarsForMonolithicHiddenapi(t, ctx, tc.expectedBootJar)
Spandan Das5be63332023-12-13 00:06:32 +000011272 }
11273}
Spandan Das3576e762024-01-03 18:57:03 +000011274
11275// Test that product packaging installs the selected mainline module (either source or a specific prebuilt)
11276// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11277func TestInstallationRulesForMultipleApexPrebuilts(t *testing.T) {
11278 // check that the LOCAL_MODULE in the generated mk file matches the name used in PRODUCT_PACKAGES
11279 // Since the name used in PRODUCT_PACKAGES does not contain prebuilt_ prefix, LOCAL_MODULE should not contain any prefix either
11280 checkLocalModuleName := func(t *testing.T, ctx *android.TestContext, soongApexModuleName string, expectedLocalModuleName string) {
11281 // Variations are created based on apex_name
11282 entries := android.AndroidMkEntriesForTest(t, ctx, ctx.ModuleForTests(soongApexModuleName, "android_common_com.android.foo").Module())
11283 android.AssertStringEquals(t, "LOCAL_MODULE of the prebuilt apex must match the name listed in PRODUCT_PACKAGES", expectedLocalModuleName, entries[0].EntryMap["LOCAL_MODULE"][0])
11284 }
11285 // for a mainline module family, check that only the flagged soong module is visible to make
11286 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleName string, hiddenModuleNames []string) {
11287 variation := func(moduleName string) string {
11288 ret := "android_common_com.android.foo"
11289 if moduleName == "com.google.android.foo" {
11290 ret = "android_common_com.google.android.foo_com.android.foo"
11291 }
11292 return ret
11293 }
11294
11295 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11296 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11297
11298 for _, hiddenModuleName := range hiddenModuleNames {
11299 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11300 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11301
11302 }
11303 }
11304
11305 bp := `
11306 apex_key {
11307 name: "com.android.foo.key",
11308 public_key: "com.android.foo.avbpubkey",
11309 private_key: "com.android.foo.pem",
11310 }
11311
11312 // AOSP source apex
11313 apex {
11314 name: "com.android.foo",
11315 key: "com.android.foo.key",
11316 updatable: false,
11317 }
11318
11319 // Google source apex
11320 override_apex {
11321 name: "com.google.android.foo",
11322 base: "com.android.foo",
11323 key: "com.android.foo.key",
11324 }
11325
11326 // Prebuilt Google APEX.
11327
11328 prebuilt_apex {
11329 name: "com.google.android.foo",
11330 apex_name: "com.android.foo",
11331 src: "com.android.foo-arm.apex",
11332 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11333 }
11334
11335 // Another Prebuilt Google APEX
11336 prebuilt_apex {
11337 name: "com.google.android.foo.v2",
11338 apex_name: "com.android.foo",
11339 source_apex_name: "com.google.android.foo", // source_apex_name becomes LOCAL_MODULE in the generated mk file
11340 src: "com.android.foo-arm.apex",
11341 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11342 }
11343
11344 // APEX contribution modules
11345
11346 apex_contributions {
11347 name: "foo.source.contributions",
11348 api_domain: "com.android.foo",
11349 contents: ["com.google.android.foo"],
11350 }
11351
11352 apex_contributions {
11353 name: "foo.prebuilt.contributions",
11354 api_domain: "com.android.foo",
11355 contents: ["prebuilt_com.google.android.foo"],
11356 }
11357
11358 apex_contributions {
11359 name: "foo.prebuilt.v2.contributions",
11360 api_domain: "com.android.foo",
11361 contents: ["prebuilt_com.google.android.foo.v2"],
11362 }
11363
11364 // This is an incompatible module because it selects multiple versions of the same mainline module
11365 apex_contributions {
11366 name: "foo.prebuilt.duplicate.contributions",
11367 api_domain: "com.android.foo",
11368 contents: [
11369 "prebuilt_com.google.android.foo",
11370 "prebuilt_com.google.android.foo.v2",
11371 ],
11372 }
11373 `
11374
11375 testCases := []struct {
11376 desc string
11377 selectedApexContributions string
11378 expectedVisibleModuleName string
11379 expectedHiddenModuleNames []string
11380 expectedError string
11381 }{
11382 {
11383 desc: "Source apex is selected, prebuilts should be hidden from make",
11384 selectedApexContributions: "foo.source.contributions",
11385 expectedVisibleModuleName: "com.google.android.foo",
11386 expectedHiddenModuleNames: []string{"prebuilt_com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11387 },
11388 {
11389 desc: "Prebuilt apex prebuilt_com.android.foo is selected, source and the other prebuilt should be hidden from make",
11390 selectedApexContributions: "foo.prebuilt.contributions",
11391 expectedVisibleModuleName: "prebuilt_com.google.android.foo",
11392 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11393 },
11394 {
11395 desc: "Prebuilt apex prebuilt_com.android.fooi.v2 is selected, source and the other prebuilt should be hidden from make",
11396 selectedApexContributions: "foo.prebuilt.v2.contributions",
11397 expectedVisibleModuleName: "prebuilt_com.google.android.foo.v2",
11398 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo"},
11399 },
11400 {
11401 desc: "Multiple versions of a prebuilt apex is selected in the same release config",
11402 selectedApexContributions: "foo.prebuilt.duplicate.contributions",
11403 expectedError: "Found duplicate variations of the same module in apex_contributions: prebuilt_com.google.android.foo and prebuilt_com.google.android.foo.v2",
11404 },
11405 }
11406
11407 for _, tc := range testCases {
11408 preparer := android.GroupFixturePreparers(
11409 android.FixtureMergeMockFs(map[string][]byte{
11410 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11411 }),
11412 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
11413 variables.BuildFlags = map[string]string{
11414 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": tc.selectedApexContributions,
11415 }
11416 }),
11417 )
11418 if tc.expectedError != "" {
11419 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(tc.expectedError))
11420 testApex(t, bp, preparer)
11421 return
11422 }
11423 ctx := testApex(t, bp, preparer)
11424
11425 // Check that the LOCAL_MODULE of the two prebuilts is com.android.foo
11426 // This ensures that product packaging can pick them for installation if it has been flagged by apex_contributions
11427 checkLocalModuleName(t, ctx, "prebuilt_com.google.android.foo", "com.google.android.foo")
11428 checkLocalModuleName(t, ctx, "prebuilt_com.google.android.foo.v2", "com.google.android.foo")
11429
11430 // Check that
11431 // 1. The contents of the selected apex_contributions are visible to make
11432 // 2. The rest of the apexes in the mainline module family (source or other prebuilt) is hidden from make
11433 checkHideFromMake(t, ctx, tc.expectedVisibleModuleName, tc.expectedHiddenModuleNames)
11434 }
11435}
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011436
11437func TestAconfifDeclarationsValidation(t *testing.T) {
11438 aconfigDeclarationLibraryString := func(moduleNames []string) (ret string) {
11439 for _, moduleName := range moduleNames {
11440 ret += fmt.Sprintf(`
11441 aconfig_declarations {
11442 name: "%[1]s",
11443 package: "com.example.package",
11444 srcs: [
11445 "%[1]s.aconfig",
11446 ],
11447 }
11448 java_aconfig_library {
11449 name: "%[1]s-lib",
11450 aconfig_declarations: "%[1]s",
11451 }
11452 `, moduleName)
11453 }
11454 return ret
11455 }
11456
11457 result := android.GroupFixturePreparers(
11458 prepareForApexTest,
11459 java.PrepareForTestWithJavaSdkLibraryFiles,
11460 java.FixtureWithLastReleaseApis("foo"),
11461 android.FixtureModifyConfig(func(config android.Config) {
11462 config.SetApiLibraries([]string{"foo"})
11463 }),
11464 ).RunTestWithBp(t, `
11465 java_library {
11466 name: "baz-java-lib",
11467 static_libs: [
11468 "baz-lib",
11469 ],
11470 }
11471 filegroup {
11472 name: "qux-filegroup",
11473 srcs: [
11474 ":qux-lib{.generated_srcjars}",
11475 ],
11476 }
11477 filegroup {
11478 name: "qux-another-filegroup",
11479 srcs: [
11480 ":qux-filegroup",
11481 ],
11482 }
11483 java_library {
11484 name: "quux-java-lib",
11485 srcs: [
11486 "a.java",
11487 ],
11488 libs: [
11489 "quux-lib",
11490 ],
11491 }
11492 java_sdk_library {
11493 name: "foo",
11494 srcs: [
11495 ":qux-another-filegroup",
11496 ],
11497 api_packages: ["foo"],
11498 system: {
11499 enabled: true,
11500 },
11501 module_lib: {
11502 enabled: true,
11503 },
11504 test: {
11505 enabled: true,
11506 },
11507 static_libs: [
11508 "bar-lib",
11509 ],
11510 libs: [
11511 "baz-java-lib",
11512 "quux-java-lib",
11513 ],
11514 aconfig_declarations: [
11515 "bar",
11516 ],
11517 }
11518 `+aconfigDeclarationLibraryString([]string{"bar", "baz", "qux", "quux"}))
11519
11520 m := result.ModuleForTests("foo.stubs.source", "android_common")
11521 outDir := "out/soong/.intermediates"
11522
11523 // Arguments passed to aconfig to retrieve the state of the flags defined in the
11524 // textproto files
11525 aconfigFlagArgs := m.Output("released-flagged-apis-exportable.txt").Args["flags_path"]
11526
11527 // "bar-lib" is a static_lib of "foo" and is passed to metalava as classpath. Thus the
11528 // cache file provided by the associated aconfig_declarations module "bar" should be passed
11529 // to aconfig.
11530 android.AssertStringDoesContain(t, "cache file of a java_aconfig_library static_lib "+
11531 "passed as an input",
11532 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "bar"))
11533
11534 // "baz-java-lib", which statically depends on "baz-lib", is a lib of "foo" and is passed
11535 // to metalava as classpath. Thus the cache file provided by the associated
11536 // aconfig_declarations module "baz" should be passed to aconfig.
11537 android.AssertStringDoesContain(t, "cache file of a lib that statically depends on "+
11538 "java_aconfig_library passed as an input",
11539 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "baz"))
11540
11541 // "qux-lib" is passed to metalava as src via the filegroup, thus the cache file provided by
11542 // the associated aconfig_declarations module "qux" should be passed to aconfig.
11543 android.AssertStringDoesContain(t, "cache file of srcs java_aconfig_library passed as an "+
11544 "input",
11545 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "qux"))
11546
11547 // "quux-java-lib" is a lib of "foo" and is passed to metalava as classpath, but does not
11548 // statically depend on "quux-lib". Therefore, the cache file provided by the associated
11549 // aconfig_declarations module "quux" should not be passed to aconfig.
11550 android.AssertStringDoesNotContain(t, "cache file of a lib that does not statically "+
11551 "depend on java_aconfig_library not passed as an input",
11552 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "quux"))
11553}