blob: 8a05716bbcf1c6dfc4ecc6de45b954cefbb20f2a [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",
Paul Duffin89f570a2021-06-16 01:42:33 +01005284 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5285 }
5286
5287 prebuilt_bootclasspath_fragment {
5288 name: "my-bootclasspath-fragment",
5289 contents: ["libfoo", "libbar"],
5290 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005291 hidden_api: {
5292 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5293 metadata: "my-bootclasspath-fragment/metadata.csv",
5294 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005295 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5296 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5297 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005298 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005299 }
5300
5301 java_import {
5302 name: "libfoo",
5303 jars: ["libfoo.jar"],
5304 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005305 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005306 }
5307
5308 java_sdk_library_import {
5309 name: "libbar",
5310 public: {
5311 jars: ["libbar.jar"],
5312 },
5313 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005314 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005315 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005316 }
5317 `
5318
Paul Duffin89f570a2021-06-16 01:42:33 +01005319 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005320 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5321 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5322
Paul Duffin537ea3d2021-05-14 10:38:00 +01005323 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005324 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005325 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005326 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005327 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5328 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005329 })
5330
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005331 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5332 bp := `
5333 prebuilt_apex {
5334 name: "myapex",
5335 arch: {
5336 arm64: {
5337 src: "myapex-arm64.apex",
5338 },
5339 arm: {
5340 src: "myapex-arm.apex",
5341 },
5342 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005343 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5344 }
5345
5346 prebuilt_bootclasspath_fragment {
5347 name: "my-bootclasspath-fragment",
5348 contents: ["libfoo", "libbar"],
5349 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005350 hidden_api: {
5351 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5352 metadata: "my-bootclasspath-fragment/metadata.csv",
5353 index: "my-bootclasspath-fragment/index.csv",
5354 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5355 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5356 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005357 }
5358
5359 java_import {
5360 name: "libfoo",
5361 jars: ["libfoo.jar"],
5362 apex_available: ["myapex"],
5363 }
5364
5365 java_library {
5366 name: "libfoo",
5367 srcs: ["foo/bar/MyClass.java"],
5368 apex_available: ["myapex"],
5369 }
Paul Duffin37856732021-02-26 14:24:15 +00005370
5371 java_sdk_library_import {
5372 name: "libbar",
5373 public: {
5374 jars: ["libbar.jar"],
5375 },
5376 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005377 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005378 }
5379
5380 java_sdk_library {
5381 name: "libbar",
5382 srcs: ["foo/bar/MyClass.java"],
5383 unsafe_ignore_missing_latest_api: true,
5384 apex_available: ["myapex"],
5385 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005386 `
5387
5388 // In this test the source (java_library) libfoo is active since the
5389 // prebuilt (java_import) defaults to prefer:false. However the
5390 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5391 // find the dex boot jar in it. We either need to disable the source libfoo
5392 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005393 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005394 // dexbootjar check is skipped if AllowMissingDependencies is true
5395 preparerAllowMissingDeps := android.GroupFixturePreparers(
5396 preparer,
5397 android.PrepareForTestWithAllowMissingDependencies,
5398 )
5399 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005400 })
5401
5402 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5403 bp := `
5404 prebuilt_apex {
5405 name: "myapex",
5406 arch: {
5407 arm64: {
5408 src: "myapex-arm64.apex",
5409 },
5410 arm: {
5411 src: "myapex-arm.apex",
5412 },
5413 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005414 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5415 }
5416
5417 prebuilt_bootclasspath_fragment {
5418 name: "my-bootclasspath-fragment",
5419 contents: ["libfoo", "libbar"],
5420 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005421 hidden_api: {
5422 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5423 metadata: "my-bootclasspath-fragment/metadata.csv",
5424 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005425 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5426 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5427 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005428 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005429 }
5430
5431 java_import {
5432 name: "libfoo",
5433 prefer: true,
5434 jars: ["libfoo.jar"],
5435 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005436 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005437 }
5438
5439 java_library {
5440 name: "libfoo",
5441 srcs: ["foo/bar/MyClass.java"],
5442 apex_available: ["myapex"],
5443 }
Paul Duffin37856732021-02-26 14:24:15 +00005444
5445 java_sdk_library_import {
5446 name: "libbar",
5447 prefer: true,
5448 public: {
5449 jars: ["libbar.jar"],
5450 },
5451 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005452 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005453 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005454 }
5455
5456 java_sdk_library {
5457 name: "libbar",
5458 srcs: ["foo/bar/MyClass.java"],
5459 unsafe_ignore_missing_latest_api: true,
5460 apex_available: ["myapex"],
5461 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005462 `
5463
Paul Duffin89f570a2021-06-16 01:42:33 +01005464 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005465 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5466 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005467
Paul Duffin537ea3d2021-05-14 10:38:00 +01005468 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005469 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005470 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005471 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005472 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5473 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005474 })
5475
5476 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5477 bp := `
5478 apex {
5479 name: "myapex",
5480 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005481 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005482 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005483 }
5484
5485 apex_key {
5486 name: "myapex.key",
5487 public_key: "testkey.avbpubkey",
5488 private_key: "testkey.pem",
5489 }
5490
5491 prebuilt_apex {
5492 name: "myapex",
5493 arch: {
5494 arm64: {
5495 src: "myapex-arm64.apex",
5496 },
5497 arm: {
5498 src: "myapex-arm.apex",
5499 },
5500 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005501 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5502 }
5503
5504 prebuilt_bootclasspath_fragment {
5505 name: "my-bootclasspath-fragment",
5506 contents: ["libfoo", "libbar"],
5507 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005508 hidden_api: {
5509 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5510 metadata: "my-bootclasspath-fragment/metadata.csv",
5511 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005512 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5513 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5514 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005515 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005516 }
5517
5518 java_import {
5519 name: "libfoo",
5520 jars: ["libfoo.jar"],
5521 apex_available: ["myapex"],
5522 }
5523
5524 java_library {
5525 name: "libfoo",
5526 srcs: ["foo/bar/MyClass.java"],
5527 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005528 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005529 }
Paul Duffin37856732021-02-26 14:24:15 +00005530
5531 java_sdk_library_import {
5532 name: "libbar",
5533 public: {
5534 jars: ["libbar.jar"],
5535 },
5536 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005537 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005538 }
5539
5540 java_sdk_library {
5541 name: "libbar",
5542 srcs: ["foo/bar/MyClass.java"],
5543 unsafe_ignore_missing_latest_api: true,
5544 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005545 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005546 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005547 `
5548
Paul Duffin89f570a2021-06-16 01:42:33 +01005549 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005550 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5551 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005552
Paul Duffin537ea3d2021-05-14 10:38:00 +01005553 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005554 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005555 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005556 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005557 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5558 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005559 })
5560
5561 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5562 bp := `
5563 apex {
5564 name: "myapex",
5565 enabled: false,
5566 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005567 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005568 }
5569
5570 apex_key {
5571 name: "myapex.key",
5572 public_key: "testkey.avbpubkey",
5573 private_key: "testkey.pem",
5574 }
5575
5576 prebuilt_apex {
5577 name: "myapex",
5578 arch: {
5579 arm64: {
5580 src: "myapex-arm64.apex",
5581 },
5582 arm: {
5583 src: "myapex-arm.apex",
5584 },
5585 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005586 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5587 }
5588
5589 prebuilt_bootclasspath_fragment {
5590 name: "my-bootclasspath-fragment",
5591 contents: ["libfoo", "libbar"],
5592 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005593 hidden_api: {
5594 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5595 metadata: "my-bootclasspath-fragment/metadata.csv",
5596 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005597 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5598 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5599 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005600 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005601 }
5602
5603 java_import {
5604 name: "libfoo",
5605 prefer: true,
5606 jars: ["libfoo.jar"],
5607 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005608 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005609 }
5610
5611 java_library {
5612 name: "libfoo",
5613 srcs: ["foo/bar/MyClass.java"],
5614 apex_available: ["myapex"],
5615 }
Paul Duffin37856732021-02-26 14:24:15 +00005616
5617 java_sdk_library_import {
5618 name: "libbar",
5619 prefer: true,
5620 public: {
5621 jars: ["libbar.jar"],
5622 },
5623 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005624 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005625 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005626 }
5627
5628 java_sdk_library {
5629 name: "libbar",
5630 srcs: ["foo/bar/MyClass.java"],
5631 unsafe_ignore_missing_latest_api: true,
5632 apex_available: ["myapex"],
5633 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005634 `
5635
Paul Duffin89f570a2021-06-16 01:42:33 +01005636 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005637 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5638 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005639
Paul Duffin537ea3d2021-05-14 10:38:00 +01005640 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005641 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005642 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005643 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005644 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5645 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005646 })
5647}
5648
Jooyung Han95513842023-04-17 11:24:54 +09005649func TestPrebuiltSkipsSymbols(t *testing.T) {
5650 testCases := []struct {
5651 name string
5652 usePrebuilt bool
5653 installSymbolFiles bool
5654 }{
5655 {
5656 name: "Source module build rule doesn't install symbol files",
5657 usePrebuilt: true,
5658 installSymbolFiles: false,
5659 },
5660 {
5661 name: "Source module is installed with symbols",
5662 usePrebuilt: false,
5663 installSymbolFiles: true,
5664 },
5665 }
5666 for _, tc := range testCases {
5667 t.Run(tc.name, func(t *testing.T) {
5668 preferProperty := "prefer: false"
5669 if tc.usePrebuilt {
5670 preferProperty = "prefer: true"
5671 }
5672 ctx := testApex(t, `
5673 // Source module
5674 apex {
5675 name: "myapex",
Inseob Kim5bedfee2023-04-20 10:16:14 +09005676 binaries: ["foo"],
Jooyung Han95513842023-04-17 11:24:54 +09005677 key: "myapex.key",
5678 updatable: false,
5679 }
5680
5681 apex_key {
5682 name: "myapex.key",
5683 public_key: "testkey.avbpubkey",
5684 private_key: "testkey.pem",
5685 }
5686
5687 apex_set {
5688 name: "myapex",
5689 set: "myapex.apks",
5690 `+preferProperty+`
5691 }
Inseob Kim5bedfee2023-04-20 10:16:14 +09005692
5693 cc_binary {
5694 name: "foo",
5695 srcs: ["mylib.cpp"],
5696 system_shared_libs: [],
5697 stl: "none",
5698 apex_available: [ "myapex" ],
5699 }
Jooyung Han95513842023-04-17 11:24:54 +09005700 `)
5701 // Symbol files are installed by installing entries under ${OUT}/apex/{apex name}
Inseob Kim5bedfee2023-04-20 10:16:14 +09005702 android.AssertStringListContainsEquals(t, "Installs",
5703 ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().FilesToInstall().Strings(),
5704 filepath.Join(ctx.Config().SoongOutDir(), "target/product/test_device/apex/myapex/bin/foo"),
Jooyung Han95513842023-04-17 11:24:54 +09005705 tc.installSymbolFiles)
5706 })
5707 }
5708}
5709
Roland Levillain630846d2019-06-26 12:48:34 +01005710func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005711 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005712 apex_test {
5713 name: "myapex",
5714 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005715 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005716 tests: [
5717 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005718 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005719 ],
5720 }
5721
5722 apex_key {
5723 name: "myapex.key",
5724 public_key: "testkey.avbpubkey",
5725 private_key: "testkey.pem",
5726 }
5727
Liz Kammer1c14a212020-05-12 15:26:55 -07005728 filegroup {
5729 name: "fg",
5730 srcs: [
5731 "baz",
5732 "bar/baz"
5733 ],
5734 }
5735
Roland Levillain630846d2019-06-26 12:48:34 +01005736 cc_test {
5737 name: "mytest",
5738 gtest: false,
5739 srcs: ["mytest.cpp"],
5740 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005741 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005742 system_shared_libs: [],
5743 static_executable: true,
5744 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005745 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005746 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005747
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005748 cc_library {
5749 name: "mylib",
5750 srcs: ["mylib.cpp"],
5751 system_shared_libs: [],
5752 stl: "none",
5753 }
5754
Liz Kammer5bd365f2020-05-27 15:15:11 -07005755 filegroup {
5756 name: "fg2",
5757 srcs: [
5758 "testdata/baz"
5759 ],
5760 }
5761
Roland Levillain9b5fde92019-06-28 15:41:19 +01005762 cc_test {
5763 name: "mytests",
5764 gtest: false,
5765 srcs: [
5766 "mytest1.cpp",
5767 "mytest2.cpp",
5768 "mytest3.cpp",
5769 ],
5770 test_per_src: true,
5771 relative_install_path: "test",
5772 system_shared_libs: [],
5773 static_executable: true,
5774 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005775 data: [
5776 ":fg",
5777 ":fg2",
5778 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005779 }
Roland Levillain630846d2019-06-26 12:48:34 +01005780 `)
5781
Sundong Ahnabb64432019-10-22 13:58:29 +09005782 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005783 copyCmds := apexRule.Args["copy_commands"]
5784
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005785 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005786 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005787 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005788
Liz Kammer1c14a212020-05-12 15:26:55 -07005789 //Ensure that test data are copied into apex.
5790 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5791 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5792
Roland Levillain9b5fde92019-06-28 15:41:19 +01005793 // Ensure that test deps built with `test_per_src` are copied into apex.
5794 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5795 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5796 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005797
5798 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005799 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005800 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005801 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005802 prefix := "TARGET_"
5803 var builder strings.Builder
5804 data.Custom(&builder, name, prefix, "", data)
5805 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005806 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5807 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5808 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5809 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
5810 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
5811 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005812 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005813
5814 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005815 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005816 data.Custom(&builder, name, prefix, "", data)
5817 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005818 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5819 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005820}
5821
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005822func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005823 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005824 apex {
5825 name: "myapex",
5826 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005827 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005828 }
5829 apex_key {
5830 name: "myapex.key",
5831 public_key: "testkey.avbpubkey",
5832 private_key: "testkey.pem",
5833 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005834 `,
5835 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5836 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5837 }),
5838 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005839 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00005840 ensureListContains(t, ab.makeModulesToInstall, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005841 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005842 var builder strings.Builder
5843 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5844 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005845 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex myapex.flattened\n")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005846}
5847
Jooyung Hand48f3c32019-08-23 11:18:57 +09005848func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5849 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5850 apex {
5851 name: "myapex",
5852 key: "myapex.key",
5853 native_shared_libs: ["libfoo"],
5854 }
5855
5856 apex_key {
5857 name: "myapex.key",
5858 public_key: "testkey.avbpubkey",
5859 private_key: "testkey.pem",
5860 }
5861
5862 cc_library {
5863 name: "libfoo",
5864 stl: "none",
5865 system_shared_libs: [],
5866 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005867 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005868 }
5869 `)
5870 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5871 apex {
5872 name: "myapex",
5873 key: "myapex.key",
5874 java_libs: ["myjar"],
5875 }
5876
5877 apex_key {
5878 name: "myapex.key",
5879 public_key: "testkey.avbpubkey",
5880 private_key: "testkey.pem",
5881 }
5882
5883 java_library {
5884 name: "myjar",
5885 srcs: ["foo/bar/MyClass.java"],
5886 sdk_version: "none",
5887 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005888 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005889 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005890 }
5891 `)
5892}
5893
Bill Peckhama41a6962021-01-11 10:58:54 -08005894func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005895 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005896 apex {
5897 name: "myapex",
5898 key: "myapex.key",
5899 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005900 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005901 }
5902
5903 apex_key {
5904 name: "myapex.key",
5905 public_key: "testkey.avbpubkey",
5906 private_key: "testkey.pem",
5907 }
5908
5909 java_import {
5910 name: "myjavaimport",
5911 apex_available: ["myapex"],
5912 jars: ["my.jar"],
5913 compile_dex: true,
5914 }
5915 `)
5916
5917 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5918 apexRule := module.Rule("apexRule")
5919 copyCmds := apexRule.Args["copy_commands"]
5920 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5921}
5922
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005923func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005924 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005925 apex {
5926 name: "myapex",
5927 key: "myapex.key",
5928 apps: [
5929 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005930 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005931 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005932 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005933 }
5934
5935 apex_key {
5936 name: "myapex.key",
5937 public_key: "testkey.avbpubkey",
5938 private_key: "testkey.pem",
5939 }
5940
5941 android_app {
5942 name: "AppFoo",
5943 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005944 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005945 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005946 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005947 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005948 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005949 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005950
5951 android_app {
5952 name: "AppFooPriv",
5953 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005954 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005955 system_modules: "none",
5956 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005957 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005958 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005959 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005960
5961 cc_library_shared {
5962 name: "libjni",
5963 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005964 shared_libs: ["libfoo"],
5965 stl: "none",
5966 system_shared_libs: [],
5967 apex_available: [ "myapex" ],
5968 sdk_version: "current",
5969 }
5970
5971 cc_library_shared {
5972 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005973 stl: "none",
5974 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005975 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005976 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005977 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005978 `)
5979
Sundong Ahnabb64432019-10-22 13:58:29 +09005980 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005981 apexRule := module.Rule("apexRule")
5982 copyCmds := apexRule.Args["copy_commands"]
5983
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005984 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5985 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005986
Colin Crossaede88c2020-08-11 12:17:01 -07005987 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005988 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005989 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005990 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005991 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005992 // JNI libraries including transitive deps are
5993 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005994 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005995 // ... embedded inside APK (jnilibs.zip)
5996 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5997 // ... and not directly inside the APEX
5998 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5999 }
Dario Frenicde2a032019-10-27 00:29:22 +01006000}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006001
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006002func TestApexWithAppImportBuildId(t *testing.T) {
6003 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
6004 for _, id := range invalidBuildIds {
6005 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
6006 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
6007 variables.BuildId = proptools.StringPtr(id)
6008 })
6009 testApexError(t, message, `apex {
6010 name: "myapex",
6011 key: "myapex.key",
6012 apps: ["AppFooPrebuilt"],
6013 updatable: false,
6014 }
6015
6016 apex_key {
6017 name: "myapex.key",
6018 public_key: "testkey.avbpubkey",
6019 private_key: "testkey.pem",
6020 }
6021
6022 android_app_import {
6023 name: "AppFooPrebuilt",
6024 apk: "PrebuiltAppFoo.apk",
6025 presigned: true,
6026 apex_available: ["myapex"],
6027 }
6028 `, fixture)
6029 }
6030}
6031
Dario Frenicde2a032019-10-27 00:29:22 +01006032func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006033 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01006034 apex {
6035 name: "myapex",
6036 key: "myapex.key",
6037 apps: [
6038 "AppFooPrebuilt",
6039 "AppFooPrivPrebuilt",
6040 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006041 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01006042 }
6043
6044 apex_key {
6045 name: "myapex.key",
6046 public_key: "testkey.avbpubkey",
6047 private_key: "testkey.pem",
6048 }
6049
6050 android_app_import {
6051 name: "AppFooPrebuilt",
6052 apk: "PrebuiltAppFoo.apk",
6053 presigned: true,
6054 dex_preopt: {
6055 enabled: false,
6056 },
Jiyong Park592a6a42020-04-21 22:34:28 +09006057 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006058 }
6059
6060 android_app_import {
6061 name: "AppFooPrivPrebuilt",
6062 apk: "PrebuiltAppFooPriv.apk",
6063 privileged: true,
6064 presigned: true,
6065 dex_preopt: {
6066 enabled: false,
6067 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006068 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006069 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006070 }
6071 `)
6072
Sundong Ahnabb64432019-10-22 13:58:29 +09006073 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01006074 apexRule := module.Rule("apexRule")
6075 copyCmds := apexRule.Args["copy_commands"]
6076
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006077 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6078 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006079}
6080
6081func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006082 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006083 apex {
6084 name: "myapex",
6085 key: "myapex.key",
6086 apps: [
6087 "AppFoo",
6088 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006089 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006090 }
6091
6092 apex_key {
6093 name: "myapex.key",
6094 public_key: "testkey.avbpubkey",
6095 private_key: "testkey.pem",
6096 }
6097
6098 android_app {
6099 name: "AppFoo",
6100 srcs: ["foo/bar/MyClass.java"],
6101 sdk_version: "none",
6102 system_modules: "none",
6103 apex_available: [ "myapex" ],
6104 }
6105
6106 android_app_import {
6107 name: "AppFoo",
6108 apk: "AppFooPrebuilt.apk",
6109 filename: "AppFooPrebuilt.apk",
6110 presigned: true,
6111 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006112 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006113 }
6114 `, withFiles(map[string][]byte{
6115 "AppFooPrebuilt.apk": nil,
6116 }))
6117
6118 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006119 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006120 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006121}
6122
Dario Freni6f3937c2019-12-20 22:58:03 +00006123func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006124 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006125 apex {
6126 name: "myapex",
6127 key: "myapex.key",
6128 apps: [
6129 "TesterHelpAppFoo",
6130 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006131 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006132 }
6133
6134 apex_key {
6135 name: "myapex.key",
6136 public_key: "testkey.avbpubkey",
6137 private_key: "testkey.pem",
6138 }
6139
6140 android_test_helper_app {
6141 name: "TesterHelpAppFoo",
6142 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006143 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006144 }
6145
6146 `)
6147
6148 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6149 apexRule := module.Rule("apexRule")
6150 copyCmds := apexRule.Args["copy_commands"]
6151
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006152 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006153}
6154
Jooyung Han18020ea2019-11-13 10:50:48 +09006155func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6156 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006157 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006158 apex {
6159 name: "myapex",
6160 key: "myapex.key",
6161 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006162 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006163 }
6164
6165 apex_key {
6166 name: "myapex.key",
6167 public_key: "testkey.avbpubkey",
6168 private_key: "testkey.pem",
6169 }
6170
6171 apex {
6172 name: "otherapex",
6173 key: "myapex.key",
6174 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006175 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006176 }
6177
6178 cc_defaults {
6179 name: "libfoo-defaults",
6180 apex_available: ["otherapex"],
6181 }
6182
6183 cc_library {
6184 name: "libfoo",
6185 defaults: ["libfoo-defaults"],
6186 stl: "none",
6187 system_shared_libs: [],
6188 }`)
6189}
6190
Paul Duffine52e66f2020-03-30 17:54:29 +01006191func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006192 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006193 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006194 apex {
6195 name: "myapex",
6196 key: "myapex.key",
6197 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006198 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006199 }
6200
6201 apex_key {
6202 name: "myapex.key",
6203 public_key: "testkey.avbpubkey",
6204 private_key: "testkey.pem",
6205 }
6206
6207 apex {
6208 name: "otherapex",
6209 key: "otherapex.key",
6210 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006211 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006212 }
6213
6214 apex_key {
6215 name: "otherapex.key",
6216 public_key: "testkey.avbpubkey",
6217 private_key: "testkey.pem",
6218 }
6219
6220 cc_library {
6221 name: "libfoo",
6222 stl: "none",
6223 system_shared_libs: [],
6224 apex_available: ["otherapex"],
6225 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006226}
Jiyong Park127b40b2019-09-30 16:04:35 +09006227
Paul Duffine52e66f2020-03-30 17:54:29 +01006228func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006229 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006230 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006231.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006232.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006233.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006234.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006235.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006236.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006237 apex {
6238 name: "myapex",
6239 key: "myapex.key",
6240 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006241 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006242 }
6243
6244 apex_key {
6245 name: "myapex.key",
6246 public_key: "testkey.avbpubkey",
6247 private_key: "testkey.pem",
6248 }
6249
Jiyong Park127b40b2019-09-30 16:04:35 +09006250 cc_library {
6251 name: "libfoo",
6252 stl: "none",
6253 shared_libs: ["libbar"],
6254 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006255 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006256 }
6257
6258 cc_library {
6259 name: "libbar",
6260 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006261 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006262 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006263 apex_available: ["myapex"],
6264 }
6265
6266 cc_library {
6267 name: "libbaz",
6268 stl: "none",
6269 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006270 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006271}
Jiyong Park127b40b2019-09-30 16:04:35 +09006272
Paul Duffine52e66f2020-03-30 17:54:29 +01006273func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006274 testApexError(t, "\"otherapex\" is not a valid module name", `
6275 apex {
6276 name: "myapex",
6277 key: "myapex.key",
6278 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006279 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006280 }
6281
6282 apex_key {
6283 name: "myapex.key",
6284 public_key: "testkey.avbpubkey",
6285 private_key: "testkey.pem",
6286 }
6287
6288 cc_library {
6289 name: "libfoo",
6290 stl: "none",
6291 system_shared_libs: [],
6292 apex_available: ["otherapex"],
6293 }`)
6294
Paul Duffine52e66f2020-03-30 17:54:29 +01006295 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006296 apex {
6297 name: "myapex",
6298 key: "myapex.key",
6299 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006300 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006301 }
6302
6303 apex_key {
6304 name: "myapex.key",
6305 public_key: "testkey.avbpubkey",
6306 private_key: "testkey.pem",
6307 }
6308
6309 cc_library {
6310 name: "libfoo",
6311 stl: "none",
6312 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006313 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006314 apex_available: ["myapex"],
6315 }
6316
6317 cc_library {
6318 name: "libbar",
6319 stl: "none",
6320 system_shared_libs: [],
6321 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006322 }
6323
6324 cc_library {
6325 name: "libbaz",
6326 stl: "none",
6327 system_shared_libs: [],
6328 stubs: {
6329 versions: ["10", "20", "30"],
6330 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006331 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006332}
Jiyong Park127b40b2019-09-30 16:04:35 +09006333
Jiyong Park89e850a2020-04-07 16:37:39 +09006334func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006335 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006336 apex {
6337 name: "myapex",
6338 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006339 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006340 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006341 }
6342
6343 apex_key {
6344 name: "myapex.key",
6345 public_key: "testkey.avbpubkey",
6346 private_key: "testkey.pem",
6347 }
6348
6349 cc_library {
6350 name: "libfoo",
6351 stl: "none",
6352 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006353 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006354 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006355 }
6356
6357 cc_library {
6358 name: "libfoo2",
6359 stl: "none",
6360 system_shared_libs: [],
6361 shared_libs: ["libbaz"],
6362 apex_available: ["//apex_available:platform"],
6363 }
6364
6365 cc_library {
6366 name: "libbar",
6367 stl: "none",
6368 system_shared_libs: [],
6369 apex_available: ["myapex"],
6370 }
6371
6372 cc_library {
6373 name: "libbaz",
6374 stl: "none",
6375 system_shared_libs: [],
6376 apex_available: ["myapex"],
6377 stubs: {
6378 versions: ["1"],
6379 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006380 }`)
6381
Jiyong Park89e850a2020-04-07 16:37:39 +09006382 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6383 // because it depends on libbar which isn't available to platform
6384 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6385 if libfoo.NotAvailableForPlatform() != true {
6386 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6387 }
6388
6389 // libfoo2 however can be available to platform because it depends on libbaz which provides
6390 // stubs
6391 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6392 if libfoo2.NotAvailableForPlatform() == true {
6393 t.Errorf("%q should be available to platform", libfoo2.String())
6394 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006395}
Jiyong Parka90ca002019-10-07 15:47:24 +09006396
Paul Duffine52e66f2020-03-30 17:54:29 +01006397func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006398 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006399 apex {
6400 name: "myapex",
6401 key: "myapex.key",
6402 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006403 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006404 }
6405
6406 apex_key {
6407 name: "myapex.key",
6408 public_key: "testkey.avbpubkey",
6409 private_key: "testkey.pem",
6410 }
6411
6412 cc_library {
6413 name: "libfoo",
6414 stl: "none",
6415 system_shared_libs: [],
6416 apex_available: ["myapex"],
6417 static: {
6418 apex_available: ["//apex_available:platform"],
6419 },
6420 }`)
6421
Jiyong Park89e850a2020-04-07 16:37:39 +09006422 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6423 if libfooShared.NotAvailableForPlatform() != true {
6424 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6425 }
6426 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6427 if libfooStatic.NotAvailableForPlatform() != false {
6428 t.Errorf("%q should be available to platform", libfooStatic.String())
6429 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006430}
6431
Jiyong Park5d790c32019-11-15 18:40:32 +09006432func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006433 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006434 apex {
6435 name: "myapex",
6436 key: "myapex.key",
6437 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006438 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006439 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006440 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006441 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006442 }
6443
6444 override_apex {
6445 name: "override_myapex",
6446 base: "myapex",
6447 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006448 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006449 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006450 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006451 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006452 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006453 key: "mynewapex.key",
6454 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006455 }
6456
6457 apex_key {
6458 name: "myapex.key",
6459 public_key: "testkey.avbpubkey",
6460 private_key: "testkey.pem",
6461 }
6462
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006463 apex_key {
6464 name: "mynewapex.key",
6465 public_key: "testkey2.avbpubkey",
6466 private_key: "testkey2.pem",
6467 }
6468
6469 android_app_certificate {
6470 name: "myapex.certificate",
6471 certificate: "testkey",
6472 }
6473
Jiyong Park5d790c32019-11-15 18:40:32 +09006474 android_app {
6475 name: "app",
6476 srcs: ["foo/bar/MyClass.java"],
6477 package_name: "foo",
6478 sdk_version: "none",
6479 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006480 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006481 }
6482
6483 override_android_app {
6484 name: "override_app",
6485 base: "app",
6486 package_name: "bar",
6487 }
markchien7c803b82021-08-26 22:10:06 +08006488
6489 bpf {
6490 name: "bpf",
6491 srcs: ["bpf.c"],
6492 }
6493
6494 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006495 name: "overrideBpf",
6496 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006497 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006498
6499 prebuilt_etc {
6500 name: "myetc",
6501 src: "myprebuilt",
6502 }
6503
6504 prebuilt_etc {
6505 name: "override_myetc",
6506 src: "override_myprebuilt",
6507 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006508 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006509
Jiyong Park317645e2019-12-05 13:20:58 +09006510 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6511 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6512 if originalVariant.GetOverriddenBy() != "" {
6513 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6514 }
6515 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6516 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6517 }
6518
Jiyong Park5d790c32019-11-15 18:40:32 +09006519 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6520 apexRule := module.Rule("apexRule")
6521 copyCmds := apexRule.Args["copy_commands"]
6522
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006523 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6524 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006525
markchien7c803b82021-08-26 22:10:06 +08006526 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006527 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006528
Daniel Norman5a3ce132021-08-26 15:44:43 -07006529 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6530 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6531
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006532 apexBundle := module.Module().(*apexBundle)
6533 name := apexBundle.Name()
6534 if name != "override_myapex" {
6535 t.Errorf("name should be \"override_myapex\", but was %q", name)
6536 }
6537
Baligh Uddin004d7172020-02-19 21:29:28 -08006538 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6539 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6540 }
6541
Jiyong Park20bacab2020-03-03 11:45:41 +09006542 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006543 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006544 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6545
6546 signApkRule := module.Rule("signapk")
6547 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006548
Colin Crossaa255532020-07-03 13:18:24 -07006549 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006550 var builder strings.Builder
6551 data.Custom(&builder, name, "TARGET_", "", data)
6552 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006553 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6554 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
6555 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006556 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006557 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006558 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006559 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006560 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006561 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
6562 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006563}
6564
Albert Martineefabcf2022-03-21 20:11:16 +00006565func TestMinSdkVersionOverride(t *testing.T) {
6566 // Override from 29 to 31
6567 minSdkOverride31 := "31"
6568 ctx := testApex(t, `
6569 apex {
6570 name: "myapex",
6571 key: "myapex.key",
6572 native_shared_libs: ["mylib"],
6573 updatable: true,
6574 min_sdk_version: "29"
6575 }
6576
6577 override_apex {
6578 name: "override_myapex",
6579 base: "myapex",
6580 logging_parent: "com.foo.bar",
6581 package_name: "test.overridden.package"
6582 }
6583
6584 apex_key {
6585 name: "myapex.key",
6586 public_key: "testkey.avbpubkey",
6587 private_key: "testkey.pem",
6588 }
6589
6590 cc_library {
6591 name: "mylib",
6592 srcs: ["mylib.cpp"],
6593 runtime_libs: ["libbar"],
6594 system_shared_libs: [],
6595 stl: "none",
6596 apex_available: [ "myapex" ],
6597 min_sdk_version: "apex_inherit"
6598 }
6599
6600 cc_library {
6601 name: "libbar",
6602 srcs: ["mylib.cpp"],
6603 system_shared_libs: [],
6604 stl: "none",
6605 apex_available: [ "myapex" ],
6606 min_sdk_version: "apex_inherit"
6607 }
6608
6609 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6610
6611 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6612 copyCmds := apexRule.Args["copy_commands"]
6613
6614 // Ensure that direct non-stubs dep is always included
6615 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6616
6617 // Ensure that runtime_libs dep in included
6618 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6619
6620 // Ensure libraries target overridden min_sdk_version value
6621 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6622}
6623
6624func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6625 // Attempt to override from 31 to 29, should be a NOOP
6626 minSdkOverride29 := "29"
6627 ctx := testApex(t, `
6628 apex {
6629 name: "myapex",
6630 key: "myapex.key",
6631 native_shared_libs: ["mylib"],
6632 updatable: true,
6633 min_sdk_version: "31"
6634 }
6635
6636 override_apex {
6637 name: "override_myapex",
6638 base: "myapex",
6639 logging_parent: "com.foo.bar",
6640 package_name: "test.overridden.package"
6641 }
6642
6643 apex_key {
6644 name: "myapex.key",
6645 public_key: "testkey.avbpubkey",
6646 private_key: "testkey.pem",
6647 }
6648
6649 cc_library {
6650 name: "mylib",
6651 srcs: ["mylib.cpp"],
6652 runtime_libs: ["libbar"],
6653 system_shared_libs: [],
6654 stl: "none",
6655 apex_available: [ "myapex" ],
6656 min_sdk_version: "apex_inherit"
6657 }
6658
6659 cc_library {
6660 name: "libbar",
6661 srcs: ["mylib.cpp"],
6662 system_shared_libs: [],
6663 stl: "none",
6664 apex_available: [ "myapex" ],
6665 min_sdk_version: "apex_inherit"
6666 }
6667
6668 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6669
6670 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6671 copyCmds := apexRule.Args["copy_commands"]
6672
6673 // Ensure that direct non-stubs dep is always included
6674 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6675
6676 // Ensure that runtime_libs dep in included
6677 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6678
6679 // Ensure libraries target the original min_sdk_version value rather than the overridden
6680 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6681}
6682
Jooyung Han214bf372019-11-12 13:03:50 +09006683func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006684 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006685 apex {
6686 name: "myapex",
6687 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006688 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006689 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006690 }
6691
6692 apex_key {
6693 name: "myapex.key",
6694 public_key: "testkey.avbpubkey",
6695 private_key: "testkey.pem",
6696 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006697
6698 cc_library {
6699 name: "mylib",
6700 srcs: ["mylib.cpp"],
6701 stl: "libc++",
6702 system_shared_libs: [],
6703 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006704 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006705 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006706 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006707
6708 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6709 args := module.Rule("apexRule").Args
6710 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006711 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006712
6713 // The copies of the libraries in the apex should have one more dependency than
6714 // the ones outside the apex, namely the unwinder. Ideally we should check
6715 // the dependency names directly here but for some reason the names are blank in
6716 // this test.
6717 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006718 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006719 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6720 if len(apexImplicits) != len(nonApexImplicits)+1 {
6721 t.Errorf("%q missing unwinder dep", lib)
6722 }
6723 }
Jooyung Han214bf372019-11-12 13:03:50 +09006724}
6725
Paul Duffine05480a2021-03-08 15:07:14 +00006726var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006727 "api/current.txt": nil,
6728 "api/removed.txt": nil,
6729 "api/system-current.txt": nil,
6730 "api/system-removed.txt": nil,
6731 "api/test-current.txt": nil,
6732 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006733
Anton Hanssondff2c782020-12-21 17:10:01 +00006734 "100/public/api/foo.txt": nil,
6735 "100/public/api/foo-removed.txt": nil,
6736 "100/system/api/foo.txt": nil,
6737 "100/system/api/foo-removed.txt": nil,
6738
Paul Duffineedc5d52020-06-12 17:46:39 +01006739 // For java_sdk_library_import
6740 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006741}
6742
Jooyung Han58f26ab2019-12-18 15:34:32 +09006743func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006744 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006745 apex {
6746 name: "myapex",
6747 key: "myapex.key",
6748 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006749 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006750 }
6751
6752 apex_key {
6753 name: "myapex.key",
6754 public_key: "testkey.avbpubkey",
6755 private_key: "testkey.pem",
6756 }
6757
6758 java_sdk_library {
6759 name: "foo",
6760 srcs: ["a.java"],
6761 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006762 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006763 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006764
6765 prebuilt_apis {
6766 name: "sdk",
6767 api_dirs: ["100"],
6768 }
Paul Duffin9b879592020-05-26 13:21:35 +01006769 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006770
6771 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006772 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006773 "javalib/foo.jar",
6774 "etc/permissions/foo.xml",
6775 })
6776 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006777 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006778 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 +09006779}
6780
Paul Duffin9b879592020-05-26 13:21:35 +01006781func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006782 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006783 apex {
6784 name: "myapex",
6785 key: "myapex.key",
6786 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006787 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006788 }
6789
6790 apex_key {
6791 name: "myapex.key",
6792 public_key: "testkey.avbpubkey",
6793 private_key: "testkey.pem",
6794 }
6795
6796 java_sdk_library {
6797 name: "foo",
6798 srcs: ["a.java"],
6799 api_packages: ["foo"],
6800 apex_available: ["myapex"],
6801 sdk_version: "none",
6802 system_modules: "none",
6803 }
6804
6805 java_library {
6806 name: "bar",
6807 srcs: ["a.java"],
6808 libs: ["foo"],
6809 apex_available: ["myapex"],
6810 sdk_version: "none",
6811 system_modules: "none",
6812 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006813
6814 prebuilt_apis {
6815 name: "sdk",
6816 api_dirs: ["100"],
6817 }
Paul Duffin9b879592020-05-26 13:21:35 +01006818 `, withFiles(filesForSdkLibrary))
6819
6820 // java_sdk_library installs both impl jar and permission XML
6821 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6822 "javalib/bar.jar",
6823 "javalib/foo.jar",
6824 "etc/permissions/foo.xml",
6825 })
6826
6827 // The bar library should depend on the implementation jar.
6828 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006829 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006830 t.Errorf("expected %q, found %#q", expected, actual)
6831 }
6832}
6833
6834func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006835 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006836 apex {
6837 name: "myapex",
6838 key: "myapex.key",
6839 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006840 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006841 }
6842
6843 apex_key {
6844 name: "myapex.key",
6845 public_key: "testkey.avbpubkey",
6846 private_key: "testkey.pem",
6847 }
6848
6849 java_sdk_library {
6850 name: "foo",
6851 srcs: ["a.java"],
6852 api_packages: ["foo"],
6853 apex_available: ["myapex"],
6854 sdk_version: "none",
6855 system_modules: "none",
6856 }
6857
6858 java_library {
6859 name: "bar",
6860 srcs: ["a.java"],
6861 libs: ["foo"],
6862 sdk_version: "none",
6863 system_modules: "none",
6864 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006865
6866 prebuilt_apis {
6867 name: "sdk",
6868 api_dirs: ["100"],
6869 }
Paul Duffin9b879592020-05-26 13:21:35 +01006870 `, withFiles(filesForSdkLibrary))
6871
6872 // java_sdk_library installs both impl jar and permission XML
6873 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6874 "javalib/foo.jar",
6875 "etc/permissions/foo.xml",
6876 })
6877
6878 // The bar library should depend on the stubs jar.
6879 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006880 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006881 t.Errorf("expected %q, found %#q", expected, actual)
6882 }
6883}
6884
Paul Duffineedc5d52020-06-12 17:46:39 +01006885func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006886 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006887 prebuilt_apis {
6888 name: "sdk",
6889 api_dirs: ["100"],
6890 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006891 withFiles(map[string][]byte{
6892 "apex/a.java": nil,
6893 "apex/apex_manifest.json": nil,
6894 "apex/Android.bp": []byte(`
6895 package {
6896 default_visibility: ["//visibility:private"],
6897 }
6898
6899 apex {
6900 name: "myapex",
6901 key: "myapex.key",
6902 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006903 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006904 }
6905
6906 apex_key {
6907 name: "myapex.key",
6908 public_key: "testkey.avbpubkey",
6909 private_key: "testkey.pem",
6910 }
6911
6912 java_library {
6913 name: "bar",
6914 srcs: ["a.java"],
6915 libs: ["foo"],
6916 apex_available: ["myapex"],
6917 sdk_version: "none",
6918 system_modules: "none",
6919 }
6920`),
6921 "source/a.java": nil,
6922 "source/api/current.txt": nil,
6923 "source/api/removed.txt": nil,
6924 "source/Android.bp": []byte(`
6925 package {
6926 default_visibility: ["//visibility:private"],
6927 }
6928
6929 java_sdk_library {
6930 name: "foo",
6931 visibility: ["//apex"],
6932 srcs: ["a.java"],
6933 api_packages: ["foo"],
6934 apex_available: ["myapex"],
6935 sdk_version: "none",
6936 system_modules: "none",
6937 public: {
6938 enabled: true,
6939 },
6940 }
6941`),
6942 "prebuilt/a.jar": nil,
6943 "prebuilt/Android.bp": []byte(`
6944 package {
6945 default_visibility: ["//visibility:private"],
6946 }
6947
6948 java_sdk_library_import {
6949 name: "foo",
6950 visibility: ["//apex", "//source"],
6951 apex_available: ["myapex"],
6952 prefer: true,
6953 public: {
6954 jars: ["a.jar"],
6955 },
6956 }
6957`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006958 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006959 )
6960
6961 // java_sdk_library installs both impl jar and permission XML
6962 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6963 "javalib/bar.jar",
6964 "javalib/foo.jar",
6965 "etc/permissions/foo.xml",
6966 })
6967
6968 // The bar library should depend on the implementation jar.
6969 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006970 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006971 t.Errorf("expected %q, found %#q", expected, actual)
6972 }
6973}
6974
6975func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6976 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6977 apex {
6978 name: "myapex",
6979 key: "myapex.key",
6980 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006981 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006982 }
6983
6984 apex_key {
6985 name: "myapex.key",
6986 public_key: "testkey.avbpubkey",
6987 private_key: "testkey.pem",
6988 }
6989
6990 java_sdk_library_import {
6991 name: "foo",
6992 apex_available: ["myapex"],
6993 prefer: true,
6994 public: {
6995 jars: ["a.jar"],
6996 },
6997 }
6998
6999 `, withFiles(filesForSdkLibrary))
7000}
7001
atrost6e126252020-01-27 17:01:16 +00007002func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01007003 result := android.GroupFixturePreparers(
7004 prepareForApexTest,
7005 java.PrepareForTestWithPlatformCompatConfig,
7006 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00007007 apex {
7008 name: "myapex",
7009 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007010 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007011 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007012 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007013 }
7014
7015 apex_key {
7016 name: "myapex.key",
7017 public_key: "testkey.avbpubkey",
7018 private_key: "testkey.pem",
7019 }
7020
7021 platform_compat_config {
7022 name: "myjar-platform-compat-config",
7023 src: ":myjar",
7024 }
7025
7026 java_library {
7027 name: "myjar",
7028 srcs: ["foo/bar/MyClass.java"],
7029 sdk_version: "none",
7030 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007031 apex_available: [ "myapex" ],
7032 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007033
7034 // Make sure that a preferred prebuilt does not affect the apex contents.
7035 prebuilt_platform_compat_config {
7036 name: "myjar-platform-compat-config",
7037 metadata: "compat-config/metadata.xml",
7038 prefer: true,
7039 }
atrost6e126252020-01-27 17:01:16 +00007040 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007041 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00007042 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7043 "etc/compatconfig/myjar-platform-compat-config.xml",
7044 "javalib/myjar.jar",
7045 })
7046}
7047
Jooyung Han862c0d62022-12-21 10:15:37 +09007048func TestNoDupeApexFiles(t *testing.T) {
7049 android.GroupFixturePreparers(
7050 android.PrepareForTestWithAndroidBuildComponents,
7051 PrepareForTestWithApexBuildComponents,
7052 prepareForTestWithMyapex,
7053 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7054 ).
7055 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7056 RunTestWithBp(t, `
7057 apex {
7058 name: "myapex",
7059 key: "myapex.key",
7060 prebuilts: ["foo", "bar"],
7061 updatable: false,
7062 }
7063
7064 apex_key {
7065 name: "myapex.key",
7066 public_key: "testkey.avbpubkey",
7067 private_key: "testkey.pem",
7068 }
7069
7070 prebuilt_etc {
7071 name: "foo",
7072 src: "myprebuilt",
7073 filename_from_src: true,
7074 }
7075
7076 prebuilt_etc {
7077 name: "bar",
7078 src: "myprebuilt",
7079 filename_from_src: true,
7080 }
7081 `)
7082}
7083
Jiyong Park479321d2019-12-16 11:47:12 +09007084func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7085 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7086 apex {
7087 name: "myapex",
7088 key: "myapex.key",
7089 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007090 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007091 }
7092
7093 apex_key {
7094 name: "myapex.key",
7095 public_key: "testkey.avbpubkey",
7096 private_key: "testkey.pem",
7097 }
7098
7099 java_library {
7100 name: "myjar",
7101 srcs: ["foo/bar/MyClass.java"],
7102 sdk_version: "none",
7103 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007104 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007105 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007106 }
7107 `)
7108}
7109
Jiyong Park7afd1072019-12-30 16:56:33 +09007110func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007111 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007112 apex {
7113 name: "myapex",
7114 key: "myapex.key",
7115 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007116 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007117 }
7118
7119 apex_key {
7120 name: "myapex.key",
7121 public_key: "testkey.avbpubkey",
7122 private_key: "testkey.pem",
7123 }
7124
7125 cc_library {
7126 name: "mylib",
7127 srcs: ["mylib.cpp"],
7128 system_shared_libs: [],
7129 stl: "none",
7130 required: ["a", "b"],
7131 host_required: ["c", "d"],
7132 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007133 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007134 }
7135 `)
7136
7137 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007138 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007139 name := apexBundle.BaseModuleName()
7140 prefix := "TARGET_"
7141 var builder strings.Builder
7142 data.Custom(&builder, name, prefix, "", data)
7143 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007144 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 -08007145 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7146 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007147}
7148
Jiyong Park7cd10e32020-01-14 09:22:18 +09007149func TestSymlinksFromApexToSystem(t *testing.T) {
7150 bp := `
7151 apex {
7152 name: "myapex",
7153 key: "myapex.key",
7154 native_shared_libs: ["mylib"],
7155 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007156 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007157 }
7158
Jiyong Park9d677202020-02-19 16:29:35 +09007159 apex {
7160 name: "myapex.updatable",
7161 key: "myapex.key",
7162 native_shared_libs: ["mylib"],
7163 java_libs: ["myjar"],
7164 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007165 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007166 }
7167
Jiyong Park7cd10e32020-01-14 09:22:18 +09007168 apex_key {
7169 name: "myapex.key",
7170 public_key: "testkey.avbpubkey",
7171 private_key: "testkey.pem",
7172 }
7173
7174 cc_library {
7175 name: "mylib",
7176 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007177 shared_libs: [
7178 "myotherlib",
7179 "myotherlib_ext",
7180 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007181 system_shared_libs: [],
7182 stl: "none",
7183 apex_available: [
7184 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007185 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007186 "//apex_available:platform",
7187 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007188 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007189 }
7190
7191 cc_library {
7192 name: "myotherlib",
7193 srcs: ["mylib.cpp"],
7194 system_shared_libs: [],
7195 stl: "none",
7196 apex_available: [
7197 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007198 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007199 "//apex_available:platform",
7200 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007201 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007202 }
7203
Jiyong Parkce243632023-02-17 18:22:25 +09007204 cc_library {
7205 name: "myotherlib_ext",
7206 srcs: ["mylib.cpp"],
7207 system_shared_libs: [],
7208 system_ext_specific: true,
7209 stl: "none",
7210 apex_available: [
7211 "myapex",
7212 "myapex.updatable",
7213 "//apex_available:platform",
7214 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007215 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007216 }
7217
Jiyong Park7cd10e32020-01-14 09:22:18 +09007218 java_library {
7219 name: "myjar",
7220 srcs: ["foo/bar/MyClass.java"],
7221 sdk_version: "none",
7222 system_modules: "none",
7223 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007224 apex_available: [
7225 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007226 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007227 "//apex_available:platform",
7228 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007229 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007230 }
7231
7232 java_library {
7233 name: "myotherjar",
7234 srcs: ["foo/bar/MyClass.java"],
7235 sdk_version: "none",
7236 system_modules: "none",
7237 apex_available: [
7238 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007239 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007240 "//apex_available:platform",
7241 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007242 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007243 }
7244 `
7245
7246 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7247 for _, f := range files {
7248 if f.path == file {
7249 if f.isLink {
7250 t.Errorf("%q is not a real file", file)
7251 }
7252 return
7253 }
7254 }
7255 t.Errorf("%q is not found", file)
7256 }
7257
Jiyong Parkce243632023-02-17 18:22:25 +09007258 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007259 for _, f := range files {
7260 if f.path == file {
7261 if !f.isLink {
7262 t.Errorf("%q is not a symlink", file)
7263 }
Jiyong Parkce243632023-02-17 18:22:25 +09007264 if f.src != target {
7265 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7266 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007267 return
7268 }
7269 }
7270 t.Errorf("%q is not found", file)
7271 }
7272
Jiyong Park9d677202020-02-19 16:29:35 +09007273 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7274 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007275 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007276 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007277 ensureRealfileExists(t, files, "javalib/myjar.jar")
7278 ensureRealfileExists(t, files, "lib64/mylib.so")
7279 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007280 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007281
Jiyong Park9d677202020-02-19 16:29:35 +09007282 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7283 ensureRealfileExists(t, files, "javalib/myjar.jar")
7284 ensureRealfileExists(t, files, "lib64/mylib.so")
7285 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007286 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007287
7288 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007289 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007290 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007291 ensureRealfileExists(t, files, "javalib/myjar.jar")
7292 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007293 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7294 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007295
7296 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7297 ensureRealfileExists(t, files, "javalib/myjar.jar")
7298 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007299 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7300 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007301}
7302
Yo Chiange8128052020-07-23 20:09:18 +08007303func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007304 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007305 apex {
7306 name: "myapex",
7307 key: "myapex.key",
7308 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007309 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007310 }
7311
7312 apex_key {
7313 name: "myapex.key",
7314 public_key: "testkey.avbpubkey",
7315 private_key: "testkey.pem",
7316 }
7317
7318 cc_library_shared {
7319 name: "mylib",
7320 srcs: ["mylib.cpp"],
7321 shared_libs: ["myotherlib"],
7322 system_shared_libs: [],
7323 stl: "none",
7324 apex_available: [
7325 "myapex",
7326 "//apex_available:platform",
7327 ],
7328 }
7329
7330 cc_prebuilt_library_shared {
7331 name: "myotherlib",
7332 srcs: ["prebuilt.so"],
7333 system_shared_libs: [],
7334 stl: "none",
7335 apex_available: [
7336 "myapex",
7337 "//apex_available:platform",
7338 ],
7339 }
7340 `)
7341
Prerana Patilb1896c82022-11-09 18:14:34 +00007342 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007343 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007344 var builder strings.Builder
7345 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7346 androidMk := builder.String()
7347 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007348 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007349 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7350 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7351 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007352 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 +08007353}
7354
Jooyung Han643adc42020-02-27 13:50:06 +09007355func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007356 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007357 apex {
7358 name: "myapex",
7359 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09007360 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007361 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007362 }
7363
7364 apex_key {
7365 name: "myapex.key",
7366 public_key: "testkey.avbpubkey",
7367 private_key: "testkey.pem",
7368 }
7369
7370 cc_library {
7371 name: "mylib",
7372 srcs: ["mylib.cpp"],
7373 shared_libs: ["mylib2"],
7374 system_shared_libs: [],
7375 stl: "none",
7376 apex_available: [ "myapex" ],
7377 }
7378
7379 cc_library {
7380 name: "mylib2",
7381 srcs: ["mylib.cpp"],
7382 system_shared_libs: [],
7383 stl: "none",
7384 apex_available: [ "myapex" ],
7385 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007386
7387 rust_ffi_shared {
7388 name: "libfoo.rust",
7389 crate_name: "foo",
7390 srcs: ["foo.rs"],
7391 shared_libs: ["libfoo.shared_from_rust"],
7392 prefer_rlib: true,
7393 apex_available: ["myapex"],
7394 }
7395
7396 cc_library_shared {
7397 name: "libfoo.shared_from_rust",
7398 srcs: ["mylib.cpp"],
7399 system_shared_libs: [],
7400 stl: "none",
7401 stubs: {
7402 versions: ["10", "11", "12"],
7403 },
7404 }
7405
Jooyung Han643adc42020-02-27 13:50:06 +09007406 `)
7407
7408 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
7409 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09007410 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007411 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7412 "lib64/mylib.so",
7413 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007414 "lib64/libfoo.rust.so",
7415 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7416 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007417 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007418
7419 // b/220397949
7420 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007421}
7422
Jooyung Han49f67012020-04-17 13:43:10 +09007423func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007424 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007425 apex {
7426 name: "myapex",
7427 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007428 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007429 }
7430 apex_key {
7431 name: "myapex.key",
7432 public_key: "testkey.avbpubkey",
7433 private_key: "testkey.pem",
7434 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007435 `,
7436 android.FixtureModifyConfig(func(config android.Config) {
7437 delete(config.Targets, android.Android)
7438 config.AndroidCommonTarget = android.Target{}
7439 }),
7440 )
Jooyung Han49f67012020-04-17 13:43:10 +09007441
7442 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7443 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7444 }
7445}
7446
Jiyong Parkbd159612020-02-28 15:22:21 +09007447func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007448 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007449 apex {
7450 name: "myapex",
7451 key: "myapex.key",
7452 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007453 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007454 }
7455
7456 apex_key {
7457 name: "myapex.key",
7458 public_key: "testkey.avbpubkey",
7459 private_key: "testkey.pem",
7460 }
7461
7462 android_app {
7463 name: "AppFoo",
7464 srcs: ["foo/bar/MyClass.java"],
7465 sdk_version: "none",
7466 system_modules: "none",
7467 apex_available: [ "myapex" ],
7468 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007469 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007470
Colin Crosscf371cc2020-11-13 11:48:42 -08007471 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007472 content := bundleConfigRule.Args["content"]
7473
7474 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007475 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 +09007476}
7477
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007478func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007479 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007480 apex {
7481 name: "myapex",
7482 key: "myapex.key",
7483 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007484 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007485 }
7486
7487 apex_key {
7488 name: "myapex.key",
7489 public_key: "testkey.avbpubkey",
7490 private_key: "testkey.pem",
7491 }
7492
7493 android_app_set {
7494 name: "AppSet",
7495 set: "AppSet.apks",
7496 }`)
7497 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007498 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007499 content := bundleConfigRule.Args["content"]
7500 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7501 s := mod.Rule("apexRule").Args["copy_commands"]
7502 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007503 if len(copyCmds) != 4 {
7504 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007505 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007506 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7507 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007508 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7509 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007510
7511 // Ensure that canned_fs_config has an entry for the app set zip file
7512 generateFsRule := mod.Rule("generateFsConfig")
7513 cmd := generateFsRule.RuleParams.Command
7514 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007515}
7516
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007517func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007518 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007519 apex_set {
7520 name: "myapex",
7521 filename: "foo_v2.apex",
7522 sanitized: {
7523 none: { set: "myapex.apks", },
7524 hwaddress: { set: "myapex.hwasan.apks", },
7525 },
Paul Duffin24704672021-04-06 16:09:30 +01007526 }
7527 `
7528 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007529
Paul Duffin24704672021-04-06 16:09:30 +01007530 // Check that the extractor produces the correct output file from the correct input file.
7531 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007532
Paul Duffin24704672021-04-06 16:09:30 +01007533 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7534 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007535
Paul Duffin24704672021-04-06 16:09:30 +01007536 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7537
7538 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007539 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7540 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007541
7542 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007543}
7544
Pranav Guptaeba03b02022-09-27 00:27:08 +00007545func TestApexSetApksModuleAssignment(t *testing.T) {
7546 ctx := testApex(t, `
7547 apex_set {
7548 name: "myapex",
7549 set: ":myapex_apks_file",
7550 }
7551
7552 filegroup {
7553 name: "myapex_apks_file",
7554 srcs: ["myapex.apks"],
7555 }
7556 `)
7557
7558 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7559
7560 // Check that the extractor produces the correct apks file from the input module
7561 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.apks"
7562 extractedApex := m.Output(extractorOutput)
7563
7564 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7565}
7566
Paul Duffin89f570a2021-06-16 01:42:33 +01007567func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007568 t.Helper()
7569
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007570 bp := `
7571 java_library {
7572 name: "some-updatable-apex-lib",
7573 srcs: ["a.java"],
7574 sdk_version: "current",
7575 apex_available: [
7576 "some-updatable-apex",
7577 ],
satayevabcd5972021-08-06 17:49:46 +01007578 permitted_packages: ["some.updatable.apex.lib"],
Spandan Dascc9d9422023-04-06 18:07:43 +00007579 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007580 }
7581
7582 java_library {
7583 name: "some-non-updatable-apex-lib",
7584 srcs: ["a.java"],
7585 apex_available: [
7586 "some-non-updatable-apex",
7587 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007588 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007589 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007590 }
7591
7592 bootclasspath_fragment {
7593 name: "some-non-updatable-fragment",
7594 contents: ["some-non-updatable-apex-lib"],
7595 apex_available: [
7596 "some-non-updatable-apex",
7597 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007598 hidden_api: {
7599 split_packages: ["*"],
7600 },
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007601 }
7602
7603 java_library {
7604 name: "some-platform-lib",
7605 srcs: ["a.java"],
7606 sdk_version: "current",
7607 installable: true,
7608 }
7609
7610 java_library {
7611 name: "some-art-lib",
7612 srcs: ["a.java"],
7613 sdk_version: "current",
7614 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007615 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007616 ],
7617 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007618 compile_dex: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007619 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007620 }
7621
7622 apex {
7623 name: "some-updatable-apex",
7624 key: "some-updatable-apex.key",
7625 java_libs: ["some-updatable-apex-lib"],
7626 updatable: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007627 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007628 }
7629
7630 apex {
7631 name: "some-non-updatable-apex",
7632 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007633 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007634 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007635 }
7636
7637 apex_key {
7638 name: "some-updatable-apex.key",
7639 }
7640
7641 apex_key {
7642 name: "some-non-updatable-apex.key",
7643 }
7644
7645 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007646 name: "com.android.art.debug",
7647 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007648 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007649 updatable: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007650 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007651 }
7652
Paul Duffinf23bc472021-04-27 12:42:20 +01007653 bootclasspath_fragment {
7654 name: "art-bootclasspath-fragment",
7655 image_name: "art",
7656 contents: ["some-art-lib"],
7657 apex_available: [
7658 "com.android.art.debug",
7659 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007660 hidden_api: {
7661 split_packages: ["*"],
7662 },
Paul Duffinf23bc472021-04-27 12:42:20 +01007663 }
7664
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007665 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007666 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007667 }
7668
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007669 filegroup {
7670 name: "some-updatable-apex-file_contexts",
7671 srcs: [
7672 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7673 ],
7674 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007675
7676 filegroup {
7677 name: "some-non-updatable-apex-file_contexts",
7678 srcs: [
7679 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7680 ],
7681 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007682 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007683
Paul Duffin89f570a2021-06-16 01:42:33 +01007684 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007685}
7686
Paul Duffin89f570a2021-06-16 01:42:33 +01007687func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007688 t.Helper()
7689
Paul Duffin55607122021-03-30 23:32:51 +01007690 fs := android.MockFS{
7691 "a.java": nil,
7692 "a.jar": nil,
7693 "apex_manifest.json": nil,
7694 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007695 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007696 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7697 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7698 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007699 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007700 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007701
Paul Duffin55607122021-03-30 23:32:51 +01007702 errorHandler := android.FixtureExpectsNoErrors
7703 if errmsg != "" {
7704 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007705 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007706
Paul Duffin55607122021-03-30 23:32:51 +01007707 result := android.GroupFixturePreparers(
7708 cc.PrepareForTestWithCcDefaultModules,
7709 java.PrepareForTestWithHiddenApiBuildComponents,
7710 java.PrepareForTestWithJavaDefaultModules,
7711 java.PrepareForTestWithJavaSdkLibraryFiles,
7712 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007713 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007714 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007715 android.FixtureModifyMockFS(func(fs android.MockFS) {
7716 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7717 insert := ""
7718 for _, fragment := range fragments {
7719 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7720 }
7721 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7722 platform_bootclasspath {
7723 name: "platform-bootclasspath",
7724 fragments: [
7725 %s
7726 ],
7727 }
7728 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007729 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007730 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007731 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007732 ).
7733 ExtendWithErrorHandler(errorHandler).
7734 RunTestWithBp(t, bp)
7735
7736 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007737}
7738
Paul Duffin5556c5f2022-06-09 17:32:21 +00007739func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007740 preparers := android.GroupFixturePreparers(
7741 java.PrepareForTestWithJavaDefaultModules,
7742 PrepareForTestWithApexBuildComponents,
7743 ).
7744 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7745 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7746
7747 bpBase := `
7748 apex_set {
7749 name: "com.android.myapex",
7750 installable: true,
7751 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7752 set: "myapex.apks",
7753 }
7754
7755 apex_set {
7756 name: "com.mycompany.android.myapex",
7757 apex_name: "com.android.myapex",
7758 installable: true,
7759 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7760 set: "company-myapex.apks",
7761 }
7762
7763 prebuilt_bootclasspath_fragment {
7764 name: "my-bootclasspath-fragment",
7765 apex_available: ["com.android.myapex"],
7766 %s
7767 }
7768 `
7769
7770 t.Run("java_import", func(t *testing.T) {
7771 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7772 java_import {
7773 name: "libfoo",
7774 jars: ["libfoo.jar"],
7775 apex_available: ["com.android.myapex"],
7776 }
7777 `)
7778 })
7779
7780 t.Run("java_sdk_library_import", func(t *testing.T) {
7781 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7782 java_sdk_library_import {
7783 name: "libfoo",
7784 public: {
7785 jars: ["libbar.jar"],
7786 },
7787 apex_available: ["com.android.myapex"],
7788 }
7789 `)
7790 })
7791
7792 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7793 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7794 image_name: "art",
7795 contents: ["libfoo"],
7796 `)+`
7797 java_sdk_library_import {
7798 name: "libfoo",
7799 public: {
7800 jars: ["libbar.jar"],
7801 },
7802 apex_available: ["com.android.myapex"],
7803 }
7804 `)
7805 })
7806}
7807
Paul Duffin5556c5f2022-06-09 17:32:21 +00007808func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
7809 preparers := android.GroupFixturePreparers(
7810 java.PrepareForTestWithJavaDefaultModules,
7811 PrepareForTestWithApexBuildComponents,
7812 )
7813
7814 bpBase := `
7815 apex_set {
7816 name: "com.android.myapex",
7817 installable: true,
7818 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7819 set: "myapex.apks",
7820 }
7821
7822 apex_set {
7823 name: "com.android.myapex_compressed",
7824 apex_name: "com.android.myapex",
7825 installable: true,
7826 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7827 set: "myapex_compressed.apks",
7828 }
7829
7830 prebuilt_bootclasspath_fragment {
7831 name: "my-bootclasspath-fragment",
7832 apex_available: [
7833 "com.android.myapex",
7834 "com.android.myapex_compressed",
7835 ],
7836 hidden_api: {
7837 annotation_flags: "annotation-flags.csv",
7838 metadata: "metadata.csv",
7839 index: "index.csv",
7840 signature_patterns: "signature_patterns.csv",
7841 },
7842 %s
7843 }
7844 `
7845
7846 t.Run("java_import", func(t *testing.T) {
7847 result := preparers.RunTestWithBp(t,
7848 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7849 java_import {
7850 name: "libfoo",
7851 jars: ["libfoo.jar"],
7852 apex_available: [
7853 "com.android.myapex",
7854 "com.android.myapex_compressed",
7855 ],
7856 }
7857 `)
7858
7859 module := result.Module("libfoo", "android_common_com.android.myapex")
7860 usesLibraryDep := module.(java.UsesLibraryDependency)
7861 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7862 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7863 usesLibraryDep.DexJarBuildPath().Path())
7864 })
7865
7866 t.Run("java_sdk_library_import", func(t *testing.T) {
7867 result := preparers.RunTestWithBp(t,
7868 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7869 java_sdk_library_import {
7870 name: "libfoo",
7871 public: {
7872 jars: ["libbar.jar"],
7873 },
7874 apex_available: [
7875 "com.android.myapex",
7876 "com.android.myapex_compressed",
7877 ],
7878 compile_dex: true,
7879 }
7880 `)
7881
7882 module := result.Module("libfoo", "android_common_com.android.myapex")
7883 usesLibraryDep := module.(java.UsesLibraryDependency)
7884 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7885 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7886 usesLibraryDep.DexJarBuildPath().Path())
7887 })
7888
7889 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7890 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7891 image_name: "art",
7892 contents: ["libfoo"],
7893 `)+`
7894 java_sdk_library_import {
7895 name: "libfoo",
7896 public: {
7897 jars: ["libbar.jar"],
7898 },
7899 apex_available: [
7900 "com.android.myapex",
7901 "com.android.myapex_compressed",
7902 ],
7903 compile_dex: true,
7904 }
7905 `)
7906 })
7907}
7908
Jooyung Han548640b2020-04-27 12:10:30 +09007909func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7910 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7911 apex {
7912 name: "myapex",
7913 key: "myapex.key",
7914 updatable: true,
7915 }
7916
7917 apex_key {
7918 name: "myapex.key",
7919 public_key: "testkey.avbpubkey",
7920 private_key: "testkey.pem",
7921 }
7922 `)
7923}
7924
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007925func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7926 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7927 apex {
7928 name: "myapex",
7929 key: "myapex.key",
7930 }
7931
7932 apex_key {
7933 name: "myapex.key",
7934 public_key: "testkey.avbpubkey",
7935 private_key: "testkey.pem",
7936 }
7937 `)
7938}
7939
Jooyung Handfc864c2023-03-20 18:19:07 +09007940func Test_use_vndk_as_stable_shouldnt_be_used_for_updatable_vendor_apexes(t *testing.T) {
7941 testApexError(t, `"myapex" .*: use_vndk_as_stable: updatable APEXes can't use external VNDK libs`, `
Daniel Norman69109112021-12-02 12:52:42 -08007942 apex {
7943 name: "myapex",
7944 key: "myapex.key",
7945 updatable: true,
Jooyung Handfc864c2023-03-20 18:19:07 +09007946 use_vndk_as_stable: true,
Daniel Norman69109112021-12-02 12:52:42 -08007947 soc_specific: true,
7948 }
7949
7950 apex_key {
7951 name: "myapex.key",
7952 public_key: "testkey.avbpubkey",
7953 private_key: "testkey.pem",
7954 }
7955 `)
7956}
7957
Jooyung Han02873da2023-03-22 17:41:03 +09007958func Test_use_vndk_as_stable_shouldnt_be_used_with_min_sdk_version(t *testing.T) {
7959 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported when min_sdk_version is set`, `
7960 apex {
7961 name: "myapex",
7962 key: "myapex.key",
7963 updatable: false,
7964 min_sdk_version: "29",
7965 use_vndk_as_stable: true,
7966 vendor: true,
7967 }
7968
7969 apex_key {
7970 name: "myapex.key",
7971 public_key: "testkey.avbpubkey",
7972 private_key: "testkey.pem",
7973 }
7974 `)
7975}
7976
Jooyung Handfc864c2023-03-20 18:19:07 +09007977func Test_use_vndk_as_stable_shouldnt_be_used_for_non_vendor_apexes(t *testing.T) {
7978 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported for system/system_ext APEXes`, `
7979 apex {
7980 name: "myapex",
7981 key: "myapex.key",
7982 updatable: false,
7983 use_vndk_as_stable: true,
7984 }
7985
7986 apex_key {
7987 name: "myapex.key",
7988 public_key: "testkey.avbpubkey",
7989 private_key: "testkey.pem",
7990 }
7991 `)
7992}
7993
satayevb98371c2021-06-15 16:49:50 +01007994func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7995 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7996 apex {
7997 name: "myapex",
7998 key: "myapex.key",
7999 systemserverclasspath_fragments: [
8000 "mysystemserverclasspathfragment",
8001 ],
8002 min_sdk_version: "29",
8003 updatable: true,
8004 }
8005
8006 apex_key {
8007 name: "myapex.key",
8008 public_key: "testkey.avbpubkey",
8009 private_key: "testkey.pem",
8010 }
8011
8012 java_library {
8013 name: "foo",
8014 srcs: ["b.java"],
8015 min_sdk_version: "29",
8016 installable: true,
8017 apex_available: [
8018 "myapex",
8019 ],
8020 }
8021
8022 systemserverclasspath_fragment {
8023 name: "mysystemserverclasspathfragment",
8024 generate_classpaths_proto: false,
8025 contents: [
8026 "foo",
8027 ],
8028 apex_available: [
8029 "myapex",
8030 ],
8031 }
satayevabcd5972021-08-06 17:49:46 +01008032 `,
8033 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8034 )
satayevb98371c2021-06-15 16:49:50 +01008035}
8036
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008037func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01008038 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
8039 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
8040 // modules to be included in the BootJars.
8041 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
8042 return android.GroupFixturePreparers(
8043 dexpreopt.FixtureSetBootJars(bootJars...),
8044 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8045 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
8046 }),
8047 )
8048 }
8049
8050 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
8051 // same value. This can result in an invalid configuration as it allows non art apex jars to be
8052 // specified in the ArtApexJars configuration.
8053 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
8054 return android.GroupFixturePreparers(
8055 dexpreopt.FixtureSetArtBootJars(bootJars...),
8056 dexpreopt.FixtureSetBootJars(bootJars...),
8057 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8058 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
8059 }),
8060 )
8061 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008062
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008063 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008064 preparer := android.GroupFixturePreparers(
8065 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
8066 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8067 )
8068 fragments := []java.ApexVariantReference{
8069 {
8070 Apex: proptools.StringPtr("com.android.art.debug"),
8071 Module: proptools.StringPtr("art-bootclasspath-fragment"),
8072 },
8073 {
8074 Apex: proptools.StringPtr("some-non-updatable-apex"),
8075 Module: proptools.StringPtr("some-non-updatable-fragment"),
8076 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008077 }
satayevabcd5972021-08-06 17:49:46 +01008078 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008079 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008080
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008081 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01008082 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
8083 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01008084 preparer := android.GroupFixturePreparers(
8085 prepareSetBootJars("com.android.art.debug:some-art-lib"),
8086 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8087 )
Paul Duffin60264a02021-04-12 20:02:36 +01008088 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008089 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008090
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008091 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 +01008092 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 +01008093 // Update the dexpreopt ArtApexJars directly.
8094 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
8095 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008096 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008097
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008098 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 +01008099 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 +01008100 // Update the dexpreopt ArtApexJars directly.
8101 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
8102 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008103 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008104
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008105 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 +01008106 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 +01008107 preparer := android.GroupFixturePreparers(
8108 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
8109 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8110 )
Paul Duffin60264a02021-04-12 20:02:36 +01008111 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008112 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008113
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008114 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 +01008115 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01008116 fragment := java.ApexVariantReference{
8117 Apex: proptools.StringPtr("some-non-updatable-apex"),
8118 Module: proptools.StringPtr("some-non-updatable-fragment"),
8119 }
8120 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008121 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008122
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008123 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008124 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008125 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8126 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008127 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008128
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008129 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008130 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008131 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8132 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008133 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008134
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008135 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01008136 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01008137 // Update the dexpreopt ArtApexJars directly.
8138 preparer := prepareSetArtJars("platform:some-platform-lib")
8139 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008140 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008141
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008142 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008143 preparer := android.GroupFixturePreparers(
8144 java.FixtureConfigureBootJars("platform:some-platform-lib"),
8145 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8146 )
8147 fragments := []java.ApexVariantReference{
8148 {
8149 Apex: proptools.StringPtr("some-non-updatable-apex"),
8150 Module: proptools.StringPtr("some-non-updatable-fragment"),
8151 },
8152 }
8153 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008154 })
Paul Duffin064b70c2020-11-02 17:32:38 +00008155}
8156
8157func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008158 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008159 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008160 fragment := java.ApexVariantReference{
8161 Apex: proptools.StringPtr("myapex"),
8162 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8163 }
8164
Paul Duffin064b70c2020-11-02 17:32:38 +00008165 testDexpreoptWithApexes(t, `
8166 prebuilt_apex {
8167 name: "myapex" ,
8168 arch: {
8169 arm64: {
8170 src: "myapex-arm64.apex",
8171 },
8172 arm: {
8173 src: "myapex-arm.apex",
8174 },
8175 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008176 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8177 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008178
Paul Duffin89f570a2021-06-16 01:42:33 +01008179 prebuilt_bootclasspath_fragment {
8180 name: "my-bootclasspath-fragment",
8181 contents: ["libfoo"],
8182 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008183 hidden_api: {
8184 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8185 metadata: "my-bootclasspath-fragment/metadata.csv",
8186 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008187 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8188 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8189 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008190 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008191 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008192
Paul Duffin89f570a2021-06-16 01:42:33 +01008193 java_import {
8194 name: "libfoo",
8195 jars: ["libfoo.jar"],
8196 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008197 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008198 }
8199 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008200 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008201}
8202
Spandan Dasf14e2542021-11-12 00:01:37 +00008203func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008204 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008205 bp += `
8206 apex_key {
8207 name: "myapex.key",
8208 public_key: "testkey.avbpubkey",
8209 private_key: "testkey.pem",
8210 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008211 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008212 "lib1/src/A.java": nil,
8213 "lib2/src/B.java": nil,
8214 "system/sepolicy/apex/myapex-file_contexts": nil,
8215 }
8216
Paul Duffin45338f02021-03-30 23:07:52 +01008217 errorHandler := android.FixtureExpectsNoErrors
8218 if errmsg != "" {
8219 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008220 }
Colin Crossae8600b2020-10-29 17:09:13 -07008221
Paul Duffin45338f02021-03-30 23:07:52 +01008222 android.GroupFixturePreparers(
8223 android.PrepareForTestWithAndroidBuildComponents,
8224 java.PrepareForTestWithJavaBuildComponents,
8225 PrepareForTestWithApexBuildComponents,
8226 android.PrepareForTestWithNeverallowRules(rules),
8227 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008228 apexBootJars := make([]string, 0, len(bootJars))
8229 for _, apexBootJar := range bootJars {
8230 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008231 }
satayevd604b212021-07-21 14:23:52 +01008232 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008233 }),
8234 fs.AddToFixture(),
8235 ).
8236 ExtendWithErrorHandler(errorHandler).
8237 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008238}
8239
8240func TestApexPermittedPackagesRules(t *testing.T) {
8241 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008242 name string
8243 expectedError string
8244 bp string
8245 bootJars []string
8246 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008247 }{
8248
8249 {
8250 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8251 expectedError: "",
8252 bp: `
8253 java_library {
8254 name: "bcp_lib1",
8255 srcs: ["lib1/src/*.java"],
8256 permitted_packages: ["foo.bar"],
8257 apex_available: ["myapex"],
8258 sdk_version: "none",
8259 system_modules: "none",
8260 }
8261 java_library {
8262 name: "nonbcp_lib2",
8263 srcs: ["lib2/src/*.java"],
8264 apex_available: ["myapex"],
8265 permitted_packages: ["a.b"],
8266 sdk_version: "none",
8267 system_modules: "none",
8268 }
8269 apex {
8270 name: "myapex",
8271 key: "myapex.key",
8272 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008273 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008274 }`,
8275 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008276 bcpPermittedPackages: map[string][]string{
8277 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008278 "foo.bar",
8279 },
8280 },
8281 },
8282 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008283 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008284 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 +01008285 bp: `
8286 java_library {
8287 name: "bcp_lib1",
8288 srcs: ["lib1/src/*.java"],
8289 apex_available: ["myapex"],
8290 permitted_packages: ["foo.bar"],
8291 sdk_version: "none",
8292 system_modules: "none",
8293 }
8294 java_library {
8295 name: "bcp_lib2",
8296 srcs: ["lib2/src/*.java"],
8297 apex_available: ["myapex"],
8298 permitted_packages: ["foo.bar", "bar.baz"],
8299 sdk_version: "none",
8300 system_modules: "none",
8301 }
8302 apex {
8303 name: "myapex",
8304 key: "myapex.key",
8305 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008306 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008307 }
8308 `,
8309 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008310 bcpPermittedPackages: map[string][]string{
8311 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008312 "foo.bar",
8313 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008314 "bcp_lib2": []string{
8315 "foo.bar",
8316 },
8317 },
8318 },
8319 {
8320 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8321 expectedError: "",
8322 bp: `
8323 java_library {
8324 name: "bcp_lib_restricted",
8325 srcs: ["lib1/src/*.java"],
8326 apex_available: ["myapex"],
8327 permitted_packages: ["foo.bar"],
8328 sdk_version: "none",
8329 min_sdk_version: "29",
8330 system_modules: "none",
8331 }
8332 java_library {
8333 name: "bcp_lib_unrestricted",
8334 srcs: ["lib2/src/*.java"],
8335 apex_available: ["myapex"],
8336 permitted_packages: ["foo.bar", "bar.baz"],
8337 sdk_version: "none",
8338 min_sdk_version: "29",
8339 system_modules: "none",
8340 }
8341 apex {
8342 name: "myapex",
8343 key: "myapex.key",
8344 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8345 updatable: true,
8346 min_sdk_version: "29",
8347 }
8348 `,
8349 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8350 bcpPermittedPackages: map[string][]string{
8351 "bcp_lib1_non_updateable": []string{
8352 "foo.bar",
8353 },
8354 // 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 +01008355 },
8356 },
8357 }
8358 for _, tc := range testcases {
8359 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008360 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8361 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008362 })
8363 }
8364}
8365
Jiyong Park62304bb2020-04-13 16:19:48 +09008366func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008367 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008368 apex {
8369 name: "myapex",
8370 key: "myapex.key",
8371 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008372 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008373 }
8374
8375 apex_key {
8376 name: "myapex.key",
8377 public_key: "testkey.avbpubkey",
8378 private_key: "testkey.pem",
8379 }
8380
8381 cc_library {
8382 name: "mylib",
8383 srcs: ["mylib.cpp"],
8384 system_shared_libs: [],
8385 stl: "none",
8386 stubs: {
8387 versions: ["1"],
8388 },
8389 apex_available: ["myapex"],
8390 }
8391
8392 cc_library {
8393 name: "myprivlib",
8394 srcs: ["mylib.cpp"],
8395 system_shared_libs: [],
8396 stl: "none",
8397 apex_available: ["myapex"],
8398 }
8399
8400
8401 cc_test {
8402 name: "mytest",
8403 gtest: false,
8404 srcs: ["mylib.cpp"],
8405 system_shared_libs: [],
8406 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008407 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008408 test_for: ["myapex"]
8409 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008410
8411 cc_library {
8412 name: "mytestlib",
8413 srcs: ["mylib.cpp"],
8414 system_shared_libs: [],
8415 shared_libs: ["mylib", "myprivlib"],
8416 stl: "none",
8417 test_for: ["myapex"],
8418 }
8419
8420 cc_benchmark {
8421 name: "mybench",
8422 srcs: ["mylib.cpp"],
8423 system_shared_libs: [],
8424 shared_libs: ["mylib", "myprivlib"],
8425 stl: "none",
8426 test_for: ["myapex"],
8427 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008428 `)
8429
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008430 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008431 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008432 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8433 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8434 }
8435
8436 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008437 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008438 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8439 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8440 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8441}
Jiyong Park46a512f2020-12-04 18:02:13 +09008442
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008443func TestIndirectTestFor(t *testing.T) {
8444 ctx := testApex(t, `
8445 apex {
8446 name: "myapex",
8447 key: "myapex.key",
8448 native_shared_libs: ["mylib", "myprivlib"],
8449 updatable: false,
8450 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008451
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008452 apex_key {
8453 name: "myapex.key",
8454 public_key: "testkey.avbpubkey",
8455 private_key: "testkey.pem",
8456 }
8457
8458 cc_library {
8459 name: "mylib",
8460 srcs: ["mylib.cpp"],
8461 system_shared_libs: [],
8462 stl: "none",
8463 stubs: {
8464 versions: ["1"],
8465 },
8466 apex_available: ["myapex"],
8467 }
8468
8469 cc_library {
8470 name: "myprivlib",
8471 srcs: ["mylib.cpp"],
8472 system_shared_libs: [],
8473 stl: "none",
8474 shared_libs: ["mylib"],
8475 apex_available: ["myapex"],
8476 }
8477
8478 cc_library {
8479 name: "mytestlib",
8480 srcs: ["mylib.cpp"],
8481 system_shared_libs: [],
8482 shared_libs: ["myprivlib"],
8483 stl: "none",
8484 test_for: ["myapex"],
8485 }
8486 `)
8487
8488 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008489 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008490 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8491 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8492 }
8493
8494 // The platform variant of mytestlib links to the platform variant of the
8495 // internal myprivlib.
8496 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8497
8498 // The platform variant of myprivlib links to the platform variant of mylib
8499 // and bypasses its stubs.
8500 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 +09008501}
8502
Martin Stjernholmec009002021-03-27 15:18:31 +00008503func TestTestForForLibInOtherApex(t *testing.T) {
8504 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8505 _ = testApex(t, `
8506 apex {
8507 name: "com.android.art",
8508 key: "myapex.key",
8509 native_shared_libs: ["mylib"],
8510 updatable: false,
8511 }
8512
8513 apex {
8514 name: "com.android.art.debug",
8515 key: "myapex.key",
8516 native_shared_libs: ["mylib", "mytestlib"],
8517 updatable: false,
8518 }
8519
8520 apex_key {
8521 name: "myapex.key",
8522 public_key: "testkey.avbpubkey",
8523 private_key: "testkey.pem",
8524 }
8525
8526 cc_library {
8527 name: "mylib",
8528 srcs: ["mylib.cpp"],
8529 system_shared_libs: [],
8530 stl: "none",
8531 stubs: {
8532 versions: ["1"],
8533 },
8534 apex_available: ["com.android.art", "com.android.art.debug"],
8535 }
8536
8537 cc_library {
8538 name: "mytestlib",
8539 srcs: ["mylib.cpp"],
8540 system_shared_libs: [],
8541 shared_libs: ["mylib"],
8542 stl: "none",
8543 apex_available: ["com.android.art.debug"],
8544 test_for: ["com.android.art"],
8545 }
8546 `,
8547 android.MockFS{
8548 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8549 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8550 }.AddToFixture())
8551}
8552
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008553// TODO(jungjw): Move this to proptools
8554func intPtr(i int) *int {
8555 return &i
8556}
8557
8558func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008559 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008560 apex_set {
8561 name: "myapex",
8562 set: "myapex.apks",
8563 filename: "foo_v2.apex",
8564 overrides: ["foo"],
8565 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008566 `,
8567 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8568 variables.Platform_sdk_version = intPtr(30)
8569 }),
8570 android.FixtureModifyConfig(func(config android.Config) {
8571 config.Targets[android.Android] = []android.Target{
8572 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8573 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8574 }
8575 }),
8576 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008577
Paul Duffin24704672021-04-06 16:09:30 +01008578 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008579
8580 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008581 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008582 actual := extractedApex.Args["abis"]
8583 expected := "ARMEABI_V7A,ARM64_V8A"
8584 if actual != expected {
8585 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8586 }
8587 actual = extractedApex.Args["sdk-version"]
8588 expected = "30"
8589 if actual != expected {
8590 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8591 }
8592
Paul Duffin6717d882021-06-15 19:09:41 +01008593 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008594 a := m.Module().(*ApexSet)
8595 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008596 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008597 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8598 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8599 }
8600}
8601
Anton Hansson805e0a52022-11-25 14:06:46 +00008602func TestApexSet_NativeBridge(t *testing.T) {
8603 ctx := testApex(t, `
8604 apex_set {
8605 name: "myapex",
8606 set: "myapex.apks",
8607 filename: "foo_v2.apex",
8608 overrides: ["foo"],
8609 }
8610 `,
8611 android.FixtureModifyConfig(func(config android.Config) {
8612 config.Targets[android.Android] = []android.Target{
8613 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8614 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8615 }
8616 }),
8617 )
8618
8619 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
8620
8621 // Check extract_apks tool parameters. No native bridge arch expected
8622 extractedApex := m.Output("extracted/myapex.apks")
8623 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8624}
8625
Jiyong Park7d95a512020-05-10 15:16:24 +09008626func TestNoStaticLinkingToStubsLib(t *testing.T) {
8627 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8628 apex {
8629 name: "myapex",
8630 key: "myapex.key",
8631 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008632 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008633 }
8634
8635 apex_key {
8636 name: "myapex.key",
8637 public_key: "testkey.avbpubkey",
8638 private_key: "testkey.pem",
8639 }
8640
8641 cc_library {
8642 name: "mylib",
8643 srcs: ["mylib.cpp"],
8644 static_libs: ["otherlib"],
8645 system_shared_libs: [],
8646 stl: "none",
8647 apex_available: [ "myapex" ],
8648 }
8649
8650 cc_library {
8651 name: "otherlib",
8652 srcs: ["mylib.cpp"],
8653 system_shared_libs: [],
8654 stl: "none",
8655 stubs: {
8656 versions: ["1", "2", "3"],
8657 },
8658 apex_available: [ "myapex" ],
8659 }
8660 `)
8661}
8662
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008663func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008664 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008665 apex {
8666 name: "myapex",
8667 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008668 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008669 custom_sign_tool: "sign_myapex",
8670 }
8671
8672 apex_key {
8673 name: "myapex.key",
8674 public_key: "testkey.avbpubkey",
8675 private_key: "testkey.pem",
8676 }
8677 `)
8678
8679 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8680 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8681 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"`)
8682}
8683
8684func TestApexKeysTxtOverrides(t *testing.T) {
8685 ctx := testApex(t, `
8686 apex {
8687 name: "myapex",
8688 key: "myapex.key",
8689 updatable: false,
8690 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008691 }
8692
8693 apex_key {
8694 name: "myapex.key",
8695 public_key: "testkey.avbpubkey",
8696 private_key: "testkey.pem",
8697 }
8698
8699 prebuilt_apex {
8700 name: "myapex",
8701 prefer: true,
8702 arch: {
8703 arm64: {
8704 src: "myapex-arm64.apex",
8705 },
8706 arm: {
8707 src: "myapex-arm.apex",
8708 },
8709 },
8710 }
8711
8712 apex_set {
8713 name: "myapex_set",
8714 set: "myapex.apks",
8715 filename: "myapex_set.apex",
8716 overrides: ["myapex"],
8717 }
8718 `)
8719
8720 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8721 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8722 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 +09008723 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 +09008724}
8725
Jooyung Han938b5932020-06-20 12:47:47 +09008726func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008727 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008728 apex {
8729 name: "myapex",
8730 key: "myapex.key",
8731 apps: ["app"],
8732 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008733 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008734 }
8735
8736 apex_key {
8737 name: "myapex.key",
8738 public_key: "testkey.avbpubkey",
8739 private_key: "testkey.pem",
8740 }
8741
8742 android_app {
8743 name: "app",
8744 srcs: ["foo/bar/MyClass.java"],
8745 package_name: "foo",
8746 sdk_version: "none",
8747 system_modules: "none",
8748 apex_available: [ "myapex" ],
8749 }
8750 `, withFiles(map[string][]byte{
8751 "sub/Android.bp": []byte(`
8752 override_apex {
8753 name: "override_myapex",
8754 base: "myapex",
8755 apps: ["override_app"],
8756 allowed_files: ":allowed",
8757 }
8758 // Overridable "path" property should be referenced indirectly
8759 filegroup {
8760 name: "allowed",
8761 srcs: ["allowed.txt"],
8762 }
8763 override_android_app {
8764 name: "override_app",
8765 base: "app",
8766 package_name: "bar",
8767 }
8768 `),
8769 }))
8770
8771 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8772 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8773 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8774 }
8775
8776 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8777 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8778 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8779 }
8780}
8781
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008782func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008783 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008784 apex {
8785 name: "myapex",
8786 key: "myapex.key",
8787 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008788 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008789 }
8790
8791 apex_key {
8792 name: "myapex.key",
8793 public_key: "testkey.avbpubkey",
8794 private_key: "testkey.pem",
8795 }
8796
8797 cc_library {
8798 name: "mylib",
8799 srcs: ["mylib.cpp"],
8800 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008801 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008802 },
8803 apex_available: ["myapex"],
8804 }
8805
8806 cc_prebuilt_library_shared {
8807 name: "mylib",
8808 prefer: false,
8809 srcs: ["prebuilt.so"],
8810 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008811 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008812 },
8813 apex_available: ["myapex"],
8814 }
8815 `)
8816}
8817
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008818func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008819 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008820 apex {
8821 name: "myapex",
8822 key: "myapex.key",
8823 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008824 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008825 }
8826 apex_key {
8827 name: "myapex.key",
8828 public_key: "testkey.avbpubkey",
8829 private_key: "testkey.pem",
8830 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008831 `,
8832 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8833 variables.CompressedApex = proptools.BoolPtr(true)
8834 }),
8835 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008836
8837 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8838 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8839
8840 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8841 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8842
8843 // Make sure output of bundle is .capex
8844 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8845 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8846
8847 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008848 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008849 var builder strings.Builder
8850 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8851 androidMk := builder.String()
8852 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8853}
8854
Martin Stjernholm2856c662020-12-02 15:03:42 +00008855func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008856 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008857 apex {
8858 name: "myapex",
8859 key: "myapex.key",
8860 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008861 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008862 }
8863
8864 apex_key {
8865 name: "myapex.key",
8866 public_key: "testkey.avbpubkey",
8867 private_key: "testkey.pem",
8868 }
8869
8870 cc_library {
8871 name: "mylib",
8872 srcs: ["mylib.cpp"],
8873 apex_available: ["myapex"],
8874 shared_libs: ["otherlib"],
8875 system_shared_libs: [],
8876 }
8877
8878 cc_library {
8879 name: "otherlib",
8880 srcs: ["mylib.cpp"],
8881 stubs: {
8882 versions: ["current"],
8883 },
8884 }
8885
8886 cc_prebuilt_library_shared {
8887 name: "otherlib",
8888 prefer: true,
8889 srcs: ["prebuilt.so"],
8890 stubs: {
8891 versions: ["current"],
8892 },
8893 }
8894 `)
8895
8896 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008897 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008898 var builder strings.Builder
8899 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8900 androidMk := builder.String()
8901
8902 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8903 // a thing there.
Diwas Sharmabb9202e2023-01-26 18:42:21 +00008904 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 +00008905}
8906
Jiyong Parke3867542020-12-03 17:28:25 +09008907func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008908 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008909 apex {
8910 name: "myapex",
8911 key: "myapex.key",
8912 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008913 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008914 }
8915
8916 apex_key {
8917 name: "myapex.key",
8918 public_key: "testkey.avbpubkey",
8919 private_key: "testkey.pem",
8920 }
8921
8922 cc_library {
8923 name: "mylib",
8924 srcs: ["mylib.cpp"],
8925 system_shared_libs: [],
8926 stl: "none",
8927 apex_available: ["myapex"],
8928 shared_libs: ["mylib2"],
8929 target: {
8930 apex: {
8931 exclude_shared_libs: ["mylib2"],
8932 },
8933 },
8934 }
8935
8936 cc_library {
8937 name: "mylib2",
8938 srcs: ["mylib.cpp"],
8939 system_shared_libs: [],
8940 stl: "none",
8941 }
8942 `)
8943
8944 // Check if mylib is linked to mylib2 for the non-apex target
8945 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8946 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8947
8948 // Make sure that the link doesn't occur for the apex target
8949 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8950 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8951
8952 // It shouldn't appear in the copy cmd as well.
8953 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8954 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8955}
8956
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008957func TestPrebuiltStubLibDep(t *testing.T) {
8958 bpBase := `
8959 apex {
8960 name: "myapex",
8961 key: "myapex.key",
8962 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008963 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008964 }
8965 apex_key {
8966 name: "myapex.key",
8967 public_key: "testkey.avbpubkey",
8968 private_key: "testkey.pem",
8969 }
8970 cc_library {
8971 name: "mylib",
8972 srcs: ["mylib.cpp"],
8973 apex_available: ["myapex"],
8974 shared_libs: ["stublib"],
8975 system_shared_libs: [],
8976 }
8977 apex {
8978 name: "otherapex",
8979 enabled: %s,
8980 key: "myapex.key",
8981 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008982 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008983 }
8984 `
8985
8986 stublibSourceBp := `
8987 cc_library {
8988 name: "stublib",
8989 srcs: ["mylib.cpp"],
8990 apex_available: ["otherapex"],
8991 system_shared_libs: [],
8992 stl: "none",
8993 stubs: {
8994 versions: ["1"],
8995 },
8996 }
8997 `
8998
8999 stublibPrebuiltBp := `
9000 cc_prebuilt_library_shared {
9001 name: "stublib",
9002 srcs: ["prebuilt.so"],
9003 apex_available: ["otherapex"],
9004 stubs: {
9005 versions: ["1"],
9006 },
9007 %s
9008 }
9009 `
9010
9011 tests := []struct {
9012 name string
9013 stublibBp string
9014 usePrebuilt bool
9015 modNames []string // Modules to collect AndroidMkEntries for
9016 otherApexEnabled []string
9017 }{
9018 {
9019 name: "only_source",
9020 stublibBp: stublibSourceBp,
9021 usePrebuilt: false,
9022 modNames: []string{"stublib"},
9023 otherApexEnabled: []string{"true", "false"},
9024 },
9025 {
9026 name: "source_preferred",
9027 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9028 usePrebuilt: false,
9029 modNames: []string{"stublib", "prebuilt_stublib"},
9030 otherApexEnabled: []string{"true", "false"},
9031 },
9032 {
9033 name: "prebuilt_preferred",
9034 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9035 usePrebuilt: true,
9036 modNames: []string{"stublib", "prebuilt_stublib"},
9037 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9038 },
9039 {
9040 name: "only_prebuilt",
9041 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9042 usePrebuilt: true,
9043 modNames: []string{"stublib"},
9044 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9045 },
9046 }
9047
9048 for _, test := range tests {
9049 t.Run(test.name, func(t *testing.T) {
9050 for _, otherApexEnabled := range test.otherApexEnabled {
9051 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009052 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009053
9054 type modAndMkEntries struct {
9055 mod *cc.Module
9056 mkEntries android.AndroidMkEntries
9057 }
9058 entries := []*modAndMkEntries{}
9059
9060 // Gather shared lib modules that are installable
9061 for _, modName := range test.modNames {
9062 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9063 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9064 continue
9065 }
9066 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08009067 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009068 continue
9069 }
Colin Crossaa255532020-07-03 13:18:24 -07009070 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009071 if ent.Disabled {
9072 continue
9073 }
9074 entries = append(entries, &modAndMkEntries{
9075 mod: mod,
9076 mkEntries: ent,
9077 })
9078 }
9079 }
9080 }
9081
9082 var entry *modAndMkEntries = nil
9083 for _, ent := range entries {
9084 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9085 if entry != nil {
9086 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9087 } else {
9088 entry = ent
9089 }
9090 }
9091 }
9092
9093 if entry == nil {
9094 t.Errorf("AndroidMk entry for \"stublib\" missing")
9095 } else {
9096 isPrebuilt := entry.mod.Prebuilt() != nil
9097 if isPrebuilt != test.usePrebuilt {
9098 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9099 }
9100 if !entry.mod.IsStubs() {
9101 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9102 }
9103 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9104 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9105 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009106 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009107 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009108 if !android.InList(expected, cflags) {
9109 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9110 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009111 }
9112 })
9113 }
9114 })
9115 }
9116}
9117
Martin Stjernholmdf298b32021-05-21 20:57:29 +01009118func TestHostApexInHostOnlyBuild(t *testing.T) {
9119 testApex(t, `
9120 apex {
9121 name: "myapex",
9122 host_supported: true,
9123 key: "myapex.key",
9124 updatable: false,
9125 payload_type: "zip",
9126 }
9127 apex_key {
9128 name: "myapex.key",
9129 public_key: "testkey.avbpubkey",
9130 private_key: "testkey.pem",
9131 }
9132 `,
9133 android.FixtureModifyConfig(func(config android.Config) {
9134 // We may not have device targets in all builds, e.g. in
9135 // prebuilts/build-tools/build-prebuilts.sh
9136 config.Targets[android.Android] = []android.Target{}
9137 }))
9138}
9139
Colin Crossc33e5212021-05-25 18:16:02 -07009140func TestApexJavaCoverage(t *testing.T) {
9141 bp := `
9142 apex {
9143 name: "myapex",
9144 key: "myapex.key",
9145 java_libs: ["mylib"],
9146 bootclasspath_fragments: ["mybootclasspathfragment"],
9147 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9148 updatable: false,
9149 }
9150
9151 apex_key {
9152 name: "myapex.key",
9153 public_key: "testkey.avbpubkey",
9154 private_key: "testkey.pem",
9155 }
9156
9157 java_library {
9158 name: "mylib",
9159 srcs: ["mylib.java"],
9160 apex_available: ["myapex"],
9161 compile_dex: true,
9162 }
9163
9164 bootclasspath_fragment {
9165 name: "mybootclasspathfragment",
9166 contents: ["mybootclasspathlib"],
9167 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009168 hidden_api: {
9169 split_packages: ["*"],
9170 },
Colin Crossc33e5212021-05-25 18:16:02 -07009171 }
9172
9173 java_library {
9174 name: "mybootclasspathlib",
9175 srcs: ["mybootclasspathlib.java"],
9176 apex_available: ["myapex"],
9177 compile_dex: true,
9178 }
9179
9180 systemserverclasspath_fragment {
9181 name: "mysystemserverclasspathfragment",
9182 contents: ["mysystemserverclasspathlib"],
9183 apex_available: ["myapex"],
9184 }
9185
9186 java_library {
9187 name: "mysystemserverclasspathlib",
9188 srcs: ["mysystemserverclasspathlib.java"],
9189 apex_available: ["myapex"],
9190 compile_dex: true,
9191 }
9192 `
9193
9194 result := android.GroupFixturePreparers(
9195 PrepareForTestWithApexBuildComponents,
9196 prepareForTestWithMyapex,
9197 java.PrepareForTestWithJavaDefaultModules,
9198 android.PrepareForTestWithAndroidBuildComponents,
9199 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009200 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9201 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009202 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009203 ).RunTest(t)
9204
9205 // Make sure jacoco ran on both mylib and mybootclasspathlib
9206 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9207 t.Errorf("Failed to find jacoco rule for mylib")
9208 }
9209 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9210 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9211 }
9212 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9213 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9214 }
9215}
9216
Jiyong Park192600a2021-08-03 07:52:17 +00009217func TestProhibitStaticExecutable(t *testing.T) {
9218 testApexError(t, `executable mybin is static`, `
9219 apex {
9220 name: "myapex",
9221 key: "myapex.key",
9222 binaries: ["mybin"],
9223 min_sdk_version: "29",
9224 }
9225
9226 apex_key {
9227 name: "myapex.key",
9228 public_key: "testkey.avbpubkey",
9229 private_key: "testkey.pem",
9230 }
9231
9232 cc_binary {
9233 name: "mybin",
9234 srcs: ["mylib.cpp"],
9235 relative_install_path: "foo/bar",
9236 static_executable: true,
9237 system_shared_libs: [],
9238 stl: "none",
9239 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009240 min_sdk_version: "29",
9241 }
9242 `)
9243
9244 testApexError(t, `executable mybin.rust is static`, `
9245 apex {
9246 name: "myapex",
9247 key: "myapex.key",
9248 binaries: ["mybin.rust"],
9249 min_sdk_version: "29",
9250 }
9251
9252 apex_key {
9253 name: "myapex.key",
9254 public_key: "testkey.avbpubkey",
9255 private_key: "testkey.pem",
9256 }
9257
9258 rust_binary {
9259 name: "mybin.rust",
9260 srcs: ["foo.rs"],
9261 static_executable: true,
9262 apex_available: ["myapex"],
9263 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009264 }
9265 `)
9266}
9267
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009268func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9269 ctx := testApex(t, `
9270 apex {
9271 name: "myapex",
9272 key: "myapex.key",
9273 updatable: false,
9274 java_libs: ["foo"],
9275 }
9276
9277 apex_key {
9278 name: "myapex.key",
9279 public_key: "testkey.avbpubkey",
9280 private_key: "testkey.pem",
9281 }
9282
9283 java_library {
9284 name: "foo",
9285 srcs: ["foo.java"],
9286 apex_available: ["myapex"],
9287 installable: true,
9288 }
9289 `,
9290 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9291 )
9292
9293 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9294 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9295 var builder strings.Builder
9296 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9297 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009298 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 +00009299}
9300
9301func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9302 ctx := testApex(t, `
9303 prebuilt_apex {
9304 name: "myapex",
9305 arch: {
9306 arm64: {
9307 src: "myapex-arm64.apex",
9308 },
9309 arm: {
9310 src: "myapex-arm.apex",
9311 },
9312 },
9313 exported_java_libs: ["foo"],
9314 }
9315
9316 java_import {
9317 name: "foo",
9318 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009319 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009320 }
9321 `,
9322 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9323 )
9324
9325 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9326 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9327 mainModuleEntries := entriesList[0]
9328 android.AssertArrayString(t,
9329 "LOCAL_REQUIRED_MODULES",
9330 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9331 []string{
9332 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9333 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9334 })
9335}
9336
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009337func TestAndroidMk_RequiredModules(t *testing.T) {
9338 ctx := testApex(t, `
9339 apex {
9340 name: "myapex",
9341 key: "myapex.key",
9342 updatable: false,
9343 java_libs: ["foo"],
9344 required: ["otherapex"],
9345 }
9346
9347 apex {
9348 name: "otherapex",
9349 key: "myapex.key",
9350 updatable: false,
9351 java_libs: ["foo"],
9352 required: ["otherapex"],
9353 }
9354
9355 apex_key {
9356 name: "myapex.key",
9357 public_key: "testkey.avbpubkey",
9358 private_key: "testkey.pem",
9359 }
9360
9361 java_library {
9362 name: "foo",
9363 srcs: ["foo.java"],
9364 apex_available: ["myapex", "otherapex"],
9365 installable: true,
9366 }
9367 `)
9368
9369 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9370 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9371 var builder strings.Builder
9372 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9373 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009374 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009375}
9376
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009377func TestAndroidMk_RequiredDeps(t *testing.T) {
9378 ctx := testApex(t, `
9379 apex {
9380 name: "myapex",
9381 key: "myapex.key",
9382 updatable: false,
9383 }
9384
9385 apex_key {
9386 name: "myapex.key",
9387 public_key: "testkey.avbpubkey",
9388 private_key: "testkey.pem",
9389 }
9390 `)
9391
9392 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009393 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009394 data := android.AndroidMkDataForTest(t, ctx, bundle)
9395 var builder strings.Builder
9396 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9397 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009398 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009399
9400 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009401 flattenedBundle.makeModulesToInstall = append(flattenedBundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009402 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
9403 var flattenedBuilder strings.Builder
9404 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
9405 flattenedAndroidMk := flattenedBuilder.String()
Sasha Smundakdcb61292022-12-08 10:41:33 -08009406 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex.flattened apex_pubkey.myapex.flattened foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009407}
9408
Jooyung Hana6d36672022-02-24 13:58:07 +09009409func TestApexOutputFileProducer(t *testing.T) {
9410 for _, tc := range []struct {
9411 name string
9412 ref string
9413 expected_data []string
9414 }{
9415 {
9416 name: "test_using_output",
9417 ref: ":myapex",
9418 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
9419 },
9420 {
9421 name: "test_using_apex",
9422 ref: ":myapex{.apex}",
9423 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
9424 },
9425 } {
9426 t.Run(tc.name, func(t *testing.T) {
9427 ctx := testApex(t, `
9428 apex {
9429 name: "myapex",
9430 key: "myapex.key",
9431 compressible: true,
9432 updatable: false,
9433 }
9434
9435 apex_key {
9436 name: "myapex.key",
9437 public_key: "testkey.avbpubkey",
9438 private_key: "testkey.pem",
9439 }
9440
9441 java_test {
9442 name: "`+tc.name+`",
9443 srcs: ["a.java"],
9444 data: ["`+tc.ref+`"],
9445 }
9446 `,
9447 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9448 variables.CompressedApex = proptools.BoolPtr(true)
9449 }))
9450 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9451 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9452 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9453 })
9454 }
9455}
9456
satayev758968a2021-12-06 11:42:40 +00009457func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9458 preparer := android.GroupFixturePreparers(
9459 PrepareForTestWithApexBuildComponents,
9460 prepareForTestWithMyapex,
9461 java.PrepareForTestWithJavaSdkLibraryFiles,
9462 java.PrepareForTestWithJavaDefaultModules,
9463 android.PrepareForTestWithAndroidBuildComponents,
9464 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9465 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9466 )
9467
9468 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9469 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9470 preparer.RunTestWithBp(t, `
9471 apex {
9472 name: "myapex",
9473 key: "myapex.key",
9474 bootclasspath_fragments: ["mybootclasspathfragment"],
9475 min_sdk_version: "30",
9476 updatable: false,
9477 }
9478
9479 apex_key {
9480 name: "myapex.key",
9481 public_key: "testkey.avbpubkey",
9482 private_key: "testkey.pem",
9483 }
9484
9485 bootclasspath_fragment {
9486 name: "mybootclasspathfragment",
9487 contents: ["mybootclasspathlib"],
9488 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009489 hidden_api: {
9490 split_packages: ["*"],
9491 },
satayev758968a2021-12-06 11:42:40 +00009492 }
9493
9494 java_sdk_library {
9495 name: "mybootclasspathlib",
9496 srcs: ["mybootclasspathlib.java"],
9497 apex_available: ["myapex"],
9498 compile_dex: true,
9499 unsafe_ignore_missing_latest_api: true,
9500 min_sdk_version: "31",
9501 static_libs: ["util"],
9502 }
9503
9504 java_library {
9505 name: "util",
9506 srcs: ["a.java"],
9507 apex_available: ["myapex"],
9508 min_sdk_version: "31",
9509 static_libs: ["another_util"],
9510 }
9511
9512 java_library {
9513 name: "another_util",
9514 srcs: ["a.java"],
9515 min_sdk_version: "31",
9516 apex_available: ["myapex"],
9517 }
9518 `)
9519 })
9520
9521 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9522 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9523 preparer.RunTestWithBp(t, `
9524 apex {
9525 name: "myapex",
9526 key: "myapex.key",
9527 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9528 min_sdk_version: "30",
9529 updatable: false,
9530 }
9531
9532 apex_key {
9533 name: "myapex.key",
9534 public_key: "testkey.avbpubkey",
9535 private_key: "testkey.pem",
9536 }
9537
9538 systemserverclasspath_fragment {
9539 name: "mysystemserverclasspathfragment",
9540 contents: ["mysystemserverclasspathlib"],
9541 apex_available: ["myapex"],
9542 }
9543
9544 java_sdk_library {
9545 name: "mysystemserverclasspathlib",
9546 srcs: ["mysystemserverclasspathlib.java"],
9547 apex_available: ["myapex"],
9548 compile_dex: true,
9549 min_sdk_version: "32",
9550 unsafe_ignore_missing_latest_api: true,
9551 static_libs: ["util"],
9552 }
9553
9554 java_library {
9555 name: "util",
9556 srcs: ["a.java"],
9557 apex_available: ["myapex"],
9558 min_sdk_version: "31",
9559 static_libs: ["another_util"],
9560 }
9561
9562 java_library {
9563 name: "another_util",
9564 srcs: ["a.java"],
9565 min_sdk_version: "31",
9566 apex_available: ["myapex"],
9567 }
9568 `)
9569 })
9570
9571 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9572 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9573 RunTestWithBp(t, `
9574 apex {
9575 name: "myapex",
9576 key: "myapex.key",
9577 bootclasspath_fragments: ["mybootclasspathfragment"],
9578 min_sdk_version: "30",
9579 updatable: false,
9580 }
9581
9582 apex_key {
9583 name: "myapex.key",
9584 public_key: "testkey.avbpubkey",
9585 private_key: "testkey.pem",
9586 }
9587
9588 bootclasspath_fragment {
9589 name: "mybootclasspathfragment",
9590 contents: ["mybootclasspathlib"],
9591 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009592 hidden_api: {
9593 split_packages: ["*"],
9594 },
satayev758968a2021-12-06 11:42:40 +00009595 }
9596
9597 java_sdk_library {
9598 name: "mybootclasspathlib",
9599 srcs: ["mybootclasspathlib.java"],
9600 apex_available: ["myapex"],
9601 compile_dex: true,
9602 unsafe_ignore_missing_latest_api: true,
9603 }
9604 `)
9605 })
9606
9607 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9608 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9609 RunTestWithBp(t, `
9610 apex {
9611 name: "myapex",
9612 key: "myapex.key",
9613 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9614 min_sdk_version: "30",
9615 updatable: false,
9616 }
9617
9618 apex_key {
9619 name: "myapex.key",
9620 public_key: "testkey.avbpubkey",
9621 private_key: "testkey.pem",
9622 }
9623
9624 systemserverclasspath_fragment {
9625 name: "mysystemserverclasspathfragment",
9626 contents: ["mysystemserverclasspathlib"],
9627 apex_available: ["myapex"],
9628 }
9629
9630 java_sdk_library {
9631 name: "mysystemserverclasspathlib",
9632 srcs: ["mysystemserverclasspathlib.java"],
9633 apex_available: ["myapex"],
9634 compile_dex: true,
9635 unsafe_ignore_missing_latest_api: true,
9636 }
9637 `)
9638 })
9639}
9640
Jiakai Zhang6decef92022-01-12 17:56:19 +00009641// Verifies that the APEX depends on all the Make modules in the list.
9642func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9643 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9644 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009645 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009646 }
9647}
9648
9649// Verifies that the APEX does not depend on any of the Make modules in the list.
9650func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9651 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9652 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009653 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009654 }
9655}
9656
Cole Faust1021ccd2023-02-26 21:15:25 -08009657// TODO(b/193460475): Re-enable this test
9658//func TestApexStrictUpdtabilityLint(t *testing.T) {
9659// bpTemplate := `
9660// apex {
9661// name: "myapex",
9662// key: "myapex.key",
9663// java_libs: ["myjavalib"],
9664// updatable: %v,
9665// min_sdk_version: "29",
9666// }
9667// apex_key {
9668// name: "myapex.key",
9669// }
9670// java_library {
9671// name: "myjavalib",
9672// srcs: ["MyClass.java"],
9673// apex_available: [ "myapex" ],
9674// lint: {
9675// strict_updatability_linting: %v,
9676// },
9677// sdk_version: "current",
9678// min_sdk_version: "29",
9679// }
9680// `
9681// fs := android.MockFS{
9682// "lint-baseline.xml": nil,
9683// }
9684//
9685// testCases := []struct {
9686// testCaseName string
9687// apexUpdatable bool
9688// javaStrictUpdtabilityLint bool
9689// lintFileExists bool
9690// disallowedFlagExpected bool
9691// }{
9692// {
9693// testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9694// apexUpdatable: true,
9695// javaStrictUpdtabilityLint: true,
9696// lintFileExists: false,
9697// disallowedFlagExpected: false,
9698// },
9699// {
9700// testCaseName: "non-updatable apex respects strict_updatability of javalib",
9701// apexUpdatable: false,
9702// javaStrictUpdtabilityLint: false,
9703// lintFileExists: true,
9704// disallowedFlagExpected: false,
9705// },
9706// {
9707// testCaseName: "non-updatable apex respects strict updatability of javalib",
9708// apexUpdatable: false,
9709// javaStrictUpdtabilityLint: true,
9710// lintFileExists: true,
9711// disallowedFlagExpected: true,
9712// },
9713// {
9714// testCaseName: "updatable apex sets strict updatability of javalib to true",
9715// apexUpdatable: true,
9716// javaStrictUpdtabilityLint: false, // will be set to true by mutator
9717// lintFileExists: true,
9718// disallowedFlagExpected: true,
9719// },
9720// }
9721//
9722// for _, testCase := range testCases {
9723// bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9724// fixtures := []android.FixturePreparer{}
9725// if testCase.lintFileExists {
9726// fixtures = append(fixtures, fs.AddToFixture())
9727// }
9728//
9729// result := testApex(t, bp, fixtures...)
9730// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9731// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9732// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9733//
9734// if disallowedFlagActual != testCase.disallowedFlagExpected {
9735// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9736// }
9737// }
9738//}
9739//
9740//func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9741// bp := `
9742// apex {
9743// name: "myapex",
9744// key: "myapex.key",
9745// java_libs: ["myjavalib"],
9746// updatable: true,
9747// min_sdk_version: "29",
9748// }
9749// apex_key {
9750// name: "myapex.key",
9751// }
9752// java_library {
9753// name: "myjavalib",
9754// srcs: ["MyClass.java"],
9755// apex_available: [ "myapex" ],
9756// sdk_version: "current",
9757// min_sdk_version: "29",
9758// }
9759// `
9760//
9761// testCases := []struct {
9762// testCaseName string
9763// moduleDirectory string
9764// disallowedFlagExpected bool
9765// }{
9766// {
9767// testCaseName: "lintable module defined outside libcore",
9768// moduleDirectory: "",
9769// disallowedFlagExpected: true,
9770// },
9771// {
9772// testCaseName: "lintable module defined in libcore root directory",
9773// moduleDirectory: "libcore/",
9774// disallowedFlagExpected: false,
9775// },
9776// {
9777// testCaseName: "lintable module defined in libcore child directory",
9778// moduleDirectory: "libcore/childdir/",
9779// disallowedFlagExpected: true,
9780// },
9781// }
9782//
9783// for _, testCase := range testCases {
9784// lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9785// bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9786// result := testApex(t, "", lintFileCreator, bpFileCreator)
9787// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9788// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9789// cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9790// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9791//
9792// if disallowedFlagActual != testCase.disallowedFlagExpected {
9793// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9794// }
9795// }
9796//}
9797//
9798//// checks transtive deps of an apex coming from bootclasspath_fragment
9799//func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9800// bp := `
9801// apex {
9802// name: "myapex",
9803// key: "myapex.key",
9804// bootclasspath_fragments: ["mybootclasspathfragment"],
9805// updatable: true,
9806// min_sdk_version: "29",
9807// }
9808// apex_key {
9809// name: "myapex.key",
9810// }
9811// bootclasspath_fragment {
9812// name: "mybootclasspathfragment",
9813// contents: ["myjavalib"],
9814// apex_available: ["myapex"],
9815// hidden_api: {
9816// split_packages: ["*"],
9817// },
9818// }
9819// java_library {
9820// name: "myjavalib",
9821// srcs: ["MyClass.java"],
9822// apex_available: [ "myapex" ],
9823// sdk_version: "current",
9824// min_sdk_version: "29",
9825// compile_dex: true,
9826// }
9827// `
9828// fs := android.MockFS{
9829// "lint-baseline.xml": nil,
9830// }
9831//
9832// result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9833// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9834// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9835// if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9836// t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9837// }
9838//}
Spandan Das66773252022-01-15 00:23:18 +00009839
Spandan Das42e89502022-05-06 22:12:55 +00009840// updatable apexes should propagate updatable=true to its apps
9841func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9842 bp := `
9843 apex {
9844 name: "myapex",
9845 key: "myapex.key",
9846 updatable: %v,
9847 apps: [
9848 "myapp",
9849 ],
9850 min_sdk_version: "30",
9851 }
9852 apex_key {
9853 name: "myapex.key",
9854 }
9855 android_app {
9856 name: "myapp",
9857 updatable: %v,
9858 apex_available: [
9859 "myapex",
9860 ],
9861 sdk_version: "current",
9862 min_sdk_version: "30",
9863 }
9864 `
9865 testCases := []struct {
9866 name string
9867 apex_is_updatable_bp bool
9868 app_is_updatable_bp bool
9869 app_is_updatable_expected bool
9870 }{
9871 {
9872 name: "Non-updatable apex respects updatable property of non-updatable app",
9873 apex_is_updatable_bp: false,
9874 app_is_updatable_bp: false,
9875 app_is_updatable_expected: false,
9876 },
9877 {
9878 name: "Non-updatable apex respects updatable property of updatable app",
9879 apex_is_updatable_bp: false,
9880 app_is_updatable_bp: true,
9881 app_is_updatable_expected: true,
9882 },
9883 {
9884 name: "Updatable apex respects updatable property of updatable app",
9885 apex_is_updatable_bp: true,
9886 app_is_updatable_bp: true,
9887 app_is_updatable_expected: true,
9888 },
9889 {
9890 name: "Updatable apex sets updatable=true on non-updatable app",
9891 apex_is_updatable_bp: true,
9892 app_is_updatable_bp: false,
9893 app_is_updatable_expected: true,
9894 },
9895 }
9896 for _, testCase := range testCases {
9897 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
9898 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
9899 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
9900 }
9901}
9902
Kiyoung Kim487689e2022-07-26 09:48:22 +09009903func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
9904 bp := `
9905 apex {
9906 name: "myapex",
9907 key: "myapex.key",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009908 native_shared_libs: ["libbaz"],
9909 binaries: ["binfoo"],
Kiyoung Kim487689e2022-07-26 09:48:22 +09009910 min_sdk_version: "29",
9911 }
9912 apex_key {
9913 name: "myapex.key",
9914 }
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009915 cc_binary {
9916 name: "binfoo",
9917 shared_libs: ["libbar", "libbaz", "libqux",],
Kiyoung Kim487689e2022-07-26 09:48:22 +09009918 apex_available: ["myapex"],
9919 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009920 recovery_available: false,
9921 }
9922 cc_library {
9923 name: "libbar",
9924 srcs: ["libbar.cc"],
9925 stubs: {
9926 symbol_file: "libbar.map.txt",
9927 versions: [
9928 "29",
9929 ],
9930 },
9931 }
9932 cc_library {
9933 name: "libbaz",
9934 srcs: ["libbaz.cc"],
9935 apex_available: ["myapex"],
9936 min_sdk_version: "29",
9937 stubs: {
9938 symbol_file: "libbaz.map.txt",
9939 versions: [
9940 "29",
9941 ],
9942 },
Kiyoung Kim487689e2022-07-26 09:48:22 +09009943 }
9944 cc_api_library {
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009945 name: "libbar",
9946 src: "libbar_stub.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +09009947 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009948 variants: ["apex.29"],
9949 }
9950 cc_api_variant {
9951 name: "libbar",
9952 variant: "apex",
9953 version: "29",
9954 src: "libbar_apex_29.so",
9955 }
9956 cc_api_library {
9957 name: "libbaz",
9958 src: "libbaz_stub.so",
9959 min_sdk_version: "29",
9960 variants: ["apex.29"],
9961 }
9962 cc_api_variant {
9963 name: "libbaz",
9964 variant: "apex",
9965 version: "29",
9966 src: "libbaz_apex_29.so",
9967 }
9968 cc_api_library {
9969 name: "libqux",
9970 src: "libqux_stub.so",
9971 min_sdk_version: "29",
9972 variants: ["apex.29"],
9973 }
9974 cc_api_variant {
9975 name: "libqux",
9976 variant: "apex",
9977 version: "29",
9978 src: "libqux_apex_29.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +09009979 }
9980 api_imports {
9981 name: "api_imports",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009982 apex_shared_libs: [
9983 "libbar",
9984 "libbaz",
9985 "libqux",
Kiyoung Kim487689e2022-07-26 09:48:22 +09009986 ],
Kiyoung Kim487689e2022-07-26 09:48:22 +09009987 }
9988 `
9989 result := testApex(t, bp)
9990
9991 hasDep := func(m android.Module, wantDep android.Module) bool {
9992 t.Helper()
9993 var found bool
9994 result.VisitDirectDeps(m, func(dep blueprint.Module) {
9995 if dep == wantDep {
9996 found = true
9997 }
9998 })
9999 return found
10000 }
10001
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010002 // Library defines stubs and cc_api_library should be used with cc_api_library
10003 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Module()
10004 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10005 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
Kiyoung Kim487689e2022-07-26 09:48:22 +090010006
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010007 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10008 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
Kiyoung Kim487689e2022-07-26 09:48:22 +090010009
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010010 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Rule("ld").Args["libFlags"]
10011 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10012 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10013 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
10014
10015 // Library defined in the same APEX should be linked with original definition instead of cc_api_library
10016 libbazApexVariant := result.ModuleForTests("libbaz", "android_arm64_armv8-a_shared_apex29").Module()
10017 libbazApiImportCoreVariant := result.ModuleForTests("libbaz.apiimport", "android_arm64_armv8-a_shared").Module()
10018 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even from same APEX", true, hasDep(binfooApexVariant, libbazApiImportCoreVariant))
10019 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbazApexVariant))
10020
10021 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbaz.so")
10022 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbaz.apiimport.so")
10023 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbaz.apex.29.apiimport.so")
10024
10025 // cc_api_library defined without original library should be linked with cc_api_library
10026 libquxApiImportApexVariant := result.ModuleForTests("libqux.apiimport", "android_arm64_armv8-a_shared").Module()
10027 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even original library definition does not exist", true, hasDep(binfooApexVariant, libquxApiImportApexVariant))
10028 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libqux.apex.29.apiimport.so")
10029}
10030
10031func TestPlatformBinaryBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10032 bp := `
10033 apex {
10034 name: "myapex",
10035 key: "myapex.key",
10036 native_shared_libs: ["libbar"],
10037 min_sdk_version: "29",
10038 }
10039 apex_key {
10040 name: "myapex.key",
10041 }
10042 cc_binary {
10043 name: "binfoo",
10044 shared_libs: ["libbar"],
10045 recovery_available: false,
10046 }
10047 cc_library {
10048 name: "libbar",
10049 srcs: ["libbar.cc"],
10050 apex_available: ["myapex"],
10051 min_sdk_version: "29",
10052 stubs: {
10053 symbol_file: "libbar.map.txt",
10054 versions: [
10055 "29",
10056 ],
10057 },
10058 }
10059 cc_api_library {
10060 name: "libbar",
10061 src: "libbar_stub.so",
10062 variants: ["apex.29"],
10063 }
10064 cc_api_variant {
10065 name: "libbar",
10066 variant: "apex",
10067 version: "29",
10068 src: "libbar_apex_29.so",
10069 }
10070 api_imports {
10071 name: "api_imports",
10072 apex_shared_libs: [
10073 "libbar",
10074 ],
10075 }
10076 `
10077
10078 result := testApex(t, bp)
10079
10080 hasDep := func(m android.Module, wantDep android.Module) bool {
10081 t.Helper()
10082 var found bool
10083 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10084 if dep == wantDep {
10085 found = true
10086 }
10087 })
10088 return found
10089 }
10090
10091 // Library defines stubs and cc_api_library should be used with cc_api_library
10092 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Module()
10093 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10094 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
10095
10096 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10097 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
10098
10099 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
10100 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10101 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10102 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
Kiyoung Kim487689e2022-07-26 09:48:22 +090010103}
Dennis Shend4f5d932023-01-31 20:27:21 +000010104
10105func TestTrimmedApex(t *testing.T) {
10106 bp := `
10107 apex {
10108 name: "myapex",
10109 key: "myapex.key",
10110 native_shared_libs: ["libfoo","libbaz"],
10111 min_sdk_version: "29",
10112 trim_against: "mydcla",
10113 }
10114 apex {
10115 name: "mydcla",
10116 key: "myapex.key",
10117 native_shared_libs: ["libfoo","libbar"],
10118 min_sdk_version: "29",
10119 file_contexts: ":myapex-file_contexts",
10120 dynamic_common_lib_apex: true,
10121 }
10122 apex_key {
10123 name: "myapex.key",
10124 }
10125 cc_library {
10126 name: "libfoo",
10127 shared_libs: ["libc"],
10128 apex_available: ["myapex","mydcla"],
10129 min_sdk_version: "29",
10130 }
10131 cc_library {
10132 name: "libbar",
10133 shared_libs: ["libc"],
10134 apex_available: ["myapex","mydcla"],
10135 min_sdk_version: "29",
10136 }
10137 cc_library {
10138 name: "libbaz",
10139 shared_libs: ["libc"],
10140 apex_available: ["myapex","mydcla"],
10141 min_sdk_version: "29",
10142 }
10143 cc_api_library {
10144 name: "libc",
10145 src: "libc.so",
10146 min_sdk_version: "29",
10147 recovery_available: true,
10148 }
10149 api_imports {
10150 name: "api_imports",
10151 shared_libs: [
10152 "libc",
10153 ],
10154 header_libs: [],
10155 }
10156 `
10157 ctx := testApex(t, bp)
10158 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10159 apexRule := module.MaybeRule("apexRule")
10160 if apexRule.Rule == nil {
10161 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10162 }
10163
10164 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
10165 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("TrimmedApexRule")
10166 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10167 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10168 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10169 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10170}
Jingwen Chendea7a642023-03-28 11:30:50 +000010171
10172func TestCannedFsConfig(t *testing.T) {
10173 ctx := testApex(t, `
10174 apex {
10175 name: "myapex",
10176 key: "myapex.key",
10177 updatable: false,
10178 }
10179
10180 apex_key {
10181 name: "myapex.key",
10182 public_key: "testkey.avbpubkey",
10183 private_key: "testkey.pem",
10184 }`)
10185 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10186 generateFsRule := mod.Rule("generateFsConfig")
10187 cmd := generateFsRule.RuleParams.Command
10188
10189 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10190}
10191
10192func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10193 ctx := testApex(t, `
10194 apex {
10195 name: "myapex",
10196 key: "myapex.key",
10197 canned_fs_config: "my_config",
10198 updatable: false,
10199 }
10200
10201 apex_key {
10202 name: "myapex.key",
10203 public_key: "testkey.avbpubkey",
10204 private_key: "testkey.pem",
10205 }`)
10206 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10207 generateFsRule := mod.Rule("generateFsConfig")
10208 cmd := generateFsRule.RuleParams.Command
10209
10210 // Ensure that canned_fs_config has "cat my_config" at the end
10211 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10212}