blob: 8a3735cbe7727f97ccd6e26ad61b934af92dfc07 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jooyung Han39edb6c2019-11-06 16:53:07 +090019 "path"
Paul Duffin37856732021-02-26 14:24:15 +000020 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010022 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090023 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090024 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090025 "strings"
26 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090027
Yu Liueae7b362023-11-16 17:05:47 -080028 "android/soong/aconfig/codegen"
Jooyung Han20348752023-12-05 15:23:56 +090029
Kiyoung Kim487689e2022-07-26 09:48:22 +090030 "github.com/google/blueprint"
Jiyong Parkda6eb592018-12-19 17:12:36 +090031 "github.com/google/blueprint/proptools"
32
33 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080034 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090035 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000036 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070037 prebuilt_etc "android/soong/etc"
Colin Crossbd3a16b2023-04-25 11:30:51 -070038 "android/soong/filesystem"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090039 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090040 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070041 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090042)
43
Jooyung Hand3639552019-08-09 12:57:43 +090044// names returns name list from white space separated string
45func names(s string) (ns []string) {
46 for _, n := range strings.Split(s, " ") {
47 if len(n) > 0 {
48 ns = append(ns, n)
49 }
50 }
51 return
52}
53
Paul Duffin40b62572021-03-20 11:39:01 +000054func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090055 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010056 android.GroupFixturePreparers(
57 prepareForApexTest,
58 android.GroupFixturePreparers(preparers...),
59 ).
Paul Duffine05480a2021-03-08 15:07:14 +000060 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000061 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090062}
63
Paul Duffin40b62572021-03-20 11:39:01 +000064func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090065 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010066
67 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000068 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010069 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000070 }
Paul Duffin284165a2021-03-29 01:50:31 +010071
72 result := android.GroupFixturePreparers(
73 prepareForApexTest,
74 android.GroupFixturePreparers(preparers...),
75 optionalBpPreparer,
76 ).RunTest(t)
77
Paul Duffine05480a2021-03-08 15:07:14 +000078 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090079}
80
Paul Duffin810f33d2021-03-09 14:12:32 +000081func withFiles(files android.MockFS) android.FixturePreparer {
82 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090083}
84
Paul Duffin810f33d2021-03-09 14:12:32 +000085func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
86 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090087 for k, v := range targets {
88 config.Targets[k] = v
89 }
Paul Duffin810f33d2021-03-09 14:12:32 +000090 })
Jooyung Han344d5432019-08-23 11:17:39 +090091}
92
Jooyung Han35155c42020-02-06 17:33:20 +090093// withNativeBridgeTargets sets configuration with targets including:
94// - X86_64 (primary)
95// - X86 (secondary)
96// - Arm64 on X86_64 (native bridge)
97// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000098var withNativeBridgeEnabled = android.FixtureModifyConfig(
99 func(config android.Config) {
100 config.Targets[android.Android] = []android.Target{
101 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
102 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
103 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
104 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
105 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
106 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
107 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
108 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
109 }
110 },
111)
112
113func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
114 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
115 variables.ManifestPackageNameOverrides = specs
116 })
Jooyung Han35155c42020-02-06 17:33:20 +0900117}
118
Albert Martineefabcf2022-03-21 20:11:16 +0000119func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
120 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
121 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
122 })
123}
124
Paul Duffin810f33d2021-03-09 14:12:32 +0000125var withBinder32bit = android.FixtureModifyProductVariables(
126 func(variables android.FixtureProductVariables) {
127 variables.Binder32bit = proptools.BoolPtr(true)
128 },
129)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900130
Paul Duffin810f33d2021-03-09 14:12:32 +0000131var withUnbundledBuild = android.FixtureModifyProductVariables(
132 func(variables android.FixtureProductVariables) {
133 variables.Unbundled_build = proptools.BoolPtr(true)
134 },
135)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900136
Paul Duffin284165a2021-03-29 01:50:31 +0100137// Legacy preparer used for running tests within the apex package.
138//
139// This includes everything that was needed to run any test in the apex package prior to the
140// introduction of the test fixtures. Tests that are being converted to use fixtures directly
141// rather than through the testApex...() methods should avoid using this and instead use the
142// various preparers directly, using android.GroupFixturePreparers(...) to group them when
143// necessary.
144//
145// deprecated
146var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000147 // General preparers in alphabetical order as test infrastructure will enforce correct
148 // registration order.
149 android.PrepareForTestWithAndroidBuildComponents,
150 bpf.PrepareForTestWithBpf,
151 cc.PrepareForTestWithCcBuildComponents,
Jiakai Zhangb95998b2023-05-11 16:39:27 +0100152 java.PrepareForTestWithDexpreopt,
Paul Duffin37aad602021-03-08 09:47:16 +0000153 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
154 rust.PrepareForTestWithRustDefaultModules,
155 sh.PrepareForTestWithShBuildComponents,
Yu Liueae7b362023-11-16 17:05:47 -0800156 codegen.PrepareForTestWithAconfigBuildComponents,
Paul Duffin37aad602021-03-08 09:47:16 +0000157
158 PrepareForTestWithApexBuildComponents,
159
160 // Additional apex test specific preparers.
161 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
162 filegroup {
163 name: "myapex-file_contexts",
164 srcs: [
165 "apex/myapex-file_contexts",
166 ],
167 }
168 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000169 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000170 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000171 "a.java": nil,
172 "PrebuiltAppFoo.apk": nil,
173 "PrebuiltAppFooPriv.apk": nil,
174 "apex_manifest.json": nil,
175 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000176 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
177 "system/sepolicy/apex/myapex2-file_contexts": nil,
178 "system/sepolicy/apex/otherapex-file_contexts": nil,
179 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
180 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700181 "mylib.cpp": nil,
182 "mytest.cpp": nil,
183 "mytest1.cpp": nil,
184 "mytest2.cpp": nil,
185 "mytest3.cpp": nil,
186 "myprebuilt": nil,
187 "my_include": nil,
188 "foo/bar/MyClass.java": nil,
189 "prebuilt.jar": nil,
190 "prebuilt.so": nil,
191 "vendor/foo/devkeys/test.x509.pem": nil,
192 "vendor/foo/devkeys/test.pk8": nil,
193 "testkey.x509.pem": nil,
194 "testkey.pk8": nil,
195 "testkey.override.x509.pem": nil,
196 "testkey.override.pk8": nil,
197 "vendor/foo/devkeys/testkey.avbpubkey": nil,
198 "vendor/foo/devkeys/testkey.pem": nil,
199 "NOTICE": nil,
200 "custom_notice": nil,
201 "custom_notice_for_static_lib": nil,
202 "testkey2.avbpubkey": nil,
203 "testkey2.pem": nil,
204 "myapex-arm64.apex": nil,
205 "myapex-arm.apex": nil,
206 "myapex.apks": nil,
207 "frameworks/base/api/current.txt": nil,
208 "framework/aidl/a.aidl": nil,
209 "dummy.txt": nil,
210 "baz": nil,
211 "bar/baz": nil,
212 "testdata/baz": nil,
213 "AppSet.apks": nil,
214 "foo.rs": nil,
215 "libfoo.jar": nil,
216 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000217 },
218 ),
219
220 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
Paul Duffin37aad602021-03-08 09:47:16 +0000221 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
222 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
223 variables.Platform_sdk_codename = proptools.StringPtr("Q")
224 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000225 // "Tiramisu" needs to be in the next line for compatibility with soong code,
226 // not because of these tests specifically (it's not used by the tests)
227 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000228 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000229 }),
230)
231
Paul Duffin52bfaa42021-03-23 23:40:12 +0000232var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
233 "system/sepolicy/apex/myapex-file_contexts": nil,
234})
235
Jooyung Han643adc42020-02-27 13:50:06 +0900236// ensure that 'result' equals 'expected'
237func ensureEquals(t *testing.T, result string, expected string) {
238 t.Helper()
239 if result != expected {
240 t.Errorf("%q != %q", expected, result)
241 }
242}
243
Jiyong Park25fc6a92018-11-18 18:02:45 +0900244// ensure that 'result' contains 'expected'
245func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900246 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900247 if !strings.Contains(result, expected) {
248 t.Errorf("%q is not found in %q", expected, result)
249 }
250}
251
Liz Kammer5bd365f2020-05-27 15:15:11 -0700252// ensure that 'result' contains 'expected' exactly one time
253func ensureContainsOnce(t *testing.T, result string, expected string) {
254 t.Helper()
255 count := strings.Count(result, expected)
256 if count != 1 {
257 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
258 }
259}
260
Jiyong Park25fc6a92018-11-18 18:02:45 +0900261// ensures that 'result' does not contain 'notExpected'
262func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900263 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900264 if strings.Contains(result, notExpected) {
265 t.Errorf("%q is found in %q", notExpected, result)
266 }
267}
268
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700269func ensureMatches(t *testing.T, result string, expectedRex string) {
270 ok, err := regexp.MatchString(expectedRex, result)
271 if err != nil {
272 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
273 return
274 }
275 if !ok {
276 t.Errorf("%s does not match regular expession %s", result, expectedRex)
277 }
278}
279
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900281 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900282 if !android.InList(expected, result) {
283 t.Errorf("%q is not found in %v", expected, result)
284 }
285}
286
287func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900288 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900289 if android.InList(notExpected, result) {
290 t.Errorf("%q is found in %v", notExpected, result)
291 }
292}
293
Jooyung Hane1633032019-08-01 17:41:43 +0900294func ensureListEmpty(t *testing.T, result []string) {
295 t.Helper()
296 if len(result) > 0 {
297 t.Errorf("%q is expected to be empty", result)
298 }
299}
300
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000301func ensureListNotEmpty(t *testing.T, result []string) {
302 t.Helper()
303 if len(result) == 0 {
304 t.Errorf("%q is expected to be not empty", result)
305 }
306}
307
Jiyong Park25fc6a92018-11-18 18:02:45 +0900308// Minimal test
309func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800310 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900311 apex_defaults {
312 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900313 manifest: ":myapex.manifest",
314 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900315 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900316 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900317 native_shared_libs: [
318 "mylib",
319 "libfoo.ffi",
320 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900321 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800322 multilib: {
323 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900324 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800325 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900326 },
Jiyong Park77acec62020-06-01 21:39:15 +0900327 java_libs: [
328 "myjar",
329 "myjar_dex",
330 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000331 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900332 }
333
Jiyong Park30ca9372019-02-07 16:27:23 +0900334 apex {
335 name: "myapex",
336 defaults: ["myapex-defaults"],
337 }
338
Jiyong Park25fc6a92018-11-18 18:02:45 +0900339 apex_key {
340 name: "myapex.key",
341 public_key: "testkey.avbpubkey",
342 private_key: "testkey.pem",
343 }
344
Jiyong Park809bb722019-02-13 21:33:49 +0900345 filegroup {
346 name: "myapex.manifest",
347 srcs: ["apex_manifest.json"],
348 }
349
350 filegroup {
351 name: "myapex.androidmanifest",
352 srcs: ["AndroidManifest.xml"],
353 }
354
Jiyong Park25fc6a92018-11-18 18:02:45 +0900355 cc_library {
356 name: "mylib",
357 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900358 shared_libs: [
359 "mylib2",
360 "libbar.ffi",
361 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900362 system_shared_libs: [],
363 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000364 // TODO: remove //apex_available:platform
365 apex_available: [
366 "//apex_available:platform",
367 "myapex",
368 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900369 }
370
Alex Light3d673592019-01-18 14:37:31 -0800371 cc_binary {
372 name: "foo",
373 srcs: ["mylib.cpp"],
374 compile_multilib: "both",
375 multilib: {
376 lib32: {
377 suffix: "32",
378 },
379 lib64: {
380 suffix: "64",
381 },
382 },
383 symlinks: ["foo_link_"],
384 symlink_preferred_arch: true,
385 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800386 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700387 apex_available: [ "myapex", "com.android.gki.*" ],
388 }
389
Jiyong Park99644e92020-11-17 22:21:02 +0900390 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000391 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900392 srcs: ["foo.rs"],
393 rlibs: ["libfoo.rlib.rust"],
Vinh Tran4eeb2a92023-08-14 13:29:30 -0400394 rustlibs: ["libfoo.dylib.rust"],
Jiyong Park99644e92020-11-17 22:21:02 +0900395 apex_available: ["myapex"],
396 }
397
398 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000399 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900400 srcs: ["foo.rs"],
401 crate_name: "foo",
402 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900403 shared_libs: ["libfoo.shared_from_rust"],
404 }
405
406 cc_library_shared {
407 name: "libfoo.shared_from_rust",
408 srcs: ["mylib.cpp"],
409 system_shared_libs: [],
410 stl: "none",
411 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900412 }
413
414 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000415 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900416 srcs: ["foo.rs"],
417 crate_name: "foo",
418 apex_available: ["myapex"],
419 }
420
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900421 rust_ffi_shared {
422 name: "libfoo.ffi",
423 srcs: ["foo.rs"],
424 crate_name: "foo",
425 apex_available: ["myapex"],
426 }
427
428 rust_ffi_shared {
429 name: "libbar.ffi",
430 srcs: ["foo.rs"],
431 crate_name: "bar",
432 apex_available: ["myapex"],
433 }
434
Yifan Hongd22a84a2020-07-28 17:37:46 -0700435 apex {
436 name: "com.android.gki.fake",
437 binaries: ["foo"],
438 key: "myapex.key",
439 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000440 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800441 }
442
Paul Duffindddd5462020-04-07 15:25:44 +0100443 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900444 name: "mylib2",
445 srcs: ["mylib.cpp"],
446 system_shared_libs: [],
447 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900448 static_libs: ["libstatic"],
449 // TODO: remove //apex_available:platform
450 apex_available: [
451 "//apex_available:platform",
452 "myapex",
453 ],
454 }
455
Paul Duffindddd5462020-04-07 15:25:44 +0100456 cc_prebuilt_library_shared {
457 name: "mylib2",
458 srcs: ["prebuilt.so"],
459 // TODO: remove //apex_available:platform
460 apex_available: [
461 "//apex_available:platform",
462 "myapex",
463 ],
464 }
465
Jiyong Park9918e1a2020-03-17 19:16:40 +0900466 cc_library_static {
467 name: "libstatic",
468 srcs: ["mylib.cpp"],
469 system_shared_libs: [],
470 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000471 // TODO: remove //apex_available:platform
472 apex_available: [
473 "//apex_available:platform",
474 "myapex",
475 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900476 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900477
478 java_library {
479 name: "myjar",
480 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900481 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 sdk_version: "none",
483 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900485 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000486 // TODO: remove //apex_available:platform
487 apex_available: [
488 "//apex_available:platform",
489 "myapex",
490 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900491 }
492
Jiyong Park77acec62020-06-01 21:39:15 +0900493 dex_import {
494 name: "myjar_dex",
495 jars: ["prebuilt.jar"],
496 apex_available: [
497 "//apex_available:platform",
498 "myapex",
499 ],
500 }
501
Jiyong Park7f7766d2019-07-25 22:02:35 +0900502 java_library {
503 name: "myotherjar",
504 srcs: ["foo/bar/MyClass.java"],
505 sdk_version: "none",
506 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900507 // TODO: remove //apex_available:platform
508 apex_available: [
509 "//apex_available:platform",
510 "myapex",
511 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900512 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900513
514 java_library {
515 name: "mysharedjar",
516 srcs: ["foo/bar/MyClass.java"],
517 sdk_version: "none",
518 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900519 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900520 `)
521
Jooyung Hana0503a52023-08-23 13:12:50 +0900522 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900523
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900524 // Make sure that Android.mk is created
Jooyung Hana0503a52023-08-23 13:12:50 +0900525 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700526 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900527 var builder strings.Builder
528 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
529
530 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000531 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900532 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
533
Jiyong Park42cca6c2019-04-01 11:15:50 +0900534 optFlags := apexRule.Args["opt_flags"]
535 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700536 // Ensure that the NOTICE output is being packaged as an asset.
Jooyung Hana0503a52023-08-23 13:12:50 +0900537 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900538
Jiyong Park25fc6a92018-11-18 18:02:45 +0900539 copyCmds := apexRule.Args["copy_commands"]
540
541 // Ensure that main rule creates an output
542 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
543
544 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700545 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
546 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
547 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900548 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900549 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900550
551 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700552 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
553 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900554 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900556 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900557 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900558
559 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800560 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
561 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900562 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900563 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900564 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
566 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900567 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900568 // .. but not for java libs
569 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900570 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800571
Colin Cross7113d202019-11-20 16:39:12 -0800572 // Ensure that the platform variant ends with _shared or _common
573 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
574 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900575 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
576 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900577 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
578
579 // Ensure that dynamic dependency to java libs are not included
580 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800581
582 // Ensure that all symlinks are present.
583 found_foo_link_64 := false
584 found_foo := false
585 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900586 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800587 if strings.HasSuffix(cmd, "bin/foo") {
588 found_foo = true
589 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
590 found_foo_link_64 = true
591 }
592 }
593 }
594 good := found_foo && found_foo_link_64
595 if !good {
596 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
597 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900598
Colin Crossf61d03d2023-11-02 16:56:39 -0700599 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
600 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100601 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100602 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
603 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
604 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100605
Colin Crossf61d03d2023-11-02 16:56:39 -0700606 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
607 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100608 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100609 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
610 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
611 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800612}
613
Jooyung Hanf21c7972019-12-16 22:32:06 +0900614func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800615 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900616 apex_defaults {
617 name: "myapex-defaults",
618 key: "myapex.key",
619 prebuilts: ["myetc"],
620 native_shared_libs: ["mylib"],
621 java_libs: ["myjar"],
622 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900623 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800624 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000625 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900626 }
627
628 prebuilt_etc {
629 name: "myetc",
630 src: "myprebuilt",
631 }
632
633 apex {
634 name: "myapex",
635 defaults: ["myapex-defaults"],
636 }
637
638 apex_key {
639 name: "myapex.key",
640 public_key: "testkey.avbpubkey",
641 private_key: "testkey.pem",
642 }
643
644 cc_library {
645 name: "mylib",
646 system_shared_libs: [],
647 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000648 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900649 }
650
651 java_library {
652 name: "myjar",
653 srcs: ["foo/bar/MyClass.java"],
654 sdk_version: "none",
655 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000656 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900657 }
658
659 android_app {
660 name: "AppFoo",
661 srcs: ["foo/bar/MyClass.java"],
662 sdk_version: "none",
663 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000664 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900665 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900666
667 runtime_resource_overlay {
668 name: "rro",
669 theme: "blue",
670 }
671
markchien2f59ec92020-09-02 16:23:38 +0800672 bpf {
673 name: "bpf",
674 srcs: ["bpf.c", "bpf2.c"],
675 }
676
Ken Chenfad7f9d2021-11-10 22:02:57 +0800677 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800678 name: "netdTest",
679 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800680 sub_dir: "netd",
681 }
682
Jooyung Hanf21c7972019-12-16 22:32:06 +0900683 `)
Jooyung Hana0503a52023-08-23 13:12:50 +0900684 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900685 "etc/myetc",
686 "javalib/myjar.jar",
687 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000688 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900689 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800690 "etc/bpf/bpf.o",
691 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800692 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900693 })
694}
695
Jooyung Han01a3ee22019-11-02 02:52:25 +0900696func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800697 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900698 apex {
699 name: "myapex",
700 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000701 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900702 }
703
704 apex_key {
705 name: "myapex.key",
706 public_key: "testkey.avbpubkey",
707 private_key: "testkey.pem",
708 }
709 `)
710
Jooyung Hana0503a52023-08-23 13:12:50 +0900711 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +0900712 args := module.Rule("apexRule").Args
713 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
714 t.Error("manifest should be apex_manifest.pb, but " + manifest)
715 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900716}
717
Liz Kammer4854a7d2021-05-27 14:28:27 -0400718func TestApexManifestMinSdkVersion(t *testing.T) {
719 ctx := testApex(t, `
720 apex_defaults {
721 name: "my_defaults",
722 key: "myapex.key",
723 product_specific: true,
724 file_contexts: ":my-file-contexts",
725 updatable: false,
726 }
727 apex {
728 name: "myapex_30",
729 min_sdk_version: "30",
730 defaults: ["my_defaults"],
731 }
732
733 apex {
734 name: "myapex_current",
735 min_sdk_version: "current",
736 defaults: ["my_defaults"],
737 }
738
739 apex {
740 name: "myapex_none",
741 defaults: ["my_defaults"],
742 }
743
744 apex_key {
745 name: "myapex.key",
746 public_key: "testkey.avbpubkey",
747 private_key: "testkey.pem",
748 }
749
750 filegroup {
751 name: "my-file-contexts",
752 srcs: ["product_specific_file_contexts"],
753 }
754 `, withFiles(map[string][]byte{
755 "product_specific_file_contexts": nil,
756 }), android.FixtureModifyProductVariables(
757 func(variables android.FixtureProductVariables) {
758 variables.Unbundled_build = proptools.BoolPtr(true)
759 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
760 }), android.FixtureMergeEnv(map[string]string{
761 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
762 }))
763
764 testCases := []struct {
765 module string
766 minSdkVersion string
767 }{
768 {
769 module: "myapex_30",
770 minSdkVersion: "30",
771 },
772 {
773 module: "myapex_current",
774 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
775 },
776 {
777 module: "myapex_none",
778 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
779 },
780 }
781 for _, tc := range testCases {
Jooyung Hana0503a52023-08-23 13:12:50 +0900782 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
Liz Kammer4854a7d2021-05-27 14:28:27 -0400783 args := module.Rule("apexRule").Args
784 optFlags := args["opt_flags"]
785 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
786 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
787 }
788 }
789}
790
Jihoon Kang842b9992024-02-08 01:41:51 +0000791func TestApexWithDessertSha(t *testing.T) {
792 ctx := testApex(t, `
793 apex_defaults {
794 name: "my_defaults",
795 key: "myapex.key",
796 product_specific: true,
797 file_contexts: ":my-file-contexts",
798 updatable: false,
799 }
800 apex {
801 name: "myapex_30",
802 min_sdk_version: "30",
803 defaults: ["my_defaults"],
804 }
805
806 apex {
807 name: "myapex_current",
808 min_sdk_version: "current",
809 defaults: ["my_defaults"],
810 }
811
812 apex {
813 name: "myapex_none",
814 defaults: ["my_defaults"],
815 }
816
817 apex_key {
818 name: "myapex.key",
819 public_key: "testkey.avbpubkey",
820 private_key: "testkey.pem",
821 }
822
823 filegroup {
824 name: "my-file-contexts",
825 srcs: ["product_specific_file_contexts"],
826 }
827 `, withFiles(map[string][]byte{
828 "product_specific_file_contexts": nil,
829 }), android.FixtureModifyProductVariables(
830 func(variables android.FixtureProductVariables) {
831 variables.Unbundled_build = proptools.BoolPtr(true)
832 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
833 }), android.FixtureMergeEnv(map[string]string{
834 "UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA": "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
835 }))
836
837 testCases := []struct {
838 module string
839 minSdkVersion string
840 }{
841 {
842 module: "myapex_30",
843 minSdkVersion: "30",
844 },
845 {
846 module: "myapex_current",
847 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
848 },
849 {
850 module: "myapex_none",
851 minSdkVersion: "UpsideDownCake.abcdefghijklmnopqrstuvwxyz123456",
852 },
853 }
854 for _, tc := range testCases {
855 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
856 args := module.Rule("apexRule").Args
857 optFlags := args["opt_flags"]
858 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
859 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
860 }
861 }
862}
863
Jooyung Hanaf730952023-02-28 14:13:38 +0900864func TestFileContexts(t *testing.T) {
Jooyung Hanbe953902023-05-31 16:42:16 +0900865 for _, vendor := range []bool{true, false} {
Jooyung Hanaf730952023-02-28 14:13:38 +0900866 prop := ""
Jooyung Hanbe953902023-05-31 16:42:16 +0900867 if vendor {
868 prop = "vendor: true,\n"
Jooyung Hanaf730952023-02-28 14:13:38 +0900869 }
870 ctx := testApex(t, `
871 apex {
872 name: "myapex",
873 key: "myapex.key",
Jooyung Hanaf730952023-02-28 14:13:38 +0900874 updatable: false,
Jooyung Hanaf730952023-02-28 14:13:38 +0900875 `+prop+`
876 }
877
878 apex_key {
879 name: "myapex.key",
880 public_key: "testkey.avbpubkey",
881 private_key: "testkey.pem",
882 }
Jooyung Hanbe953902023-05-31 16:42:16 +0900883 `)
Jooyung Hanaf730952023-02-28 14:13:38 +0900884
Jooyung Hana0503a52023-08-23 13:12:50 +0900885 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("file_contexts")
Jooyung Hanbe953902023-05-31 16:42:16 +0900886 if vendor {
887 android.AssertStringDoesContain(t, "should force-label as vendor_apex_metadata_file",
888 rule.RuleParams.Command,
889 "apex_manifest\\\\.pb u:object_r:vendor_apex_metadata_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900890 } else {
Jooyung Hanbe953902023-05-31 16:42:16 +0900891 android.AssertStringDoesContain(t, "should force-label as system_file",
892 rule.RuleParams.Command,
893 "apex_manifest\\\\.pb u:object_r:system_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900894 }
895 }
896}
897
Jiyong Park25fc6a92018-11-18 18:02:45 +0900898func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800899 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900900 apex {
901 name: "myapex",
902 key: "myapex.key",
903 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900904 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000905 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900906 }
907
908 apex_key {
909 name: "myapex.key",
910 public_key: "testkey.avbpubkey",
911 private_key: "testkey.pem",
912 }
913
914 cc_library {
915 name: "mylib",
916 srcs: ["mylib.cpp"],
917 shared_libs: ["mylib2", "mylib3"],
918 system_shared_libs: [],
919 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000920 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900921 }
922
923 cc_library {
924 name: "mylib2",
925 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900926 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927 system_shared_libs: [],
928 stl: "none",
929 stubs: {
930 versions: ["1", "2", "3"],
931 },
932 }
933
934 cc_library {
935 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900936 srcs: ["mylib.cpp"],
937 shared_libs: ["mylib4"],
938 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900939 stl: "none",
940 stubs: {
941 versions: ["10", "11", "12"],
942 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000943 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900944 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900945
946 cc_library {
947 name: "mylib4",
948 srcs: ["mylib.cpp"],
949 system_shared_libs: [],
950 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000951 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900952 }
Jiyong Park105dc322021-06-11 17:22:09 +0900953
954 rust_binary {
955 name: "foo.rust",
956 srcs: ["foo.rs"],
957 shared_libs: ["libfoo.shared_from_rust"],
958 prefer_rlib: true,
959 apex_available: ["myapex"],
960 }
961
962 cc_library_shared {
963 name: "libfoo.shared_from_rust",
964 srcs: ["mylib.cpp"],
965 system_shared_libs: [],
966 stl: "none",
967 stubs: {
968 versions: ["10", "11", "12"],
969 },
970 }
971
Jiyong Park25fc6a92018-11-18 18:02:45 +0900972 `)
973
Jooyung Hana0503a52023-08-23 13:12:50 +0900974 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900975 copyCmds := apexRule.Args["copy_commands"]
976
977 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800978 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900979
980 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800981 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900982
983 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800984 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900985
Colin Crossaede88c2020-08-11 12:17:01 -0700986 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900987
988 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900989 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900990 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900991 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900992
993 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -0700994 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900995 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700996 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900997
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700998 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
999 // is replaced by sharing of "cFlags" in cc/builder.go.
1000 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
1001 // module variable representing "cflags". So it was not detected by ensureNotContains.
1002 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
1003 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
1004 // including the original cflags's "-include mylib.h".
1005 //
Jiyong Park64379952018-12-13 18:37:29 +09001006 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -07001007 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1008 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +09001009
Jiyong Park85cc35a2022-07-17 11:30:47 +09001010 // Ensure that genstub for platform-provided lib is invoked with --systemapi
1011 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
1012 // Ensure that genstub for apex-provided lib is invoked with --apex
1013 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +09001014
Jooyung Hana0503a52023-08-23 13:12:50 +09001015 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +09001016 "lib64/mylib.so",
1017 "lib64/mylib3.so",
1018 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +09001019 "bin/foo.rust",
1020 "lib64/libc++.so", // by the implicit dependency from foo.rust
1021 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +09001022 })
Jiyong Park105dc322021-06-11 17:22:09 +09001023
1024 // Ensure that stub dependency from a rust module is not included
1025 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1026 // The rust module is linked to the stub cc library
Colin Cross004bd3f2023-10-02 11:39:17 -07001027 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park105dc322021-06-11 17:22:09 +09001028 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1029 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +09001030
Jooyung Hana0503a52023-08-23 13:12:50 +09001031 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jiyong Park34d5c332022-02-24 18:02:44 +09001032 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001033}
1034
Jooyung Han20348752023-12-05 15:23:56 +09001035func TestApexShouldNotEmbedStubVariant(t *testing.T) {
1036 testApexError(t, `module "myapex" .*: native_shared_libs: "libbar" is a stub`, `
1037 apex {
1038 name: "myapex",
1039 key: "myapex.key",
1040 vendor: true,
1041 updatable: false,
1042 native_shared_libs: ["libbar"], // should not add an LLNDK stub in a vendor apex
1043 }
1044
1045 apex_key {
1046 name: "myapex.key",
1047 public_key: "testkey.avbpubkey",
1048 private_key: "testkey.pem",
1049 }
1050
1051 cc_library {
1052 name: "libbar",
1053 srcs: ["mylib.cpp"],
1054 llndk: {
1055 symbol_file: "libbar.map.txt",
1056 }
1057 }
1058 `)
1059}
1060
Jiyong Park1bc84122021-06-22 20:23:05 +09001061func TestApexCanUsePrivateApis(t *testing.T) {
1062 ctx := testApex(t, `
1063 apex {
1064 name: "myapex",
1065 key: "myapex.key",
1066 native_shared_libs: ["mylib"],
1067 binaries: ["foo.rust"],
1068 updatable: false,
1069 platform_apis: true,
1070 }
1071
1072 apex_key {
1073 name: "myapex.key",
1074 public_key: "testkey.avbpubkey",
1075 private_key: "testkey.pem",
1076 }
1077
1078 cc_library {
1079 name: "mylib",
1080 srcs: ["mylib.cpp"],
1081 shared_libs: ["mylib2"],
1082 system_shared_libs: [],
1083 stl: "none",
1084 apex_available: [ "myapex" ],
1085 }
1086
1087 cc_library {
1088 name: "mylib2",
1089 srcs: ["mylib.cpp"],
1090 cflags: ["-include mylib.h"],
1091 system_shared_libs: [],
1092 stl: "none",
1093 stubs: {
1094 versions: ["1", "2", "3"],
1095 },
1096 }
1097
1098 rust_binary {
1099 name: "foo.rust",
1100 srcs: ["foo.rs"],
1101 shared_libs: ["libfoo.shared_from_rust"],
1102 prefer_rlib: true,
1103 apex_available: ["myapex"],
1104 }
1105
1106 cc_library_shared {
1107 name: "libfoo.shared_from_rust",
1108 srcs: ["mylib.cpp"],
1109 system_shared_libs: [],
1110 stl: "none",
1111 stubs: {
1112 versions: ["10", "11", "12"],
1113 },
1114 }
1115 `)
1116
Jooyung Hana0503a52023-08-23 13:12:50 +09001117 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park1bc84122021-06-22 20:23:05 +09001118 copyCmds := apexRule.Args["copy_commands"]
1119
1120 // Ensure that indirect stubs dep is not included
1121 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1122 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1123
1124 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1125 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001126 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001127 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1128 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Colin Cross004bd3f2023-10-02 11:39:17 -07001129 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001130 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1131 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1132}
1133
Colin Cross7812fd32020-09-25 12:35:10 -07001134func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1135 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001136 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001137 apex {
1138 name: "myapex",
1139 key: "myapex.key",
1140 native_shared_libs: ["mylib", "mylib3"],
1141 min_sdk_version: "29",
1142 }
1143
1144 apex_key {
1145 name: "myapex.key",
1146 public_key: "testkey.avbpubkey",
1147 private_key: "testkey.pem",
1148 }
1149
1150 cc_library {
1151 name: "mylib",
1152 srcs: ["mylib.cpp"],
1153 shared_libs: ["mylib2", "mylib3"],
1154 system_shared_libs: [],
1155 stl: "none",
1156 apex_available: [ "myapex" ],
1157 min_sdk_version: "28",
1158 }
1159
1160 cc_library {
1161 name: "mylib2",
1162 srcs: ["mylib.cpp"],
1163 cflags: ["-include mylib.h"],
1164 system_shared_libs: [],
1165 stl: "none",
1166 stubs: {
1167 versions: ["28", "29", "30", "current"],
1168 },
1169 min_sdk_version: "28",
1170 }
1171
1172 cc_library {
1173 name: "mylib3",
1174 srcs: ["mylib.cpp"],
1175 shared_libs: ["mylib4"],
1176 system_shared_libs: [],
1177 stl: "none",
1178 stubs: {
1179 versions: ["28", "29", "30", "current"],
1180 },
1181 apex_available: [ "myapex" ],
1182 min_sdk_version: "28",
1183 }
1184
1185 cc_library {
1186 name: "mylib4",
1187 srcs: ["mylib.cpp"],
1188 system_shared_libs: [],
1189 stl: "none",
1190 apex_available: [ "myapex" ],
1191 min_sdk_version: "28",
1192 }
1193 `)
1194
Jooyung Hana0503a52023-08-23 13:12:50 +09001195 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Colin Cross7812fd32020-09-25 12:35:10 -07001196 copyCmds := apexRule.Args["copy_commands"]
1197
1198 // Ensure that direct non-stubs dep is always included
1199 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1200
1201 // Ensure that indirect stubs dep is not included
1202 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1203
1204 // Ensure that direct stubs dep is included
1205 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1206
1207 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1208
Jiyong Park55549df2021-02-26 23:57:23 +09001209 // Ensure that mylib is linking with the latest version of stub for mylib2
1210 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001211 // ... and not linking to the non-stub (impl) variant of mylib2
1212 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1213
1214 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1215 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1216 // .. and not linking to the stubs variant of mylib3
1217 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1218
1219 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001220 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001221 ensureNotContains(t, mylib2Cflags, "-include ")
1222
Jiyong Park85cc35a2022-07-17 11:30:47 +09001223 // Ensure that genstub is invoked with --systemapi
1224 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001225
Jooyung Hana0503a52023-08-23 13:12:50 +09001226 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Colin Cross7812fd32020-09-25 12:35:10 -07001227 "lib64/mylib.so",
1228 "lib64/mylib3.so",
1229 "lib64/mylib4.so",
1230 })
1231}
1232
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001233func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1234 t.Parallel()
1235 // myapex (Z)
1236 // mylib -----------------.
1237 // |
1238 // otherapex (29) |
1239 // libstub's versions: 29 Z current
1240 // |
1241 // <platform> |
1242 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001243 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001244 apex {
1245 name: "myapex",
1246 key: "myapex.key",
1247 native_shared_libs: ["mylib"],
1248 min_sdk_version: "Z", // non-final
1249 }
1250
1251 cc_library {
1252 name: "mylib",
1253 srcs: ["mylib.cpp"],
1254 shared_libs: ["libstub"],
1255 apex_available: ["myapex"],
1256 min_sdk_version: "Z",
1257 }
1258
1259 apex_key {
1260 name: "myapex.key",
1261 public_key: "testkey.avbpubkey",
1262 private_key: "testkey.pem",
1263 }
1264
1265 apex {
1266 name: "otherapex",
1267 key: "myapex.key",
1268 native_shared_libs: ["libstub"],
1269 min_sdk_version: "29",
1270 }
1271
1272 cc_library {
1273 name: "libstub",
1274 srcs: ["mylib.cpp"],
1275 stubs: {
1276 versions: ["29", "Z", "current"],
1277 },
1278 apex_available: ["otherapex"],
1279 min_sdk_version: "29",
1280 }
1281
1282 // platform module depending on libstub from otherapex should use the latest stub("current")
1283 cc_library {
1284 name: "libplatform",
1285 srcs: ["mylib.cpp"],
1286 shared_libs: ["libstub"],
1287 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001288 `,
1289 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1290 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1291 variables.Platform_sdk_final = proptools.BoolPtr(false)
1292 variables.Platform_version_active_codenames = []string{"Z"}
1293 }),
1294 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001295
Jiyong Park55549df2021-02-26 23:57:23 +09001296 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001297 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001298 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001299 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001300 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001301
1302 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1303 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1304 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1305 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1306 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1307}
1308
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001309func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001310 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001311 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001312 name: "myapex2",
1313 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001314 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001315 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001316 }
1317
1318 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001319 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001320 public_key: "testkey.avbpubkey",
1321 private_key: "testkey.pem",
1322 }
1323
1324 cc_library {
1325 name: "mylib",
1326 srcs: ["mylib.cpp"],
1327 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001328 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001329 system_shared_libs: [],
1330 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001331 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001332 }
1333
1334 cc_library {
1335 name: "libfoo",
1336 srcs: ["mylib.cpp"],
1337 shared_libs: ["libbar"],
1338 system_shared_libs: [],
1339 stl: "none",
1340 stubs: {
1341 versions: ["10", "20", "30"],
1342 },
1343 }
1344
1345 cc_library {
1346 name: "libbar",
1347 srcs: ["mylib.cpp"],
1348 system_shared_libs: [],
1349 stl: "none",
1350 }
1351
Jiyong Park678c8812020-02-07 17:25:49 +09001352 cc_library_static {
1353 name: "libbaz",
1354 srcs: ["mylib.cpp"],
1355 system_shared_libs: [],
1356 stl: "none",
1357 apex_available: [ "myapex2" ],
1358 }
1359
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001360 `)
1361
Jooyung Hana0503a52023-08-23 13:12:50 +09001362 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001363 copyCmds := apexRule.Args["copy_commands"]
1364
1365 // Ensure that direct non-stubs dep is always included
1366 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1367
1368 // Ensure that indirect stubs dep is not included
1369 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1370
1371 // Ensure that dependency of stubs is not included
1372 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1373
Colin Crossaede88c2020-08-11 12:17:01 -07001374 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001375
1376 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001377 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001378 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001379 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001380
Jiyong Park3ff16992019-12-27 14:11:47 +09001381 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001382
1383 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1384 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001385
Colin Crossf61d03d2023-11-02 16:56:39 -07001386 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1387 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001388 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001389
Colin Crossf61d03d2023-11-02 16:56:39 -07001390 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1391 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001392 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001393}
1394
Jooyung Hand3639552019-08-09 12:57:43 +09001395func TestApexWithRuntimeLibsDependency(t *testing.T) {
1396 /*
1397 myapex
1398 |
1399 v (runtime_libs)
1400 mylib ------+------> libfoo [provides stub]
1401 |
1402 `------> libbar
1403 */
Colin Cross1c460562021-02-16 17:55:47 -08001404 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001405 apex {
1406 name: "myapex",
1407 key: "myapex.key",
1408 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001409 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001410 }
1411
1412 apex_key {
1413 name: "myapex.key",
1414 public_key: "testkey.avbpubkey",
1415 private_key: "testkey.pem",
1416 }
1417
1418 cc_library {
1419 name: "mylib",
1420 srcs: ["mylib.cpp"],
Liz Kammer5f108fa2023-05-11 14:33:17 -04001421 static_libs: ["libstatic"],
1422 shared_libs: ["libshared"],
Jooyung Hand3639552019-08-09 12:57:43 +09001423 runtime_libs: ["libfoo", "libbar"],
1424 system_shared_libs: [],
1425 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001426 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001427 }
1428
1429 cc_library {
1430 name: "libfoo",
1431 srcs: ["mylib.cpp"],
1432 system_shared_libs: [],
1433 stl: "none",
1434 stubs: {
1435 versions: ["10", "20", "30"],
1436 },
1437 }
1438
1439 cc_library {
1440 name: "libbar",
1441 srcs: ["mylib.cpp"],
1442 system_shared_libs: [],
1443 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001444 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001445 }
1446
Liz Kammer5f108fa2023-05-11 14:33:17 -04001447 cc_library {
1448 name: "libstatic",
1449 srcs: ["mylib.cpp"],
1450 system_shared_libs: [],
1451 stl: "none",
1452 apex_available: [ "myapex" ],
1453 runtime_libs: ["libstatic_to_runtime"],
1454 }
1455
1456 cc_library {
1457 name: "libshared",
1458 srcs: ["mylib.cpp"],
1459 system_shared_libs: [],
1460 stl: "none",
1461 apex_available: [ "myapex" ],
1462 runtime_libs: ["libshared_to_runtime"],
1463 }
1464
1465 cc_library {
1466 name: "libstatic_to_runtime",
1467 srcs: ["mylib.cpp"],
1468 system_shared_libs: [],
1469 stl: "none",
1470 apex_available: [ "myapex" ],
1471 }
1472
1473 cc_library {
1474 name: "libshared_to_runtime",
1475 srcs: ["mylib.cpp"],
1476 system_shared_libs: [],
1477 stl: "none",
1478 apex_available: [ "myapex" ],
1479 }
Jooyung Hand3639552019-08-09 12:57:43 +09001480 `)
1481
Jooyung Hana0503a52023-08-23 13:12:50 +09001482 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001483 copyCmds := apexRule.Args["copy_commands"]
1484
1485 // Ensure that direct non-stubs dep is always included
1486 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1487
1488 // Ensure that indirect stubs dep is not included
1489 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1490
1491 // Ensure that runtime_libs dep in included
1492 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
Liz Kammer5f108fa2023-05-11 14:33:17 -04001493 ensureContains(t, copyCmds, "image.apex/lib64/libshared.so")
1494 ensureContains(t, copyCmds, "image.apex/lib64/libshared_to_runtime.so")
1495
1496 ensureNotContains(t, copyCmds, "image.apex/lib64/libstatic_to_runtime.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001497
Jooyung Hana0503a52023-08-23 13:12:50 +09001498 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001499 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1500 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001501}
1502
Paul Duffina02cae32021-03-09 01:44:06 +00001503var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1504 cc.PrepareForTestWithCcBuildComponents,
1505 PrepareForTestWithApexBuildComponents,
1506 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001507 apex {
1508 name: "com.android.runtime",
1509 key: "com.android.runtime.key",
1510 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001511 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001512 }
1513
1514 apex_key {
1515 name: "com.android.runtime.key",
1516 public_key: "testkey.avbpubkey",
1517 private_key: "testkey.pem",
1518 }
Paul Duffina02cae32021-03-09 01:44:06 +00001519 `),
1520 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1521)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001522
Paul Duffina02cae32021-03-09 01:44:06 +00001523func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001524 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001525 cc_library {
1526 name: "libc",
1527 no_libcrt: true,
1528 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001529 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001530 stl: "none",
1531 system_shared_libs: [],
1532 stubs: { versions: ["1"] },
1533 apex_available: ["com.android.runtime"],
1534
1535 sanitize: {
1536 hwaddress: true,
1537 }
1538 }
1539
1540 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001541 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001542 no_libcrt: true,
1543 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001544 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001545 stl: "none",
1546 system_shared_libs: [],
1547 srcs: [""],
1548 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001549 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001550
1551 sanitize: {
1552 never: true,
1553 },
Spandan Das4de7b492023-05-05 21:13:01 +00001554 apex_available: [
1555 "//apex_available:anyapex",
1556 "//apex_available:platform",
1557 ],
Paul Duffina02cae32021-03-09 01:44:06 +00001558 } `)
1559 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001560
Jooyung Hana0503a52023-08-23 13:12:50 +09001561 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001562 "lib64/bionic/libc.so",
1563 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1564 })
1565
Colin Cross4c4c1be2022-02-10 11:41:18 -08001566 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001567
1568 installed := hwasan.Description("install libclang_rt.hwasan")
1569 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1570
1571 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1572 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1573 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1574}
1575
1576func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001577 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001578 prepareForTestOfRuntimeApexWithHwasan,
1579 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1580 variables.SanitizeDevice = []string{"hwaddress"}
1581 }),
1582 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001583 cc_library {
1584 name: "libc",
1585 no_libcrt: true,
1586 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001587 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001588 stl: "none",
1589 system_shared_libs: [],
1590 stubs: { versions: ["1"] },
1591 apex_available: ["com.android.runtime"],
1592 }
1593
1594 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001595 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001596 no_libcrt: true,
1597 nocrt: true,
Kalesh Singhf4ffe0a2024-01-29 13:01:51 -08001598 no_crt_pad_segment: true,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001599 stl: "none",
1600 system_shared_libs: [],
1601 srcs: [""],
1602 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001603 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001604
1605 sanitize: {
1606 never: true,
1607 },
Spandan Das4de7b492023-05-05 21:13:01 +00001608 apex_available: [
1609 "//apex_available:anyapex",
1610 "//apex_available:platform",
1611 ],
Jooyung Han8ce8db92020-05-15 19:05:05 +09001612 }
Paul Duffina02cae32021-03-09 01:44:06 +00001613 `)
1614 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001615
Jooyung Hana0503a52023-08-23 13:12:50 +09001616 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001617 "lib64/bionic/libc.so",
1618 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1619 })
1620
Colin Cross4c4c1be2022-02-10 11:41:18 -08001621 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001622
1623 installed := hwasan.Description("install libclang_rt.hwasan")
1624 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1625
1626 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1627 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1628 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1629}
1630
Jooyung Han61b66e92020-03-21 14:21:46 +00001631func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1632 testcases := []struct {
1633 name string
1634 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001635 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001636 shouldLink string
1637 shouldNotLink []string
1638 }{
1639 {
Jiyong Park55549df2021-02-26 23:57:23 +09001640 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001641 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001642 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001643 shouldLink: "current",
1644 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001645 },
1646 {
Jiyong Park55549df2021-02-26 23:57:23 +09001647 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001648 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001649 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001650 shouldLink: "current",
1651 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001652 },
1653 }
1654 for _, tc := range testcases {
1655 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001656 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001657 apex {
1658 name: "myapex",
1659 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001660 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001661 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001662 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001663 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001664
Jooyung Han61b66e92020-03-21 14:21:46 +00001665 apex_key {
1666 name: "myapex.key",
1667 public_key: "testkey.avbpubkey",
1668 private_key: "testkey.pem",
1669 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001670
Jooyung Han61b66e92020-03-21 14:21:46 +00001671 cc_library {
1672 name: "mylib",
1673 srcs: ["mylib.cpp"],
1674 vendor_available: true,
1675 shared_libs: ["libbar"],
1676 system_shared_libs: [],
1677 stl: "none",
1678 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001679 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001680 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001681
Jooyung Han61b66e92020-03-21 14:21:46 +00001682 cc_library {
1683 name: "libbar",
1684 srcs: ["mylib.cpp"],
1685 system_shared_libs: [],
1686 stl: "none",
1687 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001688 llndk: {
1689 symbol_file: "libbar.map.txt",
1690 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001691 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001692 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001693 withUnbundledBuild,
1694 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001695
Jooyung Han61b66e92020-03-21 14:21:46 +00001696 // Ensure that LLNDK dep is not included
Jooyung Hana0503a52023-08-23 13:12:50 +09001697 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00001698 "lib64/mylib.so",
1699 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001700
Jooyung Han61b66e92020-03-21 14:21:46 +00001701 // Ensure that LLNDK dep is required
Jooyung Hana0503a52023-08-23 13:12:50 +09001702 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han61b66e92020-03-21 14:21:46 +00001703 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1704 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001705
Steven Moreland2c4000c2021-04-27 02:08:49 +00001706 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1707 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001708 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001709 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001710 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001711
Steven Moreland2c4000c2021-04-27 02:08:49 +00001712 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001713 ver := tc.shouldLink
1714 if tc.shouldLink == "current" {
1715 ver = strconv.Itoa(android.FutureApiLevelInt)
1716 }
1717 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001718 })
1719 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001720}
1721
Jiyong Park25fc6a92018-11-18 18:02:45 +09001722func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001723 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001724 apex {
1725 name: "myapex",
1726 key: "myapex.key",
1727 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001728 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001729 }
1730
1731 apex_key {
1732 name: "myapex.key",
1733 public_key: "testkey.avbpubkey",
1734 private_key: "testkey.pem",
1735 }
1736
1737 cc_library {
1738 name: "mylib",
1739 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001740 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001741 shared_libs: ["libdl#27"],
1742 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001743 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001744 }
1745
1746 cc_library_shared {
1747 name: "mylib_shared",
1748 srcs: ["mylib.cpp"],
1749 shared_libs: ["libdl#27"],
1750 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001751 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001752 }
1753
1754 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001755 name: "libBootstrap",
1756 srcs: ["mylib.cpp"],
1757 stl: "none",
1758 bootstrap: true,
1759 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001760 `)
1761
Jooyung Hana0503a52023-08-23 13:12:50 +09001762 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001763 copyCmds := apexRule.Args["copy_commands"]
1764
1765 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001766 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001767 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1768 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001769
1770 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001771 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001772
Colin Crossaede88c2020-08-11 12:17:01 -07001773 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1774 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1775 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001776
1777 // For dependency to libc
1778 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001779 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001780 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001781 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001782 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001783 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1784 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001785
1786 // For dependency to libm
1787 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001788 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001789 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001790 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001791 // ... and is not compiling with the stub
1792 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1793 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1794
1795 // For dependency to libdl
1796 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001797 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001798 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001799 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1800 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001801 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001802 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001803 // ... Cflags from stub is correctly exported to mylib
1804 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1805 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001806
1807 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001808 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1809 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1810 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1811 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001812}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001813
Jooyung Han749dc692020-04-15 11:03:39 +09001814func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001815 // there are three links between liba --> libz.
1816 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001817 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001818 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001819 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001820 apex {
1821 name: "myapex",
1822 key: "myapex.key",
1823 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001824 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001825 }
1826
1827 apex {
1828 name: "otherapex",
1829 key: "myapex.key",
1830 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001831 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001832 }
1833
1834 apex_key {
1835 name: "myapex.key",
1836 public_key: "testkey.avbpubkey",
1837 private_key: "testkey.pem",
1838 }
1839
1840 cc_library {
1841 name: "libx",
1842 shared_libs: ["liba"],
1843 system_shared_libs: [],
1844 stl: "none",
1845 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001846 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001847 }
1848
1849 cc_library {
1850 name: "liby",
1851 shared_libs: ["liba"],
1852 system_shared_libs: [],
1853 stl: "none",
1854 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001855 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001856 }
1857
1858 cc_library {
1859 name: "liba",
1860 shared_libs: ["libz"],
1861 system_shared_libs: [],
1862 stl: "none",
1863 apex_available: [
1864 "//apex_available:anyapex",
1865 "//apex_available:platform",
1866 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001867 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001868 }
1869
1870 cc_library {
1871 name: "libz",
1872 system_shared_libs: [],
1873 stl: "none",
1874 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001875 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001876 },
1877 }
Jooyung Han749dc692020-04-15 11:03:39 +09001878 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001879
1880 expectLink := func(from, from_variant, to, to_variant string) {
1881 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1882 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1883 }
1884 expectNoLink := func(from, from_variant, to, to_variant string) {
1885 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1886 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1887 }
1888 // platform liba is linked to non-stub version
1889 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001890 // liba in myapex is linked to current
1891 expectLink("liba", "shared_apex29", "libz", "shared_current")
1892 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001893 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001894 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001895 // liba in otherapex is linked to current
1896 expectLink("liba", "shared_apex30", "libz", "shared_current")
1897 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001898 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1899 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001900}
1901
Jooyung Hanaed150d2020-04-02 01:41:41 +09001902func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001903 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001904 apex {
1905 name: "myapex",
1906 key: "myapex.key",
1907 native_shared_libs: ["libx"],
1908 min_sdk_version: "R",
1909 }
1910
1911 apex_key {
1912 name: "myapex.key",
1913 public_key: "testkey.avbpubkey",
1914 private_key: "testkey.pem",
1915 }
1916
1917 cc_library {
1918 name: "libx",
1919 shared_libs: ["libz"],
1920 system_shared_libs: [],
1921 stl: "none",
1922 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001923 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001924 }
1925
1926 cc_library {
1927 name: "libz",
1928 system_shared_libs: [],
1929 stl: "none",
1930 stubs: {
1931 versions: ["29", "R"],
1932 },
1933 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001934 `,
1935 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1936 variables.Platform_version_active_codenames = []string{"R"}
1937 }),
1938 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001939
1940 expectLink := func(from, from_variant, to, to_variant string) {
1941 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1942 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1943 }
1944 expectNoLink := func(from, from_variant, to, to_variant string) {
1945 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1946 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1947 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001948 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1949 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001950 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1951 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001952}
1953
Jooyung Han4c4da062021-06-23 10:23:16 +09001954func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1955 testApex(t, `
1956 apex {
1957 name: "myapex",
1958 key: "myapex.key",
1959 java_libs: ["libx"],
1960 min_sdk_version: "S",
1961 }
1962
1963 apex_key {
1964 name: "myapex.key",
1965 public_key: "testkey.avbpubkey",
1966 private_key: "testkey.pem",
1967 }
1968
1969 java_library {
1970 name: "libx",
1971 srcs: ["a.java"],
1972 apex_available: [ "myapex" ],
1973 sdk_version: "current",
1974 min_sdk_version: "S", // should be okay
1975 }
1976 `,
1977 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1978 variables.Platform_version_active_codenames = []string{"S"}
1979 variables.Platform_sdk_codename = proptools.StringPtr("S")
1980 }),
1981 )
1982}
1983
Jooyung Han749dc692020-04-15 11:03:39 +09001984func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001985 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001986 apex {
1987 name: "myapex",
1988 key: "myapex.key",
1989 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001990 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001991 }
1992
1993 apex_key {
1994 name: "myapex.key",
1995 public_key: "testkey.avbpubkey",
1996 private_key: "testkey.pem",
1997 }
1998
1999 cc_library {
2000 name: "libx",
2001 shared_libs: ["libz"],
2002 system_shared_libs: [],
2003 stl: "none",
2004 apex_available: [ "myapex" ],
2005 }
2006
2007 cc_library {
2008 name: "libz",
2009 system_shared_libs: [],
2010 stl: "none",
2011 stubs: {
2012 versions: ["1", "2"],
2013 },
2014 }
2015 `)
2016
2017 expectLink := func(from, from_variant, to, to_variant string) {
2018 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2019 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2020 }
2021 expectNoLink := func(from, from_variant, to, to_variant string) {
2022 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2023 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2024 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002025 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002026 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002027 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07002028 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09002029}
2030
Jooyung Handfc864c2023-03-20 18:19:07 +09002031func TestApexMinSdkVersion_InVendorApex(t *testing.T) {
Jiyong Park5df7bd32021-08-25 16:18:46 +09002032 ctx := testApex(t, `
2033 apex {
2034 name: "myapex",
2035 key: "myapex.key",
2036 native_shared_libs: ["mylib"],
Jooyung Handfc864c2023-03-20 18:19:07 +09002037 updatable: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002038 vendor: true,
2039 min_sdk_version: "29",
2040 }
2041
2042 apex_key {
2043 name: "myapex.key",
2044 public_key: "testkey.avbpubkey",
2045 private_key: "testkey.pem",
2046 }
2047
2048 cc_library {
2049 name: "mylib",
Jooyung Handfc864c2023-03-20 18:19:07 +09002050 srcs: ["mylib.cpp"],
Jiyong Park5df7bd32021-08-25 16:18:46 +09002051 vendor_available: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09002052 min_sdk_version: "29",
Jooyung Handfc864c2023-03-20 18:19:07 +09002053 shared_libs: ["libbar"],
2054 }
2055
2056 cc_library {
2057 name: "libbar",
2058 stubs: { versions: ["29", "30"] },
2059 llndk: { symbol_file: "libbar.map.txt" },
Jiyong Park5df7bd32021-08-25 16:18:46 +09002060 }
2061 `)
2062
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002063 vendorVariant := "android_vendor_arm64_armv8-a"
Jiyong Park5df7bd32021-08-25 16:18:46 +09002064
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09002065 mylib := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29")
Jooyung Handfc864c2023-03-20 18:19:07 +09002066
2067 // Ensure that mylib links with "current" LLNDK
2068 libFlags := names(mylib.Rule("ld").Args["libFlags"])
Jooyung Han5e8994e2024-03-12 14:12:12 +09002069 ensureListContains(t, libFlags, "out/soong/.intermediates/libbar/"+vendorVariant+"_shared/libbar.so")
Jooyung Handfc864c2023-03-20 18:19:07 +09002070
2071 // Ensure that mylib is targeting 29
2072 ccRule := ctx.ModuleForTests("mylib", vendorVariant+"_static_apex29").Output("obj/mylib.o")
2073 ensureContains(t, ccRule.Args["cFlags"], "-target aarch64-linux-android29")
2074
2075 // Ensure that the correct variant of crtbegin_so is used.
2076 crtBegin := mylib.Rule("ld").Args["crtBegin"]
2077 ensureContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
Jiyong Park5df7bd32021-08-25 16:18:46 +09002078
2079 // Ensure that the crtbegin_so used by the APEX is targeting 29
2080 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
2081 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
2082}
2083
Jooyung Han4495f842023-04-25 16:39:59 +09002084func TestTrackAllowedDeps(t *testing.T) {
2085 ctx := testApex(t, `
2086 apex {
2087 name: "myapex",
2088 key: "myapex.key",
2089 updatable: true,
2090 native_shared_libs: [
2091 "mylib",
2092 "yourlib",
2093 ],
2094 min_sdk_version: "29",
2095 }
2096
2097 apex {
2098 name: "myapex2",
2099 key: "myapex.key",
2100 updatable: false,
2101 native_shared_libs: ["yourlib"],
2102 }
2103
2104 apex_key {
2105 name: "myapex.key",
2106 public_key: "testkey.avbpubkey",
2107 private_key: "testkey.pem",
2108 }
2109
2110 cc_library {
2111 name: "mylib",
2112 srcs: ["mylib.cpp"],
2113 shared_libs: ["libbar"],
2114 min_sdk_version: "29",
2115 apex_available: ["myapex"],
2116 }
2117
2118 cc_library {
2119 name: "libbar",
2120 stubs: { versions: ["29", "30"] },
2121 }
2122
2123 cc_library {
2124 name: "yourlib",
2125 srcs: ["mylib.cpp"],
2126 min_sdk_version: "29",
2127 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2128 }
2129 `, withFiles(android.MockFS{
2130 "packages/modules/common/build/allowed_deps.txt": nil,
2131 }))
2132
2133 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2134 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2135 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002136 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002137 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002138 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002139
Jooyung Hana0503a52023-08-23 13:12:50 +09002140 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07002141 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2142 myapex.Output("depsinfo/flatlist.txt")), "\n")
Jooyung Han4495f842023-04-25 16:39:59 +09002143 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2144 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2145 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2146 flatlist, "mylib:(minSdkVersion:29)")
2147 android.AssertStringListContains(t, "track platform-available lib",
2148 flatlist, "yourlib(minSdkVersion:29)")
2149}
2150
2151func TestTrackAllowedDeps_SkipWithoutAllowedDepsTxt(t *testing.T) {
2152 ctx := testApex(t, `
2153 apex {
2154 name: "myapex",
2155 key: "myapex.key",
2156 updatable: true,
2157 min_sdk_version: "29",
2158 }
2159
2160 apex_key {
2161 name: "myapex.key",
2162 public_key: "testkey.avbpubkey",
2163 private_key: "testkey.pem",
2164 }
2165 `)
2166 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2167 if nil != depsinfo.MaybeRule("generateApexDepsInfoFilesRule").Output {
2168 t.Error("apex_depsinfo_singleton shouldn't run when allowed_deps.txt doesn't exist")
2169 }
2170}
2171
Jooyung Han03b51852020-02-26 22:45:42 +09002172func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002173 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002174 apex {
2175 name: "myapex",
2176 key: "myapex.key",
2177 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002178 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002179 }
2180
2181 apex_key {
2182 name: "myapex.key",
2183 public_key: "testkey.avbpubkey",
2184 private_key: "testkey.pem",
2185 }
2186
2187 cc_library {
2188 name: "libx",
2189 system_shared_libs: [],
2190 stl: "none",
2191 apex_available: [ "myapex" ],
2192 stubs: {
2193 versions: ["1", "2"],
2194 },
2195 }
2196
2197 cc_library {
2198 name: "libz",
2199 shared_libs: ["libx"],
2200 system_shared_libs: [],
2201 stl: "none",
2202 }
2203 `)
2204
2205 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002206 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002207 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2208 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2209 }
2210 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002211 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002212 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2213 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2214 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002215 expectLink("libz", "shared", "libx", "shared_current")
2216 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002217 expectNoLink("libz", "shared", "libz", "shared_1")
2218 expectNoLink("libz", "shared", "libz", "shared")
2219}
2220
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002221var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2222 func(variables android.FixtureProductVariables) {
2223 variables.SanitizeDevice = []string{"hwaddress"}
2224 },
2225)
2226
Jooyung Han75568392020-03-20 04:29:24 +09002227func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002228 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002229 apex {
2230 name: "myapex",
2231 key: "myapex.key",
2232 native_shared_libs: ["libx"],
2233 min_sdk_version: "29",
2234 }
2235
2236 apex_key {
2237 name: "myapex.key",
2238 public_key: "testkey.avbpubkey",
2239 private_key: "testkey.pem",
2240 }
2241
2242 cc_library {
2243 name: "libx",
2244 shared_libs: ["libbar"],
2245 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002246 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002247 }
2248
2249 cc_library {
2250 name: "libbar",
2251 stubs: {
2252 versions: ["29", "30"],
2253 },
2254 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002255 `,
2256 prepareForTestWithSantitizeHwaddress,
2257 )
Jooyung Han03b51852020-02-26 22:45:42 +09002258 expectLink := func(from, from_variant, to, to_variant string) {
2259 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2260 libFlags := ld.Args["libFlags"]
2261 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2262 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002263 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002264}
2265
Jooyung Han75568392020-03-20 04:29:24 +09002266func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002267 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002268 apex {
2269 name: "myapex",
2270 key: "myapex.key",
2271 native_shared_libs: ["libx"],
2272 min_sdk_version: "29",
2273 }
2274
2275 apex_key {
2276 name: "myapex.key",
2277 public_key: "testkey.avbpubkey",
2278 private_key: "testkey.pem",
2279 }
2280
2281 cc_library {
2282 name: "libx",
2283 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002284 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002285 }
Jooyung Han75568392020-03-20 04:29:24 +09002286 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002287
2288 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002289 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002290 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002291 // note that platform variant is not.
2292 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002293 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002294}
2295
Jooyung Han749dc692020-04-15 11:03:39 +09002296func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2297 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002298 apex {
2299 name: "myapex",
2300 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002301 native_shared_libs: ["mylib"],
2302 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002303 }
2304
2305 apex_key {
2306 name: "myapex.key",
2307 public_key: "testkey.avbpubkey",
2308 private_key: "testkey.pem",
2309 }
Jooyung Han749dc692020-04-15 11:03:39 +09002310
2311 cc_library {
2312 name: "mylib",
2313 srcs: ["mylib.cpp"],
2314 system_shared_libs: [],
2315 stl: "none",
2316 apex_available: [
2317 "myapex",
2318 ],
2319 min_sdk_version: "30",
2320 }
2321 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002322
2323 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2324 apex {
2325 name: "myapex",
2326 key: "myapex.key",
2327 native_shared_libs: ["libfoo.ffi"],
2328 min_sdk_version: "29",
2329 }
2330
2331 apex_key {
2332 name: "myapex.key",
2333 public_key: "testkey.avbpubkey",
2334 private_key: "testkey.pem",
2335 }
2336
2337 rust_ffi_shared {
2338 name: "libfoo.ffi",
2339 srcs: ["foo.rs"],
2340 crate_name: "foo",
2341 apex_available: [
2342 "myapex",
2343 ],
2344 min_sdk_version: "30",
2345 }
2346 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002347
2348 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2349 apex {
2350 name: "myapex",
2351 key: "myapex.key",
2352 java_libs: ["libfoo"],
2353 min_sdk_version: "29",
2354 }
2355
2356 apex_key {
2357 name: "myapex.key",
2358 public_key: "testkey.avbpubkey",
2359 private_key: "testkey.pem",
2360 }
2361
2362 java_import {
2363 name: "libfoo",
2364 jars: ["libfoo.jar"],
2365 apex_available: [
2366 "myapex",
2367 ],
2368 min_sdk_version: "30",
2369 }
2370 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002371
2372 // Skip check for modules compiling against core API surface
2373 testApex(t, `
2374 apex {
2375 name: "myapex",
2376 key: "myapex.key",
2377 java_libs: ["libfoo"],
2378 min_sdk_version: "29",
2379 }
2380
2381 apex_key {
2382 name: "myapex.key",
2383 public_key: "testkey.avbpubkey",
2384 private_key: "testkey.pem",
2385 }
2386
2387 java_library {
2388 name: "libfoo",
2389 srcs: ["Foo.java"],
2390 apex_available: [
2391 "myapex",
2392 ],
2393 // Compile against core API surface
2394 sdk_version: "core_current",
2395 min_sdk_version: "30",
2396 }
2397 `)
2398
Jooyung Han749dc692020-04-15 11:03:39 +09002399}
2400
2401func TestApexMinSdkVersion_Okay(t *testing.T) {
2402 testApex(t, `
2403 apex {
2404 name: "myapex",
2405 key: "myapex.key",
2406 native_shared_libs: ["libfoo"],
2407 java_libs: ["libbar"],
2408 min_sdk_version: "29",
2409 }
2410
2411 apex_key {
2412 name: "myapex.key",
2413 public_key: "testkey.avbpubkey",
2414 private_key: "testkey.pem",
2415 }
2416
2417 cc_library {
2418 name: "libfoo",
2419 srcs: ["mylib.cpp"],
2420 shared_libs: ["libfoo_dep"],
2421 apex_available: ["myapex"],
2422 min_sdk_version: "29",
2423 }
2424
2425 cc_library {
2426 name: "libfoo_dep",
2427 srcs: ["mylib.cpp"],
2428 apex_available: ["myapex"],
2429 min_sdk_version: "29",
2430 }
2431
2432 java_library {
2433 name: "libbar",
2434 sdk_version: "current",
2435 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002436 static_libs: [
2437 "libbar_dep",
2438 "libbar_import_dep",
2439 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002440 apex_available: ["myapex"],
2441 min_sdk_version: "29",
2442 }
2443
2444 java_library {
2445 name: "libbar_dep",
2446 sdk_version: "current",
2447 srcs: ["a.java"],
2448 apex_available: ["myapex"],
2449 min_sdk_version: "29",
2450 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002451
2452 java_import {
2453 name: "libbar_import_dep",
2454 jars: ["libbar.jar"],
2455 apex_available: ["myapex"],
2456 min_sdk_version: "29",
2457 }
Jooyung Han03b51852020-02-26 22:45:42 +09002458 `)
2459}
2460
Colin Cross8ca61c12022-10-06 21:00:14 -07002461func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2462 // Tests that an apex dependency with min_sdk_version higher than the
2463 // min_sdk_version of the apex is allowed as long as the dependency's
2464 // min_sdk_version is less than or equal to the api level that the
2465 // architecture was introduced in. In this case, arm64 didn't exist
2466 // until api level 21, so the arm64 code will never need to run on
2467 // an api level 20 device, even if other architectures of the apex
2468 // will.
2469 testApex(t, `
2470 apex {
2471 name: "myapex",
2472 key: "myapex.key",
2473 native_shared_libs: ["libfoo"],
2474 min_sdk_version: "20",
2475 }
2476
2477 apex_key {
2478 name: "myapex.key",
2479 public_key: "testkey.avbpubkey",
2480 private_key: "testkey.pem",
2481 }
2482
2483 cc_library {
2484 name: "libfoo",
2485 srcs: ["mylib.cpp"],
2486 apex_available: ["myapex"],
2487 min_sdk_version: "21",
2488 stl: "none",
2489 }
2490 `)
2491}
2492
Artur Satayev8cf899a2020-04-15 17:29:42 +01002493func TestJavaStableSdkVersion(t *testing.T) {
2494 testCases := []struct {
2495 name string
2496 expectedError string
2497 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002498 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002499 }{
2500 {
2501 name: "Non-updatable apex with non-stable dep",
2502 bp: `
2503 apex {
2504 name: "myapex",
2505 java_libs: ["myjar"],
2506 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002507 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002508 }
2509 apex_key {
2510 name: "myapex.key",
2511 public_key: "testkey.avbpubkey",
2512 private_key: "testkey.pem",
2513 }
2514 java_library {
2515 name: "myjar",
2516 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002517 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002518 apex_available: ["myapex"],
2519 }
2520 `,
2521 },
2522 {
2523 name: "Updatable apex with stable dep",
2524 bp: `
2525 apex {
2526 name: "myapex",
2527 java_libs: ["myjar"],
2528 key: "myapex.key",
2529 updatable: true,
2530 min_sdk_version: "29",
2531 }
2532 apex_key {
2533 name: "myapex.key",
2534 public_key: "testkey.avbpubkey",
2535 private_key: "testkey.pem",
2536 }
2537 java_library {
2538 name: "myjar",
2539 srcs: ["foo/bar/MyClass.java"],
2540 sdk_version: "current",
2541 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002542 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002543 }
2544 `,
2545 },
2546 {
2547 name: "Updatable apex with non-stable dep",
2548 expectedError: "cannot depend on \"myjar\"",
2549 bp: `
2550 apex {
2551 name: "myapex",
2552 java_libs: ["myjar"],
2553 key: "myapex.key",
2554 updatable: true,
2555 }
2556 apex_key {
2557 name: "myapex.key",
2558 public_key: "testkey.avbpubkey",
2559 private_key: "testkey.pem",
2560 }
2561 java_library {
2562 name: "myjar",
2563 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002564 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002565 apex_available: ["myapex"],
2566 }
2567 `,
2568 },
2569 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002570 name: "Updatable apex with non-stable legacy core platform dep",
2571 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2572 bp: `
2573 apex {
2574 name: "myapex",
2575 java_libs: ["myjar-uses-legacy"],
2576 key: "myapex.key",
2577 updatable: true,
2578 }
2579 apex_key {
2580 name: "myapex.key",
2581 public_key: "testkey.avbpubkey",
2582 private_key: "testkey.pem",
2583 }
2584 java_library {
2585 name: "myjar-uses-legacy",
2586 srcs: ["foo/bar/MyClass.java"],
2587 sdk_version: "core_platform",
2588 apex_available: ["myapex"],
2589 }
2590 `,
2591 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2592 },
2593 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002594 name: "Updatable apex with non-stable transitive dep",
2595 // This is not actually detecting that the transitive dependency is unstable, rather it is
2596 // detecting that the transitive dependency is building against a wider API surface than the
2597 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002598 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002599 bp: `
2600 apex {
2601 name: "myapex",
2602 java_libs: ["myjar"],
2603 key: "myapex.key",
2604 updatable: true,
2605 }
2606 apex_key {
2607 name: "myapex.key",
2608 public_key: "testkey.avbpubkey",
2609 private_key: "testkey.pem",
2610 }
2611 java_library {
2612 name: "myjar",
2613 srcs: ["foo/bar/MyClass.java"],
2614 sdk_version: "current",
2615 apex_available: ["myapex"],
2616 static_libs: ["transitive-jar"],
2617 }
2618 java_library {
2619 name: "transitive-jar",
2620 srcs: ["foo/bar/MyClass.java"],
2621 sdk_version: "core_platform",
2622 apex_available: ["myapex"],
2623 }
2624 `,
2625 },
2626 }
2627
2628 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002629 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2630 continue
2631 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002632 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002633 errorHandler := android.FixtureExpectsNoErrors
2634 if test.expectedError != "" {
2635 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002636 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002637 android.GroupFixturePreparers(
2638 java.PrepareForTestWithJavaDefaultModules,
2639 PrepareForTestWithApexBuildComponents,
2640 prepareForTestWithMyapex,
2641 android.OptionalFixturePreparer(test.preparer),
2642 ).
2643 ExtendWithErrorHandler(errorHandler).
2644 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002645 })
2646 }
2647}
2648
Jooyung Han749dc692020-04-15 11:03:39 +09002649func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2650 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2651 apex {
2652 name: "myapex",
2653 key: "myapex.key",
2654 native_shared_libs: ["mylib"],
2655 min_sdk_version: "29",
2656 }
2657
2658 apex_key {
2659 name: "myapex.key",
2660 public_key: "testkey.avbpubkey",
2661 private_key: "testkey.pem",
2662 }
2663
2664 cc_library {
2665 name: "mylib",
2666 srcs: ["mylib.cpp"],
2667 shared_libs: ["mylib2"],
2668 system_shared_libs: [],
2669 stl: "none",
2670 apex_available: [
2671 "myapex",
2672 ],
2673 min_sdk_version: "29",
2674 }
2675
2676 // indirect part of the apex
2677 cc_library {
2678 name: "mylib2",
2679 srcs: ["mylib.cpp"],
2680 system_shared_libs: [],
2681 stl: "none",
2682 apex_available: [
2683 "myapex",
2684 ],
2685 min_sdk_version: "30",
2686 }
2687 `)
2688}
2689
2690func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2691 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2692 apex {
2693 name: "myapex",
2694 key: "myapex.key",
2695 apps: ["AppFoo"],
2696 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002697 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002698 }
2699
2700 apex_key {
2701 name: "myapex.key",
2702 public_key: "testkey.avbpubkey",
2703 private_key: "testkey.pem",
2704 }
2705
2706 android_app {
2707 name: "AppFoo",
2708 srcs: ["foo/bar/MyClass.java"],
2709 sdk_version: "current",
2710 min_sdk_version: "29",
2711 system_modules: "none",
2712 stl: "none",
2713 static_libs: ["bar"],
2714 apex_available: [ "myapex" ],
2715 }
2716
2717 java_library {
2718 name: "bar",
2719 sdk_version: "current",
2720 srcs: ["a.java"],
2721 apex_available: [ "myapex" ],
2722 }
2723 `)
2724}
2725
2726func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002727 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002728 apex {
2729 name: "myapex",
2730 key: "myapex.key",
2731 native_shared_libs: ["mylib"],
2732 min_sdk_version: "29",
2733 }
2734
2735 apex_key {
2736 name: "myapex.key",
2737 public_key: "testkey.avbpubkey",
2738 private_key: "testkey.pem",
2739 }
2740
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002741 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002742 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2743 cc_library {
2744 name: "mylib",
2745 srcs: ["mylib.cpp"],
2746 shared_libs: ["mylib2"],
2747 system_shared_libs: [],
2748 stl: "none",
2749 apex_available: ["myapex", "otherapex"],
2750 min_sdk_version: "29",
2751 }
2752
2753 cc_library {
2754 name: "mylib2",
2755 srcs: ["mylib.cpp"],
2756 system_shared_libs: [],
2757 stl: "none",
2758 apex_available: ["otherapex"],
2759 stubs: { versions: ["29", "30"] },
2760 min_sdk_version: "30",
2761 }
2762
2763 apex {
2764 name: "otherapex",
2765 key: "myapex.key",
2766 native_shared_libs: ["mylib", "mylib2"],
2767 min_sdk_version: "30",
2768 }
2769 `)
2770 expectLink := func(from, from_variant, to, to_variant string) {
2771 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2772 libFlags := ld.Args["libFlags"]
2773 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2774 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002775 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002776 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002777}
2778
Jooyung Haned124c32021-01-26 11:43:46 +09002779func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002780 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2781 func(variables android.FixtureProductVariables) {
2782 variables.Platform_sdk_codename = proptools.StringPtr("S")
2783 variables.Platform_version_active_codenames = []string{"S"}
2784 },
2785 )
Jooyung Haned124c32021-01-26 11:43:46 +09002786 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2787 apex {
2788 name: "myapex",
2789 key: "myapex.key",
2790 native_shared_libs: ["libfoo"],
2791 min_sdk_version: "S",
2792 }
2793 apex_key {
2794 name: "myapex.key",
2795 public_key: "testkey.avbpubkey",
2796 private_key: "testkey.pem",
2797 }
2798 cc_library {
2799 name: "libfoo",
2800 shared_libs: ["libbar"],
2801 apex_available: ["myapex"],
2802 min_sdk_version: "29",
2803 }
2804 cc_library {
2805 name: "libbar",
2806 apex_available: ["myapex"],
2807 }
2808 `, withSAsActiveCodeNames)
2809}
2810
2811func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002812 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2813 variables.Platform_sdk_codename = proptools.StringPtr("S")
2814 variables.Platform_version_active_codenames = []string{"S", "T"}
2815 })
Colin Cross1c460562021-02-16 17:55:47 -08002816 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002817 apex {
2818 name: "myapex",
2819 key: "myapex.key",
2820 native_shared_libs: ["libfoo"],
2821 min_sdk_version: "S",
2822 }
2823 apex_key {
2824 name: "myapex.key",
2825 public_key: "testkey.avbpubkey",
2826 private_key: "testkey.pem",
2827 }
2828 cc_library {
2829 name: "libfoo",
2830 shared_libs: ["libbar"],
2831 apex_available: ["myapex"],
2832 min_sdk_version: "S",
2833 }
2834 cc_library {
2835 name: "libbar",
2836 stubs: {
2837 symbol_file: "libbar.map.txt",
2838 versions: ["30", "S", "T"],
2839 },
2840 }
2841 `, withSAsActiveCodeNames)
2842
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002843 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002844 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2845 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002846 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002847}
2848
Jiyong Park7c2ee712018-12-07 00:42:25 +09002849func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002850 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002851 apex {
2852 name: "myapex",
2853 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002854 native_shared_libs: ["mylib"],
Jooyung Han4ed512b2023-08-11 16:30:04 +09002855 binaries: ["mybin", "mybin.rust"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002856 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002857 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002858 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002859 }
2860
2861 apex_key {
2862 name: "myapex.key",
2863 public_key: "testkey.avbpubkey",
2864 private_key: "testkey.pem",
2865 }
2866
2867 prebuilt_etc {
2868 name: "myetc",
2869 src: "myprebuilt",
2870 sub_dir: "foo/bar",
2871 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002872
2873 cc_library {
2874 name: "mylib",
2875 srcs: ["mylib.cpp"],
2876 relative_install_path: "foo/bar",
2877 system_shared_libs: [],
2878 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002879 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002880 }
2881
2882 cc_binary {
2883 name: "mybin",
2884 srcs: ["mylib.cpp"],
2885 relative_install_path: "foo/bar",
2886 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002887 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002888 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002889 }
Jooyung Han4ed512b2023-08-11 16:30:04 +09002890
2891 rust_binary {
2892 name: "mybin.rust",
2893 srcs: ["foo.rs"],
2894 relative_install_path: "rust_subdir",
2895 apex_available: [ "myapex" ],
2896 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002897 `)
2898
Jooyung Hana0503a52023-08-23 13:12:50 +09002899 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002900 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002901
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002902 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002903 ensureContains(t, cmd, "/etc ")
2904 ensureContains(t, cmd, "/etc/foo ")
2905 ensureContains(t, cmd, "/etc/foo/bar ")
2906 ensureContains(t, cmd, "/lib64 ")
2907 ensureContains(t, cmd, "/lib64/foo ")
2908 ensureContains(t, cmd, "/lib64/foo/bar ")
2909 ensureContains(t, cmd, "/lib ")
2910 ensureContains(t, cmd, "/lib/foo ")
2911 ensureContains(t, cmd, "/lib/foo/bar ")
2912 ensureContains(t, cmd, "/bin ")
2913 ensureContains(t, cmd, "/bin/foo ")
2914 ensureContains(t, cmd, "/bin/foo/bar ")
Jooyung Han4ed512b2023-08-11 16:30:04 +09002915 ensureContains(t, cmd, "/bin/rust_subdir ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002916}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002917
Jooyung Han35155c42020-02-06 17:33:20 +09002918func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002919 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002920 apex {
2921 name: "myapex",
2922 key: "myapex.key",
2923 multilib: {
2924 both: {
2925 native_shared_libs: ["mylib"],
2926 binaries: ["mybin"],
2927 },
2928 },
2929 compile_multilib: "both",
2930 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002931 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002932 }
2933
2934 apex_key {
2935 name: "myapex.key",
2936 public_key: "testkey.avbpubkey",
2937 private_key: "testkey.pem",
2938 }
2939
2940 cc_library {
2941 name: "mylib",
2942 relative_install_path: "foo/bar",
2943 system_shared_libs: [],
2944 stl: "none",
2945 apex_available: [ "myapex" ],
2946 native_bridge_supported: true,
2947 }
2948
2949 cc_binary {
2950 name: "mybin",
2951 relative_install_path: "foo/bar",
2952 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002953 stl: "none",
2954 apex_available: [ "myapex" ],
2955 native_bridge_supported: true,
2956 compile_multilib: "both", // default is "first" for binary
2957 multilib: {
2958 lib64: {
2959 suffix: "64",
2960 },
2961 },
2962 }
2963 `, withNativeBridgeEnabled)
Jooyung Hana0503a52023-08-23 13:12:50 +09002964 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han35155c42020-02-06 17:33:20 +09002965 "bin/foo/bar/mybin",
2966 "bin/foo/bar/mybin64",
2967 "bin/arm/foo/bar/mybin",
2968 "bin/arm64/foo/bar/mybin64",
2969 "lib/foo/bar/mylib.so",
2970 "lib/arm/foo/bar/mylib.so",
2971 "lib64/foo/bar/mylib.so",
2972 "lib64/arm64/foo/bar/mylib.so",
2973 })
2974}
2975
Jooyung Han85d61762020-06-24 23:50:26 +09002976func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002977 result := android.GroupFixturePreparers(
2978 prepareForApexTest,
2979 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2980 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002981 apex {
2982 name: "myapex",
2983 key: "myapex.key",
2984 binaries: ["mybin"],
2985 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002986 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002987 }
2988 apex_key {
2989 name: "myapex.key",
2990 public_key: "testkey.avbpubkey",
2991 private_key: "testkey.pem",
2992 }
2993 cc_binary {
2994 name: "mybin",
2995 vendor: true,
2996 shared_libs: ["libfoo"],
2997 }
2998 cc_library {
2999 name: "libfoo",
3000 proprietary: true,
3001 }
3002 `)
3003
Jooyung Hana0503a52023-08-23 13:12:50 +09003004 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09003005 "bin/mybin",
3006 "lib64/libfoo.so",
3007 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
3008 "lib64/libc++.so",
3009 })
3010
Jooyung Hana0503a52023-08-23 13:12:50 +09003011 apexBundle := result.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossc68db4b2021-11-11 18:59:15 -08003012 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09003013 name := apexBundle.BaseModuleName()
3014 prefix := "TARGET_"
3015 var builder strings.Builder
3016 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08003017 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01003018 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01003019 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003020
Jooyung Hana0503a52023-08-23 13:12:50 +09003021 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09003022 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
3023 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09003024}
3025
Justin Yun13decfb2021-03-08 19:25:55 +09003026func TestProductVariant(t *testing.T) {
3027 ctx := testApex(t, `
3028 apex {
3029 name: "myapex",
3030 key: "myapex.key",
3031 updatable: false,
3032 product_specific: true,
3033 binaries: ["foo"],
3034 }
3035
3036 apex_key {
3037 name: "myapex.key",
3038 public_key: "testkey.avbpubkey",
3039 private_key: "testkey.pem",
3040 }
3041
3042 cc_binary {
3043 name: "foo",
3044 product_available: true,
3045 apex_available: ["myapex"],
3046 srcs: ["foo.cpp"],
3047 }
Justin Yunaf1fde42023-09-27 16:22:10 +09003048 `)
Justin Yun13decfb2021-03-08 19:25:55 +09003049
3050 cflags := strings.Fields(
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003051 ctx.ModuleForTests("foo", "android_product_arm64_armv8-a_apex10000").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09003052 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
3053 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
3054 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
3055 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
3056}
3057
Jooyung Han8e5685d2020-09-21 11:02:57 +09003058func TestApex_withPrebuiltFirmware(t *testing.T) {
3059 testCases := []struct {
3060 name string
3061 additionalProp string
3062 }{
3063 {"system apex with prebuilt_firmware", ""},
3064 {"vendor apex with prebuilt_firmware", "vendor: true,"},
3065 }
3066 for _, tc := range testCases {
3067 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003068 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09003069 apex {
3070 name: "myapex",
3071 key: "myapex.key",
3072 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003073 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09003074 `+tc.additionalProp+`
3075 }
3076 apex_key {
3077 name: "myapex.key",
3078 public_key: "testkey.avbpubkey",
3079 private_key: "testkey.pem",
3080 }
3081 prebuilt_firmware {
3082 name: "myfirmware",
3083 src: "myfirmware.bin",
3084 filename_from_src: true,
3085 `+tc.additionalProp+`
3086 }
3087 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09003088 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han8e5685d2020-09-21 11:02:57 +09003089 "etc/firmware/myfirmware.bin",
3090 })
3091 })
3092 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003093}
3094
Jooyung Hanefb184e2020-06-25 17:14:25 +09003095func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003096 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003097 apex {
3098 name: "myapex",
3099 key: "myapex.key",
3100 vendor: true,
3101 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003102 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003103 }
3104
3105 apex_key {
3106 name: "myapex.key",
3107 public_key: "testkey.avbpubkey",
3108 private_key: "testkey.pem",
3109 }
3110
3111 cc_library {
3112 name: "mylib",
3113 vendor_available: true,
3114 }
3115 `)
3116
Jooyung Hana0503a52023-08-23 13:12:50 +09003117 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003118 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003119 name := apexBundle.BaseModuleName()
3120 prefix := "TARGET_"
3121 var builder strings.Builder
3122 data.Custom(&builder, name, prefix, "", data)
3123 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09003124 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++.vendor.myapex:64 mylib.vendor.myapex:64 libc.vendor libm.vendor libdl.vendor\n")
Jooyung Hanefb184e2020-06-25 17:14:25 +09003125}
3126
Jooyung Han2ed99d02020-06-24 23:26:26 +09003127func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003128 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003129 apex {
3130 name: "myapex",
3131 key: "myapex.key",
3132 vintf_fragments: ["fragment.xml"],
3133 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003134 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003135 }
3136 apex_key {
3137 name: "myapex.key",
3138 public_key: "testkey.avbpubkey",
3139 private_key: "testkey.pem",
3140 }
3141 cc_binary {
3142 name: "mybin",
3143 }
3144 `)
3145
Jooyung Hana0503a52023-08-23 13:12:50 +09003146 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003147 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003148 name := apexBundle.BaseModuleName()
3149 prefix := "TARGET_"
3150 var builder strings.Builder
3151 data.Custom(&builder, name, prefix, "", data)
3152 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003153 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003154 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003155}
3156
Jiyong Park16e91a02018-12-20 18:18:08 +09003157func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003158 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003159 apex {
3160 name: "myapex",
3161 key: "myapex.key",
3162 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003163 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003164 }
3165
3166 apex_key {
3167 name: "myapex.key",
3168 public_key: "testkey.avbpubkey",
3169 private_key: "testkey.pem",
3170 }
3171
3172 cc_library {
3173 name: "mylib",
3174 srcs: ["mylib.cpp"],
3175 system_shared_libs: [],
3176 stl: "none",
3177 stubs: {
3178 versions: ["1", "2", "3"],
3179 },
Spandan Das20fce2d2023-04-12 17:21:39 +00003180 apex_available: ["myapex"],
Jiyong Park16e91a02018-12-20 18:18:08 +09003181 }
3182
3183 cc_binary {
3184 name: "not_in_apex",
3185 srcs: ["mylib.cpp"],
3186 static_libs: ["mylib"],
3187 static_executable: true,
3188 system_shared_libs: [],
3189 stl: "none",
3190 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003191 `)
3192
Colin Cross7113d202019-11-20 16:39:12 -08003193 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003194
3195 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003196 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003197}
Jiyong Park9335a262018-12-24 11:31:58 +09003198
3199func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003200 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003201 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003202 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003203 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003204 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003205 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003206 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003207 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003208 }
3209
3210 cc_library {
3211 name: "mylib",
3212 srcs: ["mylib.cpp"],
3213 system_shared_libs: [],
3214 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003215 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003216 }
3217
3218 apex_key {
3219 name: "myapex.key",
3220 public_key: "testkey.avbpubkey",
3221 private_key: "testkey.pem",
3222 }
3223
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003224 android_app_certificate {
3225 name: "myapex.certificate",
3226 certificate: "testkey",
3227 }
3228
3229 android_app_certificate {
3230 name: "myapex.certificate.override",
3231 certificate: "testkey.override",
3232 }
3233
Jiyong Park9335a262018-12-24 11:31:58 +09003234 `)
3235
3236 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003237 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003238
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003239 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3240 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003241 "vendor/foo/devkeys/testkey.avbpubkey")
3242 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003243 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3244 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003245 "vendor/foo/devkeys/testkey.pem")
3246 }
3247
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003248 // check the APK certs. It should be overridden to myapex.certificate.override
Jooyung Hana0503a52023-08-23 13:12:50 +09003249 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003250 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003251 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003252 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003253 }
3254}
Jiyong Park58e364a2019-01-19 19:24:06 +09003255
Jooyung Hanf121a652019-12-17 14:30:11 +09003256func TestCertificate(t *testing.T) {
3257 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003258 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003259 apex {
3260 name: "myapex",
3261 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003262 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003263 }
3264 apex_key {
3265 name: "myapex.key",
3266 public_key: "testkey.avbpubkey",
3267 private_key: "testkey.pem",
3268 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003269 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003270 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3271 if actual := rule.Args["certificates"]; actual != expected {
3272 t.Errorf("certificates should be %q, not %q", expected, actual)
3273 }
3274 })
3275 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003276 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003277 apex {
3278 name: "myapex_keytest",
3279 key: "myapex.key",
3280 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003281 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003282 }
3283 apex_key {
3284 name: "myapex.key",
3285 public_key: "testkey.avbpubkey",
3286 private_key: "testkey.pem",
3287 }
3288 android_app_certificate {
3289 name: "myapex.certificate.override",
3290 certificate: "testkey.override",
3291 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003292 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003293 expected := "testkey.override.x509.pem testkey.override.pk8"
3294 if actual := rule.Args["certificates"]; actual != expected {
3295 t.Errorf("certificates should be %q, not %q", expected, actual)
3296 }
3297 })
3298 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003299 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003300 apex {
3301 name: "myapex",
3302 key: "myapex.key",
3303 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003304 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003305 }
3306 apex_key {
3307 name: "myapex.key",
3308 public_key: "testkey.avbpubkey",
3309 private_key: "testkey.pem",
3310 }
3311 android_app_certificate {
3312 name: "myapex.certificate",
3313 certificate: "testkey",
3314 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003315 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003316 expected := "testkey.x509.pem testkey.pk8"
3317 if actual := rule.Args["certificates"]; actual != expected {
3318 t.Errorf("certificates should be %q, not %q", expected, actual)
3319 }
3320 })
3321 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003322 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003323 apex {
3324 name: "myapex_keytest",
3325 key: "myapex.key",
3326 file_contexts: ":myapex-file_contexts",
3327 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003328 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003329 }
3330 apex_key {
3331 name: "myapex.key",
3332 public_key: "testkey.avbpubkey",
3333 private_key: "testkey.pem",
3334 }
3335 android_app_certificate {
3336 name: "myapex.certificate.override",
3337 certificate: "testkey.override",
3338 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003339 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003340 expected := "testkey.override.x509.pem testkey.override.pk8"
3341 if actual := rule.Args["certificates"]; actual != expected {
3342 t.Errorf("certificates should be %q, not %q", expected, actual)
3343 }
3344 })
3345 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003346 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003347 apex {
3348 name: "myapex",
3349 key: "myapex.key",
3350 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003351 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003352 }
3353 apex_key {
3354 name: "myapex.key",
3355 public_key: "testkey.avbpubkey",
3356 private_key: "testkey.pem",
3357 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003358 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003359 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3360 if actual := rule.Args["certificates"]; actual != expected {
3361 t.Errorf("certificates should be %q, not %q", expected, actual)
3362 }
3363 })
3364 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003365 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003366 apex {
3367 name: "myapex_keytest",
3368 key: "myapex.key",
3369 file_contexts: ":myapex-file_contexts",
3370 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003371 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003372 }
3373 apex_key {
3374 name: "myapex.key",
3375 public_key: "testkey.avbpubkey",
3376 private_key: "testkey.pem",
3377 }
3378 android_app_certificate {
3379 name: "myapex.certificate.override",
3380 certificate: "testkey.override",
3381 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003382 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003383 expected := "testkey.override.x509.pem testkey.override.pk8"
3384 if actual := rule.Args["certificates"]; actual != expected {
3385 t.Errorf("certificates should be %q, not %q", expected, actual)
3386 }
3387 })
3388}
3389
Jiyong Park58e364a2019-01-19 19:24:06 +09003390func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003391 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003392 apex {
3393 name: "myapex",
3394 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003395 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003396 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003397 }
3398
3399 apex {
3400 name: "otherapex",
3401 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003402 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003403 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003404 }
3405
3406 apex_key {
3407 name: "myapex.key",
3408 public_key: "testkey.avbpubkey",
3409 private_key: "testkey.pem",
3410 }
3411
3412 cc_library {
3413 name: "mylib",
3414 srcs: ["mylib.cpp"],
3415 system_shared_libs: [],
3416 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003417 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003418 "myapex",
3419 "otherapex",
3420 ],
Jooyung Han24282772020-03-21 23:20:55 +09003421 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003422 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003423 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003424 cc_library {
3425 name: "mylib2",
3426 srcs: ["mylib.cpp"],
3427 system_shared_libs: [],
3428 stl: "none",
3429 apex_available: [
3430 "myapex",
3431 "otherapex",
3432 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003433 static_libs: ["mylib3"],
3434 recovery_available: true,
3435 min_sdk_version: "29",
3436 }
3437 cc_library {
3438 name: "mylib3",
3439 srcs: ["mylib.cpp"],
3440 system_shared_libs: [],
3441 stl: "none",
3442 apex_available: [
3443 "myapex",
3444 "otherapex",
3445 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003446 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003447 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003448 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003449 `)
3450
Jooyung Hanc87a0592020-03-02 17:44:33 +09003451 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003452 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003453 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003454
Vinh Tranf9754732023-01-19 22:41:46 -05003455 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003456 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003457 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003458
Vinh Tranf9754732023-01-19 22:41:46 -05003459 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003460 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003461 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003462
Colin Crossaede88c2020-08-11 12:17:01 -07003463 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3464 // each variant defines additional macros to distinguish which apex variant it is built for
3465
3466 // non-APEX variant does not have __ANDROID_APEX__ defined
3467 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3468 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3469
Vinh Tranf9754732023-01-19 22:41:46 -05003470 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003471 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3472 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003473
Jooyung Hanc87a0592020-03-02 17:44:33 +09003474 // non-APEX variant does not have __ANDROID_APEX__ defined
3475 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3476 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3477
Vinh Tranf9754732023-01-19 22:41:46 -05003478 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003479 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003480 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003481}
Jiyong Park7e636d02019-01-28 16:16:54 +09003482
3483func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003484 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003485 apex {
3486 name: "myapex",
3487 key: "myapex.key",
3488 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003489 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003490 }
3491
3492 apex_key {
3493 name: "myapex.key",
3494 public_key: "testkey.avbpubkey",
3495 private_key: "testkey.pem",
3496 }
3497
3498 cc_library_headers {
3499 name: "mylib_headers",
3500 export_include_dirs: ["my_include"],
3501 system_shared_libs: [],
3502 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003503 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003504 }
3505
3506 cc_library {
3507 name: "mylib",
3508 srcs: ["mylib.cpp"],
3509 system_shared_libs: [],
3510 stl: "none",
3511 header_libs: ["mylib_headers"],
3512 export_header_lib_headers: ["mylib_headers"],
3513 stubs: {
3514 versions: ["1", "2", "3"],
3515 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003516 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003517 }
3518
3519 cc_library {
3520 name: "otherlib",
3521 srcs: ["mylib.cpp"],
3522 system_shared_libs: [],
3523 stl: "none",
3524 shared_libs: ["mylib"],
3525 }
3526 `)
3527
Colin Cross7113d202019-11-20 16:39:12 -08003528 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003529
3530 // Ensure that the include path of the header lib is exported to 'otherlib'
3531 ensureContains(t, cFlags, "-Imy_include")
3532}
Alex Light9670d332019-01-29 18:07:33 -08003533
Jiyong Park7cd10e32020-01-14 09:22:18 +09003534type fileInApex struct {
3535 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003536 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003537 isLink bool
3538}
3539
Jooyung Han1724d582022-12-21 10:17:44 +09003540func (f fileInApex) String() string {
3541 return f.src + ":" + f.path
3542}
3543
3544func (f fileInApex) match(expectation string) bool {
3545 parts := strings.Split(expectation, ":")
3546 if len(parts) == 1 {
3547 match, _ := path.Match(parts[0], f.path)
3548 return match
3549 }
3550 if len(parts) == 2 {
3551 matchSrc, _ := path.Match(parts[0], f.src)
3552 matchDst, _ := path.Match(parts[1], f.path)
3553 return matchSrc && matchDst
3554 }
3555 panic("invalid expected file specification: " + expectation)
3556}
3557
Jooyung Hana57af4a2020-01-23 05:36:59 +00003558func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003559 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003560 module := ctx.ModuleForTests(moduleName, variant)
3561 apexRule := module.MaybeRule("apexRule")
3562 apexDir := "/image.apex/"
Jooyung Han31c470b2019-10-18 16:26:59 +09003563 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003564 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003565 for _, cmd := range strings.Split(copyCmds, "&&") {
3566 cmd = strings.TrimSpace(cmd)
3567 if cmd == "" {
3568 continue
3569 }
3570 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003571 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003572 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003573 switch terms[0] {
3574 case "mkdir":
3575 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003576 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003577 t.Fatal("copyCmds contains invalid cp command", cmd)
3578 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003579 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003580 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003581 isLink = false
3582 case "ln":
3583 if len(terms) != 3 && len(terms) != 4 {
3584 // ln LINK TARGET or ln -s LINK TARGET
3585 t.Fatal("copyCmds contains invalid ln command", cmd)
3586 }
3587 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003588 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003589 isLink = true
3590 default:
3591 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3592 }
3593 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003594 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003595 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003596 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003597 }
Jooyung Han1724d582022-12-21 10:17:44 +09003598 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003599 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003600 }
3601 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003602 return ret
3603}
3604
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003605func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003606 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003607 var failed bool
3608 var surplus []string
3609 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003610 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003611 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003612 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003613 if file.match(expected) {
3614 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003615 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003616 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003617 }
3618 }
Jooyung Han1724d582022-12-21 10:17:44 +09003619 if !matchFound {
3620 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003621 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003622 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003623
Jooyung Han31c470b2019-10-18 16:26:59 +09003624 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003625 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003626 t.Log("surplus files", surplus)
3627 failed = true
3628 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003629
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003630 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003631 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003632 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003633 if !filesMatched[expected] {
3634 missing = append(missing, expected)
3635 }
3636 }
3637 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003638 t.Log("missing files", missing)
3639 failed = true
3640 }
3641 if failed {
3642 t.Fail()
3643 }
3644}
3645
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003646func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3647 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3648}
3649
3650func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
Spandan Das2069c3f2023-12-06 19:40:24 +00003651 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Description("deapex")
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003652 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3653 if deapexer.Output != nil {
3654 outputs = append(outputs, deapexer.Output.String())
3655 }
3656 for _, output := range deapexer.ImplicitOutputs {
3657 outputs = append(outputs, output.String())
3658 }
3659 actualFiles := make([]fileInApex, 0, len(outputs))
3660 for _, output := range outputs {
3661 dir := "/deapexer/"
3662 pos := strings.LastIndex(output, dir)
3663 if pos == -1 {
3664 t.Fatal("Unknown deapexer output ", output)
3665 }
3666 path := output[pos+len(dir):]
3667 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3668 }
3669 assertFileListEquals(t, files, actualFiles)
3670}
3671
Jooyung Han39edb6c2019-11-06 16:53:07 +09003672func vndkLibrariesTxtFiles(vers ...string) (result string) {
3673 for _, v := range vers {
3674 if v == "current" {
Justin Yund5784122023-10-25 13:25:32 +09003675 for _, txt := range []string{"vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003676 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003677 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003678 name: "` + txt + `.libraries.txt",
Justin Yund5784122023-10-25 13:25:32 +09003679 insert_vndk_version: true,
Jooyung Han39edb6c2019-11-06 16:53:07 +09003680 }
3681 `
3682 }
Justin Yund5784122023-10-25 13:25:32 +09003683 result += `
3684 llndk_libraries_txt {
3685 name: "llndk.libraries.txt",
3686 }
3687 llndk_libraries_txt_for_apex {
3688 name: "llndk.libraries.txt.apex",
3689 stem: "llndk.libraries.txt",
3690 insert_vndk_version: true,
3691 }
3692 `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003693 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003694 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003695 result += `
3696 prebuilt_etc {
3697 name: "` + txt + `.libraries.` + v + `.txt",
3698 src: "dummy.txt",
3699 }
3700 `
3701 }
3702 }
3703 }
3704 return
3705}
3706
Jooyung Han344d5432019-08-23 11:17:39 +09003707func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003708 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003709 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003710 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003711 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003712 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003713 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003714 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003715 }
3716
3717 apex_key {
3718 name: "myapex.key",
3719 public_key: "testkey.avbpubkey",
3720 private_key: "testkey.pem",
3721 }
3722
Jooyung Han31c470b2019-10-18 16:26:59 +09003723 vndk_prebuilt_shared {
3724 name: "libvndk27",
3725 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003726 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003727 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003728 vndk: {
3729 enabled: true,
3730 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003731 target_arch: "arm64",
3732 arch: {
3733 arm: {
3734 srcs: ["libvndk27_arm.so"],
3735 },
3736 arm64: {
3737 srcs: ["libvndk27_arm64.so"],
3738 },
3739 },
Colin Cross2807f002021-03-02 10:15:29 -08003740 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003741 }
3742
3743 vndk_prebuilt_shared {
3744 name: "libvndk27",
3745 version: "27",
3746 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003747 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003748 vndk: {
3749 enabled: true,
3750 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003751 target_arch: "x86_64",
3752 arch: {
3753 x86: {
3754 srcs: ["libvndk27_x86.so"],
3755 },
3756 x86_64: {
3757 srcs: ["libvndk27_x86_64.so"],
3758 },
3759 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003760 }
3761 `+vndkLibrariesTxtFiles("27"),
3762 withFiles(map[string][]byte{
3763 "libvndk27_arm.so": nil,
3764 "libvndk27_arm64.so": nil,
3765 "libvndk27_x86.so": nil,
3766 "libvndk27_x86_64.so": nil,
3767 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003768
Jooyung Hana0503a52023-08-23 13:12:50 +09003769 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003770 "lib/libvndk27_arm.so",
3771 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003772 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003773 })
Jooyung Han344d5432019-08-23 11:17:39 +09003774}
3775
Jooyung Han90eee022019-10-01 20:02:42 +09003776func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003777 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003778 apex_vndk {
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003779 name: "com.android.vndk.v29",
Jooyung Han90eee022019-10-01 20:02:42 +09003780 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003781 file_contexts: ":myapex-file_contexts",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003782 vndk_version: "29",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003783 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003784 }
3785 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003786 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003787 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003788 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003789 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003790 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003791 }
3792 apex_key {
3793 name: "myapex.key",
3794 public_key: "testkey.avbpubkey",
3795 private_key: "testkey.pem",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003796 }`+vndkLibrariesTxtFiles("28", "29"))
Jooyung Han90eee022019-10-01 20:02:42 +09003797
3798 assertApexName := func(expected, moduleName string) {
Jooyung Hana0503a52023-08-23 13:12:50 +09003799 module := ctx.ModuleForTests(moduleName, "android_common")
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003800 apexManifestRule := module.Rule("apexManifestRule")
3801 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003802 }
3803
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003804 assertApexName("com.android.vndk.v29", "com.android.vndk.v29")
Colin Cross2807f002021-03-02 10:15:29 -08003805 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003806}
3807
Jooyung Han344d5432019-08-23 11:17:39 +09003808func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003809 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003810 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003811 name: "com.android.vndk.current",
3812 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003813 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003814 native_bridge_supported: true,
3815 }
3816
3817 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003818 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003819 public_key: "testkey.avbpubkey",
3820 private_key: "testkey.pem",
3821 }
3822
3823 cc_library {
3824 name: "libvndk",
3825 srcs: ["mylib.cpp"],
3826 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003827 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003828 native_bridge_supported: true,
3829 host_supported: true,
3830 vndk: {
3831 enabled: true,
3832 },
3833 system_shared_libs: [],
3834 stl: "none",
3835 }
3836 `)
3837}
3838
Jooyung Han31c470b2019-10-18 16:26:59 +09003839func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003840 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003841 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003842 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003843 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003844 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003845 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003846 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003847 }
3848
3849 apex_key {
3850 name: "myapex.key",
3851 public_key: "testkey.avbpubkey",
3852 private_key: "testkey.pem",
3853 }
3854
3855 vndk_prebuilt_shared {
3856 name: "libvndk27",
3857 version: "27",
3858 target_arch: "arm",
3859 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003860 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003861 vndk: {
3862 enabled: true,
3863 },
3864 arch: {
3865 arm: {
3866 srcs: ["libvndk27.so"],
3867 }
3868 },
3869 }
3870
3871 vndk_prebuilt_shared {
3872 name: "libvndk27",
3873 version: "27",
3874 target_arch: "arm",
3875 binder32bit: true,
3876 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003877 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003878 vndk: {
3879 enabled: true,
3880 },
3881 arch: {
3882 arm: {
3883 srcs: ["libvndk27binder32.so"],
3884 }
3885 },
Colin Cross2807f002021-03-02 10:15:29 -08003886 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003887 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003888 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003889 withFiles(map[string][]byte{
3890 "libvndk27.so": nil,
3891 "libvndk27binder32.so": nil,
3892 }),
3893 withBinder32bit,
3894 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07003895 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09003896 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3897 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003898 },
3899 }),
3900 )
3901
Jooyung Hana0503a52023-08-23 13:12:50 +09003902 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003903 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003904 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003905 })
3906}
3907
Jooyung Hane1633032019-08-01 17:41:43 +09003908func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003909 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003910 apex {
3911 name: "myapex_nodep",
3912 key: "myapex.key",
3913 native_shared_libs: ["lib_nodep"],
3914 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003915 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003916 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003917 }
3918
3919 apex {
3920 name: "myapex_dep",
3921 key: "myapex.key",
3922 native_shared_libs: ["lib_dep"],
3923 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003924 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003925 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003926 }
3927
3928 apex {
3929 name: "myapex_provider",
3930 key: "myapex.key",
3931 native_shared_libs: ["libfoo"],
3932 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003933 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003934 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003935 }
3936
3937 apex {
3938 name: "myapex_selfcontained",
3939 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00003940 native_shared_libs: ["lib_dep_on_bar", "libbar"],
Jooyung Hane1633032019-08-01 17:41:43 +09003941 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003942 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003943 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003944 }
3945
3946 apex_key {
3947 name: "myapex.key",
3948 public_key: "testkey.avbpubkey",
3949 private_key: "testkey.pem",
3950 }
3951
3952 cc_library {
3953 name: "lib_nodep",
3954 srcs: ["mylib.cpp"],
3955 system_shared_libs: [],
3956 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003957 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003958 }
3959
3960 cc_library {
3961 name: "lib_dep",
3962 srcs: ["mylib.cpp"],
3963 shared_libs: ["libfoo"],
3964 system_shared_libs: [],
3965 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003966 apex_available: [
3967 "myapex_dep",
3968 "myapex_provider",
3969 "myapex_selfcontained",
3970 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003971 }
3972
3973 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00003974 name: "lib_dep_on_bar",
3975 srcs: ["mylib.cpp"],
3976 shared_libs: ["libbar"],
3977 system_shared_libs: [],
3978 stl: "none",
3979 apex_available: [
3980 "myapex_selfcontained",
3981 ],
3982 }
3983
3984
3985 cc_library {
Jooyung Hane1633032019-08-01 17:41:43 +09003986 name: "libfoo",
3987 srcs: ["mytest.cpp"],
3988 stubs: {
3989 versions: ["1"],
3990 },
3991 system_shared_libs: [],
3992 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003993 apex_available: [
3994 "myapex_provider",
Spandan Das20fce2d2023-04-12 17:21:39 +00003995 ],
3996 }
3997
3998 cc_library {
3999 name: "libbar",
4000 srcs: ["mytest.cpp"],
4001 stubs: {
4002 versions: ["1"],
4003 },
4004 system_shared_libs: [],
4005 stl: "none",
4006 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004007 "myapex_selfcontained",
4008 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004009 }
Spandan Das20fce2d2023-04-12 17:21:39 +00004010
Jooyung Hane1633032019-08-01 17:41:43 +09004011 `)
4012
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004013 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004014 var provideNativeLibs, requireNativeLibs []string
4015
Jooyung Hana0503a52023-08-23 13:12:50 +09004016 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004017 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4018 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004019 ensureListEmpty(t, provideNativeLibs)
4020 ensureListEmpty(t, requireNativeLibs)
4021
Jooyung Hana0503a52023-08-23 13:12:50 +09004022 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004023 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4024 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004025 ensureListEmpty(t, provideNativeLibs)
4026 ensureListContains(t, requireNativeLibs, "libfoo.so")
4027
Jooyung Hana0503a52023-08-23 13:12:50 +09004028 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004029 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4030 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004031 ensureListContains(t, provideNativeLibs, "libfoo.so")
4032 ensureListEmpty(t, requireNativeLibs)
4033
Jooyung Hana0503a52023-08-23 13:12:50 +09004034 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004035 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4036 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Spandan Das20fce2d2023-04-12 17:21:39 +00004037 ensureListContains(t, provideNativeLibs, "libbar.so")
Jooyung Hane1633032019-08-01 17:41:43 +09004038 ensureListEmpty(t, requireNativeLibs)
4039}
4040
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004041func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4042 ctx := testApex(t, `
4043 apex {
4044 name: "myapex",
4045 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004046 native_shared_libs: ["mylib"],
4047 updatable: false,
4048 }
4049
4050 apex_key {
4051 name: "myapex.key",
4052 public_key: "testkey.avbpubkey",
4053 private_key: "testkey.pem",
4054 }
4055
4056 cc_library {
4057 name: "mylib",
4058 srcs: ["mylib.cpp"],
4059 system_shared_libs: [],
4060 stl: "none",
4061 apex_available: [
4062 "//apex_available:platform",
4063 "myapex",
4064 ],
4065 }
4066 `, android.FixtureMergeEnv(map[string]string{
4067 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4068 }))
4069
Jooyung Hana0503a52023-08-23 13:12:50 +09004070 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004071 apexManifestRule := module.Rule("apexManifestRule")
4072 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4073}
4074
Vinh Tran8f5310f2022-10-07 18:16:47 -04004075func TestCompileMultilibProp(t *testing.T) {
4076 testCases := []struct {
4077 compileMultiLibProp string
4078 containedLibs []string
4079 notContainedLibs []string
4080 }{
4081 {
4082 containedLibs: []string{
4083 "image.apex/lib64/mylib.so",
4084 "image.apex/lib/mylib.so",
4085 },
4086 compileMultiLibProp: `compile_multilib: "both",`,
4087 },
4088 {
4089 containedLibs: []string{"image.apex/lib64/mylib.so"},
4090 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4091 compileMultiLibProp: `compile_multilib: "first",`,
4092 },
4093 {
4094 containedLibs: []string{"image.apex/lib64/mylib.so"},
4095 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4096 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4097 },
4098 {
4099 containedLibs: []string{"image.apex/lib64/mylib.so"},
4100 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4101 compileMultiLibProp: `compile_multilib: "64",`,
4102 },
4103 {
4104 containedLibs: []string{"image.apex/lib/mylib.so"},
4105 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4106 compileMultiLibProp: `compile_multilib: "32",`,
4107 },
4108 }
4109 for _, testCase := range testCases {
4110 ctx := testApex(t, fmt.Sprintf(`
4111 apex {
4112 name: "myapex",
4113 key: "myapex.key",
4114 %s
4115 native_shared_libs: ["mylib"],
4116 updatable: false,
4117 }
4118 apex_key {
4119 name: "myapex.key",
4120 public_key: "testkey.avbpubkey",
4121 private_key: "testkey.pem",
4122 }
4123 cc_library {
4124 name: "mylib",
4125 srcs: ["mylib.cpp"],
4126 apex_available: [
4127 "//apex_available:platform",
4128 "myapex",
4129 ],
4130 }
4131 `, testCase.compileMultiLibProp),
4132 )
Jooyung Hana0503a52023-08-23 13:12:50 +09004133 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Vinh Tran8f5310f2022-10-07 18:16:47 -04004134 apexRule := module.Rule("apexRule")
4135 copyCmds := apexRule.Args["copy_commands"]
4136 for _, containedLib := range testCase.containedLibs {
4137 ensureContains(t, copyCmds, containedLib)
4138 }
4139 for _, notContainedLib := range testCase.notContainedLibs {
4140 ensureNotContains(t, copyCmds, notContainedLib)
4141 }
4142 }
4143}
4144
Alex Light0851b882019-02-07 13:20:53 -08004145func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004146 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004147 apex {
4148 name: "myapex",
4149 key: "myapex.key",
4150 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004151 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004152 }
4153
4154 apex_key {
4155 name: "myapex.key",
4156 public_key: "testkey.avbpubkey",
4157 private_key: "testkey.pem",
4158 }
4159
4160 cc_library {
4161 name: "mylib_common",
4162 srcs: ["mylib.cpp"],
4163 system_shared_libs: [],
4164 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004165 apex_available: [
4166 "//apex_available:platform",
4167 "myapex",
4168 ],
Alex Light0851b882019-02-07 13:20:53 -08004169 }
4170 `)
4171
Jooyung Hana0503a52023-08-23 13:12:50 +09004172 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004173 apexRule := module.Rule("apexRule")
4174 copyCmds := apexRule.Args["copy_commands"]
4175
4176 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4177 t.Log("Apex was a test apex!")
4178 t.Fail()
4179 }
4180 // Ensure that main rule creates an output
4181 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4182
4183 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004184 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004185
4186 // Ensure that both direct and indirect deps are copied into apex
4187 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4188
Colin Cross7113d202019-11-20 16:39:12 -08004189 // Ensure that the platform variant ends with _shared
4190 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004191
Colin Cross56a83212020-09-15 18:30:11 -07004192 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004193 t.Log("Found mylib_common not in any apex!")
4194 t.Fail()
4195 }
4196}
4197
4198func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004199 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004200 apex_test {
4201 name: "myapex",
4202 key: "myapex.key",
4203 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004204 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004205 }
4206
4207 apex_key {
4208 name: "myapex.key",
4209 public_key: "testkey.avbpubkey",
4210 private_key: "testkey.pem",
4211 }
4212
4213 cc_library {
4214 name: "mylib_common_test",
4215 srcs: ["mylib.cpp"],
4216 system_shared_libs: [],
4217 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004218 // TODO: remove //apex_available:platform
4219 apex_available: [
4220 "//apex_available:platform",
4221 "myapex",
4222 ],
Alex Light0851b882019-02-07 13:20:53 -08004223 }
4224 `)
4225
Jooyung Hana0503a52023-08-23 13:12:50 +09004226 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004227 apexRule := module.Rule("apexRule")
4228 copyCmds := apexRule.Args["copy_commands"]
4229
4230 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4231 t.Log("Apex was not a test apex!")
4232 t.Fail()
4233 }
4234 // Ensure that main rule creates an output
4235 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4236
4237 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004238 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004239
4240 // Ensure that both direct and indirect deps are copied into apex
4241 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4242
Colin Cross7113d202019-11-20 16:39:12 -08004243 // Ensure that the platform variant ends with _shared
4244 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004245}
4246
Jooyung Han85707de2023-12-01 14:21:13 +09004247func TestLibzVendorIsntStable(t *testing.T) {
4248 ctx := testApex(t, `
4249 apex {
4250 name: "myapex",
4251 key: "myapex.key",
4252 updatable: false,
4253 binaries: ["mybin"],
4254 }
4255 apex {
4256 name: "myvendorapex",
4257 key: "myapex.key",
4258 file_contexts: "myvendorapex_file_contexts",
4259 vendor: true,
4260 updatable: false,
4261 binaries: ["mybin"],
4262 }
4263 apex_key {
4264 name: "myapex.key",
4265 public_key: "testkey.avbpubkey",
4266 private_key: "testkey.pem",
4267 }
4268 cc_binary {
4269 name: "mybin",
4270 vendor_available: true,
4271 system_shared_libs: [],
4272 stl: "none",
4273 shared_libs: ["libz"],
4274 apex_available: ["//apex_available:anyapex"],
4275 }
4276 cc_library {
4277 name: "libz",
4278 vendor_available: true,
4279 system_shared_libs: [],
4280 stl: "none",
4281 stubs: {
4282 versions: ["28", "30"],
4283 },
4284 target: {
4285 vendor: {
4286 no_stubs: true,
4287 },
4288 },
4289 }
4290 `, withFiles(map[string][]byte{
4291 "myvendorapex_file_contexts": nil,
4292 }))
4293
4294 // libz provides stubs for core variant.
4295 {
4296 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
4297 "bin/mybin",
4298 })
4299 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
4300 android.AssertStringEquals(t, "should require libz", apexManifestRule.Args["requireNativeLibs"], "libz.so")
4301 }
4302 // libz doesn't provide stubs for vendor variant.
4303 {
4304 ensureExactContents(t, ctx, "myvendorapex", "android_common_myvendorapex", []string{
4305 "bin/mybin",
4306 "lib64/libz.so",
4307 })
4308 apexManifestRule := ctx.ModuleForTests("myvendorapex", "android_common_myvendorapex").Rule("apexManifestRule")
4309 android.AssertStringEquals(t, "should not require libz", apexManifestRule.Args["requireNativeLibs"], "")
4310 }
4311}
4312
Alex Light9670d332019-01-29 18:07:33 -08004313func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004314 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004315 apex {
4316 name: "myapex",
4317 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004318 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004319 multilib: {
4320 first: {
4321 native_shared_libs: ["mylib_common"],
4322 }
4323 },
4324 target: {
4325 android: {
4326 multilib: {
4327 first: {
4328 native_shared_libs: ["mylib"],
4329 }
4330 }
4331 },
4332 host: {
4333 multilib: {
4334 first: {
4335 native_shared_libs: ["mylib2"],
4336 }
4337 }
4338 }
4339 }
4340 }
4341
4342 apex_key {
4343 name: "myapex.key",
4344 public_key: "testkey.avbpubkey",
4345 private_key: "testkey.pem",
4346 }
4347
4348 cc_library {
4349 name: "mylib",
4350 srcs: ["mylib.cpp"],
4351 system_shared_libs: [],
4352 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004353 // TODO: remove //apex_available:platform
4354 apex_available: [
4355 "//apex_available:platform",
4356 "myapex",
4357 ],
Alex Light9670d332019-01-29 18:07:33 -08004358 }
4359
4360 cc_library {
4361 name: "mylib_common",
4362 srcs: ["mylib.cpp"],
4363 system_shared_libs: [],
4364 stl: "none",
4365 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004366 // TODO: remove //apex_available:platform
4367 apex_available: [
4368 "//apex_available:platform",
4369 "myapex",
4370 ],
Alex Light9670d332019-01-29 18:07:33 -08004371 }
4372
4373 cc_library {
4374 name: "mylib2",
4375 srcs: ["mylib.cpp"],
4376 system_shared_libs: [],
4377 stl: "none",
4378 compile_multilib: "first",
4379 }
4380 `)
4381
Jooyung Hana0503a52023-08-23 13:12:50 +09004382 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004383 copyCmds := apexRule.Args["copy_commands"]
4384
4385 // Ensure that main rule creates an output
4386 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4387
4388 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004389 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4390 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4391 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004392
4393 // Ensure that both direct and indirect deps are copied into apex
4394 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4395 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4396 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4397
Colin Cross7113d202019-11-20 16:39:12 -08004398 // Ensure that the platform variant ends with _shared
4399 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4400 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4401 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004402}
Jiyong Park04480cf2019-02-06 00:16:29 +09004403
Jiyong Park59140302020-12-14 18:44:04 +09004404func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004405 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004406 apex {
4407 name: "myapex",
4408 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004409 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004410 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004411 arch: {
4412 arm64: {
4413 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004414 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004415 },
4416 x86_64: {
4417 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004418 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004419 },
4420 }
4421 }
4422
4423 apex_key {
4424 name: "myapex.key",
4425 public_key: "testkey.avbpubkey",
4426 private_key: "testkey.pem",
4427 }
4428
4429 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004430 name: "mylib.generic",
4431 srcs: ["mylib.cpp"],
4432 system_shared_libs: [],
4433 stl: "none",
4434 // TODO: remove //apex_available:platform
4435 apex_available: [
4436 "//apex_available:platform",
4437 "myapex",
4438 ],
4439 }
4440
4441 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004442 name: "mylib.arm64",
4443 srcs: ["mylib.cpp"],
4444 system_shared_libs: [],
4445 stl: "none",
4446 // TODO: remove //apex_available:platform
4447 apex_available: [
4448 "//apex_available:platform",
4449 "myapex",
4450 ],
4451 }
4452
4453 cc_library {
4454 name: "mylib.x64",
4455 srcs: ["mylib.cpp"],
4456 system_shared_libs: [],
4457 stl: "none",
4458 // TODO: remove //apex_available:platform
4459 apex_available: [
4460 "//apex_available:platform",
4461 "myapex",
4462 ],
4463 }
4464 `)
4465
Jooyung Hana0503a52023-08-23 13:12:50 +09004466 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park59140302020-12-14 18:44:04 +09004467 copyCmds := apexRule.Args["copy_commands"]
4468
4469 // Ensure that apex variant is created for the direct dep
4470 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004471 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004472 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4473
4474 // Ensure that both direct and indirect deps are copied into apex
4475 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4476 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4477}
4478
Jiyong Park04480cf2019-02-06 00:16:29 +09004479func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004480 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004481 apex {
4482 name: "myapex",
4483 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004484 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004485 updatable: false,
Riya Thakur654461c2024-02-27 07:21:05 +00004486 compile_multilib: "both",
Jiyong Park04480cf2019-02-06 00:16:29 +09004487 }
4488
4489 apex_key {
4490 name: "myapex.key",
4491 public_key: "testkey.avbpubkey",
4492 private_key: "testkey.pem",
4493 }
4494
4495 sh_binary {
4496 name: "myscript",
4497 src: "mylib.cpp",
4498 filename: "myscript.sh",
4499 sub_dir: "script",
4500 }
4501 `)
4502
Jooyung Hana0503a52023-08-23 13:12:50 +09004503 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004504 copyCmds := apexRule.Args["copy_commands"]
4505
4506 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4507}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004508
Jooyung Han91df2082019-11-20 01:49:42 +09004509func TestApexInVariousPartition(t *testing.T) {
4510 testcases := []struct {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004511 propName, partition string
Jooyung Han91df2082019-11-20 01:49:42 +09004512 }{
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004513 {"", "system"},
4514 {"product_specific: true", "product"},
4515 {"soc_specific: true", "vendor"},
4516 {"proprietary: true", "vendor"},
4517 {"vendor: true", "vendor"},
4518 {"system_ext_specific: true", "system_ext"},
Jooyung Han91df2082019-11-20 01:49:42 +09004519 }
4520 for _, tc := range testcases {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004521 t.Run(tc.propName+":"+tc.partition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004522 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004523 apex {
4524 name: "myapex",
4525 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004526 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004527 `+tc.propName+`
4528 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004529
Jooyung Han91df2082019-11-20 01:49:42 +09004530 apex_key {
4531 name: "myapex.key",
4532 public_key: "testkey.avbpubkey",
4533 private_key: "testkey.pem",
4534 }
4535 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004536
Jooyung Hana0503a52023-08-23 13:12:50 +09004537 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004538 expected := "out/soong/target/product/test_device/" + tc.partition + "/apex"
Paul Duffin37ba3442021-03-29 00:21:08 +01004539 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004540 if actual != expected {
4541 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4542 }
Jooyung Han91df2082019-11-20 01:49:42 +09004543 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004544 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004545}
Jiyong Park67882562019-03-21 01:11:21 +09004546
Jooyung Han580eb4f2020-06-24 19:33:06 +09004547func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004548 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004549 apex {
4550 name: "myapex",
4551 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004552 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004553 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004554
Jooyung Han580eb4f2020-06-24 19:33:06 +09004555 apex_key {
4556 name: "myapex.key",
4557 public_key: "testkey.avbpubkey",
4558 private_key: "testkey.pem",
4559 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004560 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09004561 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004562 rule := module.Output("file_contexts")
4563 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4564}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004565
Jooyung Han580eb4f2020-06-24 19:33:06 +09004566func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004567 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004568 apex {
4569 name: "myapex",
4570 key: "myapex.key",
4571 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004572 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004573 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004574
Jooyung Han580eb4f2020-06-24 19:33:06 +09004575 apex_key {
4576 name: "myapex.key",
4577 public_key: "testkey.avbpubkey",
4578 private_key: "testkey.pem",
4579 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004580 `, withFiles(map[string][]byte{
4581 "my_own_file_contexts": nil,
4582 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004583}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004584
Jooyung Han580eb4f2020-06-24 19:33:06 +09004585func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004586 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004587 apex {
4588 name: "myapex",
4589 key: "myapex.key",
4590 product_specific: true,
4591 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004592 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004593 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004594
Jooyung Han580eb4f2020-06-24 19:33:06 +09004595 apex_key {
4596 name: "myapex.key",
4597 public_key: "testkey.avbpubkey",
4598 private_key: "testkey.pem",
4599 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004600 `)
4601
Colin Cross1c460562021-02-16 17:55:47 -08004602 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004603 apex {
4604 name: "myapex",
4605 key: "myapex.key",
4606 product_specific: true,
4607 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004608 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004609 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004610
Jooyung Han580eb4f2020-06-24 19:33:06 +09004611 apex_key {
4612 name: "myapex.key",
4613 public_key: "testkey.avbpubkey",
4614 private_key: "testkey.pem",
4615 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004616 `, withFiles(map[string][]byte{
4617 "product_specific_file_contexts": nil,
4618 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004619 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004620 rule := module.Output("file_contexts")
4621 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4622}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004623
Jooyung Han580eb4f2020-06-24 19:33:06 +09004624func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004625 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004626 apex {
4627 name: "myapex",
4628 key: "myapex.key",
4629 product_specific: true,
4630 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004631 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004632 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004633
Jooyung Han580eb4f2020-06-24 19:33:06 +09004634 apex_key {
4635 name: "myapex.key",
4636 public_key: "testkey.avbpubkey",
4637 private_key: "testkey.pem",
4638 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004639
Jooyung Han580eb4f2020-06-24 19:33:06 +09004640 filegroup {
4641 name: "my-file-contexts",
4642 srcs: ["product_specific_file_contexts"],
4643 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004644 `, withFiles(map[string][]byte{
4645 "product_specific_file_contexts": nil,
4646 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004647 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004648 rule := module.Output("file_contexts")
4649 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004650}
4651
Jiyong Park67882562019-03-21 01:11:21 +09004652func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004653 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004654 apex_key {
4655 name: "myapex.key",
4656 public_key: ":my.avbpubkey",
4657 private_key: ":my.pem",
4658 product_specific: true,
4659 }
4660
4661 filegroup {
4662 name: "my.avbpubkey",
4663 srcs: ["testkey2.avbpubkey"],
4664 }
4665
4666 filegroup {
4667 name: "my.pem",
4668 srcs: ["testkey2.pem"],
4669 }
4670 `)
4671
4672 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4673 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004674 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004675 if actual_pubkey != expected_pubkey {
4676 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4677 }
4678 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004679 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004680 if actual_privkey != expected_privkey {
4681 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4682 }
4683}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004684
4685func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004686 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004687 prebuilt_apex {
4688 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004689 arch: {
4690 arm64: {
4691 src: "myapex-arm64.apex",
4692 },
4693 arm: {
4694 src: "myapex-arm.apex",
4695 },
4696 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004697 }
4698 `)
4699
Wei Li340ee8e2022-03-18 17:33:24 -07004700 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4701 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004702
Jiyong Parkc95714e2019-03-29 14:23:10 +09004703 expectedInput := "myapex-arm64.apex"
4704 if prebuilt.inputApex.String() != expectedInput {
4705 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4706 }
Wei Li340ee8e2022-03-18 17:33:24 -07004707 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4708 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4709 rule := testingModule.Rule("genProvenanceMetaData")
4710 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4711 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4712 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4713 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08004714
4715 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
4716 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004717}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004718
Paul Duffinc0609c62021-03-01 17:27:16 +00004719func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004720 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004721 prebuilt_apex {
4722 name: "myapex",
4723 }
4724 `)
4725}
4726
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004727func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004728 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004729 prebuilt_apex {
4730 name: "myapex",
4731 src: "myapex-arm.apex",
4732 filename: "notmyapex.apex",
4733 }
4734 `)
4735
Wei Li340ee8e2022-03-18 17:33:24 -07004736 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4737 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004738
4739 expected := "notmyapex.apex"
4740 if p.installFilename != expected {
4741 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4742 }
Wei Li340ee8e2022-03-18 17:33:24 -07004743 rule := testingModule.Rule("genProvenanceMetaData")
4744 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4745 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4746 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4747 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004748}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004749
Samiul Islam7c02e262021-09-08 17:48:28 +01004750func TestApexSetFilenameOverride(t *testing.T) {
4751 testApex(t, `
4752 apex_set {
4753 name: "com.company.android.myapex",
4754 apex_name: "com.android.myapex",
4755 set: "company-myapex.apks",
4756 filename: "com.company.android.myapex.apex"
4757 }
4758 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4759
4760 testApex(t, `
4761 apex_set {
4762 name: "com.company.android.myapex",
4763 apex_name: "com.android.myapex",
4764 set: "company-myapex.apks",
4765 filename: "com.company.android.myapex.capex"
4766 }
4767 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4768
4769 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4770 apex_set {
4771 name: "com.company.android.myapex",
4772 apex_name: "com.android.myapex",
4773 set: "company-myapex.apks",
4774 filename: "some-random-suffix"
4775 }
4776 `)
4777}
4778
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004779func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004780 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004781 prebuilt_apex {
4782 name: "myapex.prebuilt",
4783 src: "myapex-arm.apex",
4784 overrides: [
4785 "myapex",
4786 ],
4787 }
4788 `)
4789
Wei Li340ee8e2022-03-18 17:33:24 -07004790 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4791 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004792
4793 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004794 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004795 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004796 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004797 }
Wei Li340ee8e2022-03-18 17:33:24 -07004798 rule := testingModule.Rule("genProvenanceMetaData")
4799 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4800 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4801 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4802 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004803}
4804
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004805func TestPrebuiltApexName(t *testing.T) {
4806 testApex(t, `
4807 prebuilt_apex {
4808 name: "com.company.android.myapex",
4809 apex_name: "com.android.myapex",
4810 src: "company-myapex-arm.apex",
4811 }
4812 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4813
4814 testApex(t, `
4815 apex_set {
4816 name: "com.company.android.myapex",
4817 apex_name: "com.android.myapex",
4818 set: "company-myapex.apks",
4819 }
4820 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4821}
4822
4823func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4824 _ = android.GroupFixturePreparers(
4825 java.PrepareForTestWithJavaDefaultModules,
4826 PrepareForTestWithApexBuildComponents,
4827 android.FixtureWithRootAndroidBp(`
4828 platform_bootclasspath {
4829 name: "platform-bootclasspath",
4830 fragments: [
4831 {
4832 apex: "com.android.art",
4833 module: "art-bootclasspath-fragment",
4834 },
4835 ],
4836 }
4837
4838 prebuilt_apex {
4839 name: "com.company.android.art",
4840 apex_name: "com.android.art",
4841 src: "com.company.android.art-arm.apex",
4842 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4843 }
4844
4845 prebuilt_bootclasspath_fragment {
4846 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004847 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004848 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004849 hidden_api: {
4850 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4851 metadata: "my-bootclasspath-fragment/metadata.csv",
4852 index: "my-bootclasspath-fragment/index.csv",
4853 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4854 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4855 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004856 }
4857
4858 java_import {
4859 name: "core-oj",
4860 jars: ["prebuilt.jar"],
4861 }
4862 `),
4863 ).RunTest(t)
4864}
4865
Spandan Das59a4a2b2024-01-09 21:35:56 +00004866// A minimal context object for use with DexJarBuildPath
4867type moduleErrorfTestCtx struct {
4868}
4869
4870func (ctx moduleErrorfTestCtx) ModuleErrorf(format string, args ...interface{}) {
4871}
4872
Paul Duffin092153d2021-01-26 11:42:39 +00004873// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4874// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004875func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004876 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004877
Paul Duffin89886cb2021-02-05 16:44:03 +00004878 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004879 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004880 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004881 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +00004882 dexJarBuildPath := p.DexJarBuildPath(moduleErrorfTestCtx{}).PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004883 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004884 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
Spandan Das3576e762024-01-03 18:57:03 +00004885 ".intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
Jeongik Chad5fe8782021-07-08 01:13:11 +09004886 android.NormalizePathForTesting(dexJarBuildPath))
4887 }
4888
4889 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004890 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004891 // Make sure the import has been given the correct path to the dex jar.
4892 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4893 dexJarBuildPath := p.DexJarInstallPath()
4894 stem := android.RemoveOptionalPrebuiltPrefix(name)
4895 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4896 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4897 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004898 }
4899
Paul Duffin39853512021-02-26 11:09:39 +00004900 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004901 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004902 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004903 android.AssertArrayString(t, "Check if there is no source variant",
4904 []string{"android_common"},
4905 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004906 }
4907
4908 t.Run("prebuilt only", func(t *testing.T) {
4909 bp := `
4910 prebuilt_apex {
4911 name: "myapex",
4912 arch: {
4913 arm64: {
4914 src: "myapex-arm64.apex",
4915 },
4916 arm: {
4917 src: "myapex-arm.apex",
4918 },
4919 },
Paul Duffin39853512021-02-26 11:09:39 +00004920 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004921 }
4922
4923 java_import {
4924 name: "libfoo",
4925 jars: ["libfoo.jar"],
4926 }
Paul Duffin39853512021-02-26 11:09:39 +00004927
4928 java_sdk_library_import {
4929 name: "libbar",
4930 public: {
4931 jars: ["libbar.jar"],
4932 },
4933 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004934 `
4935
4936 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4937 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4938
Spandan Das3576e762024-01-03 18:57:03 +00004939 deapexerName := deapexerModuleName("prebuilt_myapex")
4940 android.AssertStringEquals(t, "APEX module name from deapexer name", "prebuilt_myapex", apexModuleName(deapexerName))
Martin Stjernholm44825602021-09-17 01:44:12 +01004941
Paul Duffinf6932af2021-02-26 18:21:56 +00004942 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004943 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004944 rule := deapexer.Rule("deapexer")
4945 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4946 t.Errorf("expected: %q, found: %q", expected, actual)
4947 }
4948
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004949 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004950 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004951 rule = prebuiltApex.Rule("android/soong/android.Cp")
4952 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4953 t.Errorf("expected: %q, found: %q", expected, actual)
4954 }
4955
Paul Duffin89886cb2021-02-05 16:44:03 +00004956 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004957 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004958
4959 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004960 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004961 })
4962
4963 t.Run("prebuilt with source preferred", func(t *testing.T) {
4964
4965 bp := `
4966 prebuilt_apex {
4967 name: "myapex",
4968 arch: {
4969 arm64: {
4970 src: "myapex-arm64.apex",
4971 },
4972 arm: {
4973 src: "myapex-arm.apex",
4974 },
4975 },
Paul Duffin39853512021-02-26 11:09:39 +00004976 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004977 }
4978
4979 java_import {
4980 name: "libfoo",
4981 jars: ["libfoo.jar"],
4982 }
4983
4984 java_library {
4985 name: "libfoo",
4986 }
Paul Duffin39853512021-02-26 11:09:39 +00004987
4988 java_sdk_library_import {
4989 name: "libbar",
4990 public: {
4991 jars: ["libbar.jar"],
4992 },
4993 }
4994
4995 java_sdk_library {
4996 name: "libbar",
4997 srcs: ["foo/bar/MyClass.java"],
4998 unsafe_ignore_missing_latest_api: true,
4999 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005000 `
5001
5002 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5003 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5004
Paul Duffin89886cb2021-02-05 16:44:03 +00005005 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005006 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005007 ensureNoSourceVariant(t, ctx, "libfoo")
5008
5009 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005010 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005011 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005012 })
5013
5014 t.Run("prebuilt preferred with source", func(t *testing.T) {
5015 bp := `
5016 prebuilt_apex {
5017 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005018 arch: {
5019 arm64: {
5020 src: "myapex-arm64.apex",
5021 },
5022 arm: {
5023 src: "myapex-arm.apex",
5024 },
5025 },
Paul Duffin39853512021-02-26 11:09:39 +00005026 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005027 }
5028
5029 java_import {
5030 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005031 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005032 jars: ["libfoo.jar"],
5033 }
5034
5035 java_library {
5036 name: "libfoo",
5037 }
Paul Duffin39853512021-02-26 11:09:39 +00005038
5039 java_sdk_library_import {
5040 name: "libbar",
5041 prefer: true,
5042 public: {
5043 jars: ["libbar.jar"],
5044 },
5045 }
5046
5047 java_sdk_library {
5048 name: "libbar",
5049 srcs: ["foo/bar/MyClass.java"],
5050 unsafe_ignore_missing_latest_api: true,
5051 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005052 `
5053
5054 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5055 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5056
Paul Duffin89886cb2021-02-05 16:44:03 +00005057 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005058 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005059 ensureNoSourceVariant(t, ctx, "libfoo")
5060
5061 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005062 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005063 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005064 })
5065}
5066
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005067func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005068 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005069 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005070 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5071 // is disabled.
5072 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
5073 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005074
Paul Duffin37856732021-02-26 14:24:15 +00005075 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5076 t.Helper()
Jiakai Zhangc6879f32023-11-06 16:31:19 +00005077 s := ctx.ModuleForTests("dex_bootjars", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005078 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005079 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005080 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005081 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005082 foundLibfooJar = true
5083 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005084 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005085 }
5086 }
5087 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005088 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005089 }
5090 }
5091
Paul Duffin40a3f652021-07-19 13:11:24 +01005092 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005093 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005094 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005095 var rule android.TestingBuildParams
5096
5097 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5098 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005099 }
5100
Paul Duffin40a3f652021-07-19 13:11:24 +01005101 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5102 t.Helper()
5103 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5104 var rule android.TestingBuildParams
5105
5106 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5107 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5108 }
5109
Paul Duffin89f570a2021-06-16 01:42:33 +01005110 fragment := java.ApexVariantReference{
5111 Apex: proptools.StringPtr("myapex"),
5112 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5113 }
5114
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005115 t.Run("prebuilt only", func(t *testing.T) {
5116 bp := `
5117 prebuilt_apex {
5118 name: "myapex",
5119 arch: {
5120 arm64: {
5121 src: "myapex-arm64.apex",
5122 },
5123 arm: {
5124 src: "myapex-arm.apex",
5125 },
5126 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005127 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5128 }
5129
5130 prebuilt_bootclasspath_fragment {
5131 name: "my-bootclasspath-fragment",
5132 contents: ["libfoo", "libbar"],
5133 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005134 hidden_api: {
5135 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5136 metadata: "my-bootclasspath-fragment/metadata.csv",
5137 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005138 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5139 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5140 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005141 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005142 }
5143
5144 java_import {
5145 name: "libfoo",
5146 jars: ["libfoo.jar"],
5147 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005148 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005149 }
Paul Duffin37856732021-02-26 14:24:15 +00005150
5151 java_sdk_library_import {
5152 name: "libbar",
5153 public: {
5154 jars: ["libbar.jar"],
5155 },
5156 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005157 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005158 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005159 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005160 `
5161
Paul Duffin89f570a2021-06-16 01:42:33 +01005162 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005163 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5164 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005165
Paul Duffin537ea3d2021-05-14 10:38:00 +01005166 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005167 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005168 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005169 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005170 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005171 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005172 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005173 })
5174
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005175 t.Run("apex_set only", func(t *testing.T) {
5176 bp := `
5177 apex_set {
5178 name: "myapex",
5179 set: "myapex.apks",
Liz Kammer2dc72442023-04-20 10:10:48 -04005180 exported_java_libs: ["myjavalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005181 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
Liz Kammer2dc72442023-04-20 10:10:48 -04005182 exported_systemserverclasspath_fragments: ["my-systemserverclasspath-fragment"],
5183 }
5184
5185 java_import {
5186 name: "myjavalib",
5187 jars: ["myjavalib.jar"],
5188 apex_available: ["myapex"],
5189 permitted_packages: ["javalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005190 }
5191
5192 prebuilt_bootclasspath_fragment {
5193 name: "my-bootclasspath-fragment",
5194 contents: ["libfoo", "libbar"],
5195 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005196 hidden_api: {
5197 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5198 metadata: "my-bootclasspath-fragment/metadata.csv",
5199 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005200 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5201 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5202 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005203 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005204 }
5205
Liz Kammer2dc72442023-04-20 10:10:48 -04005206 prebuilt_systemserverclasspath_fragment {
5207 name: "my-systemserverclasspath-fragment",
5208 contents: ["libbaz"],
5209 apex_available: ["myapex"],
5210 }
5211
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005212 java_import {
5213 name: "libfoo",
5214 jars: ["libfoo.jar"],
5215 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005216 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005217 }
5218
5219 java_sdk_library_import {
5220 name: "libbar",
5221 public: {
5222 jars: ["libbar.jar"],
5223 },
5224 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005225 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005226 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005227 }
Liz Kammer2dc72442023-04-20 10:10:48 -04005228
5229 java_sdk_library_import {
5230 name: "libbaz",
5231 public: {
5232 jars: ["libbaz.jar"],
5233 },
5234 apex_available: ["myapex"],
5235 shared_library: false,
5236 permitted_packages: ["baz"],
5237 }
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005238 `
5239
Paul Duffin89f570a2021-06-16 01:42:33 +01005240 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005241 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5242 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005243
Paul Duffin537ea3d2021-05-14 10:38:00 +01005244 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005245 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005246 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005247 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005248 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005249 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005250 `)
Liz Kammer2dc72442023-04-20 10:10:48 -04005251
5252 myApex := ctx.ModuleForTests("myapex", "android_common_myapex").Module()
5253
5254 overrideNames := []string{
Spandan Das3576e762024-01-03 18:57:03 +00005255 "myapex",
Liz Kammer2dc72442023-04-20 10:10:48 -04005256 "myjavalib.myapex",
5257 "libfoo.myapex",
5258 "libbar.myapex",
5259 "libbaz.myapex",
5260 }
5261 mkEntries := android.AndroidMkEntriesForTest(t, ctx, myApex)
5262 for i, e := range mkEntries {
5263 g := e.OverrideName
5264 if w := overrideNames[i]; w != g {
5265 t.Errorf("Expected override name %q, got %q", w, g)
5266 }
5267 }
5268
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005269 })
5270
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005271 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5272 bp := `
5273 prebuilt_apex {
5274 name: "myapex",
5275 arch: {
5276 arm64: {
5277 src: "myapex-arm64.apex",
5278 },
5279 arm: {
5280 src: "myapex-arm.apex",
5281 },
5282 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005283 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5284 }
5285
5286 prebuilt_bootclasspath_fragment {
5287 name: "my-bootclasspath-fragment",
5288 contents: ["libfoo", "libbar"],
5289 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005290 hidden_api: {
5291 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5292 metadata: "my-bootclasspath-fragment/metadata.csv",
5293 index: "my-bootclasspath-fragment/index.csv",
5294 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5295 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5296 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005297 }
5298
5299 java_import {
5300 name: "libfoo",
5301 jars: ["libfoo.jar"],
5302 apex_available: ["myapex"],
5303 }
5304
5305 java_library {
5306 name: "libfoo",
5307 srcs: ["foo/bar/MyClass.java"],
5308 apex_available: ["myapex"],
5309 }
Paul Duffin37856732021-02-26 14:24:15 +00005310
5311 java_sdk_library_import {
5312 name: "libbar",
5313 public: {
5314 jars: ["libbar.jar"],
5315 },
5316 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005317 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005318 }
5319
5320 java_sdk_library {
5321 name: "libbar",
5322 srcs: ["foo/bar/MyClass.java"],
5323 unsafe_ignore_missing_latest_api: true,
5324 apex_available: ["myapex"],
5325 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005326 `
5327
5328 // In this test the source (java_library) libfoo is active since the
5329 // prebuilt (java_import) defaults to prefer:false. However the
5330 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5331 // find the dex boot jar in it. We either need to disable the source libfoo
5332 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005333 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005334 // dexbootjar check is skipped if AllowMissingDependencies is true
5335 preparerAllowMissingDeps := android.GroupFixturePreparers(
5336 preparer,
5337 android.PrepareForTestWithAllowMissingDependencies,
5338 )
5339 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005340 })
5341
5342 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5343 bp := `
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005344 apex {
5345 name: "myapex",
5346 key: "myapex.key",
5347 updatable: false,
5348 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5349 }
5350
5351 apex_key {
5352 name: "myapex.key",
5353 public_key: "testkey.avbpubkey",
5354 private_key: "testkey.pem",
5355 }
5356
5357 bootclasspath_fragment {
5358 name: "my-bootclasspath-fragment",
5359 contents: ["libfoo", "libbar"],
5360 apex_available: ["myapex"],
5361 hidden_api: {
5362 split_packages: ["*"],
5363 },
5364 }
5365
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005366 prebuilt_apex {
5367 name: "myapex",
5368 arch: {
5369 arm64: {
5370 src: "myapex-arm64.apex",
5371 },
5372 arm: {
5373 src: "myapex-arm.apex",
5374 },
5375 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005376 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5377 }
5378
5379 prebuilt_bootclasspath_fragment {
5380 name: "my-bootclasspath-fragment",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005381 prefer: true,
Paul Duffin89f570a2021-06-16 01:42:33 +01005382 contents: ["libfoo", "libbar"],
5383 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005384 hidden_api: {
5385 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5386 metadata: "my-bootclasspath-fragment/metadata.csv",
5387 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005388 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5389 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5390 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005391 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005392 }
5393
5394 java_import {
5395 name: "libfoo",
5396 prefer: true,
5397 jars: ["libfoo.jar"],
5398 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005399 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005400 }
5401
5402 java_library {
5403 name: "libfoo",
5404 srcs: ["foo/bar/MyClass.java"],
5405 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005406 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005407 }
Paul Duffin37856732021-02-26 14:24:15 +00005408
5409 java_sdk_library_import {
5410 name: "libbar",
5411 prefer: true,
5412 public: {
5413 jars: ["libbar.jar"],
5414 },
5415 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005416 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005417 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005418 }
5419
5420 java_sdk_library {
5421 name: "libbar",
5422 srcs: ["foo/bar/MyClass.java"],
5423 unsafe_ignore_missing_latest_api: true,
5424 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005425 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005426 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005427 `
5428
Paul Duffin89f570a2021-06-16 01:42:33 +01005429 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005430 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5431 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005432
Paul Duffin537ea3d2021-05-14 10:38:00 +01005433 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005434 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005435 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005436 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005437 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005438 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005439 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005440 })
5441
5442 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5443 bp := `
5444 apex {
5445 name: "myapex",
5446 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005447 updatable: false,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005448 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005449 }
5450
5451 apex_key {
5452 name: "myapex.key",
5453 public_key: "testkey.avbpubkey",
5454 private_key: "testkey.pem",
5455 }
5456
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005457 bootclasspath_fragment {
5458 name: "my-bootclasspath-fragment",
5459 contents: ["libfoo", "libbar"],
5460 apex_available: ["myapex"],
5461 hidden_api: {
5462 split_packages: ["*"],
5463 },
5464 }
5465
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005466 prebuilt_apex {
5467 name: "myapex",
5468 arch: {
5469 arm64: {
5470 src: "myapex-arm64.apex",
5471 },
5472 arm: {
5473 src: "myapex-arm.apex",
5474 },
5475 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005476 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5477 }
5478
5479 prebuilt_bootclasspath_fragment {
5480 name: "my-bootclasspath-fragment",
5481 contents: ["libfoo", "libbar"],
5482 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005483 hidden_api: {
5484 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5485 metadata: "my-bootclasspath-fragment/metadata.csv",
5486 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005487 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5488 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5489 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005490 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005491 }
5492
5493 java_import {
5494 name: "libfoo",
5495 jars: ["libfoo.jar"],
5496 apex_available: ["myapex"],
5497 }
5498
5499 java_library {
5500 name: "libfoo",
5501 srcs: ["foo/bar/MyClass.java"],
5502 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005503 permitted_packages: ["foo"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005504 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005505 }
Paul Duffin37856732021-02-26 14:24:15 +00005506
5507 java_sdk_library_import {
5508 name: "libbar",
5509 public: {
5510 jars: ["libbar.jar"],
5511 },
5512 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005513 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005514 }
5515
5516 java_sdk_library {
5517 name: "libbar",
5518 srcs: ["foo/bar/MyClass.java"],
5519 unsafe_ignore_missing_latest_api: true,
5520 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005521 permitted_packages: ["bar"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005522 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005523 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005524 `
5525
Paul Duffin89f570a2021-06-16 01:42:33 +01005526 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Jiakai Zhangc6879f32023-11-06 16:31:19 +00005527 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/hiddenapi-modular/encoded/libfoo.jar")
5528 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/hiddenapi-modular/encoded/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005529
Paul Duffin537ea3d2021-05-14 10:38:00 +01005530 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005531 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005532 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
5533 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005534 out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/modular-hiddenapi/index.csv
5535 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005536 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005537 })
5538
5539 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5540 bp := `
5541 apex {
5542 name: "myapex",
5543 enabled: false,
5544 key: "myapex.key",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005545 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005546 }
5547
5548 apex_key {
5549 name: "myapex.key",
5550 public_key: "testkey.avbpubkey",
5551 private_key: "testkey.pem",
5552 }
5553
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005554 bootclasspath_fragment {
5555 name: "my-bootclasspath-fragment",
5556 enabled: false,
5557 contents: ["libfoo", "libbar"],
5558 apex_available: ["myapex"],
5559 hidden_api: {
5560 split_packages: ["*"],
5561 },
5562 }
5563
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005564 prebuilt_apex {
5565 name: "myapex",
5566 arch: {
5567 arm64: {
5568 src: "myapex-arm64.apex",
5569 },
5570 arm: {
5571 src: "myapex-arm.apex",
5572 },
5573 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005574 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5575 }
5576
5577 prebuilt_bootclasspath_fragment {
5578 name: "my-bootclasspath-fragment",
5579 contents: ["libfoo", "libbar"],
5580 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005581 hidden_api: {
5582 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5583 metadata: "my-bootclasspath-fragment/metadata.csv",
5584 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005585 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5586 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5587 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005588 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005589 }
5590
5591 java_import {
5592 name: "libfoo",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005593 jars: ["libfoo.jar"],
5594 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005595 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005596 }
5597
5598 java_library {
5599 name: "libfoo",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005600 enabled: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005601 srcs: ["foo/bar/MyClass.java"],
5602 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005603 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005604 }
Paul Duffin37856732021-02-26 14:24:15 +00005605
5606 java_sdk_library_import {
5607 name: "libbar",
Paul Duffin37856732021-02-26 14:24:15 +00005608 public: {
5609 jars: ["libbar.jar"],
5610 },
5611 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005612 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005613 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005614 }
5615
5616 java_sdk_library {
5617 name: "libbar",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005618 enabled: false,
Paul Duffin37856732021-02-26 14:24:15 +00005619 srcs: ["foo/bar/MyClass.java"],
5620 unsafe_ignore_missing_latest_api: true,
5621 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005622 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005623 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005624 `
5625
Paul Duffin89f570a2021-06-16 01:42:33 +01005626 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005627 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5628 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005629
Paul Duffin537ea3d2021-05-14 10:38:00 +01005630 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005631 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005632 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005633 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005634 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005635 out/soong/.intermediates/packages/modules/com.android.art/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005636 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005637 })
Spandan Das3a392012024-01-17 18:26:27 +00005638
Spandan Dasf2c10572024-02-27 04:49:52 +00005639 t.Run("Co-existing unflagged apexes should create a duplicate module error", func(t *testing.T) {
Spandan Das3a392012024-01-17 18:26:27 +00005640 bp := `
5641 // Source
5642 apex {
5643 name: "myapex",
5644 enabled: false,
5645 key: "myapex.key",
5646 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5647 }
5648
5649 apex_key {
5650 name: "myapex.key",
5651 public_key: "testkey.avbpubkey",
5652 private_key: "testkey.pem",
5653 }
5654
5655 // Prebuilt
5656 prebuilt_apex {
5657 name: "myapex.v1",
5658 source_apex_name: "myapex",
5659 arch: {
5660 arm64: {
5661 src: "myapex-arm64.apex",
5662 },
5663 arm: {
5664 src: "myapex-arm.apex",
5665 },
5666 },
5667 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5668 prefer: true,
5669 }
5670 prebuilt_apex {
5671 name: "myapex.v2",
5672 source_apex_name: "myapex",
5673 arch: {
5674 arm64: {
5675 src: "myapex-arm64.apex",
5676 },
5677 arm: {
5678 src: "myapex-arm.apex",
5679 },
5680 },
5681 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5682 prefer: true,
5683 }
5684
5685 prebuilt_bootclasspath_fragment {
5686 name: "my-bootclasspath-fragment",
5687 contents: ["libfoo", "libbar"],
5688 apex_available: ["myapex"],
5689 hidden_api: {
5690 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5691 metadata: "my-bootclasspath-fragment/metadata.csv",
5692 index: "my-bootclasspath-fragment/index.csv",
5693 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5694 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5695 },
5696 prefer: true,
5697 }
5698
5699 java_import {
5700 name: "libfoo",
5701 jars: ["libfoo.jar"],
5702 apex_available: ["myapex"],
5703 prefer: true,
5704 }
5705 java_import {
5706 name: "libbar",
5707 jars: ["libbar.jar"],
5708 apex_available: ["myapex"],
5709 prefer: true,
5710 }
5711 `
5712
Spandan Dasf2c10572024-02-27 04:49:52 +00005713 testDexpreoptWithApexes(t, bp, "Multiple prebuilt modules prebuilt_myapex.v1 and prebuilt_myapex.v2 have been marked as preferred for this source module", preparer, fragment)
Spandan Das3a392012024-01-17 18:26:27 +00005714 })
5715
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005716}
5717
Roland Levillain630846d2019-06-26 12:48:34 +01005718func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005719 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005720 apex_test {
5721 name: "myapex",
5722 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005723 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005724 tests: [
5725 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005726 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005727 ],
5728 }
5729
5730 apex_key {
5731 name: "myapex.key",
5732 public_key: "testkey.avbpubkey",
5733 private_key: "testkey.pem",
5734 }
5735
Liz Kammer1c14a212020-05-12 15:26:55 -07005736 filegroup {
5737 name: "fg",
5738 srcs: [
5739 "baz",
5740 "bar/baz"
5741 ],
5742 }
5743
Roland Levillain630846d2019-06-26 12:48:34 +01005744 cc_test {
5745 name: "mytest",
5746 gtest: false,
5747 srcs: ["mytest.cpp"],
5748 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005749 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005750 system_shared_libs: [],
5751 static_executable: true,
5752 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005753 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005754 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005755
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005756 cc_library {
5757 name: "mylib",
5758 srcs: ["mylib.cpp"],
5759 system_shared_libs: [],
5760 stl: "none",
5761 }
5762
Liz Kammer5bd365f2020-05-27 15:15:11 -07005763 filegroup {
5764 name: "fg2",
5765 srcs: [
5766 "testdata/baz"
5767 ],
5768 }
5769
Roland Levillain9b5fde92019-06-28 15:41:19 +01005770 cc_test {
5771 name: "mytests",
5772 gtest: false,
5773 srcs: [
5774 "mytest1.cpp",
5775 "mytest2.cpp",
5776 "mytest3.cpp",
5777 ],
5778 test_per_src: true,
5779 relative_install_path: "test",
5780 system_shared_libs: [],
5781 static_executable: true,
5782 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005783 data: [
5784 ":fg",
5785 ":fg2",
5786 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005787 }
Roland Levillain630846d2019-06-26 12:48:34 +01005788 `)
5789
Jooyung Hana0503a52023-08-23 13:12:50 +09005790 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005791 copyCmds := apexRule.Args["copy_commands"]
5792
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005793 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005794 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005795 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005796
Liz Kammer1c14a212020-05-12 15:26:55 -07005797 //Ensure that test data are copied into apex.
5798 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5799 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5800
Roland Levillain9b5fde92019-06-28 15:41:19 +01005801 // Ensure that test deps built with `test_per_src` are copied into apex.
5802 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5803 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5804 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005805
5806 // Ensure the module is correctly translated.
Jooyung Hana0503a52023-08-23 13:12:50 +09005807 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005808 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005809 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005810 prefix := "TARGET_"
5811 var builder strings.Builder
5812 data.Custom(&builder, name, prefix, "", data)
5813 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005814 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5815 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5816 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5817 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005818 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005819}
5820
Jooyung Hand48f3c32019-08-23 11:18:57 +09005821func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5822 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5823 apex {
5824 name: "myapex",
5825 key: "myapex.key",
5826 native_shared_libs: ["libfoo"],
5827 }
5828
5829 apex_key {
5830 name: "myapex.key",
5831 public_key: "testkey.avbpubkey",
5832 private_key: "testkey.pem",
5833 }
5834
5835 cc_library {
5836 name: "libfoo",
5837 stl: "none",
5838 system_shared_libs: [],
5839 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005840 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005841 }
5842 `)
5843 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5844 apex {
5845 name: "myapex",
5846 key: "myapex.key",
5847 java_libs: ["myjar"],
5848 }
5849
5850 apex_key {
5851 name: "myapex.key",
5852 public_key: "testkey.avbpubkey",
5853 private_key: "testkey.pem",
5854 }
5855
5856 java_library {
5857 name: "myjar",
5858 srcs: ["foo/bar/MyClass.java"],
5859 sdk_version: "none",
5860 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005861 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005862 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005863 }
5864 `)
5865}
5866
Bill Peckhama41a6962021-01-11 10:58:54 -08005867func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005868 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005869 apex {
5870 name: "myapex",
5871 key: "myapex.key",
5872 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005873 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005874 }
5875
5876 apex_key {
5877 name: "myapex.key",
5878 public_key: "testkey.avbpubkey",
5879 private_key: "testkey.pem",
5880 }
5881
5882 java_import {
5883 name: "myjavaimport",
5884 apex_available: ["myapex"],
5885 jars: ["my.jar"],
5886 compile_dex: true,
5887 }
5888 `)
5889
Jooyung Hana0503a52023-08-23 13:12:50 +09005890 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Bill Peckhama41a6962021-01-11 10:58:54 -08005891 apexRule := module.Rule("apexRule")
5892 copyCmds := apexRule.Args["copy_commands"]
5893 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5894}
5895
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005896func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005897 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005898 apex {
5899 name: "myapex",
5900 key: "myapex.key",
5901 apps: [
5902 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005903 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005904 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005905 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005906 }
5907
5908 apex_key {
5909 name: "myapex.key",
5910 public_key: "testkey.avbpubkey",
5911 private_key: "testkey.pem",
5912 }
5913
5914 android_app {
5915 name: "AppFoo",
5916 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005917 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005918 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005919 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005920 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005921 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005922 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005923
5924 android_app {
5925 name: "AppFooPriv",
5926 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005927 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005928 system_modules: "none",
5929 privileged: true,
Sam Delmerico15809f82023-05-15 17:21:47 -04005930 privapp_allowlist: "privapp_allowlist_com.android.AppFooPriv.xml",
Colin Cross094cde42020-02-15 10:38:00 -08005931 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005932 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005933 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005934
5935 cc_library_shared {
5936 name: "libjni",
5937 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005938 shared_libs: ["libfoo"],
5939 stl: "none",
5940 system_shared_libs: [],
5941 apex_available: [ "myapex" ],
5942 sdk_version: "current",
5943 }
5944
5945 cc_library_shared {
5946 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005947 stl: "none",
5948 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005949 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005950 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005951 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005952 `)
5953
Jooyung Hana0503a52023-08-23 13:12:50 +09005954 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005955 apexRule := module.Rule("apexRule")
5956 copyCmds := apexRule.Args["copy_commands"]
5957
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005958 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5959 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Andrei Onea580636b2022-08-17 16:53:46 +00005960 ensureContains(t, copyCmds, "image.apex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005961
Colin Crossaede88c2020-08-11 12:17:01 -07005962 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005963 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005964 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005965 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005966 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005967 // JNI libraries including transitive deps are
5968 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005969 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005970 // ... embedded inside APK (jnilibs.zip)
5971 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5972 // ... and not directly inside the APEX
5973 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5974 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04005975
5976 apexBundle := module.Module().(*apexBundle)
5977 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
5978 var builder strings.Builder
5979 data.Custom(&builder, apexBundle.Name(), "TARGET_", "", data)
5980 androidMk := builder.String()
5981 ensureContains(t, androidMk, "LOCAL_MODULE := AppFooPriv.myapex")
5982 ensureContains(t, androidMk, "LOCAL_MODULE := AppFoo.myapex")
5983 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFooPriv.apk")
5984 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFoo.apk")
5985 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := \\S+AppFooPriv.apk")
5986 ensureContains(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := privapp_allowlist_com.android.AppFooPriv.xml:$(PRODUCT_OUT)/apex/myapex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Dario Frenicde2a032019-10-27 00:29:22 +01005987}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005988
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005989func TestApexWithAppImportBuildId(t *testing.T) {
5990 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5991 for _, id := range invalidBuildIds {
5992 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5993 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5994 variables.BuildId = proptools.StringPtr(id)
5995 })
5996 testApexError(t, message, `apex {
5997 name: "myapex",
5998 key: "myapex.key",
5999 apps: ["AppFooPrebuilt"],
6000 updatable: false,
6001 }
6002
6003 apex_key {
6004 name: "myapex.key",
6005 public_key: "testkey.avbpubkey",
6006 private_key: "testkey.pem",
6007 }
6008
6009 android_app_import {
6010 name: "AppFooPrebuilt",
6011 apk: "PrebuiltAppFoo.apk",
6012 presigned: true,
6013 apex_available: ["myapex"],
6014 }
6015 `, fixture)
6016 }
6017}
6018
Dario Frenicde2a032019-10-27 00:29:22 +01006019func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006020 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01006021 apex {
6022 name: "myapex",
6023 key: "myapex.key",
6024 apps: [
6025 "AppFooPrebuilt",
6026 "AppFooPrivPrebuilt",
6027 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006028 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01006029 }
6030
6031 apex_key {
6032 name: "myapex.key",
6033 public_key: "testkey.avbpubkey",
6034 private_key: "testkey.pem",
6035 }
6036
6037 android_app_import {
6038 name: "AppFooPrebuilt",
6039 apk: "PrebuiltAppFoo.apk",
6040 presigned: true,
6041 dex_preopt: {
6042 enabled: false,
6043 },
Jiyong Park592a6a42020-04-21 22:34:28 +09006044 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006045 }
6046
6047 android_app_import {
6048 name: "AppFooPrivPrebuilt",
6049 apk: "PrebuiltAppFooPriv.apk",
6050 privileged: true,
6051 presigned: true,
6052 dex_preopt: {
6053 enabled: false,
6054 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006055 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006056 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006057 }
6058 `)
6059
Jooyung Hana0503a52023-08-23 13:12:50 +09006060 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Frenicde2a032019-10-27 00:29:22 +01006061 apexRule := module.Rule("apexRule")
6062 copyCmds := apexRule.Args["copy_commands"]
6063
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006064 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6065 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006066}
6067
6068func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006069 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006070 apex {
6071 name: "myapex",
6072 key: "myapex.key",
6073 apps: [
6074 "AppFoo",
6075 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006076 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006077 }
6078
6079 apex_key {
6080 name: "myapex.key",
6081 public_key: "testkey.avbpubkey",
6082 private_key: "testkey.pem",
6083 }
6084
6085 android_app {
6086 name: "AppFoo",
6087 srcs: ["foo/bar/MyClass.java"],
6088 sdk_version: "none",
6089 system_modules: "none",
6090 apex_available: [ "myapex" ],
6091 }
6092
6093 android_app_import {
6094 name: "AppFoo",
6095 apk: "AppFooPrebuilt.apk",
6096 filename: "AppFooPrebuilt.apk",
6097 presigned: true,
6098 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006099 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006100 }
6101 `, withFiles(map[string][]byte{
6102 "AppFooPrebuilt.apk": nil,
6103 }))
6104
Jooyung Hana0503a52023-08-23 13:12:50 +09006105 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006106 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006107 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006108}
6109
Dario Freni6f3937c2019-12-20 22:58:03 +00006110func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006111 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006112 apex {
6113 name: "myapex",
6114 key: "myapex.key",
6115 apps: [
6116 "TesterHelpAppFoo",
6117 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006118 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006119 }
6120
6121 apex_key {
6122 name: "myapex.key",
6123 public_key: "testkey.avbpubkey",
6124 private_key: "testkey.pem",
6125 }
6126
6127 android_test_helper_app {
6128 name: "TesterHelpAppFoo",
6129 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006130 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006131 }
6132
6133 `)
6134
Jooyung Hana0503a52023-08-23 13:12:50 +09006135 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Freni6f3937c2019-12-20 22:58:03 +00006136 apexRule := module.Rule("apexRule")
6137 copyCmds := apexRule.Args["copy_commands"]
6138
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006139 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006140}
6141
Jooyung Han18020ea2019-11-13 10:50:48 +09006142func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6143 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006144 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006145 apex {
6146 name: "myapex",
6147 key: "myapex.key",
6148 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006149 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006150 }
6151
6152 apex_key {
6153 name: "myapex.key",
6154 public_key: "testkey.avbpubkey",
6155 private_key: "testkey.pem",
6156 }
6157
6158 apex {
6159 name: "otherapex",
6160 key: "myapex.key",
6161 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006162 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006163 }
6164
6165 cc_defaults {
6166 name: "libfoo-defaults",
6167 apex_available: ["otherapex"],
6168 }
6169
6170 cc_library {
6171 name: "libfoo",
6172 defaults: ["libfoo-defaults"],
6173 stl: "none",
6174 system_shared_libs: [],
6175 }`)
6176}
6177
Paul Duffine52e66f2020-03-30 17:54:29 +01006178func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006179 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006180 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006181 apex {
6182 name: "myapex",
6183 key: "myapex.key",
6184 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006185 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006186 }
6187
6188 apex_key {
6189 name: "myapex.key",
6190 public_key: "testkey.avbpubkey",
6191 private_key: "testkey.pem",
6192 }
6193
6194 apex {
6195 name: "otherapex",
6196 key: "otherapex.key",
6197 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006198 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006199 }
6200
6201 apex_key {
6202 name: "otherapex.key",
6203 public_key: "testkey.avbpubkey",
6204 private_key: "testkey.pem",
6205 }
6206
6207 cc_library {
6208 name: "libfoo",
6209 stl: "none",
6210 system_shared_libs: [],
6211 apex_available: ["otherapex"],
6212 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006213}
Jiyong Park127b40b2019-09-30 16:04:35 +09006214
Paul Duffine52e66f2020-03-30 17:54:29 +01006215func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006216 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006217 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006218.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006219.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006220.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006221.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006222.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006223.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006224 apex {
6225 name: "myapex",
6226 key: "myapex.key",
6227 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006228 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006229 }
6230
6231 apex_key {
6232 name: "myapex.key",
6233 public_key: "testkey.avbpubkey",
6234 private_key: "testkey.pem",
6235 }
6236
Jiyong Park127b40b2019-09-30 16:04:35 +09006237 cc_library {
6238 name: "libfoo",
6239 stl: "none",
6240 shared_libs: ["libbar"],
6241 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006242 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006243 }
6244
6245 cc_library {
6246 name: "libbar",
6247 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006248 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006249 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006250 apex_available: ["myapex"],
6251 }
6252
6253 cc_library {
6254 name: "libbaz",
6255 stl: "none",
6256 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006257 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006258}
Jiyong Park127b40b2019-09-30 16:04:35 +09006259
Liz Kammer5f108fa2023-05-11 14:33:17 -04006260func TestApexAvailable_IndirectStaticDep(t *testing.T) {
6261 testApex(t, `
6262 apex {
6263 name: "myapex",
6264 key: "myapex.key",
6265 native_shared_libs: ["libfoo"],
6266 updatable: false,
6267 }
6268
6269 apex_key {
6270 name: "myapex.key",
6271 public_key: "testkey.avbpubkey",
6272 private_key: "testkey.pem",
6273 }
6274
6275 cc_library {
6276 name: "libfoo",
6277 stl: "none",
6278 static_libs: ["libbar"],
6279 system_shared_libs: [],
6280 apex_available: ["myapex"],
6281 }
6282
6283 cc_library {
6284 name: "libbar",
6285 stl: "none",
6286 shared_libs: ["libbaz"],
6287 system_shared_libs: [],
6288 apex_available: ["myapex"],
6289 }
6290
6291 cc_library {
6292 name: "libbaz",
6293 stl: "none",
6294 system_shared_libs: [],
6295 }`)
6296
6297 testApexError(t, `requires "libbar" that doesn't list the APEX under 'apex_available'.`, `
6298 apex {
6299 name: "myapex",
6300 key: "myapex.key",
6301 native_shared_libs: ["libfoo"],
6302 updatable: false,
6303 }
6304
6305 apex_key {
6306 name: "myapex.key",
6307 public_key: "testkey.avbpubkey",
6308 private_key: "testkey.pem",
6309 }
6310
6311 cc_library {
6312 name: "libfoo",
6313 stl: "none",
6314 static_libs: ["libbar"],
6315 system_shared_libs: [],
6316 apex_available: ["myapex"],
6317 }
6318
6319 cc_library {
6320 name: "libbar",
6321 stl: "none",
6322 system_shared_libs: [],
6323 }`)
6324}
6325
Paul Duffine52e66f2020-03-30 17:54:29 +01006326func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006327 testApexError(t, "\"otherapex\" is not a valid module name", `
6328 apex {
6329 name: "myapex",
6330 key: "myapex.key",
6331 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006332 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006333 }
6334
6335 apex_key {
6336 name: "myapex.key",
6337 public_key: "testkey.avbpubkey",
6338 private_key: "testkey.pem",
6339 }
6340
6341 cc_library {
6342 name: "libfoo",
6343 stl: "none",
6344 system_shared_libs: [],
6345 apex_available: ["otherapex"],
6346 }`)
6347
Paul Duffine52e66f2020-03-30 17:54:29 +01006348 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006349 apex {
6350 name: "myapex",
6351 key: "myapex.key",
6352 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006353 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006354 }
6355
6356 apex_key {
6357 name: "myapex.key",
6358 public_key: "testkey.avbpubkey",
6359 private_key: "testkey.pem",
6360 }
6361
6362 cc_library {
6363 name: "libfoo",
6364 stl: "none",
6365 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006366 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006367 apex_available: ["myapex"],
6368 }
6369
6370 cc_library {
6371 name: "libbar",
6372 stl: "none",
6373 system_shared_libs: [],
6374 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006375 }
6376
6377 cc_library {
6378 name: "libbaz",
6379 stl: "none",
6380 system_shared_libs: [],
6381 stubs: {
6382 versions: ["10", "20", "30"],
6383 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006384 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006385}
Jiyong Park127b40b2019-09-30 16:04:35 +09006386
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006387func TestApexAvailable_ApexAvailableNameWithVersionCodeError(t *testing.T) {
6388 t.Run("negative variant_version produces error", func(t *testing.T) {
6389 testApexError(t, "expected an integer between 0-9; got -1", `
6390 apex {
6391 name: "myapex",
6392 key: "myapex.key",
6393 apex_available_name: "com.android.foo",
6394 variant_version: "-1",
6395 updatable: false,
6396 }
6397 apex_key {
6398 name: "myapex.key",
6399 public_key: "testkey.avbpubkey",
6400 private_key: "testkey.pem",
6401 }
6402 `)
6403 })
6404
6405 t.Run("variant_version greater than 9 produces error", func(t *testing.T) {
6406 testApexError(t, "expected an integer between 0-9; got 10", `
6407 apex {
6408 name: "myapex",
6409 key: "myapex.key",
6410 apex_available_name: "com.android.foo",
6411 variant_version: "10",
6412 updatable: false,
6413 }
6414 apex_key {
6415 name: "myapex.key",
6416 public_key: "testkey.avbpubkey",
6417 private_key: "testkey.pem",
6418 }
6419 `)
6420 })
6421}
6422
6423func TestApexAvailable_ApexAvailableNameWithVersionCode(t *testing.T) {
6424 context := android.GroupFixturePreparers(
6425 android.PrepareForIntegrationTestWithAndroid,
6426 PrepareForTestWithApexBuildComponents,
6427 android.FixtureMergeMockFs(android.MockFS{
6428 "system/sepolicy/apex/foo-file_contexts": nil,
6429 "system/sepolicy/apex/bar-file_contexts": nil,
6430 }),
6431 )
6432 result := context.RunTestWithBp(t, `
6433 apex {
6434 name: "foo",
6435 key: "myapex.key",
6436 apex_available_name: "com.android.foo",
6437 variant_version: "0",
6438 updatable: false,
6439 }
6440 apex {
6441 name: "bar",
6442 key: "myapex.key",
6443 apex_available_name: "com.android.foo",
6444 variant_version: "3",
6445 updatable: false,
6446 }
6447 apex_key {
6448 name: "myapex.key",
6449 public_key: "testkey.avbpubkey",
6450 private_key: "testkey.pem",
6451 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006452 override_apex {
6453 name: "myoverrideapex",
6454 base: "bar",
6455 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006456 `)
6457
Jooyung Hana0503a52023-08-23 13:12:50 +09006458 fooManifestRule := result.ModuleForTests("foo", "android_common_foo").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006459 fooExpectedDefaultVersion := android.DefaultUpdatableModuleVersion
6460 fooActualDefaultVersion := fooManifestRule.Args["default_version"]
6461 if fooActualDefaultVersion != fooExpectedDefaultVersion {
6462 t.Errorf("expected to find defaultVersion %q; got %q", fooExpectedDefaultVersion, fooActualDefaultVersion)
6463 }
6464
Jooyung Hana0503a52023-08-23 13:12:50 +09006465 barManifestRule := result.ModuleForTests("bar", "android_common_bar").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006466 defaultVersionInt, _ := strconv.Atoi(android.DefaultUpdatableModuleVersion)
6467 barExpectedDefaultVersion := fmt.Sprint(defaultVersionInt + 3)
6468 barActualDefaultVersion := barManifestRule.Args["default_version"]
6469 if barActualDefaultVersion != barExpectedDefaultVersion {
6470 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6471 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006472
Jooyung Hana0503a52023-08-23 13:12:50 +09006473 overrideBarManifestRule := result.ModuleForTests("bar", "android_common_myoverrideapex_bar").Rule("apexManifestRule")
Sam Delmerico419f9a32023-07-21 12:00:13 -04006474 overrideBarActualDefaultVersion := overrideBarManifestRule.Args["default_version"]
6475 if overrideBarActualDefaultVersion != barExpectedDefaultVersion {
6476 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6477 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006478}
6479
Sam Delmericoca816532023-06-02 14:09:50 -04006480func TestApexAvailable_ApexAvailableName(t *testing.T) {
6481 t.Run("using name of apex that sets apex_available_name is not allowed", func(t *testing.T) {
6482 testApexError(t, "Consider adding \"myapex\" to 'apex_available' property of \"AppFoo\"", `
6483 apex {
6484 name: "myapex_sminus",
6485 key: "myapex.key",
6486 apps: ["AppFoo"],
6487 apex_available_name: "myapex",
6488 updatable: false,
6489 }
6490 apex {
6491 name: "myapex",
6492 key: "myapex.key",
6493 apps: ["AppFoo"],
6494 updatable: false,
6495 }
6496 apex_key {
6497 name: "myapex.key",
6498 public_key: "testkey.avbpubkey",
6499 private_key: "testkey.pem",
6500 }
6501 android_app {
6502 name: "AppFoo",
6503 srcs: ["foo/bar/MyClass.java"],
6504 sdk_version: "none",
6505 system_modules: "none",
6506 apex_available: [ "myapex_sminus" ],
6507 }`,
6508 android.FixtureMergeMockFs(android.MockFS{
6509 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6510 }),
6511 )
6512 })
6513
6514 t.Run("apex_available_name allows module to be used in two different apexes", func(t *testing.T) {
6515 testApex(t, `
6516 apex {
6517 name: "myapex_sminus",
6518 key: "myapex.key",
6519 apps: ["AppFoo"],
6520 apex_available_name: "myapex",
6521 updatable: false,
6522 }
6523 apex {
6524 name: "myapex",
6525 key: "myapex.key",
6526 apps: ["AppFoo"],
6527 updatable: false,
6528 }
6529 apex_key {
6530 name: "myapex.key",
6531 public_key: "testkey.avbpubkey",
6532 private_key: "testkey.pem",
6533 }
6534 android_app {
6535 name: "AppFoo",
6536 srcs: ["foo/bar/MyClass.java"],
6537 sdk_version: "none",
6538 system_modules: "none",
6539 apex_available: [ "myapex" ],
6540 }`,
6541 android.FixtureMergeMockFs(android.MockFS{
6542 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6543 }),
6544 )
6545 })
6546
6547 t.Run("override_apexes work with apex_available_name", func(t *testing.T) {
6548 testApex(t, `
6549 override_apex {
6550 name: "myoverrideapex_sminus",
6551 base: "myapex_sminus",
6552 key: "myapex.key",
6553 apps: ["AppFooOverride"],
6554 }
6555 override_apex {
6556 name: "myoverrideapex",
6557 base: "myapex",
6558 key: "myapex.key",
6559 apps: ["AppFooOverride"],
6560 }
6561 apex {
6562 name: "myapex_sminus",
6563 key: "myapex.key",
6564 apps: ["AppFoo"],
6565 apex_available_name: "myapex",
6566 updatable: false,
6567 }
6568 apex {
6569 name: "myapex",
6570 key: "myapex.key",
6571 apps: ["AppFoo"],
6572 updatable: false,
6573 }
6574 apex_key {
6575 name: "myapex.key",
6576 public_key: "testkey.avbpubkey",
6577 private_key: "testkey.pem",
6578 }
6579 android_app {
6580 name: "AppFooOverride",
6581 srcs: ["foo/bar/MyClass.java"],
6582 sdk_version: "none",
6583 system_modules: "none",
6584 apex_available: [ "myapex" ],
6585 }
6586 android_app {
6587 name: "AppFoo",
6588 srcs: ["foo/bar/MyClass.java"],
6589 sdk_version: "none",
6590 system_modules: "none",
6591 apex_available: [ "myapex" ],
6592 }`,
6593 android.FixtureMergeMockFs(android.MockFS{
6594 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6595 }),
6596 )
6597 })
6598}
6599
6600func TestApexAvailable_ApexAvailableNameWithOverrides(t *testing.T) {
6601 context := android.GroupFixturePreparers(
6602 android.PrepareForIntegrationTestWithAndroid,
6603 PrepareForTestWithApexBuildComponents,
6604 java.PrepareForTestWithDexpreopt,
6605 android.FixtureMergeMockFs(android.MockFS{
6606 "system/sepolicy/apex/myapex-file_contexts": nil,
6607 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6608 }),
6609 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6610 variables.BuildId = proptools.StringPtr("buildid")
6611 }),
6612 )
6613 context.RunTestWithBp(t, `
6614 override_apex {
6615 name: "myoverrideapex_sminus",
6616 base: "myapex_sminus",
6617 }
6618 override_apex {
6619 name: "myoverrideapex",
6620 base: "myapex",
6621 }
6622 apex {
6623 name: "myapex",
6624 key: "myapex.key",
6625 apps: ["AppFoo"],
6626 updatable: false,
6627 }
6628 apex {
6629 name: "myapex_sminus",
6630 apex_available_name: "myapex",
6631 key: "myapex.key",
6632 apps: ["AppFoo_sminus"],
6633 updatable: false,
6634 }
6635 apex_key {
6636 name: "myapex.key",
6637 public_key: "testkey.avbpubkey",
6638 private_key: "testkey.pem",
6639 }
6640 android_app {
6641 name: "AppFoo",
6642 srcs: ["foo/bar/MyClass.java"],
6643 sdk_version: "none",
6644 system_modules: "none",
6645 apex_available: [ "myapex" ],
6646 }
6647 android_app {
6648 name: "AppFoo_sminus",
6649 srcs: ["foo/bar/MyClass.java"],
6650 sdk_version: "none",
6651 min_sdk_version: "29",
6652 system_modules: "none",
6653 apex_available: [ "myapex" ],
6654 }`)
6655}
6656
Jiyong Park89e850a2020-04-07 16:37:39 +09006657func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006658 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006659 apex {
6660 name: "myapex",
6661 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006662 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006663 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006664 }
6665
6666 apex_key {
6667 name: "myapex.key",
6668 public_key: "testkey.avbpubkey",
6669 private_key: "testkey.pem",
6670 }
6671
6672 cc_library {
6673 name: "libfoo",
6674 stl: "none",
6675 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006676 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006677 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006678 }
6679
6680 cc_library {
6681 name: "libfoo2",
6682 stl: "none",
6683 system_shared_libs: [],
6684 shared_libs: ["libbaz"],
6685 apex_available: ["//apex_available:platform"],
6686 }
6687
6688 cc_library {
6689 name: "libbar",
6690 stl: "none",
6691 system_shared_libs: [],
6692 apex_available: ["myapex"],
6693 }
6694
6695 cc_library {
6696 name: "libbaz",
6697 stl: "none",
6698 system_shared_libs: [],
6699 apex_available: ["myapex"],
6700 stubs: {
6701 versions: ["1"],
6702 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006703 }`)
6704
Jiyong Park89e850a2020-04-07 16:37:39 +09006705 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6706 // because it depends on libbar which isn't available to platform
6707 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6708 if libfoo.NotAvailableForPlatform() != true {
6709 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6710 }
6711
6712 // libfoo2 however can be available to platform because it depends on libbaz which provides
6713 // stubs
6714 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6715 if libfoo2.NotAvailableForPlatform() == true {
6716 t.Errorf("%q should be available to platform", libfoo2.String())
6717 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006718}
Jiyong Parka90ca002019-10-07 15:47:24 +09006719
Paul Duffine52e66f2020-03-30 17:54:29 +01006720func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006721 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006722 apex {
6723 name: "myapex",
6724 key: "myapex.key",
6725 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006726 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006727 }
6728
6729 apex_key {
6730 name: "myapex.key",
6731 public_key: "testkey.avbpubkey",
6732 private_key: "testkey.pem",
6733 }
6734
6735 cc_library {
6736 name: "libfoo",
6737 stl: "none",
6738 system_shared_libs: [],
6739 apex_available: ["myapex"],
6740 static: {
6741 apex_available: ["//apex_available:platform"],
6742 },
6743 }`)
6744
Jiyong Park89e850a2020-04-07 16:37:39 +09006745 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6746 if libfooShared.NotAvailableForPlatform() != true {
6747 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6748 }
6749 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6750 if libfooStatic.NotAvailableForPlatform() != false {
6751 t.Errorf("%q should be available to platform", libfooStatic.String())
6752 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006753}
6754
Jiyong Park5d790c32019-11-15 18:40:32 +09006755func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006756 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006757 apex {
6758 name: "myapex",
6759 key: "myapex.key",
6760 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006761 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006762 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006763 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006764 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006765 }
6766
6767 override_apex {
6768 name: "override_myapex",
6769 base: "myapex",
6770 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006771 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006772 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006773 overrides: ["unknownapex"],
Jesse Melhuishec60e252024-03-29 19:08:20 +00006774 compile_multilib: "first",
6775 multilib: {
6776 lib32: {
6777 native_shared_libs: ["mylib32"],
6778 },
6779 lib64: {
6780 native_shared_libs: ["mylib64"],
6781 },
6782 },
Baligh Uddin004d7172020-02-19 21:29:28 -08006783 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006784 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006785 key: "mynewapex.key",
6786 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006787 }
6788
6789 apex_key {
6790 name: "myapex.key",
6791 public_key: "testkey.avbpubkey",
6792 private_key: "testkey.pem",
6793 }
6794
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006795 apex_key {
6796 name: "mynewapex.key",
6797 public_key: "testkey2.avbpubkey",
6798 private_key: "testkey2.pem",
6799 }
6800
6801 android_app_certificate {
6802 name: "myapex.certificate",
6803 certificate: "testkey",
6804 }
6805
Jiyong Park5d790c32019-11-15 18:40:32 +09006806 android_app {
6807 name: "app",
6808 srcs: ["foo/bar/MyClass.java"],
6809 package_name: "foo",
6810 sdk_version: "none",
6811 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006812 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006813 }
6814
6815 override_android_app {
6816 name: "override_app",
6817 base: "app",
6818 package_name: "bar",
6819 }
markchien7c803b82021-08-26 22:10:06 +08006820
6821 bpf {
6822 name: "bpf",
6823 srcs: ["bpf.c"],
6824 }
6825
6826 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006827 name: "overrideBpf",
6828 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006829 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006830
6831 prebuilt_etc {
6832 name: "myetc",
6833 src: "myprebuilt",
6834 }
6835
6836 prebuilt_etc {
6837 name: "override_myetc",
6838 src: "override_myprebuilt",
6839 }
Jesse Melhuishec60e252024-03-29 19:08:20 +00006840
6841 cc_library {
6842 name: "mylib32",
6843 apex_available: [ "myapex" ],
6844 }
6845
6846 cc_library {
6847 name: "mylib64",
6848 apex_available: [ "myapex" ],
6849 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006850 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006851
Jooyung Hana0503a52023-08-23 13:12:50 +09006852 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(android.OverridableModule)
6853 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex").Module().(android.OverridableModule)
Jiyong Park317645e2019-12-05 13:20:58 +09006854 if originalVariant.GetOverriddenBy() != "" {
6855 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6856 }
6857 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6858 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6859 }
6860
Jooyung Hana0503a52023-08-23 13:12:50 +09006861 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006862 apexRule := module.Rule("apexRule")
6863 copyCmds := apexRule.Args["copy_commands"]
6864
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006865 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6866 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006867
markchien7c803b82021-08-26 22:10:06 +08006868 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006869 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006870
Daniel Norman5a3ce132021-08-26 15:44:43 -07006871 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6872 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6873
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006874 apexBundle := module.Module().(*apexBundle)
6875 name := apexBundle.Name()
6876 if name != "override_myapex" {
6877 t.Errorf("name should be \"override_myapex\", but was %q", name)
6878 }
6879
Baligh Uddin004d7172020-02-19 21:29:28 -08006880 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6881 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6882 }
6883
Jiyong Park20bacab2020-03-03 11:45:41 +09006884 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006885 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006886 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6887
6888 signApkRule := module.Rule("signapk")
6889 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006890
Colin Crossaa255532020-07-03 13:18:24 -07006891 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006892 var builder strings.Builder
6893 data.Custom(&builder, name, "TARGET_", "", data)
6894 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006895 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6896 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006897 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006898 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006899 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006900 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006901 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006902 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006903}
6904
Albert Martineefabcf2022-03-21 20:11:16 +00006905func TestMinSdkVersionOverride(t *testing.T) {
6906 // Override from 29 to 31
6907 minSdkOverride31 := "31"
6908 ctx := testApex(t, `
6909 apex {
6910 name: "myapex",
6911 key: "myapex.key",
6912 native_shared_libs: ["mylib"],
6913 updatable: true,
6914 min_sdk_version: "29"
6915 }
6916
6917 override_apex {
6918 name: "override_myapex",
6919 base: "myapex",
6920 logging_parent: "com.foo.bar",
6921 package_name: "test.overridden.package"
6922 }
6923
6924 apex_key {
6925 name: "myapex.key",
6926 public_key: "testkey.avbpubkey",
6927 private_key: "testkey.pem",
6928 }
6929
6930 cc_library {
6931 name: "mylib",
6932 srcs: ["mylib.cpp"],
6933 runtime_libs: ["libbar"],
6934 system_shared_libs: [],
6935 stl: "none",
6936 apex_available: [ "myapex" ],
6937 min_sdk_version: "apex_inherit"
6938 }
6939
6940 cc_library {
6941 name: "libbar",
6942 srcs: ["mylib.cpp"],
6943 system_shared_libs: [],
6944 stl: "none",
6945 apex_available: [ "myapex" ],
6946 min_sdk_version: "apex_inherit"
6947 }
6948
6949 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6950
Jooyung Hana0503a52023-08-23 13:12:50 +09006951 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00006952 copyCmds := apexRule.Args["copy_commands"]
6953
6954 // Ensure that direct non-stubs dep is always included
6955 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6956
6957 // Ensure that runtime_libs dep in included
6958 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6959
6960 // Ensure libraries target overridden min_sdk_version value
6961 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6962}
6963
6964func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6965 // Attempt to override from 31 to 29, should be a NOOP
6966 minSdkOverride29 := "29"
6967 ctx := testApex(t, `
6968 apex {
6969 name: "myapex",
6970 key: "myapex.key",
6971 native_shared_libs: ["mylib"],
6972 updatable: true,
6973 min_sdk_version: "31"
6974 }
6975
6976 override_apex {
6977 name: "override_myapex",
6978 base: "myapex",
6979 logging_parent: "com.foo.bar",
6980 package_name: "test.overridden.package"
6981 }
6982
6983 apex_key {
6984 name: "myapex.key",
6985 public_key: "testkey.avbpubkey",
6986 private_key: "testkey.pem",
6987 }
6988
6989 cc_library {
6990 name: "mylib",
6991 srcs: ["mylib.cpp"],
6992 runtime_libs: ["libbar"],
6993 system_shared_libs: [],
6994 stl: "none",
6995 apex_available: [ "myapex" ],
6996 min_sdk_version: "apex_inherit"
6997 }
6998
6999 cc_library {
7000 name: "libbar",
7001 srcs: ["mylib.cpp"],
7002 system_shared_libs: [],
7003 stl: "none",
7004 apex_available: [ "myapex" ],
7005 min_sdk_version: "apex_inherit"
7006 }
7007
7008 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
7009
Jooyung Hana0503a52023-08-23 13:12:50 +09007010 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007011 copyCmds := apexRule.Args["copy_commands"]
7012
7013 // Ensure that direct non-stubs dep is always included
7014 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7015
7016 // Ensure that runtime_libs dep in included
7017 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7018
7019 // Ensure libraries target the original min_sdk_version value rather than the overridden
7020 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7021}
7022
Jooyung Han214bf372019-11-12 13:03:50 +09007023func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007024 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09007025 apex {
7026 name: "myapex",
7027 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007028 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09007029 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09007030 }
7031
7032 apex_key {
7033 name: "myapex.key",
7034 public_key: "testkey.avbpubkey",
7035 private_key: "testkey.pem",
7036 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007037
7038 cc_library {
7039 name: "mylib",
7040 srcs: ["mylib.cpp"],
7041 stl: "libc++",
7042 system_shared_libs: [],
7043 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09007044 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007045 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007046 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09007047
Jooyung Hana0503a52023-08-23 13:12:50 +09007048 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +09007049 args := module.Rule("apexRule").Args
7050 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00007051 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007052
7053 // The copies of the libraries in the apex should have one more dependency than
7054 // the ones outside the apex, namely the unwinder. Ideally we should check
7055 // the dependency names directly here but for some reason the names are blank in
7056 // this test.
7057 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07007058 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007059 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
7060 if len(apexImplicits) != len(nonApexImplicits)+1 {
7061 t.Errorf("%q missing unwinder dep", lib)
7062 }
7063 }
Jooyung Han214bf372019-11-12 13:03:50 +09007064}
7065
Paul Duffine05480a2021-03-08 15:07:14 +00007066var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01007067 "api/current.txt": nil,
7068 "api/removed.txt": nil,
7069 "api/system-current.txt": nil,
7070 "api/system-removed.txt": nil,
7071 "api/test-current.txt": nil,
7072 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01007073
Anton Hanssondff2c782020-12-21 17:10:01 +00007074 "100/public/api/foo.txt": nil,
7075 "100/public/api/foo-removed.txt": nil,
7076 "100/system/api/foo.txt": nil,
7077 "100/system/api/foo-removed.txt": nil,
7078
Paul Duffineedc5d52020-06-12 17:46:39 +01007079 // For java_sdk_library_import
7080 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01007081}
7082
Jooyung Han58f26ab2019-12-18 15:34:32 +09007083func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007084 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09007085 apex {
7086 name: "myapex",
7087 key: "myapex.key",
7088 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007089 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09007090 }
7091
7092 apex_key {
7093 name: "myapex.key",
7094 public_key: "testkey.avbpubkey",
7095 private_key: "testkey.pem",
7096 }
7097
7098 java_sdk_library {
7099 name: "foo",
7100 srcs: ["a.java"],
7101 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007102 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09007103 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007104
7105 prebuilt_apis {
7106 name: "sdk",
7107 api_dirs: ["100"],
7108 }
Paul Duffin9b879592020-05-26 13:21:35 +01007109 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09007110
7111 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007112 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09007113 "javalib/foo.jar",
7114 "etc/permissions/foo.xml",
7115 })
7116 // Permission XML should point to the activated path of impl jar of java_sdk_library
Paul Duffin1816cde2024-04-10 10:58:21 +01007117 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Output("foo.xml")
7118 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7119 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
Jooyung Han58f26ab2019-12-18 15:34:32 +09007120}
7121
Paul Duffin9b879592020-05-26 13:21:35 +01007122func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007123 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007124 apex {
7125 name: "myapex",
7126 key: "myapex.key",
7127 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007128 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007129 }
7130
7131 apex_key {
7132 name: "myapex.key",
7133 public_key: "testkey.avbpubkey",
7134 private_key: "testkey.pem",
7135 }
7136
7137 java_sdk_library {
7138 name: "foo",
7139 srcs: ["a.java"],
7140 api_packages: ["foo"],
7141 apex_available: ["myapex"],
7142 sdk_version: "none",
7143 system_modules: "none",
7144 }
7145
7146 java_library {
7147 name: "bar",
7148 srcs: ["a.java"],
7149 libs: ["foo"],
7150 apex_available: ["myapex"],
7151 sdk_version: "none",
7152 system_modules: "none",
7153 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007154
7155 prebuilt_apis {
7156 name: "sdk",
7157 api_dirs: ["100"],
7158 }
Paul Duffin9b879592020-05-26 13:21:35 +01007159 `, withFiles(filesForSdkLibrary))
7160
7161 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007162 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007163 "javalib/bar.jar",
7164 "javalib/foo.jar",
7165 "etc/permissions/foo.xml",
7166 })
7167
7168 // The bar library should depend on the implementation jar.
7169 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kang8479dea2024-04-04 01:19:05 +00007170 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007171 t.Errorf("expected %q, found %#q", expected, actual)
7172 }
7173}
7174
7175func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007176 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007177 apex {
7178 name: "myapex",
7179 key: "myapex.key",
7180 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007181 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007182 }
7183
7184 apex_key {
7185 name: "myapex.key",
7186 public_key: "testkey.avbpubkey",
7187 private_key: "testkey.pem",
7188 }
7189
7190 java_sdk_library {
7191 name: "foo",
7192 srcs: ["a.java"],
7193 api_packages: ["foo"],
7194 apex_available: ["myapex"],
7195 sdk_version: "none",
7196 system_modules: "none",
7197 }
7198
7199 java_library {
7200 name: "bar",
7201 srcs: ["a.java"],
7202 libs: ["foo"],
7203 sdk_version: "none",
7204 system_modules: "none",
7205 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007206
7207 prebuilt_apis {
7208 name: "sdk",
7209 api_dirs: ["100"],
7210 }
Paul Duffin9b879592020-05-26 13:21:35 +01007211 `, withFiles(filesForSdkLibrary))
7212
7213 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007214 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007215 "javalib/foo.jar",
7216 "etc/permissions/foo.xml",
7217 })
7218
7219 // The bar library should depend on the stubs jar.
7220 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007221 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007222 t.Errorf("expected %q, found %#q", expected, actual)
7223 }
7224}
7225
Paul Duffineedc5d52020-06-12 17:46:39 +01007226func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007227 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00007228 prebuilt_apis {
7229 name: "sdk",
7230 api_dirs: ["100"],
7231 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01007232 withFiles(map[string][]byte{
7233 "apex/a.java": nil,
7234 "apex/apex_manifest.json": nil,
7235 "apex/Android.bp": []byte(`
7236 package {
7237 default_visibility: ["//visibility:private"],
7238 }
7239
7240 apex {
7241 name: "myapex",
7242 key: "myapex.key",
7243 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007244 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007245 }
7246
7247 apex_key {
7248 name: "myapex.key",
7249 public_key: "testkey.avbpubkey",
7250 private_key: "testkey.pem",
7251 }
7252
7253 java_library {
7254 name: "bar",
7255 srcs: ["a.java"],
7256 libs: ["foo"],
7257 apex_available: ["myapex"],
7258 sdk_version: "none",
7259 system_modules: "none",
7260 }
7261`),
7262 "source/a.java": nil,
7263 "source/api/current.txt": nil,
7264 "source/api/removed.txt": nil,
7265 "source/Android.bp": []byte(`
7266 package {
7267 default_visibility: ["//visibility:private"],
7268 }
7269
7270 java_sdk_library {
7271 name: "foo",
7272 visibility: ["//apex"],
7273 srcs: ["a.java"],
7274 api_packages: ["foo"],
7275 apex_available: ["myapex"],
7276 sdk_version: "none",
7277 system_modules: "none",
7278 public: {
7279 enabled: true,
7280 },
7281 }
7282`),
7283 "prebuilt/a.jar": nil,
7284 "prebuilt/Android.bp": []byte(`
7285 package {
7286 default_visibility: ["//visibility:private"],
7287 }
7288
7289 java_sdk_library_import {
7290 name: "foo",
7291 visibility: ["//apex", "//source"],
7292 apex_available: ["myapex"],
7293 prefer: true,
7294 public: {
7295 jars: ["a.jar"],
7296 },
7297 }
7298`),
Anton Hanssondff2c782020-12-21 17:10:01 +00007299 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01007300 )
7301
7302 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007303 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffineedc5d52020-06-12 17:46:39 +01007304 "javalib/bar.jar",
7305 "javalib/foo.jar",
7306 "etc/permissions/foo.xml",
7307 })
7308
7309 // The bar library should depend on the implementation jar.
7310 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007311 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01007312 t.Errorf("expected %q, found %#q", expected, actual)
7313 }
7314}
7315
7316func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
7317 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
7318 apex {
7319 name: "myapex",
7320 key: "myapex.key",
7321 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007322 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007323 }
7324
7325 apex_key {
7326 name: "myapex.key",
7327 public_key: "testkey.avbpubkey",
7328 private_key: "testkey.pem",
7329 }
7330
7331 java_sdk_library_import {
7332 name: "foo",
7333 apex_available: ["myapex"],
7334 prefer: true,
7335 public: {
7336 jars: ["a.jar"],
7337 },
7338 }
7339
7340 `, withFiles(filesForSdkLibrary))
7341}
7342
atrost6e126252020-01-27 17:01:16 +00007343func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007344 result := android.GroupFixturePreparers(
7345 prepareForApexTest,
7346 java.PrepareForTestWithPlatformCompatConfig,
7347 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007348 apex {
7349 name: "myapex",
7350 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007351 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007352 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007353 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007354 }
7355
7356 apex_key {
7357 name: "myapex.key",
7358 public_key: "testkey.avbpubkey",
7359 private_key: "testkey.pem",
7360 }
7361
7362 platform_compat_config {
7363 name: "myjar-platform-compat-config",
7364 src: ":myjar",
7365 }
7366
7367 java_library {
7368 name: "myjar",
7369 srcs: ["foo/bar/MyClass.java"],
7370 sdk_version: "none",
7371 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007372 apex_available: [ "myapex" ],
7373 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007374
7375 // Make sure that a preferred prebuilt does not affect the apex contents.
7376 prebuilt_platform_compat_config {
7377 name: "myjar-platform-compat-config",
7378 metadata: "compat-config/metadata.xml",
7379 prefer: true,
7380 }
atrost6e126252020-01-27 17:01:16 +00007381 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007382 ctx := result.TestContext
Jooyung Hana0503a52023-08-23 13:12:50 +09007383 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
atrost6e126252020-01-27 17:01:16 +00007384 "etc/compatconfig/myjar-platform-compat-config.xml",
7385 "javalib/myjar.jar",
7386 })
7387}
7388
Jooyung Han862c0d62022-12-21 10:15:37 +09007389func TestNoDupeApexFiles(t *testing.T) {
7390 android.GroupFixturePreparers(
7391 android.PrepareForTestWithAndroidBuildComponents,
7392 PrepareForTestWithApexBuildComponents,
7393 prepareForTestWithMyapex,
7394 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7395 ).
7396 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7397 RunTestWithBp(t, `
7398 apex {
7399 name: "myapex",
7400 key: "myapex.key",
7401 prebuilts: ["foo", "bar"],
7402 updatable: false,
7403 }
7404
7405 apex_key {
7406 name: "myapex.key",
7407 public_key: "testkey.avbpubkey",
7408 private_key: "testkey.pem",
7409 }
7410
7411 prebuilt_etc {
7412 name: "foo",
7413 src: "myprebuilt",
7414 filename_from_src: true,
7415 }
7416
7417 prebuilt_etc {
7418 name: "bar",
7419 src: "myprebuilt",
7420 filename_from_src: true,
7421 }
7422 `)
7423}
7424
Jooyung Hana8bd72a2023-11-02 11:56:48 +09007425func TestApexUnwantedTransitiveDeps(t *testing.T) {
7426 bp := `
7427 apex {
7428 name: "myapex",
7429 key: "myapex.key",
7430 native_shared_libs: ["libfoo"],
7431 updatable: false,
7432 unwanted_transitive_deps: ["libbar"],
7433 }
7434
7435 apex_key {
7436 name: "myapex.key",
7437 public_key: "testkey.avbpubkey",
7438 private_key: "testkey.pem",
7439 }
7440
7441 cc_library {
7442 name: "libfoo",
7443 srcs: ["foo.cpp"],
7444 shared_libs: ["libbar"],
7445 apex_available: ["myapex"],
7446 }
7447
7448 cc_library {
7449 name: "libbar",
7450 srcs: ["bar.cpp"],
7451 apex_available: ["myapex"],
7452 }`
7453 ctx := testApex(t, bp)
7454 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
7455 "*/libc++.so",
7456 "*/libfoo.so",
7457 // not libbar.so
7458 })
7459}
7460
Jiyong Park479321d2019-12-16 11:47:12 +09007461func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7462 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7463 apex {
7464 name: "myapex",
7465 key: "myapex.key",
7466 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007467 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007468 }
7469
7470 apex_key {
7471 name: "myapex.key",
7472 public_key: "testkey.avbpubkey",
7473 private_key: "testkey.pem",
7474 }
7475
7476 java_library {
7477 name: "myjar",
7478 srcs: ["foo/bar/MyClass.java"],
7479 sdk_version: "none",
7480 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007481 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007482 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007483 }
7484 `)
7485}
7486
Jiyong Park7afd1072019-12-30 16:56:33 +09007487func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007488 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007489 apex {
7490 name: "myapex",
7491 key: "myapex.key",
7492 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007493 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007494 }
7495
7496 apex_key {
7497 name: "myapex.key",
7498 public_key: "testkey.avbpubkey",
7499 private_key: "testkey.pem",
7500 }
7501
7502 cc_library {
7503 name: "mylib",
7504 srcs: ["mylib.cpp"],
7505 system_shared_libs: [],
7506 stl: "none",
7507 required: ["a", "b"],
7508 host_required: ["c", "d"],
7509 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007510 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007511 }
7512 `)
7513
Jooyung Hana0503a52023-08-23 13:12:50 +09007514 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007515 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007516 name := apexBundle.BaseModuleName()
7517 prefix := "TARGET_"
7518 var builder strings.Builder
7519 data.Custom(&builder, name, prefix, "", data)
7520 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007521 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 a b\n")
Sasha Smundakdcb61292022-12-08 10:41:33 -08007522 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7523 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007524}
7525
Jiyong Park7cd10e32020-01-14 09:22:18 +09007526func TestSymlinksFromApexToSystem(t *testing.T) {
7527 bp := `
7528 apex {
7529 name: "myapex",
7530 key: "myapex.key",
7531 native_shared_libs: ["mylib"],
7532 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007533 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007534 }
7535
Jiyong Park9d677202020-02-19 16:29:35 +09007536 apex {
7537 name: "myapex.updatable",
7538 key: "myapex.key",
7539 native_shared_libs: ["mylib"],
7540 java_libs: ["myjar"],
7541 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007542 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007543 }
7544
Jiyong Park7cd10e32020-01-14 09:22:18 +09007545 apex_key {
7546 name: "myapex.key",
7547 public_key: "testkey.avbpubkey",
7548 private_key: "testkey.pem",
7549 }
7550
7551 cc_library {
7552 name: "mylib",
7553 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007554 shared_libs: [
7555 "myotherlib",
7556 "myotherlib_ext",
7557 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007558 system_shared_libs: [],
7559 stl: "none",
7560 apex_available: [
7561 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007562 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007563 "//apex_available:platform",
7564 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007565 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007566 }
7567
7568 cc_library {
7569 name: "myotherlib",
7570 srcs: ["mylib.cpp"],
7571 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
Jiyong Parkce243632023-02-17 18:22:25 +09007581 cc_library {
7582 name: "myotherlib_ext",
7583 srcs: ["mylib.cpp"],
7584 system_shared_libs: [],
7585 system_ext_specific: true,
7586 stl: "none",
7587 apex_available: [
7588 "myapex",
7589 "myapex.updatable",
7590 "//apex_available:platform",
7591 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007592 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007593 }
7594
Jiyong Park7cd10e32020-01-14 09:22:18 +09007595 java_library {
7596 name: "myjar",
7597 srcs: ["foo/bar/MyClass.java"],
7598 sdk_version: "none",
7599 system_modules: "none",
7600 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007601 apex_available: [
7602 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007603 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007604 "//apex_available:platform",
7605 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007606 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007607 }
7608
7609 java_library {
7610 name: "myotherjar",
7611 srcs: ["foo/bar/MyClass.java"],
7612 sdk_version: "none",
7613 system_modules: "none",
7614 apex_available: [
7615 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007616 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007617 "//apex_available:platform",
7618 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007619 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007620 }
7621 `
7622
7623 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7624 for _, f := range files {
7625 if f.path == file {
7626 if f.isLink {
7627 t.Errorf("%q is not a real file", file)
7628 }
7629 return
7630 }
7631 }
7632 t.Errorf("%q is not found", file)
7633 }
7634
Jiyong Parkce243632023-02-17 18:22:25 +09007635 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007636 for _, f := range files {
7637 if f.path == file {
7638 if !f.isLink {
7639 t.Errorf("%q is not a symlink", file)
7640 }
Jiyong Parkce243632023-02-17 18:22:25 +09007641 if f.src != target {
7642 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7643 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007644 return
7645 }
7646 }
7647 t.Errorf("%q is not found", file)
7648 }
7649
Jiyong Park9d677202020-02-19 16:29:35 +09007650 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7651 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007652 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana0503a52023-08-23 13:12:50 +09007653 files := getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007654 ensureRealfileExists(t, files, "javalib/myjar.jar")
7655 ensureRealfileExists(t, files, "lib64/mylib.so")
7656 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007657 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007658
Jooyung Hana0503a52023-08-23 13:12:50 +09007659 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007660 ensureRealfileExists(t, files, "javalib/myjar.jar")
7661 ensureRealfileExists(t, files, "lib64/mylib.so")
7662 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007663 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007664
7665 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007666 ctx = testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +09007667 files = getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007668 ensureRealfileExists(t, files, "javalib/myjar.jar")
7669 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007670 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7671 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007672
Jooyung Hana0503a52023-08-23 13:12:50 +09007673 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007674 ensureRealfileExists(t, files, "javalib/myjar.jar")
7675 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007676 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7677 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007678}
7679
Yo Chiange8128052020-07-23 20:09:18 +08007680func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007681 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007682 apex {
7683 name: "myapex",
7684 key: "myapex.key",
7685 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007686 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007687 }
7688
7689 apex_key {
7690 name: "myapex.key",
7691 public_key: "testkey.avbpubkey",
7692 private_key: "testkey.pem",
7693 }
7694
7695 cc_library_shared {
7696 name: "mylib",
7697 srcs: ["mylib.cpp"],
7698 shared_libs: ["myotherlib"],
7699 system_shared_libs: [],
7700 stl: "none",
7701 apex_available: [
7702 "myapex",
7703 "//apex_available:platform",
7704 ],
7705 }
7706
7707 cc_prebuilt_library_shared {
7708 name: "myotherlib",
7709 srcs: ["prebuilt.so"],
7710 system_shared_libs: [],
7711 stl: "none",
7712 apex_available: [
7713 "myapex",
7714 "//apex_available:platform",
7715 ],
7716 }
7717 `)
7718
Jooyung Hana0503a52023-08-23 13:12:50 +09007719 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007720 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007721 var builder strings.Builder
7722 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7723 androidMk := builder.String()
7724 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007725 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007726 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7727 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7728 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007729 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 myotherlib:64\n")
Yo Chiange8128052020-07-23 20:09:18 +08007730}
7731
Jooyung Han643adc42020-02-27 13:50:06 +09007732func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007733 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007734 apex {
7735 name: "myapex",
7736 key: "myapex.key",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007737 binaries: ["mybin"],
7738 jni_libs: ["mylib", "mylib3", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007739 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007740 }
7741
7742 apex_key {
7743 name: "myapex.key",
7744 public_key: "testkey.avbpubkey",
7745 private_key: "testkey.pem",
7746 }
7747
7748 cc_library {
7749 name: "mylib",
7750 srcs: ["mylib.cpp"],
7751 shared_libs: ["mylib2"],
7752 system_shared_libs: [],
7753 stl: "none",
7754 apex_available: [ "myapex" ],
7755 }
7756
7757 cc_library {
7758 name: "mylib2",
7759 srcs: ["mylib.cpp"],
7760 system_shared_libs: [],
7761 stl: "none",
7762 apex_available: [ "myapex" ],
7763 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007764
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007765 // Used as both a JNI library and a regular shared library.
7766 cc_library {
7767 name: "mylib3",
7768 srcs: ["mylib.cpp"],
7769 system_shared_libs: [],
7770 stl: "none",
7771 apex_available: [ "myapex" ],
7772 }
7773
7774 cc_binary {
7775 name: "mybin",
7776 srcs: ["mybin.cpp"],
7777 shared_libs: ["mylib3"],
7778 system_shared_libs: [],
7779 stl: "none",
7780 apex_available: [ "myapex" ],
7781 }
7782
Jiyong Park34d5c332022-02-24 18:02:44 +09007783 rust_ffi_shared {
7784 name: "libfoo.rust",
7785 crate_name: "foo",
7786 srcs: ["foo.rs"],
7787 shared_libs: ["libfoo.shared_from_rust"],
7788 prefer_rlib: true,
7789 apex_available: ["myapex"],
7790 }
7791
7792 cc_library_shared {
7793 name: "libfoo.shared_from_rust",
7794 srcs: ["mylib.cpp"],
7795 system_shared_libs: [],
7796 stl: "none",
7797 stubs: {
7798 versions: ["10", "11", "12"],
7799 },
7800 }
7801
Jooyung Han643adc42020-02-27 13:50:06 +09007802 `)
7803
Jooyung Hana0503a52023-08-23 13:12:50 +09007804 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han643adc42020-02-27 13:50:06 +09007805 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007806 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so mylib3.so")
Jooyung Hana0503a52023-08-23 13:12:50 +09007807 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007808 "bin/mybin",
Jooyung Han643adc42020-02-27 13:50:06 +09007809 "lib64/mylib.so",
7810 "lib64/mylib2.so",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007811 "lib64/mylib3.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007812 "lib64/libfoo.rust.so",
7813 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7814 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007815 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007816
7817 // b/220397949
7818 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007819}
7820
Jooyung Han49f67012020-04-17 13:43:10 +09007821func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007822 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007823 apex {
7824 name: "myapex",
7825 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007826 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007827 }
7828 apex_key {
7829 name: "myapex.key",
7830 public_key: "testkey.avbpubkey",
7831 private_key: "testkey.pem",
7832 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007833 `,
7834 android.FixtureModifyConfig(func(config android.Config) {
7835 delete(config.Targets, android.Android)
7836 config.AndroidCommonTarget = android.Target{}
7837 }),
7838 )
Jooyung Han49f67012020-04-17 13:43:10 +09007839
7840 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7841 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7842 }
7843}
7844
Jiyong Parkbd159612020-02-28 15:22:21 +09007845func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007846 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007847 apex {
7848 name: "myapex",
7849 key: "myapex.key",
7850 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007851 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007852 }
7853
7854 apex_key {
7855 name: "myapex.key",
7856 public_key: "testkey.avbpubkey",
7857 private_key: "testkey.pem",
7858 }
7859
7860 android_app {
7861 name: "AppFoo",
7862 srcs: ["foo/bar/MyClass.java"],
7863 sdk_version: "none",
7864 system_modules: "none",
7865 apex_available: [ "myapex" ],
7866 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007867 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007868
Jooyung Hana0503a52023-08-23 13:12:50 +09007869 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07007870 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Jiyong Parkbd159612020-02-28 15:22:21 +09007871
7872 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007873 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 +09007874}
7875
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007876func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007877 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007878 apex {
7879 name: "myapex",
7880 key: "myapex.key",
7881 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007882 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007883 }
7884
7885 apex_key {
7886 name: "myapex.key",
7887 public_key: "testkey.avbpubkey",
7888 private_key: "testkey.pem",
7889 }
7890
7891 android_app_set {
7892 name: "AppSet",
7893 set: "AppSet.apks",
7894 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09007895 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crosscf371cc2020-11-13 11:48:42 -08007896 bundleConfigRule := mod.Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07007897 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007898 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7899 s := mod.Rule("apexRule").Args["copy_commands"]
7900 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007901 if len(copyCmds) != 4 {
7902 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007903 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007904 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7905 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007906 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7907 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007908
7909 // Ensure that canned_fs_config has an entry for the app set zip file
7910 generateFsRule := mod.Rule("generateFsConfig")
7911 cmd := generateFsRule.RuleParams.Command
7912 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007913}
7914
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007915func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007916 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007917 apex_set {
7918 name: "myapex",
7919 filename: "foo_v2.apex",
7920 sanitized: {
7921 none: { set: "myapex.apks", },
7922 hwaddress: { set: "myapex.hwasan.apks", },
7923 },
Paul Duffin24704672021-04-06 16:09:30 +01007924 }
7925 `
7926 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007927
Paul Duffin24704672021-04-06 16:09:30 +01007928 // Check that the extractor produces the correct output file from the correct input file.
Spandan Das3576e762024-01-03 18:57:03 +00007929 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007930
Spandan Das3576e762024-01-03 18:57:03 +00007931 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Paul Duffin24704672021-04-06 16:09:30 +01007932 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007933
Paul Duffin24704672021-04-06 16:09:30 +01007934 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7935
7936 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007937 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7938 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007939
7940 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007941}
7942
Pranav Guptaeba03b02022-09-27 00:27:08 +00007943func TestApexSetApksModuleAssignment(t *testing.T) {
7944 ctx := testApex(t, `
7945 apex_set {
7946 name: "myapex",
7947 set: ":myapex_apks_file",
7948 }
7949
7950 filegroup {
7951 name: "myapex_apks_file",
7952 srcs: ["myapex.apks"],
7953 }
7954 `)
7955
Spandan Das3576e762024-01-03 18:57:03 +00007956 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Pranav Guptaeba03b02022-09-27 00:27:08 +00007957
7958 // Check that the extractor produces the correct apks file from the input module
Spandan Das3576e762024-01-03 18:57:03 +00007959 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.apks"
Pranav Guptaeba03b02022-09-27 00:27:08 +00007960 extractedApex := m.Output(extractorOutput)
7961
7962 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7963}
7964
Paul Duffin89f570a2021-06-16 01:42:33 +01007965func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007966 t.Helper()
7967
Paul Duffin55607122021-03-30 23:32:51 +01007968 fs := android.MockFS{
7969 "a.java": nil,
7970 "a.jar": nil,
7971 "apex_manifest.json": nil,
7972 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007973 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007974 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7975 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7976 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007977 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007978 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007979
Paul Duffin55607122021-03-30 23:32:51 +01007980 errorHandler := android.FixtureExpectsNoErrors
7981 if errmsg != "" {
7982 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007983 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007984
Paul Duffin55607122021-03-30 23:32:51 +01007985 result := android.GroupFixturePreparers(
7986 cc.PrepareForTestWithCcDefaultModules,
7987 java.PrepareForTestWithHiddenApiBuildComponents,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01007988 java.PrepareForTestWithDexpreopt,
Paul Duffin55607122021-03-30 23:32:51 +01007989 java.PrepareForTestWithJavaSdkLibraryFiles,
7990 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007991 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007992 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007993 android.FixtureModifyMockFS(func(fs android.MockFS) {
7994 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7995 insert := ""
7996 for _, fragment := range fragments {
7997 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7998 }
7999 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
8000 platform_bootclasspath {
8001 name: "platform-bootclasspath",
8002 fragments: [
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008003 {apex: "com.android.art", module: "art-bootclasspath-fragment"},
Paul Duffin89f570a2021-06-16 01:42:33 +01008004 %s
8005 ],
8006 }
8007 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01008008 }
Paul Duffin89f570a2021-06-16 01:42:33 +01008009 }),
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008010 // Dexpreopt for boot jars requires the ART boot image profile.
8011 java.PrepareApexBootJarModule("com.android.art", "core-oj"),
8012 dexpreopt.FixtureSetArtBootJars("com.android.art:core-oj"),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00008013 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01008014 ).
8015 ExtendWithErrorHandler(errorHandler).
8016 RunTestWithBp(t, bp)
8017
8018 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008019}
8020
Paul Duffin5556c5f2022-06-09 17:32:21 +00008021func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008022 preparers := android.GroupFixturePreparers(
8023 java.PrepareForTestWithJavaDefaultModules,
Spandan Das5be63332023-12-13 00:06:32 +00008024 prepareForTestWithBootclasspathFragment,
8025 dexpreopt.FixtureSetTestOnlyArtBootImageJars("com.android.art:libfoo"),
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008026 PrepareForTestWithApexBuildComponents,
8027 ).
8028 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
Spandan Das3576e762024-01-03 18:57:03 +00008029 "Multiple installable prebuilt APEXes provide ambiguous deapexers: prebuilt_com.android.art and prebuilt_com.mycompany.android.art"))
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008030
8031 bpBase := `
8032 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008033 name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008034 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008035 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008036 set: "myapex.apks",
8037 }
8038
8039 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008040 name: "com.mycompany.android.art",
8041 apex_name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008042 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008043 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008044 set: "company-myapex.apks",
8045 }
8046
8047 prebuilt_bootclasspath_fragment {
Spandan Das5be63332023-12-13 00:06:32 +00008048 name: "art-bootclasspath-fragment",
8049 apex_available: ["com.android.art"],
Spandan Dasfae468e2023-12-12 23:23:53 +00008050 hidden_api: {
8051 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8052 metadata: "my-bootclasspath-fragment/metadata.csv",
8053 index: "my-bootclasspath-fragment/index.csv",
8054 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
8055 all_flags: "my-bootclasspath-fragment/all-flags.csv",
8056 },
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008057 %s
8058 }
8059 `
8060
8061 t.Run("java_import", func(t *testing.T) {
8062 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8063 java_import {
8064 name: "libfoo",
8065 jars: ["libfoo.jar"],
Spandan Das5be63332023-12-13 00:06:32 +00008066 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008067 }
8068 `)
8069 })
8070
8071 t.Run("java_sdk_library_import", func(t *testing.T) {
8072 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8073 java_sdk_library_import {
8074 name: "libfoo",
8075 public: {
8076 jars: ["libbar.jar"],
8077 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008078 shared_library: false,
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("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8085 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8086 image_name: "art",
8087 contents: ["libfoo"],
8088 `)+`
8089 java_sdk_library_import {
8090 name: "libfoo",
8091 public: {
8092 jars: ["libbar.jar"],
8093 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008094 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008095 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008096 }
8097 `)
8098 })
8099}
8100
Paul Duffin5556c5f2022-06-09 17:32:21 +00008101func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
8102 preparers := android.GroupFixturePreparers(
8103 java.PrepareForTestWithJavaDefaultModules,
8104 PrepareForTestWithApexBuildComponents,
8105 )
8106
Spandan Das59a4a2b2024-01-09 21:35:56 +00008107 errCtx := moduleErrorfTestCtx{}
8108
Paul Duffin5556c5f2022-06-09 17:32:21 +00008109 bpBase := `
8110 apex_set {
8111 name: "com.android.myapex",
8112 installable: true,
8113 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8114 set: "myapex.apks",
8115 }
8116
8117 apex_set {
8118 name: "com.android.myapex_compressed",
8119 apex_name: "com.android.myapex",
8120 installable: true,
8121 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8122 set: "myapex_compressed.apks",
8123 }
8124
8125 prebuilt_bootclasspath_fragment {
8126 name: "my-bootclasspath-fragment",
8127 apex_available: [
8128 "com.android.myapex",
8129 "com.android.myapex_compressed",
8130 ],
8131 hidden_api: {
8132 annotation_flags: "annotation-flags.csv",
8133 metadata: "metadata.csv",
8134 index: "index.csv",
8135 signature_patterns: "signature_patterns.csv",
8136 },
8137 %s
8138 }
8139 `
8140
8141 t.Run("java_import", func(t *testing.T) {
8142 result := preparers.RunTestWithBp(t,
8143 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8144 java_import {
8145 name: "libfoo",
8146 jars: ["libfoo.jar"],
8147 apex_available: [
8148 "com.android.myapex",
8149 "com.android.myapex_compressed",
8150 ],
8151 }
8152 `)
8153
8154 module := result.Module("libfoo", "android_common_com.android.myapex")
8155 usesLibraryDep := module.(java.UsesLibraryDependency)
8156 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008157 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008158 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008159 })
8160
8161 t.Run("java_sdk_library_import", func(t *testing.T) {
8162 result := preparers.RunTestWithBp(t,
8163 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8164 java_sdk_library_import {
8165 name: "libfoo",
8166 public: {
8167 jars: ["libbar.jar"],
8168 },
8169 apex_available: [
8170 "com.android.myapex",
8171 "com.android.myapex_compressed",
8172 ],
8173 compile_dex: true,
8174 }
8175 `)
8176
8177 module := result.Module("libfoo", "android_common_com.android.myapex")
8178 usesLibraryDep := module.(java.UsesLibraryDependency)
8179 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008180 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008181 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008182 })
8183
8184 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8185 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8186 image_name: "art",
8187 contents: ["libfoo"],
8188 `)+`
8189 java_sdk_library_import {
8190 name: "libfoo",
8191 public: {
8192 jars: ["libbar.jar"],
8193 },
8194 apex_available: [
8195 "com.android.myapex",
8196 "com.android.myapex_compressed",
8197 ],
8198 compile_dex: true,
8199 }
8200 `)
8201 })
8202}
8203
Jooyung Han548640b2020-04-27 12:10:30 +09008204func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
8205 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8206 apex {
8207 name: "myapex",
8208 key: "myapex.key",
8209 updatable: true,
8210 }
8211
8212 apex_key {
8213 name: "myapex.key",
8214 public_key: "testkey.avbpubkey",
8215 private_key: "testkey.pem",
8216 }
8217 `)
8218}
8219
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008220func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
8221 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8222 apex {
8223 name: "myapex",
8224 key: "myapex.key",
8225 }
8226
8227 apex_key {
8228 name: "myapex.key",
8229 public_key: "testkey.avbpubkey",
8230 private_key: "testkey.pem",
8231 }
8232 `)
8233}
8234
Jooyung Handfc864c2023-03-20 18:19:07 +09008235func Test_use_vndk_as_stable_shouldnt_be_used_for_updatable_vendor_apexes(t *testing.T) {
8236 testApexError(t, `"myapex" .*: use_vndk_as_stable: updatable APEXes can't use external VNDK libs`, `
Daniel Norman69109112021-12-02 12:52:42 -08008237 apex {
8238 name: "myapex",
8239 key: "myapex.key",
8240 updatable: true,
Jooyung Handfc864c2023-03-20 18:19:07 +09008241 use_vndk_as_stable: true,
Daniel Norman69109112021-12-02 12:52:42 -08008242 soc_specific: true,
8243 }
8244
8245 apex_key {
8246 name: "myapex.key",
8247 public_key: "testkey.avbpubkey",
8248 private_key: "testkey.pem",
8249 }
8250 `)
8251}
8252
Jooyung Han02873da2023-03-22 17:41:03 +09008253func Test_use_vndk_as_stable_shouldnt_be_used_with_min_sdk_version(t *testing.T) {
8254 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported when min_sdk_version is set`, `
8255 apex {
8256 name: "myapex",
8257 key: "myapex.key",
8258 updatable: false,
8259 min_sdk_version: "29",
8260 use_vndk_as_stable: true,
8261 vendor: true,
8262 }
8263
8264 apex_key {
8265 name: "myapex.key",
8266 public_key: "testkey.avbpubkey",
8267 private_key: "testkey.pem",
8268 }
8269 `)
8270}
8271
Jooyung Handfc864c2023-03-20 18:19:07 +09008272func Test_use_vndk_as_stable_shouldnt_be_used_for_non_vendor_apexes(t *testing.T) {
8273 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported for system/system_ext APEXes`, `
8274 apex {
8275 name: "myapex",
8276 key: "myapex.key",
8277 updatable: false,
8278 use_vndk_as_stable: true,
8279 }
8280
8281 apex_key {
8282 name: "myapex.key",
8283 public_key: "testkey.avbpubkey",
8284 private_key: "testkey.pem",
8285 }
8286 `)
8287}
8288
satayevb98371c2021-06-15 16:49:50 +01008289func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
8290 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
8291 apex {
8292 name: "myapex",
8293 key: "myapex.key",
8294 systemserverclasspath_fragments: [
8295 "mysystemserverclasspathfragment",
8296 ],
8297 min_sdk_version: "29",
8298 updatable: true,
8299 }
8300
8301 apex_key {
8302 name: "myapex.key",
8303 public_key: "testkey.avbpubkey",
8304 private_key: "testkey.pem",
8305 }
8306
8307 java_library {
8308 name: "foo",
8309 srcs: ["b.java"],
8310 min_sdk_version: "29",
8311 installable: true,
8312 apex_available: [
8313 "myapex",
8314 ],
8315 }
8316
8317 systemserverclasspath_fragment {
8318 name: "mysystemserverclasspathfragment",
8319 generate_classpaths_proto: false,
8320 contents: [
8321 "foo",
8322 ],
8323 apex_available: [
8324 "myapex",
8325 ],
8326 }
satayevabcd5972021-08-06 17:49:46 +01008327 `,
8328 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8329 )
satayevb98371c2021-06-15 16:49:50 +01008330}
8331
Paul Duffin064b70c2020-11-02 17:32:38 +00008332func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008333 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008334 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008335 fragment := java.ApexVariantReference{
8336 Apex: proptools.StringPtr("myapex"),
8337 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8338 }
8339
Paul Duffin064b70c2020-11-02 17:32:38 +00008340 testDexpreoptWithApexes(t, `
8341 prebuilt_apex {
8342 name: "myapex" ,
8343 arch: {
8344 arm64: {
8345 src: "myapex-arm64.apex",
8346 },
8347 arm: {
8348 src: "myapex-arm.apex",
8349 },
8350 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008351 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8352 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008353
Paul Duffin89f570a2021-06-16 01:42:33 +01008354 prebuilt_bootclasspath_fragment {
8355 name: "my-bootclasspath-fragment",
8356 contents: ["libfoo"],
8357 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008358 hidden_api: {
8359 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8360 metadata: "my-bootclasspath-fragment/metadata.csv",
8361 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008362 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8363 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8364 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008365 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008366 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008367
Paul Duffin89f570a2021-06-16 01:42:33 +01008368 java_import {
8369 name: "libfoo",
8370 jars: ["libfoo.jar"],
8371 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008372 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008373 }
8374 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008375 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008376}
8377
Spandan Dasf14e2542021-11-12 00:01:37 +00008378func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008379 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008380 bp += `
8381 apex_key {
8382 name: "myapex.key",
8383 public_key: "testkey.avbpubkey",
8384 private_key: "testkey.pem",
8385 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008386 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008387 "lib1/src/A.java": nil,
8388 "lib2/src/B.java": nil,
8389 "system/sepolicy/apex/myapex-file_contexts": nil,
8390 }
8391
Paul Duffin45338f02021-03-30 23:07:52 +01008392 errorHandler := android.FixtureExpectsNoErrors
8393 if errmsg != "" {
8394 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008395 }
Colin Crossae8600b2020-10-29 17:09:13 -07008396
Paul Duffin45338f02021-03-30 23:07:52 +01008397 android.GroupFixturePreparers(
8398 android.PrepareForTestWithAndroidBuildComponents,
8399 java.PrepareForTestWithJavaBuildComponents,
8400 PrepareForTestWithApexBuildComponents,
8401 android.PrepareForTestWithNeverallowRules(rules),
8402 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008403 apexBootJars := make([]string, 0, len(bootJars))
8404 for _, apexBootJar := range bootJars {
8405 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008406 }
satayevd604b212021-07-21 14:23:52 +01008407 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008408 }),
8409 fs.AddToFixture(),
8410 ).
8411 ExtendWithErrorHandler(errorHandler).
8412 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008413}
8414
8415func TestApexPermittedPackagesRules(t *testing.T) {
8416 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008417 name string
8418 expectedError string
8419 bp string
8420 bootJars []string
8421 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008422 }{
8423
8424 {
8425 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8426 expectedError: "",
8427 bp: `
8428 java_library {
8429 name: "bcp_lib1",
8430 srcs: ["lib1/src/*.java"],
8431 permitted_packages: ["foo.bar"],
8432 apex_available: ["myapex"],
8433 sdk_version: "none",
8434 system_modules: "none",
8435 }
8436 java_library {
8437 name: "nonbcp_lib2",
8438 srcs: ["lib2/src/*.java"],
8439 apex_available: ["myapex"],
8440 permitted_packages: ["a.b"],
8441 sdk_version: "none",
8442 system_modules: "none",
8443 }
8444 apex {
8445 name: "myapex",
8446 key: "myapex.key",
8447 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008448 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008449 }`,
8450 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008451 bcpPermittedPackages: map[string][]string{
8452 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008453 "foo.bar",
8454 },
8455 },
8456 },
8457 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008458 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008459 expectedError: `(?s)module "bcp_lib2" .* which is restricted because bcp_lib2 bootjar may only use these package prefixes: foo.bar. Please consider the following alternatives:\n 1. If the offending code is from a statically linked library, consider removing that dependency and using an alternative already in the bootclasspath, or perhaps a shared library. 2. Move the offending code into an allowed package.\n 3. Jarjar the offending code. Please be mindful of the potential system health implications of bundling that code, particularly if the offending jar is part of the bootclasspath.`,
Andrei Onea115e7e72020-06-05 21:14:03 +01008460 bp: `
8461 java_library {
8462 name: "bcp_lib1",
8463 srcs: ["lib1/src/*.java"],
8464 apex_available: ["myapex"],
8465 permitted_packages: ["foo.bar"],
8466 sdk_version: "none",
8467 system_modules: "none",
8468 }
8469 java_library {
8470 name: "bcp_lib2",
8471 srcs: ["lib2/src/*.java"],
8472 apex_available: ["myapex"],
8473 permitted_packages: ["foo.bar", "bar.baz"],
8474 sdk_version: "none",
8475 system_modules: "none",
8476 }
8477 apex {
8478 name: "myapex",
8479 key: "myapex.key",
8480 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008481 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008482 }
8483 `,
8484 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008485 bcpPermittedPackages: map[string][]string{
8486 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008487 "foo.bar",
8488 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008489 "bcp_lib2": []string{
8490 "foo.bar",
8491 },
8492 },
8493 },
8494 {
8495 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8496 expectedError: "",
8497 bp: `
8498 java_library {
8499 name: "bcp_lib_restricted",
8500 srcs: ["lib1/src/*.java"],
8501 apex_available: ["myapex"],
8502 permitted_packages: ["foo.bar"],
8503 sdk_version: "none",
8504 min_sdk_version: "29",
8505 system_modules: "none",
8506 }
8507 java_library {
8508 name: "bcp_lib_unrestricted",
8509 srcs: ["lib2/src/*.java"],
8510 apex_available: ["myapex"],
8511 permitted_packages: ["foo.bar", "bar.baz"],
8512 sdk_version: "none",
8513 min_sdk_version: "29",
8514 system_modules: "none",
8515 }
8516 apex {
8517 name: "myapex",
8518 key: "myapex.key",
8519 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8520 updatable: true,
8521 min_sdk_version: "29",
8522 }
8523 `,
8524 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8525 bcpPermittedPackages: map[string][]string{
8526 "bcp_lib1_non_updateable": []string{
8527 "foo.bar",
8528 },
8529 // bcp_lib2_updateable has no entry here since updateable bcp can contain new packages - tracking via an allowlist is not necessary
Andrei Onea115e7e72020-06-05 21:14:03 +01008530 },
8531 },
8532 }
8533 for _, tc := range testcases {
8534 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008535 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8536 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008537 })
8538 }
8539}
8540
Jiyong Park62304bb2020-04-13 16:19:48 +09008541func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008542 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008543 apex {
8544 name: "myapex",
8545 key: "myapex.key",
8546 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008547 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008548 }
8549
8550 apex_key {
8551 name: "myapex.key",
8552 public_key: "testkey.avbpubkey",
8553 private_key: "testkey.pem",
8554 }
8555
8556 cc_library {
8557 name: "mylib",
8558 srcs: ["mylib.cpp"],
8559 system_shared_libs: [],
8560 stl: "none",
8561 stubs: {
8562 versions: ["1"],
8563 },
8564 apex_available: ["myapex"],
8565 }
8566
8567 cc_library {
8568 name: "myprivlib",
8569 srcs: ["mylib.cpp"],
8570 system_shared_libs: [],
8571 stl: "none",
8572 apex_available: ["myapex"],
8573 }
8574
8575
8576 cc_test {
8577 name: "mytest",
8578 gtest: false,
8579 srcs: ["mylib.cpp"],
8580 system_shared_libs: [],
8581 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008582 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008583 test_for: ["myapex"]
8584 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008585
8586 cc_library {
8587 name: "mytestlib",
8588 srcs: ["mylib.cpp"],
8589 system_shared_libs: [],
8590 shared_libs: ["mylib", "myprivlib"],
8591 stl: "none",
8592 test_for: ["myapex"],
8593 }
8594
8595 cc_benchmark {
8596 name: "mybench",
8597 srcs: ["mylib.cpp"],
8598 system_shared_libs: [],
8599 shared_libs: ["mylib", "myprivlib"],
8600 stl: "none",
8601 test_for: ["myapex"],
8602 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008603 `)
8604
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008605 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008606 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008607 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8608 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8609 }
8610
8611 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008612 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008613 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8614 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8615 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8616}
Jiyong Park46a512f2020-12-04 18:02:13 +09008617
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008618func TestIndirectTestFor(t *testing.T) {
8619 ctx := testApex(t, `
8620 apex {
8621 name: "myapex",
8622 key: "myapex.key",
8623 native_shared_libs: ["mylib", "myprivlib"],
8624 updatable: false,
8625 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008626
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008627 apex_key {
8628 name: "myapex.key",
8629 public_key: "testkey.avbpubkey",
8630 private_key: "testkey.pem",
8631 }
8632
8633 cc_library {
8634 name: "mylib",
8635 srcs: ["mylib.cpp"],
8636 system_shared_libs: [],
8637 stl: "none",
8638 stubs: {
8639 versions: ["1"],
8640 },
8641 apex_available: ["myapex"],
8642 }
8643
8644 cc_library {
8645 name: "myprivlib",
8646 srcs: ["mylib.cpp"],
8647 system_shared_libs: [],
8648 stl: "none",
8649 shared_libs: ["mylib"],
8650 apex_available: ["myapex"],
8651 }
8652
8653 cc_library {
8654 name: "mytestlib",
8655 srcs: ["mylib.cpp"],
8656 system_shared_libs: [],
8657 shared_libs: ["myprivlib"],
8658 stl: "none",
8659 test_for: ["myapex"],
8660 }
8661 `)
8662
8663 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008664 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008665 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8666 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8667 }
8668
8669 // The platform variant of mytestlib links to the platform variant of the
8670 // internal myprivlib.
8671 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8672
8673 // The platform variant of myprivlib links to the platform variant of mylib
8674 // and bypasses its stubs.
8675 ensureLinkedLibIs("myprivlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
Jiyong Park62304bb2020-04-13 16:19:48 +09008676}
8677
Martin Stjernholmec009002021-03-27 15:18:31 +00008678func TestTestForForLibInOtherApex(t *testing.T) {
8679 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8680 _ = testApex(t, `
8681 apex {
8682 name: "com.android.art",
8683 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008684 native_shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008685 updatable: false,
8686 }
8687
8688 apex {
8689 name: "com.android.art.debug",
8690 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008691 native_shared_libs: ["libnativebridge", "libnativebrdige_test"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008692 updatable: false,
8693 }
8694
8695 apex_key {
8696 name: "myapex.key",
8697 public_key: "testkey.avbpubkey",
8698 private_key: "testkey.pem",
8699 }
8700
8701 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008702 name: "libnativebridge",
8703 srcs: ["libnativebridge.cpp"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008704 system_shared_libs: [],
8705 stl: "none",
8706 stubs: {
8707 versions: ["1"],
8708 },
8709 apex_available: ["com.android.art", "com.android.art.debug"],
8710 }
8711
8712 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008713 name: "libnativebrdige_test",
Martin Stjernholmec009002021-03-27 15:18:31 +00008714 srcs: ["mylib.cpp"],
8715 system_shared_libs: [],
Spandan Das20fce2d2023-04-12 17:21:39 +00008716 shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008717 stl: "none",
8718 apex_available: ["com.android.art.debug"],
8719 test_for: ["com.android.art"],
8720 }
8721 `,
8722 android.MockFS{
8723 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8724 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8725 }.AddToFixture())
8726}
8727
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008728// TODO(jungjw): Move this to proptools
8729func intPtr(i int) *int {
8730 return &i
8731}
8732
8733func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008734 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008735 apex_set {
8736 name: "myapex",
8737 set: "myapex.apks",
8738 filename: "foo_v2.apex",
8739 overrides: ["foo"],
8740 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008741 `,
8742 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8743 variables.Platform_sdk_version = intPtr(30)
8744 }),
8745 android.FixtureModifyConfig(func(config android.Config) {
8746 config.Targets[android.Android] = []android.Target{
8747 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8748 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8749 }
8750 }),
8751 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008752
Spandan Das3576e762024-01-03 18:57:03 +00008753 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008754
8755 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008756 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008757 actual := extractedApex.Args["abis"]
8758 expected := "ARMEABI_V7A,ARM64_V8A"
8759 if actual != expected {
8760 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8761 }
8762 actual = extractedApex.Args["sdk-version"]
8763 expected = "30"
8764 if actual != expected {
8765 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8766 }
8767
Paul Duffin6717d882021-06-15 19:09:41 +01008768 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008769 a := m.Module().(*ApexSet)
8770 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008771 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008772 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8773 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8774 }
8775}
8776
Anton Hansson805e0a52022-11-25 14:06:46 +00008777func TestApexSet_NativeBridge(t *testing.T) {
8778 ctx := testApex(t, `
8779 apex_set {
8780 name: "myapex",
8781 set: "myapex.apks",
8782 filename: "foo_v2.apex",
8783 overrides: ["foo"],
8784 }
8785 `,
8786 android.FixtureModifyConfig(func(config android.Config) {
8787 config.Targets[android.Android] = []android.Target{
8788 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8789 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8790 }
8791 }),
8792 )
8793
Spandan Das3576e762024-01-03 18:57:03 +00008794 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Anton Hansson805e0a52022-11-25 14:06:46 +00008795
8796 // Check extract_apks tool parameters. No native bridge arch expected
8797 extractedApex := m.Output("extracted/myapex.apks")
8798 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8799}
8800
Jiyong Park7d95a512020-05-10 15:16:24 +09008801func TestNoStaticLinkingToStubsLib(t *testing.T) {
8802 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8803 apex {
8804 name: "myapex",
8805 key: "myapex.key",
8806 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008807 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008808 }
8809
8810 apex_key {
8811 name: "myapex.key",
8812 public_key: "testkey.avbpubkey",
8813 private_key: "testkey.pem",
8814 }
8815
8816 cc_library {
8817 name: "mylib",
8818 srcs: ["mylib.cpp"],
8819 static_libs: ["otherlib"],
8820 system_shared_libs: [],
8821 stl: "none",
8822 apex_available: [ "myapex" ],
8823 }
8824
8825 cc_library {
8826 name: "otherlib",
8827 srcs: ["mylib.cpp"],
8828 system_shared_libs: [],
8829 stl: "none",
8830 stubs: {
8831 versions: ["1", "2", "3"],
8832 },
8833 apex_available: [ "myapex" ],
8834 }
8835 `)
8836}
8837
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008838func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008839 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008840 apex {
8841 name: "myapex",
8842 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008843 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008844 custom_sign_tool: "sign_myapex",
8845 }
8846
8847 apex_key {
8848 name: "myapex.key",
8849 public_key: "testkey.avbpubkey",
8850 private_key: "testkey.pem",
8851 }
8852 `)
8853
Jooyung Han286957d2023-10-30 16:17:56 +09008854 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07008855 content := android.ContentFromFileRuleForTests(t, ctx, myapex.Output("apexkeys.txt"))
Jooyung Haneec1b3f2023-06-20 16:25:59 +09008856 ensureContains(t, content, `name="myapex.apex" public_key="vendor/foo/devkeys/testkey.avbpubkey" private_key="vendor/foo/devkeys/testkey.pem" container_certificate="vendor/foo/devkeys/test.x509.pem" container_private_key="vendor/foo/devkeys/test.pk8" partition="system" sign_tool="sign_myapex"`)
Jooyung Han09c11ad2021-10-27 03:45:31 +09008857}
8858
8859func TestApexKeysTxtOverrides(t *testing.T) {
8860 ctx := testApex(t, `
8861 apex {
8862 name: "myapex",
8863 key: "myapex.key",
8864 updatable: false,
8865 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008866 }
8867
8868 apex_key {
8869 name: "myapex.key",
8870 public_key: "testkey.avbpubkey",
8871 private_key: "testkey.pem",
8872 }
8873
8874 prebuilt_apex {
8875 name: "myapex",
8876 prefer: true,
8877 arch: {
8878 arm64: {
8879 src: "myapex-arm64.apex",
8880 },
8881 arm: {
8882 src: "myapex-arm.apex",
8883 },
8884 },
8885 }
8886
8887 apex_set {
8888 name: "myapex_set",
8889 set: "myapex.apks",
8890 filename: "myapex_set.apex",
8891 overrides: ["myapex"],
8892 }
8893 `)
8894
Colin Crossf61d03d2023-11-02 16:56:39 -07008895 content := android.ContentFromFileRuleForTests(t, ctx,
8896 ctx.ModuleForTests("myapex", "android_common_myapex").Output("apexkeys.txt"))
Jooyung Han286957d2023-10-30 16:17:56 +09008897 ensureContains(t, content, `name="myapex.apex" public_key="vendor/foo/devkeys/testkey.avbpubkey" private_key="vendor/foo/devkeys/testkey.pem" container_certificate="vendor/foo/devkeys/test.x509.pem" container_private_key="vendor/foo/devkeys/test.pk8" partition="system" sign_tool="sign_myapex"`)
Colin Crossf61d03d2023-11-02 16:56:39 -07008898 content = android.ContentFromFileRuleForTests(t, ctx,
8899 ctx.ModuleForTests("myapex_set", "android_common_myapex_set").Output("apexkeys.txt"))
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008900 ensureContains(t, content, `name="myapex_set.apex" public_key="PRESIGNED" private_key="PRESIGNED" container_certificate="PRESIGNED" container_private_key="PRESIGNED" partition="system"`)
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008901}
8902
Jooyung Han938b5932020-06-20 12:47:47 +09008903func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008904 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008905 apex {
8906 name: "myapex",
8907 key: "myapex.key",
8908 apps: ["app"],
8909 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008910 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008911 }
8912
8913 apex_key {
8914 name: "myapex.key",
8915 public_key: "testkey.avbpubkey",
8916 private_key: "testkey.pem",
8917 }
8918
8919 android_app {
8920 name: "app",
8921 srcs: ["foo/bar/MyClass.java"],
8922 package_name: "foo",
8923 sdk_version: "none",
8924 system_modules: "none",
8925 apex_available: [ "myapex" ],
8926 }
8927 `, withFiles(map[string][]byte{
8928 "sub/Android.bp": []byte(`
8929 override_apex {
8930 name: "override_myapex",
8931 base: "myapex",
8932 apps: ["override_app"],
8933 allowed_files: ":allowed",
8934 }
8935 // Overridable "path" property should be referenced indirectly
8936 filegroup {
8937 name: "allowed",
8938 srcs: ["allowed.txt"],
8939 }
8940 override_android_app {
8941 name: "override_app",
8942 base: "app",
8943 package_name: "bar",
8944 }
8945 `),
8946 }))
8947
Jooyung Hana0503a52023-08-23 13:12:50 +09008948 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008949 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8950 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8951 }
8952
Jooyung Hana0503a52023-08-23 13:12:50 +09008953 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008954 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8955 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8956 }
8957}
8958
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008959func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008960 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008961 apex {
8962 name: "myapex",
8963 key: "myapex.key",
8964 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008965 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008966 }
8967
8968 apex_key {
8969 name: "myapex.key",
8970 public_key: "testkey.avbpubkey",
8971 private_key: "testkey.pem",
8972 }
8973
8974 cc_library {
8975 name: "mylib",
8976 srcs: ["mylib.cpp"],
8977 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008978 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008979 },
8980 apex_available: ["myapex"],
8981 }
8982
8983 cc_prebuilt_library_shared {
8984 name: "mylib",
8985 prefer: false,
8986 srcs: ["prebuilt.so"],
8987 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008988 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008989 },
8990 apex_available: ["myapex"],
8991 }
8992 `)
8993}
8994
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008995func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008996 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008997 apex {
8998 name: "myapex",
8999 key: "myapex.key",
9000 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009001 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009002 }
9003 apex_key {
9004 name: "myapex.key",
9005 public_key: "testkey.avbpubkey",
9006 private_key: "testkey.pem",
9007 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00009008 `,
9009 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9010 variables.CompressedApex = proptools.BoolPtr(true)
9011 }),
9012 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009013
Jooyung Hana0503a52023-08-23 13:12:50 +09009014 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("compressRule")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009015 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
9016
Jooyung Hana0503a52023-08-23 13:12:50 +09009017 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex").Description("sign compressedApex")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009018 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
9019
9020 // Make sure output of bundle is .capex
Jooyung Hana0503a52023-08-23 13:12:50 +09009021 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009022 ensureContains(t, ab.outputFile.String(), "myapex.capex")
9023
9024 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07009025 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009026 var builder strings.Builder
9027 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9028 androidMk := builder.String()
9029 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
9030}
9031
Martin Stjernholm2856c662020-12-02 15:03:42 +00009032func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009033 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00009034 apex {
9035 name: "myapex",
9036 key: "myapex.key",
9037 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009038 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00009039 }
9040
9041 apex_key {
9042 name: "myapex.key",
9043 public_key: "testkey.avbpubkey",
9044 private_key: "testkey.pem",
9045 }
9046
9047 cc_library {
9048 name: "mylib",
9049 srcs: ["mylib.cpp"],
9050 apex_available: ["myapex"],
9051 shared_libs: ["otherlib"],
9052 system_shared_libs: [],
9053 }
9054
9055 cc_library {
9056 name: "otherlib",
9057 srcs: ["mylib.cpp"],
9058 stubs: {
9059 versions: ["current"],
9060 },
9061 }
9062
9063 cc_prebuilt_library_shared {
9064 name: "otherlib",
9065 prefer: true,
9066 srcs: ["prebuilt.so"],
9067 stubs: {
9068 versions: ["current"],
9069 },
9070 }
9071 `)
9072
Jooyung Hana0503a52023-08-23 13:12:50 +09009073 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07009074 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00009075 var builder strings.Builder
9076 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9077 androidMk := builder.String()
9078
9079 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
9080 // a thing there.
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009081 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 otherlib\n")
Martin Stjernholm2856c662020-12-02 15:03:42 +00009082}
9083
Jiyong Parke3867542020-12-03 17:28:25 +09009084func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009085 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09009086 apex {
9087 name: "myapex",
9088 key: "myapex.key",
9089 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009090 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09009091 }
9092
9093 apex_key {
9094 name: "myapex.key",
9095 public_key: "testkey.avbpubkey",
9096 private_key: "testkey.pem",
9097 }
9098
9099 cc_library {
9100 name: "mylib",
9101 srcs: ["mylib.cpp"],
9102 system_shared_libs: [],
9103 stl: "none",
9104 apex_available: ["myapex"],
9105 shared_libs: ["mylib2"],
9106 target: {
9107 apex: {
9108 exclude_shared_libs: ["mylib2"],
9109 },
9110 },
9111 }
9112
9113 cc_library {
9114 name: "mylib2",
9115 srcs: ["mylib.cpp"],
9116 system_shared_libs: [],
9117 stl: "none",
9118 }
9119 `)
9120
9121 // Check if mylib is linked to mylib2 for the non-apex target
9122 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
9123 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
9124
9125 // Make sure that the link doesn't occur for the apex target
9126 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
9127 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
9128
9129 // It shouldn't appear in the copy cmd as well.
Jooyung Hana0503a52023-08-23 13:12:50 +09009130 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule").Args["copy_commands"]
Jiyong Parke3867542020-12-03 17:28:25 +09009131 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
9132}
9133
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009134func TestPrebuiltStubLibDep(t *testing.T) {
9135 bpBase := `
9136 apex {
9137 name: "myapex",
9138 key: "myapex.key",
9139 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009140 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009141 }
9142 apex_key {
9143 name: "myapex.key",
9144 public_key: "testkey.avbpubkey",
9145 private_key: "testkey.pem",
9146 }
9147 cc_library {
9148 name: "mylib",
9149 srcs: ["mylib.cpp"],
9150 apex_available: ["myapex"],
9151 shared_libs: ["stublib"],
9152 system_shared_libs: [],
9153 }
9154 apex {
9155 name: "otherapex",
9156 enabled: %s,
9157 key: "myapex.key",
9158 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009159 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009160 }
9161 `
9162
9163 stublibSourceBp := `
9164 cc_library {
9165 name: "stublib",
9166 srcs: ["mylib.cpp"],
9167 apex_available: ["otherapex"],
9168 system_shared_libs: [],
9169 stl: "none",
9170 stubs: {
9171 versions: ["1"],
9172 },
9173 }
9174 `
9175
9176 stublibPrebuiltBp := `
9177 cc_prebuilt_library_shared {
9178 name: "stublib",
9179 srcs: ["prebuilt.so"],
9180 apex_available: ["otherapex"],
9181 stubs: {
9182 versions: ["1"],
9183 },
9184 %s
9185 }
9186 `
9187
9188 tests := []struct {
9189 name string
9190 stublibBp string
9191 usePrebuilt bool
9192 modNames []string // Modules to collect AndroidMkEntries for
9193 otherApexEnabled []string
9194 }{
9195 {
9196 name: "only_source",
9197 stublibBp: stublibSourceBp,
9198 usePrebuilt: false,
9199 modNames: []string{"stublib"},
9200 otherApexEnabled: []string{"true", "false"},
9201 },
9202 {
9203 name: "source_preferred",
9204 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9205 usePrebuilt: false,
9206 modNames: []string{"stublib", "prebuilt_stublib"},
9207 otherApexEnabled: []string{"true", "false"},
9208 },
9209 {
9210 name: "prebuilt_preferred",
9211 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9212 usePrebuilt: true,
9213 modNames: []string{"stublib", "prebuilt_stublib"},
9214 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9215 },
9216 {
9217 name: "only_prebuilt",
9218 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9219 usePrebuilt: true,
9220 modNames: []string{"stublib"},
9221 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9222 },
9223 }
9224
9225 for _, test := range tests {
9226 t.Run(test.name, func(t *testing.T) {
9227 for _, otherApexEnabled := range test.otherApexEnabled {
9228 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009229 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009230
9231 type modAndMkEntries struct {
9232 mod *cc.Module
9233 mkEntries android.AndroidMkEntries
9234 }
9235 entries := []*modAndMkEntries{}
9236
9237 // Gather shared lib modules that are installable
9238 for _, modName := range test.modNames {
9239 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9240 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9241 continue
9242 }
9243 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08009244 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009245 continue
9246 }
Colin Crossaa255532020-07-03 13:18:24 -07009247 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009248 if ent.Disabled {
9249 continue
9250 }
9251 entries = append(entries, &modAndMkEntries{
9252 mod: mod,
9253 mkEntries: ent,
9254 })
9255 }
9256 }
9257 }
9258
9259 var entry *modAndMkEntries = nil
9260 for _, ent := range entries {
9261 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9262 if entry != nil {
9263 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9264 } else {
9265 entry = ent
9266 }
9267 }
9268 }
9269
9270 if entry == nil {
9271 t.Errorf("AndroidMk entry for \"stublib\" missing")
9272 } else {
9273 isPrebuilt := entry.mod.Prebuilt() != nil
9274 if isPrebuilt != test.usePrebuilt {
9275 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9276 }
9277 if !entry.mod.IsStubs() {
9278 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9279 }
9280 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9281 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9282 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009283 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009284 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009285 if !android.InList(expected, cflags) {
9286 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9287 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009288 }
9289 })
9290 }
9291 })
9292 }
9293}
9294
Colin Crossc33e5212021-05-25 18:16:02 -07009295func TestApexJavaCoverage(t *testing.T) {
9296 bp := `
9297 apex {
9298 name: "myapex",
9299 key: "myapex.key",
9300 java_libs: ["mylib"],
9301 bootclasspath_fragments: ["mybootclasspathfragment"],
9302 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9303 updatable: false,
9304 }
9305
9306 apex_key {
9307 name: "myapex.key",
9308 public_key: "testkey.avbpubkey",
9309 private_key: "testkey.pem",
9310 }
9311
9312 java_library {
9313 name: "mylib",
9314 srcs: ["mylib.java"],
9315 apex_available: ["myapex"],
9316 compile_dex: true,
9317 }
9318
9319 bootclasspath_fragment {
9320 name: "mybootclasspathfragment",
9321 contents: ["mybootclasspathlib"],
9322 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009323 hidden_api: {
9324 split_packages: ["*"],
9325 },
Colin Crossc33e5212021-05-25 18:16:02 -07009326 }
9327
9328 java_library {
9329 name: "mybootclasspathlib",
9330 srcs: ["mybootclasspathlib.java"],
9331 apex_available: ["myapex"],
9332 compile_dex: true,
9333 }
9334
9335 systemserverclasspath_fragment {
9336 name: "mysystemserverclasspathfragment",
9337 contents: ["mysystemserverclasspathlib"],
9338 apex_available: ["myapex"],
9339 }
9340
9341 java_library {
9342 name: "mysystemserverclasspathlib",
9343 srcs: ["mysystemserverclasspathlib.java"],
9344 apex_available: ["myapex"],
9345 compile_dex: true,
9346 }
9347 `
9348
9349 result := android.GroupFixturePreparers(
9350 PrepareForTestWithApexBuildComponents,
9351 prepareForTestWithMyapex,
9352 java.PrepareForTestWithJavaDefaultModules,
9353 android.PrepareForTestWithAndroidBuildComponents,
9354 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009355 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9356 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009357 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009358 ).RunTest(t)
9359
9360 // Make sure jacoco ran on both mylib and mybootclasspathlib
9361 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9362 t.Errorf("Failed to find jacoco rule for mylib")
9363 }
9364 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9365 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9366 }
9367 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9368 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9369 }
9370}
9371
Jiyong Park192600a2021-08-03 07:52:17 +00009372func TestProhibitStaticExecutable(t *testing.T) {
9373 testApexError(t, `executable mybin is static`, `
9374 apex {
9375 name: "myapex",
9376 key: "myapex.key",
9377 binaries: ["mybin"],
9378 min_sdk_version: "29",
9379 }
9380
9381 apex_key {
9382 name: "myapex.key",
9383 public_key: "testkey.avbpubkey",
9384 private_key: "testkey.pem",
9385 }
9386
9387 cc_binary {
9388 name: "mybin",
9389 srcs: ["mylib.cpp"],
9390 relative_install_path: "foo/bar",
9391 static_executable: true,
9392 system_shared_libs: [],
9393 stl: "none",
9394 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009395 min_sdk_version: "29",
9396 }
9397 `)
9398
9399 testApexError(t, `executable mybin.rust is static`, `
9400 apex {
9401 name: "myapex",
9402 key: "myapex.key",
9403 binaries: ["mybin.rust"],
9404 min_sdk_version: "29",
9405 }
9406
9407 apex_key {
9408 name: "myapex.key",
9409 public_key: "testkey.avbpubkey",
9410 private_key: "testkey.pem",
9411 }
9412
9413 rust_binary {
9414 name: "mybin.rust",
9415 srcs: ["foo.rs"],
9416 static_executable: true,
9417 apex_available: ["myapex"],
9418 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009419 }
9420 `)
9421}
9422
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009423func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9424 ctx := testApex(t, `
9425 apex {
9426 name: "myapex",
9427 key: "myapex.key",
9428 updatable: false,
9429 java_libs: ["foo"],
9430 }
9431
9432 apex_key {
9433 name: "myapex.key",
9434 public_key: "testkey.avbpubkey",
9435 private_key: "testkey.pem",
9436 }
9437
9438 java_library {
9439 name: "foo",
9440 srcs: ["foo.java"],
9441 apex_available: ["myapex"],
9442 installable: true,
9443 }
9444 `,
9445 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9446 )
9447
Jooyung Hana0503a52023-08-23 13:12:50 +09009448 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009449 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9450 var builder strings.Builder
9451 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9452 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009453 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 +00009454}
9455
9456func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9457 ctx := testApex(t, `
9458 prebuilt_apex {
9459 name: "myapex",
9460 arch: {
9461 arm64: {
9462 src: "myapex-arm64.apex",
9463 },
9464 arm: {
9465 src: "myapex-arm.apex",
9466 },
9467 },
9468 exported_java_libs: ["foo"],
9469 }
9470
9471 java_import {
9472 name: "foo",
9473 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009474 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009475 }
9476 `,
9477 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9478 )
9479
9480 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9481 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9482 mainModuleEntries := entriesList[0]
9483 android.AssertArrayString(t,
9484 "LOCAL_REQUIRED_MODULES",
9485 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9486 []string{
9487 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9488 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9489 })
9490}
9491
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009492func TestAndroidMk_RequiredModules(t *testing.T) {
9493 ctx := testApex(t, `
9494 apex {
9495 name: "myapex",
9496 key: "myapex.key",
9497 updatable: false,
9498 java_libs: ["foo"],
9499 required: ["otherapex"],
9500 }
9501
9502 apex {
9503 name: "otherapex",
9504 key: "myapex.key",
9505 updatable: false,
9506 java_libs: ["foo"],
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009507 }
9508
9509 apex_key {
9510 name: "myapex.key",
9511 public_key: "testkey.avbpubkey",
9512 private_key: "testkey.pem",
9513 }
9514
9515 java_library {
9516 name: "foo",
9517 srcs: ["foo.java"],
9518 apex_available: ["myapex", "otherapex"],
9519 installable: true,
9520 }
9521 `)
9522
Jooyung Hana0503a52023-08-23 13:12:50 +09009523 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009524 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9525 var builder strings.Builder
9526 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9527 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009528 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009529}
9530
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009531func TestAndroidMk_RequiredDeps(t *testing.T) {
9532 ctx := testApex(t, `
9533 apex {
9534 name: "myapex",
9535 key: "myapex.key",
9536 updatable: false,
9537 }
9538
9539 apex_key {
9540 name: "myapex.key",
9541 public_key: "testkey.avbpubkey",
9542 private_key: "testkey.pem",
9543 }
9544 `)
9545
Jooyung Hana0503a52023-08-23 13:12:50 +09009546 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009547 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009548 data := android.AndroidMkDataForTest(t, ctx, bundle)
9549 var builder strings.Builder
9550 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9551 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009552 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009553}
9554
Jooyung Hana6d36672022-02-24 13:58:07 +09009555func TestApexOutputFileProducer(t *testing.T) {
9556 for _, tc := range []struct {
9557 name string
9558 ref string
9559 expected_data []string
9560 }{
9561 {
9562 name: "test_using_output",
9563 ref: ":myapex",
Jooyung Hana0503a52023-08-23 13:12:50 +09009564 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.capex:myapex.capex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009565 },
9566 {
9567 name: "test_using_apex",
9568 ref: ":myapex{.apex}",
Jooyung Hana0503a52023-08-23 13:12:50 +09009569 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.apex:myapex.apex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009570 },
9571 } {
9572 t.Run(tc.name, func(t *testing.T) {
9573 ctx := testApex(t, `
9574 apex {
9575 name: "myapex",
9576 key: "myapex.key",
9577 compressible: true,
9578 updatable: false,
9579 }
9580
9581 apex_key {
9582 name: "myapex.key",
9583 public_key: "testkey.avbpubkey",
9584 private_key: "testkey.pem",
9585 }
9586
9587 java_test {
9588 name: "`+tc.name+`",
9589 srcs: ["a.java"],
9590 data: ["`+tc.ref+`"],
9591 }
9592 `,
9593 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9594 variables.CompressedApex = proptools.BoolPtr(true)
9595 }))
9596 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9597 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9598 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9599 })
9600 }
9601}
9602
satayev758968a2021-12-06 11:42:40 +00009603func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9604 preparer := android.GroupFixturePreparers(
9605 PrepareForTestWithApexBuildComponents,
9606 prepareForTestWithMyapex,
9607 java.PrepareForTestWithJavaSdkLibraryFiles,
9608 java.PrepareForTestWithJavaDefaultModules,
9609 android.PrepareForTestWithAndroidBuildComponents,
9610 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9611 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9612 )
9613
9614 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9615 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9616 preparer.RunTestWithBp(t, `
9617 apex {
9618 name: "myapex",
9619 key: "myapex.key",
9620 bootclasspath_fragments: ["mybootclasspathfragment"],
9621 min_sdk_version: "30",
9622 updatable: false,
9623 }
9624
9625 apex_key {
9626 name: "myapex.key",
9627 public_key: "testkey.avbpubkey",
9628 private_key: "testkey.pem",
9629 }
9630
9631 bootclasspath_fragment {
9632 name: "mybootclasspathfragment",
9633 contents: ["mybootclasspathlib"],
9634 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009635 hidden_api: {
9636 split_packages: ["*"],
9637 },
satayev758968a2021-12-06 11:42:40 +00009638 }
9639
9640 java_sdk_library {
9641 name: "mybootclasspathlib",
9642 srcs: ["mybootclasspathlib.java"],
9643 apex_available: ["myapex"],
9644 compile_dex: true,
9645 unsafe_ignore_missing_latest_api: true,
9646 min_sdk_version: "31",
9647 static_libs: ["util"],
9648 }
9649
9650 java_library {
9651 name: "util",
9652 srcs: ["a.java"],
9653 apex_available: ["myapex"],
9654 min_sdk_version: "31",
9655 static_libs: ["another_util"],
9656 }
9657
9658 java_library {
9659 name: "another_util",
9660 srcs: ["a.java"],
9661 min_sdk_version: "31",
9662 apex_available: ["myapex"],
9663 }
9664 `)
9665 })
9666
9667 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9668 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9669 preparer.RunTestWithBp(t, `
9670 apex {
9671 name: "myapex",
9672 key: "myapex.key",
9673 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9674 min_sdk_version: "30",
9675 updatable: false,
9676 }
9677
9678 apex_key {
9679 name: "myapex.key",
9680 public_key: "testkey.avbpubkey",
9681 private_key: "testkey.pem",
9682 }
9683
9684 systemserverclasspath_fragment {
9685 name: "mysystemserverclasspathfragment",
9686 contents: ["mysystemserverclasspathlib"],
9687 apex_available: ["myapex"],
9688 }
9689
9690 java_sdk_library {
9691 name: "mysystemserverclasspathlib",
9692 srcs: ["mysystemserverclasspathlib.java"],
9693 apex_available: ["myapex"],
9694 compile_dex: true,
9695 min_sdk_version: "32",
9696 unsafe_ignore_missing_latest_api: true,
9697 static_libs: ["util"],
9698 }
9699
9700 java_library {
9701 name: "util",
9702 srcs: ["a.java"],
9703 apex_available: ["myapex"],
9704 min_sdk_version: "31",
9705 static_libs: ["another_util"],
9706 }
9707
9708 java_library {
9709 name: "another_util",
9710 srcs: ["a.java"],
9711 min_sdk_version: "31",
9712 apex_available: ["myapex"],
9713 }
9714 `)
9715 })
9716
9717 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9718 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9719 RunTestWithBp(t, `
9720 apex {
9721 name: "myapex",
9722 key: "myapex.key",
9723 bootclasspath_fragments: ["mybootclasspathfragment"],
9724 min_sdk_version: "30",
9725 updatable: false,
9726 }
9727
9728 apex_key {
9729 name: "myapex.key",
9730 public_key: "testkey.avbpubkey",
9731 private_key: "testkey.pem",
9732 }
9733
9734 bootclasspath_fragment {
9735 name: "mybootclasspathfragment",
9736 contents: ["mybootclasspathlib"],
9737 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009738 hidden_api: {
9739 split_packages: ["*"],
9740 },
satayev758968a2021-12-06 11:42:40 +00009741 }
9742
9743 java_sdk_library {
9744 name: "mybootclasspathlib",
9745 srcs: ["mybootclasspathlib.java"],
9746 apex_available: ["myapex"],
9747 compile_dex: true,
9748 unsafe_ignore_missing_latest_api: true,
9749 }
9750 `)
9751 })
9752
9753 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9754 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9755 RunTestWithBp(t, `
9756 apex {
9757 name: "myapex",
9758 key: "myapex.key",
9759 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9760 min_sdk_version: "30",
9761 updatable: false,
9762 }
9763
9764 apex_key {
9765 name: "myapex.key",
9766 public_key: "testkey.avbpubkey",
9767 private_key: "testkey.pem",
9768 }
9769
9770 systemserverclasspath_fragment {
9771 name: "mysystemserverclasspathfragment",
9772 contents: ["mysystemserverclasspathlib"],
9773 apex_available: ["myapex"],
9774 }
9775
9776 java_sdk_library {
9777 name: "mysystemserverclasspathlib",
9778 srcs: ["mysystemserverclasspathlib.java"],
9779 apex_available: ["myapex"],
9780 compile_dex: true,
9781 unsafe_ignore_missing_latest_api: true,
9782 }
9783 `)
9784 })
9785}
9786
Jiakai Zhang6decef92022-01-12 17:56:19 +00009787// Verifies that the APEX depends on all the Make modules in the list.
9788func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9789 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9790 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009791 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009792 }
9793}
9794
9795// Verifies that the APEX does not depend on any of the Make modules in the list.
9796func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9797 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9798 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009799 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009800 }
9801}
9802
Cole Faust1021ccd2023-02-26 21:15:25 -08009803// TODO(b/193460475): Re-enable this test
9804//func TestApexStrictUpdtabilityLint(t *testing.T) {
9805// bpTemplate := `
9806// apex {
9807// name: "myapex",
9808// key: "myapex.key",
9809// java_libs: ["myjavalib"],
9810// updatable: %v,
9811// min_sdk_version: "29",
9812// }
9813// apex_key {
9814// name: "myapex.key",
9815// }
9816// java_library {
9817// name: "myjavalib",
9818// srcs: ["MyClass.java"],
9819// apex_available: [ "myapex" ],
9820// lint: {
9821// strict_updatability_linting: %v,
9822// },
9823// sdk_version: "current",
9824// min_sdk_version: "29",
9825// }
9826// `
9827// fs := android.MockFS{
9828// "lint-baseline.xml": nil,
9829// }
9830//
9831// testCases := []struct {
9832// testCaseName string
9833// apexUpdatable bool
9834// javaStrictUpdtabilityLint bool
9835// lintFileExists bool
9836// disallowedFlagExpected bool
9837// }{
9838// {
9839// testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9840// apexUpdatable: true,
9841// javaStrictUpdtabilityLint: true,
9842// lintFileExists: false,
9843// disallowedFlagExpected: false,
9844// },
9845// {
9846// testCaseName: "non-updatable apex respects strict_updatability of javalib",
9847// apexUpdatable: false,
9848// javaStrictUpdtabilityLint: false,
9849// lintFileExists: true,
9850// disallowedFlagExpected: false,
9851// },
9852// {
9853// testCaseName: "non-updatable apex respects strict updatability of javalib",
9854// apexUpdatable: false,
9855// javaStrictUpdtabilityLint: true,
9856// lintFileExists: true,
9857// disallowedFlagExpected: true,
9858// },
9859// {
9860// testCaseName: "updatable apex sets strict updatability of javalib to true",
9861// apexUpdatable: true,
9862// javaStrictUpdtabilityLint: false, // will be set to true by mutator
9863// lintFileExists: true,
9864// disallowedFlagExpected: true,
9865// },
9866// }
9867//
9868// for _, testCase := range testCases {
9869// bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9870// fixtures := []android.FixturePreparer{}
9871// if testCase.lintFileExists {
9872// fixtures = append(fixtures, fs.AddToFixture())
9873// }
9874//
9875// result := testApex(t, bp, fixtures...)
9876// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9877// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9878// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9879//
9880// if disallowedFlagActual != testCase.disallowedFlagExpected {
9881// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9882// }
9883// }
9884//}
9885//
9886//func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9887// bp := `
9888// apex {
9889// name: "myapex",
9890// key: "myapex.key",
9891// java_libs: ["myjavalib"],
9892// updatable: true,
9893// min_sdk_version: "29",
9894// }
9895// apex_key {
9896// name: "myapex.key",
9897// }
9898// java_library {
9899// name: "myjavalib",
9900// srcs: ["MyClass.java"],
9901// apex_available: [ "myapex" ],
9902// sdk_version: "current",
9903// min_sdk_version: "29",
9904// }
9905// `
9906//
9907// testCases := []struct {
9908// testCaseName string
9909// moduleDirectory string
9910// disallowedFlagExpected bool
9911// }{
9912// {
9913// testCaseName: "lintable module defined outside libcore",
9914// moduleDirectory: "",
9915// disallowedFlagExpected: true,
9916// },
9917// {
9918// testCaseName: "lintable module defined in libcore root directory",
9919// moduleDirectory: "libcore/",
9920// disallowedFlagExpected: false,
9921// },
9922// {
9923// testCaseName: "lintable module defined in libcore child directory",
9924// moduleDirectory: "libcore/childdir/",
9925// disallowedFlagExpected: true,
9926// },
9927// }
9928//
9929// for _, testCase := range testCases {
9930// lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9931// bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9932// result := testApex(t, "", lintFileCreator, bpFileCreator)
9933// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9934// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9935// cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9936// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9937//
9938// if disallowedFlagActual != testCase.disallowedFlagExpected {
9939// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9940// }
9941// }
9942//}
9943//
9944//// checks transtive deps of an apex coming from bootclasspath_fragment
9945//func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9946// bp := `
9947// apex {
9948// name: "myapex",
9949// key: "myapex.key",
9950// bootclasspath_fragments: ["mybootclasspathfragment"],
9951// updatable: true,
9952// min_sdk_version: "29",
9953// }
9954// apex_key {
9955// name: "myapex.key",
9956// }
9957// bootclasspath_fragment {
9958// name: "mybootclasspathfragment",
9959// contents: ["myjavalib"],
9960// apex_available: ["myapex"],
9961// hidden_api: {
9962// split_packages: ["*"],
9963// },
9964// }
9965// java_library {
9966// name: "myjavalib",
9967// srcs: ["MyClass.java"],
9968// apex_available: [ "myapex" ],
9969// sdk_version: "current",
9970// min_sdk_version: "29",
9971// compile_dex: true,
9972// }
9973// `
9974// fs := android.MockFS{
9975// "lint-baseline.xml": nil,
9976// }
9977//
9978// result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9979// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9980// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9981// if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9982// t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9983// }
9984//}
Spandan Das66773252022-01-15 00:23:18 +00009985
Spandan Das42e89502022-05-06 22:12:55 +00009986// updatable apexes should propagate updatable=true to its apps
9987func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9988 bp := `
9989 apex {
9990 name: "myapex",
9991 key: "myapex.key",
9992 updatable: %v,
9993 apps: [
9994 "myapp",
9995 ],
9996 min_sdk_version: "30",
9997 }
9998 apex_key {
9999 name: "myapex.key",
10000 }
10001 android_app {
10002 name: "myapp",
10003 updatable: %v,
10004 apex_available: [
10005 "myapex",
10006 ],
10007 sdk_version: "current",
10008 min_sdk_version: "30",
10009 }
10010 `
10011 testCases := []struct {
10012 name string
10013 apex_is_updatable_bp bool
10014 app_is_updatable_bp bool
10015 app_is_updatable_expected bool
10016 }{
10017 {
10018 name: "Non-updatable apex respects updatable property of non-updatable app",
10019 apex_is_updatable_bp: false,
10020 app_is_updatable_bp: false,
10021 app_is_updatable_expected: false,
10022 },
10023 {
10024 name: "Non-updatable apex respects updatable property of updatable app",
10025 apex_is_updatable_bp: false,
10026 app_is_updatable_bp: true,
10027 app_is_updatable_expected: true,
10028 },
10029 {
10030 name: "Updatable apex respects updatable property of updatable app",
10031 apex_is_updatable_bp: true,
10032 app_is_updatable_bp: true,
10033 app_is_updatable_expected: true,
10034 },
10035 {
10036 name: "Updatable apex sets updatable=true on non-updatable app",
10037 apex_is_updatable_bp: true,
10038 app_is_updatable_bp: false,
10039 app_is_updatable_expected: true,
10040 },
10041 }
10042 for _, testCase := range testCases {
10043 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
10044 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
10045 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
10046 }
10047}
10048
Kiyoung Kim487689e2022-07-26 09:48:22 +090010049func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10050 bp := `
10051 apex {
10052 name: "myapex",
10053 key: "myapex.key",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010054 native_shared_libs: ["libbaz"],
10055 binaries: ["binfoo"],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010056 min_sdk_version: "29",
10057 }
10058 apex_key {
10059 name: "myapex.key",
10060 }
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010061 cc_binary {
10062 name: "binfoo",
10063 shared_libs: ["libbar", "libbaz", "libqux",],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010064 apex_available: ["myapex"],
10065 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010066 recovery_available: false,
10067 }
10068 cc_library {
10069 name: "libbar",
10070 srcs: ["libbar.cc"],
10071 stubs: {
10072 symbol_file: "libbar.map.txt",
10073 versions: [
10074 "29",
10075 ],
10076 },
10077 }
10078 cc_library {
10079 name: "libbaz",
10080 srcs: ["libbaz.cc"],
10081 apex_available: ["myapex"],
10082 min_sdk_version: "29",
10083 stubs: {
10084 symbol_file: "libbaz.map.txt",
10085 versions: [
10086 "29",
10087 ],
10088 },
Kiyoung Kim487689e2022-07-26 09:48:22 +090010089 }
10090 cc_api_library {
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010091 name: "libbar",
10092 src: "libbar_stub.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010093 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010094 variants: ["apex.29"],
10095 }
10096 cc_api_variant {
10097 name: "libbar",
10098 variant: "apex",
10099 version: "29",
10100 src: "libbar_apex_29.so",
10101 }
10102 cc_api_library {
10103 name: "libbaz",
10104 src: "libbaz_stub.so",
10105 min_sdk_version: "29",
10106 variants: ["apex.29"],
10107 }
10108 cc_api_variant {
10109 name: "libbaz",
10110 variant: "apex",
10111 version: "29",
10112 src: "libbaz_apex_29.so",
10113 }
10114 cc_api_library {
10115 name: "libqux",
10116 src: "libqux_stub.so",
10117 min_sdk_version: "29",
10118 variants: ["apex.29"],
10119 }
10120 cc_api_variant {
10121 name: "libqux",
10122 variant: "apex",
10123 version: "29",
10124 src: "libqux_apex_29.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010125 }
10126 api_imports {
10127 name: "api_imports",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010128 apex_shared_libs: [
10129 "libbar",
10130 "libbaz",
10131 "libqux",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010132 ],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010133 }
10134 `
10135 result := testApex(t, bp)
10136
10137 hasDep := func(m android.Module, wantDep android.Module) bool {
10138 t.Helper()
10139 var found bool
10140 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10141 if dep == wantDep {
10142 found = true
10143 }
10144 })
10145 return found
10146 }
10147
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010148 // Library defines stubs and cc_api_library should be used with cc_api_library
10149 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Module()
10150 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10151 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
Kiyoung Kim487689e2022-07-26 09:48:22 +090010152
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010153 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10154 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
Kiyoung Kim487689e2022-07-26 09:48:22 +090010155
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010156 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Rule("ld").Args["libFlags"]
10157 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10158 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10159 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
10160
10161 // Library defined in the same APEX should be linked with original definition instead of cc_api_library
10162 libbazApexVariant := result.ModuleForTests("libbaz", "android_arm64_armv8-a_shared_apex29").Module()
10163 libbazApiImportCoreVariant := result.ModuleForTests("libbaz.apiimport", "android_arm64_armv8-a_shared").Module()
10164 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even from same APEX", true, hasDep(binfooApexVariant, libbazApiImportCoreVariant))
10165 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbazApexVariant))
10166
10167 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbaz.so")
10168 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbaz.apiimport.so")
10169 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbaz.apex.29.apiimport.so")
10170
10171 // cc_api_library defined without original library should be linked with cc_api_library
10172 libquxApiImportApexVariant := result.ModuleForTests("libqux.apiimport", "android_arm64_armv8-a_shared").Module()
10173 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even original library definition does not exist", true, hasDep(binfooApexVariant, libquxApiImportApexVariant))
10174 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libqux.apex.29.apiimport.so")
10175}
10176
10177func TestPlatformBinaryBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10178 bp := `
10179 apex {
10180 name: "myapex",
10181 key: "myapex.key",
10182 native_shared_libs: ["libbar"],
10183 min_sdk_version: "29",
10184 }
10185 apex_key {
10186 name: "myapex.key",
10187 }
10188 cc_binary {
10189 name: "binfoo",
10190 shared_libs: ["libbar"],
10191 recovery_available: false,
10192 }
10193 cc_library {
10194 name: "libbar",
10195 srcs: ["libbar.cc"],
10196 apex_available: ["myapex"],
10197 min_sdk_version: "29",
10198 stubs: {
10199 symbol_file: "libbar.map.txt",
10200 versions: [
10201 "29",
10202 ],
10203 },
10204 }
10205 cc_api_library {
10206 name: "libbar",
10207 src: "libbar_stub.so",
10208 variants: ["apex.29"],
10209 }
10210 cc_api_variant {
10211 name: "libbar",
10212 variant: "apex",
10213 version: "29",
10214 src: "libbar_apex_29.so",
10215 }
10216 api_imports {
10217 name: "api_imports",
10218 apex_shared_libs: [
10219 "libbar",
10220 ],
10221 }
10222 `
10223
10224 result := testApex(t, bp)
10225
10226 hasDep := func(m android.Module, wantDep android.Module) bool {
10227 t.Helper()
10228 var found bool
10229 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10230 if dep == wantDep {
10231 found = true
10232 }
10233 })
10234 return found
10235 }
10236
10237 // Library defines stubs and cc_api_library should be used with cc_api_library
10238 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Module()
10239 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10240 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
10241
10242 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10243 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
10244
10245 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
10246 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10247 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10248 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
Kiyoung Kim487689e2022-07-26 09:48:22 +090010249}
Dennis Shend4f5d932023-01-31 20:27:21 +000010250
10251func TestTrimmedApex(t *testing.T) {
10252 bp := `
10253 apex {
10254 name: "myapex",
10255 key: "myapex.key",
10256 native_shared_libs: ["libfoo","libbaz"],
10257 min_sdk_version: "29",
10258 trim_against: "mydcla",
10259 }
10260 apex {
10261 name: "mydcla",
10262 key: "myapex.key",
10263 native_shared_libs: ["libfoo","libbar"],
10264 min_sdk_version: "29",
10265 file_contexts: ":myapex-file_contexts",
10266 dynamic_common_lib_apex: true,
10267 }
10268 apex_key {
10269 name: "myapex.key",
10270 }
10271 cc_library {
10272 name: "libfoo",
10273 shared_libs: ["libc"],
10274 apex_available: ["myapex","mydcla"],
10275 min_sdk_version: "29",
10276 }
10277 cc_library {
10278 name: "libbar",
10279 shared_libs: ["libc"],
10280 apex_available: ["myapex","mydcla"],
10281 min_sdk_version: "29",
10282 }
10283 cc_library {
10284 name: "libbaz",
10285 shared_libs: ["libc"],
10286 apex_available: ["myapex","mydcla"],
10287 min_sdk_version: "29",
10288 }
10289 cc_api_library {
10290 name: "libc",
10291 src: "libc.so",
10292 min_sdk_version: "29",
10293 recovery_available: true,
Ivan Lozanoadd122a2023-07-13 11:01:41 -040010294 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +090010295 product_available: true,
Dennis Shend4f5d932023-01-31 20:27:21 +000010296 }
10297 api_imports {
10298 name: "api_imports",
10299 shared_libs: [
10300 "libc",
10301 ],
10302 header_libs: [],
10303 }
10304 `
10305 ctx := testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +090010306 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dennis Shend4f5d932023-01-31 20:27:21 +000010307 apexRule := module.MaybeRule("apexRule")
10308 if apexRule.Rule == nil {
10309 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10310 }
10311
10312 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
Jooyung Hana0503a52023-08-23 13:12:50 +090010313 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("TrimmedApexRule")
Dennis Shend4f5d932023-01-31 20:27:21 +000010314 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10315 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10316 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10317 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10318}
Jingwen Chendea7a642023-03-28 11:30:50 +000010319
10320func TestCannedFsConfig(t *testing.T) {
10321 ctx := testApex(t, `
10322 apex {
10323 name: "myapex",
10324 key: "myapex.key",
10325 updatable: false,
10326 }
10327
10328 apex_key {
10329 name: "myapex.key",
10330 public_key: "testkey.avbpubkey",
10331 private_key: "testkey.pem",
10332 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010333 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010334 generateFsRule := mod.Rule("generateFsConfig")
10335 cmd := generateFsRule.RuleParams.Command
10336
10337 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10338}
10339
10340func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10341 ctx := testApex(t, `
10342 apex {
10343 name: "myapex",
10344 key: "myapex.key",
10345 canned_fs_config: "my_config",
10346 updatable: false,
10347 }
10348
10349 apex_key {
10350 name: "myapex.key",
10351 public_key: "testkey.avbpubkey",
10352 private_key: "testkey.pem",
10353 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010354 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010355 generateFsRule := mod.Rule("generateFsConfig")
10356 cmd := generateFsRule.RuleParams.Command
10357
10358 // Ensure that canned_fs_config has "cat my_config" at the end
10359 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10360}
Spandan Das20fce2d2023-04-12 17:21:39 +000010361
10362func TestStubLibrariesMultipleApexViolation(t *testing.T) {
10363 testCases := []struct {
10364 desc string
10365 hasStubs bool
10366 apexAvailable string
10367 expectedError string
10368 }{
10369 {
10370 desc: "non-stub library can have multiple apex_available",
10371 hasStubs: false,
10372 apexAvailable: `["myapex", "otherapex"]`,
10373 },
10374 {
10375 desc: "stub library should not be available to anyapex",
10376 hasStubs: true,
10377 apexAvailable: `["//apex_available:anyapex"]`,
10378 expectedError: "Stub libraries should have a single apex_available.*anyapex",
10379 },
10380 {
10381 desc: "stub library should not be available to multiple apexes",
10382 hasStubs: true,
10383 apexAvailable: `["myapex", "otherapex"]`,
10384 expectedError: "Stub libraries should have a single apex_available.*myapex.*otherapex",
10385 },
10386 {
10387 desc: "stub library can be available to a core apex and a test apex",
10388 hasStubs: true,
10389 apexAvailable: `["myapex", "test_myapex"]`,
10390 },
10391 }
10392 bpTemplate := `
10393 cc_library {
10394 name: "libfoo",
10395 %v
10396 apex_available: %v,
10397 }
10398 apex {
10399 name: "myapex",
10400 key: "apex.key",
10401 updatable: false,
10402 native_shared_libs: ["libfoo"],
10403 }
10404 apex {
10405 name: "otherapex",
10406 key: "apex.key",
10407 updatable: false,
10408 }
10409 apex_test {
10410 name: "test_myapex",
10411 key: "apex.key",
10412 updatable: false,
10413 native_shared_libs: ["libfoo"],
10414 }
10415 apex_key {
10416 name: "apex.key",
10417 }
10418 `
10419 for _, tc := range testCases {
10420 stubs := ""
10421 if tc.hasStubs {
10422 stubs = `stubs: {symbol_file: "libfoo.map.txt"},`
10423 }
10424 bp := fmt.Sprintf(bpTemplate, stubs, tc.apexAvailable)
10425 mockFsFixturePreparer := android.FixtureModifyMockFS(func(fs android.MockFS) {
10426 fs["system/sepolicy/apex/test_myapex-file_contexts"] = nil
10427 })
10428 if tc.expectedError == "" {
10429 testApex(t, bp, mockFsFixturePreparer)
10430 } else {
10431 testApexError(t, tc.expectedError, bp, mockFsFixturePreparer)
10432 }
10433 }
10434}
Colin Crossbd3a16b2023-04-25 11:30:51 -070010435
10436func TestFileSystemShouldSkipApexLibraries(t *testing.T) {
10437 context := android.GroupFixturePreparers(
10438 android.PrepareForIntegrationTestWithAndroid,
10439 cc.PrepareForIntegrationTestWithCc,
10440 PrepareForTestWithApexBuildComponents,
10441 prepareForTestWithMyapex,
10442 filesystem.PrepareForTestWithFilesystemBuildComponents,
10443 )
10444 result := context.RunTestWithBp(t, `
10445 android_system_image {
10446 name: "myfilesystem",
10447 deps: [
10448 "libfoo",
10449 ],
10450 linker_config_src: "linker.config.json",
10451 }
10452
10453 cc_library {
10454 name: "libfoo",
10455 shared_libs: [
10456 "libbar",
10457 ],
10458 stl: "none",
10459 }
10460
10461 cc_library {
10462 name: "libbar",
10463 stl: "none",
10464 apex_available: ["myapex"],
10465 }
10466
10467 apex {
10468 name: "myapex",
10469 native_shared_libs: ["libbar"],
10470 key: "myapex.key",
10471 updatable: false,
10472 }
10473
10474 apex_key {
10475 name: "myapex.key",
10476 public_key: "testkey.avbpubkey",
10477 private_key: "testkey.pem",
10478 }
10479 `)
10480
Cole Faust3b806d32024-03-11 15:15:03 -070010481 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits
Colin Crossbd3a16b2023-04-25 11:30:51 -070010482 android.AssertStringListDoesNotContain(t, "filesystem should not have libbar",
10483 inputs.Strings(),
10484 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
10485}
Yu Liueae7b362023-11-16 17:05:47 -080010486
10487var apex_default_bp = `
10488 apex_key {
10489 name: "myapex.key",
10490 public_key: "testkey.avbpubkey",
10491 private_key: "testkey.pem",
10492 }
10493
10494 filegroup {
10495 name: "myapex.manifest",
10496 srcs: ["apex_manifest.json"],
10497 }
10498
10499 filegroup {
10500 name: "myapex.androidmanifest",
10501 srcs: ["AndroidManifest.xml"],
10502 }
10503`
10504
10505func TestAconfigFilesJavaDeps(t *testing.T) {
10506 ctx := testApex(t, apex_default_bp+`
10507 apex {
10508 name: "myapex",
10509 manifest: ":myapex.manifest",
10510 androidManifest: ":myapex.androidmanifest",
10511 key: "myapex.key",
10512 java_libs: [
10513 "my_java_library_foo",
10514 "my_java_library_bar",
10515 ],
10516 updatable: false,
10517 }
10518
10519 java_library {
10520 name: "my_java_library_foo",
10521 srcs: ["foo/bar/MyClass.java"],
10522 sdk_version: "none",
10523 system_modules: "none",
10524 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010525 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010526 "myapex",
10527 ],
10528 }
10529
10530 java_library {
10531 name: "my_java_library_bar",
10532 srcs: ["foo/bar/MyClass.java"],
10533 sdk_version: "none",
10534 system_modules: "none",
10535 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010536 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010537 "myapex",
10538 ],
10539 }
10540
10541 aconfig_declarations {
10542 name: "my_aconfig_declarations_foo",
10543 package: "com.example.package",
10544 container: "myapex",
10545 srcs: ["foo.aconfig"],
10546 }
10547
10548 java_aconfig_library {
10549 name: "my_java_aconfig_library_foo",
10550 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010551 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010552 "myapex",
10553 ],
10554 }
10555
10556 aconfig_declarations {
10557 name: "my_aconfig_declarations_bar",
10558 package: "com.example.package",
10559 container: "myapex",
10560 srcs: ["bar.aconfig"],
10561 }
10562
10563 java_aconfig_library {
10564 name: "my_java_aconfig_library_bar",
10565 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010566 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010567 "myapex",
10568 ],
10569 }
10570 `)
10571
10572 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10573 s := mod.Rule("apexRule").Args["copy_commands"]
10574 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Yu Liubba555e2024-02-17 00:36:42 +000010575 if len(copyCmds) != 8 {
Yu Liueae7b362023-11-16 17:05:47 -080010576 t.Fatalf("Expected 5 commands, got %d in:\n%s", len(copyCmds), s)
10577 }
10578
Yu Liuab31c822024-02-28 22:21:31 +000010579 ensureMatches(t, copyCmds[4], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10580 ensureMatches(t, copyCmds[5], "^cp -f .*/package.map .*/image.apex/etc$")
10581 ensureMatches(t, copyCmds[6], "^cp -f .*/flag.map .*/image.apex/etc$")
10582 ensureMatches(t, copyCmds[7], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liueae7b362023-11-16 17:05:47 -080010583
Yu Liubba555e2024-02-17 00:36:42 +000010584 inputs := []string{
10585 "my_aconfig_declarations_foo/intermediate.pb",
10586 "my_aconfig_declarations_bar/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010587 }
Yu Liubba555e2024-02-17 00:36:42 +000010588 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10589 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10590 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10591 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010592}
10593
10594func TestAconfigFilesJavaAndCcDeps(t *testing.T) {
10595 ctx := testApex(t, apex_default_bp+`
10596 apex {
10597 name: "myapex",
10598 manifest: ":myapex.manifest",
10599 androidManifest: ":myapex.androidmanifest",
10600 key: "myapex.key",
10601 java_libs: [
10602 "my_java_library_foo",
10603 ],
10604 native_shared_libs: [
10605 "my_cc_library_bar",
10606 ],
10607 binaries: [
10608 "my_cc_binary_baz",
10609 ],
10610 updatable: false,
10611 }
10612
10613 java_library {
10614 name: "my_java_library_foo",
10615 srcs: ["foo/bar/MyClass.java"],
10616 sdk_version: "none",
10617 system_modules: "none",
10618 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010619 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010620 "myapex",
10621 ],
10622 }
10623
10624 cc_library {
10625 name: "my_cc_library_bar",
10626 srcs: ["foo/bar/MyClass.cc"],
Yu Liucec0e412023-11-30 16:45:50 -080010627 static_libs: [
10628 "my_cc_aconfig_library_bar",
10629 "my_cc_aconfig_library_baz",
10630 ],
Yu Liueae7b362023-11-16 17:05:47 -080010631 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010632 "myapex",
10633 ],
10634 }
10635
10636 cc_binary {
10637 name: "my_cc_binary_baz",
10638 srcs: ["foo/bar/MyClass.cc"],
10639 static_libs: ["my_cc_aconfig_library_baz"],
Yu Liueae7b362023-11-16 17:05:47 -080010640 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010641 "myapex",
10642 ],
10643 }
10644
10645 aconfig_declarations {
10646 name: "my_aconfig_declarations_foo",
10647 package: "com.example.package",
10648 container: "myapex",
10649 srcs: ["foo.aconfig"],
10650 }
10651
10652 java_aconfig_library {
10653 name: "my_java_aconfig_library_foo",
10654 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010655 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010656 "myapex",
10657 ],
10658 }
10659
10660 aconfig_declarations {
10661 name: "my_aconfig_declarations_bar",
10662 package: "com.example.package",
10663 container: "myapex",
10664 srcs: ["bar.aconfig"],
10665 }
10666
10667 cc_aconfig_library {
10668 name: "my_cc_aconfig_library_bar",
10669 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010670 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010671 "myapex",
10672 ],
10673 }
10674
10675 aconfig_declarations {
10676 name: "my_aconfig_declarations_baz",
10677 package: "com.example.package",
10678 container: "myapex",
10679 srcs: ["baz.aconfig"],
10680 }
10681
10682 cc_aconfig_library {
10683 name: "my_cc_aconfig_library_baz",
10684 aconfig_declarations: "my_aconfig_declarations_baz",
Yu Liueae7b362023-11-16 17:05:47 -080010685 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010686 "myapex",
10687 ],
10688 }
10689
10690 cc_library {
10691 name: "server_configurable_flags",
10692 srcs: ["server_configurable_flags.cc"],
10693 }
Ted Bauerf0f18592024-04-23 18:25:26 +000010694 cc_library {
10695 name: "libbase",
10696 srcs: ["libbase.cc"],
10697 }
10698 cc_library {
10699 name: "libaconfig_storage_read_api_cc",
10700 srcs: ["libaconfig_storage_read_api_cc.cc"],
10701 }
10702 cc_library {
10703 name: "libaconfig_storage_protos_cc",
10704 srcs: ["libaconfig_storage_protos_cc.cc"],
10705 }
Yu Liueae7b362023-11-16 17:05:47 -080010706 `)
10707
10708 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10709 s := mod.Rule("apexRule").Args["copy_commands"]
10710 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Yu Liubba555e2024-02-17 00:36:42 +000010711 if len(copyCmds) != 12 {
10712 t.Fatalf("Expected 12 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010713 }
10714
Yu Liuab31c822024-02-28 22:21:31 +000010715 ensureMatches(t, copyCmds[8], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10716 ensureMatches(t, copyCmds[9], "^cp -f .*/package.map .*/image.apex/etc$")
10717 ensureMatches(t, copyCmds[10], "^cp -f .*/flag.map .*/image.apex/etc$")
10718 ensureMatches(t, copyCmds[11], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liueae7b362023-11-16 17:05:47 -080010719
Yu Liubba555e2024-02-17 00:36:42 +000010720 inputs := []string{
10721 "my_aconfig_declarations_foo/intermediate.pb",
10722 "my_cc_library_bar/android_arm64_armv8-a_shared_apex10000/myapex/aconfig_merged.pb",
10723 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010724 }
Yu Liubba555e2024-02-17 00:36:42 +000010725 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10726 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10727 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10728 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010729}
10730
Yu Liucec0e412023-11-30 16:45:50 -080010731func TestAconfigFilesRustDeps(t *testing.T) {
10732 ctx := testApex(t, apex_default_bp+`
10733 apex {
10734 name: "myapex",
10735 manifest: ":myapex.manifest",
10736 androidManifest: ":myapex.androidmanifest",
10737 key: "myapex.key",
10738 native_shared_libs: [
10739 "libmy_rust_library",
10740 ],
10741 binaries: [
10742 "my_rust_binary",
10743 ],
10744 rust_dyn_libs: [
10745 "libmy_rust_dylib",
10746 ],
10747 updatable: false,
10748 }
10749
10750 rust_library {
10751 name: "libflags_rust", // test mock
10752 crate_name: "flags_rust",
10753 srcs: ["lib.rs"],
10754 apex_available: [
10755 "myapex",
10756 ],
10757 }
10758
10759 rust_library {
10760 name: "liblazy_static", // test mock
10761 crate_name: "lazy_static",
10762 srcs: ["src/lib.rs"],
10763 apex_available: [
10764 "myapex",
10765 ],
10766 }
10767
Ted Bauer02d475c2024-03-27 20:56:26 +000010768 rust_library {
10769 name: "libaconfig_storage_read_api", // test mock
10770 crate_name: "aconfig_storage_read_api",
10771 srcs: ["src/lib.rs"],
10772 apex_available: [
10773 "myapex",
10774 ],
10775 }
10776
Ted Bauer6ef40db2024-03-29 14:04:10 +000010777 rust_library {
10778 name: "liblogger", // test mock
10779 crate_name: "logger",
10780 srcs: ["src/lib.rs"],
10781 apex_available: [
10782 "myapex",
10783 ],
10784 }
10785
10786 rust_library {
10787 name: "liblog_rust", // test mock
10788 crate_name: "log_rust",
10789 srcs: ["src/lib.rs"],
10790 apex_available: [
10791 "myapex",
10792 ],
10793 }
10794
Yu Liucec0e412023-11-30 16:45:50 -080010795 rust_ffi_shared {
10796 name: "libmy_rust_library",
10797 srcs: ["src/lib.rs"],
10798 rustlibs: ["libmy_rust_aconfig_library_foo"],
10799 crate_name: "my_rust_library",
10800 apex_available: [
10801 "myapex",
10802 ],
10803 }
10804
10805 rust_library_dylib {
10806 name: "libmy_rust_dylib",
10807 srcs: ["foo/bar/MyClass.rs"],
10808 rustlibs: ["libmy_rust_aconfig_library_bar"],
10809 crate_name: "my_rust_dylib",
10810 apex_available: [
10811 "myapex",
10812 ],
10813 }
10814
10815 rust_binary {
10816 name: "my_rust_binary",
10817 srcs: ["foo/bar/MyClass.rs"],
10818 rustlibs: [
10819 "libmy_rust_aconfig_library_baz",
10820 "libmy_rust_dylib",
10821 ],
10822 apex_available: [
10823 "myapex",
10824 ],
10825 }
10826
10827 aconfig_declarations {
10828 name: "my_aconfig_declarations_foo",
10829 package: "com.example.package",
10830 container: "myapex",
10831 srcs: ["foo.aconfig"],
10832 }
10833
10834 aconfig_declarations {
10835 name: "my_aconfig_declarations_bar",
10836 package: "com.example.package",
10837 container: "myapex",
10838 srcs: ["bar.aconfig"],
10839 }
10840
10841 aconfig_declarations {
10842 name: "my_aconfig_declarations_baz",
10843 package: "com.example.package",
10844 container: "myapex",
10845 srcs: ["baz.aconfig"],
10846 }
10847
10848 rust_aconfig_library {
10849 name: "libmy_rust_aconfig_library_foo",
10850 aconfig_declarations: "my_aconfig_declarations_foo",
10851 crate_name: "my_rust_aconfig_library_foo",
10852 apex_available: [
10853 "myapex",
10854 ],
10855 }
10856
10857 rust_aconfig_library {
10858 name: "libmy_rust_aconfig_library_bar",
10859 aconfig_declarations: "my_aconfig_declarations_bar",
10860 crate_name: "my_rust_aconfig_library_bar",
10861 apex_available: [
10862 "myapex",
10863 ],
10864 }
10865
10866 rust_aconfig_library {
10867 name: "libmy_rust_aconfig_library_baz",
10868 aconfig_declarations: "my_aconfig_declarations_baz",
10869 crate_name: "my_rust_aconfig_library_baz",
10870 apex_available: [
10871 "myapex",
10872 ],
10873 }
10874 `)
10875
10876 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10877 s := mod.Rule("apexRule").Args["copy_commands"]
10878 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Ted Bauer6ef40db2024-03-29 14:04:10 +000010879 if len(copyCmds) != 32 {
Ted Bauer02d475c2024-03-27 20:56:26 +000010880 t.Fatalf("Expected 28 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liucec0e412023-11-30 16:45:50 -080010881 }
10882
Ted Bauer6ef40db2024-03-29 14:04:10 +000010883 ensureMatches(t, copyCmds[28], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10884 ensureMatches(t, copyCmds[29], "^cp -f .*/package.map .*/image.apex/etc$")
10885 ensureMatches(t, copyCmds[30], "^cp -f .*/flag.map .*/image.apex/etc$")
10886 ensureMatches(t, copyCmds[31], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liucec0e412023-11-30 16:45:50 -080010887
Yu Liubba555e2024-02-17 00:36:42 +000010888 inputs := []string{
10889 "my_aconfig_declarations_foo/intermediate.pb",
Yu Liuab31c822024-02-28 22:21:31 +000010890 "my_aconfig_declarations_bar/intermediate.pb",
10891 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liubba555e2024-02-17 00:36:42 +000010892 "my_rust_binary/android_arm64_armv8-a_apex10000/myapex/aconfig_merged.pb",
10893 }
10894 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10895 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10896 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10897 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
10898}
10899
10900func VerifyAconfigRule(t *testing.T, mod *android.TestingModule, desc string, inputs []string, output string, container string, file_type string) {
10901 aconfigRule := mod.Description(desc)
10902 s := " " + aconfigRule.Args["cache_files"]
Yu Liucec0e412023-11-30 16:45:50 -080010903 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
Yu Liubba555e2024-02-17 00:36:42 +000010904 if len(aconfigArgs) != len(inputs) {
10905 t.Fatalf("Expected %d commands, got %d in:\n%s", len(inputs), len(aconfigArgs), s)
Yu Liucec0e412023-11-30 16:45:50 -080010906 }
Yu Liucec0e412023-11-30 16:45:50 -080010907
Yu Liubba555e2024-02-17 00:36:42 +000010908 ensureEquals(t, container, aconfigRule.Args["container"])
10909 ensureEquals(t, file_type, aconfigRule.Args["file_type"])
10910
10911 buildParams := aconfigRule.BuildParams
10912 for _, input := range inputs {
10913 android.EnsureListContainsSuffix(t, aconfigArgs, input)
10914 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), input)
Yu Liucec0e412023-11-30 16:45:50 -080010915 }
Yu Liubba555e2024-02-17 00:36:42 +000010916
10917 ensureContains(t, buildParams.Output.String(), output)
Yu Liucec0e412023-11-30 16:45:50 -080010918}
10919
Yu Liueae7b362023-11-16 17:05:47 -080010920func TestAconfigFilesOnlyMatchCurrentApex(t *testing.T) {
10921 ctx := testApex(t, apex_default_bp+`
10922 apex {
10923 name: "myapex",
10924 manifest: ":myapex.manifest",
10925 androidManifest: ":myapex.androidmanifest",
10926 key: "myapex.key",
10927 java_libs: [
10928 "my_java_library_foo",
10929 "other_java_library_bar",
10930 ],
10931 updatable: false,
10932 }
10933
10934 java_library {
10935 name: "my_java_library_foo",
10936 srcs: ["foo/bar/MyClass.java"],
10937 sdk_version: "none",
10938 system_modules: "none",
10939 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010940 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010941 "myapex",
10942 ],
10943 }
10944
10945 java_library {
10946 name: "other_java_library_bar",
10947 srcs: ["foo/bar/MyClass.java"],
10948 sdk_version: "none",
10949 system_modules: "none",
10950 static_libs: ["other_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010951 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010952 "myapex",
10953 ],
10954 }
10955
10956 aconfig_declarations {
10957 name: "my_aconfig_declarations_foo",
10958 package: "com.example.package",
10959 container: "myapex",
10960 srcs: ["foo.aconfig"],
10961 }
10962
10963 java_aconfig_library {
10964 name: "my_java_aconfig_library_foo",
10965 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010966 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010967 "myapex",
10968 ],
10969 }
10970
10971 aconfig_declarations {
10972 name: "other_aconfig_declarations_bar",
10973 package: "com.example.package",
10974 container: "otherapex",
10975 srcs: ["bar.aconfig"],
10976 }
10977
10978 java_aconfig_library {
10979 name: "other_java_aconfig_library_bar",
10980 aconfig_declarations: "other_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010981 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010982 "myapex",
10983 ],
10984 }
10985 `)
10986
10987 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10988 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
10989 s := " " + combineAconfigRule.Args["cache_files"]
10990 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
10991 if len(aconfigArgs) != 1 {
10992 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
10993 }
10994 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
10995
10996 buildParams := combineAconfigRule.BuildParams
10997 if len(buildParams.Inputs) != 1 {
10998 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
10999 }
11000 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11001 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11002}
11003
11004func TestAconfigFilesRemoveDuplicates(t *testing.T) {
11005 ctx := testApex(t, apex_default_bp+`
11006 apex {
11007 name: "myapex",
11008 manifest: ":myapex.manifest",
11009 androidManifest: ":myapex.androidmanifest",
11010 key: "myapex.key",
11011 java_libs: [
11012 "my_java_library_foo",
11013 "my_java_library_bar",
11014 ],
11015 updatable: false,
11016 }
11017
11018 java_library {
11019 name: "my_java_library_foo",
11020 srcs: ["foo/bar/MyClass.java"],
11021 sdk_version: "none",
11022 system_modules: "none",
11023 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080011024 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011025 "myapex",
11026 ],
11027 }
11028
11029 java_library {
11030 name: "my_java_library_bar",
11031 srcs: ["foo/bar/MyClass.java"],
11032 sdk_version: "none",
11033 system_modules: "none",
11034 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080011035 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011036 "myapex",
11037 ],
11038 }
11039
11040 aconfig_declarations {
11041 name: "my_aconfig_declarations_foo",
11042 package: "com.example.package",
11043 container: "myapex",
11044 srcs: ["foo.aconfig"],
11045 }
11046
11047 java_aconfig_library {
11048 name: "my_java_aconfig_library_foo",
11049 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011050 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011051 "myapex",
11052 ],
11053 }
11054
11055 java_aconfig_library {
11056 name: "my_java_aconfig_library_bar",
11057 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011058 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011059 "myapex",
11060 ],
11061 }
11062 `)
11063
11064 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11065 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11066 s := " " + combineAconfigRule.Args["cache_files"]
11067 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11068 if len(aconfigArgs) != 1 {
11069 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
11070 }
11071 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11072
11073 buildParams := combineAconfigRule.BuildParams
11074 if len(buildParams.Inputs) != 1 {
11075 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
11076 }
11077 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11078 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11079}
Spandan Das5be63332023-12-13 00:06:32 +000011080
11081// Test that the boot jars come from the _selected_ apex prebuilt
11082// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11083func TestBootDexJarsMultipleApexPrebuilts(t *testing.T) {
11084 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
11085 t.Helper()
11086 s := ctx.ModuleForTests("dex_bootjars", "android_common")
11087 foundLibfooJar := false
11088 base := stem + ".jar"
11089 for _, output := range s.AllOutputs() {
11090 if filepath.Base(output) == base {
11091 foundLibfooJar = true
11092 buildRule := s.Output(output)
11093 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
11094 }
11095 }
11096 if !foundLibfooJar {
11097 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
11098 }
11099 }
11100
Spandan Das64c9e0c2023-12-20 20:13:34 +000011101 // Check that the boot jars of the selected apex are run through boot_jars_package_check
11102 // This validates that the jars on the bootclasspath do not contain packages outside an allowlist
11103 checkBootJarsPackageCheck := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11104 platformBcp := ctx.ModuleForTests("platform-bootclasspath", "android_common")
11105 bootJarsCheckRule := platformBcp.Rule("boot_jars_package_check")
11106 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)
11107 }
11108
11109 // Check that the boot jars used to generate the monolithic hiddenapi flags come from the selected apex
11110 checkBootJarsForMonolithicHiddenapi := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11111 monolithicHiddenapiFlagsCmd := ctx.ModuleForTests("platform-bootclasspath", "android_common").Output("out/soong/hiddenapi/hiddenapi-stub-flags.txt").RuleParams.Command
11112 android.AssertStringMatches(t, "Could not find the correct boot dex jar in monolithic hiddenapi flags generation command", monolithicHiddenapiFlagsCmd, "--boot-dex="+expectedBootJar)
11113 }
11114
Spandan Das5be63332023-12-13 00:06:32 +000011115 bp := `
11116 // Source APEX.
11117
11118 java_library {
11119 name: "framework-foo",
11120 srcs: ["foo.java"],
11121 installable: true,
11122 apex_available: [
11123 "com.android.foo",
11124 ],
11125 }
11126
11127 bootclasspath_fragment {
11128 name: "foo-bootclasspath-fragment",
11129 contents: ["framework-foo"],
11130 apex_available: [
11131 "com.android.foo",
11132 ],
11133 hidden_api: {
11134 split_packages: ["*"],
11135 },
11136 }
11137
11138 apex_key {
11139 name: "com.android.foo.key",
11140 public_key: "com.android.foo.avbpubkey",
11141 private_key: "com.android.foo.pem",
11142 }
11143
11144 apex {
11145 name: "com.android.foo",
11146 key: "com.android.foo.key",
11147 bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11148 updatable: false,
11149 }
11150
11151 // Prebuilt APEX.
11152
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011153 java_sdk_library_import {
Spandan Das5be63332023-12-13 00:06:32 +000011154 name: "framework-foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011155 public: {
11156 jars: ["foo.jar"],
11157 },
Spandan Das5be63332023-12-13 00:06:32 +000011158 apex_available: ["com.android.foo"],
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011159 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +000011160 }
11161
11162 prebuilt_bootclasspath_fragment {
11163 name: "foo-bootclasspath-fragment",
11164 contents: ["framework-foo"],
11165 hidden_api: {
11166 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
11167 metadata: "my-bootclasspath-fragment/metadata.csv",
11168 index: "my-bootclasspath-fragment/index.csv",
11169 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
11170 all_flags: "my-bootclasspath-fragment/all-flags.csv",
11171 },
11172 apex_available: [
11173 "com.android.foo",
11174 ],
11175 }
11176
11177 prebuilt_apex {
11178 name: "com.android.foo",
11179 apex_name: "com.android.foo",
11180 src: "com.android.foo-arm.apex",
11181 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11182 }
11183
11184 // Another Prebuilt ART APEX
11185 prebuilt_apex {
11186 name: "com.android.foo.v2",
11187 apex_name: "com.android.foo", // Used to determine the API domain
11188 src: "com.android.foo-arm.apex",
11189 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11190 }
11191
11192 // APEX contribution modules
11193
11194 apex_contributions {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011195 name: "foo.source.contributions",
Spandan Das5be63332023-12-13 00:06:32 +000011196 api_domain: "com.android.foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011197 contents: ["com.android.foo"],
11198 }
11199
11200 apex_contributions {
11201 name: "foo.prebuilt.contributions",
11202 api_domain: "com.android.foo",
11203 contents: ["prebuilt_com.android.foo"],
11204 }
11205
11206 apex_contributions {
11207 name: "foo.prebuilt.v2.contributions",
11208 api_domain: "com.android.foo",
11209 contents: ["com.android.foo.v2"], // prebuilt_ prefix is missing because of prebuilt_rename mutator
Spandan Das5be63332023-12-13 00:06:32 +000011210 }
11211 `
11212
11213 testCases := []struct {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011214 desc string
11215 selectedApexContributions string
11216 expectedBootJar string
Spandan Das5be63332023-12-13 00:06:32 +000011217 }{
11218 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011219 desc: "Source apex com.android.foo is selected, bootjar should come from source java library",
11220 selectedApexContributions: "foo.source.contributions",
11221 expectedBootJar: "out/soong/.intermediates/foo-bootclasspath-fragment/android_common_apex10000/hiddenapi-modular/encoded/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011222 },
11223 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011224 desc: "Prebuilt apex prebuilt_com.android.foo is selected, profile should come from .prof deapexed from the prebuilt",
11225 selectedApexContributions: "foo.prebuilt.contributions",
11226 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011227 },
11228 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011229 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected, profile should come from .prof deapexed from the prebuilt",
11230 selectedApexContributions: "foo.prebuilt.v2.contributions",
11231 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.v2.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011232 },
11233 }
11234
11235 fragment := java.ApexVariantReference{
11236 Apex: proptools.StringPtr("com.android.foo"),
11237 Module: proptools.StringPtr("foo-bootclasspath-fragment"),
11238 }
11239
11240 for _, tc := range testCases {
11241 preparer := android.GroupFixturePreparers(
11242 java.FixtureConfigureApexBootJars("com.android.foo:framework-foo"),
11243 android.FixtureMergeMockFs(map[string][]byte{
11244 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11245 }),
11246 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
11247 variables.BuildFlags = map[string]string{
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011248 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": tc.selectedApexContributions,
Spandan Das5be63332023-12-13 00:06:32 +000011249 }
11250 }),
11251 )
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011252 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das5be63332023-12-13 00:06:32 +000011253 checkBootDexJarPath(t, ctx, "framework-foo", tc.expectedBootJar)
Spandan Das64c9e0c2023-12-20 20:13:34 +000011254 checkBootJarsPackageCheck(t, ctx, tc.expectedBootJar)
11255 checkBootJarsForMonolithicHiddenapi(t, ctx, tc.expectedBootJar)
Spandan Das5be63332023-12-13 00:06:32 +000011256 }
11257}
Spandan Das3576e762024-01-03 18:57:03 +000011258
11259// Test that product packaging installs the selected mainline module (either source or a specific prebuilt)
11260// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11261func TestInstallationRulesForMultipleApexPrebuilts(t *testing.T) {
11262 // check that the LOCAL_MODULE in the generated mk file matches the name used in PRODUCT_PACKAGES
11263 // Since the name used in PRODUCT_PACKAGES does not contain prebuilt_ prefix, LOCAL_MODULE should not contain any prefix either
11264 checkLocalModuleName := func(t *testing.T, ctx *android.TestContext, soongApexModuleName string, expectedLocalModuleName string) {
11265 // Variations are created based on apex_name
11266 entries := android.AndroidMkEntriesForTest(t, ctx, ctx.ModuleForTests(soongApexModuleName, "android_common_com.android.foo").Module())
11267 android.AssertStringEquals(t, "LOCAL_MODULE of the prebuilt apex must match the name listed in PRODUCT_PACKAGES", expectedLocalModuleName, entries[0].EntryMap["LOCAL_MODULE"][0])
11268 }
11269 // for a mainline module family, check that only the flagged soong module is visible to make
11270 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleName string, hiddenModuleNames []string) {
11271 variation := func(moduleName string) string {
11272 ret := "android_common_com.android.foo"
11273 if moduleName == "com.google.android.foo" {
11274 ret = "android_common_com.google.android.foo_com.android.foo"
11275 }
11276 return ret
11277 }
11278
11279 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11280 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11281
11282 for _, hiddenModuleName := range hiddenModuleNames {
11283 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11284 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11285
11286 }
11287 }
11288
11289 bp := `
11290 apex_key {
11291 name: "com.android.foo.key",
11292 public_key: "com.android.foo.avbpubkey",
11293 private_key: "com.android.foo.pem",
11294 }
11295
11296 // AOSP source apex
11297 apex {
11298 name: "com.android.foo",
11299 key: "com.android.foo.key",
11300 updatable: false,
11301 }
11302
11303 // Google source apex
11304 override_apex {
11305 name: "com.google.android.foo",
11306 base: "com.android.foo",
11307 key: "com.android.foo.key",
11308 }
11309
11310 // Prebuilt Google APEX.
11311
11312 prebuilt_apex {
11313 name: "com.google.android.foo",
11314 apex_name: "com.android.foo",
11315 src: "com.android.foo-arm.apex",
11316 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11317 }
11318
11319 // Another Prebuilt Google APEX
11320 prebuilt_apex {
11321 name: "com.google.android.foo.v2",
11322 apex_name: "com.android.foo",
11323 source_apex_name: "com.google.android.foo", // source_apex_name becomes LOCAL_MODULE in the generated mk file
11324 src: "com.android.foo-arm.apex",
11325 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11326 }
11327
11328 // APEX contribution modules
11329
11330 apex_contributions {
11331 name: "foo.source.contributions",
11332 api_domain: "com.android.foo",
11333 contents: ["com.google.android.foo"],
11334 }
11335
11336 apex_contributions {
11337 name: "foo.prebuilt.contributions",
11338 api_domain: "com.android.foo",
11339 contents: ["prebuilt_com.google.android.foo"],
11340 }
11341
11342 apex_contributions {
11343 name: "foo.prebuilt.v2.contributions",
11344 api_domain: "com.android.foo",
11345 contents: ["prebuilt_com.google.android.foo.v2"],
11346 }
11347
11348 // This is an incompatible module because it selects multiple versions of the same mainline module
11349 apex_contributions {
11350 name: "foo.prebuilt.duplicate.contributions",
11351 api_domain: "com.android.foo",
11352 contents: [
11353 "prebuilt_com.google.android.foo",
11354 "prebuilt_com.google.android.foo.v2",
11355 ],
11356 }
11357 `
11358
11359 testCases := []struct {
11360 desc string
11361 selectedApexContributions string
11362 expectedVisibleModuleName string
11363 expectedHiddenModuleNames []string
11364 expectedError string
11365 }{
11366 {
11367 desc: "Source apex is selected, prebuilts should be hidden from make",
11368 selectedApexContributions: "foo.source.contributions",
11369 expectedVisibleModuleName: "com.google.android.foo",
11370 expectedHiddenModuleNames: []string{"prebuilt_com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11371 },
11372 {
11373 desc: "Prebuilt apex prebuilt_com.android.foo is selected, source and the other prebuilt should be hidden from make",
11374 selectedApexContributions: "foo.prebuilt.contributions",
11375 expectedVisibleModuleName: "prebuilt_com.google.android.foo",
11376 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11377 },
11378 {
11379 desc: "Prebuilt apex prebuilt_com.android.fooi.v2 is selected, source and the other prebuilt should be hidden from make",
11380 selectedApexContributions: "foo.prebuilt.v2.contributions",
11381 expectedVisibleModuleName: "prebuilt_com.google.android.foo.v2",
11382 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo"},
11383 },
11384 {
11385 desc: "Multiple versions of a prebuilt apex is selected in the same release config",
11386 selectedApexContributions: "foo.prebuilt.duplicate.contributions",
11387 expectedError: "Found duplicate variations of the same module in apex_contributions: prebuilt_com.google.android.foo and prebuilt_com.google.android.foo.v2",
11388 },
11389 }
11390
11391 for _, tc := range testCases {
11392 preparer := android.GroupFixturePreparers(
11393 android.FixtureMergeMockFs(map[string][]byte{
11394 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11395 }),
11396 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
11397 variables.BuildFlags = map[string]string{
11398 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": tc.selectedApexContributions,
11399 }
11400 }),
11401 )
11402 if tc.expectedError != "" {
11403 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(tc.expectedError))
11404 testApex(t, bp, preparer)
11405 return
11406 }
11407 ctx := testApex(t, bp, preparer)
11408
11409 // Check that the LOCAL_MODULE of the two prebuilts is com.android.foo
11410 // This ensures that product packaging can pick them for installation if it has been flagged by apex_contributions
11411 checkLocalModuleName(t, ctx, "prebuilt_com.google.android.foo", "com.google.android.foo")
11412 checkLocalModuleName(t, ctx, "prebuilt_com.google.android.foo.v2", "com.google.android.foo")
11413
11414 // Check that
11415 // 1. The contents of the selected apex_contributions are visible to make
11416 // 2. The rest of the apexes in the mainline module family (source or other prebuilt) is hidden from make
11417 checkHideFromMake(t, ctx, tc.expectedVisibleModuleName, tc.expectedHiddenModuleNames)
11418 }
11419}
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011420
11421func TestAconfifDeclarationsValidation(t *testing.T) {
11422 aconfigDeclarationLibraryString := func(moduleNames []string) (ret string) {
11423 for _, moduleName := range moduleNames {
11424 ret += fmt.Sprintf(`
11425 aconfig_declarations {
11426 name: "%[1]s",
11427 package: "com.example.package",
11428 srcs: [
11429 "%[1]s.aconfig",
11430 ],
11431 }
11432 java_aconfig_library {
11433 name: "%[1]s-lib",
11434 aconfig_declarations: "%[1]s",
11435 }
11436 `, moduleName)
11437 }
11438 return ret
11439 }
11440
11441 result := android.GroupFixturePreparers(
11442 prepareForApexTest,
11443 java.PrepareForTestWithJavaSdkLibraryFiles,
11444 java.FixtureWithLastReleaseApis("foo"),
11445 android.FixtureModifyConfig(func(config android.Config) {
11446 config.SetApiLibraries([]string{"foo"})
11447 }),
11448 ).RunTestWithBp(t, `
11449 java_library {
11450 name: "baz-java-lib",
11451 static_libs: [
11452 "baz-lib",
11453 ],
11454 }
11455 filegroup {
11456 name: "qux-filegroup",
11457 srcs: [
11458 ":qux-lib{.generated_srcjars}",
11459 ],
11460 }
11461 filegroup {
11462 name: "qux-another-filegroup",
11463 srcs: [
11464 ":qux-filegroup",
11465 ],
11466 }
11467 java_library {
11468 name: "quux-java-lib",
11469 srcs: [
11470 "a.java",
11471 ],
11472 libs: [
11473 "quux-lib",
11474 ],
11475 }
11476 java_sdk_library {
11477 name: "foo",
11478 srcs: [
11479 ":qux-another-filegroup",
11480 ],
11481 api_packages: ["foo"],
11482 system: {
11483 enabled: true,
11484 },
11485 module_lib: {
11486 enabled: true,
11487 },
11488 test: {
11489 enabled: true,
11490 },
11491 static_libs: [
11492 "bar-lib",
11493 ],
11494 libs: [
11495 "baz-java-lib",
11496 "quux-java-lib",
11497 ],
11498 aconfig_declarations: [
11499 "bar",
11500 ],
11501 }
11502 `+aconfigDeclarationLibraryString([]string{"bar", "baz", "qux", "quux"}))
11503
11504 m := result.ModuleForTests("foo.stubs.source", "android_common")
11505 outDir := "out/soong/.intermediates"
11506
11507 // Arguments passed to aconfig to retrieve the state of the flags defined in the
11508 // textproto files
11509 aconfigFlagArgs := m.Output("released-flagged-apis-exportable.txt").Args["flags_path"]
11510
11511 // "bar-lib" is a static_lib of "foo" and is passed to metalava as classpath. Thus the
11512 // cache file provided by the associated aconfig_declarations module "bar" should be passed
11513 // to aconfig.
11514 android.AssertStringDoesContain(t, "cache file of a java_aconfig_library static_lib "+
11515 "passed as an input",
11516 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "bar"))
11517
11518 // "baz-java-lib", which statically depends on "baz-lib", is a lib of "foo" and is passed
11519 // to metalava as classpath. Thus the cache file provided by the associated
11520 // aconfig_declarations module "baz" should be passed to aconfig.
11521 android.AssertStringDoesContain(t, "cache file of a lib that statically depends on "+
11522 "java_aconfig_library passed as an input",
11523 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "baz"))
11524
11525 // "qux-lib" is passed to metalava as src via the filegroup, thus the cache file provided by
11526 // the associated aconfig_declarations module "qux" should be passed to aconfig.
11527 android.AssertStringDoesContain(t, "cache file of srcs java_aconfig_library passed as an "+
11528 "input",
11529 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "qux"))
11530
11531 // "quux-java-lib" is a lib of "foo" and is passed to metalava as classpath, but does not
11532 // statically depend on "quux-lib". Therefore, the cache file provided by the associated
11533 // aconfig_declarations module "quux" should not be passed to aconfig.
11534 android.AssertStringDoesNotContain(t, "cache file of a lib that does not statically "+
11535 "depend on java_aconfig_library not passed as an input",
11536 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "quux"))
11537}