blob: 1be10483ff6e298bb520d2668a2c946f615be572 [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 {
Kiyoung Kim973cb6f2024-04-29 14:14:53 +09003674 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Justin Yund5784122023-10-25 13:25:32 +09003675 result += `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003676 prebuilt_etc {
3677 name: "` + txt + `.libraries.` + v + `.txt",
3678 src: "dummy.txt",
3679 }
3680 `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003681 }
3682 }
3683 return
3684}
3685
Jooyung Han344d5432019-08-23 11:17:39 +09003686func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003687 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003688 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003689 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003690 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003691 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003692 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003693 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003694 }
3695
3696 apex_key {
3697 name: "myapex.key",
3698 public_key: "testkey.avbpubkey",
3699 private_key: "testkey.pem",
3700 }
3701
Jooyung Han31c470b2019-10-18 16:26:59 +09003702 vndk_prebuilt_shared {
3703 name: "libvndk27",
3704 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003705 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003706 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003707 vndk: {
3708 enabled: true,
3709 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003710 target_arch: "arm64",
3711 arch: {
3712 arm: {
3713 srcs: ["libvndk27_arm.so"],
3714 },
3715 arm64: {
3716 srcs: ["libvndk27_arm64.so"],
3717 },
3718 },
Colin Cross2807f002021-03-02 10:15:29 -08003719 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003720 }
3721
3722 vndk_prebuilt_shared {
3723 name: "libvndk27",
3724 version: "27",
3725 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003726 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003727 vndk: {
3728 enabled: true,
3729 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003730 target_arch: "x86_64",
3731 arch: {
3732 x86: {
3733 srcs: ["libvndk27_x86.so"],
3734 },
3735 x86_64: {
3736 srcs: ["libvndk27_x86_64.so"],
3737 },
3738 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003739 }
3740 `+vndkLibrariesTxtFiles("27"),
3741 withFiles(map[string][]byte{
3742 "libvndk27_arm.so": nil,
3743 "libvndk27_arm64.so": nil,
3744 "libvndk27_x86.so": nil,
3745 "libvndk27_x86_64.so": nil,
3746 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003747
Jooyung Hana0503a52023-08-23 13:12:50 +09003748 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003749 "lib/libvndk27_arm.so",
3750 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003751 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003752 })
Jooyung Han344d5432019-08-23 11:17:39 +09003753}
3754
Jooyung Han90eee022019-10-01 20:02:42 +09003755func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003756 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003757 apex_vndk {
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003758 name: "com.android.vndk.v29",
Jooyung Han90eee022019-10-01 20:02:42 +09003759 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003760 file_contexts: ":myapex-file_contexts",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003761 vndk_version: "29",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003762 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003763 }
3764 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003765 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003766 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003767 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003768 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003769 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003770 }
3771 apex_key {
3772 name: "myapex.key",
3773 public_key: "testkey.avbpubkey",
3774 private_key: "testkey.pem",
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003775 }`+vndkLibrariesTxtFiles("28", "29"))
Jooyung Han90eee022019-10-01 20:02:42 +09003776
3777 assertApexName := func(expected, moduleName string) {
Jooyung Hana0503a52023-08-23 13:12:50 +09003778 module := ctx.ModuleForTests(moduleName, "android_common")
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003779 apexManifestRule := module.Rule("apexManifestRule")
3780 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003781 }
3782
Kiyoung Kim0d1c1e62024-03-26 16:33:58 +09003783 assertApexName("com.android.vndk.v29", "com.android.vndk.v29")
Colin Cross2807f002021-03-02 10:15:29 -08003784 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003785}
3786
Jooyung Han344d5432019-08-23 11:17:39 +09003787func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003788 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003789 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003790 name: "com.android.vndk.current",
3791 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003792 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003793 native_bridge_supported: true,
3794 }
3795
3796 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003797 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003798 public_key: "testkey.avbpubkey",
3799 private_key: "testkey.pem",
3800 }
3801
3802 cc_library {
3803 name: "libvndk",
3804 srcs: ["mylib.cpp"],
3805 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003806 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003807 native_bridge_supported: true,
3808 host_supported: true,
3809 vndk: {
3810 enabled: true,
3811 },
3812 system_shared_libs: [],
3813 stl: "none",
3814 }
3815 `)
3816}
3817
Jooyung Han31c470b2019-10-18 16:26:59 +09003818func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003819 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003820 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003821 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003822 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003823 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003824 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003825 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003826 }
3827
3828 apex_key {
3829 name: "myapex.key",
3830 public_key: "testkey.avbpubkey",
3831 private_key: "testkey.pem",
3832 }
3833
3834 vndk_prebuilt_shared {
3835 name: "libvndk27",
3836 version: "27",
3837 target_arch: "arm",
3838 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003839 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003840 vndk: {
3841 enabled: true,
3842 },
3843 arch: {
3844 arm: {
3845 srcs: ["libvndk27.so"],
3846 }
3847 },
3848 }
3849
3850 vndk_prebuilt_shared {
3851 name: "libvndk27",
3852 version: "27",
3853 target_arch: "arm",
3854 binder32bit: true,
3855 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003856 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003857 vndk: {
3858 enabled: true,
3859 },
3860 arch: {
3861 arm: {
3862 srcs: ["libvndk27binder32.so"],
3863 }
3864 },
Colin Cross2807f002021-03-02 10:15:29 -08003865 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003866 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003867 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003868 withFiles(map[string][]byte{
3869 "libvndk27.so": nil,
3870 "libvndk27binder32.so": nil,
3871 }),
3872 withBinder32bit,
3873 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07003874 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09003875 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3876 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003877 },
3878 }),
3879 )
3880
Jooyung Hana0503a52023-08-23 13:12:50 +09003881 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003882 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003883 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003884 })
3885}
3886
Jooyung Hane1633032019-08-01 17:41:43 +09003887func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003888 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09003889 apex {
3890 name: "myapex_nodep",
3891 key: "myapex.key",
3892 native_shared_libs: ["lib_nodep"],
3893 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003894 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003895 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003896 }
3897
3898 apex {
3899 name: "myapex_dep",
3900 key: "myapex.key",
3901 native_shared_libs: ["lib_dep"],
3902 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003903 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003904 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003905 }
3906
3907 apex {
3908 name: "myapex_provider",
3909 key: "myapex.key",
3910 native_shared_libs: ["libfoo"],
3911 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003912 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003913 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003914 }
3915
3916 apex {
3917 name: "myapex_selfcontained",
3918 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00003919 native_shared_libs: ["lib_dep_on_bar", "libbar"],
Jooyung Hane1633032019-08-01 17:41:43 +09003920 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003921 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003922 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09003923 }
3924
3925 apex_key {
3926 name: "myapex.key",
3927 public_key: "testkey.avbpubkey",
3928 private_key: "testkey.pem",
3929 }
3930
3931 cc_library {
3932 name: "lib_nodep",
3933 srcs: ["mylib.cpp"],
3934 system_shared_libs: [],
3935 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003936 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09003937 }
3938
3939 cc_library {
3940 name: "lib_dep",
3941 srcs: ["mylib.cpp"],
3942 shared_libs: ["libfoo"],
3943 system_shared_libs: [],
3944 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003945 apex_available: [
3946 "myapex_dep",
3947 "myapex_provider",
3948 "myapex_selfcontained",
3949 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003950 }
3951
3952 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00003953 name: "lib_dep_on_bar",
3954 srcs: ["mylib.cpp"],
3955 shared_libs: ["libbar"],
3956 system_shared_libs: [],
3957 stl: "none",
3958 apex_available: [
3959 "myapex_selfcontained",
3960 ],
3961 }
3962
3963
3964 cc_library {
Jooyung Hane1633032019-08-01 17:41:43 +09003965 name: "libfoo",
3966 srcs: ["mytest.cpp"],
3967 stubs: {
3968 versions: ["1"],
3969 },
3970 system_shared_libs: [],
3971 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003972 apex_available: [
3973 "myapex_provider",
Spandan Das20fce2d2023-04-12 17:21:39 +00003974 ],
3975 }
3976
3977 cc_library {
3978 name: "libbar",
3979 srcs: ["mytest.cpp"],
3980 stubs: {
3981 versions: ["1"],
3982 },
3983 system_shared_libs: [],
3984 stl: "none",
3985 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003986 "myapex_selfcontained",
3987 ],
Jooyung Hane1633032019-08-01 17:41:43 +09003988 }
Spandan Das20fce2d2023-04-12 17:21:39 +00003989
Jooyung Hane1633032019-08-01 17:41:43 +09003990 `)
3991
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003992 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09003993 var provideNativeLibs, requireNativeLibs []string
3994
Jooyung Hana0503a52023-08-23 13:12:50 +09003995 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09003996 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
3997 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09003998 ensureListEmpty(t, provideNativeLibs)
3999 ensureListEmpty(t, requireNativeLibs)
4000
Jooyung Hana0503a52023-08-23 13:12:50 +09004001 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004002 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4003 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004004 ensureListEmpty(t, provideNativeLibs)
4005 ensureListContains(t, requireNativeLibs, "libfoo.so")
4006
Jooyung Hana0503a52023-08-23 13:12:50 +09004007 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004008 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4009 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004010 ensureListContains(t, provideNativeLibs, "libfoo.so")
4011 ensureListEmpty(t, requireNativeLibs)
4012
Jooyung Hana0503a52023-08-23 13:12:50 +09004013 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004014 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4015 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Spandan Das20fce2d2023-04-12 17:21:39 +00004016 ensureListContains(t, provideNativeLibs, "libbar.so")
Jooyung Hane1633032019-08-01 17:41:43 +09004017 ensureListEmpty(t, requireNativeLibs)
4018}
4019
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004020func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4021 ctx := testApex(t, `
4022 apex {
4023 name: "myapex",
4024 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004025 native_shared_libs: ["mylib"],
4026 updatable: false,
4027 }
4028
4029 apex_key {
4030 name: "myapex.key",
4031 public_key: "testkey.avbpubkey",
4032 private_key: "testkey.pem",
4033 }
4034
4035 cc_library {
4036 name: "mylib",
4037 srcs: ["mylib.cpp"],
4038 system_shared_libs: [],
4039 stl: "none",
4040 apex_available: [
4041 "//apex_available:platform",
4042 "myapex",
4043 ],
4044 }
4045 `, android.FixtureMergeEnv(map[string]string{
4046 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4047 }))
4048
Jooyung Hana0503a52023-08-23 13:12:50 +09004049 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004050 apexManifestRule := module.Rule("apexManifestRule")
4051 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4052}
4053
Vinh Tran8f5310f2022-10-07 18:16:47 -04004054func TestCompileMultilibProp(t *testing.T) {
4055 testCases := []struct {
4056 compileMultiLibProp string
4057 containedLibs []string
4058 notContainedLibs []string
4059 }{
4060 {
4061 containedLibs: []string{
4062 "image.apex/lib64/mylib.so",
4063 "image.apex/lib/mylib.so",
4064 },
4065 compileMultiLibProp: `compile_multilib: "both",`,
4066 },
4067 {
4068 containedLibs: []string{"image.apex/lib64/mylib.so"},
4069 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4070 compileMultiLibProp: `compile_multilib: "first",`,
4071 },
4072 {
4073 containedLibs: []string{"image.apex/lib64/mylib.so"},
4074 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4075 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4076 },
4077 {
4078 containedLibs: []string{"image.apex/lib64/mylib.so"},
4079 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4080 compileMultiLibProp: `compile_multilib: "64",`,
4081 },
4082 {
4083 containedLibs: []string{"image.apex/lib/mylib.so"},
4084 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4085 compileMultiLibProp: `compile_multilib: "32",`,
4086 },
4087 }
4088 for _, testCase := range testCases {
4089 ctx := testApex(t, fmt.Sprintf(`
4090 apex {
4091 name: "myapex",
4092 key: "myapex.key",
4093 %s
4094 native_shared_libs: ["mylib"],
4095 updatable: false,
4096 }
4097 apex_key {
4098 name: "myapex.key",
4099 public_key: "testkey.avbpubkey",
4100 private_key: "testkey.pem",
4101 }
4102 cc_library {
4103 name: "mylib",
4104 srcs: ["mylib.cpp"],
4105 apex_available: [
4106 "//apex_available:platform",
4107 "myapex",
4108 ],
4109 }
4110 `, testCase.compileMultiLibProp),
4111 )
Jooyung Hana0503a52023-08-23 13:12:50 +09004112 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Vinh Tran8f5310f2022-10-07 18:16:47 -04004113 apexRule := module.Rule("apexRule")
4114 copyCmds := apexRule.Args["copy_commands"]
4115 for _, containedLib := range testCase.containedLibs {
4116 ensureContains(t, copyCmds, containedLib)
4117 }
4118 for _, notContainedLib := range testCase.notContainedLibs {
4119 ensureNotContains(t, copyCmds, notContainedLib)
4120 }
4121 }
4122}
4123
Alex Light0851b882019-02-07 13:20:53 -08004124func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004125 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004126 apex {
4127 name: "myapex",
4128 key: "myapex.key",
4129 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004130 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004131 }
4132
4133 apex_key {
4134 name: "myapex.key",
4135 public_key: "testkey.avbpubkey",
4136 private_key: "testkey.pem",
4137 }
4138
4139 cc_library {
4140 name: "mylib_common",
4141 srcs: ["mylib.cpp"],
4142 system_shared_libs: [],
4143 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004144 apex_available: [
4145 "//apex_available:platform",
4146 "myapex",
4147 ],
Alex Light0851b882019-02-07 13:20:53 -08004148 }
4149 `)
4150
Jooyung Hana0503a52023-08-23 13:12:50 +09004151 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004152 apexRule := module.Rule("apexRule")
4153 copyCmds := apexRule.Args["copy_commands"]
4154
4155 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4156 t.Log("Apex was a test apex!")
4157 t.Fail()
4158 }
4159 // Ensure that main rule creates an output
4160 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4161
4162 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004163 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004164
4165 // Ensure that both direct and indirect deps are copied into apex
4166 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4167
Colin Cross7113d202019-11-20 16:39:12 -08004168 // Ensure that the platform variant ends with _shared
4169 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004170
Colin Cross56a83212020-09-15 18:30:11 -07004171 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004172 t.Log("Found mylib_common not in any apex!")
4173 t.Fail()
4174 }
4175}
4176
4177func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004178 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004179 apex_test {
4180 name: "myapex",
4181 key: "myapex.key",
4182 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004183 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004184 }
4185
4186 apex_key {
4187 name: "myapex.key",
4188 public_key: "testkey.avbpubkey",
4189 private_key: "testkey.pem",
4190 }
4191
4192 cc_library {
4193 name: "mylib_common_test",
4194 srcs: ["mylib.cpp"],
4195 system_shared_libs: [],
4196 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004197 // TODO: remove //apex_available:platform
4198 apex_available: [
4199 "//apex_available:platform",
4200 "myapex",
4201 ],
Alex Light0851b882019-02-07 13:20:53 -08004202 }
4203 `)
4204
Jooyung Hana0503a52023-08-23 13:12:50 +09004205 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004206 apexRule := module.Rule("apexRule")
4207 copyCmds := apexRule.Args["copy_commands"]
4208
4209 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4210 t.Log("Apex was not a test apex!")
4211 t.Fail()
4212 }
4213 // Ensure that main rule creates an output
4214 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4215
4216 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004217 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004218
4219 // Ensure that both direct and indirect deps are copied into apex
4220 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4221
Colin Cross7113d202019-11-20 16:39:12 -08004222 // Ensure that the platform variant ends with _shared
4223 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004224}
4225
Jooyung Han85707de2023-12-01 14:21:13 +09004226func TestLibzVendorIsntStable(t *testing.T) {
4227 ctx := testApex(t, `
4228 apex {
4229 name: "myapex",
4230 key: "myapex.key",
4231 updatable: false,
4232 binaries: ["mybin"],
4233 }
4234 apex {
4235 name: "myvendorapex",
4236 key: "myapex.key",
4237 file_contexts: "myvendorapex_file_contexts",
4238 vendor: true,
4239 updatable: false,
4240 binaries: ["mybin"],
4241 }
4242 apex_key {
4243 name: "myapex.key",
4244 public_key: "testkey.avbpubkey",
4245 private_key: "testkey.pem",
4246 }
4247 cc_binary {
4248 name: "mybin",
4249 vendor_available: true,
4250 system_shared_libs: [],
4251 stl: "none",
4252 shared_libs: ["libz"],
4253 apex_available: ["//apex_available:anyapex"],
4254 }
4255 cc_library {
4256 name: "libz",
4257 vendor_available: true,
4258 system_shared_libs: [],
4259 stl: "none",
4260 stubs: {
4261 versions: ["28", "30"],
4262 },
4263 target: {
4264 vendor: {
4265 no_stubs: true,
4266 },
4267 },
4268 }
4269 `, withFiles(map[string][]byte{
4270 "myvendorapex_file_contexts": nil,
4271 }))
4272
4273 // libz provides stubs for core variant.
4274 {
4275 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
4276 "bin/mybin",
4277 })
4278 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
4279 android.AssertStringEquals(t, "should require libz", apexManifestRule.Args["requireNativeLibs"], "libz.so")
4280 }
4281 // libz doesn't provide stubs for vendor variant.
4282 {
4283 ensureExactContents(t, ctx, "myvendorapex", "android_common_myvendorapex", []string{
4284 "bin/mybin",
4285 "lib64/libz.so",
4286 })
4287 apexManifestRule := ctx.ModuleForTests("myvendorapex", "android_common_myvendorapex").Rule("apexManifestRule")
4288 android.AssertStringEquals(t, "should not require libz", apexManifestRule.Args["requireNativeLibs"], "")
4289 }
4290}
4291
Alex Light9670d332019-01-29 18:07:33 -08004292func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004293 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004294 apex {
4295 name: "myapex",
4296 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004297 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004298 multilib: {
4299 first: {
4300 native_shared_libs: ["mylib_common"],
4301 }
4302 },
4303 target: {
4304 android: {
4305 multilib: {
4306 first: {
4307 native_shared_libs: ["mylib"],
4308 }
4309 }
4310 },
4311 host: {
4312 multilib: {
4313 first: {
4314 native_shared_libs: ["mylib2"],
4315 }
4316 }
4317 }
4318 }
4319 }
4320
4321 apex_key {
4322 name: "myapex.key",
4323 public_key: "testkey.avbpubkey",
4324 private_key: "testkey.pem",
4325 }
4326
4327 cc_library {
4328 name: "mylib",
4329 srcs: ["mylib.cpp"],
4330 system_shared_libs: [],
4331 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004332 // TODO: remove //apex_available:platform
4333 apex_available: [
4334 "//apex_available:platform",
4335 "myapex",
4336 ],
Alex Light9670d332019-01-29 18:07:33 -08004337 }
4338
4339 cc_library {
4340 name: "mylib_common",
4341 srcs: ["mylib.cpp"],
4342 system_shared_libs: [],
4343 stl: "none",
4344 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004345 // TODO: remove //apex_available:platform
4346 apex_available: [
4347 "//apex_available:platform",
4348 "myapex",
4349 ],
Alex Light9670d332019-01-29 18:07:33 -08004350 }
4351
4352 cc_library {
4353 name: "mylib2",
4354 srcs: ["mylib.cpp"],
4355 system_shared_libs: [],
4356 stl: "none",
4357 compile_multilib: "first",
4358 }
4359 `)
4360
Jooyung Hana0503a52023-08-23 13:12:50 +09004361 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004362 copyCmds := apexRule.Args["copy_commands"]
4363
4364 // Ensure that main rule creates an output
4365 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4366
4367 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004368 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4369 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4370 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004371
4372 // Ensure that both direct and indirect deps are copied into apex
4373 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4374 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4375 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4376
Colin Cross7113d202019-11-20 16:39:12 -08004377 // Ensure that the platform variant ends with _shared
4378 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4379 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4380 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004381}
Jiyong Park04480cf2019-02-06 00:16:29 +09004382
Jiyong Park59140302020-12-14 18:44:04 +09004383func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004384 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004385 apex {
4386 name: "myapex",
4387 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004388 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004389 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004390 arch: {
4391 arm64: {
4392 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004393 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004394 },
4395 x86_64: {
4396 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004397 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004398 },
4399 }
4400 }
4401
4402 apex_key {
4403 name: "myapex.key",
4404 public_key: "testkey.avbpubkey",
4405 private_key: "testkey.pem",
4406 }
4407
4408 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004409 name: "mylib.generic",
4410 srcs: ["mylib.cpp"],
4411 system_shared_libs: [],
4412 stl: "none",
4413 // TODO: remove //apex_available:platform
4414 apex_available: [
4415 "//apex_available:platform",
4416 "myapex",
4417 ],
4418 }
4419
4420 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004421 name: "mylib.arm64",
4422 srcs: ["mylib.cpp"],
4423 system_shared_libs: [],
4424 stl: "none",
4425 // TODO: remove //apex_available:platform
4426 apex_available: [
4427 "//apex_available:platform",
4428 "myapex",
4429 ],
4430 }
4431
4432 cc_library {
4433 name: "mylib.x64",
4434 srcs: ["mylib.cpp"],
4435 system_shared_libs: [],
4436 stl: "none",
4437 // TODO: remove //apex_available:platform
4438 apex_available: [
4439 "//apex_available:platform",
4440 "myapex",
4441 ],
4442 }
4443 `)
4444
Jooyung Hana0503a52023-08-23 13:12:50 +09004445 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park59140302020-12-14 18:44:04 +09004446 copyCmds := apexRule.Args["copy_commands"]
4447
4448 // Ensure that apex variant is created for the direct dep
4449 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004450 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004451 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4452
4453 // Ensure that both direct and indirect deps are copied into apex
4454 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4455 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4456}
4457
Jiyong Park04480cf2019-02-06 00:16:29 +09004458func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004459 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004460 apex {
4461 name: "myapex",
4462 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004463 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004464 updatable: false,
Riya Thakur654461c2024-02-27 07:21:05 +00004465 compile_multilib: "both",
Jiyong Park04480cf2019-02-06 00:16:29 +09004466 }
4467
4468 apex_key {
4469 name: "myapex.key",
4470 public_key: "testkey.avbpubkey",
4471 private_key: "testkey.pem",
4472 }
4473
4474 sh_binary {
4475 name: "myscript",
4476 src: "mylib.cpp",
4477 filename: "myscript.sh",
4478 sub_dir: "script",
4479 }
4480 `)
4481
Jooyung Hana0503a52023-08-23 13:12:50 +09004482 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004483 copyCmds := apexRule.Args["copy_commands"]
4484
4485 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4486}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004487
Jooyung Han91df2082019-11-20 01:49:42 +09004488func TestApexInVariousPartition(t *testing.T) {
4489 testcases := []struct {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004490 propName, partition string
Jooyung Han91df2082019-11-20 01:49:42 +09004491 }{
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004492 {"", "system"},
4493 {"product_specific: true", "product"},
4494 {"soc_specific: true", "vendor"},
4495 {"proprietary: true", "vendor"},
4496 {"vendor: true", "vendor"},
4497 {"system_ext_specific: true", "system_ext"},
Jooyung Han91df2082019-11-20 01:49:42 +09004498 }
4499 for _, tc := range testcases {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004500 t.Run(tc.propName+":"+tc.partition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004501 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004502 apex {
4503 name: "myapex",
4504 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004505 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004506 `+tc.propName+`
4507 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004508
Jooyung Han91df2082019-11-20 01:49:42 +09004509 apex_key {
4510 name: "myapex.key",
4511 public_key: "testkey.avbpubkey",
4512 private_key: "testkey.pem",
4513 }
4514 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004515
Jooyung Hana0503a52023-08-23 13:12:50 +09004516 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jooyung Haneec1b3f2023-06-20 16:25:59 +09004517 expected := "out/soong/target/product/test_device/" + tc.partition + "/apex"
Paul Duffin37ba3442021-03-29 00:21:08 +01004518 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004519 if actual != expected {
4520 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4521 }
Jooyung Han91df2082019-11-20 01:49:42 +09004522 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004523 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004524}
Jiyong Park67882562019-03-21 01:11:21 +09004525
Jooyung Han580eb4f2020-06-24 19:33:06 +09004526func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004527 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004528 apex {
4529 name: "myapex",
4530 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004531 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004532 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004533
Jooyung Han580eb4f2020-06-24 19:33:06 +09004534 apex_key {
4535 name: "myapex.key",
4536 public_key: "testkey.avbpubkey",
4537 private_key: "testkey.pem",
4538 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004539 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09004540 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004541 rule := module.Output("file_contexts")
4542 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4543}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004544
Jooyung Han580eb4f2020-06-24 19:33:06 +09004545func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004546 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004547 apex {
4548 name: "myapex",
4549 key: "myapex.key",
4550 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004551 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004552 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004553
Jooyung Han580eb4f2020-06-24 19:33:06 +09004554 apex_key {
4555 name: "myapex.key",
4556 public_key: "testkey.avbpubkey",
4557 private_key: "testkey.pem",
4558 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004559 `, withFiles(map[string][]byte{
4560 "my_own_file_contexts": nil,
4561 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004562}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004563
Jooyung Han580eb4f2020-06-24 19:33:06 +09004564func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004565 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004566 apex {
4567 name: "myapex",
4568 key: "myapex.key",
4569 product_specific: true,
4570 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004571 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004572 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004573
Jooyung Han580eb4f2020-06-24 19:33:06 +09004574 apex_key {
4575 name: "myapex.key",
4576 public_key: "testkey.avbpubkey",
4577 private_key: "testkey.pem",
4578 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004579 `)
4580
Colin Cross1c460562021-02-16 17:55:47 -08004581 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004582 apex {
4583 name: "myapex",
4584 key: "myapex.key",
4585 product_specific: true,
4586 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004587 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004588 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004589
Jooyung Han580eb4f2020-06-24 19:33:06 +09004590 apex_key {
4591 name: "myapex.key",
4592 public_key: "testkey.avbpubkey",
4593 private_key: "testkey.pem",
4594 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004595 `, withFiles(map[string][]byte{
4596 "product_specific_file_contexts": nil,
4597 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004598 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004599 rule := module.Output("file_contexts")
4600 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4601}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004602
Jooyung Han580eb4f2020-06-24 19:33:06 +09004603func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004604 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004605 apex {
4606 name: "myapex",
4607 key: "myapex.key",
4608 product_specific: true,
4609 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004610 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004611 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004612
Jooyung Han580eb4f2020-06-24 19:33:06 +09004613 apex_key {
4614 name: "myapex.key",
4615 public_key: "testkey.avbpubkey",
4616 private_key: "testkey.pem",
4617 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004618
Jooyung Han580eb4f2020-06-24 19:33:06 +09004619 filegroup {
4620 name: "my-file-contexts",
4621 srcs: ["product_specific_file_contexts"],
4622 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004623 `, withFiles(map[string][]byte{
4624 "product_specific_file_contexts": nil,
4625 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09004626 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004627 rule := module.Output("file_contexts")
4628 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004629}
4630
Jiyong Park67882562019-03-21 01:11:21 +09004631func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004632 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004633 apex_key {
4634 name: "myapex.key",
4635 public_key: ":my.avbpubkey",
4636 private_key: ":my.pem",
4637 product_specific: true,
4638 }
4639
4640 filegroup {
4641 name: "my.avbpubkey",
4642 srcs: ["testkey2.avbpubkey"],
4643 }
4644
4645 filegroup {
4646 name: "my.pem",
4647 srcs: ["testkey2.pem"],
4648 }
4649 `)
4650
4651 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4652 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004653 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004654 if actual_pubkey != expected_pubkey {
4655 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4656 }
4657 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004658 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004659 if actual_privkey != expected_privkey {
4660 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4661 }
4662}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004663
4664func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004665 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004666 prebuilt_apex {
4667 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004668 arch: {
4669 arm64: {
4670 src: "myapex-arm64.apex",
4671 },
4672 arm: {
4673 src: "myapex-arm.apex",
4674 },
4675 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004676 }
4677 `)
4678
Wei Li340ee8e2022-03-18 17:33:24 -07004679 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4680 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004681
Jiyong Parkc95714e2019-03-29 14:23:10 +09004682 expectedInput := "myapex-arm64.apex"
4683 if prebuilt.inputApex.String() != expectedInput {
4684 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4685 }
Wei Li340ee8e2022-03-18 17:33:24 -07004686 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4687 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4688 rule := testingModule.Rule("genProvenanceMetaData")
4689 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4690 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4691 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4692 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08004693
4694 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
4695 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004696}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004697
Paul Duffinc0609c62021-03-01 17:27:16 +00004698func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004699 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004700 prebuilt_apex {
4701 name: "myapex",
4702 }
4703 `)
4704}
4705
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004706func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004707 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004708 prebuilt_apex {
4709 name: "myapex",
4710 src: "myapex-arm.apex",
4711 filename: "notmyapex.apex",
4712 }
4713 `)
4714
Wei Li340ee8e2022-03-18 17:33:24 -07004715 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4716 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004717
4718 expected := "notmyapex.apex"
4719 if p.installFilename != expected {
4720 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4721 }
Wei Li340ee8e2022-03-18 17:33:24 -07004722 rule := testingModule.Rule("genProvenanceMetaData")
4723 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4724 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4725 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4726 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004727}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004728
Samiul Islam7c02e262021-09-08 17:48:28 +01004729func TestApexSetFilenameOverride(t *testing.T) {
4730 testApex(t, `
4731 apex_set {
4732 name: "com.company.android.myapex",
4733 apex_name: "com.android.myapex",
4734 set: "company-myapex.apks",
4735 filename: "com.company.android.myapex.apex"
4736 }
4737 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4738
4739 testApex(t, `
4740 apex_set {
4741 name: "com.company.android.myapex",
4742 apex_name: "com.android.myapex",
4743 set: "company-myapex.apks",
4744 filename: "com.company.android.myapex.capex"
4745 }
4746 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4747
4748 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4749 apex_set {
4750 name: "com.company.android.myapex",
4751 apex_name: "com.android.myapex",
4752 set: "company-myapex.apks",
4753 filename: "some-random-suffix"
4754 }
4755 `)
4756}
4757
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004758func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004759 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004760 prebuilt_apex {
4761 name: "myapex.prebuilt",
4762 src: "myapex-arm.apex",
4763 overrides: [
4764 "myapex",
4765 ],
4766 }
4767 `)
4768
Wei Li340ee8e2022-03-18 17:33:24 -07004769 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4770 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004771
4772 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004773 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004774 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004775 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004776 }
Wei Li340ee8e2022-03-18 17:33:24 -07004777 rule := testingModule.Rule("genProvenanceMetaData")
4778 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4779 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4780 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4781 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004782}
4783
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004784func TestPrebuiltApexName(t *testing.T) {
4785 testApex(t, `
4786 prebuilt_apex {
4787 name: "com.company.android.myapex",
4788 apex_name: "com.android.myapex",
4789 src: "company-myapex-arm.apex",
4790 }
4791 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4792
4793 testApex(t, `
4794 apex_set {
4795 name: "com.company.android.myapex",
4796 apex_name: "com.android.myapex",
4797 set: "company-myapex.apks",
4798 }
4799 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4800}
4801
4802func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4803 _ = android.GroupFixturePreparers(
4804 java.PrepareForTestWithJavaDefaultModules,
4805 PrepareForTestWithApexBuildComponents,
4806 android.FixtureWithRootAndroidBp(`
4807 platform_bootclasspath {
4808 name: "platform-bootclasspath",
4809 fragments: [
4810 {
4811 apex: "com.android.art",
4812 module: "art-bootclasspath-fragment",
4813 },
4814 ],
4815 }
4816
4817 prebuilt_apex {
4818 name: "com.company.android.art",
4819 apex_name: "com.android.art",
4820 src: "com.company.android.art-arm.apex",
4821 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4822 }
4823
4824 prebuilt_bootclasspath_fragment {
4825 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004826 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004827 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004828 hidden_api: {
4829 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4830 metadata: "my-bootclasspath-fragment/metadata.csv",
4831 index: "my-bootclasspath-fragment/index.csv",
4832 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4833 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4834 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004835 }
4836
4837 java_import {
4838 name: "core-oj",
4839 jars: ["prebuilt.jar"],
4840 }
4841 `),
4842 ).RunTest(t)
4843}
4844
Spandan Das59a4a2b2024-01-09 21:35:56 +00004845// A minimal context object for use with DexJarBuildPath
4846type moduleErrorfTestCtx struct {
4847}
4848
4849func (ctx moduleErrorfTestCtx) ModuleErrorf(format string, args ...interface{}) {
4850}
4851
Paul Duffin092153d2021-01-26 11:42:39 +00004852// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4853// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004854func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004855 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004856
Paul Duffin89886cb2021-02-05 16:44:03 +00004857 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004858 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004859 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004860 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Spandan Das59a4a2b2024-01-09 21:35:56 +00004861 dexJarBuildPath := p.DexJarBuildPath(moduleErrorfTestCtx{}).PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004862 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004863 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
Spandan Das3576e762024-01-03 18:57:03 +00004864 ".intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
Jeongik Chad5fe8782021-07-08 01:13:11 +09004865 android.NormalizePathForTesting(dexJarBuildPath))
4866 }
4867
4868 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004869 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004870 // Make sure the import has been given the correct path to the dex jar.
4871 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4872 dexJarBuildPath := p.DexJarInstallPath()
4873 stem := android.RemoveOptionalPrebuiltPrefix(name)
4874 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4875 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4876 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004877 }
4878
Paul Duffin39853512021-02-26 11:09:39 +00004879 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004880 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004881 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004882 android.AssertArrayString(t, "Check if there is no source variant",
4883 []string{"android_common"},
4884 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004885 }
4886
4887 t.Run("prebuilt only", func(t *testing.T) {
4888 bp := `
4889 prebuilt_apex {
4890 name: "myapex",
4891 arch: {
4892 arm64: {
4893 src: "myapex-arm64.apex",
4894 },
4895 arm: {
4896 src: "myapex-arm.apex",
4897 },
4898 },
Paul Duffin39853512021-02-26 11:09:39 +00004899 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004900 }
4901
4902 java_import {
4903 name: "libfoo",
4904 jars: ["libfoo.jar"],
4905 }
Paul Duffin39853512021-02-26 11:09:39 +00004906
4907 java_sdk_library_import {
4908 name: "libbar",
4909 public: {
4910 jars: ["libbar.jar"],
4911 },
4912 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004913 `
4914
4915 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4916 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4917
Spandan Das3576e762024-01-03 18:57:03 +00004918 deapexerName := deapexerModuleName("prebuilt_myapex")
4919 android.AssertStringEquals(t, "APEX module name from deapexer name", "prebuilt_myapex", apexModuleName(deapexerName))
Martin Stjernholm44825602021-09-17 01:44:12 +01004920
Paul Duffinf6932af2021-02-26 18:21:56 +00004921 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01004922 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00004923 rule := deapexer.Rule("deapexer")
4924 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
4925 t.Errorf("expected: %q, found: %q", expected, actual)
4926 }
4927
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004928 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01004929 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00004930 rule = prebuiltApex.Rule("android/soong/android.Cp")
4931 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
4932 t.Errorf("expected: %q, found: %q", expected, actual)
4933 }
4934
Paul Duffin89886cb2021-02-05 16:44:03 +00004935 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004936 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004937
4938 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004939 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004940 })
4941
4942 t.Run("prebuilt with source preferred", func(t *testing.T) {
4943
4944 bp := `
4945 prebuilt_apex {
4946 name: "myapex",
4947 arch: {
4948 arm64: {
4949 src: "myapex-arm64.apex",
4950 },
4951 arm: {
4952 src: "myapex-arm.apex",
4953 },
4954 },
Paul Duffin39853512021-02-26 11:09:39 +00004955 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004956 }
4957
4958 java_import {
4959 name: "libfoo",
4960 jars: ["libfoo.jar"],
4961 }
4962
4963 java_library {
4964 name: "libfoo",
4965 }
Paul Duffin39853512021-02-26 11:09:39 +00004966
4967 java_sdk_library_import {
4968 name: "libbar",
4969 public: {
4970 jars: ["libbar.jar"],
4971 },
4972 }
4973
4974 java_sdk_library {
4975 name: "libbar",
4976 srcs: ["foo/bar/MyClass.java"],
4977 unsafe_ignore_missing_latest_api: true,
4978 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004979 `
4980
4981 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
4982 ctx := testDexpreoptWithApexes(t, bp, "", transform)
4983
Paul Duffin89886cb2021-02-05 16:44:03 +00004984 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004985 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00004986 ensureNoSourceVariant(t, ctx, "libfoo")
4987
4988 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09004989 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00004990 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00004991 })
4992
4993 t.Run("prebuilt preferred with source", func(t *testing.T) {
4994 bp := `
4995 prebuilt_apex {
4996 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00004997 arch: {
4998 arm64: {
4999 src: "myapex-arm64.apex",
5000 },
5001 arm: {
5002 src: "myapex-arm.apex",
5003 },
5004 },
Paul Duffin39853512021-02-26 11:09:39 +00005005 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005006 }
5007
5008 java_import {
5009 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005010 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005011 jars: ["libfoo.jar"],
5012 }
5013
5014 java_library {
5015 name: "libfoo",
5016 }
Paul Duffin39853512021-02-26 11:09:39 +00005017
5018 java_sdk_library_import {
5019 name: "libbar",
5020 prefer: true,
5021 public: {
5022 jars: ["libbar.jar"],
5023 },
5024 }
5025
5026 java_sdk_library {
5027 name: "libbar",
5028 srcs: ["foo/bar/MyClass.java"],
5029 unsafe_ignore_missing_latest_api: true,
5030 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005031 `
5032
5033 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5034 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5035
Paul Duffin89886cb2021-02-05 16:44:03 +00005036 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005037 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005038 ensureNoSourceVariant(t, ctx, "libfoo")
5039
5040 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005041 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005042 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005043 })
5044}
5045
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005046func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005047 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005048 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005049 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5050 // is disabled.
5051 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
5052 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005053
Paul Duffin37856732021-02-26 14:24:15 +00005054 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5055 t.Helper()
Jiakai Zhangc6879f32023-11-06 16:31:19 +00005056 s := ctx.ModuleForTests("dex_bootjars", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005057 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005058 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005059 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005060 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005061 foundLibfooJar = true
5062 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005063 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005064 }
5065 }
5066 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005067 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 +00005068 }
5069 }
5070
Paul Duffin40a3f652021-07-19 13:11:24 +01005071 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005072 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005073 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005074 var rule android.TestingBuildParams
5075
5076 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5077 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005078 }
5079
Paul Duffin40a3f652021-07-19 13:11:24 +01005080 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5081 t.Helper()
5082 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5083 var rule android.TestingBuildParams
5084
5085 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5086 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5087 }
5088
Paul Duffin89f570a2021-06-16 01:42:33 +01005089 fragment := java.ApexVariantReference{
5090 Apex: proptools.StringPtr("myapex"),
5091 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5092 }
5093
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005094 t.Run("prebuilt only", func(t *testing.T) {
5095 bp := `
5096 prebuilt_apex {
5097 name: "myapex",
5098 arch: {
5099 arm64: {
5100 src: "myapex-arm64.apex",
5101 },
5102 arm: {
5103 src: "myapex-arm.apex",
5104 },
5105 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005106 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5107 }
5108
5109 prebuilt_bootclasspath_fragment {
5110 name: "my-bootclasspath-fragment",
5111 contents: ["libfoo", "libbar"],
5112 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005113 hidden_api: {
5114 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5115 metadata: "my-bootclasspath-fragment/metadata.csv",
5116 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005117 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5118 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5119 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005120 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005121 }
5122
5123 java_import {
5124 name: "libfoo",
5125 jars: ["libfoo.jar"],
5126 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005127 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005128 }
Paul Duffin37856732021-02-26 14:24:15 +00005129
5130 java_sdk_library_import {
5131 name: "libbar",
5132 public: {
5133 jars: ["libbar.jar"],
5134 },
5135 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005136 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005137 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005138 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005139 `
5140
Paul Duffin89f570a2021-06-16 01:42:33 +01005141 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005142 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5143 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005144
Paul Duffin537ea3d2021-05-14 10:38:00 +01005145 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005146 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005147 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005148 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005149 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005150 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 +01005151 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005152 })
5153
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005154 t.Run("apex_set only", func(t *testing.T) {
5155 bp := `
5156 apex_set {
5157 name: "myapex",
5158 set: "myapex.apks",
Liz Kammer2dc72442023-04-20 10:10:48 -04005159 exported_java_libs: ["myjavalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005160 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
Liz Kammer2dc72442023-04-20 10:10:48 -04005161 exported_systemserverclasspath_fragments: ["my-systemserverclasspath-fragment"],
5162 }
5163
5164 java_import {
5165 name: "myjavalib",
5166 jars: ["myjavalib.jar"],
5167 apex_available: ["myapex"],
5168 permitted_packages: ["javalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005169 }
5170
5171 prebuilt_bootclasspath_fragment {
5172 name: "my-bootclasspath-fragment",
5173 contents: ["libfoo", "libbar"],
5174 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005175 hidden_api: {
5176 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5177 metadata: "my-bootclasspath-fragment/metadata.csv",
5178 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005179 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5180 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5181 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005182 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005183 }
5184
Liz Kammer2dc72442023-04-20 10:10:48 -04005185 prebuilt_systemserverclasspath_fragment {
5186 name: "my-systemserverclasspath-fragment",
5187 contents: ["libbaz"],
5188 apex_available: ["myapex"],
5189 }
5190
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005191 java_import {
5192 name: "libfoo",
5193 jars: ["libfoo.jar"],
5194 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005195 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005196 }
5197
5198 java_sdk_library_import {
5199 name: "libbar",
5200 public: {
5201 jars: ["libbar.jar"],
5202 },
5203 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005204 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005205 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005206 }
Liz Kammer2dc72442023-04-20 10:10:48 -04005207
5208 java_sdk_library_import {
5209 name: "libbaz",
5210 public: {
5211 jars: ["libbaz.jar"],
5212 },
5213 apex_available: ["myapex"],
5214 shared_library: false,
5215 permitted_packages: ["baz"],
5216 }
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005217 `
5218
Paul Duffin89f570a2021-06-16 01:42:33 +01005219 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005220 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5221 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005222
Paul Duffin537ea3d2021-05-14 10:38:00 +01005223 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005224 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005225 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005226 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005227 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005228 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 +01005229 `)
Liz Kammer2dc72442023-04-20 10:10:48 -04005230
5231 myApex := ctx.ModuleForTests("myapex", "android_common_myapex").Module()
5232
5233 overrideNames := []string{
Spandan Das3576e762024-01-03 18:57:03 +00005234 "myapex",
Liz Kammer2dc72442023-04-20 10:10:48 -04005235 "myjavalib.myapex",
5236 "libfoo.myapex",
5237 "libbar.myapex",
5238 "libbaz.myapex",
5239 }
5240 mkEntries := android.AndroidMkEntriesForTest(t, ctx, myApex)
5241 for i, e := range mkEntries {
5242 g := e.OverrideName
5243 if w := overrideNames[i]; w != g {
5244 t.Errorf("Expected override name %q, got %q", w, g)
5245 }
5246 }
5247
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005248 })
5249
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005250 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5251 bp := `
5252 prebuilt_apex {
5253 name: "myapex",
5254 arch: {
5255 arm64: {
5256 src: "myapex-arm64.apex",
5257 },
5258 arm: {
5259 src: "myapex-arm.apex",
5260 },
5261 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005262 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5263 }
5264
5265 prebuilt_bootclasspath_fragment {
5266 name: "my-bootclasspath-fragment",
5267 contents: ["libfoo", "libbar"],
5268 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005269 hidden_api: {
5270 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5271 metadata: "my-bootclasspath-fragment/metadata.csv",
5272 index: "my-bootclasspath-fragment/index.csv",
5273 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5274 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5275 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005276 }
5277
5278 java_import {
5279 name: "libfoo",
5280 jars: ["libfoo.jar"],
5281 apex_available: ["myapex"],
5282 }
5283
5284 java_library {
5285 name: "libfoo",
5286 srcs: ["foo/bar/MyClass.java"],
5287 apex_available: ["myapex"],
5288 }
Paul Duffin37856732021-02-26 14:24:15 +00005289
5290 java_sdk_library_import {
5291 name: "libbar",
5292 public: {
5293 jars: ["libbar.jar"],
5294 },
5295 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005296 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005297 }
5298
5299 java_sdk_library {
5300 name: "libbar",
5301 srcs: ["foo/bar/MyClass.java"],
5302 unsafe_ignore_missing_latest_api: true,
5303 apex_available: ["myapex"],
5304 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005305 `
5306
5307 // In this test the source (java_library) libfoo is active since the
5308 // prebuilt (java_import) defaults to prefer:false. However the
5309 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5310 // find the dex boot jar in it. We either need to disable the source libfoo
5311 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005312 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005313 // dexbootjar check is skipped if AllowMissingDependencies is true
5314 preparerAllowMissingDeps := android.GroupFixturePreparers(
5315 preparer,
5316 android.PrepareForTestWithAllowMissingDependencies,
5317 )
5318 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005319 })
5320
5321 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5322 bp := `
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005323 apex {
5324 name: "myapex",
5325 key: "myapex.key",
5326 updatable: false,
5327 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5328 }
5329
5330 apex_key {
5331 name: "myapex.key",
5332 public_key: "testkey.avbpubkey",
5333 private_key: "testkey.pem",
5334 }
5335
5336 bootclasspath_fragment {
5337 name: "my-bootclasspath-fragment",
5338 contents: ["libfoo", "libbar"],
5339 apex_available: ["myapex"],
5340 hidden_api: {
5341 split_packages: ["*"],
5342 },
5343 }
5344
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005345 prebuilt_apex {
5346 name: "myapex",
5347 arch: {
5348 arm64: {
5349 src: "myapex-arm64.apex",
5350 },
5351 arm: {
5352 src: "myapex-arm.apex",
5353 },
5354 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005355 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5356 }
5357
5358 prebuilt_bootclasspath_fragment {
5359 name: "my-bootclasspath-fragment",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005360 prefer: true,
Paul Duffin89f570a2021-06-16 01:42:33 +01005361 contents: ["libfoo", "libbar"],
5362 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005363 hidden_api: {
5364 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5365 metadata: "my-bootclasspath-fragment/metadata.csv",
5366 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005367 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5368 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5369 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005370 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005371 }
5372
5373 java_import {
5374 name: "libfoo",
5375 prefer: true,
5376 jars: ["libfoo.jar"],
5377 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005378 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005379 }
5380
5381 java_library {
5382 name: "libfoo",
5383 srcs: ["foo/bar/MyClass.java"],
5384 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005385 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005386 }
Paul Duffin37856732021-02-26 14:24:15 +00005387
5388 java_sdk_library_import {
5389 name: "libbar",
5390 prefer: true,
5391 public: {
5392 jars: ["libbar.jar"],
5393 },
5394 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005395 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005396 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005397 }
5398
5399 java_sdk_library {
5400 name: "libbar",
5401 srcs: ["foo/bar/MyClass.java"],
5402 unsafe_ignore_missing_latest_api: true,
5403 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005404 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005405 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005406 `
5407
Paul Duffin89f570a2021-06-16 01:42:33 +01005408 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005409 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5410 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005411
Paul Duffin537ea3d2021-05-14 10:38:00 +01005412 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005413 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005414 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005415 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005416 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005417 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 +01005418 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005419 })
5420
5421 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5422 bp := `
5423 apex {
5424 name: "myapex",
5425 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005426 updatable: false,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005427 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005428 }
5429
5430 apex_key {
5431 name: "myapex.key",
5432 public_key: "testkey.avbpubkey",
5433 private_key: "testkey.pem",
5434 }
5435
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005436 bootclasspath_fragment {
5437 name: "my-bootclasspath-fragment",
5438 contents: ["libfoo", "libbar"],
5439 apex_available: ["myapex"],
5440 hidden_api: {
5441 split_packages: ["*"],
5442 },
5443 }
5444
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005445 prebuilt_apex {
5446 name: "myapex",
5447 arch: {
5448 arm64: {
5449 src: "myapex-arm64.apex",
5450 },
5451 arm: {
5452 src: "myapex-arm.apex",
5453 },
5454 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005455 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5456 }
5457
5458 prebuilt_bootclasspath_fragment {
5459 name: "my-bootclasspath-fragment",
5460 contents: ["libfoo", "libbar"],
5461 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005462 hidden_api: {
5463 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5464 metadata: "my-bootclasspath-fragment/metadata.csv",
5465 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005466 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5467 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5468 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005469 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005470 }
5471
5472 java_import {
5473 name: "libfoo",
5474 jars: ["libfoo.jar"],
5475 apex_available: ["myapex"],
5476 }
5477
5478 java_library {
5479 name: "libfoo",
5480 srcs: ["foo/bar/MyClass.java"],
5481 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005482 permitted_packages: ["foo"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005483 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005484 }
Paul Duffin37856732021-02-26 14:24:15 +00005485
5486 java_sdk_library_import {
5487 name: "libbar",
5488 public: {
5489 jars: ["libbar.jar"],
5490 },
5491 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005492 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005493 }
5494
5495 java_sdk_library {
5496 name: "libbar",
5497 srcs: ["foo/bar/MyClass.java"],
5498 unsafe_ignore_missing_latest_api: true,
5499 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005500 permitted_packages: ["bar"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005501 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005502 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005503 `
5504
Paul Duffin89f570a2021-06-16 01:42:33 +01005505 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Jiakai Zhangc6879f32023-11-06 16:31:19 +00005506 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/hiddenapi-modular/encoded/libfoo.jar")
5507 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 +00005508
Paul Duffin537ea3d2021-05-14 10:38:00 +01005509 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005510 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005511 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
5512 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005513 out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/modular-hiddenapi/index.csv
5514 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 +01005515 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005516 })
5517
5518 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5519 bp := `
5520 apex {
5521 name: "myapex",
5522 enabled: false,
5523 key: "myapex.key",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005524 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005525 }
5526
5527 apex_key {
5528 name: "myapex.key",
5529 public_key: "testkey.avbpubkey",
5530 private_key: "testkey.pem",
5531 }
5532
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005533 bootclasspath_fragment {
5534 name: "my-bootclasspath-fragment",
5535 enabled: false,
5536 contents: ["libfoo", "libbar"],
5537 apex_available: ["myapex"],
5538 hidden_api: {
5539 split_packages: ["*"],
5540 },
5541 }
5542
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005543 prebuilt_apex {
5544 name: "myapex",
5545 arch: {
5546 arm64: {
5547 src: "myapex-arm64.apex",
5548 },
5549 arm: {
5550 src: "myapex-arm.apex",
5551 },
5552 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005553 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5554 }
5555
5556 prebuilt_bootclasspath_fragment {
5557 name: "my-bootclasspath-fragment",
5558 contents: ["libfoo", "libbar"],
5559 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005560 hidden_api: {
5561 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5562 metadata: "my-bootclasspath-fragment/metadata.csv",
5563 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005564 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5565 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5566 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005567 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005568 }
5569
5570 java_import {
5571 name: "libfoo",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005572 jars: ["libfoo.jar"],
5573 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005574 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005575 }
5576
5577 java_library {
5578 name: "libfoo",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005579 enabled: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005580 srcs: ["foo/bar/MyClass.java"],
5581 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005582 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005583 }
Paul Duffin37856732021-02-26 14:24:15 +00005584
5585 java_sdk_library_import {
5586 name: "libbar",
Paul Duffin37856732021-02-26 14:24:15 +00005587 public: {
5588 jars: ["libbar.jar"],
5589 },
5590 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005591 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005592 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005593 }
5594
5595 java_sdk_library {
5596 name: "libbar",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005597 enabled: false,
Paul Duffin37856732021-02-26 14:24:15 +00005598 srcs: ["foo/bar/MyClass.java"],
5599 unsafe_ignore_missing_latest_api: true,
5600 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005601 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005602 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005603 `
5604
Paul Duffin89f570a2021-06-16 01:42:33 +01005605 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das3576e762024-01-03 18:57:03 +00005606 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5607 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/prebuilt_myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005608
Paul Duffin537ea3d2021-05-14 10:38:00 +01005609 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005610 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005611 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005612 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005613 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005614 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 +01005615 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005616 })
Spandan Das3a392012024-01-17 18:26:27 +00005617
Spandan Dasf2c10572024-02-27 04:49:52 +00005618 t.Run("Co-existing unflagged apexes should create a duplicate module error", func(t *testing.T) {
Spandan Das3a392012024-01-17 18:26:27 +00005619 bp := `
5620 // Source
5621 apex {
5622 name: "myapex",
5623 enabled: false,
5624 key: "myapex.key",
5625 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5626 }
5627
5628 apex_key {
5629 name: "myapex.key",
5630 public_key: "testkey.avbpubkey",
5631 private_key: "testkey.pem",
5632 }
5633
5634 // Prebuilt
5635 prebuilt_apex {
5636 name: "myapex.v1",
5637 source_apex_name: "myapex",
5638 arch: {
5639 arm64: {
5640 src: "myapex-arm64.apex",
5641 },
5642 arm: {
5643 src: "myapex-arm.apex",
5644 },
5645 },
5646 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5647 prefer: true,
5648 }
5649 prebuilt_apex {
5650 name: "myapex.v2",
5651 source_apex_name: "myapex",
5652 arch: {
5653 arm64: {
5654 src: "myapex-arm64.apex",
5655 },
5656 arm: {
5657 src: "myapex-arm.apex",
5658 },
5659 },
5660 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5661 prefer: true,
5662 }
5663
5664 prebuilt_bootclasspath_fragment {
5665 name: "my-bootclasspath-fragment",
5666 contents: ["libfoo", "libbar"],
5667 apex_available: ["myapex"],
5668 hidden_api: {
5669 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5670 metadata: "my-bootclasspath-fragment/metadata.csv",
5671 index: "my-bootclasspath-fragment/index.csv",
5672 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5673 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5674 },
5675 prefer: true,
5676 }
5677
5678 java_import {
5679 name: "libfoo",
5680 jars: ["libfoo.jar"],
5681 apex_available: ["myapex"],
5682 prefer: true,
5683 }
5684 java_import {
5685 name: "libbar",
5686 jars: ["libbar.jar"],
5687 apex_available: ["myapex"],
5688 prefer: true,
5689 }
5690 `
5691
Spandan Dasf2c10572024-02-27 04:49:52 +00005692 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 +00005693 })
5694
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005695}
5696
Roland Levillain630846d2019-06-26 12:48:34 +01005697func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005698 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005699 apex_test {
5700 name: "myapex",
5701 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005702 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005703 tests: [
5704 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005705 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005706 ],
5707 }
5708
5709 apex_key {
5710 name: "myapex.key",
5711 public_key: "testkey.avbpubkey",
5712 private_key: "testkey.pem",
5713 }
5714
Liz Kammer1c14a212020-05-12 15:26:55 -07005715 filegroup {
5716 name: "fg",
5717 srcs: [
5718 "baz",
5719 "bar/baz"
5720 ],
5721 }
5722
Roland Levillain630846d2019-06-26 12:48:34 +01005723 cc_test {
5724 name: "mytest",
5725 gtest: false,
5726 srcs: ["mytest.cpp"],
5727 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005728 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005729 system_shared_libs: [],
5730 static_executable: true,
5731 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005732 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005733 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005734
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005735 cc_library {
5736 name: "mylib",
5737 srcs: ["mylib.cpp"],
5738 system_shared_libs: [],
5739 stl: "none",
5740 }
5741
Liz Kammer5bd365f2020-05-27 15:15:11 -07005742 filegroup {
5743 name: "fg2",
5744 srcs: [
5745 "testdata/baz"
5746 ],
5747 }
5748
Roland Levillain9b5fde92019-06-28 15:41:19 +01005749 cc_test {
5750 name: "mytests",
5751 gtest: false,
5752 srcs: [
5753 "mytest1.cpp",
5754 "mytest2.cpp",
5755 "mytest3.cpp",
5756 ],
5757 test_per_src: true,
5758 relative_install_path: "test",
5759 system_shared_libs: [],
5760 static_executable: true,
5761 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005762 data: [
5763 ":fg",
5764 ":fg2",
5765 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005766 }
Roland Levillain630846d2019-06-26 12:48:34 +01005767 `)
5768
Jooyung Hana0503a52023-08-23 13:12:50 +09005769 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005770 copyCmds := apexRule.Args["copy_commands"]
5771
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005772 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005773 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005774 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005775
Liz Kammer1c14a212020-05-12 15:26:55 -07005776 //Ensure that test data are copied into apex.
5777 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5778 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5779
Roland Levillain9b5fde92019-06-28 15:41:19 +01005780 // Ensure that test deps built with `test_per_src` are copied into apex.
5781 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5782 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5783 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005784
5785 // Ensure the module is correctly translated.
Jooyung Hana0503a52023-08-23 13:12:50 +09005786 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005787 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005788 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005789 prefix := "TARGET_"
5790 var builder strings.Builder
5791 data.Custom(&builder, name, prefix, "", data)
5792 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005793 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5794 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5795 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5796 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005797 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005798}
5799
Jooyung Hand48f3c32019-08-23 11:18:57 +09005800func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5801 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5802 apex {
5803 name: "myapex",
5804 key: "myapex.key",
5805 native_shared_libs: ["libfoo"],
5806 }
5807
5808 apex_key {
5809 name: "myapex.key",
5810 public_key: "testkey.avbpubkey",
5811 private_key: "testkey.pem",
5812 }
5813
5814 cc_library {
5815 name: "libfoo",
5816 stl: "none",
5817 system_shared_libs: [],
5818 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005819 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005820 }
5821 `)
5822 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5823 apex {
5824 name: "myapex",
5825 key: "myapex.key",
5826 java_libs: ["myjar"],
5827 }
5828
5829 apex_key {
5830 name: "myapex.key",
5831 public_key: "testkey.avbpubkey",
5832 private_key: "testkey.pem",
5833 }
5834
5835 java_library {
5836 name: "myjar",
5837 srcs: ["foo/bar/MyClass.java"],
5838 sdk_version: "none",
5839 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005840 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005841 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005842 }
5843 `)
5844}
5845
Bill Peckhama41a6962021-01-11 10:58:54 -08005846func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005847 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005848 apex {
5849 name: "myapex",
5850 key: "myapex.key",
5851 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005852 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005853 }
5854
5855 apex_key {
5856 name: "myapex.key",
5857 public_key: "testkey.avbpubkey",
5858 private_key: "testkey.pem",
5859 }
5860
5861 java_import {
5862 name: "myjavaimport",
5863 apex_available: ["myapex"],
5864 jars: ["my.jar"],
5865 compile_dex: true,
5866 }
5867 `)
5868
Jooyung Hana0503a52023-08-23 13:12:50 +09005869 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Bill Peckhama41a6962021-01-11 10:58:54 -08005870 apexRule := module.Rule("apexRule")
5871 copyCmds := apexRule.Args["copy_commands"]
5872 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5873}
5874
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005875func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005876 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005877 apex {
5878 name: "myapex",
5879 key: "myapex.key",
5880 apps: [
5881 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005882 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005883 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005884 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005885 }
5886
5887 apex_key {
5888 name: "myapex.key",
5889 public_key: "testkey.avbpubkey",
5890 private_key: "testkey.pem",
5891 }
5892
5893 android_app {
5894 name: "AppFoo",
5895 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005896 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005897 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005898 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005899 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005900 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005901 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005902
5903 android_app {
5904 name: "AppFooPriv",
5905 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005906 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005907 system_modules: "none",
5908 privileged: true,
Sam Delmerico15809f82023-05-15 17:21:47 -04005909 privapp_allowlist: "privapp_allowlist_com.android.AppFooPriv.xml",
Colin Cross094cde42020-02-15 10:38:00 -08005910 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005911 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005912 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005913
5914 cc_library_shared {
5915 name: "libjni",
5916 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005917 shared_libs: ["libfoo"],
5918 stl: "none",
5919 system_shared_libs: [],
5920 apex_available: [ "myapex" ],
5921 sdk_version: "current",
5922 }
5923
5924 cc_library_shared {
5925 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005926 stl: "none",
5927 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005928 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005929 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005930 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005931 `)
5932
Jooyung Hana0503a52023-08-23 13:12:50 +09005933 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005934 apexRule := module.Rule("apexRule")
5935 copyCmds := apexRule.Args["copy_commands"]
5936
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005937 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5938 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Andrei Onea580636b2022-08-17 16:53:46 +00005939 ensureContains(t, copyCmds, "image.apex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005940
Colin Crossaede88c2020-08-11 12:17:01 -07005941 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005942 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005943 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005944 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005945 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005946 // JNI libraries including transitive deps are
5947 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005948 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005949 // ... embedded inside APK (jnilibs.zip)
5950 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5951 // ... and not directly inside the APEX
5952 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5953 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04005954
5955 apexBundle := module.Module().(*apexBundle)
5956 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
5957 var builder strings.Builder
5958 data.Custom(&builder, apexBundle.Name(), "TARGET_", "", data)
5959 androidMk := builder.String()
5960 ensureContains(t, androidMk, "LOCAL_MODULE := AppFooPriv.myapex")
5961 ensureContains(t, androidMk, "LOCAL_MODULE := AppFoo.myapex")
5962 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFooPriv.apk")
5963 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFoo.apk")
5964 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := \\S+AppFooPriv.apk")
5965 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 +01005966}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005967
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005968func TestApexWithAppImportBuildId(t *testing.T) {
5969 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5970 for _, id := range invalidBuildIds {
5971 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5972 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5973 variables.BuildId = proptools.StringPtr(id)
5974 })
5975 testApexError(t, message, `apex {
5976 name: "myapex",
5977 key: "myapex.key",
5978 apps: ["AppFooPrebuilt"],
5979 updatable: false,
5980 }
5981
5982 apex_key {
5983 name: "myapex.key",
5984 public_key: "testkey.avbpubkey",
5985 private_key: "testkey.pem",
5986 }
5987
5988 android_app_import {
5989 name: "AppFooPrebuilt",
5990 apk: "PrebuiltAppFoo.apk",
5991 presigned: true,
5992 apex_available: ["myapex"],
5993 }
5994 `, fixture)
5995 }
5996}
5997
Dario Frenicde2a032019-10-27 00:29:22 +01005998func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005999 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01006000 apex {
6001 name: "myapex",
6002 key: "myapex.key",
6003 apps: [
6004 "AppFooPrebuilt",
6005 "AppFooPrivPrebuilt",
6006 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006007 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01006008 }
6009
6010 apex_key {
6011 name: "myapex.key",
6012 public_key: "testkey.avbpubkey",
6013 private_key: "testkey.pem",
6014 }
6015
6016 android_app_import {
6017 name: "AppFooPrebuilt",
6018 apk: "PrebuiltAppFoo.apk",
6019 presigned: true,
6020 dex_preopt: {
6021 enabled: false,
6022 },
Jiyong Park592a6a42020-04-21 22:34:28 +09006023 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006024 }
6025
6026 android_app_import {
6027 name: "AppFooPrivPrebuilt",
6028 apk: "PrebuiltAppFooPriv.apk",
6029 privileged: true,
6030 presigned: true,
6031 dex_preopt: {
6032 enabled: false,
6033 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006034 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006035 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006036 }
6037 `)
6038
Jooyung Hana0503a52023-08-23 13:12:50 +09006039 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Frenicde2a032019-10-27 00:29:22 +01006040 apexRule := module.Rule("apexRule")
6041 copyCmds := apexRule.Args["copy_commands"]
6042
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006043 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6044 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006045}
6046
6047func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006048 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006049 apex {
6050 name: "myapex",
6051 key: "myapex.key",
6052 apps: [
6053 "AppFoo",
6054 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006055 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006056 }
6057
6058 apex_key {
6059 name: "myapex.key",
6060 public_key: "testkey.avbpubkey",
6061 private_key: "testkey.pem",
6062 }
6063
6064 android_app {
6065 name: "AppFoo",
6066 srcs: ["foo/bar/MyClass.java"],
6067 sdk_version: "none",
6068 system_modules: "none",
6069 apex_available: [ "myapex" ],
6070 }
6071
6072 android_app_import {
6073 name: "AppFoo",
6074 apk: "AppFooPrebuilt.apk",
6075 filename: "AppFooPrebuilt.apk",
6076 presigned: true,
6077 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006078 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006079 }
6080 `, withFiles(map[string][]byte{
6081 "AppFooPrebuilt.apk": nil,
6082 }))
6083
Jooyung Hana0503a52023-08-23 13:12:50 +09006084 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006085 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006086 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006087}
6088
Dario Freni6f3937c2019-12-20 22:58:03 +00006089func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006090 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006091 apex {
6092 name: "myapex",
6093 key: "myapex.key",
6094 apps: [
6095 "TesterHelpAppFoo",
6096 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006097 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006098 }
6099
6100 apex_key {
6101 name: "myapex.key",
6102 public_key: "testkey.avbpubkey",
6103 private_key: "testkey.pem",
6104 }
6105
6106 android_test_helper_app {
6107 name: "TesterHelpAppFoo",
6108 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006109 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006110 }
6111
6112 `)
6113
Jooyung Hana0503a52023-08-23 13:12:50 +09006114 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Freni6f3937c2019-12-20 22:58:03 +00006115 apexRule := module.Rule("apexRule")
6116 copyCmds := apexRule.Args["copy_commands"]
6117
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006118 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006119}
6120
Jooyung Han18020ea2019-11-13 10:50:48 +09006121func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6122 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006123 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006124 apex {
6125 name: "myapex",
6126 key: "myapex.key",
6127 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006128 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006129 }
6130
6131 apex_key {
6132 name: "myapex.key",
6133 public_key: "testkey.avbpubkey",
6134 private_key: "testkey.pem",
6135 }
6136
6137 apex {
6138 name: "otherapex",
6139 key: "myapex.key",
6140 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006141 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006142 }
6143
6144 cc_defaults {
6145 name: "libfoo-defaults",
6146 apex_available: ["otherapex"],
6147 }
6148
6149 cc_library {
6150 name: "libfoo",
6151 defaults: ["libfoo-defaults"],
6152 stl: "none",
6153 system_shared_libs: [],
6154 }`)
6155}
6156
Paul Duffine52e66f2020-03-30 17:54:29 +01006157func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006158 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006159 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006160 apex {
6161 name: "myapex",
6162 key: "myapex.key",
6163 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006164 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006165 }
6166
6167 apex_key {
6168 name: "myapex.key",
6169 public_key: "testkey.avbpubkey",
6170 private_key: "testkey.pem",
6171 }
6172
6173 apex {
6174 name: "otherapex",
6175 key: "otherapex.key",
6176 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006177 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006178 }
6179
6180 apex_key {
6181 name: "otherapex.key",
6182 public_key: "testkey.avbpubkey",
6183 private_key: "testkey.pem",
6184 }
6185
6186 cc_library {
6187 name: "libfoo",
6188 stl: "none",
6189 system_shared_libs: [],
6190 apex_available: ["otherapex"],
6191 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006192}
Jiyong Park127b40b2019-09-30 16:04:35 +09006193
Paul Duffine52e66f2020-03-30 17:54:29 +01006194func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006195 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006196 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006197.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006198.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006199.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006200.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006201.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006202.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006203 apex {
6204 name: "myapex",
6205 key: "myapex.key",
6206 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006207 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006208 }
6209
6210 apex_key {
6211 name: "myapex.key",
6212 public_key: "testkey.avbpubkey",
6213 private_key: "testkey.pem",
6214 }
6215
Jiyong Park127b40b2019-09-30 16:04:35 +09006216 cc_library {
6217 name: "libfoo",
6218 stl: "none",
6219 shared_libs: ["libbar"],
6220 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006221 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006222 }
6223
6224 cc_library {
6225 name: "libbar",
6226 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006227 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006228 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006229 apex_available: ["myapex"],
6230 }
6231
6232 cc_library {
6233 name: "libbaz",
6234 stl: "none",
6235 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006236 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006237}
Jiyong Park127b40b2019-09-30 16:04:35 +09006238
Liz Kammer5f108fa2023-05-11 14:33:17 -04006239func TestApexAvailable_IndirectStaticDep(t *testing.T) {
6240 testApex(t, `
6241 apex {
6242 name: "myapex",
6243 key: "myapex.key",
6244 native_shared_libs: ["libfoo"],
6245 updatable: false,
6246 }
6247
6248 apex_key {
6249 name: "myapex.key",
6250 public_key: "testkey.avbpubkey",
6251 private_key: "testkey.pem",
6252 }
6253
6254 cc_library {
6255 name: "libfoo",
6256 stl: "none",
6257 static_libs: ["libbar"],
6258 system_shared_libs: [],
6259 apex_available: ["myapex"],
6260 }
6261
6262 cc_library {
6263 name: "libbar",
6264 stl: "none",
6265 shared_libs: ["libbaz"],
6266 system_shared_libs: [],
6267 apex_available: ["myapex"],
6268 }
6269
6270 cc_library {
6271 name: "libbaz",
6272 stl: "none",
6273 system_shared_libs: [],
6274 }`)
6275
6276 testApexError(t, `requires "libbar" that doesn't list the APEX under 'apex_available'.`, `
6277 apex {
6278 name: "myapex",
6279 key: "myapex.key",
6280 native_shared_libs: ["libfoo"],
6281 updatable: false,
6282 }
6283
6284 apex_key {
6285 name: "myapex.key",
6286 public_key: "testkey.avbpubkey",
6287 private_key: "testkey.pem",
6288 }
6289
6290 cc_library {
6291 name: "libfoo",
6292 stl: "none",
6293 static_libs: ["libbar"],
6294 system_shared_libs: [],
6295 apex_available: ["myapex"],
6296 }
6297
6298 cc_library {
6299 name: "libbar",
6300 stl: "none",
6301 system_shared_libs: [],
6302 }`)
6303}
6304
Paul Duffine52e66f2020-03-30 17:54:29 +01006305func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006306 testApexError(t, "\"otherapex\" is not a valid module name", `
6307 apex {
6308 name: "myapex",
6309 key: "myapex.key",
6310 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006311 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006312 }
6313
6314 apex_key {
6315 name: "myapex.key",
6316 public_key: "testkey.avbpubkey",
6317 private_key: "testkey.pem",
6318 }
6319
6320 cc_library {
6321 name: "libfoo",
6322 stl: "none",
6323 system_shared_libs: [],
6324 apex_available: ["otherapex"],
6325 }`)
6326
Paul Duffine52e66f2020-03-30 17:54:29 +01006327 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006328 apex {
6329 name: "myapex",
6330 key: "myapex.key",
6331 native_shared_libs: ["libfoo", "libbar"],
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: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006345 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006346 apex_available: ["myapex"],
6347 }
6348
6349 cc_library {
6350 name: "libbar",
6351 stl: "none",
6352 system_shared_libs: [],
6353 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006354 }
6355
6356 cc_library {
6357 name: "libbaz",
6358 stl: "none",
6359 system_shared_libs: [],
6360 stubs: {
6361 versions: ["10", "20", "30"],
6362 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006363 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006364}
Jiyong Park127b40b2019-09-30 16:04:35 +09006365
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006366func TestApexAvailable_ApexAvailableNameWithVersionCodeError(t *testing.T) {
6367 t.Run("negative variant_version produces error", func(t *testing.T) {
6368 testApexError(t, "expected an integer between 0-9; got -1", `
6369 apex {
6370 name: "myapex",
6371 key: "myapex.key",
6372 apex_available_name: "com.android.foo",
6373 variant_version: "-1",
6374 updatable: false,
6375 }
6376 apex_key {
6377 name: "myapex.key",
6378 public_key: "testkey.avbpubkey",
6379 private_key: "testkey.pem",
6380 }
6381 `)
6382 })
6383
6384 t.Run("variant_version greater than 9 produces error", func(t *testing.T) {
6385 testApexError(t, "expected an integer between 0-9; got 10", `
6386 apex {
6387 name: "myapex",
6388 key: "myapex.key",
6389 apex_available_name: "com.android.foo",
6390 variant_version: "10",
6391 updatable: false,
6392 }
6393 apex_key {
6394 name: "myapex.key",
6395 public_key: "testkey.avbpubkey",
6396 private_key: "testkey.pem",
6397 }
6398 `)
6399 })
6400}
6401
6402func TestApexAvailable_ApexAvailableNameWithVersionCode(t *testing.T) {
6403 context := android.GroupFixturePreparers(
6404 android.PrepareForIntegrationTestWithAndroid,
6405 PrepareForTestWithApexBuildComponents,
6406 android.FixtureMergeMockFs(android.MockFS{
6407 "system/sepolicy/apex/foo-file_contexts": nil,
6408 "system/sepolicy/apex/bar-file_contexts": nil,
6409 }),
6410 )
6411 result := context.RunTestWithBp(t, `
6412 apex {
6413 name: "foo",
6414 key: "myapex.key",
6415 apex_available_name: "com.android.foo",
6416 variant_version: "0",
6417 updatable: false,
6418 }
6419 apex {
6420 name: "bar",
6421 key: "myapex.key",
6422 apex_available_name: "com.android.foo",
6423 variant_version: "3",
6424 updatable: false,
6425 }
6426 apex_key {
6427 name: "myapex.key",
6428 public_key: "testkey.avbpubkey",
6429 private_key: "testkey.pem",
6430 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006431 override_apex {
6432 name: "myoverrideapex",
6433 base: "bar",
6434 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006435 `)
6436
Jooyung Hana0503a52023-08-23 13:12:50 +09006437 fooManifestRule := result.ModuleForTests("foo", "android_common_foo").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006438 fooExpectedDefaultVersion := android.DefaultUpdatableModuleVersion
6439 fooActualDefaultVersion := fooManifestRule.Args["default_version"]
6440 if fooActualDefaultVersion != fooExpectedDefaultVersion {
6441 t.Errorf("expected to find defaultVersion %q; got %q", fooExpectedDefaultVersion, fooActualDefaultVersion)
6442 }
6443
Jooyung Hana0503a52023-08-23 13:12:50 +09006444 barManifestRule := result.ModuleForTests("bar", "android_common_bar").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006445 defaultVersionInt, _ := strconv.Atoi(android.DefaultUpdatableModuleVersion)
6446 barExpectedDefaultVersion := fmt.Sprint(defaultVersionInt + 3)
6447 barActualDefaultVersion := barManifestRule.Args["default_version"]
6448 if barActualDefaultVersion != barExpectedDefaultVersion {
6449 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6450 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006451
Jooyung Hana0503a52023-08-23 13:12:50 +09006452 overrideBarManifestRule := result.ModuleForTests("bar", "android_common_myoverrideapex_bar").Rule("apexManifestRule")
Sam Delmerico419f9a32023-07-21 12:00:13 -04006453 overrideBarActualDefaultVersion := overrideBarManifestRule.Args["default_version"]
6454 if overrideBarActualDefaultVersion != barExpectedDefaultVersion {
6455 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6456 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006457}
6458
Sam Delmericoca816532023-06-02 14:09:50 -04006459func TestApexAvailable_ApexAvailableName(t *testing.T) {
6460 t.Run("using name of apex that sets apex_available_name is not allowed", func(t *testing.T) {
6461 testApexError(t, "Consider adding \"myapex\" to 'apex_available' property of \"AppFoo\"", `
6462 apex {
6463 name: "myapex_sminus",
6464 key: "myapex.key",
6465 apps: ["AppFoo"],
6466 apex_available_name: "myapex",
6467 updatable: false,
6468 }
6469 apex {
6470 name: "myapex",
6471 key: "myapex.key",
6472 apps: ["AppFoo"],
6473 updatable: false,
6474 }
6475 apex_key {
6476 name: "myapex.key",
6477 public_key: "testkey.avbpubkey",
6478 private_key: "testkey.pem",
6479 }
6480 android_app {
6481 name: "AppFoo",
6482 srcs: ["foo/bar/MyClass.java"],
6483 sdk_version: "none",
6484 system_modules: "none",
6485 apex_available: [ "myapex_sminus" ],
6486 }`,
6487 android.FixtureMergeMockFs(android.MockFS{
6488 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6489 }),
6490 )
6491 })
6492
6493 t.Run("apex_available_name allows module to be used in two different apexes", func(t *testing.T) {
6494 testApex(t, `
6495 apex {
6496 name: "myapex_sminus",
6497 key: "myapex.key",
6498 apps: ["AppFoo"],
6499 apex_available_name: "myapex",
6500 updatable: false,
6501 }
6502 apex {
6503 name: "myapex",
6504 key: "myapex.key",
6505 apps: ["AppFoo"],
6506 updatable: false,
6507 }
6508 apex_key {
6509 name: "myapex.key",
6510 public_key: "testkey.avbpubkey",
6511 private_key: "testkey.pem",
6512 }
6513 android_app {
6514 name: "AppFoo",
6515 srcs: ["foo/bar/MyClass.java"],
6516 sdk_version: "none",
6517 system_modules: "none",
6518 apex_available: [ "myapex" ],
6519 }`,
6520 android.FixtureMergeMockFs(android.MockFS{
6521 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6522 }),
6523 )
6524 })
6525
6526 t.Run("override_apexes work with apex_available_name", func(t *testing.T) {
6527 testApex(t, `
6528 override_apex {
6529 name: "myoverrideapex_sminus",
6530 base: "myapex_sminus",
6531 key: "myapex.key",
6532 apps: ["AppFooOverride"],
6533 }
6534 override_apex {
6535 name: "myoverrideapex",
6536 base: "myapex",
6537 key: "myapex.key",
6538 apps: ["AppFooOverride"],
6539 }
6540 apex {
6541 name: "myapex_sminus",
6542 key: "myapex.key",
6543 apps: ["AppFoo"],
6544 apex_available_name: "myapex",
6545 updatable: false,
6546 }
6547 apex {
6548 name: "myapex",
6549 key: "myapex.key",
6550 apps: ["AppFoo"],
6551 updatable: false,
6552 }
6553 apex_key {
6554 name: "myapex.key",
6555 public_key: "testkey.avbpubkey",
6556 private_key: "testkey.pem",
6557 }
6558 android_app {
6559 name: "AppFooOverride",
6560 srcs: ["foo/bar/MyClass.java"],
6561 sdk_version: "none",
6562 system_modules: "none",
6563 apex_available: [ "myapex" ],
6564 }
6565 android_app {
6566 name: "AppFoo",
6567 srcs: ["foo/bar/MyClass.java"],
6568 sdk_version: "none",
6569 system_modules: "none",
6570 apex_available: [ "myapex" ],
6571 }`,
6572 android.FixtureMergeMockFs(android.MockFS{
6573 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6574 }),
6575 )
6576 })
6577}
6578
6579func TestApexAvailable_ApexAvailableNameWithOverrides(t *testing.T) {
6580 context := android.GroupFixturePreparers(
6581 android.PrepareForIntegrationTestWithAndroid,
6582 PrepareForTestWithApexBuildComponents,
6583 java.PrepareForTestWithDexpreopt,
6584 android.FixtureMergeMockFs(android.MockFS{
6585 "system/sepolicy/apex/myapex-file_contexts": nil,
6586 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6587 }),
6588 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6589 variables.BuildId = proptools.StringPtr("buildid")
6590 }),
6591 )
6592 context.RunTestWithBp(t, `
6593 override_apex {
6594 name: "myoverrideapex_sminus",
6595 base: "myapex_sminus",
6596 }
6597 override_apex {
6598 name: "myoverrideapex",
6599 base: "myapex",
6600 }
6601 apex {
6602 name: "myapex",
6603 key: "myapex.key",
6604 apps: ["AppFoo"],
6605 updatable: false,
6606 }
6607 apex {
6608 name: "myapex_sminus",
6609 apex_available_name: "myapex",
6610 key: "myapex.key",
6611 apps: ["AppFoo_sminus"],
6612 updatable: false,
6613 }
6614 apex_key {
6615 name: "myapex.key",
6616 public_key: "testkey.avbpubkey",
6617 private_key: "testkey.pem",
6618 }
6619 android_app {
6620 name: "AppFoo",
6621 srcs: ["foo/bar/MyClass.java"],
6622 sdk_version: "none",
6623 system_modules: "none",
6624 apex_available: [ "myapex" ],
6625 }
6626 android_app {
6627 name: "AppFoo_sminus",
6628 srcs: ["foo/bar/MyClass.java"],
6629 sdk_version: "none",
6630 min_sdk_version: "29",
6631 system_modules: "none",
6632 apex_available: [ "myapex" ],
6633 }`)
6634}
6635
Jiyong Park89e850a2020-04-07 16:37:39 +09006636func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006637 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006638 apex {
6639 name: "myapex",
6640 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006641 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006642 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006643 }
6644
6645 apex_key {
6646 name: "myapex.key",
6647 public_key: "testkey.avbpubkey",
6648 private_key: "testkey.pem",
6649 }
6650
6651 cc_library {
6652 name: "libfoo",
6653 stl: "none",
6654 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006655 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006656 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006657 }
6658
6659 cc_library {
6660 name: "libfoo2",
6661 stl: "none",
6662 system_shared_libs: [],
6663 shared_libs: ["libbaz"],
6664 apex_available: ["//apex_available:platform"],
6665 }
6666
6667 cc_library {
6668 name: "libbar",
6669 stl: "none",
6670 system_shared_libs: [],
6671 apex_available: ["myapex"],
6672 }
6673
6674 cc_library {
6675 name: "libbaz",
6676 stl: "none",
6677 system_shared_libs: [],
6678 apex_available: ["myapex"],
6679 stubs: {
6680 versions: ["1"],
6681 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006682 }`)
6683
Jiyong Park89e850a2020-04-07 16:37:39 +09006684 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6685 // because it depends on libbar which isn't available to platform
6686 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6687 if libfoo.NotAvailableForPlatform() != true {
6688 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6689 }
6690
6691 // libfoo2 however can be available to platform because it depends on libbaz which provides
6692 // stubs
6693 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6694 if libfoo2.NotAvailableForPlatform() == true {
6695 t.Errorf("%q should be available to platform", libfoo2.String())
6696 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006697}
Jiyong Parka90ca002019-10-07 15:47:24 +09006698
Paul Duffine52e66f2020-03-30 17:54:29 +01006699func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006700 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006701 apex {
6702 name: "myapex",
6703 key: "myapex.key",
6704 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006705 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006706 }
6707
6708 apex_key {
6709 name: "myapex.key",
6710 public_key: "testkey.avbpubkey",
6711 private_key: "testkey.pem",
6712 }
6713
6714 cc_library {
6715 name: "libfoo",
6716 stl: "none",
6717 system_shared_libs: [],
6718 apex_available: ["myapex"],
6719 static: {
6720 apex_available: ["//apex_available:platform"],
6721 },
6722 }`)
6723
Jiyong Park89e850a2020-04-07 16:37:39 +09006724 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6725 if libfooShared.NotAvailableForPlatform() != true {
6726 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6727 }
6728 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6729 if libfooStatic.NotAvailableForPlatform() != false {
6730 t.Errorf("%q should be available to platform", libfooStatic.String())
6731 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006732}
6733
Jiyong Park5d790c32019-11-15 18:40:32 +09006734func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006735 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006736 apex {
6737 name: "myapex",
6738 key: "myapex.key",
6739 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006740 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006741 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006742 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006743 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006744 }
6745
6746 override_apex {
6747 name: "override_myapex",
6748 base: "myapex",
6749 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006750 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006751 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006752 overrides: ["unknownapex"],
Jesse Melhuishec60e252024-03-29 19:08:20 +00006753 compile_multilib: "first",
6754 multilib: {
6755 lib32: {
6756 native_shared_libs: ["mylib32"],
6757 },
6758 lib64: {
6759 native_shared_libs: ["mylib64"],
6760 },
6761 },
Baligh Uddin004d7172020-02-19 21:29:28 -08006762 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006763 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006764 key: "mynewapex.key",
6765 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006766 }
6767
6768 apex_key {
6769 name: "myapex.key",
6770 public_key: "testkey.avbpubkey",
6771 private_key: "testkey.pem",
6772 }
6773
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006774 apex_key {
6775 name: "mynewapex.key",
6776 public_key: "testkey2.avbpubkey",
6777 private_key: "testkey2.pem",
6778 }
6779
6780 android_app_certificate {
6781 name: "myapex.certificate",
6782 certificate: "testkey",
6783 }
6784
Jiyong Park5d790c32019-11-15 18:40:32 +09006785 android_app {
6786 name: "app",
6787 srcs: ["foo/bar/MyClass.java"],
6788 package_name: "foo",
6789 sdk_version: "none",
6790 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006791 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006792 }
6793
6794 override_android_app {
6795 name: "override_app",
6796 base: "app",
6797 package_name: "bar",
6798 }
markchien7c803b82021-08-26 22:10:06 +08006799
6800 bpf {
6801 name: "bpf",
6802 srcs: ["bpf.c"],
6803 }
6804
6805 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006806 name: "overrideBpf",
6807 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006808 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006809
6810 prebuilt_etc {
6811 name: "myetc",
6812 src: "myprebuilt",
6813 }
6814
6815 prebuilt_etc {
6816 name: "override_myetc",
6817 src: "override_myprebuilt",
6818 }
Jesse Melhuishec60e252024-03-29 19:08:20 +00006819
6820 cc_library {
6821 name: "mylib32",
6822 apex_available: [ "myapex" ],
6823 }
6824
6825 cc_library {
6826 name: "mylib64",
6827 apex_available: [ "myapex" ],
6828 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006829 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006830
Jooyung Hana0503a52023-08-23 13:12:50 +09006831 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(android.OverridableModule)
6832 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex").Module().(android.OverridableModule)
Jiyong Park317645e2019-12-05 13:20:58 +09006833 if originalVariant.GetOverriddenBy() != "" {
6834 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6835 }
6836 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6837 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6838 }
6839
Jooyung Hana0503a52023-08-23 13:12:50 +09006840 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006841 apexRule := module.Rule("apexRule")
6842 copyCmds := apexRule.Args["copy_commands"]
6843
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006844 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6845 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006846
markchien7c803b82021-08-26 22:10:06 +08006847 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006848 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006849
Daniel Norman5a3ce132021-08-26 15:44:43 -07006850 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6851 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6852
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006853 apexBundle := module.Module().(*apexBundle)
6854 name := apexBundle.Name()
6855 if name != "override_myapex" {
6856 t.Errorf("name should be \"override_myapex\", but was %q", name)
6857 }
6858
Baligh Uddin004d7172020-02-19 21:29:28 -08006859 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6860 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6861 }
6862
Jiyong Park20bacab2020-03-03 11:45:41 +09006863 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006864 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006865 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6866
6867 signApkRule := module.Rule("signapk")
6868 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006869
Colin Crossaa255532020-07-03 13:18:24 -07006870 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006871 var builder strings.Builder
6872 data.Custom(&builder, name, "TARGET_", "", data)
6873 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006874 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6875 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006876 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006877 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006878 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006879 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006880 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006881 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006882}
6883
Albert Martineefabcf2022-03-21 20:11:16 +00006884func TestMinSdkVersionOverride(t *testing.T) {
6885 // Override from 29 to 31
6886 minSdkOverride31 := "31"
6887 ctx := testApex(t, `
6888 apex {
6889 name: "myapex",
6890 key: "myapex.key",
6891 native_shared_libs: ["mylib"],
6892 updatable: true,
6893 min_sdk_version: "29"
6894 }
6895
6896 override_apex {
6897 name: "override_myapex",
6898 base: "myapex",
6899 logging_parent: "com.foo.bar",
6900 package_name: "test.overridden.package"
6901 }
6902
6903 apex_key {
6904 name: "myapex.key",
6905 public_key: "testkey.avbpubkey",
6906 private_key: "testkey.pem",
6907 }
6908
6909 cc_library {
6910 name: "mylib",
6911 srcs: ["mylib.cpp"],
6912 runtime_libs: ["libbar"],
6913 system_shared_libs: [],
6914 stl: "none",
6915 apex_available: [ "myapex" ],
6916 min_sdk_version: "apex_inherit"
6917 }
6918
6919 cc_library {
6920 name: "libbar",
6921 srcs: ["mylib.cpp"],
6922 system_shared_libs: [],
6923 stl: "none",
6924 apex_available: [ "myapex" ],
6925 min_sdk_version: "apex_inherit"
6926 }
6927
6928 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6929
Jooyung Hana0503a52023-08-23 13:12:50 +09006930 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00006931 copyCmds := apexRule.Args["copy_commands"]
6932
6933 // Ensure that direct non-stubs dep is always included
6934 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6935
6936 // Ensure that runtime_libs dep in included
6937 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6938
6939 // Ensure libraries target overridden min_sdk_version value
6940 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6941}
6942
6943func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6944 // Attempt to override from 31 to 29, should be a NOOP
6945 minSdkOverride29 := "29"
6946 ctx := testApex(t, `
6947 apex {
6948 name: "myapex",
6949 key: "myapex.key",
6950 native_shared_libs: ["mylib"],
6951 updatable: true,
6952 min_sdk_version: "31"
6953 }
6954
6955 override_apex {
6956 name: "override_myapex",
6957 base: "myapex",
6958 logging_parent: "com.foo.bar",
6959 package_name: "test.overridden.package"
6960 }
6961
6962 apex_key {
6963 name: "myapex.key",
6964 public_key: "testkey.avbpubkey",
6965 private_key: "testkey.pem",
6966 }
6967
6968 cc_library {
6969 name: "mylib",
6970 srcs: ["mylib.cpp"],
6971 runtime_libs: ["libbar"],
6972 system_shared_libs: [],
6973 stl: "none",
6974 apex_available: [ "myapex" ],
6975 min_sdk_version: "apex_inherit"
6976 }
6977
6978 cc_library {
6979 name: "libbar",
6980 srcs: ["mylib.cpp"],
6981 system_shared_libs: [],
6982 stl: "none",
6983 apex_available: [ "myapex" ],
6984 min_sdk_version: "apex_inherit"
6985 }
6986
6987 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6988
Jooyung Hana0503a52023-08-23 13:12:50 +09006989 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00006990 copyCmds := apexRule.Args["copy_commands"]
6991
6992 // Ensure that direct non-stubs dep is always included
6993 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6994
6995 // Ensure that runtime_libs dep in included
6996 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6997
6998 // Ensure libraries target the original min_sdk_version value rather than the overridden
6999 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7000}
7001
Jooyung Han214bf372019-11-12 13:03:50 +09007002func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007003 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09007004 apex {
7005 name: "myapex",
7006 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007007 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09007008 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09007009 }
7010
7011 apex_key {
7012 name: "myapex.key",
7013 public_key: "testkey.avbpubkey",
7014 private_key: "testkey.pem",
7015 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007016
7017 cc_library {
7018 name: "mylib",
7019 srcs: ["mylib.cpp"],
7020 stl: "libc++",
7021 system_shared_libs: [],
7022 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09007023 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007024 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007025 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09007026
Jooyung Hana0503a52023-08-23 13:12:50 +09007027 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +09007028 args := module.Rule("apexRule").Args
7029 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00007030 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007031
7032 // The copies of the libraries in the apex should have one more dependency than
7033 // the ones outside the apex, namely the unwinder. Ideally we should check
7034 // the dependency names directly here but for some reason the names are blank in
7035 // this test.
7036 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07007037 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007038 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
7039 if len(apexImplicits) != len(nonApexImplicits)+1 {
7040 t.Errorf("%q missing unwinder dep", lib)
7041 }
7042 }
Jooyung Han214bf372019-11-12 13:03:50 +09007043}
7044
Paul Duffine05480a2021-03-08 15:07:14 +00007045var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01007046 "api/current.txt": nil,
7047 "api/removed.txt": nil,
7048 "api/system-current.txt": nil,
7049 "api/system-removed.txt": nil,
7050 "api/test-current.txt": nil,
7051 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01007052
Anton Hanssondff2c782020-12-21 17:10:01 +00007053 "100/public/api/foo.txt": nil,
7054 "100/public/api/foo-removed.txt": nil,
7055 "100/system/api/foo.txt": nil,
7056 "100/system/api/foo-removed.txt": nil,
7057
Paul Duffineedc5d52020-06-12 17:46:39 +01007058 // For java_sdk_library_import
7059 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01007060}
7061
Jooyung Han58f26ab2019-12-18 15:34:32 +09007062func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007063 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09007064 apex {
7065 name: "myapex",
7066 key: "myapex.key",
7067 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007068 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09007069 }
7070
7071 apex_key {
7072 name: "myapex.key",
7073 public_key: "testkey.avbpubkey",
7074 private_key: "testkey.pem",
7075 }
7076
7077 java_sdk_library {
7078 name: "foo",
7079 srcs: ["a.java"],
7080 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007081 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09007082 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007083
7084 prebuilt_apis {
7085 name: "sdk",
7086 api_dirs: ["100"],
7087 }
Paul Duffin9b879592020-05-26 13:21:35 +01007088 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09007089
7090 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007091 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09007092 "javalib/foo.jar",
7093 "etc/permissions/foo.xml",
7094 })
7095 // Permission XML should point to the activated path of impl jar of java_sdk_library
Paul Duffin1816cde2024-04-10 10:58:21 +01007096 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Output("foo.xml")
7097 contents := android.ContentFromFileRuleForTests(t, ctx, sdkLibrary)
7098 ensureMatches(t, contents, "<library\\n\\s+name=\\\"foo\\\"\\n\\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"")
Jooyung Han58f26ab2019-12-18 15:34:32 +09007099}
7100
Paul Duffin9b879592020-05-26 13:21:35 +01007101func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007102 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007103 apex {
7104 name: "myapex",
7105 key: "myapex.key",
7106 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007107 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007108 }
7109
7110 apex_key {
7111 name: "myapex.key",
7112 public_key: "testkey.avbpubkey",
7113 private_key: "testkey.pem",
7114 }
7115
7116 java_sdk_library {
7117 name: "foo",
7118 srcs: ["a.java"],
7119 api_packages: ["foo"],
7120 apex_available: ["myapex"],
7121 sdk_version: "none",
7122 system_modules: "none",
7123 }
7124
7125 java_library {
7126 name: "bar",
7127 srcs: ["a.java"],
7128 libs: ["foo"],
7129 apex_available: ["myapex"],
7130 sdk_version: "none",
7131 system_modules: "none",
7132 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007133
7134 prebuilt_apis {
7135 name: "sdk",
7136 api_dirs: ["100"],
7137 }
Paul Duffin9b879592020-05-26 13:21:35 +01007138 `, withFiles(filesForSdkLibrary))
7139
7140 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007141 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007142 "javalib/bar.jar",
7143 "javalib/foo.jar",
7144 "etc/permissions/foo.xml",
7145 })
7146
7147 // The bar library should depend on the implementation jar.
7148 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Jihoon Kang8479dea2024-04-04 01:19:05 +00007149 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007150 t.Errorf("expected %q, found %#q", expected, actual)
7151 }
7152}
7153
7154func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007155 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007156 apex {
7157 name: "myapex",
7158 key: "myapex.key",
7159 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007160 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007161 }
7162
7163 apex_key {
7164 name: "myapex.key",
7165 public_key: "testkey.avbpubkey",
7166 private_key: "testkey.pem",
7167 }
7168
7169 java_sdk_library {
7170 name: "foo",
7171 srcs: ["a.java"],
7172 api_packages: ["foo"],
7173 apex_available: ["myapex"],
7174 sdk_version: "none",
7175 system_modules: "none",
7176 }
7177
7178 java_library {
7179 name: "bar",
7180 srcs: ["a.java"],
7181 libs: ["foo"],
7182 sdk_version: "none",
7183 system_modules: "none",
7184 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007185
7186 prebuilt_apis {
7187 name: "sdk",
7188 api_dirs: ["100"],
7189 }
Paul Duffin9b879592020-05-26 13:21:35 +01007190 `, withFiles(filesForSdkLibrary))
7191
7192 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007193 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007194 "javalib/foo.jar",
7195 "etc/permissions/foo.xml",
7196 })
7197
7198 // The bar library should depend on the stubs jar.
7199 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007200 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007201 t.Errorf("expected %q, found %#q", expected, actual)
7202 }
7203}
7204
Paul Duffineedc5d52020-06-12 17:46:39 +01007205func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007206 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00007207 prebuilt_apis {
7208 name: "sdk",
7209 api_dirs: ["100"],
7210 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01007211 withFiles(map[string][]byte{
7212 "apex/a.java": nil,
7213 "apex/apex_manifest.json": nil,
7214 "apex/Android.bp": []byte(`
7215 package {
7216 default_visibility: ["//visibility:private"],
7217 }
7218
7219 apex {
7220 name: "myapex",
7221 key: "myapex.key",
7222 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007223 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007224 }
7225
7226 apex_key {
7227 name: "myapex.key",
7228 public_key: "testkey.avbpubkey",
7229 private_key: "testkey.pem",
7230 }
7231
7232 java_library {
7233 name: "bar",
7234 srcs: ["a.java"],
7235 libs: ["foo"],
7236 apex_available: ["myapex"],
7237 sdk_version: "none",
7238 system_modules: "none",
7239 }
7240`),
7241 "source/a.java": nil,
7242 "source/api/current.txt": nil,
7243 "source/api/removed.txt": nil,
7244 "source/Android.bp": []byte(`
7245 package {
7246 default_visibility: ["//visibility:private"],
7247 }
7248
7249 java_sdk_library {
7250 name: "foo",
7251 visibility: ["//apex"],
7252 srcs: ["a.java"],
7253 api_packages: ["foo"],
7254 apex_available: ["myapex"],
7255 sdk_version: "none",
7256 system_modules: "none",
7257 public: {
7258 enabled: true,
7259 },
7260 }
7261`),
7262 "prebuilt/a.jar": nil,
7263 "prebuilt/Android.bp": []byte(`
7264 package {
7265 default_visibility: ["//visibility:private"],
7266 }
7267
7268 java_sdk_library_import {
7269 name: "foo",
7270 visibility: ["//apex", "//source"],
7271 apex_available: ["myapex"],
7272 prefer: true,
7273 public: {
7274 jars: ["a.jar"],
7275 },
7276 }
7277`),
Anton Hanssondff2c782020-12-21 17:10:01 +00007278 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01007279 )
7280
7281 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007282 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffineedc5d52020-06-12 17:46:39 +01007283 "javalib/bar.jar",
7284 "javalib/foo.jar",
7285 "etc/permissions/foo.xml",
7286 })
7287
7288 // The bar library should depend on the implementation jar.
7289 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007290 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01007291 t.Errorf("expected %q, found %#q", expected, actual)
7292 }
7293}
7294
7295func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
7296 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
7297 apex {
7298 name: "myapex",
7299 key: "myapex.key",
7300 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007301 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007302 }
7303
7304 apex_key {
7305 name: "myapex.key",
7306 public_key: "testkey.avbpubkey",
7307 private_key: "testkey.pem",
7308 }
7309
7310 java_sdk_library_import {
7311 name: "foo",
7312 apex_available: ["myapex"],
7313 prefer: true,
7314 public: {
7315 jars: ["a.jar"],
7316 },
7317 }
7318
7319 `, withFiles(filesForSdkLibrary))
7320}
7321
atrost6e126252020-01-27 17:01:16 +00007322func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007323 result := android.GroupFixturePreparers(
7324 prepareForApexTest,
7325 java.PrepareForTestWithPlatformCompatConfig,
7326 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007327 apex {
7328 name: "myapex",
7329 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007330 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007331 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007332 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007333 }
7334
7335 apex_key {
7336 name: "myapex.key",
7337 public_key: "testkey.avbpubkey",
7338 private_key: "testkey.pem",
7339 }
7340
7341 platform_compat_config {
7342 name: "myjar-platform-compat-config",
7343 src: ":myjar",
7344 }
7345
7346 java_library {
7347 name: "myjar",
7348 srcs: ["foo/bar/MyClass.java"],
7349 sdk_version: "none",
7350 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007351 apex_available: [ "myapex" ],
7352 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007353
7354 // Make sure that a preferred prebuilt does not affect the apex contents.
7355 prebuilt_platform_compat_config {
7356 name: "myjar-platform-compat-config",
7357 metadata: "compat-config/metadata.xml",
7358 prefer: true,
7359 }
atrost6e126252020-01-27 17:01:16 +00007360 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007361 ctx := result.TestContext
Jooyung Hana0503a52023-08-23 13:12:50 +09007362 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
atrost6e126252020-01-27 17:01:16 +00007363 "etc/compatconfig/myjar-platform-compat-config.xml",
7364 "javalib/myjar.jar",
7365 })
7366}
7367
Jooyung Han862c0d62022-12-21 10:15:37 +09007368func TestNoDupeApexFiles(t *testing.T) {
7369 android.GroupFixturePreparers(
7370 android.PrepareForTestWithAndroidBuildComponents,
7371 PrepareForTestWithApexBuildComponents,
7372 prepareForTestWithMyapex,
7373 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7374 ).
7375 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7376 RunTestWithBp(t, `
7377 apex {
7378 name: "myapex",
7379 key: "myapex.key",
7380 prebuilts: ["foo", "bar"],
7381 updatable: false,
7382 }
7383
7384 apex_key {
7385 name: "myapex.key",
7386 public_key: "testkey.avbpubkey",
7387 private_key: "testkey.pem",
7388 }
7389
7390 prebuilt_etc {
7391 name: "foo",
7392 src: "myprebuilt",
7393 filename_from_src: true,
7394 }
7395
7396 prebuilt_etc {
7397 name: "bar",
7398 src: "myprebuilt",
7399 filename_from_src: true,
7400 }
7401 `)
7402}
7403
Jooyung Hana8bd72a2023-11-02 11:56:48 +09007404func TestApexUnwantedTransitiveDeps(t *testing.T) {
7405 bp := `
7406 apex {
7407 name: "myapex",
7408 key: "myapex.key",
7409 native_shared_libs: ["libfoo"],
7410 updatable: false,
7411 unwanted_transitive_deps: ["libbar"],
7412 }
7413
7414 apex_key {
7415 name: "myapex.key",
7416 public_key: "testkey.avbpubkey",
7417 private_key: "testkey.pem",
7418 }
7419
7420 cc_library {
7421 name: "libfoo",
7422 srcs: ["foo.cpp"],
7423 shared_libs: ["libbar"],
7424 apex_available: ["myapex"],
7425 }
7426
7427 cc_library {
7428 name: "libbar",
7429 srcs: ["bar.cpp"],
7430 apex_available: ["myapex"],
7431 }`
7432 ctx := testApex(t, bp)
7433 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
7434 "*/libc++.so",
7435 "*/libfoo.so",
7436 // not libbar.so
7437 })
7438}
7439
Jiyong Park479321d2019-12-16 11:47:12 +09007440func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7441 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7442 apex {
7443 name: "myapex",
7444 key: "myapex.key",
7445 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007446 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007447 }
7448
7449 apex_key {
7450 name: "myapex.key",
7451 public_key: "testkey.avbpubkey",
7452 private_key: "testkey.pem",
7453 }
7454
7455 java_library {
7456 name: "myjar",
7457 srcs: ["foo/bar/MyClass.java"],
7458 sdk_version: "none",
7459 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007460 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007461 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007462 }
7463 `)
7464}
7465
Jiyong Park7afd1072019-12-30 16:56:33 +09007466func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007467 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007468 apex {
7469 name: "myapex",
7470 key: "myapex.key",
7471 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007472 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007473 }
7474
7475 apex_key {
7476 name: "myapex.key",
7477 public_key: "testkey.avbpubkey",
7478 private_key: "testkey.pem",
7479 }
7480
7481 cc_library {
7482 name: "mylib",
7483 srcs: ["mylib.cpp"],
7484 system_shared_libs: [],
7485 stl: "none",
7486 required: ["a", "b"],
7487 host_required: ["c", "d"],
7488 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007489 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007490 }
7491 `)
7492
Jooyung Hana0503a52023-08-23 13:12:50 +09007493 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007494 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007495 name := apexBundle.BaseModuleName()
7496 prefix := "TARGET_"
7497 var builder strings.Builder
7498 data.Custom(&builder, name, prefix, "", data)
7499 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007500 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 a b\n")
Sasha Smundakdcb61292022-12-08 10:41:33 -08007501 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7502 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007503}
7504
Jiyong Park7cd10e32020-01-14 09:22:18 +09007505func TestSymlinksFromApexToSystem(t *testing.T) {
7506 bp := `
7507 apex {
7508 name: "myapex",
7509 key: "myapex.key",
7510 native_shared_libs: ["mylib"],
7511 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007512 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007513 }
7514
Jiyong Park9d677202020-02-19 16:29:35 +09007515 apex {
7516 name: "myapex.updatable",
7517 key: "myapex.key",
7518 native_shared_libs: ["mylib"],
7519 java_libs: ["myjar"],
7520 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007521 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007522 }
7523
Jiyong Park7cd10e32020-01-14 09:22:18 +09007524 apex_key {
7525 name: "myapex.key",
7526 public_key: "testkey.avbpubkey",
7527 private_key: "testkey.pem",
7528 }
7529
7530 cc_library {
7531 name: "mylib",
7532 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007533 shared_libs: [
7534 "myotherlib",
7535 "myotherlib_ext",
7536 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007537 system_shared_libs: [],
7538 stl: "none",
7539 apex_available: [
7540 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007541 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007542 "//apex_available:platform",
7543 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007544 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007545 }
7546
7547 cc_library {
7548 name: "myotherlib",
7549 srcs: ["mylib.cpp"],
7550 system_shared_libs: [],
7551 stl: "none",
7552 apex_available: [
7553 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007554 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007555 "//apex_available:platform",
7556 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007557 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007558 }
7559
Jiyong Parkce243632023-02-17 18:22:25 +09007560 cc_library {
7561 name: "myotherlib_ext",
7562 srcs: ["mylib.cpp"],
7563 system_shared_libs: [],
7564 system_ext_specific: true,
7565 stl: "none",
7566 apex_available: [
7567 "myapex",
7568 "myapex.updatable",
7569 "//apex_available:platform",
7570 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007571 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007572 }
7573
Jiyong Park7cd10e32020-01-14 09:22:18 +09007574 java_library {
7575 name: "myjar",
7576 srcs: ["foo/bar/MyClass.java"],
7577 sdk_version: "none",
7578 system_modules: "none",
7579 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007580 apex_available: [
7581 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007582 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007583 "//apex_available:platform",
7584 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007585 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007586 }
7587
7588 java_library {
7589 name: "myotherjar",
7590 srcs: ["foo/bar/MyClass.java"],
7591 sdk_version: "none",
7592 system_modules: "none",
7593 apex_available: [
7594 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007595 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007596 "//apex_available:platform",
7597 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007598 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007599 }
7600 `
7601
7602 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7603 for _, f := range files {
7604 if f.path == file {
7605 if f.isLink {
7606 t.Errorf("%q is not a real file", file)
7607 }
7608 return
7609 }
7610 }
7611 t.Errorf("%q is not found", file)
7612 }
7613
Jiyong Parkce243632023-02-17 18:22:25 +09007614 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007615 for _, f := range files {
7616 if f.path == file {
7617 if !f.isLink {
7618 t.Errorf("%q is not a symlink", file)
7619 }
Jiyong Parkce243632023-02-17 18:22:25 +09007620 if f.src != target {
7621 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7622 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007623 return
7624 }
7625 }
7626 t.Errorf("%q is not found", file)
7627 }
7628
Jiyong Park9d677202020-02-19 16:29:35 +09007629 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7630 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007631 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana0503a52023-08-23 13:12:50 +09007632 files := getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007633 ensureRealfileExists(t, files, "javalib/myjar.jar")
7634 ensureRealfileExists(t, files, "lib64/mylib.so")
7635 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007636 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007637
Jooyung Hana0503a52023-08-23 13:12:50 +09007638 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007639 ensureRealfileExists(t, files, "javalib/myjar.jar")
7640 ensureRealfileExists(t, files, "lib64/mylib.so")
7641 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007642 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007643
7644 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007645 ctx = testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +09007646 files = getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007647 ensureRealfileExists(t, files, "javalib/myjar.jar")
7648 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007649 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7650 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007651
Jooyung Hana0503a52023-08-23 13:12:50 +09007652 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09007653 ensureRealfileExists(t, files, "javalib/myjar.jar")
7654 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007655 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7656 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007657}
7658
Yo Chiange8128052020-07-23 20:09:18 +08007659func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007660 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007661 apex {
7662 name: "myapex",
7663 key: "myapex.key",
7664 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007665 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007666 }
7667
7668 apex_key {
7669 name: "myapex.key",
7670 public_key: "testkey.avbpubkey",
7671 private_key: "testkey.pem",
7672 }
7673
7674 cc_library_shared {
7675 name: "mylib",
7676 srcs: ["mylib.cpp"],
7677 shared_libs: ["myotherlib"],
7678 system_shared_libs: [],
7679 stl: "none",
7680 apex_available: [
7681 "myapex",
7682 "//apex_available:platform",
7683 ],
7684 }
7685
7686 cc_prebuilt_library_shared {
7687 name: "myotherlib",
7688 srcs: ["prebuilt.so"],
7689 system_shared_libs: [],
7690 stl: "none",
7691 apex_available: [
7692 "myapex",
7693 "//apex_available:platform",
7694 ],
7695 }
7696 `)
7697
Jooyung Hana0503a52023-08-23 13:12:50 +09007698 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007699 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007700 var builder strings.Builder
7701 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7702 androidMk := builder.String()
7703 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007704 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007705 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7706 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7707 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007708 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 myotherlib:64\n")
Yo Chiange8128052020-07-23 20:09:18 +08007709}
7710
Jooyung Han643adc42020-02-27 13:50:06 +09007711func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007712 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007713 apex {
7714 name: "myapex",
7715 key: "myapex.key",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007716 binaries: ["mybin"],
7717 jni_libs: ["mylib", "mylib3", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007718 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007719 }
7720
7721 apex_key {
7722 name: "myapex.key",
7723 public_key: "testkey.avbpubkey",
7724 private_key: "testkey.pem",
7725 }
7726
7727 cc_library {
7728 name: "mylib",
7729 srcs: ["mylib.cpp"],
7730 shared_libs: ["mylib2"],
7731 system_shared_libs: [],
7732 stl: "none",
7733 apex_available: [ "myapex" ],
7734 }
7735
7736 cc_library {
7737 name: "mylib2",
7738 srcs: ["mylib.cpp"],
7739 system_shared_libs: [],
7740 stl: "none",
7741 apex_available: [ "myapex" ],
7742 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007743
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007744 // Used as both a JNI library and a regular shared library.
7745 cc_library {
7746 name: "mylib3",
7747 srcs: ["mylib.cpp"],
7748 system_shared_libs: [],
7749 stl: "none",
7750 apex_available: [ "myapex" ],
7751 }
7752
7753 cc_binary {
7754 name: "mybin",
7755 srcs: ["mybin.cpp"],
7756 shared_libs: ["mylib3"],
7757 system_shared_libs: [],
7758 stl: "none",
7759 apex_available: [ "myapex" ],
7760 }
7761
Jiyong Park34d5c332022-02-24 18:02:44 +09007762 rust_ffi_shared {
7763 name: "libfoo.rust",
7764 crate_name: "foo",
7765 srcs: ["foo.rs"],
7766 shared_libs: ["libfoo.shared_from_rust"],
7767 prefer_rlib: true,
7768 apex_available: ["myapex"],
7769 }
7770
7771 cc_library_shared {
7772 name: "libfoo.shared_from_rust",
7773 srcs: ["mylib.cpp"],
7774 system_shared_libs: [],
7775 stl: "none",
7776 stubs: {
7777 versions: ["10", "11", "12"],
7778 },
7779 }
7780
Jooyung Han643adc42020-02-27 13:50:06 +09007781 `)
7782
Jooyung Hana0503a52023-08-23 13:12:50 +09007783 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han643adc42020-02-27 13:50:06 +09007784 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007785 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so mylib3.so")
Jooyung Hana0503a52023-08-23 13:12:50 +09007786 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007787 "bin/mybin",
Jooyung Han643adc42020-02-27 13:50:06 +09007788 "lib64/mylib.so",
7789 "lib64/mylib2.so",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01007790 "lib64/mylib3.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007791 "lib64/libfoo.rust.so",
7792 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7793 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007794 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007795
7796 // b/220397949
7797 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007798}
7799
Jooyung Han49f67012020-04-17 13:43:10 +09007800func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007801 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007802 apex {
7803 name: "myapex",
7804 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007805 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007806 }
7807 apex_key {
7808 name: "myapex.key",
7809 public_key: "testkey.avbpubkey",
7810 private_key: "testkey.pem",
7811 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007812 `,
7813 android.FixtureModifyConfig(func(config android.Config) {
7814 delete(config.Targets, android.Android)
7815 config.AndroidCommonTarget = android.Target{}
7816 }),
7817 )
Jooyung Han49f67012020-04-17 13:43:10 +09007818
7819 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7820 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7821 }
7822}
7823
Jiyong Parkbd159612020-02-28 15:22:21 +09007824func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007825 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007826 apex {
7827 name: "myapex",
7828 key: "myapex.key",
7829 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007830 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007831 }
7832
7833 apex_key {
7834 name: "myapex.key",
7835 public_key: "testkey.avbpubkey",
7836 private_key: "testkey.pem",
7837 }
7838
7839 android_app {
7840 name: "AppFoo",
7841 srcs: ["foo/bar/MyClass.java"],
7842 sdk_version: "none",
7843 system_modules: "none",
7844 apex_available: [ "myapex" ],
7845 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007846 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007847
Jooyung Hana0503a52023-08-23 13:12:50 +09007848 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07007849 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Jiyong Parkbd159612020-02-28 15:22:21 +09007850
7851 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007852 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 +09007853}
7854
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007855func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007856 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007857 apex {
7858 name: "myapex",
7859 key: "myapex.key",
7860 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007861 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007862 }
7863
7864 apex_key {
7865 name: "myapex.key",
7866 public_key: "testkey.avbpubkey",
7867 private_key: "testkey.pem",
7868 }
7869
7870 android_app_set {
7871 name: "AppSet",
7872 set: "AppSet.apks",
7873 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09007874 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crosscf371cc2020-11-13 11:48:42 -08007875 bundleConfigRule := mod.Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07007876 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007877 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7878 s := mod.Rule("apexRule").Args["copy_commands"]
7879 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007880 if len(copyCmds) != 4 {
7881 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007882 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007883 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7884 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007885 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7886 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007887
7888 // Ensure that canned_fs_config has an entry for the app set zip file
7889 generateFsRule := mod.Rule("generateFsConfig")
7890 cmd := generateFsRule.RuleParams.Command
7891 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007892}
7893
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007894func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007895 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007896 apex_set {
7897 name: "myapex",
7898 filename: "foo_v2.apex",
7899 sanitized: {
7900 none: { set: "myapex.apks", },
7901 hwaddress: { set: "myapex.hwasan.apks", },
7902 },
Paul Duffin24704672021-04-06 16:09:30 +01007903 }
7904 `
7905 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007906
Paul Duffin24704672021-04-06 16:09:30 +01007907 // Check that the extractor produces the correct output file from the correct input file.
Spandan Das3576e762024-01-03 18:57:03 +00007908 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007909
Spandan Das3576e762024-01-03 18:57:03 +00007910 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Paul Duffin24704672021-04-06 16:09:30 +01007911 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007912
Paul Duffin24704672021-04-06 16:09:30 +01007913 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7914
7915 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007916 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7917 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007918
7919 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007920}
7921
Pranav Guptaeba03b02022-09-27 00:27:08 +00007922func TestApexSetApksModuleAssignment(t *testing.T) {
7923 ctx := testApex(t, `
7924 apex_set {
7925 name: "myapex",
7926 set: ":myapex_apks_file",
7927 }
7928
7929 filegroup {
7930 name: "myapex_apks_file",
7931 srcs: ["myapex.apks"],
7932 }
7933 `)
7934
Spandan Das3576e762024-01-03 18:57:03 +00007935 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Pranav Guptaeba03b02022-09-27 00:27:08 +00007936
7937 // Check that the extractor produces the correct apks file from the input module
Spandan Das3576e762024-01-03 18:57:03 +00007938 extractorOutput := "out/soong/.intermediates/prebuilt_myapex.apex.extractor/android_common/extracted/myapex.apks"
Pranav Guptaeba03b02022-09-27 00:27:08 +00007939 extractedApex := m.Output(extractorOutput)
7940
7941 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7942}
7943
Paul Duffin89f570a2021-06-16 01:42:33 +01007944func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007945 t.Helper()
7946
Paul Duffin55607122021-03-30 23:32:51 +01007947 fs := android.MockFS{
7948 "a.java": nil,
7949 "a.jar": nil,
7950 "apex_manifest.json": nil,
7951 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007952 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007953 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7954 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7955 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007956 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007957 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007958
Paul Duffin55607122021-03-30 23:32:51 +01007959 errorHandler := android.FixtureExpectsNoErrors
7960 if errmsg != "" {
7961 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007962 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007963
Paul Duffin55607122021-03-30 23:32:51 +01007964 result := android.GroupFixturePreparers(
7965 cc.PrepareForTestWithCcDefaultModules,
7966 java.PrepareForTestWithHiddenApiBuildComponents,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01007967 java.PrepareForTestWithDexpreopt,
Paul Duffin55607122021-03-30 23:32:51 +01007968 java.PrepareForTestWithJavaSdkLibraryFiles,
7969 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007970 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007971 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007972 android.FixtureModifyMockFS(func(fs android.MockFS) {
7973 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7974 insert := ""
7975 for _, fragment := range fragments {
7976 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7977 }
7978 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7979 platform_bootclasspath {
7980 name: "platform-bootclasspath",
7981 fragments: [
Jiakai Zhangb69e8952023-07-11 14:31:22 +01007982 {apex: "com.android.art", module: "art-bootclasspath-fragment"},
Paul Duffin89f570a2021-06-16 01:42:33 +01007983 %s
7984 ],
7985 }
7986 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007987 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007988 }),
Jiakai Zhangb69e8952023-07-11 14:31:22 +01007989 // Dexpreopt for boot jars requires the ART boot image profile.
7990 java.PrepareApexBootJarModule("com.android.art", "core-oj"),
7991 dexpreopt.FixtureSetArtBootJars("com.android.art:core-oj"),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007992 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007993 ).
7994 ExtendWithErrorHandler(errorHandler).
7995 RunTestWithBp(t, bp)
7996
7997 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007998}
7999
Paul Duffin5556c5f2022-06-09 17:32:21 +00008000func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008001 preparers := android.GroupFixturePreparers(
8002 java.PrepareForTestWithJavaDefaultModules,
Spandan Das5be63332023-12-13 00:06:32 +00008003 prepareForTestWithBootclasspathFragment,
8004 dexpreopt.FixtureSetTestOnlyArtBootImageJars("com.android.art:libfoo"),
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008005 PrepareForTestWithApexBuildComponents,
8006 ).
8007 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
Spandan Das3576e762024-01-03 18:57:03 +00008008 "Multiple installable prebuilt APEXes provide ambiguous deapexers: prebuilt_com.android.art and prebuilt_com.mycompany.android.art"))
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008009
8010 bpBase := `
8011 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008012 name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008013 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008014 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008015 set: "myapex.apks",
8016 }
8017
8018 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008019 name: "com.mycompany.android.art",
8020 apex_name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008021 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008022 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008023 set: "company-myapex.apks",
8024 }
8025
8026 prebuilt_bootclasspath_fragment {
Spandan Das5be63332023-12-13 00:06:32 +00008027 name: "art-bootclasspath-fragment",
8028 apex_available: ["com.android.art"],
Spandan Dasfae468e2023-12-12 23:23:53 +00008029 hidden_api: {
8030 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8031 metadata: "my-bootclasspath-fragment/metadata.csv",
8032 index: "my-bootclasspath-fragment/index.csv",
8033 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
8034 all_flags: "my-bootclasspath-fragment/all-flags.csv",
8035 },
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008036 %s
8037 }
8038 `
8039
8040 t.Run("java_import", func(t *testing.T) {
8041 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8042 java_import {
8043 name: "libfoo",
8044 jars: ["libfoo.jar"],
Spandan Das5be63332023-12-13 00:06:32 +00008045 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008046 }
8047 `)
8048 })
8049
8050 t.Run("java_sdk_library_import", func(t *testing.T) {
8051 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8052 java_sdk_library_import {
8053 name: "libfoo",
8054 public: {
8055 jars: ["libbar.jar"],
8056 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008057 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008058 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008059 }
8060 `)
8061 })
8062
8063 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8064 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8065 image_name: "art",
8066 contents: ["libfoo"],
8067 `)+`
8068 java_sdk_library_import {
8069 name: "libfoo",
8070 public: {
8071 jars: ["libbar.jar"],
8072 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008073 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008074 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008075 }
8076 `)
8077 })
8078}
8079
Paul Duffin5556c5f2022-06-09 17:32:21 +00008080func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
8081 preparers := android.GroupFixturePreparers(
8082 java.PrepareForTestWithJavaDefaultModules,
8083 PrepareForTestWithApexBuildComponents,
8084 )
8085
Spandan Das59a4a2b2024-01-09 21:35:56 +00008086 errCtx := moduleErrorfTestCtx{}
8087
Paul Duffin5556c5f2022-06-09 17:32:21 +00008088 bpBase := `
8089 apex_set {
8090 name: "com.android.myapex",
8091 installable: true,
8092 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8093 set: "myapex.apks",
8094 }
8095
8096 apex_set {
8097 name: "com.android.myapex_compressed",
8098 apex_name: "com.android.myapex",
8099 installable: true,
8100 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8101 set: "myapex_compressed.apks",
8102 }
8103
8104 prebuilt_bootclasspath_fragment {
8105 name: "my-bootclasspath-fragment",
8106 apex_available: [
8107 "com.android.myapex",
8108 "com.android.myapex_compressed",
8109 ],
8110 hidden_api: {
8111 annotation_flags: "annotation-flags.csv",
8112 metadata: "metadata.csv",
8113 index: "index.csv",
8114 signature_patterns: "signature_patterns.csv",
8115 },
8116 %s
8117 }
8118 `
8119
8120 t.Run("java_import", func(t *testing.T) {
8121 result := preparers.RunTestWithBp(t,
8122 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8123 java_import {
8124 name: "libfoo",
8125 jars: ["libfoo.jar"],
8126 apex_available: [
8127 "com.android.myapex",
8128 "com.android.myapex_compressed",
8129 ],
8130 }
8131 `)
8132
8133 module := result.Module("libfoo", "android_common_com.android.myapex")
8134 usesLibraryDep := module.(java.UsesLibraryDependency)
8135 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008136 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008137 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008138 })
8139
8140 t.Run("java_sdk_library_import", func(t *testing.T) {
8141 result := preparers.RunTestWithBp(t,
8142 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8143 java_sdk_library_import {
8144 name: "libfoo",
8145 public: {
8146 jars: ["libbar.jar"],
8147 },
8148 apex_available: [
8149 "com.android.myapex",
8150 "com.android.myapex_compressed",
8151 ],
8152 compile_dex: true,
8153 }
8154 `)
8155
8156 module := result.Module("libfoo", "android_common_com.android.myapex")
8157 usesLibraryDep := module.(java.UsesLibraryDependency)
8158 android.AssertPathRelativeToTopEquals(t, "dex jar path",
Spandan Das3576e762024-01-03 18:57:03 +00008159 "out/soong/.intermediates/prebuilt_com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
Spandan Das59a4a2b2024-01-09 21:35:56 +00008160 usesLibraryDep.DexJarBuildPath(errCtx).Path())
Paul Duffin5556c5f2022-06-09 17:32:21 +00008161 })
8162
8163 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8164 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8165 image_name: "art",
8166 contents: ["libfoo"],
8167 `)+`
8168 java_sdk_library_import {
8169 name: "libfoo",
8170 public: {
8171 jars: ["libbar.jar"],
8172 },
8173 apex_available: [
8174 "com.android.myapex",
8175 "com.android.myapex_compressed",
8176 ],
8177 compile_dex: true,
8178 }
8179 `)
8180 })
8181}
8182
Jooyung Han548640b2020-04-27 12:10:30 +09008183func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
8184 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8185 apex {
8186 name: "myapex",
8187 key: "myapex.key",
8188 updatable: true,
8189 }
8190
8191 apex_key {
8192 name: "myapex.key",
8193 public_key: "testkey.avbpubkey",
8194 private_key: "testkey.pem",
8195 }
8196 `)
8197}
8198
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008199func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
8200 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8201 apex {
8202 name: "myapex",
8203 key: "myapex.key",
8204 }
8205
8206 apex_key {
8207 name: "myapex.key",
8208 public_key: "testkey.avbpubkey",
8209 private_key: "testkey.pem",
8210 }
8211 `)
8212}
8213
Jooyung Handfc864c2023-03-20 18:19:07 +09008214func Test_use_vndk_as_stable_shouldnt_be_used_for_updatable_vendor_apexes(t *testing.T) {
8215 testApexError(t, `"myapex" .*: use_vndk_as_stable: updatable APEXes can't use external VNDK libs`, `
Daniel Norman69109112021-12-02 12:52:42 -08008216 apex {
8217 name: "myapex",
8218 key: "myapex.key",
8219 updatable: true,
Jooyung Handfc864c2023-03-20 18:19:07 +09008220 use_vndk_as_stable: true,
Daniel Norman69109112021-12-02 12:52:42 -08008221 soc_specific: true,
8222 }
8223
8224 apex_key {
8225 name: "myapex.key",
8226 public_key: "testkey.avbpubkey",
8227 private_key: "testkey.pem",
8228 }
8229 `)
8230}
8231
Jooyung Han02873da2023-03-22 17:41:03 +09008232func Test_use_vndk_as_stable_shouldnt_be_used_with_min_sdk_version(t *testing.T) {
8233 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported when min_sdk_version is set`, `
8234 apex {
8235 name: "myapex",
8236 key: "myapex.key",
8237 updatable: false,
8238 min_sdk_version: "29",
8239 use_vndk_as_stable: true,
8240 vendor: true,
8241 }
8242
8243 apex_key {
8244 name: "myapex.key",
8245 public_key: "testkey.avbpubkey",
8246 private_key: "testkey.pem",
8247 }
8248 `)
8249}
8250
Jooyung Handfc864c2023-03-20 18:19:07 +09008251func Test_use_vndk_as_stable_shouldnt_be_used_for_non_vendor_apexes(t *testing.T) {
8252 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported for system/system_ext APEXes`, `
8253 apex {
8254 name: "myapex",
8255 key: "myapex.key",
8256 updatable: false,
8257 use_vndk_as_stable: true,
8258 }
8259
8260 apex_key {
8261 name: "myapex.key",
8262 public_key: "testkey.avbpubkey",
8263 private_key: "testkey.pem",
8264 }
8265 `)
8266}
8267
satayevb98371c2021-06-15 16:49:50 +01008268func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
8269 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
8270 apex {
8271 name: "myapex",
8272 key: "myapex.key",
8273 systemserverclasspath_fragments: [
8274 "mysystemserverclasspathfragment",
8275 ],
8276 min_sdk_version: "29",
8277 updatable: true,
8278 }
8279
8280 apex_key {
8281 name: "myapex.key",
8282 public_key: "testkey.avbpubkey",
8283 private_key: "testkey.pem",
8284 }
8285
8286 java_library {
8287 name: "foo",
8288 srcs: ["b.java"],
8289 min_sdk_version: "29",
8290 installable: true,
8291 apex_available: [
8292 "myapex",
8293 ],
8294 }
8295
8296 systemserverclasspath_fragment {
8297 name: "mysystemserverclasspathfragment",
8298 generate_classpaths_proto: false,
8299 contents: [
8300 "foo",
8301 ],
8302 apex_available: [
8303 "myapex",
8304 ],
8305 }
satayevabcd5972021-08-06 17:49:46 +01008306 `,
8307 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8308 )
satayevb98371c2021-06-15 16:49:50 +01008309}
8310
Paul Duffin064b70c2020-11-02 17:32:38 +00008311func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008312 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008313 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008314 fragment := java.ApexVariantReference{
8315 Apex: proptools.StringPtr("myapex"),
8316 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8317 }
8318
Paul Duffin064b70c2020-11-02 17:32:38 +00008319 testDexpreoptWithApexes(t, `
8320 prebuilt_apex {
8321 name: "myapex" ,
8322 arch: {
8323 arm64: {
8324 src: "myapex-arm64.apex",
8325 },
8326 arm: {
8327 src: "myapex-arm.apex",
8328 },
8329 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008330 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8331 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008332
Paul Duffin89f570a2021-06-16 01:42:33 +01008333 prebuilt_bootclasspath_fragment {
8334 name: "my-bootclasspath-fragment",
8335 contents: ["libfoo"],
8336 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008337 hidden_api: {
8338 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8339 metadata: "my-bootclasspath-fragment/metadata.csv",
8340 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008341 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8342 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8343 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008344 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008345 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008346
Paul Duffin89f570a2021-06-16 01:42:33 +01008347 java_import {
8348 name: "libfoo",
8349 jars: ["libfoo.jar"],
8350 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008351 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008352 }
8353 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008354 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008355}
8356
Spandan Dasf14e2542021-11-12 00:01:37 +00008357func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008358 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008359 bp += `
8360 apex_key {
8361 name: "myapex.key",
8362 public_key: "testkey.avbpubkey",
8363 private_key: "testkey.pem",
8364 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008365 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008366 "lib1/src/A.java": nil,
8367 "lib2/src/B.java": nil,
8368 "system/sepolicy/apex/myapex-file_contexts": nil,
8369 }
8370
Paul Duffin45338f02021-03-30 23:07:52 +01008371 errorHandler := android.FixtureExpectsNoErrors
8372 if errmsg != "" {
8373 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008374 }
Colin Crossae8600b2020-10-29 17:09:13 -07008375
Paul Duffin45338f02021-03-30 23:07:52 +01008376 android.GroupFixturePreparers(
8377 android.PrepareForTestWithAndroidBuildComponents,
8378 java.PrepareForTestWithJavaBuildComponents,
8379 PrepareForTestWithApexBuildComponents,
8380 android.PrepareForTestWithNeverallowRules(rules),
8381 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008382 apexBootJars := make([]string, 0, len(bootJars))
8383 for _, apexBootJar := range bootJars {
8384 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008385 }
satayevd604b212021-07-21 14:23:52 +01008386 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008387 }),
8388 fs.AddToFixture(),
8389 ).
8390 ExtendWithErrorHandler(errorHandler).
8391 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008392}
8393
8394func TestApexPermittedPackagesRules(t *testing.T) {
8395 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008396 name string
8397 expectedError string
8398 bp string
8399 bootJars []string
8400 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008401 }{
8402
8403 {
8404 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8405 expectedError: "",
8406 bp: `
8407 java_library {
8408 name: "bcp_lib1",
8409 srcs: ["lib1/src/*.java"],
8410 permitted_packages: ["foo.bar"],
8411 apex_available: ["myapex"],
8412 sdk_version: "none",
8413 system_modules: "none",
8414 }
8415 java_library {
8416 name: "nonbcp_lib2",
8417 srcs: ["lib2/src/*.java"],
8418 apex_available: ["myapex"],
8419 permitted_packages: ["a.b"],
8420 sdk_version: "none",
8421 system_modules: "none",
8422 }
8423 apex {
8424 name: "myapex",
8425 key: "myapex.key",
8426 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008427 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008428 }`,
8429 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008430 bcpPermittedPackages: map[string][]string{
8431 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008432 "foo.bar",
8433 },
8434 },
8435 },
8436 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008437 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008438 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 +01008439 bp: `
8440 java_library {
8441 name: "bcp_lib1",
8442 srcs: ["lib1/src/*.java"],
8443 apex_available: ["myapex"],
8444 permitted_packages: ["foo.bar"],
8445 sdk_version: "none",
8446 system_modules: "none",
8447 }
8448 java_library {
8449 name: "bcp_lib2",
8450 srcs: ["lib2/src/*.java"],
8451 apex_available: ["myapex"],
8452 permitted_packages: ["foo.bar", "bar.baz"],
8453 sdk_version: "none",
8454 system_modules: "none",
8455 }
8456 apex {
8457 name: "myapex",
8458 key: "myapex.key",
8459 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008460 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008461 }
8462 `,
8463 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008464 bcpPermittedPackages: map[string][]string{
8465 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008466 "foo.bar",
8467 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008468 "bcp_lib2": []string{
8469 "foo.bar",
8470 },
8471 },
8472 },
8473 {
8474 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8475 expectedError: "",
8476 bp: `
8477 java_library {
8478 name: "bcp_lib_restricted",
8479 srcs: ["lib1/src/*.java"],
8480 apex_available: ["myapex"],
8481 permitted_packages: ["foo.bar"],
8482 sdk_version: "none",
8483 min_sdk_version: "29",
8484 system_modules: "none",
8485 }
8486 java_library {
8487 name: "bcp_lib_unrestricted",
8488 srcs: ["lib2/src/*.java"],
8489 apex_available: ["myapex"],
8490 permitted_packages: ["foo.bar", "bar.baz"],
8491 sdk_version: "none",
8492 min_sdk_version: "29",
8493 system_modules: "none",
8494 }
8495 apex {
8496 name: "myapex",
8497 key: "myapex.key",
8498 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8499 updatable: true,
8500 min_sdk_version: "29",
8501 }
8502 `,
8503 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8504 bcpPermittedPackages: map[string][]string{
8505 "bcp_lib1_non_updateable": []string{
8506 "foo.bar",
8507 },
8508 // 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 +01008509 },
8510 },
8511 }
8512 for _, tc := range testcases {
8513 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008514 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8515 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008516 })
8517 }
8518}
8519
Jiyong Park62304bb2020-04-13 16:19:48 +09008520func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008521 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008522 apex {
8523 name: "myapex",
8524 key: "myapex.key",
8525 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008526 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008527 }
8528
8529 apex_key {
8530 name: "myapex.key",
8531 public_key: "testkey.avbpubkey",
8532 private_key: "testkey.pem",
8533 }
8534
8535 cc_library {
8536 name: "mylib",
8537 srcs: ["mylib.cpp"],
8538 system_shared_libs: [],
8539 stl: "none",
8540 stubs: {
8541 versions: ["1"],
8542 },
8543 apex_available: ["myapex"],
8544 }
8545
8546 cc_library {
8547 name: "myprivlib",
8548 srcs: ["mylib.cpp"],
8549 system_shared_libs: [],
8550 stl: "none",
8551 apex_available: ["myapex"],
8552 }
8553
8554
8555 cc_test {
8556 name: "mytest",
8557 gtest: false,
8558 srcs: ["mylib.cpp"],
8559 system_shared_libs: [],
8560 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008561 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008562 test_for: ["myapex"]
8563 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008564
8565 cc_library {
8566 name: "mytestlib",
8567 srcs: ["mylib.cpp"],
8568 system_shared_libs: [],
8569 shared_libs: ["mylib", "myprivlib"],
8570 stl: "none",
8571 test_for: ["myapex"],
8572 }
8573
8574 cc_benchmark {
8575 name: "mybench",
8576 srcs: ["mylib.cpp"],
8577 system_shared_libs: [],
8578 shared_libs: ["mylib", "myprivlib"],
8579 stl: "none",
8580 test_for: ["myapex"],
8581 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008582 `)
8583
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008584 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008585 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008586 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8587 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8588 }
8589
8590 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008591 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008592 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8593 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8594 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8595}
Jiyong Park46a512f2020-12-04 18:02:13 +09008596
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008597func TestIndirectTestFor(t *testing.T) {
8598 ctx := testApex(t, `
8599 apex {
8600 name: "myapex",
8601 key: "myapex.key",
8602 native_shared_libs: ["mylib", "myprivlib"],
8603 updatable: false,
8604 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008605
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008606 apex_key {
8607 name: "myapex.key",
8608 public_key: "testkey.avbpubkey",
8609 private_key: "testkey.pem",
8610 }
8611
8612 cc_library {
8613 name: "mylib",
8614 srcs: ["mylib.cpp"],
8615 system_shared_libs: [],
8616 stl: "none",
8617 stubs: {
8618 versions: ["1"],
8619 },
8620 apex_available: ["myapex"],
8621 }
8622
8623 cc_library {
8624 name: "myprivlib",
8625 srcs: ["mylib.cpp"],
8626 system_shared_libs: [],
8627 stl: "none",
8628 shared_libs: ["mylib"],
8629 apex_available: ["myapex"],
8630 }
8631
8632 cc_library {
8633 name: "mytestlib",
8634 srcs: ["mylib.cpp"],
8635 system_shared_libs: [],
8636 shared_libs: ["myprivlib"],
8637 stl: "none",
8638 test_for: ["myapex"],
8639 }
8640 `)
8641
8642 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008643 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008644 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8645 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8646 }
8647
8648 // The platform variant of mytestlib links to the platform variant of the
8649 // internal myprivlib.
8650 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8651
8652 // The platform variant of myprivlib links to the platform variant of mylib
8653 // and bypasses its stubs.
8654 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 +09008655}
8656
Martin Stjernholmec009002021-03-27 15:18:31 +00008657func TestTestForForLibInOtherApex(t *testing.T) {
8658 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8659 _ = testApex(t, `
8660 apex {
8661 name: "com.android.art",
8662 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008663 native_shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008664 updatable: false,
8665 }
8666
8667 apex {
8668 name: "com.android.art.debug",
8669 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00008670 native_shared_libs: ["libnativebridge", "libnativebrdige_test"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008671 updatable: false,
8672 }
8673
8674 apex_key {
8675 name: "myapex.key",
8676 public_key: "testkey.avbpubkey",
8677 private_key: "testkey.pem",
8678 }
8679
8680 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008681 name: "libnativebridge",
8682 srcs: ["libnativebridge.cpp"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008683 system_shared_libs: [],
8684 stl: "none",
8685 stubs: {
8686 versions: ["1"],
8687 },
8688 apex_available: ["com.android.art", "com.android.art.debug"],
8689 }
8690
8691 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00008692 name: "libnativebrdige_test",
Martin Stjernholmec009002021-03-27 15:18:31 +00008693 srcs: ["mylib.cpp"],
8694 system_shared_libs: [],
Spandan Das20fce2d2023-04-12 17:21:39 +00008695 shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00008696 stl: "none",
8697 apex_available: ["com.android.art.debug"],
8698 test_for: ["com.android.art"],
8699 }
8700 `,
8701 android.MockFS{
8702 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8703 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8704 }.AddToFixture())
8705}
8706
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008707// TODO(jungjw): Move this to proptools
8708func intPtr(i int) *int {
8709 return &i
8710}
8711
8712func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008713 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008714 apex_set {
8715 name: "myapex",
8716 set: "myapex.apks",
8717 filename: "foo_v2.apex",
8718 overrides: ["foo"],
8719 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008720 `,
8721 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8722 variables.Platform_sdk_version = intPtr(30)
8723 }),
8724 android.FixtureModifyConfig(func(config android.Config) {
8725 config.Targets[android.Android] = []android.Target{
8726 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8727 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8728 }
8729 }),
8730 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008731
Spandan Das3576e762024-01-03 18:57:03 +00008732 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008733
8734 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008735 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008736 actual := extractedApex.Args["abis"]
8737 expected := "ARMEABI_V7A,ARM64_V8A"
8738 if actual != expected {
8739 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8740 }
8741 actual = extractedApex.Args["sdk-version"]
8742 expected = "30"
8743 if actual != expected {
8744 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8745 }
8746
Paul Duffin6717d882021-06-15 19:09:41 +01008747 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008748 a := m.Module().(*ApexSet)
8749 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008750 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008751 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8752 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8753 }
8754}
8755
Anton Hansson805e0a52022-11-25 14:06:46 +00008756func TestApexSet_NativeBridge(t *testing.T) {
8757 ctx := testApex(t, `
8758 apex_set {
8759 name: "myapex",
8760 set: "myapex.apks",
8761 filename: "foo_v2.apex",
8762 overrides: ["foo"],
8763 }
8764 `,
8765 android.FixtureModifyConfig(func(config android.Config) {
8766 config.Targets[android.Android] = []android.Target{
8767 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8768 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8769 }
8770 }),
8771 )
8772
Spandan Das3576e762024-01-03 18:57:03 +00008773 m := ctx.ModuleForTests("prebuilt_myapex.apex.extractor", "android_common")
Anton Hansson805e0a52022-11-25 14:06:46 +00008774
8775 // Check extract_apks tool parameters. No native bridge arch expected
8776 extractedApex := m.Output("extracted/myapex.apks")
8777 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8778}
8779
Jiyong Park7d95a512020-05-10 15:16:24 +09008780func TestNoStaticLinkingToStubsLib(t *testing.T) {
8781 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8782 apex {
8783 name: "myapex",
8784 key: "myapex.key",
8785 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008786 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008787 }
8788
8789 apex_key {
8790 name: "myapex.key",
8791 public_key: "testkey.avbpubkey",
8792 private_key: "testkey.pem",
8793 }
8794
8795 cc_library {
8796 name: "mylib",
8797 srcs: ["mylib.cpp"],
8798 static_libs: ["otherlib"],
8799 system_shared_libs: [],
8800 stl: "none",
8801 apex_available: [ "myapex" ],
8802 }
8803
8804 cc_library {
8805 name: "otherlib",
8806 srcs: ["mylib.cpp"],
8807 system_shared_libs: [],
8808 stl: "none",
8809 stubs: {
8810 versions: ["1", "2", "3"],
8811 },
8812 apex_available: [ "myapex" ],
8813 }
8814 `)
8815}
8816
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008817func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008818 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008819 apex {
8820 name: "myapex",
8821 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008822 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008823 custom_sign_tool: "sign_myapex",
8824 }
8825
8826 apex_key {
8827 name: "myapex.key",
8828 public_key: "testkey.avbpubkey",
8829 private_key: "testkey.pem",
8830 }
8831 `)
8832
Jooyung Han286957d2023-10-30 16:17:56 +09008833 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07008834 content := android.ContentFromFileRuleForTests(t, ctx, myapex.Output("apexkeys.txt"))
Jooyung Haneec1b3f2023-06-20 16:25:59 +09008835 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 +09008836}
8837
8838func TestApexKeysTxtOverrides(t *testing.T) {
8839 ctx := testApex(t, `
8840 apex {
8841 name: "myapex",
8842 key: "myapex.key",
8843 updatable: false,
8844 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008845 }
8846
8847 apex_key {
8848 name: "myapex.key",
8849 public_key: "testkey.avbpubkey",
8850 private_key: "testkey.pem",
8851 }
8852
8853 prebuilt_apex {
8854 name: "myapex",
8855 prefer: true,
8856 arch: {
8857 arm64: {
8858 src: "myapex-arm64.apex",
8859 },
8860 arm: {
8861 src: "myapex-arm.apex",
8862 },
8863 },
8864 }
8865
8866 apex_set {
8867 name: "myapex_set",
8868 set: "myapex.apks",
8869 filename: "myapex_set.apex",
8870 overrides: ["myapex"],
8871 }
8872 `)
8873
Colin Crossf61d03d2023-11-02 16:56:39 -07008874 content := android.ContentFromFileRuleForTests(t, ctx,
8875 ctx.ModuleForTests("myapex", "android_common_myapex").Output("apexkeys.txt"))
Jooyung Han286957d2023-10-30 16:17:56 +09008876 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 -07008877 content = android.ContentFromFileRuleForTests(t, ctx,
8878 ctx.ModuleForTests("myapex_set", "android_common_myapex_set").Output("apexkeys.txt"))
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008879 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 +09008880}
8881
Jooyung Han938b5932020-06-20 12:47:47 +09008882func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008883 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008884 apex {
8885 name: "myapex",
8886 key: "myapex.key",
8887 apps: ["app"],
8888 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008889 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008890 }
8891
8892 apex_key {
8893 name: "myapex.key",
8894 public_key: "testkey.avbpubkey",
8895 private_key: "testkey.pem",
8896 }
8897
8898 android_app {
8899 name: "app",
8900 srcs: ["foo/bar/MyClass.java"],
8901 package_name: "foo",
8902 sdk_version: "none",
8903 system_modules: "none",
8904 apex_available: [ "myapex" ],
8905 }
8906 `, withFiles(map[string][]byte{
8907 "sub/Android.bp": []byte(`
8908 override_apex {
8909 name: "override_myapex",
8910 base: "myapex",
8911 apps: ["override_app"],
8912 allowed_files: ":allowed",
8913 }
8914 // Overridable "path" property should be referenced indirectly
8915 filegroup {
8916 name: "allowed",
8917 srcs: ["allowed.txt"],
8918 }
8919 override_android_app {
8920 name: "override_app",
8921 base: "app",
8922 package_name: "bar",
8923 }
8924 `),
8925 }))
8926
Jooyung Hana0503a52023-08-23 13:12:50 +09008927 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008928 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8929 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8930 }
8931
Jooyung Hana0503a52023-08-23 13:12:50 +09008932 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09008933 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8934 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8935 }
8936}
8937
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008938func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008939 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008940 apex {
8941 name: "myapex",
8942 key: "myapex.key",
8943 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008944 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008945 }
8946
8947 apex_key {
8948 name: "myapex.key",
8949 public_key: "testkey.avbpubkey",
8950 private_key: "testkey.pem",
8951 }
8952
8953 cc_library {
8954 name: "mylib",
8955 srcs: ["mylib.cpp"],
8956 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008957 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008958 },
8959 apex_available: ["myapex"],
8960 }
8961
8962 cc_prebuilt_library_shared {
8963 name: "mylib",
8964 prefer: false,
8965 srcs: ["prebuilt.so"],
8966 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008967 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008968 },
8969 apex_available: ["myapex"],
8970 }
8971 `)
8972}
8973
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008974func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008975 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008976 apex {
8977 name: "myapex",
8978 key: "myapex.key",
8979 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008980 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008981 }
8982 apex_key {
8983 name: "myapex.key",
8984 public_key: "testkey.avbpubkey",
8985 private_key: "testkey.pem",
8986 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008987 `,
8988 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8989 variables.CompressedApex = proptools.BoolPtr(true)
8990 }),
8991 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008992
Jooyung Hana0503a52023-08-23 13:12:50 +09008993 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("compressRule")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008994 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8995
Jooyung Hana0503a52023-08-23 13:12:50 +09008996 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex").Description("sign compressedApex")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008997 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8998
8999 // Make sure output of bundle is .capex
Jooyung Hana0503a52023-08-23 13:12:50 +09009000 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009001 ensureContains(t, ab.outputFile.String(), "myapex.capex")
9002
9003 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07009004 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009005 var builder strings.Builder
9006 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9007 androidMk := builder.String()
9008 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
9009}
9010
Martin Stjernholm2856c662020-12-02 15:03:42 +00009011func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009012 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00009013 apex {
9014 name: "myapex",
9015 key: "myapex.key",
9016 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009017 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00009018 }
9019
9020 apex_key {
9021 name: "myapex.key",
9022 public_key: "testkey.avbpubkey",
9023 private_key: "testkey.pem",
9024 }
9025
9026 cc_library {
9027 name: "mylib",
9028 srcs: ["mylib.cpp"],
9029 apex_available: ["myapex"],
9030 shared_libs: ["otherlib"],
9031 system_shared_libs: [],
9032 }
9033
9034 cc_library {
9035 name: "otherlib",
9036 srcs: ["mylib.cpp"],
9037 stubs: {
9038 versions: ["current"],
9039 },
9040 }
9041
9042 cc_prebuilt_library_shared {
9043 name: "otherlib",
9044 prefer: true,
9045 srcs: ["prebuilt.so"],
9046 stubs: {
9047 versions: ["current"],
9048 },
9049 }
9050 `)
9051
Jooyung Hana0503a52023-08-23 13:12:50 +09009052 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07009053 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00009054 var builder strings.Builder
9055 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9056 androidMk := builder.String()
9057
9058 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
9059 // a thing there.
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009060 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 otherlib\n")
Martin Stjernholm2856c662020-12-02 15:03:42 +00009061}
9062
Jiyong Parke3867542020-12-03 17:28:25 +09009063func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009064 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09009065 apex {
9066 name: "myapex",
9067 key: "myapex.key",
9068 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009069 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09009070 }
9071
9072 apex_key {
9073 name: "myapex.key",
9074 public_key: "testkey.avbpubkey",
9075 private_key: "testkey.pem",
9076 }
9077
9078 cc_library {
9079 name: "mylib",
9080 srcs: ["mylib.cpp"],
9081 system_shared_libs: [],
9082 stl: "none",
9083 apex_available: ["myapex"],
9084 shared_libs: ["mylib2"],
9085 target: {
9086 apex: {
9087 exclude_shared_libs: ["mylib2"],
9088 },
9089 },
9090 }
9091
9092 cc_library {
9093 name: "mylib2",
9094 srcs: ["mylib.cpp"],
9095 system_shared_libs: [],
9096 stl: "none",
9097 }
9098 `)
9099
9100 // Check if mylib is linked to mylib2 for the non-apex target
9101 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
9102 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
9103
9104 // Make sure that the link doesn't occur for the apex target
9105 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
9106 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
9107
9108 // It shouldn't appear in the copy cmd as well.
Jooyung Hana0503a52023-08-23 13:12:50 +09009109 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule").Args["copy_commands"]
Jiyong Parke3867542020-12-03 17:28:25 +09009110 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
9111}
9112
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009113func TestPrebuiltStubLibDep(t *testing.T) {
9114 bpBase := `
9115 apex {
9116 name: "myapex",
9117 key: "myapex.key",
9118 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009119 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009120 }
9121 apex_key {
9122 name: "myapex.key",
9123 public_key: "testkey.avbpubkey",
9124 private_key: "testkey.pem",
9125 }
9126 cc_library {
9127 name: "mylib",
9128 srcs: ["mylib.cpp"],
9129 apex_available: ["myapex"],
9130 shared_libs: ["stublib"],
9131 system_shared_libs: [],
9132 }
9133 apex {
9134 name: "otherapex",
9135 enabled: %s,
9136 key: "myapex.key",
9137 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009138 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009139 }
9140 `
9141
9142 stublibSourceBp := `
9143 cc_library {
9144 name: "stublib",
9145 srcs: ["mylib.cpp"],
9146 apex_available: ["otherapex"],
9147 system_shared_libs: [],
9148 stl: "none",
9149 stubs: {
9150 versions: ["1"],
9151 },
9152 }
9153 `
9154
9155 stublibPrebuiltBp := `
9156 cc_prebuilt_library_shared {
9157 name: "stublib",
9158 srcs: ["prebuilt.so"],
9159 apex_available: ["otherapex"],
9160 stubs: {
9161 versions: ["1"],
9162 },
9163 %s
9164 }
9165 `
9166
9167 tests := []struct {
9168 name string
9169 stublibBp string
9170 usePrebuilt bool
9171 modNames []string // Modules to collect AndroidMkEntries for
9172 otherApexEnabled []string
9173 }{
9174 {
9175 name: "only_source",
9176 stublibBp: stublibSourceBp,
9177 usePrebuilt: false,
9178 modNames: []string{"stublib"},
9179 otherApexEnabled: []string{"true", "false"},
9180 },
9181 {
9182 name: "source_preferred",
9183 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9184 usePrebuilt: false,
9185 modNames: []string{"stublib", "prebuilt_stublib"},
9186 otherApexEnabled: []string{"true", "false"},
9187 },
9188 {
9189 name: "prebuilt_preferred",
9190 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9191 usePrebuilt: true,
9192 modNames: []string{"stublib", "prebuilt_stublib"},
9193 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9194 },
9195 {
9196 name: "only_prebuilt",
9197 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9198 usePrebuilt: true,
9199 modNames: []string{"stublib"},
9200 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9201 },
9202 }
9203
9204 for _, test := range tests {
9205 t.Run(test.name, func(t *testing.T) {
9206 for _, otherApexEnabled := range test.otherApexEnabled {
9207 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009208 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009209
9210 type modAndMkEntries struct {
9211 mod *cc.Module
9212 mkEntries android.AndroidMkEntries
9213 }
9214 entries := []*modAndMkEntries{}
9215
9216 // Gather shared lib modules that are installable
9217 for _, modName := range test.modNames {
9218 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9219 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9220 continue
9221 }
9222 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08009223 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009224 continue
9225 }
Colin Crossaa255532020-07-03 13:18:24 -07009226 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009227 if ent.Disabled {
9228 continue
9229 }
9230 entries = append(entries, &modAndMkEntries{
9231 mod: mod,
9232 mkEntries: ent,
9233 })
9234 }
9235 }
9236 }
9237
9238 var entry *modAndMkEntries = nil
9239 for _, ent := range entries {
9240 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9241 if entry != nil {
9242 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9243 } else {
9244 entry = ent
9245 }
9246 }
9247 }
9248
9249 if entry == nil {
9250 t.Errorf("AndroidMk entry for \"stublib\" missing")
9251 } else {
9252 isPrebuilt := entry.mod.Prebuilt() != nil
9253 if isPrebuilt != test.usePrebuilt {
9254 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9255 }
9256 if !entry.mod.IsStubs() {
9257 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9258 }
9259 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9260 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9261 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009262 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009263 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009264 if !android.InList(expected, cflags) {
9265 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9266 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009267 }
9268 })
9269 }
9270 })
9271 }
9272}
9273
Colin Crossc33e5212021-05-25 18:16:02 -07009274func TestApexJavaCoverage(t *testing.T) {
9275 bp := `
9276 apex {
9277 name: "myapex",
9278 key: "myapex.key",
9279 java_libs: ["mylib"],
9280 bootclasspath_fragments: ["mybootclasspathfragment"],
9281 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9282 updatable: false,
9283 }
9284
9285 apex_key {
9286 name: "myapex.key",
9287 public_key: "testkey.avbpubkey",
9288 private_key: "testkey.pem",
9289 }
9290
9291 java_library {
9292 name: "mylib",
9293 srcs: ["mylib.java"],
9294 apex_available: ["myapex"],
9295 compile_dex: true,
9296 }
9297
9298 bootclasspath_fragment {
9299 name: "mybootclasspathfragment",
9300 contents: ["mybootclasspathlib"],
9301 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009302 hidden_api: {
9303 split_packages: ["*"],
9304 },
Colin Crossc33e5212021-05-25 18:16:02 -07009305 }
9306
9307 java_library {
9308 name: "mybootclasspathlib",
9309 srcs: ["mybootclasspathlib.java"],
9310 apex_available: ["myapex"],
9311 compile_dex: true,
9312 }
9313
9314 systemserverclasspath_fragment {
9315 name: "mysystemserverclasspathfragment",
9316 contents: ["mysystemserverclasspathlib"],
9317 apex_available: ["myapex"],
9318 }
9319
9320 java_library {
9321 name: "mysystemserverclasspathlib",
9322 srcs: ["mysystemserverclasspathlib.java"],
9323 apex_available: ["myapex"],
9324 compile_dex: true,
9325 }
9326 `
9327
9328 result := android.GroupFixturePreparers(
9329 PrepareForTestWithApexBuildComponents,
9330 prepareForTestWithMyapex,
9331 java.PrepareForTestWithJavaDefaultModules,
9332 android.PrepareForTestWithAndroidBuildComponents,
9333 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009334 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9335 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009336 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009337 ).RunTest(t)
9338
9339 // Make sure jacoco ran on both mylib and mybootclasspathlib
9340 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9341 t.Errorf("Failed to find jacoco rule for mylib")
9342 }
9343 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9344 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9345 }
9346 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9347 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9348 }
9349}
9350
Jiyong Park192600a2021-08-03 07:52:17 +00009351func TestProhibitStaticExecutable(t *testing.T) {
9352 testApexError(t, `executable mybin is static`, `
9353 apex {
9354 name: "myapex",
9355 key: "myapex.key",
9356 binaries: ["mybin"],
9357 min_sdk_version: "29",
9358 }
9359
9360 apex_key {
9361 name: "myapex.key",
9362 public_key: "testkey.avbpubkey",
9363 private_key: "testkey.pem",
9364 }
9365
9366 cc_binary {
9367 name: "mybin",
9368 srcs: ["mylib.cpp"],
9369 relative_install_path: "foo/bar",
9370 static_executable: true,
9371 system_shared_libs: [],
9372 stl: "none",
9373 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009374 min_sdk_version: "29",
9375 }
9376 `)
9377
9378 testApexError(t, `executable mybin.rust is static`, `
9379 apex {
9380 name: "myapex",
9381 key: "myapex.key",
9382 binaries: ["mybin.rust"],
9383 min_sdk_version: "29",
9384 }
9385
9386 apex_key {
9387 name: "myapex.key",
9388 public_key: "testkey.avbpubkey",
9389 private_key: "testkey.pem",
9390 }
9391
9392 rust_binary {
9393 name: "mybin.rust",
9394 srcs: ["foo.rs"],
9395 static_executable: true,
9396 apex_available: ["myapex"],
9397 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009398 }
9399 `)
9400}
9401
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009402func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9403 ctx := testApex(t, `
9404 apex {
9405 name: "myapex",
9406 key: "myapex.key",
9407 updatable: false,
9408 java_libs: ["foo"],
9409 }
9410
9411 apex_key {
9412 name: "myapex.key",
9413 public_key: "testkey.avbpubkey",
9414 private_key: "testkey.pem",
9415 }
9416
9417 java_library {
9418 name: "foo",
9419 srcs: ["foo.java"],
9420 apex_available: ["myapex"],
9421 installable: true,
9422 }
9423 `,
9424 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9425 )
9426
Jooyung Hana0503a52023-08-23 13:12:50 +09009427 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009428 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9429 var builder strings.Builder
9430 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9431 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009432 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 +00009433}
9434
9435func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9436 ctx := testApex(t, `
9437 prebuilt_apex {
9438 name: "myapex",
9439 arch: {
9440 arm64: {
9441 src: "myapex-arm64.apex",
9442 },
9443 arm: {
9444 src: "myapex-arm.apex",
9445 },
9446 },
9447 exported_java_libs: ["foo"],
9448 }
9449
9450 java_import {
9451 name: "foo",
9452 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009453 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009454 }
9455 `,
9456 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9457 )
9458
9459 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9460 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9461 mainModuleEntries := entriesList[0]
9462 android.AssertArrayString(t,
9463 "LOCAL_REQUIRED_MODULES",
9464 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9465 []string{
9466 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9467 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9468 })
9469}
9470
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009471func TestAndroidMk_RequiredModules(t *testing.T) {
9472 ctx := testApex(t, `
9473 apex {
9474 name: "myapex",
9475 key: "myapex.key",
9476 updatable: false,
9477 java_libs: ["foo"],
9478 required: ["otherapex"],
9479 }
9480
9481 apex {
9482 name: "otherapex",
9483 key: "myapex.key",
9484 updatable: false,
9485 java_libs: ["foo"],
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009486 }
9487
9488 apex_key {
9489 name: "myapex.key",
9490 public_key: "testkey.avbpubkey",
9491 private_key: "testkey.pem",
9492 }
9493
9494 java_library {
9495 name: "foo",
9496 srcs: ["foo.java"],
9497 apex_available: ["myapex", "otherapex"],
9498 installable: true,
9499 }
9500 `)
9501
Jooyung Hana0503a52023-08-23 13:12:50 +09009502 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009503 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9504 var builder strings.Builder
9505 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9506 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009507 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009508}
9509
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009510func TestAndroidMk_RequiredDeps(t *testing.T) {
9511 ctx := testApex(t, `
9512 apex {
9513 name: "myapex",
9514 key: "myapex.key",
9515 updatable: false,
9516 }
9517
9518 apex_key {
9519 name: "myapex.key",
9520 public_key: "testkey.avbpubkey",
9521 private_key: "testkey.pem",
9522 }
9523 `)
9524
Jooyung Hana0503a52023-08-23 13:12:50 +09009525 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009526 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009527 data := android.AndroidMkDataForTest(t, ctx, bundle)
9528 var builder strings.Builder
9529 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9530 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009531 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009532}
9533
Jooyung Hana6d36672022-02-24 13:58:07 +09009534func TestApexOutputFileProducer(t *testing.T) {
9535 for _, tc := range []struct {
9536 name string
9537 ref string
9538 expected_data []string
9539 }{
9540 {
9541 name: "test_using_output",
9542 ref: ":myapex",
Jooyung Hana0503a52023-08-23 13:12:50 +09009543 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.capex:myapex.capex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009544 },
9545 {
9546 name: "test_using_apex",
9547 ref: ":myapex{.apex}",
Jooyung Hana0503a52023-08-23 13:12:50 +09009548 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.apex:myapex.apex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009549 },
9550 } {
9551 t.Run(tc.name, func(t *testing.T) {
9552 ctx := testApex(t, `
9553 apex {
9554 name: "myapex",
9555 key: "myapex.key",
9556 compressible: true,
9557 updatable: false,
9558 }
9559
9560 apex_key {
9561 name: "myapex.key",
9562 public_key: "testkey.avbpubkey",
9563 private_key: "testkey.pem",
9564 }
9565
9566 java_test {
9567 name: "`+tc.name+`",
9568 srcs: ["a.java"],
9569 data: ["`+tc.ref+`"],
9570 }
9571 `,
9572 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9573 variables.CompressedApex = proptools.BoolPtr(true)
9574 }))
9575 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9576 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9577 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9578 })
9579 }
9580}
9581
satayev758968a2021-12-06 11:42:40 +00009582func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9583 preparer := android.GroupFixturePreparers(
9584 PrepareForTestWithApexBuildComponents,
9585 prepareForTestWithMyapex,
9586 java.PrepareForTestWithJavaSdkLibraryFiles,
9587 java.PrepareForTestWithJavaDefaultModules,
9588 android.PrepareForTestWithAndroidBuildComponents,
9589 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9590 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9591 )
9592
9593 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9594 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9595 preparer.RunTestWithBp(t, `
9596 apex {
9597 name: "myapex",
9598 key: "myapex.key",
9599 bootclasspath_fragments: ["mybootclasspathfragment"],
9600 min_sdk_version: "30",
9601 updatable: false,
9602 }
9603
9604 apex_key {
9605 name: "myapex.key",
9606 public_key: "testkey.avbpubkey",
9607 private_key: "testkey.pem",
9608 }
9609
9610 bootclasspath_fragment {
9611 name: "mybootclasspathfragment",
9612 contents: ["mybootclasspathlib"],
9613 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009614 hidden_api: {
9615 split_packages: ["*"],
9616 },
satayev758968a2021-12-06 11:42:40 +00009617 }
9618
9619 java_sdk_library {
9620 name: "mybootclasspathlib",
9621 srcs: ["mybootclasspathlib.java"],
9622 apex_available: ["myapex"],
9623 compile_dex: true,
9624 unsafe_ignore_missing_latest_api: true,
9625 min_sdk_version: "31",
9626 static_libs: ["util"],
9627 }
9628
9629 java_library {
9630 name: "util",
9631 srcs: ["a.java"],
9632 apex_available: ["myapex"],
9633 min_sdk_version: "31",
9634 static_libs: ["another_util"],
9635 }
9636
9637 java_library {
9638 name: "another_util",
9639 srcs: ["a.java"],
9640 min_sdk_version: "31",
9641 apex_available: ["myapex"],
9642 }
9643 `)
9644 })
9645
9646 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9647 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9648 preparer.RunTestWithBp(t, `
9649 apex {
9650 name: "myapex",
9651 key: "myapex.key",
9652 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9653 min_sdk_version: "30",
9654 updatable: false,
9655 }
9656
9657 apex_key {
9658 name: "myapex.key",
9659 public_key: "testkey.avbpubkey",
9660 private_key: "testkey.pem",
9661 }
9662
9663 systemserverclasspath_fragment {
9664 name: "mysystemserverclasspathfragment",
9665 contents: ["mysystemserverclasspathlib"],
9666 apex_available: ["myapex"],
9667 }
9668
9669 java_sdk_library {
9670 name: "mysystemserverclasspathlib",
9671 srcs: ["mysystemserverclasspathlib.java"],
9672 apex_available: ["myapex"],
9673 compile_dex: true,
9674 min_sdk_version: "32",
9675 unsafe_ignore_missing_latest_api: true,
9676 static_libs: ["util"],
9677 }
9678
9679 java_library {
9680 name: "util",
9681 srcs: ["a.java"],
9682 apex_available: ["myapex"],
9683 min_sdk_version: "31",
9684 static_libs: ["another_util"],
9685 }
9686
9687 java_library {
9688 name: "another_util",
9689 srcs: ["a.java"],
9690 min_sdk_version: "31",
9691 apex_available: ["myapex"],
9692 }
9693 `)
9694 })
9695
9696 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9697 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9698 RunTestWithBp(t, `
9699 apex {
9700 name: "myapex",
9701 key: "myapex.key",
9702 bootclasspath_fragments: ["mybootclasspathfragment"],
9703 min_sdk_version: "30",
9704 updatable: false,
9705 }
9706
9707 apex_key {
9708 name: "myapex.key",
9709 public_key: "testkey.avbpubkey",
9710 private_key: "testkey.pem",
9711 }
9712
9713 bootclasspath_fragment {
9714 name: "mybootclasspathfragment",
9715 contents: ["mybootclasspathlib"],
9716 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009717 hidden_api: {
9718 split_packages: ["*"],
9719 },
satayev758968a2021-12-06 11:42:40 +00009720 }
9721
9722 java_sdk_library {
9723 name: "mybootclasspathlib",
9724 srcs: ["mybootclasspathlib.java"],
9725 apex_available: ["myapex"],
9726 compile_dex: true,
9727 unsafe_ignore_missing_latest_api: true,
9728 }
9729 `)
9730 })
9731
9732 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9733 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9734 RunTestWithBp(t, `
9735 apex {
9736 name: "myapex",
9737 key: "myapex.key",
9738 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9739 min_sdk_version: "30",
9740 updatable: false,
9741 }
9742
9743 apex_key {
9744 name: "myapex.key",
9745 public_key: "testkey.avbpubkey",
9746 private_key: "testkey.pem",
9747 }
9748
9749 systemserverclasspath_fragment {
9750 name: "mysystemserverclasspathfragment",
9751 contents: ["mysystemserverclasspathlib"],
9752 apex_available: ["myapex"],
9753 }
9754
9755 java_sdk_library {
9756 name: "mysystemserverclasspathlib",
9757 srcs: ["mysystemserverclasspathlib.java"],
9758 apex_available: ["myapex"],
9759 compile_dex: true,
9760 unsafe_ignore_missing_latest_api: true,
9761 }
9762 `)
9763 })
9764}
9765
Jiakai Zhang6decef92022-01-12 17:56:19 +00009766// Verifies that the APEX depends on all the Make modules in the list.
9767func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9768 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9769 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009770 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009771 }
9772}
9773
9774// Verifies that the APEX does not depend on any of the Make modules in the list.
9775func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9776 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9777 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009778 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009779 }
9780}
9781
Cole Faust1021ccd2023-02-26 21:15:25 -08009782// TODO(b/193460475): Re-enable this test
9783//func TestApexStrictUpdtabilityLint(t *testing.T) {
9784// bpTemplate := `
9785// apex {
9786// name: "myapex",
9787// key: "myapex.key",
9788// java_libs: ["myjavalib"],
9789// updatable: %v,
9790// min_sdk_version: "29",
9791// }
9792// apex_key {
9793// name: "myapex.key",
9794// }
9795// java_library {
9796// name: "myjavalib",
9797// srcs: ["MyClass.java"],
9798// apex_available: [ "myapex" ],
9799// lint: {
9800// strict_updatability_linting: %v,
9801// },
9802// sdk_version: "current",
9803// min_sdk_version: "29",
9804// }
9805// `
9806// fs := android.MockFS{
9807// "lint-baseline.xml": nil,
9808// }
9809//
9810// testCases := []struct {
9811// testCaseName string
9812// apexUpdatable bool
9813// javaStrictUpdtabilityLint bool
9814// lintFileExists bool
9815// disallowedFlagExpected bool
9816// }{
9817// {
9818// testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9819// apexUpdatable: true,
9820// javaStrictUpdtabilityLint: true,
9821// lintFileExists: false,
9822// disallowedFlagExpected: false,
9823// },
9824// {
9825// testCaseName: "non-updatable apex respects strict_updatability of javalib",
9826// apexUpdatable: false,
9827// javaStrictUpdtabilityLint: false,
9828// lintFileExists: true,
9829// disallowedFlagExpected: false,
9830// },
9831// {
9832// testCaseName: "non-updatable apex respects strict updatability of javalib",
9833// apexUpdatable: false,
9834// javaStrictUpdtabilityLint: true,
9835// lintFileExists: true,
9836// disallowedFlagExpected: true,
9837// },
9838// {
9839// testCaseName: "updatable apex sets strict updatability of javalib to true",
9840// apexUpdatable: true,
9841// javaStrictUpdtabilityLint: false, // will be set to true by mutator
9842// lintFileExists: true,
9843// disallowedFlagExpected: true,
9844// },
9845// }
9846//
9847// for _, testCase := range testCases {
9848// bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9849// fixtures := []android.FixturePreparer{}
9850// if testCase.lintFileExists {
9851// fixtures = append(fixtures, fs.AddToFixture())
9852// }
9853//
9854// result := testApex(t, bp, fixtures...)
9855// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9856// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9857// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9858//
9859// if disallowedFlagActual != testCase.disallowedFlagExpected {
9860// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9861// }
9862// }
9863//}
9864//
9865//func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9866// bp := `
9867// apex {
9868// name: "myapex",
9869// key: "myapex.key",
9870// java_libs: ["myjavalib"],
9871// updatable: true,
9872// min_sdk_version: "29",
9873// }
9874// apex_key {
9875// name: "myapex.key",
9876// }
9877// java_library {
9878// name: "myjavalib",
9879// srcs: ["MyClass.java"],
9880// apex_available: [ "myapex" ],
9881// sdk_version: "current",
9882// min_sdk_version: "29",
9883// }
9884// `
9885//
9886// testCases := []struct {
9887// testCaseName string
9888// moduleDirectory string
9889// disallowedFlagExpected bool
9890// }{
9891// {
9892// testCaseName: "lintable module defined outside libcore",
9893// moduleDirectory: "",
9894// disallowedFlagExpected: true,
9895// },
9896// {
9897// testCaseName: "lintable module defined in libcore root directory",
9898// moduleDirectory: "libcore/",
9899// disallowedFlagExpected: false,
9900// },
9901// {
9902// testCaseName: "lintable module defined in libcore child directory",
9903// moduleDirectory: "libcore/childdir/",
9904// disallowedFlagExpected: true,
9905// },
9906// }
9907//
9908// for _, testCase := range testCases {
9909// lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9910// bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9911// result := testApex(t, "", lintFileCreator, bpFileCreator)
9912// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9913// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9914// cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9915// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9916//
9917// if disallowedFlagActual != testCase.disallowedFlagExpected {
9918// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9919// }
9920// }
9921//}
9922//
9923//// checks transtive deps of an apex coming from bootclasspath_fragment
9924//func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9925// bp := `
9926// apex {
9927// name: "myapex",
9928// key: "myapex.key",
9929// bootclasspath_fragments: ["mybootclasspathfragment"],
9930// updatable: true,
9931// min_sdk_version: "29",
9932// }
9933// apex_key {
9934// name: "myapex.key",
9935// }
9936// bootclasspath_fragment {
9937// name: "mybootclasspathfragment",
9938// contents: ["myjavalib"],
9939// apex_available: ["myapex"],
9940// hidden_api: {
9941// split_packages: ["*"],
9942// },
9943// }
9944// java_library {
9945// name: "myjavalib",
9946// srcs: ["MyClass.java"],
9947// apex_available: [ "myapex" ],
9948// sdk_version: "current",
9949// min_sdk_version: "29",
9950// compile_dex: true,
9951// }
9952// `
9953// fs := android.MockFS{
9954// "lint-baseline.xml": nil,
9955// }
9956//
9957// result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9958// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9959// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9960// if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9961// t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9962// }
9963//}
Spandan Das66773252022-01-15 00:23:18 +00009964
Spandan Das42e89502022-05-06 22:12:55 +00009965// updatable apexes should propagate updatable=true to its apps
9966func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9967 bp := `
9968 apex {
9969 name: "myapex",
9970 key: "myapex.key",
9971 updatable: %v,
9972 apps: [
9973 "myapp",
9974 ],
9975 min_sdk_version: "30",
9976 }
9977 apex_key {
9978 name: "myapex.key",
9979 }
9980 android_app {
9981 name: "myapp",
9982 updatable: %v,
9983 apex_available: [
9984 "myapex",
9985 ],
9986 sdk_version: "current",
9987 min_sdk_version: "30",
9988 }
9989 `
9990 testCases := []struct {
9991 name string
9992 apex_is_updatable_bp bool
9993 app_is_updatable_bp bool
9994 app_is_updatable_expected bool
9995 }{
9996 {
9997 name: "Non-updatable apex respects updatable property of non-updatable app",
9998 apex_is_updatable_bp: false,
9999 app_is_updatable_bp: false,
10000 app_is_updatable_expected: false,
10001 },
10002 {
10003 name: "Non-updatable apex respects updatable property of updatable app",
10004 apex_is_updatable_bp: false,
10005 app_is_updatable_bp: true,
10006 app_is_updatable_expected: true,
10007 },
10008 {
10009 name: "Updatable apex respects updatable property of updatable app",
10010 apex_is_updatable_bp: true,
10011 app_is_updatable_bp: true,
10012 app_is_updatable_expected: true,
10013 },
10014 {
10015 name: "Updatable apex sets updatable=true on non-updatable app",
10016 apex_is_updatable_bp: true,
10017 app_is_updatable_bp: false,
10018 app_is_updatable_expected: true,
10019 },
10020 }
10021 for _, testCase := range testCases {
10022 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
10023 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
10024 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
10025 }
10026}
10027
Kiyoung Kim487689e2022-07-26 09:48:22 +090010028func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10029 bp := `
10030 apex {
10031 name: "myapex",
10032 key: "myapex.key",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010033 native_shared_libs: ["libbaz"],
10034 binaries: ["binfoo"],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010035 min_sdk_version: "29",
10036 }
10037 apex_key {
10038 name: "myapex.key",
10039 }
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010040 cc_binary {
10041 name: "binfoo",
10042 shared_libs: ["libbar", "libbaz", "libqux",],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010043 apex_available: ["myapex"],
10044 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010045 recovery_available: false,
10046 }
10047 cc_library {
10048 name: "libbar",
10049 srcs: ["libbar.cc"],
10050 stubs: {
10051 symbol_file: "libbar.map.txt",
10052 versions: [
10053 "29",
10054 ],
10055 },
10056 }
10057 cc_library {
10058 name: "libbaz",
10059 srcs: ["libbaz.cc"],
10060 apex_available: ["myapex"],
10061 min_sdk_version: "29",
10062 stubs: {
10063 symbol_file: "libbaz.map.txt",
10064 versions: [
10065 "29",
10066 ],
10067 },
Kiyoung Kim487689e2022-07-26 09:48:22 +090010068 }
10069 cc_api_library {
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010070 name: "libbar",
10071 src: "libbar_stub.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010072 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010073 variants: ["apex.29"],
10074 }
10075 cc_api_variant {
10076 name: "libbar",
10077 variant: "apex",
10078 version: "29",
10079 src: "libbar_apex_29.so",
10080 }
10081 cc_api_library {
10082 name: "libbaz",
10083 src: "libbaz_stub.so",
10084 min_sdk_version: "29",
10085 variants: ["apex.29"],
10086 }
10087 cc_api_variant {
10088 name: "libbaz",
10089 variant: "apex",
10090 version: "29",
10091 src: "libbaz_apex_29.so",
10092 }
10093 cc_api_library {
10094 name: "libqux",
10095 src: "libqux_stub.so",
10096 min_sdk_version: "29",
10097 variants: ["apex.29"],
10098 }
10099 cc_api_variant {
10100 name: "libqux",
10101 variant: "apex",
10102 version: "29",
10103 src: "libqux_apex_29.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010104 }
10105 api_imports {
10106 name: "api_imports",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010107 apex_shared_libs: [
10108 "libbar",
10109 "libbaz",
10110 "libqux",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010111 ],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010112 }
10113 `
10114 result := testApex(t, bp)
10115
10116 hasDep := func(m android.Module, wantDep android.Module) bool {
10117 t.Helper()
10118 var found bool
10119 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10120 if dep == wantDep {
10121 found = true
10122 }
10123 })
10124 return found
10125 }
10126
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010127 // Library defines stubs and cc_api_library should be used with cc_api_library
10128 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Module()
10129 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10130 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
Kiyoung Kim487689e2022-07-26 09:48:22 +090010131
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010132 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10133 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
Kiyoung Kim487689e2022-07-26 09:48:22 +090010134
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010135 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Rule("ld").Args["libFlags"]
10136 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10137 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10138 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
10139
10140 // Library defined in the same APEX should be linked with original definition instead of cc_api_library
10141 libbazApexVariant := result.ModuleForTests("libbaz", "android_arm64_armv8-a_shared_apex29").Module()
10142 libbazApiImportCoreVariant := result.ModuleForTests("libbaz.apiimport", "android_arm64_armv8-a_shared").Module()
10143 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even from same APEX", true, hasDep(binfooApexVariant, libbazApiImportCoreVariant))
10144 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbazApexVariant))
10145
10146 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbaz.so")
10147 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbaz.apiimport.so")
10148 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbaz.apex.29.apiimport.so")
10149
10150 // cc_api_library defined without original library should be linked with cc_api_library
10151 libquxApiImportApexVariant := result.ModuleForTests("libqux.apiimport", "android_arm64_armv8-a_shared").Module()
10152 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even original library definition does not exist", true, hasDep(binfooApexVariant, libquxApiImportApexVariant))
10153 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libqux.apex.29.apiimport.so")
10154}
10155
10156func TestPlatformBinaryBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10157 bp := `
10158 apex {
10159 name: "myapex",
10160 key: "myapex.key",
10161 native_shared_libs: ["libbar"],
10162 min_sdk_version: "29",
10163 }
10164 apex_key {
10165 name: "myapex.key",
10166 }
10167 cc_binary {
10168 name: "binfoo",
10169 shared_libs: ["libbar"],
10170 recovery_available: false,
10171 }
10172 cc_library {
10173 name: "libbar",
10174 srcs: ["libbar.cc"],
10175 apex_available: ["myapex"],
10176 min_sdk_version: "29",
10177 stubs: {
10178 symbol_file: "libbar.map.txt",
10179 versions: [
10180 "29",
10181 ],
10182 },
10183 }
10184 cc_api_library {
10185 name: "libbar",
10186 src: "libbar_stub.so",
10187 variants: ["apex.29"],
10188 }
10189 cc_api_variant {
10190 name: "libbar",
10191 variant: "apex",
10192 version: "29",
10193 src: "libbar_apex_29.so",
10194 }
10195 api_imports {
10196 name: "api_imports",
10197 apex_shared_libs: [
10198 "libbar",
10199 ],
10200 }
10201 `
10202
10203 result := testApex(t, bp)
10204
10205 hasDep := func(m android.Module, wantDep android.Module) bool {
10206 t.Helper()
10207 var found bool
10208 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10209 if dep == wantDep {
10210 found = true
10211 }
10212 })
10213 return found
10214 }
10215
10216 // Library defines stubs and cc_api_library should be used with cc_api_library
10217 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Module()
10218 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10219 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
10220
10221 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10222 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
10223
10224 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
10225 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10226 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10227 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
Kiyoung Kim487689e2022-07-26 09:48:22 +090010228}
Dennis Shend4f5d932023-01-31 20:27:21 +000010229
10230func TestTrimmedApex(t *testing.T) {
10231 bp := `
10232 apex {
10233 name: "myapex",
10234 key: "myapex.key",
10235 native_shared_libs: ["libfoo","libbaz"],
10236 min_sdk_version: "29",
10237 trim_against: "mydcla",
10238 }
10239 apex {
10240 name: "mydcla",
10241 key: "myapex.key",
10242 native_shared_libs: ["libfoo","libbar"],
10243 min_sdk_version: "29",
10244 file_contexts: ":myapex-file_contexts",
10245 dynamic_common_lib_apex: true,
10246 }
10247 apex_key {
10248 name: "myapex.key",
10249 }
10250 cc_library {
10251 name: "libfoo",
10252 shared_libs: ["libc"],
10253 apex_available: ["myapex","mydcla"],
10254 min_sdk_version: "29",
10255 }
10256 cc_library {
10257 name: "libbar",
10258 shared_libs: ["libc"],
10259 apex_available: ["myapex","mydcla"],
10260 min_sdk_version: "29",
10261 }
10262 cc_library {
10263 name: "libbaz",
10264 shared_libs: ["libc"],
10265 apex_available: ["myapex","mydcla"],
10266 min_sdk_version: "29",
10267 }
10268 cc_api_library {
10269 name: "libc",
10270 src: "libc.so",
10271 min_sdk_version: "29",
10272 recovery_available: true,
Ivan Lozanoadd122a2023-07-13 11:01:41 -040010273 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +090010274 product_available: true,
Dennis Shend4f5d932023-01-31 20:27:21 +000010275 }
10276 api_imports {
10277 name: "api_imports",
10278 shared_libs: [
10279 "libc",
10280 ],
10281 header_libs: [],
10282 }
10283 `
10284 ctx := testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +090010285 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dennis Shend4f5d932023-01-31 20:27:21 +000010286 apexRule := module.MaybeRule("apexRule")
10287 if apexRule.Rule == nil {
10288 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10289 }
10290
10291 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
Jooyung Hana0503a52023-08-23 13:12:50 +090010292 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("TrimmedApexRule")
Dennis Shend4f5d932023-01-31 20:27:21 +000010293 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10294 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10295 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10296 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10297}
Jingwen Chendea7a642023-03-28 11:30:50 +000010298
10299func TestCannedFsConfig(t *testing.T) {
10300 ctx := testApex(t, `
10301 apex {
10302 name: "myapex",
10303 key: "myapex.key",
10304 updatable: false,
10305 }
10306
10307 apex_key {
10308 name: "myapex.key",
10309 public_key: "testkey.avbpubkey",
10310 private_key: "testkey.pem",
10311 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010312 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010313 generateFsRule := mod.Rule("generateFsConfig")
10314 cmd := generateFsRule.RuleParams.Command
10315
10316 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10317}
10318
10319func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10320 ctx := testApex(t, `
10321 apex {
10322 name: "myapex",
10323 key: "myapex.key",
10324 canned_fs_config: "my_config",
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 // Ensure that canned_fs_config has "cat my_config" at the end
10338 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10339}
Spandan Das20fce2d2023-04-12 17:21:39 +000010340
10341func TestStubLibrariesMultipleApexViolation(t *testing.T) {
10342 testCases := []struct {
10343 desc string
10344 hasStubs bool
10345 apexAvailable string
10346 expectedError string
10347 }{
10348 {
10349 desc: "non-stub library can have multiple apex_available",
10350 hasStubs: false,
10351 apexAvailable: `["myapex", "otherapex"]`,
10352 },
10353 {
10354 desc: "stub library should not be available to anyapex",
10355 hasStubs: true,
10356 apexAvailable: `["//apex_available:anyapex"]`,
10357 expectedError: "Stub libraries should have a single apex_available.*anyapex",
10358 },
10359 {
10360 desc: "stub library should not be available to multiple apexes",
10361 hasStubs: true,
10362 apexAvailable: `["myapex", "otherapex"]`,
10363 expectedError: "Stub libraries should have a single apex_available.*myapex.*otherapex",
10364 },
10365 {
10366 desc: "stub library can be available to a core apex and a test apex",
10367 hasStubs: true,
10368 apexAvailable: `["myapex", "test_myapex"]`,
10369 },
10370 }
10371 bpTemplate := `
10372 cc_library {
10373 name: "libfoo",
10374 %v
10375 apex_available: %v,
10376 }
10377 apex {
10378 name: "myapex",
10379 key: "apex.key",
10380 updatable: false,
10381 native_shared_libs: ["libfoo"],
10382 }
10383 apex {
10384 name: "otherapex",
10385 key: "apex.key",
10386 updatable: false,
10387 }
10388 apex_test {
10389 name: "test_myapex",
10390 key: "apex.key",
10391 updatable: false,
10392 native_shared_libs: ["libfoo"],
10393 }
10394 apex_key {
10395 name: "apex.key",
10396 }
10397 `
10398 for _, tc := range testCases {
10399 stubs := ""
10400 if tc.hasStubs {
10401 stubs = `stubs: {symbol_file: "libfoo.map.txt"},`
10402 }
10403 bp := fmt.Sprintf(bpTemplate, stubs, tc.apexAvailable)
10404 mockFsFixturePreparer := android.FixtureModifyMockFS(func(fs android.MockFS) {
10405 fs["system/sepolicy/apex/test_myapex-file_contexts"] = nil
10406 })
10407 if tc.expectedError == "" {
10408 testApex(t, bp, mockFsFixturePreparer)
10409 } else {
10410 testApexError(t, tc.expectedError, bp, mockFsFixturePreparer)
10411 }
10412 }
10413}
Colin Crossbd3a16b2023-04-25 11:30:51 -070010414
10415func TestFileSystemShouldSkipApexLibraries(t *testing.T) {
10416 context := android.GroupFixturePreparers(
10417 android.PrepareForIntegrationTestWithAndroid,
10418 cc.PrepareForIntegrationTestWithCc,
10419 PrepareForTestWithApexBuildComponents,
10420 prepareForTestWithMyapex,
10421 filesystem.PrepareForTestWithFilesystemBuildComponents,
10422 )
10423 result := context.RunTestWithBp(t, `
10424 android_system_image {
10425 name: "myfilesystem",
10426 deps: [
10427 "libfoo",
10428 ],
10429 linker_config_src: "linker.config.json",
10430 }
10431
10432 cc_library {
10433 name: "libfoo",
10434 shared_libs: [
10435 "libbar",
10436 ],
10437 stl: "none",
10438 }
10439
10440 cc_library {
10441 name: "libbar",
10442 stl: "none",
10443 apex_available: ["myapex"],
10444 }
10445
10446 apex {
10447 name: "myapex",
10448 native_shared_libs: ["libbar"],
10449 key: "myapex.key",
10450 updatable: false,
10451 }
10452
10453 apex_key {
10454 name: "myapex.key",
10455 public_key: "testkey.avbpubkey",
10456 private_key: "testkey.pem",
10457 }
10458 `)
10459
Cole Faust3b806d32024-03-11 15:15:03 -070010460 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("myfilesystem.img").Implicits
Colin Crossbd3a16b2023-04-25 11:30:51 -070010461 android.AssertStringListDoesNotContain(t, "filesystem should not have libbar",
10462 inputs.Strings(),
10463 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
10464}
Yu Liueae7b362023-11-16 17:05:47 -080010465
10466var apex_default_bp = `
10467 apex_key {
10468 name: "myapex.key",
10469 public_key: "testkey.avbpubkey",
10470 private_key: "testkey.pem",
10471 }
10472
10473 filegroup {
10474 name: "myapex.manifest",
10475 srcs: ["apex_manifest.json"],
10476 }
10477
10478 filegroup {
10479 name: "myapex.androidmanifest",
10480 srcs: ["AndroidManifest.xml"],
10481 }
10482`
10483
10484func TestAconfigFilesJavaDeps(t *testing.T) {
10485 ctx := testApex(t, apex_default_bp+`
10486 apex {
10487 name: "myapex",
10488 manifest: ":myapex.manifest",
10489 androidManifest: ":myapex.androidmanifest",
10490 key: "myapex.key",
10491 java_libs: [
10492 "my_java_library_foo",
10493 "my_java_library_bar",
10494 ],
10495 updatable: false,
10496 }
10497
10498 java_library {
10499 name: "my_java_library_foo",
10500 srcs: ["foo/bar/MyClass.java"],
10501 sdk_version: "none",
10502 system_modules: "none",
10503 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010504 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010505 "myapex",
10506 ],
10507 }
10508
10509 java_library {
10510 name: "my_java_library_bar",
10511 srcs: ["foo/bar/MyClass.java"],
10512 sdk_version: "none",
10513 system_modules: "none",
10514 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010515 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010516 "myapex",
10517 ],
10518 }
10519
10520 aconfig_declarations {
10521 name: "my_aconfig_declarations_foo",
10522 package: "com.example.package",
10523 container: "myapex",
10524 srcs: ["foo.aconfig"],
10525 }
10526
10527 java_aconfig_library {
10528 name: "my_java_aconfig_library_foo",
10529 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010530 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010531 "myapex",
10532 ],
10533 }
10534
10535 aconfig_declarations {
10536 name: "my_aconfig_declarations_bar",
10537 package: "com.example.package",
10538 container: "myapex",
10539 srcs: ["bar.aconfig"],
10540 }
10541
10542 java_aconfig_library {
10543 name: "my_java_aconfig_library_bar",
10544 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010545 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010546 "myapex",
10547 ],
10548 }
10549 `)
10550
10551 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10552 s := mod.Rule("apexRule").Args["copy_commands"]
10553 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Yu Liubba555e2024-02-17 00:36:42 +000010554 if len(copyCmds) != 8 {
Yu Liueae7b362023-11-16 17:05:47 -080010555 t.Fatalf("Expected 5 commands, got %d in:\n%s", len(copyCmds), s)
10556 }
10557
Yu Liuab31c822024-02-28 22:21:31 +000010558 ensureMatches(t, copyCmds[4], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10559 ensureMatches(t, copyCmds[5], "^cp -f .*/package.map .*/image.apex/etc$")
10560 ensureMatches(t, copyCmds[6], "^cp -f .*/flag.map .*/image.apex/etc$")
10561 ensureMatches(t, copyCmds[7], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liueae7b362023-11-16 17:05:47 -080010562
Yu Liubba555e2024-02-17 00:36:42 +000010563 inputs := []string{
10564 "my_aconfig_declarations_foo/intermediate.pb",
10565 "my_aconfig_declarations_bar/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010566 }
Yu Liubba555e2024-02-17 00:36:42 +000010567 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10568 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10569 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10570 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010571}
10572
10573func TestAconfigFilesJavaAndCcDeps(t *testing.T) {
10574 ctx := testApex(t, apex_default_bp+`
10575 apex {
10576 name: "myapex",
10577 manifest: ":myapex.manifest",
10578 androidManifest: ":myapex.androidmanifest",
10579 key: "myapex.key",
10580 java_libs: [
10581 "my_java_library_foo",
10582 ],
10583 native_shared_libs: [
10584 "my_cc_library_bar",
10585 ],
10586 binaries: [
10587 "my_cc_binary_baz",
10588 ],
10589 updatable: false,
10590 }
10591
10592 java_library {
10593 name: "my_java_library_foo",
10594 srcs: ["foo/bar/MyClass.java"],
10595 sdk_version: "none",
10596 system_modules: "none",
10597 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010598 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010599 "myapex",
10600 ],
10601 }
10602
10603 cc_library {
10604 name: "my_cc_library_bar",
10605 srcs: ["foo/bar/MyClass.cc"],
Yu Liucec0e412023-11-30 16:45:50 -080010606 static_libs: [
10607 "my_cc_aconfig_library_bar",
10608 "my_cc_aconfig_library_baz",
10609 ],
Yu Liueae7b362023-11-16 17:05:47 -080010610 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010611 "myapex",
10612 ],
10613 }
10614
10615 cc_binary {
10616 name: "my_cc_binary_baz",
10617 srcs: ["foo/bar/MyClass.cc"],
10618 static_libs: ["my_cc_aconfig_library_baz"],
Yu Liueae7b362023-11-16 17:05:47 -080010619 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010620 "myapex",
10621 ],
10622 }
10623
10624 aconfig_declarations {
10625 name: "my_aconfig_declarations_foo",
10626 package: "com.example.package",
10627 container: "myapex",
10628 srcs: ["foo.aconfig"],
10629 }
10630
10631 java_aconfig_library {
10632 name: "my_java_aconfig_library_foo",
10633 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010634 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010635 "myapex",
10636 ],
10637 }
10638
10639 aconfig_declarations {
10640 name: "my_aconfig_declarations_bar",
10641 package: "com.example.package",
10642 container: "myapex",
10643 srcs: ["bar.aconfig"],
10644 }
10645
10646 cc_aconfig_library {
10647 name: "my_cc_aconfig_library_bar",
10648 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010649 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010650 "myapex",
10651 ],
10652 }
10653
10654 aconfig_declarations {
10655 name: "my_aconfig_declarations_baz",
10656 package: "com.example.package",
10657 container: "myapex",
10658 srcs: ["baz.aconfig"],
10659 }
10660
10661 cc_aconfig_library {
10662 name: "my_cc_aconfig_library_baz",
10663 aconfig_declarations: "my_aconfig_declarations_baz",
Yu Liueae7b362023-11-16 17:05:47 -080010664 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010665 "myapex",
10666 ],
10667 }
10668
10669 cc_library {
10670 name: "server_configurable_flags",
10671 srcs: ["server_configurable_flags.cc"],
10672 }
Ted Bauerf0f18592024-04-23 18:25:26 +000010673 cc_library {
10674 name: "libbase",
10675 srcs: ["libbase.cc"],
Ted Bauer1e96f8c2024-04-25 19:50:01 +000010676 apex_available: [
10677 "myapex",
10678 ],
Ted Bauerf0f18592024-04-23 18:25:26 +000010679 }
10680 cc_library {
10681 name: "libaconfig_storage_read_api_cc",
10682 srcs: ["libaconfig_storage_read_api_cc.cc"],
10683 }
10684 cc_library {
10685 name: "libaconfig_storage_protos_cc",
10686 srcs: ["libaconfig_storage_protos_cc.cc"],
10687 }
Yu Liueae7b362023-11-16 17:05:47 -080010688 `)
10689
10690 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10691 s := mod.Rule("apexRule").Args["copy_commands"]
10692 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Yu Liubba555e2024-02-17 00:36:42 +000010693 if len(copyCmds) != 12 {
10694 t.Fatalf("Expected 12 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liueae7b362023-11-16 17:05:47 -080010695 }
10696
Yu Liuab31c822024-02-28 22:21:31 +000010697 ensureMatches(t, copyCmds[8], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10698 ensureMatches(t, copyCmds[9], "^cp -f .*/package.map .*/image.apex/etc$")
10699 ensureMatches(t, copyCmds[10], "^cp -f .*/flag.map .*/image.apex/etc$")
10700 ensureMatches(t, copyCmds[11], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liueae7b362023-11-16 17:05:47 -080010701
Yu Liubba555e2024-02-17 00:36:42 +000010702 inputs := []string{
10703 "my_aconfig_declarations_foo/intermediate.pb",
10704 "my_cc_library_bar/android_arm64_armv8-a_shared_apex10000/myapex/aconfig_merged.pb",
10705 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liueae7b362023-11-16 17:05:47 -080010706 }
Yu Liubba555e2024-02-17 00:36:42 +000010707 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10708 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10709 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10710 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
Yu Liueae7b362023-11-16 17:05:47 -080010711}
10712
Yu Liucec0e412023-11-30 16:45:50 -080010713func TestAconfigFilesRustDeps(t *testing.T) {
10714 ctx := testApex(t, apex_default_bp+`
10715 apex {
10716 name: "myapex",
10717 manifest: ":myapex.manifest",
10718 androidManifest: ":myapex.androidmanifest",
10719 key: "myapex.key",
10720 native_shared_libs: [
10721 "libmy_rust_library",
10722 ],
10723 binaries: [
10724 "my_rust_binary",
10725 ],
10726 rust_dyn_libs: [
10727 "libmy_rust_dylib",
10728 ],
10729 updatable: false,
10730 }
10731
10732 rust_library {
10733 name: "libflags_rust", // test mock
10734 crate_name: "flags_rust",
10735 srcs: ["lib.rs"],
10736 apex_available: [
10737 "myapex",
10738 ],
10739 }
10740
10741 rust_library {
10742 name: "liblazy_static", // test mock
10743 crate_name: "lazy_static",
10744 srcs: ["src/lib.rs"],
10745 apex_available: [
10746 "myapex",
10747 ],
10748 }
10749
Ted Bauer02d475c2024-03-27 20:56:26 +000010750 rust_library {
10751 name: "libaconfig_storage_read_api", // test mock
10752 crate_name: "aconfig_storage_read_api",
10753 srcs: ["src/lib.rs"],
10754 apex_available: [
10755 "myapex",
10756 ],
10757 }
10758
Ted Bauer6ef40db2024-03-29 14:04:10 +000010759 rust_library {
10760 name: "liblogger", // test mock
10761 crate_name: "logger",
10762 srcs: ["src/lib.rs"],
10763 apex_available: [
10764 "myapex",
10765 ],
10766 }
10767
10768 rust_library {
10769 name: "liblog_rust", // test mock
10770 crate_name: "log_rust",
10771 srcs: ["src/lib.rs"],
10772 apex_available: [
10773 "myapex",
10774 ],
10775 }
10776
Yu Liucec0e412023-11-30 16:45:50 -080010777 rust_ffi_shared {
10778 name: "libmy_rust_library",
10779 srcs: ["src/lib.rs"],
10780 rustlibs: ["libmy_rust_aconfig_library_foo"],
10781 crate_name: "my_rust_library",
10782 apex_available: [
10783 "myapex",
10784 ],
10785 }
10786
10787 rust_library_dylib {
10788 name: "libmy_rust_dylib",
10789 srcs: ["foo/bar/MyClass.rs"],
10790 rustlibs: ["libmy_rust_aconfig_library_bar"],
10791 crate_name: "my_rust_dylib",
10792 apex_available: [
10793 "myapex",
10794 ],
10795 }
10796
10797 rust_binary {
10798 name: "my_rust_binary",
10799 srcs: ["foo/bar/MyClass.rs"],
10800 rustlibs: [
10801 "libmy_rust_aconfig_library_baz",
10802 "libmy_rust_dylib",
10803 ],
10804 apex_available: [
10805 "myapex",
10806 ],
10807 }
10808
10809 aconfig_declarations {
10810 name: "my_aconfig_declarations_foo",
10811 package: "com.example.package",
10812 container: "myapex",
10813 srcs: ["foo.aconfig"],
10814 }
10815
10816 aconfig_declarations {
10817 name: "my_aconfig_declarations_bar",
10818 package: "com.example.package",
10819 container: "myapex",
10820 srcs: ["bar.aconfig"],
10821 }
10822
10823 aconfig_declarations {
10824 name: "my_aconfig_declarations_baz",
10825 package: "com.example.package",
10826 container: "myapex",
10827 srcs: ["baz.aconfig"],
10828 }
10829
10830 rust_aconfig_library {
10831 name: "libmy_rust_aconfig_library_foo",
10832 aconfig_declarations: "my_aconfig_declarations_foo",
10833 crate_name: "my_rust_aconfig_library_foo",
10834 apex_available: [
10835 "myapex",
10836 ],
10837 }
10838
10839 rust_aconfig_library {
10840 name: "libmy_rust_aconfig_library_bar",
10841 aconfig_declarations: "my_aconfig_declarations_bar",
10842 crate_name: "my_rust_aconfig_library_bar",
10843 apex_available: [
10844 "myapex",
10845 ],
10846 }
10847
10848 rust_aconfig_library {
10849 name: "libmy_rust_aconfig_library_baz",
10850 aconfig_declarations: "my_aconfig_declarations_baz",
10851 crate_name: "my_rust_aconfig_library_baz",
10852 apex_available: [
10853 "myapex",
10854 ],
10855 }
10856 `)
10857
10858 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10859 s := mod.Rule("apexRule").Args["copy_commands"]
10860 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Ted Bauer6ef40db2024-03-29 14:04:10 +000010861 if len(copyCmds) != 32 {
Ted Bauer02d475c2024-03-27 20:56:26 +000010862 t.Fatalf("Expected 28 commands, got %d in:\n%s", len(copyCmds), s)
Yu Liucec0e412023-11-30 16:45:50 -080010863 }
10864
Ted Bauer6ef40db2024-03-29 14:04:10 +000010865 ensureMatches(t, copyCmds[28], "^cp -f .*/aconfig_flags.pb .*/image.apex/etc$")
10866 ensureMatches(t, copyCmds[29], "^cp -f .*/package.map .*/image.apex/etc$")
10867 ensureMatches(t, copyCmds[30], "^cp -f .*/flag.map .*/image.apex/etc$")
10868 ensureMatches(t, copyCmds[31], "^cp -f .*/flag.val .*/image.apex/etc$")
Yu Liucec0e412023-11-30 16:45:50 -080010869
Yu Liubba555e2024-02-17 00:36:42 +000010870 inputs := []string{
10871 "my_aconfig_declarations_foo/intermediate.pb",
Yu Liuab31c822024-02-28 22:21:31 +000010872 "my_aconfig_declarations_bar/intermediate.pb",
10873 "my_aconfig_declarations_baz/intermediate.pb",
Yu Liubba555e2024-02-17 00:36:42 +000010874 "my_rust_binary/android_arm64_armv8-a_apex10000/myapex/aconfig_merged.pb",
10875 }
10876 VerifyAconfigRule(t, &mod, "combine_aconfig_declarations", inputs, "android_common_myapex/aconfig_flags.pb", "", "")
10877 VerifyAconfigRule(t, &mod, "create_aconfig_package_map_file", inputs, "android_common_myapex/package.map", "myapex", "package_map")
10878 VerifyAconfigRule(t, &mod, "create_aconfig_flag_map_file", inputs, "android_common_myapex/flag.map", "myapex", "flag_map")
10879 VerifyAconfigRule(t, &mod, "create_aconfig_flag_val_file", inputs, "android_common_myapex/flag.val", "myapex", "flag_val")
10880}
10881
10882func VerifyAconfigRule(t *testing.T, mod *android.TestingModule, desc string, inputs []string, output string, container string, file_type string) {
10883 aconfigRule := mod.Description(desc)
10884 s := " " + aconfigRule.Args["cache_files"]
Yu Liucec0e412023-11-30 16:45:50 -080010885 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
Yu Liubba555e2024-02-17 00:36:42 +000010886 if len(aconfigArgs) != len(inputs) {
10887 t.Fatalf("Expected %d commands, got %d in:\n%s", len(inputs), len(aconfigArgs), s)
Yu Liucec0e412023-11-30 16:45:50 -080010888 }
Yu Liucec0e412023-11-30 16:45:50 -080010889
Yu Liubba555e2024-02-17 00:36:42 +000010890 ensureEquals(t, container, aconfigRule.Args["container"])
10891 ensureEquals(t, file_type, aconfigRule.Args["file_type"])
10892
10893 buildParams := aconfigRule.BuildParams
10894 for _, input := range inputs {
10895 android.EnsureListContainsSuffix(t, aconfigArgs, input)
10896 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), input)
Yu Liucec0e412023-11-30 16:45:50 -080010897 }
Yu Liubba555e2024-02-17 00:36:42 +000010898
10899 ensureContains(t, buildParams.Output.String(), output)
Yu Liucec0e412023-11-30 16:45:50 -080010900}
10901
Yu Liueae7b362023-11-16 17:05:47 -080010902func TestAconfigFilesOnlyMatchCurrentApex(t *testing.T) {
10903 ctx := testApex(t, apex_default_bp+`
10904 apex {
10905 name: "myapex",
10906 manifest: ":myapex.manifest",
10907 androidManifest: ":myapex.androidmanifest",
10908 key: "myapex.key",
10909 java_libs: [
10910 "my_java_library_foo",
10911 "other_java_library_bar",
10912 ],
10913 updatable: false,
10914 }
10915
10916 java_library {
10917 name: "my_java_library_foo",
10918 srcs: ["foo/bar/MyClass.java"],
10919 sdk_version: "none",
10920 system_modules: "none",
10921 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010922 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010923 "myapex",
10924 ],
10925 }
10926
10927 java_library {
10928 name: "other_java_library_bar",
10929 srcs: ["foo/bar/MyClass.java"],
10930 sdk_version: "none",
10931 system_modules: "none",
10932 static_libs: ["other_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010933 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010934 "myapex",
10935 ],
10936 }
10937
10938 aconfig_declarations {
10939 name: "my_aconfig_declarations_foo",
10940 package: "com.example.package",
10941 container: "myapex",
10942 srcs: ["foo.aconfig"],
10943 }
10944
10945 java_aconfig_library {
10946 name: "my_java_aconfig_library_foo",
10947 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010948 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010949 "myapex",
10950 ],
10951 }
10952
10953 aconfig_declarations {
10954 name: "other_aconfig_declarations_bar",
10955 package: "com.example.package",
10956 container: "otherapex",
10957 srcs: ["bar.aconfig"],
10958 }
10959
10960 java_aconfig_library {
10961 name: "other_java_aconfig_library_bar",
10962 aconfig_declarations: "other_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010963 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010964 "myapex",
10965 ],
10966 }
10967 `)
10968
10969 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10970 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
10971 s := " " + combineAconfigRule.Args["cache_files"]
10972 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
10973 if len(aconfigArgs) != 1 {
10974 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
10975 }
10976 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
10977
10978 buildParams := combineAconfigRule.BuildParams
10979 if len(buildParams.Inputs) != 1 {
10980 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
10981 }
10982 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
10983 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
10984}
10985
10986func TestAconfigFilesRemoveDuplicates(t *testing.T) {
10987 ctx := testApex(t, apex_default_bp+`
10988 apex {
10989 name: "myapex",
10990 manifest: ":myapex.manifest",
10991 androidManifest: ":myapex.androidmanifest",
10992 key: "myapex.key",
10993 java_libs: [
10994 "my_java_library_foo",
10995 "my_java_library_bar",
10996 ],
10997 updatable: false,
10998 }
10999
11000 java_library {
11001 name: "my_java_library_foo",
11002 srcs: ["foo/bar/MyClass.java"],
11003 sdk_version: "none",
11004 system_modules: "none",
11005 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080011006 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011007 "myapex",
11008 ],
11009 }
11010
11011 java_library {
11012 name: "my_java_library_bar",
11013 srcs: ["foo/bar/MyClass.java"],
11014 sdk_version: "none",
11015 system_modules: "none",
11016 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080011017 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011018 "myapex",
11019 ],
11020 }
11021
11022 aconfig_declarations {
11023 name: "my_aconfig_declarations_foo",
11024 package: "com.example.package",
11025 container: "myapex",
11026 srcs: ["foo.aconfig"],
11027 }
11028
11029 java_aconfig_library {
11030 name: "my_java_aconfig_library_foo",
11031 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011032 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011033 "myapex",
11034 ],
11035 }
11036
11037 java_aconfig_library {
11038 name: "my_java_aconfig_library_bar",
11039 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011040 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011041 "myapex",
11042 ],
11043 }
11044 `)
11045
11046 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11047 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11048 s := " " + combineAconfigRule.Args["cache_files"]
11049 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11050 if len(aconfigArgs) != 1 {
11051 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
11052 }
11053 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11054
11055 buildParams := combineAconfigRule.BuildParams
11056 if len(buildParams.Inputs) != 1 {
11057 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
11058 }
11059 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11060 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11061}
Spandan Das5be63332023-12-13 00:06:32 +000011062
11063// Test that the boot jars come from the _selected_ apex prebuilt
11064// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11065func TestBootDexJarsMultipleApexPrebuilts(t *testing.T) {
11066 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
11067 t.Helper()
11068 s := ctx.ModuleForTests("dex_bootjars", "android_common")
11069 foundLibfooJar := false
11070 base := stem + ".jar"
11071 for _, output := range s.AllOutputs() {
11072 if filepath.Base(output) == base {
11073 foundLibfooJar = true
11074 buildRule := s.Output(output)
11075 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
11076 }
11077 }
11078 if !foundLibfooJar {
11079 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
11080 }
11081 }
11082
Spandan Das64c9e0c2023-12-20 20:13:34 +000011083 // Check that the boot jars of the selected apex are run through boot_jars_package_check
11084 // This validates that the jars on the bootclasspath do not contain packages outside an allowlist
11085 checkBootJarsPackageCheck := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11086 platformBcp := ctx.ModuleForTests("platform-bootclasspath", "android_common")
11087 bootJarsCheckRule := platformBcp.Rule("boot_jars_package_check")
11088 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)
11089 }
11090
11091 // Check that the boot jars used to generate the monolithic hiddenapi flags come from the selected apex
11092 checkBootJarsForMonolithicHiddenapi := func(t *testing.T, ctx *android.TestContext, expectedBootJar string) {
11093 monolithicHiddenapiFlagsCmd := ctx.ModuleForTests("platform-bootclasspath", "android_common").Output("out/soong/hiddenapi/hiddenapi-stub-flags.txt").RuleParams.Command
11094 android.AssertStringMatches(t, "Could not find the correct boot dex jar in monolithic hiddenapi flags generation command", monolithicHiddenapiFlagsCmd, "--boot-dex="+expectedBootJar)
11095 }
11096
Spandan Das5be63332023-12-13 00:06:32 +000011097 bp := `
11098 // Source APEX.
11099
11100 java_library {
11101 name: "framework-foo",
11102 srcs: ["foo.java"],
11103 installable: true,
11104 apex_available: [
11105 "com.android.foo",
11106 ],
11107 }
11108
11109 bootclasspath_fragment {
11110 name: "foo-bootclasspath-fragment",
11111 contents: ["framework-foo"],
11112 apex_available: [
11113 "com.android.foo",
11114 ],
11115 hidden_api: {
11116 split_packages: ["*"],
11117 },
11118 }
11119
11120 apex_key {
11121 name: "com.android.foo.key",
11122 public_key: "com.android.foo.avbpubkey",
11123 private_key: "com.android.foo.pem",
11124 }
11125
11126 apex {
11127 name: "com.android.foo",
11128 key: "com.android.foo.key",
11129 bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11130 updatable: false,
11131 }
11132
11133 // Prebuilt APEX.
11134
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011135 java_sdk_library_import {
Spandan Das5be63332023-12-13 00:06:32 +000011136 name: "framework-foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011137 public: {
11138 jars: ["foo.jar"],
11139 },
Spandan Das5be63332023-12-13 00:06:32 +000011140 apex_available: ["com.android.foo"],
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011141 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +000011142 }
11143
11144 prebuilt_bootclasspath_fragment {
11145 name: "foo-bootclasspath-fragment",
11146 contents: ["framework-foo"],
11147 hidden_api: {
11148 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
11149 metadata: "my-bootclasspath-fragment/metadata.csv",
11150 index: "my-bootclasspath-fragment/index.csv",
11151 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
11152 all_flags: "my-bootclasspath-fragment/all-flags.csv",
11153 },
11154 apex_available: [
11155 "com.android.foo",
11156 ],
11157 }
11158
11159 prebuilt_apex {
11160 name: "com.android.foo",
11161 apex_name: "com.android.foo",
11162 src: "com.android.foo-arm.apex",
11163 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11164 }
11165
11166 // Another Prebuilt ART APEX
11167 prebuilt_apex {
11168 name: "com.android.foo.v2",
11169 apex_name: "com.android.foo", // Used to determine the API domain
11170 src: "com.android.foo-arm.apex",
11171 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11172 }
11173
11174 // APEX contribution modules
11175
11176 apex_contributions {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011177 name: "foo.source.contributions",
Spandan Das5be63332023-12-13 00:06:32 +000011178 api_domain: "com.android.foo",
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011179 contents: ["com.android.foo"],
11180 }
11181
11182 apex_contributions {
11183 name: "foo.prebuilt.contributions",
11184 api_domain: "com.android.foo",
11185 contents: ["prebuilt_com.android.foo"],
11186 }
11187
11188 apex_contributions {
11189 name: "foo.prebuilt.v2.contributions",
11190 api_domain: "com.android.foo",
11191 contents: ["com.android.foo.v2"], // prebuilt_ prefix is missing because of prebuilt_rename mutator
Spandan Das5be63332023-12-13 00:06:32 +000011192 }
11193 `
11194
11195 testCases := []struct {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011196 desc string
11197 selectedApexContributions string
11198 expectedBootJar string
Spandan Das5be63332023-12-13 00:06:32 +000011199 }{
11200 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011201 desc: "Source apex com.android.foo is selected, bootjar should come from source java library",
11202 selectedApexContributions: "foo.source.contributions",
11203 expectedBootJar: "out/soong/.intermediates/foo-bootclasspath-fragment/android_common_apex10000/hiddenapi-modular/encoded/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011204 },
11205 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011206 desc: "Prebuilt apex prebuilt_com.android.foo is selected, profile should come from .prof deapexed from the prebuilt",
11207 selectedApexContributions: "foo.prebuilt.contributions",
11208 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011209 },
11210 {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011211 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected, profile should come from .prof deapexed from the prebuilt",
11212 selectedApexContributions: "foo.prebuilt.v2.contributions",
11213 expectedBootJar: "out/soong/.intermediates/prebuilt_com.android.foo.v2.deapexer/android_common/deapexer/javalib/framework-foo.jar",
Spandan Das5be63332023-12-13 00:06:32 +000011214 },
11215 }
11216
11217 fragment := java.ApexVariantReference{
11218 Apex: proptools.StringPtr("com.android.foo"),
11219 Module: proptools.StringPtr("foo-bootclasspath-fragment"),
11220 }
11221
11222 for _, tc := range testCases {
11223 preparer := android.GroupFixturePreparers(
11224 java.FixtureConfigureApexBootJars("com.android.foo:framework-foo"),
11225 android.FixtureMergeMockFs(map[string][]byte{
11226 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11227 }),
11228 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
11229 variables.BuildFlags = map[string]string{
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011230 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": tc.selectedApexContributions,
Spandan Das5be63332023-12-13 00:06:32 +000011231 }
11232 }),
11233 )
Spandan Dasb2fd4ff2024-01-25 04:25:38 +000011234 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Spandan Das5be63332023-12-13 00:06:32 +000011235 checkBootDexJarPath(t, ctx, "framework-foo", tc.expectedBootJar)
Spandan Das64c9e0c2023-12-20 20:13:34 +000011236 checkBootJarsPackageCheck(t, ctx, tc.expectedBootJar)
11237 checkBootJarsForMonolithicHiddenapi(t, ctx, tc.expectedBootJar)
Spandan Das5be63332023-12-13 00:06:32 +000011238 }
11239}
Spandan Das3576e762024-01-03 18:57:03 +000011240
11241// Test that product packaging installs the selected mainline module (either source or a specific prebuilt)
11242// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11243func TestInstallationRulesForMultipleApexPrebuilts(t *testing.T) {
11244 // check that the LOCAL_MODULE in the generated mk file matches the name used in PRODUCT_PACKAGES
11245 // Since the name used in PRODUCT_PACKAGES does not contain prebuilt_ prefix, LOCAL_MODULE should not contain any prefix either
11246 checkLocalModuleName := func(t *testing.T, ctx *android.TestContext, soongApexModuleName string, expectedLocalModuleName string) {
11247 // Variations are created based on apex_name
11248 entries := android.AndroidMkEntriesForTest(t, ctx, ctx.ModuleForTests(soongApexModuleName, "android_common_com.android.foo").Module())
11249 android.AssertStringEquals(t, "LOCAL_MODULE of the prebuilt apex must match the name listed in PRODUCT_PACKAGES", expectedLocalModuleName, entries[0].EntryMap["LOCAL_MODULE"][0])
11250 }
11251 // for a mainline module family, check that only the flagged soong module is visible to make
11252 checkHideFromMake := func(t *testing.T, ctx *android.TestContext, visibleModuleName string, hiddenModuleNames []string) {
11253 variation := func(moduleName string) string {
11254 ret := "android_common_com.android.foo"
11255 if moduleName == "com.google.android.foo" {
11256 ret = "android_common_com.google.android.foo_com.android.foo"
11257 }
11258 return ret
11259 }
11260
11261 visibleModule := ctx.ModuleForTests(visibleModuleName, variation(visibleModuleName)).Module()
11262 android.AssertBoolEquals(t, "Apex "+visibleModuleName+" selected using apex_contributions should be visible to make", false, visibleModule.IsHideFromMake())
11263
11264 for _, hiddenModuleName := range hiddenModuleNames {
11265 hiddenModule := ctx.ModuleForTests(hiddenModuleName, variation(hiddenModuleName)).Module()
11266 android.AssertBoolEquals(t, "Apex "+hiddenModuleName+" not selected using apex_contributions should be hidden from make", true, hiddenModule.IsHideFromMake())
11267
11268 }
11269 }
11270
11271 bp := `
11272 apex_key {
11273 name: "com.android.foo.key",
11274 public_key: "com.android.foo.avbpubkey",
11275 private_key: "com.android.foo.pem",
11276 }
11277
11278 // AOSP source apex
11279 apex {
11280 name: "com.android.foo",
11281 key: "com.android.foo.key",
11282 updatable: false,
11283 }
11284
11285 // Google source apex
11286 override_apex {
11287 name: "com.google.android.foo",
11288 base: "com.android.foo",
11289 key: "com.android.foo.key",
11290 }
11291
11292 // Prebuilt Google APEX.
11293
11294 prebuilt_apex {
11295 name: "com.google.android.foo",
11296 apex_name: "com.android.foo",
11297 src: "com.android.foo-arm.apex",
11298 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11299 }
11300
11301 // Another Prebuilt Google APEX
11302 prebuilt_apex {
11303 name: "com.google.android.foo.v2",
11304 apex_name: "com.android.foo",
11305 source_apex_name: "com.google.android.foo", // source_apex_name becomes LOCAL_MODULE in the generated mk file
11306 src: "com.android.foo-arm.apex",
11307 prefer: true, // prefer is set to true on both the prebuilts to induce an error if flagging is not present
11308 }
11309
11310 // APEX contribution modules
11311
11312 apex_contributions {
11313 name: "foo.source.contributions",
11314 api_domain: "com.android.foo",
11315 contents: ["com.google.android.foo"],
11316 }
11317
11318 apex_contributions {
11319 name: "foo.prebuilt.contributions",
11320 api_domain: "com.android.foo",
11321 contents: ["prebuilt_com.google.android.foo"],
11322 }
11323
11324 apex_contributions {
11325 name: "foo.prebuilt.v2.contributions",
11326 api_domain: "com.android.foo",
11327 contents: ["prebuilt_com.google.android.foo.v2"],
11328 }
11329
11330 // This is an incompatible module because it selects multiple versions of the same mainline module
11331 apex_contributions {
11332 name: "foo.prebuilt.duplicate.contributions",
11333 api_domain: "com.android.foo",
11334 contents: [
11335 "prebuilt_com.google.android.foo",
11336 "prebuilt_com.google.android.foo.v2",
11337 ],
11338 }
11339 `
11340
11341 testCases := []struct {
11342 desc string
11343 selectedApexContributions string
11344 expectedVisibleModuleName string
11345 expectedHiddenModuleNames []string
11346 expectedError string
11347 }{
11348 {
11349 desc: "Source apex is selected, prebuilts should be hidden from make",
11350 selectedApexContributions: "foo.source.contributions",
11351 expectedVisibleModuleName: "com.google.android.foo",
11352 expectedHiddenModuleNames: []string{"prebuilt_com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11353 },
11354 {
11355 desc: "Prebuilt apex prebuilt_com.android.foo is selected, source and the other prebuilt should be hidden from make",
11356 selectedApexContributions: "foo.prebuilt.contributions",
11357 expectedVisibleModuleName: "prebuilt_com.google.android.foo",
11358 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo.v2"},
11359 },
11360 {
11361 desc: "Prebuilt apex prebuilt_com.android.fooi.v2 is selected, source and the other prebuilt should be hidden from make",
11362 selectedApexContributions: "foo.prebuilt.v2.contributions",
11363 expectedVisibleModuleName: "prebuilt_com.google.android.foo.v2",
11364 expectedHiddenModuleNames: []string{"com.google.android.foo", "prebuilt_com.google.android.foo"},
11365 },
11366 {
11367 desc: "Multiple versions of a prebuilt apex is selected in the same release config",
11368 selectedApexContributions: "foo.prebuilt.duplicate.contributions",
11369 expectedError: "Found duplicate variations of the same module in apex_contributions: prebuilt_com.google.android.foo and prebuilt_com.google.android.foo.v2",
11370 },
11371 }
11372
11373 for _, tc := range testCases {
11374 preparer := android.GroupFixturePreparers(
11375 android.FixtureMergeMockFs(map[string][]byte{
11376 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11377 }),
11378 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
11379 variables.BuildFlags = map[string]string{
11380 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": tc.selectedApexContributions,
11381 }
11382 }),
11383 )
11384 if tc.expectedError != "" {
11385 preparer = preparer.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(tc.expectedError))
11386 testApex(t, bp, preparer)
11387 return
11388 }
11389 ctx := testApex(t, bp, preparer)
11390
11391 // Check that the LOCAL_MODULE of the two prebuilts is com.android.foo
11392 // This ensures that product packaging can pick them for installation if it has been flagged by apex_contributions
11393 checkLocalModuleName(t, ctx, "prebuilt_com.google.android.foo", "com.google.android.foo")
11394 checkLocalModuleName(t, ctx, "prebuilt_com.google.android.foo.v2", "com.google.android.foo")
11395
11396 // Check that
11397 // 1. The contents of the selected apex_contributions are visible to make
11398 // 2. The rest of the apexes in the mainline module family (source or other prebuilt) is hidden from make
11399 checkHideFromMake(t, ctx, tc.expectedVisibleModuleName, tc.expectedHiddenModuleNames)
11400 }
11401}
Jihoon Kang3921f0b2024-03-12 23:51:37 +000011402
11403func TestAconfifDeclarationsValidation(t *testing.T) {
11404 aconfigDeclarationLibraryString := func(moduleNames []string) (ret string) {
11405 for _, moduleName := range moduleNames {
11406 ret += fmt.Sprintf(`
11407 aconfig_declarations {
11408 name: "%[1]s",
11409 package: "com.example.package",
11410 srcs: [
11411 "%[1]s.aconfig",
11412 ],
11413 }
11414 java_aconfig_library {
11415 name: "%[1]s-lib",
11416 aconfig_declarations: "%[1]s",
11417 }
11418 `, moduleName)
11419 }
11420 return ret
11421 }
11422
11423 result := android.GroupFixturePreparers(
11424 prepareForApexTest,
11425 java.PrepareForTestWithJavaSdkLibraryFiles,
11426 java.FixtureWithLastReleaseApis("foo"),
11427 android.FixtureModifyConfig(func(config android.Config) {
11428 config.SetApiLibraries([]string{"foo"})
11429 }),
11430 ).RunTestWithBp(t, `
11431 java_library {
11432 name: "baz-java-lib",
11433 static_libs: [
11434 "baz-lib",
11435 ],
11436 }
11437 filegroup {
11438 name: "qux-filegroup",
11439 srcs: [
11440 ":qux-lib{.generated_srcjars}",
11441 ],
11442 }
11443 filegroup {
11444 name: "qux-another-filegroup",
11445 srcs: [
11446 ":qux-filegroup",
11447 ],
11448 }
11449 java_library {
11450 name: "quux-java-lib",
11451 srcs: [
11452 "a.java",
11453 ],
11454 libs: [
11455 "quux-lib",
11456 ],
11457 }
11458 java_sdk_library {
11459 name: "foo",
11460 srcs: [
11461 ":qux-another-filegroup",
11462 ],
11463 api_packages: ["foo"],
11464 system: {
11465 enabled: true,
11466 },
11467 module_lib: {
11468 enabled: true,
11469 },
11470 test: {
11471 enabled: true,
11472 },
11473 static_libs: [
11474 "bar-lib",
11475 ],
11476 libs: [
11477 "baz-java-lib",
11478 "quux-java-lib",
11479 ],
11480 aconfig_declarations: [
11481 "bar",
11482 ],
11483 }
11484 `+aconfigDeclarationLibraryString([]string{"bar", "baz", "qux", "quux"}))
11485
11486 m := result.ModuleForTests("foo.stubs.source", "android_common")
11487 outDir := "out/soong/.intermediates"
11488
11489 // Arguments passed to aconfig to retrieve the state of the flags defined in the
11490 // textproto files
11491 aconfigFlagArgs := m.Output("released-flagged-apis-exportable.txt").Args["flags_path"]
11492
11493 // "bar-lib" is a static_lib of "foo" and is passed to metalava as classpath. Thus the
11494 // cache file provided by the associated aconfig_declarations module "bar" should be passed
11495 // to aconfig.
11496 android.AssertStringDoesContain(t, "cache file of a java_aconfig_library static_lib "+
11497 "passed as an input",
11498 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "bar"))
11499
11500 // "baz-java-lib", which statically depends on "baz-lib", is a lib of "foo" and is passed
11501 // to metalava as classpath. Thus the cache file provided by the associated
11502 // aconfig_declarations module "baz" should be passed to aconfig.
11503 android.AssertStringDoesContain(t, "cache file of a lib that statically depends on "+
11504 "java_aconfig_library passed as an input",
11505 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "baz"))
11506
11507 // "qux-lib" is passed to metalava as src via the filegroup, thus the cache file provided by
11508 // the associated aconfig_declarations module "qux" should be passed to aconfig.
11509 android.AssertStringDoesContain(t, "cache file of srcs java_aconfig_library passed as an "+
11510 "input",
11511 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "qux"))
11512
11513 // "quux-java-lib" is a lib of "foo" and is passed to metalava as classpath, but does not
11514 // statically depend on "quux-lib". Therefore, the cache file provided by the associated
11515 // aconfig_declarations module "quux" should not be passed to aconfig.
11516 android.AssertStringDoesNotContain(t, "cache file of a lib that does not statically "+
11517 "depend on java_aconfig_library not passed as an input",
11518 aconfigFlagArgs, fmt.Sprintf("%s/%s/intermediate.pb", outDir, "quux"))
11519}