blob: ebb7c37dca88f2c3ce1608f5833108fe8258e650 [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
Alex Light5098a612018-11-29 17:12:15 -0800787func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800788 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800789 apex {
790 name: "myapex",
791 key: "myapex.key",
792 payload_type: "zip",
793 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000794 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800795 }
796
797 apex_key {
798 name: "myapex.key",
799 public_key: "testkey.avbpubkey",
800 private_key: "testkey.pem",
801 }
802
803 cc_library {
804 name: "mylib",
805 srcs: ["mylib.cpp"],
806 shared_libs: ["mylib2"],
807 system_shared_libs: [],
808 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000809 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800810 }
811
812 cc_library {
813 name: "mylib2",
814 srcs: ["mylib.cpp"],
815 system_shared_libs: [],
816 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000817 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800818 }
819 `)
820
Sundong Ahnabb64432019-10-22 13:58:29 +0900821 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800822 copyCmds := zipApexRule.Args["copy_commands"]
823
824 // Ensure that main rule creates an output
825 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
826
827 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700828 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800829
830 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700831 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800832
833 // Ensure that both direct and indirect deps are copied into apex
834 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
835 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900836}
837
838func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800839 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900840 apex {
841 name: "myapex",
842 key: "myapex.key",
843 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900844 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000845 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900846 }
847
848 apex_key {
849 name: "myapex.key",
850 public_key: "testkey.avbpubkey",
851 private_key: "testkey.pem",
852 }
853
854 cc_library {
855 name: "mylib",
856 srcs: ["mylib.cpp"],
857 shared_libs: ["mylib2", "mylib3"],
858 system_shared_libs: [],
859 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000860 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900861 }
862
863 cc_library {
864 name: "mylib2",
865 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900866 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900867 system_shared_libs: [],
868 stl: "none",
869 stubs: {
870 versions: ["1", "2", "3"],
871 },
872 }
873
874 cc_library {
875 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900876 srcs: ["mylib.cpp"],
877 shared_libs: ["mylib4"],
878 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900879 stl: "none",
880 stubs: {
881 versions: ["10", "11", "12"],
882 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000883 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900884 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900885
886 cc_library {
887 name: "mylib4",
888 srcs: ["mylib.cpp"],
889 system_shared_libs: [],
890 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000891 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900892 }
Jiyong Park105dc322021-06-11 17:22:09 +0900893
894 rust_binary {
895 name: "foo.rust",
896 srcs: ["foo.rs"],
897 shared_libs: ["libfoo.shared_from_rust"],
898 prefer_rlib: true,
899 apex_available: ["myapex"],
900 }
901
902 cc_library_shared {
903 name: "libfoo.shared_from_rust",
904 srcs: ["mylib.cpp"],
905 system_shared_libs: [],
906 stl: "none",
907 stubs: {
908 versions: ["10", "11", "12"],
909 },
910 }
911
Jiyong Park25fc6a92018-11-18 18:02:45 +0900912 `)
913
Sundong Ahnabb64432019-10-22 13:58:29 +0900914 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900915 copyCmds := apexRule.Args["copy_commands"]
916
917 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800918 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900919
920 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800921 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900922
923 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800924 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900925
Colin Crossaede88c2020-08-11 12:17:01 -0700926 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900927
928 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900929 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900930 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900931 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900932
933 // 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 -0700934 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900935 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700936 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900937
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700938 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
939 // is replaced by sharing of "cFlags" in cc/builder.go.
940 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
941 // module variable representing "cflags". So it was not detected by ensureNotContains.
942 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
943 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
944 // including the original cflags's "-include mylib.h".
945 //
Jiyong Park64379952018-12-13 18:37:29 +0900946 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700947 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
948 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900949
Jiyong Park85cc35a2022-07-17 11:30:47 +0900950 // Ensure that genstub for platform-provided lib is invoked with --systemapi
951 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
952 // Ensure that genstub for apex-provided lib is invoked with --apex
953 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +0900954
Jooyung Hana57af4a2020-01-23 05:36:59 +0000955 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900956 "lib64/mylib.so",
957 "lib64/mylib3.so",
958 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +0900959 "bin/foo.rust",
960 "lib64/libc++.so", // by the implicit dependency from foo.rust
961 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900962 })
Jiyong Park105dc322021-06-11 17:22:09 +0900963
964 // Ensure that stub dependency from a rust module is not included
965 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
966 // The rust module is linked to the stub cc library
967 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
968 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
969 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +0900970
971 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
972 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900973}
974
Jiyong Park1bc84122021-06-22 20:23:05 +0900975func TestApexCanUsePrivateApis(t *testing.T) {
976 ctx := testApex(t, `
977 apex {
978 name: "myapex",
979 key: "myapex.key",
980 native_shared_libs: ["mylib"],
981 binaries: ["foo.rust"],
982 updatable: false,
983 platform_apis: true,
984 }
985
986 apex_key {
987 name: "myapex.key",
988 public_key: "testkey.avbpubkey",
989 private_key: "testkey.pem",
990 }
991
992 cc_library {
993 name: "mylib",
994 srcs: ["mylib.cpp"],
995 shared_libs: ["mylib2"],
996 system_shared_libs: [],
997 stl: "none",
998 apex_available: [ "myapex" ],
999 }
1000
1001 cc_library {
1002 name: "mylib2",
1003 srcs: ["mylib.cpp"],
1004 cflags: ["-include mylib.h"],
1005 system_shared_libs: [],
1006 stl: "none",
1007 stubs: {
1008 versions: ["1", "2", "3"],
1009 },
1010 }
1011
1012 rust_binary {
1013 name: "foo.rust",
1014 srcs: ["foo.rs"],
1015 shared_libs: ["libfoo.shared_from_rust"],
1016 prefer_rlib: true,
1017 apex_available: ["myapex"],
1018 }
1019
1020 cc_library_shared {
1021 name: "libfoo.shared_from_rust",
1022 srcs: ["mylib.cpp"],
1023 system_shared_libs: [],
1024 stl: "none",
1025 stubs: {
1026 versions: ["10", "11", "12"],
1027 },
1028 }
1029 `)
1030
1031 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1032 copyCmds := apexRule.Args["copy_commands"]
1033
1034 // Ensure that indirect stubs dep is not included
1035 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1036 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1037
1038 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1039 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001040 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001041 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1042 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Parkd4a00632022-04-12 12:23:20 +09001043 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustc").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001044 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1045 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1046}
1047
Colin Cross7812fd32020-09-25 12:35:10 -07001048func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1049 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001050 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001051 apex {
1052 name: "myapex",
1053 key: "myapex.key",
1054 native_shared_libs: ["mylib", "mylib3"],
1055 min_sdk_version: "29",
1056 }
1057
1058 apex_key {
1059 name: "myapex.key",
1060 public_key: "testkey.avbpubkey",
1061 private_key: "testkey.pem",
1062 }
1063
1064 cc_library {
1065 name: "mylib",
1066 srcs: ["mylib.cpp"],
1067 shared_libs: ["mylib2", "mylib3"],
1068 system_shared_libs: [],
1069 stl: "none",
1070 apex_available: [ "myapex" ],
1071 min_sdk_version: "28",
1072 }
1073
1074 cc_library {
1075 name: "mylib2",
1076 srcs: ["mylib.cpp"],
1077 cflags: ["-include mylib.h"],
1078 system_shared_libs: [],
1079 stl: "none",
1080 stubs: {
1081 versions: ["28", "29", "30", "current"],
1082 },
1083 min_sdk_version: "28",
1084 }
1085
1086 cc_library {
1087 name: "mylib3",
1088 srcs: ["mylib.cpp"],
1089 shared_libs: ["mylib4"],
1090 system_shared_libs: [],
1091 stl: "none",
1092 stubs: {
1093 versions: ["28", "29", "30", "current"],
1094 },
1095 apex_available: [ "myapex" ],
1096 min_sdk_version: "28",
1097 }
1098
1099 cc_library {
1100 name: "mylib4",
1101 srcs: ["mylib.cpp"],
1102 system_shared_libs: [],
1103 stl: "none",
1104 apex_available: [ "myapex" ],
1105 min_sdk_version: "28",
1106 }
1107 `)
1108
1109 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1110 copyCmds := apexRule.Args["copy_commands"]
1111
1112 // Ensure that direct non-stubs dep is always included
1113 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1114
1115 // Ensure that indirect stubs dep is not included
1116 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1117
1118 // Ensure that direct stubs dep is included
1119 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1120
1121 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1122
Jiyong Park55549df2021-02-26 23:57:23 +09001123 // Ensure that mylib is linking with the latest version of stub for mylib2
1124 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001125 // ... and not linking to the non-stub (impl) variant of mylib2
1126 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1127
1128 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1129 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1130 // .. and not linking to the stubs variant of mylib3
1131 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1132
1133 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001134 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001135 ensureNotContains(t, mylib2Cflags, "-include ")
1136
Jiyong Park85cc35a2022-07-17 11:30:47 +09001137 // Ensure that genstub is invoked with --systemapi
1138 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001139
1140 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1141 "lib64/mylib.so",
1142 "lib64/mylib3.so",
1143 "lib64/mylib4.so",
1144 })
1145}
1146
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001147func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1148 t.Parallel()
1149 // myapex (Z)
1150 // mylib -----------------.
1151 // |
1152 // otherapex (29) |
1153 // libstub's versions: 29 Z current
1154 // |
1155 // <platform> |
1156 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001157 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001158 apex {
1159 name: "myapex",
1160 key: "myapex.key",
1161 native_shared_libs: ["mylib"],
1162 min_sdk_version: "Z", // non-final
1163 }
1164
1165 cc_library {
1166 name: "mylib",
1167 srcs: ["mylib.cpp"],
1168 shared_libs: ["libstub"],
1169 apex_available: ["myapex"],
1170 min_sdk_version: "Z",
1171 }
1172
1173 apex_key {
1174 name: "myapex.key",
1175 public_key: "testkey.avbpubkey",
1176 private_key: "testkey.pem",
1177 }
1178
1179 apex {
1180 name: "otherapex",
1181 key: "myapex.key",
1182 native_shared_libs: ["libstub"],
1183 min_sdk_version: "29",
1184 }
1185
1186 cc_library {
1187 name: "libstub",
1188 srcs: ["mylib.cpp"],
1189 stubs: {
1190 versions: ["29", "Z", "current"],
1191 },
1192 apex_available: ["otherapex"],
1193 min_sdk_version: "29",
1194 }
1195
1196 // platform module depending on libstub from otherapex should use the latest stub("current")
1197 cc_library {
1198 name: "libplatform",
1199 srcs: ["mylib.cpp"],
1200 shared_libs: ["libstub"],
1201 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001202 `,
1203 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1204 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1205 variables.Platform_sdk_final = proptools.BoolPtr(false)
1206 variables.Platform_version_active_codenames = []string{"Z"}
1207 }),
1208 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001209
Jiyong Park55549df2021-02-26 23:57:23 +09001210 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001211 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001212 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001213 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001214 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001215
1216 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1217 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1218 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1219 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1220 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1221}
1222
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001223func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001224 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001225 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001226 name: "myapex2",
1227 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001228 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001229 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001230 }
1231
1232 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001233 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001234 public_key: "testkey.avbpubkey",
1235 private_key: "testkey.pem",
1236 }
1237
1238 cc_library {
1239 name: "mylib",
1240 srcs: ["mylib.cpp"],
1241 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001242 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001243 system_shared_libs: [],
1244 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001245 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001246 }
1247
1248 cc_library {
1249 name: "libfoo",
1250 srcs: ["mylib.cpp"],
1251 shared_libs: ["libbar"],
1252 system_shared_libs: [],
1253 stl: "none",
1254 stubs: {
1255 versions: ["10", "20", "30"],
1256 },
1257 }
1258
1259 cc_library {
1260 name: "libbar",
1261 srcs: ["mylib.cpp"],
1262 system_shared_libs: [],
1263 stl: "none",
1264 }
1265
Jiyong Park678c8812020-02-07 17:25:49 +09001266 cc_library_static {
1267 name: "libbaz",
1268 srcs: ["mylib.cpp"],
1269 system_shared_libs: [],
1270 stl: "none",
1271 apex_available: [ "myapex2" ],
1272 }
1273
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001274 `)
1275
Jiyong Park83dc74b2020-01-14 18:38:44 +09001276 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001277 copyCmds := apexRule.Args["copy_commands"]
1278
1279 // Ensure that direct non-stubs dep is always included
1280 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1281
1282 // Ensure that indirect stubs dep is not included
1283 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1284
1285 // Ensure that dependency of stubs is not included
1286 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1287
Colin Crossaede88c2020-08-11 12:17:01 -07001288 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001289
1290 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001291 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001292 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001293 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001294
Jiyong Park3ff16992019-12-27 14:11:47 +09001295 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001296
1297 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1298 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001299
Artur Satayeva8bd1132020-04-27 18:07:06 +01001300 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001301 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001302
Artur Satayeva8bd1132020-04-27 18:07:06 +01001303 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001304 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001305}
1306
Jooyung Hand3639552019-08-09 12:57:43 +09001307func TestApexWithRuntimeLibsDependency(t *testing.T) {
1308 /*
1309 myapex
1310 |
1311 v (runtime_libs)
1312 mylib ------+------> libfoo [provides stub]
1313 |
1314 `------> libbar
1315 */
Colin Cross1c460562021-02-16 17:55:47 -08001316 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001317 apex {
1318 name: "myapex",
1319 key: "myapex.key",
1320 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001321 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001322 }
1323
1324 apex_key {
1325 name: "myapex.key",
1326 public_key: "testkey.avbpubkey",
1327 private_key: "testkey.pem",
1328 }
1329
1330 cc_library {
1331 name: "mylib",
1332 srcs: ["mylib.cpp"],
1333 runtime_libs: ["libfoo", "libbar"],
1334 system_shared_libs: [],
1335 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001336 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001337 }
1338
1339 cc_library {
1340 name: "libfoo",
1341 srcs: ["mylib.cpp"],
1342 system_shared_libs: [],
1343 stl: "none",
1344 stubs: {
1345 versions: ["10", "20", "30"],
1346 },
1347 }
1348
1349 cc_library {
1350 name: "libbar",
1351 srcs: ["mylib.cpp"],
1352 system_shared_libs: [],
1353 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001354 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001355 }
1356
1357 `)
1358
Sundong Ahnabb64432019-10-22 13:58:29 +09001359 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001360 copyCmds := apexRule.Args["copy_commands"]
1361
1362 // Ensure that direct non-stubs dep is always included
1363 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1364
1365 // Ensure that indirect stubs dep is not included
1366 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1367
1368 // Ensure that runtime_libs dep in included
1369 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1370
Sundong Ahnabb64432019-10-22 13:58:29 +09001371 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001372 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1373 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001374
1375}
1376
Paul Duffina02cae32021-03-09 01:44:06 +00001377var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1378 cc.PrepareForTestWithCcBuildComponents,
1379 PrepareForTestWithApexBuildComponents,
1380 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001381 apex {
1382 name: "com.android.runtime",
1383 key: "com.android.runtime.key",
1384 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001385 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001386 }
1387
1388 apex_key {
1389 name: "com.android.runtime.key",
1390 public_key: "testkey.avbpubkey",
1391 private_key: "testkey.pem",
1392 }
Paul Duffina02cae32021-03-09 01:44:06 +00001393 `),
1394 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1395)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001396
Paul Duffina02cae32021-03-09 01:44:06 +00001397func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001398 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001399 cc_library {
1400 name: "libc",
1401 no_libcrt: true,
1402 nocrt: true,
1403 stl: "none",
1404 system_shared_libs: [],
1405 stubs: { versions: ["1"] },
1406 apex_available: ["com.android.runtime"],
1407
1408 sanitize: {
1409 hwaddress: true,
1410 }
1411 }
1412
1413 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001414 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001415 no_libcrt: true,
1416 nocrt: true,
1417 stl: "none",
1418 system_shared_libs: [],
1419 srcs: [""],
1420 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001421 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001422
1423 sanitize: {
1424 never: true,
1425 },
Paul Duffina02cae32021-03-09 01:44:06 +00001426 } `)
1427 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001428
1429 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1430 "lib64/bionic/libc.so",
1431 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1432 })
1433
Colin Cross4c4c1be2022-02-10 11:41:18 -08001434 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001435
1436 installed := hwasan.Description("install libclang_rt.hwasan")
1437 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1438
1439 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1440 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1441 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1442}
1443
1444func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001445 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001446 prepareForTestOfRuntimeApexWithHwasan,
1447 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1448 variables.SanitizeDevice = []string{"hwaddress"}
1449 }),
1450 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001451 cc_library {
1452 name: "libc",
1453 no_libcrt: true,
1454 nocrt: true,
1455 stl: "none",
1456 system_shared_libs: [],
1457 stubs: { versions: ["1"] },
1458 apex_available: ["com.android.runtime"],
1459 }
1460
1461 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001462 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001463 no_libcrt: true,
1464 nocrt: true,
1465 stl: "none",
1466 system_shared_libs: [],
1467 srcs: [""],
1468 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001469 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001470
1471 sanitize: {
1472 never: true,
1473 },
1474 }
Paul Duffina02cae32021-03-09 01:44:06 +00001475 `)
1476 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001477
1478 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1479 "lib64/bionic/libc.so",
1480 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1481 })
1482
Colin Cross4c4c1be2022-02-10 11:41:18 -08001483 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001484
1485 installed := hwasan.Description("install libclang_rt.hwasan")
1486 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1487
1488 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1489 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1490 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1491}
1492
Jooyung Han61b66e92020-03-21 14:21:46 +00001493func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1494 testcases := []struct {
1495 name string
1496 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001497 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001498 shouldLink string
1499 shouldNotLink []string
1500 }{
1501 {
Jiyong Park55549df2021-02-26 23:57:23 +09001502 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001503 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001504 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001505 shouldLink: "current",
1506 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001507 },
1508 {
Jiyong Park55549df2021-02-26 23:57:23 +09001509 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001510 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001511 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001512 shouldLink: "current",
1513 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001514 },
1515 }
1516 for _, tc := range testcases {
1517 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001518 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001519 apex {
1520 name: "myapex",
1521 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001522 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001523 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001524 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001525 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001526
Jooyung Han61b66e92020-03-21 14:21:46 +00001527 apex_key {
1528 name: "myapex.key",
1529 public_key: "testkey.avbpubkey",
1530 private_key: "testkey.pem",
1531 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001532
Jooyung Han61b66e92020-03-21 14:21:46 +00001533 cc_library {
1534 name: "mylib",
1535 srcs: ["mylib.cpp"],
1536 vendor_available: true,
1537 shared_libs: ["libbar"],
1538 system_shared_libs: [],
1539 stl: "none",
1540 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001541 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001542 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001543
Jooyung Han61b66e92020-03-21 14:21:46 +00001544 cc_library {
1545 name: "libbar",
1546 srcs: ["mylib.cpp"],
1547 system_shared_libs: [],
1548 stl: "none",
1549 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001550 llndk: {
1551 symbol_file: "libbar.map.txt",
1552 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001553 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001554 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001555 withUnbundledBuild,
1556 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001557
Jooyung Han61b66e92020-03-21 14:21:46 +00001558 // Ensure that LLNDK dep is not included
1559 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1560 "lib64/mylib.so",
1561 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001562
Jooyung Han61b66e92020-03-21 14:21:46 +00001563 // Ensure that LLNDK dep is required
1564 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1565 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1566 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001567
Steven Moreland2c4000c2021-04-27 02:08:49 +00001568 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1569 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001570 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001571 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001572 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001573
Steven Moreland2c4000c2021-04-27 02:08:49 +00001574 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001575 ver := tc.shouldLink
1576 if tc.shouldLink == "current" {
1577 ver = strconv.Itoa(android.FutureApiLevelInt)
1578 }
1579 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001580 })
1581 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001582}
1583
Jiyong Park25fc6a92018-11-18 18:02:45 +09001584func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001585 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001586 apex {
1587 name: "myapex",
1588 key: "myapex.key",
1589 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001590 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001591 }
1592
1593 apex_key {
1594 name: "myapex.key",
1595 public_key: "testkey.avbpubkey",
1596 private_key: "testkey.pem",
1597 }
1598
1599 cc_library {
1600 name: "mylib",
1601 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001602 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001603 shared_libs: ["libdl#27"],
1604 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001605 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001606 }
1607
1608 cc_library_shared {
1609 name: "mylib_shared",
1610 srcs: ["mylib.cpp"],
1611 shared_libs: ["libdl#27"],
1612 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001613 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001614 }
1615
1616 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001617 name: "libBootstrap",
1618 srcs: ["mylib.cpp"],
1619 stl: "none",
1620 bootstrap: true,
1621 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001622 `)
1623
Sundong Ahnabb64432019-10-22 13:58:29 +09001624 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001625 copyCmds := apexRule.Args["copy_commands"]
1626
1627 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001628 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001629 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1630 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001631
1632 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001633 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001634
Colin Crossaede88c2020-08-11 12:17:01 -07001635 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1636 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1637 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001638
1639 // For dependency to libc
1640 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001641 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001642 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001643 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001644 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001645 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1646 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001647
1648 // For dependency to libm
1649 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001650 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001651 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001652 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001653 // ... and is not compiling with the stub
1654 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1655 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1656
1657 // For dependency to libdl
1658 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001659 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001660 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001661 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1662 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001663 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001664 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001665 // ... Cflags from stub is correctly exported to mylib
1666 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1667 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001668
1669 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001670 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1671 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1672 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1673 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001674}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001675
Jooyung Han749dc692020-04-15 11:03:39 +09001676func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001677 // there are three links between liba --> libz.
1678 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001679 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001680 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001681 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001682 apex {
1683 name: "myapex",
1684 key: "myapex.key",
1685 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001686 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001687 }
1688
1689 apex {
1690 name: "otherapex",
1691 key: "myapex.key",
1692 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001693 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001694 }
1695
1696 apex_key {
1697 name: "myapex.key",
1698 public_key: "testkey.avbpubkey",
1699 private_key: "testkey.pem",
1700 }
1701
1702 cc_library {
1703 name: "libx",
1704 shared_libs: ["liba"],
1705 system_shared_libs: [],
1706 stl: "none",
1707 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001708 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001709 }
1710
1711 cc_library {
1712 name: "liby",
1713 shared_libs: ["liba"],
1714 system_shared_libs: [],
1715 stl: "none",
1716 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001717 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001718 }
1719
1720 cc_library {
1721 name: "liba",
1722 shared_libs: ["libz"],
1723 system_shared_libs: [],
1724 stl: "none",
1725 apex_available: [
1726 "//apex_available:anyapex",
1727 "//apex_available:platform",
1728 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001729 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001730 }
1731
1732 cc_library {
1733 name: "libz",
1734 system_shared_libs: [],
1735 stl: "none",
1736 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001737 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001738 },
1739 }
Jooyung Han749dc692020-04-15 11:03:39 +09001740 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001741
1742 expectLink := func(from, from_variant, to, to_variant string) {
1743 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1744 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1745 }
1746 expectNoLink := func(from, from_variant, to, to_variant string) {
1747 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1748 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1749 }
1750 // platform liba is linked to non-stub version
1751 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001752 // liba in myapex is linked to current
1753 expectLink("liba", "shared_apex29", "libz", "shared_current")
1754 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001755 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001756 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001757 // liba in otherapex is linked to current
1758 expectLink("liba", "shared_apex30", "libz", "shared_current")
1759 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001760 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1761 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001762}
1763
Jooyung Hanaed150d2020-04-02 01:41:41 +09001764func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001765 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001766 apex {
1767 name: "myapex",
1768 key: "myapex.key",
1769 native_shared_libs: ["libx"],
1770 min_sdk_version: "R",
1771 }
1772
1773 apex_key {
1774 name: "myapex.key",
1775 public_key: "testkey.avbpubkey",
1776 private_key: "testkey.pem",
1777 }
1778
1779 cc_library {
1780 name: "libx",
1781 shared_libs: ["libz"],
1782 system_shared_libs: [],
1783 stl: "none",
1784 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001785 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001786 }
1787
1788 cc_library {
1789 name: "libz",
1790 system_shared_libs: [],
1791 stl: "none",
1792 stubs: {
1793 versions: ["29", "R"],
1794 },
1795 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001796 `,
1797 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1798 variables.Platform_version_active_codenames = []string{"R"}
1799 }),
1800 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001801
1802 expectLink := func(from, from_variant, to, to_variant string) {
1803 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1804 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1805 }
1806 expectNoLink := func(from, from_variant, to, to_variant string) {
1807 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1808 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1809 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001810 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1811 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001812 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1813 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001814}
1815
Jooyung Han4c4da062021-06-23 10:23:16 +09001816func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1817 testApex(t, `
1818 apex {
1819 name: "myapex",
1820 key: "myapex.key",
1821 java_libs: ["libx"],
1822 min_sdk_version: "S",
1823 }
1824
1825 apex_key {
1826 name: "myapex.key",
1827 public_key: "testkey.avbpubkey",
1828 private_key: "testkey.pem",
1829 }
1830
1831 java_library {
1832 name: "libx",
1833 srcs: ["a.java"],
1834 apex_available: [ "myapex" ],
1835 sdk_version: "current",
1836 min_sdk_version: "S", // should be okay
1837 }
1838 `,
1839 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1840 variables.Platform_version_active_codenames = []string{"S"}
1841 variables.Platform_sdk_codename = proptools.StringPtr("S")
1842 }),
1843 )
1844}
1845
Jooyung Han749dc692020-04-15 11:03:39 +09001846func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001847 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001848 apex {
1849 name: "myapex",
1850 key: "myapex.key",
1851 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001852 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001853 }
1854
1855 apex_key {
1856 name: "myapex.key",
1857 public_key: "testkey.avbpubkey",
1858 private_key: "testkey.pem",
1859 }
1860
1861 cc_library {
1862 name: "libx",
1863 shared_libs: ["libz"],
1864 system_shared_libs: [],
1865 stl: "none",
1866 apex_available: [ "myapex" ],
1867 }
1868
1869 cc_library {
1870 name: "libz",
1871 system_shared_libs: [],
1872 stl: "none",
1873 stubs: {
1874 versions: ["1", "2"],
1875 },
1876 }
1877 `)
1878
1879 expectLink := func(from, from_variant, to, to_variant string) {
1880 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1881 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1882 }
1883 expectNoLink := func(from, from_variant, to, to_variant string) {
1884 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1885 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1886 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001887 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001888 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001889 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001890 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001891}
1892
Jiyong Park5df7bd32021-08-25 16:18:46 +09001893func TestApexMinSdkVersion_crtobjectInVendorApex(t *testing.T) {
1894 ctx := testApex(t, `
1895 apex {
1896 name: "myapex",
1897 key: "myapex.key",
1898 native_shared_libs: ["mylib"],
1899 updatable: false,
1900 vendor: true,
1901 min_sdk_version: "29",
1902 }
1903
1904 apex_key {
1905 name: "myapex.key",
1906 public_key: "testkey.avbpubkey",
1907 private_key: "testkey.pem",
1908 }
1909
1910 cc_library {
1911 name: "mylib",
1912 vendor_available: true,
1913 system_shared_libs: [],
1914 stl: "none",
1915 apex_available: [ "myapex" ],
1916 min_sdk_version: "29",
1917 }
1918 `)
1919
1920 vendorVariant := "android_vendor.29_arm64_armv8-a"
1921
1922 // First check that the correct variant of crtbegin_so is used.
1923 ldRule := ctx.ModuleForTests("mylib", vendorVariant+"_shared_apex29").Rule("ld")
1924 crtBegin := names(ldRule.Args["crtBegin"])
1925 ensureListContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
1926
1927 // Ensure that the crtbegin_so used by the APEX is targeting 29
1928 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
1929 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
1930}
1931
Jooyung Han03b51852020-02-26 22:45:42 +09001932func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001933 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001934 apex {
1935 name: "myapex",
1936 key: "myapex.key",
1937 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001938 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001939 }
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: "libx",
1949 system_shared_libs: [],
1950 stl: "none",
1951 apex_available: [ "myapex" ],
1952 stubs: {
1953 versions: ["1", "2"],
1954 },
1955 }
1956
1957 cc_library {
1958 name: "libz",
1959 shared_libs: ["libx"],
1960 system_shared_libs: [],
1961 stl: "none",
1962 }
1963 `)
1964
1965 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001966 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001967 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1968 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1969 }
1970 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07001971 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09001972 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1973 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1974 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001975 expectLink("libz", "shared", "libx", "shared_current")
1976 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09001977 expectNoLink("libz", "shared", "libz", "shared_1")
1978 expectNoLink("libz", "shared", "libz", "shared")
1979}
1980
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001981var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
1982 func(variables android.FixtureProductVariables) {
1983 variables.SanitizeDevice = []string{"hwaddress"}
1984 },
1985)
1986
Jooyung Han75568392020-03-20 04:29:24 +09001987func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001988 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001989 apex {
1990 name: "myapex",
1991 key: "myapex.key",
1992 native_shared_libs: ["libx"],
1993 min_sdk_version: "29",
1994 }
1995
1996 apex_key {
1997 name: "myapex.key",
1998 public_key: "testkey.avbpubkey",
1999 private_key: "testkey.pem",
2000 }
2001
2002 cc_library {
2003 name: "libx",
2004 shared_libs: ["libbar"],
2005 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002006 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002007 }
2008
2009 cc_library {
2010 name: "libbar",
2011 stubs: {
2012 versions: ["29", "30"],
2013 },
2014 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002015 `,
2016 prepareForTestWithSantitizeHwaddress,
2017 )
Jooyung Han03b51852020-02-26 22:45:42 +09002018 expectLink := func(from, from_variant, to, to_variant string) {
2019 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2020 libFlags := ld.Args["libFlags"]
2021 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2022 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002023 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002024}
2025
Jooyung Han75568392020-03-20 04:29:24 +09002026func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002027 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002028 apex {
2029 name: "myapex",
2030 key: "myapex.key",
2031 native_shared_libs: ["libx"],
2032 min_sdk_version: "29",
2033 }
2034
2035 apex_key {
2036 name: "myapex.key",
2037 public_key: "testkey.avbpubkey",
2038 private_key: "testkey.pem",
2039 }
2040
2041 cc_library {
2042 name: "libx",
2043 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002044 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002045 }
Jooyung Han75568392020-03-20 04:29:24 +09002046 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002047
2048 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002049 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002050 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002051 // note that platform variant is not.
2052 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002053 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002054}
2055
Jooyung Han749dc692020-04-15 11:03:39 +09002056func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2057 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002058 apex {
2059 name: "myapex",
2060 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002061 native_shared_libs: ["mylib"],
2062 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002063 }
2064
2065 apex_key {
2066 name: "myapex.key",
2067 public_key: "testkey.avbpubkey",
2068 private_key: "testkey.pem",
2069 }
Jooyung Han749dc692020-04-15 11:03:39 +09002070
2071 cc_library {
2072 name: "mylib",
2073 srcs: ["mylib.cpp"],
2074 system_shared_libs: [],
2075 stl: "none",
2076 apex_available: [
2077 "myapex",
2078 ],
2079 min_sdk_version: "30",
2080 }
2081 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002082
2083 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2084 apex {
2085 name: "myapex",
2086 key: "myapex.key",
2087 native_shared_libs: ["libfoo.ffi"],
2088 min_sdk_version: "29",
2089 }
2090
2091 apex_key {
2092 name: "myapex.key",
2093 public_key: "testkey.avbpubkey",
2094 private_key: "testkey.pem",
2095 }
2096
2097 rust_ffi_shared {
2098 name: "libfoo.ffi",
2099 srcs: ["foo.rs"],
2100 crate_name: "foo",
2101 apex_available: [
2102 "myapex",
2103 ],
2104 min_sdk_version: "30",
2105 }
2106 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002107
2108 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2109 apex {
2110 name: "myapex",
2111 key: "myapex.key",
2112 java_libs: ["libfoo"],
2113 min_sdk_version: "29",
2114 }
2115
2116 apex_key {
2117 name: "myapex.key",
2118 public_key: "testkey.avbpubkey",
2119 private_key: "testkey.pem",
2120 }
2121
2122 java_import {
2123 name: "libfoo",
2124 jars: ["libfoo.jar"],
2125 apex_available: [
2126 "myapex",
2127 ],
2128 min_sdk_version: "30",
2129 }
2130 `)
Jooyung Han749dc692020-04-15 11:03:39 +09002131}
2132
2133func TestApexMinSdkVersion_Okay(t *testing.T) {
2134 testApex(t, `
2135 apex {
2136 name: "myapex",
2137 key: "myapex.key",
2138 native_shared_libs: ["libfoo"],
2139 java_libs: ["libbar"],
2140 min_sdk_version: "29",
2141 }
2142
2143 apex_key {
2144 name: "myapex.key",
2145 public_key: "testkey.avbpubkey",
2146 private_key: "testkey.pem",
2147 }
2148
2149 cc_library {
2150 name: "libfoo",
2151 srcs: ["mylib.cpp"],
2152 shared_libs: ["libfoo_dep"],
2153 apex_available: ["myapex"],
2154 min_sdk_version: "29",
2155 }
2156
2157 cc_library {
2158 name: "libfoo_dep",
2159 srcs: ["mylib.cpp"],
2160 apex_available: ["myapex"],
2161 min_sdk_version: "29",
2162 }
2163
2164 java_library {
2165 name: "libbar",
2166 sdk_version: "current",
2167 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002168 static_libs: [
2169 "libbar_dep",
2170 "libbar_import_dep",
2171 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002172 apex_available: ["myapex"],
2173 min_sdk_version: "29",
2174 }
2175
2176 java_library {
2177 name: "libbar_dep",
2178 sdk_version: "current",
2179 srcs: ["a.java"],
2180 apex_available: ["myapex"],
2181 min_sdk_version: "29",
2182 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002183
2184 java_import {
2185 name: "libbar_import_dep",
2186 jars: ["libbar.jar"],
2187 apex_available: ["myapex"],
2188 min_sdk_version: "29",
2189 }
Jooyung Han03b51852020-02-26 22:45:42 +09002190 `)
2191}
2192
Colin Cross8ca61c12022-10-06 21:00:14 -07002193func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2194 // Tests that an apex dependency with min_sdk_version higher than the
2195 // min_sdk_version of the apex is allowed as long as the dependency's
2196 // min_sdk_version is less than or equal to the api level that the
2197 // architecture was introduced in. In this case, arm64 didn't exist
2198 // until api level 21, so the arm64 code will never need to run on
2199 // an api level 20 device, even if other architectures of the apex
2200 // will.
2201 testApex(t, `
2202 apex {
2203 name: "myapex",
2204 key: "myapex.key",
2205 native_shared_libs: ["libfoo"],
2206 min_sdk_version: "20",
2207 }
2208
2209 apex_key {
2210 name: "myapex.key",
2211 public_key: "testkey.avbpubkey",
2212 private_key: "testkey.pem",
2213 }
2214
2215 cc_library {
2216 name: "libfoo",
2217 srcs: ["mylib.cpp"],
2218 apex_available: ["myapex"],
2219 min_sdk_version: "21",
2220 stl: "none",
2221 }
2222 `)
2223}
2224
Artur Satayev8cf899a2020-04-15 17:29:42 +01002225func TestJavaStableSdkVersion(t *testing.T) {
2226 testCases := []struct {
2227 name string
2228 expectedError string
2229 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002230 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002231 }{
2232 {
2233 name: "Non-updatable apex with non-stable dep",
2234 bp: `
2235 apex {
2236 name: "myapex",
2237 java_libs: ["myjar"],
2238 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002239 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002240 }
2241 apex_key {
2242 name: "myapex.key",
2243 public_key: "testkey.avbpubkey",
2244 private_key: "testkey.pem",
2245 }
2246 java_library {
2247 name: "myjar",
2248 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002249 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002250 apex_available: ["myapex"],
2251 }
2252 `,
2253 },
2254 {
2255 name: "Updatable apex with stable dep",
2256 bp: `
2257 apex {
2258 name: "myapex",
2259 java_libs: ["myjar"],
2260 key: "myapex.key",
2261 updatable: true,
2262 min_sdk_version: "29",
2263 }
2264 apex_key {
2265 name: "myapex.key",
2266 public_key: "testkey.avbpubkey",
2267 private_key: "testkey.pem",
2268 }
2269 java_library {
2270 name: "myjar",
2271 srcs: ["foo/bar/MyClass.java"],
2272 sdk_version: "current",
2273 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002274 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002275 }
2276 `,
2277 },
2278 {
2279 name: "Updatable apex with non-stable dep",
2280 expectedError: "cannot depend on \"myjar\"",
2281 bp: `
2282 apex {
2283 name: "myapex",
2284 java_libs: ["myjar"],
2285 key: "myapex.key",
2286 updatable: true,
2287 }
2288 apex_key {
2289 name: "myapex.key",
2290 public_key: "testkey.avbpubkey",
2291 private_key: "testkey.pem",
2292 }
2293 java_library {
2294 name: "myjar",
2295 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002296 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002297 apex_available: ["myapex"],
2298 }
2299 `,
2300 },
2301 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002302 name: "Updatable apex with non-stable legacy core platform dep",
2303 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2304 bp: `
2305 apex {
2306 name: "myapex",
2307 java_libs: ["myjar-uses-legacy"],
2308 key: "myapex.key",
2309 updatable: true,
2310 }
2311 apex_key {
2312 name: "myapex.key",
2313 public_key: "testkey.avbpubkey",
2314 private_key: "testkey.pem",
2315 }
2316 java_library {
2317 name: "myjar-uses-legacy",
2318 srcs: ["foo/bar/MyClass.java"],
2319 sdk_version: "core_platform",
2320 apex_available: ["myapex"],
2321 }
2322 `,
2323 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2324 },
2325 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002326 name: "Updatable apex with non-stable transitive dep",
2327 // This is not actually detecting that the transitive dependency is unstable, rather it is
2328 // detecting that the transitive dependency is building against a wider API surface than the
2329 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002330 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002331 bp: `
2332 apex {
2333 name: "myapex",
2334 java_libs: ["myjar"],
2335 key: "myapex.key",
2336 updatable: true,
2337 }
2338 apex_key {
2339 name: "myapex.key",
2340 public_key: "testkey.avbpubkey",
2341 private_key: "testkey.pem",
2342 }
2343 java_library {
2344 name: "myjar",
2345 srcs: ["foo/bar/MyClass.java"],
2346 sdk_version: "current",
2347 apex_available: ["myapex"],
2348 static_libs: ["transitive-jar"],
2349 }
2350 java_library {
2351 name: "transitive-jar",
2352 srcs: ["foo/bar/MyClass.java"],
2353 sdk_version: "core_platform",
2354 apex_available: ["myapex"],
2355 }
2356 `,
2357 },
2358 }
2359
2360 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002361 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2362 continue
2363 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002364 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002365 errorHandler := android.FixtureExpectsNoErrors
2366 if test.expectedError != "" {
2367 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002368 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002369 android.GroupFixturePreparers(
2370 java.PrepareForTestWithJavaDefaultModules,
2371 PrepareForTestWithApexBuildComponents,
2372 prepareForTestWithMyapex,
2373 android.OptionalFixturePreparer(test.preparer),
2374 ).
2375 ExtendWithErrorHandler(errorHandler).
2376 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002377 })
2378 }
2379}
2380
Jooyung Han749dc692020-04-15 11:03:39 +09002381func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2382 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2383 apex {
2384 name: "myapex",
2385 key: "myapex.key",
2386 native_shared_libs: ["mylib"],
2387 min_sdk_version: "29",
2388 }
2389
2390 apex_key {
2391 name: "myapex.key",
2392 public_key: "testkey.avbpubkey",
2393 private_key: "testkey.pem",
2394 }
2395
2396 cc_library {
2397 name: "mylib",
2398 srcs: ["mylib.cpp"],
2399 shared_libs: ["mylib2"],
2400 system_shared_libs: [],
2401 stl: "none",
2402 apex_available: [
2403 "myapex",
2404 ],
2405 min_sdk_version: "29",
2406 }
2407
2408 // indirect part of the apex
2409 cc_library {
2410 name: "mylib2",
2411 srcs: ["mylib.cpp"],
2412 system_shared_libs: [],
2413 stl: "none",
2414 apex_available: [
2415 "myapex",
2416 ],
2417 min_sdk_version: "30",
2418 }
2419 `)
2420}
2421
2422func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2423 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2424 apex {
2425 name: "myapex",
2426 key: "myapex.key",
2427 apps: ["AppFoo"],
2428 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002429 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002430 }
2431
2432 apex_key {
2433 name: "myapex.key",
2434 public_key: "testkey.avbpubkey",
2435 private_key: "testkey.pem",
2436 }
2437
2438 android_app {
2439 name: "AppFoo",
2440 srcs: ["foo/bar/MyClass.java"],
2441 sdk_version: "current",
2442 min_sdk_version: "29",
2443 system_modules: "none",
2444 stl: "none",
2445 static_libs: ["bar"],
2446 apex_available: [ "myapex" ],
2447 }
2448
2449 java_library {
2450 name: "bar",
2451 sdk_version: "current",
2452 srcs: ["a.java"],
2453 apex_available: [ "myapex" ],
2454 }
2455 `)
2456}
2457
2458func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002459 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002460 apex {
2461 name: "myapex",
2462 key: "myapex.key",
2463 native_shared_libs: ["mylib"],
2464 min_sdk_version: "29",
2465 }
2466
2467 apex_key {
2468 name: "myapex.key",
2469 public_key: "testkey.avbpubkey",
2470 private_key: "testkey.pem",
2471 }
2472
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002473 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002474 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2475 cc_library {
2476 name: "mylib",
2477 srcs: ["mylib.cpp"],
2478 shared_libs: ["mylib2"],
2479 system_shared_libs: [],
2480 stl: "none",
2481 apex_available: ["myapex", "otherapex"],
2482 min_sdk_version: "29",
2483 }
2484
2485 cc_library {
2486 name: "mylib2",
2487 srcs: ["mylib.cpp"],
2488 system_shared_libs: [],
2489 stl: "none",
2490 apex_available: ["otherapex"],
2491 stubs: { versions: ["29", "30"] },
2492 min_sdk_version: "30",
2493 }
2494
2495 apex {
2496 name: "otherapex",
2497 key: "myapex.key",
2498 native_shared_libs: ["mylib", "mylib2"],
2499 min_sdk_version: "30",
2500 }
2501 `)
2502 expectLink := func(from, from_variant, to, to_variant string) {
2503 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2504 libFlags := ld.Args["libFlags"]
2505 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2506 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002507 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002508 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002509}
2510
Jooyung Haned124c32021-01-26 11:43:46 +09002511func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002512 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2513 func(variables android.FixtureProductVariables) {
2514 variables.Platform_sdk_codename = proptools.StringPtr("S")
2515 variables.Platform_version_active_codenames = []string{"S"}
2516 },
2517 )
Jooyung Haned124c32021-01-26 11:43:46 +09002518 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2519 apex {
2520 name: "myapex",
2521 key: "myapex.key",
2522 native_shared_libs: ["libfoo"],
2523 min_sdk_version: "S",
2524 }
2525 apex_key {
2526 name: "myapex.key",
2527 public_key: "testkey.avbpubkey",
2528 private_key: "testkey.pem",
2529 }
2530 cc_library {
2531 name: "libfoo",
2532 shared_libs: ["libbar"],
2533 apex_available: ["myapex"],
2534 min_sdk_version: "29",
2535 }
2536 cc_library {
2537 name: "libbar",
2538 apex_available: ["myapex"],
2539 }
2540 `, withSAsActiveCodeNames)
2541}
2542
2543func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002544 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2545 variables.Platform_sdk_codename = proptools.StringPtr("S")
2546 variables.Platform_version_active_codenames = []string{"S", "T"}
2547 })
Colin Cross1c460562021-02-16 17:55:47 -08002548 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002549 apex {
2550 name: "myapex",
2551 key: "myapex.key",
2552 native_shared_libs: ["libfoo"],
2553 min_sdk_version: "S",
2554 }
2555 apex_key {
2556 name: "myapex.key",
2557 public_key: "testkey.avbpubkey",
2558 private_key: "testkey.pem",
2559 }
2560 cc_library {
2561 name: "libfoo",
2562 shared_libs: ["libbar"],
2563 apex_available: ["myapex"],
2564 min_sdk_version: "S",
2565 }
2566 cc_library {
2567 name: "libbar",
2568 stubs: {
2569 symbol_file: "libbar.map.txt",
2570 versions: ["30", "S", "T"],
2571 },
2572 }
2573 `, withSAsActiveCodeNames)
2574
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002575 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002576 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2577 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002578 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002579}
2580
Jiyong Park7c2ee712018-12-07 00:42:25 +09002581func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002582 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002583 apex {
2584 name: "myapex",
2585 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002586 native_shared_libs: ["mylib"],
2587 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002588 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002589 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002590 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002591 }
2592
2593 apex_key {
2594 name: "myapex.key",
2595 public_key: "testkey.avbpubkey",
2596 private_key: "testkey.pem",
2597 }
2598
2599 prebuilt_etc {
2600 name: "myetc",
2601 src: "myprebuilt",
2602 sub_dir: "foo/bar",
2603 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002604
2605 cc_library {
2606 name: "mylib",
2607 srcs: ["mylib.cpp"],
2608 relative_install_path: "foo/bar",
2609 system_shared_libs: [],
2610 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002611 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002612 }
2613
2614 cc_binary {
2615 name: "mybin",
2616 srcs: ["mylib.cpp"],
2617 relative_install_path: "foo/bar",
2618 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002619 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002620 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002621 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002622 `)
2623
Sundong Ahnabb64432019-10-22 13:58:29 +09002624 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002625 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002626
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002627 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002628 ensureContains(t, cmd, "/etc ")
2629 ensureContains(t, cmd, "/etc/foo ")
2630 ensureContains(t, cmd, "/etc/foo/bar ")
2631 ensureContains(t, cmd, "/lib64 ")
2632 ensureContains(t, cmd, "/lib64/foo ")
2633 ensureContains(t, cmd, "/lib64/foo/bar ")
2634 ensureContains(t, cmd, "/lib ")
2635 ensureContains(t, cmd, "/lib/foo ")
2636 ensureContains(t, cmd, "/lib/foo/bar ")
2637 ensureContains(t, cmd, "/bin ")
2638 ensureContains(t, cmd, "/bin/foo ")
2639 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002640}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002641
Jooyung Han35155c42020-02-06 17:33:20 +09002642func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002643 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002644 apex {
2645 name: "myapex",
2646 key: "myapex.key",
2647 multilib: {
2648 both: {
2649 native_shared_libs: ["mylib"],
2650 binaries: ["mybin"],
2651 },
2652 },
2653 compile_multilib: "both",
2654 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002655 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002656 }
2657
2658 apex_key {
2659 name: "myapex.key",
2660 public_key: "testkey.avbpubkey",
2661 private_key: "testkey.pem",
2662 }
2663
2664 cc_library {
2665 name: "mylib",
2666 relative_install_path: "foo/bar",
2667 system_shared_libs: [],
2668 stl: "none",
2669 apex_available: [ "myapex" ],
2670 native_bridge_supported: true,
2671 }
2672
2673 cc_binary {
2674 name: "mybin",
2675 relative_install_path: "foo/bar",
2676 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002677 stl: "none",
2678 apex_available: [ "myapex" ],
2679 native_bridge_supported: true,
2680 compile_multilib: "both", // default is "first" for binary
2681 multilib: {
2682 lib64: {
2683 suffix: "64",
2684 },
2685 },
2686 }
2687 `, withNativeBridgeEnabled)
2688 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2689 "bin/foo/bar/mybin",
2690 "bin/foo/bar/mybin64",
2691 "bin/arm/foo/bar/mybin",
2692 "bin/arm64/foo/bar/mybin64",
2693 "lib/foo/bar/mylib.so",
2694 "lib/arm/foo/bar/mylib.so",
2695 "lib64/foo/bar/mylib.so",
2696 "lib64/arm64/foo/bar/mylib.so",
2697 })
2698}
2699
Jooyung Han85d61762020-06-24 23:50:26 +09002700func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002701 result := android.GroupFixturePreparers(
2702 prepareForApexTest,
2703 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2704 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002705 apex {
2706 name: "myapex",
2707 key: "myapex.key",
2708 binaries: ["mybin"],
2709 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002710 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002711 }
2712 apex_key {
2713 name: "myapex.key",
2714 public_key: "testkey.avbpubkey",
2715 private_key: "testkey.pem",
2716 }
2717 cc_binary {
2718 name: "mybin",
2719 vendor: true,
2720 shared_libs: ["libfoo"],
2721 }
2722 cc_library {
2723 name: "libfoo",
2724 proprietary: true,
2725 }
2726 `)
2727
Colin Crossc68db4b2021-11-11 18:59:15 -08002728 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002729 "bin/mybin",
2730 "lib64/libfoo.so",
2731 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2732 "lib64/libc++.so",
2733 })
2734
Colin Crossc68db4b2021-11-11 18:59:15 -08002735 apexBundle := result.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2736 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002737 name := apexBundle.BaseModuleName()
2738 prefix := "TARGET_"
2739 var builder strings.Builder
2740 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002741 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002742 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002743 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002744
Colin Crossc68db4b2021-11-11 18:59:15 -08002745 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002746 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2747 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002748}
2749
Jooyung Hanc5a96762022-02-04 11:54:50 +09002750func TestVendorApex_use_vndk_as_stable_TryingToIncludeVNDKLib(t *testing.T) {
2751 testApexError(t, `Trying to include a VNDK library`, `
2752 apex {
2753 name: "myapex",
2754 key: "myapex.key",
2755 native_shared_libs: ["libc++"], // libc++ is a VNDK lib
2756 vendor: true,
2757 use_vndk_as_stable: true,
2758 updatable: false,
2759 }
2760 apex_key {
2761 name: "myapex.key",
2762 public_key: "testkey.avbpubkey",
2763 private_key: "testkey.pem",
2764 }`)
2765}
2766
Jooyung Handf78e212020-07-22 15:54:47 +09002767func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Jooyung Han91f92032022-02-04 12:36:33 +09002768 // myapex myapex2
2769 // | |
2770 // mybin ------. mybin2
2771 // \ \ / |
2772 // (stable) .---\--------` |
2773 // \ / \ |
2774 // \ / \ /
2775 // libvndk libvendor
2776 // (vndk)
Colin Cross1c460562021-02-16 17:55:47 -08002777 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002778 apex {
2779 name: "myapex",
2780 key: "myapex.key",
2781 binaries: ["mybin"],
2782 vendor: true,
2783 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002784 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002785 }
2786 apex_key {
2787 name: "myapex.key",
2788 public_key: "testkey.avbpubkey",
2789 private_key: "testkey.pem",
2790 }
2791 cc_binary {
2792 name: "mybin",
2793 vendor: true,
2794 shared_libs: ["libvndk", "libvendor"],
2795 }
2796 cc_library {
2797 name: "libvndk",
2798 vndk: {
2799 enabled: true,
2800 },
2801 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002802 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002803 }
2804 cc_library {
2805 name: "libvendor",
2806 vendor: true,
Jooyung Han91f92032022-02-04 12:36:33 +09002807 stl: "none",
2808 }
2809 apex {
2810 name: "myapex2",
2811 key: "myapex.key",
2812 binaries: ["mybin2"],
2813 vendor: true,
2814 use_vndk_as_stable: false,
2815 updatable: false,
2816 }
2817 cc_binary {
2818 name: "mybin2",
2819 vendor: true,
2820 shared_libs: ["libvndk", "libvendor"],
Jooyung Handf78e212020-07-22 15:54:47 +09002821 }
2822 `)
2823
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002824 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002825
Jooyung Han91f92032022-02-04 12:36:33 +09002826 for _, tc := range []struct {
2827 name string
2828 apexName string
2829 moduleName string
2830 moduleVariant string
2831 libs []string
2832 contents []string
2833 requireVndkNamespace bool
2834 }{
2835 {
2836 name: "use_vndk_as_stable",
2837 apexName: "myapex",
2838 moduleName: "mybin",
2839 moduleVariant: vendorVariant + "_apex10000",
2840 libs: []string{
2841 // should link with vendor variants of VNDK libs(libvndk/libc++)
2842 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared/libvndk.so",
2843 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared/libc++.so",
2844 // unstable Vendor libs as APEX variant
2845 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2846 },
2847 contents: []string{
2848 "bin/mybin",
2849 "lib64/libvendor.so",
2850 // VNDK libs (libvndk/libc++) are not included
2851 },
2852 requireVndkNamespace: true,
2853 },
2854 {
2855 name: "!use_vndk_as_stable",
2856 apexName: "myapex2",
2857 moduleName: "mybin2",
2858 moduleVariant: vendorVariant + "_myapex2",
2859 libs: []string{
2860 // should link with "unique" APEX(myapex2) variant of VNDK libs(libvndk/libc++)
2861 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared_myapex2/libvndk.so",
2862 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared_myapex2/libc++.so",
2863 // unstable vendor libs have "merged" APEX variants
2864 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2865 },
2866 contents: []string{
2867 "bin/mybin2",
2868 "lib64/libvendor.so",
2869 // VNDK libs are included as well
2870 "lib64/libvndk.so",
2871 "lib64/libc++.so",
2872 },
2873 requireVndkNamespace: false,
2874 },
2875 } {
2876 t.Run(tc.name, func(t *testing.T) {
2877 // Check linked libs
2878 ldRule := ctx.ModuleForTests(tc.moduleName, tc.moduleVariant).Rule("ld")
2879 libs := names(ldRule.Args["libFlags"])
2880 for _, lib := range tc.libs {
2881 ensureListContains(t, libs, lib)
2882 }
2883 // Check apex contents
2884 ensureExactContents(t, ctx, tc.apexName, "android_common_"+tc.apexName+"_image", tc.contents)
Jooyung Handf78e212020-07-22 15:54:47 +09002885
Jooyung Han91f92032022-02-04 12:36:33 +09002886 // Check "requireNativeLibs"
2887 apexManifestRule := ctx.ModuleForTests(tc.apexName, "android_common_"+tc.apexName+"_image").Rule("apexManifestRule")
2888 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2889 if tc.requireVndkNamespace {
2890 ensureListContains(t, requireNativeLibs, ":vndk")
2891 } else {
2892 ensureListNotContains(t, requireNativeLibs, ":vndk")
2893 }
2894 })
2895 }
Jooyung Handf78e212020-07-22 15:54:47 +09002896}
2897
Justin Yun13decfb2021-03-08 19:25:55 +09002898func TestProductVariant(t *testing.T) {
2899 ctx := testApex(t, `
2900 apex {
2901 name: "myapex",
2902 key: "myapex.key",
2903 updatable: false,
2904 product_specific: true,
2905 binaries: ["foo"],
2906 }
2907
2908 apex_key {
2909 name: "myapex.key",
2910 public_key: "testkey.avbpubkey",
2911 private_key: "testkey.pem",
2912 }
2913
2914 cc_binary {
2915 name: "foo",
2916 product_available: true,
2917 apex_available: ["myapex"],
2918 srcs: ["foo.cpp"],
2919 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002920 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2921 variables.ProductVndkVersion = proptools.StringPtr("current")
2922 }),
2923 )
Justin Yun13decfb2021-03-08 19:25:55 +09002924
2925 cflags := strings.Fields(
Jooyung Han91f92032022-02-04 12:36:33 +09002926 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_myapex").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09002927 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
2928 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
2929 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
2930 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
2931}
2932
Jooyung Han8e5685d2020-09-21 11:02:57 +09002933func TestApex_withPrebuiltFirmware(t *testing.T) {
2934 testCases := []struct {
2935 name string
2936 additionalProp string
2937 }{
2938 {"system apex with prebuilt_firmware", ""},
2939 {"vendor apex with prebuilt_firmware", "vendor: true,"},
2940 }
2941 for _, tc := range testCases {
2942 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002943 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09002944 apex {
2945 name: "myapex",
2946 key: "myapex.key",
2947 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002948 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09002949 `+tc.additionalProp+`
2950 }
2951 apex_key {
2952 name: "myapex.key",
2953 public_key: "testkey.avbpubkey",
2954 private_key: "testkey.pem",
2955 }
2956 prebuilt_firmware {
2957 name: "myfirmware",
2958 src: "myfirmware.bin",
2959 filename_from_src: true,
2960 `+tc.additionalProp+`
2961 }
2962 `)
2963 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2964 "etc/firmware/myfirmware.bin",
2965 })
2966 })
2967 }
Jooyung Han0703fd82020-08-26 22:11:53 +09002968}
2969
Jooyung Hanefb184e2020-06-25 17:14:25 +09002970func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002971 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09002972 apex {
2973 name: "myapex",
2974 key: "myapex.key",
2975 vendor: true,
2976 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002977 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09002978 }
2979
2980 apex_key {
2981 name: "myapex.key",
2982 public_key: "testkey.avbpubkey",
2983 private_key: "testkey.pem",
2984 }
2985
2986 cc_library {
2987 name: "mylib",
2988 vendor_available: true,
2989 }
2990 `)
2991
2992 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07002993 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09002994 name := apexBundle.BaseModuleName()
2995 prefix := "TARGET_"
2996 var builder strings.Builder
2997 data.Custom(&builder, name, prefix, "", data)
2998 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00002999 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 +09003000}
3001
Jooyung Han2ed99d02020-06-24 23:26:26 +09003002func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003003 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003004 apex {
3005 name: "myapex",
3006 key: "myapex.key",
3007 vintf_fragments: ["fragment.xml"],
3008 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003009 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003010 }
3011 apex_key {
3012 name: "myapex.key",
3013 public_key: "testkey.avbpubkey",
3014 private_key: "testkey.pem",
3015 }
3016 cc_binary {
3017 name: "mybin",
3018 }
3019 `)
3020
3021 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003022 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003023 name := apexBundle.BaseModuleName()
3024 prefix := "TARGET_"
3025 var builder strings.Builder
3026 data.Custom(&builder, name, prefix, "", data)
3027 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003028 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003029 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003030}
3031
Jiyong Park16e91a02018-12-20 18:18:08 +09003032func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003033 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003034 apex {
3035 name: "myapex",
3036 key: "myapex.key",
3037 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003038 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003039 }
3040
3041 apex_key {
3042 name: "myapex.key",
3043 public_key: "testkey.avbpubkey",
3044 private_key: "testkey.pem",
3045 }
3046
3047 cc_library {
3048 name: "mylib",
3049 srcs: ["mylib.cpp"],
3050 system_shared_libs: [],
3051 stl: "none",
3052 stubs: {
3053 versions: ["1", "2", "3"],
3054 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003055 apex_available: [
3056 "//apex_available:platform",
3057 "myapex",
3058 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09003059 }
3060
3061 cc_binary {
3062 name: "not_in_apex",
3063 srcs: ["mylib.cpp"],
3064 static_libs: ["mylib"],
3065 static_executable: true,
3066 system_shared_libs: [],
3067 stl: "none",
3068 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003069 `)
3070
Colin Cross7113d202019-11-20 16:39:12 -08003071 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003072
3073 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003074 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003075}
Jiyong Park9335a262018-12-24 11:31:58 +09003076
3077func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003078 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003079 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003080 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003081 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003082 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003083 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003084 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003085 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003086 }
3087
3088 cc_library {
3089 name: "mylib",
3090 srcs: ["mylib.cpp"],
3091 system_shared_libs: [],
3092 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003093 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003094 }
3095
3096 apex_key {
3097 name: "myapex.key",
3098 public_key: "testkey.avbpubkey",
3099 private_key: "testkey.pem",
3100 }
3101
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003102 android_app_certificate {
3103 name: "myapex.certificate",
3104 certificate: "testkey",
3105 }
3106
3107 android_app_certificate {
3108 name: "myapex.certificate.override",
3109 certificate: "testkey.override",
3110 }
3111
Jiyong Park9335a262018-12-24 11:31:58 +09003112 `)
3113
3114 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003115 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003116
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003117 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3118 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003119 "vendor/foo/devkeys/testkey.avbpubkey")
3120 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003121 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3122 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003123 "vendor/foo/devkeys/testkey.pem")
3124 }
3125
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003126 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003127 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003128 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003129 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003130 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003131 }
3132}
Jiyong Park58e364a2019-01-19 19:24:06 +09003133
Jooyung Hanf121a652019-12-17 14:30:11 +09003134func TestCertificate(t *testing.T) {
3135 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003136 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003137 apex {
3138 name: "myapex",
3139 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003140 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003141 }
3142 apex_key {
3143 name: "myapex.key",
3144 public_key: "testkey.avbpubkey",
3145 private_key: "testkey.pem",
3146 }`)
3147 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3148 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3149 if actual := rule.Args["certificates"]; actual != expected {
3150 t.Errorf("certificates should be %q, not %q", expected, actual)
3151 }
3152 })
3153 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003154 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003155 apex {
3156 name: "myapex_keytest",
3157 key: "myapex.key",
3158 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003159 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003160 }
3161 apex_key {
3162 name: "myapex.key",
3163 public_key: "testkey.avbpubkey",
3164 private_key: "testkey.pem",
3165 }
3166 android_app_certificate {
3167 name: "myapex.certificate.override",
3168 certificate: "testkey.override",
3169 }`)
3170 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3171 expected := "testkey.override.x509.pem testkey.override.pk8"
3172 if actual := rule.Args["certificates"]; actual != expected {
3173 t.Errorf("certificates should be %q, not %q", expected, actual)
3174 }
3175 })
3176 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003177 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003178 apex {
3179 name: "myapex",
3180 key: "myapex.key",
3181 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003182 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003183 }
3184 apex_key {
3185 name: "myapex.key",
3186 public_key: "testkey.avbpubkey",
3187 private_key: "testkey.pem",
3188 }
3189 android_app_certificate {
3190 name: "myapex.certificate",
3191 certificate: "testkey",
3192 }`)
3193 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3194 expected := "testkey.x509.pem testkey.pk8"
3195 if actual := rule.Args["certificates"]; actual != expected {
3196 t.Errorf("certificates should be %q, not %q", expected, actual)
3197 }
3198 })
3199 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003200 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003201 apex {
3202 name: "myapex_keytest",
3203 key: "myapex.key",
3204 file_contexts: ":myapex-file_contexts",
3205 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003206 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003207 }
3208 apex_key {
3209 name: "myapex.key",
3210 public_key: "testkey.avbpubkey",
3211 private_key: "testkey.pem",
3212 }
3213 android_app_certificate {
3214 name: "myapex.certificate.override",
3215 certificate: "testkey.override",
3216 }`)
3217 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3218 expected := "testkey.override.x509.pem testkey.override.pk8"
3219 if actual := rule.Args["certificates"]; actual != expected {
3220 t.Errorf("certificates should be %q, not %q", expected, actual)
3221 }
3222 })
3223 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003224 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003225 apex {
3226 name: "myapex",
3227 key: "myapex.key",
3228 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003229 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003230 }
3231 apex_key {
3232 name: "myapex.key",
3233 public_key: "testkey.avbpubkey",
3234 private_key: "testkey.pem",
3235 }`)
3236 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3237 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3238 if actual := rule.Args["certificates"]; actual != expected {
3239 t.Errorf("certificates should be %q, not %q", expected, actual)
3240 }
3241 })
3242 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003243 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003244 apex {
3245 name: "myapex_keytest",
3246 key: "myapex.key",
3247 file_contexts: ":myapex-file_contexts",
3248 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003249 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003250 }
3251 apex_key {
3252 name: "myapex.key",
3253 public_key: "testkey.avbpubkey",
3254 private_key: "testkey.pem",
3255 }
3256 android_app_certificate {
3257 name: "myapex.certificate.override",
3258 certificate: "testkey.override",
3259 }`)
3260 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3261 expected := "testkey.override.x509.pem testkey.override.pk8"
3262 if actual := rule.Args["certificates"]; actual != expected {
3263 t.Errorf("certificates should be %q, not %q", expected, actual)
3264 }
3265 })
3266}
3267
Jiyong Park58e364a2019-01-19 19:24:06 +09003268func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003269 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003270 apex {
3271 name: "myapex",
3272 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003273 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003274 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003275 }
3276
3277 apex {
3278 name: "otherapex",
3279 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003280 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003281 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003282 }
3283
3284 apex_key {
3285 name: "myapex.key",
3286 public_key: "testkey.avbpubkey",
3287 private_key: "testkey.pem",
3288 }
3289
3290 cc_library {
3291 name: "mylib",
3292 srcs: ["mylib.cpp"],
3293 system_shared_libs: [],
3294 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003295 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003296 "myapex",
3297 "otherapex",
3298 ],
Jooyung Han24282772020-03-21 23:20:55 +09003299 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003300 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003301 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003302 cc_library {
3303 name: "mylib2",
3304 srcs: ["mylib.cpp"],
3305 system_shared_libs: [],
3306 stl: "none",
3307 apex_available: [
3308 "myapex",
3309 "otherapex",
3310 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003311 static_libs: ["mylib3"],
3312 recovery_available: true,
3313 min_sdk_version: "29",
3314 }
3315 cc_library {
3316 name: "mylib3",
3317 srcs: ["mylib.cpp"],
3318 system_shared_libs: [],
3319 stl: "none",
3320 apex_available: [
3321 "myapex",
3322 "otherapex",
3323 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003324 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003325 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003326 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003327 `)
3328
Jooyung Hanc87a0592020-03-02 17:44:33 +09003329 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003330 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003331 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003332
Vinh Tranf9754732023-01-19 22:41:46 -05003333 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003334 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003335 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003336
Vinh Tranf9754732023-01-19 22:41:46 -05003337 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003338 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003339 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003340
Colin Crossaede88c2020-08-11 12:17:01 -07003341 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3342 // each variant defines additional macros to distinguish which apex variant it is built for
3343
3344 // non-APEX variant does not have __ANDROID_APEX__ defined
3345 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3346 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3347
Vinh Tranf9754732023-01-19 22:41:46 -05003348 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003349 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3350 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003351
Jooyung Hanc87a0592020-03-02 17:44:33 +09003352 // non-APEX variant does not have __ANDROID_APEX__ defined
3353 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3354 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3355
Vinh Tranf9754732023-01-19 22:41:46 -05003356 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003357 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003358 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003359}
Jiyong Park7e636d02019-01-28 16:16:54 +09003360
3361func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003362 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003363 apex {
3364 name: "myapex",
3365 key: "myapex.key",
3366 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003367 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003368 }
3369
3370 apex_key {
3371 name: "myapex.key",
3372 public_key: "testkey.avbpubkey",
3373 private_key: "testkey.pem",
3374 }
3375
3376 cc_library_headers {
3377 name: "mylib_headers",
3378 export_include_dirs: ["my_include"],
3379 system_shared_libs: [],
3380 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003381 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003382 }
3383
3384 cc_library {
3385 name: "mylib",
3386 srcs: ["mylib.cpp"],
3387 system_shared_libs: [],
3388 stl: "none",
3389 header_libs: ["mylib_headers"],
3390 export_header_lib_headers: ["mylib_headers"],
3391 stubs: {
3392 versions: ["1", "2", "3"],
3393 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003394 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003395 }
3396
3397 cc_library {
3398 name: "otherlib",
3399 srcs: ["mylib.cpp"],
3400 system_shared_libs: [],
3401 stl: "none",
3402 shared_libs: ["mylib"],
3403 }
3404 `)
3405
Colin Cross7113d202019-11-20 16:39:12 -08003406 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003407
3408 // Ensure that the include path of the header lib is exported to 'otherlib'
3409 ensureContains(t, cFlags, "-Imy_include")
3410}
Alex Light9670d332019-01-29 18:07:33 -08003411
Jiyong Park7cd10e32020-01-14 09:22:18 +09003412type fileInApex struct {
3413 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003414 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003415 isLink bool
3416}
3417
Jooyung Han1724d582022-12-21 10:17:44 +09003418func (f fileInApex) String() string {
3419 return f.src + ":" + f.path
3420}
3421
3422func (f fileInApex) match(expectation string) bool {
3423 parts := strings.Split(expectation, ":")
3424 if len(parts) == 1 {
3425 match, _ := path.Match(parts[0], f.path)
3426 return match
3427 }
3428 if len(parts) == 2 {
3429 matchSrc, _ := path.Match(parts[0], f.src)
3430 matchDst, _ := path.Match(parts[1], f.path)
3431 return matchSrc && matchDst
3432 }
3433 panic("invalid expected file specification: " + expectation)
3434}
3435
Jooyung Hana57af4a2020-01-23 05:36:59 +00003436func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003437 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003438 module := ctx.ModuleForTests(moduleName, variant)
3439 apexRule := module.MaybeRule("apexRule")
3440 apexDir := "/image.apex/"
3441 if apexRule.Rule == nil {
3442 apexRule = module.Rule("zipApexRule")
3443 apexDir = "/image.zipapex/"
3444 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003445 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003446 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003447 for _, cmd := range strings.Split(copyCmds, "&&") {
3448 cmd = strings.TrimSpace(cmd)
3449 if cmd == "" {
3450 continue
3451 }
3452 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003453 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003454 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003455 switch terms[0] {
3456 case "mkdir":
3457 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003458 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003459 t.Fatal("copyCmds contains invalid cp command", cmd)
3460 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003461 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003462 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003463 isLink = false
3464 case "ln":
3465 if len(terms) != 3 && len(terms) != 4 {
3466 // ln LINK TARGET or ln -s LINK TARGET
3467 t.Fatal("copyCmds contains invalid ln command", cmd)
3468 }
3469 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003470 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003471 isLink = true
3472 default:
3473 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3474 }
3475 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003476 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003477 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003478 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003479 }
Jooyung Han1724d582022-12-21 10:17:44 +09003480 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003481 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003482 }
3483 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003484 return ret
3485}
3486
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003487func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003488 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003489 var failed bool
3490 var surplus []string
3491 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003492 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003493 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003494 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003495 if file.match(expected) {
3496 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003497 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003498 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003499 }
3500 }
Jooyung Han1724d582022-12-21 10:17:44 +09003501 if !matchFound {
3502 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003503 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003504 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003505
Jooyung Han31c470b2019-10-18 16:26:59 +09003506 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003507 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003508 t.Log("surplus files", surplus)
3509 failed = true
3510 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003511
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003512 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003513 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003514 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003515 if !filesMatched[expected] {
3516 missing = append(missing, expected)
3517 }
3518 }
3519 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003520 t.Log("missing files", missing)
3521 failed = true
3522 }
3523 if failed {
3524 t.Fail()
3525 }
3526}
3527
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003528func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3529 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3530}
3531
3532func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
3533 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Rule("deapexer")
3534 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3535 if deapexer.Output != nil {
3536 outputs = append(outputs, deapexer.Output.String())
3537 }
3538 for _, output := range deapexer.ImplicitOutputs {
3539 outputs = append(outputs, output.String())
3540 }
3541 actualFiles := make([]fileInApex, 0, len(outputs))
3542 for _, output := range outputs {
3543 dir := "/deapexer/"
3544 pos := strings.LastIndex(output, dir)
3545 if pos == -1 {
3546 t.Fatal("Unknown deapexer output ", output)
3547 }
3548 path := output[pos+len(dir):]
3549 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3550 }
3551 assertFileListEquals(t, files, actualFiles)
3552}
3553
Jooyung Han344d5432019-08-23 11:17:39 +09003554func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003555 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003556 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003557 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003558 "etc/llndk.libraries.29.txt",
3559 "etc/vndkcore.libraries.29.txt",
3560 "etc/vndksp.libraries.29.txt",
3561 "etc/vndkprivate.libraries.29.txt",
3562 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003563 }
3564 testCases := []struct {
3565 vndkVersion string
3566 expectedFiles []string
3567 }{
3568 {
3569 vndkVersion: "current",
3570 expectedFiles: append(commonFiles,
3571 "lib/libvndk.so",
3572 "lib/libvndksp.so",
3573 "lib64/libvndk.so",
3574 "lib64/libvndksp.so"),
3575 },
3576 {
3577 vndkVersion: "",
3578 expectedFiles: append(commonFiles,
3579 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3580 "lib/libvndksp.so",
3581 "lib64/libvndksp.so"),
3582 },
3583 }
3584 for _, tc := range testCases {
3585 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3586 ctx := testApex(t, `
3587 apex_vndk {
3588 name: "com.android.vndk.current",
3589 key: "com.android.vndk.current.key",
3590 updatable: false,
3591 }
3592
3593 apex_key {
3594 name: "com.android.vndk.current.key",
3595 public_key: "testkey.avbpubkey",
3596 private_key: "testkey.pem",
3597 }
3598
3599 cc_library {
3600 name: "libvndk",
3601 srcs: ["mylib.cpp"],
3602 vendor_available: true,
3603 product_available: true,
3604 vndk: {
3605 enabled: true,
3606 },
3607 system_shared_libs: [],
3608 stl: "none",
3609 apex_available: [ "com.android.vndk.current" ],
3610 }
3611
3612 cc_library {
3613 name: "libvndksp",
3614 srcs: ["mylib.cpp"],
3615 vendor_available: true,
3616 product_available: true,
3617 vndk: {
3618 enabled: true,
3619 support_system_process: true,
3620 },
3621 system_shared_libs: [],
3622 stl: "none",
3623 apex_available: [ "com.android.vndk.current" ],
3624 }
3625
3626 // VNDK-Ext should not cause any problems
3627
3628 cc_library {
3629 name: "libvndk.ext",
3630 srcs: ["mylib2.cpp"],
3631 vendor: true,
3632 vndk: {
3633 enabled: true,
3634 extends: "libvndk",
3635 },
3636 system_shared_libs: [],
3637 stl: "none",
3638 }
3639
3640 cc_library {
3641 name: "libvndksp.ext",
3642 srcs: ["mylib2.cpp"],
3643 vendor: true,
3644 vndk: {
3645 enabled: true,
3646 support_system_process: true,
3647 extends: "libvndksp",
3648 },
3649 system_shared_libs: [],
3650 stl: "none",
3651 }
3652 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3653 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3654 }))
3655 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3656 })
3657 }
Jooyung Han344d5432019-08-23 11:17:39 +09003658}
3659
3660func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003661 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003662 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003663 name: "com.android.vndk.current",
3664 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003665 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003666 }
3667
3668 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003669 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003670 public_key: "testkey.avbpubkey",
3671 private_key: "testkey.pem",
3672 }
3673
3674 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003675 name: "libvndk",
3676 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003677 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003678 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003679 vndk: {
3680 enabled: true,
3681 },
3682 system_shared_libs: [],
3683 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003684 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003685 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003686
3687 cc_prebuilt_library_shared {
3688 name: "libvndk.arm",
3689 srcs: ["libvndk.arm.so"],
3690 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003691 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003692 vndk: {
3693 enabled: true,
3694 },
3695 enabled: false,
3696 arch: {
3697 arm: {
3698 enabled: true,
3699 },
3700 },
3701 system_shared_libs: [],
3702 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003703 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003704 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003705 `+vndkLibrariesTxtFiles("current"),
3706 withFiles(map[string][]byte{
3707 "libvndk.so": nil,
3708 "libvndk.arm.so": nil,
3709 }))
Colin Cross2807f002021-03-02 10:15:29 -08003710 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003711 "lib/libvndk.so",
3712 "lib/libvndk.arm.so",
3713 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003714 "lib/libc++.so",
3715 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003716 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003717 })
Jooyung Han344d5432019-08-23 11:17:39 +09003718}
3719
Jooyung Han39edb6c2019-11-06 16:53:07 +09003720func vndkLibrariesTxtFiles(vers ...string) (result string) {
3721 for _, v := range vers {
3722 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003723 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003724 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003725 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003726 name: "` + txt + `.libraries.txt",
3727 }
3728 `
3729 }
3730 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003731 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003732 result += `
3733 prebuilt_etc {
3734 name: "` + txt + `.libraries.` + v + `.txt",
3735 src: "dummy.txt",
3736 }
3737 `
3738 }
3739 }
3740 }
3741 return
3742}
3743
Jooyung Han344d5432019-08-23 11:17:39 +09003744func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003745 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003746 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003747 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003748 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003749 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003750 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003751 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003752 }
3753
3754 apex_key {
3755 name: "myapex.key",
3756 public_key: "testkey.avbpubkey",
3757 private_key: "testkey.pem",
3758 }
3759
Jooyung Han31c470b2019-10-18 16:26:59 +09003760 vndk_prebuilt_shared {
3761 name: "libvndk27",
3762 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003763 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003764 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003765 vndk: {
3766 enabled: true,
3767 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003768 target_arch: "arm64",
3769 arch: {
3770 arm: {
3771 srcs: ["libvndk27_arm.so"],
3772 },
3773 arm64: {
3774 srcs: ["libvndk27_arm64.so"],
3775 },
3776 },
Colin Cross2807f002021-03-02 10:15:29 -08003777 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003778 }
3779
3780 vndk_prebuilt_shared {
3781 name: "libvndk27",
3782 version: "27",
3783 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003784 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003785 vndk: {
3786 enabled: true,
3787 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003788 target_arch: "x86_64",
3789 arch: {
3790 x86: {
3791 srcs: ["libvndk27_x86.so"],
3792 },
3793 x86_64: {
3794 srcs: ["libvndk27_x86_64.so"],
3795 },
3796 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003797 }
3798 `+vndkLibrariesTxtFiles("27"),
3799 withFiles(map[string][]byte{
3800 "libvndk27_arm.so": nil,
3801 "libvndk27_arm64.so": nil,
3802 "libvndk27_x86.so": nil,
3803 "libvndk27_x86_64.so": nil,
3804 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003805
Colin Cross2807f002021-03-02 10:15:29 -08003806 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003807 "lib/libvndk27_arm.so",
3808 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003809 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003810 })
Jooyung Han344d5432019-08-23 11:17:39 +09003811}
3812
Jooyung Han90eee022019-10-01 20:02:42 +09003813func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003814 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003815 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003816 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003817 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003818 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003819 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003820 }
3821 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003822 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003823 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003824 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003825 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003826 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003827 }
3828 apex_key {
3829 name: "myapex.key",
3830 public_key: "testkey.avbpubkey",
3831 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003832 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003833
3834 assertApexName := func(expected, moduleName string) {
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003835 module := ctx.ModuleForTests(moduleName, "android_common_image")
3836 apexManifestRule := module.Rule("apexManifestRule")
3837 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003838 }
3839
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003840 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003841 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003842}
3843
Jooyung Han344d5432019-08-23 11:17:39 +09003844func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003845 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003846 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003847 name: "com.android.vndk.current",
3848 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003849 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003850 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003851 }
3852
3853 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003854 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003855 public_key: "testkey.avbpubkey",
3856 private_key: "testkey.pem",
3857 }
3858
3859 cc_library {
3860 name: "libvndk",
3861 srcs: ["mylib.cpp"],
3862 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003863 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003864 native_bridge_supported: true,
3865 host_supported: true,
3866 vndk: {
3867 enabled: true,
3868 },
3869 system_shared_libs: [],
3870 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003871 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003872 }
Colin Cross2807f002021-03-02 10:15:29 -08003873 `+vndkLibrariesTxtFiles("current"),
3874 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003875
Colin Cross2807f002021-03-02 10:15:29 -08003876 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003877 "lib/libvndk.so",
3878 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003879 "lib/libc++.so",
3880 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003881 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003882 })
Jooyung Han344d5432019-08-23 11:17:39 +09003883}
3884
3885func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003886 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003887 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003888 name: "com.android.vndk.current",
3889 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003890 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003891 native_bridge_supported: true,
3892 }
3893
3894 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003895 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003896 public_key: "testkey.avbpubkey",
3897 private_key: "testkey.pem",
3898 }
3899
3900 cc_library {
3901 name: "libvndk",
3902 srcs: ["mylib.cpp"],
3903 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003904 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003905 native_bridge_supported: true,
3906 host_supported: true,
3907 vndk: {
3908 enabled: true,
3909 },
3910 system_shared_libs: [],
3911 stl: "none",
3912 }
3913 `)
3914}
3915
Jooyung Han31c470b2019-10-18 16:26:59 +09003916func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003917 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003918 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003919 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003920 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003921 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09003922 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003923 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09003924 }
3925
3926 apex_key {
3927 name: "myapex.key",
3928 public_key: "testkey.avbpubkey",
3929 private_key: "testkey.pem",
3930 }
3931
3932 vndk_prebuilt_shared {
3933 name: "libvndk27",
3934 version: "27",
3935 target_arch: "arm",
3936 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003937 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003938 vndk: {
3939 enabled: true,
3940 },
3941 arch: {
3942 arm: {
3943 srcs: ["libvndk27.so"],
3944 }
3945 },
3946 }
3947
3948 vndk_prebuilt_shared {
3949 name: "libvndk27",
3950 version: "27",
3951 target_arch: "arm",
3952 binder32bit: true,
3953 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003954 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003955 vndk: {
3956 enabled: true,
3957 },
3958 arch: {
3959 arm: {
3960 srcs: ["libvndk27binder32.so"],
3961 }
3962 },
Colin Cross2807f002021-03-02 10:15:29 -08003963 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003964 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003965 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09003966 withFiles(map[string][]byte{
3967 "libvndk27.so": nil,
3968 "libvndk27binder32.so": nil,
3969 }),
3970 withBinder32bit,
3971 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07003972 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09003973 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
3974 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09003975 },
3976 }),
3977 )
3978
Colin Cross2807f002021-03-02 10:15:29 -08003979 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003980 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003981 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003982 })
3983}
3984
Jooyung Han45a96772020-06-15 14:59:42 +09003985func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003986 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09003987 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003988 name: "com.android.vndk.current",
3989 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003990 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003991 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09003992 }
3993
3994 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003995 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09003996 public_key: "testkey.avbpubkey",
3997 private_key: "testkey.pem",
3998 }
3999
4000 cc_library {
4001 name: "libz",
4002 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004003 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09004004 vndk: {
4005 enabled: true,
4006 },
4007 stubs: {
4008 symbol_file: "libz.map.txt",
4009 versions: ["30"],
4010 }
4011 }
4012 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
4013 "libz.map.txt": nil,
4014 }))
4015
Colin Cross2807f002021-03-02 10:15:29 -08004016 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09004017 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
4018 ensureListEmpty(t, provideNativeLibs)
Jooyung Han1724d582022-12-21 10:17:44 +09004019 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
4020 "out/soong/.intermediates/libz/android_vendor.29_arm64_armv8-a_shared/libz.so:lib64/libz.so",
4021 "out/soong/.intermediates/libz/android_vendor.29_arm_armv7-a-neon_shared/libz.so:lib/libz.so",
4022 "*/*",
4023 })
Jooyung Han45a96772020-06-15 14:59:42 +09004024}
4025
Jooyung Hane1633032019-08-01 17:41:43 +09004026func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004027 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09004028 apex {
4029 name: "myapex_nodep",
4030 key: "myapex.key",
4031 native_shared_libs: ["lib_nodep"],
4032 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004033 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004034 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004035 }
4036
4037 apex {
4038 name: "myapex_dep",
4039 key: "myapex.key",
4040 native_shared_libs: ["lib_dep"],
4041 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004042 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004043 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004044 }
4045
4046 apex {
4047 name: "myapex_provider",
4048 key: "myapex.key",
4049 native_shared_libs: ["libfoo"],
4050 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004051 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004052 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004053 }
4054
4055 apex {
4056 name: "myapex_selfcontained",
4057 key: "myapex.key",
4058 native_shared_libs: ["lib_dep", "libfoo"],
4059 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004060 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004061 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004062 }
4063
4064 apex_key {
4065 name: "myapex.key",
4066 public_key: "testkey.avbpubkey",
4067 private_key: "testkey.pem",
4068 }
4069
4070 cc_library {
4071 name: "lib_nodep",
4072 srcs: ["mylib.cpp"],
4073 system_shared_libs: [],
4074 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004075 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004076 }
4077
4078 cc_library {
4079 name: "lib_dep",
4080 srcs: ["mylib.cpp"],
4081 shared_libs: ["libfoo"],
4082 system_shared_libs: [],
4083 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004084 apex_available: [
4085 "myapex_dep",
4086 "myapex_provider",
4087 "myapex_selfcontained",
4088 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004089 }
4090
4091 cc_library {
4092 name: "libfoo",
4093 srcs: ["mytest.cpp"],
4094 stubs: {
4095 versions: ["1"],
4096 },
4097 system_shared_libs: [],
4098 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004099 apex_available: [
4100 "myapex_provider",
4101 "myapex_selfcontained",
4102 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004103 }
4104 `)
4105
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004106 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004107 var provideNativeLibs, requireNativeLibs []string
4108
Sundong Ahnabb64432019-10-22 13:58:29 +09004109 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004110 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4111 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004112 ensureListEmpty(t, provideNativeLibs)
4113 ensureListEmpty(t, requireNativeLibs)
4114
Sundong Ahnabb64432019-10-22 13:58:29 +09004115 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004116 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4117 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004118 ensureListEmpty(t, provideNativeLibs)
4119 ensureListContains(t, requireNativeLibs, "libfoo.so")
4120
Sundong Ahnabb64432019-10-22 13:58:29 +09004121 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004122 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4123 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004124 ensureListContains(t, provideNativeLibs, "libfoo.so")
4125 ensureListEmpty(t, requireNativeLibs)
4126
Sundong Ahnabb64432019-10-22 13:58:29 +09004127 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004128 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4129 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004130 ensureListContains(t, provideNativeLibs, "libfoo.so")
4131 ensureListEmpty(t, requireNativeLibs)
4132}
4133
Marco Loaizad1209a82023-02-08 10:14:01 +00004134func TestApexName(t *testing.T) {
4135 ctx := testApex(t, `
4136 apex {
4137 name: "myapex",
4138 key: "myapex.key",
4139 apex_name: "com.android.myapex",
4140 native_shared_libs: ["mylib"],
4141 updatable: false,
4142 }
4143
4144 apex_key {
4145 name: "myapex.key",
4146 public_key: "testkey.avbpubkey",
4147 private_key: "testkey.pem",
4148 }
4149
4150 cc_library {
4151 name: "mylib",
4152 srcs: ["mylib.cpp"],
4153 system_shared_libs: [],
4154 stl: "none",
4155 apex_available: [
4156 "//apex_available:platform",
4157 "myapex",
4158 ],
4159 }
4160 `)
4161
4162 module := ctx.ModuleForTests("myapex", "android_common_com.android.myapex_image")
4163 apexBundle := module.Module().(*apexBundle)
4164 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
4165 name := apexBundle.BaseModuleName()
4166 prefix := "TARGET_"
4167 var builder strings.Builder
4168 data.Custom(&builder, name, prefix, "", data)
4169 androidMk := builder.String()
4170 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
4171 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
4172}
4173
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004174func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4175 ctx := testApex(t, `
4176 apex {
4177 name: "myapex",
4178 key: "myapex.key",
Marco Loaizad1209a82023-02-08 10:14:01 +00004179 apex_name: "com.android.myapex",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004180 native_shared_libs: ["mylib"],
4181 updatable: false,
4182 }
4183
4184 apex_key {
4185 name: "myapex.key",
4186 public_key: "testkey.avbpubkey",
4187 private_key: "testkey.pem",
4188 }
4189
4190 cc_library {
4191 name: "mylib",
4192 srcs: ["mylib.cpp"],
4193 system_shared_libs: [],
4194 stl: "none",
4195 apex_available: [
4196 "//apex_available:platform",
4197 "myapex",
4198 ],
4199 }
4200 `, android.FixtureMergeEnv(map[string]string{
4201 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4202 }))
4203
Marco Loaizad1209a82023-02-08 10:14:01 +00004204 module := ctx.ModuleForTests("myapex", "android_common_com.android.myapex_image")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004205 apexManifestRule := module.Rule("apexManifestRule")
4206 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4207}
4208
Vinh Tran8f5310f2022-10-07 18:16:47 -04004209func TestCompileMultilibProp(t *testing.T) {
4210 testCases := []struct {
4211 compileMultiLibProp string
4212 containedLibs []string
4213 notContainedLibs []string
4214 }{
4215 {
4216 containedLibs: []string{
4217 "image.apex/lib64/mylib.so",
4218 "image.apex/lib/mylib.so",
4219 },
4220 compileMultiLibProp: `compile_multilib: "both",`,
4221 },
4222 {
4223 containedLibs: []string{"image.apex/lib64/mylib.so"},
4224 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4225 compileMultiLibProp: `compile_multilib: "first",`,
4226 },
4227 {
4228 containedLibs: []string{"image.apex/lib64/mylib.so"},
4229 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4230 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4231 },
4232 {
4233 containedLibs: []string{"image.apex/lib64/mylib.so"},
4234 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4235 compileMultiLibProp: `compile_multilib: "64",`,
4236 },
4237 {
4238 containedLibs: []string{"image.apex/lib/mylib.so"},
4239 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4240 compileMultiLibProp: `compile_multilib: "32",`,
4241 },
4242 }
4243 for _, testCase := range testCases {
4244 ctx := testApex(t, fmt.Sprintf(`
4245 apex {
4246 name: "myapex",
4247 key: "myapex.key",
4248 %s
4249 native_shared_libs: ["mylib"],
4250 updatable: false,
4251 }
4252 apex_key {
4253 name: "myapex.key",
4254 public_key: "testkey.avbpubkey",
4255 private_key: "testkey.pem",
4256 }
4257 cc_library {
4258 name: "mylib",
4259 srcs: ["mylib.cpp"],
4260 apex_available: [
4261 "//apex_available:platform",
4262 "myapex",
4263 ],
4264 }
4265 `, testCase.compileMultiLibProp),
4266 )
4267 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4268 apexRule := module.Rule("apexRule")
4269 copyCmds := apexRule.Args["copy_commands"]
4270 for _, containedLib := range testCase.containedLibs {
4271 ensureContains(t, copyCmds, containedLib)
4272 }
4273 for _, notContainedLib := range testCase.notContainedLibs {
4274 ensureNotContains(t, copyCmds, notContainedLib)
4275 }
4276 }
4277}
4278
Alex Light0851b882019-02-07 13:20:53 -08004279func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004280 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004281 apex {
4282 name: "myapex",
4283 key: "myapex.key",
4284 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004285 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004286 }
4287
4288 apex_key {
4289 name: "myapex.key",
4290 public_key: "testkey.avbpubkey",
4291 private_key: "testkey.pem",
4292 }
4293
4294 cc_library {
4295 name: "mylib_common",
4296 srcs: ["mylib.cpp"],
4297 system_shared_libs: [],
4298 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004299 apex_available: [
4300 "//apex_available:platform",
4301 "myapex",
4302 ],
Alex Light0851b882019-02-07 13:20:53 -08004303 }
4304 `)
4305
Sundong Ahnabb64432019-10-22 13:58:29 +09004306 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004307 apexRule := module.Rule("apexRule")
4308 copyCmds := apexRule.Args["copy_commands"]
4309
4310 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4311 t.Log("Apex was a test apex!")
4312 t.Fail()
4313 }
4314 // Ensure that main rule creates an output
4315 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4316
4317 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004318 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004319
4320 // Ensure that both direct and indirect deps are copied into apex
4321 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4322
Colin Cross7113d202019-11-20 16:39:12 -08004323 // Ensure that the platform variant ends with _shared
4324 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004325
Colin Cross56a83212020-09-15 18:30:11 -07004326 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004327 t.Log("Found mylib_common not in any apex!")
4328 t.Fail()
4329 }
4330}
4331
4332func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004333 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004334 apex_test {
4335 name: "myapex",
4336 key: "myapex.key",
4337 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004338 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004339 }
4340
4341 apex_key {
4342 name: "myapex.key",
4343 public_key: "testkey.avbpubkey",
4344 private_key: "testkey.pem",
4345 }
4346
4347 cc_library {
4348 name: "mylib_common_test",
4349 srcs: ["mylib.cpp"],
4350 system_shared_libs: [],
4351 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004352 // TODO: remove //apex_available:platform
4353 apex_available: [
4354 "//apex_available:platform",
4355 "myapex",
4356 ],
Alex Light0851b882019-02-07 13:20:53 -08004357 }
4358 `)
4359
Sundong Ahnabb64432019-10-22 13:58:29 +09004360 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004361 apexRule := module.Rule("apexRule")
4362 copyCmds := apexRule.Args["copy_commands"]
4363
4364 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4365 t.Log("Apex was not a test apex!")
4366 t.Fail()
4367 }
4368 // Ensure that main rule creates an output
4369 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4370
4371 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004372 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004373
4374 // Ensure that both direct and indirect deps are copied into apex
4375 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4376
Colin Cross7113d202019-11-20 16:39:12 -08004377 // Ensure that the platform variant ends with _shared
4378 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004379}
4380
Alex Light9670d332019-01-29 18:07:33 -08004381func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004382 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004383 apex {
4384 name: "myapex",
4385 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004386 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004387 multilib: {
4388 first: {
4389 native_shared_libs: ["mylib_common"],
4390 }
4391 },
4392 target: {
4393 android: {
4394 multilib: {
4395 first: {
4396 native_shared_libs: ["mylib"],
4397 }
4398 }
4399 },
4400 host: {
4401 multilib: {
4402 first: {
4403 native_shared_libs: ["mylib2"],
4404 }
4405 }
4406 }
4407 }
4408 }
4409
4410 apex_key {
4411 name: "myapex.key",
4412 public_key: "testkey.avbpubkey",
4413 private_key: "testkey.pem",
4414 }
4415
4416 cc_library {
4417 name: "mylib",
4418 srcs: ["mylib.cpp"],
4419 system_shared_libs: [],
4420 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004421 // TODO: remove //apex_available:platform
4422 apex_available: [
4423 "//apex_available:platform",
4424 "myapex",
4425 ],
Alex Light9670d332019-01-29 18:07:33 -08004426 }
4427
4428 cc_library {
4429 name: "mylib_common",
4430 srcs: ["mylib.cpp"],
4431 system_shared_libs: [],
4432 stl: "none",
4433 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004434 // TODO: remove //apex_available:platform
4435 apex_available: [
4436 "//apex_available:platform",
4437 "myapex",
4438 ],
Alex Light9670d332019-01-29 18:07:33 -08004439 }
4440
4441 cc_library {
4442 name: "mylib2",
4443 srcs: ["mylib.cpp"],
4444 system_shared_libs: [],
4445 stl: "none",
4446 compile_multilib: "first",
4447 }
4448 `)
4449
Sundong Ahnabb64432019-10-22 13:58:29 +09004450 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004451 copyCmds := apexRule.Args["copy_commands"]
4452
4453 // Ensure that main rule creates an output
4454 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4455
4456 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004457 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4458 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4459 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004460
4461 // Ensure that both direct and indirect deps are copied into apex
4462 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4463 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4464 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4465
Colin Cross7113d202019-11-20 16:39:12 -08004466 // Ensure that the platform variant ends with _shared
4467 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4468 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4469 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004470}
Jiyong Park04480cf2019-02-06 00:16:29 +09004471
Jiyong Park59140302020-12-14 18:44:04 +09004472func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004473 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004474 apex {
4475 name: "myapex",
4476 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004477 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004478 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004479 arch: {
4480 arm64: {
4481 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004482 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004483 },
4484 x86_64: {
4485 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004486 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004487 },
4488 }
4489 }
4490
4491 apex_key {
4492 name: "myapex.key",
4493 public_key: "testkey.avbpubkey",
4494 private_key: "testkey.pem",
4495 }
4496
4497 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004498 name: "mylib.generic",
4499 srcs: ["mylib.cpp"],
4500 system_shared_libs: [],
4501 stl: "none",
4502 // TODO: remove //apex_available:platform
4503 apex_available: [
4504 "//apex_available:platform",
4505 "myapex",
4506 ],
4507 }
4508
4509 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004510 name: "mylib.arm64",
4511 srcs: ["mylib.cpp"],
4512 system_shared_libs: [],
4513 stl: "none",
4514 // TODO: remove //apex_available:platform
4515 apex_available: [
4516 "//apex_available:platform",
4517 "myapex",
4518 ],
4519 }
4520
4521 cc_library {
4522 name: "mylib.x64",
4523 srcs: ["mylib.cpp"],
4524 system_shared_libs: [],
4525 stl: "none",
4526 // TODO: remove //apex_available:platform
4527 apex_available: [
4528 "//apex_available:platform",
4529 "myapex",
4530 ],
4531 }
4532 `)
4533
4534 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4535 copyCmds := apexRule.Args["copy_commands"]
4536
4537 // Ensure that apex variant is created for the direct dep
4538 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004539 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004540 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4541
4542 // Ensure that both direct and indirect deps are copied into apex
4543 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4544 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4545}
4546
Jiyong Park04480cf2019-02-06 00:16:29 +09004547func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004548 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004549 apex {
4550 name: "myapex",
4551 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004552 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004553 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004554 }
4555
4556 apex_key {
4557 name: "myapex.key",
4558 public_key: "testkey.avbpubkey",
4559 private_key: "testkey.pem",
4560 }
4561
4562 sh_binary {
4563 name: "myscript",
4564 src: "mylib.cpp",
4565 filename: "myscript.sh",
4566 sub_dir: "script",
4567 }
4568 `)
4569
Sundong Ahnabb64432019-10-22 13:58:29 +09004570 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004571 copyCmds := apexRule.Args["copy_commands"]
4572
4573 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4574}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004575
Jooyung Han91df2082019-11-20 01:49:42 +09004576func TestApexInVariousPartition(t *testing.T) {
4577 testcases := []struct {
4578 propName, parition, flattenedPartition string
4579 }{
4580 {"", "system", "system_ext"},
4581 {"product_specific: true", "product", "product"},
4582 {"soc_specific: true", "vendor", "vendor"},
4583 {"proprietary: true", "vendor", "vendor"},
4584 {"vendor: true", "vendor", "vendor"},
4585 {"system_ext_specific: true", "system_ext", "system_ext"},
4586 }
4587 for _, tc := range testcases {
4588 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004589 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004590 apex {
4591 name: "myapex",
4592 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004593 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004594 `+tc.propName+`
4595 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004596
Jooyung Han91df2082019-11-20 01:49:42 +09004597 apex_key {
4598 name: "myapex.key",
4599 public_key: "testkey.avbpubkey",
4600 private_key: "testkey.pem",
4601 }
4602 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004603
Jooyung Han91df2082019-11-20 01:49:42 +09004604 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004605 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4606 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004607 if actual != expected {
4608 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4609 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004610
Jooyung Han91df2082019-11-20 01:49:42 +09004611 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004612 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4613 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004614 if actual != expected {
4615 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4616 }
4617 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004618 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004619}
Jiyong Park67882562019-03-21 01:11:21 +09004620
Jooyung Han580eb4f2020-06-24 19:33:06 +09004621func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004622 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004623 apex {
4624 name: "myapex",
4625 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004626 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004627 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004628
Jooyung Han580eb4f2020-06-24 19:33:06 +09004629 apex_key {
4630 name: "myapex.key",
4631 public_key: "testkey.avbpubkey",
4632 private_key: "testkey.pem",
4633 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004634 `)
4635 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004636 rule := module.Output("file_contexts")
4637 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4638}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004639
Jooyung Han580eb4f2020-06-24 19:33:06 +09004640func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004641 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004642 apex {
4643 name: "myapex",
4644 key: "myapex.key",
4645 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004646 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004647 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004648
Jooyung Han580eb4f2020-06-24 19:33:06 +09004649 apex_key {
4650 name: "myapex.key",
4651 public_key: "testkey.avbpubkey",
4652 private_key: "testkey.pem",
4653 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004654 `, withFiles(map[string][]byte{
4655 "my_own_file_contexts": nil,
4656 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004657}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004658
Jooyung Han580eb4f2020-06-24 19:33:06 +09004659func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004660 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004661 apex {
4662 name: "myapex",
4663 key: "myapex.key",
4664 product_specific: true,
4665 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004666 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004667 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004668
Jooyung Han580eb4f2020-06-24 19:33:06 +09004669 apex_key {
4670 name: "myapex.key",
4671 public_key: "testkey.avbpubkey",
4672 private_key: "testkey.pem",
4673 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004674 `)
4675
Colin Cross1c460562021-02-16 17:55:47 -08004676 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004677 apex {
4678 name: "myapex",
4679 key: "myapex.key",
4680 product_specific: true,
4681 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004682 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004683 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004684
Jooyung Han580eb4f2020-06-24 19:33:06 +09004685 apex_key {
4686 name: "myapex.key",
4687 public_key: "testkey.avbpubkey",
4688 private_key: "testkey.pem",
4689 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004690 `, withFiles(map[string][]byte{
4691 "product_specific_file_contexts": nil,
4692 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004693 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4694 rule := module.Output("file_contexts")
4695 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4696}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004697
Jooyung Han580eb4f2020-06-24 19:33:06 +09004698func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004699 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004700 apex {
4701 name: "myapex",
4702 key: "myapex.key",
4703 product_specific: true,
4704 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004705 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004706 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004707
Jooyung Han580eb4f2020-06-24 19:33:06 +09004708 apex_key {
4709 name: "myapex.key",
4710 public_key: "testkey.avbpubkey",
4711 private_key: "testkey.pem",
4712 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004713
Jooyung Han580eb4f2020-06-24 19:33:06 +09004714 filegroup {
4715 name: "my-file-contexts",
4716 srcs: ["product_specific_file_contexts"],
4717 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004718 `, withFiles(map[string][]byte{
4719 "product_specific_file_contexts": nil,
4720 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004721 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4722 rule := module.Output("file_contexts")
4723 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004724}
4725
Jiyong Park67882562019-03-21 01:11:21 +09004726func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004727 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004728 apex_key {
4729 name: "myapex.key",
4730 public_key: ":my.avbpubkey",
4731 private_key: ":my.pem",
4732 product_specific: true,
4733 }
4734
4735 filegroup {
4736 name: "my.avbpubkey",
4737 srcs: ["testkey2.avbpubkey"],
4738 }
4739
4740 filegroup {
4741 name: "my.pem",
4742 srcs: ["testkey2.pem"],
4743 }
4744 `)
4745
4746 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4747 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004748 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004749 if actual_pubkey != expected_pubkey {
4750 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4751 }
4752 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004753 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004754 if actual_privkey != expected_privkey {
4755 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4756 }
4757}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004758
4759func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004760 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004761 prebuilt_apex {
4762 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004763 arch: {
4764 arm64: {
4765 src: "myapex-arm64.apex",
4766 },
4767 arm: {
4768 src: "myapex-arm.apex",
4769 },
4770 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004771 }
4772 `)
4773
Wei Li340ee8e2022-03-18 17:33:24 -07004774 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4775 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004776
Jiyong Parkc95714e2019-03-29 14:23:10 +09004777 expectedInput := "myapex-arm64.apex"
4778 if prebuilt.inputApex.String() != expectedInput {
4779 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4780 }
Wei Li340ee8e2022-03-18 17:33:24 -07004781 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4782 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4783 rule := testingModule.Rule("genProvenanceMetaData")
4784 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4785 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4786 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4787 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004788}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004789
Paul Duffinc0609c62021-03-01 17:27:16 +00004790func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004791 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004792 prebuilt_apex {
4793 name: "myapex",
4794 }
4795 `)
4796}
4797
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004798func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004799 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004800 prebuilt_apex {
4801 name: "myapex",
4802 src: "myapex-arm.apex",
4803 filename: "notmyapex.apex",
4804 }
4805 `)
4806
Wei Li340ee8e2022-03-18 17:33:24 -07004807 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4808 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004809
4810 expected := "notmyapex.apex"
4811 if p.installFilename != expected {
4812 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4813 }
Wei Li340ee8e2022-03-18 17:33:24 -07004814 rule := testingModule.Rule("genProvenanceMetaData")
4815 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4816 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4817 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4818 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004819}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004820
Samiul Islam7c02e262021-09-08 17:48:28 +01004821func TestApexSetFilenameOverride(t *testing.T) {
4822 testApex(t, `
4823 apex_set {
4824 name: "com.company.android.myapex",
4825 apex_name: "com.android.myapex",
4826 set: "company-myapex.apks",
4827 filename: "com.company.android.myapex.apex"
4828 }
4829 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4830
4831 testApex(t, `
4832 apex_set {
4833 name: "com.company.android.myapex",
4834 apex_name: "com.android.myapex",
4835 set: "company-myapex.apks",
4836 filename: "com.company.android.myapex.capex"
4837 }
4838 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4839
4840 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4841 apex_set {
4842 name: "com.company.android.myapex",
4843 apex_name: "com.android.myapex",
4844 set: "company-myapex.apks",
4845 filename: "some-random-suffix"
4846 }
4847 `)
4848}
4849
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004850func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004851 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004852 prebuilt_apex {
4853 name: "myapex.prebuilt",
4854 src: "myapex-arm.apex",
4855 overrides: [
4856 "myapex",
4857 ],
4858 }
4859 `)
4860
Wei Li340ee8e2022-03-18 17:33:24 -07004861 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4862 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004863
4864 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004865 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004866 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004867 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004868 }
Wei Li340ee8e2022-03-18 17:33:24 -07004869 rule := testingModule.Rule("genProvenanceMetaData")
4870 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4871 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4872 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4873 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004874}
4875
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004876func TestPrebuiltApexName(t *testing.T) {
4877 testApex(t, `
4878 prebuilt_apex {
4879 name: "com.company.android.myapex",
4880 apex_name: "com.android.myapex",
4881 src: "company-myapex-arm.apex",
4882 }
4883 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4884
4885 testApex(t, `
4886 apex_set {
4887 name: "com.company.android.myapex",
4888 apex_name: "com.android.myapex",
4889 set: "company-myapex.apks",
4890 }
4891 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4892}
4893
4894func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4895 _ = android.GroupFixturePreparers(
4896 java.PrepareForTestWithJavaDefaultModules,
4897 PrepareForTestWithApexBuildComponents,
4898 android.FixtureWithRootAndroidBp(`
4899 platform_bootclasspath {
4900 name: "platform-bootclasspath",
4901 fragments: [
4902 {
4903 apex: "com.android.art",
4904 module: "art-bootclasspath-fragment",
4905 },
4906 ],
4907 }
4908
4909 prebuilt_apex {
4910 name: "com.company.android.art",
4911 apex_name: "com.android.art",
4912 src: "com.company.android.art-arm.apex",
4913 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4914 }
4915
4916 prebuilt_bootclasspath_fragment {
4917 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004918 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004919 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004920 hidden_api: {
4921 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4922 metadata: "my-bootclasspath-fragment/metadata.csv",
4923 index: "my-bootclasspath-fragment/index.csv",
4924 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4925 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4926 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004927 }
4928
4929 java_import {
4930 name: "core-oj",
4931 jars: ["prebuilt.jar"],
4932 }
4933 `),
4934 ).RunTest(t)
4935}
4936
Paul Duffin092153d2021-01-26 11:42:39 +00004937// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4938// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004939func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004940 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004941
Paul Duffin89886cb2021-02-05 16:44:03 +00004942 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004943 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004944 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004945 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004946 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004947 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004948 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4949 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4950 android.NormalizePathForTesting(dexJarBuildPath))
4951 }
4952
4953 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004954 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004955 // Make sure the import has been given the correct path to the dex jar.
4956 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4957 dexJarBuildPath := p.DexJarInstallPath()
4958 stem := android.RemoveOptionalPrebuiltPrefix(name)
4959 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
4960 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
4961 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00004962 }
4963
Paul Duffin39853512021-02-26 11:09:39 +00004964 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004965 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004966 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09004967 android.AssertArrayString(t, "Check if there is no source variant",
4968 []string{"android_common"},
4969 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00004970 }
4971
4972 t.Run("prebuilt only", func(t *testing.T) {
4973 bp := `
4974 prebuilt_apex {
4975 name: "myapex",
4976 arch: {
4977 arm64: {
4978 src: "myapex-arm64.apex",
4979 },
4980 arm: {
4981 src: "myapex-arm.apex",
4982 },
4983 },
Paul Duffin39853512021-02-26 11:09:39 +00004984 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00004985 }
4986
4987 java_import {
4988 name: "libfoo",
4989 jars: ["libfoo.jar"],
4990 }
Paul Duffin39853512021-02-26 11:09:39 +00004991
4992 java_sdk_library_import {
4993 name: "libbar",
4994 public: {
4995 jars: ["libbar.jar"],
4996 },
4997 }
Paul Duffin064b70c2020-11-02 17:32:38 +00004998 `
4999
5000 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5001 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5002
Martin Stjernholm44825602021-09-17 01:44:12 +01005003 deapexerName := deapexerModuleName("myapex")
5004 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
5005
Paul Duffinf6932af2021-02-26 18:21:56 +00005006 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01005007 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00005008 rule := deapexer.Rule("deapexer")
5009 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
5010 t.Errorf("expected: %q, found: %q", expected, actual)
5011 }
5012
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005013 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01005014 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005015 rule = prebuiltApex.Rule("android/soong/android.Cp")
5016 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
5017 t.Errorf("expected: %q, found: %q", expected, actual)
5018 }
5019
Paul Duffin89886cb2021-02-05 16:44:03 +00005020 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005021 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005022
5023 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005024 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005025 })
5026
5027 t.Run("prebuilt with source preferred", func(t *testing.T) {
5028
5029 bp := `
5030 prebuilt_apex {
5031 name: "myapex",
5032 arch: {
5033 arm64: {
5034 src: "myapex-arm64.apex",
5035 },
5036 arm: {
5037 src: "myapex-arm.apex",
5038 },
5039 },
Paul Duffin39853512021-02-26 11:09:39 +00005040 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005041 }
5042
5043 java_import {
5044 name: "libfoo",
5045 jars: ["libfoo.jar"],
5046 }
5047
5048 java_library {
5049 name: "libfoo",
5050 }
Paul Duffin39853512021-02-26 11:09:39 +00005051
5052 java_sdk_library_import {
5053 name: "libbar",
5054 public: {
5055 jars: ["libbar.jar"],
5056 },
5057 }
5058
5059 java_sdk_library {
5060 name: "libbar",
5061 srcs: ["foo/bar/MyClass.java"],
5062 unsafe_ignore_missing_latest_api: true,
5063 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005064 `
5065
5066 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5067 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5068
Paul Duffin89886cb2021-02-05 16:44:03 +00005069 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005070 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005071 ensureNoSourceVariant(t, ctx, "libfoo")
5072
5073 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005074 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005075 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005076 })
5077
5078 t.Run("prebuilt preferred with source", func(t *testing.T) {
5079 bp := `
5080 prebuilt_apex {
5081 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005082 arch: {
5083 arm64: {
5084 src: "myapex-arm64.apex",
5085 },
5086 arm: {
5087 src: "myapex-arm.apex",
5088 },
5089 },
Paul Duffin39853512021-02-26 11:09:39 +00005090 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005091 }
5092
5093 java_import {
5094 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005095 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005096 jars: ["libfoo.jar"],
5097 }
5098
5099 java_library {
5100 name: "libfoo",
5101 }
Paul Duffin39853512021-02-26 11:09:39 +00005102
5103 java_sdk_library_import {
5104 name: "libbar",
5105 prefer: true,
5106 public: {
5107 jars: ["libbar.jar"],
5108 },
5109 }
5110
5111 java_sdk_library {
5112 name: "libbar",
5113 srcs: ["foo/bar/MyClass.java"],
5114 unsafe_ignore_missing_latest_api: true,
5115 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005116 `
5117
5118 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5119 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5120
Paul Duffin89886cb2021-02-05 16:44:03 +00005121 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005122 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005123 ensureNoSourceVariant(t, ctx, "libfoo")
5124
5125 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005126 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005127 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005128 })
5129}
5130
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005131func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005132 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005133 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005134 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5135 // is disabled.
5136 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
5137 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005138
Paul Duffin37856732021-02-26 14:24:15 +00005139 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5140 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01005141 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005142 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005143 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005144 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005145 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005146 foundLibfooJar = true
5147 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005148 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005149 }
5150 }
5151 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005152 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 +00005153 }
5154 }
5155
Paul Duffin40a3f652021-07-19 13:11:24 +01005156 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005157 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005158 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005159 var rule android.TestingBuildParams
5160
5161 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5162 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005163 }
5164
Paul Duffin40a3f652021-07-19 13:11:24 +01005165 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5166 t.Helper()
5167 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5168 var rule android.TestingBuildParams
5169
5170 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5171 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5172 }
5173
Paul Duffin89f570a2021-06-16 01:42:33 +01005174 fragment := java.ApexVariantReference{
5175 Apex: proptools.StringPtr("myapex"),
5176 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5177 }
5178
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005179 t.Run("prebuilt only", func(t *testing.T) {
5180 bp := `
5181 prebuilt_apex {
5182 name: "myapex",
5183 arch: {
5184 arm64: {
5185 src: "myapex-arm64.apex",
5186 },
5187 arm: {
5188 src: "myapex-arm.apex",
5189 },
5190 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005191 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5192 }
5193
5194 prebuilt_bootclasspath_fragment {
5195 name: "my-bootclasspath-fragment",
5196 contents: ["libfoo", "libbar"],
5197 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005198 hidden_api: {
5199 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5200 metadata: "my-bootclasspath-fragment/metadata.csv",
5201 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005202 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5203 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5204 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005205 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005206 }
5207
5208 java_import {
5209 name: "libfoo",
5210 jars: ["libfoo.jar"],
5211 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005212 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005213 }
Paul Duffin37856732021-02-26 14:24:15 +00005214
5215 java_sdk_library_import {
5216 name: "libbar",
5217 public: {
5218 jars: ["libbar.jar"],
5219 },
5220 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005221 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005222 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005223 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005224 `
5225
Paul Duffin89f570a2021-06-16 01:42:33 +01005226 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005227 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5228 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005229
Paul Duffin537ea3d2021-05-14 10:38:00 +01005230 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005231 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005232 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005233 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005234 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5235 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005236 })
5237
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005238 t.Run("apex_set only", func(t *testing.T) {
5239 bp := `
5240 apex_set {
5241 name: "myapex",
5242 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005243 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5244 }
5245
5246 prebuilt_bootclasspath_fragment {
5247 name: "my-bootclasspath-fragment",
5248 contents: ["libfoo", "libbar"],
5249 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005250 hidden_api: {
5251 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5252 metadata: "my-bootclasspath-fragment/metadata.csv",
5253 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005254 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5255 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5256 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005257 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005258 }
5259
5260 java_import {
5261 name: "libfoo",
5262 jars: ["libfoo.jar"],
5263 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005264 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005265 }
5266
5267 java_sdk_library_import {
5268 name: "libbar",
5269 public: {
5270 jars: ["libbar.jar"],
5271 },
5272 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005273 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005274 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005275 }
5276 `
5277
Paul Duffin89f570a2021-06-16 01:42:33 +01005278 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005279 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5280 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5281
Paul Duffin537ea3d2021-05-14 10:38:00 +01005282 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005283 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005284 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005285 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005286 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5287 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005288 })
5289
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005290 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5291 bp := `
5292 prebuilt_apex {
5293 name: "myapex",
5294 arch: {
5295 arm64: {
5296 src: "myapex-arm64.apex",
5297 },
5298 arm: {
5299 src: "myapex-arm.apex",
5300 },
5301 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005302 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5303 }
5304
5305 prebuilt_bootclasspath_fragment {
5306 name: "my-bootclasspath-fragment",
5307 contents: ["libfoo", "libbar"],
5308 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005309 hidden_api: {
5310 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5311 metadata: "my-bootclasspath-fragment/metadata.csv",
5312 index: "my-bootclasspath-fragment/index.csv",
5313 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5314 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5315 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005316 }
5317
5318 java_import {
5319 name: "libfoo",
5320 jars: ["libfoo.jar"],
5321 apex_available: ["myapex"],
5322 }
5323
5324 java_library {
5325 name: "libfoo",
5326 srcs: ["foo/bar/MyClass.java"],
5327 apex_available: ["myapex"],
5328 }
Paul Duffin37856732021-02-26 14:24:15 +00005329
5330 java_sdk_library_import {
5331 name: "libbar",
5332 public: {
5333 jars: ["libbar.jar"],
5334 },
5335 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005336 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005337 }
5338
5339 java_sdk_library {
5340 name: "libbar",
5341 srcs: ["foo/bar/MyClass.java"],
5342 unsafe_ignore_missing_latest_api: true,
5343 apex_available: ["myapex"],
5344 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005345 `
5346
5347 // In this test the source (java_library) libfoo is active since the
5348 // prebuilt (java_import) defaults to prefer:false. However the
5349 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5350 // find the dex boot jar in it. We either need to disable the source libfoo
5351 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005352 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005353 // dexbootjar check is skipped if AllowMissingDependencies is true
5354 preparerAllowMissingDeps := android.GroupFixturePreparers(
5355 preparer,
5356 android.PrepareForTestWithAllowMissingDependencies,
5357 )
5358 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005359 })
5360
5361 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5362 bp := `
5363 prebuilt_apex {
5364 name: "myapex",
5365 arch: {
5366 arm64: {
5367 src: "myapex-arm64.apex",
5368 },
5369 arm: {
5370 src: "myapex-arm.apex",
5371 },
5372 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005373 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5374 }
5375
5376 prebuilt_bootclasspath_fragment {
5377 name: "my-bootclasspath-fragment",
5378 contents: ["libfoo", "libbar"],
5379 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005380 hidden_api: {
5381 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5382 metadata: "my-bootclasspath-fragment/metadata.csv",
5383 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005384 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5385 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5386 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005387 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005388 }
5389
5390 java_import {
5391 name: "libfoo",
5392 prefer: true,
5393 jars: ["libfoo.jar"],
5394 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005395 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005396 }
5397
5398 java_library {
5399 name: "libfoo",
5400 srcs: ["foo/bar/MyClass.java"],
5401 apex_available: ["myapex"],
5402 }
Paul Duffin37856732021-02-26 14:24:15 +00005403
5404 java_sdk_library_import {
5405 name: "libbar",
5406 prefer: true,
5407 public: {
5408 jars: ["libbar.jar"],
5409 },
5410 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005411 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005412 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005413 }
5414
5415 java_sdk_library {
5416 name: "libbar",
5417 srcs: ["foo/bar/MyClass.java"],
5418 unsafe_ignore_missing_latest_api: true,
5419 apex_available: ["myapex"],
5420 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005421 `
5422
Paul Duffin89f570a2021-06-16 01:42:33 +01005423 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005424 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5425 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005426
Paul Duffin537ea3d2021-05-14 10:38:00 +01005427 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005428 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005429 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005430 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005431 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5432 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005433 })
5434
5435 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5436 bp := `
5437 apex {
5438 name: "myapex",
5439 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005440 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005441 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005442 }
5443
5444 apex_key {
5445 name: "myapex.key",
5446 public_key: "testkey.avbpubkey",
5447 private_key: "testkey.pem",
5448 }
5449
5450 prebuilt_apex {
5451 name: "myapex",
5452 arch: {
5453 arm64: {
5454 src: "myapex-arm64.apex",
5455 },
5456 arm: {
5457 src: "myapex-arm.apex",
5458 },
5459 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005460 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5461 }
5462
5463 prebuilt_bootclasspath_fragment {
5464 name: "my-bootclasspath-fragment",
5465 contents: ["libfoo", "libbar"],
5466 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005467 hidden_api: {
5468 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5469 metadata: "my-bootclasspath-fragment/metadata.csv",
5470 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005471 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5472 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5473 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005474 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005475 }
5476
5477 java_import {
5478 name: "libfoo",
5479 jars: ["libfoo.jar"],
5480 apex_available: ["myapex"],
5481 }
5482
5483 java_library {
5484 name: "libfoo",
5485 srcs: ["foo/bar/MyClass.java"],
5486 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005487 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005488 }
Paul Duffin37856732021-02-26 14:24:15 +00005489
5490 java_sdk_library_import {
5491 name: "libbar",
5492 public: {
5493 jars: ["libbar.jar"],
5494 },
5495 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005496 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005497 }
5498
5499 java_sdk_library {
5500 name: "libbar",
5501 srcs: ["foo/bar/MyClass.java"],
5502 unsafe_ignore_missing_latest_api: true,
5503 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005504 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005505 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005506 `
5507
Paul Duffin89f570a2021-06-16 01:42:33 +01005508 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005509 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5510 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005511
Paul Duffin537ea3d2021-05-14 10:38:00 +01005512 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005513 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005514 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005515 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005516 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5517 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005518 })
5519
5520 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5521 bp := `
5522 apex {
5523 name: "myapex",
5524 enabled: false,
5525 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005526 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005527 }
5528
5529 apex_key {
5530 name: "myapex.key",
5531 public_key: "testkey.avbpubkey",
5532 private_key: "testkey.pem",
5533 }
5534
5535 prebuilt_apex {
5536 name: "myapex",
5537 arch: {
5538 arm64: {
5539 src: "myapex-arm64.apex",
5540 },
5541 arm: {
5542 src: "myapex-arm.apex",
5543 },
5544 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005545 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5546 }
5547
5548 prebuilt_bootclasspath_fragment {
5549 name: "my-bootclasspath-fragment",
5550 contents: ["libfoo", "libbar"],
5551 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005552 hidden_api: {
5553 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5554 metadata: "my-bootclasspath-fragment/metadata.csv",
5555 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005556 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5557 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5558 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005559 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005560 }
5561
5562 java_import {
5563 name: "libfoo",
5564 prefer: true,
5565 jars: ["libfoo.jar"],
5566 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005567 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005568 }
5569
5570 java_library {
5571 name: "libfoo",
5572 srcs: ["foo/bar/MyClass.java"],
5573 apex_available: ["myapex"],
5574 }
Paul Duffin37856732021-02-26 14:24:15 +00005575
5576 java_sdk_library_import {
5577 name: "libbar",
5578 prefer: true,
5579 public: {
5580 jars: ["libbar.jar"],
5581 },
5582 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005583 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005584 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005585 }
5586
5587 java_sdk_library {
5588 name: "libbar",
5589 srcs: ["foo/bar/MyClass.java"],
5590 unsafe_ignore_missing_latest_api: true,
5591 apex_available: ["myapex"],
5592 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005593 `
5594
Paul Duffin89f570a2021-06-16 01:42:33 +01005595 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005596 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5597 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005598
Paul Duffin537ea3d2021-05-14 10:38:00 +01005599 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005600 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005601 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005602 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005603 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5604 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005605 })
5606}
5607
Roland Levillain630846d2019-06-26 12:48:34 +01005608func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005609 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005610 apex_test {
5611 name: "myapex",
5612 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005613 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005614 tests: [
5615 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005616 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005617 ],
5618 }
5619
5620 apex_key {
5621 name: "myapex.key",
5622 public_key: "testkey.avbpubkey",
5623 private_key: "testkey.pem",
5624 }
5625
Liz Kammer1c14a212020-05-12 15:26:55 -07005626 filegroup {
5627 name: "fg",
5628 srcs: [
5629 "baz",
5630 "bar/baz"
5631 ],
5632 }
5633
Roland Levillain630846d2019-06-26 12:48:34 +01005634 cc_test {
5635 name: "mytest",
5636 gtest: false,
5637 srcs: ["mytest.cpp"],
5638 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005639 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005640 system_shared_libs: [],
5641 static_executable: true,
5642 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005643 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005644 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005645
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005646 cc_library {
5647 name: "mylib",
5648 srcs: ["mylib.cpp"],
5649 system_shared_libs: [],
5650 stl: "none",
5651 }
5652
Liz Kammer5bd365f2020-05-27 15:15:11 -07005653 filegroup {
5654 name: "fg2",
5655 srcs: [
5656 "testdata/baz"
5657 ],
5658 }
5659
Roland Levillain9b5fde92019-06-28 15:41:19 +01005660 cc_test {
5661 name: "mytests",
5662 gtest: false,
5663 srcs: [
5664 "mytest1.cpp",
5665 "mytest2.cpp",
5666 "mytest3.cpp",
5667 ],
5668 test_per_src: true,
5669 relative_install_path: "test",
5670 system_shared_libs: [],
5671 static_executable: true,
5672 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005673 data: [
5674 ":fg",
5675 ":fg2",
5676 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005677 }
Roland Levillain630846d2019-06-26 12:48:34 +01005678 `)
5679
Sundong Ahnabb64432019-10-22 13:58:29 +09005680 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005681 copyCmds := apexRule.Args["copy_commands"]
5682
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005683 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005684 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005685 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005686
Liz Kammer1c14a212020-05-12 15:26:55 -07005687 //Ensure that test data are copied into apex.
5688 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5689 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5690
Roland Levillain9b5fde92019-06-28 15:41:19 +01005691 // Ensure that test deps built with `test_per_src` are copied into apex.
5692 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5693 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5694 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005695
5696 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005697 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005698 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005699 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005700 prefix := "TARGET_"
5701 var builder strings.Builder
5702 data.Custom(&builder, name, prefix, "", data)
5703 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005704 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5705 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5706 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5707 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
5708 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
5709 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005710 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005711
5712 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005713 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005714 data.Custom(&builder, name, prefix, "", data)
5715 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005716 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5717 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005718}
5719
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005720func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005721 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005722 apex {
5723 name: "myapex",
5724 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005725 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005726 }
5727 apex_key {
5728 name: "myapex.key",
5729 public_key: "testkey.avbpubkey",
5730 private_key: "testkey.pem",
5731 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005732 `,
5733 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5734 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5735 }),
5736 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005737 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00005738 ensureListContains(t, ab.makeModulesToInstall, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005739 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005740 var builder strings.Builder
5741 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5742 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005743 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex myapex.flattened\n")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005744}
5745
Jooyung Hand48f3c32019-08-23 11:18:57 +09005746func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5747 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5748 apex {
5749 name: "myapex",
5750 key: "myapex.key",
5751 native_shared_libs: ["libfoo"],
5752 }
5753
5754 apex_key {
5755 name: "myapex.key",
5756 public_key: "testkey.avbpubkey",
5757 private_key: "testkey.pem",
5758 }
5759
5760 cc_library {
5761 name: "libfoo",
5762 stl: "none",
5763 system_shared_libs: [],
5764 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005765 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005766 }
5767 `)
5768 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5769 apex {
5770 name: "myapex",
5771 key: "myapex.key",
5772 java_libs: ["myjar"],
5773 }
5774
5775 apex_key {
5776 name: "myapex.key",
5777 public_key: "testkey.avbpubkey",
5778 private_key: "testkey.pem",
5779 }
5780
5781 java_library {
5782 name: "myjar",
5783 srcs: ["foo/bar/MyClass.java"],
5784 sdk_version: "none",
5785 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005786 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005787 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005788 }
5789 `)
5790}
5791
Bill Peckhama41a6962021-01-11 10:58:54 -08005792func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005793 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005794 apex {
5795 name: "myapex",
5796 key: "myapex.key",
5797 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005798 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005799 }
5800
5801 apex_key {
5802 name: "myapex.key",
5803 public_key: "testkey.avbpubkey",
5804 private_key: "testkey.pem",
5805 }
5806
5807 java_import {
5808 name: "myjavaimport",
5809 apex_available: ["myapex"],
5810 jars: ["my.jar"],
5811 compile_dex: true,
5812 }
5813 `)
5814
5815 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5816 apexRule := module.Rule("apexRule")
5817 copyCmds := apexRule.Args["copy_commands"]
5818 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5819}
5820
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005821func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005822 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005823 apex {
5824 name: "myapex",
5825 key: "myapex.key",
5826 apps: [
5827 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005828 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005829 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005830 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005831 }
5832
5833 apex_key {
5834 name: "myapex.key",
5835 public_key: "testkey.avbpubkey",
5836 private_key: "testkey.pem",
5837 }
5838
5839 android_app {
5840 name: "AppFoo",
5841 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005842 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005843 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005844 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005845 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005846 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005847 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005848
5849 android_app {
5850 name: "AppFooPriv",
5851 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005852 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005853 system_modules: "none",
5854 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005855 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005856 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005857 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005858
5859 cc_library_shared {
5860 name: "libjni",
5861 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005862 shared_libs: ["libfoo"],
5863 stl: "none",
5864 system_shared_libs: [],
5865 apex_available: [ "myapex" ],
5866 sdk_version: "current",
5867 }
5868
5869 cc_library_shared {
5870 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005871 stl: "none",
5872 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005873 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005874 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005875 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005876 `)
5877
Sundong Ahnabb64432019-10-22 13:58:29 +09005878 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005879 apexRule := module.Rule("apexRule")
5880 copyCmds := apexRule.Args["copy_commands"]
5881
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005882 ensureContains(t, copyCmds, "image.apex/app/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5883 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005884
Colin Crossaede88c2020-08-11 12:17:01 -07005885 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005886 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005887 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005888 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005889 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005890 // JNI libraries including transitive deps are
5891 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005892 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005893 // ... embedded inside APK (jnilibs.zip)
5894 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5895 // ... and not directly inside the APEX
5896 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5897 }
Dario Frenicde2a032019-10-27 00:29:22 +01005898}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005899
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005900func TestApexWithAppImportBuildId(t *testing.T) {
5901 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5902 for _, id := range invalidBuildIds {
5903 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5904 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5905 variables.BuildId = proptools.StringPtr(id)
5906 })
5907 testApexError(t, message, `apex {
5908 name: "myapex",
5909 key: "myapex.key",
5910 apps: ["AppFooPrebuilt"],
5911 updatable: false,
5912 }
5913
5914 apex_key {
5915 name: "myapex.key",
5916 public_key: "testkey.avbpubkey",
5917 private_key: "testkey.pem",
5918 }
5919
5920 android_app_import {
5921 name: "AppFooPrebuilt",
5922 apk: "PrebuiltAppFoo.apk",
5923 presigned: true,
5924 apex_available: ["myapex"],
5925 }
5926 `, fixture)
5927 }
5928}
5929
Dario Frenicde2a032019-10-27 00:29:22 +01005930func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005931 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01005932 apex {
5933 name: "myapex",
5934 key: "myapex.key",
5935 apps: [
5936 "AppFooPrebuilt",
5937 "AppFooPrivPrebuilt",
5938 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005939 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01005940 }
5941
5942 apex_key {
5943 name: "myapex.key",
5944 public_key: "testkey.avbpubkey",
5945 private_key: "testkey.pem",
5946 }
5947
5948 android_app_import {
5949 name: "AppFooPrebuilt",
5950 apk: "PrebuiltAppFoo.apk",
5951 presigned: true,
5952 dex_preopt: {
5953 enabled: false,
5954 },
Jiyong Park592a6a42020-04-21 22:34:28 +09005955 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005956 }
5957
5958 android_app_import {
5959 name: "AppFooPrivPrebuilt",
5960 apk: "PrebuiltAppFooPriv.apk",
5961 privileged: true,
5962 presigned: true,
5963 dex_preopt: {
5964 enabled: false,
5965 },
Jooyung Han39ee1192020-03-23 20:21:11 +09005966 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09005967 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01005968 }
5969 `)
5970
Sundong Ahnabb64432019-10-22 13:58:29 +09005971 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01005972 apexRule := module.Rule("apexRule")
5973 copyCmds := apexRule.Args["copy_commands"]
5974
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005975 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
5976 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09005977}
5978
5979func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005980 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09005981 apex {
5982 name: "myapex",
5983 key: "myapex.key",
5984 apps: [
5985 "AppFoo",
5986 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005987 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09005988 }
5989
5990 apex_key {
5991 name: "myapex.key",
5992 public_key: "testkey.avbpubkey",
5993 private_key: "testkey.pem",
5994 }
5995
5996 android_app {
5997 name: "AppFoo",
5998 srcs: ["foo/bar/MyClass.java"],
5999 sdk_version: "none",
6000 system_modules: "none",
6001 apex_available: [ "myapex" ],
6002 }
6003
6004 android_app_import {
6005 name: "AppFoo",
6006 apk: "AppFooPrebuilt.apk",
6007 filename: "AppFooPrebuilt.apk",
6008 presigned: true,
6009 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006010 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006011 }
6012 `, withFiles(map[string][]byte{
6013 "AppFooPrebuilt.apk": nil,
6014 }))
6015
6016 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006017 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006018 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006019}
6020
Dario Freni6f3937c2019-12-20 22:58:03 +00006021func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006022 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006023 apex {
6024 name: "myapex",
6025 key: "myapex.key",
6026 apps: [
6027 "TesterHelpAppFoo",
6028 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006029 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006030 }
6031
6032 apex_key {
6033 name: "myapex.key",
6034 public_key: "testkey.avbpubkey",
6035 private_key: "testkey.pem",
6036 }
6037
6038 android_test_helper_app {
6039 name: "TesterHelpAppFoo",
6040 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006041 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006042 }
6043
6044 `)
6045
6046 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6047 apexRule := module.Rule("apexRule")
6048 copyCmds := apexRule.Args["copy_commands"]
6049
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006050 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006051}
6052
Jooyung Han18020ea2019-11-13 10:50:48 +09006053func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6054 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006055 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006056 apex {
6057 name: "myapex",
6058 key: "myapex.key",
6059 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006060 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006061 }
6062
6063 apex_key {
6064 name: "myapex.key",
6065 public_key: "testkey.avbpubkey",
6066 private_key: "testkey.pem",
6067 }
6068
6069 apex {
6070 name: "otherapex",
6071 key: "myapex.key",
6072 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006073 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006074 }
6075
6076 cc_defaults {
6077 name: "libfoo-defaults",
6078 apex_available: ["otherapex"],
6079 }
6080
6081 cc_library {
6082 name: "libfoo",
6083 defaults: ["libfoo-defaults"],
6084 stl: "none",
6085 system_shared_libs: [],
6086 }`)
6087}
6088
Paul Duffine52e66f2020-03-30 17:54:29 +01006089func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006090 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006091 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006092 apex {
6093 name: "myapex",
6094 key: "myapex.key",
6095 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006096 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006097 }
6098
6099 apex_key {
6100 name: "myapex.key",
6101 public_key: "testkey.avbpubkey",
6102 private_key: "testkey.pem",
6103 }
6104
6105 apex {
6106 name: "otherapex",
6107 key: "otherapex.key",
6108 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006109 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006110 }
6111
6112 apex_key {
6113 name: "otherapex.key",
6114 public_key: "testkey.avbpubkey",
6115 private_key: "testkey.pem",
6116 }
6117
6118 cc_library {
6119 name: "libfoo",
6120 stl: "none",
6121 system_shared_libs: [],
6122 apex_available: ["otherapex"],
6123 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006124}
Jiyong Park127b40b2019-09-30 16:04:35 +09006125
Paul Duffine52e66f2020-03-30 17:54:29 +01006126func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006127 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006128 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006129.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006130.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006131.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006132.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006133.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006134.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006135 apex {
6136 name: "myapex",
6137 key: "myapex.key",
6138 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006139 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006140 }
6141
6142 apex_key {
6143 name: "myapex.key",
6144 public_key: "testkey.avbpubkey",
6145 private_key: "testkey.pem",
6146 }
6147
Jiyong Park127b40b2019-09-30 16:04:35 +09006148 cc_library {
6149 name: "libfoo",
6150 stl: "none",
6151 shared_libs: ["libbar"],
6152 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006153 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006154 }
6155
6156 cc_library {
6157 name: "libbar",
6158 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006159 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006160 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006161 apex_available: ["myapex"],
6162 }
6163
6164 cc_library {
6165 name: "libbaz",
6166 stl: "none",
6167 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006168 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006169}
Jiyong Park127b40b2019-09-30 16:04:35 +09006170
Paul Duffine52e66f2020-03-30 17:54:29 +01006171func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006172 testApexError(t, "\"otherapex\" is not a valid module name", `
6173 apex {
6174 name: "myapex",
6175 key: "myapex.key",
6176 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006177 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006178 }
6179
6180 apex_key {
6181 name: "myapex.key",
6182 public_key: "testkey.avbpubkey",
6183 private_key: "testkey.pem",
6184 }
6185
6186 cc_library {
6187 name: "libfoo",
6188 stl: "none",
6189 system_shared_libs: [],
6190 apex_available: ["otherapex"],
6191 }`)
6192
Paul Duffine52e66f2020-03-30 17:54:29 +01006193 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006194 apex {
6195 name: "myapex",
6196 key: "myapex.key",
6197 native_shared_libs: ["libfoo", "libbar"],
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 cc_library {
6208 name: "libfoo",
6209 stl: "none",
6210 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006211 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006212 apex_available: ["myapex"],
6213 }
6214
6215 cc_library {
6216 name: "libbar",
6217 stl: "none",
6218 system_shared_libs: [],
6219 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006220 }
6221
6222 cc_library {
6223 name: "libbaz",
6224 stl: "none",
6225 system_shared_libs: [],
6226 stubs: {
6227 versions: ["10", "20", "30"],
6228 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006229 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006230}
Jiyong Park127b40b2019-09-30 16:04:35 +09006231
Jiyong Park89e850a2020-04-07 16:37:39 +09006232func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006233 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006234 apex {
6235 name: "myapex",
6236 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006237 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006238 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006239 }
6240
6241 apex_key {
6242 name: "myapex.key",
6243 public_key: "testkey.avbpubkey",
6244 private_key: "testkey.pem",
6245 }
6246
6247 cc_library {
6248 name: "libfoo",
6249 stl: "none",
6250 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006251 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006252 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006253 }
6254
6255 cc_library {
6256 name: "libfoo2",
6257 stl: "none",
6258 system_shared_libs: [],
6259 shared_libs: ["libbaz"],
6260 apex_available: ["//apex_available:platform"],
6261 }
6262
6263 cc_library {
6264 name: "libbar",
6265 stl: "none",
6266 system_shared_libs: [],
6267 apex_available: ["myapex"],
6268 }
6269
6270 cc_library {
6271 name: "libbaz",
6272 stl: "none",
6273 system_shared_libs: [],
6274 apex_available: ["myapex"],
6275 stubs: {
6276 versions: ["1"],
6277 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006278 }`)
6279
Jiyong Park89e850a2020-04-07 16:37:39 +09006280 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6281 // because it depends on libbar which isn't available to platform
6282 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6283 if libfoo.NotAvailableForPlatform() != true {
6284 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6285 }
6286
6287 // libfoo2 however can be available to platform because it depends on libbaz which provides
6288 // stubs
6289 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6290 if libfoo2.NotAvailableForPlatform() == true {
6291 t.Errorf("%q should be available to platform", libfoo2.String())
6292 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006293}
Jiyong Parka90ca002019-10-07 15:47:24 +09006294
Paul Duffine52e66f2020-03-30 17:54:29 +01006295func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006296 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006297 apex {
6298 name: "myapex",
6299 key: "myapex.key",
6300 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006301 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006302 }
6303
6304 apex_key {
6305 name: "myapex.key",
6306 public_key: "testkey.avbpubkey",
6307 private_key: "testkey.pem",
6308 }
6309
6310 cc_library {
6311 name: "libfoo",
6312 stl: "none",
6313 system_shared_libs: [],
6314 apex_available: ["myapex"],
6315 static: {
6316 apex_available: ["//apex_available:platform"],
6317 },
6318 }`)
6319
Jiyong Park89e850a2020-04-07 16:37:39 +09006320 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6321 if libfooShared.NotAvailableForPlatform() != true {
6322 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6323 }
6324 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6325 if libfooStatic.NotAvailableForPlatform() != false {
6326 t.Errorf("%q should be available to platform", libfooStatic.String())
6327 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006328}
6329
Jiyong Park5d790c32019-11-15 18:40:32 +09006330func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006331 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006332 apex {
6333 name: "myapex",
6334 key: "myapex.key",
6335 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006336 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006337 prebuilts: ["myetc"],
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006338 bootclasspath_fragments: ["mybootclasspath_fragment"],
6339 systemserverclasspath_fragments: ["mysystemserverclasspath_fragment"],
6340 java_libs: ["myjava_library"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006341 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006342 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006343 }
6344
6345 override_apex {
6346 name: "override_myapex",
6347 base: "myapex",
6348 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006349 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006350 prebuilts: ["override_myetc"],
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006351 bootclasspath_fragments: ["override_bootclasspath_fragment"],
6352 systemserverclasspath_fragments: ["override_systemserverclasspath_fragment"],
6353 java_libs: ["override_java_library"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006354 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006355 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006356 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006357 key: "mynewapex.key",
6358 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006359 }
6360
6361 apex_key {
6362 name: "myapex.key",
6363 public_key: "testkey.avbpubkey",
6364 private_key: "testkey.pem",
6365 }
6366
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006367 apex_key {
6368 name: "mynewapex.key",
6369 public_key: "testkey2.avbpubkey",
6370 private_key: "testkey2.pem",
6371 }
6372
6373 android_app_certificate {
6374 name: "myapex.certificate",
6375 certificate: "testkey",
6376 }
6377
Jiyong Park5d790c32019-11-15 18:40:32 +09006378 android_app {
6379 name: "app",
6380 srcs: ["foo/bar/MyClass.java"],
6381 package_name: "foo",
6382 sdk_version: "none",
6383 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006384 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006385 }
6386
6387 override_android_app {
6388 name: "override_app",
6389 base: "app",
6390 package_name: "bar",
6391 }
markchien7c803b82021-08-26 22:10:06 +08006392
6393 bpf {
6394 name: "bpf",
6395 srcs: ["bpf.c"],
6396 }
6397
6398 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006399 name: "overrideBpf",
6400 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006401 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006402
6403 prebuilt_etc {
6404 name: "myetc",
6405 src: "myprebuilt",
6406 }
6407
6408 prebuilt_etc {
6409 name: "override_myetc",
6410 src: "override_myprebuilt",
6411 }
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006412
6413 java_library {
6414 name: "bcplib",
6415 srcs: ["a.java"],
6416 compile_dex: true,
6417 apex_available: ["myapex"],
6418 permitted_packages: ["bcp.lib"],
6419 }
6420
6421 bootclasspath_fragment {
6422 name: "mybootclasspath_fragment",
6423 contents: ["bcplib"],
6424 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01006425 hidden_api: {
6426 split_packages: ["*"],
6427 },
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006428 }
6429
6430 java_library {
6431 name: "override_bcplib",
6432 srcs: ["a.java"],
6433 compile_dex: true,
6434 apex_available: ["myapex"],
6435 permitted_packages: ["override.bcp.lib"],
6436 }
6437
6438 bootclasspath_fragment {
6439 name: "override_bootclasspath_fragment",
6440 contents: ["override_bcplib"],
6441 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01006442 hidden_api: {
6443 split_packages: ["*"],
6444 },
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006445 }
6446
6447 java_library {
6448 name: "systemserverlib",
6449 srcs: ["a.java"],
6450 apex_available: ["myapex"],
6451 }
6452
6453 systemserverclasspath_fragment {
6454 name: "mysystemserverclasspath_fragment",
6455 standalone_contents: ["systemserverlib"],
6456 apex_available: ["myapex"],
6457 }
6458
6459 java_library {
6460 name: "override_systemserverlib",
6461 srcs: ["a.java"],
6462 apex_available: ["myapex"],
6463 }
6464
6465 systemserverclasspath_fragment {
6466 name: "override_systemserverclasspath_fragment",
6467 standalone_contents: ["override_systemserverlib"],
6468 apex_available: ["myapex"],
6469 }
6470
6471 java_library {
6472 name: "myjava_library",
6473 srcs: ["a.java"],
6474 compile_dex: true,
6475 apex_available: ["myapex"],
6476 }
6477
6478 java_library {
6479 name: "override_java_library",
6480 srcs: ["a.java"],
6481 compile_dex: true,
6482 apex_available: ["myapex"],
6483 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006484 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006485
Jiyong Park317645e2019-12-05 13:20:58 +09006486 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6487 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6488 if originalVariant.GetOverriddenBy() != "" {
6489 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6490 }
6491 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6492 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6493 }
6494
Jiyong Park5d790c32019-11-15 18:40:32 +09006495 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6496 apexRule := module.Rule("apexRule")
6497 copyCmds := apexRule.Args["copy_commands"]
6498
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006499 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6500 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006501
markchien7c803b82021-08-26 22:10:06 +08006502 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006503 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006504
Daniel Norman5a3ce132021-08-26 15:44:43 -07006505 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6506 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6507
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006508 apexBundle := module.Module().(*apexBundle)
6509 name := apexBundle.Name()
6510 if name != "override_myapex" {
6511 t.Errorf("name should be \"override_myapex\", but was %q", name)
6512 }
6513
Baligh Uddin004d7172020-02-19 21:29:28 -08006514 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6515 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6516 }
6517
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006518 android.AssertArrayString(t, "Bootclasspath_fragments does not match",
6519 []string{"override_bootclasspath_fragment"}, apexBundle.overridableProperties.Bootclasspath_fragments)
6520 android.AssertArrayString(t, "Systemserverclasspath_fragments does not match",
6521 []string{"override_systemserverclasspath_fragment"}, apexBundle.overridableProperties.Systemserverclasspath_fragments)
6522 android.AssertArrayString(t, "Java_libs does not match",
6523 []string{"override_java_library"}, apexBundle.overridableProperties.Java_libs)
6524
Jiyong Park20bacab2020-03-03 11:45:41 +09006525 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006526 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006527 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6528
6529 signApkRule := module.Rule("signapk")
6530 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006531
Colin Crossaa255532020-07-03 13:18:24 -07006532 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006533 var builder strings.Builder
6534 data.Custom(&builder, name, "TARGET_", "", data)
6535 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006536 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6537 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
6538 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
6539 ensureContains(t, androidMk, "LOCAL_MODULE := override_bcplib.override_myapex")
6540 ensureContains(t, androidMk, "LOCAL_MODULE := override_systemserverlib.override_myapex")
6541 ensureContains(t, androidMk, "LOCAL_MODULE := override_java_library.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006542 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006543 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006544 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006545 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006546 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006547 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
Remi NGUYEN VANbe901722022-03-02 21:00:33 +09006548 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_bcplib.myapex")
6549 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_systemserverlib.myapex")
6550 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_java_library.pb.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006551 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006552}
6553
Albert Martineefabcf2022-03-21 20:11:16 +00006554func TestMinSdkVersionOverride(t *testing.T) {
6555 // Override from 29 to 31
6556 minSdkOverride31 := "31"
6557 ctx := testApex(t, `
6558 apex {
6559 name: "myapex",
6560 key: "myapex.key",
6561 native_shared_libs: ["mylib"],
6562 updatable: true,
6563 min_sdk_version: "29"
6564 }
6565
6566 override_apex {
6567 name: "override_myapex",
6568 base: "myapex",
6569 logging_parent: "com.foo.bar",
6570 package_name: "test.overridden.package"
6571 }
6572
6573 apex_key {
6574 name: "myapex.key",
6575 public_key: "testkey.avbpubkey",
6576 private_key: "testkey.pem",
6577 }
6578
6579 cc_library {
6580 name: "mylib",
6581 srcs: ["mylib.cpp"],
6582 runtime_libs: ["libbar"],
6583 system_shared_libs: [],
6584 stl: "none",
6585 apex_available: [ "myapex" ],
6586 min_sdk_version: "apex_inherit"
6587 }
6588
6589 cc_library {
6590 name: "libbar",
6591 srcs: ["mylib.cpp"],
6592 system_shared_libs: [],
6593 stl: "none",
6594 apex_available: [ "myapex" ],
6595 min_sdk_version: "apex_inherit"
6596 }
6597
6598 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6599
6600 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6601 copyCmds := apexRule.Args["copy_commands"]
6602
6603 // Ensure that direct non-stubs dep is always included
6604 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6605
6606 // Ensure that runtime_libs dep in included
6607 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6608
6609 // Ensure libraries target overridden min_sdk_version value
6610 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6611}
6612
6613func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6614 // Attempt to override from 31 to 29, should be a NOOP
6615 minSdkOverride29 := "29"
6616 ctx := testApex(t, `
6617 apex {
6618 name: "myapex",
6619 key: "myapex.key",
6620 native_shared_libs: ["mylib"],
6621 updatable: true,
6622 min_sdk_version: "31"
6623 }
6624
6625 override_apex {
6626 name: "override_myapex",
6627 base: "myapex",
6628 logging_parent: "com.foo.bar",
6629 package_name: "test.overridden.package"
6630 }
6631
6632 apex_key {
6633 name: "myapex.key",
6634 public_key: "testkey.avbpubkey",
6635 private_key: "testkey.pem",
6636 }
6637
6638 cc_library {
6639 name: "mylib",
6640 srcs: ["mylib.cpp"],
6641 runtime_libs: ["libbar"],
6642 system_shared_libs: [],
6643 stl: "none",
6644 apex_available: [ "myapex" ],
6645 min_sdk_version: "apex_inherit"
6646 }
6647
6648 cc_library {
6649 name: "libbar",
6650 srcs: ["mylib.cpp"],
6651 system_shared_libs: [],
6652 stl: "none",
6653 apex_available: [ "myapex" ],
6654 min_sdk_version: "apex_inherit"
6655 }
6656
6657 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6658
6659 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6660 copyCmds := apexRule.Args["copy_commands"]
6661
6662 // Ensure that direct non-stubs dep is always included
6663 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6664
6665 // Ensure that runtime_libs dep in included
6666 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6667
6668 // Ensure libraries target the original min_sdk_version value rather than the overridden
6669 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6670}
6671
Jooyung Han214bf372019-11-12 13:03:50 +09006672func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006673 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006674 apex {
6675 name: "myapex",
6676 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006677 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006678 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006679 }
6680
6681 apex_key {
6682 name: "myapex.key",
6683 public_key: "testkey.avbpubkey",
6684 private_key: "testkey.pem",
6685 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006686
6687 cc_library {
6688 name: "mylib",
6689 srcs: ["mylib.cpp"],
6690 stl: "libc++",
6691 system_shared_libs: [],
6692 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006693 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006694 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006695 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006696
6697 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6698 args := module.Rule("apexRule").Args
6699 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006700 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006701
6702 // The copies of the libraries in the apex should have one more dependency than
6703 // the ones outside the apex, namely the unwinder. Ideally we should check
6704 // the dependency names directly here but for some reason the names are blank in
6705 // this test.
6706 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006707 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006708 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6709 if len(apexImplicits) != len(nonApexImplicits)+1 {
6710 t.Errorf("%q missing unwinder dep", lib)
6711 }
6712 }
Jooyung Han214bf372019-11-12 13:03:50 +09006713}
6714
Paul Duffine05480a2021-03-08 15:07:14 +00006715var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006716 "api/current.txt": nil,
6717 "api/removed.txt": nil,
6718 "api/system-current.txt": nil,
6719 "api/system-removed.txt": nil,
6720 "api/test-current.txt": nil,
6721 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006722
Anton Hanssondff2c782020-12-21 17:10:01 +00006723 "100/public/api/foo.txt": nil,
6724 "100/public/api/foo-removed.txt": nil,
6725 "100/system/api/foo.txt": nil,
6726 "100/system/api/foo-removed.txt": nil,
6727
Paul Duffineedc5d52020-06-12 17:46:39 +01006728 // For java_sdk_library_import
6729 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006730}
6731
Jooyung Han58f26ab2019-12-18 15:34:32 +09006732func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006733 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006734 apex {
6735 name: "myapex",
6736 key: "myapex.key",
6737 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006738 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006739 }
6740
6741 apex_key {
6742 name: "myapex.key",
6743 public_key: "testkey.avbpubkey",
6744 private_key: "testkey.pem",
6745 }
6746
6747 java_sdk_library {
6748 name: "foo",
6749 srcs: ["a.java"],
6750 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006751 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006752 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006753
6754 prebuilt_apis {
6755 name: "sdk",
6756 api_dirs: ["100"],
6757 }
Paul Duffin9b879592020-05-26 13:21:35 +01006758 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006759
6760 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006761 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006762 "javalib/foo.jar",
6763 "etc/permissions/foo.xml",
6764 })
6765 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006766 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006767 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 +09006768}
6769
Paul Duffin9b879592020-05-26 13:21:35 +01006770func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006771 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006772 apex {
6773 name: "myapex",
6774 key: "myapex.key",
6775 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006776 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006777 }
6778
6779 apex_key {
6780 name: "myapex.key",
6781 public_key: "testkey.avbpubkey",
6782 private_key: "testkey.pem",
6783 }
6784
6785 java_sdk_library {
6786 name: "foo",
6787 srcs: ["a.java"],
6788 api_packages: ["foo"],
6789 apex_available: ["myapex"],
6790 sdk_version: "none",
6791 system_modules: "none",
6792 }
6793
6794 java_library {
6795 name: "bar",
6796 srcs: ["a.java"],
6797 libs: ["foo"],
6798 apex_available: ["myapex"],
6799 sdk_version: "none",
6800 system_modules: "none",
6801 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006802
6803 prebuilt_apis {
6804 name: "sdk",
6805 api_dirs: ["100"],
6806 }
Paul Duffin9b879592020-05-26 13:21:35 +01006807 `, withFiles(filesForSdkLibrary))
6808
6809 // java_sdk_library installs both impl jar and permission XML
6810 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6811 "javalib/bar.jar",
6812 "javalib/foo.jar",
6813 "etc/permissions/foo.xml",
6814 })
6815
6816 // The bar library should depend on the implementation jar.
6817 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006818 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006819 t.Errorf("expected %q, found %#q", expected, actual)
6820 }
6821}
6822
6823func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006824 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006825 apex {
6826 name: "myapex",
6827 key: "myapex.key",
6828 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006829 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006830 }
6831
6832 apex_key {
6833 name: "myapex.key",
6834 public_key: "testkey.avbpubkey",
6835 private_key: "testkey.pem",
6836 }
6837
6838 java_sdk_library {
6839 name: "foo",
6840 srcs: ["a.java"],
6841 api_packages: ["foo"],
6842 apex_available: ["myapex"],
6843 sdk_version: "none",
6844 system_modules: "none",
6845 }
6846
6847 java_library {
6848 name: "bar",
6849 srcs: ["a.java"],
6850 libs: ["foo"],
6851 sdk_version: "none",
6852 system_modules: "none",
6853 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006854
6855 prebuilt_apis {
6856 name: "sdk",
6857 api_dirs: ["100"],
6858 }
Paul Duffin9b879592020-05-26 13:21:35 +01006859 `, withFiles(filesForSdkLibrary))
6860
6861 // java_sdk_library installs both impl jar and permission XML
6862 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6863 "javalib/foo.jar",
6864 "etc/permissions/foo.xml",
6865 })
6866
6867 // The bar library should depend on the stubs jar.
6868 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006869 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006870 t.Errorf("expected %q, found %#q", expected, actual)
6871 }
6872}
6873
Paul Duffineedc5d52020-06-12 17:46:39 +01006874func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006875 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006876 prebuilt_apis {
6877 name: "sdk",
6878 api_dirs: ["100"],
6879 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006880 withFiles(map[string][]byte{
6881 "apex/a.java": nil,
6882 "apex/apex_manifest.json": nil,
6883 "apex/Android.bp": []byte(`
6884 package {
6885 default_visibility: ["//visibility:private"],
6886 }
6887
6888 apex {
6889 name: "myapex",
6890 key: "myapex.key",
6891 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006892 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006893 }
6894
6895 apex_key {
6896 name: "myapex.key",
6897 public_key: "testkey.avbpubkey",
6898 private_key: "testkey.pem",
6899 }
6900
6901 java_library {
6902 name: "bar",
6903 srcs: ["a.java"],
6904 libs: ["foo"],
6905 apex_available: ["myapex"],
6906 sdk_version: "none",
6907 system_modules: "none",
6908 }
6909`),
6910 "source/a.java": nil,
6911 "source/api/current.txt": nil,
6912 "source/api/removed.txt": nil,
6913 "source/Android.bp": []byte(`
6914 package {
6915 default_visibility: ["//visibility:private"],
6916 }
6917
6918 java_sdk_library {
6919 name: "foo",
6920 visibility: ["//apex"],
6921 srcs: ["a.java"],
6922 api_packages: ["foo"],
6923 apex_available: ["myapex"],
6924 sdk_version: "none",
6925 system_modules: "none",
6926 public: {
6927 enabled: true,
6928 },
6929 }
6930`),
6931 "prebuilt/a.jar": nil,
6932 "prebuilt/Android.bp": []byte(`
6933 package {
6934 default_visibility: ["//visibility:private"],
6935 }
6936
6937 java_sdk_library_import {
6938 name: "foo",
6939 visibility: ["//apex", "//source"],
6940 apex_available: ["myapex"],
6941 prefer: true,
6942 public: {
6943 jars: ["a.jar"],
6944 },
6945 }
6946`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006947 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006948 )
6949
6950 // java_sdk_library installs both impl jar and permission XML
6951 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6952 "javalib/bar.jar",
6953 "javalib/foo.jar",
6954 "etc/permissions/foo.xml",
6955 })
6956
6957 // The bar library should depend on the implementation jar.
6958 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006959 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006960 t.Errorf("expected %q, found %#q", expected, actual)
6961 }
6962}
6963
6964func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6965 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6966 apex {
6967 name: "myapex",
6968 key: "myapex.key",
6969 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006970 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006971 }
6972
6973 apex_key {
6974 name: "myapex.key",
6975 public_key: "testkey.avbpubkey",
6976 private_key: "testkey.pem",
6977 }
6978
6979 java_sdk_library_import {
6980 name: "foo",
6981 apex_available: ["myapex"],
6982 prefer: true,
6983 public: {
6984 jars: ["a.jar"],
6985 },
6986 }
6987
6988 `, withFiles(filesForSdkLibrary))
6989}
6990
atrost6e126252020-01-27 17:01:16 +00006991func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006992 result := android.GroupFixturePreparers(
6993 prepareForApexTest,
6994 java.PrepareForTestWithPlatformCompatConfig,
6995 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006996 apex {
6997 name: "myapex",
6998 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00006999 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007000 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007001 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007002 }
7003
7004 apex_key {
7005 name: "myapex.key",
7006 public_key: "testkey.avbpubkey",
7007 private_key: "testkey.pem",
7008 }
7009
7010 platform_compat_config {
7011 name: "myjar-platform-compat-config",
7012 src: ":myjar",
7013 }
7014
7015 java_library {
7016 name: "myjar",
7017 srcs: ["foo/bar/MyClass.java"],
7018 sdk_version: "none",
7019 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007020 apex_available: [ "myapex" ],
7021 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007022
7023 // Make sure that a preferred prebuilt does not affect the apex contents.
7024 prebuilt_platform_compat_config {
7025 name: "myjar-platform-compat-config",
7026 metadata: "compat-config/metadata.xml",
7027 prefer: true,
7028 }
atrost6e126252020-01-27 17:01:16 +00007029 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007030 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00007031 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7032 "etc/compatconfig/myjar-platform-compat-config.xml",
7033 "javalib/myjar.jar",
7034 })
7035}
7036
Jooyung Han862c0d62022-12-21 10:15:37 +09007037func TestNoDupeApexFiles(t *testing.T) {
7038 android.GroupFixturePreparers(
7039 android.PrepareForTestWithAndroidBuildComponents,
7040 PrepareForTestWithApexBuildComponents,
7041 prepareForTestWithMyapex,
7042 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7043 ).
7044 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7045 RunTestWithBp(t, `
7046 apex {
7047 name: "myapex",
7048 key: "myapex.key",
7049 prebuilts: ["foo", "bar"],
7050 updatable: false,
7051 }
7052
7053 apex_key {
7054 name: "myapex.key",
7055 public_key: "testkey.avbpubkey",
7056 private_key: "testkey.pem",
7057 }
7058
7059 prebuilt_etc {
7060 name: "foo",
7061 src: "myprebuilt",
7062 filename_from_src: true,
7063 }
7064
7065 prebuilt_etc {
7066 name: "bar",
7067 src: "myprebuilt",
7068 filename_from_src: true,
7069 }
7070 `)
7071}
7072
Jiyong Park479321d2019-12-16 11:47:12 +09007073func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7074 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7075 apex {
7076 name: "myapex",
7077 key: "myapex.key",
7078 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007079 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007080 }
7081
7082 apex_key {
7083 name: "myapex.key",
7084 public_key: "testkey.avbpubkey",
7085 private_key: "testkey.pem",
7086 }
7087
7088 java_library {
7089 name: "myjar",
7090 srcs: ["foo/bar/MyClass.java"],
7091 sdk_version: "none",
7092 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007093 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007094 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007095 }
7096 `)
7097}
7098
Jiyong Park7afd1072019-12-30 16:56:33 +09007099func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007100 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007101 apex {
7102 name: "myapex",
7103 key: "myapex.key",
7104 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007105 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007106 }
7107
7108 apex_key {
7109 name: "myapex.key",
7110 public_key: "testkey.avbpubkey",
7111 private_key: "testkey.pem",
7112 }
7113
7114 cc_library {
7115 name: "mylib",
7116 srcs: ["mylib.cpp"],
7117 system_shared_libs: [],
7118 stl: "none",
7119 required: ["a", "b"],
7120 host_required: ["c", "d"],
7121 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007122 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007123 }
7124 `)
7125
7126 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007127 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007128 name := apexBundle.BaseModuleName()
7129 prefix := "TARGET_"
7130 var builder strings.Builder
7131 data.Custom(&builder, name, prefix, "", data)
7132 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007133 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 -08007134 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7135 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007136}
7137
Jiyong Park7cd10e32020-01-14 09:22:18 +09007138func TestSymlinksFromApexToSystem(t *testing.T) {
7139 bp := `
7140 apex {
7141 name: "myapex",
7142 key: "myapex.key",
7143 native_shared_libs: ["mylib"],
7144 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007145 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007146 }
7147
Jiyong Park9d677202020-02-19 16:29:35 +09007148 apex {
7149 name: "myapex.updatable",
7150 key: "myapex.key",
7151 native_shared_libs: ["mylib"],
7152 java_libs: ["myjar"],
7153 updatable: true,
Jooyung Han548640b2020-04-27 12:10:30 +09007154 min_sdk_version: "current",
Jiyong Park9d677202020-02-19 16:29:35 +09007155 }
7156
Jiyong Park7cd10e32020-01-14 09:22:18 +09007157 apex_key {
7158 name: "myapex.key",
7159 public_key: "testkey.avbpubkey",
7160 private_key: "testkey.pem",
7161 }
7162
7163 cc_library {
7164 name: "mylib",
7165 srcs: ["mylib.cpp"],
7166 shared_libs: ["myotherlib"],
7167 system_shared_libs: [],
7168 stl: "none",
7169 apex_available: [
7170 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007171 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007172 "//apex_available:platform",
7173 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007174 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007175 }
7176
7177 cc_library {
7178 name: "myotherlib",
7179 srcs: ["mylib.cpp"],
7180 system_shared_libs: [],
7181 stl: "none",
7182 apex_available: [
7183 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007184 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007185 "//apex_available:platform",
7186 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007187 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007188 }
7189
7190 java_library {
7191 name: "myjar",
7192 srcs: ["foo/bar/MyClass.java"],
7193 sdk_version: "none",
7194 system_modules: "none",
7195 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007196 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 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007201 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007202 }
7203
7204 java_library {
7205 name: "myotherjar",
7206 srcs: ["foo/bar/MyClass.java"],
7207 sdk_version: "none",
7208 system_modules: "none",
7209 apex_available: [
7210 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007211 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007212 "//apex_available:platform",
7213 ],
Jooyung Han749dc692020-04-15 11:03:39 +09007214 min_sdk_version: "current",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007215 }
7216 `
7217
7218 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7219 for _, f := range files {
7220 if f.path == file {
7221 if f.isLink {
7222 t.Errorf("%q is not a real file", file)
7223 }
7224 return
7225 }
7226 }
7227 t.Errorf("%q is not found", file)
7228 }
7229
7230 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string) {
7231 for _, f := range files {
7232 if f.path == file {
7233 if !f.isLink {
7234 t.Errorf("%q is not a symlink", file)
7235 }
7236 return
7237 }
7238 }
7239 t.Errorf("%q is not found", file)
7240 }
7241
Jiyong Park9d677202020-02-19 16:29:35 +09007242 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7243 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007244 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007245 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007246 ensureRealfileExists(t, files, "javalib/myjar.jar")
7247 ensureRealfileExists(t, files, "lib64/mylib.so")
7248 ensureRealfileExists(t, files, "lib64/myotherlib.so")
7249
Jiyong Park9d677202020-02-19 16:29:35 +09007250 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7251 ensureRealfileExists(t, files, "javalib/myjar.jar")
7252 ensureRealfileExists(t, files, "lib64/mylib.so")
7253 ensureRealfileExists(t, files, "lib64/myotherlib.so")
7254
7255 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007256 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007257 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007258 ensureRealfileExists(t, files, "javalib/myjar.jar")
7259 ensureRealfileExists(t, files, "lib64/mylib.so")
7260 ensureSymlinkExists(t, files, "lib64/myotherlib.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007261
7262 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7263 ensureRealfileExists(t, files, "javalib/myjar.jar")
7264 ensureRealfileExists(t, files, "lib64/mylib.so")
7265 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007266}
7267
Yo Chiange8128052020-07-23 20:09:18 +08007268func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007269 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007270 apex {
7271 name: "myapex",
7272 key: "myapex.key",
7273 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007274 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007275 }
7276
7277 apex_key {
7278 name: "myapex.key",
7279 public_key: "testkey.avbpubkey",
7280 private_key: "testkey.pem",
7281 }
7282
7283 cc_library_shared {
7284 name: "mylib",
7285 srcs: ["mylib.cpp"],
7286 shared_libs: ["myotherlib"],
7287 system_shared_libs: [],
7288 stl: "none",
7289 apex_available: [
7290 "myapex",
7291 "//apex_available:platform",
7292 ],
7293 }
7294
7295 cc_prebuilt_library_shared {
7296 name: "myotherlib",
7297 srcs: ["prebuilt.so"],
7298 system_shared_libs: [],
7299 stl: "none",
7300 apex_available: [
7301 "myapex",
7302 "//apex_available:platform",
7303 ],
7304 }
7305 `)
7306
Prerana Patilb1896c82022-11-09 18:14:34 +00007307 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007308 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007309 var builder strings.Builder
7310 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7311 androidMk := builder.String()
7312 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007313 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007314 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7315 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7316 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007317 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 +08007318}
7319
Jooyung Han643adc42020-02-27 13:50:06 +09007320func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007321 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007322 apex {
7323 name: "myapex",
7324 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09007325 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007326 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007327 }
7328
7329 apex_key {
7330 name: "myapex.key",
7331 public_key: "testkey.avbpubkey",
7332 private_key: "testkey.pem",
7333 }
7334
7335 cc_library {
7336 name: "mylib",
7337 srcs: ["mylib.cpp"],
7338 shared_libs: ["mylib2"],
7339 system_shared_libs: [],
7340 stl: "none",
7341 apex_available: [ "myapex" ],
7342 }
7343
7344 cc_library {
7345 name: "mylib2",
7346 srcs: ["mylib.cpp"],
7347 system_shared_libs: [],
7348 stl: "none",
7349 apex_available: [ "myapex" ],
7350 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007351
7352 rust_ffi_shared {
7353 name: "libfoo.rust",
7354 crate_name: "foo",
7355 srcs: ["foo.rs"],
7356 shared_libs: ["libfoo.shared_from_rust"],
7357 prefer_rlib: true,
7358 apex_available: ["myapex"],
7359 }
7360
7361 cc_library_shared {
7362 name: "libfoo.shared_from_rust",
7363 srcs: ["mylib.cpp"],
7364 system_shared_libs: [],
7365 stl: "none",
7366 stubs: {
7367 versions: ["10", "11", "12"],
7368 },
7369 }
7370
Jooyung Han643adc42020-02-27 13:50:06 +09007371 `)
7372
7373 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
7374 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09007375 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007376 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7377 "lib64/mylib.so",
7378 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007379 "lib64/libfoo.rust.so",
7380 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7381 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007382 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007383
7384 // b/220397949
7385 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007386}
7387
Jooyung Han49f67012020-04-17 13:43:10 +09007388func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007389 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007390 apex {
7391 name: "myapex",
7392 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007393 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007394 }
7395 apex_key {
7396 name: "myapex.key",
7397 public_key: "testkey.avbpubkey",
7398 private_key: "testkey.pem",
7399 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007400 `,
7401 android.FixtureModifyConfig(func(config android.Config) {
7402 delete(config.Targets, android.Android)
7403 config.AndroidCommonTarget = android.Target{}
7404 }),
7405 )
Jooyung Han49f67012020-04-17 13:43:10 +09007406
7407 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7408 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7409 }
7410}
7411
Jiyong Parkbd159612020-02-28 15:22:21 +09007412func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007413 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007414 apex {
7415 name: "myapex",
7416 key: "myapex.key",
7417 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007418 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007419 }
7420
7421 apex_key {
7422 name: "myapex.key",
7423 public_key: "testkey.avbpubkey",
7424 private_key: "testkey.pem",
7425 }
7426
7427 android_app {
7428 name: "AppFoo",
7429 srcs: ["foo/bar/MyClass.java"],
7430 sdk_version: "none",
7431 system_modules: "none",
7432 apex_available: [ "myapex" ],
7433 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007434 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007435
Colin Crosscf371cc2020-11-13 11:48:42 -08007436 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007437 content := bundleConfigRule.Args["content"]
7438
7439 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007440 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 +09007441}
7442
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007443func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007444 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007445 apex {
7446 name: "myapex",
7447 key: "myapex.key",
7448 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007449 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007450 }
7451
7452 apex_key {
7453 name: "myapex.key",
7454 public_key: "testkey.avbpubkey",
7455 private_key: "testkey.pem",
7456 }
7457
7458 android_app_set {
7459 name: "AppSet",
7460 set: "AppSet.apks",
7461 }`)
7462 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007463 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007464 content := bundleConfigRule.Args["content"]
7465 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7466 s := mod.Rule("apexRule").Args["copy_commands"]
7467 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007468 if len(copyCmds) != 4 {
7469 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007470 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007471 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7472 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007473 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7474 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007475
7476 // Ensure that canned_fs_config has an entry for the app set zip file
7477 generateFsRule := mod.Rule("generateFsConfig")
7478 cmd := generateFsRule.RuleParams.Command
7479 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007480}
7481
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007482func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007483 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007484 apex_set {
7485 name: "myapex",
7486 filename: "foo_v2.apex",
7487 sanitized: {
7488 none: { set: "myapex.apks", },
7489 hwaddress: { set: "myapex.hwasan.apks", },
7490 },
Paul Duffin24704672021-04-06 16:09:30 +01007491 }
7492 `
7493 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007494
Paul Duffin24704672021-04-06 16:09:30 +01007495 // Check that the extractor produces the correct output file from the correct input file.
7496 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007497
Paul Duffin24704672021-04-06 16:09:30 +01007498 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7499 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007500
Paul Duffin24704672021-04-06 16:09:30 +01007501 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7502
7503 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007504 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7505 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007506
7507 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007508}
7509
Pranav Guptaeba03b02022-09-27 00:27:08 +00007510func TestApexSetApksModuleAssignment(t *testing.T) {
7511 ctx := testApex(t, `
7512 apex_set {
7513 name: "myapex",
7514 set: ":myapex_apks_file",
7515 }
7516
7517 filegroup {
7518 name: "myapex_apks_file",
7519 srcs: ["myapex.apks"],
7520 }
7521 `)
7522
7523 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7524
7525 // Check that the extractor produces the correct apks file from the input module
7526 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.apks"
7527 extractedApex := m.Output(extractorOutput)
7528
7529 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7530}
7531
Paul Duffin89f570a2021-06-16 01:42:33 +01007532func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007533 t.Helper()
7534
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007535 bp := `
7536 java_library {
7537 name: "some-updatable-apex-lib",
7538 srcs: ["a.java"],
7539 sdk_version: "current",
7540 apex_available: [
7541 "some-updatable-apex",
7542 ],
satayevabcd5972021-08-06 17:49:46 +01007543 permitted_packages: ["some.updatable.apex.lib"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007544 }
7545
7546 java_library {
7547 name: "some-non-updatable-apex-lib",
7548 srcs: ["a.java"],
7549 apex_available: [
7550 "some-non-updatable-apex",
7551 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007552 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007553 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007554 }
7555
7556 bootclasspath_fragment {
7557 name: "some-non-updatable-fragment",
7558 contents: ["some-non-updatable-apex-lib"],
7559 apex_available: [
7560 "some-non-updatable-apex",
7561 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007562 hidden_api: {
7563 split_packages: ["*"],
7564 },
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007565 }
7566
7567 java_library {
7568 name: "some-platform-lib",
7569 srcs: ["a.java"],
7570 sdk_version: "current",
7571 installable: true,
7572 }
7573
7574 java_library {
7575 name: "some-art-lib",
7576 srcs: ["a.java"],
7577 sdk_version: "current",
7578 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007579 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007580 ],
7581 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007582 compile_dex: true,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007583 }
7584
7585 apex {
7586 name: "some-updatable-apex",
7587 key: "some-updatable-apex.key",
7588 java_libs: ["some-updatable-apex-lib"],
7589 updatable: true,
7590 min_sdk_version: "current",
7591 }
7592
7593 apex {
7594 name: "some-non-updatable-apex",
7595 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007596 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007597 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007598 }
7599
7600 apex_key {
7601 name: "some-updatable-apex.key",
7602 }
7603
7604 apex_key {
7605 name: "some-non-updatable-apex.key",
7606 }
7607
7608 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007609 name: "com.android.art.debug",
7610 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007611 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007612 updatable: true,
7613 min_sdk_version: "current",
7614 }
7615
Paul Duffinf23bc472021-04-27 12:42:20 +01007616 bootclasspath_fragment {
7617 name: "art-bootclasspath-fragment",
7618 image_name: "art",
7619 contents: ["some-art-lib"],
7620 apex_available: [
7621 "com.android.art.debug",
7622 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007623 hidden_api: {
7624 split_packages: ["*"],
7625 },
Paul Duffinf23bc472021-04-27 12:42:20 +01007626 }
7627
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007628 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007629 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007630 }
7631
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007632 filegroup {
7633 name: "some-updatable-apex-file_contexts",
7634 srcs: [
7635 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7636 ],
7637 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007638
7639 filegroup {
7640 name: "some-non-updatable-apex-file_contexts",
7641 srcs: [
7642 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7643 ],
7644 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007645 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007646
Paul Duffin89f570a2021-06-16 01:42:33 +01007647 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007648}
7649
Paul Duffin89f570a2021-06-16 01:42:33 +01007650func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007651 t.Helper()
7652
Paul Duffin55607122021-03-30 23:32:51 +01007653 fs := android.MockFS{
7654 "a.java": nil,
7655 "a.jar": nil,
7656 "apex_manifest.json": nil,
7657 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007658 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007659 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7660 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7661 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007662 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007663 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007664
Paul Duffin55607122021-03-30 23:32:51 +01007665 errorHandler := android.FixtureExpectsNoErrors
7666 if errmsg != "" {
7667 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007668 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007669
Paul Duffin55607122021-03-30 23:32:51 +01007670 result := android.GroupFixturePreparers(
7671 cc.PrepareForTestWithCcDefaultModules,
7672 java.PrepareForTestWithHiddenApiBuildComponents,
7673 java.PrepareForTestWithJavaDefaultModules,
7674 java.PrepareForTestWithJavaSdkLibraryFiles,
7675 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007676 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007677 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007678 android.FixtureModifyMockFS(func(fs android.MockFS) {
7679 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7680 insert := ""
7681 for _, fragment := range fragments {
7682 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7683 }
7684 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7685 platform_bootclasspath {
7686 name: "platform-bootclasspath",
7687 fragments: [
7688 %s
7689 ],
7690 }
7691 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007692 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007693 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007694 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007695 ).
7696 ExtendWithErrorHandler(errorHandler).
7697 RunTestWithBp(t, bp)
7698
7699 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007700}
7701
Paul Duffin5556c5f2022-06-09 17:32:21 +00007702func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007703 preparers := android.GroupFixturePreparers(
7704 java.PrepareForTestWithJavaDefaultModules,
7705 PrepareForTestWithApexBuildComponents,
7706 ).
7707 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7708 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7709
7710 bpBase := `
7711 apex_set {
7712 name: "com.android.myapex",
7713 installable: true,
7714 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7715 set: "myapex.apks",
7716 }
7717
7718 apex_set {
7719 name: "com.mycompany.android.myapex",
7720 apex_name: "com.android.myapex",
7721 installable: true,
7722 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7723 set: "company-myapex.apks",
7724 }
7725
7726 prebuilt_bootclasspath_fragment {
7727 name: "my-bootclasspath-fragment",
7728 apex_available: ["com.android.myapex"],
7729 %s
7730 }
7731 `
7732
7733 t.Run("java_import", func(t *testing.T) {
7734 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7735 java_import {
7736 name: "libfoo",
7737 jars: ["libfoo.jar"],
7738 apex_available: ["com.android.myapex"],
7739 }
7740 `)
7741 })
7742
7743 t.Run("java_sdk_library_import", func(t *testing.T) {
7744 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7745 java_sdk_library_import {
7746 name: "libfoo",
7747 public: {
7748 jars: ["libbar.jar"],
7749 },
7750 apex_available: ["com.android.myapex"],
7751 }
7752 `)
7753 })
7754
7755 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7756 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7757 image_name: "art",
7758 contents: ["libfoo"],
7759 `)+`
7760 java_sdk_library_import {
7761 name: "libfoo",
7762 public: {
7763 jars: ["libbar.jar"],
7764 },
7765 apex_available: ["com.android.myapex"],
7766 }
7767 `)
7768 })
7769}
7770
Paul Duffin5556c5f2022-06-09 17:32:21 +00007771func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
7772 preparers := android.GroupFixturePreparers(
7773 java.PrepareForTestWithJavaDefaultModules,
7774 PrepareForTestWithApexBuildComponents,
7775 )
7776
7777 bpBase := `
7778 apex_set {
7779 name: "com.android.myapex",
7780 installable: true,
7781 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7782 set: "myapex.apks",
7783 }
7784
7785 apex_set {
7786 name: "com.android.myapex_compressed",
7787 apex_name: "com.android.myapex",
7788 installable: true,
7789 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7790 set: "myapex_compressed.apks",
7791 }
7792
7793 prebuilt_bootclasspath_fragment {
7794 name: "my-bootclasspath-fragment",
7795 apex_available: [
7796 "com.android.myapex",
7797 "com.android.myapex_compressed",
7798 ],
7799 hidden_api: {
7800 annotation_flags: "annotation-flags.csv",
7801 metadata: "metadata.csv",
7802 index: "index.csv",
7803 signature_patterns: "signature_patterns.csv",
7804 },
7805 %s
7806 }
7807 `
7808
7809 t.Run("java_import", func(t *testing.T) {
7810 result := preparers.RunTestWithBp(t,
7811 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7812 java_import {
7813 name: "libfoo",
7814 jars: ["libfoo.jar"],
7815 apex_available: [
7816 "com.android.myapex",
7817 "com.android.myapex_compressed",
7818 ],
7819 }
7820 `)
7821
7822 module := result.Module("libfoo", "android_common_com.android.myapex")
7823 usesLibraryDep := module.(java.UsesLibraryDependency)
7824 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7825 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7826 usesLibraryDep.DexJarBuildPath().Path())
7827 })
7828
7829 t.Run("java_sdk_library_import", func(t *testing.T) {
7830 result := preparers.RunTestWithBp(t,
7831 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7832 java_sdk_library_import {
7833 name: "libfoo",
7834 public: {
7835 jars: ["libbar.jar"],
7836 },
7837 apex_available: [
7838 "com.android.myapex",
7839 "com.android.myapex_compressed",
7840 ],
7841 compile_dex: true,
7842 }
7843 `)
7844
7845 module := result.Module("libfoo", "android_common_com.android.myapex")
7846 usesLibraryDep := module.(java.UsesLibraryDependency)
7847 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7848 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7849 usesLibraryDep.DexJarBuildPath().Path())
7850 })
7851
7852 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7853 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7854 image_name: "art",
7855 contents: ["libfoo"],
7856 `)+`
7857 java_sdk_library_import {
7858 name: "libfoo",
7859 public: {
7860 jars: ["libbar.jar"],
7861 },
7862 apex_available: [
7863 "com.android.myapex",
7864 "com.android.myapex_compressed",
7865 ],
7866 compile_dex: true,
7867 }
7868 `)
7869 })
7870}
7871
Jooyung Han548640b2020-04-27 12:10:30 +09007872func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7873 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7874 apex {
7875 name: "myapex",
7876 key: "myapex.key",
7877 updatable: true,
7878 }
7879
7880 apex_key {
7881 name: "myapex.key",
7882 public_key: "testkey.avbpubkey",
7883 private_key: "testkey.pem",
7884 }
7885 `)
7886}
7887
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007888func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7889 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7890 apex {
7891 name: "myapex",
7892 key: "myapex.key",
7893 }
7894
7895 apex_key {
7896 name: "myapex.key",
7897 public_key: "testkey.avbpubkey",
7898 private_key: "testkey.pem",
7899 }
7900 `)
7901}
7902
Daniel Norman69109112021-12-02 12:52:42 -08007903func TestUpdatable_cannot_be_vendor_apex(t *testing.T) {
7904 testApexError(t, `"myapex" .*: updatable: vendor APEXes are not updatable`, `
7905 apex {
7906 name: "myapex",
7907 key: "myapex.key",
7908 updatable: true,
7909 soc_specific: true,
7910 }
7911
7912 apex_key {
7913 name: "myapex.key",
7914 public_key: "testkey.avbpubkey",
7915 private_key: "testkey.pem",
7916 }
7917 `)
7918}
7919
satayevb98371c2021-06-15 16:49:50 +01007920func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7921 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7922 apex {
7923 name: "myapex",
7924 key: "myapex.key",
7925 systemserverclasspath_fragments: [
7926 "mysystemserverclasspathfragment",
7927 ],
7928 min_sdk_version: "29",
7929 updatable: true,
7930 }
7931
7932 apex_key {
7933 name: "myapex.key",
7934 public_key: "testkey.avbpubkey",
7935 private_key: "testkey.pem",
7936 }
7937
7938 java_library {
7939 name: "foo",
7940 srcs: ["b.java"],
7941 min_sdk_version: "29",
7942 installable: true,
7943 apex_available: [
7944 "myapex",
7945 ],
7946 }
7947
7948 systemserverclasspath_fragment {
7949 name: "mysystemserverclasspathfragment",
7950 generate_classpaths_proto: false,
7951 contents: [
7952 "foo",
7953 ],
7954 apex_available: [
7955 "myapex",
7956 ],
7957 }
satayevabcd5972021-08-06 17:49:46 +01007958 `,
7959 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
7960 )
satayevb98371c2021-06-15 16:49:50 +01007961}
7962
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007963func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01007964 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
7965 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
7966 // modules to be included in the BootJars.
7967 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
7968 return android.GroupFixturePreparers(
7969 dexpreopt.FixtureSetBootJars(bootJars...),
7970 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7971 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7972 }),
7973 )
7974 }
7975
7976 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
7977 // same value. This can result in an invalid configuration as it allows non art apex jars to be
7978 // specified in the ArtApexJars configuration.
7979 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
7980 return android.GroupFixturePreparers(
7981 dexpreopt.FixtureSetArtBootJars(bootJars...),
7982 dexpreopt.FixtureSetBootJars(bootJars...),
7983 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
7984 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
7985 }),
7986 )
7987 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007988
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007989 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01007990 preparer := android.GroupFixturePreparers(
7991 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
7992 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
7993 )
7994 fragments := []java.ApexVariantReference{
7995 {
7996 Apex: proptools.StringPtr("com.android.art.debug"),
7997 Module: proptools.StringPtr("art-bootclasspath-fragment"),
7998 },
7999 {
8000 Apex: proptools.StringPtr("some-non-updatable-apex"),
8001 Module: proptools.StringPtr("some-non-updatable-fragment"),
8002 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008003 }
satayevabcd5972021-08-06 17:49:46 +01008004 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008005 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008006
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008007 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01008008 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
8009 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01008010 preparer := android.GroupFixturePreparers(
8011 prepareSetBootJars("com.android.art.debug:some-art-lib"),
8012 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8013 )
Paul Duffin60264a02021-04-12 20:02:36 +01008014 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008015 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008016
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008017 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 +01008018 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 +01008019 // Update the dexpreopt ArtApexJars directly.
8020 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
8021 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008022 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008023
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008024 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 +01008025 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 +01008026 // Update the dexpreopt ArtApexJars directly.
8027 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
8028 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008029 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008030
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008031 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 +01008032 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 +01008033 preparer := android.GroupFixturePreparers(
8034 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
8035 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8036 )
Paul Duffin60264a02021-04-12 20:02:36 +01008037 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008038 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008039
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008040 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 +01008041 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01008042 fragment := java.ApexVariantReference{
8043 Apex: proptools.StringPtr("some-non-updatable-apex"),
8044 Module: proptools.StringPtr("some-non-updatable-fragment"),
8045 }
8046 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008047 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008048
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008049 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008050 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008051 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8052 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008053 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008054
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008055 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008056 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008057 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8058 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008059 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008060
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008061 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01008062 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01008063 // Update the dexpreopt ArtApexJars directly.
8064 preparer := prepareSetArtJars("platform:some-platform-lib")
8065 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008066 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008067
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008068 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008069 preparer := android.GroupFixturePreparers(
8070 java.FixtureConfigureBootJars("platform:some-platform-lib"),
8071 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8072 )
8073 fragments := []java.ApexVariantReference{
8074 {
8075 Apex: proptools.StringPtr("some-non-updatable-apex"),
8076 Module: proptools.StringPtr("some-non-updatable-fragment"),
8077 },
8078 }
8079 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008080 })
Paul Duffin064b70c2020-11-02 17:32:38 +00008081}
8082
8083func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008084 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008085 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008086 fragment := java.ApexVariantReference{
8087 Apex: proptools.StringPtr("myapex"),
8088 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8089 }
8090
Paul Duffin064b70c2020-11-02 17:32:38 +00008091 testDexpreoptWithApexes(t, `
8092 prebuilt_apex {
8093 name: "myapex" ,
8094 arch: {
8095 arm64: {
8096 src: "myapex-arm64.apex",
8097 },
8098 arm: {
8099 src: "myapex-arm.apex",
8100 },
8101 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008102 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8103 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008104
Paul Duffin89f570a2021-06-16 01:42:33 +01008105 prebuilt_bootclasspath_fragment {
8106 name: "my-bootclasspath-fragment",
8107 contents: ["libfoo"],
8108 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008109 hidden_api: {
8110 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8111 metadata: "my-bootclasspath-fragment/metadata.csv",
8112 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008113 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8114 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8115 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008116 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008117 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008118
Paul Duffin89f570a2021-06-16 01:42:33 +01008119 java_import {
8120 name: "libfoo",
8121 jars: ["libfoo.jar"],
8122 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008123 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008124 }
8125 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008126 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008127}
8128
Spandan Dasf14e2542021-11-12 00:01:37 +00008129func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008130 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008131 bp += `
8132 apex_key {
8133 name: "myapex.key",
8134 public_key: "testkey.avbpubkey",
8135 private_key: "testkey.pem",
8136 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008137 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008138 "lib1/src/A.java": nil,
8139 "lib2/src/B.java": nil,
8140 "system/sepolicy/apex/myapex-file_contexts": nil,
8141 }
8142
Paul Duffin45338f02021-03-30 23:07:52 +01008143 errorHandler := android.FixtureExpectsNoErrors
8144 if errmsg != "" {
8145 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008146 }
Colin Crossae8600b2020-10-29 17:09:13 -07008147
Paul Duffin45338f02021-03-30 23:07:52 +01008148 android.GroupFixturePreparers(
8149 android.PrepareForTestWithAndroidBuildComponents,
8150 java.PrepareForTestWithJavaBuildComponents,
8151 PrepareForTestWithApexBuildComponents,
8152 android.PrepareForTestWithNeverallowRules(rules),
8153 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008154 apexBootJars := make([]string, 0, len(bootJars))
8155 for _, apexBootJar := range bootJars {
8156 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008157 }
satayevd604b212021-07-21 14:23:52 +01008158 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008159 }),
8160 fs.AddToFixture(),
8161 ).
8162 ExtendWithErrorHandler(errorHandler).
8163 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008164}
8165
8166func TestApexPermittedPackagesRules(t *testing.T) {
8167 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008168 name string
8169 expectedError string
8170 bp string
8171 bootJars []string
8172 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008173 }{
8174
8175 {
8176 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8177 expectedError: "",
8178 bp: `
8179 java_library {
8180 name: "bcp_lib1",
8181 srcs: ["lib1/src/*.java"],
8182 permitted_packages: ["foo.bar"],
8183 apex_available: ["myapex"],
8184 sdk_version: "none",
8185 system_modules: "none",
8186 }
8187 java_library {
8188 name: "nonbcp_lib2",
8189 srcs: ["lib2/src/*.java"],
8190 apex_available: ["myapex"],
8191 permitted_packages: ["a.b"],
8192 sdk_version: "none",
8193 system_modules: "none",
8194 }
8195 apex {
8196 name: "myapex",
8197 key: "myapex.key",
8198 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008199 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008200 }`,
8201 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008202 bcpPermittedPackages: map[string][]string{
8203 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008204 "foo.bar",
8205 },
8206 },
8207 },
8208 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008209 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008210 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 +01008211 bp: `
8212 java_library {
8213 name: "bcp_lib1",
8214 srcs: ["lib1/src/*.java"],
8215 apex_available: ["myapex"],
8216 permitted_packages: ["foo.bar"],
8217 sdk_version: "none",
8218 system_modules: "none",
8219 }
8220 java_library {
8221 name: "bcp_lib2",
8222 srcs: ["lib2/src/*.java"],
8223 apex_available: ["myapex"],
8224 permitted_packages: ["foo.bar", "bar.baz"],
8225 sdk_version: "none",
8226 system_modules: "none",
8227 }
8228 apex {
8229 name: "myapex",
8230 key: "myapex.key",
8231 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008232 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008233 }
8234 `,
8235 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008236 bcpPermittedPackages: map[string][]string{
8237 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008238 "foo.bar",
8239 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008240 "bcp_lib2": []string{
8241 "foo.bar",
8242 },
8243 },
8244 },
8245 {
8246 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8247 expectedError: "",
8248 bp: `
8249 java_library {
8250 name: "bcp_lib_restricted",
8251 srcs: ["lib1/src/*.java"],
8252 apex_available: ["myapex"],
8253 permitted_packages: ["foo.bar"],
8254 sdk_version: "none",
8255 min_sdk_version: "29",
8256 system_modules: "none",
8257 }
8258 java_library {
8259 name: "bcp_lib_unrestricted",
8260 srcs: ["lib2/src/*.java"],
8261 apex_available: ["myapex"],
8262 permitted_packages: ["foo.bar", "bar.baz"],
8263 sdk_version: "none",
8264 min_sdk_version: "29",
8265 system_modules: "none",
8266 }
8267 apex {
8268 name: "myapex",
8269 key: "myapex.key",
8270 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8271 updatable: true,
8272 min_sdk_version: "29",
8273 }
8274 `,
8275 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8276 bcpPermittedPackages: map[string][]string{
8277 "bcp_lib1_non_updateable": []string{
8278 "foo.bar",
8279 },
8280 // 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 +01008281 },
8282 },
8283 }
8284 for _, tc := range testcases {
8285 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008286 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8287 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008288 })
8289 }
8290}
8291
Jiyong Park62304bb2020-04-13 16:19:48 +09008292func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008293 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008294 apex {
8295 name: "myapex",
8296 key: "myapex.key",
8297 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008298 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008299 }
8300
8301 apex_key {
8302 name: "myapex.key",
8303 public_key: "testkey.avbpubkey",
8304 private_key: "testkey.pem",
8305 }
8306
8307 cc_library {
8308 name: "mylib",
8309 srcs: ["mylib.cpp"],
8310 system_shared_libs: [],
8311 stl: "none",
8312 stubs: {
8313 versions: ["1"],
8314 },
8315 apex_available: ["myapex"],
8316 }
8317
8318 cc_library {
8319 name: "myprivlib",
8320 srcs: ["mylib.cpp"],
8321 system_shared_libs: [],
8322 stl: "none",
8323 apex_available: ["myapex"],
8324 }
8325
8326
8327 cc_test {
8328 name: "mytest",
8329 gtest: false,
8330 srcs: ["mylib.cpp"],
8331 system_shared_libs: [],
8332 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008333 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008334 test_for: ["myapex"]
8335 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008336
8337 cc_library {
8338 name: "mytestlib",
8339 srcs: ["mylib.cpp"],
8340 system_shared_libs: [],
8341 shared_libs: ["mylib", "myprivlib"],
8342 stl: "none",
8343 test_for: ["myapex"],
8344 }
8345
8346 cc_benchmark {
8347 name: "mybench",
8348 srcs: ["mylib.cpp"],
8349 system_shared_libs: [],
8350 shared_libs: ["mylib", "myprivlib"],
8351 stl: "none",
8352 test_for: ["myapex"],
8353 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008354 `)
8355
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008356 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008357 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008358 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8359 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8360 }
8361
8362 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008363 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008364 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8365 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8366 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8367}
Jiyong Park46a512f2020-12-04 18:02:13 +09008368
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008369func TestIndirectTestFor(t *testing.T) {
8370 ctx := testApex(t, `
8371 apex {
8372 name: "myapex",
8373 key: "myapex.key",
8374 native_shared_libs: ["mylib", "myprivlib"],
8375 updatable: false,
8376 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008377
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008378 apex_key {
8379 name: "myapex.key",
8380 public_key: "testkey.avbpubkey",
8381 private_key: "testkey.pem",
8382 }
8383
8384 cc_library {
8385 name: "mylib",
8386 srcs: ["mylib.cpp"],
8387 system_shared_libs: [],
8388 stl: "none",
8389 stubs: {
8390 versions: ["1"],
8391 },
8392 apex_available: ["myapex"],
8393 }
8394
8395 cc_library {
8396 name: "myprivlib",
8397 srcs: ["mylib.cpp"],
8398 system_shared_libs: [],
8399 stl: "none",
8400 shared_libs: ["mylib"],
8401 apex_available: ["myapex"],
8402 }
8403
8404 cc_library {
8405 name: "mytestlib",
8406 srcs: ["mylib.cpp"],
8407 system_shared_libs: [],
8408 shared_libs: ["myprivlib"],
8409 stl: "none",
8410 test_for: ["myapex"],
8411 }
8412 `)
8413
8414 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008415 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008416 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8417 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8418 }
8419
8420 // The platform variant of mytestlib links to the platform variant of the
8421 // internal myprivlib.
8422 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8423
8424 // The platform variant of myprivlib links to the platform variant of mylib
8425 // and bypasses its stubs.
8426 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 +09008427}
8428
Martin Stjernholmec009002021-03-27 15:18:31 +00008429func TestTestForForLibInOtherApex(t *testing.T) {
8430 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8431 _ = testApex(t, `
8432 apex {
8433 name: "com.android.art",
8434 key: "myapex.key",
8435 native_shared_libs: ["mylib"],
8436 updatable: false,
8437 }
8438
8439 apex {
8440 name: "com.android.art.debug",
8441 key: "myapex.key",
8442 native_shared_libs: ["mylib", "mytestlib"],
8443 updatable: false,
8444 }
8445
8446 apex_key {
8447 name: "myapex.key",
8448 public_key: "testkey.avbpubkey",
8449 private_key: "testkey.pem",
8450 }
8451
8452 cc_library {
8453 name: "mylib",
8454 srcs: ["mylib.cpp"],
8455 system_shared_libs: [],
8456 stl: "none",
8457 stubs: {
8458 versions: ["1"],
8459 },
8460 apex_available: ["com.android.art", "com.android.art.debug"],
8461 }
8462
8463 cc_library {
8464 name: "mytestlib",
8465 srcs: ["mylib.cpp"],
8466 system_shared_libs: [],
8467 shared_libs: ["mylib"],
8468 stl: "none",
8469 apex_available: ["com.android.art.debug"],
8470 test_for: ["com.android.art"],
8471 }
8472 `,
8473 android.MockFS{
8474 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8475 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8476 }.AddToFixture())
8477}
8478
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008479// TODO(jungjw): Move this to proptools
8480func intPtr(i int) *int {
8481 return &i
8482}
8483
8484func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008485 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008486 apex_set {
8487 name: "myapex",
8488 set: "myapex.apks",
8489 filename: "foo_v2.apex",
8490 overrides: ["foo"],
8491 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008492 `,
8493 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8494 variables.Platform_sdk_version = intPtr(30)
8495 }),
8496 android.FixtureModifyConfig(func(config android.Config) {
8497 config.Targets[android.Android] = []android.Target{
8498 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8499 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8500 }
8501 }),
8502 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008503
Paul Duffin24704672021-04-06 16:09:30 +01008504 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008505
8506 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008507 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008508 actual := extractedApex.Args["abis"]
8509 expected := "ARMEABI_V7A,ARM64_V8A"
8510 if actual != expected {
8511 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8512 }
8513 actual = extractedApex.Args["sdk-version"]
8514 expected = "30"
8515 if actual != expected {
8516 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8517 }
8518
Paul Duffin6717d882021-06-15 19:09:41 +01008519 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008520 a := m.Module().(*ApexSet)
8521 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008522 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008523 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8524 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8525 }
8526}
8527
Anton Hansson805e0a52022-11-25 14:06:46 +00008528func TestApexSet_NativeBridge(t *testing.T) {
8529 ctx := testApex(t, `
8530 apex_set {
8531 name: "myapex",
8532 set: "myapex.apks",
8533 filename: "foo_v2.apex",
8534 overrides: ["foo"],
8535 }
8536 `,
8537 android.FixtureModifyConfig(func(config android.Config) {
8538 config.Targets[android.Android] = []android.Target{
8539 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8540 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8541 }
8542 }),
8543 )
8544
8545 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
8546
8547 // Check extract_apks tool parameters. No native bridge arch expected
8548 extractedApex := m.Output("extracted/myapex.apks")
8549 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8550}
8551
Jiyong Park7d95a512020-05-10 15:16:24 +09008552func TestNoStaticLinkingToStubsLib(t *testing.T) {
8553 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8554 apex {
8555 name: "myapex",
8556 key: "myapex.key",
8557 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008558 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008559 }
8560
8561 apex_key {
8562 name: "myapex.key",
8563 public_key: "testkey.avbpubkey",
8564 private_key: "testkey.pem",
8565 }
8566
8567 cc_library {
8568 name: "mylib",
8569 srcs: ["mylib.cpp"],
8570 static_libs: ["otherlib"],
8571 system_shared_libs: [],
8572 stl: "none",
8573 apex_available: [ "myapex" ],
8574 }
8575
8576 cc_library {
8577 name: "otherlib",
8578 srcs: ["mylib.cpp"],
8579 system_shared_libs: [],
8580 stl: "none",
8581 stubs: {
8582 versions: ["1", "2", "3"],
8583 },
8584 apex_available: [ "myapex" ],
8585 }
8586 `)
8587}
8588
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008589func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008590 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008591 apex {
8592 name: "myapex",
8593 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008594 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008595 custom_sign_tool: "sign_myapex",
8596 }
8597
8598 apex_key {
8599 name: "myapex.key",
8600 public_key: "testkey.avbpubkey",
8601 private_key: "testkey.pem",
8602 }
8603 `)
8604
8605 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8606 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8607 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"`)
8608}
8609
8610func TestApexKeysTxtOverrides(t *testing.T) {
8611 ctx := testApex(t, `
8612 apex {
8613 name: "myapex",
8614 key: "myapex.key",
8615 updatable: false,
8616 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008617 }
8618
8619 apex_key {
8620 name: "myapex.key",
8621 public_key: "testkey.avbpubkey",
8622 private_key: "testkey.pem",
8623 }
8624
8625 prebuilt_apex {
8626 name: "myapex",
8627 prefer: true,
8628 arch: {
8629 arm64: {
8630 src: "myapex-arm64.apex",
8631 },
8632 arm: {
8633 src: "myapex-arm.apex",
8634 },
8635 },
8636 }
8637
8638 apex_set {
8639 name: "myapex_set",
8640 set: "myapex.apks",
8641 filename: "myapex_set.apex",
8642 overrides: ["myapex"],
8643 }
8644 `)
8645
8646 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8647 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8648 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 +09008649 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 +09008650}
8651
Jooyung Han938b5932020-06-20 12:47:47 +09008652func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008653 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008654 apex {
8655 name: "myapex",
8656 key: "myapex.key",
8657 apps: ["app"],
8658 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008659 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008660 }
8661
8662 apex_key {
8663 name: "myapex.key",
8664 public_key: "testkey.avbpubkey",
8665 private_key: "testkey.pem",
8666 }
8667
8668 android_app {
8669 name: "app",
8670 srcs: ["foo/bar/MyClass.java"],
8671 package_name: "foo",
8672 sdk_version: "none",
8673 system_modules: "none",
8674 apex_available: [ "myapex" ],
8675 }
8676 `, withFiles(map[string][]byte{
8677 "sub/Android.bp": []byte(`
8678 override_apex {
8679 name: "override_myapex",
8680 base: "myapex",
8681 apps: ["override_app"],
8682 allowed_files: ":allowed",
8683 }
8684 // Overridable "path" property should be referenced indirectly
8685 filegroup {
8686 name: "allowed",
8687 srcs: ["allowed.txt"],
8688 }
8689 override_android_app {
8690 name: "override_app",
8691 base: "app",
8692 package_name: "bar",
8693 }
8694 `),
8695 }))
8696
8697 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8698 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8699 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8700 }
8701
8702 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8703 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8704 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8705 }
8706}
8707
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008708func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008709 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008710 apex {
8711 name: "myapex",
8712 key: "myapex.key",
8713 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008714 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008715 }
8716
8717 apex_key {
8718 name: "myapex.key",
8719 public_key: "testkey.avbpubkey",
8720 private_key: "testkey.pem",
8721 }
8722
8723 cc_library {
8724 name: "mylib",
8725 srcs: ["mylib.cpp"],
8726 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008727 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008728 },
8729 apex_available: ["myapex"],
8730 }
8731
8732 cc_prebuilt_library_shared {
8733 name: "mylib",
8734 prefer: false,
8735 srcs: ["prebuilt.so"],
8736 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008737 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008738 },
8739 apex_available: ["myapex"],
8740 }
8741 `)
8742}
8743
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008744func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008745 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008746 apex {
8747 name: "myapex",
8748 key: "myapex.key",
8749 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008750 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008751 }
8752 apex_key {
8753 name: "myapex.key",
8754 public_key: "testkey.avbpubkey",
8755 private_key: "testkey.pem",
8756 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008757 `,
8758 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8759 variables.CompressedApex = proptools.BoolPtr(true)
8760 }),
8761 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008762
8763 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8764 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8765
8766 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8767 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8768
8769 // Make sure output of bundle is .capex
8770 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8771 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8772
8773 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008774 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008775 var builder strings.Builder
8776 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8777 androidMk := builder.String()
8778 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8779}
8780
Martin Stjernholm2856c662020-12-02 15:03:42 +00008781func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008782 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008783 apex {
8784 name: "myapex",
8785 key: "myapex.key",
8786 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008787 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008788 }
8789
8790 apex_key {
8791 name: "myapex.key",
8792 public_key: "testkey.avbpubkey",
8793 private_key: "testkey.pem",
8794 }
8795
8796 cc_library {
8797 name: "mylib",
8798 srcs: ["mylib.cpp"],
8799 apex_available: ["myapex"],
8800 shared_libs: ["otherlib"],
8801 system_shared_libs: [],
8802 }
8803
8804 cc_library {
8805 name: "otherlib",
8806 srcs: ["mylib.cpp"],
8807 stubs: {
8808 versions: ["current"],
8809 },
8810 }
8811
8812 cc_prebuilt_library_shared {
8813 name: "otherlib",
8814 prefer: true,
8815 srcs: ["prebuilt.so"],
8816 stubs: {
8817 versions: ["current"],
8818 },
8819 }
8820 `)
8821
8822 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008823 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008824 var builder strings.Builder
8825 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8826 androidMk := builder.String()
8827
8828 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8829 // a thing there.
Diwas Sharmabb9202e2023-01-26 18:42:21 +00008830 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 +00008831}
8832
Jiyong Parke3867542020-12-03 17:28:25 +09008833func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008834 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008835 apex {
8836 name: "myapex",
8837 key: "myapex.key",
8838 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008839 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008840 }
8841
8842 apex_key {
8843 name: "myapex.key",
8844 public_key: "testkey.avbpubkey",
8845 private_key: "testkey.pem",
8846 }
8847
8848 cc_library {
8849 name: "mylib",
8850 srcs: ["mylib.cpp"],
8851 system_shared_libs: [],
8852 stl: "none",
8853 apex_available: ["myapex"],
8854 shared_libs: ["mylib2"],
8855 target: {
8856 apex: {
8857 exclude_shared_libs: ["mylib2"],
8858 },
8859 },
8860 }
8861
8862 cc_library {
8863 name: "mylib2",
8864 srcs: ["mylib.cpp"],
8865 system_shared_libs: [],
8866 stl: "none",
8867 }
8868 `)
8869
8870 // Check if mylib is linked to mylib2 for the non-apex target
8871 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8872 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8873
8874 // Make sure that the link doesn't occur for the apex target
8875 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8876 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8877
8878 // It shouldn't appear in the copy cmd as well.
8879 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8880 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8881}
8882
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008883func TestPrebuiltStubLibDep(t *testing.T) {
8884 bpBase := `
8885 apex {
8886 name: "myapex",
8887 key: "myapex.key",
8888 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008889 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008890 }
8891 apex_key {
8892 name: "myapex.key",
8893 public_key: "testkey.avbpubkey",
8894 private_key: "testkey.pem",
8895 }
8896 cc_library {
8897 name: "mylib",
8898 srcs: ["mylib.cpp"],
8899 apex_available: ["myapex"],
8900 shared_libs: ["stublib"],
8901 system_shared_libs: [],
8902 }
8903 apex {
8904 name: "otherapex",
8905 enabled: %s,
8906 key: "myapex.key",
8907 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008908 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008909 }
8910 `
8911
8912 stublibSourceBp := `
8913 cc_library {
8914 name: "stublib",
8915 srcs: ["mylib.cpp"],
8916 apex_available: ["otherapex"],
8917 system_shared_libs: [],
8918 stl: "none",
8919 stubs: {
8920 versions: ["1"],
8921 },
8922 }
8923 `
8924
8925 stublibPrebuiltBp := `
8926 cc_prebuilt_library_shared {
8927 name: "stublib",
8928 srcs: ["prebuilt.so"],
8929 apex_available: ["otherapex"],
8930 stubs: {
8931 versions: ["1"],
8932 },
8933 %s
8934 }
8935 `
8936
8937 tests := []struct {
8938 name string
8939 stublibBp string
8940 usePrebuilt bool
8941 modNames []string // Modules to collect AndroidMkEntries for
8942 otherApexEnabled []string
8943 }{
8944 {
8945 name: "only_source",
8946 stublibBp: stublibSourceBp,
8947 usePrebuilt: false,
8948 modNames: []string{"stublib"},
8949 otherApexEnabled: []string{"true", "false"},
8950 },
8951 {
8952 name: "source_preferred",
8953 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
8954 usePrebuilt: false,
8955 modNames: []string{"stublib", "prebuilt_stublib"},
8956 otherApexEnabled: []string{"true", "false"},
8957 },
8958 {
8959 name: "prebuilt_preferred",
8960 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
8961 usePrebuilt: true,
8962 modNames: []string{"stublib", "prebuilt_stublib"},
8963 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8964 },
8965 {
8966 name: "only_prebuilt",
8967 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
8968 usePrebuilt: true,
8969 modNames: []string{"stublib"},
8970 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
8971 },
8972 }
8973
8974 for _, test := range tests {
8975 t.Run(test.name, func(t *testing.T) {
8976 for _, otherApexEnabled := range test.otherApexEnabled {
8977 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008978 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008979
8980 type modAndMkEntries struct {
8981 mod *cc.Module
8982 mkEntries android.AndroidMkEntries
8983 }
8984 entries := []*modAndMkEntries{}
8985
8986 // Gather shared lib modules that are installable
8987 for _, modName := range test.modNames {
8988 for _, variant := range ctx.ModuleVariantsForTests(modName) {
8989 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
8990 continue
8991 }
8992 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08008993 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008994 continue
8995 }
Colin Crossaa255532020-07-03 13:18:24 -07008996 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008997 if ent.Disabled {
8998 continue
8999 }
9000 entries = append(entries, &modAndMkEntries{
9001 mod: mod,
9002 mkEntries: ent,
9003 })
9004 }
9005 }
9006 }
9007
9008 var entry *modAndMkEntries = nil
9009 for _, ent := range entries {
9010 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9011 if entry != nil {
9012 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9013 } else {
9014 entry = ent
9015 }
9016 }
9017 }
9018
9019 if entry == nil {
9020 t.Errorf("AndroidMk entry for \"stublib\" missing")
9021 } else {
9022 isPrebuilt := entry.mod.Prebuilt() != nil
9023 if isPrebuilt != test.usePrebuilt {
9024 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9025 }
9026 if !entry.mod.IsStubs() {
9027 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9028 }
9029 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9030 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9031 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009032 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009033 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009034 if !android.InList(expected, cflags) {
9035 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9036 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009037 }
9038 })
9039 }
9040 })
9041 }
9042}
9043
Martin Stjernholmdf298b32021-05-21 20:57:29 +01009044func TestHostApexInHostOnlyBuild(t *testing.T) {
9045 testApex(t, `
9046 apex {
9047 name: "myapex",
9048 host_supported: true,
9049 key: "myapex.key",
9050 updatable: false,
9051 payload_type: "zip",
9052 }
9053 apex_key {
9054 name: "myapex.key",
9055 public_key: "testkey.avbpubkey",
9056 private_key: "testkey.pem",
9057 }
9058 `,
9059 android.FixtureModifyConfig(func(config android.Config) {
9060 // We may not have device targets in all builds, e.g. in
9061 // prebuilts/build-tools/build-prebuilts.sh
9062 config.Targets[android.Android] = []android.Target{}
9063 }))
9064}
9065
Colin Crossc33e5212021-05-25 18:16:02 -07009066func TestApexJavaCoverage(t *testing.T) {
9067 bp := `
9068 apex {
9069 name: "myapex",
9070 key: "myapex.key",
9071 java_libs: ["mylib"],
9072 bootclasspath_fragments: ["mybootclasspathfragment"],
9073 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9074 updatable: false,
9075 }
9076
9077 apex_key {
9078 name: "myapex.key",
9079 public_key: "testkey.avbpubkey",
9080 private_key: "testkey.pem",
9081 }
9082
9083 java_library {
9084 name: "mylib",
9085 srcs: ["mylib.java"],
9086 apex_available: ["myapex"],
9087 compile_dex: true,
9088 }
9089
9090 bootclasspath_fragment {
9091 name: "mybootclasspathfragment",
9092 contents: ["mybootclasspathlib"],
9093 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009094 hidden_api: {
9095 split_packages: ["*"],
9096 },
Colin Crossc33e5212021-05-25 18:16:02 -07009097 }
9098
9099 java_library {
9100 name: "mybootclasspathlib",
9101 srcs: ["mybootclasspathlib.java"],
9102 apex_available: ["myapex"],
9103 compile_dex: true,
9104 }
9105
9106 systemserverclasspath_fragment {
9107 name: "mysystemserverclasspathfragment",
9108 contents: ["mysystemserverclasspathlib"],
9109 apex_available: ["myapex"],
9110 }
9111
9112 java_library {
9113 name: "mysystemserverclasspathlib",
9114 srcs: ["mysystemserverclasspathlib.java"],
9115 apex_available: ["myapex"],
9116 compile_dex: true,
9117 }
9118 `
9119
9120 result := android.GroupFixturePreparers(
9121 PrepareForTestWithApexBuildComponents,
9122 prepareForTestWithMyapex,
9123 java.PrepareForTestWithJavaDefaultModules,
9124 android.PrepareForTestWithAndroidBuildComponents,
9125 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009126 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9127 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009128 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009129 ).RunTest(t)
9130
9131 // Make sure jacoco ran on both mylib and mybootclasspathlib
9132 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9133 t.Errorf("Failed to find jacoco rule for mylib")
9134 }
9135 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9136 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9137 }
9138 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9139 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9140 }
9141}
9142
Jiyong Park192600a2021-08-03 07:52:17 +00009143func TestProhibitStaticExecutable(t *testing.T) {
9144 testApexError(t, `executable mybin is static`, `
9145 apex {
9146 name: "myapex",
9147 key: "myapex.key",
9148 binaries: ["mybin"],
9149 min_sdk_version: "29",
9150 }
9151
9152 apex_key {
9153 name: "myapex.key",
9154 public_key: "testkey.avbpubkey",
9155 private_key: "testkey.pem",
9156 }
9157
9158 cc_binary {
9159 name: "mybin",
9160 srcs: ["mylib.cpp"],
9161 relative_install_path: "foo/bar",
9162 static_executable: true,
9163 system_shared_libs: [],
9164 stl: "none",
9165 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009166 min_sdk_version: "29",
9167 }
9168 `)
9169
9170 testApexError(t, `executable mybin.rust is static`, `
9171 apex {
9172 name: "myapex",
9173 key: "myapex.key",
9174 binaries: ["mybin.rust"],
9175 min_sdk_version: "29",
9176 }
9177
9178 apex_key {
9179 name: "myapex.key",
9180 public_key: "testkey.avbpubkey",
9181 private_key: "testkey.pem",
9182 }
9183
9184 rust_binary {
9185 name: "mybin.rust",
9186 srcs: ["foo.rs"],
9187 static_executable: true,
9188 apex_available: ["myapex"],
9189 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009190 }
9191 `)
9192}
9193
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009194func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9195 ctx := testApex(t, `
9196 apex {
9197 name: "myapex",
9198 key: "myapex.key",
9199 updatable: false,
9200 java_libs: ["foo"],
9201 }
9202
9203 apex_key {
9204 name: "myapex.key",
9205 public_key: "testkey.avbpubkey",
9206 private_key: "testkey.pem",
9207 }
9208
9209 java_library {
9210 name: "foo",
9211 srcs: ["foo.java"],
9212 apex_available: ["myapex"],
9213 installable: true,
9214 }
9215 `,
9216 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9217 )
9218
9219 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9220 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9221 var builder strings.Builder
9222 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9223 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009224 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 +00009225}
9226
9227func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9228 ctx := testApex(t, `
9229 prebuilt_apex {
9230 name: "myapex",
9231 arch: {
9232 arm64: {
9233 src: "myapex-arm64.apex",
9234 },
9235 arm: {
9236 src: "myapex-arm.apex",
9237 },
9238 },
9239 exported_java_libs: ["foo"],
9240 }
9241
9242 java_import {
9243 name: "foo",
9244 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009245 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009246 }
9247 `,
9248 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9249 )
9250
9251 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9252 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9253 mainModuleEntries := entriesList[0]
9254 android.AssertArrayString(t,
9255 "LOCAL_REQUIRED_MODULES",
9256 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9257 []string{
9258 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9259 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9260 })
9261}
9262
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009263func TestAndroidMk_RequiredModules(t *testing.T) {
9264 ctx := testApex(t, `
9265 apex {
9266 name: "myapex",
9267 key: "myapex.key",
9268 updatable: false,
9269 java_libs: ["foo"],
9270 required: ["otherapex"],
9271 }
9272
9273 apex {
9274 name: "otherapex",
9275 key: "myapex.key",
9276 updatable: false,
9277 java_libs: ["foo"],
9278 required: ["otherapex"],
9279 }
9280
9281 apex_key {
9282 name: "myapex.key",
9283 public_key: "testkey.avbpubkey",
9284 private_key: "testkey.pem",
9285 }
9286
9287 java_library {
9288 name: "foo",
9289 srcs: ["foo.java"],
9290 apex_available: ["myapex", "otherapex"],
9291 installable: true,
9292 }
9293 `)
9294
9295 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9296 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9297 var builder strings.Builder
9298 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9299 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009300 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009301}
9302
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009303func TestAndroidMk_RequiredDeps(t *testing.T) {
9304 ctx := testApex(t, `
9305 apex {
9306 name: "myapex",
9307 key: "myapex.key",
9308 updatable: false,
9309 }
9310
9311 apex_key {
9312 name: "myapex.key",
9313 public_key: "testkey.avbpubkey",
9314 private_key: "testkey.pem",
9315 }
9316 `)
9317
9318 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009319 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009320 data := android.AndroidMkDataForTest(t, ctx, bundle)
9321 var builder strings.Builder
9322 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9323 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009324 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009325
9326 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009327 flattenedBundle.makeModulesToInstall = append(flattenedBundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009328 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
9329 var flattenedBuilder strings.Builder
9330 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
9331 flattenedAndroidMk := flattenedBuilder.String()
Sasha Smundakdcb61292022-12-08 10:41:33 -08009332 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex.flattened apex_pubkey.myapex.flattened foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009333}
9334
Jooyung Hana6d36672022-02-24 13:58:07 +09009335func TestApexOutputFileProducer(t *testing.T) {
9336 for _, tc := range []struct {
9337 name string
9338 ref string
9339 expected_data []string
9340 }{
9341 {
9342 name: "test_using_output",
9343 ref: ":myapex",
9344 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
9345 },
9346 {
9347 name: "test_using_apex",
9348 ref: ":myapex{.apex}",
9349 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
9350 },
9351 } {
9352 t.Run(tc.name, func(t *testing.T) {
9353 ctx := testApex(t, `
9354 apex {
9355 name: "myapex",
9356 key: "myapex.key",
9357 compressible: true,
9358 updatable: false,
9359 }
9360
9361 apex_key {
9362 name: "myapex.key",
9363 public_key: "testkey.avbpubkey",
9364 private_key: "testkey.pem",
9365 }
9366
9367 java_test {
9368 name: "`+tc.name+`",
9369 srcs: ["a.java"],
9370 data: ["`+tc.ref+`"],
9371 }
9372 `,
9373 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9374 variables.CompressedApex = proptools.BoolPtr(true)
9375 }))
9376 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9377 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9378 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9379 })
9380 }
9381}
9382
satayev758968a2021-12-06 11:42:40 +00009383func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9384 preparer := android.GroupFixturePreparers(
9385 PrepareForTestWithApexBuildComponents,
9386 prepareForTestWithMyapex,
9387 java.PrepareForTestWithJavaSdkLibraryFiles,
9388 java.PrepareForTestWithJavaDefaultModules,
9389 android.PrepareForTestWithAndroidBuildComponents,
9390 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9391 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9392 )
9393
9394 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9395 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9396 preparer.RunTestWithBp(t, `
9397 apex {
9398 name: "myapex",
9399 key: "myapex.key",
9400 bootclasspath_fragments: ["mybootclasspathfragment"],
9401 min_sdk_version: "30",
9402 updatable: false,
9403 }
9404
9405 apex_key {
9406 name: "myapex.key",
9407 public_key: "testkey.avbpubkey",
9408 private_key: "testkey.pem",
9409 }
9410
9411 bootclasspath_fragment {
9412 name: "mybootclasspathfragment",
9413 contents: ["mybootclasspathlib"],
9414 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009415 hidden_api: {
9416 split_packages: ["*"],
9417 },
satayev758968a2021-12-06 11:42:40 +00009418 }
9419
9420 java_sdk_library {
9421 name: "mybootclasspathlib",
9422 srcs: ["mybootclasspathlib.java"],
9423 apex_available: ["myapex"],
9424 compile_dex: true,
9425 unsafe_ignore_missing_latest_api: true,
9426 min_sdk_version: "31",
9427 static_libs: ["util"],
9428 }
9429
9430 java_library {
9431 name: "util",
9432 srcs: ["a.java"],
9433 apex_available: ["myapex"],
9434 min_sdk_version: "31",
9435 static_libs: ["another_util"],
9436 }
9437
9438 java_library {
9439 name: "another_util",
9440 srcs: ["a.java"],
9441 min_sdk_version: "31",
9442 apex_available: ["myapex"],
9443 }
9444 `)
9445 })
9446
9447 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9448 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9449 preparer.RunTestWithBp(t, `
9450 apex {
9451 name: "myapex",
9452 key: "myapex.key",
9453 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9454 min_sdk_version: "30",
9455 updatable: false,
9456 }
9457
9458 apex_key {
9459 name: "myapex.key",
9460 public_key: "testkey.avbpubkey",
9461 private_key: "testkey.pem",
9462 }
9463
9464 systemserverclasspath_fragment {
9465 name: "mysystemserverclasspathfragment",
9466 contents: ["mysystemserverclasspathlib"],
9467 apex_available: ["myapex"],
9468 }
9469
9470 java_sdk_library {
9471 name: "mysystemserverclasspathlib",
9472 srcs: ["mysystemserverclasspathlib.java"],
9473 apex_available: ["myapex"],
9474 compile_dex: true,
9475 min_sdk_version: "32",
9476 unsafe_ignore_missing_latest_api: true,
9477 static_libs: ["util"],
9478 }
9479
9480 java_library {
9481 name: "util",
9482 srcs: ["a.java"],
9483 apex_available: ["myapex"],
9484 min_sdk_version: "31",
9485 static_libs: ["another_util"],
9486 }
9487
9488 java_library {
9489 name: "another_util",
9490 srcs: ["a.java"],
9491 min_sdk_version: "31",
9492 apex_available: ["myapex"],
9493 }
9494 `)
9495 })
9496
9497 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9498 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9499 RunTestWithBp(t, `
9500 apex {
9501 name: "myapex",
9502 key: "myapex.key",
9503 bootclasspath_fragments: ["mybootclasspathfragment"],
9504 min_sdk_version: "30",
9505 updatable: false,
9506 }
9507
9508 apex_key {
9509 name: "myapex.key",
9510 public_key: "testkey.avbpubkey",
9511 private_key: "testkey.pem",
9512 }
9513
9514 bootclasspath_fragment {
9515 name: "mybootclasspathfragment",
9516 contents: ["mybootclasspathlib"],
9517 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009518 hidden_api: {
9519 split_packages: ["*"],
9520 },
satayev758968a2021-12-06 11:42:40 +00009521 }
9522
9523 java_sdk_library {
9524 name: "mybootclasspathlib",
9525 srcs: ["mybootclasspathlib.java"],
9526 apex_available: ["myapex"],
9527 compile_dex: true,
9528 unsafe_ignore_missing_latest_api: true,
9529 }
9530 `)
9531 })
9532
9533 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9534 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9535 RunTestWithBp(t, `
9536 apex {
9537 name: "myapex",
9538 key: "myapex.key",
9539 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9540 min_sdk_version: "30",
9541 updatable: false,
9542 }
9543
9544 apex_key {
9545 name: "myapex.key",
9546 public_key: "testkey.avbpubkey",
9547 private_key: "testkey.pem",
9548 }
9549
9550 systemserverclasspath_fragment {
9551 name: "mysystemserverclasspathfragment",
9552 contents: ["mysystemserverclasspathlib"],
9553 apex_available: ["myapex"],
9554 }
9555
9556 java_sdk_library {
9557 name: "mysystemserverclasspathlib",
9558 srcs: ["mysystemserverclasspathlib.java"],
9559 apex_available: ["myapex"],
9560 compile_dex: true,
9561 unsafe_ignore_missing_latest_api: true,
9562 }
9563 `)
9564 })
9565}
9566
Jiakai Zhang6decef92022-01-12 17:56:19 +00009567// Verifies that the APEX depends on all the Make modules in the list.
9568func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9569 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9570 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009571 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009572 }
9573}
9574
9575// Verifies that the APEX does not depend on any of the Make modules in the list.
9576func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9577 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9578 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009579 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009580 }
9581}
9582
Spandan Das66773252022-01-15 00:23:18 +00009583func TestApexStrictUpdtabilityLint(t *testing.T) {
9584 bpTemplate := `
9585 apex {
9586 name: "myapex",
9587 key: "myapex.key",
9588 java_libs: ["myjavalib"],
9589 updatable: %v,
9590 min_sdk_version: "29",
9591 }
9592 apex_key {
9593 name: "myapex.key",
9594 }
9595 java_library {
9596 name: "myjavalib",
9597 srcs: ["MyClass.java"],
9598 apex_available: [ "myapex" ],
9599 lint: {
9600 strict_updatability_linting: %v,
9601 },
9602 sdk_version: "current",
9603 min_sdk_version: "29",
9604 }
9605 `
9606 fs := android.MockFS{
9607 "lint-baseline.xml": nil,
9608 }
9609
9610 testCases := []struct {
9611 testCaseName string
9612 apexUpdatable bool
9613 javaStrictUpdtabilityLint bool
9614 lintFileExists bool
9615 disallowedFlagExpected bool
9616 }{
9617 {
9618 testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9619 apexUpdatable: true,
9620 javaStrictUpdtabilityLint: true,
9621 lintFileExists: false,
9622 disallowedFlagExpected: false,
9623 },
9624 {
9625 testCaseName: "non-updatable apex respects strict_updatability of javalib",
9626 apexUpdatable: false,
9627 javaStrictUpdtabilityLint: false,
9628 lintFileExists: true,
9629 disallowedFlagExpected: false,
9630 },
9631 {
9632 testCaseName: "non-updatable apex respects strict updatability of javalib",
9633 apexUpdatable: false,
9634 javaStrictUpdtabilityLint: true,
9635 lintFileExists: true,
9636 disallowedFlagExpected: true,
9637 },
9638 {
9639 testCaseName: "updatable apex sets strict updatability of javalib to true",
9640 apexUpdatable: true,
9641 javaStrictUpdtabilityLint: false, // will be set to true by mutator
9642 lintFileExists: true,
9643 disallowedFlagExpected: true,
9644 },
9645 }
9646
9647 for _, testCase := range testCases {
9648 bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9649 fixtures := []android.FixturePreparer{}
9650 if testCase.lintFileExists {
9651 fixtures = append(fixtures, fs.AddToFixture())
9652 }
9653
9654 result := testApex(t, bp, fixtures...)
9655 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9656 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9657 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9658
9659 if disallowedFlagActual != testCase.disallowedFlagExpected {
9660 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9661 }
9662 }
9663}
9664
Spandan Dasd9c23ab2022-02-10 02:34:13 +00009665func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9666 bp := `
9667 apex {
9668 name: "myapex",
9669 key: "myapex.key",
9670 java_libs: ["myjavalib"],
9671 updatable: true,
9672 min_sdk_version: "29",
9673 }
9674 apex_key {
9675 name: "myapex.key",
9676 }
9677 java_library {
9678 name: "myjavalib",
9679 srcs: ["MyClass.java"],
9680 apex_available: [ "myapex" ],
9681 sdk_version: "current",
9682 min_sdk_version: "29",
9683 }
9684 `
9685
9686 testCases := []struct {
9687 testCaseName string
9688 moduleDirectory string
9689 disallowedFlagExpected bool
9690 }{
9691 {
9692 testCaseName: "lintable module defined outside libcore",
9693 moduleDirectory: "",
9694 disallowedFlagExpected: true,
9695 },
9696 {
9697 testCaseName: "lintable module defined in libcore root directory",
9698 moduleDirectory: "libcore/",
9699 disallowedFlagExpected: false,
9700 },
9701 {
9702 testCaseName: "lintable module defined in libcore child directory",
9703 moduleDirectory: "libcore/childdir/",
9704 disallowedFlagExpected: true,
9705 },
9706 }
9707
9708 for _, testCase := range testCases {
9709 lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9710 bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9711 result := testApex(t, "", lintFileCreator, bpFileCreator)
9712 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9713 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9714 cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9715 disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9716
9717 if disallowedFlagActual != testCase.disallowedFlagExpected {
9718 t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9719 }
9720 }
9721}
9722
Spandan Das66773252022-01-15 00:23:18 +00009723// checks transtive deps of an apex coming from bootclasspath_fragment
9724func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9725 bp := `
9726 apex {
9727 name: "myapex",
9728 key: "myapex.key",
9729 bootclasspath_fragments: ["mybootclasspathfragment"],
9730 updatable: true,
9731 min_sdk_version: "29",
9732 }
9733 apex_key {
9734 name: "myapex.key",
9735 }
9736 bootclasspath_fragment {
9737 name: "mybootclasspathfragment",
9738 contents: ["myjavalib"],
9739 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009740 hidden_api: {
9741 split_packages: ["*"],
9742 },
Spandan Das66773252022-01-15 00:23:18 +00009743 }
9744 java_library {
9745 name: "myjavalib",
9746 srcs: ["MyClass.java"],
9747 apex_available: [ "myapex" ],
9748 sdk_version: "current",
9749 min_sdk_version: "29",
9750 compile_dex: true,
9751 }
9752 `
9753 fs := android.MockFS{
9754 "lint-baseline.xml": nil,
9755 }
9756
9757 result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9758 myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9759 sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9760 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9761 t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9762 }
9763}
9764
Spandan Das42e89502022-05-06 22:12:55 +00009765// updatable apexes should propagate updatable=true to its apps
9766func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9767 bp := `
9768 apex {
9769 name: "myapex",
9770 key: "myapex.key",
9771 updatable: %v,
9772 apps: [
9773 "myapp",
9774 ],
9775 min_sdk_version: "30",
9776 }
9777 apex_key {
9778 name: "myapex.key",
9779 }
9780 android_app {
9781 name: "myapp",
9782 updatable: %v,
9783 apex_available: [
9784 "myapex",
9785 ],
9786 sdk_version: "current",
9787 min_sdk_version: "30",
9788 }
9789 `
9790 testCases := []struct {
9791 name string
9792 apex_is_updatable_bp bool
9793 app_is_updatable_bp bool
9794 app_is_updatable_expected bool
9795 }{
9796 {
9797 name: "Non-updatable apex respects updatable property of non-updatable app",
9798 apex_is_updatable_bp: false,
9799 app_is_updatable_bp: false,
9800 app_is_updatable_expected: false,
9801 },
9802 {
9803 name: "Non-updatable apex respects updatable property of updatable app",
9804 apex_is_updatable_bp: false,
9805 app_is_updatable_bp: true,
9806 app_is_updatable_expected: true,
9807 },
9808 {
9809 name: "Updatable apex respects updatable property of updatable app",
9810 apex_is_updatable_bp: true,
9811 app_is_updatable_bp: true,
9812 app_is_updatable_expected: true,
9813 },
9814 {
9815 name: "Updatable apex sets updatable=true on non-updatable app",
9816 apex_is_updatable_bp: true,
9817 app_is_updatable_bp: false,
9818 app_is_updatable_expected: true,
9819 },
9820 }
9821 for _, testCase := range testCases {
9822 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
9823 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
9824 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
9825 }
9826}
9827
Kiyoung Kim487689e2022-07-26 09:48:22 +09009828func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
9829 bp := `
9830 apex {
9831 name: "myapex",
9832 key: "myapex.key",
9833 native_shared_libs: ["libfoo"],
9834 min_sdk_version: "29",
9835 }
9836 apex_key {
9837 name: "myapex.key",
9838 }
9839 cc_library {
9840 name: "libfoo",
9841 shared_libs: ["libc"],
9842 apex_available: ["myapex"],
9843 min_sdk_version: "29",
9844 }
9845 cc_api_library {
9846 name: "libc",
9847 src: "libc.so",
9848 min_sdk_version: "29",
9849 recovery_available: true,
9850 }
9851 api_imports {
9852 name: "api_imports",
9853 shared_libs: [
9854 "libc",
9855 ],
9856 header_libs: [],
9857 }
9858 `
9859 result := testApex(t, bp)
9860
9861 hasDep := func(m android.Module, wantDep android.Module) bool {
9862 t.Helper()
9863 var found bool
9864 result.VisitDirectDeps(m, func(dep blueprint.Module) {
9865 if dep == wantDep {
9866 found = true
9867 }
9868 })
9869 return found
9870 }
9871
9872 libfooApexVariant := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex29").Module()
9873 libcApexVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared_apex29").Module()
9874
9875 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(libfooApexVariant, libcApexVariant))
9876
9877 // libfoo core variant should be buildable in the same inner tree since
9878 // certain mcombo files might build system and apexes in the same inner tree
9879 // libfoo core variant should link against source libc
9880 libfooCoreVariant := result.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
9881 libcCoreVariant := result.ModuleForTests("libc.apiimport", "android_arm64_armv8-a_shared").Module()
9882 android.AssertBoolEquals(t, "core variant should link against source libc", true, hasDep(libfooCoreVariant, libcCoreVariant))
9883}
Dennis Shend4f5d932023-01-31 20:27:21 +00009884
9885func TestTrimmedApex(t *testing.T) {
9886 bp := `
9887 apex {
9888 name: "myapex",
9889 key: "myapex.key",
9890 native_shared_libs: ["libfoo","libbaz"],
9891 min_sdk_version: "29",
9892 trim_against: "mydcla",
9893 }
9894 apex {
9895 name: "mydcla",
9896 key: "myapex.key",
9897 native_shared_libs: ["libfoo","libbar"],
9898 min_sdk_version: "29",
9899 file_contexts: ":myapex-file_contexts",
9900 dynamic_common_lib_apex: true,
9901 }
9902 apex_key {
9903 name: "myapex.key",
9904 }
9905 cc_library {
9906 name: "libfoo",
9907 shared_libs: ["libc"],
9908 apex_available: ["myapex","mydcla"],
9909 min_sdk_version: "29",
9910 }
9911 cc_library {
9912 name: "libbar",
9913 shared_libs: ["libc"],
9914 apex_available: ["myapex","mydcla"],
9915 min_sdk_version: "29",
9916 }
9917 cc_library {
9918 name: "libbaz",
9919 shared_libs: ["libc"],
9920 apex_available: ["myapex","mydcla"],
9921 min_sdk_version: "29",
9922 }
9923 cc_api_library {
9924 name: "libc",
9925 src: "libc.so",
9926 min_sdk_version: "29",
9927 recovery_available: true,
9928 }
9929 api_imports {
9930 name: "api_imports",
9931 shared_libs: [
9932 "libc",
9933 ],
9934 header_libs: [],
9935 }
9936 `
9937 ctx := testApex(t, bp)
9938 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
9939 apexRule := module.MaybeRule("apexRule")
9940 if apexRule.Rule == nil {
9941 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
9942 }
9943
9944 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
9945 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("TrimmedApexRule")
9946 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
9947 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
9948 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
9949 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
9950}