blob: 440afec97a94448bc4f70dd1a09d14a31e4bea68 [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
Kiyoung Kim487689e2022-07-26 09:48:22 +090028 "github.com/google/blueprint"
Jiyong Parkda6eb592018-12-19 17:12:36 +090029 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Albert Martineefabcf2022-03-21 20:11:16 +0000116func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
117 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
118 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
119 })
120}
121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withBinder32bit = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Binder32bit = proptools.BoolPtr(true)
125 },
126)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900127
Paul Duffin810f33d2021-03-09 14:12:32 +0000128var withUnbundledBuild = android.FixtureModifyProductVariables(
129 func(variables android.FixtureProductVariables) {
130 variables.Unbundled_build = proptools.BoolPtr(true)
131 },
132)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900133
Paul Duffin284165a2021-03-29 01:50:31 +0100134// Legacy preparer used for running tests within the apex package.
135//
136// This includes everything that was needed to run any test in the apex package prior to the
137// introduction of the test fixtures. Tests that are being converted to use fixtures directly
138// rather than through the testApex...() methods should avoid using this and instead use the
139// various preparers directly, using android.GroupFixturePreparers(...) to group them when
140// necessary.
141//
142// deprecated
143var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000144 // General preparers in alphabetical order as test infrastructure will enforce correct
145 // registration order.
146 android.PrepareForTestWithAndroidBuildComponents,
147 bpf.PrepareForTestWithBpf,
148 cc.PrepareForTestWithCcBuildComponents,
149 java.PrepareForTestWithJavaDefaultModules,
150 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
151 rust.PrepareForTestWithRustDefaultModules,
152 sh.PrepareForTestWithShBuildComponents,
153
154 PrepareForTestWithApexBuildComponents,
155
156 // Additional apex test specific preparers.
157 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
158 filegroup {
159 name: "myapex-file_contexts",
160 srcs: [
161 "apex/myapex-file_contexts",
162 ],
163 }
164 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000165 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000167 "a.java": nil,
168 "PrebuiltAppFoo.apk": nil,
169 "PrebuiltAppFooPriv.apk": nil,
170 "apex_manifest.json": nil,
171 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000172 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
173 "system/sepolicy/apex/myapex2-file_contexts": nil,
174 "system/sepolicy/apex/otherapex-file_contexts": nil,
175 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
176 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700177 "mylib.cpp": nil,
178 "mytest.cpp": nil,
179 "mytest1.cpp": nil,
180 "mytest2.cpp": nil,
181 "mytest3.cpp": nil,
182 "myprebuilt": nil,
183 "my_include": nil,
184 "foo/bar/MyClass.java": nil,
185 "prebuilt.jar": nil,
186 "prebuilt.so": nil,
187 "vendor/foo/devkeys/test.x509.pem": nil,
188 "vendor/foo/devkeys/test.pk8": nil,
189 "testkey.x509.pem": nil,
190 "testkey.pk8": nil,
191 "testkey.override.x509.pem": nil,
192 "testkey.override.pk8": nil,
193 "vendor/foo/devkeys/testkey.avbpubkey": nil,
194 "vendor/foo/devkeys/testkey.pem": nil,
195 "NOTICE": nil,
196 "custom_notice": nil,
197 "custom_notice_for_static_lib": nil,
198 "testkey2.avbpubkey": nil,
199 "testkey2.pem": nil,
200 "myapex-arm64.apex": nil,
201 "myapex-arm.apex": nil,
202 "myapex.apks": nil,
203 "frameworks/base/api/current.txt": nil,
204 "framework/aidl/a.aidl": nil,
205 "dummy.txt": nil,
206 "baz": nil,
207 "bar/baz": nil,
208 "testdata/baz": nil,
209 "AppSet.apks": nil,
210 "foo.rs": nil,
211 "libfoo.jar": nil,
212 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000213 },
214 ),
215
216 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
217 variables.DeviceVndkVersion = proptools.StringPtr("current")
218 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
219 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
220 variables.Platform_sdk_codename = proptools.StringPtr("Q")
221 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000222 // "Tiramisu" needs to be in the next line for compatibility with soong code,
223 // not because of these tests specifically (it's not used by the tests)
224 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900225 variables.Platform_vndk_version = proptools.StringPtr("29")
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000226 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000227 }),
228)
229
Paul Duffin52bfaa42021-03-23 23:40:12 +0000230var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
231 "system/sepolicy/apex/myapex-file_contexts": nil,
232})
233
Jooyung Han643adc42020-02-27 13:50:06 +0900234// ensure that 'result' equals 'expected'
235func ensureEquals(t *testing.T, result string, expected string) {
236 t.Helper()
237 if result != expected {
238 t.Errorf("%q != %q", expected, result)
239 }
240}
241
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242// ensure that 'result' contains 'expected'
243func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900244 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245 if !strings.Contains(result, expected) {
246 t.Errorf("%q is not found in %q", expected, result)
247 }
248}
249
Liz Kammer5bd365f2020-05-27 15:15:11 -0700250// ensure that 'result' contains 'expected' exactly one time
251func ensureContainsOnce(t *testing.T, result string, expected string) {
252 t.Helper()
253 count := strings.Count(result, expected)
254 if count != 1 {
255 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
256 }
257}
258
Jiyong Park25fc6a92018-11-18 18:02:45 +0900259// ensures that 'result' does not contain 'notExpected'
260func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900261 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 if strings.Contains(result, notExpected) {
263 t.Errorf("%q is found in %q", notExpected, result)
264 }
265}
266
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700267func ensureMatches(t *testing.T, result string, expectedRex string) {
268 ok, err := regexp.MatchString(expectedRex, result)
269 if err != nil {
270 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
271 return
272 }
273 if !ok {
274 t.Errorf("%s does not match regular expession %s", result, expectedRex)
275 }
276}
277
Jiyong Park25fc6a92018-11-18 18:02:45 +0900278func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if !android.InList(expected, result) {
281 t.Errorf("%q is not found in %v", expected, result)
282 }
283}
284
285func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900286 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 if android.InList(notExpected, result) {
288 t.Errorf("%q is found in %v", notExpected, result)
289 }
290}
291
Jooyung Hane1633032019-08-01 17:41:43 +0900292func ensureListEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) > 0 {
295 t.Errorf("%q is expected to be empty", result)
296 }
297}
298
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000299func ensureListNotEmpty(t *testing.T, result []string) {
300 t.Helper()
301 if len(result) == 0 {
302 t.Errorf("%q is expected to be not empty", result)
303 }
304}
305
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306// Minimal test
307func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800308 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900309 apex_defaults {
310 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900311 manifest: ":myapex.manifest",
312 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900313 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900314 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900315 native_shared_libs: [
316 "mylib",
317 "libfoo.ffi",
318 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900319 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800320 multilib: {
321 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900322 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800323 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900324 },
Jiyong Park77acec62020-06-01 21:39:15 +0900325 java_libs: [
326 "myjar",
327 "myjar_dex",
328 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000329 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 }
331
Jiyong Park30ca9372019-02-07 16:27:23 +0900332 apex {
333 name: "myapex",
334 defaults: ["myapex-defaults"],
335 }
336
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337 apex_key {
338 name: "myapex.key",
339 public_key: "testkey.avbpubkey",
340 private_key: "testkey.pem",
341 }
342
Jiyong Park809bb722019-02-13 21:33:49 +0900343 filegroup {
344 name: "myapex.manifest",
345 srcs: ["apex_manifest.json"],
346 }
347
348 filegroup {
349 name: "myapex.androidmanifest",
350 srcs: ["AndroidManifest.xml"],
351 }
352
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 cc_library {
354 name: "mylib",
355 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900356 shared_libs: [
357 "mylib2",
358 "libbar.ffi",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 system_shared_libs: [],
361 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000362 // TODO: remove //apex_available:platform
363 apex_available: [
364 "//apex_available:platform",
365 "myapex",
366 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900367 }
368
Alex Light3d673592019-01-18 14:37:31 -0800369 cc_binary {
370 name: "foo",
371 srcs: ["mylib.cpp"],
372 compile_multilib: "both",
373 multilib: {
374 lib32: {
375 suffix: "32",
376 },
377 lib64: {
378 suffix: "64",
379 },
380 },
381 symlinks: ["foo_link_"],
382 symlink_preferred_arch: true,
383 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800384 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700385 apex_available: [ "myapex", "com.android.gki.*" ],
386 }
387
Jiyong Park99644e92020-11-17 22:21:02 +0900388 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000389 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900390 srcs: ["foo.rs"],
391 rlibs: ["libfoo.rlib.rust"],
392 dylibs: ["libfoo.dylib.rust"],
393 apex_available: ["myapex"],
394 }
395
396 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000397 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900398 srcs: ["foo.rs"],
399 crate_name: "foo",
400 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900401 shared_libs: ["libfoo.shared_from_rust"],
402 }
403
404 cc_library_shared {
405 name: "libfoo.shared_from_rust",
406 srcs: ["mylib.cpp"],
407 system_shared_libs: [],
408 stl: "none",
409 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900410 }
411
412 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000413 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900414 srcs: ["foo.rs"],
415 crate_name: "foo",
416 apex_available: ["myapex"],
417 }
418
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900419 rust_ffi_shared {
420 name: "libfoo.ffi",
421 srcs: ["foo.rs"],
422 crate_name: "foo",
423 apex_available: ["myapex"],
424 }
425
426 rust_ffi_shared {
427 name: "libbar.ffi",
428 srcs: ["foo.rs"],
429 crate_name: "bar",
430 apex_available: ["myapex"],
431 }
432
Yifan Hongd22a84a2020-07-28 17:37:46 -0700433 apex {
434 name: "com.android.gki.fake",
435 binaries: ["foo"],
436 key: "myapex.key",
437 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000438 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800439 }
440
Paul Duffindddd5462020-04-07 15:25:44 +0100441 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900442 name: "mylib2",
443 srcs: ["mylib.cpp"],
444 system_shared_libs: [],
445 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900446 static_libs: ["libstatic"],
447 // TODO: remove //apex_available:platform
448 apex_available: [
449 "//apex_available:platform",
450 "myapex",
451 ],
452 }
453
Paul Duffindddd5462020-04-07 15:25:44 +0100454 cc_prebuilt_library_shared {
455 name: "mylib2",
456 srcs: ["prebuilt.so"],
457 // TODO: remove //apex_available:platform
458 apex_available: [
459 "//apex_available:platform",
460 "myapex",
461 ],
462 }
463
Jiyong Park9918e1a2020-03-17 19:16:40 +0900464 cc_library_static {
465 name: "libstatic",
466 srcs: ["mylib.cpp"],
467 system_shared_libs: [],
468 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000469 // TODO: remove //apex_available:platform
470 apex_available: [
471 "//apex_available:platform",
472 "myapex",
473 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900474 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475
476 java_library {
477 name: "myjar",
478 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900479 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900480 sdk_version: "none",
481 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900483 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000484 // TODO: remove //apex_available:platform
485 apex_available: [
486 "//apex_available:platform",
487 "myapex",
488 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900489 }
490
Jiyong Park77acec62020-06-01 21:39:15 +0900491 dex_import {
492 name: "myjar_dex",
493 jars: ["prebuilt.jar"],
494 apex_available: [
495 "//apex_available:platform",
496 "myapex",
497 ],
498 }
499
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 java_library {
501 name: "myotherjar",
502 srcs: ["foo/bar/MyClass.java"],
503 sdk_version: "none",
504 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900505 // TODO: remove //apex_available:platform
506 apex_available: [
507 "//apex_available:platform",
508 "myapex",
509 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900510 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900511
512 java_library {
513 name: "mysharedjar",
514 srcs: ["foo/bar/MyClass.java"],
515 sdk_version: "none",
516 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900517 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900518 `)
519
Paul Duffina71a67a2021-03-29 00:42:57 +0100520 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900521
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900522 // Make sure that Android.mk is created
523 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700524 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900525 var builder strings.Builder
526 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
527
528 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000529 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900530 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
531
Jiyong Park42cca6c2019-04-01 11:15:50 +0900532 optFlags := apexRule.Args["opt_flags"]
533 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700534 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100535 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900536
Jiyong Park25fc6a92018-11-18 18:02:45 +0900537 copyCmds := apexRule.Args["copy_commands"]
538
539 // Ensure that main rule creates an output
540 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
541
542 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700543 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
544 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
545 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900546 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900547 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900548
549 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700550 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
551 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900552 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
553 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900554 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900556
557 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800558 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
559 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900560 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900561 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900562 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900563 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
564 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900566 // .. but not for java libs
567 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900568 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800569
Colin Cross7113d202019-11-20 16:39:12 -0800570 // Ensure that the platform variant ends with _shared or _common
571 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
572 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900573 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
574 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900575 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
576
577 // Ensure that dynamic dependency to java libs are not included
578 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800579
580 // Ensure that all symlinks are present.
581 found_foo_link_64 := false
582 found_foo := false
583 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900584 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800585 if strings.HasSuffix(cmd, "bin/foo") {
586 found_foo = true
587 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
588 found_foo_link_64 = true
589 }
590 }
591 }
592 good := found_foo && found_foo_link_64
593 if !good {
594 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
595 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900596
Artur Satayeva8bd1132020-04-27 18:07:06 +0100597 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100598 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100599 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
600 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
601 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100602
603 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100605 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
606 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
607 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800608}
609
Jooyung Hanf21c7972019-12-16 22:32:06 +0900610func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800611 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900612 apex_defaults {
613 name: "myapex-defaults",
614 key: "myapex.key",
615 prebuilts: ["myetc"],
616 native_shared_libs: ["mylib"],
617 java_libs: ["myjar"],
618 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900619 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800620 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000621 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900622 }
623
624 prebuilt_etc {
625 name: "myetc",
626 src: "myprebuilt",
627 }
628
629 apex {
630 name: "myapex",
631 defaults: ["myapex-defaults"],
632 }
633
634 apex_key {
635 name: "myapex.key",
636 public_key: "testkey.avbpubkey",
637 private_key: "testkey.pem",
638 }
639
640 cc_library {
641 name: "mylib",
642 system_shared_libs: [],
643 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000644 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900645 }
646
647 java_library {
648 name: "myjar",
649 srcs: ["foo/bar/MyClass.java"],
650 sdk_version: "none",
651 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000652 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900653 }
654
655 android_app {
656 name: "AppFoo",
657 srcs: ["foo/bar/MyClass.java"],
658 sdk_version: "none",
659 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000660 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900661 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900662
663 runtime_resource_overlay {
664 name: "rro",
665 theme: "blue",
666 }
667
markchien2f59ec92020-09-02 16:23:38 +0800668 bpf {
669 name: "bpf",
670 srcs: ["bpf.c", "bpf2.c"],
671 }
672
Ken Chenfad7f9d2021-11-10 22:02:57 +0800673 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800674 name: "netdTest",
675 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800676 sub_dir: "netd",
677 }
678
Jooyung Hanf21c7972019-12-16 22:32:06 +0900679 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000680 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900681 "etc/myetc",
682 "javalib/myjar.jar",
683 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000684 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900685 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800686 "etc/bpf/bpf.o",
687 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800688 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900689 })
690}
691
Jooyung Han01a3ee22019-11-02 02:52:25 +0900692func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800693 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900694 apex {
695 name: "myapex",
696 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000697 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900698 }
699
700 apex_key {
701 name: "myapex.key",
702 public_key: "testkey.avbpubkey",
703 private_key: "testkey.pem",
704 }
705 `)
706
707 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900708 args := module.Rule("apexRule").Args
709 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
710 t.Error("manifest should be apex_manifest.pb, but " + manifest)
711 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900712}
713
Liz Kammer4854a7d2021-05-27 14:28:27 -0400714func TestApexManifestMinSdkVersion(t *testing.T) {
715 ctx := testApex(t, `
716 apex_defaults {
717 name: "my_defaults",
718 key: "myapex.key",
719 product_specific: true,
720 file_contexts: ":my-file-contexts",
721 updatable: false,
722 }
723 apex {
724 name: "myapex_30",
725 min_sdk_version: "30",
726 defaults: ["my_defaults"],
727 }
728
729 apex {
730 name: "myapex_current",
731 min_sdk_version: "current",
732 defaults: ["my_defaults"],
733 }
734
735 apex {
736 name: "myapex_none",
737 defaults: ["my_defaults"],
738 }
739
740 apex_key {
741 name: "myapex.key",
742 public_key: "testkey.avbpubkey",
743 private_key: "testkey.pem",
744 }
745
746 filegroup {
747 name: "my-file-contexts",
748 srcs: ["product_specific_file_contexts"],
749 }
750 `, withFiles(map[string][]byte{
751 "product_specific_file_contexts": nil,
752 }), android.FixtureModifyProductVariables(
753 func(variables android.FixtureProductVariables) {
754 variables.Unbundled_build = proptools.BoolPtr(true)
755 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
756 }), android.FixtureMergeEnv(map[string]string{
757 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
758 }))
759
760 testCases := []struct {
761 module string
762 minSdkVersion string
763 }{
764 {
765 module: "myapex_30",
766 minSdkVersion: "30",
767 },
768 {
769 module: "myapex_current",
770 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
771 },
772 {
773 module: "myapex_none",
774 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
775 },
776 }
777 for _, tc := range testCases {
778 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
779 args := module.Rule("apexRule").Args
780 optFlags := args["opt_flags"]
781 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
782 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
783 }
784 }
785}
786
Jooyung Hanaf730952023-02-28 14:13:38 +0900787func TestFileContexts(t *testing.T) {
788 for _, useFileContextsAsIs := range []bool{true, false} {
789 prop := ""
790 if useFileContextsAsIs {
791 prop = "use_file_contexts_as_is: true,\n"
792 }
793 ctx := testApex(t, `
794 apex {
795 name: "myapex",
796 key: "myapex.key",
797 file_contexts: "file_contexts",
798 updatable: false,
799 vendor: true,
800 `+prop+`
801 }
802
803 apex_key {
804 name: "myapex.key",
805 public_key: "testkey.avbpubkey",
806 private_key: "testkey.pem",
807 }
808 `, withFiles(map[string][]byte{
809 "file_contexts": nil,
810 }))
811
812 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("file_contexts")
813 forceLabellingCommand := "apex_manifest\\\\.pb u:object_r:system_file:s0"
814 if useFileContextsAsIs {
815 android.AssertStringDoesNotContain(t, "should force-label",
816 rule.RuleParams.Command, forceLabellingCommand)
817 } else {
818 android.AssertStringDoesContain(t, "shouldn't force-label",
819 rule.RuleParams.Command, forceLabellingCommand)
820 }
821 }
822}
823
Alex Light5098a612018-11-29 17:12:15 -0800824func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800825 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800826 apex {
827 name: "myapex",
828 key: "myapex.key",
829 payload_type: "zip",
830 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000831 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800832 }
833
834 apex_key {
835 name: "myapex.key",
836 public_key: "testkey.avbpubkey",
837 private_key: "testkey.pem",
838 }
839
840 cc_library {
841 name: "mylib",
842 srcs: ["mylib.cpp"],
843 shared_libs: ["mylib2"],
844 system_shared_libs: [],
845 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000846 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800847 }
848
849 cc_library {
850 name: "mylib2",
851 srcs: ["mylib.cpp"],
852 system_shared_libs: [],
853 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000854 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800855 }
856 `)
857
Sundong Ahnabb64432019-10-22 13:58:29 +0900858 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800859 copyCmds := zipApexRule.Args["copy_commands"]
860
861 // Ensure that main rule creates an output
862 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
863
864 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700865 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800866
867 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700868 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800869
870 // Ensure that both direct and indirect deps are copied into apex
871 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
872 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900873}
874
875func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800876 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900877 apex {
878 name: "myapex",
879 key: "myapex.key",
880 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900881 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000882 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900883 }
884
885 apex_key {
886 name: "myapex.key",
887 public_key: "testkey.avbpubkey",
888 private_key: "testkey.pem",
889 }
890
891 cc_library {
892 name: "mylib",
893 srcs: ["mylib.cpp"],
894 shared_libs: ["mylib2", "mylib3"],
895 system_shared_libs: [],
896 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000897 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900898 }
899
900 cc_library {
901 name: "mylib2",
902 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900903 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900904 system_shared_libs: [],
905 stl: "none",
906 stubs: {
907 versions: ["1", "2", "3"],
908 },
909 }
910
911 cc_library {
912 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900913 srcs: ["mylib.cpp"],
914 shared_libs: ["mylib4"],
915 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900916 stl: "none",
917 stubs: {
918 versions: ["10", "11", "12"],
919 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000920 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900921 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900922
923 cc_library {
924 name: "mylib4",
925 srcs: ["mylib.cpp"],
926 system_shared_libs: [],
927 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000928 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900929 }
Jiyong Park105dc322021-06-11 17:22:09 +0900930
931 rust_binary {
932 name: "foo.rust",
933 srcs: ["foo.rs"],
934 shared_libs: ["libfoo.shared_from_rust"],
935 prefer_rlib: true,
936 apex_available: ["myapex"],
937 }
938
939 cc_library_shared {
940 name: "libfoo.shared_from_rust",
941 srcs: ["mylib.cpp"],
942 system_shared_libs: [],
943 stl: "none",
944 stubs: {
945 versions: ["10", "11", "12"],
946 },
947 }
948
Jiyong Park25fc6a92018-11-18 18:02:45 +0900949 `)
950
Sundong Ahnabb64432019-10-22 13:58:29 +0900951 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900952 copyCmds := apexRule.Args["copy_commands"]
953
954 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800955 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900956
957 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800958 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900959
960 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800961 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900962
Colin Crossaede88c2020-08-11 12:17:01 -0700963 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900964
965 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900966 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900967 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900968 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900969
970 // 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 -0700971 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900972 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700973 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900974
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700975 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
976 // is replaced by sharing of "cFlags" in cc/builder.go.
977 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
978 // module variable representing "cflags". So it was not detected by ensureNotContains.
979 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
980 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
981 // including the original cflags's "-include mylib.h".
982 //
Jiyong Park64379952018-12-13 18:37:29 +0900983 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700984 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
985 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900986
Jiyong Park85cc35a2022-07-17 11:30:47 +0900987 // Ensure that genstub for platform-provided lib is invoked with --systemapi
988 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
989 // Ensure that genstub for apex-provided lib is invoked with --apex
990 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +0900991
Jooyung Hana57af4a2020-01-23 05:36:59 +0000992 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900993 "lib64/mylib.so",
994 "lib64/mylib3.so",
995 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +0900996 "bin/foo.rust",
997 "lib64/libc++.so", // by the implicit dependency from foo.rust
998 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900999 })
Jiyong Park105dc322021-06-11 17:22:09 +09001000
1001 // Ensure that stub dependency from a rust module is not included
1002 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1003 // The rust module is linked to the stub cc library
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001004 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustLink").Args["linkFlags"]
Jiyong Park105dc322021-06-11 17:22:09 +09001005 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1006 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +09001007
1008 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1009 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010}
1011
Jiyong Park1bc84122021-06-22 20:23:05 +09001012func TestApexCanUsePrivateApis(t *testing.T) {
1013 ctx := testApex(t, `
1014 apex {
1015 name: "myapex",
1016 key: "myapex.key",
1017 native_shared_libs: ["mylib"],
1018 binaries: ["foo.rust"],
1019 updatable: false,
1020 platform_apis: true,
1021 }
1022
1023 apex_key {
1024 name: "myapex.key",
1025 public_key: "testkey.avbpubkey",
1026 private_key: "testkey.pem",
1027 }
1028
1029 cc_library {
1030 name: "mylib",
1031 srcs: ["mylib.cpp"],
1032 shared_libs: ["mylib2"],
1033 system_shared_libs: [],
1034 stl: "none",
1035 apex_available: [ "myapex" ],
1036 }
1037
1038 cc_library {
1039 name: "mylib2",
1040 srcs: ["mylib.cpp"],
1041 cflags: ["-include mylib.h"],
1042 system_shared_libs: [],
1043 stl: "none",
1044 stubs: {
1045 versions: ["1", "2", "3"],
1046 },
1047 }
1048
1049 rust_binary {
1050 name: "foo.rust",
1051 srcs: ["foo.rs"],
1052 shared_libs: ["libfoo.shared_from_rust"],
1053 prefer_rlib: true,
1054 apex_available: ["myapex"],
1055 }
1056
1057 cc_library_shared {
1058 name: "libfoo.shared_from_rust",
1059 srcs: ["mylib.cpp"],
1060 system_shared_libs: [],
1061 stl: "none",
1062 stubs: {
1063 versions: ["10", "11", "12"],
1064 },
1065 }
1066 `)
1067
1068 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1069 copyCmds := apexRule.Args["copy_commands"]
1070
1071 // Ensure that indirect stubs dep is not included
1072 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1073 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1074
1075 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1076 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001077 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001078 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1079 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001080 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustLink").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001081 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1082 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1083}
1084
Colin Cross7812fd32020-09-25 12:35:10 -07001085func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1086 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001087 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001088 apex {
1089 name: "myapex",
1090 key: "myapex.key",
1091 native_shared_libs: ["mylib", "mylib3"],
1092 min_sdk_version: "29",
1093 }
1094
1095 apex_key {
1096 name: "myapex.key",
1097 public_key: "testkey.avbpubkey",
1098 private_key: "testkey.pem",
1099 }
1100
1101 cc_library {
1102 name: "mylib",
1103 srcs: ["mylib.cpp"],
1104 shared_libs: ["mylib2", "mylib3"],
1105 system_shared_libs: [],
1106 stl: "none",
1107 apex_available: [ "myapex" ],
1108 min_sdk_version: "28",
1109 }
1110
1111 cc_library {
1112 name: "mylib2",
1113 srcs: ["mylib.cpp"],
1114 cflags: ["-include mylib.h"],
1115 system_shared_libs: [],
1116 stl: "none",
1117 stubs: {
1118 versions: ["28", "29", "30", "current"],
1119 },
1120 min_sdk_version: "28",
1121 }
1122
1123 cc_library {
1124 name: "mylib3",
1125 srcs: ["mylib.cpp"],
1126 shared_libs: ["mylib4"],
1127 system_shared_libs: [],
1128 stl: "none",
1129 stubs: {
1130 versions: ["28", "29", "30", "current"],
1131 },
1132 apex_available: [ "myapex" ],
1133 min_sdk_version: "28",
1134 }
1135
1136 cc_library {
1137 name: "mylib4",
1138 srcs: ["mylib.cpp"],
1139 system_shared_libs: [],
1140 stl: "none",
1141 apex_available: [ "myapex" ],
1142 min_sdk_version: "28",
1143 }
1144 `)
1145
1146 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1147 copyCmds := apexRule.Args["copy_commands"]
1148
1149 // Ensure that direct non-stubs dep is always included
1150 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1151
1152 // Ensure that indirect stubs dep is not included
1153 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1154
1155 // Ensure that direct stubs dep is included
1156 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1157
1158 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1159
Jiyong Park55549df2021-02-26 23:57:23 +09001160 // Ensure that mylib is linking with the latest version of stub for mylib2
1161 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001162 // ... and not linking to the non-stub (impl) variant of mylib2
1163 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1164
1165 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1166 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1167 // .. and not linking to the stubs variant of mylib3
1168 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1169
1170 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001171 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001172 ensureNotContains(t, mylib2Cflags, "-include ")
1173
Jiyong Park85cc35a2022-07-17 11:30:47 +09001174 // Ensure that genstub is invoked with --systemapi
1175 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001176
1177 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1178 "lib64/mylib.so",
1179 "lib64/mylib3.so",
1180 "lib64/mylib4.so",
1181 })
1182}
1183
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001184func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1185 t.Parallel()
1186 // myapex (Z)
1187 // mylib -----------------.
1188 // |
1189 // otherapex (29) |
1190 // libstub's versions: 29 Z current
1191 // |
1192 // <platform> |
1193 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001194 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001195 apex {
1196 name: "myapex",
1197 key: "myapex.key",
1198 native_shared_libs: ["mylib"],
1199 min_sdk_version: "Z", // non-final
1200 }
1201
1202 cc_library {
1203 name: "mylib",
1204 srcs: ["mylib.cpp"],
1205 shared_libs: ["libstub"],
1206 apex_available: ["myapex"],
1207 min_sdk_version: "Z",
1208 }
1209
1210 apex_key {
1211 name: "myapex.key",
1212 public_key: "testkey.avbpubkey",
1213 private_key: "testkey.pem",
1214 }
1215
1216 apex {
1217 name: "otherapex",
1218 key: "myapex.key",
1219 native_shared_libs: ["libstub"],
1220 min_sdk_version: "29",
1221 }
1222
1223 cc_library {
1224 name: "libstub",
1225 srcs: ["mylib.cpp"],
1226 stubs: {
1227 versions: ["29", "Z", "current"],
1228 },
1229 apex_available: ["otherapex"],
1230 min_sdk_version: "29",
1231 }
1232
1233 // platform module depending on libstub from otherapex should use the latest stub("current")
1234 cc_library {
1235 name: "libplatform",
1236 srcs: ["mylib.cpp"],
1237 shared_libs: ["libstub"],
1238 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001239 `,
1240 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1241 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1242 variables.Platform_sdk_final = proptools.BoolPtr(false)
1243 variables.Platform_version_active_codenames = []string{"Z"}
1244 }),
1245 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001246
Jiyong Park55549df2021-02-26 23:57:23 +09001247 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001248 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001249 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001250 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001251 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001252
1253 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1254 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1255 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1256 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1257 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1258}
1259
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001260func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001261 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001262 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001263 name: "myapex2",
1264 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001265 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001266 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001267 }
1268
1269 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001270 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001271 public_key: "testkey.avbpubkey",
1272 private_key: "testkey.pem",
1273 }
1274
1275 cc_library {
1276 name: "mylib",
1277 srcs: ["mylib.cpp"],
1278 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001279 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001280 system_shared_libs: [],
1281 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001282 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001283 }
1284
1285 cc_library {
1286 name: "libfoo",
1287 srcs: ["mylib.cpp"],
1288 shared_libs: ["libbar"],
1289 system_shared_libs: [],
1290 stl: "none",
1291 stubs: {
1292 versions: ["10", "20", "30"],
1293 },
1294 }
1295
1296 cc_library {
1297 name: "libbar",
1298 srcs: ["mylib.cpp"],
1299 system_shared_libs: [],
1300 stl: "none",
1301 }
1302
Jiyong Park678c8812020-02-07 17:25:49 +09001303 cc_library_static {
1304 name: "libbaz",
1305 srcs: ["mylib.cpp"],
1306 system_shared_libs: [],
1307 stl: "none",
1308 apex_available: [ "myapex2" ],
1309 }
1310
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001311 `)
1312
Jiyong Park83dc74b2020-01-14 18:38:44 +09001313 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001314 copyCmds := apexRule.Args["copy_commands"]
1315
1316 // Ensure that direct non-stubs dep is always included
1317 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1318
1319 // Ensure that indirect stubs dep is not included
1320 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1321
1322 // Ensure that dependency of stubs is not included
1323 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1324
Colin Crossaede88c2020-08-11 12:17:01 -07001325 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001326
1327 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001328 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001329 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001330 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001331
Jiyong Park3ff16992019-12-27 14:11:47 +09001332 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001333
1334 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1335 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001336
Artur Satayeva8bd1132020-04-27 18:07:06 +01001337 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001338 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001339
Artur Satayeva8bd1132020-04-27 18:07:06 +01001340 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001341 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001342}
1343
Jooyung Hand3639552019-08-09 12:57:43 +09001344func TestApexWithRuntimeLibsDependency(t *testing.T) {
1345 /*
1346 myapex
1347 |
1348 v (runtime_libs)
1349 mylib ------+------> libfoo [provides stub]
1350 |
1351 `------> libbar
1352 */
Colin Cross1c460562021-02-16 17:55:47 -08001353 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001354 apex {
1355 name: "myapex",
1356 key: "myapex.key",
1357 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001358 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001359 }
1360
1361 apex_key {
1362 name: "myapex.key",
1363 public_key: "testkey.avbpubkey",
1364 private_key: "testkey.pem",
1365 }
1366
1367 cc_library {
1368 name: "mylib",
1369 srcs: ["mylib.cpp"],
1370 runtime_libs: ["libfoo", "libbar"],
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
1376 cc_library {
1377 name: "libfoo",
1378 srcs: ["mylib.cpp"],
1379 system_shared_libs: [],
1380 stl: "none",
1381 stubs: {
1382 versions: ["10", "20", "30"],
1383 },
1384 }
1385
1386 cc_library {
1387 name: "libbar",
1388 srcs: ["mylib.cpp"],
1389 system_shared_libs: [],
1390 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001391 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001392 }
1393
1394 `)
1395
Sundong Ahnabb64432019-10-22 13:58:29 +09001396 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001397 copyCmds := apexRule.Args["copy_commands"]
1398
1399 // Ensure that direct non-stubs dep is always included
1400 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1401
1402 // Ensure that indirect stubs dep is not included
1403 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1404
1405 // Ensure that runtime_libs dep in included
1406 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1407
Sundong Ahnabb64432019-10-22 13:58:29 +09001408 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001409 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1410 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001411
1412}
1413
Paul Duffina02cae32021-03-09 01:44:06 +00001414var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1415 cc.PrepareForTestWithCcBuildComponents,
1416 PrepareForTestWithApexBuildComponents,
1417 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001418 apex {
1419 name: "com.android.runtime",
1420 key: "com.android.runtime.key",
1421 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001422 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001423 }
1424
1425 apex_key {
1426 name: "com.android.runtime.key",
1427 public_key: "testkey.avbpubkey",
1428 private_key: "testkey.pem",
1429 }
Paul Duffina02cae32021-03-09 01:44:06 +00001430 `),
1431 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1432)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001433
Paul Duffina02cae32021-03-09 01:44:06 +00001434func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001435 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001436 cc_library {
1437 name: "libc",
1438 no_libcrt: true,
1439 nocrt: true,
1440 stl: "none",
1441 system_shared_libs: [],
1442 stubs: { versions: ["1"] },
1443 apex_available: ["com.android.runtime"],
1444
1445 sanitize: {
1446 hwaddress: true,
1447 }
1448 }
1449
1450 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001451 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001452 no_libcrt: true,
1453 nocrt: true,
1454 stl: "none",
1455 system_shared_libs: [],
1456 srcs: [""],
1457 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001458 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001459
1460 sanitize: {
1461 never: true,
1462 },
Paul Duffina02cae32021-03-09 01:44:06 +00001463 } `)
1464 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001465
1466 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1467 "lib64/bionic/libc.so",
1468 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1469 })
1470
Colin Cross4c4c1be2022-02-10 11:41:18 -08001471 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001472
1473 installed := hwasan.Description("install libclang_rt.hwasan")
1474 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1475
1476 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1477 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1478 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1479}
1480
1481func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001482 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001483 prepareForTestOfRuntimeApexWithHwasan,
1484 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1485 variables.SanitizeDevice = []string{"hwaddress"}
1486 }),
1487 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001488 cc_library {
1489 name: "libc",
1490 no_libcrt: true,
1491 nocrt: true,
1492 stl: "none",
1493 system_shared_libs: [],
1494 stubs: { versions: ["1"] },
1495 apex_available: ["com.android.runtime"],
1496 }
1497
1498 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001499 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001500 no_libcrt: true,
1501 nocrt: true,
1502 stl: "none",
1503 system_shared_libs: [],
1504 srcs: [""],
1505 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001506 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001507
1508 sanitize: {
1509 never: true,
1510 },
1511 }
Paul Duffina02cae32021-03-09 01:44:06 +00001512 `)
1513 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001514
1515 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1516 "lib64/bionic/libc.so",
1517 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1518 })
1519
Colin Cross4c4c1be2022-02-10 11:41:18 -08001520 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001521
1522 installed := hwasan.Description("install libclang_rt.hwasan")
1523 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1524
1525 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1526 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1527 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1528}
1529
Jooyung Han61b66e92020-03-21 14:21:46 +00001530func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1531 testcases := []struct {
1532 name string
1533 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001534 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001535 shouldLink string
1536 shouldNotLink []string
1537 }{
1538 {
Jiyong Park55549df2021-02-26 23:57:23 +09001539 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001540 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001541 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001542 shouldLink: "current",
1543 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001544 },
1545 {
Jiyong Park55549df2021-02-26 23:57:23 +09001546 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001547 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001548 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001549 shouldLink: "current",
1550 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001551 },
1552 }
1553 for _, tc := range testcases {
1554 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001555 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001556 apex {
1557 name: "myapex",
1558 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001559 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001560 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001561 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001562 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001563
Jooyung Han61b66e92020-03-21 14:21:46 +00001564 apex_key {
1565 name: "myapex.key",
1566 public_key: "testkey.avbpubkey",
1567 private_key: "testkey.pem",
1568 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001569
Jooyung Han61b66e92020-03-21 14:21:46 +00001570 cc_library {
1571 name: "mylib",
1572 srcs: ["mylib.cpp"],
1573 vendor_available: true,
1574 shared_libs: ["libbar"],
1575 system_shared_libs: [],
1576 stl: "none",
1577 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001578 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001579 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001580
Jooyung Han61b66e92020-03-21 14:21:46 +00001581 cc_library {
1582 name: "libbar",
1583 srcs: ["mylib.cpp"],
1584 system_shared_libs: [],
1585 stl: "none",
1586 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001587 llndk: {
1588 symbol_file: "libbar.map.txt",
1589 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001590 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001591 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001592 withUnbundledBuild,
1593 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001594
Jooyung Han61b66e92020-03-21 14:21:46 +00001595 // Ensure that LLNDK dep is not included
1596 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1597 "lib64/mylib.so",
1598 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001599
Jooyung Han61b66e92020-03-21 14:21:46 +00001600 // Ensure that LLNDK dep is required
1601 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1602 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1603 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001604
Steven Moreland2c4000c2021-04-27 02:08:49 +00001605 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1606 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001607 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001608 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001609 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001610
Steven Moreland2c4000c2021-04-27 02:08:49 +00001611 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001612 ver := tc.shouldLink
1613 if tc.shouldLink == "current" {
1614 ver = strconv.Itoa(android.FutureApiLevelInt)
1615 }
1616 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001617 })
1618 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001619}
1620
Jiyong Park25fc6a92018-11-18 18:02:45 +09001621func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001622 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001623 apex {
1624 name: "myapex",
1625 key: "myapex.key",
1626 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001627 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001628 }
1629
1630 apex_key {
1631 name: "myapex.key",
1632 public_key: "testkey.avbpubkey",
1633 private_key: "testkey.pem",
1634 }
1635
1636 cc_library {
1637 name: "mylib",
1638 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001639 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001640 shared_libs: ["libdl#27"],
1641 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001642 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001643 }
1644
1645 cc_library_shared {
1646 name: "mylib_shared",
1647 srcs: ["mylib.cpp"],
1648 shared_libs: ["libdl#27"],
1649 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001650 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001651 }
1652
1653 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001654 name: "libBootstrap",
1655 srcs: ["mylib.cpp"],
1656 stl: "none",
1657 bootstrap: true,
1658 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001659 `)
1660
Sundong Ahnabb64432019-10-22 13:58:29 +09001661 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001662 copyCmds := apexRule.Args["copy_commands"]
1663
1664 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001665 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001666 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1667 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001668
1669 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001670 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001671
Colin Crossaede88c2020-08-11 12:17:01 -07001672 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1673 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1674 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001675
1676 // For dependency to libc
1677 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001678 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001679 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001680 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001681 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001682 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1683 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001684
1685 // For dependency to libm
1686 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001687 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001688 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001689 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001690 // ... and is not compiling with the stub
1691 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1692 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1693
1694 // For dependency to libdl
1695 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001696 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001697 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001698 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1699 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001700 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001701 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001702 // ... Cflags from stub is correctly exported to mylib
1703 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1704 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001705
1706 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001707 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1708 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1709 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1710 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001711}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001712
Jooyung Han749dc692020-04-15 11:03:39 +09001713func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001714 // there are three links between liba --> libz.
1715 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001716 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001717 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001718 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001719 apex {
1720 name: "myapex",
1721 key: "myapex.key",
1722 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001723 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001724 }
1725
1726 apex {
1727 name: "otherapex",
1728 key: "myapex.key",
1729 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001730 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001731 }
1732
1733 apex_key {
1734 name: "myapex.key",
1735 public_key: "testkey.avbpubkey",
1736 private_key: "testkey.pem",
1737 }
1738
1739 cc_library {
1740 name: "libx",
1741 shared_libs: ["liba"],
1742 system_shared_libs: [],
1743 stl: "none",
1744 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001745 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001746 }
1747
1748 cc_library {
1749 name: "liby",
1750 shared_libs: ["liba"],
1751 system_shared_libs: [],
1752 stl: "none",
1753 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001754 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001755 }
1756
1757 cc_library {
1758 name: "liba",
1759 shared_libs: ["libz"],
1760 system_shared_libs: [],
1761 stl: "none",
1762 apex_available: [
1763 "//apex_available:anyapex",
1764 "//apex_available:platform",
1765 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001766 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001767 }
1768
1769 cc_library {
1770 name: "libz",
1771 system_shared_libs: [],
1772 stl: "none",
1773 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001774 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001775 },
1776 }
Jooyung Han749dc692020-04-15 11:03:39 +09001777 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001778
1779 expectLink := func(from, from_variant, to, to_variant string) {
1780 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1781 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1782 }
1783 expectNoLink := func(from, from_variant, to, to_variant string) {
1784 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1785 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1786 }
1787 // platform liba is linked to non-stub version
1788 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001789 // liba in myapex is linked to current
1790 expectLink("liba", "shared_apex29", "libz", "shared_current")
1791 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001792 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001793 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001794 // liba in otherapex is linked to current
1795 expectLink("liba", "shared_apex30", "libz", "shared_current")
1796 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001797 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1798 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001799}
1800
Jooyung Hanaed150d2020-04-02 01:41:41 +09001801func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001802 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001803 apex {
1804 name: "myapex",
1805 key: "myapex.key",
1806 native_shared_libs: ["libx"],
1807 min_sdk_version: "R",
1808 }
1809
1810 apex_key {
1811 name: "myapex.key",
1812 public_key: "testkey.avbpubkey",
1813 private_key: "testkey.pem",
1814 }
1815
1816 cc_library {
1817 name: "libx",
1818 shared_libs: ["libz"],
1819 system_shared_libs: [],
1820 stl: "none",
1821 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001822 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001823 }
1824
1825 cc_library {
1826 name: "libz",
1827 system_shared_libs: [],
1828 stl: "none",
1829 stubs: {
1830 versions: ["29", "R"],
1831 },
1832 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001833 `,
1834 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1835 variables.Platform_version_active_codenames = []string{"R"}
1836 }),
1837 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001838
1839 expectLink := func(from, from_variant, to, to_variant string) {
1840 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1841 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1842 }
1843 expectNoLink := func(from, from_variant, to, to_variant string) {
1844 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1845 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1846 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001847 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1848 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001849 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1850 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001851}
1852
Jooyung Han4c4da062021-06-23 10:23:16 +09001853func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1854 testApex(t, `
1855 apex {
1856 name: "myapex",
1857 key: "myapex.key",
1858 java_libs: ["libx"],
1859 min_sdk_version: "S",
1860 }
1861
1862 apex_key {
1863 name: "myapex.key",
1864 public_key: "testkey.avbpubkey",
1865 private_key: "testkey.pem",
1866 }
1867
1868 java_library {
1869 name: "libx",
1870 srcs: ["a.java"],
1871 apex_available: [ "myapex" ],
1872 sdk_version: "current",
1873 min_sdk_version: "S", // should be okay
1874 }
1875 `,
1876 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1877 variables.Platform_version_active_codenames = []string{"S"}
1878 variables.Platform_sdk_codename = proptools.StringPtr("S")
1879 }),
1880 )
1881}
1882
Jooyung Han749dc692020-04-15 11:03:39 +09001883func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001884 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001885 apex {
1886 name: "myapex",
1887 key: "myapex.key",
1888 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001889 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001890 }
1891
1892 apex_key {
1893 name: "myapex.key",
1894 public_key: "testkey.avbpubkey",
1895 private_key: "testkey.pem",
1896 }
1897
1898 cc_library {
1899 name: "libx",
1900 shared_libs: ["libz"],
1901 system_shared_libs: [],
1902 stl: "none",
1903 apex_available: [ "myapex" ],
1904 }
1905
1906 cc_library {
1907 name: "libz",
1908 system_shared_libs: [],
1909 stl: "none",
1910 stubs: {
1911 versions: ["1", "2"],
1912 },
1913 }
1914 `)
1915
1916 expectLink := func(from, from_variant, to, to_variant string) {
1917 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1918 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1919 }
1920 expectNoLink := func(from, from_variant, to, to_variant string) {
1921 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1922 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1923 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001924 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001925 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001926 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001927 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001928}
1929
Jooyung Handfc864c2023-03-20 18:19:07 +09001930func TestApexMinSdkVersion_InVendorApex(t *testing.T) {
Jiyong Park5df7bd32021-08-25 16:18:46 +09001931 ctx := testApex(t, `
1932 apex {
1933 name: "myapex",
1934 key: "myapex.key",
1935 native_shared_libs: ["mylib"],
Jooyung Handfc864c2023-03-20 18:19:07 +09001936 updatable: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09001937 vendor: true,
1938 min_sdk_version: "29",
1939 }
1940
1941 apex_key {
1942 name: "myapex.key",
1943 public_key: "testkey.avbpubkey",
1944 private_key: "testkey.pem",
1945 }
1946
1947 cc_library {
1948 name: "mylib",
Jooyung Handfc864c2023-03-20 18:19:07 +09001949 srcs: ["mylib.cpp"],
Jiyong Park5df7bd32021-08-25 16:18:46 +09001950 vendor_available: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09001951 min_sdk_version: "29",
Jooyung Handfc864c2023-03-20 18:19:07 +09001952 shared_libs: ["libbar"],
1953 }
1954
1955 cc_library {
1956 name: "libbar",
1957 stubs: { versions: ["29", "30"] },
1958 llndk: { symbol_file: "libbar.map.txt" },
Jiyong Park5df7bd32021-08-25 16:18:46 +09001959 }
1960 `)
1961
1962 vendorVariant := "android_vendor.29_arm64_armv8-a"
1963
Jooyung Handfc864c2023-03-20 18:19:07 +09001964 mylib := ctx.ModuleForTests("mylib", vendorVariant+"_shared_myapex")
1965
1966 // Ensure that mylib links with "current" LLNDK
1967 libFlags := names(mylib.Rule("ld").Args["libFlags"])
1968 ensureListContains(t, libFlags, "out/soong/.intermediates/libbar/"+vendorVariant+"_shared_current/libbar.so")
1969
1970 // Ensure that mylib is targeting 29
1971 ccRule := ctx.ModuleForTests("mylib", vendorVariant+"_static_apex29").Output("obj/mylib.o")
1972 ensureContains(t, ccRule.Args["cFlags"], "-target aarch64-linux-android29")
1973
1974 // Ensure that the correct variant of crtbegin_so is used.
1975 crtBegin := mylib.Rule("ld").Args["crtBegin"]
1976 ensureContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
Jiyong Park5df7bd32021-08-25 16:18:46 +09001977
1978 // Ensure that the crtbegin_so used by the APEX is targeting 29
1979 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
1980 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
1981}
1982
Jooyung Han03b51852020-02-26 22:45:42 +09001983func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001984 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001985 apex {
1986 name: "myapex",
1987 key: "myapex.key",
1988 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001989 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001990 }
1991
1992 apex_key {
1993 name: "myapex.key",
1994 public_key: "testkey.avbpubkey",
1995 private_key: "testkey.pem",
1996 }
1997
1998 cc_library {
1999 name: "libx",
2000 system_shared_libs: [],
2001 stl: "none",
2002 apex_available: [ "myapex" ],
2003 stubs: {
2004 versions: ["1", "2"],
2005 },
2006 }
2007
2008 cc_library {
2009 name: "libz",
2010 shared_libs: ["libx"],
2011 system_shared_libs: [],
2012 stl: "none",
2013 }
2014 `)
2015
2016 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002017 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002018 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2019 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2020 }
2021 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002022 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002023 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2024 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2025 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002026 expectLink("libz", "shared", "libx", "shared_current")
2027 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002028 expectNoLink("libz", "shared", "libz", "shared_1")
2029 expectNoLink("libz", "shared", "libz", "shared")
2030}
2031
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002032var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2033 func(variables android.FixtureProductVariables) {
2034 variables.SanitizeDevice = []string{"hwaddress"}
2035 },
2036)
2037
Jooyung Han75568392020-03-20 04:29:24 +09002038func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002039 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002040 apex {
2041 name: "myapex",
2042 key: "myapex.key",
2043 native_shared_libs: ["libx"],
2044 min_sdk_version: "29",
2045 }
2046
2047 apex_key {
2048 name: "myapex.key",
2049 public_key: "testkey.avbpubkey",
2050 private_key: "testkey.pem",
2051 }
2052
2053 cc_library {
2054 name: "libx",
2055 shared_libs: ["libbar"],
2056 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002057 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002058 }
2059
2060 cc_library {
2061 name: "libbar",
2062 stubs: {
2063 versions: ["29", "30"],
2064 },
2065 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002066 `,
2067 prepareForTestWithSantitizeHwaddress,
2068 )
Jooyung Han03b51852020-02-26 22:45:42 +09002069 expectLink := func(from, from_variant, to, to_variant string) {
2070 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2071 libFlags := ld.Args["libFlags"]
2072 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2073 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002074 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002075}
2076
Jooyung Han75568392020-03-20 04:29:24 +09002077func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002078 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002079 apex {
2080 name: "myapex",
2081 key: "myapex.key",
2082 native_shared_libs: ["libx"],
2083 min_sdk_version: "29",
2084 }
2085
2086 apex_key {
2087 name: "myapex.key",
2088 public_key: "testkey.avbpubkey",
2089 private_key: "testkey.pem",
2090 }
2091
2092 cc_library {
2093 name: "libx",
2094 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002095 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002096 }
Jooyung Han75568392020-03-20 04:29:24 +09002097 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002098
2099 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002100 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002101 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002102 // note that platform variant is not.
2103 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002104 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002105}
2106
Jooyung Han749dc692020-04-15 11:03:39 +09002107func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2108 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002109 apex {
2110 name: "myapex",
2111 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002112 native_shared_libs: ["mylib"],
2113 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002114 }
2115
2116 apex_key {
2117 name: "myapex.key",
2118 public_key: "testkey.avbpubkey",
2119 private_key: "testkey.pem",
2120 }
Jooyung Han749dc692020-04-15 11:03:39 +09002121
2122 cc_library {
2123 name: "mylib",
2124 srcs: ["mylib.cpp"],
2125 system_shared_libs: [],
2126 stl: "none",
2127 apex_available: [
2128 "myapex",
2129 ],
2130 min_sdk_version: "30",
2131 }
2132 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002133
2134 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2135 apex {
2136 name: "myapex",
2137 key: "myapex.key",
2138 native_shared_libs: ["libfoo.ffi"],
2139 min_sdk_version: "29",
2140 }
2141
2142 apex_key {
2143 name: "myapex.key",
2144 public_key: "testkey.avbpubkey",
2145 private_key: "testkey.pem",
2146 }
2147
2148 rust_ffi_shared {
2149 name: "libfoo.ffi",
2150 srcs: ["foo.rs"],
2151 crate_name: "foo",
2152 apex_available: [
2153 "myapex",
2154 ],
2155 min_sdk_version: "30",
2156 }
2157 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002158
2159 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2160 apex {
2161 name: "myapex",
2162 key: "myapex.key",
2163 java_libs: ["libfoo"],
2164 min_sdk_version: "29",
2165 }
2166
2167 apex_key {
2168 name: "myapex.key",
2169 public_key: "testkey.avbpubkey",
2170 private_key: "testkey.pem",
2171 }
2172
2173 java_import {
2174 name: "libfoo",
2175 jars: ["libfoo.jar"],
2176 apex_available: [
2177 "myapex",
2178 ],
2179 min_sdk_version: "30",
2180 }
2181 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002182
2183 // Skip check for modules compiling against core API surface
2184 testApex(t, `
2185 apex {
2186 name: "myapex",
2187 key: "myapex.key",
2188 java_libs: ["libfoo"],
2189 min_sdk_version: "29",
2190 }
2191
2192 apex_key {
2193 name: "myapex.key",
2194 public_key: "testkey.avbpubkey",
2195 private_key: "testkey.pem",
2196 }
2197
2198 java_library {
2199 name: "libfoo",
2200 srcs: ["Foo.java"],
2201 apex_available: [
2202 "myapex",
2203 ],
2204 // Compile against core API surface
2205 sdk_version: "core_current",
2206 min_sdk_version: "30",
2207 }
2208 `)
2209
Jooyung Han749dc692020-04-15 11:03:39 +09002210}
2211
2212func TestApexMinSdkVersion_Okay(t *testing.T) {
2213 testApex(t, `
2214 apex {
2215 name: "myapex",
2216 key: "myapex.key",
2217 native_shared_libs: ["libfoo"],
2218 java_libs: ["libbar"],
2219 min_sdk_version: "29",
2220 }
2221
2222 apex_key {
2223 name: "myapex.key",
2224 public_key: "testkey.avbpubkey",
2225 private_key: "testkey.pem",
2226 }
2227
2228 cc_library {
2229 name: "libfoo",
2230 srcs: ["mylib.cpp"],
2231 shared_libs: ["libfoo_dep"],
2232 apex_available: ["myapex"],
2233 min_sdk_version: "29",
2234 }
2235
2236 cc_library {
2237 name: "libfoo_dep",
2238 srcs: ["mylib.cpp"],
2239 apex_available: ["myapex"],
2240 min_sdk_version: "29",
2241 }
2242
2243 java_library {
2244 name: "libbar",
2245 sdk_version: "current",
2246 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002247 static_libs: [
2248 "libbar_dep",
2249 "libbar_import_dep",
2250 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002251 apex_available: ["myapex"],
2252 min_sdk_version: "29",
2253 }
2254
2255 java_library {
2256 name: "libbar_dep",
2257 sdk_version: "current",
2258 srcs: ["a.java"],
2259 apex_available: ["myapex"],
2260 min_sdk_version: "29",
2261 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002262
2263 java_import {
2264 name: "libbar_import_dep",
2265 jars: ["libbar.jar"],
2266 apex_available: ["myapex"],
2267 min_sdk_version: "29",
2268 }
Jooyung Han03b51852020-02-26 22:45:42 +09002269 `)
2270}
2271
Colin Cross8ca61c12022-10-06 21:00:14 -07002272func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2273 // Tests that an apex dependency with min_sdk_version higher than the
2274 // min_sdk_version of the apex is allowed as long as the dependency's
2275 // min_sdk_version is less than or equal to the api level that the
2276 // architecture was introduced in. In this case, arm64 didn't exist
2277 // until api level 21, so the arm64 code will never need to run on
2278 // an api level 20 device, even if other architectures of the apex
2279 // will.
2280 testApex(t, `
2281 apex {
2282 name: "myapex",
2283 key: "myapex.key",
2284 native_shared_libs: ["libfoo"],
2285 min_sdk_version: "20",
2286 }
2287
2288 apex_key {
2289 name: "myapex.key",
2290 public_key: "testkey.avbpubkey",
2291 private_key: "testkey.pem",
2292 }
2293
2294 cc_library {
2295 name: "libfoo",
2296 srcs: ["mylib.cpp"],
2297 apex_available: ["myapex"],
2298 min_sdk_version: "21",
2299 stl: "none",
2300 }
2301 `)
2302}
2303
Artur Satayev8cf899a2020-04-15 17:29:42 +01002304func TestJavaStableSdkVersion(t *testing.T) {
2305 testCases := []struct {
2306 name string
2307 expectedError string
2308 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002309 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002310 }{
2311 {
2312 name: "Non-updatable apex with non-stable dep",
2313 bp: `
2314 apex {
2315 name: "myapex",
2316 java_libs: ["myjar"],
2317 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002318 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002319 }
2320 apex_key {
2321 name: "myapex.key",
2322 public_key: "testkey.avbpubkey",
2323 private_key: "testkey.pem",
2324 }
2325 java_library {
2326 name: "myjar",
2327 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002328 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002329 apex_available: ["myapex"],
2330 }
2331 `,
2332 },
2333 {
2334 name: "Updatable apex with stable dep",
2335 bp: `
2336 apex {
2337 name: "myapex",
2338 java_libs: ["myjar"],
2339 key: "myapex.key",
2340 updatable: true,
2341 min_sdk_version: "29",
2342 }
2343 apex_key {
2344 name: "myapex.key",
2345 public_key: "testkey.avbpubkey",
2346 private_key: "testkey.pem",
2347 }
2348 java_library {
2349 name: "myjar",
2350 srcs: ["foo/bar/MyClass.java"],
2351 sdk_version: "current",
2352 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002353 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002354 }
2355 `,
2356 },
2357 {
2358 name: "Updatable apex with non-stable dep",
2359 expectedError: "cannot depend on \"myjar\"",
2360 bp: `
2361 apex {
2362 name: "myapex",
2363 java_libs: ["myjar"],
2364 key: "myapex.key",
2365 updatable: true,
2366 }
2367 apex_key {
2368 name: "myapex.key",
2369 public_key: "testkey.avbpubkey",
2370 private_key: "testkey.pem",
2371 }
2372 java_library {
2373 name: "myjar",
2374 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002375 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002376 apex_available: ["myapex"],
2377 }
2378 `,
2379 },
2380 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002381 name: "Updatable apex with non-stable legacy core platform dep",
2382 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2383 bp: `
2384 apex {
2385 name: "myapex",
2386 java_libs: ["myjar-uses-legacy"],
2387 key: "myapex.key",
2388 updatable: true,
2389 }
2390 apex_key {
2391 name: "myapex.key",
2392 public_key: "testkey.avbpubkey",
2393 private_key: "testkey.pem",
2394 }
2395 java_library {
2396 name: "myjar-uses-legacy",
2397 srcs: ["foo/bar/MyClass.java"],
2398 sdk_version: "core_platform",
2399 apex_available: ["myapex"],
2400 }
2401 `,
2402 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2403 },
2404 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002405 name: "Updatable apex with non-stable transitive dep",
2406 // This is not actually detecting that the transitive dependency is unstable, rather it is
2407 // detecting that the transitive dependency is building against a wider API surface than the
2408 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002409 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002410 bp: `
2411 apex {
2412 name: "myapex",
2413 java_libs: ["myjar"],
2414 key: "myapex.key",
2415 updatable: true,
2416 }
2417 apex_key {
2418 name: "myapex.key",
2419 public_key: "testkey.avbpubkey",
2420 private_key: "testkey.pem",
2421 }
2422 java_library {
2423 name: "myjar",
2424 srcs: ["foo/bar/MyClass.java"],
2425 sdk_version: "current",
2426 apex_available: ["myapex"],
2427 static_libs: ["transitive-jar"],
2428 }
2429 java_library {
2430 name: "transitive-jar",
2431 srcs: ["foo/bar/MyClass.java"],
2432 sdk_version: "core_platform",
2433 apex_available: ["myapex"],
2434 }
2435 `,
2436 },
2437 }
2438
2439 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002440 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2441 continue
2442 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002443 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002444 errorHandler := android.FixtureExpectsNoErrors
2445 if test.expectedError != "" {
2446 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002447 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002448 android.GroupFixturePreparers(
2449 java.PrepareForTestWithJavaDefaultModules,
2450 PrepareForTestWithApexBuildComponents,
2451 prepareForTestWithMyapex,
2452 android.OptionalFixturePreparer(test.preparer),
2453 ).
2454 ExtendWithErrorHandler(errorHandler).
2455 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002456 })
2457 }
2458}
2459
Jooyung Han749dc692020-04-15 11:03:39 +09002460func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2461 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2462 apex {
2463 name: "myapex",
2464 key: "myapex.key",
2465 native_shared_libs: ["mylib"],
2466 min_sdk_version: "29",
2467 }
2468
2469 apex_key {
2470 name: "myapex.key",
2471 public_key: "testkey.avbpubkey",
2472 private_key: "testkey.pem",
2473 }
2474
2475 cc_library {
2476 name: "mylib",
2477 srcs: ["mylib.cpp"],
2478 shared_libs: ["mylib2"],
2479 system_shared_libs: [],
2480 stl: "none",
2481 apex_available: [
2482 "myapex",
2483 ],
2484 min_sdk_version: "29",
2485 }
2486
2487 // indirect part of the apex
2488 cc_library {
2489 name: "mylib2",
2490 srcs: ["mylib.cpp"],
2491 system_shared_libs: [],
2492 stl: "none",
2493 apex_available: [
2494 "myapex",
2495 ],
2496 min_sdk_version: "30",
2497 }
2498 `)
2499}
2500
2501func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2502 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2503 apex {
2504 name: "myapex",
2505 key: "myapex.key",
2506 apps: ["AppFoo"],
2507 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002508 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002509 }
2510
2511 apex_key {
2512 name: "myapex.key",
2513 public_key: "testkey.avbpubkey",
2514 private_key: "testkey.pem",
2515 }
2516
2517 android_app {
2518 name: "AppFoo",
2519 srcs: ["foo/bar/MyClass.java"],
2520 sdk_version: "current",
2521 min_sdk_version: "29",
2522 system_modules: "none",
2523 stl: "none",
2524 static_libs: ["bar"],
2525 apex_available: [ "myapex" ],
2526 }
2527
2528 java_library {
2529 name: "bar",
2530 sdk_version: "current",
2531 srcs: ["a.java"],
2532 apex_available: [ "myapex" ],
2533 }
2534 `)
2535}
2536
2537func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002538 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002539 apex {
2540 name: "myapex",
2541 key: "myapex.key",
2542 native_shared_libs: ["mylib"],
2543 min_sdk_version: "29",
2544 }
2545
2546 apex_key {
2547 name: "myapex.key",
2548 public_key: "testkey.avbpubkey",
2549 private_key: "testkey.pem",
2550 }
2551
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002552 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002553 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2554 cc_library {
2555 name: "mylib",
2556 srcs: ["mylib.cpp"],
2557 shared_libs: ["mylib2"],
2558 system_shared_libs: [],
2559 stl: "none",
2560 apex_available: ["myapex", "otherapex"],
2561 min_sdk_version: "29",
2562 }
2563
2564 cc_library {
2565 name: "mylib2",
2566 srcs: ["mylib.cpp"],
2567 system_shared_libs: [],
2568 stl: "none",
2569 apex_available: ["otherapex"],
2570 stubs: { versions: ["29", "30"] },
2571 min_sdk_version: "30",
2572 }
2573
2574 apex {
2575 name: "otherapex",
2576 key: "myapex.key",
2577 native_shared_libs: ["mylib", "mylib2"],
2578 min_sdk_version: "30",
2579 }
2580 `)
2581 expectLink := func(from, from_variant, to, to_variant string) {
2582 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2583 libFlags := ld.Args["libFlags"]
2584 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2585 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002586 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002587 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002588}
2589
Jooyung Haned124c32021-01-26 11:43:46 +09002590func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002591 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2592 func(variables android.FixtureProductVariables) {
2593 variables.Platform_sdk_codename = proptools.StringPtr("S")
2594 variables.Platform_version_active_codenames = []string{"S"}
2595 },
2596 )
Jooyung Haned124c32021-01-26 11:43:46 +09002597 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2598 apex {
2599 name: "myapex",
2600 key: "myapex.key",
2601 native_shared_libs: ["libfoo"],
2602 min_sdk_version: "S",
2603 }
2604 apex_key {
2605 name: "myapex.key",
2606 public_key: "testkey.avbpubkey",
2607 private_key: "testkey.pem",
2608 }
2609 cc_library {
2610 name: "libfoo",
2611 shared_libs: ["libbar"],
2612 apex_available: ["myapex"],
2613 min_sdk_version: "29",
2614 }
2615 cc_library {
2616 name: "libbar",
2617 apex_available: ["myapex"],
2618 }
2619 `, withSAsActiveCodeNames)
2620}
2621
2622func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002623 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2624 variables.Platform_sdk_codename = proptools.StringPtr("S")
2625 variables.Platform_version_active_codenames = []string{"S", "T"}
2626 })
Colin Cross1c460562021-02-16 17:55:47 -08002627 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002628 apex {
2629 name: "myapex",
2630 key: "myapex.key",
2631 native_shared_libs: ["libfoo"],
2632 min_sdk_version: "S",
2633 }
2634 apex_key {
2635 name: "myapex.key",
2636 public_key: "testkey.avbpubkey",
2637 private_key: "testkey.pem",
2638 }
2639 cc_library {
2640 name: "libfoo",
2641 shared_libs: ["libbar"],
2642 apex_available: ["myapex"],
2643 min_sdk_version: "S",
2644 }
2645 cc_library {
2646 name: "libbar",
2647 stubs: {
2648 symbol_file: "libbar.map.txt",
2649 versions: ["30", "S", "T"],
2650 },
2651 }
2652 `, withSAsActiveCodeNames)
2653
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002654 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002655 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2656 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002657 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002658}
2659
Jiyong Park7c2ee712018-12-07 00:42:25 +09002660func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002661 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002662 apex {
2663 name: "myapex",
2664 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002665 native_shared_libs: ["mylib"],
2666 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002667 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002668 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002669 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002670 }
2671
2672 apex_key {
2673 name: "myapex.key",
2674 public_key: "testkey.avbpubkey",
2675 private_key: "testkey.pem",
2676 }
2677
2678 prebuilt_etc {
2679 name: "myetc",
2680 src: "myprebuilt",
2681 sub_dir: "foo/bar",
2682 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002683
2684 cc_library {
2685 name: "mylib",
2686 srcs: ["mylib.cpp"],
2687 relative_install_path: "foo/bar",
2688 system_shared_libs: [],
2689 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002690 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002691 }
2692
2693 cc_binary {
2694 name: "mybin",
2695 srcs: ["mylib.cpp"],
2696 relative_install_path: "foo/bar",
2697 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002698 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002699 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002700 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002701 `)
2702
Sundong Ahnabb64432019-10-22 13:58:29 +09002703 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002704 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002705
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002706 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002707 ensureContains(t, cmd, "/etc ")
2708 ensureContains(t, cmd, "/etc/foo ")
2709 ensureContains(t, cmd, "/etc/foo/bar ")
2710 ensureContains(t, cmd, "/lib64 ")
2711 ensureContains(t, cmd, "/lib64/foo ")
2712 ensureContains(t, cmd, "/lib64/foo/bar ")
2713 ensureContains(t, cmd, "/lib ")
2714 ensureContains(t, cmd, "/lib/foo ")
2715 ensureContains(t, cmd, "/lib/foo/bar ")
2716 ensureContains(t, cmd, "/bin ")
2717 ensureContains(t, cmd, "/bin/foo ")
2718 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002719}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002720
Jooyung Han35155c42020-02-06 17:33:20 +09002721func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002722 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002723 apex {
2724 name: "myapex",
2725 key: "myapex.key",
2726 multilib: {
2727 both: {
2728 native_shared_libs: ["mylib"],
2729 binaries: ["mybin"],
2730 },
2731 },
2732 compile_multilib: "both",
2733 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002734 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002735 }
2736
2737 apex_key {
2738 name: "myapex.key",
2739 public_key: "testkey.avbpubkey",
2740 private_key: "testkey.pem",
2741 }
2742
2743 cc_library {
2744 name: "mylib",
2745 relative_install_path: "foo/bar",
2746 system_shared_libs: [],
2747 stl: "none",
2748 apex_available: [ "myapex" ],
2749 native_bridge_supported: true,
2750 }
2751
2752 cc_binary {
2753 name: "mybin",
2754 relative_install_path: "foo/bar",
2755 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002756 stl: "none",
2757 apex_available: [ "myapex" ],
2758 native_bridge_supported: true,
2759 compile_multilib: "both", // default is "first" for binary
2760 multilib: {
2761 lib64: {
2762 suffix: "64",
2763 },
2764 },
2765 }
2766 `, withNativeBridgeEnabled)
2767 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2768 "bin/foo/bar/mybin",
2769 "bin/foo/bar/mybin64",
2770 "bin/arm/foo/bar/mybin",
2771 "bin/arm64/foo/bar/mybin64",
2772 "lib/foo/bar/mylib.so",
2773 "lib/arm/foo/bar/mylib.so",
2774 "lib64/foo/bar/mylib.so",
2775 "lib64/arm64/foo/bar/mylib.so",
2776 })
2777}
2778
Jooyung Han85d61762020-06-24 23:50:26 +09002779func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002780 result := android.GroupFixturePreparers(
2781 prepareForApexTest,
2782 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2783 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002784 apex {
2785 name: "myapex",
2786 key: "myapex.key",
2787 binaries: ["mybin"],
2788 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002789 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002790 }
2791 apex_key {
2792 name: "myapex.key",
2793 public_key: "testkey.avbpubkey",
2794 private_key: "testkey.pem",
2795 }
2796 cc_binary {
2797 name: "mybin",
2798 vendor: true,
2799 shared_libs: ["libfoo"],
2800 }
2801 cc_library {
2802 name: "libfoo",
2803 proprietary: true,
2804 }
2805 `)
2806
Colin Crossc68db4b2021-11-11 18:59:15 -08002807 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002808 "bin/mybin",
2809 "lib64/libfoo.so",
2810 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2811 "lib64/libc++.so",
2812 })
2813
Colin Crossc68db4b2021-11-11 18:59:15 -08002814 apexBundle := result.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2815 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002816 name := apexBundle.BaseModuleName()
2817 prefix := "TARGET_"
2818 var builder strings.Builder
2819 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002820 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002821 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002822 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002823
Colin Crossc68db4b2021-11-11 18:59:15 -08002824 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002825 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2826 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002827}
2828
Jooyung Hanc5a96762022-02-04 11:54:50 +09002829func TestVendorApex_use_vndk_as_stable_TryingToIncludeVNDKLib(t *testing.T) {
2830 testApexError(t, `Trying to include a VNDK library`, `
2831 apex {
2832 name: "myapex",
2833 key: "myapex.key",
2834 native_shared_libs: ["libc++"], // libc++ is a VNDK lib
2835 vendor: true,
2836 use_vndk_as_stable: true,
2837 updatable: false,
2838 }
2839 apex_key {
2840 name: "myapex.key",
2841 public_key: "testkey.avbpubkey",
2842 private_key: "testkey.pem",
2843 }`)
2844}
2845
Jooyung Handf78e212020-07-22 15:54:47 +09002846func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Jooyung Han91f92032022-02-04 12:36:33 +09002847 // myapex myapex2
2848 // | |
2849 // mybin ------. mybin2
2850 // \ \ / |
2851 // (stable) .---\--------` |
2852 // \ / \ |
2853 // \ / \ /
2854 // libvndk libvendor
2855 // (vndk)
Colin Cross1c460562021-02-16 17:55:47 -08002856 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002857 apex {
2858 name: "myapex",
2859 key: "myapex.key",
2860 binaries: ["mybin"],
2861 vendor: true,
2862 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002863 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002864 }
2865 apex_key {
2866 name: "myapex.key",
2867 public_key: "testkey.avbpubkey",
2868 private_key: "testkey.pem",
2869 }
2870 cc_binary {
2871 name: "mybin",
2872 vendor: true,
2873 shared_libs: ["libvndk", "libvendor"],
2874 }
2875 cc_library {
2876 name: "libvndk",
2877 vndk: {
2878 enabled: true,
2879 },
2880 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002881 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002882 }
2883 cc_library {
2884 name: "libvendor",
2885 vendor: true,
Jooyung Han91f92032022-02-04 12:36:33 +09002886 stl: "none",
2887 }
2888 apex {
2889 name: "myapex2",
2890 key: "myapex.key",
2891 binaries: ["mybin2"],
2892 vendor: true,
2893 use_vndk_as_stable: false,
2894 updatable: false,
2895 }
2896 cc_binary {
2897 name: "mybin2",
2898 vendor: true,
2899 shared_libs: ["libvndk", "libvendor"],
Jooyung Handf78e212020-07-22 15:54:47 +09002900 }
2901 `)
2902
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002903 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002904
Jooyung Han91f92032022-02-04 12:36:33 +09002905 for _, tc := range []struct {
2906 name string
2907 apexName string
2908 moduleName string
2909 moduleVariant string
2910 libs []string
2911 contents []string
2912 requireVndkNamespace bool
2913 }{
2914 {
2915 name: "use_vndk_as_stable",
2916 apexName: "myapex",
2917 moduleName: "mybin",
2918 moduleVariant: vendorVariant + "_apex10000",
2919 libs: []string{
2920 // should link with vendor variants of VNDK libs(libvndk/libc++)
2921 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared/libvndk.so",
2922 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared/libc++.so",
2923 // unstable Vendor libs as APEX variant
2924 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2925 },
2926 contents: []string{
2927 "bin/mybin",
2928 "lib64/libvendor.so",
2929 // VNDK libs (libvndk/libc++) are not included
2930 },
2931 requireVndkNamespace: true,
2932 },
2933 {
2934 name: "!use_vndk_as_stable",
2935 apexName: "myapex2",
2936 moduleName: "mybin2",
2937 moduleVariant: vendorVariant + "_myapex2",
2938 libs: []string{
2939 // should link with "unique" APEX(myapex2) variant of VNDK libs(libvndk/libc++)
2940 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared_myapex2/libvndk.so",
2941 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared_myapex2/libc++.so",
2942 // unstable vendor libs have "merged" APEX variants
2943 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2944 },
2945 contents: []string{
2946 "bin/mybin2",
2947 "lib64/libvendor.so",
2948 // VNDK libs are included as well
2949 "lib64/libvndk.so",
2950 "lib64/libc++.so",
2951 },
2952 requireVndkNamespace: false,
2953 },
2954 } {
2955 t.Run(tc.name, func(t *testing.T) {
2956 // Check linked libs
2957 ldRule := ctx.ModuleForTests(tc.moduleName, tc.moduleVariant).Rule("ld")
2958 libs := names(ldRule.Args["libFlags"])
2959 for _, lib := range tc.libs {
2960 ensureListContains(t, libs, lib)
2961 }
2962 // Check apex contents
2963 ensureExactContents(t, ctx, tc.apexName, "android_common_"+tc.apexName+"_image", tc.contents)
Jooyung Handf78e212020-07-22 15:54:47 +09002964
Jooyung Han91f92032022-02-04 12:36:33 +09002965 // Check "requireNativeLibs"
2966 apexManifestRule := ctx.ModuleForTests(tc.apexName, "android_common_"+tc.apexName+"_image").Rule("apexManifestRule")
2967 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2968 if tc.requireVndkNamespace {
2969 ensureListContains(t, requireNativeLibs, ":vndk")
2970 } else {
2971 ensureListNotContains(t, requireNativeLibs, ":vndk")
2972 }
2973 })
2974 }
Jooyung Handf78e212020-07-22 15:54:47 +09002975}
2976
Justin Yun13decfb2021-03-08 19:25:55 +09002977func TestProductVariant(t *testing.T) {
2978 ctx := testApex(t, `
2979 apex {
2980 name: "myapex",
2981 key: "myapex.key",
2982 updatable: false,
2983 product_specific: true,
2984 binaries: ["foo"],
2985 }
2986
2987 apex_key {
2988 name: "myapex.key",
2989 public_key: "testkey.avbpubkey",
2990 private_key: "testkey.pem",
2991 }
2992
2993 cc_binary {
2994 name: "foo",
2995 product_available: true,
2996 apex_available: ["myapex"],
2997 srcs: ["foo.cpp"],
2998 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002999 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3000 variables.ProductVndkVersion = proptools.StringPtr("current")
3001 }),
3002 )
Justin Yun13decfb2021-03-08 19:25:55 +09003003
3004 cflags := strings.Fields(
Jooyung Han91f92032022-02-04 12:36:33 +09003005 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_myapex").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09003006 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
3007 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
3008 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
3009 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
3010}
3011
Jooyung Han8e5685d2020-09-21 11:02:57 +09003012func TestApex_withPrebuiltFirmware(t *testing.T) {
3013 testCases := []struct {
3014 name string
3015 additionalProp string
3016 }{
3017 {"system apex with prebuilt_firmware", ""},
3018 {"vendor apex with prebuilt_firmware", "vendor: true,"},
3019 }
3020 for _, tc := range testCases {
3021 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003022 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09003023 apex {
3024 name: "myapex",
3025 key: "myapex.key",
3026 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003027 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09003028 `+tc.additionalProp+`
3029 }
3030 apex_key {
3031 name: "myapex.key",
3032 public_key: "testkey.avbpubkey",
3033 private_key: "testkey.pem",
3034 }
3035 prebuilt_firmware {
3036 name: "myfirmware",
3037 src: "myfirmware.bin",
3038 filename_from_src: true,
3039 `+tc.additionalProp+`
3040 }
3041 `)
3042 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3043 "etc/firmware/myfirmware.bin",
3044 })
3045 })
3046 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003047}
3048
Jooyung Hanefb184e2020-06-25 17:14:25 +09003049func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003050 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003051 apex {
3052 name: "myapex",
3053 key: "myapex.key",
3054 vendor: true,
3055 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003056 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003057 }
3058
3059 apex_key {
3060 name: "myapex.key",
3061 public_key: "testkey.avbpubkey",
3062 private_key: "testkey.pem",
3063 }
3064
3065 cc_library {
3066 name: "mylib",
3067 vendor_available: true,
3068 }
3069 `)
3070
3071 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003072 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003073 name := apexBundle.BaseModuleName()
3074 prefix := "TARGET_"
3075 var builder strings.Builder
3076 data.Custom(&builder, name, prefix, "", data)
3077 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00003078 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++.vendor.myapex:64 mylib.vendor.myapex:64 apex_manifest.pb.myapex apex_pubkey.myapex libc.vendor libm.vendor libdl.vendor\n")
Jooyung Hanefb184e2020-06-25 17:14:25 +09003079}
3080
Jooyung Han2ed99d02020-06-24 23:26:26 +09003081func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003082 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003083 apex {
3084 name: "myapex",
3085 key: "myapex.key",
3086 vintf_fragments: ["fragment.xml"],
3087 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003088 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003089 }
3090 apex_key {
3091 name: "myapex.key",
3092 public_key: "testkey.avbpubkey",
3093 private_key: "testkey.pem",
3094 }
3095 cc_binary {
3096 name: "mybin",
3097 }
3098 `)
3099
3100 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003101 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003102 name := apexBundle.BaseModuleName()
3103 prefix := "TARGET_"
3104 var builder strings.Builder
3105 data.Custom(&builder, name, prefix, "", data)
3106 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003107 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003108 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003109}
3110
Jiyong Park16e91a02018-12-20 18:18:08 +09003111func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003112 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003113 apex {
3114 name: "myapex",
3115 key: "myapex.key",
3116 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003117 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003118 }
3119
3120 apex_key {
3121 name: "myapex.key",
3122 public_key: "testkey.avbpubkey",
3123 private_key: "testkey.pem",
3124 }
3125
3126 cc_library {
3127 name: "mylib",
3128 srcs: ["mylib.cpp"],
3129 system_shared_libs: [],
3130 stl: "none",
3131 stubs: {
3132 versions: ["1", "2", "3"],
3133 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003134 apex_available: [
3135 "//apex_available:platform",
3136 "myapex",
3137 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09003138 }
3139
3140 cc_binary {
3141 name: "not_in_apex",
3142 srcs: ["mylib.cpp"],
3143 static_libs: ["mylib"],
3144 static_executable: true,
3145 system_shared_libs: [],
3146 stl: "none",
3147 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003148 `)
3149
Colin Cross7113d202019-11-20 16:39:12 -08003150 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003151
3152 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003153 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003154}
Jiyong Park9335a262018-12-24 11:31:58 +09003155
3156func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003157 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003158 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003159 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003160 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003161 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003162 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003163 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003164 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003165 }
3166
3167 cc_library {
3168 name: "mylib",
3169 srcs: ["mylib.cpp"],
3170 system_shared_libs: [],
3171 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003172 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003173 }
3174
3175 apex_key {
3176 name: "myapex.key",
3177 public_key: "testkey.avbpubkey",
3178 private_key: "testkey.pem",
3179 }
3180
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003181 android_app_certificate {
3182 name: "myapex.certificate",
3183 certificate: "testkey",
3184 }
3185
3186 android_app_certificate {
3187 name: "myapex.certificate.override",
3188 certificate: "testkey.override",
3189 }
3190
Jiyong Park9335a262018-12-24 11:31:58 +09003191 `)
3192
3193 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003194 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003195
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003196 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3197 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003198 "vendor/foo/devkeys/testkey.avbpubkey")
3199 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003200 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3201 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003202 "vendor/foo/devkeys/testkey.pem")
3203 }
3204
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003205 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003206 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003207 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003208 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003209 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003210 }
3211}
Jiyong Park58e364a2019-01-19 19:24:06 +09003212
Jooyung Hanf121a652019-12-17 14:30:11 +09003213func TestCertificate(t *testing.T) {
3214 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003215 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003216 apex {
3217 name: "myapex",
3218 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003219 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003220 }
3221 apex_key {
3222 name: "myapex.key",
3223 public_key: "testkey.avbpubkey",
3224 private_key: "testkey.pem",
3225 }`)
3226 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3227 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3228 if actual := rule.Args["certificates"]; actual != expected {
3229 t.Errorf("certificates should be %q, not %q", expected, actual)
3230 }
3231 })
3232 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003233 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003234 apex {
3235 name: "myapex_keytest",
3236 key: "myapex.key",
3237 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003238 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003239 }
3240 apex_key {
3241 name: "myapex.key",
3242 public_key: "testkey.avbpubkey",
3243 private_key: "testkey.pem",
3244 }
3245 android_app_certificate {
3246 name: "myapex.certificate.override",
3247 certificate: "testkey.override",
3248 }`)
3249 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3250 expected := "testkey.override.x509.pem testkey.override.pk8"
3251 if actual := rule.Args["certificates"]; actual != expected {
3252 t.Errorf("certificates should be %q, not %q", expected, actual)
3253 }
3254 })
3255 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003256 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003257 apex {
3258 name: "myapex",
3259 key: "myapex.key",
3260 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003261 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003262 }
3263 apex_key {
3264 name: "myapex.key",
3265 public_key: "testkey.avbpubkey",
3266 private_key: "testkey.pem",
3267 }
3268 android_app_certificate {
3269 name: "myapex.certificate",
3270 certificate: "testkey",
3271 }`)
3272 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3273 expected := "testkey.x509.pem testkey.pk8"
3274 if actual := rule.Args["certificates"]; actual != expected {
3275 t.Errorf("certificates should be %q, not %q", expected, actual)
3276 }
3277 })
3278 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003279 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003280 apex {
3281 name: "myapex_keytest",
3282 key: "myapex.key",
3283 file_contexts: ":myapex-file_contexts",
3284 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003285 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003286 }
3287 apex_key {
3288 name: "myapex.key",
3289 public_key: "testkey.avbpubkey",
3290 private_key: "testkey.pem",
3291 }
3292 android_app_certificate {
3293 name: "myapex.certificate.override",
3294 certificate: "testkey.override",
3295 }`)
3296 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3297 expected := "testkey.override.x509.pem testkey.override.pk8"
3298 if actual := rule.Args["certificates"]; actual != expected {
3299 t.Errorf("certificates should be %q, not %q", expected, actual)
3300 }
3301 })
3302 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003303 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003304 apex {
3305 name: "myapex",
3306 key: "myapex.key",
3307 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003308 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003309 }
3310 apex_key {
3311 name: "myapex.key",
3312 public_key: "testkey.avbpubkey",
3313 private_key: "testkey.pem",
3314 }`)
3315 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3316 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3317 if actual := rule.Args["certificates"]; actual != expected {
3318 t.Errorf("certificates should be %q, not %q", expected, actual)
3319 }
3320 })
3321 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003322 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003323 apex {
3324 name: "myapex_keytest",
3325 key: "myapex.key",
3326 file_contexts: ":myapex-file_contexts",
3327 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003328 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003329 }
3330 apex_key {
3331 name: "myapex.key",
3332 public_key: "testkey.avbpubkey",
3333 private_key: "testkey.pem",
3334 }
3335 android_app_certificate {
3336 name: "myapex.certificate.override",
3337 certificate: "testkey.override",
3338 }`)
3339 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3340 expected := "testkey.override.x509.pem testkey.override.pk8"
3341 if actual := rule.Args["certificates"]; actual != expected {
3342 t.Errorf("certificates should be %q, not %q", expected, actual)
3343 }
3344 })
3345}
3346
Jiyong Park58e364a2019-01-19 19:24:06 +09003347func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003348 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003349 apex {
3350 name: "myapex",
3351 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003352 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003353 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003354 }
3355
3356 apex {
3357 name: "otherapex",
3358 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003359 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003360 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003361 }
3362
3363 apex_key {
3364 name: "myapex.key",
3365 public_key: "testkey.avbpubkey",
3366 private_key: "testkey.pem",
3367 }
3368
3369 cc_library {
3370 name: "mylib",
3371 srcs: ["mylib.cpp"],
3372 system_shared_libs: [],
3373 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003374 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003375 "myapex",
3376 "otherapex",
3377 ],
Jooyung Han24282772020-03-21 23:20:55 +09003378 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003379 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003380 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003381 cc_library {
3382 name: "mylib2",
3383 srcs: ["mylib.cpp"],
3384 system_shared_libs: [],
3385 stl: "none",
3386 apex_available: [
3387 "myapex",
3388 "otherapex",
3389 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003390 static_libs: ["mylib3"],
3391 recovery_available: true,
3392 min_sdk_version: "29",
3393 }
3394 cc_library {
3395 name: "mylib3",
3396 srcs: ["mylib.cpp"],
3397 system_shared_libs: [],
3398 stl: "none",
3399 apex_available: [
3400 "myapex",
3401 "otherapex",
3402 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003403 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003404 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003405 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003406 `)
3407
Jooyung Hanc87a0592020-03-02 17:44:33 +09003408 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003409 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003410 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003411
Vinh Tranf9754732023-01-19 22:41:46 -05003412 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003413 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003414 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003415
Vinh Tranf9754732023-01-19 22:41:46 -05003416 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003417 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003418 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003419
Colin Crossaede88c2020-08-11 12:17:01 -07003420 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3421 // each variant defines additional macros to distinguish which apex variant it is built for
3422
3423 // non-APEX variant does not have __ANDROID_APEX__ defined
3424 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3425 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3426
Vinh Tranf9754732023-01-19 22:41:46 -05003427 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003428 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3429 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003430
Jooyung Hanc87a0592020-03-02 17:44:33 +09003431 // non-APEX variant does not have __ANDROID_APEX__ defined
3432 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3433 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3434
Vinh Tranf9754732023-01-19 22:41:46 -05003435 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003436 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003437 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003438}
Jiyong Park7e636d02019-01-28 16:16:54 +09003439
3440func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003441 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003442 apex {
3443 name: "myapex",
3444 key: "myapex.key",
3445 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003446 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003447 }
3448
3449 apex_key {
3450 name: "myapex.key",
3451 public_key: "testkey.avbpubkey",
3452 private_key: "testkey.pem",
3453 }
3454
3455 cc_library_headers {
3456 name: "mylib_headers",
3457 export_include_dirs: ["my_include"],
3458 system_shared_libs: [],
3459 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003460 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003461 }
3462
3463 cc_library {
3464 name: "mylib",
3465 srcs: ["mylib.cpp"],
3466 system_shared_libs: [],
3467 stl: "none",
3468 header_libs: ["mylib_headers"],
3469 export_header_lib_headers: ["mylib_headers"],
3470 stubs: {
3471 versions: ["1", "2", "3"],
3472 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003473 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003474 }
3475
3476 cc_library {
3477 name: "otherlib",
3478 srcs: ["mylib.cpp"],
3479 system_shared_libs: [],
3480 stl: "none",
3481 shared_libs: ["mylib"],
3482 }
3483 `)
3484
Colin Cross7113d202019-11-20 16:39:12 -08003485 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003486
3487 // Ensure that the include path of the header lib is exported to 'otherlib'
3488 ensureContains(t, cFlags, "-Imy_include")
3489}
Alex Light9670d332019-01-29 18:07:33 -08003490
Jiyong Park7cd10e32020-01-14 09:22:18 +09003491type fileInApex struct {
3492 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003493 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003494 isLink bool
3495}
3496
Jooyung Han1724d582022-12-21 10:17:44 +09003497func (f fileInApex) String() string {
3498 return f.src + ":" + f.path
3499}
3500
3501func (f fileInApex) match(expectation string) bool {
3502 parts := strings.Split(expectation, ":")
3503 if len(parts) == 1 {
3504 match, _ := path.Match(parts[0], f.path)
3505 return match
3506 }
3507 if len(parts) == 2 {
3508 matchSrc, _ := path.Match(parts[0], f.src)
3509 matchDst, _ := path.Match(parts[1], f.path)
3510 return matchSrc && matchDst
3511 }
3512 panic("invalid expected file specification: " + expectation)
3513}
3514
Jooyung Hana57af4a2020-01-23 05:36:59 +00003515func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003516 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003517 module := ctx.ModuleForTests(moduleName, variant)
3518 apexRule := module.MaybeRule("apexRule")
3519 apexDir := "/image.apex/"
3520 if apexRule.Rule == nil {
3521 apexRule = module.Rule("zipApexRule")
3522 apexDir = "/image.zipapex/"
3523 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003524 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003525 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003526 for _, cmd := range strings.Split(copyCmds, "&&") {
3527 cmd = strings.TrimSpace(cmd)
3528 if cmd == "" {
3529 continue
3530 }
3531 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003532 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003533 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003534 switch terms[0] {
3535 case "mkdir":
3536 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003537 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003538 t.Fatal("copyCmds contains invalid cp command", cmd)
3539 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003540 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003541 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003542 isLink = false
3543 case "ln":
3544 if len(terms) != 3 && len(terms) != 4 {
3545 // ln LINK TARGET or ln -s LINK TARGET
3546 t.Fatal("copyCmds contains invalid ln command", cmd)
3547 }
3548 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003549 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003550 isLink = true
3551 default:
3552 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3553 }
3554 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003555 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003556 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003557 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003558 }
Jooyung Han1724d582022-12-21 10:17:44 +09003559 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003560 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003561 }
3562 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003563 return ret
3564}
3565
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003566func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003567 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003568 var failed bool
3569 var surplus []string
3570 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003571 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003572 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003573 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003574 if file.match(expected) {
3575 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003576 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003577 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003578 }
3579 }
Jooyung Han1724d582022-12-21 10:17:44 +09003580 if !matchFound {
3581 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003582 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003583 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003584
Jooyung Han31c470b2019-10-18 16:26:59 +09003585 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003586 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003587 t.Log("surplus files", surplus)
3588 failed = true
3589 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003590
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003591 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003592 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003593 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003594 if !filesMatched[expected] {
3595 missing = append(missing, expected)
3596 }
3597 }
3598 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003599 t.Log("missing files", missing)
3600 failed = true
3601 }
3602 if failed {
3603 t.Fail()
3604 }
3605}
3606
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003607func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3608 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3609}
3610
3611func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
3612 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Rule("deapexer")
3613 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3614 if deapexer.Output != nil {
3615 outputs = append(outputs, deapexer.Output.String())
3616 }
3617 for _, output := range deapexer.ImplicitOutputs {
3618 outputs = append(outputs, output.String())
3619 }
3620 actualFiles := make([]fileInApex, 0, len(outputs))
3621 for _, output := range outputs {
3622 dir := "/deapexer/"
3623 pos := strings.LastIndex(output, dir)
3624 if pos == -1 {
3625 t.Fatal("Unknown deapexer output ", output)
3626 }
3627 path := output[pos+len(dir):]
3628 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3629 }
3630 assertFileListEquals(t, files, actualFiles)
3631}
3632
Jooyung Han344d5432019-08-23 11:17:39 +09003633func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003634 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003635 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003636 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003637 "etc/llndk.libraries.29.txt",
3638 "etc/vndkcore.libraries.29.txt",
3639 "etc/vndksp.libraries.29.txt",
3640 "etc/vndkprivate.libraries.29.txt",
3641 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003642 }
3643 testCases := []struct {
3644 vndkVersion string
3645 expectedFiles []string
3646 }{
3647 {
3648 vndkVersion: "current",
3649 expectedFiles: append(commonFiles,
3650 "lib/libvndk.so",
3651 "lib/libvndksp.so",
3652 "lib64/libvndk.so",
3653 "lib64/libvndksp.so"),
3654 },
3655 {
3656 vndkVersion: "",
3657 expectedFiles: append(commonFiles,
3658 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3659 "lib/libvndksp.so",
3660 "lib64/libvndksp.so"),
3661 },
3662 }
3663 for _, tc := range testCases {
3664 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3665 ctx := testApex(t, `
3666 apex_vndk {
3667 name: "com.android.vndk.current",
3668 key: "com.android.vndk.current.key",
3669 updatable: false,
3670 }
3671
3672 apex_key {
3673 name: "com.android.vndk.current.key",
3674 public_key: "testkey.avbpubkey",
3675 private_key: "testkey.pem",
3676 }
3677
3678 cc_library {
3679 name: "libvndk",
3680 srcs: ["mylib.cpp"],
3681 vendor_available: true,
3682 product_available: true,
3683 vndk: {
3684 enabled: true,
3685 },
3686 system_shared_libs: [],
3687 stl: "none",
3688 apex_available: [ "com.android.vndk.current" ],
3689 }
3690
3691 cc_library {
3692 name: "libvndksp",
3693 srcs: ["mylib.cpp"],
3694 vendor_available: true,
3695 product_available: true,
3696 vndk: {
3697 enabled: true,
3698 support_system_process: true,
3699 },
3700 system_shared_libs: [],
3701 stl: "none",
3702 apex_available: [ "com.android.vndk.current" ],
3703 }
3704
3705 // VNDK-Ext should not cause any problems
3706
3707 cc_library {
3708 name: "libvndk.ext",
3709 srcs: ["mylib2.cpp"],
3710 vendor: true,
3711 vndk: {
3712 enabled: true,
3713 extends: "libvndk",
3714 },
3715 system_shared_libs: [],
3716 stl: "none",
3717 }
3718
3719 cc_library {
3720 name: "libvndksp.ext",
3721 srcs: ["mylib2.cpp"],
3722 vendor: true,
3723 vndk: {
3724 enabled: true,
3725 support_system_process: true,
3726 extends: "libvndksp",
3727 },
3728 system_shared_libs: [],
3729 stl: "none",
3730 }
3731 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3732 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3733 }))
3734 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3735 })
3736 }
Jooyung Han344d5432019-08-23 11:17:39 +09003737}
3738
3739func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003740 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003741 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003742 name: "com.android.vndk.current",
3743 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003744 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003745 }
3746
3747 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003748 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003749 public_key: "testkey.avbpubkey",
3750 private_key: "testkey.pem",
3751 }
3752
3753 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003754 name: "libvndk",
3755 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003756 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003757 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003758 vndk: {
3759 enabled: true,
3760 },
3761 system_shared_libs: [],
3762 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003763 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003764 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003765
3766 cc_prebuilt_library_shared {
3767 name: "libvndk.arm",
3768 srcs: ["libvndk.arm.so"],
3769 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003770 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003771 vndk: {
3772 enabled: true,
3773 },
3774 enabled: false,
3775 arch: {
3776 arm: {
3777 enabled: true,
3778 },
3779 },
3780 system_shared_libs: [],
3781 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003782 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003783 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003784 `+vndkLibrariesTxtFiles("current"),
3785 withFiles(map[string][]byte{
3786 "libvndk.so": nil,
3787 "libvndk.arm.so": nil,
3788 }))
Colin Cross2807f002021-03-02 10:15:29 -08003789 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003790 "lib/libvndk.so",
3791 "lib/libvndk.arm.so",
3792 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003793 "lib/libc++.so",
3794 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003795 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003796 })
Jooyung Han344d5432019-08-23 11:17:39 +09003797}
3798
Jooyung Han39edb6c2019-11-06 16:53:07 +09003799func vndkLibrariesTxtFiles(vers ...string) (result string) {
3800 for _, v := range vers {
3801 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003802 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003803 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003804 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003805 name: "` + txt + `.libraries.txt",
3806 }
3807 `
3808 }
3809 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003810 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003811 result += `
3812 prebuilt_etc {
3813 name: "` + txt + `.libraries.` + v + `.txt",
3814 src: "dummy.txt",
3815 }
3816 `
3817 }
3818 }
3819 }
3820 return
3821}
3822
Jooyung Han344d5432019-08-23 11:17:39 +09003823func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003824 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003825 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003826 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003827 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003828 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003829 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003830 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003831 }
3832
3833 apex_key {
3834 name: "myapex.key",
3835 public_key: "testkey.avbpubkey",
3836 private_key: "testkey.pem",
3837 }
3838
Jooyung Han31c470b2019-10-18 16:26:59 +09003839 vndk_prebuilt_shared {
3840 name: "libvndk27",
3841 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003842 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003843 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003844 vndk: {
3845 enabled: true,
3846 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003847 target_arch: "arm64",
3848 arch: {
3849 arm: {
3850 srcs: ["libvndk27_arm.so"],
3851 },
3852 arm64: {
3853 srcs: ["libvndk27_arm64.so"],
3854 },
3855 },
Colin Cross2807f002021-03-02 10:15:29 -08003856 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003857 }
3858
3859 vndk_prebuilt_shared {
3860 name: "libvndk27",
3861 version: "27",
3862 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003863 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003864 vndk: {
3865 enabled: true,
3866 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003867 target_arch: "x86_64",
3868 arch: {
3869 x86: {
3870 srcs: ["libvndk27_x86.so"],
3871 },
3872 x86_64: {
3873 srcs: ["libvndk27_x86_64.so"],
3874 },
3875 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003876 }
3877 `+vndkLibrariesTxtFiles("27"),
3878 withFiles(map[string][]byte{
3879 "libvndk27_arm.so": nil,
3880 "libvndk27_arm64.so": nil,
3881 "libvndk27_x86.so": nil,
3882 "libvndk27_x86_64.so": nil,
3883 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003884
Colin Cross2807f002021-03-02 10:15:29 -08003885 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003886 "lib/libvndk27_arm.so",
3887 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003888 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003889 })
Jooyung Han344d5432019-08-23 11:17:39 +09003890}
3891
Jooyung Han90eee022019-10-01 20:02:42 +09003892func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003893 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003894 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003895 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003896 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003897 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003898 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003899 }
3900 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003901 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003902 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003903 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003904 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003905 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003906 }
3907 apex_key {
3908 name: "myapex.key",
3909 public_key: "testkey.avbpubkey",
3910 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003911 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003912
3913 assertApexName := func(expected, moduleName string) {
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003914 module := ctx.ModuleForTests(moduleName, "android_common_image")
3915 apexManifestRule := module.Rule("apexManifestRule")
3916 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003917 }
3918
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003919 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003920 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003921}
3922
Jooyung Han344d5432019-08-23 11:17:39 +09003923func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003924 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003925 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003926 name: "com.android.vndk.current",
3927 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003928 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003929 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003930 }
3931
3932 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003933 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003934 public_key: "testkey.avbpubkey",
3935 private_key: "testkey.pem",
3936 }
3937
3938 cc_library {
3939 name: "libvndk",
3940 srcs: ["mylib.cpp"],
3941 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003942 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003943 native_bridge_supported: true,
3944 host_supported: true,
3945 vndk: {
3946 enabled: true,
3947 },
3948 system_shared_libs: [],
3949 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003950 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003951 }
Colin Cross2807f002021-03-02 10:15:29 -08003952 `+vndkLibrariesTxtFiles("current"),
3953 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003954
Colin Cross2807f002021-03-02 10:15:29 -08003955 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003956 "lib/libvndk.so",
3957 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003958 "lib/libc++.so",
3959 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003960 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003961 })
Jooyung Han344d5432019-08-23 11:17:39 +09003962}
3963
3964func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003965 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003966 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003967 name: "com.android.vndk.current",
3968 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003969 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003970 native_bridge_supported: true,
3971 }
3972
3973 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003974 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003975 public_key: "testkey.avbpubkey",
3976 private_key: "testkey.pem",
3977 }
3978
3979 cc_library {
3980 name: "libvndk",
3981 srcs: ["mylib.cpp"],
3982 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003983 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003984 native_bridge_supported: true,
3985 host_supported: true,
3986 vndk: {
3987 enabled: true,
3988 },
3989 system_shared_libs: [],
3990 stl: "none",
3991 }
3992 `)
3993}
3994
Jooyung Han31c470b2019-10-18 16:26:59 +09003995func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003996 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003997 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003998 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003999 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004000 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09004001 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004002 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09004003 }
4004
4005 apex_key {
4006 name: "myapex.key",
4007 public_key: "testkey.avbpubkey",
4008 private_key: "testkey.pem",
4009 }
4010
4011 vndk_prebuilt_shared {
4012 name: "libvndk27",
4013 version: "27",
4014 target_arch: "arm",
4015 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004016 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004017 vndk: {
4018 enabled: true,
4019 },
4020 arch: {
4021 arm: {
4022 srcs: ["libvndk27.so"],
4023 }
4024 },
4025 }
4026
4027 vndk_prebuilt_shared {
4028 name: "libvndk27",
4029 version: "27",
4030 target_arch: "arm",
4031 binder32bit: true,
4032 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004033 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004034 vndk: {
4035 enabled: true,
4036 },
4037 arch: {
4038 arm: {
4039 srcs: ["libvndk27binder32.so"],
4040 }
4041 },
Colin Cross2807f002021-03-02 10:15:29 -08004042 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09004043 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09004044 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09004045 withFiles(map[string][]byte{
4046 "libvndk27.so": nil,
4047 "libvndk27binder32.so": nil,
4048 }),
4049 withBinder32bit,
4050 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07004051 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09004052 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
4053 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09004054 },
4055 }),
4056 )
4057
Colin Cross2807f002021-03-02 10:15:29 -08004058 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09004059 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004060 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09004061 })
4062}
4063
Jooyung Han45a96772020-06-15 14:59:42 +09004064func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004065 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09004066 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004067 name: "com.android.vndk.current",
4068 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004069 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004070 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09004071 }
4072
4073 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08004074 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004075 public_key: "testkey.avbpubkey",
4076 private_key: "testkey.pem",
4077 }
4078
4079 cc_library {
4080 name: "libz",
4081 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004082 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09004083 vndk: {
4084 enabled: true,
4085 },
4086 stubs: {
4087 symbol_file: "libz.map.txt",
4088 versions: ["30"],
4089 }
4090 }
4091 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
4092 "libz.map.txt": nil,
4093 }))
4094
Colin Cross2807f002021-03-02 10:15:29 -08004095 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09004096 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
4097 ensureListEmpty(t, provideNativeLibs)
Jooyung Han1724d582022-12-21 10:17:44 +09004098 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
4099 "out/soong/.intermediates/libz/android_vendor.29_arm64_armv8-a_shared/libz.so:lib64/libz.so",
4100 "out/soong/.intermediates/libz/android_vendor.29_arm_armv7-a-neon_shared/libz.so:lib/libz.so",
4101 "*/*",
4102 })
Jooyung Han45a96772020-06-15 14:59:42 +09004103}
4104
Jooyung Hane1633032019-08-01 17:41:43 +09004105func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004106 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09004107 apex {
4108 name: "myapex_nodep",
4109 key: "myapex.key",
4110 native_shared_libs: ["lib_nodep"],
4111 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004112 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004113 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004114 }
4115
4116 apex {
4117 name: "myapex_dep",
4118 key: "myapex.key",
4119 native_shared_libs: ["lib_dep"],
4120 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004121 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004122 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004123 }
4124
4125 apex {
4126 name: "myapex_provider",
4127 key: "myapex.key",
4128 native_shared_libs: ["libfoo"],
4129 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004130 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004131 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004132 }
4133
4134 apex {
4135 name: "myapex_selfcontained",
4136 key: "myapex.key",
4137 native_shared_libs: ["lib_dep", "libfoo"],
4138 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004139 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004140 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004141 }
4142
4143 apex_key {
4144 name: "myapex.key",
4145 public_key: "testkey.avbpubkey",
4146 private_key: "testkey.pem",
4147 }
4148
4149 cc_library {
4150 name: "lib_nodep",
4151 srcs: ["mylib.cpp"],
4152 system_shared_libs: [],
4153 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004154 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004155 }
4156
4157 cc_library {
4158 name: "lib_dep",
4159 srcs: ["mylib.cpp"],
4160 shared_libs: ["libfoo"],
4161 system_shared_libs: [],
4162 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004163 apex_available: [
4164 "myapex_dep",
4165 "myapex_provider",
4166 "myapex_selfcontained",
4167 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004168 }
4169
4170 cc_library {
4171 name: "libfoo",
4172 srcs: ["mytest.cpp"],
4173 stubs: {
4174 versions: ["1"],
4175 },
4176 system_shared_libs: [],
4177 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004178 apex_available: [
4179 "myapex_provider",
4180 "myapex_selfcontained",
4181 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004182 }
4183 `)
4184
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004185 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004186 var provideNativeLibs, requireNativeLibs []string
4187
Sundong Ahnabb64432019-10-22 13:58:29 +09004188 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004189 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4190 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004191 ensureListEmpty(t, provideNativeLibs)
4192 ensureListEmpty(t, requireNativeLibs)
4193
Sundong Ahnabb64432019-10-22 13:58:29 +09004194 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004195 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4196 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004197 ensureListEmpty(t, provideNativeLibs)
4198 ensureListContains(t, requireNativeLibs, "libfoo.so")
4199
Sundong Ahnabb64432019-10-22 13:58:29 +09004200 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004201 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4202 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004203 ensureListContains(t, provideNativeLibs, "libfoo.so")
4204 ensureListEmpty(t, requireNativeLibs)
4205
Sundong Ahnabb64432019-10-22 13:58:29 +09004206 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004207 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4208 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004209 ensureListContains(t, provideNativeLibs, "libfoo.so")
4210 ensureListEmpty(t, requireNativeLibs)
4211}
4212
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004213func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4214 ctx := testApex(t, `
4215 apex {
4216 name: "myapex",
4217 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004218 native_shared_libs: ["mylib"],
4219 updatable: false,
4220 }
4221
4222 apex_key {
4223 name: "myapex.key",
4224 public_key: "testkey.avbpubkey",
4225 private_key: "testkey.pem",
4226 }
4227
4228 cc_library {
4229 name: "mylib",
4230 srcs: ["mylib.cpp"],
4231 system_shared_libs: [],
4232 stl: "none",
4233 apex_available: [
4234 "//apex_available:platform",
4235 "myapex",
4236 ],
4237 }
4238 `, android.FixtureMergeEnv(map[string]string{
4239 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4240 }))
4241
Jooyung Han63dff462023-02-09 00:11:27 +00004242 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004243 apexManifestRule := module.Rule("apexManifestRule")
4244 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4245}
4246
Vinh Tran8f5310f2022-10-07 18:16:47 -04004247func TestCompileMultilibProp(t *testing.T) {
4248 testCases := []struct {
4249 compileMultiLibProp string
4250 containedLibs []string
4251 notContainedLibs []string
4252 }{
4253 {
4254 containedLibs: []string{
4255 "image.apex/lib64/mylib.so",
4256 "image.apex/lib/mylib.so",
4257 },
4258 compileMultiLibProp: `compile_multilib: "both",`,
4259 },
4260 {
4261 containedLibs: []string{"image.apex/lib64/mylib.so"},
4262 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4263 compileMultiLibProp: `compile_multilib: "first",`,
4264 },
4265 {
4266 containedLibs: []string{"image.apex/lib64/mylib.so"},
4267 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4268 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4269 },
4270 {
4271 containedLibs: []string{"image.apex/lib64/mylib.so"},
4272 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4273 compileMultiLibProp: `compile_multilib: "64",`,
4274 },
4275 {
4276 containedLibs: []string{"image.apex/lib/mylib.so"},
4277 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4278 compileMultiLibProp: `compile_multilib: "32",`,
4279 },
4280 }
4281 for _, testCase := range testCases {
4282 ctx := testApex(t, fmt.Sprintf(`
4283 apex {
4284 name: "myapex",
4285 key: "myapex.key",
4286 %s
4287 native_shared_libs: ["mylib"],
4288 updatable: false,
4289 }
4290 apex_key {
4291 name: "myapex.key",
4292 public_key: "testkey.avbpubkey",
4293 private_key: "testkey.pem",
4294 }
4295 cc_library {
4296 name: "mylib",
4297 srcs: ["mylib.cpp"],
4298 apex_available: [
4299 "//apex_available:platform",
4300 "myapex",
4301 ],
4302 }
4303 `, testCase.compileMultiLibProp),
4304 )
4305 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4306 apexRule := module.Rule("apexRule")
4307 copyCmds := apexRule.Args["copy_commands"]
4308 for _, containedLib := range testCase.containedLibs {
4309 ensureContains(t, copyCmds, containedLib)
4310 }
4311 for _, notContainedLib := range testCase.notContainedLibs {
4312 ensureNotContains(t, copyCmds, notContainedLib)
4313 }
4314 }
4315}
4316
Alex Light0851b882019-02-07 13:20:53 -08004317func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004318 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004319 apex {
4320 name: "myapex",
4321 key: "myapex.key",
4322 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004323 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004324 }
4325
4326 apex_key {
4327 name: "myapex.key",
4328 public_key: "testkey.avbpubkey",
4329 private_key: "testkey.pem",
4330 }
4331
4332 cc_library {
4333 name: "mylib_common",
4334 srcs: ["mylib.cpp"],
4335 system_shared_libs: [],
4336 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004337 apex_available: [
4338 "//apex_available:platform",
4339 "myapex",
4340 ],
Alex Light0851b882019-02-07 13:20:53 -08004341 }
4342 `)
4343
Sundong Ahnabb64432019-10-22 13:58:29 +09004344 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004345 apexRule := module.Rule("apexRule")
4346 copyCmds := apexRule.Args["copy_commands"]
4347
4348 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4349 t.Log("Apex was a test apex!")
4350 t.Fail()
4351 }
4352 // Ensure that main rule creates an output
4353 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4354
4355 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004356 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004357
4358 // Ensure that both direct and indirect deps are copied into apex
4359 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4360
Colin Cross7113d202019-11-20 16:39:12 -08004361 // Ensure that the platform variant ends with _shared
4362 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004363
Colin Cross56a83212020-09-15 18:30:11 -07004364 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004365 t.Log("Found mylib_common not in any apex!")
4366 t.Fail()
4367 }
4368}
4369
4370func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004371 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004372 apex_test {
4373 name: "myapex",
4374 key: "myapex.key",
4375 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004376 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004377 }
4378
4379 apex_key {
4380 name: "myapex.key",
4381 public_key: "testkey.avbpubkey",
4382 private_key: "testkey.pem",
4383 }
4384
4385 cc_library {
4386 name: "mylib_common_test",
4387 srcs: ["mylib.cpp"],
4388 system_shared_libs: [],
4389 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004390 // TODO: remove //apex_available:platform
4391 apex_available: [
4392 "//apex_available:platform",
4393 "myapex",
4394 ],
Alex Light0851b882019-02-07 13:20:53 -08004395 }
4396 `)
4397
Sundong Ahnabb64432019-10-22 13:58:29 +09004398 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004399 apexRule := module.Rule("apexRule")
4400 copyCmds := apexRule.Args["copy_commands"]
4401
4402 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4403 t.Log("Apex was not a test apex!")
4404 t.Fail()
4405 }
4406 // Ensure that main rule creates an output
4407 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4408
4409 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004410 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004411
4412 // Ensure that both direct and indirect deps are copied into apex
4413 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4414
Colin Cross7113d202019-11-20 16:39:12 -08004415 // Ensure that the platform variant ends with _shared
4416 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004417}
4418
Alex Light9670d332019-01-29 18:07:33 -08004419func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004420 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004421 apex {
4422 name: "myapex",
4423 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004424 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004425 multilib: {
4426 first: {
4427 native_shared_libs: ["mylib_common"],
4428 }
4429 },
4430 target: {
4431 android: {
4432 multilib: {
4433 first: {
4434 native_shared_libs: ["mylib"],
4435 }
4436 }
4437 },
4438 host: {
4439 multilib: {
4440 first: {
4441 native_shared_libs: ["mylib2"],
4442 }
4443 }
4444 }
4445 }
4446 }
4447
4448 apex_key {
4449 name: "myapex.key",
4450 public_key: "testkey.avbpubkey",
4451 private_key: "testkey.pem",
4452 }
4453
4454 cc_library {
4455 name: "mylib",
4456 srcs: ["mylib.cpp"],
4457 system_shared_libs: [],
4458 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004459 // TODO: remove //apex_available:platform
4460 apex_available: [
4461 "//apex_available:platform",
4462 "myapex",
4463 ],
Alex Light9670d332019-01-29 18:07:33 -08004464 }
4465
4466 cc_library {
4467 name: "mylib_common",
4468 srcs: ["mylib.cpp"],
4469 system_shared_libs: [],
4470 stl: "none",
4471 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004472 // TODO: remove //apex_available:platform
4473 apex_available: [
4474 "//apex_available:platform",
4475 "myapex",
4476 ],
Alex Light9670d332019-01-29 18:07:33 -08004477 }
4478
4479 cc_library {
4480 name: "mylib2",
4481 srcs: ["mylib.cpp"],
4482 system_shared_libs: [],
4483 stl: "none",
4484 compile_multilib: "first",
4485 }
4486 `)
4487
Sundong Ahnabb64432019-10-22 13:58:29 +09004488 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004489 copyCmds := apexRule.Args["copy_commands"]
4490
4491 // Ensure that main rule creates an output
4492 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4493
4494 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004495 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4496 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4497 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004498
4499 // Ensure that both direct and indirect deps are copied into apex
4500 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4501 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4502 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4503
Colin Cross7113d202019-11-20 16:39:12 -08004504 // Ensure that the platform variant ends with _shared
4505 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4506 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4507 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004508}
Jiyong Park04480cf2019-02-06 00:16:29 +09004509
Jiyong Park59140302020-12-14 18:44:04 +09004510func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004511 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004512 apex {
4513 name: "myapex",
4514 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004515 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004516 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004517 arch: {
4518 arm64: {
4519 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004520 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004521 },
4522 x86_64: {
4523 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004524 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004525 },
4526 }
4527 }
4528
4529 apex_key {
4530 name: "myapex.key",
4531 public_key: "testkey.avbpubkey",
4532 private_key: "testkey.pem",
4533 }
4534
4535 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004536 name: "mylib.generic",
4537 srcs: ["mylib.cpp"],
4538 system_shared_libs: [],
4539 stl: "none",
4540 // TODO: remove //apex_available:platform
4541 apex_available: [
4542 "//apex_available:platform",
4543 "myapex",
4544 ],
4545 }
4546
4547 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004548 name: "mylib.arm64",
4549 srcs: ["mylib.cpp"],
4550 system_shared_libs: [],
4551 stl: "none",
4552 // TODO: remove //apex_available:platform
4553 apex_available: [
4554 "//apex_available:platform",
4555 "myapex",
4556 ],
4557 }
4558
4559 cc_library {
4560 name: "mylib.x64",
4561 srcs: ["mylib.cpp"],
4562 system_shared_libs: [],
4563 stl: "none",
4564 // TODO: remove //apex_available:platform
4565 apex_available: [
4566 "//apex_available:platform",
4567 "myapex",
4568 ],
4569 }
4570 `)
4571
4572 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4573 copyCmds := apexRule.Args["copy_commands"]
4574
4575 // Ensure that apex variant is created for the direct dep
4576 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004577 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004578 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4579
4580 // Ensure that both direct and indirect deps are copied into apex
4581 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4582 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4583}
4584
Jiyong Park04480cf2019-02-06 00:16:29 +09004585func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004586 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004587 apex {
4588 name: "myapex",
4589 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004590 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004591 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004592 }
4593
4594 apex_key {
4595 name: "myapex.key",
4596 public_key: "testkey.avbpubkey",
4597 private_key: "testkey.pem",
4598 }
4599
4600 sh_binary {
4601 name: "myscript",
4602 src: "mylib.cpp",
4603 filename: "myscript.sh",
4604 sub_dir: "script",
4605 }
4606 `)
4607
Sundong Ahnabb64432019-10-22 13:58:29 +09004608 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004609 copyCmds := apexRule.Args["copy_commands"]
4610
4611 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4612}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004613
Jooyung Han91df2082019-11-20 01:49:42 +09004614func TestApexInVariousPartition(t *testing.T) {
4615 testcases := []struct {
4616 propName, parition, flattenedPartition string
4617 }{
4618 {"", "system", "system_ext"},
4619 {"product_specific: true", "product", "product"},
4620 {"soc_specific: true", "vendor", "vendor"},
4621 {"proprietary: true", "vendor", "vendor"},
4622 {"vendor: true", "vendor", "vendor"},
4623 {"system_ext_specific: true", "system_ext", "system_ext"},
4624 }
4625 for _, tc := range testcases {
4626 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004627 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004628 apex {
4629 name: "myapex",
4630 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004631 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004632 `+tc.propName+`
4633 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004634
Jooyung Han91df2082019-11-20 01:49:42 +09004635 apex_key {
4636 name: "myapex.key",
4637 public_key: "testkey.avbpubkey",
4638 private_key: "testkey.pem",
4639 }
4640 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004641
Jooyung Han91df2082019-11-20 01:49:42 +09004642 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004643 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4644 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004645 if actual != expected {
4646 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4647 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004648
Jooyung Han91df2082019-11-20 01:49:42 +09004649 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004650 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4651 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004652 if actual != expected {
4653 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4654 }
4655 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004656 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004657}
Jiyong Park67882562019-03-21 01:11:21 +09004658
Jooyung Han580eb4f2020-06-24 19:33:06 +09004659func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004660 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004661 apex {
4662 name: "myapex",
4663 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004664 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004665 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004666
Jooyung Han580eb4f2020-06-24 19:33:06 +09004667 apex_key {
4668 name: "myapex.key",
4669 public_key: "testkey.avbpubkey",
4670 private_key: "testkey.pem",
4671 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004672 `)
4673 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004674 rule := module.Output("file_contexts")
4675 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4676}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004677
Jooyung Han580eb4f2020-06-24 19:33:06 +09004678func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004679 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004680 apex {
4681 name: "myapex",
4682 key: "myapex.key",
4683 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004684 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004685 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004686
Jooyung Han580eb4f2020-06-24 19:33:06 +09004687 apex_key {
4688 name: "myapex.key",
4689 public_key: "testkey.avbpubkey",
4690 private_key: "testkey.pem",
4691 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004692 `, withFiles(map[string][]byte{
4693 "my_own_file_contexts": nil,
4694 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004695}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004696
Jooyung Han580eb4f2020-06-24 19:33:06 +09004697func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004698 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004699 apex {
4700 name: "myapex",
4701 key: "myapex.key",
4702 product_specific: true,
4703 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004704 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004705 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004706
Jooyung Han580eb4f2020-06-24 19:33:06 +09004707 apex_key {
4708 name: "myapex.key",
4709 public_key: "testkey.avbpubkey",
4710 private_key: "testkey.pem",
4711 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004712 `)
4713
Colin Cross1c460562021-02-16 17:55:47 -08004714 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004715 apex {
4716 name: "myapex",
4717 key: "myapex.key",
4718 product_specific: true,
4719 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004720 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004721 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004722
Jooyung Han580eb4f2020-06-24 19:33:06 +09004723 apex_key {
4724 name: "myapex.key",
4725 public_key: "testkey.avbpubkey",
4726 private_key: "testkey.pem",
4727 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004728 `, withFiles(map[string][]byte{
4729 "product_specific_file_contexts": nil,
4730 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004731 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4732 rule := module.Output("file_contexts")
4733 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4734}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004735
Jooyung Han580eb4f2020-06-24 19:33:06 +09004736func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004737 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004738 apex {
4739 name: "myapex",
4740 key: "myapex.key",
4741 product_specific: true,
4742 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004743 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004744 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004745
Jooyung Han580eb4f2020-06-24 19:33:06 +09004746 apex_key {
4747 name: "myapex.key",
4748 public_key: "testkey.avbpubkey",
4749 private_key: "testkey.pem",
4750 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004751
Jooyung Han580eb4f2020-06-24 19:33:06 +09004752 filegroup {
4753 name: "my-file-contexts",
4754 srcs: ["product_specific_file_contexts"],
4755 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004756 `, withFiles(map[string][]byte{
4757 "product_specific_file_contexts": nil,
4758 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004759 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4760 rule := module.Output("file_contexts")
4761 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004762}
4763
Jiyong Park67882562019-03-21 01:11:21 +09004764func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004765 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004766 apex_key {
4767 name: "myapex.key",
4768 public_key: ":my.avbpubkey",
4769 private_key: ":my.pem",
4770 product_specific: true,
4771 }
4772
4773 filegroup {
4774 name: "my.avbpubkey",
4775 srcs: ["testkey2.avbpubkey"],
4776 }
4777
4778 filegroup {
4779 name: "my.pem",
4780 srcs: ["testkey2.pem"],
4781 }
4782 `)
4783
4784 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4785 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004786 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004787 if actual_pubkey != expected_pubkey {
4788 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4789 }
4790 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004791 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004792 if actual_privkey != expected_privkey {
4793 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4794 }
4795}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004796
4797func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004798 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004799 prebuilt_apex {
4800 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004801 arch: {
4802 arm64: {
4803 src: "myapex-arm64.apex",
4804 },
4805 arm: {
4806 src: "myapex-arm.apex",
4807 },
4808 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004809 }
4810 `)
4811
Wei Li340ee8e2022-03-18 17:33:24 -07004812 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4813 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004814
Jiyong Parkc95714e2019-03-29 14:23:10 +09004815 expectedInput := "myapex-arm64.apex"
4816 if prebuilt.inputApex.String() != expectedInput {
4817 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4818 }
Wei Li340ee8e2022-03-18 17:33:24 -07004819 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4820 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4821 rule := testingModule.Rule("genProvenanceMetaData")
4822 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4823 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4824 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4825 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08004826
4827 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
4828 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004829}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004830
Paul Duffinc0609c62021-03-01 17:27:16 +00004831func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004832 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004833 prebuilt_apex {
4834 name: "myapex",
4835 }
4836 `)
4837}
4838
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004839func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004840 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004841 prebuilt_apex {
4842 name: "myapex",
4843 src: "myapex-arm.apex",
4844 filename: "notmyapex.apex",
4845 }
4846 `)
4847
Wei Li340ee8e2022-03-18 17:33:24 -07004848 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4849 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004850
4851 expected := "notmyapex.apex"
4852 if p.installFilename != expected {
4853 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4854 }
Wei Li340ee8e2022-03-18 17:33:24 -07004855 rule := testingModule.Rule("genProvenanceMetaData")
4856 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4857 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4858 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4859 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004860}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004861
Samiul Islam7c02e262021-09-08 17:48:28 +01004862func TestApexSetFilenameOverride(t *testing.T) {
4863 testApex(t, `
4864 apex_set {
4865 name: "com.company.android.myapex",
4866 apex_name: "com.android.myapex",
4867 set: "company-myapex.apks",
4868 filename: "com.company.android.myapex.apex"
4869 }
4870 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4871
4872 testApex(t, `
4873 apex_set {
4874 name: "com.company.android.myapex",
4875 apex_name: "com.android.myapex",
4876 set: "company-myapex.apks",
4877 filename: "com.company.android.myapex.capex"
4878 }
4879 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4880
4881 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4882 apex_set {
4883 name: "com.company.android.myapex",
4884 apex_name: "com.android.myapex",
4885 set: "company-myapex.apks",
4886 filename: "some-random-suffix"
4887 }
4888 `)
4889}
4890
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004891func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004892 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004893 prebuilt_apex {
4894 name: "myapex.prebuilt",
4895 src: "myapex-arm.apex",
4896 overrides: [
4897 "myapex",
4898 ],
4899 }
4900 `)
4901
Wei Li340ee8e2022-03-18 17:33:24 -07004902 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4903 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004904
4905 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004906 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004907 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004908 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004909 }
Wei Li340ee8e2022-03-18 17:33:24 -07004910 rule := testingModule.Rule("genProvenanceMetaData")
4911 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4912 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4913 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4914 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004915}
4916
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004917func TestPrebuiltApexName(t *testing.T) {
4918 testApex(t, `
4919 prebuilt_apex {
4920 name: "com.company.android.myapex",
4921 apex_name: "com.android.myapex",
4922 src: "company-myapex-arm.apex",
4923 }
4924 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4925
4926 testApex(t, `
4927 apex_set {
4928 name: "com.company.android.myapex",
4929 apex_name: "com.android.myapex",
4930 set: "company-myapex.apks",
4931 }
4932 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4933}
4934
4935func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4936 _ = android.GroupFixturePreparers(
4937 java.PrepareForTestWithJavaDefaultModules,
4938 PrepareForTestWithApexBuildComponents,
4939 android.FixtureWithRootAndroidBp(`
4940 platform_bootclasspath {
4941 name: "platform-bootclasspath",
4942 fragments: [
4943 {
4944 apex: "com.android.art",
4945 module: "art-bootclasspath-fragment",
4946 },
4947 ],
4948 }
4949
4950 prebuilt_apex {
4951 name: "com.company.android.art",
4952 apex_name: "com.android.art",
4953 src: "com.company.android.art-arm.apex",
4954 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4955 }
4956
4957 prebuilt_bootclasspath_fragment {
4958 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004959 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004960 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004961 hidden_api: {
4962 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4963 metadata: "my-bootclasspath-fragment/metadata.csv",
4964 index: "my-bootclasspath-fragment/index.csv",
4965 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4966 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4967 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004968 }
4969
4970 java_import {
4971 name: "core-oj",
4972 jars: ["prebuilt.jar"],
4973 }
4974 `),
4975 ).RunTest(t)
4976}
4977
Paul Duffin092153d2021-01-26 11:42:39 +00004978// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4979// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004980func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004981 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004982
Paul Duffin89886cb2021-02-05 16:44:03 +00004983 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004984 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004985 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004986 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004987 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004988 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004989 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4990 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4991 android.NormalizePathForTesting(dexJarBuildPath))
4992 }
4993
4994 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004995 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004996 // Make sure the import has been given the correct path to the dex jar.
4997 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4998 dexJarBuildPath := p.DexJarInstallPath()
4999 stem := android.RemoveOptionalPrebuiltPrefix(name)
5000 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
5001 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
5002 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00005003 }
5004
Paul Duffin39853512021-02-26 11:09:39 +00005005 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01005006 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00005007 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09005008 android.AssertArrayString(t, "Check if there is no source variant",
5009 []string{"android_common"},
5010 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00005011 }
5012
5013 t.Run("prebuilt only", func(t *testing.T) {
5014 bp := `
5015 prebuilt_apex {
5016 name: "myapex",
5017 arch: {
5018 arm64: {
5019 src: "myapex-arm64.apex",
5020 },
5021 arm: {
5022 src: "myapex-arm.apex",
5023 },
5024 },
Paul Duffin39853512021-02-26 11:09:39 +00005025 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005026 }
5027
5028 java_import {
5029 name: "libfoo",
5030 jars: ["libfoo.jar"],
5031 }
Paul Duffin39853512021-02-26 11:09:39 +00005032
5033 java_sdk_library_import {
5034 name: "libbar",
5035 public: {
5036 jars: ["libbar.jar"],
5037 },
5038 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005039 `
5040
5041 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5042 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5043
Martin Stjernholm44825602021-09-17 01:44:12 +01005044 deapexerName := deapexerModuleName("myapex")
5045 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
5046
Paul Duffinf6932af2021-02-26 18:21:56 +00005047 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01005048 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00005049 rule := deapexer.Rule("deapexer")
5050 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
5051 t.Errorf("expected: %q, found: %q", expected, actual)
5052 }
5053
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005054 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01005055 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005056 rule = prebuiltApex.Rule("android/soong/android.Cp")
5057 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
5058 t.Errorf("expected: %q, found: %q", expected, actual)
5059 }
5060
Paul Duffin89886cb2021-02-05 16:44:03 +00005061 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005062 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005063
5064 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005065 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005066 })
5067
5068 t.Run("prebuilt with source preferred", func(t *testing.T) {
5069
5070 bp := `
5071 prebuilt_apex {
5072 name: "myapex",
5073 arch: {
5074 arm64: {
5075 src: "myapex-arm64.apex",
5076 },
5077 arm: {
5078 src: "myapex-arm.apex",
5079 },
5080 },
Paul Duffin39853512021-02-26 11:09:39 +00005081 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005082 }
5083
5084 java_import {
5085 name: "libfoo",
5086 jars: ["libfoo.jar"],
5087 }
5088
5089 java_library {
5090 name: "libfoo",
5091 }
Paul Duffin39853512021-02-26 11:09:39 +00005092
5093 java_sdk_library_import {
5094 name: "libbar",
5095 public: {
5096 jars: ["libbar.jar"],
5097 },
5098 }
5099
5100 java_sdk_library {
5101 name: "libbar",
5102 srcs: ["foo/bar/MyClass.java"],
5103 unsafe_ignore_missing_latest_api: true,
5104 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005105 `
5106
5107 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5108 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5109
Paul Duffin89886cb2021-02-05 16:44:03 +00005110 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005111 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005112 ensureNoSourceVariant(t, ctx, "libfoo")
5113
5114 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005115 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005116 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005117 })
5118
5119 t.Run("prebuilt preferred with source", func(t *testing.T) {
5120 bp := `
5121 prebuilt_apex {
5122 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005123 arch: {
5124 arm64: {
5125 src: "myapex-arm64.apex",
5126 },
5127 arm: {
5128 src: "myapex-arm.apex",
5129 },
5130 },
Paul Duffin39853512021-02-26 11:09:39 +00005131 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005132 }
5133
5134 java_import {
5135 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005136 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005137 jars: ["libfoo.jar"],
5138 }
5139
5140 java_library {
5141 name: "libfoo",
5142 }
Paul Duffin39853512021-02-26 11:09:39 +00005143
5144 java_sdk_library_import {
5145 name: "libbar",
5146 prefer: true,
5147 public: {
5148 jars: ["libbar.jar"],
5149 },
5150 }
5151
5152 java_sdk_library {
5153 name: "libbar",
5154 srcs: ["foo/bar/MyClass.java"],
5155 unsafe_ignore_missing_latest_api: true,
5156 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005157 `
5158
5159 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5160 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5161
Paul Duffin89886cb2021-02-05 16:44:03 +00005162 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005163 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005164 ensureNoSourceVariant(t, ctx, "libfoo")
5165
5166 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005167 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005168 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005169 })
5170}
5171
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005172func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005173 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005174 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005175 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5176 // is disabled.
5177 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
5178 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005179
Paul Duffin37856732021-02-26 14:24:15 +00005180 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5181 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01005182 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005183 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005184 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005185 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005186 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005187 foundLibfooJar = true
5188 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005189 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005190 }
5191 }
5192 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005193 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 +00005194 }
5195 }
5196
Paul Duffin40a3f652021-07-19 13:11:24 +01005197 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005198 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005199 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005200 var rule android.TestingBuildParams
5201
5202 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5203 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005204 }
5205
Paul Duffin40a3f652021-07-19 13:11:24 +01005206 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5207 t.Helper()
5208 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5209 var rule android.TestingBuildParams
5210
5211 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5212 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5213 }
5214
Paul Duffin89f570a2021-06-16 01:42:33 +01005215 fragment := java.ApexVariantReference{
5216 Apex: proptools.StringPtr("myapex"),
5217 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5218 }
5219
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005220 t.Run("prebuilt only", func(t *testing.T) {
5221 bp := `
5222 prebuilt_apex {
5223 name: "myapex",
5224 arch: {
5225 arm64: {
5226 src: "myapex-arm64.apex",
5227 },
5228 arm: {
5229 src: "myapex-arm.apex",
5230 },
5231 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005232 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5233 }
5234
5235 prebuilt_bootclasspath_fragment {
5236 name: "my-bootclasspath-fragment",
5237 contents: ["libfoo", "libbar"],
5238 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005239 hidden_api: {
5240 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5241 metadata: "my-bootclasspath-fragment/metadata.csv",
5242 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005243 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5244 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5245 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005246 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005247 }
5248
5249 java_import {
5250 name: "libfoo",
5251 jars: ["libfoo.jar"],
5252 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005253 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005254 }
Paul Duffin37856732021-02-26 14:24:15 +00005255
5256 java_sdk_library_import {
5257 name: "libbar",
5258 public: {
5259 jars: ["libbar.jar"],
5260 },
5261 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005262 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005263 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005264 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005265 `
5266
Paul Duffin89f570a2021-06-16 01:42:33 +01005267 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005268 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5269 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005270
Paul Duffin537ea3d2021-05-14 10:38:00 +01005271 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005272 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005273 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005274 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005275 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5276 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005277 })
5278
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005279 t.Run("apex_set only", func(t *testing.T) {
5280 bp := `
5281 apex_set {
5282 name: "myapex",
5283 set: "myapex.apks",
Liz Kammer2dc72442023-04-20 10:10:48 -04005284 exported_java_libs: ["myjavalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005285 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
Liz Kammer2dc72442023-04-20 10:10:48 -04005286 exported_systemserverclasspath_fragments: ["my-systemserverclasspath-fragment"],
5287 }
5288
5289 java_import {
5290 name: "myjavalib",
5291 jars: ["myjavalib.jar"],
5292 apex_available: ["myapex"],
5293 permitted_packages: ["javalib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005294 }
5295
5296 prebuilt_bootclasspath_fragment {
5297 name: "my-bootclasspath-fragment",
5298 contents: ["libfoo", "libbar"],
5299 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005300 hidden_api: {
5301 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5302 metadata: "my-bootclasspath-fragment/metadata.csv",
5303 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005304 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5305 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5306 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005307 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005308 }
5309
Liz Kammer2dc72442023-04-20 10:10:48 -04005310 prebuilt_systemserverclasspath_fragment {
5311 name: "my-systemserverclasspath-fragment",
5312 contents: ["libbaz"],
5313 apex_available: ["myapex"],
5314 }
5315
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005316 java_import {
5317 name: "libfoo",
5318 jars: ["libfoo.jar"],
5319 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005320 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005321 }
5322
5323 java_sdk_library_import {
5324 name: "libbar",
5325 public: {
5326 jars: ["libbar.jar"],
5327 },
5328 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005329 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005330 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005331 }
Liz Kammer2dc72442023-04-20 10:10:48 -04005332
5333 java_sdk_library_import {
5334 name: "libbaz",
5335 public: {
5336 jars: ["libbaz.jar"],
5337 },
5338 apex_available: ["myapex"],
5339 shared_library: false,
5340 permitted_packages: ["baz"],
5341 }
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005342 `
5343
Paul Duffin89f570a2021-06-16 01:42:33 +01005344 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005345 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5346 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5347
Paul Duffin537ea3d2021-05-14 10:38:00 +01005348 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005349 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005350 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005351 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005352 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5353 `)
Liz Kammer2dc72442023-04-20 10:10:48 -04005354
5355 myApex := ctx.ModuleForTests("myapex", "android_common_myapex").Module()
5356
5357 overrideNames := []string{
5358 "",
5359 "myjavalib.myapex",
5360 "libfoo.myapex",
5361 "libbar.myapex",
5362 "libbaz.myapex",
5363 }
5364 mkEntries := android.AndroidMkEntriesForTest(t, ctx, myApex)
5365 for i, e := range mkEntries {
5366 g := e.OverrideName
5367 if w := overrideNames[i]; w != g {
5368 t.Errorf("Expected override name %q, got %q", w, g)
5369 }
5370 }
5371
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005372 })
5373
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005374 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5375 bp := `
5376 prebuilt_apex {
5377 name: "myapex",
5378 arch: {
5379 arm64: {
5380 src: "myapex-arm64.apex",
5381 },
5382 arm: {
5383 src: "myapex-arm.apex",
5384 },
5385 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005386 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5387 }
5388
5389 prebuilt_bootclasspath_fragment {
5390 name: "my-bootclasspath-fragment",
5391 contents: ["libfoo", "libbar"],
5392 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005393 hidden_api: {
5394 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5395 metadata: "my-bootclasspath-fragment/metadata.csv",
5396 index: "my-bootclasspath-fragment/index.csv",
5397 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5398 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5399 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005400 }
5401
5402 java_import {
5403 name: "libfoo",
5404 jars: ["libfoo.jar"],
5405 apex_available: ["myapex"],
5406 }
5407
5408 java_library {
5409 name: "libfoo",
5410 srcs: ["foo/bar/MyClass.java"],
5411 apex_available: ["myapex"],
5412 }
Paul Duffin37856732021-02-26 14:24:15 +00005413
5414 java_sdk_library_import {
5415 name: "libbar",
5416 public: {
5417 jars: ["libbar.jar"],
5418 },
5419 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005420 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005421 }
5422
5423 java_sdk_library {
5424 name: "libbar",
5425 srcs: ["foo/bar/MyClass.java"],
5426 unsafe_ignore_missing_latest_api: true,
5427 apex_available: ["myapex"],
5428 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005429 `
5430
5431 // In this test the source (java_library) libfoo is active since the
5432 // prebuilt (java_import) defaults to prefer:false. However the
5433 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5434 // find the dex boot jar in it. We either need to disable the source libfoo
5435 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005436 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005437 // dexbootjar check is skipped if AllowMissingDependencies is true
5438 preparerAllowMissingDeps := android.GroupFixturePreparers(
5439 preparer,
5440 android.PrepareForTestWithAllowMissingDependencies,
5441 )
5442 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005443 })
5444
5445 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5446 bp := `
5447 prebuilt_apex {
5448 name: "myapex",
5449 arch: {
5450 arm64: {
5451 src: "myapex-arm64.apex",
5452 },
5453 arm: {
5454 src: "myapex-arm.apex",
5455 },
5456 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005457 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5458 }
5459
5460 prebuilt_bootclasspath_fragment {
5461 name: "my-bootclasspath-fragment",
5462 contents: ["libfoo", "libbar"],
5463 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005464 hidden_api: {
5465 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5466 metadata: "my-bootclasspath-fragment/metadata.csv",
5467 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005468 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5469 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5470 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005471 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005472 }
5473
5474 java_import {
5475 name: "libfoo",
5476 prefer: true,
5477 jars: ["libfoo.jar"],
5478 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005479 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005480 }
5481
5482 java_library {
5483 name: "libfoo",
5484 srcs: ["foo/bar/MyClass.java"],
5485 apex_available: ["myapex"],
5486 }
Paul Duffin37856732021-02-26 14:24:15 +00005487
5488 java_sdk_library_import {
5489 name: "libbar",
5490 prefer: true,
5491 public: {
5492 jars: ["libbar.jar"],
5493 },
5494 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005495 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005496 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005497 }
5498
5499 java_sdk_library {
5500 name: "libbar",
5501 srcs: ["foo/bar/MyClass.java"],
5502 unsafe_ignore_missing_latest_api: true,
5503 apex_available: ["myapex"],
5504 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005505 `
5506
Paul Duffin89f570a2021-06-16 01:42:33 +01005507 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005508 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5509 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005510
Paul Duffin537ea3d2021-05-14 10:38:00 +01005511 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005512 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005513 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005514 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005515 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5516 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005517 })
5518
5519 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5520 bp := `
5521 apex {
5522 name: "myapex",
5523 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005524 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005525 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005526 }
5527
5528 apex_key {
5529 name: "myapex.key",
5530 public_key: "testkey.avbpubkey",
5531 private_key: "testkey.pem",
5532 }
5533
5534 prebuilt_apex {
5535 name: "myapex",
5536 arch: {
5537 arm64: {
5538 src: "myapex-arm64.apex",
5539 },
5540 arm: {
5541 src: "myapex-arm.apex",
5542 },
5543 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005544 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5545 }
5546
5547 prebuilt_bootclasspath_fragment {
5548 name: "my-bootclasspath-fragment",
5549 contents: ["libfoo", "libbar"],
5550 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005551 hidden_api: {
5552 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5553 metadata: "my-bootclasspath-fragment/metadata.csv",
5554 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005555 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5556 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5557 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005558 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005559 }
5560
5561 java_import {
5562 name: "libfoo",
5563 jars: ["libfoo.jar"],
5564 apex_available: ["myapex"],
5565 }
5566
5567 java_library {
5568 name: "libfoo",
5569 srcs: ["foo/bar/MyClass.java"],
5570 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005571 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005572 }
Paul Duffin37856732021-02-26 14:24:15 +00005573
5574 java_sdk_library_import {
5575 name: "libbar",
5576 public: {
5577 jars: ["libbar.jar"],
5578 },
5579 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005580 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005581 }
5582
5583 java_sdk_library {
5584 name: "libbar",
5585 srcs: ["foo/bar/MyClass.java"],
5586 unsafe_ignore_missing_latest_api: true,
5587 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005588 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005589 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005590 `
5591
Paul Duffin89f570a2021-06-16 01:42:33 +01005592 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005593 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5594 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005595
Paul Duffin537ea3d2021-05-14 10:38:00 +01005596 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005597 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005598 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005599 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005600 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5601 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005602 })
5603
5604 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5605 bp := `
5606 apex {
5607 name: "myapex",
5608 enabled: false,
5609 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005610 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005611 }
5612
5613 apex_key {
5614 name: "myapex.key",
5615 public_key: "testkey.avbpubkey",
5616 private_key: "testkey.pem",
5617 }
5618
5619 prebuilt_apex {
5620 name: "myapex",
5621 arch: {
5622 arm64: {
5623 src: "myapex-arm64.apex",
5624 },
5625 arm: {
5626 src: "myapex-arm.apex",
5627 },
5628 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005629 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5630 }
5631
5632 prebuilt_bootclasspath_fragment {
5633 name: "my-bootclasspath-fragment",
5634 contents: ["libfoo", "libbar"],
5635 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005636 hidden_api: {
5637 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5638 metadata: "my-bootclasspath-fragment/metadata.csv",
5639 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005640 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5641 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5642 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005643 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005644 }
5645
5646 java_import {
5647 name: "libfoo",
5648 prefer: true,
5649 jars: ["libfoo.jar"],
5650 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005651 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005652 }
5653
5654 java_library {
5655 name: "libfoo",
5656 srcs: ["foo/bar/MyClass.java"],
5657 apex_available: ["myapex"],
5658 }
Paul Duffin37856732021-02-26 14:24:15 +00005659
5660 java_sdk_library_import {
5661 name: "libbar",
5662 prefer: true,
5663 public: {
5664 jars: ["libbar.jar"],
5665 },
5666 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005667 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005668 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005669 }
5670
5671 java_sdk_library {
5672 name: "libbar",
5673 srcs: ["foo/bar/MyClass.java"],
5674 unsafe_ignore_missing_latest_api: true,
5675 apex_available: ["myapex"],
5676 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005677 `
5678
Paul Duffin89f570a2021-06-16 01:42:33 +01005679 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005680 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5681 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005682
Paul Duffin537ea3d2021-05-14 10:38:00 +01005683 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005684 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005685 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005686 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005687 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5688 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005689 })
5690}
5691
Jooyung Han95513842023-04-17 11:24:54 +09005692func TestPrebuiltSkipsSymbols(t *testing.T) {
5693 testCases := []struct {
5694 name string
5695 usePrebuilt bool
5696 installSymbolFiles bool
5697 }{
5698 {
5699 name: "Source module build rule doesn't install symbol files",
5700 usePrebuilt: true,
5701 installSymbolFiles: false,
5702 },
5703 {
5704 name: "Source module is installed with symbols",
5705 usePrebuilt: false,
5706 installSymbolFiles: true,
5707 },
5708 }
5709 for _, tc := range testCases {
5710 t.Run(tc.name, func(t *testing.T) {
5711 preferProperty := "prefer: false"
5712 if tc.usePrebuilt {
5713 preferProperty = "prefer: true"
5714 }
5715 ctx := testApex(t, `
5716 // Source module
5717 apex {
5718 name: "myapex",
Inseob Kim5bedfee2023-04-20 10:16:14 +09005719 binaries: ["foo"],
Jooyung Han95513842023-04-17 11:24:54 +09005720 key: "myapex.key",
5721 updatable: false,
5722 }
5723
5724 apex_key {
5725 name: "myapex.key",
5726 public_key: "testkey.avbpubkey",
5727 private_key: "testkey.pem",
5728 }
5729
5730 apex_set {
5731 name: "myapex",
5732 set: "myapex.apks",
5733 `+preferProperty+`
5734 }
Inseob Kim5bedfee2023-04-20 10:16:14 +09005735
5736 cc_binary {
5737 name: "foo",
5738 srcs: ["mylib.cpp"],
5739 system_shared_libs: [],
5740 stl: "none",
5741 apex_available: [ "myapex" ],
5742 }
Jooyung Han95513842023-04-17 11:24:54 +09005743 `)
5744 // Symbol files are installed by installing entries under ${OUT}/apex/{apex name}
Inseob Kim5bedfee2023-04-20 10:16:14 +09005745 android.AssertStringListContainsEquals(t, "Installs",
5746 ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().FilesToInstall().Strings(),
5747 filepath.Join(ctx.Config().SoongOutDir(), "target/product/test_device/apex/myapex/bin/foo"),
Jooyung Han95513842023-04-17 11:24:54 +09005748 tc.installSymbolFiles)
5749 })
5750 }
5751}
5752
Roland Levillain630846d2019-06-26 12:48:34 +01005753func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005754 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005755 apex_test {
5756 name: "myapex",
5757 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005758 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005759 tests: [
5760 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005761 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005762 ],
5763 }
5764
5765 apex_key {
5766 name: "myapex.key",
5767 public_key: "testkey.avbpubkey",
5768 private_key: "testkey.pem",
5769 }
5770
Liz Kammer1c14a212020-05-12 15:26:55 -07005771 filegroup {
5772 name: "fg",
5773 srcs: [
5774 "baz",
5775 "bar/baz"
5776 ],
5777 }
5778
Roland Levillain630846d2019-06-26 12:48:34 +01005779 cc_test {
5780 name: "mytest",
5781 gtest: false,
5782 srcs: ["mytest.cpp"],
5783 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005784 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005785 system_shared_libs: [],
5786 static_executable: true,
5787 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005788 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005789 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005790
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005791 cc_library {
5792 name: "mylib",
5793 srcs: ["mylib.cpp"],
5794 system_shared_libs: [],
5795 stl: "none",
5796 }
5797
Liz Kammer5bd365f2020-05-27 15:15:11 -07005798 filegroup {
5799 name: "fg2",
5800 srcs: [
5801 "testdata/baz"
5802 ],
5803 }
5804
Roland Levillain9b5fde92019-06-28 15:41:19 +01005805 cc_test {
5806 name: "mytests",
5807 gtest: false,
5808 srcs: [
5809 "mytest1.cpp",
5810 "mytest2.cpp",
5811 "mytest3.cpp",
5812 ],
5813 test_per_src: true,
5814 relative_install_path: "test",
5815 system_shared_libs: [],
5816 static_executable: true,
5817 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005818 data: [
5819 ":fg",
5820 ":fg2",
5821 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005822 }
Roland Levillain630846d2019-06-26 12:48:34 +01005823 `)
5824
Sundong Ahnabb64432019-10-22 13:58:29 +09005825 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005826 copyCmds := apexRule.Args["copy_commands"]
5827
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005828 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005829 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005830 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005831
Liz Kammer1c14a212020-05-12 15:26:55 -07005832 //Ensure that test data are copied into apex.
5833 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5834 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5835
Roland Levillain9b5fde92019-06-28 15:41:19 +01005836 // Ensure that test deps built with `test_per_src` are copied into apex.
5837 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5838 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5839 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005840
5841 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005842 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005843 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005844 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005845 prefix := "TARGET_"
5846 var builder strings.Builder
5847 data.Custom(&builder, name, prefix, "", data)
5848 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005849 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5850 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5851 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5852 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
5853 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
5854 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005855 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005856
5857 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005858 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005859 data.Custom(&builder, name, prefix, "", data)
5860 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005861 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5862 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005863}
5864
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005865func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005866 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005867 apex {
5868 name: "myapex",
5869 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005870 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005871 }
5872 apex_key {
5873 name: "myapex.key",
5874 public_key: "testkey.avbpubkey",
5875 private_key: "testkey.pem",
5876 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005877 `,
5878 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5879 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5880 }),
5881 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005882 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00005883 ensureListContains(t, ab.makeModulesToInstall, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005884 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005885 var builder strings.Builder
5886 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5887 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005888 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex myapex.flattened\n")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005889}
5890
Jooyung Hand48f3c32019-08-23 11:18:57 +09005891func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5892 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5893 apex {
5894 name: "myapex",
5895 key: "myapex.key",
5896 native_shared_libs: ["libfoo"],
5897 }
5898
5899 apex_key {
5900 name: "myapex.key",
5901 public_key: "testkey.avbpubkey",
5902 private_key: "testkey.pem",
5903 }
5904
5905 cc_library {
5906 name: "libfoo",
5907 stl: "none",
5908 system_shared_libs: [],
5909 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005910 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005911 }
5912 `)
5913 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5914 apex {
5915 name: "myapex",
5916 key: "myapex.key",
5917 java_libs: ["myjar"],
5918 }
5919
5920 apex_key {
5921 name: "myapex.key",
5922 public_key: "testkey.avbpubkey",
5923 private_key: "testkey.pem",
5924 }
5925
5926 java_library {
5927 name: "myjar",
5928 srcs: ["foo/bar/MyClass.java"],
5929 sdk_version: "none",
5930 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005931 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005932 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005933 }
5934 `)
5935}
5936
Bill Peckhama41a6962021-01-11 10:58:54 -08005937func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005938 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005939 apex {
5940 name: "myapex",
5941 key: "myapex.key",
5942 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005943 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005944 }
5945
5946 apex_key {
5947 name: "myapex.key",
5948 public_key: "testkey.avbpubkey",
5949 private_key: "testkey.pem",
5950 }
5951
5952 java_import {
5953 name: "myjavaimport",
5954 apex_available: ["myapex"],
5955 jars: ["my.jar"],
5956 compile_dex: true,
5957 }
5958 `)
5959
5960 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5961 apexRule := module.Rule("apexRule")
5962 copyCmds := apexRule.Args["copy_commands"]
5963 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5964}
5965
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005966func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005967 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005968 apex {
5969 name: "myapex",
5970 key: "myapex.key",
5971 apps: [
5972 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005973 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005974 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005975 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005976 }
5977
5978 apex_key {
5979 name: "myapex.key",
5980 public_key: "testkey.avbpubkey",
5981 private_key: "testkey.pem",
5982 }
5983
5984 android_app {
5985 name: "AppFoo",
5986 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005987 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005988 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005989 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005990 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005991 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005992 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005993
5994 android_app {
5995 name: "AppFooPriv",
5996 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005997 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005998 system_modules: "none",
5999 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08006000 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006001 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09006002 }
Jiyong Park8be103b2019-11-08 15:53:48 +09006003
6004 cc_library_shared {
6005 name: "libjni",
6006 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006007 shared_libs: ["libfoo"],
6008 stl: "none",
6009 system_shared_libs: [],
6010 apex_available: [ "myapex" ],
6011 sdk_version: "current",
6012 }
6013
6014 cc_library_shared {
6015 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09006016 stl: "none",
6017 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09006018 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08006019 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09006020 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006021 `)
6022
Sundong Ahnabb64432019-10-22 13:58:29 +09006023 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006024 apexRule := module.Rule("apexRule")
6025 copyCmds := apexRule.Args["copy_commands"]
6026
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006027 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
6028 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09006029
Colin Crossaede88c2020-08-11 12:17:01 -07006030 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006031 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09006032 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006033 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09006034 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006035 // JNI libraries including transitive deps are
6036 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01006037 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09006038 // ... embedded inside APK (jnilibs.zip)
6039 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
6040 // ... and not directly inside the APEX
6041 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
6042 }
Dario Frenicde2a032019-10-27 00:29:22 +01006043}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006044
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006045func TestApexWithAppImportBuildId(t *testing.T) {
6046 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
6047 for _, id := range invalidBuildIds {
6048 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
6049 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6050 variables.BuildId = proptools.StringPtr(id)
6051 })
6052 testApexError(t, message, `apex {
6053 name: "myapex",
6054 key: "myapex.key",
6055 apps: ["AppFooPrebuilt"],
6056 updatable: false,
6057 }
6058
6059 apex_key {
6060 name: "myapex.key",
6061 public_key: "testkey.avbpubkey",
6062 private_key: "testkey.pem",
6063 }
6064
6065 android_app_import {
6066 name: "AppFooPrebuilt",
6067 apk: "PrebuiltAppFoo.apk",
6068 presigned: true,
6069 apex_available: ["myapex"],
6070 }
6071 `, fixture)
6072 }
6073}
6074
Dario Frenicde2a032019-10-27 00:29:22 +01006075func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006076 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01006077 apex {
6078 name: "myapex",
6079 key: "myapex.key",
6080 apps: [
6081 "AppFooPrebuilt",
6082 "AppFooPrivPrebuilt",
6083 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006084 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01006085 }
6086
6087 apex_key {
6088 name: "myapex.key",
6089 public_key: "testkey.avbpubkey",
6090 private_key: "testkey.pem",
6091 }
6092
6093 android_app_import {
6094 name: "AppFooPrebuilt",
6095 apk: "PrebuiltAppFoo.apk",
6096 presigned: true,
6097 dex_preopt: {
6098 enabled: false,
6099 },
Jiyong Park592a6a42020-04-21 22:34:28 +09006100 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006101 }
6102
6103 android_app_import {
6104 name: "AppFooPrivPrebuilt",
6105 apk: "PrebuiltAppFooPriv.apk",
6106 privileged: true,
6107 presigned: true,
6108 dex_preopt: {
6109 enabled: false,
6110 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006111 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006112 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006113 }
6114 `)
6115
Sundong Ahnabb64432019-10-22 13:58:29 +09006116 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01006117 apexRule := module.Rule("apexRule")
6118 copyCmds := apexRule.Args["copy_commands"]
6119
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006120 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6121 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006122}
6123
6124func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006125 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006126 apex {
6127 name: "myapex",
6128 key: "myapex.key",
6129 apps: [
6130 "AppFoo",
6131 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006132 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006133 }
6134
6135 apex_key {
6136 name: "myapex.key",
6137 public_key: "testkey.avbpubkey",
6138 private_key: "testkey.pem",
6139 }
6140
6141 android_app {
6142 name: "AppFoo",
6143 srcs: ["foo/bar/MyClass.java"],
6144 sdk_version: "none",
6145 system_modules: "none",
6146 apex_available: [ "myapex" ],
6147 }
6148
6149 android_app_import {
6150 name: "AppFoo",
6151 apk: "AppFooPrebuilt.apk",
6152 filename: "AppFooPrebuilt.apk",
6153 presigned: true,
6154 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006155 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006156 }
6157 `, withFiles(map[string][]byte{
6158 "AppFooPrebuilt.apk": nil,
6159 }))
6160
6161 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006162 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006163 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006164}
6165
Dario Freni6f3937c2019-12-20 22:58:03 +00006166func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006167 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006168 apex {
6169 name: "myapex",
6170 key: "myapex.key",
6171 apps: [
6172 "TesterHelpAppFoo",
6173 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006174 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006175 }
6176
6177 apex_key {
6178 name: "myapex.key",
6179 public_key: "testkey.avbpubkey",
6180 private_key: "testkey.pem",
6181 }
6182
6183 android_test_helper_app {
6184 name: "TesterHelpAppFoo",
6185 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006186 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006187 }
6188
6189 `)
6190
6191 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6192 apexRule := module.Rule("apexRule")
6193 copyCmds := apexRule.Args["copy_commands"]
6194
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006195 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006196}
6197
Jooyung Han18020ea2019-11-13 10:50:48 +09006198func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6199 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006200 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006201 apex {
6202 name: "myapex",
6203 key: "myapex.key",
6204 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006205 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006206 }
6207
6208 apex_key {
6209 name: "myapex.key",
6210 public_key: "testkey.avbpubkey",
6211 private_key: "testkey.pem",
6212 }
6213
6214 apex {
6215 name: "otherapex",
6216 key: "myapex.key",
6217 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006218 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006219 }
6220
6221 cc_defaults {
6222 name: "libfoo-defaults",
6223 apex_available: ["otherapex"],
6224 }
6225
6226 cc_library {
6227 name: "libfoo",
6228 defaults: ["libfoo-defaults"],
6229 stl: "none",
6230 system_shared_libs: [],
6231 }`)
6232}
6233
Paul Duffine52e66f2020-03-30 17:54:29 +01006234func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006235 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006236 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006237 apex {
6238 name: "myapex",
6239 key: "myapex.key",
6240 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006241 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006242 }
6243
6244 apex_key {
6245 name: "myapex.key",
6246 public_key: "testkey.avbpubkey",
6247 private_key: "testkey.pem",
6248 }
6249
6250 apex {
6251 name: "otherapex",
6252 key: "otherapex.key",
6253 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006254 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006255 }
6256
6257 apex_key {
6258 name: "otherapex.key",
6259 public_key: "testkey.avbpubkey",
6260 private_key: "testkey.pem",
6261 }
6262
6263 cc_library {
6264 name: "libfoo",
6265 stl: "none",
6266 system_shared_libs: [],
6267 apex_available: ["otherapex"],
6268 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006269}
Jiyong Park127b40b2019-09-30 16:04:35 +09006270
Paul Duffine52e66f2020-03-30 17:54:29 +01006271func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006272 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006273 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006274.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006275.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006276.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006277.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006278.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006279.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006280 apex {
6281 name: "myapex",
6282 key: "myapex.key",
6283 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006284 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006285 }
6286
6287 apex_key {
6288 name: "myapex.key",
6289 public_key: "testkey.avbpubkey",
6290 private_key: "testkey.pem",
6291 }
6292
Jiyong Park127b40b2019-09-30 16:04:35 +09006293 cc_library {
6294 name: "libfoo",
6295 stl: "none",
6296 shared_libs: ["libbar"],
6297 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006298 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006299 }
6300
6301 cc_library {
6302 name: "libbar",
6303 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006304 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006305 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006306 apex_available: ["myapex"],
6307 }
6308
6309 cc_library {
6310 name: "libbaz",
6311 stl: "none",
6312 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006313 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006314}
Jiyong Park127b40b2019-09-30 16:04:35 +09006315
Paul Duffine52e66f2020-03-30 17:54:29 +01006316func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006317 testApexError(t, "\"otherapex\" is not a valid module name", `
6318 apex {
6319 name: "myapex",
6320 key: "myapex.key",
6321 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006322 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006323 }
6324
6325 apex_key {
6326 name: "myapex.key",
6327 public_key: "testkey.avbpubkey",
6328 private_key: "testkey.pem",
6329 }
6330
6331 cc_library {
6332 name: "libfoo",
6333 stl: "none",
6334 system_shared_libs: [],
6335 apex_available: ["otherapex"],
6336 }`)
6337
Paul Duffine52e66f2020-03-30 17:54:29 +01006338 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006339 apex {
6340 name: "myapex",
6341 key: "myapex.key",
6342 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006343 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006344 }
6345
6346 apex_key {
6347 name: "myapex.key",
6348 public_key: "testkey.avbpubkey",
6349 private_key: "testkey.pem",
6350 }
6351
6352 cc_library {
6353 name: "libfoo",
6354 stl: "none",
6355 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006356 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006357 apex_available: ["myapex"],
6358 }
6359
6360 cc_library {
6361 name: "libbar",
6362 stl: "none",
6363 system_shared_libs: [],
6364 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006365 }
6366
6367 cc_library {
6368 name: "libbaz",
6369 stl: "none",
6370 system_shared_libs: [],
6371 stubs: {
6372 versions: ["10", "20", "30"],
6373 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006374 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006375}
Jiyong Park127b40b2019-09-30 16:04:35 +09006376
Jiyong Park89e850a2020-04-07 16:37:39 +09006377func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006378 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006379 apex {
6380 name: "myapex",
6381 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006382 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006383 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006384 }
6385
6386 apex_key {
6387 name: "myapex.key",
6388 public_key: "testkey.avbpubkey",
6389 private_key: "testkey.pem",
6390 }
6391
6392 cc_library {
6393 name: "libfoo",
6394 stl: "none",
6395 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006396 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006397 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006398 }
6399
6400 cc_library {
6401 name: "libfoo2",
6402 stl: "none",
6403 system_shared_libs: [],
6404 shared_libs: ["libbaz"],
6405 apex_available: ["//apex_available:platform"],
6406 }
6407
6408 cc_library {
6409 name: "libbar",
6410 stl: "none",
6411 system_shared_libs: [],
6412 apex_available: ["myapex"],
6413 }
6414
6415 cc_library {
6416 name: "libbaz",
6417 stl: "none",
6418 system_shared_libs: [],
6419 apex_available: ["myapex"],
6420 stubs: {
6421 versions: ["1"],
6422 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006423 }`)
6424
Jiyong Park89e850a2020-04-07 16:37:39 +09006425 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6426 // because it depends on libbar which isn't available to platform
6427 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6428 if libfoo.NotAvailableForPlatform() != true {
6429 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6430 }
6431
6432 // libfoo2 however can be available to platform because it depends on libbaz which provides
6433 // stubs
6434 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6435 if libfoo2.NotAvailableForPlatform() == true {
6436 t.Errorf("%q should be available to platform", libfoo2.String())
6437 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006438}
Jiyong Parka90ca002019-10-07 15:47:24 +09006439
Paul Duffine52e66f2020-03-30 17:54:29 +01006440func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006441 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006442 apex {
6443 name: "myapex",
6444 key: "myapex.key",
6445 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006446 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006447 }
6448
6449 apex_key {
6450 name: "myapex.key",
6451 public_key: "testkey.avbpubkey",
6452 private_key: "testkey.pem",
6453 }
6454
6455 cc_library {
6456 name: "libfoo",
6457 stl: "none",
6458 system_shared_libs: [],
6459 apex_available: ["myapex"],
6460 static: {
6461 apex_available: ["//apex_available:platform"],
6462 },
6463 }`)
6464
Jiyong Park89e850a2020-04-07 16:37:39 +09006465 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6466 if libfooShared.NotAvailableForPlatform() != true {
6467 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6468 }
6469 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6470 if libfooStatic.NotAvailableForPlatform() != false {
6471 t.Errorf("%q should be available to platform", libfooStatic.String())
6472 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006473}
6474
Jiyong Park5d790c32019-11-15 18:40:32 +09006475func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006476 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006477 apex {
6478 name: "myapex",
6479 key: "myapex.key",
6480 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006481 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006482 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006483 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006484 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006485 }
6486
6487 override_apex {
6488 name: "override_myapex",
6489 base: "myapex",
6490 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006491 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006492 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006493 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006494 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006495 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006496 key: "mynewapex.key",
6497 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006498 }
6499
6500 apex_key {
6501 name: "myapex.key",
6502 public_key: "testkey.avbpubkey",
6503 private_key: "testkey.pem",
6504 }
6505
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006506 apex_key {
6507 name: "mynewapex.key",
6508 public_key: "testkey2.avbpubkey",
6509 private_key: "testkey2.pem",
6510 }
6511
6512 android_app_certificate {
6513 name: "myapex.certificate",
6514 certificate: "testkey",
6515 }
6516
Jiyong Park5d790c32019-11-15 18:40:32 +09006517 android_app {
6518 name: "app",
6519 srcs: ["foo/bar/MyClass.java"],
6520 package_name: "foo",
6521 sdk_version: "none",
6522 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006523 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006524 }
6525
6526 override_android_app {
6527 name: "override_app",
6528 base: "app",
6529 package_name: "bar",
6530 }
markchien7c803b82021-08-26 22:10:06 +08006531
6532 bpf {
6533 name: "bpf",
6534 srcs: ["bpf.c"],
6535 }
6536
6537 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006538 name: "overrideBpf",
6539 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006540 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006541
6542 prebuilt_etc {
6543 name: "myetc",
6544 src: "myprebuilt",
6545 }
6546
6547 prebuilt_etc {
6548 name: "override_myetc",
6549 src: "override_myprebuilt",
6550 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006551 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006552
Jiyong Park317645e2019-12-05 13:20:58 +09006553 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6554 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6555 if originalVariant.GetOverriddenBy() != "" {
6556 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6557 }
6558 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6559 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6560 }
6561
Jiyong Park5d790c32019-11-15 18:40:32 +09006562 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6563 apexRule := module.Rule("apexRule")
6564 copyCmds := apexRule.Args["copy_commands"]
6565
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006566 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6567 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006568
markchien7c803b82021-08-26 22:10:06 +08006569 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006570 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006571
Daniel Norman5a3ce132021-08-26 15:44:43 -07006572 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6573 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6574
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006575 apexBundle := module.Module().(*apexBundle)
6576 name := apexBundle.Name()
6577 if name != "override_myapex" {
6578 t.Errorf("name should be \"override_myapex\", but was %q", name)
6579 }
6580
Baligh Uddin004d7172020-02-19 21:29:28 -08006581 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6582 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6583 }
6584
Jiyong Park20bacab2020-03-03 11:45:41 +09006585 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006586 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006587 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6588
6589 signApkRule := module.Rule("signapk")
6590 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006591
Colin Crossaa255532020-07-03 13:18:24 -07006592 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006593 var builder strings.Builder
6594 data.Custom(&builder, name, "TARGET_", "", data)
6595 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006596 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6597 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
6598 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006599 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006600 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006601 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006602 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006603 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006604 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
6605 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006606}
6607
Albert Martineefabcf2022-03-21 20:11:16 +00006608func TestMinSdkVersionOverride(t *testing.T) {
6609 // Override from 29 to 31
6610 minSdkOverride31 := "31"
6611 ctx := testApex(t, `
6612 apex {
6613 name: "myapex",
6614 key: "myapex.key",
6615 native_shared_libs: ["mylib"],
6616 updatable: true,
6617 min_sdk_version: "29"
6618 }
6619
6620 override_apex {
6621 name: "override_myapex",
6622 base: "myapex",
6623 logging_parent: "com.foo.bar",
6624 package_name: "test.overridden.package"
6625 }
6626
6627 apex_key {
6628 name: "myapex.key",
6629 public_key: "testkey.avbpubkey",
6630 private_key: "testkey.pem",
6631 }
6632
6633 cc_library {
6634 name: "mylib",
6635 srcs: ["mylib.cpp"],
6636 runtime_libs: ["libbar"],
6637 system_shared_libs: [],
6638 stl: "none",
6639 apex_available: [ "myapex" ],
6640 min_sdk_version: "apex_inherit"
6641 }
6642
6643 cc_library {
6644 name: "libbar",
6645 srcs: ["mylib.cpp"],
6646 system_shared_libs: [],
6647 stl: "none",
6648 apex_available: [ "myapex" ],
6649 min_sdk_version: "apex_inherit"
6650 }
6651
6652 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6653
6654 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6655 copyCmds := apexRule.Args["copy_commands"]
6656
6657 // Ensure that direct non-stubs dep is always included
6658 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6659
6660 // Ensure that runtime_libs dep in included
6661 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6662
6663 // Ensure libraries target overridden min_sdk_version value
6664 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6665}
6666
6667func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6668 // Attempt to override from 31 to 29, should be a NOOP
6669 minSdkOverride29 := "29"
6670 ctx := testApex(t, `
6671 apex {
6672 name: "myapex",
6673 key: "myapex.key",
6674 native_shared_libs: ["mylib"],
6675 updatable: true,
6676 min_sdk_version: "31"
6677 }
6678
6679 override_apex {
6680 name: "override_myapex",
6681 base: "myapex",
6682 logging_parent: "com.foo.bar",
6683 package_name: "test.overridden.package"
6684 }
6685
6686 apex_key {
6687 name: "myapex.key",
6688 public_key: "testkey.avbpubkey",
6689 private_key: "testkey.pem",
6690 }
6691
6692 cc_library {
6693 name: "mylib",
6694 srcs: ["mylib.cpp"],
6695 runtime_libs: ["libbar"],
6696 system_shared_libs: [],
6697 stl: "none",
6698 apex_available: [ "myapex" ],
6699 min_sdk_version: "apex_inherit"
6700 }
6701
6702 cc_library {
6703 name: "libbar",
6704 srcs: ["mylib.cpp"],
6705 system_shared_libs: [],
6706 stl: "none",
6707 apex_available: [ "myapex" ],
6708 min_sdk_version: "apex_inherit"
6709 }
6710
6711 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6712
6713 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6714 copyCmds := apexRule.Args["copy_commands"]
6715
6716 // Ensure that direct non-stubs dep is always included
6717 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6718
6719 // Ensure that runtime_libs dep in included
6720 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6721
6722 // Ensure libraries target the original min_sdk_version value rather than the overridden
6723 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6724}
6725
Jooyung Han214bf372019-11-12 13:03:50 +09006726func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006727 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006728 apex {
6729 name: "myapex",
6730 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006731 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006732 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006733 }
6734
6735 apex_key {
6736 name: "myapex.key",
6737 public_key: "testkey.avbpubkey",
6738 private_key: "testkey.pem",
6739 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006740
6741 cc_library {
6742 name: "mylib",
6743 srcs: ["mylib.cpp"],
6744 stl: "libc++",
6745 system_shared_libs: [],
6746 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006747 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006748 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006749 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006750
6751 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6752 args := module.Rule("apexRule").Args
6753 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006754 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006755
6756 // The copies of the libraries in the apex should have one more dependency than
6757 // the ones outside the apex, namely the unwinder. Ideally we should check
6758 // the dependency names directly here but for some reason the names are blank in
6759 // this test.
6760 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006761 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006762 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6763 if len(apexImplicits) != len(nonApexImplicits)+1 {
6764 t.Errorf("%q missing unwinder dep", lib)
6765 }
6766 }
Jooyung Han214bf372019-11-12 13:03:50 +09006767}
6768
Paul Duffine05480a2021-03-08 15:07:14 +00006769var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006770 "api/current.txt": nil,
6771 "api/removed.txt": nil,
6772 "api/system-current.txt": nil,
6773 "api/system-removed.txt": nil,
6774 "api/test-current.txt": nil,
6775 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006776
Anton Hanssondff2c782020-12-21 17:10:01 +00006777 "100/public/api/foo.txt": nil,
6778 "100/public/api/foo-removed.txt": nil,
6779 "100/system/api/foo.txt": nil,
6780 "100/system/api/foo-removed.txt": nil,
6781
Paul Duffineedc5d52020-06-12 17:46:39 +01006782 // For java_sdk_library_import
6783 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006784}
6785
Jooyung Han58f26ab2019-12-18 15:34:32 +09006786func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006787 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006788 apex {
6789 name: "myapex",
6790 key: "myapex.key",
6791 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006792 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006793 }
6794
6795 apex_key {
6796 name: "myapex.key",
6797 public_key: "testkey.avbpubkey",
6798 private_key: "testkey.pem",
6799 }
6800
6801 java_sdk_library {
6802 name: "foo",
6803 srcs: ["a.java"],
6804 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006805 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006806 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006807
6808 prebuilt_apis {
6809 name: "sdk",
6810 api_dirs: ["100"],
6811 }
Paul Duffin9b879592020-05-26 13:21:35 +01006812 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006813
6814 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006815 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006816 "javalib/foo.jar",
6817 "etc/permissions/foo.xml",
6818 })
6819 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006820 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006821 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 +09006822}
6823
Paul Duffin9b879592020-05-26 13:21:35 +01006824func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006825 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006826 apex {
6827 name: "myapex",
6828 key: "myapex.key",
6829 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006830 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006831 }
6832
6833 apex_key {
6834 name: "myapex.key",
6835 public_key: "testkey.avbpubkey",
6836 private_key: "testkey.pem",
6837 }
6838
6839 java_sdk_library {
6840 name: "foo",
6841 srcs: ["a.java"],
6842 api_packages: ["foo"],
6843 apex_available: ["myapex"],
6844 sdk_version: "none",
6845 system_modules: "none",
6846 }
6847
6848 java_library {
6849 name: "bar",
6850 srcs: ["a.java"],
6851 libs: ["foo"],
6852 apex_available: ["myapex"],
6853 sdk_version: "none",
6854 system_modules: "none",
6855 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006856
6857 prebuilt_apis {
6858 name: "sdk",
6859 api_dirs: ["100"],
6860 }
Paul Duffin9b879592020-05-26 13:21:35 +01006861 `, withFiles(filesForSdkLibrary))
6862
6863 // java_sdk_library installs both impl jar and permission XML
6864 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6865 "javalib/bar.jar",
6866 "javalib/foo.jar",
6867 "etc/permissions/foo.xml",
6868 })
6869
6870 // The bar library should depend on the implementation jar.
6871 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006872 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006873 t.Errorf("expected %q, found %#q", expected, actual)
6874 }
6875}
6876
6877func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006878 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006879 apex {
6880 name: "myapex",
6881 key: "myapex.key",
6882 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006883 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006884 }
6885
6886 apex_key {
6887 name: "myapex.key",
6888 public_key: "testkey.avbpubkey",
6889 private_key: "testkey.pem",
6890 }
6891
6892 java_sdk_library {
6893 name: "foo",
6894 srcs: ["a.java"],
6895 api_packages: ["foo"],
6896 apex_available: ["myapex"],
6897 sdk_version: "none",
6898 system_modules: "none",
6899 }
6900
6901 java_library {
6902 name: "bar",
6903 srcs: ["a.java"],
6904 libs: ["foo"],
6905 sdk_version: "none",
6906 system_modules: "none",
6907 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006908
6909 prebuilt_apis {
6910 name: "sdk",
6911 api_dirs: ["100"],
6912 }
Paul Duffin9b879592020-05-26 13:21:35 +01006913 `, withFiles(filesForSdkLibrary))
6914
6915 // java_sdk_library installs both impl jar and permission XML
6916 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6917 "javalib/foo.jar",
6918 "etc/permissions/foo.xml",
6919 })
6920
6921 // The bar library should depend on the stubs jar.
6922 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006923 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006924 t.Errorf("expected %q, found %#q", expected, actual)
6925 }
6926}
6927
Paul Duffineedc5d52020-06-12 17:46:39 +01006928func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006929 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006930 prebuilt_apis {
6931 name: "sdk",
6932 api_dirs: ["100"],
6933 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006934 withFiles(map[string][]byte{
6935 "apex/a.java": nil,
6936 "apex/apex_manifest.json": nil,
6937 "apex/Android.bp": []byte(`
6938 package {
6939 default_visibility: ["//visibility:private"],
6940 }
6941
6942 apex {
6943 name: "myapex",
6944 key: "myapex.key",
6945 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006946 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006947 }
6948
6949 apex_key {
6950 name: "myapex.key",
6951 public_key: "testkey.avbpubkey",
6952 private_key: "testkey.pem",
6953 }
6954
6955 java_library {
6956 name: "bar",
6957 srcs: ["a.java"],
6958 libs: ["foo"],
6959 apex_available: ["myapex"],
6960 sdk_version: "none",
6961 system_modules: "none",
6962 }
6963`),
6964 "source/a.java": nil,
6965 "source/api/current.txt": nil,
6966 "source/api/removed.txt": nil,
6967 "source/Android.bp": []byte(`
6968 package {
6969 default_visibility: ["//visibility:private"],
6970 }
6971
6972 java_sdk_library {
6973 name: "foo",
6974 visibility: ["//apex"],
6975 srcs: ["a.java"],
6976 api_packages: ["foo"],
6977 apex_available: ["myapex"],
6978 sdk_version: "none",
6979 system_modules: "none",
6980 public: {
6981 enabled: true,
6982 },
6983 }
6984`),
6985 "prebuilt/a.jar": nil,
6986 "prebuilt/Android.bp": []byte(`
6987 package {
6988 default_visibility: ["//visibility:private"],
6989 }
6990
6991 java_sdk_library_import {
6992 name: "foo",
6993 visibility: ["//apex", "//source"],
6994 apex_available: ["myapex"],
6995 prefer: true,
6996 public: {
6997 jars: ["a.jar"],
6998 },
6999 }
7000`),
Anton Hanssondff2c782020-12-21 17:10:01 +00007001 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01007002 )
7003
7004 // java_sdk_library installs both impl jar and permission XML
7005 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7006 "javalib/bar.jar",
7007 "javalib/foo.jar",
7008 "etc/permissions/foo.xml",
7009 })
7010
7011 // The bar library should depend on the implementation jar.
7012 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01007013 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01007014 t.Errorf("expected %q, found %#q", expected, actual)
7015 }
7016}
7017
7018func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
7019 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
7020 apex {
7021 name: "myapex",
7022 key: "myapex.key",
7023 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007024 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01007025 }
7026
7027 apex_key {
7028 name: "myapex.key",
7029 public_key: "testkey.avbpubkey",
7030 private_key: "testkey.pem",
7031 }
7032
7033 java_sdk_library_import {
7034 name: "foo",
7035 apex_available: ["myapex"],
7036 prefer: true,
7037 public: {
7038 jars: ["a.jar"],
7039 },
7040 }
7041
7042 `, withFiles(filesForSdkLibrary))
7043}
7044
atrost6e126252020-01-27 17:01:16 +00007045func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007046 result := android.GroupFixturePreparers(
7047 prepareForApexTest,
7048 java.PrepareForTestWithPlatformCompatConfig,
7049 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007050 apex {
7051 name: "myapex",
7052 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007053 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007054 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007055 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007056 }
7057
7058 apex_key {
7059 name: "myapex.key",
7060 public_key: "testkey.avbpubkey",
7061 private_key: "testkey.pem",
7062 }
7063
7064 platform_compat_config {
7065 name: "myjar-platform-compat-config",
7066 src: ":myjar",
7067 }
7068
7069 java_library {
7070 name: "myjar",
7071 srcs: ["foo/bar/MyClass.java"],
7072 sdk_version: "none",
7073 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007074 apex_available: [ "myapex" ],
7075 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007076
7077 // Make sure that a preferred prebuilt does not affect the apex contents.
7078 prebuilt_platform_compat_config {
7079 name: "myjar-platform-compat-config",
7080 metadata: "compat-config/metadata.xml",
7081 prefer: true,
7082 }
atrost6e126252020-01-27 17:01:16 +00007083 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007084 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00007085 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7086 "etc/compatconfig/myjar-platform-compat-config.xml",
7087 "javalib/myjar.jar",
7088 })
7089}
7090
Jooyung Han862c0d62022-12-21 10:15:37 +09007091func TestNoDupeApexFiles(t *testing.T) {
7092 android.GroupFixturePreparers(
7093 android.PrepareForTestWithAndroidBuildComponents,
7094 PrepareForTestWithApexBuildComponents,
7095 prepareForTestWithMyapex,
7096 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7097 ).
7098 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7099 RunTestWithBp(t, `
7100 apex {
7101 name: "myapex",
7102 key: "myapex.key",
7103 prebuilts: ["foo", "bar"],
7104 updatable: false,
7105 }
7106
7107 apex_key {
7108 name: "myapex.key",
7109 public_key: "testkey.avbpubkey",
7110 private_key: "testkey.pem",
7111 }
7112
7113 prebuilt_etc {
7114 name: "foo",
7115 src: "myprebuilt",
7116 filename_from_src: true,
7117 }
7118
7119 prebuilt_etc {
7120 name: "bar",
7121 src: "myprebuilt",
7122 filename_from_src: true,
7123 }
7124 `)
7125}
7126
Jiyong Park479321d2019-12-16 11:47:12 +09007127func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7128 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7129 apex {
7130 name: "myapex",
7131 key: "myapex.key",
7132 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007133 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007134 }
7135
7136 apex_key {
7137 name: "myapex.key",
7138 public_key: "testkey.avbpubkey",
7139 private_key: "testkey.pem",
7140 }
7141
7142 java_library {
7143 name: "myjar",
7144 srcs: ["foo/bar/MyClass.java"],
7145 sdk_version: "none",
7146 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007147 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007148 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007149 }
7150 `)
7151}
7152
Jiyong Park7afd1072019-12-30 16:56:33 +09007153func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007154 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007155 apex {
7156 name: "myapex",
7157 key: "myapex.key",
7158 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007159 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007160 }
7161
7162 apex_key {
7163 name: "myapex.key",
7164 public_key: "testkey.avbpubkey",
7165 private_key: "testkey.pem",
7166 }
7167
7168 cc_library {
7169 name: "mylib",
7170 srcs: ["mylib.cpp"],
7171 system_shared_libs: [],
7172 stl: "none",
7173 required: ["a", "b"],
7174 host_required: ["c", "d"],
7175 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007176 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007177 }
7178 `)
7179
7180 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007181 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007182 name := apexBundle.BaseModuleName()
7183 prefix := "TARGET_"
7184 var builder strings.Builder
7185 data.Custom(&builder, name, prefix, "", data)
7186 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007187 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 apex_manifest.pb.myapex apex_pubkey.myapex a b\n")
Sasha Smundakdcb61292022-12-08 10:41:33 -08007188 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7189 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007190}
7191
Jiyong Park7cd10e32020-01-14 09:22:18 +09007192func TestSymlinksFromApexToSystem(t *testing.T) {
7193 bp := `
7194 apex {
7195 name: "myapex",
7196 key: "myapex.key",
7197 native_shared_libs: ["mylib"],
7198 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007199 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007200 }
7201
Jiyong Park9d677202020-02-19 16:29:35 +09007202 apex {
7203 name: "myapex.updatable",
7204 key: "myapex.key",
7205 native_shared_libs: ["mylib"],
7206 java_libs: ["myjar"],
7207 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007208 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007209 }
7210
Jiyong Park7cd10e32020-01-14 09:22:18 +09007211 apex_key {
7212 name: "myapex.key",
7213 public_key: "testkey.avbpubkey",
7214 private_key: "testkey.pem",
7215 }
7216
7217 cc_library {
7218 name: "mylib",
7219 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007220 shared_libs: [
7221 "myotherlib",
7222 "myotherlib_ext",
7223 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007224 system_shared_libs: [],
7225 stl: "none",
7226 apex_available: [
7227 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007228 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007229 "//apex_available:platform",
7230 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007231 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007232 }
7233
7234 cc_library {
7235 name: "myotherlib",
7236 srcs: ["mylib.cpp"],
7237 system_shared_libs: [],
7238 stl: "none",
7239 apex_available: [
7240 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007241 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007242 "//apex_available:platform",
7243 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007244 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007245 }
7246
Jiyong Parkce243632023-02-17 18:22:25 +09007247 cc_library {
7248 name: "myotherlib_ext",
7249 srcs: ["mylib.cpp"],
7250 system_shared_libs: [],
7251 system_ext_specific: true,
7252 stl: "none",
7253 apex_available: [
7254 "myapex",
7255 "myapex.updatable",
7256 "//apex_available:platform",
7257 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007258 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007259 }
7260
Jiyong Park7cd10e32020-01-14 09:22:18 +09007261 java_library {
7262 name: "myjar",
7263 srcs: ["foo/bar/MyClass.java"],
7264 sdk_version: "none",
7265 system_modules: "none",
7266 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007267 apex_available: [
7268 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007269 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007270 "//apex_available:platform",
7271 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007272 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007273 }
7274
7275 java_library {
7276 name: "myotherjar",
7277 srcs: ["foo/bar/MyClass.java"],
7278 sdk_version: "none",
7279 system_modules: "none",
7280 apex_available: [
7281 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007282 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007283 "//apex_available:platform",
7284 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007285 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007286 }
7287 `
7288
7289 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7290 for _, f := range files {
7291 if f.path == file {
7292 if f.isLink {
7293 t.Errorf("%q is not a real file", file)
7294 }
7295 return
7296 }
7297 }
7298 t.Errorf("%q is not found", file)
7299 }
7300
Jiyong Parkce243632023-02-17 18:22:25 +09007301 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007302 for _, f := range files {
7303 if f.path == file {
7304 if !f.isLink {
7305 t.Errorf("%q is not a symlink", file)
7306 }
Jiyong Parkce243632023-02-17 18:22:25 +09007307 if f.src != target {
7308 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7309 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007310 return
7311 }
7312 }
7313 t.Errorf("%q is not found", file)
7314 }
7315
Jiyong Park9d677202020-02-19 16:29:35 +09007316 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7317 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007318 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007319 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007320 ensureRealfileExists(t, files, "javalib/myjar.jar")
7321 ensureRealfileExists(t, files, "lib64/mylib.so")
7322 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007323 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007324
Jiyong Park9d677202020-02-19 16:29:35 +09007325 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7326 ensureRealfileExists(t, files, "javalib/myjar.jar")
7327 ensureRealfileExists(t, files, "lib64/mylib.so")
7328 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007329 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007330
7331 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007332 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007333 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007334 ensureRealfileExists(t, files, "javalib/myjar.jar")
7335 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007336 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7337 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007338
7339 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7340 ensureRealfileExists(t, files, "javalib/myjar.jar")
7341 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007342 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7343 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007344}
7345
Yo Chiange8128052020-07-23 20:09:18 +08007346func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007347 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007348 apex {
7349 name: "myapex",
7350 key: "myapex.key",
7351 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007352 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007353 }
7354
7355 apex_key {
7356 name: "myapex.key",
7357 public_key: "testkey.avbpubkey",
7358 private_key: "testkey.pem",
7359 }
7360
7361 cc_library_shared {
7362 name: "mylib",
7363 srcs: ["mylib.cpp"],
7364 shared_libs: ["myotherlib"],
7365 system_shared_libs: [],
7366 stl: "none",
7367 apex_available: [
7368 "myapex",
7369 "//apex_available:platform",
7370 ],
7371 }
7372
7373 cc_prebuilt_library_shared {
7374 name: "myotherlib",
7375 srcs: ["prebuilt.so"],
7376 system_shared_libs: [],
7377 stl: "none",
7378 apex_available: [
7379 "myapex",
7380 "//apex_available:platform",
7381 ],
7382 }
7383 `)
7384
Prerana Patilb1896c82022-11-09 18:14:34 +00007385 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007386 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007387 var builder strings.Builder
7388 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7389 androidMk := builder.String()
7390 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007391 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007392 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7393 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7394 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007395 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := mylib.myapex:64 myotherlib:64 apex_manifest.pb.myapex apex_pubkey.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007396}
7397
Jooyung Han643adc42020-02-27 13:50:06 +09007398func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007399 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007400 apex {
7401 name: "myapex",
7402 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09007403 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007404 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007405 }
7406
7407 apex_key {
7408 name: "myapex.key",
7409 public_key: "testkey.avbpubkey",
7410 private_key: "testkey.pem",
7411 }
7412
7413 cc_library {
7414 name: "mylib",
7415 srcs: ["mylib.cpp"],
7416 shared_libs: ["mylib2"],
7417 system_shared_libs: [],
7418 stl: "none",
7419 apex_available: [ "myapex" ],
7420 }
7421
7422 cc_library {
7423 name: "mylib2",
7424 srcs: ["mylib.cpp"],
7425 system_shared_libs: [],
7426 stl: "none",
7427 apex_available: [ "myapex" ],
7428 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007429
7430 rust_ffi_shared {
7431 name: "libfoo.rust",
7432 crate_name: "foo",
7433 srcs: ["foo.rs"],
7434 shared_libs: ["libfoo.shared_from_rust"],
7435 prefer_rlib: true,
7436 apex_available: ["myapex"],
7437 }
7438
7439 cc_library_shared {
7440 name: "libfoo.shared_from_rust",
7441 srcs: ["mylib.cpp"],
7442 system_shared_libs: [],
7443 stl: "none",
7444 stubs: {
7445 versions: ["10", "11", "12"],
7446 },
7447 }
7448
Jooyung Han643adc42020-02-27 13:50:06 +09007449 `)
7450
7451 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
7452 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09007453 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007454 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7455 "lib64/mylib.so",
7456 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007457 "lib64/libfoo.rust.so",
7458 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7459 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007460 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007461
7462 // b/220397949
7463 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007464}
7465
Jooyung Han49f67012020-04-17 13:43:10 +09007466func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007467 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007468 apex {
7469 name: "myapex",
7470 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007471 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007472 }
7473 apex_key {
7474 name: "myapex.key",
7475 public_key: "testkey.avbpubkey",
7476 private_key: "testkey.pem",
7477 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007478 `,
7479 android.FixtureModifyConfig(func(config android.Config) {
7480 delete(config.Targets, android.Android)
7481 config.AndroidCommonTarget = android.Target{}
7482 }),
7483 )
Jooyung Han49f67012020-04-17 13:43:10 +09007484
7485 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7486 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7487 }
7488}
7489
Jiyong Parkbd159612020-02-28 15:22:21 +09007490func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007491 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007492 apex {
7493 name: "myapex",
7494 key: "myapex.key",
7495 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007496 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007497 }
7498
7499 apex_key {
7500 name: "myapex.key",
7501 public_key: "testkey.avbpubkey",
7502 private_key: "testkey.pem",
7503 }
7504
7505 android_app {
7506 name: "AppFoo",
7507 srcs: ["foo/bar/MyClass.java"],
7508 sdk_version: "none",
7509 system_modules: "none",
7510 apex_available: [ "myapex" ],
7511 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007512 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007513
Colin Crosscf371cc2020-11-13 11:48:42 -08007514 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007515 content := bundleConfigRule.Args["content"]
7516
7517 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007518 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 +09007519}
7520
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007521func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007522 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007523 apex {
7524 name: "myapex",
7525 key: "myapex.key",
7526 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007527 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007528 }
7529
7530 apex_key {
7531 name: "myapex.key",
7532 public_key: "testkey.avbpubkey",
7533 private_key: "testkey.pem",
7534 }
7535
7536 android_app_set {
7537 name: "AppSet",
7538 set: "AppSet.apks",
7539 }`)
7540 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007541 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007542 content := bundleConfigRule.Args["content"]
7543 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7544 s := mod.Rule("apexRule").Args["copy_commands"]
7545 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007546 if len(copyCmds) != 4 {
7547 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007548 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007549 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7550 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007551 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7552 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007553
7554 // Ensure that canned_fs_config has an entry for the app set zip file
7555 generateFsRule := mod.Rule("generateFsConfig")
7556 cmd := generateFsRule.RuleParams.Command
7557 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007558}
7559
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007560func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007561 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007562 apex_set {
7563 name: "myapex",
7564 filename: "foo_v2.apex",
7565 sanitized: {
7566 none: { set: "myapex.apks", },
7567 hwaddress: { set: "myapex.hwasan.apks", },
7568 },
Paul Duffin24704672021-04-06 16:09:30 +01007569 }
7570 `
7571 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007572
Paul Duffin24704672021-04-06 16:09:30 +01007573 // Check that the extractor produces the correct output file from the correct input file.
7574 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007575
Paul Duffin24704672021-04-06 16:09:30 +01007576 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7577 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007578
Paul Duffin24704672021-04-06 16:09:30 +01007579 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7580
7581 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007582 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7583 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007584
7585 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007586}
7587
Pranav Guptaeba03b02022-09-27 00:27:08 +00007588func TestApexSetApksModuleAssignment(t *testing.T) {
7589 ctx := testApex(t, `
7590 apex_set {
7591 name: "myapex",
7592 set: ":myapex_apks_file",
7593 }
7594
7595 filegroup {
7596 name: "myapex_apks_file",
7597 srcs: ["myapex.apks"],
7598 }
7599 `)
7600
7601 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7602
7603 // Check that the extractor produces the correct apks file from the input module
7604 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.apks"
7605 extractedApex := m.Output(extractorOutput)
7606
7607 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7608}
7609
Paul Duffin89f570a2021-06-16 01:42:33 +01007610func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007611 t.Helper()
7612
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007613 bp := `
7614 java_library {
7615 name: "some-updatable-apex-lib",
7616 srcs: ["a.java"],
7617 sdk_version: "current",
7618 apex_available: [
7619 "some-updatable-apex",
7620 ],
satayevabcd5972021-08-06 17:49:46 +01007621 permitted_packages: ["some.updatable.apex.lib"],
Spandan Dascc9d9422023-04-06 18:07:43 +00007622 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007623 }
7624
7625 java_library {
7626 name: "some-non-updatable-apex-lib",
7627 srcs: ["a.java"],
7628 apex_available: [
7629 "some-non-updatable-apex",
7630 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007631 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007632 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007633 }
7634
7635 bootclasspath_fragment {
7636 name: "some-non-updatable-fragment",
7637 contents: ["some-non-updatable-apex-lib"],
7638 apex_available: [
7639 "some-non-updatable-apex",
7640 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007641 hidden_api: {
7642 split_packages: ["*"],
7643 },
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007644 }
7645
7646 java_library {
7647 name: "some-platform-lib",
7648 srcs: ["a.java"],
7649 sdk_version: "current",
7650 installable: true,
7651 }
7652
7653 java_library {
7654 name: "some-art-lib",
7655 srcs: ["a.java"],
7656 sdk_version: "current",
7657 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007658 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007659 ],
7660 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007661 compile_dex: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007662 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007663 }
7664
7665 apex {
7666 name: "some-updatable-apex",
7667 key: "some-updatable-apex.key",
7668 java_libs: ["some-updatable-apex-lib"],
7669 updatable: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007670 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007671 }
7672
7673 apex {
7674 name: "some-non-updatable-apex",
7675 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007676 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007677 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007678 }
7679
7680 apex_key {
7681 name: "some-updatable-apex.key",
7682 }
7683
7684 apex_key {
7685 name: "some-non-updatable-apex.key",
7686 }
7687
7688 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007689 name: "com.android.art.debug",
7690 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007691 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007692 updatable: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007693 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007694 }
7695
Paul Duffinf23bc472021-04-27 12:42:20 +01007696 bootclasspath_fragment {
7697 name: "art-bootclasspath-fragment",
7698 image_name: "art",
7699 contents: ["some-art-lib"],
7700 apex_available: [
7701 "com.android.art.debug",
7702 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007703 hidden_api: {
7704 split_packages: ["*"],
7705 },
Paul Duffinf23bc472021-04-27 12:42:20 +01007706 }
7707
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007708 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007709 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007710 }
7711
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007712 filegroup {
7713 name: "some-updatable-apex-file_contexts",
7714 srcs: [
7715 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7716 ],
7717 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007718
7719 filegroup {
7720 name: "some-non-updatable-apex-file_contexts",
7721 srcs: [
7722 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7723 ],
7724 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007725 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007726
Paul Duffin89f570a2021-06-16 01:42:33 +01007727 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007728}
7729
Paul Duffin89f570a2021-06-16 01:42:33 +01007730func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007731 t.Helper()
7732
Paul Duffin55607122021-03-30 23:32:51 +01007733 fs := android.MockFS{
7734 "a.java": nil,
7735 "a.jar": nil,
7736 "apex_manifest.json": nil,
7737 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007738 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007739 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7740 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7741 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007742 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007743 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007744
Paul Duffin55607122021-03-30 23:32:51 +01007745 errorHandler := android.FixtureExpectsNoErrors
7746 if errmsg != "" {
7747 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007748 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007749
Paul Duffin55607122021-03-30 23:32:51 +01007750 result := android.GroupFixturePreparers(
7751 cc.PrepareForTestWithCcDefaultModules,
7752 java.PrepareForTestWithHiddenApiBuildComponents,
7753 java.PrepareForTestWithJavaDefaultModules,
7754 java.PrepareForTestWithJavaSdkLibraryFiles,
7755 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007756 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007757 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007758 android.FixtureModifyMockFS(func(fs android.MockFS) {
7759 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7760 insert := ""
7761 for _, fragment := range fragments {
7762 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7763 }
7764 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7765 platform_bootclasspath {
7766 name: "platform-bootclasspath",
7767 fragments: [
7768 %s
7769 ],
7770 }
7771 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007772 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007773 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007774 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007775 ).
7776 ExtendWithErrorHandler(errorHandler).
7777 RunTestWithBp(t, bp)
7778
7779 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007780}
7781
Paul Duffin5556c5f2022-06-09 17:32:21 +00007782func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007783 preparers := android.GroupFixturePreparers(
7784 java.PrepareForTestWithJavaDefaultModules,
7785 PrepareForTestWithApexBuildComponents,
7786 ).
7787 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7788 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7789
7790 bpBase := `
7791 apex_set {
7792 name: "com.android.myapex",
7793 installable: true,
7794 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7795 set: "myapex.apks",
7796 }
7797
7798 apex_set {
7799 name: "com.mycompany.android.myapex",
7800 apex_name: "com.android.myapex",
7801 installable: true,
7802 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7803 set: "company-myapex.apks",
7804 }
7805
7806 prebuilt_bootclasspath_fragment {
7807 name: "my-bootclasspath-fragment",
7808 apex_available: ["com.android.myapex"],
7809 %s
7810 }
7811 `
7812
7813 t.Run("java_import", func(t *testing.T) {
7814 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7815 java_import {
7816 name: "libfoo",
7817 jars: ["libfoo.jar"],
7818 apex_available: ["com.android.myapex"],
7819 }
7820 `)
7821 })
7822
7823 t.Run("java_sdk_library_import", func(t *testing.T) {
7824 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7825 java_sdk_library_import {
7826 name: "libfoo",
7827 public: {
7828 jars: ["libbar.jar"],
7829 },
7830 apex_available: ["com.android.myapex"],
7831 }
7832 `)
7833 })
7834
7835 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7836 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7837 image_name: "art",
7838 contents: ["libfoo"],
7839 `)+`
7840 java_sdk_library_import {
7841 name: "libfoo",
7842 public: {
7843 jars: ["libbar.jar"],
7844 },
7845 apex_available: ["com.android.myapex"],
7846 }
7847 `)
7848 })
7849}
7850
Paul Duffin5556c5f2022-06-09 17:32:21 +00007851func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
7852 preparers := android.GroupFixturePreparers(
7853 java.PrepareForTestWithJavaDefaultModules,
7854 PrepareForTestWithApexBuildComponents,
7855 )
7856
7857 bpBase := `
7858 apex_set {
7859 name: "com.android.myapex",
7860 installable: true,
7861 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7862 set: "myapex.apks",
7863 }
7864
7865 apex_set {
7866 name: "com.android.myapex_compressed",
7867 apex_name: "com.android.myapex",
7868 installable: true,
7869 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7870 set: "myapex_compressed.apks",
7871 }
7872
7873 prebuilt_bootclasspath_fragment {
7874 name: "my-bootclasspath-fragment",
7875 apex_available: [
7876 "com.android.myapex",
7877 "com.android.myapex_compressed",
7878 ],
7879 hidden_api: {
7880 annotation_flags: "annotation-flags.csv",
7881 metadata: "metadata.csv",
7882 index: "index.csv",
7883 signature_patterns: "signature_patterns.csv",
7884 },
7885 %s
7886 }
7887 `
7888
7889 t.Run("java_import", func(t *testing.T) {
7890 result := preparers.RunTestWithBp(t,
7891 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7892 java_import {
7893 name: "libfoo",
7894 jars: ["libfoo.jar"],
7895 apex_available: [
7896 "com.android.myapex",
7897 "com.android.myapex_compressed",
7898 ],
7899 }
7900 `)
7901
7902 module := result.Module("libfoo", "android_common_com.android.myapex")
7903 usesLibraryDep := module.(java.UsesLibraryDependency)
7904 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7905 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7906 usesLibraryDep.DexJarBuildPath().Path())
7907 })
7908
7909 t.Run("java_sdk_library_import", func(t *testing.T) {
7910 result := preparers.RunTestWithBp(t,
7911 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7912 java_sdk_library_import {
7913 name: "libfoo",
7914 public: {
7915 jars: ["libbar.jar"],
7916 },
7917 apex_available: [
7918 "com.android.myapex",
7919 "com.android.myapex_compressed",
7920 ],
7921 compile_dex: true,
7922 }
7923 `)
7924
7925 module := result.Module("libfoo", "android_common_com.android.myapex")
7926 usesLibraryDep := module.(java.UsesLibraryDependency)
7927 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7928 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7929 usesLibraryDep.DexJarBuildPath().Path())
7930 })
7931
7932 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7933 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7934 image_name: "art",
7935 contents: ["libfoo"],
7936 `)+`
7937 java_sdk_library_import {
7938 name: "libfoo",
7939 public: {
7940 jars: ["libbar.jar"],
7941 },
7942 apex_available: [
7943 "com.android.myapex",
7944 "com.android.myapex_compressed",
7945 ],
7946 compile_dex: true,
7947 }
7948 `)
7949 })
7950}
7951
Jooyung Han548640b2020-04-27 12:10:30 +09007952func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7953 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7954 apex {
7955 name: "myapex",
7956 key: "myapex.key",
7957 updatable: true,
7958 }
7959
7960 apex_key {
7961 name: "myapex.key",
7962 public_key: "testkey.avbpubkey",
7963 private_key: "testkey.pem",
7964 }
7965 `)
7966}
7967
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007968func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7969 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7970 apex {
7971 name: "myapex",
7972 key: "myapex.key",
7973 }
7974
7975 apex_key {
7976 name: "myapex.key",
7977 public_key: "testkey.avbpubkey",
7978 private_key: "testkey.pem",
7979 }
7980 `)
7981}
7982
Jooyung Handfc864c2023-03-20 18:19:07 +09007983func Test_use_vndk_as_stable_shouldnt_be_used_for_updatable_vendor_apexes(t *testing.T) {
7984 testApexError(t, `"myapex" .*: use_vndk_as_stable: updatable APEXes can't use external VNDK libs`, `
Daniel Norman69109112021-12-02 12:52:42 -08007985 apex {
7986 name: "myapex",
7987 key: "myapex.key",
7988 updatable: true,
Jooyung Handfc864c2023-03-20 18:19:07 +09007989 use_vndk_as_stable: true,
Daniel Norman69109112021-12-02 12:52:42 -08007990 soc_specific: true,
7991 }
7992
7993 apex_key {
7994 name: "myapex.key",
7995 public_key: "testkey.avbpubkey",
7996 private_key: "testkey.pem",
7997 }
7998 `)
7999}
8000
Jooyung Han02873da2023-03-22 17:41:03 +09008001func Test_use_vndk_as_stable_shouldnt_be_used_with_min_sdk_version(t *testing.T) {
8002 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported when min_sdk_version is set`, `
8003 apex {
8004 name: "myapex",
8005 key: "myapex.key",
8006 updatable: false,
8007 min_sdk_version: "29",
8008 use_vndk_as_stable: true,
8009 vendor: true,
8010 }
8011
8012 apex_key {
8013 name: "myapex.key",
8014 public_key: "testkey.avbpubkey",
8015 private_key: "testkey.pem",
8016 }
8017 `)
8018}
8019
Jooyung Handfc864c2023-03-20 18:19:07 +09008020func Test_use_vndk_as_stable_shouldnt_be_used_for_non_vendor_apexes(t *testing.T) {
8021 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported for system/system_ext APEXes`, `
8022 apex {
8023 name: "myapex",
8024 key: "myapex.key",
8025 updatable: false,
8026 use_vndk_as_stable: true,
8027 }
8028
8029 apex_key {
8030 name: "myapex.key",
8031 public_key: "testkey.avbpubkey",
8032 private_key: "testkey.pem",
8033 }
8034 `)
8035}
8036
satayevb98371c2021-06-15 16:49:50 +01008037func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
8038 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
8039 apex {
8040 name: "myapex",
8041 key: "myapex.key",
8042 systemserverclasspath_fragments: [
8043 "mysystemserverclasspathfragment",
8044 ],
8045 min_sdk_version: "29",
8046 updatable: true,
8047 }
8048
8049 apex_key {
8050 name: "myapex.key",
8051 public_key: "testkey.avbpubkey",
8052 private_key: "testkey.pem",
8053 }
8054
8055 java_library {
8056 name: "foo",
8057 srcs: ["b.java"],
8058 min_sdk_version: "29",
8059 installable: true,
8060 apex_available: [
8061 "myapex",
8062 ],
8063 }
8064
8065 systemserverclasspath_fragment {
8066 name: "mysystemserverclasspathfragment",
8067 generate_classpaths_proto: false,
8068 contents: [
8069 "foo",
8070 ],
8071 apex_available: [
8072 "myapex",
8073 ],
8074 }
satayevabcd5972021-08-06 17:49:46 +01008075 `,
8076 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8077 )
satayevb98371c2021-06-15 16:49:50 +01008078}
8079
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008080func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01008081 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
8082 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
8083 // modules to be included in the BootJars.
8084 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
8085 return android.GroupFixturePreparers(
8086 dexpreopt.FixtureSetBootJars(bootJars...),
8087 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8088 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
8089 }),
8090 )
8091 }
8092
8093 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
8094 // same value. This can result in an invalid configuration as it allows non art apex jars to be
8095 // specified in the ArtApexJars configuration.
8096 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
8097 return android.GroupFixturePreparers(
8098 dexpreopt.FixtureSetArtBootJars(bootJars...),
8099 dexpreopt.FixtureSetBootJars(bootJars...),
8100 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8101 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
8102 }),
8103 )
8104 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008105
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008106 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008107 preparer := android.GroupFixturePreparers(
8108 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
8109 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8110 )
8111 fragments := []java.ApexVariantReference{
8112 {
8113 Apex: proptools.StringPtr("com.android.art.debug"),
8114 Module: proptools.StringPtr("art-bootclasspath-fragment"),
8115 },
8116 {
8117 Apex: proptools.StringPtr("some-non-updatable-apex"),
8118 Module: proptools.StringPtr("some-non-updatable-fragment"),
8119 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008120 }
satayevabcd5972021-08-06 17:49:46 +01008121 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008122 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008123
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008124 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01008125 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
8126 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01008127 preparer := android.GroupFixturePreparers(
8128 prepareSetBootJars("com.android.art.debug:some-art-lib"),
8129 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8130 )
Paul Duffin60264a02021-04-12 20:02:36 +01008131 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008132 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008133
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008134 t.Run("updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01008135 err := `ArtApexJars expects this to be in apex "some-updatable-apex" but this is only in apexes.*"com.android.art.debug"`
Paul Duffin60264a02021-04-12 20:02:36 +01008136 // Update the dexpreopt ArtApexJars directly.
8137 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
8138 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008139 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008140
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008141 t.Run("non-updatable jar from some other apex in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01008142 err := `ArtApexJars expects this to be in apex "some-non-updatable-apex" but this is only in apexes.*"com.android.art.debug"`
Paul Duffin60264a02021-04-12 20:02:36 +01008143 // Update the dexpreopt ArtApexJars directly.
8144 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
8145 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008146 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008147
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008148 t.Run("updatable jar from some other apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01008149 err := `module "some-updatable-apex-lib" from updatable apexes \["some-updatable-apex"\] is not allowed in the framework boot image`
satayevabcd5972021-08-06 17:49:46 +01008150 preparer := android.GroupFixturePreparers(
8151 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
8152 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8153 )
Paul Duffin60264a02021-04-12 20:02:36 +01008154 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008155 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008156
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008157 t.Run("non-updatable jar from some other apex in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008158 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01008159 fragment := java.ApexVariantReference{
8160 Apex: proptools.StringPtr("some-non-updatable-apex"),
8161 Module: proptools.StringPtr("some-non-updatable-fragment"),
8162 }
8163 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008164 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008165
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008166 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008167 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008168 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8169 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008170 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008171
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008172 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008173 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008174 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8175 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008176 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008177
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008178 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01008179 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01008180 // Update the dexpreopt ArtApexJars directly.
8181 preparer := prepareSetArtJars("platform:some-platform-lib")
8182 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008183 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008184
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008185 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008186 preparer := android.GroupFixturePreparers(
8187 java.FixtureConfigureBootJars("platform:some-platform-lib"),
8188 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8189 )
8190 fragments := []java.ApexVariantReference{
8191 {
8192 Apex: proptools.StringPtr("some-non-updatable-apex"),
8193 Module: proptools.StringPtr("some-non-updatable-fragment"),
8194 },
8195 }
8196 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008197 })
Paul Duffin064b70c2020-11-02 17:32:38 +00008198}
8199
8200func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008201 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008202 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008203 fragment := java.ApexVariantReference{
8204 Apex: proptools.StringPtr("myapex"),
8205 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8206 }
8207
Paul Duffin064b70c2020-11-02 17:32:38 +00008208 testDexpreoptWithApexes(t, `
8209 prebuilt_apex {
8210 name: "myapex" ,
8211 arch: {
8212 arm64: {
8213 src: "myapex-arm64.apex",
8214 },
8215 arm: {
8216 src: "myapex-arm.apex",
8217 },
8218 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008219 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8220 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008221
Paul Duffin89f570a2021-06-16 01:42:33 +01008222 prebuilt_bootclasspath_fragment {
8223 name: "my-bootclasspath-fragment",
8224 contents: ["libfoo"],
8225 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008226 hidden_api: {
8227 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8228 metadata: "my-bootclasspath-fragment/metadata.csv",
8229 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008230 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8231 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8232 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008233 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008234 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008235
Paul Duffin89f570a2021-06-16 01:42:33 +01008236 java_import {
8237 name: "libfoo",
8238 jars: ["libfoo.jar"],
8239 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008240 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008241 }
8242 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008243 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008244}
8245
Spandan Dasf14e2542021-11-12 00:01:37 +00008246func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008247 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008248 bp += `
8249 apex_key {
8250 name: "myapex.key",
8251 public_key: "testkey.avbpubkey",
8252 private_key: "testkey.pem",
8253 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008254 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008255 "lib1/src/A.java": nil,
8256 "lib2/src/B.java": nil,
8257 "system/sepolicy/apex/myapex-file_contexts": nil,
8258 }
8259
Paul Duffin45338f02021-03-30 23:07:52 +01008260 errorHandler := android.FixtureExpectsNoErrors
8261 if errmsg != "" {
8262 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008263 }
Colin Crossae8600b2020-10-29 17:09:13 -07008264
Paul Duffin45338f02021-03-30 23:07:52 +01008265 android.GroupFixturePreparers(
8266 android.PrepareForTestWithAndroidBuildComponents,
8267 java.PrepareForTestWithJavaBuildComponents,
8268 PrepareForTestWithApexBuildComponents,
8269 android.PrepareForTestWithNeverallowRules(rules),
8270 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008271 apexBootJars := make([]string, 0, len(bootJars))
8272 for _, apexBootJar := range bootJars {
8273 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008274 }
satayevd604b212021-07-21 14:23:52 +01008275 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008276 }),
8277 fs.AddToFixture(),
8278 ).
8279 ExtendWithErrorHandler(errorHandler).
8280 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008281}
8282
8283func TestApexPermittedPackagesRules(t *testing.T) {
8284 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008285 name string
8286 expectedError string
8287 bp string
8288 bootJars []string
8289 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008290 }{
8291
8292 {
8293 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8294 expectedError: "",
8295 bp: `
8296 java_library {
8297 name: "bcp_lib1",
8298 srcs: ["lib1/src/*.java"],
8299 permitted_packages: ["foo.bar"],
8300 apex_available: ["myapex"],
8301 sdk_version: "none",
8302 system_modules: "none",
8303 }
8304 java_library {
8305 name: "nonbcp_lib2",
8306 srcs: ["lib2/src/*.java"],
8307 apex_available: ["myapex"],
8308 permitted_packages: ["a.b"],
8309 sdk_version: "none",
8310 system_modules: "none",
8311 }
8312 apex {
8313 name: "myapex",
8314 key: "myapex.key",
8315 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008316 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008317 }`,
8318 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008319 bcpPermittedPackages: map[string][]string{
8320 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008321 "foo.bar",
8322 },
8323 },
8324 },
8325 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008326 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008327 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 +01008328 bp: `
8329 java_library {
8330 name: "bcp_lib1",
8331 srcs: ["lib1/src/*.java"],
8332 apex_available: ["myapex"],
8333 permitted_packages: ["foo.bar"],
8334 sdk_version: "none",
8335 system_modules: "none",
8336 }
8337 java_library {
8338 name: "bcp_lib2",
8339 srcs: ["lib2/src/*.java"],
8340 apex_available: ["myapex"],
8341 permitted_packages: ["foo.bar", "bar.baz"],
8342 sdk_version: "none",
8343 system_modules: "none",
8344 }
8345 apex {
8346 name: "myapex",
8347 key: "myapex.key",
8348 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008349 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008350 }
8351 `,
8352 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008353 bcpPermittedPackages: map[string][]string{
8354 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008355 "foo.bar",
8356 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008357 "bcp_lib2": []string{
8358 "foo.bar",
8359 },
8360 },
8361 },
8362 {
8363 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8364 expectedError: "",
8365 bp: `
8366 java_library {
8367 name: "bcp_lib_restricted",
8368 srcs: ["lib1/src/*.java"],
8369 apex_available: ["myapex"],
8370 permitted_packages: ["foo.bar"],
8371 sdk_version: "none",
8372 min_sdk_version: "29",
8373 system_modules: "none",
8374 }
8375 java_library {
8376 name: "bcp_lib_unrestricted",
8377 srcs: ["lib2/src/*.java"],
8378 apex_available: ["myapex"],
8379 permitted_packages: ["foo.bar", "bar.baz"],
8380 sdk_version: "none",
8381 min_sdk_version: "29",
8382 system_modules: "none",
8383 }
8384 apex {
8385 name: "myapex",
8386 key: "myapex.key",
8387 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8388 updatable: true,
8389 min_sdk_version: "29",
8390 }
8391 `,
8392 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8393 bcpPermittedPackages: map[string][]string{
8394 "bcp_lib1_non_updateable": []string{
8395 "foo.bar",
8396 },
8397 // 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 +01008398 },
8399 },
8400 }
8401 for _, tc := range testcases {
8402 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008403 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8404 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008405 })
8406 }
8407}
8408
Jiyong Park62304bb2020-04-13 16:19:48 +09008409func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008410 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008411 apex {
8412 name: "myapex",
8413 key: "myapex.key",
8414 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008415 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008416 }
8417
8418 apex_key {
8419 name: "myapex.key",
8420 public_key: "testkey.avbpubkey",
8421 private_key: "testkey.pem",
8422 }
8423
8424 cc_library {
8425 name: "mylib",
8426 srcs: ["mylib.cpp"],
8427 system_shared_libs: [],
8428 stl: "none",
8429 stubs: {
8430 versions: ["1"],
8431 },
8432 apex_available: ["myapex"],
8433 }
8434
8435 cc_library {
8436 name: "myprivlib",
8437 srcs: ["mylib.cpp"],
8438 system_shared_libs: [],
8439 stl: "none",
8440 apex_available: ["myapex"],
8441 }
8442
8443
8444 cc_test {
8445 name: "mytest",
8446 gtest: false,
8447 srcs: ["mylib.cpp"],
8448 system_shared_libs: [],
8449 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008450 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008451 test_for: ["myapex"]
8452 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008453
8454 cc_library {
8455 name: "mytestlib",
8456 srcs: ["mylib.cpp"],
8457 system_shared_libs: [],
8458 shared_libs: ["mylib", "myprivlib"],
8459 stl: "none",
8460 test_for: ["myapex"],
8461 }
8462
8463 cc_benchmark {
8464 name: "mybench",
8465 srcs: ["mylib.cpp"],
8466 system_shared_libs: [],
8467 shared_libs: ["mylib", "myprivlib"],
8468 stl: "none",
8469 test_for: ["myapex"],
8470 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008471 `)
8472
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008473 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008474 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008475 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8476 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8477 }
8478
8479 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008480 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008481 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8482 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8483 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8484}
Jiyong Park46a512f2020-12-04 18:02:13 +09008485
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008486func TestIndirectTestFor(t *testing.T) {
8487 ctx := testApex(t, `
8488 apex {
8489 name: "myapex",
8490 key: "myapex.key",
8491 native_shared_libs: ["mylib", "myprivlib"],
8492 updatable: false,
8493 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008494
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008495 apex_key {
8496 name: "myapex.key",
8497 public_key: "testkey.avbpubkey",
8498 private_key: "testkey.pem",
8499 }
8500
8501 cc_library {
8502 name: "mylib",
8503 srcs: ["mylib.cpp"],
8504 system_shared_libs: [],
8505 stl: "none",
8506 stubs: {
8507 versions: ["1"],
8508 },
8509 apex_available: ["myapex"],
8510 }
8511
8512 cc_library {
8513 name: "myprivlib",
8514 srcs: ["mylib.cpp"],
8515 system_shared_libs: [],
8516 stl: "none",
8517 shared_libs: ["mylib"],
8518 apex_available: ["myapex"],
8519 }
8520
8521 cc_library {
8522 name: "mytestlib",
8523 srcs: ["mylib.cpp"],
8524 system_shared_libs: [],
8525 shared_libs: ["myprivlib"],
8526 stl: "none",
8527 test_for: ["myapex"],
8528 }
8529 `)
8530
8531 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008532 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008533 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8534 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8535 }
8536
8537 // The platform variant of mytestlib links to the platform variant of the
8538 // internal myprivlib.
8539 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8540
8541 // The platform variant of myprivlib links to the platform variant of mylib
8542 // and bypasses its stubs.
8543 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 +09008544}
8545
Martin Stjernholmec009002021-03-27 15:18:31 +00008546func TestTestForForLibInOtherApex(t *testing.T) {
8547 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8548 _ = testApex(t, `
8549 apex {
8550 name: "com.android.art",
8551 key: "myapex.key",
8552 native_shared_libs: ["mylib"],
8553 updatable: false,
8554 }
8555
8556 apex {
8557 name: "com.android.art.debug",
8558 key: "myapex.key",
8559 native_shared_libs: ["mylib", "mytestlib"],
8560 updatable: false,
8561 }
8562
8563 apex_key {
8564 name: "myapex.key",
8565 public_key: "testkey.avbpubkey",
8566 private_key: "testkey.pem",
8567 }
8568
8569 cc_library {
8570 name: "mylib",
8571 srcs: ["mylib.cpp"],
8572 system_shared_libs: [],
8573 stl: "none",
8574 stubs: {
8575 versions: ["1"],
8576 },
8577 apex_available: ["com.android.art", "com.android.art.debug"],
8578 }
8579
8580 cc_library {
8581 name: "mytestlib",
8582 srcs: ["mylib.cpp"],
8583 system_shared_libs: [],
8584 shared_libs: ["mylib"],
8585 stl: "none",
8586 apex_available: ["com.android.art.debug"],
8587 test_for: ["com.android.art"],
8588 }
8589 `,
8590 android.MockFS{
8591 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8592 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8593 }.AddToFixture())
8594}
8595
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008596// TODO(jungjw): Move this to proptools
8597func intPtr(i int) *int {
8598 return &i
8599}
8600
8601func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008602 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008603 apex_set {
8604 name: "myapex",
8605 set: "myapex.apks",
8606 filename: "foo_v2.apex",
8607 overrides: ["foo"],
8608 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008609 `,
8610 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8611 variables.Platform_sdk_version = intPtr(30)
8612 }),
8613 android.FixtureModifyConfig(func(config android.Config) {
8614 config.Targets[android.Android] = []android.Target{
8615 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8616 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8617 }
8618 }),
8619 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008620
Paul Duffin24704672021-04-06 16:09:30 +01008621 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008622
8623 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008624 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008625 actual := extractedApex.Args["abis"]
8626 expected := "ARMEABI_V7A,ARM64_V8A"
8627 if actual != expected {
8628 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8629 }
8630 actual = extractedApex.Args["sdk-version"]
8631 expected = "30"
8632 if actual != expected {
8633 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8634 }
8635
Paul Duffin6717d882021-06-15 19:09:41 +01008636 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008637 a := m.Module().(*ApexSet)
8638 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008639 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008640 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8641 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8642 }
8643}
8644
Anton Hansson805e0a52022-11-25 14:06:46 +00008645func TestApexSet_NativeBridge(t *testing.T) {
8646 ctx := testApex(t, `
8647 apex_set {
8648 name: "myapex",
8649 set: "myapex.apks",
8650 filename: "foo_v2.apex",
8651 overrides: ["foo"],
8652 }
8653 `,
8654 android.FixtureModifyConfig(func(config android.Config) {
8655 config.Targets[android.Android] = []android.Target{
8656 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8657 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8658 }
8659 }),
8660 )
8661
8662 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
8663
8664 // Check extract_apks tool parameters. No native bridge arch expected
8665 extractedApex := m.Output("extracted/myapex.apks")
8666 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8667}
8668
Jiyong Park7d95a512020-05-10 15:16:24 +09008669func TestNoStaticLinkingToStubsLib(t *testing.T) {
8670 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8671 apex {
8672 name: "myapex",
8673 key: "myapex.key",
8674 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008675 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008676 }
8677
8678 apex_key {
8679 name: "myapex.key",
8680 public_key: "testkey.avbpubkey",
8681 private_key: "testkey.pem",
8682 }
8683
8684 cc_library {
8685 name: "mylib",
8686 srcs: ["mylib.cpp"],
8687 static_libs: ["otherlib"],
8688 system_shared_libs: [],
8689 stl: "none",
8690 apex_available: [ "myapex" ],
8691 }
8692
8693 cc_library {
8694 name: "otherlib",
8695 srcs: ["mylib.cpp"],
8696 system_shared_libs: [],
8697 stl: "none",
8698 stubs: {
8699 versions: ["1", "2", "3"],
8700 },
8701 apex_available: [ "myapex" ],
8702 }
8703 `)
8704}
8705
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008706func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008707 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008708 apex {
8709 name: "myapex",
8710 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008711 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008712 custom_sign_tool: "sign_myapex",
8713 }
8714
8715 apex_key {
8716 name: "myapex.key",
8717 public_key: "testkey.avbpubkey",
8718 private_key: "testkey.pem",
8719 }
8720 `)
8721
8722 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8723 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8724 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_ext" sign_tool="sign_myapex"`)
8725}
8726
8727func TestApexKeysTxtOverrides(t *testing.T) {
8728 ctx := testApex(t, `
8729 apex {
8730 name: "myapex",
8731 key: "myapex.key",
8732 updatable: false,
8733 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008734 }
8735
8736 apex_key {
8737 name: "myapex.key",
8738 public_key: "testkey.avbpubkey",
8739 private_key: "testkey.pem",
8740 }
8741
8742 prebuilt_apex {
8743 name: "myapex",
8744 prefer: true,
8745 arch: {
8746 arm64: {
8747 src: "myapex-arm64.apex",
8748 },
8749 arm: {
8750 src: "myapex-arm.apex",
8751 },
8752 },
8753 }
8754
8755 apex_set {
8756 name: "myapex_set",
8757 set: "myapex.apks",
8758 filename: "myapex_set.apex",
8759 overrides: ["myapex"],
8760 }
8761 `)
8762
8763 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8764 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8765 ensureContains(t, content, `name="myapex_set.apex" public_key="PRESIGNED" private_key="PRESIGNED" container_certificate="PRESIGNED" container_private_key="PRESIGNED" partition="system"`)
Jiyong Park03a7f3e2020-06-18 19:34:42 +09008766 ensureContains(t, content, `name="myapex.apex" public_key="PRESIGNED" private_key="PRESIGNED" container_certificate="PRESIGNED" container_private_key="PRESIGNED" partition="system"`)
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008767}
8768
Jooyung Han938b5932020-06-20 12:47:47 +09008769func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008770 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008771 apex {
8772 name: "myapex",
8773 key: "myapex.key",
8774 apps: ["app"],
8775 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008776 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008777 }
8778
8779 apex_key {
8780 name: "myapex.key",
8781 public_key: "testkey.avbpubkey",
8782 private_key: "testkey.pem",
8783 }
8784
8785 android_app {
8786 name: "app",
8787 srcs: ["foo/bar/MyClass.java"],
8788 package_name: "foo",
8789 sdk_version: "none",
8790 system_modules: "none",
8791 apex_available: [ "myapex" ],
8792 }
8793 `, withFiles(map[string][]byte{
8794 "sub/Android.bp": []byte(`
8795 override_apex {
8796 name: "override_myapex",
8797 base: "myapex",
8798 apps: ["override_app"],
8799 allowed_files: ":allowed",
8800 }
8801 // Overridable "path" property should be referenced indirectly
8802 filegroup {
8803 name: "allowed",
8804 srcs: ["allowed.txt"],
8805 }
8806 override_android_app {
8807 name: "override_app",
8808 base: "app",
8809 package_name: "bar",
8810 }
8811 `),
8812 }))
8813
8814 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8815 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8816 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8817 }
8818
8819 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8820 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8821 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8822 }
8823}
8824
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008825func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008826 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008827 apex {
8828 name: "myapex",
8829 key: "myapex.key",
8830 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008831 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008832 }
8833
8834 apex_key {
8835 name: "myapex.key",
8836 public_key: "testkey.avbpubkey",
8837 private_key: "testkey.pem",
8838 }
8839
8840 cc_library {
8841 name: "mylib",
8842 srcs: ["mylib.cpp"],
8843 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008844 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008845 },
8846 apex_available: ["myapex"],
8847 }
8848
8849 cc_prebuilt_library_shared {
8850 name: "mylib",
8851 prefer: false,
8852 srcs: ["prebuilt.so"],
8853 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008854 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008855 },
8856 apex_available: ["myapex"],
8857 }
8858 `)
8859}
8860
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008861func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008862 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008863 apex {
8864 name: "myapex",
8865 key: "myapex.key",
8866 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008867 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008868 }
8869 apex_key {
8870 name: "myapex.key",
8871 public_key: "testkey.avbpubkey",
8872 private_key: "testkey.pem",
8873 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008874 `,
8875 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8876 variables.CompressedApex = proptools.BoolPtr(true)
8877 }),
8878 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008879
8880 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8881 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8882
8883 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8884 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8885
8886 // Make sure output of bundle is .capex
8887 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8888 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8889
8890 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008891 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008892 var builder strings.Builder
8893 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8894 androidMk := builder.String()
8895 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8896}
8897
Martin Stjernholm2856c662020-12-02 15:03:42 +00008898func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008899 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008900 apex {
8901 name: "myapex",
8902 key: "myapex.key",
8903 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008904 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008905 }
8906
8907 apex_key {
8908 name: "myapex.key",
8909 public_key: "testkey.avbpubkey",
8910 private_key: "testkey.pem",
8911 }
8912
8913 cc_library {
8914 name: "mylib",
8915 srcs: ["mylib.cpp"],
8916 apex_available: ["myapex"],
8917 shared_libs: ["otherlib"],
8918 system_shared_libs: [],
8919 }
8920
8921 cc_library {
8922 name: "otherlib",
8923 srcs: ["mylib.cpp"],
8924 stubs: {
8925 versions: ["current"],
8926 },
8927 }
8928
8929 cc_prebuilt_library_shared {
8930 name: "otherlib",
8931 prefer: true,
8932 srcs: ["prebuilt.so"],
8933 stubs: {
8934 versions: ["current"],
8935 },
8936 }
8937 `)
8938
8939 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008940 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008941 var builder strings.Builder
8942 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8943 androidMk := builder.String()
8944
8945 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8946 // a thing there.
Diwas Sharmabb9202e2023-01-26 18:42:21 +00008947 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++:64 mylib.myapex:64 apex_manifest.pb.myapex apex_pubkey.myapex otherlib\n")
Martin Stjernholm2856c662020-12-02 15:03:42 +00008948}
8949
Jiyong Parke3867542020-12-03 17:28:25 +09008950func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008951 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008952 apex {
8953 name: "myapex",
8954 key: "myapex.key",
8955 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008956 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008957 }
8958
8959 apex_key {
8960 name: "myapex.key",
8961 public_key: "testkey.avbpubkey",
8962 private_key: "testkey.pem",
8963 }
8964
8965 cc_library {
8966 name: "mylib",
8967 srcs: ["mylib.cpp"],
8968 system_shared_libs: [],
8969 stl: "none",
8970 apex_available: ["myapex"],
8971 shared_libs: ["mylib2"],
8972 target: {
8973 apex: {
8974 exclude_shared_libs: ["mylib2"],
8975 },
8976 },
8977 }
8978
8979 cc_library {
8980 name: "mylib2",
8981 srcs: ["mylib.cpp"],
8982 system_shared_libs: [],
8983 stl: "none",
8984 }
8985 `)
8986
8987 // Check if mylib is linked to mylib2 for the non-apex target
8988 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8989 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8990
8991 // Make sure that the link doesn't occur for the apex target
8992 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8993 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8994
8995 // It shouldn't appear in the copy cmd as well.
8996 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8997 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8998}
8999
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009000func TestPrebuiltStubLibDep(t *testing.T) {
9001 bpBase := `
9002 apex {
9003 name: "myapex",
9004 key: "myapex.key",
9005 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009006 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009007 }
9008 apex_key {
9009 name: "myapex.key",
9010 public_key: "testkey.avbpubkey",
9011 private_key: "testkey.pem",
9012 }
9013 cc_library {
9014 name: "mylib",
9015 srcs: ["mylib.cpp"],
9016 apex_available: ["myapex"],
9017 shared_libs: ["stublib"],
9018 system_shared_libs: [],
9019 }
9020 apex {
9021 name: "otherapex",
9022 enabled: %s,
9023 key: "myapex.key",
9024 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00009025 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009026 }
9027 `
9028
9029 stublibSourceBp := `
9030 cc_library {
9031 name: "stublib",
9032 srcs: ["mylib.cpp"],
9033 apex_available: ["otherapex"],
9034 system_shared_libs: [],
9035 stl: "none",
9036 stubs: {
9037 versions: ["1"],
9038 },
9039 }
9040 `
9041
9042 stublibPrebuiltBp := `
9043 cc_prebuilt_library_shared {
9044 name: "stublib",
9045 srcs: ["prebuilt.so"],
9046 apex_available: ["otherapex"],
9047 stubs: {
9048 versions: ["1"],
9049 },
9050 %s
9051 }
9052 `
9053
9054 tests := []struct {
9055 name string
9056 stublibBp string
9057 usePrebuilt bool
9058 modNames []string // Modules to collect AndroidMkEntries for
9059 otherApexEnabled []string
9060 }{
9061 {
9062 name: "only_source",
9063 stublibBp: stublibSourceBp,
9064 usePrebuilt: false,
9065 modNames: []string{"stublib"},
9066 otherApexEnabled: []string{"true", "false"},
9067 },
9068 {
9069 name: "source_preferred",
9070 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9071 usePrebuilt: false,
9072 modNames: []string{"stublib", "prebuilt_stublib"},
9073 otherApexEnabled: []string{"true", "false"},
9074 },
9075 {
9076 name: "prebuilt_preferred",
9077 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9078 usePrebuilt: true,
9079 modNames: []string{"stublib", "prebuilt_stublib"},
9080 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9081 },
9082 {
9083 name: "only_prebuilt",
9084 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9085 usePrebuilt: true,
9086 modNames: []string{"stublib"},
9087 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9088 },
9089 }
9090
9091 for _, test := range tests {
9092 t.Run(test.name, func(t *testing.T) {
9093 for _, otherApexEnabled := range test.otherApexEnabled {
9094 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009095 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009096
9097 type modAndMkEntries struct {
9098 mod *cc.Module
9099 mkEntries android.AndroidMkEntries
9100 }
9101 entries := []*modAndMkEntries{}
9102
9103 // Gather shared lib modules that are installable
9104 for _, modName := range test.modNames {
9105 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9106 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9107 continue
9108 }
9109 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08009110 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009111 continue
9112 }
Colin Crossaa255532020-07-03 13:18:24 -07009113 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009114 if ent.Disabled {
9115 continue
9116 }
9117 entries = append(entries, &modAndMkEntries{
9118 mod: mod,
9119 mkEntries: ent,
9120 })
9121 }
9122 }
9123 }
9124
9125 var entry *modAndMkEntries = nil
9126 for _, ent := range entries {
9127 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9128 if entry != nil {
9129 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9130 } else {
9131 entry = ent
9132 }
9133 }
9134 }
9135
9136 if entry == nil {
9137 t.Errorf("AndroidMk entry for \"stublib\" missing")
9138 } else {
9139 isPrebuilt := entry.mod.Prebuilt() != nil
9140 if isPrebuilt != test.usePrebuilt {
9141 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9142 }
9143 if !entry.mod.IsStubs() {
9144 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9145 }
9146 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9147 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9148 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009149 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009150 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009151 if !android.InList(expected, cflags) {
9152 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9153 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009154 }
9155 })
9156 }
9157 })
9158 }
9159}
9160
Martin Stjernholmdf298b32021-05-21 20:57:29 +01009161func TestHostApexInHostOnlyBuild(t *testing.T) {
9162 testApex(t, `
9163 apex {
9164 name: "myapex",
9165 host_supported: true,
9166 key: "myapex.key",
9167 updatable: false,
9168 payload_type: "zip",
9169 }
9170 apex_key {
9171 name: "myapex.key",
9172 public_key: "testkey.avbpubkey",
9173 private_key: "testkey.pem",
9174 }
9175 `,
9176 android.FixtureModifyConfig(func(config android.Config) {
9177 // We may not have device targets in all builds, e.g. in
9178 // prebuilts/build-tools/build-prebuilts.sh
9179 config.Targets[android.Android] = []android.Target{}
9180 }))
9181}
9182
Colin Crossc33e5212021-05-25 18:16:02 -07009183func TestApexJavaCoverage(t *testing.T) {
9184 bp := `
9185 apex {
9186 name: "myapex",
9187 key: "myapex.key",
9188 java_libs: ["mylib"],
9189 bootclasspath_fragments: ["mybootclasspathfragment"],
9190 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9191 updatable: false,
9192 }
9193
9194 apex_key {
9195 name: "myapex.key",
9196 public_key: "testkey.avbpubkey",
9197 private_key: "testkey.pem",
9198 }
9199
9200 java_library {
9201 name: "mylib",
9202 srcs: ["mylib.java"],
9203 apex_available: ["myapex"],
9204 compile_dex: true,
9205 }
9206
9207 bootclasspath_fragment {
9208 name: "mybootclasspathfragment",
9209 contents: ["mybootclasspathlib"],
9210 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009211 hidden_api: {
9212 split_packages: ["*"],
9213 },
Colin Crossc33e5212021-05-25 18:16:02 -07009214 }
9215
9216 java_library {
9217 name: "mybootclasspathlib",
9218 srcs: ["mybootclasspathlib.java"],
9219 apex_available: ["myapex"],
9220 compile_dex: true,
9221 }
9222
9223 systemserverclasspath_fragment {
9224 name: "mysystemserverclasspathfragment",
9225 contents: ["mysystemserverclasspathlib"],
9226 apex_available: ["myapex"],
9227 }
9228
9229 java_library {
9230 name: "mysystemserverclasspathlib",
9231 srcs: ["mysystemserverclasspathlib.java"],
9232 apex_available: ["myapex"],
9233 compile_dex: true,
9234 }
9235 `
9236
9237 result := android.GroupFixturePreparers(
9238 PrepareForTestWithApexBuildComponents,
9239 prepareForTestWithMyapex,
9240 java.PrepareForTestWithJavaDefaultModules,
9241 android.PrepareForTestWithAndroidBuildComponents,
9242 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009243 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9244 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009245 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009246 ).RunTest(t)
9247
9248 // Make sure jacoco ran on both mylib and mybootclasspathlib
9249 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9250 t.Errorf("Failed to find jacoco rule for mylib")
9251 }
9252 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9253 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9254 }
9255 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9256 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9257 }
9258}
9259
Jiyong Park192600a2021-08-03 07:52:17 +00009260func TestProhibitStaticExecutable(t *testing.T) {
9261 testApexError(t, `executable mybin is static`, `
9262 apex {
9263 name: "myapex",
9264 key: "myapex.key",
9265 binaries: ["mybin"],
9266 min_sdk_version: "29",
9267 }
9268
9269 apex_key {
9270 name: "myapex.key",
9271 public_key: "testkey.avbpubkey",
9272 private_key: "testkey.pem",
9273 }
9274
9275 cc_binary {
9276 name: "mybin",
9277 srcs: ["mylib.cpp"],
9278 relative_install_path: "foo/bar",
9279 static_executable: true,
9280 system_shared_libs: [],
9281 stl: "none",
9282 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009283 min_sdk_version: "29",
9284 }
9285 `)
9286
9287 testApexError(t, `executable mybin.rust is static`, `
9288 apex {
9289 name: "myapex",
9290 key: "myapex.key",
9291 binaries: ["mybin.rust"],
9292 min_sdk_version: "29",
9293 }
9294
9295 apex_key {
9296 name: "myapex.key",
9297 public_key: "testkey.avbpubkey",
9298 private_key: "testkey.pem",
9299 }
9300
9301 rust_binary {
9302 name: "mybin.rust",
9303 srcs: ["foo.rs"],
9304 static_executable: true,
9305 apex_available: ["myapex"],
9306 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009307 }
9308 `)
9309}
9310
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009311func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9312 ctx := testApex(t, `
9313 apex {
9314 name: "myapex",
9315 key: "myapex.key",
9316 updatable: false,
9317 java_libs: ["foo"],
9318 }
9319
9320 apex_key {
9321 name: "myapex.key",
9322 public_key: "testkey.avbpubkey",
9323 private_key: "testkey.pem",
9324 }
9325
9326 java_library {
9327 name: "foo",
9328 srcs: ["foo.java"],
9329 apex_available: ["myapex"],
9330 installable: true,
9331 }
9332 `,
9333 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9334 )
9335
9336 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9337 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9338 var builder strings.Builder
9339 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9340 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009341 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.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 +00009342}
9343
9344func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9345 ctx := testApex(t, `
9346 prebuilt_apex {
9347 name: "myapex",
9348 arch: {
9349 arm64: {
9350 src: "myapex-arm64.apex",
9351 },
9352 arm: {
9353 src: "myapex-arm.apex",
9354 },
9355 },
9356 exported_java_libs: ["foo"],
9357 }
9358
9359 java_import {
9360 name: "foo",
9361 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009362 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009363 }
9364 `,
9365 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9366 )
9367
9368 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9369 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9370 mainModuleEntries := entriesList[0]
9371 android.AssertArrayString(t,
9372 "LOCAL_REQUIRED_MODULES",
9373 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9374 []string{
9375 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9376 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9377 })
9378}
9379
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009380func TestAndroidMk_RequiredModules(t *testing.T) {
9381 ctx := testApex(t, `
9382 apex {
9383 name: "myapex",
9384 key: "myapex.key",
9385 updatable: false,
9386 java_libs: ["foo"],
9387 required: ["otherapex"],
9388 }
9389
9390 apex {
9391 name: "otherapex",
9392 key: "myapex.key",
9393 updatable: false,
9394 java_libs: ["foo"],
9395 required: ["otherapex"],
9396 }
9397
9398 apex_key {
9399 name: "myapex.key",
9400 public_key: "testkey.avbpubkey",
9401 private_key: "testkey.pem",
9402 }
9403
9404 java_library {
9405 name: "foo",
9406 srcs: ["foo.java"],
9407 apex_available: ["myapex", "otherapex"],
9408 installable: true,
9409 }
9410 `)
9411
9412 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9413 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9414 var builder strings.Builder
9415 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9416 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009417 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009418}
9419
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009420func TestAndroidMk_RequiredDeps(t *testing.T) {
9421 ctx := testApex(t, `
9422 apex {
9423 name: "myapex",
9424 key: "myapex.key",
9425 updatable: false,
9426 }
9427
9428 apex_key {
9429 name: "myapex.key",
9430 public_key: "testkey.avbpubkey",
9431 private_key: "testkey.pem",
9432 }
9433 `)
9434
9435 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009436 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009437 data := android.AndroidMkDataForTest(t, ctx, bundle)
9438 var builder strings.Builder
9439 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9440 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009441 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009442
9443 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009444 flattenedBundle.makeModulesToInstall = append(flattenedBundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009445 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
9446 var flattenedBuilder strings.Builder
9447 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
9448 flattenedAndroidMk := flattenedBuilder.String()
Sasha Smundakdcb61292022-12-08 10:41:33 -08009449 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex.flattened apex_pubkey.myapex.flattened foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009450}
9451
Jooyung Hana6d36672022-02-24 13:58:07 +09009452func TestApexOutputFileProducer(t *testing.T) {
9453 for _, tc := range []struct {
9454 name string
9455 ref string
9456 expected_data []string
9457 }{
9458 {
9459 name: "test_using_output",
9460 ref: ":myapex",
9461 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
9462 },
9463 {
9464 name: "test_using_apex",
9465 ref: ":myapex{.apex}",
9466 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
9467 },
9468 } {
9469 t.Run(tc.name, func(t *testing.T) {
9470 ctx := testApex(t, `
9471 apex {
9472 name: "myapex",
9473 key: "myapex.key",
9474 compressible: true,
9475 updatable: false,
9476 }
9477
9478 apex_key {
9479 name: "myapex.key",
9480 public_key: "testkey.avbpubkey",
9481 private_key: "testkey.pem",
9482 }
9483
9484 java_test {
9485 name: "`+tc.name+`",
9486 srcs: ["a.java"],
9487 data: ["`+tc.ref+`"],
9488 }
9489 `,
9490 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9491 variables.CompressedApex = proptools.BoolPtr(true)
9492 }))
9493 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9494 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9495 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9496 })
9497 }
9498}
9499
satayev758968a2021-12-06 11:42:40 +00009500func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9501 preparer := android.GroupFixturePreparers(
9502 PrepareForTestWithApexBuildComponents,
9503 prepareForTestWithMyapex,
9504 java.PrepareForTestWithJavaSdkLibraryFiles,
9505 java.PrepareForTestWithJavaDefaultModules,
9506 android.PrepareForTestWithAndroidBuildComponents,
9507 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9508 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9509 )
9510
9511 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9512 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9513 preparer.RunTestWithBp(t, `
9514 apex {
9515 name: "myapex",
9516 key: "myapex.key",
9517 bootclasspath_fragments: ["mybootclasspathfragment"],
9518 min_sdk_version: "30",
9519 updatable: false,
9520 }
9521
9522 apex_key {
9523 name: "myapex.key",
9524 public_key: "testkey.avbpubkey",
9525 private_key: "testkey.pem",
9526 }
9527
9528 bootclasspath_fragment {
9529 name: "mybootclasspathfragment",
9530 contents: ["mybootclasspathlib"],
9531 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009532 hidden_api: {
9533 split_packages: ["*"],
9534 },
satayev758968a2021-12-06 11:42:40 +00009535 }
9536
9537 java_sdk_library {
9538 name: "mybootclasspathlib",
9539 srcs: ["mybootclasspathlib.java"],
9540 apex_available: ["myapex"],
9541 compile_dex: true,
9542 unsafe_ignore_missing_latest_api: true,
9543 min_sdk_version: "31",
9544 static_libs: ["util"],
9545 }
9546
9547 java_library {
9548 name: "util",
9549 srcs: ["a.java"],
9550 apex_available: ["myapex"],
9551 min_sdk_version: "31",
9552 static_libs: ["another_util"],
9553 }
9554
9555 java_library {
9556 name: "another_util",
9557 srcs: ["a.java"],
9558 min_sdk_version: "31",
9559 apex_available: ["myapex"],
9560 }
9561 `)
9562 })
9563
9564 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9565 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9566 preparer.RunTestWithBp(t, `
9567 apex {
9568 name: "myapex",
9569 key: "myapex.key",
9570 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9571 min_sdk_version: "30",
9572 updatable: false,
9573 }
9574
9575 apex_key {
9576 name: "myapex.key",
9577 public_key: "testkey.avbpubkey",
9578 private_key: "testkey.pem",
9579 }
9580
9581 systemserverclasspath_fragment {
9582 name: "mysystemserverclasspathfragment",
9583 contents: ["mysystemserverclasspathlib"],
9584 apex_available: ["myapex"],
9585 }
9586
9587 java_sdk_library {
9588 name: "mysystemserverclasspathlib",
9589 srcs: ["mysystemserverclasspathlib.java"],
9590 apex_available: ["myapex"],
9591 compile_dex: true,
9592 min_sdk_version: "32",
9593 unsafe_ignore_missing_latest_api: true,
9594 static_libs: ["util"],
9595 }
9596
9597 java_library {
9598 name: "util",
9599 srcs: ["a.java"],
9600 apex_available: ["myapex"],
9601 min_sdk_version: "31",
9602 static_libs: ["another_util"],
9603 }
9604
9605 java_library {
9606 name: "another_util",
9607 srcs: ["a.java"],
9608 min_sdk_version: "31",
9609 apex_available: ["myapex"],
9610 }
9611 `)
9612 })
9613
9614 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9615 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9616 RunTestWithBp(t, `
9617 apex {
9618 name: "myapex",
9619 key: "myapex.key",
9620 bootclasspath_fragments: ["mybootclasspathfragment"],
9621 min_sdk_version: "30",
9622 updatable: false,
9623 }
9624
9625 apex_key {
9626 name: "myapex.key",
9627 public_key: "testkey.avbpubkey",
9628 private_key: "testkey.pem",
9629 }
9630
9631 bootclasspath_fragment {
9632 name: "mybootclasspathfragment",
9633 contents: ["mybootclasspathlib"],
9634 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009635 hidden_api: {
9636 split_packages: ["*"],
9637 },
satayev758968a2021-12-06 11:42:40 +00009638 }
9639
9640 java_sdk_library {
9641 name: "mybootclasspathlib",
9642 srcs: ["mybootclasspathlib.java"],
9643 apex_available: ["myapex"],
9644 compile_dex: true,
9645 unsafe_ignore_missing_latest_api: true,
9646 }
9647 `)
9648 })
9649
9650 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9651 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9652 RunTestWithBp(t, `
9653 apex {
9654 name: "myapex",
9655 key: "myapex.key",
9656 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9657 min_sdk_version: "30",
9658 updatable: false,
9659 }
9660
9661 apex_key {
9662 name: "myapex.key",
9663 public_key: "testkey.avbpubkey",
9664 private_key: "testkey.pem",
9665 }
9666
9667 systemserverclasspath_fragment {
9668 name: "mysystemserverclasspathfragment",
9669 contents: ["mysystemserverclasspathlib"],
9670 apex_available: ["myapex"],
9671 }
9672
9673 java_sdk_library {
9674 name: "mysystemserverclasspathlib",
9675 srcs: ["mysystemserverclasspathlib.java"],
9676 apex_available: ["myapex"],
9677 compile_dex: true,
9678 unsafe_ignore_missing_latest_api: true,
9679 }
9680 `)
9681 })
9682}
9683
Jiakai Zhang6decef92022-01-12 17:56:19 +00009684// Verifies that the APEX depends on all the Make modules in the list.
9685func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9686 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9687 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009688 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009689 }
9690}
9691
9692// Verifies that the APEX does not depend on any of the Make modules in the list.
9693func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9694 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9695 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009696 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009697 }
9698}
9699
Cole Faust1021ccd2023-02-26 21:15:25 -08009700// TODO(b/193460475): Re-enable this test
9701//func TestApexStrictUpdtabilityLint(t *testing.T) {
9702// bpTemplate := `
9703// apex {
9704// name: "myapex",
9705// key: "myapex.key",
9706// java_libs: ["myjavalib"],
9707// updatable: %v,
9708// min_sdk_version: "29",
9709// }
9710// apex_key {
9711// name: "myapex.key",
9712// }
9713// java_library {
9714// name: "myjavalib",
9715// srcs: ["MyClass.java"],
9716// apex_available: [ "myapex" ],
9717// lint: {
9718// strict_updatability_linting: %v,
9719// },
9720// sdk_version: "current",
9721// min_sdk_version: "29",
9722// }
9723// `
9724// fs := android.MockFS{
9725// "lint-baseline.xml": nil,
9726// }
9727//
9728// testCases := []struct {
9729// testCaseName string
9730// apexUpdatable bool
9731// javaStrictUpdtabilityLint bool
9732// lintFileExists bool
9733// disallowedFlagExpected bool
9734// }{
9735// {
9736// testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9737// apexUpdatable: true,
9738// javaStrictUpdtabilityLint: true,
9739// lintFileExists: false,
9740// disallowedFlagExpected: false,
9741// },
9742// {
9743// testCaseName: "non-updatable apex respects strict_updatability of javalib",
9744// apexUpdatable: false,
9745// javaStrictUpdtabilityLint: false,
9746// lintFileExists: true,
9747// disallowedFlagExpected: false,
9748// },
9749// {
9750// testCaseName: "non-updatable apex respects strict updatability of javalib",
9751// apexUpdatable: false,
9752// javaStrictUpdtabilityLint: true,
9753// lintFileExists: true,
9754// disallowedFlagExpected: true,
9755// },
9756// {
9757// testCaseName: "updatable apex sets strict updatability of javalib to true",
9758// apexUpdatable: true,
9759// javaStrictUpdtabilityLint: false, // will be set to true by mutator
9760// lintFileExists: true,
9761// disallowedFlagExpected: true,
9762// },
9763// }
9764//
9765// for _, testCase := range testCases {
9766// bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9767// fixtures := []android.FixturePreparer{}
9768// if testCase.lintFileExists {
9769// fixtures = append(fixtures, fs.AddToFixture())
9770// }
9771//
9772// result := testApex(t, bp, fixtures...)
9773// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9774// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9775// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9776//
9777// if disallowedFlagActual != testCase.disallowedFlagExpected {
9778// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9779// }
9780// }
9781//}
9782//
9783//func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9784// bp := `
9785// apex {
9786// name: "myapex",
9787// key: "myapex.key",
9788// java_libs: ["myjavalib"],
9789// updatable: true,
9790// min_sdk_version: "29",
9791// }
9792// apex_key {
9793// name: "myapex.key",
9794// }
9795// java_library {
9796// name: "myjavalib",
9797// srcs: ["MyClass.java"],
9798// apex_available: [ "myapex" ],
9799// sdk_version: "current",
9800// min_sdk_version: "29",
9801// }
9802// `
9803//
9804// testCases := []struct {
9805// testCaseName string
9806// moduleDirectory string
9807// disallowedFlagExpected bool
9808// }{
9809// {
9810// testCaseName: "lintable module defined outside libcore",
9811// moduleDirectory: "",
9812// disallowedFlagExpected: true,
9813// },
9814// {
9815// testCaseName: "lintable module defined in libcore root directory",
9816// moduleDirectory: "libcore/",
9817// disallowedFlagExpected: false,
9818// },
9819// {
9820// testCaseName: "lintable module defined in libcore child directory",
9821// moduleDirectory: "libcore/childdir/",
9822// disallowedFlagExpected: true,
9823// },
9824// }
9825//
9826// for _, testCase := range testCases {
9827// lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9828// bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9829// result := testApex(t, "", lintFileCreator, bpFileCreator)
9830// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9831// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9832// cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9833// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9834//
9835// if disallowedFlagActual != testCase.disallowedFlagExpected {
9836// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9837// }
9838// }
9839//}
9840//
9841//// checks transtive deps of an apex coming from bootclasspath_fragment
9842//func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9843// bp := `
9844// apex {
9845// name: "myapex",
9846// key: "myapex.key",
9847// bootclasspath_fragments: ["mybootclasspathfragment"],
9848// updatable: true,
9849// min_sdk_version: "29",
9850// }
9851// apex_key {
9852// name: "myapex.key",
9853// }
9854// bootclasspath_fragment {
9855// name: "mybootclasspathfragment",
9856// contents: ["myjavalib"],
9857// apex_available: ["myapex"],
9858// hidden_api: {
9859// split_packages: ["*"],
9860// },
9861// }
9862// java_library {
9863// name: "myjavalib",
9864// srcs: ["MyClass.java"],
9865// apex_available: [ "myapex" ],
9866// sdk_version: "current",
9867// min_sdk_version: "29",
9868// compile_dex: true,
9869// }
9870// `
9871// fs := android.MockFS{
9872// "lint-baseline.xml": nil,
9873// }
9874//
9875// result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9876// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9877// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9878// if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9879// t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9880// }
9881//}
Spandan Das66773252022-01-15 00:23:18 +00009882
Spandan Das42e89502022-05-06 22:12:55 +00009883// updatable apexes should propagate updatable=true to its apps
9884func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9885 bp := `
9886 apex {
9887 name: "myapex",
9888 key: "myapex.key",
9889 updatable: %v,
9890 apps: [
9891 "myapp",
9892 ],
9893 min_sdk_version: "30",
9894 }
9895 apex_key {
9896 name: "myapex.key",
9897 }
9898 android_app {
9899 name: "myapp",
9900 updatable: %v,
9901 apex_available: [
9902 "myapex",
9903 ],
9904 sdk_version: "current",
9905 min_sdk_version: "30",
9906 }
9907 `
9908 testCases := []struct {
9909 name string
9910 apex_is_updatable_bp bool
9911 app_is_updatable_bp bool
9912 app_is_updatable_expected bool
9913 }{
9914 {
9915 name: "Non-updatable apex respects updatable property of non-updatable app",
9916 apex_is_updatable_bp: false,
9917 app_is_updatable_bp: false,
9918 app_is_updatable_expected: false,
9919 },
9920 {
9921 name: "Non-updatable apex respects updatable property of updatable app",
9922 apex_is_updatable_bp: false,
9923 app_is_updatable_bp: true,
9924 app_is_updatable_expected: true,
9925 },
9926 {
9927 name: "Updatable apex respects updatable property of updatable app",
9928 apex_is_updatable_bp: true,
9929 app_is_updatable_bp: true,
9930 app_is_updatable_expected: true,
9931 },
9932 {
9933 name: "Updatable apex sets updatable=true on non-updatable app",
9934 apex_is_updatable_bp: true,
9935 app_is_updatable_bp: false,
9936 app_is_updatable_expected: true,
9937 },
9938 }
9939 for _, testCase := range testCases {
9940 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
9941 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
9942 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
9943 }
9944}
9945
Kiyoung Kim487689e2022-07-26 09:48:22 +09009946func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
9947 bp := `
9948 apex {
9949 name: "myapex",
9950 key: "myapex.key",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009951 native_shared_libs: ["libbaz"],
9952 binaries: ["binfoo"],
Kiyoung Kim487689e2022-07-26 09:48:22 +09009953 min_sdk_version: "29",
9954 }
9955 apex_key {
9956 name: "myapex.key",
9957 }
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009958 cc_binary {
9959 name: "binfoo",
9960 shared_libs: ["libbar", "libbaz", "libqux",],
Kiyoung Kim487689e2022-07-26 09:48:22 +09009961 apex_available: ["myapex"],
9962 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009963 recovery_available: false,
9964 }
9965 cc_library {
9966 name: "libbar",
9967 srcs: ["libbar.cc"],
9968 stubs: {
9969 symbol_file: "libbar.map.txt",
9970 versions: [
9971 "29",
9972 ],
9973 },
9974 }
9975 cc_library {
9976 name: "libbaz",
9977 srcs: ["libbaz.cc"],
9978 apex_available: ["myapex"],
9979 min_sdk_version: "29",
9980 stubs: {
9981 symbol_file: "libbaz.map.txt",
9982 versions: [
9983 "29",
9984 ],
9985 },
Kiyoung Kim487689e2022-07-26 09:48:22 +09009986 }
9987 cc_api_library {
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009988 name: "libbar",
9989 src: "libbar_stub.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +09009990 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009991 variants: ["apex.29"],
9992 }
9993 cc_api_variant {
9994 name: "libbar",
9995 variant: "apex",
9996 version: "29",
9997 src: "libbar_apex_29.so",
9998 }
9999 cc_api_library {
10000 name: "libbaz",
10001 src: "libbaz_stub.so",
10002 min_sdk_version: "29",
10003 variants: ["apex.29"],
10004 }
10005 cc_api_variant {
10006 name: "libbaz",
10007 variant: "apex",
10008 version: "29",
10009 src: "libbaz_apex_29.so",
10010 }
10011 cc_api_library {
10012 name: "libqux",
10013 src: "libqux_stub.so",
10014 min_sdk_version: "29",
10015 variants: ["apex.29"],
10016 }
10017 cc_api_variant {
10018 name: "libqux",
10019 variant: "apex",
10020 version: "29",
10021 src: "libqux_apex_29.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010022 }
10023 api_imports {
10024 name: "api_imports",
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010025 apex_shared_libs: [
10026 "libbar",
10027 "libbaz",
10028 "libqux",
Kiyoung Kim487689e2022-07-26 09:48:22 +090010029 ],
Kiyoung Kim487689e2022-07-26 09:48:22 +090010030 }
10031 `
10032 result := testApex(t, bp)
10033
10034 hasDep := func(m android.Module, wantDep android.Module) bool {
10035 t.Helper()
10036 var found bool
10037 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10038 if dep == wantDep {
10039 found = true
10040 }
10041 })
10042 return found
10043 }
10044
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010045 // Library defines stubs and cc_api_library should be used with cc_api_library
10046 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Module()
10047 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10048 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
Kiyoung Kim487689e2022-07-26 09:48:22 +090010049
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010050 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10051 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
Kiyoung Kim487689e2022-07-26 09:48:22 +090010052
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010053 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Rule("ld").Args["libFlags"]
10054 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10055 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10056 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
10057
10058 // Library defined in the same APEX should be linked with original definition instead of cc_api_library
10059 libbazApexVariant := result.ModuleForTests("libbaz", "android_arm64_armv8-a_shared_apex29").Module()
10060 libbazApiImportCoreVariant := result.ModuleForTests("libbaz.apiimport", "android_arm64_armv8-a_shared").Module()
10061 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even from same APEX", true, hasDep(binfooApexVariant, libbazApiImportCoreVariant))
10062 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbazApexVariant))
10063
10064 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbaz.so")
10065 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbaz.apiimport.so")
10066 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbaz.apex.29.apiimport.so")
10067
10068 // cc_api_library defined without original library should be linked with cc_api_library
10069 libquxApiImportApexVariant := result.ModuleForTests("libqux.apiimport", "android_arm64_armv8-a_shared").Module()
10070 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even original library definition does not exist", true, hasDep(binfooApexVariant, libquxApiImportApexVariant))
10071 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libqux.apex.29.apiimport.so")
10072}
10073
10074func TestPlatformBinaryBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10075 bp := `
10076 apex {
10077 name: "myapex",
10078 key: "myapex.key",
10079 native_shared_libs: ["libbar"],
10080 min_sdk_version: "29",
10081 }
10082 apex_key {
10083 name: "myapex.key",
10084 }
10085 cc_binary {
10086 name: "binfoo",
10087 shared_libs: ["libbar"],
10088 recovery_available: false,
10089 }
10090 cc_library {
10091 name: "libbar",
10092 srcs: ["libbar.cc"],
10093 apex_available: ["myapex"],
10094 min_sdk_version: "29",
10095 stubs: {
10096 symbol_file: "libbar.map.txt",
10097 versions: [
10098 "29",
10099 ],
10100 },
10101 }
10102 cc_api_library {
10103 name: "libbar",
10104 src: "libbar_stub.so",
10105 variants: ["apex.29"],
10106 }
10107 cc_api_variant {
10108 name: "libbar",
10109 variant: "apex",
10110 version: "29",
10111 src: "libbar_apex_29.so",
10112 }
10113 api_imports {
10114 name: "api_imports",
10115 apex_shared_libs: [
10116 "libbar",
10117 ],
10118 }
10119 `
10120
10121 result := testApex(t, bp)
10122
10123 hasDep := func(m android.Module, wantDep android.Module) bool {
10124 t.Helper()
10125 var found bool
10126 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10127 if dep == wantDep {
10128 found = true
10129 }
10130 })
10131 return found
10132 }
10133
10134 // Library defines stubs and cc_api_library should be used with cc_api_library
10135 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Module()
10136 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10137 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
10138
10139 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10140 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
10141
10142 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
10143 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10144 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10145 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
Kiyoung Kim487689e2022-07-26 09:48:22 +090010146}
Dennis Shend4f5d932023-01-31 20:27:21 +000010147
10148func TestTrimmedApex(t *testing.T) {
10149 bp := `
10150 apex {
10151 name: "myapex",
10152 key: "myapex.key",
10153 native_shared_libs: ["libfoo","libbaz"],
10154 min_sdk_version: "29",
10155 trim_against: "mydcla",
10156 }
10157 apex {
10158 name: "mydcla",
10159 key: "myapex.key",
10160 native_shared_libs: ["libfoo","libbar"],
10161 min_sdk_version: "29",
10162 file_contexts: ":myapex-file_contexts",
10163 dynamic_common_lib_apex: true,
10164 }
10165 apex_key {
10166 name: "myapex.key",
10167 }
10168 cc_library {
10169 name: "libfoo",
10170 shared_libs: ["libc"],
10171 apex_available: ["myapex","mydcla"],
10172 min_sdk_version: "29",
10173 }
10174 cc_library {
10175 name: "libbar",
10176 shared_libs: ["libc"],
10177 apex_available: ["myapex","mydcla"],
10178 min_sdk_version: "29",
10179 }
10180 cc_library {
10181 name: "libbaz",
10182 shared_libs: ["libc"],
10183 apex_available: ["myapex","mydcla"],
10184 min_sdk_version: "29",
10185 }
10186 cc_api_library {
10187 name: "libc",
10188 src: "libc.so",
10189 min_sdk_version: "29",
10190 recovery_available: true,
10191 }
10192 api_imports {
10193 name: "api_imports",
10194 shared_libs: [
10195 "libc",
10196 ],
10197 header_libs: [],
10198 }
10199 `
10200 ctx := testApex(t, bp)
10201 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10202 apexRule := module.MaybeRule("apexRule")
10203 if apexRule.Rule == nil {
10204 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10205 }
10206
10207 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
10208 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("TrimmedApexRule")
10209 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10210 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10211 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10212 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10213}
Jingwen Chendea7a642023-03-28 11:30:50 +000010214
10215func TestCannedFsConfig(t *testing.T) {
10216 ctx := testApex(t, `
10217 apex {
10218 name: "myapex",
10219 key: "myapex.key",
10220 updatable: false,
10221 }
10222
10223 apex_key {
10224 name: "myapex.key",
10225 public_key: "testkey.avbpubkey",
10226 private_key: "testkey.pem",
10227 }`)
10228 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10229 generateFsRule := mod.Rule("generateFsConfig")
10230 cmd := generateFsRule.RuleParams.Command
10231
10232 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10233}
10234
10235func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10236 ctx := testApex(t, `
10237 apex {
10238 name: "myapex",
10239 key: "myapex.key",
10240 canned_fs_config: "my_config",
10241 updatable: false,
10242 }
10243
10244 apex_key {
10245 name: "myapex.key",
10246 public_key: "testkey.avbpubkey",
10247 private_key: "testkey.pem",
10248 }`)
10249 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10250 generateFsRule := mod.Rule("generateFsConfig")
10251 cmd := generateFsRule.RuleParams.Command
10252
10253 // Ensure that canned_fs_config has "cat my_config" at the end
10254 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10255}