blob: 616421af50d3a34181666a3f84ddaabcbb208c5d [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) {
221 variables.DeviceVndkVersion = proptools.StringPtr("current")
222 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
223 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
224 variables.Platform_sdk_codename = proptools.StringPtr("Q")
225 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000226 // "Tiramisu" needs to be in the next line for compatibility with soong code,
227 // not because of these tests specifically (it's not used by the tests)
228 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900229 variables.Platform_vndk_version = proptools.StringPtr("29")
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000230 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000231 }),
232)
233
Paul Duffin52bfaa42021-03-23 23:40:12 +0000234var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
235 "system/sepolicy/apex/myapex-file_contexts": nil,
236})
237
Jooyung Han643adc42020-02-27 13:50:06 +0900238// ensure that 'result' equals 'expected'
239func ensureEquals(t *testing.T, result string, expected string) {
240 t.Helper()
241 if result != expected {
242 t.Errorf("%q != %q", expected, result)
243 }
244}
245
Jiyong Park25fc6a92018-11-18 18:02:45 +0900246// ensure that 'result' contains 'expected'
247func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900248 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900249 if !strings.Contains(result, expected) {
250 t.Errorf("%q is not found in %q", expected, result)
251 }
252}
253
Liz Kammer5bd365f2020-05-27 15:15:11 -0700254// ensure that 'result' contains 'expected' exactly one time
255func ensureContainsOnce(t *testing.T, result string, expected string) {
256 t.Helper()
257 count := strings.Count(result, expected)
258 if count != 1 {
259 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
260 }
261}
262
Jiyong Park25fc6a92018-11-18 18:02:45 +0900263// ensures that 'result' does not contain 'notExpected'
264func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900265 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900266 if strings.Contains(result, notExpected) {
267 t.Errorf("%q is found in %q", notExpected, result)
268 }
269}
270
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700271func ensureMatches(t *testing.T, result string, expectedRex string) {
272 ok, err := regexp.MatchString(expectedRex, result)
273 if err != nil {
274 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
275 return
276 }
277 if !ok {
278 t.Errorf("%s does not match regular expession %s", result, expectedRex)
279 }
280}
281
Jiyong Park25fc6a92018-11-18 18:02:45 +0900282func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900283 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900284 if !android.InList(expected, result) {
285 t.Errorf("%q is not found in %v", expected, result)
286 }
287}
288
289func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900290 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900291 if android.InList(notExpected, result) {
292 t.Errorf("%q is found in %v", notExpected, result)
293 }
294}
295
Jooyung Hane1633032019-08-01 17:41:43 +0900296func ensureListEmpty(t *testing.T, result []string) {
297 t.Helper()
298 if len(result) > 0 {
299 t.Errorf("%q is expected to be empty", result)
300 }
301}
302
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000303func ensureListNotEmpty(t *testing.T, result []string) {
304 t.Helper()
305 if len(result) == 0 {
306 t.Errorf("%q is expected to be not empty", result)
307 }
308}
309
Jiyong Park25fc6a92018-11-18 18:02:45 +0900310// Minimal test
311func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800312 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900313 apex_defaults {
314 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900315 manifest: ":myapex.manifest",
316 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900317 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900318 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900319 native_shared_libs: [
320 "mylib",
321 "libfoo.ffi",
322 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900323 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800324 multilib: {
325 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900326 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800327 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900328 },
Jiyong Park77acec62020-06-01 21:39:15 +0900329 java_libs: [
330 "myjar",
331 "myjar_dex",
332 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000333 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900334 }
335
Jiyong Park30ca9372019-02-07 16:27:23 +0900336 apex {
337 name: "myapex",
338 defaults: ["myapex-defaults"],
339 }
340
Jiyong Park25fc6a92018-11-18 18:02:45 +0900341 apex_key {
342 name: "myapex.key",
343 public_key: "testkey.avbpubkey",
344 private_key: "testkey.pem",
345 }
346
Jiyong Park809bb722019-02-13 21:33:49 +0900347 filegroup {
348 name: "myapex.manifest",
349 srcs: ["apex_manifest.json"],
350 }
351
352 filegroup {
353 name: "myapex.androidmanifest",
354 srcs: ["AndroidManifest.xml"],
355 }
356
Jiyong Park25fc6a92018-11-18 18:02:45 +0900357 cc_library {
358 name: "mylib",
359 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900360 shared_libs: [
361 "mylib2",
362 "libbar.ffi",
363 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900364 system_shared_libs: [],
365 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000366 // TODO: remove //apex_available:platform
367 apex_available: [
368 "//apex_available:platform",
369 "myapex",
370 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900371 }
372
Alex Light3d673592019-01-18 14:37:31 -0800373 cc_binary {
374 name: "foo",
375 srcs: ["mylib.cpp"],
376 compile_multilib: "both",
377 multilib: {
378 lib32: {
379 suffix: "32",
380 },
381 lib64: {
382 suffix: "64",
383 },
384 },
385 symlinks: ["foo_link_"],
386 symlink_preferred_arch: true,
387 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800388 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700389 apex_available: [ "myapex", "com.android.gki.*" ],
390 }
391
Jiyong Park99644e92020-11-17 22:21:02 +0900392 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000393 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900394 srcs: ["foo.rs"],
395 rlibs: ["libfoo.rlib.rust"],
Vinh Tran4eeb2a92023-08-14 13:29:30 -0400396 rustlibs: ["libfoo.dylib.rust"],
Jiyong Park99644e92020-11-17 22:21:02 +0900397 apex_available: ["myapex"],
398 }
399
400 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000401 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900402 srcs: ["foo.rs"],
403 crate_name: "foo",
404 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900405 shared_libs: ["libfoo.shared_from_rust"],
406 }
407
408 cc_library_shared {
409 name: "libfoo.shared_from_rust",
410 srcs: ["mylib.cpp"],
411 system_shared_libs: [],
412 stl: "none",
413 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900414 }
415
416 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000417 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900418 srcs: ["foo.rs"],
419 crate_name: "foo",
420 apex_available: ["myapex"],
421 }
422
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900423 rust_ffi_shared {
424 name: "libfoo.ffi",
425 srcs: ["foo.rs"],
426 crate_name: "foo",
427 apex_available: ["myapex"],
428 }
429
430 rust_ffi_shared {
431 name: "libbar.ffi",
432 srcs: ["foo.rs"],
433 crate_name: "bar",
434 apex_available: ["myapex"],
435 }
436
Yifan Hongd22a84a2020-07-28 17:37:46 -0700437 apex {
438 name: "com.android.gki.fake",
439 binaries: ["foo"],
440 key: "myapex.key",
441 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000442 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800443 }
444
Paul Duffindddd5462020-04-07 15:25:44 +0100445 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900446 name: "mylib2",
447 srcs: ["mylib.cpp"],
448 system_shared_libs: [],
449 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900450 static_libs: ["libstatic"],
451 // TODO: remove //apex_available:platform
452 apex_available: [
453 "//apex_available:platform",
454 "myapex",
455 ],
456 }
457
Paul Duffindddd5462020-04-07 15:25:44 +0100458 cc_prebuilt_library_shared {
459 name: "mylib2",
460 srcs: ["prebuilt.so"],
461 // TODO: remove //apex_available:platform
462 apex_available: [
463 "//apex_available:platform",
464 "myapex",
465 ],
466 }
467
Jiyong Park9918e1a2020-03-17 19:16:40 +0900468 cc_library_static {
469 name: "libstatic",
470 srcs: ["mylib.cpp"],
471 system_shared_libs: [],
472 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000473 // TODO: remove //apex_available:platform
474 apex_available: [
475 "//apex_available:platform",
476 "myapex",
477 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900478 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900479
480 java_library {
481 name: "myjar",
482 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900483 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900484 sdk_version: "none",
485 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900486 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900487 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000488 // TODO: remove //apex_available:platform
489 apex_available: [
490 "//apex_available:platform",
491 "myapex",
492 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900493 }
494
Jiyong Park77acec62020-06-01 21:39:15 +0900495 dex_import {
496 name: "myjar_dex",
497 jars: ["prebuilt.jar"],
498 apex_available: [
499 "//apex_available:platform",
500 "myapex",
501 ],
502 }
503
Jiyong Park7f7766d2019-07-25 22:02:35 +0900504 java_library {
505 name: "myotherjar",
506 srcs: ["foo/bar/MyClass.java"],
507 sdk_version: "none",
508 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900509 // TODO: remove //apex_available:platform
510 apex_available: [
511 "//apex_available:platform",
512 "myapex",
513 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900514 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900515
516 java_library {
517 name: "mysharedjar",
518 srcs: ["foo/bar/MyClass.java"],
519 sdk_version: "none",
520 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900521 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900522 `)
523
Jooyung Hana0503a52023-08-23 13:12:50 +0900524 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900525
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900526 // Make sure that Android.mk is created
Jooyung Hana0503a52023-08-23 13:12:50 +0900527 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700528 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900529 var builder strings.Builder
530 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
531
532 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000533 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900534 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
535
Jiyong Park42cca6c2019-04-01 11:15:50 +0900536 optFlags := apexRule.Args["opt_flags"]
537 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700538 // Ensure that the NOTICE output is being packaged as an asset.
Jooyung Hana0503a52023-08-23 13:12:50 +0900539 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900540
Jiyong Park25fc6a92018-11-18 18:02:45 +0900541 copyCmds := apexRule.Args["copy_commands"]
542
543 // Ensure that main rule creates an output
544 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
545
546 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700547 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
548 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
549 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900550 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900551 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900552
553 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700554 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
555 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900556 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
557 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900558 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900559 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900560
561 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800562 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
563 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900564 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900565 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900566 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900567 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
568 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900569 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900570 // .. but not for java libs
571 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900572 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800573
Colin Cross7113d202019-11-20 16:39:12 -0800574 // Ensure that the platform variant ends with _shared or _common
575 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
576 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900577 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
578 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900579 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
580
581 // Ensure that dynamic dependency to java libs are not included
582 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800583
584 // Ensure that all symlinks are present.
585 found_foo_link_64 := false
586 found_foo := false
587 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900588 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800589 if strings.HasSuffix(cmd, "bin/foo") {
590 found_foo = true
591 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
592 found_foo_link_64 = true
593 }
594 }
595 }
596 good := found_foo && found_foo_link_64
597 if !good {
598 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
599 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900600
Colin Crossf61d03d2023-11-02 16:56:39 -0700601 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
602 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100603 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
605 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
606 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100607
Colin Crossf61d03d2023-11-02 16:56:39 -0700608 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
609 ctx.ModuleForTests("myapex", "android_common_myapex").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100610 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100611 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
612 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
613 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800614}
615
Jooyung Hanf21c7972019-12-16 22:32:06 +0900616func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800617 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900618 apex_defaults {
619 name: "myapex-defaults",
620 key: "myapex.key",
621 prebuilts: ["myetc"],
622 native_shared_libs: ["mylib"],
623 java_libs: ["myjar"],
624 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900625 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800626 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000627 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900628 }
629
630 prebuilt_etc {
631 name: "myetc",
632 src: "myprebuilt",
633 }
634
635 apex {
636 name: "myapex",
637 defaults: ["myapex-defaults"],
638 }
639
640 apex_key {
641 name: "myapex.key",
642 public_key: "testkey.avbpubkey",
643 private_key: "testkey.pem",
644 }
645
646 cc_library {
647 name: "mylib",
648 system_shared_libs: [],
649 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000650 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900651 }
652
653 java_library {
654 name: "myjar",
655 srcs: ["foo/bar/MyClass.java"],
656 sdk_version: "none",
657 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000658 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900659 }
660
661 android_app {
662 name: "AppFoo",
663 srcs: ["foo/bar/MyClass.java"],
664 sdk_version: "none",
665 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000666 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900667 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900668
669 runtime_resource_overlay {
670 name: "rro",
671 theme: "blue",
672 }
673
markchien2f59ec92020-09-02 16:23:38 +0800674 bpf {
675 name: "bpf",
676 srcs: ["bpf.c", "bpf2.c"],
677 }
678
Ken Chenfad7f9d2021-11-10 22:02:57 +0800679 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800680 name: "netdTest",
681 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800682 sub_dir: "netd",
683 }
684
Jooyung Hanf21c7972019-12-16 22:32:06 +0900685 `)
Jooyung Hana0503a52023-08-23 13:12:50 +0900686 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900687 "etc/myetc",
688 "javalib/myjar.jar",
689 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000690 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900691 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800692 "etc/bpf/bpf.o",
693 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800694 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900695 })
696}
697
Jooyung Han01a3ee22019-11-02 02:52:25 +0900698func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800699 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900700 apex {
701 name: "myapex",
702 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000703 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900704 }
705
706 apex_key {
707 name: "myapex.key",
708 public_key: "testkey.avbpubkey",
709 private_key: "testkey.pem",
710 }
711 `)
712
Jooyung Hana0503a52023-08-23 13:12:50 +0900713 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +0900714 args := module.Rule("apexRule").Args
715 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
716 t.Error("manifest should be apex_manifest.pb, but " + manifest)
717 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900718}
719
Liz Kammer4854a7d2021-05-27 14:28:27 -0400720func TestApexManifestMinSdkVersion(t *testing.T) {
721 ctx := testApex(t, `
722 apex_defaults {
723 name: "my_defaults",
724 key: "myapex.key",
725 product_specific: true,
726 file_contexts: ":my-file-contexts",
727 updatable: false,
728 }
729 apex {
730 name: "myapex_30",
731 min_sdk_version: "30",
732 defaults: ["my_defaults"],
733 }
734
735 apex {
736 name: "myapex_current",
737 min_sdk_version: "current",
738 defaults: ["my_defaults"],
739 }
740
741 apex {
742 name: "myapex_none",
743 defaults: ["my_defaults"],
744 }
745
746 apex_key {
747 name: "myapex.key",
748 public_key: "testkey.avbpubkey",
749 private_key: "testkey.pem",
750 }
751
752 filegroup {
753 name: "my-file-contexts",
754 srcs: ["product_specific_file_contexts"],
755 }
756 `, withFiles(map[string][]byte{
757 "product_specific_file_contexts": nil,
758 }), android.FixtureModifyProductVariables(
759 func(variables android.FixtureProductVariables) {
760 variables.Unbundled_build = proptools.BoolPtr(true)
761 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
762 }), android.FixtureMergeEnv(map[string]string{
763 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
764 }))
765
766 testCases := []struct {
767 module string
768 minSdkVersion string
769 }{
770 {
771 module: "myapex_30",
772 minSdkVersion: "30",
773 },
774 {
775 module: "myapex_current",
776 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
777 },
778 {
779 module: "myapex_none",
780 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
781 },
782 }
783 for _, tc := range testCases {
Jooyung Hana0503a52023-08-23 13:12:50 +0900784 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module)
Liz Kammer4854a7d2021-05-27 14:28:27 -0400785 args := module.Rule("apexRule").Args
786 optFlags := args["opt_flags"]
787 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
788 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
789 }
790 }
791}
792
Jooyung Hanaf730952023-02-28 14:13:38 +0900793func TestFileContexts(t *testing.T) {
Jooyung Hanbe953902023-05-31 16:42:16 +0900794 for _, vendor := range []bool{true, false} {
Jooyung Hanaf730952023-02-28 14:13:38 +0900795 prop := ""
Jooyung Hanbe953902023-05-31 16:42:16 +0900796 if vendor {
797 prop = "vendor: true,\n"
Jooyung Hanaf730952023-02-28 14:13:38 +0900798 }
799 ctx := testApex(t, `
800 apex {
801 name: "myapex",
802 key: "myapex.key",
Jooyung Hanaf730952023-02-28 14:13:38 +0900803 updatable: false,
Jooyung Hanaf730952023-02-28 14:13:38 +0900804 `+prop+`
805 }
806
807 apex_key {
808 name: "myapex.key",
809 public_key: "testkey.avbpubkey",
810 private_key: "testkey.pem",
811 }
Jooyung Hanbe953902023-05-31 16:42:16 +0900812 `)
Jooyung Hanaf730952023-02-28 14:13:38 +0900813
Jooyung Hana0503a52023-08-23 13:12:50 +0900814 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("file_contexts")
Jooyung Hanbe953902023-05-31 16:42:16 +0900815 if vendor {
816 android.AssertStringDoesContain(t, "should force-label as vendor_apex_metadata_file",
817 rule.RuleParams.Command,
818 "apex_manifest\\\\.pb u:object_r:vendor_apex_metadata_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900819 } else {
Jooyung Hanbe953902023-05-31 16:42:16 +0900820 android.AssertStringDoesContain(t, "should force-label as system_file",
821 rule.RuleParams.Command,
822 "apex_manifest\\\\.pb u:object_r:system_file:s0")
Jooyung Hanaf730952023-02-28 14:13:38 +0900823 }
824 }
825}
826
Jiyong Park25fc6a92018-11-18 18:02:45 +0900827func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800828 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900829 apex {
830 name: "myapex",
831 key: "myapex.key",
832 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900833 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000834 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900835 }
836
837 apex_key {
838 name: "myapex.key",
839 public_key: "testkey.avbpubkey",
840 private_key: "testkey.pem",
841 }
842
843 cc_library {
844 name: "mylib",
845 srcs: ["mylib.cpp"],
846 shared_libs: ["mylib2", "mylib3"],
847 system_shared_libs: [],
848 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000849 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900850 }
851
852 cc_library {
853 name: "mylib2",
854 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900855 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900856 system_shared_libs: [],
857 stl: "none",
858 stubs: {
859 versions: ["1", "2", "3"],
860 },
861 }
862
863 cc_library {
864 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900865 srcs: ["mylib.cpp"],
866 shared_libs: ["mylib4"],
867 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900868 stl: "none",
869 stubs: {
870 versions: ["10", "11", "12"],
871 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000872 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900873 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900874
875 cc_library {
876 name: "mylib4",
877 srcs: ["mylib.cpp"],
878 system_shared_libs: [],
879 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000880 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900881 }
Jiyong Park105dc322021-06-11 17:22:09 +0900882
883 rust_binary {
884 name: "foo.rust",
885 srcs: ["foo.rs"],
886 shared_libs: ["libfoo.shared_from_rust"],
887 prefer_rlib: true,
888 apex_available: ["myapex"],
889 }
890
891 cc_library_shared {
892 name: "libfoo.shared_from_rust",
893 srcs: ["mylib.cpp"],
894 system_shared_libs: [],
895 stl: "none",
896 stubs: {
897 versions: ["10", "11", "12"],
898 },
899 }
900
Jiyong Park25fc6a92018-11-18 18:02:45 +0900901 `)
902
Jooyung Hana0503a52023-08-23 13:12:50 +0900903 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900904 copyCmds := apexRule.Args["copy_commands"]
905
906 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800907 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900908
909 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800910 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900911
912 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800913 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900914
Colin Crossaede88c2020-08-11 12:17:01 -0700915 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900916
917 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900918 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900919 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900920 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900921
922 // 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 -0700923 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900924 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700925 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900926
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700927 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
928 // is replaced by sharing of "cFlags" in cc/builder.go.
929 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
930 // module variable representing "cflags". So it was not detected by ensureNotContains.
931 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
932 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
933 // including the original cflags's "-include mylib.h".
934 //
Jiyong Park64379952018-12-13 18:37:29 +0900935 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700936 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
937 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900938
Jiyong Park85cc35a2022-07-17 11:30:47 +0900939 // Ensure that genstub for platform-provided lib is invoked with --systemapi
940 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
941 // Ensure that genstub for apex-provided lib is invoked with --apex
942 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +0900943
Jooyung Hana0503a52023-08-23 13:12:50 +0900944 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900945 "lib64/mylib.so",
946 "lib64/mylib3.so",
947 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +0900948 "bin/foo.rust",
949 "lib64/libc++.so", // by the implicit dependency from foo.rust
950 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900951 })
Jiyong Park105dc322021-06-11 17:22:09 +0900952
953 // Ensure that stub dependency from a rust module is not included
954 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
955 // The rust module is linked to the stub cc library
Colin Cross004bd3f2023-10-02 11:39:17 -0700956 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park105dc322021-06-11 17:22:09 +0900957 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
958 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +0900959
Jooyung Hana0503a52023-08-23 13:12:50 +0900960 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jiyong Park34d5c332022-02-24 18:02:44 +0900961 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900962}
963
Jooyung Han20348752023-12-05 15:23:56 +0900964func TestApexShouldNotEmbedStubVariant(t *testing.T) {
965 testApexError(t, `module "myapex" .*: native_shared_libs: "libbar" is a stub`, `
966 apex {
967 name: "myapex",
968 key: "myapex.key",
969 vendor: true,
970 updatable: false,
971 native_shared_libs: ["libbar"], // should not add an LLNDK stub in a vendor apex
972 }
973
974 apex_key {
975 name: "myapex.key",
976 public_key: "testkey.avbpubkey",
977 private_key: "testkey.pem",
978 }
979
980 cc_library {
981 name: "libbar",
982 srcs: ["mylib.cpp"],
983 llndk: {
984 symbol_file: "libbar.map.txt",
985 }
986 }
987 `)
988}
989
Jiyong Park1bc84122021-06-22 20:23:05 +0900990func TestApexCanUsePrivateApis(t *testing.T) {
991 ctx := testApex(t, `
992 apex {
993 name: "myapex",
994 key: "myapex.key",
995 native_shared_libs: ["mylib"],
996 binaries: ["foo.rust"],
997 updatable: false,
998 platform_apis: true,
999 }
1000
1001 apex_key {
1002 name: "myapex.key",
1003 public_key: "testkey.avbpubkey",
1004 private_key: "testkey.pem",
1005 }
1006
1007 cc_library {
1008 name: "mylib",
1009 srcs: ["mylib.cpp"],
1010 shared_libs: ["mylib2"],
1011 system_shared_libs: [],
1012 stl: "none",
1013 apex_available: [ "myapex" ],
1014 }
1015
1016 cc_library {
1017 name: "mylib2",
1018 srcs: ["mylib.cpp"],
1019 cflags: ["-include mylib.h"],
1020 system_shared_libs: [],
1021 stl: "none",
1022 stubs: {
1023 versions: ["1", "2", "3"],
1024 },
1025 }
1026
1027 rust_binary {
1028 name: "foo.rust",
1029 srcs: ["foo.rs"],
1030 shared_libs: ["libfoo.shared_from_rust"],
1031 prefer_rlib: true,
1032 apex_available: ["myapex"],
1033 }
1034
1035 cc_library_shared {
1036 name: "libfoo.shared_from_rust",
1037 srcs: ["mylib.cpp"],
1038 system_shared_libs: [],
1039 stl: "none",
1040 stubs: {
1041 versions: ["10", "11", "12"],
1042 },
1043 }
1044 `)
1045
Jooyung Hana0503a52023-08-23 13:12:50 +09001046 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park1bc84122021-06-22 20:23:05 +09001047 copyCmds := apexRule.Args["copy_commands"]
1048
1049 // Ensure that indirect stubs dep is not included
1050 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1051 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1052
1053 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1054 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001055 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001056 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1057 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Colin Cross004bd3f2023-10-02 11:39:17 -07001058 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001059 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1060 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1061}
1062
Colin Cross7812fd32020-09-25 12:35:10 -07001063func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1064 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001065 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001066 apex {
1067 name: "myapex",
1068 key: "myapex.key",
1069 native_shared_libs: ["mylib", "mylib3"],
1070 min_sdk_version: "29",
1071 }
1072
1073 apex_key {
1074 name: "myapex.key",
1075 public_key: "testkey.avbpubkey",
1076 private_key: "testkey.pem",
1077 }
1078
1079 cc_library {
1080 name: "mylib",
1081 srcs: ["mylib.cpp"],
1082 shared_libs: ["mylib2", "mylib3"],
1083 system_shared_libs: [],
1084 stl: "none",
1085 apex_available: [ "myapex" ],
1086 min_sdk_version: "28",
1087 }
1088
1089 cc_library {
1090 name: "mylib2",
1091 srcs: ["mylib.cpp"],
1092 cflags: ["-include mylib.h"],
1093 system_shared_libs: [],
1094 stl: "none",
1095 stubs: {
1096 versions: ["28", "29", "30", "current"],
1097 },
1098 min_sdk_version: "28",
1099 }
1100
1101 cc_library {
1102 name: "mylib3",
1103 srcs: ["mylib.cpp"],
1104 shared_libs: ["mylib4"],
1105 system_shared_libs: [],
1106 stl: "none",
1107 stubs: {
1108 versions: ["28", "29", "30", "current"],
1109 },
1110 apex_available: [ "myapex" ],
1111 min_sdk_version: "28",
1112 }
1113
1114 cc_library {
1115 name: "mylib4",
1116 srcs: ["mylib.cpp"],
1117 system_shared_libs: [],
1118 stl: "none",
1119 apex_available: [ "myapex" ],
1120 min_sdk_version: "28",
1121 }
1122 `)
1123
Jooyung Hana0503a52023-08-23 13:12:50 +09001124 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Colin Cross7812fd32020-09-25 12:35:10 -07001125 copyCmds := apexRule.Args["copy_commands"]
1126
1127 // Ensure that direct non-stubs dep is always included
1128 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1129
1130 // Ensure that indirect stubs dep is not included
1131 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1132
1133 // Ensure that direct stubs dep is included
1134 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1135
1136 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1137
Jiyong Park55549df2021-02-26 23:57:23 +09001138 // Ensure that mylib is linking with the latest version of stub for mylib2
1139 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001140 // ... and not linking to the non-stub (impl) variant of mylib2
1141 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1142
1143 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1144 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1145 // .. and not linking to the stubs variant of mylib3
1146 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1147
1148 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001149 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001150 ensureNotContains(t, mylib2Cflags, "-include ")
1151
Jiyong Park85cc35a2022-07-17 11:30:47 +09001152 // Ensure that genstub is invoked with --systemapi
1153 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001154
Jooyung Hana0503a52023-08-23 13:12:50 +09001155 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Colin Cross7812fd32020-09-25 12:35:10 -07001156 "lib64/mylib.so",
1157 "lib64/mylib3.so",
1158 "lib64/mylib4.so",
1159 })
1160}
1161
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001162func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1163 t.Parallel()
1164 // myapex (Z)
1165 // mylib -----------------.
1166 // |
1167 // otherapex (29) |
1168 // libstub's versions: 29 Z current
1169 // |
1170 // <platform> |
1171 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001172 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001173 apex {
1174 name: "myapex",
1175 key: "myapex.key",
1176 native_shared_libs: ["mylib"],
1177 min_sdk_version: "Z", // non-final
1178 }
1179
1180 cc_library {
1181 name: "mylib",
1182 srcs: ["mylib.cpp"],
1183 shared_libs: ["libstub"],
1184 apex_available: ["myapex"],
1185 min_sdk_version: "Z",
1186 }
1187
1188 apex_key {
1189 name: "myapex.key",
1190 public_key: "testkey.avbpubkey",
1191 private_key: "testkey.pem",
1192 }
1193
1194 apex {
1195 name: "otherapex",
1196 key: "myapex.key",
1197 native_shared_libs: ["libstub"],
1198 min_sdk_version: "29",
1199 }
1200
1201 cc_library {
1202 name: "libstub",
1203 srcs: ["mylib.cpp"],
1204 stubs: {
1205 versions: ["29", "Z", "current"],
1206 },
1207 apex_available: ["otherapex"],
1208 min_sdk_version: "29",
1209 }
1210
1211 // platform module depending on libstub from otherapex should use the latest stub("current")
1212 cc_library {
1213 name: "libplatform",
1214 srcs: ["mylib.cpp"],
1215 shared_libs: ["libstub"],
1216 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001217 `,
1218 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1219 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1220 variables.Platform_sdk_final = proptools.BoolPtr(false)
1221 variables.Platform_version_active_codenames = []string{"Z"}
1222 }),
1223 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001224
Jiyong Park55549df2021-02-26 23:57:23 +09001225 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001226 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001227 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001228 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001229 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001230
1231 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1232 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1233 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1234 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1235 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1236}
1237
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001238func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001239 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001240 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001241 name: "myapex2",
1242 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001243 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001244 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001245 }
1246
1247 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001248 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001249 public_key: "testkey.avbpubkey",
1250 private_key: "testkey.pem",
1251 }
1252
1253 cc_library {
1254 name: "mylib",
1255 srcs: ["mylib.cpp"],
1256 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001257 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001258 system_shared_libs: [],
1259 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001260 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001261 }
1262
1263 cc_library {
1264 name: "libfoo",
1265 srcs: ["mylib.cpp"],
1266 shared_libs: ["libbar"],
1267 system_shared_libs: [],
1268 stl: "none",
1269 stubs: {
1270 versions: ["10", "20", "30"],
1271 },
1272 }
1273
1274 cc_library {
1275 name: "libbar",
1276 srcs: ["mylib.cpp"],
1277 system_shared_libs: [],
1278 stl: "none",
1279 }
1280
Jiyong Park678c8812020-02-07 17:25:49 +09001281 cc_library_static {
1282 name: "libbaz",
1283 srcs: ["mylib.cpp"],
1284 system_shared_libs: [],
1285 stl: "none",
1286 apex_available: [ "myapex2" ],
1287 }
1288
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001289 `)
1290
Jooyung Hana0503a52023-08-23 13:12:50 +09001291 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001292 copyCmds := apexRule.Args["copy_commands"]
1293
1294 // Ensure that direct non-stubs dep is always included
1295 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1296
1297 // Ensure that indirect stubs dep is not included
1298 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1299
1300 // Ensure that dependency of stubs is not included
1301 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1302
Colin Crossaede88c2020-08-11 12:17:01 -07001303 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001304
1305 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001306 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001307 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001308 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001309
Jiyong Park3ff16992019-12-27 14:11:47 +09001310 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001311
1312 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1313 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001314
Colin Crossf61d03d2023-11-02 16:56:39 -07001315 fullDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1316 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/fulllist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001317 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001318
Colin Crossf61d03d2023-11-02 16:56:39 -07001319 flatDepsInfo := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
1320 ctx.ModuleForTests("myapex2", "android_common_myapex2").Output("depsinfo/flatlist.txt")), "\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001321 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001322}
1323
Jooyung Hand3639552019-08-09 12:57:43 +09001324func TestApexWithRuntimeLibsDependency(t *testing.T) {
1325 /*
1326 myapex
1327 |
1328 v (runtime_libs)
1329 mylib ------+------> libfoo [provides stub]
1330 |
1331 `------> libbar
1332 */
Colin Cross1c460562021-02-16 17:55:47 -08001333 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001334 apex {
1335 name: "myapex",
1336 key: "myapex.key",
1337 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001338 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001339 }
1340
1341 apex_key {
1342 name: "myapex.key",
1343 public_key: "testkey.avbpubkey",
1344 private_key: "testkey.pem",
1345 }
1346
1347 cc_library {
1348 name: "mylib",
1349 srcs: ["mylib.cpp"],
Liz Kammer5f108fa2023-05-11 14:33:17 -04001350 static_libs: ["libstatic"],
1351 shared_libs: ["libshared"],
Jooyung Hand3639552019-08-09 12:57:43 +09001352 runtime_libs: ["libfoo", "libbar"],
1353 system_shared_libs: [],
1354 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001355 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001356 }
1357
1358 cc_library {
1359 name: "libfoo",
1360 srcs: ["mylib.cpp"],
1361 system_shared_libs: [],
1362 stl: "none",
1363 stubs: {
1364 versions: ["10", "20", "30"],
1365 },
1366 }
1367
1368 cc_library {
1369 name: "libbar",
1370 srcs: ["mylib.cpp"],
1371 system_shared_libs: [],
1372 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001373 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001374 }
1375
Liz Kammer5f108fa2023-05-11 14:33:17 -04001376 cc_library {
1377 name: "libstatic",
1378 srcs: ["mylib.cpp"],
1379 system_shared_libs: [],
1380 stl: "none",
1381 apex_available: [ "myapex" ],
1382 runtime_libs: ["libstatic_to_runtime"],
1383 }
1384
1385 cc_library {
1386 name: "libshared",
1387 srcs: ["mylib.cpp"],
1388 system_shared_libs: [],
1389 stl: "none",
1390 apex_available: [ "myapex" ],
1391 runtime_libs: ["libshared_to_runtime"],
1392 }
1393
1394 cc_library {
1395 name: "libstatic_to_runtime",
1396 srcs: ["mylib.cpp"],
1397 system_shared_libs: [],
1398 stl: "none",
1399 apex_available: [ "myapex" ],
1400 }
1401
1402 cc_library {
1403 name: "libshared_to_runtime",
1404 srcs: ["mylib.cpp"],
1405 system_shared_libs: [],
1406 stl: "none",
1407 apex_available: [ "myapex" ],
1408 }
Jooyung Hand3639552019-08-09 12:57:43 +09001409 `)
1410
Jooyung Hana0503a52023-08-23 13:12:50 +09001411 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001412 copyCmds := apexRule.Args["copy_commands"]
1413
1414 // Ensure that direct non-stubs dep is always included
1415 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1416
1417 // Ensure that indirect stubs dep is not included
1418 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1419
1420 // Ensure that runtime_libs dep in included
1421 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
Liz Kammer5f108fa2023-05-11 14:33:17 -04001422 ensureContains(t, copyCmds, "image.apex/lib64/libshared.so")
1423 ensureContains(t, copyCmds, "image.apex/lib64/libshared_to_runtime.so")
1424
1425 ensureNotContains(t, copyCmds, "image.apex/lib64/libstatic_to_runtime.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001426
Jooyung Hana0503a52023-08-23 13:12:50 +09001427 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001428 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1429 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001430}
1431
Paul Duffina02cae32021-03-09 01:44:06 +00001432var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1433 cc.PrepareForTestWithCcBuildComponents,
1434 PrepareForTestWithApexBuildComponents,
1435 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001436 apex {
1437 name: "com.android.runtime",
1438 key: "com.android.runtime.key",
1439 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001440 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001441 }
1442
1443 apex_key {
1444 name: "com.android.runtime.key",
1445 public_key: "testkey.avbpubkey",
1446 private_key: "testkey.pem",
1447 }
Paul Duffina02cae32021-03-09 01:44:06 +00001448 `),
1449 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1450)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001451
Paul Duffina02cae32021-03-09 01:44:06 +00001452func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001453 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001454 cc_library {
1455 name: "libc",
1456 no_libcrt: true,
1457 nocrt: true,
1458 stl: "none",
1459 system_shared_libs: [],
1460 stubs: { versions: ["1"] },
1461 apex_available: ["com.android.runtime"],
1462
1463 sanitize: {
1464 hwaddress: true,
1465 }
1466 }
1467
1468 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001469 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001470 no_libcrt: true,
1471 nocrt: true,
1472 stl: "none",
1473 system_shared_libs: [],
1474 srcs: [""],
1475 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001476 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001477
1478 sanitize: {
1479 never: true,
1480 },
Spandan Das4de7b492023-05-05 21:13:01 +00001481 apex_available: [
1482 "//apex_available:anyapex",
1483 "//apex_available:platform",
1484 ],
Paul Duffina02cae32021-03-09 01:44:06 +00001485 } `)
1486 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001487
Jooyung Hana0503a52023-08-23 13:12:50 +09001488 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001489 "lib64/bionic/libc.so",
1490 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1491 })
1492
Colin Cross4c4c1be2022-02-10 11:41:18 -08001493 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001494
1495 installed := hwasan.Description("install libclang_rt.hwasan")
1496 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1497
1498 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1499 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1500 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1501}
1502
1503func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001504 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001505 prepareForTestOfRuntimeApexWithHwasan,
1506 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1507 variables.SanitizeDevice = []string{"hwaddress"}
1508 }),
1509 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001510 cc_library {
1511 name: "libc",
1512 no_libcrt: true,
1513 nocrt: true,
1514 stl: "none",
1515 system_shared_libs: [],
1516 stubs: { versions: ["1"] },
1517 apex_available: ["com.android.runtime"],
1518 }
1519
1520 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001521 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001522 no_libcrt: true,
1523 nocrt: true,
1524 stl: "none",
1525 system_shared_libs: [],
1526 srcs: [""],
1527 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001528 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001529
1530 sanitize: {
1531 never: true,
1532 },
Spandan Das4de7b492023-05-05 21:13:01 +00001533 apex_available: [
1534 "//apex_available:anyapex",
1535 "//apex_available:platform",
1536 ],
Jooyung Han8ce8db92020-05-15 19:05:05 +09001537 }
Paul Duffina02cae32021-03-09 01:44:06 +00001538 `)
1539 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001540
Jooyung Hana0503a52023-08-23 13:12:50 +09001541 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime", []string{
Jooyung Han8ce8db92020-05-15 19:05:05 +09001542 "lib64/bionic/libc.so",
1543 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1544 })
1545
Colin Cross4c4c1be2022-02-10 11:41:18 -08001546 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001547
1548 installed := hwasan.Description("install libclang_rt.hwasan")
1549 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1550
1551 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1552 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1553 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1554}
1555
Jooyung Han61b66e92020-03-21 14:21:46 +00001556func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1557 testcases := []struct {
1558 name string
1559 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001560 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001561 shouldLink string
1562 shouldNotLink []string
1563 }{
1564 {
Jiyong Park55549df2021-02-26 23:57:23 +09001565 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001566 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001567 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001568 shouldLink: "current",
1569 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001570 },
1571 {
Jiyong Park55549df2021-02-26 23:57:23 +09001572 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001573 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001574 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001575 shouldLink: "current",
1576 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001577 },
1578 }
1579 for _, tc := range testcases {
1580 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001581 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001582 apex {
1583 name: "myapex",
1584 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001585 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001586 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001587 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001588 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001589
Jooyung Han61b66e92020-03-21 14:21:46 +00001590 apex_key {
1591 name: "myapex.key",
1592 public_key: "testkey.avbpubkey",
1593 private_key: "testkey.pem",
1594 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001595
Jooyung Han61b66e92020-03-21 14:21:46 +00001596 cc_library {
1597 name: "mylib",
1598 srcs: ["mylib.cpp"],
1599 vendor_available: true,
1600 shared_libs: ["libbar"],
1601 system_shared_libs: [],
1602 stl: "none",
1603 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001604 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001605 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001606
Jooyung Han61b66e92020-03-21 14:21:46 +00001607 cc_library {
1608 name: "libbar",
1609 srcs: ["mylib.cpp"],
1610 system_shared_libs: [],
1611 stl: "none",
1612 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001613 llndk: {
1614 symbol_file: "libbar.map.txt",
1615 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001616 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001617 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001618 withUnbundledBuild,
1619 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001620
Jooyung Han61b66e92020-03-21 14:21:46 +00001621 // Ensure that LLNDK dep is not included
Jooyung Hana0503a52023-08-23 13:12:50 +09001622 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00001623 "lib64/mylib.so",
1624 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001625
Jooyung Han61b66e92020-03-21 14:21:46 +00001626 // Ensure that LLNDK dep is required
Jooyung Hana0503a52023-08-23 13:12:50 +09001627 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han61b66e92020-03-21 14:21:46 +00001628 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1629 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001630
Steven Moreland2c4000c2021-04-27 02:08:49 +00001631 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1632 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001633 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001634 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001635 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001636
Steven Moreland2c4000c2021-04-27 02:08:49 +00001637 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001638 ver := tc.shouldLink
1639 if tc.shouldLink == "current" {
1640 ver = strconv.Itoa(android.FutureApiLevelInt)
1641 }
1642 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001643 })
1644 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001645}
1646
Jiyong Park25fc6a92018-11-18 18:02:45 +09001647func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001648 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001649 apex {
1650 name: "myapex",
1651 key: "myapex.key",
1652 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001653 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001654 }
1655
1656 apex_key {
1657 name: "myapex.key",
1658 public_key: "testkey.avbpubkey",
1659 private_key: "testkey.pem",
1660 }
1661
1662 cc_library {
1663 name: "mylib",
1664 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001665 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001666 shared_libs: ["libdl#27"],
1667 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001668 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001669 }
1670
1671 cc_library_shared {
1672 name: "mylib_shared",
1673 srcs: ["mylib.cpp"],
1674 shared_libs: ["libdl#27"],
1675 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001676 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001677 }
1678
1679 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001680 name: "libBootstrap",
1681 srcs: ["mylib.cpp"],
1682 stl: "none",
1683 bootstrap: true,
1684 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001685 `)
1686
Jooyung Hana0503a52023-08-23 13:12:50 +09001687 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001688 copyCmds := apexRule.Args["copy_commands"]
1689
1690 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001691 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001692 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1693 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001694
1695 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001696 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001697
Colin Crossaede88c2020-08-11 12:17:01 -07001698 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1699 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1700 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001701
1702 // For dependency to libc
1703 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001704 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001705 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001706 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001707 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001708 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1709 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001710
1711 // For dependency to libm
1712 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001713 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001714 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001715 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001716 // ... and is not compiling with the stub
1717 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1718 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1719
1720 // For dependency to libdl
1721 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001722 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001723 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001724 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1725 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001726 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001727 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001728 // ... Cflags from stub is correctly exported to mylib
1729 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1730 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001731
1732 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001733 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1734 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1735 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1736 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001737}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001738
Jooyung Han749dc692020-04-15 11:03:39 +09001739func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001740 // there are three links between liba --> libz.
1741 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001742 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001743 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001744 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001745 apex {
1746 name: "myapex",
1747 key: "myapex.key",
1748 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001749 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001750 }
1751
1752 apex {
1753 name: "otherapex",
1754 key: "myapex.key",
1755 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001756 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001757 }
1758
1759 apex_key {
1760 name: "myapex.key",
1761 public_key: "testkey.avbpubkey",
1762 private_key: "testkey.pem",
1763 }
1764
1765 cc_library {
1766 name: "libx",
1767 shared_libs: ["liba"],
1768 system_shared_libs: [],
1769 stl: "none",
1770 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001771 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001772 }
1773
1774 cc_library {
1775 name: "liby",
1776 shared_libs: ["liba"],
1777 system_shared_libs: [],
1778 stl: "none",
1779 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001780 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001781 }
1782
1783 cc_library {
1784 name: "liba",
1785 shared_libs: ["libz"],
1786 system_shared_libs: [],
1787 stl: "none",
1788 apex_available: [
1789 "//apex_available:anyapex",
1790 "//apex_available:platform",
1791 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001792 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001793 }
1794
1795 cc_library {
1796 name: "libz",
1797 system_shared_libs: [],
1798 stl: "none",
1799 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001800 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001801 },
1802 }
Jooyung Han749dc692020-04-15 11:03:39 +09001803 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001804
1805 expectLink := func(from, from_variant, to, to_variant string) {
1806 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1807 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1808 }
1809 expectNoLink := func(from, from_variant, to, to_variant string) {
1810 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1811 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1812 }
1813 // platform liba is linked to non-stub version
1814 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001815 // liba in myapex is linked to current
1816 expectLink("liba", "shared_apex29", "libz", "shared_current")
1817 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001818 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001819 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001820 // liba in otherapex is linked to current
1821 expectLink("liba", "shared_apex30", "libz", "shared_current")
1822 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001823 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1824 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001825}
1826
Jooyung Hanaed150d2020-04-02 01:41:41 +09001827func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001828 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001829 apex {
1830 name: "myapex",
1831 key: "myapex.key",
1832 native_shared_libs: ["libx"],
1833 min_sdk_version: "R",
1834 }
1835
1836 apex_key {
1837 name: "myapex.key",
1838 public_key: "testkey.avbpubkey",
1839 private_key: "testkey.pem",
1840 }
1841
1842 cc_library {
1843 name: "libx",
1844 shared_libs: ["libz"],
1845 system_shared_libs: [],
1846 stl: "none",
1847 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001848 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001849 }
1850
1851 cc_library {
1852 name: "libz",
1853 system_shared_libs: [],
1854 stl: "none",
1855 stubs: {
1856 versions: ["29", "R"],
1857 },
1858 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001859 `,
1860 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1861 variables.Platform_version_active_codenames = []string{"R"}
1862 }),
1863 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001864
1865 expectLink := func(from, from_variant, to, to_variant string) {
1866 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1867 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1868 }
1869 expectNoLink := func(from, from_variant, to, to_variant string) {
1870 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1871 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1872 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001873 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1874 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001875 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1876 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001877}
1878
Jooyung Han4c4da062021-06-23 10:23:16 +09001879func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1880 testApex(t, `
1881 apex {
1882 name: "myapex",
1883 key: "myapex.key",
1884 java_libs: ["libx"],
1885 min_sdk_version: "S",
1886 }
1887
1888 apex_key {
1889 name: "myapex.key",
1890 public_key: "testkey.avbpubkey",
1891 private_key: "testkey.pem",
1892 }
1893
1894 java_library {
1895 name: "libx",
1896 srcs: ["a.java"],
1897 apex_available: [ "myapex" ],
1898 sdk_version: "current",
1899 min_sdk_version: "S", // should be okay
1900 }
1901 `,
1902 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1903 variables.Platform_version_active_codenames = []string{"S"}
1904 variables.Platform_sdk_codename = proptools.StringPtr("S")
1905 }),
1906 )
1907}
1908
Jooyung Han749dc692020-04-15 11:03:39 +09001909func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001910 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001911 apex {
1912 name: "myapex",
1913 key: "myapex.key",
1914 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001915 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001916 }
1917
1918 apex_key {
1919 name: "myapex.key",
1920 public_key: "testkey.avbpubkey",
1921 private_key: "testkey.pem",
1922 }
1923
1924 cc_library {
1925 name: "libx",
1926 shared_libs: ["libz"],
1927 system_shared_libs: [],
1928 stl: "none",
1929 apex_available: [ "myapex" ],
1930 }
1931
1932 cc_library {
1933 name: "libz",
1934 system_shared_libs: [],
1935 stl: "none",
1936 stubs: {
1937 versions: ["1", "2"],
1938 },
1939 }
1940 `)
1941
1942 expectLink := func(from, from_variant, to, to_variant string) {
1943 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1944 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1945 }
1946 expectNoLink := func(from, from_variant, to, to_variant string) {
1947 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1948 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1949 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001950 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001951 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001952 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001953 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001954}
1955
Jooyung Handfc864c2023-03-20 18:19:07 +09001956func TestApexMinSdkVersion_InVendorApex(t *testing.T) {
Jiyong Park5df7bd32021-08-25 16:18:46 +09001957 ctx := testApex(t, `
1958 apex {
1959 name: "myapex",
1960 key: "myapex.key",
1961 native_shared_libs: ["mylib"],
Jooyung Handfc864c2023-03-20 18:19:07 +09001962 updatable: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09001963 vendor: true,
1964 min_sdk_version: "29",
1965 }
1966
1967 apex_key {
1968 name: "myapex.key",
1969 public_key: "testkey.avbpubkey",
1970 private_key: "testkey.pem",
1971 }
1972
1973 cc_library {
1974 name: "mylib",
Jooyung Handfc864c2023-03-20 18:19:07 +09001975 srcs: ["mylib.cpp"],
Jiyong Park5df7bd32021-08-25 16:18:46 +09001976 vendor_available: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09001977 min_sdk_version: "29",
Jooyung Handfc864c2023-03-20 18:19:07 +09001978 shared_libs: ["libbar"],
1979 }
1980
1981 cc_library {
1982 name: "libbar",
1983 stubs: { versions: ["29", "30"] },
1984 llndk: { symbol_file: "libbar.map.txt" },
Jiyong Park5df7bd32021-08-25 16:18:46 +09001985 }
1986 `)
1987
1988 vendorVariant := "android_vendor.29_arm64_armv8-a"
1989
Jooyung Handfc864c2023-03-20 18:19:07 +09001990 mylib := ctx.ModuleForTests("mylib", vendorVariant+"_shared_myapex")
1991
1992 // Ensure that mylib links with "current" LLNDK
1993 libFlags := names(mylib.Rule("ld").Args["libFlags"])
1994 ensureListContains(t, libFlags, "out/soong/.intermediates/libbar/"+vendorVariant+"_shared_current/libbar.so")
1995
1996 // Ensure that mylib is targeting 29
1997 ccRule := ctx.ModuleForTests("mylib", vendorVariant+"_static_apex29").Output("obj/mylib.o")
1998 ensureContains(t, ccRule.Args["cFlags"], "-target aarch64-linux-android29")
1999
2000 // Ensure that the correct variant of crtbegin_so is used.
2001 crtBegin := mylib.Rule("ld").Args["crtBegin"]
2002 ensureContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
Jiyong Park5df7bd32021-08-25 16:18:46 +09002003
2004 // Ensure that the crtbegin_so used by the APEX is targeting 29
2005 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
2006 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
2007}
2008
Jooyung Han4495f842023-04-25 16:39:59 +09002009func TestTrackAllowedDeps(t *testing.T) {
2010 ctx := testApex(t, `
2011 apex {
2012 name: "myapex",
2013 key: "myapex.key",
2014 updatable: true,
2015 native_shared_libs: [
2016 "mylib",
2017 "yourlib",
2018 ],
2019 min_sdk_version: "29",
2020 }
2021
2022 apex {
2023 name: "myapex2",
2024 key: "myapex.key",
2025 updatable: false,
2026 native_shared_libs: ["yourlib"],
2027 }
2028
2029 apex_key {
2030 name: "myapex.key",
2031 public_key: "testkey.avbpubkey",
2032 private_key: "testkey.pem",
2033 }
2034
2035 cc_library {
2036 name: "mylib",
2037 srcs: ["mylib.cpp"],
2038 shared_libs: ["libbar"],
2039 min_sdk_version: "29",
2040 apex_available: ["myapex"],
2041 }
2042
2043 cc_library {
2044 name: "libbar",
2045 stubs: { versions: ["29", "30"] },
2046 }
2047
2048 cc_library {
2049 name: "yourlib",
2050 srcs: ["mylib.cpp"],
2051 min_sdk_version: "29",
2052 apex_available: ["myapex", "myapex2", "//apex_available:platform"],
2053 }
2054 `, withFiles(android.MockFS{
2055 "packages/modules/common/build/allowed_deps.txt": nil,
2056 }))
2057
2058 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2059 inputs := depsinfo.Rule("generateApexDepsInfoFilesRule").BuildParams.Inputs.Strings()
2060 android.AssertStringListContains(t, "updatable myapex should generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002061 "out/soong/.intermediates/myapex/android_common_myapex/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002062 android.AssertStringListDoesNotContain(t, "non-updatable myapex2 should not generate depsinfo file", inputs,
Jooyung Hana0503a52023-08-23 13:12:50 +09002063 "out/soong/.intermediates/myapex2/android_common_myapex2/depsinfo/flatlist.txt")
Jooyung Han4495f842023-04-25 16:39:59 +09002064
Jooyung Hana0503a52023-08-23 13:12:50 +09002065 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07002066 flatlist := strings.Split(android.ContentFromFileRuleForTests(t, ctx,
2067 myapex.Output("depsinfo/flatlist.txt")), "\n")
Jooyung Han4495f842023-04-25 16:39:59 +09002068 android.AssertStringListContains(t, "deps with stubs should be tracked in depsinfo as external dep",
2069 flatlist, "libbar(minSdkVersion:(no version)) (external)")
2070 android.AssertStringListDoesNotContain(t, "do not track if not available for platform",
2071 flatlist, "mylib:(minSdkVersion:29)")
2072 android.AssertStringListContains(t, "track platform-available lib",
2073 flatlist, "yourlib(minSdkVersion:29)")
2074}
2075
2076func TestTrackAllowedDeps_SkipWithoutAllowedDepsTxt(t *testing.T) {
2077 ctx := testApex(t, `
2078 apex {
2079 name: "myapex",
2080 key: "myapex.key",
2081 updatable: true,
2082 min_sdk_version: "29",
2083 }
2084
2085 apex_key {
2086 name: "myapex.key",
2087 public_key: "testkey.avbpubkey",
2088 private_key: "testkey.pem",
2089 }
2090 `)
2091 depsinfo := ctx.SingletonForTests("apex_depsinfo_singleton")
2092 if nil != depsinfo.MaybeRule("generateApexDepsInfoFilesRule").Output {
2093 t.Error("apex_depsinfo_singleton shouldn't run when allowed_deps.txt doesn't exist")
2094 }
2095}
2096
Jooyung Han03b51852020-02-26 22:45:42 +09002097func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002098 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002099 apex {
2100 name: "myapex",
2101 key: "myapex.key",
2102 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002103 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09002104 }
2105
2106 apex_key {
2107 name: "myapex.key",
2108 public_key: "testkey.avbpubkey",
2109 private_key: "testkey.pem",
2110 }
2111
2112 cc_library {
2113 name: "libx",
2114 system_shared_libs: [],
2115 stl: "none",
2116 apex_available: [ "myapex" ],
2117 stubs: {
2118 versions: ["1", "2"],
2119 },
2120 }
2121
2122 cc_library {
2123 name: "libz",
2124 shared_libs: ["libx"],
2125 system_shared_libs: [],
2126 stl: "none",
2127 }
2128 `)
2129
2130 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002131 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002132 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2133 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2134 }
2135 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002136 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002137 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2138 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2139 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002140 expectLink("libz", "shared", "libx", "shared_current")
2141 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002142 expectNoLink("libz", "shared", "libz", "shared_1")
2143 expectNoLink("libz", "shared", "libz", "shared")
2144}
2145
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002146var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2147 func(variables android.FixtureProductVariables) {
2148 variables.SanitizeDevice = []string{"hwaddress"}
2149 },
2150)
2151
Jooyung Han75568392020-03-20 04:29:24 +09002152func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002153 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002154 apex {
2155 name: "myapex",
2156 key: "myapex.key",
2157 native_shared_libs: ["libx"],
2158 min_sdk_version: "29",
2159 }
2160
2161 apex_key {
2162 name: "myapex.key",
2163 public_key: "testkey.avbpubkey",
2164 private_key: "testkey.pem",
2165 }
2166
2167 cc_library {
2168 name: "libx",
2169 shared_libs: ["libbar"],
2170 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002171 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002172 }
2173
2174 cc_library {
2175 name: "libbar",
2176 stubs: {
2177 versions: ["29", "30"],
2178 },
2179 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002180 `,
2181 prepareForTestWithSantitizeHwaddress,
2182 )
Jooyung Han03b51852020-02-26 22:45:42 +09002183 expectLink := func(from, from_variant, to, to_variant string) {
2184 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2185 libFlags := ld.Args["libFlags"]
2186 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2187 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002188 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002189}
2190
Jooyung Han75568392020-03-20 04:29:24 +09002191func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002192 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002193 apex {
2194 name: "myapex",
2195 key: "myapex.key",
2196 native_shared_libs: ["libx"],
2197 min_sdk_version: "29",
2198 }
2199
2200 apex_key {
2201 name: "myapex.key",
2202 public_key: "testkey.avbpubkey",
2203 private_key: "testkey.pem",
2204 }
2205
2206 cc_library {
2207 name: "libx",
2208 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002209 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002210 }
Jooyung Han75568392020-03-20 04:29:24 +09002211 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002212
2213 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002214 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002215 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002216 // note that platform variant is not.
2217 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002218 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002219}
2220
Jooyung Han749dc692020-04-15 11:03:39 +09002221func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2222 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002223 apex {
2224 name: "myapex",
2225 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002226 native_shared_libs: ["mylib"],
2227 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002228 }
2229
2230 apex_key {
2231 name: "myapex.key",
2232 public_key: "testkey.avbpubkey",
2233 private_key: "testkey.pem",
2234 }
Jooyung Han749dc692020-04-15 11:03:39 +09002235
2236 cc_library {
2237 name: "mylib",
2238 srcs: ["mylib.cpp"],
2239 system_shared_libs: [],
2240 stl: "none",
2241 apex_available: [
2242 "myapex",
2243 ],
2244 min_sdk_version: "30",
2245 }
2246 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002247
2248 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2249 apex {
2250 name: "myapex",
2251 key: "myapex.key",
2252 native_shared_libs: ["libfoo.ffi"],
2253 min_sdk_version: "29",
2254 }
2255
2256 apex_key {
2257 name: "myapex.key",
2258 public_key: "testkey.avbpubkey",
2259 private_key: "testkey.pem",
2260 }
2261
2262 rust_ffi_shared {
2263 name: "libfoo.ffi",
2264 srcs: ["foo.rs"],
2265 crate_name: "foo",
2266 apex_available: [
2267 "myapex",
2268 ],
2269 min_sdk_version: "30",
2270 }
2271 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002272
2273 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2274 apex {
2275 name: "myapex",
2276 key: "myapex.key",
2277 java_libs: ["libfoo"],
2278 min_sdk_version: "29",
2279 }
2280
2281 apex_key {
2282 name: "myapex.key",
2283 public_key: "testkey.avbpubkey",
2284 private_key: "testkey.pem",
2285 }
2286
2287 java_import {
2288 name: "libfoo",
2289 jars: ["libfoo.jar"],
2290 apex_available: [
2291 "myapex",
2292 ],
2293 min_sdk_version: "30",
2294 }
2295 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002296
2297 // Skip check for modules compiling against core API surface
2298 testApex(t, `
2299 apex {
2300 name: "myapex",
2301 key: "myapex.key",
2302 java_libs: ["libfoo"],
2303 min_sdk_version: "29",
2304 }
2305
2306 apex_key {
2307 name: "myapex.key",
2308 public_key: "testkey.avbpubkey",
2309 private_key: "testkey.pem",
2310 }
2311
2312 java_library {
2313 name: "libfoo",
2314 srcs: ["Foo.java"],
2315 apex_available: [
2316 "myapex",
2317 ],
2318 // Compile against core API surface
2319 sdk_version: "core_current",
2320 min_sdk_version: "30",
2321 }
2322 `)
2323
Jooyung Han749dc692020-04-15 11:03:39 +09002324}
2325
2326func TestApexMinSdkVersion_Okay(t *testing.T) {
2327 testApex(t, `
2328 apex {
2329 name: "myapex",
2330 key: "myapex.key",
2331 native_shared_libs: ["libfoo"],
2332 java_libs: ["libbar"],
2333 min_sdk_version: "29",
2334 }
2335
2336 apex_key {
2337 name: "myapex.key",
2338 public_key: "testkey.avbpubkey",
2339 private_key: "testkey.pem",
2340 }
2341
2342 cc_library {
2343 name: "libfoo",
2344 srcs: ["mylib.cpp"],
2345 shared_libs: ["libfoo_dep"],
2346 apex_available: ["myapex"],
2347 min_sdk_version: "29",
2348 }
2349
2350 cc_library {
2351 name: "libfoo_dep",
2352 srcs: ["mylib.cpp"],
2353 apex_available: ["myapex"],
2354 min_sdk_version: "29",
2355 }
2356
2357 java_library {
2358 name: "libbar",
2359 sdk_version: "current",
2360 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002361 static_libs: [
2362 "libbar_dep",
2363 "libbar_import_dep",
2364 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002365 apex_available: ["myapex"],
2366 min_sdk_version: "29",
2367 }
2368
2369 java_library {
2370 name: "libbar_dep",
2371 sdk_version: "current",
2372 srcs: ["a.java"],
2373 apex_available: ["myapex"],
2374 min_sdk_version: "29",
2375 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002376
2377 java_import {
2378 name: "libbar_import_dep",
2379 jars: ["libbar.jar"],
2380 apex_available: ["myapex"],
2381 min_sdk_version: "29",
2382 }
Jooyung Han03b51852020-02-26 22:45:42 +09002383 `)
2384}
2385
Colin Cross8ca61c12022-10-06 21:00:14 -07002386func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2387 // Tests that an apex dependency with min_sdk_version higher than the
2388 // min_sdk_version of the apex is allowed as long as the dependency's
2389 // min_sdk_version is less than or equal to the api level that the
2390 // architecture was introduced in. In this case, arm64 didn't exist
2391 // until api level 21, so the arm64 code will never need to run on
2392 // an api level 20 device, even if other architectures of the apex
2393 // will.
2394 testApex(t, `
2395 apex {
2396 name: "myapex",
2397 key: "myapex.key",
2398 native_shared_libs: ["libfoo"],
2399 min_sdk_version: "20",
2400 }
2401
2402 apex_key {
2403 name: "myapex.key",
2404 public_key: "testkey.avbpubkey",
2405 private_key: "testkey.pem",
2406 }
2407
2408 cc_library {
2409 name: "libfoo",
2410 srcs: ["mylib.cpp"],
2411 apex_available: ["myapex"],
2412 min_sdk_version: "21",
2413 stl: "none",
2414 }
2415 `)
2416}
2417
Artur Satayev8cf899a2020-04-15 17:29:42 +01002418func TestJavaStableSdkVersion(t *testing.T) {
2419 testCases := []struct {
2420 name string
2421 expectedError string
2422 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002423 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002424 }{
2425 {
2426 name: "Non-updatable apex with non-stable dep",
2427 bp: `
2428 apex {
2429 name: "myapex",
2430 java_libs: ["myjar"],
2431 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002432 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002433 }
2434 apex_key {
2435 name: "myapex.key",
2436 public_key: "testkey.avbpubkey",
2437 private_key: "testkey.pem",
2438 }
2439 java_library {
2440 name: "myjar",
2441 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002442 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002443 apex_available: ["myapex"],
2444 }
2445 `,
2446 },
2447 {
2448 name: "Updatable apex with stable dep",
2449 bp: `
2450 apex {
2451 name: "myapex",
2452 java_libs: ["myjar"],
2453 key: "myapex.key",
2454 updatable: true,
2455 min_sdk_version: "29",
2456 }
2457 apex_key {
2458 name: "myapex.key",
2459 public_key: "testkey.avbpubkey",
2460 private_key: "testkey.pem",
2461 }
2462 java_library {
2463 name: "myjar",
2464 srcs: ["foo/bar/MyClass.java"],
2465 sdk_version: "current",
2466 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002467 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002468 }
2469 `,
2470 },
2471 {
2472 name: "Updatable apex with non-stable dep",
2473 expectedError: "cannot depend on \"myjar\"",
2474 bp: `
2475 apex {
2476 name: "myapex",
2477 java_libs: ["myjar"],
2478 key: "myapex.key",
2479 updatable: true,
2480 }
2481 apex_key {
2482 name: "myapex.key",
2483 public_key: "testkey.avbpubkey",
2484 private_key: "testkey.pem",
2485 }
2486 java_library {
2487 name: "myjar",
2488 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002489 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002490 apex_available: ["myapex"],
2491 }
2492 `,
2493 },
2494 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002495 name: "Updatable apex with non-stable legacy core platform dep",
2496 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2497 bp: `
2498 apex {
2499 name: "myapex",
2500 java_libs: ["myjar-uses-legacy"],
2501 key: "myapex.key",
2502 updatable: true,
2503 }
2504 apex_key {
2505 name: "myapex.key",
2506 public_key: "testkey.avbpubkey",
2507 private_key: "testkey.pem",
2508 }
2509 java_library {
2510 name: "myjar-uses-legacy",
2511 srcs: ["foo/bar/MyClass.java"],
2512 sdk_version: "core_platform",
2513 apex_available: ["myapex"],
2514 }
2515 `,
2516 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2517 },
2518 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002519 name: "Updatable apex with non-stable transitive dep",
2520 // This is not actually detecting that the transitive dependency is unstable, rather it is
2521 // detecting that the transitive dependency is building against a wider API surface than the
2522 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002523 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002524 bp: `
2525 apex {
2526 name: "myapex",
2527 java_libs: ["myjar"],
2528 key: "myapex.key",
2529 updatable: true,
2530 }
2531 apex_key {
2532 name: "myapex.key",
2533 public_key: "testkey.avbpubkey",
2534 private_key: "testkey.pem",
2535 }
2536 java_library {
2537 name: "myjar",
2538 srcs: ["foo/bar/MyClass.java"],
2539 sdk_version: "current",
2540 apex_available: ["myapex"],
2541 static_libs: ["transitive-jar"],
2542 }
2543 java_library {
2544 name: "transitive-jar",
2545 srcs: ["foo/bar/MyClass.java"],
2546 sdk_version: "core_platform",
2547 apex_available: ["myapex"],
2548 }
2549 `,
2550 },
2551 }
2552
2553 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002554 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2555 continue
2556 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002557 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002558 errorHandler := android.FixtureExpectsNoErrors
2559 if test.expectedError != "" {
2560 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002561 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002562 android.GroupFixturePreparers(
2563 java.PrepareForTestWithJavaDefaultModules,
2564 PrepareForTestWithApexBuildComponents,
2565 prepareForTestWithMyapex,
2566 android.OptionalFixturePreparer(test.preparer),
2567 ).
2568 ExtendWithErrorHandler(errorHandler).
2569 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002570 })
2571 }
2572}
2573
Jooyung Han749dc692020-04-15 11:03:39 +09002574func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2575 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2576 apex {
2577 name: "myapex",
2578 key: "myapex.key",
2579 native_shared_libs: ["mylib"],
2580 min_sdk_version: "29",
2581 }
2582
2583 apex_key {
2584 name: "myapex.key",
2585 public_key: "testkey.avbpubkey",
2586 private_key: "testkey.pem",
2587 }
2588
2589 cc_library {
2590 name: "mylib",
2591 srcs: ["mylib.cpp"],
2592 shared_libs: ["mylib2"],
2593 system_shared_libs: [],
2594 stl: "none",
2595 apex_available: [
2596 "myapex",
2597 ],
2598 min_sdk_version: "29",
2599 }
2600
2601 // indirect part of the apex
2602 cc_library {
2603 name: "mylib2",
2604 srcs: ["mylib.cpp"],
2605 system_shared_libs: [],
2606 stl: "none",
2607 apex_available: [
2608 "myapex",
2609 ],
2610 min_sdk_version: "30",
2611 }
2612 `)
2613}
2614
2615func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2616 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2617 apex {
2618 name: "myapex",
2619 key: "myapex.key",
2620 apps: ["AppFoo"],
2621 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002622 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002623 }
2624
2625 apex_key {
2626 name: "myapex.key",
2627 public_key: "testkey.avbpubkey",
2628 private_key: "testkey.pem",
2629 }
2630
2631 android_app {
2632 name: "AppFoo",
2633 srcs: ["foo/bar/MyClass.java"],
2634 sdk_version: "current",
2635 min_sdk_version: "29",
2636 system_modules: "none",
2637 stl: "none",
2638 static_libs: ["bar"],
2639 apex_available: [ "myapex" ],
2640 }
2641
2642 java_library {
2643 name: "bar",
2644 sdk_version: "current",
2645 srcs: ["a.java"],
2646 apex_available: [ "myapex" ],
2647 }
2648 `)
2649}
2650
2651func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002652 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002653 apex {
2654 name: "myapex",
2655 key: "myapex.key",
2656 native_shared_libs: ["mylib"],
2657 min_sdk_version: "29",
2658 }
2659
2660 apex_key {
2661 name: "myapex.key",
2662 public_key: "testkey.avbpubkey",
2663 private_key: "testkey.pem",
2664 }
2665
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002666 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002667 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2668 cc_library {
2669 name: "mylib",
2670 srcs: ["mylib.cpp"],
2671 shared_libs: ["mylib2"],
2672 system_shared_libs: [],
2673 stl: "none",
2674 apex_available: ["myapex", "otherapex"],
2675 min_sdk_version: "29",
2676 }
2677
2678 cc_library {
2679 name: "mylib2",
2680 srcs: ["mylib.cpp"],
2681 system_shared_libs: [],
2682 stl: "none",
2683 apex_available: ["otherapex"],
2684 stubs: { versions: ["29", "30"] },
2685 min_sdk_version: "30",
2686 }
2687
2688 apex {
2689 name: "otherapex",
2690 key: "myapex.key",
2691 native_shared_libs: ["mylib", "mylib2"],
2692 min_sdk_version: "30",
2693 }
2694 `)
2695 expectLink := func(from, from_variant, to, to_variant string) {
2696 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2697 libFlags := ld.Args["libFlags"]
2698 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2699 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002700 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002701 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002702}
2703
Jooyung Haned124c32021-01-26 11:43:46 +09002704func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002705 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2706 func(variables android.FixtureProductVariables) {
2707 variables.Platform_sdk_codename = proptools.StringPtr("S")
2708 variables.Platform_version_active_codenames = []string{"S"}
2709 },
2710 )
Jooyung Haned124c32021-01-26 11:43:46 +09002711 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2712 apex {
2713 name: "myapex",
2714 key: "myapex.key",
2715 native_shared_libs: ["libfoo"],
2716 min_sdk_version: "S",
2717 }
2718 apex_key {
2719 name: "myapex.key",
2720 public_key: "testkey.avbpubkey",
2721 private_key: "testkey.pem",
2722 }
2723 cc_library {
2724 name: "libfoo",
2725 shared_libs: ["libbar"],
2726 apex_available: ["myapex"],
2727 min_sdk_version: "29",
2728 }
2729 cc_library {
2730 name: "libbar",
2731 apex_available: ["myapex"],
2732 }
2733 `, withSAsActiveCodeNames)
2734}
2735
2736func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002737 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2738 variables.Platform_sdk_codename = proptools.StringPtr("S")
2739 variables.Platform_version_active_codenames = []string{"S", "T"}
2740 })
Colin Cross1c460562021-02-16 17:55:47 -08002741 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002742 apex {
2743 name: "myapex",
2744 key: "myapex.key",
2745 native_shared_libs: ["libfoo"],
2746 min_sdk_version: "S",
2747 }
2748 apex_key {
2749 name: "myapex.key",
2750 public_key: "testkey.avbpubkey",
2751 private_key: "testkey.pem",
2752 }
2753 cc_library {
2754 name: "libfoo",
2755 shared_libs: ["libbar"],
2756 apex_available: ["myapex"],
2757 min_sdk_version: "S",
2758 }
2759 cc_library {
2760 name: "libbar",
2761 stubs: {
2762 symbol_file: "libbar.map.txt",
2763 versions: ["30", "S", "T"],
2764 },
2765 }
2766 `, withSAsActiveCodeNames)
2767
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002768 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002769 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2770 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002771 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002772}
2773
Jiyong Park7c2ee712018-12-07 00:42:25 +09002774func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002775 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002776 apex {
2777 name: "myapex",
2778 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002779 native_shared_libs: ["mylib"],
Jooyung Han4ed512b2023-08-11 16:30:04 +09002780 binaries: ["mybin", "mybin.rust"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002781 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002782 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002783 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002784 }
2785
2786 apex_key {
2787 name: "myapex.key",
2788 public_key: "testkey.avbpubkey",
2789 private_key: "testkey.pem",
2790 }
2791
2792 prebuilt_etc {
2793 name: "myetc",
2794 src: "myprebuilt",
2795 sub_dir: "foo/bar",
2796 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002797
2798 cc_library {
2799 name: "mylib",
2800 srcs: ["mylib.cpp"],
2801 relative_install_path: "foo/bar",
2802 system_shared_libs: [],
2803 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002804 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002805 }
2806
2807 cc_binary {
2808 name: "mybin",
2809 srcs: ["mylib.cpp"],
2810 relative_install_path: "foo/bar",
2811 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002812 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002813 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002814 }
Jooyung Han4ed512b2023-08-11 16:30:04 +09002815
2816 rust_binary {
2817 name: "mybin.rust",
2818 srcs: ["foo.rs"],
2819 relative_install_path: "rust_subdir",
2820 apex_available: [ "myapex" ],
2821 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002822 `)
2823
Jooyung Hana0503a52023-08-23 13:12:50 +09002824 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002825 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002826
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002827 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002828 ensureContains(t, cmd, "/etc ")
2829 ensureContains(t, cmd, "/etc/foo ")
2830 ensureContains(t, cmd, "/etc/foo/bar ")
2831 ensureContains(t, cmd, "/lib64 ")
2832 ensureContains(t, cmd, "/lib64/foo ")
2833 ensureContains(t, cmd, "/lib64/foo/bar ")
2834 ensureContains(t, cmd, "/lib ")
2835 ensureContains(t, cmd, "/lib/foo ")
2836 ensureContains(t, cmd, "/lib/foo/bar ")
2837 ensureContains(t, cmd, "/bin ")
2838 ensureContains(t, cmd, "/bin/foo ")
2839 ensureContains(t, cmd, "/bin/foo/bar ")
Jooyung Han4ed512b2023-08-11 16:30:04 +09002840 ensureContains(t, cmd, "/bin/rust_subdir ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002841}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002842
Jooyung Han35155c42020-02-06 17:33:20 +09002843func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002844 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002845 apex {
2846 name: "myapex",
2847 key: "myapex.key",
2848 multilib: {
2849 both: {
2850 native_shared_libs: ["mylib"],
2851 binaries: ["mybin"],
2852 },
2853 },
2854 compile_multilib: "both",
2855 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002856 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002857 }
2858
2859 apex_key {
2860 name: "myapex.key",
2861 public_key: "testkey.avbpubkey",
2862 private_key: "testkey.pem",
2863 }
2864
2865 cc_library {
2866 name: "mylib",
2867 relative_install_path: "foo/bar",
2868 system_shared_libs: [],
2869 stl: "none",
2870 apex_available: [ "myapex" ],
2871 native_bridge_supported: true,
2872 }
2873
2874 cc_binary {
2875 name: "mybin",
2876 relative_install_path: "foo/bar",
2877 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002878 stl: "none",
2879 apex_available: [ "myapex" ],
2880 native_bridge_supported: true,
2881 compile_multilib: "both", // default is "first" for binary
2882 multilib: {
2883 lib64: {
2884 suffix: "64",
2885 },
2886 },
2887 }
2888 `, withNativeBridgeEnabled)
Jooyung Hana0503a52023-08-23 13:12:50 +09002889 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han35155c42020-02-06 17:33:20 +09002890 "bin/foo/bar/mybin",
2891 "bin/foo/bar/mybin64",
2892 "bin/arm/foo/bar/mybin",
2893 "bin/arm64/foo/bar/mybin64",
2894 "lib/foo/bar/mylib.so",
2895 "lib/arm/foo/bar/mylib.so",
2896 "lib64/foo/bar/mylib.so",
2897 "lib64/arm64/foo/bar/mylib.so",
2898 })
2899}
2900
Jooyung Han85d61762020-06-24 23:50:26 +09002901func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002902 result := android.GroupFixturePreparers(
2903 prepareForApexTest,
2904 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2905 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002906 apex {
2907 name: "myapex",
2908 key: "myapex.key",
2909 binaries: ["mybin"],
2910 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002911 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002912 }
2913 apex_key {
2914 name: "myapex.key",
2915 public_key: "testkey.avbpubkey",
2916 private_key: "testkey.pem",
2917 }
2918 cc_binary {
2919 name: "mybin",
2920 vendor: true,
2921 shared_libs: ["libfoo"],
2922 }
2923 cc_library {
2924 name: "libfoo",
2925 proprietary: true,
2926 }
2927 `)
2928
Jooyung Hana0503a52023-08-23 13:12:50 +09002929 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002930 "bin/mybin",
2931 "lib64/libfoo.so",
2932 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2933 "lib64/libc++.so",
2934 })
2935
Jooyung Hana0503a52023-08-23 13:12:50 +09002936 apexBundle := result.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossc68db4b2021-11-11 18:59:15 -08002937 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002938 name := apexBundle.BaseModuleName()
2939 prefix := "TARGET_"
2940 var builder strings.Builder
2941 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002942 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002943 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002944 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002945
Jooyung Hana0503a52023-08-23 13:12:50 +09002946 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002947 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2948 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002949}
2950
Jooyung Hanc5a96762022-02-04 11:54:50 +09002951func TestVendorApex_use_vndk_as_stable_TryingToIncludeVNDKLib(t *testing.T) {
2952 testApexError(t, `Trying to include a VNDK library`, `
2953 apex {
2954 name: "myapex",
2955 key: "myapex.key",
2956 native_shared_libs: ["libc++"], // libc++ is a VNDK lib
2957 vendor: true,
2958 use_vndk_as_stable: true,
2959 updatable: false,
2960 }
2961 apex_key {
2962 name: "myapex.key",
2963 public_key: "testkey.avbpubkey",
2964 private_key: "testkey.pem",
2965 }`)
2966}
2967
Jooyung Handf78e212020-07-22 15:54:47 +09002968func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Jooyung Han91f92032022-02-04 12:36:33 +09002969 // myapex myapex2
2970 // | |
2971 // mybin ------. mybin2
2972 // \ \ / |
2973 // (stable) .---\--------` |
2974 // \ / \ |
2975 // \ / \ /
2976 // libvndk libvendor
2977 // (vndk)
Colin Cross1c460562021-02-16 17:55:47 -08002978 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002979 apex {
2980 name: "myapex",
2981 key: "myapex.key",
2982 binaries: ["mybin"],
2983 vendor: true,
2984 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002985 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002986 }
2987 apex_key {
2988 name: "myapex.key",
2989 public_key: "testkey.avbpubkey",
2990 private_key: "testkey.pem",
2991 }
2992 cc_binary {
2993 name: "mybin",
2994 vendor: true,
2995 shared_libs: ["libvndk", "libvendor"],
2996 }
2997 cc_library {
2998 name: "libvndk",
2999 vndk: {
3000 enabled: true,
3001 },
3002 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003003 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09003004 }
3005 cc_library {
3006 name: "libvendor",
3007 vendor: true,
Jooyung Han91f92032022-02-04 12:36:33 +09003008 stl: "none",
3009 }
3010 apex {
3011 name: "myapex2",
3012 key: "myapex.key",
3013 binaries: ["mybin2"],
3014 vendor: true,
3015 use_vndk_as_stable: false,
3016 updatable: false,
3017 }
3018 cc_binary {
3019 name: "mybin2",
3020 vendor: true,
3021 shared_libs: ["libvndk", "libvendor"],
Jooyung Handf78e212020-07-22 15:54:47 +09003022 }
Kiyoung Kim8269cee2023-07-26 12:39:19 +09003023 `,
3024 android.FixtureModifyConfig(func(config android.Config) {
3025 config.TestProductVariables.KeepVndk = proptools.BoolPtr(true)
3026 }),
3027 )
Jooyung Handf78e212020-07-22 15:54:47 +09003028
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003029 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09003030
Jooyung Han91f92032022-02-04 12:36:33 +09003031 for _, tc := range []struct {
3032 name string
3033 apexName string
3034 moduleName string
3035 moduleVariant string
3036 libs []string
3037 contents []string
3038 requireVndkNamespace bool
3039 }{
3040 {
3041 name: "use_vndk_as_stable",
3042 apexName: "myapex",
3043 moduleName: "mybin",
3044 moduleVariant: vendorVariant + "_apex10000",
3045 libs: []string{
3046 // should link with vendor variants of VNDK libs(libvndk/libc++)
3047 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared/libvndk.so",
3048 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared/libc++.so",
3049 // unstable Vendor libs as APEX variant
3050 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
3051 },
3052 contents: []string{
3053 "bin/mybin",
3054 "lib64/libvendor.so",
3055 // VNDK libs (libvndk/libc++) are not included
3056 },
3057 requireVndkNamespace: true,
3058 },
3059 {
3060 name: "!use_vndk_as_stable",
3061 apexName: "myapex2",
3062 moduleName: "mybin2",
3063 moduleVariant: vendorVariant + "_myapex2",
3064 libs: []string{
3065 // should link with "unique" APEX(myapex2) variant of VNDK libs(libvndk/libc++)
3066 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared_myapex2/libvndk.so",
3067 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared_myapex2/libc++.so",
3068 // unstable vendor libs have "merged" APEX variants
3069 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
3070 },
3071 contents: []string{
3072 "bin/mybin2",
3073 "lib64/libvendor.so",
3074 // VNDK libs are included as well
3075 "lib64/libvndk.so",
3076 "lib64/libc++.so",
3077 },
3078 requireVndkNamespace: false,
3079 },
3080 } {
3081 t.Run(tc.name, func(t *testing.T) {
3082 // Check linked libs
3083 ldRule := ctx.ModuleForTests(tc.moduleName, tc.moduleVariant).Rule("ld")
3084 libs := names(ldRule.Args["libFlags"])
3085 for _, lib := range tc.libs {
3086 ensureListContains(t, libs, lib)
3087 }
3088 // Check apex contents
Jooyung Hana0503a52023-08-23 13:12:50 +09003089 ensureExactContents(t, ctx, tc.apexName, "android_common_"+tc.apexName, tc.contents)
Jooyung Handf78e212020-07-22 15:54:47 +09003090
Jooyung Han91f92032022-02-04 12:36:33 +09003091 // Check "requireNativeLibs"
Jooyung Hana0503a52023-08-23 13:12:50 +09003092 apexManifestRule := ctx.ModuleForTests(tc.apexName, "android_common_"+tc.apexName).Rule("apexManifestRule")
Jooyung Han91f92032022-02-04 12:36:33 +09003093 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
3094 if tc.requireVndkNamespace {
3095 ensureListContains(t, requireNativeLibs, ":vndk")
3096 } else {
3097 ensureListNotContains(t, requireNativeLibs, ":vndk")
3098 }
3099 })
3100 }
Jooyung Handf78e212020-07-22 15:54:47 +09003101}
3102
Justin Yun13decfb2021-03-08 19:25:55 +09003103func TestProductVariant(t *testing.T) {
3104 ctx := testApex(t, `
3105 apex {
3106 name: "myapex",
3107 key: "myapex.key",
3108 updatable: false,
3109 product_specific: true,
3110 binaries: ["foo"],
3111 }
3112
3113 apex_key {
3114 name: "myapex.key",
3115 public_key: "testkey.avbpubkey",
3116 private_key: "testkey.pem",
3117 }
3118
3119 cc_binary {
3120 name: "foo",
3121 product_available: true,
3122 apex_available: ["myapex"],
3123 srcs: ["foo.cpp"],
3124 }
Justin Yunaf1fde42023-09-27 16:22:10 +09003125 `)
Justin Yun13decfb2021-03-08 19:25:55 +09003126
3127 cflags := strings.Fields(
Jooyung Han91f92032022-02-04 12:36:33 +09003128 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_myapex").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09003129 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
3130 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
3131 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
3132 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
3133}
3134
Jooyung Han8e5685d2020-09-21 11:02:57 +09003135func TestApex_withPrebuiltFirmware(t *testing.T) {
3136 testCases := []struct {
3137 name string
3138 additionalProp string
3139 }{
3140 {"system apex with prebuilt_firmware", ""},
3141 {"vendor apex with prebuilt_firmware", "vendor: true,"},
3142 }
3143 for _, tc := range testCases {
3144 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003145 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09003146 apex {
3147 name: "myapex",
3148 key: "myapex.key",
3149 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003150 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09003151 `+tc.additionalProp+`
3152 }
3153 apex_key {
3154 name: "myapex.key",
3155 public_key: "testkey.avbpubkey",
3156 private_key: "testkey.pem",
3157 }
3158 prebuilt_firmware {
3159 name: "myfirmware",
3160 src: "myfirmware.bin",
3161 filename_from_src: true,
3162 `+tc.additionalProp+`
3163 }
3164 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09003165 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han8e5685d2020-09-21 11:02:57 +09003166 "etc/firmware/myfirmware.bin",
3167 })
3168 })
3169 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003170}
3171
Jooyung Hanefb184e2020-06-25 17:14:25 +09003172func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003173 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003174 apex {
3175 name: "myapex",
3176 key: "myapex.key",
3177 vendor: true,
3178 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003179 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003180 }
3181
3182 apex_key {
3183 name: "myapex.key",
3184 public_key: "testkey.avbpubkey",
3185 private_key: "testkey.pem",
3186 }
3187
3188 cc_library {
3189 name: "mylib",
3190 vendor_available: true,
3191 }
3192 `)
3193
Jooyung Hana0503a52023-08-23 13:12:50 +09003194 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003195 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003196 name := apexBundle.BaseModuleName()
3197 prefix := "TARGET_"
3198 var builder strings.Builder
3199 data.Custom(&builder, name, prefix, "", data)
3200 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09003201 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 +09003202}
3203
Jooyung Han2ed99d02020-06-24 23:26:26 +09003204func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003205 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003206 apex {
3207 name: "myapex",
3208 key: "myapex.key",
3209 vintf_fragments: ["fragment.xml"],
3210 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003211 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003212 }
3213 apex_key {
3214 name: "myapex.key",
3215 public_key: "testkey.avbpubkey",
3216 private_key: "testkey.pem",
3217 }
3218 cc_binary {
3219 name: "mybin",
3220 }
3221 `)
3222
Jooyung Hana0503a52023-08-23 13:12:50 +09003223 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003224 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003225 name := apexBundle.BaseModuleName()
3226 prefix := "TARGET_"
3227 var builder strings.Builder
3228 data.Custom(&builder, name, prefix, "", data)
3229 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003230 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003231 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003232}
3233
Jiyong Park16e91a02018-12-20 18:18:08 +09003234func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003235 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003236 apex {
3237 name: "myapex",
3238 key: "myapex.key",
3239 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003240 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003241 }
3242
3243 apex_key {
3244 name: "myapex.key",
3245 public_key: "testkey.avbpubkey",
3246 private_key: "testkey.pem",
3247 }
3248
3249 cc_library {
3250 name: "mylib",
3251 srcs: ["mylib.cpp"],
3252 system_shared_libs: [],
3253 stl: "none",
3254 stubs: {
3255 versions: ["1", "2", "3"],
3256 },
Spandan Das20fce2d2023-04-12 17:21:39 +00003257 apex_available: ["myapex"],
Jiyong Park16e91a02018-12-20 18:18:08 +09003258 }
3259
3260 cc_binary {
3261 name: "not_in_apex",
3262 srcs: ["mylib.cpp"],
3263 static_libs: ["mylib"],
3264 static_executable: true,
3265 system_shared_libs: [],
3266 stl: "none",
3267 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003268 `)
3269
Colin Cross7113d202019-11-20 16:39:12 -08003270 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003271
3272 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003273 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003274}
Jiyong Park9335a262018-12-24 11:31:58 +09003275
3276func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003277 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003278 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003279 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003280 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003281 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003282 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003283 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003284 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003285 }
3286
3287 cc_library {
3288 name: "mylib",
3289 srcs: ["mylib.cpp"],
3290 system_shared_libs: [],
3291 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003292 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003293 }
3294
3295 apex_key {
3296 name: "myapex.key",
3297 public_key: "testkey.avbpubkey",
3298 private_key: "testkey.pem",
3299 }
3300
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003301 android_app_certificate {
3302 name: "myapex.certificate",
3303 certificate: "testkey",
3304 }
3305
3306 android_app_certificate {
3307 name: "myapex.certificate.override",
3308 certificate: "testkey.override",
3309 }
3310
Jiyong Park9335a262018-12-24 11:31:58 +09003311 `)
3312
3313 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003314 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003315
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003316 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3317 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003318 "vendor/foo/devkeys/testkey.avbpubkey")
3319 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003320 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3321 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003322 "vendor/foo/devkeys/testkey.pem")
3323 }
3324
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003325 // check the APK certs. It should be overridden to myapex.certificate.override
Jooyung Hana0503a52023-08-23 13:12:50 +09003326 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003327 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003328 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003329 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003330 }
3331}
Jiyong Park58e364a2019-01-19 19:24:06 +09003332
Jooyung Hanf121a652019-12-17 14:30:11 +09003333func TestCertificate(t *testing.T) {
3334 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003335 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003336 apex {
3337 name: "myapex",
3338 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003339 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003340 }
3341 apex_key {
3342 name: "myapex.key",
3343 public_key: "testkey.avbpubkey",
3344 private_key: "testkey.pem",
3345 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003346 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003347 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3348 if actual := rule.Args["certificates"]; actual != expected {
3349 t.Errorf("certificates should be %q, not %q", expected, actual)
3350 }
3351 })
3352 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003353 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003354 apex {
3355 name: "myapex_keytest",
3356 key: "myapex.key",
3357 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003358 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003359 }
3360 apex_key {
3361 name: "myapex.key",
3362 public_key: "testkey.avbpubkey",
3363 private_key: "testkey.pem",
3364 }
3365 android_app_certificate {
3366 name: "myapex.certificate.override",
3367 certificate: "testkey.override",
3368 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003369 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003370 expected := "testkey.override.x509.pem testkey.override.pk8"
3371 if actual := rule.Args["certificates"]; actual != expected {
3372 t.Errorf("certificates should be %q, not %q", expected, actual)
3373 }
3374 })
3375 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003376 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003377 apex {
3378 name: "myapex",
3379 key: "myapex.key",
3380 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003381 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003382 }
3383 apex_key {
3384 name: "myapex.key",
3385 public_key: "testkey.avbpubkey",
3386 private_key: "testkey.pem",
3387 }
3388 android_app_certificate {
3389 name: "myapex.certificate",
3390 certificate: "testkey",
3391 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003392 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003393 expected := "testkey.x509.pem testkey.pk8"
3394 if actual := rule.Args["certificates"]; actual != expected {
3395 t.Errorf("certificates should be %q, not %q", expected, actual)
3396 }
3397 })
3398 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003399 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003400 apex {
3401 name: "myapex_keytest",
3402 key: "myapex.key",
3403 file_contexts: ":myapex-file_contexts",
3404 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003405 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003406 }
3407 apex_key {
3408 name: "myapex.key",
3409 public_key: "testkey.avbpubkey",
3410 private_key: "testkey.pem",
3411 }
3412 android_app_certificate {
3413 name: "myapex.certificate.override",
3414 certificate: "testkey.override",
3415 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003416 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003417 expected := "testkey.override.x509.pem testkey.override.pk8"
3418 if actual := rule.Args["certificates"]; actual != expected {
3419 t.Errorf("certificates should be %q, not %q", expected, actual)
3420 }
3421 })
3422 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003423 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003424 apex {
3425 name: "myapex",
3426 key: "myapex.key",
3427 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003428 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003429 }
3430 apex_key {
3431 name: "myapex.key",
3432 public_key: "testkey.avbpubkey",
3433 private_key: "testkey.pem",
3434 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003435 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003436 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3437 if actual := rule.Args["certificates"]; actual != expected {
3438 t.Errorf("certificates should be %q, not %q", expected, actual)
3439 }
3440 })
3441 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003442 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003443 apex {
3444 name: "myapex_keytest",
3445 key: "myapex.key",
3446 file_contexts: ":myapex-file_contexts",
3447 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003448 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003449 }
3450 apex_key {
3451 name: "myapex.key",
3452 public_key: "testkey.avbpubkey",
3453 private_key: "testkey.pem",
3454 }
3455 android_app_certificate {
3456 name: "myapex.certificate.override",
3457 certificate: "testkey.override",
3458 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09003459 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest").Rule("signapk")
Jooyung Hanf121a652019-12-17 14:30:11 +09003460 expected := "testkey.override.x509.pem testkey.override.pk8"
3461 if actual := rule.Args["certificates"]; actual != expected {
3462 t.Errorf("certificates should be %q, not %q", expected, actual)
3463 }
3464 })
3465}
3466
Jiyong Park58e364a2019-01-19 19:24:06 +09003467func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003468 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003469 apex {
3470 name: "myapex",
3471 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003472 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003473 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003474 }
3475
3476 apex {
3477 name: "otherapex",
3478 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003479 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003480 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003481 }
3482
3483 apex_key {
3484 name: "myapex.key",
3485 public_key: "testkey.avbpubkey",
3486 private_key: "testkey.pem",
3487 }
3488
3489 cc_library {
3490 name: "mylib",
3491 srcs: ["mylib.cpp"],
3492 system_shared_libs: [],
3493 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003494 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003495 "myapex",
3496 "otherapex",
3497 ],
Jooyung Han24282772020-03-21 23:20:55 +09003498 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003499 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003500 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003501 cc_library {
3502 name: "mylib2",
3503 srcs: ["mylib.cpp"],
3504 system_shared_libs: [],
3505 stl: "none",
3506 apex_available: [
3507 "myapex",
3508 "otherapex",
3509 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003510 static_libs: ["mylib3"],
3511 recovery_available: true,
3512 min_sdk_version: "29",
3513 }
3514 cc_library {
3515 name: "mylib3",
3516 srcs: ["mylib.cpp"],
3517 system_shared_libs: [],
3518 stl: "none",
3519 apex_available: [
3520 "myapex",
3521 "otherapex",
3522 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003523 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003524 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003525 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003526 `)
3527
Jooyung Hanc87a0592020-03-02 17:44:33 +09003528 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003529 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003530 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003531
Vinh Tranf9754732023-01-19 22:41:46 -05003532 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003533 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003534 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003535
Vinh Tranf9754732023-01-19 22:41:46 -05003536 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003537 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003538 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003539
Colin Crossaede88c2020-08-11 12:17:01 -07003540 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3541 // each variant defines additional macros to distinguish which apex variant it is built for
3542
3543 // non-APEX variant does not have __ANDROID_APEX__ defined
3544 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3545 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3546
Vinh Tranf9754732023-01-19 22:41:46 -05003547 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003548 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3549 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003550
Jooyung Hanc87a0592020-03-02 17:44:33 +09003551 // non-APEX variant does not have __ANDROID_APEX__ defined
3552 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3553 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3554
Vinh Tranf9754732023-01-19 22:41:46 -05003555 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003556 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003557 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003558}
Jiyong Park7e636d02019-01-28 16:16:54 +09003559
3560func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003561 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003562 apex {
3563 name: "myapex",
3564 key: "myapex.key",
3565 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003566 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003567 }
3568
3569 apex_key {
3570 name: "myapex.key",
3571 public_key: "testkey.avbpubkey",
3572 private_key: "testkey.pem",
3573 }
3574
3575 cc_library_headers {
3576 name: "mylib_headers",
3577 export_include_dirs: ["my_include"],
3578 system_shared_libs: [],
3579 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003580 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003581 }
3582
3583 cc_library {
3584 name: "mylib",
3585 srcs: ["mylib.cpp"],
3586 system_shared_libs: [],
3587 stl: "none",
3588 header_libs: ["mylib_headers"],
3589 export_header_lib_headers: ["mylib_headers"],
3590 stubs: {
3591 versions: ["1", "2", "3"],
3592 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003593 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003594 }
3595
3596 cc_library {
3597 name: "otherlib",
3598 srcs: ["mylib.cpp"],
3599 system_shared_libs: [],
3600 stl: "none",
3601 shared_libs: ["mylib"],
3602 }
3603 `)
3604
Colin Cross7113d202019-11-20 16:39:12 -08003605 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003606
3607 // Ensure that the include path of the header lib is exported to 'otherlib'
3608 ensureContains(t, cFlags, "-Imy_include")
3609}
Alex Light9670d332019-01-29 18:07:33 -08003610
Jiyong Park7cd10e32020-01-14 09:22:18 +09003611type fileInApex struct {
3612 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003613 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003614 isLink bool
3615}
3616
Jooyung Han1724d582022-12-21 10:17:44 +09003617func (f fileInApex) String() string {
3618 return f.src + ":" + f.path
3619}
3620
3621func (f fileInApex) match(expectation string) bool {
3622 parts := strings.Split(expectation, ":")
3623 if len(parts) == 1 {
3624 match, _ := path.Match(parts[0], f.path)
3625 return match
3626 }
3627 if len(parts) == 2 {
3628 matchSrc, _ := path.Match(parts[0], f.src)
3629 matchDst, _ := path.Match(parts[1], f.path)
3630 return matchSrc && matchDst
3631 }
3632 panic("invalid expected file specification: " + expectation)
3633}
3634
Jooyung Hana57af4a2020-01-23 05:36:59 +00003635func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003636 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003637 module := ctx.ModuleForTests(moduleName, variant)
3638 apexRule := module.MaybeRule("apexRule")
3639 apexDir := "/image.apex/"
Jooyung Han31c470b2019-10-18 16:26:59 +09003640 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003641 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003642 for _, cmd := range strings.Split(copyCmds, "&&") {
3643 cmd = strings.TrimSpace(cmd)
3644 if cmd == "" {
3645 continue
3646 }
3647 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003648 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003649 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003650 switch terms[0] {
3651 case "mkdir":
3652 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003653 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003654 t.Fatal("copyCmds contains invalid cp command", cmd)
3655 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003656 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003657 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003658 isLink = false
3659 case "ln":
3660 if len(terms) != 3 && len(terms) != 4 {
3661 // ln LINK TARGET or ln -s LINK TARGET
3662 t.Fatal("copyCmds contains invalid ln command", cmd)
3663 }
3664 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003665 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003666 isLink = true
3667 default:
3668 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3669 }
3670 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003671 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003672 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003673 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003674 }
Jooyung Han1724d582022-12-21 10:17:44 +09003675 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003676 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003677 }
3678 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003679 return ret
3680}
3681
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003682func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003683 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003684 var failed bool
3685 var surplus []string
3686 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003687 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003688 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003689 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003690 if file.match(expected) {
3691 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003692 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003693 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003694 }
3695 }
Jooyung Han1724d582022-12-21 10:17:44 +09003696 if !matchFound {
3697 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003698 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003699 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003700
Jooyung Han31c470b2019-10-18 16:26:59 +09003701 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003702 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003703 t.Log("surplus files", surplus)
3704 failed = true
3705 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003706
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003707 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003708 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003709 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003710 if !filesMatched[expected] {
3711 missing = append(missing, expected)
3712 }
3713 }
3714 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003715 t.Log("missing files", missing)
3716 failed = true
3717 }
3718 if failed {
3719 t.Fail()
3720 }
3721}
3722
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003723func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3724 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3725}
3726
3727func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
Spandan Das2069c3f2023-12-06 19:40:24 +00003728 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Description("deapex")
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003729 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3730 if deapexer.Output != nil {
3731 outputs = append(outputs, deapexer.Output.String())
3732 }
3733 for _, output := range deapexer.ImplicitOutputs {
3734 outputs = append(outputs, output.String())
3735 }
3736 actualFiles := make([]fileInApex, 0, len(outputs))
3737 for _, output := range outputs {
3738 dir := "/deapexer/"
3739 pos := strings.LastIndex(output, dir)
3740 if pos == -1 {
3741 t.Fatal("Unknown deapexer output ", output)
3742 }
3743 path := output[pos+len(dir):]
3744 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3745 }
3746 assertFileListEquals(t, files, actualFiles)
3747}
3748
Jooyung Han344d5432019-08-23 11:17:39 +09003749func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003750 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003751 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003752 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003753 "etc/llndk.libraries.29.txt",
3754 "etc/vndkcore.libraries.29.txt",
3755 "etc/vndksp.libraries.29.txt",
3756 "etc/vndkprivate.libraries.29.txt",
3757 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003758 }
3759 testCases := []struct {
3760 vndkVersion string
3761 expectedFiles []string
3762 }{
3763 {
3764 vndkVersion: "current",
3765 expectedFiles: append(commonFiles,
3766 "lib/libvndk.so",
3767 "lib/libvndksp.so",
3768 "lib64/libvndk.so",
3769 "lib64/libvndksp.so"),
3770 },
3771 {
3772 vndkVersion: "",
3773 expectedFiles: append(commonFiles,
3774 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3775 "lib/libvndksp.so",
3776 "lib64/libvndksp.so"),
3777 },
3778 }
3779 for _, tc := range testCases {
3780 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3781 ctx := testApex(t, `
3782 apex_vndk {
3783 name: "com.android.vndk.current",
3784 key: "com.android.vndk.current.key",
3785 updatable: false,
3786 }
3787
3788 apex_key {
3789 name: "com.android.vndk.current.key",
3790 public_key: "testkey.avbpubkey",
3791 private_key: "testkey.pem",
3792 }
3793
3794 cc_library {
3795 name: "libvndk",
3796 srcs: ["mylib.cpp"],
3797 vendor_available: true,
3798 product_available: true,
3799 vndk: {
3800 enabled: true,
3801 },
3802 system_shared_libs: [],
3803 stl: "none",
3804 apex_available: [ "com.android.vndk.current" ],
3805 }
3806
3807 cc_library {
3808 name: "libvndksp",
3809 srcs: ["mylib.cpp"],
3810 vendor_available: true,
3811 product_available: true,
3812 vndk: {
3813 enabled: true,
3814 support_system_process: true,
3815 },
3816 system_shared_libs: [],
3817 stl: "none",
3818 apex_available: [ "com.android.vndk.current" ],
3819 }
3820
3821 // VNDK-Ext should not cause any problems
3822
3823 cc_library {
3824 name: "libvndk.ext",
3825 srcs: ["mylib2.cpp"],
3826 vendor: true,
3827 vndk: {
3828 enabled: true,
3829 extends: "libvndk",
3830 },
3831 system_shared_libs: [],
3832 stl: "none",
3833 }
3834
3835 cc_library {
3836 name: "libvndksp.ext",
3837 srcs: ["mylib2.cpp"],
3838 vendor: true,
3839 vndk: {
3840 enabled: true,
3841 support_system_process: true,
3842 extends: "libvndksp",
3843 },
3844 system_shared_libs: [],
3845 stl: "none",
3846 }
3847 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3848 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
Kiyoung Kima2d6dee2023-08-11 10:14:43 +09003849 variables.KeepVndk = proptools.BoolPtr(true)
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003850 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09003851 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common", tc.expectedFiles)
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003852 })
3853 }
Jooyung Han344d5432019-08-23 11:17:39 +09003854}
3855
3856func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003857 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003858 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003859 name: "com.android.vndk.current",
3860 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003861 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003862 }
3863
3864 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003865 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003866 public_key: "testkey.avbpubkey",
3867 private_key: "testkey.pem",
3868 }
3869
3870 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003871 name: "libvndk",
3872 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003873 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003874 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003875 vndk: {
3876 enabled: true,
3877 },
3878 system_shared_libs: [],
3879 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003880 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003881 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003882
3883 cc_prebuilt_library_shared {
3884 name: "libvndk.arm",
3885 srcs: ["libvndk.arm.so"],
3886 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003887 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003888 vndk: {
3889 enabled: true,
3890 },
3891 enabled: false,
3892 arch: {
3893 arm: {
3894 enabled: true,
3895 },
3896 },
3897 system_shared_libs: [],
3898 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003899 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003900 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003901 `+vndkLibrariesTxtFiles("current"),
3902 withFiles(map[string][]byte{
3903 "libvndk.so": nil,
3904 "libvndk.arm.so": nil,
3905 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09003906 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003907 "lib/libvndk.so",
3908 "lib/libvndk.arm.so",
3909 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003910 "lib/libc++.so",
3911 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003912 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003913 })
Jooyung Han344d5432019-08-23 11:17:39 +09003914}
3915
Jooyung Han39edb6c2019-11-06 16:53:07 +09003916func vndkLibrariesTxtFiles(vers ...string) (result string) {
3917 for _, v := range vers {
3918 if v == "current" {
Justin Yund5784122023-10-25 13:25:32 +09003919 for _, txt := range []string{"vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003920 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003921 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003922 name: "` + txt + `.libraries.txt",
Justin Yund5784122023-10-25 13:25:32 +09003923 insert_vndk_version: true,
Jooyung Han39edb6c2019-11-06 16:53:07 +09003924 }
3925 `
3926 }
Justin Yund5784122023-10-25 13:25:32 +09003927 result += `
3928 llndk_libraries_txt {
3929 name: "llndk.libraries.txt",
3930 }
3931 llndk_libraries_txt_for_apex {
3932 name: "llndk.libraries.txt.apex",
3933 stem: "llndk.libraries.txt",
3934 insert_vndk_version: true,
3935 }
3936 `
Jooyung Han39edb6c2019-11-06 16:53:07 +09003937 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003938 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003939 result += `
3940 prebuilt_etc {
3941 name: "` + txt + `.libraries.` + v + `.txt",
3942 src: "dummy.txt",
3943 }
3944 `
3945 }
3946 }
3947 }
3948 return
3949}
3950
Jooyung Han344d5432019-08-23 11:17:39 +09003951func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003952 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003953 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003954 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003955 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003956 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003957 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003958 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003959 }
3960
3961 apex_key {
3962 name: "myapex.key",
3963 public_key: "testkey.avbpubkey",
3964 private_key: "testkey.pem",
3965 }
3966
Jooyung Han31c470b2019-10-18 16:26:59 +09003967 vndk_prebuilt_shared {
3968 name: "libvndk27",
3969 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003970 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003971 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003972 vndk: {
3973 enabled: true,
3974 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003975 target_arch: "arm64",
3976 arch: {
3977 arm: {
3978 srcs: ["libvndk27_arm.so"],
3979 },
3980 arm64: {
3981 srcs: ["libvndk27_arm64.so"],
3982 },
3983 },
Colin Cross2807f002021-03-02 10:15:29 -08003984 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003985 }
3986
3987 vndk_prebuilt_shared {
3988 name: "libvndk27",
3989 version: "27",
3990 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003991 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003992 vndk: {
3993 enabled: true,
3994 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003995 target_arch: "x86_64",
3996 arch: {
3997 x86: {
3998 srcs: ["libvndk27_x86.so"],
3999 },
4000 x86_64: {
4001 srcs: ["libvndk27_x86_64.so"],
4002 },
4003 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09004004 }
4005 `+vndkLibrariesTxtFiles("27"),
4006 withFiles(map[string][]byte{
4007 "libvndk27_arm.so": nil,
4008 "libvndk27_arm64.so": nil,
4009 "libvndk27_x86.so": nil,
4010 "libvndk27_x86_64.so": nil,
4011 }))
Jooyung Han344d5432019-08-23 11:17:39 +09004012
Jooyung Hana0503a52023-08-23 13:12:50 +09004013 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09004014 "lib/libvndk27_arm.so",
4015 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004016 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09004017 })
Jooyung Han344d5432019-08-23 11:17:39 +09004018}
4019
Jooyung Han90eee022019-10-01 20:02:42 +09004020func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004021 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09004022 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004023 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09004024 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004025 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004026 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09004027 }
4028 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004029 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09004030 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004031 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09004032 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004033 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09004034 }
4035 apex_key {
4036 name: "myapex.key",
4037 public_key: "testkey.avbpubkey",
4038 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004039 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09004040
4041 assertApexName := func(expected, moduleName string) {
Jooyung Hana0503a52023-08-23 13:12:50 +09004042 module := ctx.ModuleForTests(moduleName, "android_common")
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09004043 apexManifestRule := module.Rule("apexManifestRule")
4044 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09004045 }
4046
Jiyong Parkf58c46e2021-04-01 21:35:20 +09004047 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08004048 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09004049}
4050
Jooyung Han344d5432019-08-23 11:17:39 +09004051func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004052 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09004053 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004054 name: "com.android.vndk.current",
4055 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004056 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004057 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09004058 }
4059
4060 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08004061 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09004062 public_key: "testkey.avbpubkey",
4063 private_key: "testkey.pem",
4064 }
4065
4066 cc_library {
4067 name: "libvndk",
4068 srcs: ["mylib.cpp"],
4069 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004070 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09004071 native_bridge_supported: true,
4072 host_supported: true,
4073 vndk: {
4074 enabled: true,
4075 },
4076 system_shared_libs: [],
4077 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08004078 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09004079 }
Colin Cross2807f002021-03-02 10:15:29 -08004080 `+vndkLibrariesTxtFiles("current"),
4081 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09004082
Jooyung Hana0503a52023-08-23 13:12:50 +09004083 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09004084 "lib/libvndk.so",
4085 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09004086 "lib/libc++.so",
4087 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004088 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09004089 })
Jooyung Han344d5432019-08-23 11:17:39 +09004090}
4091
4092func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08004093 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09004094 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004095 name: "com.android.vndk.current",
4096 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004097 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09004098 native_bridge_supported: true,
4099 }
4100
4101 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08004102 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09004103 public_key: "testkey.avbpubkey",
4104 private_key: "testkey.pem",
4105 }
4106
4107 cc_library {
4108 name: "libvndk",
4109 srcs: ["mylib.cpp"],
4110 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004111 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09004112 native_bridge_supported: true,
4113 host_supported: true,
4114 vndk: {
4115 enabled: true,
4116 },
4117 system_shared_libs: [],
4118 stl: "none",
4119 }
4120 `)
4121}
4122
Jooyung Han31c470b2019-10-18 16:26:59 +09004123func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004124 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09004125 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004126 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09004127 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004128 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09004129 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004130 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09004131 }
4132
4133 apex_key {
4134 name: "myapex.key",
4135 public_key: "testkey.avbpubkey",
4136 private_key: "testkey.pem",
4137 }
4138
4139 vndk_prebuilt_shared {
4140 name: "libvndk27",
4141 version: "27",
4142 target_arch: "arm",
4143 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004144 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004145 vndk: {
4146 enabled: true,
4147 },
4148 arch: {
4149 arm: {
4150 srcs: ["libvndk27.so"],
4151 }
4152 },
4153 }
4154
4155 vndk_prebuilt_shared {
4156 name: "libvndk27",
4157 version: "27",
4158 target_arch: "arm",
4159 binder32bit: true,
4160 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004161 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004162 vndk: {
4163 enabled: true,
4164 },
4165 arch: {
4166 arm: {
4167 srcs: ["libvndk27binder32.so"],
4168 }
4169 },
Colin Cross2807f002021-03-02 10:15:29 -08004170 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09004171 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09004172 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09004173 withFiles(map[string][]byte{
4174 "libvndk27.so": nil,
4175 "libvndk27binder32.so": nil,
4176 }),
4177 withBinder32bit,
4178 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07004179 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09004180 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
4181 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09004182 },
4183 }),
4184 )
4185
Jooyung Hana0503a52023-08-23 13:12:50 +09004186 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09004187 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004188 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09004189 })
4190}
4191
Jooyung Han45a96772020-06-15 14:59:42 +09004192func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004193 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09004194 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004195 name: "com.android.vndk.current",
4196 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004197 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004198 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09004199 }
4200
4201 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08004202 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004203 public_key: "testkey.avbpubkey",
4204 private_key: "testkey.pem",
4205 }
4206
4207 cc_library {
4208 name: "libz",
4209 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004210 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09004211 vndk: {
4212 enabled: true,
4213 },
4214 stubs: {
4215 symbol_file: "libz.map.txt",
4216 versions: ["30"],
4217 }
4218 }
4219 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
4220 "libz.map.txt": nil,
4221 }))
4222
Jooyung Hana0503a52023-08-23 13:12:50 +09004223 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09004224 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
4225 ensureListEmpty(t, provideNativeLibs)
Jooyung Hana0503a52023-08-23 13:12:50 +09004226 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common", []string{
Jooyung Han1724d582022-12-21 10:17:44 +09004227 "out/soong/.intermediates/libz/android_vendor.29_arm64_armv8-a_shared/libz.so:lib64/libz.so",
4228 "out/soong/.intermediates/libz/android_vendor.29_arm_armv7-a-neon_shared/libz.so:lib/libz.so",
4229 "*/*",
4230 })
Jooyung Han45a96772020-06-15 14:59:42 +09004231}
4232
Jooyung Hane3f02812023-05-08 13:54:50 +09004233func TestVendorApexWithVndkPrebuilts(t *testing.T) {
4234 ctx := testApex(t, "",
4235 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4236 variables.DeviceVndkVersion = proptools.StringPtr("27")
4237 }),
4238 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
4239 cc.RegisterVendorSnapshotModules(ctx)
4240 }),
4241 withFiles(map[string][]byte{
4242 "vendor/foo/Android.bp": []byte(`
4243 apex {
4244 name: "myapex",
4245 binaries: ["foo"],
4246 key: "myapex.key",
4247 min_sdk_version: "27",
4248 vendor: true,
4249 }
4250
4251 cc_binary {
4252 name: "foo",
4253 vendor: true,
4254 srcs: ["abc.cpp"],
4255 shared_libs: [
4256 "libllndk",
4257 "libvndk",
4258 ],
4259 nocrt: true,
4260 system_shared_libs: [],
4261 min_sdk_version: "27",
4262 }
4263
4264 apex_key {
4265 name: "myapex.key",
4266 public_key: "testkey.avbpubkey",
4267 private_key: "testkey.pem",
4268 }
4269 `),
4270 // Simulate VNDK prebuilts with vendor_snapshot
4271 "prebuilts/vndk/Android.bp": []byte(`
4272 vndk_prebuilt_shared {
4273 name: "libllndk",
4274 version: "27",
4275 vendor_available: true,
4276 product_available: true,
4277 target_arch: "arm64",
4278 arch: {
4279 arm64: {
4280 srcs: ["libllndk.so"],
4281 },
4282 },
4283 }
4284
4285 vndk_prebuilt_shared {
4286 name: "libvndk",
4287 version: "27",
4288 vendor_available: true,
4289 product_available: true,
4290 target_arch: "arm64",
4291 arch: {
4292 arm64: {
4293 srcs: ["libvndk.so"],
4294 },
4295 },
4296 vndk: {
4297 enabled: true,
4298 },
4299 min_sdk_version: "27",
4300 }
4301
4302 vndk_prebuilt_shared {
4303 name: "libc++",
4304 version: "27",
4305 target_arch: "arm64",
4306 vendor_available: true,
4307 product_available: true,
4308 vndk: {
4309 enabled: true,
4310 support_system_process: true,
4311 },
4312 arch: {
4313 arm64: {
4314 srcs: ["libc++.so"],
4315 },
4316 },
4317 min_sdk_version: "apex_inherit",
4318 }
4319
4320 vendor_snapshot {
4321 name: "vendor_snapshot",
4322 version: "27",
4323 arch: {
4324 arm64: {
4325 vndk_libs: [
4326 "libc++",
4327 "libllndk",
4328 "libvndk",
4329 ],
4330 static_libs: [
4331 "libc++demangle",
4332 "libclang_rt.builtins",
4333 "libunwind",
4334 ],
4335 },
4336 }
4337 }
4338
4339 vendor_snapshot_static {
4340 name: "libclang_rt.builtins",
4341 version: "27",
4342 target_arch: "arm64",
4343 vendor: true,
4344 arch: {
4345 arm64: {
4346 src: "libclang_rt.builtins-aarch64-android.a",
4347 },
4348 },
4349 }
4350
4351 vendor_snapshot_static {
4352 name: "libc++demangle",
4353 version: "27",
4354 target_arch: "arm64",
4355 compile_multilib: "64",
4356 vendor: true,
4357 arch: {
4358 arm64: {
4359 src: "libc++demangle.a",
4360 },
4361 },
4362 min_sdk_version: "apex_inherit",
4363 }
4364
4365 vendor_snapshot_static {
4366 name: "libunwind",
4367 version: "27",
4368 target_arch: "arm64",
4369 compile_multilib: "64",
4370 vendor: true,
4371 arch: {
4372 arm64: {
4373 src: "libunwind.a",
4374 },
4375 },
4376 min_sdk_version: "apex_inherit",
4377 }
4378 `),
4379 }))
4380
4381 // Should embed the prebuilt VNDK libraries in the apex
Jooyung Hana0503a52023-08-23 13:12:50 +09004382 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Hane3f02812023-05-08 13:54:50 +09004383 "bin/foo",
4384 "prebuilts/vndk/libc++.so:lib64/libc++.so",
4385 "prebuilts/vndk/libvndk.so:lib64/libvndk.so",
4386 })
4387
4388 // Should link foo with prebuilt libraries (shared/static)
4389 ldRule := ctx.ModuleForTests("foo", "android_vendor.27_arm64_armv8-a_myapex").Rule("ld")
4390 android.AssertStringDoesContain(t, "should link to prebuilt llndk", ldRule.Args["libFlags"], "prebuilts/vndk/libllndk.so")
4391 android.AssertStringDoesContain(t, "should link to prebuilt vndk", ldRule.Args["libFlags"], "prebuilts/vndk/libvndk.so")
4392 android.AssertStringDoesContain(t, "should link to prebuilt libc++demangle", ldRule.Args["libFlags"], "prebuilts/vndk/libc++demangle.a")
4393 android.AssertStringDoesContain(t, "should link to prebuilt libunwind", ldRule.Args["libFlags"], "prebuilts/vndk/libunwind.a")
4394
4395 // Should declare the LLNDK library as a "required" external dependency
Jooyung Hana0503a52023-08-23 13:12:50 +09004396 manifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Hane3f02812023-05-08 13:54:50 +09004397 requireNativeLibs := names(manifestRule.Args["requireNativeLibs"])
4398 ensureListContains(t, requireNativeLibs, "libllndk.so")
4399}
4400
Jooyung Hane1633032019-08-01 17:41:43 +09004401func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004402 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09004403 apex {
4404 name: "myapex_nodep",
4405 key: "myapex.key",
4406 native_shared_libs: ["lib_nodep"],
4407 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004408 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004409 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004410 }
4411
4412 apex {
4413 name: "myapex_dep",
4414 key: "myapex.key",
4415 native_shared_libs: ["lib_dep"],
4416 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004417 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004418 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004419 }
4420
4421 apex {
4422 name: "myapex_provider",
4423 key: "myapex.key",
4424 native_shared_libs: ["libfoo"],
4425 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004426 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004427 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004428 }
4429
4430 apex {
4431 name: "myapex_selfcontained",
4432 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00004433 native_shared_libs: ["lib_dep_on_bar", "libbar"],
Jooyung Hane1633032019-08-01 17:41:43 +09004434 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004435 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004436 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004437 }
4438
4439 apex_key {
4440 name: "myapex.key",
4441 public_key: "testkey.avbpubkey",
4442 private_key: "testkey.pem",
4443 }
4444
4445 cc_library {
4446 name: "lib_nodep",
4447 srcs: ["mylib.cpp"],
4448 system_shared_libs: [],
4449 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004450 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004451 }
4452
4453 cc_library {
4454 name: "lib_dep",
4455 srcs: ["mylib.cpp"],
4456 shared_libs: ["libfoo"],
4457 system_shared_libs: [],
4458 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004459 apex_available: [
4460 "myapex_dep",
4461 "myapex_provider",
4462 "myapex_selfcontained",
4463 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004464 }
4465
4466 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00004467 name: "lib_dep_on_bar",
4468 srcs: ["mylib.cpp"],
4469 shared_libs: ["libbar"],
4470 system_shared_libs: [],
4471 stl: "none",
4472 apex_available: [
4473 "myapex_selfcontained",
4474 ],
4475 }
4476
4477
4478 cc_library {
Jooyung Hane1633032019-08-01 17:41:43 +09004479 name: "libfoo",
4480 srcs: ["mytest.cpp"],
4481 stubs: {
4482 versions: ["1"],
4483 },
4484 system_shared_libs: [],
4485 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004486 apex_available: [
4487 "myapex_provider",
Spandan Das20fce2d2023-04-12 17:21:39 +00004488 ],
4489 }
4490
4491 cc_library {
4492 name: "libbar",
4493 srcs: ["mytest.cpp"],
4494 stubs: {
4495 versions: ["1"],
4496 },
4497 system_shared_libs: [],
4498 stl: "none",
4499 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004500 "myapex_selfcontained",
4501 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004502 }
Spandan Das20fce2d2023-04-12 17:21:39 +00004503
Jooyung Hane1633032019-08-01 17:41:43 +09004504 `)
4505
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004506 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004507 var provideNativeLibs, requireNativeLibs []string
4508
Jooyung Hana0503a52023-08-23 13:12:50 +09004509 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004510 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4511 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004512 ensureListEmpty(t, provideNativeLibs)
4513 ensureListEmpty(t, requireNativeLibs)
4514
Jooyung Hana0503a52023-08-23 13:12:50 +09004515 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004516 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4517 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004518 ensureListEmpty(t, provideNativeLibs)
4519 ensureListContains(t, requireNativeLibs, "libfoo.so")
4520
Jooyung Hana0503a52023-08-23 13:12:50 +09004521 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004522 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4523 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004524 ensureListContains(t, provideNativeLibs, "libfoo.so")
4525 ensureListEmpty(t, requireNativeLibs)
4526
Jooyung Hana0503a52023-08-23 13:12:50 +09004527 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004528 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4529 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Spandan Das20fce2d2023-04-12 17:21:39 +00004530 ensureListContains(t, provideNativeLibs, "libbar.so")
Jooyung Hane1633032019-08-01 17:41:43 +09004531 ensureListEmpty(t, requireNativeLibs)
4532}
4533
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004534func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4535 ctx := testApex(t, `
4536 apex {
4537 name: "myapex",
4538 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004539 native_shared_libs: ["mylib"],
4540 updatable: false,
4541 }
4542
4543 apex_key {
4544 name: "myapex.key",
4545 public_key: "testkey.avbpubkey",
4546 private_key: "testkey.pem",
4547 }
4548
4549 cc_library {
4550 name: "mylib",
4551 srcs: ["mylib.cpp"],
4552 system_shared_libs: [],
4553 stl: "none",
4554 apex_available: [
4555 "//apex_available:platform",
4556 "myapex",
4557 ],
4558 }
4559 `, android.FixtureMergeEnv(map[string]string{
4560 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4561 }))
4562
Jooyung Hana0503a52023-08-23 13:12:50 +09004563 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004564 apexManifestRule := module.Rule("apexManifestRule")
4565 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4566}
4567
Vinh Tran8f5310f2022-10-07 18:16:47 -04004568func TestCompileMultilibProp(t *testing.T) {
4569 testCases := []struct {
4570 compileMultiLibProp string
4571 containedLibs []string
4572 notContainedLibs []string
4573 }{
4574 {
4575 containedLibs: []string{
4576 "image.apex/lib64/mylib.so",
4577 "image.apex/lib/mylib.so",
4578 },
4579 compileMultiLibProp: `compile_multilib: "both",`,
4580 },
4581 {
4582 containedLibs: []string{"image.apex/lib64/mylib.so"},
4583 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4584 compileMultiLibProp: `compile_multilib: "first",`,
4585 },
4586 {
4587 containedLibs: []string{"image.apex/lib64/mylib.so"},
4588 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4589 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4590 },
4591 {
4592 containedLibs: []string{"image.apex/lib64/mylib.so"},
4593 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4594 compileMultiLibProp: `compile_multilib: "64",`,
4595 },
4596 {
4597 containedLibs: []string{"image.apex/lib/mylib.so"},
4598 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4599 compileMultiLibProp: `compile_multilib: "32",`,
4600 },
4601 }
4602 for _, testCase := range testCases {
4603 ctx := testApex(t, fmt.Sprintf(`
4604 apex {
4605 name: "myapex",
4606 key: "myapex.key",
4607 %s
4608 native_shared_libs: ["mylib"],
4609 updatable: false,
4610 }
4611 apex_key {
4612 name: "myapex.key",
4613 public_key: "testkey.avbpubkey",
4614 private_key: "testkey.pem",
4615 }
4616 cc_library {
4617 name: "mylib",
4618 srcs: ["mylib.cpp"],
4619 apex_available: [
4620 "//apex_available:platform",
4621 "myapex",
4622 ],
4623 }
4624 `, testCase.compileMultiLibProp),
4625 )
Jooyung Hana0503a52023-08-23 13:12:50 +09004626 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Vinh Tran8f5310f2022-10-07 18:16:47 -04004627 apexRule := module.Rule("apexRule")
4628 copyCmds := apexRule.Args["copy_commands"]
4629 for _, containedLib := range testCase.containedLibs {
4630 ensureContains(t, copyCmds, containedLib)
4631 }
4632 for _, notContainedLib := range testCase.notContainedLibs {
4633 ensureNotContains(t, copyCmds, notContainedLib)
4634 }
4635 }
4636}
4637
Alex Light0851b882019-02-07 13:20:53 -08004638func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004639 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004640 apex {
4641 name: "myapex",
4642 key: "myapex.key",
4643 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004644 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004645 }
4646
4647 apex_key {
4648 name: "myapex.key",
4649 public_key: "testkey.avbpubkey",
4650 private_key: "testkey.pem",
4651 }
4652
4653 cc_library {
4654 name: "mylib_common",
4655 srcs: ["mylib.cpp"],
4656 system_shared_libs: [],
4657 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004658 apex_available: [
4659 "//apex_available:platform",
4660 "myapex",
4661 ],
Alex Light0851b882019-02-07 13:20:53 -08004662 }
4663 `)
4664
Jooyung Hana0503a52023-08-23 13:12:50 +09004665 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004666 apexRule := module.Rule("apexRule")
4667 copyCmds := apexRule.Args["copy_commands"]
4668
4669 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4670 t.Log("Apex was a test apex!")
4671 t.Fail()
4672 }
4673 // Ensure that main rule creates an output
4674 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4675
4676 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004677 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004678
4679 // Ensure that both direct and indirect deps are copied into apex
4680 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4681
Colin Cross7113d202019-11-20 16:39:12 -08004682 // Ensure that the platform variant ends with _shared
4683 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004684
Colin Cross56a83212020-09-15 18:30:11 -07004685 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004686 t.Log("Found mylib_common not in any apex!")
4687 t.Fail()
4688 }
4689}
4690
4691func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004692 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004693 apex_test {
4694 name: "myapex",
4695 key: "myapex.key",
4696 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004697 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004698 }
4699
4700 apex_key {
4701 name: "myapex.key",
4702 public_key: "testkey.avbpubkey",
4703 private_key: "testkey.pem",
4704 }
4705
4706 cc_library {
4707 name: "mylib_common_test",
4708 srcs: ["mylib.cpp"],
4709 system_shared_libs: [],
4710 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004711 // TODO: remove //apex_available:platform
4712 apex_available: [
4713 "//apex_available:platform",
4714 "myapex",
4715 ],
Alex Light0851b882019-02-07 13:20:53 -08004716 }
4717 `)
4718
Jooyung Hana0503a52023-08-23 13:12:50 +09004719 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Alex Light0851b882019-02-07 13:20:53 -08004720 apexRule := module.Rule("apexRule")
4721 copyCmds := apexRule.Args["copy_commands"]
4722
4723 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4724 t.Log("Apex was not a test apex!")
4725 t.Fail()
4726 }
4727 // Ensure that main rule creates an output
4728 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4729
4730 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004731 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004732
4733 // Ensure that both direct and indirect deps are copied into apex
4734 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4735
Colin Cross7113d202019-11-20 16:39:12 -08004736 // Ensure that the platform variant ends with _shared
4737 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004738}
4739
Jooyung Han85707de2023-12-01 14:21:13 +09004740func TestLibzVendorIsntStable(t *testing.T) {
4741 ctx := testApex(t, `
4742 apex {
4743 name: "myapex",
4744 key: "myapex.key",
4745 updatable: false,
4746 binaries: ["mybin"],
4747 }
4748 apex {
4749 name: "myvendorapex",
4750 key: "myapex.key",
4751 file_contexts: "myvendorapex_file_contexts",
4752 vendor: true,
4753 updatable: false,
4754 binaries: ["mybin"],
4755 }
4756 apex_key {
4757 name: "myapex.key",
4758 public_key: "testkey.avbpubkey",
4759 private_key: "testkey.pem",
4760 }
4761 cc_binary {
4762 name: "mybin",
4763 vendor_available: true,
4764 system_shared_libs: [],
4765 stl: "none",
4766 shared_libs: ["libz"],
4767 apex_available: ["//apex_available:anyapex"],
4768 }
4769 cc_library {
4770 name: "libz",
4771 vendor_available: true,
4772 system_shared_libs: [],
4773 stl: "none",
4774 stubs: {
4775 versions: ["28", "30"],
4776 },
4777 target: {
4778 vendor: {
4779 no_stubs: true,
4780 },
4781 },
4782 }
4783 `, withFiles(map[string][]byte{
4784 "myvendorapex_file_contexts": nil,
4785 }))
4786
4787 // libz provides stubs for core variant.
4788 {
4789 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
4790 "bin/mybin",
4791 })
4792 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
4793 android.AssertStringEquals(t, "should require libz", apexManifestRule.Args["requireNativeLibs"], "libz.so")
4794 }
4795 // libz doesn't provide stubs for vendor variant.
4796 {
4797 ensureExactContents(t, ctx, "myvendorapex", "android_common_myvendorapex", []string{
4798 "bin/mybin",
4799 "lib64/libz.so",
4800 })
4801 apexManifestRule := ctx.ModuleForTests("myvendorapex", "android_common_myvendorapex").Rule("apexManifestRule")
4802 android.AssertStringEquals(t, "should not require libz", apexManifestRule.Args["requireNativeLibs"], "")
4803 }
4804}
4805
Alex Light9670d332019-01-29 18:07:33 -08004806func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004807 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004808 apex {
4809 name: "myapex",
4810 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004811 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004812 multilib: {
4813 first: {
4814 native_shared_libs: ["mylib_common"],
4815 }
4816 },
4817 target: {
4818 android: {
4819 multilib: {
4820 first: {
4821 native_shared_libs: ["mylib"],
4822 }
4823 }
4824 },
4825 host: {
4826 multilib: {
4827 first: {
4828 native_shared_libs: ["mylib2"],
4829 }
4830 }
4831 }
4832 }
4833 }
4834
4835 apex_key {
4836 name: "myapex.key",
4837 public_key: "testkey.avbpubkey",
4838 private_key: "testkey.pem",
4839 }
4840
4841 cc_library {
4842 name: "mylib",
4843 srcs: ["mylib.cpp"],
4844 system_shared_libs: [],
4845 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004846 // TODO: remove //apex_available:platform
4847 apex_available: [
4848 "//apex_available:platform",
4849 "myapex",
4850 ],
Alex Light9670d332019-01-29 18:07:33 -08004851 }
4852
4853 cc_library {
4854 name: "mylib_common",
4855 srcs: ["mylib.cpp"],
4856 system_shared_libs: [],
4857 stl: "none",
4858 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004859 // TODO: remove //apex_available:platform
4860 apex_available: [
4861 "//apex_available:platform",
4862 "myapex",
4863 ],
Alex Light9670d332019-01-29 18:07:33 -08004864 }
4865
4866 cc_library {
4867 name: "mylib2",
4868 srcs: ["mylib.cpp"],
4869 system_shared_libs: [],
4870 stl: "none",
4871 compile_multilib: "first",
4872 }
4873 `)
4874
Jooyung Hana0503a52023-08-23 13:12:50 +09004875 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004876 copyCmds := apexRule.Args["copy_commands"]
4877
4878 // Ensure that main rule creates an output
4879 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4880
4881 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004882 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4883 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4884 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004885
4886 // Ensure that both direct and indirect deps are copied into apex
4887 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4888 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4889 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4890
Colin Cross7113d202019-11-20 16:39:12 -08004891 // Ensure that the platform variant ends with _shared
4892 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4893 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4894 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004895}
Jiyong Park04480cf2019-02-06 00:16:29 +09004896
Jiyong Park59140302020-12-14 18:44:04 +09004897func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004898 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004899 apex {
4900 name: "myapex",
4901 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004902 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004903 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004904 arch: {
4905 arm64: {
4906 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004907 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004908 },
4909 x86_64: {
4910 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004911 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004912 },
4913 }
4914 }
4915
4916 apex_key {
4917 name: "myapex.key",
4918 public_key: "testkey.avbpubkey",
4919 private_key: "testkey.pem",
4920 }
4921
4922 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004923 name: "mylib.generic",
4924 srcs: ["mylib.cpp"],
4925 system_shared_libs: [],
4926 stl: "none",
4927 // TODO: remove //apex_available:platform
4928 apex_available: [
4929 "//apex_available:platform",
4930 "myapex",
4931 ],
4932 }
4933
4934 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004935 name: "mylib.arm64",
4936 srcs: ["mylib.cpp"],
4937 system_shared_libs: [],
4938 stl: "none",
4939 // TODO: remove //apex_available:platform
4940 apex_available: [
4941 "//apex_available:platform",
4942 "myapex",
4943 ],
4944 }
4945
4946 cc_library {
4947 name: "mylib.x64",
4948 srcs: ["mylib.cpp"],
4949 system_shared_libs: [],
4950 stl: "none",
4951 // TODO: remove //apex_available:platform
4952 apex_available: [
4953 "//apex_available:platform",
4954 "myapex",
4955 ],
4956 }
4957 `)
4958
Jooyung Hana0503a52023-08-23 13:12:50 +09004959 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park59140302020-12-14 18:44:04 +09004960 copyCmds := apexRule.Args["copy_commands"]
4961
4962 // Ensure that apex variant is created for the direct dep
4963 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004964 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004965 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4966
4967 // Ensure that both direct and indirect deps are copied into apex
4968 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4969 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4970}
4971
Jiyong Park04480cf2019-02-06 00:16:29 +09004972func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004973 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004974 apex {
4975 name: "myapex",
4976 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004977 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004978 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004979 }
4980
4981 apex_key {
4982 name: "myapex.key",
4983 public_key: "testkey.avbpubkey",
4984 private_key: "testkey.pem",
4985 }
4986
4987 sh_binary {
4988 name: "myscript",
4989 src: "mylib.cpp",
4990 filename: "myscript.sh",
4991 sub_dir: "script",
4992 }
4993 `)
4994
Jooyung Hana0503a52023-08-23 13:12:50 +09004995 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004996 copyCmds := apexRule.Args["copy_commands"]
4997
4998 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4999}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09005000
Jooyung Han91df2082019-11-20 01:49:42 +09005001func TestApexInVariousPartition(t *testing.T) {
5002 testcases := []struct {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09005003 propName, partition string
Jooyung Han91df2082019-11-20 01:49:42 +09005004 }{
Jooyung Haneec1b3f2023-06-20 16:25:59 +09005005 {"", "system"},
5006 {"product_specific: true", "product"},
5007 {"soc_specific: true", "vendor"},
5008 {"proprietary: true", "vendor"},
5009 {"vendor: true", "vendor"},
5010 {"system_ext_specific: true", "system_ext"},
Jooyung Han91df2082019-11-20 01:49:42 +09005011 }
5012 for _, tc := range testcases {
Jooyung Haneec1b3f2023-06-20 16:25:59 +09005013 t.Run(tc.propName+":"+tc.partition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005014 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09005015 apex {
5016 name: "myapex",
5017 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005018 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09005019 `+tc.propName+`
5020 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09005021
Jooyung Han91df2082019-11-20 01:49:42 +09005022 apex_key {
5023 name: "myapex.key",
5024 public_key: "testkey.avbpubkey",
5025 private_key: "testkey.pem",
5026 }
5027 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09005028
Jooyung Hana0503a52023-08-23 13:12:50 +09005029 apex := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jooyung Haneec1b3f2023-06-20 16:25:59 +09005030 expected := "out/soong/target/product/test_device/" + tc.partition + "/apex"
Paul Duffin37ba3442021-03-29 00:21:08 +01005031 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09005032 if actual != expected {
5033 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
5034 }
Jooyung Han91df2082019-11-20 01:49:42 +09005035 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09005036 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09005037}
Jiyong Park67882562019-03-21 01:11:21 +09005038
Jooyung Han580eb4f2020-06-24 19:33:06 +09005039func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005040 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09005041 apex {
5042 name: "myapex",
5043 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005044 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09005045 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09005046
Jooyung Han580eb4f2020-06-24 19:33:06 +09005047 apex_key {
5048 name: "myapex.key",
5049 public_key: "testkey.avbpubkey",
5050 private_key: "testkey.pem",
5051 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09005052 `)
Jooyung Hana0503a52023-08-23 13:12:50 +09005053 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09005054 rule := module.Output("file_contexts")
5055 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
5056}
Jooyung Han54aca7b2019-11-20 02:26:02 +09005057
Jooyung Han580eb4f2020-06-24 19:33:06 +09005058func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09005059 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09005060 apex {
5061 name: "myapex",
5062 key: "myapex.key",
5063 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005064 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09005065 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09005066
Jooyung Han580eb4f2020-06-24 19:33:06 +09005067 apex_key {
5068 name: "myapex.key",
5069 public_key: "testkey.avbpubkey",
5070 private_key: "testkey.pem",
5071 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09005072 `, withFiles(map[string][]byte{
5073 "my_own_file_contexts": nil,
5074 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09005075}
Jooyung Han54aca7b2019-11-20 02:26:02 +09005076
Jooyung Han580eb4f2020-06-24 19:33:06 +09005077func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09005078 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09005079 apex {
5080 name: "myapex",
5081 key: "myapex.key",
5082 product_specific: true,
5083 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005084 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09005085 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09005086
Jooyung Han580eb4f2020-06-24 19:33:06 +09005087 apex_key {
5088 name: "myapex.key",
5089 public_key: "testkey.avbpubkey",
5090 private_key: "testkey.pem",
5091 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09005092 `)
5093
Colin Cross1c460562021-02-16 17:55:47 -08005094 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09005095 apex {
5096 name: "myapex",
5097 key: "myapex.key",
5098 product_specific: true,
5099 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005100 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09005101 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09005102
Jooyung Han580eb4f2020-06-24 19:33:06 +09005103 apex_key {
5104 name: "myapex.key",
5105 public_key: "testkey.avbpubkey",
5106 private_key: "testkey.pem",
5107 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09005108 `, withFiles(map[string][]byte{
5109 "product_specific_file_contexts": nil,
5110 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09005111 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09005112 rule := module.Output("file_contexts")
5113 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
5114}
Jooyung Han54aca7b2019-11-20 02:26:02 +09005115
Jooyung Han580eb4f2020-06-24 19:33:06 +09005116func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005117 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09005118 apex {
5119 name: "myapex",
5120 key: "myapex.key",
5121 product_specific: true,
5122 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005123 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09005124 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09005125
Jooyung Han580eb4f2020-06-24 19:33:06 +09005126 apex_key {
5127 name: "myapex.key",
5128 public_key: "testkey.avbpubkey",
5129 private_key: "testkey.pem",
5130 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09005131
Jooyung Han580eb4f2020-06-24 19:33:06 +09005132 filegroup {
5133 name: "my-file-contexts",
5134 srcs: ["product_specific_file_contexts"],
5135 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09005136 `, withFiles(map[string][]byte{
5137 "product_specific_file_contexts": nil,
5138 }))
Jooyung Hana0503a52023-08-23 13:12:50 +09005139 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han580eb4f2020-06-24 19:33:06 +09005140 rule := module.Output("file_contexts")
5141 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09005142}
5143
Jiyong Park67882562019-03-21 01:11:21 +09005144func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005145 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09005146 apex_key {
5147 name: "myapex.key",
5148 public_key: ":my.avbpubkey",
5149 private_key: ":my.pem",
5150 product_specific: true,
5151 }
5152
5153 filegroup {
5154 name: "my.avbpubkey",
5155 srcs: ["testkey2.avbpubkey"],
5156 }
5157
5158 filegroup {
5159 name: "my.pem",
5160 srcs: ["testkey2.pem"],
5161 }
5162 `)
5163
5164 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
5165 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08005166 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09005167 if actual_pubkey != expected_pubkey {
5168 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
5169 }
5170 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08005171 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09005172 if actual_privkey != expected_privkey {
5173 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
5174 }
5175}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07005176
5177func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005178 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07005179 prebuilt_apex {
5180 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09005181 arch: {
5182 arm64: {
5183 src: "myapex-arm64.apex",
5184 },
5185 arm: {
5186 src: "myapex-arm.apex",
5187 },
5188 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07005189 }
5190 `)
5191
Wei Li340ee8e2022-03-18 17:33:24 -07005192 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
5193 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07005194
Jiyong Parkc95714e2019-03-29 14:23:10 +09005195 expectedInput := "myapex-arm64.apex"
5196 if prebuilt.inputApex.String() != expectedInput {
5197 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
5198 }
Wei Li340ee8e2022-03-18 17:33:24 -07005199 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
5200 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
5201 rule := testingModule.Rule("genProvenanceMetaData")
5202 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
5203 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
5204 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
5205 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08005206
5207 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
5208 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07005209}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01005210
Paul Duffinc0609c62021-03-01 17:27:16 +00005211func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01005212 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00005213 prebuilt_apex {
5214 name: "myapex",
5215 }
5216 `)
5217}
5218
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01005219func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005220 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01005221 prebuilt_apex {
5222 name: "myapex",
5223 src: "myapex-arm.apex",
5224 filename: "notmyapex.apex",
5225 }
5226 `)
5227
Wei Li340ee8e2022-03-18 17:33:24 -07005228 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
5229 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01005230
5231 expected := "notmyapex.apex"
5232 if p.installFilename != expected {
5233 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
5234 }
Wei Li340ee8e2022-03-18 17:33:24 -07005235 rule := testingModule.Rule("genProvenanceMetaData")
5236 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
5237 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
5238 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
5239 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01005240}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07005241
Samiul Islam7c02e262021-09-08 17:48:28 +01005242func TestApexSetFilenameOverride(t *testing.T) {
5243 testApex(t, `
5244 apex_set {
5245 name: "com.company.android.myapex",
5246 apex_name: "com.android.myapex",
5247 set: "company-myapex.apks",
5248 filename: "com.company.android.myapex.apex"
5249 }
5250 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
5251
5252 testApex(t, `
5253 apex_set {
5254 name: "com.company.android.myapex",
5255 apex_name: "com.android.myapex",
5256 set: "company-myapex.apks",
5257 filename: "com.company.android.myapex.capex"
5258 }
5259 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
5260
5261 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
5262 apex_set {
5263 name: "com.company.android.myapex",
5264 apex_name: "com.android.myapex",
5265 set: "company-myapex.apks",
5266 filename: "some-random-suffix"
5267 }
5268 `)
5269}
5270
Jaewoong Jung22f7d182019-07-16 18:25:41 -07005271func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005272 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07005273 prebuilt_apex {
5274 name: "myapex.prebuilt",
5275 src: "myapex-arm.apex",
5276 overrides: [
5277 "myapex",
5278 ],
5279 }
5280 `)
5281
Wei Li340ee8e2022-03-18 17:33:24 -07005282 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
5283 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07005284
5285 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07005286 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07005287 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09005288 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07005289 }
Wei Li340ee8e2022-03-18 17:33:24 -07005290 rule := testingModule.Rule("genProvenanceMetaData")
5291 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
5292 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
5293 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
5294 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07005295}
5296
Martin Stjernholmbfffae72021-06-24 14:37:13 +01005297func TestPrebuiltApexName(t *testing.T) {
5298 testApex(t, `
5299 prebuilt_apex {
5300 name: "com.company.android.myapex",
5301 apex_name: "com.android.myapex",
5302 src: "company-myapex-arm.apex",
5303 }
5304 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
5305
5306 testApex(t, `
5307 apex_set {
5308 name: "com.company.android.myapex",
5309 apex_name: "com.android.myapex",
5310 set: "company-myapex.apks",
5311 }
5312 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
5313}
5314
5315func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
5316 _ = android.GroupFixturePreparers(
5317 java.PrepareForTestWithJavaDefaultModules,
5318 PrepareForTestWithApexBuildComponents,
5319 android.FixtureWithRootAndroidBp(`
5320 platform_bootclasspath {
5321 name: "platform-bootclasspath",
5322 fragments: [
5323 {
5324 apex: "com.android.art",
5325 module: "art-bootclasspath-fragment",
5326 },
5327 ],
5328 }
5329
5330 prebuilt_apex {
5331 name: "com.company.android.art",
5332 apex_name: "com.android.art",
5333 src: "com.company.android.art-arm.apex",
5334 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
5335 }
5336
5337 prebuilt_bootclasspath_fragment {
5338 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01005339 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01005340 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01005341 hidden_api: {
5342 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5343 metadata: "my-bootclasspath-fragment/metadata.csv",
5344 index: "my-bootclasspath-fragment/index.csv",
5345 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5346 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5347 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01005348 }
5349
5350 java_import {
5351 name: "core-oj",
5352 jars: ["prebuilt.jar"],
5353 }
5354 `),
5355 ).RunTest(t)
5356}
5357
Paul Duffin092153d2021-01-26 11:42:39 +00005358// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
5359// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00005360func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01005361 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00005362
Paul Duffin89886cb2021-02-05 16:44:03 +00005363 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01005364 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00005365 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08005366 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01005367 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00005368 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09005369 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
5370 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
5371 android.NormalizePathForTesting(dexJarBuildPath))
5372 }
5373
5374 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01005375 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09005376 // Make sure the import has been given the correct path to the dex jar.
5377 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
5378 dexJarBuildPath := p.DexJarInstallPath()
5379 stem := android.RemoveOptionalPrebuiltPrefix(name)
5380 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
5381 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
5382 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00005383 }
5384
Paul Duffin39853512021-02-26 11:09:39 +00005385 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01005386 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00005387 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09005388 android.AssertArrayString(t, "Check if there is no source variant",
5389 []string{"android_common"},
5390 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00005391 }
5392
5393 t.Run("prebuilt only", func(t *testing.T) {
5394 bp := `
5395 prebuilt_apex {
5396 name: "myapex",
5397 arch: {
5398 arm64: {
5399 src: "myapex-arm64.apex",
5400 },
5401 arm: {
5402 src: "myapex-arm.apex",
5403 },
5404 },
Paul Duffin39853512021-02-26 11:09:39 +00005405 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005406 }
5407
5408 java_import {
5409 name: "libfoo",
5410 jars: ["libfoo.jar"],
5411 }
Paul Duffin39853512021-02-26 11:09:39 +00005412
5413 java_sdk_library_import {
5414 name: "libbar",
5415 public: {
5416 jars: ["libbar.jar"],
5417 },
5418 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005419 `
5420
5421 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5422 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5423
Martin Stjernholm44825602021-09-17 01:44:12 +01005424 deapexerName := deapexerModuleName("myapex")
5425 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
5426
Paul Duffinf6932af2021-02-26 18:21:56 +00005427 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01005428 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00005429 rule := deapexer.Rule("deapexer")
5430 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
5431 t.Errorf("expected: %q, found: %q", expected, actual)
5432 }
5433
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005434 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01005435 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005436 rule = prebuiltApex.Rule("android/soong/android.Cp")
5437 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
5438 t.Errorf("expected: %q, found: %q", expected, actual)
5439 }
5440
Paul Duffin89886cb2021-02-05 16:44:03 +00005441 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005442 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005443
5444 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005445 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005446 })
5447
5448 t.Run("prebuilt with source preferred", func(t *testing.T) {
5449
5450 bp := `
5451 prebuilt_apex {
5452 name: "myapex",
5453 arch: {
5454 arm64: {
5455 src: "myapex-arm64.apex",
5456 },
5457 arm: {
5458 src: "myapex-arm.apex",
5459 },
5460 },
Paul Duffin39853512021-02-26 11:09:39 +00005461 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005462 }
5463
5464 java_import {
5465 name: "libfoo",
5466 jars: ["libfoo.jar"],
5467 }
5468
5469 java_library {
5470 name: "libfoo",
5471 }
Paul Duffin39853512021-02-26 11:09:39 +00005472
5473 java_sdk_library_import {
5474 name: "libbar",
5475 public: {
5476 jars: ["libbar.jar"],
5477 },
5478 }
5479
5480 java_sdk_library {
5481 name: "libbar",
5482 srcs: ["foo/bar/MyClass.java"],
5483 unsafe_ignore_missing_latest_api: true,
5484 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005485 `
5486
5487 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5488 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5489
Paul Duffin89886cb2021-02-05 16:44:03 +00005490 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005491 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005492 ensureNoSourceVariant(t, ctx, "libfoo")
5493
5494 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005495 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005496 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005497 })
5498
5499 t.Run("prebuilt preferred with source", func(t *testing.T) {
5500 bp := `
5501 prebuilt_apex {
5502 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005503 arch: {
5504 arm64: {
5505 src: "myapex-arm64.apex",
5506 },
5507 arm: {
5508 src: "myapex-arm.apex",
5509 },
5510 },
Paul Duffin39853512021-02-26 11:09:39 +00005511 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005512 }
5513
5514 java_import {
5515 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005516 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005517 jars: ["libfoo.jar"],
5518 }
5519
5520 java_library {
5521 name: "libfoo",
5522 }
Paul Duffin39853512021-02-26 11:09:39 +00005523
5524 java_sdk_library_import {
5525 name: "libbar",
5526 prefer: true,
5527 public: {
5528 jars: ["libbar.jar"],
5529 },
5530 }
5531
5532 java_sdk_library {
5533 name: "libbar",
5534 srcs: ["foo/bar/MyClass.java"],
5535 unsafe_ignore_missing_latest_api: true,
5536 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005537 `
5538
5539 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5540 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5541
Paul Duffin89886cb2021-02-05 16:44:03 +00005542 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005543 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005544 ensureNoSourceVariant(t, ctx, "libfoo")
5545
5546 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005547 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005548 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005549 })
5550}
5551
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005552func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005553 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005554 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005555 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5556 // is disabled.
5557 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
5558 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005559
Paul Duffin37856732021-02-26 14:24:15 +00005560 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5561 t.Helper()
Jiakai Zhangc6879f32023-11-06 16:31:19 +00005562 s := ctx.ModuleForTests("dex_bootjars", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005563 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005564 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005565 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005566 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005567 foundLibfooJar = true
5568 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005569 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005570 }
5571 }
5572 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005573 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 +00005574 }
5575 }
5576
Paul Duffin40a3f652021-07-19 13:11:24 +01005577 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005578 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005579 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005580 var rule android.TestingBuildParams
5581
5582 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5583 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005584 }
5585
Paul Duffin40a3f652021-07-19 13:11:24 +01005586 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5587 t.Helper()
5588 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5589 var rule android.TestingBuildParams
5590
5591 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5592 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5593 }
5594
Paul Duffin89f570a2021-06-16 01:42:33 +01005595 fragment := java.ApexVariantReference{
5596 Apex: proptools.StringPtr("myapex"),
5597 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5598 }
5599
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005600 t.Run("prebuilt only", func(t *testing.T) {
5601 bp := `
5602 prebuilt_apex {
5603 name: "myapex",
5604 arch: {
5605 arm64: {
5606 src: "myapex-arm64.apex",
5607 },
5608 arm: {
5609 src: "myapex-arm.apex",
5610 },
5611 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005612 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5613 }
5614
5615 prebuilt_bootclasspath_fragment {
5616 name: "my-bootclasspath-fragment",
5617 contents: ["libfoo", "libbar"],
5618 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005619 hidden_api: {
5620 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5621 metadata: "my-bootclasspath-fragment/metadata.csv",
5622 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005623 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5624 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5625 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005626 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005627 }
5628
5629 java_import {
5630 name: "libfoo",
5631 jars: ["libfoo.jar"],
5632 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005633 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005634 }
Paul Duffin37856732021-02-26 14:24:15 +00005635
5636 java_sdk_library_import {
5637 name: "libbar",
5638 public: {
5639 jars: ["libbar.jar"],
5640 },
5641 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005642 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005643 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005644 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005645 `
5646
Paul Duffin89f570a2021-06-16 01:42:33 +01005647 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005648 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5649 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005650
Paul Duffin537ea3d2021-05-14 10:38:00 +01005651 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005652 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005653 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005654 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005655 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005656 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 +01005657 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005658 })
5659
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005660 t.Run("apex_set only", func(t *testing.T) {
5661 bp := `
5662 apex_set {
5663 name: "myapex",
5664 set: "myapex.apks",
Liz Kammer2dc72442023-04-20 10:10:48 -04005665 exported_java_libs: ["myjavalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005666 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
Liz Kammer2dc72442023-04-20 10:10:48 -04005667 exported_systemserverclasspath_fragments: ["my-systemserverclasspath-fragment"],
5668 }
5669
5670 java_import {
5671 name: "myjavalib",
5672 jars: ["myjavalib.jar"],
5673 apex_available: ["myapex"],
5674 permitted_packages: ["javalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005675 }
5676
5677 prebuilt_bootclasspath_fragment {
5678 name: "my-bootclasspath-fragment",
5679 contents: ["libfoo", "libbar"],
5680 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005681 hidden_api: {
5682 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5683 metadata: "my-bootclasspath-fragment/metadata.csv",
5684 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005685 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5686 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5687 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005688 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005689 }
5690
Liz Kammer2dc72442023-04-20 10:10:48 -04005691 prebuilt_systemserverclasspath_fragment {
5692 name: "my-systemserverclasspath-fragment",
5693 contents: ["libbaz"],
5694 apex_available: ["myapex"],
5695 }
5696
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005697 java_import {
5698 name: "libfoo",
5699 jars: ["libfoo.jar"],
5700 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005701 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005702 }
5703
5704 java_sdk_library_import {
5705 name: "libbar",
5706 public: {
5707 jars: ["libbar.jar"],
5708 },
5709 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005710 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005711 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005712 }
Liz Kammer2dc72442023-04-20 10:10:48 -04005713
5714 java_sdk_library_import {
5715 name: "libbaz",
5716 public: {
5717 jars: ["libbaz.jar"],
5718 },
5719 apex_available: ["myapex"],
5720 shared_library: false,
5721 permitted_packages: ["baz"],
5722 }
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005723 `
5724
Paul Duffin89f570a2021-06-16 01:42:33 +01005725 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005726 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5727 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5728
Paul Duffin537ea3d2021-05-14 10:38:00 +01005729 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005730 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005731 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005732 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005733 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005734 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 +01005735 `)
Liz Kammer2dc72442023-04-20 10:10:48 -04005736
5737 myApex := ctx.ModuleForTests("myapex", "android_common_myapex").Module()
5738
5739 overrideNames := []string{
5740 "",
5741 "myjavalib.myapex",
5742 "libfoo.myapex",
5743 "libbar.myapex",
5744 "libbaz.myapex",
5745 }
5746 mkEntries := android.AndroidMkEntriesForTest(t, ctx, myApex)
5747 for i, e := range mkEntries {
5748 g := e.OverrideName
5749 if w := overrideNames[i]; w != g {
5750 t.Errorf("Expected override name %q, got %q", w, g)
5751 }
5752 }
5753
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005754 })
5755
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005756 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5757 bp := `
5758 prebuilt_apex {
5759 name: "myapex",
5760 arch: {
5761 arm64: {
5762 src: "myapex-arm64.apex",
5763 },
5764 arm: {
5765 src: "myapex-arm.apex",
5766 },
5767 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005768 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5769 }
5770
5771 prebuilt_bootclasspath_fragment {
5772 name: "my-bootclasspath-fragment",
5773 contents: ["libfoo", "libbar"],
5774 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005775 hidden_api: {
5776 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5777 metadata: "my-bootclasspath-fragment/metadata.csv",
5778 index: "my-bootclasspath-fragment/index.csv",
5779 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5780 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5781 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005782 }
5783
5784 java_import {
5785 name: "libfoo",
5786 jars: ["libfoo.jar"],
5787 apex_available: ["myapex"],
5788 }
5789
5790 java_library {
5791 name: "libfoo",
5792 srcs: ["foo/bar/MyClass.java"],
5793 apex_available: ["myapex"],
5794 }
Paul Duffin37856732021-02-26 14:24:15 +00005795
5796 java_sdk_library_import {
5797 name: "libbar",
5798 public: {
5799 jars: ["libbar.jar"],
5800 },
5801 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005802 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005803 }
5804
5805 java_sdk_library {
5806 name: "libbar",
5807 srcs: ["foo/bar/MyClass.java"],
5808 unsafe_ignore_missing_latest_api: true,
5809 apex_available: ["myapex"],
5810 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005811 `
5812
5813 // In this test the source (java_library) libfoo is active since the
5814 // prebuilt (java_import) defaults to prefer:false. However the
5815 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5816 // find the dex boot jar in it. We either need to disable the source libfoo
5817 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005818 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005819 // dexbootjar check is skipped if AllowMissingDependencies is true
5820 preparerAllowMissingDeps := android.GroupFixturePreparers(
5821 preparer,
5822 android.PrepareForTestWithAllowMissingDependencies,
5823 )
5824 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005825 })
5826
5827 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5828 bp := `
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005829 apex {
5830 name: "myapex",
5831 key: "myapex.key",
5832 updatable: false,
5833 bootclasspath_fragments: ["my-bootclasspath-fragment"],
5834 }
5835
5836 apex_key {
5837 name: "myapex.key",
5838 public_key: "testkey.avbpubkey",
5839 private_key: "testkey.pem",
5840 }
5841
5842 bootclasspath_fragment {
5843 name: "my-bootclasspath-fragment",
5844 contents: ["libfoo", "libbar"],
5845 apex_available: ["myapex"],
5846 hidden_api: {
5847 split_packages: ["*"],
5848 },
5849 }
5850
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005851 prebuilt_apex {
5852 name: "myapex",
5853 arch: {
5854 arm64: {
5855 src: "myapex-arm64.apex",
5856 },
5857 arm: {
5858 src: "myapex-arm.apex",
5859 },
5860 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005861 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5862 }
5863
5864 prebuilt_bootclasspath_fragment {
5865 name: "my-bootclasspath-fragment",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005866 prefer: true,
Paul Duffin89f570a2021-06-16 01:42:33 +01005867 contents: ["libfoo", "libbar"],
5868 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005869 hidden_api: {
5870 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5871 metadata: "my-bootclasspath-fragment/metadata.csv",
5872 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005873 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5874 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5875 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005876 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005877 }
5878
5879 java_import {
5880 name: "libfoo",
5881 prefer: true,
5882 jars: ["libfoo.jar"],
5883 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005884 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005885 }
5886
5887 java_library {
5888 name: "libfoo",
5889 srcs: ["foo/bar/MyClass.java"],
5890 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005891 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005892 }
Paul Duffin37856732021-02-26 14:24:15 +00005893
5894 java_sdk_library_import {
5895 name: "libbar",
5896 prefer: true,
5897 public: {
5898 jars: ["libbar.jar"],
5899 },
5900 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005901 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005902 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005903 }
5904
5905 java_sdk_library {
5906 name: "libbar",
5907 srcs: ["foo/bar/MyClass.java"],
5908 unsafe_ignore_missing_latest_api: true,
5909 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005910 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00005911 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005912 `
5913
Paul Duffin89f570a2021-06-16 01:42:33 +01005914 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005915 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5916 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005917
Paul Duffin537ea3d2021-05-14 10:38:00 +01005918 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005919 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005920 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005921 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005922 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005923 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 +01005924 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005925 })
5926
5927 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5928 bp := `
5929 apex {
5930 name: "myapex",
5931 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005932 updatable: false,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005933 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005934 }
5935
5936 apex_key {
5937 name: "myapex.key",
5938 public_key: "testkey.avbpubkey",
5939 private_key: "testkey.pem",
5940 }
5941
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005942 bootclasspath_fragment {
5943 name: "my-bootclasspath-fragment",
5944 contents: ["libfoo", "libbar"],
5945 apex_available: ["myapex"],
5946 hidden_api: {
5947 split_packages: ["*"],
5948 },
5949 }
5950
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005951 prebuilt_apex {
5952 name: "myapex",
5953 arch: {
5954 arm64: {
5955 src: "myapex-arm64.apex",
5956 },
5957 arm: {
5958 src: "myapex-arm.apex",
5959 },
5960 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005961 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5962 }
5963
5964 prebuilt_bootclasspath_fragment {
5965 name: "my-bootclasspath-fragment",
5966 contents: ["libfoo", "libbar"],
5967 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005968 hidden_api: {
5969 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5970 metadata: "my-bootclasspath-fragment/metadata.csv",
5971 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005972 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5973 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5974 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005975 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005976 }
5977
5978 java_import {
5979 name: "libfoo",
5980 jars: ["libfoo.jar"],
5981 apex_available: ["myapex"],
5982 }
5983
5984 java_library {
5985 name: "libfoo",
5986 srcs: ["foo/bar/MyClass.java"],
5987 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005988 permitted_packages: ["foo"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01005989 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005990 }
Paul Duffin37856732021-02-26 14:24:15 +00005991
5992 java_sdk_library_import {
5993 name: "libbar",
5994 public: {
5995 jars: ["libbar.jar"],
5996 },
5997 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005998 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005999 }
6000
6001 java_sdk_library {
6002 name: "libbar",
6003 srcs: ["foo/bar/MyClass.java"],
6004 unsafe_ignore_missing_latest_api: true,
6005 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01006006 permitted_packages: ["bar"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01006007 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00006008 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006009 `
6010
Paul Duffin89f570a2021-06-16 01:42:33 +01006011 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Jiakai Zhangc6879f32023-11-06 16:31:19 +00006012 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/hiddenapi-modular/encoded/libfoo.jar")
6013 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 +00006014
Paul Duffin537ea3d2021-05-14 10:38:00 +01006015 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01006016 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01006017 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
6018 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01006019 out/soong/.intermediates/my-bootclasspath-fragment/android_common_myapex/modular-hiddenapi/index.csv
6020 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 +01006021 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006022 })
6023
6024 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
6025 bp := `
6026 apex {
6027 name: "myapex",
6028 enabled: false,
6029 key: "myapex.key",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01006030 bootclasspath_fragments: ["my-bootclasspath-fragment"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006031 }
6032
6033 apex_key {
6034 name: "myapex.key",
6035 public_key: "testkey.avbpubkey",
6036 private_key: "testkey.pem",
6037 }
6038
Jiakai Zhangb69e8952023-07-11 14:31:22 +01006039 bootclasspath_fragment {
6040 name: "my-bootclasspath-fragment",
6041 enabled: false,
6042 contents: ["libfoo", "libbar"],
6043 apex_available: ["myapex"],
6044 hidden_api: {
6045 split_packages: ["*"],
6046 },
6047 }
6048
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006049 prebuilt_apex {
6050 name: "myapex",
6051 arch: {
6052 arm64: {
6053 src: "myapex-arm64.apex",
6054 },
6055 arm: {
6056 src: "myapex-arm.apex",
6057 },
6058 },
Paul Duffin89f570a2021-06-16 01:42:33 +01006059 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
6060 }
6061
6062 prebuilt_bootclasspath_fragment {
6063 name: "my-bootclasspath-fragment",
6064 contents: ["libfoo", "libbar"],
6065 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01006066 hidden_api: {
6067 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
6068 metadata: "my-bootclasspath-fragment/metadata.csv",
6069 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01006070 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
6071 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
6072 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01006073 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006074 }
6075
6076 java_import {
6077 name: "libfoo",
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006078 jars: ["libfoo.jar"],
6079 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01006080 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006081 }
6082
6083 java_library {
6084 name: "libfoo",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01006085 enabled: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006086 srcs: ["foo/bar/MyClass.java"],
6087 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01006088 installable: true,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006089 }
Paul Duffin37856732021-02-26 14:24:15 +00006090
6091 java_sdk_library_import {
6092 name: "libbar",
Paul Duffin37856732021-02-26 14:24:15 +00006093 public: {
6094 jars: ["libbar.jar"],
6095 },
6096 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01006097 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01006098 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00006099 }
6100
6101 java_sdk_library {
6102 name: "libbar",
Jiakai Zhangb69e8952023-07-11 14:31:22 +01006103 enabled: false,
Paul Duffin37856732021-02-26 14:24:15 +00006104 srcs: ["foo/bar/MyClass.java"],
6105 unsafe_ignore_missing_latest_api: true,
6106 apex_available: ["myapex"],
Jiakai Zhangb69e8952023-07-11 14:31:22 +01006107 compile_dex: true,
Paul Duffin37856732021-02-26 14:24:15 +00006108 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006109 `
6110
Paul Duffin89f570a2021-06-16 01:42:33 +01006111 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01006112 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
6113 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00006114
Paul Duffin537ea3d2021-05-14 10:38:00 +01006115 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01006116 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01006117 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01006118 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01006119 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
Jiakai Zhangb69e8952023-07-11 14:31:22 +01006120 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 +01006121 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00006122 })
6123}
6124
Roland Levillain630846d2019-06-26 12:48:34 +01006125func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006126 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01006127 apex_test {
6128 name: "myapex",
6129 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006130 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01006131 tests: [
6132 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01006133 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01006134 ],
6135 }
6136
6137 apex_key {
6138 name: "myapex.key",
6139 public_key: "testkey.avbpubkey",
6140 private_key: "testkey.pem",
6141 }
6142
Liz Kammer1c14a212020-05-12 15:26:55 -07006143 filegroup {
6144 name: "fg",
6145 srcs: [
6146 "baz",
6147 "bar/baz"
6148 ],
6149 }
6150
Roland Levillain630846d2019-06-26 12:48:34 +01006151 cc_test {
6152 name: "mytest",
6153 gtest: false,
6154 srcs: ["mytest.cpp"],
6155 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09006156 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01006157 system_shared_libs: [],
6158 static_executable: true,
6159 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07006160 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01006161 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01006162
Jiyong Parkaf9539f2020-05-04 10:31:32 +09006163 cc_library {
6164 name: "mylib",
6165 srcs: ["mylib.cpp"],
6166 system_shared_libs: [],
6167 stl: "none",
6168 }
6169
Liz Kammer5bd365f2020-05-27 15:15:11 -07006170 filegroup {
6171 name: "fg2",
6172 srcs: [
6173 "testdata/baz"
6174 ],
6175 }
6176
Roland Levillain9b5fde92019-06-28 15:41:19 +01006177 cc_test {
6178 name: "mytests",
6179 gtest: false,
6180 srcs: [
6181 "mytest1.cpp",
6182 "mytest2.cpp",
6183 "mytest3.cpp",
6184 ],
6185 test_per_src: true,
6186 relative_install_path: "test",
6187 system_shared_libs: [],
6188 static_executable: true,
6189 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07006190 data: [
6191 ":fg",
6192 ":fg2",
6193 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01006194 }
Roland Levillain630846d2019-06-26 12:48:34 +01006195 `)
6196
Jooyung Hana0503a52023-08-23 13:12:50 +09006197 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01006198 copyCmds := apexRule.Args["copy_commands"]
6199
Jiyong Parkaf9539f2020-05-04 10:31:32 +09006200 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01006201 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09006202 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01006203
Liz Kammer1c14a212020-05-12 15:26:55 -07006204 //Ensure that test data are copied into apex.
6205 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
6206 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
6207
Roland Levillain9b5fde92019-06-28 15:41:19 +01006208 // Ensure that test deps built with `test_per_src` are copied into apex.
6209 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
6210 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
6211 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01006212
6213 // Ensure the module is correctly translated.
Jooyung Hana0503a52023-08-23 13:12:50 +09006214 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07006215 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07006216 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01006217 prefix := "TARGET_"
6218 var builder strings.Builder
6219 data.Custom(&builder, name, prefix, "", data)
6220 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006221 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
6222 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
6223 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
6224 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01006225 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Roland Levillain630846d2019-06-26 12:48:34 +01006226}
6227
Jooyung Hand48f3c32019-08-23 11:18:57 +09006228func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
6229 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
6230 apex {
6231 name: "myapex",
6232 key: "myapex.key",
6233 native_shared_libs: ["libfoo"],
6234 }
6235
6236 apex_key {
6237 name: "myapex.key",
6238 public_key: "testkey.avbpubkey",
6239 private_key: "testkey.pem",
6240 }
6241
6242 cc_library {
6243 name: "libfoo",
6244 stl: "none",
6245 system_shared_libs: [],
6246 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006247 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09006248 }
6249 `)
6250 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
6251 apex {
6252 name: "myapex",
6253 key: "myapex.key",
6254 java_libs: ["myjar"],
6255 }
6256
6257 apex_key {
6258 name: "myapex.key",
6259 public_key: "testkey.avbpubkey",
6260 private_key: "testkey.pem",
6261 }
6262
6263 java_library {
6264 name: "myjar",
6265 srcs: ["foo/bar/MyClass.java"],
6266 sdk_version: "none",
6267 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09006268 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09006269 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09006270 }
6271 `)
6272}
6273
Bill Peckhama41a6962021-01-11 10:58:54 -08006274func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006275 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08006276 apex {
6277 name: "myapex",
6278 key: "myapex.key",
6279 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006280 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08006281 }
6282
6283 apex_key {
6284 name: "myapex.key",
6285 public_key: "testkey.avbpubkey",
6286 private_key: "testkey.pem",
6287 }
6288
6289 java_import {
6290 name: "myjavaimport",
6291 apex_available: ["myapex"],
6292 jars: ["my.jar"],
6293 compile_dex: true,
6294 }
6295 `)
6296
Jooyung Hana0503a52023-08-23 13:12:50 +09006297 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Bill Peckhama41a6962021-01-11 10:58:54 -08006298 apexRule := module.Rule("apexRule")
6299 copyCmds := apexRule.Args["copy_commands"]
6300 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
6301}
6302
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006303func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006304 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006305 apex {
6306 name: "myapex",
6307 key: "myapex.key",
6308 apps: [
6309 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09006310 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006311 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006312 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006313 }
6314
6315 apex_key {
6316 name: "myapex.key",
6317 public_key: "testkey.avbpubkey",
6318 private_key: "testkey.pem",
6319 }
6320
6321 android_app {
6322 name: "AppFoo",
6323 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08006324 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006325 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09006326 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08006327 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006328 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006329 }
Jiyong Parkf7487312019-10-17 12:54:30 +09006330
6331 android_app {
6332 name: "AppFooPriv",
6333 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08006334 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09006335 system_modules: "none",
6336 privileged: true,
Sam Delmerico15809f82023-05-15 17:21:47 -04006337 privapp_allowlist: "privapp_allowlist_com.android.AppFooPriv.xml",
Colin Cross094cde42020-02-15 10:38:00 -08006338 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006339 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09006340 }
Jiyong Park8be103b2019-11-08 15:53:48 +09006341
6342 cc_library_shared {
6343 name: "libjni",
6344 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006345 shared_libs: ["libfoo"],
6346 stl: "none",
6347 system_shared_libs: [],
6348 apex_available: [ "myapex" ],
6349 sdk_version: "current",
6350 }
6351
6352 cc_library_shared {
6353 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09006354 stl: "none",
6355 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09006356 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08006357 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09006358 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006359 `)
6360
Jooyung Hana0503a52023-08-23 13:12:50 +09006361 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006362 apexRule := module.Rule("apexRule")
6363 copyCmds := apexRule.Args["copy_commands"]
6364
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006365 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
6366 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Andrei Onea580636b2022-08-17 16:53:46 +00006367 ensureContains(t, copyCmds, "image.apex/etc/permissions/privapp_allowlist_com.android.AppFooPriv.xml")
Jiyong Park52cd06f2019-11-11 10:14:32 +09006368
Colin Crossaede88c2020-08-11 12:17:01 -07006369 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006370 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09006371 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006372 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09006373 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006374 // JNI libraries including transitive deps are
6375 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01006376 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006377 // ... embedded inside APK (jnilibs.zip)
6378 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
6379 // ... and not directly inside the APEX
6380 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
6381 }
Sam Delmericob1daccd2023-05-25 14:45:30 -04006382
6383 apexBundle := module.Module().(*apexBundle)
6384 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
6385 var builder strings.Builder
6386 data.Custom(&builder, apexBundle.Name(), "TARGET_", "", data)
6387 androidMk := builder.String()
6388 ensureContains(t, androidMk, "LOCAL_MODULE := AppFooPriv.myapex")
6389 ensureContains(t, androidMk, "LOCAL_MODULE := AppFoo.myapex")
6390 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFooPriv.apk")
6391 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALLED_MODULE := \\S+AppFoo.apk")
6392 ensureMatches(t, androidMk, "LOCAL_SOONG_INSTALL_PAIRS := \\S+AppFooPriv.apk")
6393 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 +01006394}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006395
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006396func TestApexWithAppImportBuildId(t *testing.T) {
6397 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
6398 for _, id := range invalidBuildIds {
6399 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
6400 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6401 variables.BuildId = proptools.StringPtr(id)
6402 })
6403 testApexError(t, message, `apex {
6404 name: "myapex",
6405 key: "myapex.key",
6406 apps: ["AppFooPrebuilt"],
6407 updatable: false,
6408 }
6409
6410 apex_key {
6411 name: "myapex.key",
6412 public_key: "testkey.avbpubkey",
6413 private_key: "testkey.pem",
6414 }
6415
6416 android_app_import {
6417 name: "AppFooPrebuilt",
6418 apk: "PrebuiltAppFoo.apk",
6419 presigned: true,
6420 apex_available: ["myapex"],
6421 }
6422 `, fixture)
6423 }
6424}
6425
Dario Frenicde2a032019-10-27 00:29:22 +01006426func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006427 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01006428 apex {
6429 name: "myapex",
6430 key: "myapex.key",
6431 apps: [
6432 "AppFooPrebuilt",
6433 "AppFooPrivPrebuilt",
6434 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006435 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01006436 }
6437
6438 apex_key {
6439 name: "myapex.key",
6440 public_key: "testkey.avbpubkey",
6441 private_key: "testkey.pem",
6442 }
6443
6444 android_app_import {
6445 name: "AppFooPrebuilt",
6446 apk: "PrebuiltAppFoo.apk",
6447 presigned: true,
6448 dex_preopt: {
6449 enabled: false,
6450 },
Jiyong Park592a6a42020-04-21 22:34:28 +09006451 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006452 }
6453
6454 android_app_import {
6455 name: "AppFooPrivPrebuilt",
6456 apk: "PrebuiltAppFooPriv.apk",
6457 privileged: true,
6458 presigned: true,
6459 dex_preopt: {
6460 enabled: false,
6461 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006462 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006463 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006464 }
6465 `)
6466
Jooyung Hana0503a52023-08-23 13:12:50 +09006467 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Frenicde2a032019-10-27 00:29:22 +01006468 apexRule := module.Rule("apexRule")
6469 copyCmds := apexRule.Args["copy_commands"]
6470
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006471 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6472 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006473}
6474
6475func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006476 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006477 apex {
6478 name: "myapex",
6479 key: "myapex.key",
6480 apps: [
6481 "AppFoo",
6482 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006483 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006484 }
6485
6486 apex_key {
6487 name: "myapex.key",
6488 public_key: "testkey.avbpubkey",
6489 private_key: "testkey.pem",
6490 }
6491
6492 android_app {
6493 name: "AppFoo",
6494 srcs: ["foo/bar/MyClass.java"],
6495 sdk_version: "none",
6496 system_modules: "none",
6497 apex_available: [ "myapex" ],
6498 }
6499
6500 android_app_import {
6501 name: "AppFoo",
6502 apk: "AppFooPrebuilt.apk",
6503 filename: "AppFooPrebuilt.apk",
6504 presigned: true,
6505 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006506 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006507 }
6508 `, withFiles(map[string][]byte{
6509 "AppFooPrebuilt.apk": nil,
6510 }))
6511
Jooyung Hana0503a52023-08-23 13:12:50 +09006512 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006513 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006514 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006515}
6516
Dario Freni6f3937c2019-12-20 22:58:03 +00006517func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006518 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006519 apex {
6520 name: "myapex",
6521 key: "myapex.key",
6522 apps: [
6523 "TesterHelpAppFoo",
6524 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006525 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006526 }
6527
6528 apex_key {
6529 name: "myapex.key",
6530 public_key: "testkey.avbpubkey",
6531 private_key: "testkey.pem",
6532 }
6533
6534 android_test_helper_app {
6535 name: "TesterHelpAppFoo",
6536 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006537 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006538 }
6539
6540 `)
6541
Jooyung Hana0503a52023-08-23 13:12:50 +09006542 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dario Freni6f3937c2019-12-20 22:58:03 +00006543 apexRule := module.Rule("apexRule")
6544 copyCmds := apexRule.Args["copy_commands"]
6545
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006546 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006547}
6548
Jooyung Han18020ea2019-11-13 10:50:48 +09006549func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6550 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006551 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006552 apex {
6553 name: "myapex",
6554 key: "myapex.key",
6555 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006556 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006557 }
6558
6559 apex_key {
6560 name: "myapex.key",
6561 public_key: "testkey.avbpubkey",
6562 private_key: "testkey.pem",
6563 }
6564
6565 apex {
6566 name: "otherapex",
6567 key: "myapex.key",
6568 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006569 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006570 }
6571
6572 cc_defaults {
6573 name: "libfoo-defaults",
6574 apex_available: ["otherapex"],
6575 }
6576
6577 cc_library {
6578 name: "libfoo",
6579 defaults: ["libfoo-defaults"],
6580 stl: "none",
6581 system_shared_libs: [],
6582 }`)
6583}
6584
Paul Duffine52e66f2020-03-30 17:54:29 +01006585func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006586 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006587 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006588 apex {
6589 name: "myapex",
6590 key: "myapex.key",
6591 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006592 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006593 }
6594
6595 apex_key {
6596 name: "myapex.key",
6597 public_key: "testkey.avbpubkey",
6598 private_key: "testkey.pem",
6599 }
6600
6601 apex {
6602 name: "otherapex",
6603 key: "otherapex.key",
6604 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006605 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006606 }
6607
6608 apex_key {
6609 name: "otherapex.key",
6610 public_key: "testkey.avbpubkey",
6611 private_key: "testkey.pem",
6612 }
6613
6614 cc_library {
6615 name: "libfoo",
6616 stl: "none",
6617 system_shared_libs: [],
6618 apex_available: ["otherapex"],
6619 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006620}
Jiyong Park127b40b2019-09-30 16:04:35 +09006621
Paul Duffine52e66f2020-03-30 17:54:29 +01006622func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006623 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006624 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006625.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006626.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006627.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006628.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006629.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006630.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006631 apex {
6632 name: "myapex",
6633 key: "myapex.key",
6634 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006635 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006636 }
6637
6638 apex_key {
6639 name: "myapex.key",
6640 public_key: "testkey.avbpubkey",
6641 private_key: "testkey.pem",
6642 }
6643
Jiyong Park127b40b2019-09-30 16:04:35 +09006644 cc_library {
6645 name: "libfoo",
6646 stl: "none",
6647 shared_libs: ["libbar"],
6648 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006649 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006650 }
6651
6652 cc_library {
6653 name: "libbar",
6654 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006655 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006656 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006657 apex_available: ["myapex"],
6658 }
6659
6660 cc_library {
6661 name: "libbaz",
6662 stl: "none",
6663 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006664 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006665}
Jiyong Park127b40b2019-09-30 16:04:35 +09006666
Liz Kammer5f108fa2023-05-11 14:33:17 -04006667func TestApexAvailable_IndirectStaticDep(t *testing.T) {
6668 testApex(t, `
6669 apex {
6670 name: "myapex",
6671 key: "myapex.key",
6672 native_shared_libs: ["libfoo"],
6673 updatable: false,
6674 }
6675
6676 apex_key {
6677 name: "myapex.key",
6678 public_key: "testkey.avbpubkey",
6679 private_key: "testkey.pem",
6680 }
6681
6682 cc_library {
6683 name: "libfoo",
6684 stl: "none",
6685 static_libs: ["libbar"],
6686 system_shared_libs: [],
6687 apex_available: ["myapex"],
6688 }
6689
6690 cc_library {
6691 name: "libbar",
6692 stl: "none",
6693 shared_libs: ["libbaz"],
6694 system_shared_libs: [],
6695 apex_available: ["myapex"],
6696 }
6697
6698 cc_library {
6699 name: "libbaz",
6700 stl: "none",
6701 system_shared_libs: [],
6702 }`)
6703
6704 testApexError(t, `requires "libbar" that doesn't list the APEX under 'apex_available'.`, `
6705 apex {
6706 name: "myapex",
6707 key: "myapex.key",
6708 native_shared_libs: ["libfoo"],
6709 updatable: false,
6710 }
6711
6712 apex_key {
6713 name: "myapex.key",
6714 public_key: "testkey.avbpubkey",
6715 private_key: "testkey.pem",
6716 }
6717
6718 cc_library {
6719 name: "libfoo",
6720 stl: "none",
6721 static_libs: ["libbar"],
6722 system_shared_libs: [],
6723 apex_available: ["myapex"],
6724 }
6725
6726 cc_library {
6727 name: "libbar",
6728 stl: "none",
6729 system_shared_libs: [],
6730 }`)
6731}
6732
Paul Duffine52e66f2020-03-30 17:54:29 +01006733func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006734 testApexError(t, "\"otherapex\" is not a valid module name", `
6735 apex {
6736 name: "myapex",
6737 key: "myapex.key",
6738 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006739 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006740 }
6741
6742 apex_key {
6743 name: "myapex.key",
6744 public_key: "testkey.avbpubkey",
6745 private_key: "testkey.pem",
6746 }
6747
6748 cc_library {
6749 name: "libfoo",
6750 stl: "none",
6751 system_shared_libs: [],
6752 apex_available: ["otherapex"],
6753 }`)
6754
Paul Duffine52e66f2020-03-30 17:54:29 +01006755 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006756 apex {
6757 name: "myapex",
6758 key: "myapex.key",
6759 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006760 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006761 }
6762
6763 apex_key {
6764 name: "myapex.key",
6765 public_key: "testkey.avbpubkey",
6766 private_key: "testkey.pem",
6767 }
6768
6769 cc_library {
6770 name: "libfoo",
6771 stl: "none",
6772 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006773 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006774 apex_available: ["myapex"],
6775 }
6776
6777 cc_library {
6778 name: "libbar",
6779 stl: "none",
6780 system_shared_libs: [],
6781 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006782 }
6783
6784 cc_library {
6785 name: "libbaz",
6786 stl: "none",
6787 system_shared_libs: [],
6788 stubs: {
6789 versions: ["10", "20", "30"],
6790 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006791 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006792}
Jiyong Park127b40b2019-09-30 16:04:35 +09006793
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006794func TestApexAvailable_ApexAvailableNameWithVersionCodeError(t *testing.T) {
6795 t.Run("negative variant_version produces error", func(t *testing.T) {
6796 testApexError(t, "expected an integer between 0-9; got -1", `
6797 apex {
6798 name: "myapex",
6799 key: "myapex.key",
6800 apex_available_name: "com.android.foo",
6801 variant_version: "-1",
6802 updatable: false,
6803 }
6804 apex_key {
6805 name: "myapex.key",
6806 public_key: "testkey.avbpubkey",
6807 private_key: "testkey.pem",
6808 }
6809 `)
6810 })
6811
6812 t.Run("variant_version greater than 9 produces error", func(t *testing.T) {
6813 testApexError(t, "expected an integer between 0-9; got 10", `
6814 apex {
6815 name: "myapex",
6816 key: "myapex.key",
6817 apex_available_name: "com.android.foo",
6818 variant_version: "10",
6819 updatable: false,
6820 }
6821 apex_key {
6822 name: "myapex.key",
6823 public_key: "testkey.avbpubkey",
6824 private_key: "testkey.pem",
6825 }
6826 `)
6827 })
6828}
6829
6830func TestApexAvailable_ApexAvailableNameWithVersionCode(t *testing.T) {
6831 context := android.GroupFixturePreparers(
6832 android.PrepareForIntegrationTestWithAndroid,
6833 PrepareForTestWithApexBuildComponents,
6834 android.FixtureMergeMockFs(android.MockFS{
6835 "system/sepolicy/apex/foo-file_contexts": nil,
6836 "system/sepolicy/apex/bar-file_contexts": nil,
6837 }),
6838 )
6839 result := context.RunTestWithBp(t, `
6840 apex {
6841 name: "foo",
6842 key: "myapex.key",
6843 apex_available_name: "com.android.foo",
6844 variant_version: "0",
6845 updatable: false,
6846 }
6847 apex {
6848 name: "bar",
6849 key: "myapex.key",
6850 apex_available_name: "com.android.foo",
6851 variant_version: "3",
6852 updatable: false,
6853 }
6854 apex_key {
6855 name: "myapex.key",
6856 public_key: "testkey.avbpubkey",
6857 private_key: "testkey.pem",
6858 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006859 override_apex {
6860 name: "myoverrideapex",
6861 base: "bar",
6862 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006863 `)
6864
Jooyung Hana0503a52023-08-23 13:12:50 +09006865 fooManifestRule := result.ModuleForTests("foo", "android_common_foo").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006866 fooExpectedDefaultVersion := android.DefaultUpdatableModuleVersion
6867 fooActualDefaultVersion := fooManifestRule.Args["default_version"]
6868 if fooActualDefaultVersion != fooExpectedDefaultVersion {
6869 t.Errorf("expected to find defaultVersion %q; got %q", fooExpectedDefaultVersion, fooActualDefaultVersion)
6870 }
6871
Jooyung Hana0503a52023-08-23 13:12:50 +09006872 barManifestRule := result.ModuleForTests("bar", "android_common_bar").Rule("apexManifestRule")
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006873 defaultVersionInt, _ := strconv.Atoi(android.DefaultUpdatableModuleVersion)
6874 barExpectedDefaultVersion := fmt.Sprint(defaultVersionInt + 3)
6875 barActualDefaultVersion := barManifestRule.Args["default_version"]
6876 if barActualDefaultVersion != barExpectedDefaultVersion {
6877 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6878 }
Sam Delmerico419f9a32023-07-21 12:00:13 -04006879
Jooyung Hana0503a52023-08-23 13:12:50 +09006880 overrideBarManifestRule := result.ModuleForTests("bar", "android_common_myoverrideapex_bar").Rule("apexManifestRule")
Sam Delmerico419f9a32023-07-21 12:00:13 -04006881 overrideBarActualDefaultVersion := overrideBarManifestRule.Args["default_version"]
6882 if overrideBarActualDefaultVersion != barExpectedDefaultVersion {
6883 t.Errorf("expected to find defaultVersion %q; got %q", barExpectedDefaultVersion, barActualDefaultVersion)
6884 }
Sam Delmerico6d65a0f2023-06-05 15:55:57 -04006885}
6886
Sam Delmericoca816532023-06-02 14:09:50 -04006887func TestApexAvailable_ApexAvailableName(t *testing.T) {
6888 t.Run("using name of apex that sets apex_available_name is not allowed", func(t *testing.T) {
6889 testApexError(t, "Consider adding \"myapex\" to 'apex_available' property of \"AppFoo\"", `
6890 apex {
6891 name: "myapex_sminus",
6892 key: "myapex.key",
6893 apps: ["AppFoo"],
6894 apex_available_name: "myapex",
6895 updatable: false,
6896 }
6897 apex {
6898 name: "myapex",
6899 key: "myapex.key",
6900 apps: ["AppFoo"],
6901 updatable: false,
6902 }
6903 apex_key {
6904 name: "myapex.key",
6905 public_key: "testkey.avbpubkey",
6906 private_key: "testkey.pem",
6907 }
6908 android_app {
6909 name: "AppFoo",
6910 srcs: ["foo/bar/MyClass.java"],
6911 sdk_version: "none",
6912 system_modules: "none",
6913 apex_available: [ "myapex_sminus" ],
6914 }`,
6915 android.FixtureMergeMockFs(android.MockFS{
6916 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6917 }),
6918 )
6919 })
6920
6921 t.Run("apex_available_name allows module to be used in two different apexes", func(t *testing.T) {
6922 testApex(t, `
6923 apex {
6924 name: "myapex_sminus",
6925 key: "myapex.key",
6926 apps: ["AppFoo"],
6927 apex_available_name: "myapex",
6928 updatable: false,
6929 }
6930 apex {
6931 name: "myapex",
6932 key: "myapex.key",
6933 apps: ["AppFoo"],
6934 updatable: false,
6935 }
6936 apex_key {
6937 name: "myapex.key",
6938 public_key: "testkey.avbpubkey",
6939 private_key: "testkey.pem",
6940 }
6941 android_app {
6942 name: "AppFoo",
6943 srcs: ["foo/bar/MyClass.java"],
6944 sdk_version: "none",
6945 system_modules: "none",
6946 apex_available: [ "myapex" ],
6947 }`,
6948 android.FixtureMergeMockFs(android.MockFS{
6949 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
6950 }),
6951 )
6952 })
6953
6954 t.Run("override_apexes work with apex_available_name", func(t *testing.T) {
6955 testApex(t, `
6956 override_apex {
6957 name: "myoverrideapex_sminus",
6958 base: "myapex_sminus",
6959 key: "myapex.key",
6960 apps: ["AppFooOverride"],
6961 }
6962 override_apex {
6963 name: "myoverrideapex",
6964 base: "myapex",
6965 key: "myapex.key",
6966 apps: ["AppFooOverride"],
6967 }
6968 apex {
6969 name: "myapex_sminus",
6970 key: "myapex.key",
6971 apps: ["AppFoo"],
6972 apex_available_name: "myapex",
6973 updatable: false,
6974 }
6975 apex {
6976 name: "myapex",
6977 key: "myapex.key",
6978 apps: ["AppFoo"],
6979 updatable: false,
6980 }
6981 apex_key {
6982 name: "myapex.key",
6983 public_key: "testkey.avbpubkey",
6984 private_key: "testkey.pem",
6985 }
6986 android_app {
6987 name: "AppFooOverride",
6988 srcs: ["foo/bar/MyClass.java"],
6989 sdk_version: "none",
6990 system_modules: "none",
6991 apex_available: [ "myapex" ],
6992 }
6993 android_app {
6994 name: "AppFoo",
6995 srcs: ["foo/bar/MyClass.java"],
6996 sdk_version: "none",
6997 system_modules: "none",
6998 apex_available: [ "myapex" ],
6999 }`,
7000 android.FixtureMergeMockFs(android.MockFS{
7001 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
7002 }),
7003 )
7004 })
7005}
7006
7007func TestApexAvailable_ApexAvailableNameWithOverrides(t *testing.T) {
7008 context := android.GroupFixturePreparers(
7009 android.PrepareForIntegrationTestWithAndroid,
7010 PrepareForTestWithApexBuildComponents,
7011 java.PrepareForTestWithDexpreopt,
7012 android.FixtureMergeMockFs(android.MockFS{
7013 "system/sepolicy/apex/myapex-file_contexts": nil,
7014 "system/sepolicy/apex/myapex_sminus-file_contexts": nil,
7015 }),
7016 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7017 variables.BuildId = proptools.StringPtr("buildid")
7018 }),
7019 )
7020 context.RunTestWithBp(t, `
7021 override_apex {
7022 name: "myoverrideapex_sminus",
7023 base: "myapex_sminus",
7024 }
7025 override_apex {
7026 name: "myoverrideapex",
7027 base: "myapex",
7028 }
7029 apex {
7030 name: "myapex",
7031 key: "myapex.key",
7032 apps: ["AppFoo"],
7033 updatable: false,
7034 }
7035 apex {
7036 name: "myapex_sminus",
7037 apex_available_name: "myapex",
7038 key: "myapex.key",
7039 apps: ["AppFoo_sminus"],
7040 updatable: false,
7041 }
7042 apex_key {
7043 name: "myapex.key",
7044 public_key: "testkey.avbpubkey",
7045 private_key: "testkey.pem",
7046 }
7047 android_app {
7048 name: "AppFoo",
7049 srcs: ["foo/bar/MyClass.java"],
7050 sdk_version: "none",
7051 system_modules: "none",
7052 apex_available: [ "myapex" ],
7053 }
7054 android_app {
7055 name: "AppFoo_sminus",
7056 srcs: ["foo/bar/MyClass.java"],
7057 sdk_version: "none",
7058 min_sdk_version: "29",
7059 system_modules: "none",
7060 apex_available: [ "myapex" ],
7061 }`)
7062}
7063
Jiyong Park89e850a2020-04-07 16:37:39 +09007064func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007065 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09007066 apex {
7067 name: "myapex",
7068 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09007069 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007070 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09007071 }
7072
7073 apex_key {
7074 name: "myapex.key",
7075 public_key: "testkey.avbpubkey",
7076 private_key: "testkey.pem",
7077 }
7078
7079 cc_library {
7080 name: "libfoo",
7081 stl: "none",
7082 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09007083 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09007084 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09007085 }
7086
7087 cc_library {
7088 name: "libfoo2",
7089 stl: "none",
7090 system_shared_libs: [],
7091 shared_libs: ["libbaz"],
7092 apex_available: ["//apex_available:platform"],
7093 }
7094
7095 cc_library {
7096 name: "libbar",
7097 stl: "none",
7098 system_shared_libs: [],
7099 apex_available: ["myapex"],
7100 }
7101
7102 cc_library {
7103 name: "libbaz",
7104 stl: "none",
7105 system_shared_libs: [],
7106 apex_available: ["myapex"],
7107 stubs: {
7108 versions: ["1"],
7109 },
Jiyong Park127b40b2019-09-30 16:04:35 +09007110 }`)
7111
Jiyong Park89e850a2020-04-07 16:37:39 +09007112 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
7113 // because it depends on libbar which isn't available to platform
7114 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
7115 if libfoo.NotAvailableForPlatform() != true {
7116 t.Errorf("%q shouldn't be available to platform", libfoo.String())
7117 }
7118
7119 // libfoo2 however can be available to platform because it depends on libbaz which provides
7120 // stubs
7121 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
7122 if libfoo2.NotAvailableForPlatform() == true {
7123 t.Errorf("%q should be available to platform", libfoo2.String())
7124 }
Paul Duffine52e66f2020-03-30 17:54:29 +01007125}
Jiyong Parka90ca002019-10-07 15:47:24 +09007126
Paul Duffine52e66f2020-03-30 17:54:29 +01007127func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007128 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09007129 apex {
7130 name: "myapex",
7131 key: "myapex.key",
7132 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007133 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09007134 }
7135
7136 apex_key {
7137 name: "myapex.key",
7138 public_key: "testkey.avbpubkey",
7139 private_key: "testkey.pem",
7140 }
7141
7142 cc_library {
7143 name: "libfoo",
7144 stl: "none",
7145 system_shared_libs: [],
7146 apex_available: ["myapex"],
7147 static: {
7148 apex_available: ["//apex_available:platform"],
7149 },
7150 }`)
7151
Jiyong Park89e850a2020-04-07 16:37:39 +09007152 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
7153 if libfooShared.NotAvailableForPlatform() != true {
7154 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
7155 }
7156 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
7157 if libfooStatic.NotAvailableForPlatform() != false {
7158 t.Errorf("%q should be available to platform", libfooStatic.String())
7159 }
Jiyong Park127b40b2019-09-30 16:04:35 +09007160}
7161
Jiyong Park5d790c32019-11-15 18:40:32 +09007162func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007163 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09007164 apex {
7165 name: "myapex",
7166 key: "myapex.key",
7167 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08007168 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07007169 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08007170 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007171 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09007172 }
7173
7174 override_apex {
7175 name: "override_myapex",
7176 base: "myapex",
7177 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08007178 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07007179 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08007180 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08007181 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07007182 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07007183 key: "mynewapex.key",
7184 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09007185 }
7186
7187 apex_key {
7188 name: "myapex.key",
7189 public_key: "testkey.avbpubkey",
7190 private_key: "testkey.pem",
7191 }
7192
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07007193 apex_key {
7194 name: "mynewapex.key",
7195 public_key: "testkey2.avbpubkey",
7196 private_key: "testkey2.pem",
7197 }
7198
7199 android_app_certificate {
7200 name: "myapex.certificate",
7201 certificate: "testkey",
7202 }
7203
Jiyong Park5d790c32019-11-15 18:40:32 +09007204 android_app {
7205 name: "app",
7206 srcs: ["foo/bar/MyClass.java"],
7207 package_name: "foo",
7208 sdk_version: "none",
7209 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007210 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09007211 }
7212
7213 override_android_app {
7214 name: "override_app",
7215 base: "app",
7216 package_name: "bar",
7217 }
markchien7c803b82021-08-26 22:10:06 +08007218
7219 bpf {
7220 name: "bpf",
7221 srcs: ["bpf.c"],
7222 }
7223
7224 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08007225 name: "overrideBpf",
7226 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08007227 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07007228
7229 prebuilt_etc {
7230 name: "myetc",
7231 src: "myprebuilt",
7232 }
7233
7234 prebuilt_etc {
7235 name: "override_myetc",
7236 src: "override_myprebuilt",
7237 }
Jiyong Park20bacab2020-03-03 11:45:41 +09007238 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09007239
Jooyung Hana0503a52023-08-23 13:12:50 +09007240 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(android.OverridableModule)
7241 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex").Module().(android.OverridableModule)
Jiyong Park317645e2019-12-05 13:20:58 +09007242 if originalVariant.GetOverriddenBy() != "" {
7243 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
7244 }
7245 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
7246 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
7247 }
7248
Jooyung Hana0503a52023-08-23 13:12:50 +09007249 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex")
Jiyong Park5d790c32019-11-15 18:40:32 +09007250 apexRule := module.Rule("apexRule")
7251 copyCmds := apexRule.Args["copy_commands"]
7252
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007253 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
7254 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007255
markchien7c803b82021-08-26 22:10:06 +08007256 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08007257 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08007258
Daniel Norman5a3ce132021-08-26 15:44:43 -07007259 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
7260 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
7261
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007262 apexBundle := module.Module().(*apexBundle)
7263 name := apexBundle.Name()
7264 if name != "override_myapex" {
7265 t.Errorf("name should be \"override_myapex\", but was %q", name)
7266 }
7267
Baligh Uddin004d7172020-02-19 21:29:28 -08007268 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
7269 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
7270 }
7271
Jiyong Park20bacab2020-03-03 11:45:41 +09007272 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07007273 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07007274 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
7275
7276 signApkRule := module.Rule("signapk")
7277 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09007278
Colin Crossaa255532020-07-03 13:18:24 -07007279 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007280 var builder strings.Builder
7281 data.Custom(&builder, name, "TARGET_", "", data)
7282 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007283 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
7284 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007285 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08007286 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007287 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08007288 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09007289 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08007290 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09007291}
7292
Albert Martineefabcf2022-03-21 20:11:16 +00007293func TestMinSdkVersionOverride(t *testing.T) {
7294 // Override from 29 to 31
7295 minSdkOverride31 := "31"
7296 ctx := testApex(t, `
7297 apex {
7298 name: "myapex",
7299 key: "myapex.key",
7300 native_shared_libs: ["mylib"],
7301 updatable: true,
7302 min_sdk_version: "29"
7303 }
7304
7305 override_apex {
7306 name: "override_myapex",
7307 base: "myapex",
7308 logging_parent: "com.foo.bar",
7309 package_name: "test.overridden.package"
7310 }
7311
7312 apex_key {
7313 name: "myapex.key",
7314 public_key: "testkey.avbpubkey",
7315 private_key: "testkey.pem",
7316 }
7317
7318 cc_library {
7319 name: "mylib",
7320 srcs: ["mylib.cpp"],
7321 runtime_libs: ["libbar"],
7322 system_shared_libs: [],
7323 stl: "none",
7324 apex_available: [ "myapex" ],
7325 min_sdk_version: "apex_inherit"
7326 }
7327
7328 cc_library {
7329 name: "libbar",
7330 srcs: ["mylib.cpp"],
7331 system_shared_libs: [],
7332 stl: "none",
7333 apex_available: [ "myapex" ],
7334 min_sdk_version: "apex_inherit"
7335 }
7336
7337 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
7338
Jooyung Hana0503a52023-08-23 13:12:50 +09007339 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007340 copyCmds := apexRule.Args["copy_commands"]
7341
7342 // Ensure that direct non-stubs dep is always included
7343 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7344
7345 // Ensure that runtime_libs dep in included
7346 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7347
7348 // Ensure libraries target overridden min_sdk_version value
7349 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7350}
7351
7352func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
7353 // Attempt to override from 31 to 29, should be a NOOP
7354 minSdkOverride29 := "29"
7355 ctx := testApex(t, `
7356 apex {
7357 name: "myapex",
7358 key: "myapex.key",
7359 native_shared_libs: ["mylib"],
7360 updatable: true,
7361 min_sdk_version: "31"
7362 }
7363
7364 override_apex {
7365 name: "override_myapex",
7366 base: "myapex",
7367 logging_parent: "com.foo.bar",
7368 package_name: "test.overridden.package"
7369 }
7370
7371 apex_key {
7372 name: "myapex.key",
7373 public_key: "testkey.avbpubkey",
7374 private_key: "testkey.pem",
7375 }
7376
7377 cc_library {
7378 name: "mylib",
7379 srcs: ["mylib.cpp"],
7380 runtime_libs: ["libbar"],
7381 system_shared_libs: [],
7382 stl: "none",
7383 apex_available: [ "myapex" ],
7384 min_sdk_version: "apex_inherit"
7385 }
7386
7387 cc_library {
7388 name: "libbar",
7389 srcs: ["mylib.cpp"],
7390 system_shared_libs: [],
7391 stl: "none",
7392 apex_available: [ "myapex" ],
7393 min_sdk_version: "apex_inherit"
7394 }
7395
7396 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
7397
Jooyung Hana0503a52023-08-23 13:12:50 +09007398 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule")
Albert Martineefabcf2022-03-21 20:11:16 +00007399 copyCmds := apexRule.Args["copy_commands"]
7400
7401 // Ensure that direct non-stubs dep is always included
7402 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
7403
7404 // Ensure that runtime_libs dep in included
7405 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
7406
7407 // Ensure libraries target the original min_sdk_version value rather than the overridden
7408 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
7409}
7410
Jooyung Han214bf372019-11-12 13:03:50 +09007411func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007412 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09007413 apex {
7414 name: "myapex",
7415 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007416 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09007417 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09007418 }
7419
7420 apex_key {
7421 name: "myapex.key",
7422 public_key: "testkey.avbpubkey",
7423 private_key: "testkey.pem",
7424 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007425
7426 cc_library {
7427 name: "mylib",
7428 srcs: ["mylib.cpp"],
7429 stl: "libc++",
7430 system_shared_libs: [],
7431 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09007432 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007433 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007434 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09007435
Jooyung Hana0503a52023-08-23 13:12:50 +09007436 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Jooyung Han214bf372019-11-12 13:03:50 +09007437 args := module.Rule("apexRule").Args
7438 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00007439 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007440
7441 // The copies of the libraries in the apex should have one more dependency than
7442 // the ones outside the apex, namely the unwinder. Ideally we should check
7443 // the dependency names directly here but for some reason the names are blank in
7444 // this test.
7445 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07007446 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08007447 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
7448 if len(apexImplicits) != len(nonApexImplicits)+1 {
7449 t.Errorf("%q missing unwinder dep", lib)
7450 }
7451 }
Jooyung Han214bf372019-11-12 13:03:50 +09007452}
7453
Paul Duffine05480a2021-03-08 15:07:14 +00007454var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01007455 "api/current.txt": nil,
7456 "api/removed.txt": nil,
7457 "api/system-current.txt": nil,
7458 "api/system-removed.txt": nil,
7459 "api/test-current.txt": nil,
7460 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01007461
Anton Hanssondff2c782020-12-21 17:10:01 +00007462 "100/public/api/foo.txt": nil,
7463 "100/public/api/foo-removed.txt": nil,
7464 "100/system/api/foo.txt": nil,
7465 "100/system/api/foo-removed.txt": nil,
7466
Paul Duffineedc5d52020-06-12 17:46:39 +01007467 // For java_sdk_library_import
7468 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01007469}
7470
Jooyung Han58f26ab2019-12-18 15:34:32 +09007471func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007472 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09007473 apex {
7474 name: "myapex",
7475 key: "myapex.key",
7476 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007477 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09007478 }
7479
7480 apex_key {
7481 name: "myapex.key",
7482 public_key: "testkey.avbpubkey",
7483 private_key: "testkey.pem",
7484 }
7485
7486 java_sdk_library {
7487 name: "foo",
7488 srcs: ["a.java"],
7489 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007490 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09007491 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007492
7493 prebuilt_apis {
7494 name: "sdk",
7495 api_dirs: ["100"],
7496 }
Paul Duffin9b879592020-05-26 13:21:35 +01007497 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09007498
7499 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007500 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09007501 "javalib/foo.jar",
7502 "etc/permissions/foo.xml",
7503 })
7504 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09007505 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00007506 ensureMatches(t, sdkLibrary.RuleParams.Command, `<library\\n\s+name=\\\"foo\\\"\\n\s+file=\\\"/apex/myapex/javalib/foo.jar\\\"`)
Jooyung Han58f26ab2019-12-18 15:34:32 +09007507}
7508
Paul Duffin9b879592020-05-26 13:21:35 +01007509func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007510 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007511 apex {
7512 name: "myapex",
7513 key: "myapex.key",
7514 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007515 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007516 }
7517
7518 apex_key {
7519 name: "myapex.key",
7520 public_key: "testkey.avbpubkey",
7521 private_key: "testkey.pem",
7522 }
7523
7524 java_sdk_library {
7525 name: "foo",
7526 srcs: ["a.java"],
7527 api_packages: ["foo"],
7528 apex_available: ["myapex"],
7529 sdk_version: "none",
7530 system_modules: "none",
7531 }
7532
7533 java_library {
7534 name: "bar",
7535 srcs: ["a.java"],
7536 libs: ["foo"],
7537 apex_available: ["myapex"],
7538 sdk_version: "none",
7539 system_modules: "none",
7540 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007541
7542 prebuilt_apis {
7543 name: "sdk",
7544 api_dirs: ["100"],
7545 }
Paul Duffin9b879592020-05-26 13:21:35 +01007546 `, withFiles(filesForSdkLibrary))
7547
7548 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007549 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007550 "javalib/bar.jar",
7551 "javalib/foo.jar",
7552 "etc/permissions/foo.xml",
7553 })
7554
7555 // The bar library should depend on the implementation jar.
7556 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007557 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007558 t.Errorf("expected %q, found %#q", expected, actual)
7559 }
7560}
7561
7562func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007563 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01007564 apex {
7565 name: "myapex",
7566 key: "myapex.key",
7567 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007568 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01007569 }
7570
7571 apex_key {
7572 name: "myapex.key",
7573 public_key: "testkey.avbpubkey",
7574 private_key: "testkey.pem",
7575 }
7576
7577 java_sdk_library {
7578 name: "foo",
7579 srcs: ["a.java"],
7580 api_packages: ["foo"],
7581 apex_available: ["myapex"],
7582 sdk_version: "none",
7583 system_modules: "none",
7584 }
7585
7586 java_library {
7587 name: "bar",
7588 srcs: ["a.java"],
7589 libs: ["foo"],
7590 sdk_version: "none",
7591 system_modules: "none",
7592 }
Anton Hanssondff2c782020-12-21 17:10:01 +00007593
7594 prebuilt_apis {
7595 name: "sdk",
7596 api_dirs: ["100"],
7597 }
Paul Duffin9b879592020-05-26 13:21:35 +01007598 `, withFiles(filesForSdkLibrary))
7599
7600 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007601 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffin9b879592020-05-26 13:21:35 +01007602 "javalib/foo.jar",
7603 "etc/permissions/foo.xml",
7604 })
7605
7606 // The bar library should depend on the stubs jar.
7607 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007608 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01007609 t.Errorf("expected %q, found %#q", expected, actual)
7610 }
7611}
7612
Paul Duffineedc5d52020-06-12 17:46:39 +01007613func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007614 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00007615 prebuilt_apis {
7616 name: "sdk",
7617 api_dirs: ["100"],
7618 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01007619 withFiles(map[string][]byte{
7620 "apex/a.java": nil,
7621 "apex/apex_manifest.json": nil,
7622 "apex/Android.bp": []byte(`
7623 package {
7624 default_visibility: ["//visibility:private"],
7625 }
7626
7627 apex {
7628 name: "myapex",
7629 key: "myapex.key",
7630 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007631 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007632 }
7633
7634 apex_key {
7635 name: "myapex.key",
7636 public_key: "testkey.avbpubkey",
7637 private_key: "testkey.pem",
7638 }
7639
7640 java_library {
7641 name: "bar",
7642 srcs: ["a.java"],
7643 libs: ["foo"],
7644 apex_available: ["myapex"],
7645 sdk_version: "none",
7646 system_modules: "none",
7647 }
7648`),
7649 "source/a.java": nil,
7650 "source/api/current.txt": nil,
7651 "source/api/removed.txt": nil,
7652 "source/Android.bp": []byte(`
7653 package {
7654 default_visibility: ["//visibility:private"],
7655 }
7656
7657 java_sdk_library {
7658 name: "foo",
7659 visibility: ["//apex"],
7660 srcs: ["a.java"],
7661 api_packages: ["foo"],
7662 apex_available: ["myapex"],
7663 sdk_version: "none",
7664 system_modules: "none",
7665 public: {
7666 enabled: true,
7667 },
7668 }
7669`),
7670 "prebuilt/a.jar": nil,
7671 "prebuilt/Android.bp": []byte(`
7672 package {
7673 default_visibility: ["//visibility:private"],
7674 }
7675
7676 java_sdk_library_import {
7677 name: "foo",
7678 visibility: ["//apex", "//source"],
7679 apex_available: ["myapex"],
7680 prefer: true,
7681 public: {
7682 jars: ["a.jar"],
7683 },
7684 }
7685`),
Anton Hanssondff2c782020-12-21 17:10:01 +00007686 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01007687 )
7688
7689 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana0503a52023-08-23 13:12:50 +09007690 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Paul Duffineedc5d52020-06-12 17:46:39 +01007691 "javalib/bar.jar",
7692 "javalib/foo.jar",
7693 "etc/permissions/foo.xml",
7694 })
7695
7696 // The bar library should depend on the implementation jar.
7697 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007698 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01007699 t.Errorf("expected %q, found %#q", expected, actual)
7700 }
7701}
7702
7703func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
7704 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
7705 apex {
7706 name: "myapex",
7707 key: "myapex.key",
7708 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007709 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007710 }
7711
7712 apex_key {
7713 name: "myapex.key",
7714 public_key: "testkey.avbpubkey",
7715 private_key: "testkey.pem",
7716 }
7717
7718 java_sdk_library_import {
7719 name: "foo",
7720 apex_available: ["myapex"],
7721 prefer: true,
7722 public: {
7723 jars: ["a.jar"],
7724 },
7725 }
7726
7727 `, withFiles(filesForSdkLibrary))
7728}
7729
atrost6e126252020-01-27 17:01:16 +00007730func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007731 result := android.GroupFixturePreparers(
7732 prepareForApexTest,
7733 java.PrepareForTestWithPlatformCompatConfig,
7734 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007735 apex {
7736 name: "myapex",
7737 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007738 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007739 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007740 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007741 }
7742
7743 apex_key {
7744 name: "myapex.key",
7745 public_key: "testkey.avbpubkey",
7746 private_key: "testkey.pem",
7747 }
7748
7749 platform_compat_config {
7750 name: "myjar-platform-compat-config",
7751 src: ":myjar",
7752 }
7753
7754 java_library {
7755 name: "myjar",
7756 srcs: ["foo/bar/MyClass.java"],
7757 sdk_version: "none",
7758 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007759 apex_available: [ "myapex" ],
7760 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007761
7762 // Make sure that a preferred prebuilt does not affect the apex contents.
7763 prebuilt_platform_compat_config {
7764 name: "myjar-platform-compat-config",
7765 metadata: "compat-config/metadata.xml",
7766 prefer: true,
7767 }
atrost6e126252020-01-27 17:01:16 +00007768 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007769 ctx := result.TestContext
Jooyung Hana0503a52023-08-23 13:12:50 +09007770 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
atrost6e126252020-01-27 17:01:16 +00007771 "etc/compatconfig/myjar-platform-compat-config.xml",
7772 "javalib/myjar.jar",
7773 })
7774}
7775
Jooyung Han862c0d62022-12-21 10:15:37 +09007776func TestNoDupeApexFiles(t *testing.T) {
7777 android.GroupFixturePreparers(
7778 android.PrepareForTestWithAndroidBuildComponents,
7779 PrepareForTestWithApexBuildComponents,
7780 prepareForTestWithMyapex,
7781 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7782 ).
7783 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7784 RunTestWithBp(t, `
7785 apex {
7786 name: "myapex",
7787 key: "myapex.key",
7788 prebuilts: ["foo", "bar"],
7789 updatable: false,
7790 }
7791
7792 apex_key {
7793 name: "myapex.key",
7794 public_key: "testkey.avbpubkey",
7795 private_key: "testkey.pem",
7796 }
7797
7798 prebuilt_etc {
7799 name: "foo",
7800 src: "myprebuilt",
7801 filename_from_src: true,
7802 }
7803
7804 prebuilt_etc {
7805 name: "bar",
7806 src: "myprebuilt",
7807 filename_from_src: true,
7808 }
7809 `)
7810}
7811
Jooyung Hana8bd72a2023-11-02 11:56:48 +09007812func TestApexUnwantedTransitiveDeps(t *testing.T) {
7813 bp := `
7814 apex {
7815 name: "myapex",
7816 key: "myapex.key",
7817 native_shared_libs: ["libfoo"],
7818 updatable: false,
7819 unwanted_transitive_deps: ["libbar"],
7820 }
7821
7822 apex_key {
7823 name: "myapex.key",
7824 public_key: "testkey.avbpubkey",
7825 private_key: "testkey.pem",
7826 }
7827
7828 cc_library {
7829 name: "libfoo",
7830 srcs: ["foo.cpp"],
7831 shared_libs: ["libbar"],
7832 apex_available: ["myapex"],
7833 }
7834
7835 cc_library {
7836 name: "libbar",
7837 srcs: ["bar.cpp"],
7838 apex_available: ["myapex"],
7839 }`
7840 ctx := testApex(t, bp)
7841 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
7842 "*/libc++.so",
7843 "*/libfoo.so",
7844 // not libbar.so
7845 })
7846}
7847
Jiyong Park479321d2019-12-16 11:47:12 +09007848func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7849 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7850 apex {
7851 name: "myapex",
7852 key: "myapex.key",
7853 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007854 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007855 }
7856
7857 apex_key {
7858 name: "myapex.key",
7859 public_key: "testkey.avbpubkey",
7860 private_key: "testkey.pem",
7861 }
7862
7863 java_library {
7864 name: "myjar",
7865 srcs: ["foo/bar/MyClass.java"],
7866 sdk_version: "none",
7867 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007868 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007869 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007870 }
7871 `)
7872}
7873
Jiyong Park7afd1072019-12-30 16:56:33 +09007874func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007875 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007876 apex {
7877 name: "myapex",
7878 key: "myapex.key",
7879 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007880 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007881 }
7882
7883 apex_key {
7884 name: "myapex.key",
7885 public_key: "testkey.avbpubkey",
7886 private_key: "testkey.pem",
7887 }
7888
7889 cc_library {
7890 name: "mylib",
7891 srcs: ["mylib.cpp"],
7892 system_shared_libs: [],
7893 stl: "none",
7894 required: ["a", "b"],
7895 host_required: ["c", "d"],
7896 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007897 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007898 }
7899 `)
7900
Jooyung Hana0503a52023-08-23 13:12:50 +09007901 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007902 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007903 name := apexBundle.BaseModuleName()
7904 prefix := "TARGET_"
7905 var builder strings.Builder
7906 data.Custom(&builder, name, prefix, "", data)
7907 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09007908 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 a b\n")
Sasha Smundakdcb61292022-12-08 10:41:33 -08007909 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7910 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007911}
7912
Jiyong Park7cd10e32020-01-14 09:22:18 +09007913func TestSymlinksFromApexToSystem(t *testing.T) {
7914 bp := `
7915 apex {
7916 name: "myapex",
7917 key: "myapex.key",
7918 native_shared_libs: ["mylib"],
7919 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007920 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007921 }
7922
Jiyong Park9d677202020-02-19 16:29:35 +09007923 apex {
7924 name: "myapex.updatable",
7925 key: "myapex.key",
7926 native_shared_libs: ["mylib"],
7927 java_libs: ["myjar"],
7928 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007929 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007930 }
7931
Jiyong Park7cd10e32020-01-14 09:22:18 +09007932 apex_key {
7933 name: "myapex.key",
7934 public_key: "testkey.avbpubkey",
7935 private_key: "testkey.pem",
7936 }
7937
7938 cc_library {
7939 name: "mylib",
7940 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007941 shared_libs: [
7942 "myotherlib",
7943 "myotherlib_ext",
7944 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007945 system_shared_libs: [],
7946 stl: "none",
7947 apex_available: [
7948 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007949 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007950 "//apex_available:platform",
7951 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007952 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007953 }
7954
7955 cc_library {
7956 name: "myotherlib",
7957 srcs: ["mylib.cpp"],
7958 system_shared_libs: [],
7959 stl: "none",
7960 apex_available: [
7961 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007962 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007963 "//apex_available:platform",
7964 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007965 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007966 }
7967
Jiyong Parkce243632023-02-17 18:22:25 +09007968 cc_library {
7969 name: "myotherlib_ext",
7970 srcs: ["mylib.cpp"],
7971 system_shared_libs: [],
7972 system_ext_specific: true,
7973 stl: "none",
7974 apex_available: [
7975 "myapex",
7976 "myapex.updatable",
7977 "//apex_available:platform",
7978 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007979 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007980 }
7981
Jiyong Park7cd10e32020-01-14 09:22:18 +09007982 java_library {
7983 name: "myjar",
7984 srcs: ["foo/bar/MyClass.java"],
7985 sdk_version: "none",
7986 system_modules: "none",
7987 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007988 apex_available: [
7989 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007990 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007991 "//apex_available:platform",
7992 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007993 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007994 }
7995
7996 java_library {
7997 name: "myotherjar",
7998 srcs: ["foo/bar/MyClass.java"],
7999 sdk_version: "none",
8000 system_modules: "none",
8001 apex_available: [
8002 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09008003 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09008004 "//apex_available:platform",
8005 ],
Spandan Das1a92db52023-04-06 18:55:06 +00008006 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09008007 }
8008 `
8009
8010 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
8011 for _, f := range files {
8012 if f.path == file {
8013 if f.isLink {
8014 t.Errorf("%q is not a real file", file)
8015 }
8016 return
8017 }
8018 }
8019 t.Errorf("%q is not found", file)
8020 }
8021
Jiyong Parkce243632023-02-17 18:22:25 +09008022 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09008023 for _, f := range files {
8024 if f.path == file {
8025 if !f.isLink {
8026 t.Errorf("%q is not a symlink", file)
8027 }
Jiyong Parkce243632023-02-17 18:22:25 +09008028 if f.src != target {
8029 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
8030 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09008031 return
8032 }
8033 }
8034 t.Errorf("%q is not found", file)
8035 }
8036
Jiyong Park9d677202020-02-19 16:29:35 +09008037 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
8038 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08008039 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana0503a52023-08-23 13:12:50 +09008040 files := getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09008041 ensureRealfileExists(t, files, "javalib/myjar.jar")
8042 ensureRealfileExists(t, files, "lib64/mylib.so")
8043 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09008044 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09008045
Jooyung Hana0503a52023-08-23 13:12:50 +09008046 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09008047 ensureRealfileExists(t, files, "javalib/myjar.jar")
8048 ensureRealfileExists(t, files, "lib64/mylib.so")
8049 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09008050 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09008051
8052 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08008053 ctx = testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +09008054 files = getFiles(t, ctx, "myapex", "android_common_myapex")
Jiyong Park7cd10e32020-01-14 09:22:18 +09008055 ensureRealfileExists(t, files, "javalib/myjar.jar")
8056 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09008057 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
8058 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09008059
Jooyung Hana0503a52023-08-23 13:12:50 +09008060 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable")
Jiyong Park9d677202020-02-19 16:29:35 +09008061 ensureRealfileExists(t, files, "javalib/myjar.jar")
8062 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09008063 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
8064 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09008065}
8066
Yo Chiange8128052020-07-23 20:09:18 +08008067func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008068 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08008069 apex {
8070 name: "myapex",
8071 key: "myapex.key",
8072 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008073 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08008074 }
8075
8076 apex_key {
8077 name: "myapex.key",
8078 public_key: "testkey.avbpubkey",
8079 private_key: "testkey.pem",
8080 }
8081
8082 cc_library_shared {
8083 name: "mylib",
8084 srcs: ["mylib.cpp"],
8085 shared_libs: ["myotherlib"],
8086 system_shared_libs: [],
8087 stl: "none",
8088 apex_available: [
8089 "myapex",
8090 "//apex_available:platform",
8091 ],
8092 }
8093
8094 cc_prebuilt_library_shared {
8095 name: "myotherlib",
8096 srcs: ["prebuilt.so"],
8097 system_shared_libs: [],
8098 stl: "none",
8099 apex_available: [
8100 "myapex",
8101 "//apex_available:platform",
8102 ],
8103 }
8104 `)
8105
Jooyung Hana0503a52023-08-23 13:12:50 +09008106 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008107 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08008108 var builder strings.Builder
8109 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
8110 androidMk := builder.String()
8111 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00008112 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08008113 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
8114 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
8115 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Jooyung Haneec1b3f2023-06-20 16:25:59 +09008116 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 myotherlib:64\n")
Yo Chiange8128052020-07-23 20:09:18 +08008117}
8118
Jooyung Han643adc42020-02-27 13:50:06 +09008119func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008120 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09008121 apex {
8122 name: "myapex",
8123 key: "myapex.key",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008124 binaries: ["mybin"],
8125 jni_libs: ["mylib", "mylib3", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008126 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09008127 }
8128
8129 apex_key {
8130 name: "myapex.key",
8131 public_key: "testkey.avbpubkey",
8132 private_key: "testkey.pem",
8133 }
8134
8135 cc_library {
8136 name: "mylib",
8137 srcs: ["mylib.cpp"],
8138 shared_libs: ["mylib2"],
8139 system_shared_libs: [],
8140 stl: "none",
8141 apex_available: [ "myapex" ],
8142 }
8143
8144 cc_library {
8145 name: "mylib2",
8146 srcs: ["mylib.cpp"],
8147 system_shared_libs: [],
8148 stl: "none",
8149 apex_available: [ "myapex" ],
8150 }
Jiyong Park34d5c332022-02-24 18:02:44 +09008151
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008152 // Used as both a JNI library and a regular shared library.
8153 cc_library {
8154 name: "mylib3",
8155 srcs: ["mylib.cpp"],
8156 system_shared_libs: [],
8157 stl: "none",
8158 apex_available: [ "myapex" ],
8159 }
8160
8161 cc_binary {
8162 name: "mybin",
8163 srcs: ["mybin.cpp"],
8164 shared_libs: ["mylib3"],
8165 system_shared_libs: [],
8166 stl: "none",
8167 apex_available: [ "myapex" ],
8168 }
8169
Jiyong Park34d5c332022-02-24 18:02:44 +09008170 rust_ffi_shared {
8171 name: "libfoo.rust",
8172 crate_name: "foo",
8173 srcs: ["foo.rs"],
8174 shared_libs: ["libfoo.shared_from_rust"],
8175 prefer_rlib: true,
8176 apex_available: ["myapex"],
8177 }
8178
8179 cc_library_shared {
8180 name: "libfoo.shared_from_rust",
8181 srcs: ["mylib.cpp"],
8182 system_shared_libs: [],
8183 stl: "none",
8184 stubs: {
8185 versions: ["10", "11", "12"],
8186 },
8187 }
8188
Jooyung Han643adc42020-02-27 13:50:06 +09008189 `)
8190
Jooyung Hana0503a52023-08-23 13:12:50 +09008191 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexManifestRule")
Jooyung Han643adc42020-02-27 13:50:06 +09008192 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008193 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so mylib3.so")
Jooyung Hana0503a52023-08-23 13:12:50 +09008194 ensureExactContents(t, ctx, "myapex", "android_common_myapex", []string{
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008195 "bin/mybin",
Jooyung Han643adc42020-02-27 13:50:06 +09008196 "lib64/mylib.so",
8197 "lib64/mylib2.so",
Jiakai Zhang9c60c172023-09-05 15:19:21 +01008198 "lib64/mylib3.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09008199 "lib64/libfoo.rust.so",
8200 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
8201 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09008202 })
Jiyong Park34d5c332022-02-24 18:02:44 +09008203
8204 // b/220397949
8205 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09008206}
8207
Jooyung Han49f67012020-04-17 13:43:10 +09008208func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008209 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09008210 apex {
8211 name: "myapex",
8212 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008213 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09008214 }
8215 apex_key {
8216 name: "myapex.key",
8217 public_key: "testkey.avbpubkey",
8218 private_key: "testkey.pem",
8219 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008220 `,
8221 android.FixtureModifyConfig(func(config android.Config) {
8222 delete(config.Targets, android.Android)
8223 config.AndroidCommonTarget = android.Target{}
8224 }),
8225 )
Jooyung Han49f67012020-04-17 13:43:10 +09008226
8227 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
8228 t.Errorf("Expected variants: %v, but got: %v", expected, got)
8229 }
8230}
8231
Jiyong Parkbd159612020-02-28 15:22:21 +09008232func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008233 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09008234 apex {
8235 name: "myapex",
8236 key: "myapex.key",
8237 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008238 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09008239 }
8240
8241 apex_key {
8242 name: "myapex.key",
8243 public_key: "testkey.avbpubkey",
8244 private_key: "testkey.pem",
8245 }
8246
8247 android_app {
8248 name: "AppFoo",
8249 srcs: ["foo/bar/MyClass.java"],
8250 sdk_version: "none",
8251 system_modules: "none",
8252 apex_available: [ "myapex" ],
8253 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09008254 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09008255
Jooyung Hana0503a52023-08-23 13:12:50 +09008256 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex").Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07008257 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Jiyong Parkbd159612020-02-28 15:22:21 +09008258
8259 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00008260 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 +09008261}
8262
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008263func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008264 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008265 apex {
8266 name: "myapex",
8267 key: "myapex.key",
8268 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008269 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008270 }
8271
8272 apex_key {
8273 name: "myapex.key",
8274 public_key: "testkey.avbpubkey",
8275 private_key: "testkey.pem",
8276 }
8277
8278 android_app_set {
8279 name: "AppSet",
8280 set: "AppSet.apks",
8281 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +09008282 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crosscf371cc2020-11-13 11:48:42 -08008283 bundleConfigRule := mod.Output("bundle_config.json")
Colin Crossf61d03d2023-11-02 16:56:39 -07008284 content := android.ContentFromFileRuleForTests(t, ctx, bundleConfigRule)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008285 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
8286 s := mod.Rule("apexRule").Args["copy_commands"]
8287 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09008288 if len(copyCmds) != 4 {
8289 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008290 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00008291 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
8292 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09008293 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
8294 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09008295
8296 // Ensure that canned_fs_config has an entry for the app set zip file
8297 generateFsRule := mod.Rule("generateFsConfig")
8298 cmd := generateFsRule.RuleParams.Command
8299 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07008300}
8301
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008302func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01008303 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008304 apex_set {
8305 name: "myapex",
8306 filename: "foo_v2.apex",
8307 sanitized: {
8308 none: { set: "myapex.apks", },
8309 hwaddress: { set: "myapex.hwasan.apks", },
8310 },
Paul Duffin24704672021-04-06 16:09:30 +01008311 }
8312 `
8313 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008314
Paul Duffin24704672021-04-06 16:09:30 +01008315 // Check that the extractor produces the correct output file from the correct input file.
8316 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008317
Paul Duffin24704672021-04-06 16:09:30 +01008318 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
8319 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008320
Paul Duffin24704672021-04-06 16:09:30 +01008321 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
8322
8323 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01008324 m = ctx.ModuleForTests("myapex", "android_common_myapex")
8325 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01008326
8327 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07008328}
8329
Pranav Guptaeba03b02022-09-27 00:27:08 +00008330func TestApexSetApksModuleAssignment(t *testing.T) {
8331 ctx := testApex(t, `
8332 apex_set {
8333 name: "myapex",
8334 set: ":myapex_apks_file",
8335 }
8336
8337 filegroup {
8338 name: "myapex_apks_file",
8339 srcs: ["myapex.apks"],
8340 }
8341 `)
8342
8343 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
8344
8345 // Check that the extractor produces the correct apks file from the input module
8346 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.apks"
8347 extractedApex := m.Output(extractorOutput)
8348
8349 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
8350}
8351
Paul Duffin89f570a2021-06-16 01:42:33 +01008352func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00008353 t.Helper()
8354
Paul Duffin55607122021-03-30 23:32:51 +01008355 fs := android.MockFS{
8356 "a.java": nil,
8357 "a.jar": nil,
8358 "apex_manifest.json": nil,
8359 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00008360 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00008361 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
8362 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
8363 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00008364 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008365 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008366
Paul Duffin55607122021-03-30 23:32:51 +01008367 errorHandler := android.FixtureExpectsNoErrors
8368 if errmsg != "" {
8369 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008370 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008371
Paul Duffin55607122021-03-30 23:32:51 +01008372 result := android.GroupFixturePreparers(
8373 cc.PrepareForTestWithCcDefaultModules,
8374 java.PrepareForTestWithHiddenApiBuildComponents,
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008375 java.PrepareForTestWithDexpreopt,
Paul Duffin55607122021-03-30 23:32:51 +01008376 java.PrepareForTestWithJavaSdkLibraryFiles,
8377 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01008378 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01008379 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01008380 android.FixtureModifyMockFS(func(fs android.MockFS) {
8381 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
8382 insert := ""
8383 for _, fragment := range fragments {
8384 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
8385 }
8386 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
8387 platform_bootclasspath {
8388 name: "platform-bootclasspath",
8389 fragments: [
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008390 {apex: "com.android.art", module: "art-bootclasspath-fragment"},
Paul Duffin89f570a2021-06-16 01:42:33 +01008391 %s
8392 ],
8393 }
8394 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01008395 }
Paul Duffin89f570a2021-06-16 01:42:33 +01008396 }),
Jiakai Zhangb69e8952023-07-11 14:31:22 +01008397 // Dexpreopt for boot jars requires the ART boot image profile.
8398 java.PrepareApexBootJarModule("com.android.art", "core-oj"),
8399 dexpreopt.FixtureSetArtBootJars("com.android.art:core-oj"),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00008400 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01008401 ).
8402 ExtendWithErrorHandler(errorHandler).
8403 RunTestWithBp(t, bp)
8404
8405 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008406}
8407
Paul Duffin5556c5f2022-06-09 17:32:21 +00008408func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008409 preparers := android.GroupFixturePreparers(
8410 java.PrepareForTestWithJavaDefaultModules,
Spandan Das5be63332023-12-13 00:06:32 +00008411 prepareForTestWithBootclasspathFragment,
8412 dexpreopt.FixtureSetTestOnlyArtBootImageJars("com.android.art:libfoo"),
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008413 PrepareForTestWithApexBuildComponents,
8414 ).
8415 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
Spandan Das5be63332023-12-13 00:06:32 +00008416 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.art and com.mycompany.android.art"))
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008417
8418 bpBase := `
8419 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008420 name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008421 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008422 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008423 set: "myapex.apks",
8424 }
8425
8426 apex_set {
Spandan Das5be63332023-12-13 00:06:32 +00008427 name: "com.mycompany.android.art",
8428 apex_name: "com.android.art",
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008429 installable: true,
Spandan Das5be63332023-12-13 00:06:32 +00008430 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008431 set: "company-myapex.apks",
8432 }
8433
8434 prebuilt_bootclasspath_fragment {
Spandan Das5be63332023-12-13 00:06:32 +00008435 name: "art-bootclasspath-fragment",
8436 apex_available: ["com.android.art"],
Spandan Dasfae468e2023-12-12 23:23:53 +00008437 hidden_api: {
8438 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8439 metadata: "my-bootclasspath-fragment/metadata.csv",
8440 index: "my-bootclasspath-fragment/index.csv",
8441 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
8442 all_flags: "my-bootclasspath-fragment/all-flags.csv",
8443 },
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008444 %s
8445 }
8446 `
8447
8448 t.Run("java_import", func(t *testing.T) {
8449 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8450 java_import {
8451 name: "libfoo",
8452 jars: ["libfoo.jar"],
Spandan Das5be63332023-12-13 00:06:32 +00008453 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008454 }
8455 `)
8456 })
8457
8458 t.Run("java_sdk_library_import", func(t *testing.T) {
8459 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8460 java_sdk_library_import {
8461 name: "libfoo",
8462 public: {
8463 jars: ["libbar.jar"],
8464 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008465 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008466 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008467 }
8468 `)
8469 })
8470
8471 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8472 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8473 image_name: "art",
8474 contents: ["libfoo"],
8475 `)+`
8476 java_sdk_library_import {
8477 name: "libfoo",
8478 public: {
8479 jars: ["libbar.jar"],
8480 },
Spandan Dasfae468e2023-12-12 23:23:53 +00008481 shared_library: false,
Spandan Das5be63332023-12-13 00:06:32 +00008482 apex_available: ["com.android.art"],
Martin Stjernholm43c44b02021-06-30 16:35:07 +01008483 }
8484 `)
8485 })
8486}
8487
Paul Duffin5556c5f2022-06-09 17:32:21 +00008488func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
8489 preparers := android.GroupFixturePreparers(
8490 java.PrepareForTestWithJavaDefaultModules,
8491 PrepareForTestWithApexBuildComponents,
8492 )
8493
8494 bpBase := `
8495 apex_set {
8496 name: "com.android.myapex",
8497 installable: true,
8498 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8499 set: "myapex.apks",
8500 }
8501
8502 apex_set {
8503 name: "com.android.myapex_compressed",
8504 apex_name: "com.android.myapex",
8505 installable: true,
8506 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8507 set: "myapex_compressed.apks",
8508 }
8509
8510 prebuilt_bootclasspath_fragment {
8511 name: "my-bootclasspath-fragment",
8512 apex_available: [
8513 "com.android.myapex",
8514 "com.android.myapex_compressed",
8515 ],
8516 hidden_api: {
8517 annotation_flags: "annotation-flags.csv",
8518 metadata: "metadata.csv",
8519 index: "index.csv",
8520 signature_patterns: "signature_patterns.csv",
8521 },
8522 %s
8523 }
8524 `
8525
8526 t.Run("java_import", func(t *testing.T) {
8527 result := preparers.RunTestWithBp(t,
8528 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8529 java_import {
8530 name: "libfoo",
8531 jars: ["libfoo.jar"],
8532 apex_available: [
8533 "com.android.myapex",
8534 "com.android.myapex_compressed",
8535 ],
8536 }
8537 `)
8538
8539 module := result.Module("libfoo", "android_common_com.android.myapex")
8540 usesLibraryDep := module.(java.UsesLibraryDependency)
8541 android.AssertPathRelativeToTopEquals(t, "dex jar path",
8542 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
8543 usesLibraryDep.DexJarBuildPath().Path())
8544 })
8545
8546 t.Run("java_sdk_library_import", func(t *testing.T) {
8547 result := preparers.RunTestWithBp(t,
8548 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
8549 java_sdk_library_import {
8550 name: "libfoo",
8551 public: {
8552 jars: ["libbar.jar"],
8553 },
8554 apex_available: [
8555 "com.android.myapex",
8556 "com.android.myapex_compressed",
8557 ],
8558 compile_dex: true,
8559 }
8560 `)
8561
8562 module := result.Module("libfoo", "android_common_com.android.myapex")
8563 usesLibraryDep := module.(java.UsesLibraryDependency)
8564 android.AssertPathRelativeToTopEquals(t, "dex jar path",
8565 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
8566 usesLibraryDep.DexJarBuildPath().Path())
8567 })
8568
8569 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
8570 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
8571 image_name: "art",
8572 contents: ["libfoo"],
8573 `)+`
8574 java_sdk_library_import {
8575 name: "libfoo",
8576 public: {
8577 jars: ["libbar.jar"],
8578 },
8579 apex_available: [
8580 "com.android.myapex",
8581 "com.android.myapex_compressed",
8582 ],
8583 compile_dex: true,
8584 }
8585 `)
8586 })
8587}
8588
Jooyung Han548640b2020-04-27 12:10:30 +09008589func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
8590 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8591 apex {
8592 name: "myapex",
8593 key: "myapex.key",
8594 updatable: true,
8595 }
8596
8597 apex_key {
8598 name: "myapex.key",
8599 public_key: "testkey.avbpubkey",
8600 private_key: "testkey.pem",
8601 }
8602 `)
8603}
8604
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008605func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
8606 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
8607 apex {
8608 name: "myapex",
8609 key: "myapex.key",
8610 }
8611
8612 apex_key {
8613 name: "myapex.key",
8614 public_key: "testkey.avbpubkey",
8615 private_key: "testkey.pem",
8616 }
8617 `)
8618}
8619
Jooyung Handfc864c2023-03-20 18:19:07 +09008620func Test_use_vndk_as_stable_shouldnt_be_used_for_updatable_vendor_apexes(t *testing.T) {
8621 testApexError(t, `"myapex" .*: use_vndk_as_stable: updatable APEXes can't use external VNDK libs`, `
Daniel Norman69109112021-12-02 12:52:42 -08008622 apex {
8623 name: "myapex",
8624 key: "myapex.key",
8625 updatable: true,
Jooyung Handfc864c2023-03-20 18:19:07 +09008626 use_vndk_as_stable: true,
Daniel Norman69109112021-12-02 12:52:42 -08008627 soc_specific: true,
8628 }
8629
8630 apex_key {
8631 name: "myapex.key",
8632 public_key: "testkey.avbpubkey",
8633 private_key: "testkey.pem",
8634 }
8635 `)
8636}
8637
Jooyung Han02873da2023-03-22 17:41:03 +09008638func Test_use_vndk_as_stable_shouldnt_be_used_with_min_sdk_version(t *testing.T) {
8639 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported when min_sdk_version is set`, `
8640 apex {
8641 name: "myapex",
8642 key: "myapex.key",
8643 updatable: false,
8644 min_sdk_version: "29",
8645 use_vndk_as_stable: true,
8646 vendor: true,
8647 }
8648
8649 apex_key {
8650 name: "myapex.key",
8651 public_key: "testkey.avbpubkey",
8652 private_key: "testkey.pem",
8653 }
8654 `)
8655}
8656
Jooyung Handfc864c2023-03-20 18:19:07 +09008657func Test_use_vndk_as_stable_shouldnt_be_used_for_non_vendor_apexes(t *testing.T) {
8658 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported for system/system_ext APEXes`, `
8659 apex {
8660 name: "myapex",
8661 key: "myapex.key",
8662 updatable: false,
8663 use_vndk_as_stable: true,
8664 }
8665
8666 apex_key {
8667 name: "myapex.key",
8668 public_key: "testkey.avbpubkey",
8669 private_key: "testkey.pem",
8670 }
8671 `)
8672}
8673
satayevb98371c2021-06-15 16:49:50 +01008674func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
8675 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
8676 apex {
8677 name: "myapex",
8678 key: "myapex.key",
8679 systemserverclasspath_fragments: [
8680 "mysystemserverclasspathfragment",
8681 ],
8682 min_sdk_version: "29",
8683 updatable: true,
8684 }
8685
8686 apex_key {
8687 name: "myapex.key",
8688 public_key: "testkey.avbpubkey",
8689 private_key: "testkey.pem",
8690 }
8691
8692 java_library {
8693 name: "foo",
8694 srcs: ["b.java"],
8695 min_sdk_version: "29",
8696 installable: true,
8697 apex_available: [
8698 "myapex",
8699 ],
8700 }
8701
8702 systemserverclasspath_fragment {
8703 name: "mysystemserverclasspathfragment",
8704 generate_classpaths_proto: false,
8705 contents: [
8706 "foo",
8707 ],
8708 apex_available: [
8709 "myapex",
8710 ],
8711 }
satayevabcd5972021-08-06 17:49:46 +01008712 `,
8713 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8714 )
satayevb98371c2021-06-15 16:49:50 +01008715}
8716
Paul Duffin064b70c2020-11-02 17:32:38 +00008717func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008718 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008719 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008720 fragment := java.ApexVariantReference{
8721 Apex: proptools.StringPtr("myapex"),
8722 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8723 }
8724
Paul Duffin064b70c2020-11-02 17:32:38 +00008725 testDexpreoptWithApexes(t, `
8726 prebuilt_apex {
8727 name: "myapex" ,
8728 arch: {
8729 arm64: {
8730 src: "myapex-arm64.apex",
8731 },
8732 arm: {
8733 src: "myapex-arm.apex",
8734 },
8735 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008736 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8737 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008738
Paul Duffin89f570a2021-06-16 01:42:33 +01008739 prebuilt_bootclasspath_fragment {
8740 name: "my-bootclasspath-fragment",
8741 contents: ["libfoo"],
8742 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008743 hidden_api: {
8744 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8745 metadata: "my-bootclasspath-fragment/metadata.csv",
8746 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008747 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8748 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8749 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008750 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008751 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008752
Paul Duffin89f570a2021-06-16 01:42:33 +01008753 java_import {
8754 name: "libfoo",
8755 jars: ["libfoo.jar"],
8756 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008757 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008758 }
8759 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008760 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008761}
8762
Spandan Dasf14e2542021-11-12 00:01:37 +00008763func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008764 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008765 bp += `
8766 apex_key {
8767 name: "myapex.key",
8768 public_key: "testkey.avbpubkey",
8769 private_key: "testkey.pem",
8770 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008771 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008772 "lib1/src/A.java": nil,
8773 "lib2/src/B.java": nil,
8774 "system/sepolicy/apex/myapex-file_contexts": nil,
8775 }
8776
Paul Duffin45338f02021-03-30 23:07:52 +01008777 errorHandler := android.FixtureExpectsNoErrors
8778 if errmsg != "" {
8779 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008780 }
Colin Crossae8600b2020-10-29 17:09:13 -07008781
Paul Duffin45338f02021-03-30 23:07:52 +01008782 android.GroupFixturePreparers(
8783 android.PrepareForTestWithAndroidBuildComponents,
8784 java.PrepareForTestWithJavaBuildComponents,
8785 PrepareForTestWithApexBuildComponents,
8786 android.PrepareForTestWithNeverallowRules(rules),
8787 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008788 apexBootJars := make([]string, 0, len(bootJars))
8789 for _, apexBootJar := range bootJars {
8790 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008791 }
satayevd604b212021-07-21 14:23:52 +01008792 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008793 }),
8794 fs.AddToFixture(),
8795 ).
8796 ExtendWithErrorHandler(errorHandler).
8797 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008798}
8799
8800func TestApexPermittedPackagesRules(t *testing.T) {
8801 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008802 name string
8803 expectedError string
8804 bp string
8805 bootJars []string
8806 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008807 }{
8808
8809 {
8810 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8811 expectedError: "",
8812 bp: `
8813 java_library {
8814 name: "bcp_lib1",
8815 srcs: ["lib1/src/*.java"],
8816 permitted_packages: ["foo.bar"],
8817 apex_available: ["myapex"],
8818 sdk_version: "none",
8819 system_modules: "none",
8820 }
8821 java_library {
8822 name: "nonbcp_lib2",
8823 srcs: ["lib2/src/*.java"],
8824 apex_available: ["myapex"],
8825 permitted_packages: ["a.b"],
8826 sdk_version: "none",
8827 system_modules: "none",
8828 }
8829 apex {
8830 name: "myapex",
8831 key: "myapex.key",
8832 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008833 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008834 }`,
8835 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008836 bcpPermittedPackages: map[string][]string{
8837 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008838 "foo.bar",
8839 },
8840 },
8841 },
8842 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008843 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008844 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 +01008845 bp: `
8846 java_library {
8847 name: "bcp_lib1",
8848 srcs: ["lib1/src/*.java"],
8849 apex_available: ["myapex"],
8850 permitted_packages: ["foo.bar"],
8851 sdk_version: "none",
8852 system_modules: "none",
8853 }
8854 java_library {
8855 name: "bcp_lib2",
8856 srcs: ["lib2/src/*.java"],
8857 apex_available: ["myapex"],
8858 permitted_packages: ["foo.bar", "bar.baz"],
8859 sdk_version: "none",
8860 system_modules: "none",
8861 }
8862 apex {
8863 name: "myapex",
8864 key: "myapex.key",
8865 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008866 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008867 }
8868 `,
8869 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008870 bcpPermittedPackages: map[string][]string{
8871 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008872 "foo.bar",
8873 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008874 "bcp_lib2": []string{
8875 "foo.bar",
8876 },
8877 },
8878 },
8879 {
8880 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8881 expectedError: "",
8882 bp: `
8883 java_library {
8884 name: "bcp_lib_restricted",
8885 srcs: ["lib1/src/*.java"],
8886 apex_available: ["myapex"],
8887 permitted_packages: ["foo.bar"],
8888 sdk_version: "none",
8889 min_sdk_version: "29",
8890 system_modules: "none",
8891 }
8892 java_library {
8893 name: "bcp_lib_unrestricted",
8894 srcs: ["lib2/src/*.java"],
8895 apex_available: ["myapex"],
8896 permitted_packages: ["foo.bar", "bar.baz"],
8897 sdk_version: "none",
8898 min_sdk_version: "29",
8899 system_modules: "none",
8900 }
8901 apex {
8902 name: "myapex",
8903 key: "myapex.key",
8904 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8905 updatable: true,
8906 min_sdk_version: "29",
8907 }
8908 `,
8909 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8910 bcpPermittedPackages: map[string][]string{
8911 "bcp_lib1_non_updateable": []string{
8912 "foo.bar",
8913 },
8914 // 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 +01008915 },
8916 },
8917 }
8918 for _, tc := range testcases {
8919 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008920 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8921 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008922 })
8923 }
8924}
8925
Jiyong Park62304bb2020-04-13 16:19:48 +09008926func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008927 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008928 apex {
8929 name: "myapex",
8930 key: "myapex.key",
8931 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008932 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008933 }
8934
8935 apex_key {
8936 name: "myapex.key",
8937 public_key: "testkey.avbpubkey",
8938 private_key: "testkey.pem",
8939 }
8940
8941 cc_library {
8942 name: "mylib",
8943 srcs: ["mylib.cpp"],
8944 system_shared_libs: [],
8945 stl: "none",
8946 stubs: {
8947 versions: ["1"],
8948 },
8949 apex_available: ["myapex"],
8950 }
8951
8952 cc_library {
8953 name: "myprivlib",
8954 srcs: ["mylib.cpp"],
8955 system_shared_libs: [],
8956 stl: "none",
8957 apex_available: ["myapex"],
8958 }
8959
8960
8961 cc_test {
8962 name: "mytest",
8963 gtest: false,
8964 srcs: ["mylib.cpp"],
8965 system_shared_libs: [],
8966 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008967 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008968 test_for: ["myapex"]
8969 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008970
8971 cc_library {
8972 name: "mytestlib",
8973 srcs: ["mylib.cpp"],
8974 system_shared_libs: [],
8975 shared_libs: ["mylib", "myprivlib"],
8976 stl: "none",
8977 test_for: ["myapex"],
8978 }
8979
8980 cc_benchmark {
8981 name: "mybench",
8982 srcs: ["mylib.cpp"],
8983 system_shared_libs: [],
8984 shared_libs: ["mylib", "myprivlib"],
8985 stl: "none",
8986 test_for: ["myapex"],
8987 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008988 `)
8989
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008990 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008991 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008992 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8993 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8994 }
8995
8996 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008997 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008998 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8999 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
9000 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
9001}
Jiyong Park46a512f2020-12-04 18:02:13 +09009002
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00009003func TestIndirectTestFor(t *testing.T) {
9004 ctx := testApex(t, `
9005 apex {
9006 name: "myapex",
9007 key: "myapex.key",
9008 native_shared_libs: ["mylib", "myprivlib"],
9009 updatable: false,
9010 }
Jiyong Park46a512f2020-12-04 18:02:13 +09009011
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00009012 apex_key {
9013 name: "myapex.key",
9014 public_key: "testkey.avbpubkey",
9015 private_key: "testkey.pem",
9016 }
9017
9018 cc_library {
9019 name: "mylib",
9020 srcs: ["mylib.cpp"],
9021 system_shared_libs: [],
9022 stl: "none",
9023 stubs: {
9024 versions: ["1"],
9025 },
9026 apex_available: ["myapex"],
9027 }
9028
9029 cc_library {
9030 name: "myprivlib",
9031 srcs: ["mylib.cpp"],
9032 system_shared_libs: [],
9033 stl: "none",
9034 shared_libs: ["mylib"],
9035 apex_available: ["myapex"],
9036 }
9037
9038 cc_library {
9039 name: "mytestlib",
9040 srcs: ["mylib.cpp"],
9041 system_shared_libs: [],
9042 shared_libs: ["myprivlib"],
9043 stl: "none",
9044 test_for: ["myapex"],
9045 }
9046 `)
9047
9048 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01009049 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00009050 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
9051 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
9052 }
9053
9054 // The platform variant of mytestlib links to the platform variant of the
9055 // internal myprivlib.
9056 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
9057
9058 // The platform variant of myprivlib links to the platform variant of mylib
9059 // and bypasses its stubs.
9060 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 +09009061}
9062
Martin Stjernholmec009002021-03-27 15:18:31 +00009063func TestTestForForLibInOtherApex(t *testing.T) {
9064 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
9065 _ = testApex(t, `
9066 apex {
9067 name: "com.android.art",
9068 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00009069 native_shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00009070 updatable: false,
9071 }
9072
9073 apex {
9074 name: "com.android.art.debug",
9075 key: "myapex.key",
Spandan Das20fce2d2023-04-12 17:21:39 +00009076 native_shared_libs: ["libnativebridge", "libnativebrdige_test"],
Martin Stjernholmec009002021-03-27 15:18:31 +00009077 updatable: false,
9078 }
9079
9080 apex_key {
9081 name: "myapex.key",
9082 public_key: "testkey.avbpubkey",
9083 private_key: "testkey.pem",
9084 }
9085
9086 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00009087 name: "libnativebridge",
9088 srcs: ["libnativebridge.cpp"],
Martin Stjernholmec009002021-03-27 15:18:31 +00009089 system_shared_libs: [],
9090 stl: "none",
9091 stubs: {
9092 versions: ["1"],
9093 },
9094 apex_available: ["com.android.art", "com.android.art.debug"],
9095 }
9096
9097 cc_library {
Spandan Das20fce2d2023-04-12 17:21:39 +00009098 name: "libnativebrdige_test",
Martin Stjernholmec009002021-03-27 15:18:31 +00009099 srcs: ["mylib.cpp"],
9100 system_shared_libs: [],
Spandan Das20fce2d2023-04-12 17:21:39 +00009101 shared_libs: ["libnativebridge"],
Martin Stjernholmec009002021-03-27 15:18:31 +00009102 stl: "none",
9103 apex_available: ["com.android.art.debug"],
9104 test_for: ["com.android.art"],
9105 }
9106 `,
9107 android.MockFS{
9108 "system/sepolicy/apex/com.android.art-file_contexts": nil,
9109 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
9110 }.AddToFixture())
9111}
9112
Jaewoong Jungfa00c062020-05-14 14:15:24 -07009113// TODO(jungjw): Move this to proptools
9114func intPtr(i int) *int {
9115 return &i
9116}
9117
9118func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009119 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07009120 apex_set {
9121 name: "myapex",
9122 set: "myapex.apks",
9123 filename: "foo_v2.apex",
9124 overrides: ["foo"],
9125 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00009126 `,
9127 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9128 variables.Platform_sdk_version = intPtr(30)
9129 }),
9130 android.FixtureModifyConfig(func(config android.Config) {
9131 config.Targets[android.Android] = []android.Target{
9132 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
9133 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
9134 }
9135 }),
9136 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07009137
Paul Duffin24704672021-04-06 16:09:30 +01009138 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07009139
9140 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01009141 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07009142 actual := extractedApex.Args["abis"]
9143 expected := "ARMEABI_V7A,ARM64_V8A"
9144 if actual != expected {
9145 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
9146 }
9147 actual = extractedApex.Args["sdk-version"]
9148 expected = "30"
9149 if actual != expected {
9150 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
9151 }
9152
Paul Duffin6717d882021-06-15 19:09:41 +01009153 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07009154 a := m.Module().(*ApexSet)
9155 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07009156 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07009157 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
9158 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
9159 }
9160}
9161
Anton Hansson805e0a52022-11-25 14:06:46 +00009162func TestApexSet_NativeBridge(t *testing.T) {
9163 ctx := testApex(t, `
9164 apex_set {
9165 name: "myapex",
9166 set: "myapex.apks",
9167 filename: "foo_v2.apex",
9168 overrides: ["foo"],
9169 }
9170 `,
9171 android.FixtureModifyConfig(func(config android.Config) {
9172 config.Targets[android.Android] = []android.Target{
9173 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
9174 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
9175 }
9176 }),
9177 )
9178
9179 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
9180
9181 // Check extract_apks tool parameters. No native bridge arch expected
9182 extractedApex := m.Output("extracted/myapex.apks")
9183 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
9184}
9185
Jiyong Park7d95a512020-05-10 15:16:24 +09009186func TestNoStaticLinkingToStubsLib(t *testing.T) {
9187 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
9188 apex {
9189 name: "myapex",
9190 key: "myapex.key",
9191 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009192 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09009193 }
9194
9195 apex_key {
9196 name: "myapex.key",
9197 public_key: "testkey.avbpubkey",
9198 private_key: "testkey.pem",
9199 }
9200
9201 cc_library {
9202 name: "mylib",
9203 srcs: ["mylib.cpp"],
9204 static_libs: ["otherlib"],
9205 system_shared_libs: [],
9206 stl: "none",
9207 apex_available: [ "myapex" ],
9208 }
9209
9210 cc_library {
9211 name: "otherlib",
9212 srcs: ["mylib.cpp"],
9213 system_shared_libs: [],
9214 stl: "none",
9215 stubs: {
9216 versions: ["1", "2", "3"],
9217 },
9218 apex_available: [ "myapex" ],
9219 }
9220 `)
9221}
9222
Jiyong Park8d6c51e2020-06-12 17:26:31 +09009223func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009224 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09009225 apex {
9226 name: "myapex",
9227 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009228 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09009229 custom_sign_tool: "sign_myapex",
9230 }
9231
9232 apex_key {
9233 name: "myapex.key",
9234 public_key: "testkey.avbpubkey",
9235 private_key: "testkey.pem",
9236 }
9237 `)
9238
Jooyung Han286957d2023-10-30 16:17:56 +09009239 myapex := ctx.ModuleForTests("myapex", "android_common_myapex")
Colin Crossf61d03d2023-11-02 16:56:39 -07009240 content := android.ContentFromFileRuleForTests(t, ctx, myapex.Output("apexkeys.txt"))
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009241 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 +09009242}
9243
9244func TestApexKeysTxtOverrides(t *testing.T) {
9245 ctx := testApex(t, `
9246 apex {
9247 name: "myapex",
9248 key: "myapex.key",
9249 updatable: false,
9250 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09009251 }
9252
9253 apex_key {
9254 name: "myapex.key",
9255 public_key: "testkey.avbpubkey",
9256 private_key: "testkey.pem",
9257 }
9258
9259 prebuilt_apex {
9260 name: "myapex",
9261 prefer: true,
9262 arch: {
9263 arm64: {
9264 src: "myapex-arm64.apex",
9265 },
9266 arm: {
9267 src: "myapex-arm.apex",
9268 },
9269 },
9270 }
9271
9272 apex_set {
9273 name: "myapex_set",
9274 set: "myapex.apks",
9275 filename: "myapex_set.apex",
9276 overrides: ["myapex"],
9277 }
9278 `)
9279
Colin Crossf61d03d2023-11-02 16:56:39 -07009280 content := android.ContentFromFileRuleForTests(t, ctx,
9281 ctx.ModuleForTests("myapex", "android_common_myapex").Output("apexkeys.txt"))
Jooyung Han286957d2023-10-30 16:17:56 +09009282 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 -07009283 content = android.ContentFromFileRuleForTests(t, ctx,
9284 ctx.ModuleForTests("myapex_set", "android_common_myapex_set").Output("apexkeys.txt"))
Jiyong Park8d6c51e2020-06-12 17:26:31 +09009285 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 +09009286}
9287
Jooyung Han938b5932020-06-20 12:47:47 +09009288func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009289 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09009290 apex {
9291 name: "myapex",
9292 key: "myapex.key",
9293 apps: ["app"],
9294 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009295 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09009296 }
9297
9298 apex_key {
9299 name: "myapex.key",
9300 public_key: "testkey.avbpubkey",
9301 private_key: "testkey.pem",
9302 }
9303
9304 android_app {
9305 name: "app",
9306 srcs: ["foo/bar/MyClass.java"],
9307 package_name: "foo",
9308 sdk_version: "none",
9309 system_modules: "none",
9310 apex_available: [ "myapex" ],
9311 }
9312 `, withFiles(map[string][]byte{
9313 "sub/Android.bp": []byte(`
9314 override_apex {
9315 name: "override_myapex",
9316 base: "myapex",
9317 apps: ["override_app"],
9318 allowed_files: ":allowed",
9319 }
9320 // Overridable "path" property should be referenced indirectly
9321 filegroup {
9322 name: "allowed",
9323 srcs: ["allowed.txt"],
9324 }
9325 override_android_app {
9326 name: "override_app",
9327 base: "app",
9328 package_name: "bar",
9329 }
9330 `),
9331 }))
9332
Jooyung Hana0503a52023-08-23 13:12:50 +09009333 rule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09009334 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
9335 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
9336 }
9337
Jooyung Hana0503a52023-08-23 13:12:50 +09009338 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex").Rule("diffApexContentRule")
Jooyung Han938b5932020-06-20 12:47:47 +09009339 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
9340 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
9341 }
9342}
9343
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009344func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009345 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009346 apex {
9347 name: "myapex",
9348 key: "myapex.key",
9349 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009350 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009351 }
9352
9353 apex_key {
9354 name: "myapex.key",
9355 public_key: "testkey.avbpubkey",
9356 private_key: "testkey.pem",
9357 }
9358
9359 cc_library {
9360 name: "mylib",
9361 srcs: ["mylib.cpp"],
9362 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07009363 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009364 },
9365 apex_available: ["myapex"],
9366 }
9367
9368 cc_prebuilt_library_shared {
9369 name: "mylib",
9370 prefer: false,
9371 srcs: ["prebuilt.so"],
9372 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07009373 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01009374 },
9375 apex_available: ["myapex"],
9376 }
9377 `)
9378}
9379
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009380func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009381 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009382 apex {
9383 name: "myapex",
9384 key: "myapex.key",
9385 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009386 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009387 }
9388 apex_key {
9389 name: "myapex.key",
9390 public_key: "testkey.avbpubkey",
9391 private_key: "testkey.pem",
9392 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00009393 `,
9394 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9395 variables.CompressedApex = proptools.BoolPtr(true)
9396 }),
9397 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009398
Jooyung Hana0503a52023-08-23 13:12:50 +09009399 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("compressRule")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009400 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
9401
Jooyung Hana0503a52023-08-23 13:12:50 +09009402 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex").Description("sign compressedApex")
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009403 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
9404
9405 // Make sure output of bundle is .capex
Jooyung Hana0503a52023-08-23 13:12:50 +09009406 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009407 ensureContains(t, ab.outputFile.String(), "myapex.capex")
9408
9409 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07009410 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00009411 var builder strings.Builder
9412 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9413 androidMk := builder.String()
9414 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
9415}
9416
Martin Stjernholm2856c662020-12-02 15:03:42 +00009417func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009418 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00009419 apex {
9420 name: "myapex",
9421 key: "myapex.key",
9422 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009423 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00009424 }
9425
9426 apex_key {
9427 name: "myapex.key",
9428 public_key: "testkey.avbpubkey",
9429 private_key: "testkey.pem",
9430 }
9431
9432 cc_library {
9433 name: "mylib",
9434 srcs: ["mylib.cpp"],
9435 apex_available: ["myapex"],
9436 shared_libs: ["otherlib"],
9437 system_shared_libs: [],
9438 }
9439
9440 cc_library {
9441 name: "otherlib",
9442 srcs: ["mylib.cpp"],
9443 stubs: {
9444 versions: ["current"],
9445 },
9446 }
9447
9448 cc_prebuilt_library_shared {
9449 name: "otherlib",
9450 prefer: true,
9451 srcs: ["prebuilt.so"],
9452 stubs: {
9453 versions: ["current"],
9454 },
9455 }
9456 `)
9457
Jooyung Hana0503a52023-08-23 13:12:50 +09009458 ab := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07009459 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00009460 var builder strings.Builder
9461 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
9462 androidMk := builder.String()
9463
9464 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
9465 // a thing there.
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009466 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 otherlib\n")
Martin Stjernholm2856c662020-12-02 15:03:42 +00009467}
9468
Jiyong Parke3867542020-12-03 17:28:25 +09009469func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009470 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09009471 apex {
9472 name: "myapex",
9473 key: "myapex.key",
9474 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009475 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09009476 }
9477
9478 apex_key {
9479 name: "myapex.key",
9480 public_key: "testkey.avbpubkey",
9481 private_key: "testkey.pem",
9482 }
9483
9484 cc_library {
9485 name: "mylib",
9486 srcs: ["mylib.cpp"],
9487 system_shared_libs: [],
9488 stl: "none",
9489 apex_available: ["myapex"],
9490 shared_libs: ["mylib2"],
9491 target: {
9492 apex: {
9493 exclude_shared_libs: ["mylib2"],
9494 },
9495 },
9496 }
9497
9498 cc_library {
9499 name: "mylib2",
9500 srcs: ["mylib.cpp"],
9501 system_shared_libs: [],
9502 stl: "none",
9503 }
9504 `)
9505
9506 // Check if mylib is linked to mylib2 for the non-apex target
9507 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
9508 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
9509
9510 // Make sure that the link doesn't occur for the apex target
9511 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
9512 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
9513
9514 // It shouldn't appear in the copy cmd as well.
Jooyung Hana0503a52023-08-23 13:12:50 +09009515 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("apexRule").Args["copy_commands"]
Jiyong Parke3867542020-12-03 17:28:25 +09009516 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
9517}
9518
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009519func TestPrebuiltStubLibDep(t *testing.T) {
9520 bpBase := `
9521 apex {
9522 name: "myapex",
9523 key: "myapex.key",
9524 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009525 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009526 }
9527 apex_key {
9528 name: "myapex.key",
9529 public_key: "testkey.avbpubkey",
9530 private_key: "testkey.pem",
9531 }
9532 cc_library {
9533 name: "mylib",
9534 srcs: ["mylib.cpp"],
9535 apex_available: ["myapex"],
9536 shared_libs: ["stublib"],
9537 system_shared_libs: [],
9538 }
9539 apex {
9540 name: "otherapex",
9541 enabled: %s,
9542 key: "myapex.key",
9543 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009544 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009545 }
9546 `
9547
9548 stublibSourceBp := `
9549 cc_library {
9550 name: "stublib",
9551 srcs: ["mylib.cpp"],
9552 apex_available: ["otherapex"],
9553 system_shared_libs: [],
9554 stl: "none",
9555 stubs: {
9556 versions: ["1"],
9557 },
9558 }
9559 `
9560
9561 stublibPrebuiltBp := `
9562 cc_prebuilt_library_shared {
9563 name: "stublib",
9564 srcs: ["prebuilt.so"],
9565 apex_available: ["otherapex"],
9566 stubs: {
9567 versions: ["1"],
9568 },
9569 %s
9570 }
9571 `
9572
9573 tests := []struct {
9574 name string
9575 stublibBp string
9576 usePrebuilt bool
9577 modNames []string // Modules to collect AndroidMkEntries for
9578 otherApexEnabled []string
9579 }{
9580 {
9581 name: "only_source",
9582 stublibBp: stublibSourceBp,
9583 usePrebuilt: false,
9584 modNames: []string{"stublib"},
9585 otherApexEnabled: []string{"true", "false"},
9586 },
9587 {
9588 name: "source_preferred",
9589 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9590 usePrebuilt: false,
9591 modNames: []string{"stublib", "prebuilt_stublib"},
9592 otherApexEnabled: []string{"true", "false"},
9593 },
9594 {
9595 name: "prebuilt_preferred",
9596 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9597 usePrebuilt: true,
9598 modNames: []string{"stublib", "prebuilt_stublib"},
9599 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9600 },
9601 {
9602 name: "only_prebuilt",
9603 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9604 usePrebuilt: true,
9605 modNames: []string{"stublib"},
9606 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9607 },
9608 }
9609
9610 for _, test := range tests {
9611 t.Run(test.name, func(t *testing.T) {
9612 for _, otherApexEnabled := range test.otherApexEnabled {
9613 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009614 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009615
9616 type modAndMkEntries struct {
9617 mod *cc.Module
9618 mkEntries android.AndroidMkEntries
9619 }
9620 entries := []*modAndMkEntries{}
9621
9622 // Gather shared lib modules that are installable
9623 for _, modName := range test.modNames {
9624 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9625 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9626 continue
9627 }
9628 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08009629 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009630 continue
9631 }
Colin Crossaa255532020-07-03 13:18:24 -07009632 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009633 if ent.Disabled {
9634 continue
9635 }
9636 entries = append(entries, &modAndMkEntries{
9637 mod: mod,
9638 mkEntries: ent,
9639 })
9640 }
9641 }
9642 }
9643
9644 var entry *modAndMkEntries = nil
9645 for _, ent := range entries {
9646 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9647 if entry != nil {
9648 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9649 } else {
9650 entry = ent
9651 }
9652 }
9653 }
9654
9655 if entry == nil {
9656 t.Errorf("AndroidMk entry for \"stublib\" missing")
9657 } else {
9658 isPrebuilt := entry.mod.Prebuilt() != nil
9659 if isPrebuilt != test.usePrebuilt {
9660 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9661 }
9662 if !entry.mod.IsStubs() {
9663 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9664 }
9665 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9666 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9667 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009668 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009669 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009670 if !android.InList(expected, cflags) {
9671 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9672 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009673 }
9674 })
9675 }
9676 })
9677 }
9678}
9679
Colin Crossc33e5212021-05-25 18:16:02 -07009680func TestApexJavaCoverage(t *testing.T) {
9681 bp := `
9682 apex {
9683 name: "myapex",
9684 key: "myapex.key",
9685 java_libs: ["mylib"],
9686 bootclasspath_fragments: ["mybootclasspathfragment"],
9687 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9688 updatable: false,
9689 }
9690
9691 apex_key {
9692 name: "myapex.key",
9693 public_key: "testkey.avbpubkey",
9694 private_key: "testkey.pem",
9695 }
9696
9697 java_library {
9698 name: "mylib",
9699 srcs: ["mylib.java"],
9700 apex_available: ["myapex"],
9701 compile_dex: true,
9702 }
9703
9704 bootclasspath_fragment {
9705 name: "mybootclasspathfragment",
9706 contents: ["mybootclasspathlib"],
9707 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009708 hidden_api: {
9709 split_packages: ["*"],
9710 },
Colin Crossc33e5212021-05-25 18:16:02 -07009711 }
9712
9713 java_library {
9714 name: "mybootclasspathlib",
9715 srcs: ["mybootclasspathlib.java"],
9716 apex_available: ["myapex"],
9717 compile_dex: true,
9718 }
9719
9720 systemserverclasspath_fragment {
9721 name: "mysystemserverclasspathfragment",
9722 contents: ["mysystemserverclasspathlib"],
9723 apex_available: ["myapex"],
9724 }
9725
9726 java_library {
9727 name: "mysystemserverclasspathlib",
9728 srcs: ["mysystemserverclasspathlib.java"],
9729 apex_available: ["myapex"],
9730 compile_dex: true,
9731 }
9732 `
9733
9734 result := android.GroupFixturePreparers(
9735 PrepareForTestWithApexBuildComponents,
9736 prepareForTestWithMyapex,
9737 java.PrepareForTestWithJavaDefaultModules,
9738 android.PrepareForTestWithAndroidBuildComponents,
9739 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009740 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9741 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009742 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009743 ).RunTest(t)
9744
9745 // Make sure jacoco ran on both mylib and mybootclasspathlib
9746 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9747 t.Errorf("Failed to find jacoco rule for mylib")
9748 }
9749 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9750 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9751 }
9752 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9753 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9754 }
9755}
9756
Jiyong Park192600a2021-08-03 07:52:17 +00009757func TestProhibitStaticExecutable(t *testing.T) {
9758 testApexError(t, `executable mybin is static`, `
9759 apex {
9760 name: "myapex",
9761 key: "myapex.key",
9762 binaries: ["mybin"],
9763 min_sdk_version: "29",
9764 }
9765
9766 apex_key {
9767 name: "myapex.key",
9768 public_key: "testkey.avbpubkey",
9769 private_key: "testkey.pem",
9770 }
9771
9772 cc_binary {
9773 name: "mybin",
9774 srcs: ["mylib.cpp"],
9775 relative_install_path: "foo/bar",
9776 static_executable: true,
9777 system_shared_libs: [],
9778 stl: "none",
9779 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009780 min_sdk_version: "29",
9781 }
9782 `)
9783
9784 testApexError(t, `executable mybin.rust is static`, `
9785 apex {
9786 name: "myapex",
9787 key: "myapex.key",
9788 binaries: ["mybin.rust"],
9789 min_sdk_version: "29",
9790 }
9791
9792 apex_key {
9793 name: "myapex.key",
9794 public_key: "testkey.avbpubkey",
9795 private_key: "testkey.pem",
9796 }
9797
9798 rust_binary {
9799 name: "mybin.rust",
9800 srcs: ["foo.rs"],
9801 static_executable: true,
9802 apex_available: ["myapex"],
9803 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009804 }
9805 `)
9806}
9807
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009808func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9809 ctx := testApex(t, `
9810 apex {
9811 name: "myapex",
9812 key: "myapex.key",
9813 updatable: false,
9814 java_libs: ["foo"],
9815 }
9816
9817 apex_key {
9818 name: "myapex.key",
9819 public_key: "testkey.avbpubkey",
9820 private_key: "testkey.pem",
9821 }
9822
9823 java_library {
9824 name: "foo",
9825 srcs: ["foo.java"],
9826 apex_available: ["myapex"],
9827 installable: true,
9828 }
9829 `,
9830 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9831 )
9832
Jooyung Hana0503a52023-08-23 13:12:50 +09009833 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009834 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9835 var builder strings.Builder
9836 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9837 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009838 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 +00009839}
9840
9841func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9842 ctx := testApex(t, `
9843 prebuilt_apex {
9844 name: "myapex",
9845 arch: {
9846 arm64: {
9847 src: "myapex-arm64.apex",
9848 },
9849 arm: {
9850 src: "myapex-arm.apex",
9851 },
9852 },
9853 exported_java_libs: ["foo"],
9854 }
9855
9856 java_import {
9857 name: "foo",
9858 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009859 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009860 }
9861 `,
9862 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9863 )
9864
9865 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9866 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9867 mainModuleEntries := entriesList[0]
9868 android.AssertArrayString(t,
9869 "LOCAL_REQUIRED_MODULES",
9870 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9871 []string{
9872 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9873 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9874 })
9875}
9876
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009877func TestAndroidMk_RequiredModules(t *testing.T) {
9878 ctx := testApex(t, `
9879 apex {
9880 name: "myapex",
9881 key: "myapex.key",
9882 updatable: false,
9883 java_libs: ["foo"],
9884 required: ["otherapex"],
9885 }
9886
9887 apex {
9888 name: "otherapex",
9889 key: "myapex.key",
9890 updatable: false,
9891 java_libs: ["foo"],
9892 required: ["otherapex"],
9893 }
9894
9895 apex_key {
9896 name: "myapex.key",
9897 public_key: "testkey.avbpubkey",
9898 private_key: "testkey.pem",
9899 }
9900
9901 java_library {
9902 name: "foo",
9903 srcs: ["foo.java"],
9904 apex_available: ["myapex", "otherapex"],
9905 installable: true,
9906 }
9907 `)
9908
Jooyung Hana0503a52023-08-23 13:12:50 +09009909 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009910 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9911 var builder strings.Builder
9912 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9913 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009914 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009915}
9916
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009917func TestAndroidMk_RequiredDeps(t *testing.T) {
9918 ctx := testApex(t, `
9919 apex {
9920 name: "myapex",
9921 key: "myapex.key",
9922 updatable: false,
9923 }
9924
9925 apex_key {
9926 name: "myapex.key",
9927 public_key: "testkey.avbpubkey",
9928 private_key: "testkey.pem",
9929 }
9930 `)
9931
Jooyung Hana0503a52023-08-23 13:12:50 +09009932 bundle := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009933 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009934 data := android.AndroidMkDataForTest(t, ctx, bundle)
9935 var builder strings.Builder
9936 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9937 androidMk := builder.String()
Jooyung Haneec1b3f2023-06-20 16:25:59 +09009938 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009939}
9940
Jooyung Hana6d36672022-02-24 13:58:07 +09009941func TestApexOutputFileProducer(t *testing.T) {
9942 for _, tc := range []struct {
9943 name string
9944 ref string
9945 expected_data []string
9946 }{
9947 {
9948 name: "test_using_output",
9949 ref: ":myapex",
Jooyung Hana0503a52023-08-23 13:12:50 +09009950 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.capex:myapex.capex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009951 },
9952 {
9953 name: "test_using_apex",
9954 ref: ":myapex{.apex}",
Jooyung Hana0503a52023-08-23 13:12:50 +09009955 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex/myapex.apex:myapex.apex"},
Jooyung Hana6d36672022-02-24 13:58:07 +09009956 },
9957 } {
9958 t.Run(tc.name, func(t *testing.T) {
9959 ctx := testApex(t, `
9960 apex {
9961 name: "myapex",
9962 key: "myapex.key",
9963 compressible: true,
9964 updatable: false,
9965 }
9966
9967 apex_key {
9968 name: "myapex.key",
9969 public_key: "testkey.avbpubkey",
9970 private_key: "testkey.pem",
9971 }
9972
9973 java_test {
9974 name: "`+tc.name+`",
9975 srcs: ["a.java"],
9976 data: ["`+tc.ref+`"],
9977 }
9978 `,
9979 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9980 variables.CompressedApex = proptools.BoolPtr(true)
9981 }))
9982 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9983 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9984 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9985 })
9986 }
9987}
9988
satayev758968a2021-12-06 11:42:40 +00009989func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9990 preparer := android.GroupFixturePreparers(
9991 PrepareForTestWithApexBuildComponents,
9992 prepareForTestWithMyapex,
9993 java.PrepareForTestWithJavaSdkLibraryFiles,
9994 java.PrepareForTestWithJavaDefaultModules,
9995 android.PrepareForTestWithAndroidBuildComponents,
9996 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9997 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9998 )
9999
10000 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
10001 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
10002 preparer.RunTestWithBp(t, `
10003 apex {
10004 name: "myapex",
10005 key: "myapex.key",
10006 bootclasspath_fragments: ["mybootclasspathfragment"],
10007 min_sdk_version: "30",
10008 updatable: false,
10009 }
10010
10011 apex_key {
10012 name: "myapex.key",
10013 public_key: "testkey.avbpubkey",
10014 private_key: "testkey.pem",
10015 }
10016
10017 bootclasspath_fragment {
10018 name: "mybootclasspathfragment",
10019 contents: ["mybootclasspathlib"],
10020 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +010010021 hidden_api: {
10022 split_packages: ["*"],
10023 },
satayev758968a2021-12-06 11:42:40 +000010024 }
10025
10026 java_sdk_library {
10027 name: "mybootclasspathlib",
10028 srcs: ["mybootclasspathlib.java"],
10029 apex_available: ["myapex"],
10030 compile_dex: true,
10031 unsafe_ignore_missing_latest_api: true,
10032 min_sdk_version: "31",
10033 static_libs: ["util"],
10034 }
10035
10036 java_library {
10037 name: "util",
10038 srcs: ["a.java"],
10039 apex_available: ["myapex"],
10040 min_sdk_version: "31",
10041 static_libs: ["another_util"],
10042 }
10043
10044 java_library {
10045 name: "another_util",
10046 srcs: ["a.java"],
10047 min_sdk_version: "31",
10048 apex_available: ["myapex"],
10049 }
10050 `)
10051 })
10052
10053 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
10054 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
10055 preparer.RunTestWithBp(t, `
10056 apex {
10057 name: "myapex",
10058 key: "myapex.key",
10059 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
10060 min_sdk_version: "30",
10061 updatable: false,
10062 }
10063
10064 apex_key {
10065 name: "myapex.key",
10066 public_key: "testkey.avbpubkey",
10067 private_key: "testkey.pem",
10068 }
10069
10070 systemserverclasspath_fragment {
10071 name: "mysystemserverclasspathfragment",
10072 contents: ["mysystemserverclasspathlib"],
10073 apex_available: ["myapex"],
10074 }
10075
10076 java_sdk_library {
10077 name: "mysystemserverclasspathlib",
10078 srcs: ["mysystemserverclasspathlib.java"],
10079 apex_available: ["myapex"],
10080 compile_dex: true,
10081 min_sdk_version: "32",
10082 unsafe_ignore_missing_latest_api: true,
10083 static_libs: ["util"],
10084 }
10085
10086 java_library {
10087 name: "util",
10088 srcs: ["a.java"],
10089 apex_available: ["myapex"],
10090 min_sdk_version: "31",
10091 static_libs: ["another_util"],
10092 }
10093
10094 java_library {
10095 name: "another_util",
10096 srcs: ["a.java"],
10097 min_sdk_version: "31",
10098 apex_available: ["myapex"],
10099 }
10100 `)
10101 })
10102
10103 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
10104 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
10105 RunTestWithBp(t, `
10106 apex {
10107 name: "myapex",
10108 key: "myapex.key",
10109 bootclasspath_fragments: ["mybootclasspathfragment"],
10110 min_sdk_version: "30",
10111 updatable: false,
10112 }
10113
10114 apex_key {
10115 name: "myapex.key",
10116 public_key: "testkey.avbpubkey",
10117 private_key: "testkey.pem",
10118 }
10119
10120 bootclasspath_fragment {
10121 name: "mybootclasspathfragment",
10122 contents: ["mybootclasspathlib"],
10123 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +010010124 hidden_api: {
10125 split_packages: ["*"],
10126 },
satayev758968a2021-12-06 11:42:40 +000010127 }
10128
10129 java_sdk_library {
10130 name: "mybootclasspathlib",
10131 srcs: ["mybootclasspathlib.java"],
10132 apex_available: ["myapex"],
10133 compile_dex: true,
10134 unsafe_ignore_missing_latest_api: true,
10135 }
10136 `)
10137 })
10138
10139 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
10140 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
10141 RunTestWithBp(t, `
10142 apex {
10143 name: "myapex",
10144 key: "myapex.key",
10145 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
10146 min_sdk_version: "30",
10147 updatable: false,
10148 }
10149
10150 apex_key {
10151 name: "myapex.key",
10152 public_key: "testkey.avbpubkey",
10153 private_key: "testkey.pem",
10154 }
10155
10156 systemserverclasspath_fragment {
10157 name: "mysystemserverclasspathfragment",
10158 contents: ["mysystemserverclasspathlib"],
10159 apex_available: ["myapex"],
10160 }
10161
10162 java_sdk_library {
10163 name: "mysystemserverclasspathlib",
10164 srcs: ["mysystemserverclasspathlib.java"],
10165 apex_available: ["myapex"],
10166 compile_dex: true,
10167 unsafe_ignore_missing_latest_api: true,
10168 }
10169 `)
10170 })
10171}
10172
Jiakai Zhang6decef92022-01-12 17:56:19 +000010173// Verifies that the APEX depends on all the Make modules in the list.
10174func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
10175 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
10176 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +000010177 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +000010178 }
10179}
10180
10181// Verifies that the APEX does not depend on any of the Make modules in the list.
10182func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
10183 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
10184 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +000010185 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +000010186 }
10187}
10188
Cole Faust1021ccd2023-02-26 21:15:25 -080010189// TODO(b/193460475): Re-enable this test
10190//func TestApexStrictUpdtabilityLint(t *testing.T) {
10191// bpTemplate := `
10192// apex {
10193// name: "myapex",
10194// key: "myapex.key",
10195// java_libs: ["myjavalib"],
10196// updatable: %v,
10197// min_sdk_version: "29",
10198// }
10199// apex_key {
10200// name: "myapex.key",
10201// }
10202// java_library {
10203// name: "myjavalib",
10204// srcs: ["MyClass.java"],
10205// apex_available: [ "myapex" ],
10206// lint: {
10207// strict_updatability_linting: %v,
10208// },
10209// sdk_version: "current",
10210// min_sdk_version: "29",
10211// }
10212// `
10213// fs := android.MockFS{
10214// "lint-baseline.xml": nil,
10215// }
10216//
10217// testCases := []struct {
10218// testCaseName string
10219// apexUpdatable bool
10220// javaStrictUpdtabilityLint bool
10221// lintFileExists bool
10222// disallowedFlagExpected bool
10223// }{
10224// {
10225// testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
10226// apexUpdatable: true,
10227// javaStrictUpdtabilityLint: true,
10228// lintFileExists: false,
10229// disallowedFlagExpected: false,
10230// },
10231// {
10232// testCaseName: "non-updatable apex respects strict_updatability of javalib",
10233// apexUpdatable: false,
10234// javaStrictUpdtabilityLint: false,
10235// lintFileExists: true,
10236// disallowedFlagExpected: false,
10237// },
10238// {
10239// testCaseName: "non-updatable apex respects strict updatability of javalib",
10240// apexUpdatable: false,
10241// javaStrictUpdtabilityLint: true,
10242// lintFileExists: true,
10243// disallowedFlagExpected: true,
10244// },
10245// {
10246// testCaseName: "updatable apex sets strict updatability of javalib to true",
10247// apexUpdatable: true,
10248// javaStrictUpdtabilityLint: false, // will be set to true by mutator
10249// lintFileExists: true,
10250// disallowedFlagExpected: true,
10251// },
10252// }
10253//
10254// for _, testCase := range testCases {
10255// bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
10256// fixtures := []android.FixturePreparer{}
10257// if testCase.lintFileExists {
10258// fixtures = append(fixtures, fs.AddToFixture())
10259// }
10260//
10261// result := testApex(t, bp, fixtures...)
10262// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
10263// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
10264// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
10265//
10266// if disallowedFlagActual != testCase.disallowedFlagExpected {
10267// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
10268// }
10269// }
10270//}
10271//
10272//func TestUpdatabilityLintSkipLibcore(t *testing.T) {
10273// bp := `
10274// apex {
10275// name: "myapex",
10276// key: "myapex.key",
10277// java_libs: ["myjavalib"],
10278// updatable: true,
10279// min_sdk_version: "29",
10280// }
10281// apex_key {
10282// name: "myapex.key",
10283// }
10284// java_library {
10285// name: "myjavalib",
10286// srcs: ["MyClass.java"],
10287// apex_available: [ "myapex" ],
10288// sdk_version: "current",
10289// min_sdk_version: "29",
10290// }
10291// `
10292//
10293// testCases := []struct {
10294// testCaseName string
10295// moduleDirectory string
10296// disallowedFlagExpected bool
10297// }{
10298// {
10299// testCaseName: "lintable module defined outside libcore",
10300// moduleDirectory: "",
10301// disallowedFlagExpected: true,
10302// },
10303// {
10304// testCaseName: "lintable module defined in libcore root directory",
10305// moduleDirectory: "libcore/",
10306// disallowedFlagExpected: false,
10307// },
10308// {
10309// testCaseName: "lintable module defined in libcore child directory",
10310// moduleDirectory: "libcore/childdir/",
10311// disallowedFlagExpected: true,
10312// },
10313// }
10314//
10315// for _, testCase := range testCases {
10316// lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
10317// bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
10318// result := testApex(t, "", lintFileCreator, bpFileCreator)
10319// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
10320// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
10321// cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
10322// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
10323//
10324// if disallowedFlagActual != testCase.disallowedFlagExpected {
10325// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
10326// }
10327// }
10328//}
10329//
10330//// checks transtive deps of an apex coming from bootclasspath_fragment
10331//func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
10332// bp := `
10333// apex {
10334// name: "myapex",
10335// key: "myapex.key",
10336// bootclasspath_fragments: ["mybootclasspathfragment"],
10337// updatable: true,
10338// min_sdk_version: "29",
10339// }
10340// apex_key {
10341// name: "myapex.key",
10342// }
10343// bootclasspath_fragment {
10344// name: "mybootclasspathfragment",
10345// contents: ["myjavalib"],
10346// apex_available: ["myapex"],
10347// hidden_api: {
10348// split_packages: ["*"],
10349// },
10350// }
10351// java_library {
10352// name: "myjavalib",
10353// srcs: ["MyClass.java"],
10354// apex_available: [ "myapex" ],
10355// sdk_version: "current",
10356// min_sdk_version: "29",
10357// compile_dex: true,
10358// }
10359// `
10360// fs := android.MockFS{
10361// "lint-baseline.xml": nil,
10362// }
10363//
10364// result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
10365// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
10366// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
10367// if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
10368// t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
10369// }
10370//}
Spandan Das66773252022-01-15 00:23:18 +000010371
Spandan Das42e89502022-05-06 22:12:55 +000010372// updatable apexes should propagate updatable=true to its apps
10373func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
10374 bp := `
10375 apex {
10376 name: "myapex",
10377 key: "myapex.key",
10378 updatable: %v,
10379 apps: [
10380 "myapp",
10381 ],
10382 min_sdk_version: "30",
10383 }
10384 apex_key {
10385 name: "myapex.key",
10386 }
10387 android_app {
10388 name: "myapp",
10389 updatable: %v,
10390 apex_available: [
10391 "myapex",
10392 ],
10393 sdk_version: "current",
10394 min_sdk_version: "30",
10395 }
10396 `
10397 testCases := []struct {
10398 name string
10399 apex_is_updatable_bp bool
10400 app_is_updatable_bp bool
10401 app_is_updatable_expected bool
10402 }{
10403 {
10404 name: "Non-updatable apex respects updatable property of non-updatable app",
10405 apex_is_updatable_bp: false,
10406 app_is_updatable_bp: false,
10407 app_is_updatable_expected: false,
10408 },
10409 {
10410 name: "Non-updatable apex respects updatable property of updatable app",
10411 apex_is_updatable_bp: false,
10412 app_is_updatable_bp: true,
10413 app_is_updatable_expected: true,
10414 },
10415 {
10416 name: "Updatable apex respects updatable property of updatable app",
10417 apex_is_updatable_bp: true,
10418 app_is_updatable_bp: true,
10419 app_is_updatable_expected: true,
10420 },
10421 {
10422 name: "Updatable apex sets updatable=true on non-updatable app",
10423 apex_is_updatable_bp: true,
10424 app_is_updatable_bp: false,
10425 app_is_updatable_expected: true,
10426 },
10427 }
10428 for _, testCase := range testCases {
10429 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
10430 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
10431 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
10432 }
10433}
10434
Kiyoung Kim487689e2022-07-26 09:48:22 +090010435func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10436 bp := `
10437 apex {
10438 name: "myapex",
10439 key: "myapex.key",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010440 native_shared_libs: ["libbaz"],
10441 binaries: ["binfoo"],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010442 min_sdk_version: "29",
10443 }
10444 apex_key {
10445 name: "myapex.key",
10446 }
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010447 cc_binary {
10448 name: "binfoo",
10449 shared_libs: ["libbar", "libbaz", "libqux",],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010450 apex_available: ["myapex"],
10451 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010452 recovery_available: false,
10453 }
10454 cc_library {
10455 name: "libbar",
10456 srcs: ["libbar.cc"],
10457 stubs: {
10458 symbol_file: "libbar.map.txt",
10459 versions: [
10460 "29",
10461 ],
10462 },
10463 }
10464 cc_library {
10465 name: "libbaz",
10466 srcs: ["libbaz.cc"],
10467 apex_available: ["myapex"],
10468 min_sdk_version: "29",
10469 stubs: {
10470 symbol_file: "libbaz.map.txt",
10471 versions: [
10472 "29",
10473 ],
10474 },
Kiyoung Kim487689e2022-07-26 09:48:22 +090010475 }
10476 cc_api_library {
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010477 name: "libbar",
10478 src: "libbar_stub.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010479 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010480 variants: ["apex.29"],
10481 }
10482 cc_api_variant {
10483 name: "libbar",
10484 variant: "apex",
10485 version: "29",
10486 src: "libbar_apex_29.so",
10487 }
10488 cc_api_library {
10489 name: "libbaz",
10490 src: "libbaz_stub.so",
10491 min_sdk_version: "29",
10492 variants: ["apex.29"],
10493 }
10494 cc_api_variant {
10495 name: "libbaz",
10496 variant: "apex",
10497 version: "29",
10498 src: "libbaz_apex_29.so",
10499 }
10500 cc_api_library {
10501 name: "libqux",
10502 src: "libqux_stub.so",
10503 min_sdk_version: "29",
10504 variants: ["apex.29"],
10505 }
10506 cc_api_variant {
10507 name: "libqux",
10508 variant: "apex",
10509 version: "29",
10510 src: "libqux_apex_29.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010511 }
10512 api_imports {
10513 name: "api_imports",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010514 apex_shared_libs: [
10515 "libbar",
10516 "libbaz",
10517 "libqux",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010518 ],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010519 }
10520 `
10521 result := testApex(t, bp)
10522
10523 hasDep := func(m android.Module, wantDep android.Module) bool {
10524 t.Helper()
10525 var found bool
10526 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10527 if dep == wantDep {
10528 found = true
10529 }
10530 })
10531 return found
10532 }
10533
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010534 // Library defines stubs and cc_api_library should be used with cc_api_library
10535 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Module()
10536 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10537 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
Kiyoung Kim487689e2022-07-26 09:48:22 +090010538
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010539 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10540 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
Kiyoung Kim487689e2022-07-26 09:48:22 +090010541
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010542 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Rule("ld").Args["libFlags"]
10543 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10544 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10545 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
10546
10547 // Library defined in the same APEX should be linked with original definition instead of cc_api_library
10548 libbazApexVariant := result.ModuleForTests("libbaz", "android_arm64_armv8-a_shared_apex29").Module()
10549 libbazApiImportCoreVariant := result.ModuleForTests("libbaz.apiimport", "android_arm64_armv8-a_shared").Module()
10550 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even from same APEX", true, hasDep(binfooApexVariant, libbazApiImportCoreVariant))
10551 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbazApexVariant))
10552
10553 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbaz.so")
10554 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbaz.apiimport.so")
10555 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbaz.apex.29.apiimport.so")
10556
10557 // cc_api_library defined without original library should be linked with cc_api_library
10558 libquxApiImportApexVariant := result.ModuleForTests("libqux.apiimport", "android_arm64_armv8-a_shared").Module()
10559 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even original library definition does not exist", true, hasDep(binfooApexVariant, libquxApiImportApexVariant))
10560 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libqux.apex.29.apiimport.so")
10561}
10562
10563func TestPlatformBinaryBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10564 bp := `
10565 apex {
10566 name: "myapex",
10567 key: "myapex.key",
10568 native_shared_libs: ["libbar"],
10569 min_sdk_version: "29",
10570 }
10571 apex_key {
10572 name: "myapex.key",
10573 }
10574 cc_binary {
10575 name: "binfoo",
10576 shared_libs: ["libbar"],
10577 recovery_available: false,
10578 }
10579 cc_library {
10580 name: "libbar",
10581 srcs: ["libbar.cc"],
10582 apex_available: ["myapex"],
10583 min_sdk_version: "29",
10584 stubs: {
10585 symbol_file: "libbar.map.txt",
10586 versions: [
10587 "29",
10588 ],
10589 },
10590 }
10591 cc_api_library {
10592 name: "libbar",
10593 src: "libbar_stub.so",
10594 variants: ["apex.29"],
10595 }
10596 cc_api_variant {
10597 name: "libbar",
10598 variant: "apex",
10599 version: "29",
10600 src: "libbar_apex_29.so",
10601 }
10602 api_imports {
10603 name: "api_imports",
10604 apex_shared_libs: [
10605 "libbar",
10606 ],
10607 }
10608 `
10609
10610 result := testApex(t, bp)
10611
10612 hasDep := func(m android.Module, wantDep android.Module) bool {
10613 t.Helper()
10614 var found bool
10615 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10616 if dep == wantDep {
10617 found = true
10618 }
10619 })
10620 return found
10621 }
10622
10623 // Library defines stubs and cc_api_library should be used with cc_api_library
10624 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Module()
10625 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10626 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
10627
10628 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10629 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
10630
10631 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
10632 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10633 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10634 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
Kiyoung Kim487689e2022-07-26 09:48:22 +090010635}
Dennis Shend4f5d932023-01-31 20:27:21 +000010636
10637func TestTrimmedApex(t *testing.T) {
10638 bp := `
10639 apex {
10640 name: "myapex",
10641 key: "myapex.key",
10642 native_shared_libs: ["libfoo","libbaz"],
10643 min_sdk_version: "29",
10644 trim_against: "mydcla",
10645 }
10646 apex {
10647 name: "mydcla",
10648 key: "myapex.key",
10649 native_shared_libs: ["libfoo","libbar"],
10650 min_sdk_version: "29",
10651 file_contexts: ":myapex-file_contexts",
10652 dynamic_common_lib_apex: true,
10653 }
10654 apex_key {
10655 name: "myapex.key",
10656 }
10657 cc_library {
10658 name: "libfoo",
10659 shared_libs: ["libc"],
10660 apex_available: ["myapex","mydcla"],
10661 min_sdk_version: "29",
10662 }
10663 cc_library {
10664 name: "libbar",
10665 shared_libs: ["libc"],
10666 apex_available: ["myapex","mydcla"],
10667 min_sdk_version: "29",
10668 }
10669 cc_library {
10670 name: "libbaz",
10671 shared_libs: ["libc"],
10672 apex_available: ["myapex","mydcla"],
10673 min_sdk_version: "29",
10674 }
10675 cc_api_library {
10676 name: "libc",
10677 src: "libc.so",
10678 min_sdk_version: "29",
10679 recovery_available: true,
Ivan Lozanoadd122a2023-07-13 11:01:41 -040010680 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +090010681 product_available: true,
Dennis Shend4f5d932023-01-31 20:27:21 +000010682 }
10683 api_imports {
10684 name: "api_imports",
10685 shared_libs: [
10686 "libc",
10687 ],
10688 header_libs: [],
10689 }
10690 `
10691 ctx := testApex(t, bp)
Jooyung Hana0503a52023-08-23 13:12:50 +090010692 module := ctx.ModuleForTests("myapex", "android_common_myapex")
Dennis Shend4f5d932023-01-31 20:27:21 +000010693 apexRule := module.MaybeRule("apexRule")
10694 if apexRule.Rule == nil {
10695 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10696 }
10697
10698 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
Jooyung Hana0503a52023-08-23 13:12:50 +090010699 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex").Rule("TrimmedApexRule")
Dennis Shend4f5d932023-01-31 20:27:21 +000010700 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10701 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10702 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10703 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10704}
Jingwen Chendea7a642023-03-28 11:30:50 +000010705
10706func TestCannedFsConfig(t *testing.T) {
10707 ctx := testApex(t, `
10708 apex {
10709 name: "myapex",
10710 key: "myapex.key",
10711 updatable: false,
10712 }
10713
10714 apex_key {
10715 name: "myapex.key",
10716 public_key: "testkey.avbpubkey",
10717 private_key: "testkey.pem",
10718 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010719 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010720 generateFsRule := mod.Rule("generateFsConfig")
10721 cmd := generateFsRule.RuleParams.Command
10722
10723 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10724}
10725
10726func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10727 ctx := testApex(t, `
10728 apex {
10729 name: "myapex",
10730 key: "myapex.key",
10731 canned_fs_config: "my_config",
10732 updatable: false,
10733 }
10734
10735 apex_key {
10736 name: "myapex.key",
10737 public_key: "testkey.avbpubkey",
10738 private_key: "testkey.pem",
10739 }`)
Jooyung Hana0503a52023-08-23 13:12:50 +090010740 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
Jingwen Chendea7a642023-03-28 11:30:50 +000010741 generateFsRule := mod.Rule("generateFsConfig")
10742 cmd := generateFsRule.RuleParams.Command
10743
10744 // Ensure that canned_fs_config has "cat my_config" at the end
10745 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10746}
Spandan Das20fce2d2023-04-12 17:21:39 +000010747
10748func TestStubLibrariesMultipleApexViolation(t *testing.T) {
10749 testCases := []struct {
10750 desc string
10751 hasStubs bool
10752 apexAvailable string
10753 expectedError string
10754 }{
10755 {
10756 desc: "non-stub library can have multiple apex_available",
10757 hasStubs: false,
10758 apexAvailable: `["myapex", "otherapex"]`,
10759 },
10760 {
10761 desc: "stub library should not be available to anyapex",
10762 hasStubs: true,
10763 apexAvailable: `["//apex_available:anyapex"]`,
10764 expectedError: "Stub libraries should have a single apex_available.*anyapex",
10765 },
10766 {
10767 desc: "stub library should not be available to multiple apexes",
10768 hasStubs: true,
10769 apexAvailable: `["myapex", "otherapex"]`,
10770 expectedError: "Stub libraries should have a single apex_available.*myapex.*otherapex",
10771 },
10772 {
10773 desc: "stub library can be available to a core apex and a test apex",
10774 hasStubs: true,
10775 apexAvailable: `["myapex", "test_myapex"]`,
10776 },
10777 }
10778 bpTemplate := `
10779 cc_library {
10780 name: "libfoo",
10781 %v
10782 apex_available: %v,
10783 }
10784 apex {
10785 name: "myapex",
10786 key: "apex.key",
10787 updatable: false,
10788 native_shared_libs: ["libfoo"],
10789 }
10790 apex {
10791 name: "otherapex",
10792 key: "apex.key",
10793 updatable: false,
10794 }
10795 apex_test {
10796 name: "test_myapex",
10797 key: "apex.key",
10798 updatable: false,
10799 native_shared_libs: ["libfoo"],
10800 }
10801 apex_key {
10802 name: "apex.key",
10803 }
10804 `
10805 for _, tc := range testCases {
10806 stubs := ""
10807 if tc.hasStubs {
10808 stubs = `stubs: {symbol_file: "libfoo.map.txt"},`
10809 }
10810 bp := fmt.Sprintf(bpTemplate, stubs, tc.apexAvailable)
10811 mockFsFixturePreparer := android.FixtureModifyMockFS(func(fs android.MockFS) {
10812 fs["system/sepolicy/apex/test_myapex-file_contexts"] = nil
10813 })
10814 if tc.expectedError == "" {
10815 testApex(t, bp, mockFsFixturePreparer)
10816 } else {
10817 testApexError(t, tc.expectedError, bp, mockFsFixturePreparer)
10818 }
10819 }
10820}
Colin Crossbd3a16b2023-04-25 11:30:51 -070010821
10822func TestFileSystemShouldSkipApexLibraries(t *testing.T) {
10823 context := android.GroupFixturePreparers(
10824 android.PrepareForIntegrationTestWithAndroid,
10825 cc.PrepareForIntegrationTestWithCc,
10826 PrepareForTestWithApexBuildComponents,
10827 prepareForTestWithMyapex,
10828 filesystem.PrepareForTestWithFilesystemBuildComponents,
10829 )
10830 result := context.RunTestWithBp(t, `
10831 android_system_image {
10832 name: "myfilesystem",
10833 deps: [
10834 "libfoo",
10835 ],
10836 linker_config_src: "linker.config.json",
10837 }
10838
10839 cc_library {
10840 name: "libfoo",
10841 shared_libs: [
10842 "libbar",
10843 ],
10844 stl: "none",
10845 }
10846
10847 cc_library {
10848 name: "libbar",
10849 stl: "none",
10850 apex_available: ["myapex"],
10851 }
10852
10853 apex {
10854 name: "myapex",
10855 native_shared_libs: ["libbar"],
10856 key: "myapex.key",
10857 updatable: false,
10858 }
10859
10860 apex_key {
10861 name: "myapex.key",
10862 public_key: "testkey.avbpubkey",
10863 private_key: "testkey.pem",
10864 }
10865 `)
10866
10867 inputs := result.ModuleForTests("myfilesystem", "android_common").Output("deps.zip").Implicits
10868 android.AssertStringListDoesNotContain(t, "filesystem should not have libbar",
10869 inputs.Strings(),
10870 "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
10871}
Yu Liueae7b362023-11-16 17:05:47 -080010872
10873var apex_default_bp = `
10874 apex_key {
10875 name: "myapex.key",
10876 public_key: "testkey.avbpubkey",
10877 private_key: "testkey.pem",
10878 }
10879
10880 filegroup {
10881 name: "myapex.manifest",
10882 srcs: ["apex_manifest.json"],
10883 }
10884
10885 filegroup {
10886 name: "myapex.androidmanifest",
10887 srcs: ["AndroidManifest.xml"],
10888 }
10889`
10890
10891func TestAconfigFilesJavaDeps(t *testing.T) {
10892 ctx := testApex(t, apex_default_bp+`
10893 apex {
10894 name: "myapex",
10895 manifest: ":myapex.manifest",
10896 androidManifest: ":myapex.androidmanifest",
10897 key: "myapex.key",
10898 java_libs: [
10899 "my_java_library_foo",
10900 "my_java_library_bar",
10901 ],
10902 updatable: false,
10903 }
10904
10905 java_library {
10906 name: "my_java_library_foo",
10907 srcs: ["foo/bar/MyClass.java"],
10908 sdk_version: "none",
10909 system_modules: "none",
10910 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080010911 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010912 "myapex",
10913 ],
10914 }
10915
10916 java_library {
10917 name: "my_java_library_bar",
10918 srcs: ["foo/bar/MyClass.java"],
10919 sdk_version: "none",
10920 system_modules: "none",
10921 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080010922 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010923 "myapex",
10924 ],
10925 }
10926
10927 aconfig_declarations {
10928 name: "my_aconfig_declarations_foo",
10929 package: "com.example.package",
10930 container: "myapex",
10931 srcs: ["foo.aconfig"],
10932 }
10933
10934 java_aconfig_library {
10935 name: "my_java_aconfig_library_foo",
10936 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080010937 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010938 "myapex",
10939 ],
10940 }
10941
10942 aconfig_declarations {
10943 name: "my_aconfig_declarations_bar",
10944 package: "com.example.package",
10945 container: "myapex",
10946 srcs: ["bar.aconfig"],
10947 }
10948
10949 java_aconfig_library {
10950 name: "my_java_aconfig_library_bar",
10951 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080010952 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080010953 "myapex",
10954 ],
10955 }
10956 `)
10957
10958 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
10959 s := mod.Rule("apexRule").Args["copy_commands"]
10960 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
10961 if len(copyCmds) != 5 {
10962 t.Fatalf("Expected 5 commands, got %d in:\n%s", len(copyCmds), s)
10963 }
10964
10965 ensureMatches(t, copyCmds[4], "^cp -f .*/aconfig_flags.pb .*/image.apex$")
10966
10967 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
10968 s = " " + combineAconfigRule.Args["cache_files"]
10969 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
10970 if len(aconfigArgs) != 2 {
10971 t.Fatalf("Expected 2 commands, got %d in:\n%s", len(aconfigArgs), s)
10972 }
10973 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
10974 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_bar/intermediate.pb")
10975
10976 buildParams := combineAconfigRule.BuildParams
10977 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
10978 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_bar/intermediate.pb")
10979 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
10980}
10981
10982func TestAconfigFilesJavaAndCcDeps(t *testing.T) {
10983 ctx := testApex(t, apex_default_bp+`
10984 apex {
10985 name: "myapex",
10986 manifest: ":myapex.manifest",
10987 androidManifest: ":myapex.androidmanifest",
10988 key: "myapex.key",
10989 java_libs: [
10990 "my_java_library_foo",
10991 ],
10992 native_shared_libs: [
10993 "my_cc_library_bar",
10994 ],
10995 binaries: [
10996 "my_cc_binary_baz",
10997 ],
10998 updatable: false,
10999 }
11000
11001 java_library {
11002 name: "my_java_library_foo",
11003 srcs: ["foo/bar/MyClass.java"],
11004 sdk_version: "none",
11005 system_modules: "none",
11006 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080011007 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011008 "myapex",
11009 ],
11010 }
11011
11012 cc_library {
11013 name: "my_cc_library_bar",
11014 srcs: ["foo/bar/MyClass.cc"],
Yu Liucec0e412023-11-30 16:45:50 -080011015 static_libs: [
11016 "my_cc_aconfig_library_bar",
11017 "my_cc_aconfig_library_baz",
11018 ],
Yu Liueae7b362023-11-16 17:05:47 -080011019 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011020 "myapex",
11021 ],
11022 }
11023
11024 cc_binary {
11025 name: "my_cc_binary_baz",
11026 srcs: ["foo/bar/MyClass.cc"],
11027 static_libs: ["my_cc_aconfig_library_baz"],
Yu Liueae7b362023-11-16 17:05:47 -080011028 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011029 "myapex",
11030 ],
11031 }
11032
11033 aconfig_declarations {
11034 name: "my_aconfig_declarations_foo",
11035 package: "com.example.package",
11036 container: "myapex",
11037 srcs: ["foo.aconfig"],
11038 }
11039
11040 java_aconfig_library {
11041 name: "my_java_aconfig_library_foo",
11042 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011043 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011044 "myapex",
11045 ],
11046 }
11047
11048 aconfig_declarations {
11049 name: "my_aconfig_declarations_bar",
11050 package: "com.example.package",
11051 container: "myapex",
11052 srcs: ["bar.aconfig"],
11053 }
11054
11055 cc_aconfig_library {
11056 name: "my_cc_aconfig_library_bar",
11057 aconfig_declarations: "my_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080011058 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011059 "myapex",
11060 ],
11061 }
11062
11063 aconfig_declarations {
11064 name: "my_aconfig_declarations_baz",
11065 package: "com.example.package",
11066 container: "myapex",
11067 srcs: ["baz.aconfig"],
11068 }
11069
11070 cc_aconfig_library {
11071 name: "my_cc_aconfig_library_baz",
11072 aconfig_declarations: "my_aconfig_declarations_baz",
Yu Liueae7b362023-11-16 17:05:47 -080011073 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011074 "myapex",
11075 ],
11076 }
11077
11078 cc_library {
11079 name: "server_configurable_flags",
11080 srcs: ["server_configurable_flags.cc"],
11081 }
11082 `)
11083
11084 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11085 s := mod.Rule("apexRule").Args["copy_commands"]
11086 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
11087 if len(copyCmds) != 9 {
11088 t.Fatalf("Expected 9 commands, got %d in:\n%s", len(copyCmds), s)
11089 }
11090
11091 ensureMatches(t, copyCmds[8], "^cp -f .*/aconfig_flags.pb .*/image.apex$")
11092
11093 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11094 s = " " + combineAconfigRule.Args["cache_files"]
11095 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11096 if len(aconfigArgs) != 3 {
11097 t.Fatalf("Expected 3 commands, got %d in:\n%s", len(aconfigArgs), s)
11098 }
11099 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
Colin Crossd788b3e2023-11-28 13:14:56 -080011100 android.EnsureListContainsSuffix(t, aconfigArgs, "my_cc_library_bar/android_arm64_armv8-a_shared_apex10000/aconfig_merged.pb")
Yu Liueae7b362023-11-16 17:05:47 -080011101 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_baz/intermediate.pb")
11102
11103 buildParams := combineAconfigRule.BuildParams
11104 if len(buildParams.Inputs) != 3 {
11105 t.Fatalf("Expected 3 input, got %d", len(buildParams.Inputs))
11106 }
11107 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
Colin Crossd788b3e2023-11-28 13:14:56 -080011108 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_cc_library_bar/android_arm64_armv8-a_shared_apex10000/aconfig_merged.pb")
Yu Liueae7b362023-11-16 17:05:47 -080011109 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_baz/intermediate.pb")
11110 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11111}
11112
Yu Liucec0e412023-11-30 16:45:50 -080011113func TestAconfigFilesRustDeps(t *testing.T) {
11114 ctx := testApex(t, apex_default_bp+`
11115 apex {
11116 name: "myapex",
11117 manifest: ":myapex.manifest",
11118 androidManifest: ":myapex.androidmanifest",
11119 key: "myapex.key",
11120 native_shared_libs: [
11121 "libmy_rust_library",
11122 ],
11123 binaries: [
11124 "my_rust_binary",
11125 ],
11126 rust_dyn_libs: [
11127 "libmy_rust_dylib",
11128 ],
11129 updatable: false,
11130 }
11131
11132 rust_library {
11133 name: "libflags_rust", // test mock
11134 crate_name: "flags_rust",
11135 srcs: ["lib.rs"],
11136 apex_available: [
11137 "myapex",
11138 ],
11139 }
11140
11141 rust_library {
11142 name: "liblazy_static", // test mock
11143 crate_name: "lazy_static",
11144 srcs: ["src/lib.rs"],
11145 apex_available: [
11146 "myapex",
11147 ],
11148 }
11149
11150 rust_ffi_shared {
11151 name: "libmy_rust_library",
11152 srcs: ["src/lib.rs"],
11153 rustlibs: ["libmy_rust_aconfig_library_foo"],
11154 crate_name: "my_rust_library",
11155 apex_available: [
11156 "myapex",
11157 ],
11158 }
11159
11160 rust_library_dylib {
11161 name: "libmy_rust_dylib",
11162 srcs: ["foo/bar/MyClass.rs"],
11163 rustlibs: ["libmy_rust_aconfig_library_bar"],
11164 crate_name: "my_rust_dylib",
11165 apex_available: [
11166 "myapex",
11167 ],
11168 }
11169
11170 rust_binary {
11171 name: "my_rust_binary",
11172 srcs: ["foo/bar/MyClass.rs"],
11173 rustlibs: [
11174 "libmy_rust_aconfig_library_baz",
11175 "libmy_rust_dylib",
11176 ],
11177 apex_available: [
11178 "myapex",
11179 ],
11180 }
11181
11182 aconfig_declarations {
11183 name: "my_aconfig_declarations_foo",
11184 package: "com.example.package",
11185 container: "myapex",
11186 srcs: ["foo.aconfig"],
11187 }
11188
11189 aconfig_declarations {
11190 name: "my_aconfig_declarations_bar",
11191 package: "com.example.package",
11192 container: "myapex",
11193 srcs: ["bar.aconfig"],
11194 }
11195
11196 aconfig_declarations {
11197 name: "my_aconfig_declarations_baz",
11198 package: "com.example.package",
11199 container: "myapex",
11200 srcs: ["baz.aconfig"],
11201 }
11202
11203 rust_aconfig_library {
11204 name: "libmy_rust_aconfig_library_foo",
11205 aconfig_declarations: "my_aconfig_declarations_foo",
11206 crate_name: "my_rust_aconfig_library_foo",
11207 apex_available: [
11208 "myapex",
11209 ],
11210 }
11211
11212 rust_aconfig_library {
11213 name: "libmy_rust_aconfig_library_bar",
11214 aconfig_declarations: "my_aconfig_declarations_bar",
11215 crate_name: "my_rust_aconfig_library_bar",
11216 apex_available: [
11217 "myapex",
11218 ],
11219 }
11220
11221 rust_aconfig_library {
11222 name: "libmy_rust_aconfig_library_baz",
11223 aconfig_declarations: "my_aconfig_declarations_baz",
11224 crate_name: "my_rust_aconfig_library_baz",
11225 apex_available: [
11226 "myapex",
11227 ],
11228 }
11229 `)
11230
11231 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11232 s := mod.Rule("apexRule").Args["copy_commands"]
11233 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
11234 if len(copyCmds) != 23 {
11235 t.Fatalf("Expected 23 commands, got %d in:\n%s", len(copyCmds), s)
11236 }
11237
11238 ensureMatches(t, copyCmds[22], "^cp -f .*/aconfig_flags.pb .*/image.apex$")
11239
11240 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11241 s = " " + combineAconfigRule.Args["cache_files"]
11242 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11243 if len(aconfigArgs) != 2 {
11244 t.Fatalf("Expected 2 commands, got %d in:\n%s", len(aconfigArgs), s)
11245 }
11246 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11247 android.EnsureListContainsSuffix(t, aconfigArgs, "my_rust_binary/android_arm64_armv8-a_apex10000/aconfig_merged.pb")
11248
11249 buildParams := combineAconfigRule.BuildParams
11250 if len(buildParams.Inputs) != 2 {
11251 t.Fatalf("Expected 3 input, got %d", len(buildParams.Inputs))
11252 }
11253 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11254 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_rust_binary/android_arm64_armv8-a_apex10000/aconfig_merged.pb")
11255 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11256}
11257
Yu Liueae7b362023-11-16 17:05:47 -080011258func TestAconfigFilesOnlyMatchCurrentApex(t *testing.T) {
11259 ctx := testApex(t, apex_default_bp+`
11260 apex {
11261 name: "myapex",
11262 manifest: ":myapex.manifest",
11263 androidManifest: ":myapex.androidmanifest",
11264 key: "myapex.key",
11265 java_libs: [
11266 "my_java_library_foo",
11267 "other_java_library_bar",
11268 ],
11269 updatable: false,
11270 }
11271
11272 java_library {
11273 name: "my_java_library_foo",
11274 srcs: ["foo/bar/MyClass.java"],
11275 sdk_version: "none",
11276 system_modules: "none",
11277 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080011278 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011279 "myapex",
11280 ],
11281 }
11282
11283 java_library {
11284 name: "other_java_library_bar",
11285 srcs: ["foo/bar/MyClass.java"],
11286 sdk_version: "none",
11287 system_modules: "none",
11288 static_libs: ["other_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080011289 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011290 "myapex",
11291 ],
11292 }
11293
11294 aconfig_declarations {
11295 name: "my_aconfig_declarations_foo",
11296 package: "com.example.package",
11297 container: "myapex",
11298 srcs: ["foo.aconfig"],
11299 }
11300
11301 java_aconfig_library {
11302 name: "my_java_aconfig_library_foo",
11303 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011304 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011305 "myapex",
11306 ],
11307 }
11308
11309 aconfig_declarations {
11310 name: "other_aconfig_declarations_bar",
11311 package: "com.example.package",
11312 container: "otherapex",
11313 srcs: ["bar.aconfig"],
11314 }
11315
11316 java_aconfig_library {
11317 name: "other_java_aconfig_library_bar",
11318 aconfig_declarations: "other_aconfig_declarations_bar",
Yu Liueae7b362023-11-16 17:05:47 -080011319 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011320 "myapex",
11321 ],
11322 }
11323 `)
11324
11325 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11326 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11327 s := " " + combineAconfigRule.Args["cache_files"]
11328 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11329 if len(aconfigArgs) != 1 {
11330 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
11331 }
11332 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11333
11334 buildParams := combineAconfigRule.BuildParams
11335 if len(buildParams.Inputs) != 1 {
11336 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
11337 }
11338 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11339 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11340}
11341
11342func TestAconfigFilesRemoveDuplicates(t *testing.T) {
11343 ctx := testApex(t, apex_default_bp+`
11344 apex {
11345 name: "myapex",
11346 manifest: ":myapex.manifest",
11347 androidManifest: ":myapex.androidmanifest",
11348 key: "myapex.key",
11349 java_libs: [
11350 "my_java_library_foo",
11351 "my_java_library_bar",
11352 ],
11353 updatable: false,
11354 }
11355
11356 java_library {
11357 name: "my_java_library_foo",
11358 srcs: ["foo/bar/MyClass.java"],
11359 sdk_version: "none",
11360 system_modules: "none",
11361 static_libs: ["my_java_aconfig_library_foo"],
Yu Liueae7b362023-11-16 17:05:47 -080011362 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011363 "myapex",
11364 ],
11365 }
11366
11367 java_library {
11368 name: "my_java_library_bar",
11369 srcs: ["foo/bar/MyClass.java"],
11370 sdk_version: "none",
11371 system_modules: "none",
11372 static_libs: ["my_java_aconfig_library_bar"],
Yu Liueae7b362023-11-16 17:05:47 -080011373 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011374 "myapex",
11375 ],
11376 }
11377
11378 aconfig_declarations {
11379 name: "my_aconfig_declarations_foo",
11380 package: "com.example.package",
11381 container: "myapex",
11382 srcs: ["foo.aconfig"],
11383 }
11384
11385 java_aconfig_library {
11386 name: "my_java_aconfig_library_foo",
11387 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011388 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011389 "myapex",
11390 ],
11391 }
11392
11393 java_aconfig_library {
11394 name: "my_java_aconfig_library_bar",
11395 aconfig_declarations: "my_aconfig_declarations_foo",
Yu Liueae7b362023-11-16 17:05:47 -080011396 apex_available: [
Yu Liueae7b362023-11-16 17:05:47 -080011397 "myapex",
11398 ],
11399 }
11400 `)
11401
11402 mod := ctx.ModuleForTests("myapex", "android_common_myapex")
11403 combineAconfigRule := mod.Rule("All_aconfig_declarations_dump")
11404 s := " " + combineAconfigRule.Args["cache_files"]
11405 aconfigArgs := regexp.MustCompile(" --cache ").Split(s, -1)[1:]
11406 if len(aconfigArgs) != 1 {
11407 t.Fatalf("Expected 1 commands, got %d in:\n%s", len(aconfigArgs), s)
11408 }
11409 android.EnsureListContainsSuffix(t, aconfigArgs, "my_aconfig_declarations_foo/intermediate.pb")
11410
11411 buildParams := combineAconfigRule.BuildParams
11412 if len(buildParams.Inputs) != 1 {
11413 t.Fatalf("Expected 1 input, got %d", len(buildParams.Inputs))
11414 }
11415 android.EnsureListContainsSuffix(t, buildParams.Inputs.Strings(), "my_aconfig_declarations_foo/intermediate.pb")
11416 ensureContains(t, buildParams.Output.String(), "android_common_myapex/aconfig_flags.pb")
11417}
Spandan Das5be63332023-12-13 00:06:32 +000011418
11419// Test that the boot jars come from the _selected_ apex prebuilt
11420// RELEASE_APEX_CONTIRBUTIONS_* build flags will be used to select the correct prebuilt for a specific release config
11421func TestBootDexJarsMultipleApexPrebuilts(t *testing.T) {
11422 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
11423 t.Helper()
11424 s := ctx.ModuleForTests("dex_bootjars", "android_common")
11425 foundLibfooJar := false
11426 base := stem + ".jar"
11427 for _, output := range s.AllOutputs() {
11428 if filepath.Base(output) == base {
11429 foundLibfooJar = true
11430 buildRule := s.Output(output)
11431 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
11432 }
11433 }
11434 if !foundLibfooJar {
11435 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
11436 }
11437 }
11438
11439 bp := `
11440 // Source APEX.
11441
11442 java_library {
11443 name: "framework-foo",
11444 srcs: ["foo.java"],
11445 installable: true,
11446 apex_available: [
11447 "com.android.foo",
11448 ],
11449 }
11450
11451 bootclasspath_fragment {
11452 name: "foo-bootclasspath-fragment",
11453 contents: ["framework-foo"],
11454 apex_available: [
11455 "com.android.foo",
11456 ],
11457 hidden_api: {
11458 split_packages: ["*"],
11459 },
11460 }
11461
11462 apex_key {
11463 name: "com.android.foo.key",
11464 public_key: "com.android.foo.avbpubkey",
11465 private_key: "com.android.foo.pem",
11466 }
11467
11468 apex {
11469 name: "com.android.foo",
11470 key: "com.android.foo.key",
11471 bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11472 updatable: false,
11473 }
11474
11475 // Prebuilt APEX.
11476
11477 java_sdk_library_import {
11478 name: "framework-foo",
11479 public: {
11480 jars: ["foo.jar"],
11481 },
11482 apex_available: ["com.android.foo"],
11483 shared_library: false,
11484 }
11485
11486 prebuilt_bootclasspath_fragment {
11487 name: "foo-bootclasspath-fragment",
11488 contents: ["framework-foo"],
11489 hidden_api: {
11490 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
11491 metadata: "my-bootclasspath-fragment/metadata.csv",
11492 index: "my-bootclasspath-fragment/index.csv",
11493 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
11494 all_flags: "my-bootclasspath-fragment/all-flags.csv",
11495 },
11496 apex_available: [
11497 "com.android.foo",
11498 ],
11499 }
11500
11501 prebuilt_apex {
11502 name: "com.android.foo",
11503 apex_name: "com.android.foo",
11504 src: "com.android.foo-arm.apex",
11505 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11506 }
11507
11508 // Another Prebuilt ART APEX
11509 prebuilt_apex {
11510 name: "com.android.foo.v2",
11511 apex_name: "com.android.foo", // Used to determine the API domain
11512 src: "com.android.foo-arm.apex",
11513 exported_bootclasspath_fragments: ["foo-bootclasspath-fragment"],
11514 }
11515
11516 // APEX contribution modules
11517
11518 apex_contributions {
11519 name: "foo.source.contributions",
11520 api_domain: "com.android.foo",
11521 contents: ["com.android.foo"],
11522 }
11523
11524 apex_contributions {
11525 name: "foo.prebuilt.contributions",
11526 api_domain: "com.android.foo",
11527 contents: ["prebuilt_com.android.foo"],
11528 }
11529
11530 apex_contributions {
11531 name: "foo.prebuilt.v2.contributions",
11532 api_domain: "com.android.foo",
11533 contents: ["com.android.foo.v2"], // prebuilt_ prefix is missing because of prebuilt_rename mutator
11534 }
11535 `
11536
11537 testCases := []struct {
11538 desc string
11539 selectedApexContributions string
11540 expectedBootJar string
11541 }{
11542 {
11543 desc: "Source apex com.android.foo is selected, bootjar should come from source java library",
11544 selectedApexContributions: "foo.source.contributions",
11545 expectedBootJar: "out/soong/.intermediates/foo-bootclasspath-fragment/android_common_apex10000/hiddenapi-modular/encoded/framework-foo.jar",
11546 },
11547 {
11548 desc: "Prebuilt apex prebuilt_com.android.foo is selected, profile should come from .prof deapexed from the prebuilt",
11549 selectedApexContributions: "foo.prebuilt.contributions",
11550 expectedBootJar: "out/soong/.intermediates/com.android.foo.deapexer/android_common/deapexer/javalib/framework-foo.jar",
11551 },
11552 {
11553 desc: "Prebuilt apex prebuilt_com.android.foo.v2 is selected, profile should come from .prof deapexed from the prebuilt",
11554 selectedApexContributions: "foo.prebuilt.v2.contributions",
11555 expectedBootJar: "out/soong/.intermediates/com.android.foo.v2.deapexer/android_common/deapexer/javalib/framework-foo.jar",
11556 },
11557 }
11558
11559 fragment := java.ApexVariantReference{
11560 Apex: proptools.StringPtr("com.android.foo"),
11561 Module: proptools.StringPtr("foo-bootclasspath-fragment"),
11562 }
11563
11564 for _, tc := range testCases {
11565 preparer := android.GroupFixturePreparers(
11566 java.FixtureConfigureApexBootJars("com.android.foo:framework-foo"),
11567 android.FixtureMergeMockFs(map[string][]byte{
11568 "system/sepolicy/apex/com.android.foo-file_contexts": nil,
11569 }),
11570 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
11571 variables.BuildFlags = map[string]string{
11572 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": tc.selectedApexContributions,
11573 }
11574 }),
11575 )
11576 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
11577 checkBootDexJarPath(t, ctx, "framework-foo", tc.expectedBootJar)
11578 }
11579}