blob: 924f92344d34c6104bee89a16dd46d41d8c6a180 [file] [log] [blame]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +090018 "fmt"
Jooyung Han39edb6c2019-11-06 16:53:07 +090019 "path"
Paul Duffin37856732021-02-26 14:24:15 +000020 "path/filepath"
Jaewoong Jung22f7d182019-07-16 18:25:41 -070021 "reflect"
Paul Duffin9b879592020-05-26 13:21:35 +010022 "regexp"
Jooyung Han31c470b2019-10-18 16:26:59 +090023 "sort"
Jiyong Parkd4a3a132021-03-17 20:21:35 +090024 "strconv"
Jiyong Park25fc6a92018-11-18 18:02:45 +090025 "strings"
26 "testing"
Jiyong Parkda6eb592018-12-19 17:12:36 +090027
Kiyoung Kim487689e2022-07-26 09:48:22 +090028 "github.com/google/blueprint"
Jiyong Parkda6eb592018-12-19 17:12:36 +090029 "github.com/google/blueprint/proptools"
30
31 "android/soong/android"
markchien2f59ec92020-09-02 16:23:38 +080032 "android/soong/bpf"
Jiyong Parkda6eb592018-12-19 17:12:36 +090033 "android/soong/cc"
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000034 "android/soong/dexpreopt"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035 prebuilt_etc "android/soong/etc"
Jiyong Parkb2742fd2019-02-11 11:38:15 +090036 "android/soong/java"
Jiyong Park99644e92020-11-17 22:21:02 +090037 "android/soong/rust"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 "android/soong/sh"
Jiyong Park25fc6a92018-11-18 18:02:45 +090039)
40
Jooyung Hand3639552019-08-09 12:57:43 +090041// names returns name list from white space separated string
42func names(s string) (ns []string) {
43 for _, n := range strings.Split(s, " ") {
44 if len(n) > 0 {
45 ns = append(ns, n)
46 }
47 }
48 return
49}
50
Paul Duffin40b62572021-03-20 11:39:01 +000051func testApexError(t *testing.T, pattern, bp string, preparers ...android.FixturePreparer) {
Jooyung Han344d5432019-08-23 11:17:39 +090052 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010053 android.GroupFixturePreparers(
54 prepareForApexTest,
55 android.GroupFixturePreparers(preparers...),
56 ).
Paul Duffine05480a2021-03-08 15:07:14 +000057 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
Paul Duffin40b62572021-03-20 11:39:01 +000058 RunTestWithBp(t, bp)
Jooyung Han5c998b92019-06-27 11:30:33 +090059}
60
Paul Duffin40b62572021-03-20 11:39:01 +000061func testApex(t *testing.T, bp string, preparers ...android.FixturePreparer) *android.TestContext {
Jooyung Han344d5432019-08-23 11:17:39 +090062 t.Helper()
Paul Duffin284165a2021-03-29 01:50:31 +010063
64 optionalBpPreparer := android.NullFixturePreparer
Paul Duffin40b62572021-03-20 11:39:01 +000065 if bp != "" {
Paul Duffin284165a2021-03-29 01:50:31 +010066 optionalBpPreparer = android.FixtureWithRootAndroidBp(bp)
Paul Duffin40b62572021-03-20 11:39:01 +000067 }
Paul Duffin284165a2021-03-29 01:50:31 +010068
69 result := android.GroupFixturePreparers(
70 prepareForApexTest,
71 android.GroupFixturePreparers(preparers...),
72 optionalBpPreparer,
73 ).RunTest(t)
74
Paul Duffine05480a2021-03-08 15:07:14 +000075 return result.TestContext
Jooyung Han5c998b92019-06-27 11:30:33 +090076}
77
Paul Duffin810f33d2021-03-09 14:12:32 +000078func withFiles(files android.MockFS) android.FixturePreparer {
79 return files.AddToFixture()
Jooyung Han344d5432019-08-23 11:17:39 +090080}
81
Paul Duffin810f33d2021-03-09 14:12:32 +000082func withTargets(targets map[android.OsType][]android.Target) android.FixturePreparer {
83 return android.FixtureModifyConfig(func(config android.Config) {
Jooyung Han344d5432019-08-23 11:17:39 +090084 for k, v := range targets {
85 config.Targets[k] = v
86 }
Paul Duffin810f33d2021-03-09 14:12:32 +000087 })
Jooyung Han344d5432019-08-23 11:17:39 +090088}
89
Jooyung Han35155c42020-02-06 17:33:20 +090090// withNativeBridgeTargets sets configuration with targets including:
91// - X86_64 (primary)
92// - X86 (secondary)
93// - Arm64 on X86_64 (native bridge)
94// - Arm on X86 (native bridge)
Paul Duffin810f33d2021-03-09 14:12:32 +000095var withNativeBridgeEnabled = android.FixtureModifyConfig(
96 func(config android.Config) {
97 config.Targets[android.Android] = []android.Target{
98 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}},
99 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
100 {Os: android.Android, Arch: android.Arch{ArchType: android.X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}},
101 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
102 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}},
103 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86_64", NativeBridgeRelativePath: "arm64"},
104 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
105 NativeBridge: android.NativeBridgeEnabled, NativeBridgeHostArchName: "x86", NativeBridgeRelativePath: "arm"},
106 }
107 },
108)
109
110func withManifestPackageNameOverrides(specs []string) android.FixturePreparer {
111 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
112 variables.ManifestPackageNameOverrides = specs
113 })
Jooyung Han35155c42020-02-06 17:33:20 +0900114}
115
Albert Martineefabcf2022-03-21 20:11:16 +0000116func withApexGlobalMinSdkVersionOverride(minSdkOverride *string) android.FixturePreparer {
117 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
118 variables.ApexGlobalMinSdkVersionOverride = minSdkOverride
119 })
120}
121
Paul Duffin810f33d2021-03-09 14:12:32 +0000122var withBinder32bit = android.FixtureModifyProductVariables(
123 func(variables android.FixtureProductVariables) {
124 variables.Binder32bit = proptools.BoolPtr(true)
125 },
126)
Jiyong Parkcfaa1642020-02-28 16:51:07 +0900127
Paul Duffin810f33d2021-03-09 14:12:32 +0000128var withUnbundledBuild = android.FixtureModifyProductVariables(
129 func(variables android.FixtureProductVariables) {
130 variables.Unbundled_build = proptools.BoolPtr(true)
131 },
132)
Jiyong Park7cd10e32020-01-14 09:22:18 +0900133
Paul Duffin284165a2021-03-29 01:50:31 +0100134// Legacy preparer used for running tests within the apex package.
135//
136// This includes everything that was needed to run any test in the apex package prior to the
137// introduction of the test fixtures. Tests that are being converted to use fixtures directly
138// rather than through the testApex...() methods should avoid using this and instead use the
139// various preparers directly, using android.GroupFixturePreparers(...) to group them when
140// necessary.
141//
142// deprecated
143var prepareForApexTest = android.GroupFixturePreparers(
Paul Duffin37aad602021-03-08 09:47:16 +0000144 // General preparers in alphabetical order as test infrastructure will enforce correct
145 // registration order.
146 android.PrepareForTestWithAndroidBuildComponents,
147 bpf.PrepareForTestWithBpf,
148 cc.PrepareForTestWithCcBuildComponents,
149 java.PrepareForTestWithJavaDefaultModules,
150 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
151 rust.PrepareForTestWithRustDefaultModules,
152 sh.PrepareForTestWithShBuildComponents,
153
154 PrepareForTestWithApexBuildComponents,
155
156 // Additional apex test specific preparers.
157 android.FixtureAddTextFile("system/sepolicy/Android.bp", `
158 filegroup {
159 name: "myapex-file_contexts",
160 srcs: [
161 "apex/myapex-file_contexts",
162 ],
163 }
164 `),
Paul Duffin52bfaa42021-03-23 23:40:12 +0000165 prepareForTestWithMyapex,
Paul Duffin37aad602021-03-08 09:47:16 +0000166 android.FixtureMergeMockFs(android.MockFS{
Paul Duffin52bfaa42021-03-23 23:40:12 +0000167 "a.java": nil,
168 "PrebuiltAppFoo.apk": nil,
169 "PrebuiltAppFooPriv.apk": nil,
170 "apex_manifest.json": nil,
171 "AndroidManifest.xml": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000172 "system/sepolicy/apex/myapex.updatable-file_contexts": nil,
173 "system/sepolicy/apex/myapex2-file_contexts": nil,
174 "system/sepolicy/apex/otherapex-file_contexts": nil,
175 "system/sepolicy/apex/com.android.vndk-file_contexts": nil,
176 "system/sepolicy/apex/com.android.vndk.current-file_contexts": nil,
Colin Crossabc0dab2022-04-07 17:39:21 -0700177 "mylib.cpp": nil,
178 "mytest.cpp": nil,
179 "mytest1.cpp": nil,
180 "mytest2.cpp": nil,
181 "mytest3.cpp": nil,
182 "myprebuilt": nil,
183 "my_include": nil,
184 "foo/bar/MyClass.java": nil,
185 "prebuilt.jar": nil,
186 "prebuilt.so": nil,
187 "vendor/foo/devkeys/test.x509.pem": nil,
188 "vendor/foo/devkeys/test.pk8": nil,
189 "testkey.x509.pem": nil,
190 "testkey.pk8": nil,
191 "testkey.override.x509.pem": nil,
192 "testkey.override.pk8": nil,
193 "vendor/foo/devkeys/testkey.avbpubkey": nil,
194 "vendor/foo/devkeys/testkey.pem": nil,
195 "NOTICE": nil,
196 "custom_notice": nil,
197 "custom_notice_for_static_lib": nil,
198 "testkey2.avbpubkey": nil,
199 "testkey2.pem": nil,
200 "myapex-arm64.apex": nil,
201 "myapex-arm.apex": nil,
202 "myapex.apks": nil,
203 "frameworks/base/api/current.txt": nil,
204 "framework/aidl/a.aidl": nil,
205 "dummy.txt": nil,
206 "baz": nil,
207 "bar/baz": nil,
208 "testdata/baz": nil,
209 "AppSet.apks": nil,
210 "foo.rs": nil,
211 "libfoo.jar": nil,
212 "libbar.jar": nil,
Paul Duffin37aad602021-03-08 09:47:16 +0000213 },
214 ),
215
216 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
217 variables.DeviceVndkVersion = proptools.StringPtr("current")
218 variables.DefaultAppCertificate = proptools.StringPtr("vendor/foo/devkeys/test")
219 variables.CertificateOverrides = []string{"myapex_keytest:myapex.certificate.override"}
220 variables.Platform_sdk_codename = proptools.StringPtr("Q")
221 variables.Platform_sdk_final = proptools.BoolPtr(false)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000222 // "Tiramisu" needs to be in the next line for compatibility with soong code,
223 // not because of these tests specifically (it's not used by the tests)
224 variables.Platform_version_active_codenames = []string{"Q", "Tiramisu"}
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900225 variables.Platform_vndk_version = proptools.StringPtr("29")
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000226 variables.BuildId = proptools.StringPtr("TEST.BUILD_ID")
Paul Duffin37aad602021-03-08 09:47:16 +0000227 }),
228)
229
Paul Duffin52bfaa42021-03-23 23:40:12 +0000230var prepareForTestWithMyapex = android.FixtureMergeMockFs(android.MockFS{
231 "system/sepolicy/apex/myapex-file_contexts": nil,
232})
233
Jooyung Han643adc42020-02-27 13:50:06 +0900234// ensure that 'result' equals 'expected'
235func ensureEquals(t *testing.T, result string, expected string) {
236 t.Helper()
237 if result != expected {
238 t.Errorf("%q != %q", expected, result)
239 }
240}
241
Jiyong Park25fc6a92018-11-18 18:02:45 +0900242// ensure that 'result' contains 'expected'
243func ensureContains(t *testing.T, result string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900244 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900245 if !strings.Contains(result, expected) {
246 t.Errorf("%q is not found in %q", expected, result)
247 }
248}
249
Liz Kammer5bd365f2020-05-27 15:15:11 -0700250// ensure that 'result' contains 'expected' exactly one time
251func ensureContainsOnce(t *testing.T, result string, expected string) {
252 t.Helper()
253 count := strings.Count(result, expected)
254 if count != 1 {
255 t.Errorf("%q is found %d times (expected 1 time) in %q", expected, count, result)
256 }
257}
258
Jiyong Park25fc6a92018-11-18 18:02:45 +0900259// ensures that 'result' does not contain 'notExpected'
260func ensureNotContains(t *testing.T, result string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900261 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900262 if strings.Contains(result, notExpected) {
263 t.Errorf("%q is found in %q", notExpected, result)
264 }
265}
266
Sasha Smundak18d98bc2020-05-27 16:36:07 -0700267func ensureMatches(t *testing.T, result string, expectedRex string) {
268 ok, err := regexp.MatchString(expectedRex, result)
269 if err != nil {
270 t.Fatalf("regexp failure trying to match %s against `%s` expression: %s", result, expectedRex, err)
271 return
272 }
273 if !ok {
274 t.Errorf("%s does not match regular expession %s", result, expectedRex)
275 }
276}
277
Jiyong Park25fc6a92018-11-18 18:02:45 +0900278func ensureListContains(t *testing.T, result []string, expected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900279 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900280 if !android.InList(expected, result) {
281 t.Errorf("%q is not found in %v", expected, result)
282 }
283}
284
285func ensureListNotContains(t *testing.T, result []string, notExpected string) {
Jooyung Han5c998b92019-06-27 11:30:33 +0900286 t.Helper()
Jiyong Park25fc6a92018-11-18 18:02:45 +0900287 if android.InList(notExpected, result) {
288 t.Errorf("%q is found in %v", notExpected, result)
289 }
290}
291
Jooyung Hane1633032019-08-01 17:41:43 +0900292func ensureListEmpty(t *testing.T, result []string) {
293 t.Helper()
294 if len(result) > 0 {
295 t.Errorf("%q is expected to be empty", result)
296 }
297}
298
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +0000299func ensureListNotEmpty(t *testing.T, result []string) {
300 t.Helper()
301 if len(result) == 0 {
302 t.Errorf("%q is expected to be not empty", result)
303 }
304}
305
Jiyong Park25fc6a92018-11-18 18:02:45 +0900306// Minimal test
307func TestBasicApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800308 ctx := testApex(t, `
Jiyong Park30ca9372019-02-07 16:27:23 +0900309 apex_defaults {
310 name: "myapex-defaults",
Jiyong Park809bb722019-02-13 21:33:49 +0900311 manifest: ":myapex.manifest",
312 androidManifest: ":myapex.androidmanifest",
Jiyong Park25fc6a92018-11-18 18:02:45 +0900313 key: "myapex.key",
Jiyong Park99644e92020-11-17 22:21:02 +0900314 binaries: ["foo.rust"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900315 native_shared_libs: [
316 "mylib",
317 "libfoo.ffi",
318 ],
Jiyong Park99644e92020-11-17 22:21:02 +0900319 rust_dyn_libs: ["libfoo.dylib.rust"],
Alex Light3d673592019-01-18 14:37:31 -0800320 multilib: {
321 both: {
Jiyong Park99644e92020-11-17 22:21:02 +0900322 binaries: ["foo"],
Alex Light3d673592019-01-18 14:37:31 -0800323 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900324 },
Jiyong Park77acec62020-06-01 21:39:15 +0900325 java_libs: [
326 "myjar",
327 "myjar_dex",
328 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000329 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900330 }
331
Jiyong Park30ca9372019-02-07 16:27:23 +0900332 apex {
333 name: "myapex",
334 defaults: ["myapex-defaults"],
335 }
336
Jiyong Park25fc6a92018-11-18 18:02:45 +0900337 apex_key {
338 name: "myapex.key",
339 public_key: "testkey.avbpubkey",
340 private_key: "testkey.pem",
341 }
342
Jiyong Park809bb722019-02-13 21:33:49 +0900343 filegroup {
344 name: "myapex.manifest",
345 srcs: ["apex_manifest.json"],
346 }
347
348 filegroup {
349 name: "myapex.androidmanifest",
350 srcs: ["AndroidManifest.xml"],
351 }
352
Jiyong Park25fc6a92018-11-18 18:02:45 +0900353 cc_library {
354 name: "mylib",
355 srcs: ["mylib.cpp"],
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900356 shared_libs: [
357 "mylib2",
358 "libbar.ffi",
359 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900360 system_shared_libs: [],
361 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000362 // TODO: remove //apex_available:platform
363 apex_available: [
364 "//apex_available:platform",
365 "myapex",
366 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900367 }
368
Alex Light3d673592019-01-18 14:37:31 -0800369 cc_binary {
370 name: "foo",
371 srcs: ["mylib.cpp"],
372 compile_multilib: "both",
373 multilib: {
374 lib32: {
375 suffix: "32",
376 },
377 lib64: {
378 suffix: "64",
379 },
380 },
381 symlinks: ["foo_link_"],
382 symlink_preferred_arch: true,
383 system_shared_libs: [],
Alex Light3d673592019-01-18 14:37:31 -0800384 stl: "none",
Yifan Hongd22a84a2020-07-28 17:37:46 -0700385 apex_available: [ "myapex", "com.android.gki.*" ],
386 }
387
Jiyong Park99644e92020-11-17 22:21:02 +0900388 rust_binary {
Artur Satayev533b98c2021-03-11 18:03:42 +0000389 name: "foo.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900390 srcs: ["foo.rs"],
391 rlibs: ["libfoo.rlib.rust"],
392 dylibs: ["libfoo.dylib.rust"],
393 apex_available: ["myapex"],
394 }
395
396 rust_library_rlib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000397 name: "libfoo.rlib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900398 srcs: ["foo.rs"],
399 crate_name: "foo",
400 apex_available: ["myapex"],
Jiyong Park94e22fd2021-04-08 18:19:15 +0900401 shared_libs: ["libfoo.shared_from_rust"],
402 }
403
404 cc_library_shared {
405 name: "libfoo.shared_from_rust",
406 srcs: ["mylib.cpp"],
407 system_shared_libs: [],
408 stl: "none",
409 apex_available: ["myapex"],
Jiyong Park99644e92020-11-17 22:21:02 +0900410 }
411
412 rust_library_dylib {
Artur Satayev533b98c2021-03-11 18:03:42 +0000413 name: "libfoo.dylib.rust",
Jiyong Park99644e92020-11-17 22:21:02 +0900414 srcs: ["foo.rs"],
415 crate_name: "foo",
416 apex_available: ["myapex"],
417 }
418
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900419 rust_ffi_shared {
420 name: "libfoo.ffi",
421 srcs: ["foo.rs"],
422 crate_name: "foo",
423 apex_available: ["myapex"],
424 }
425
426 rust_ffi_shared {
427 name: "libbar.ffi",
428 srcs: ["foo.rs"],
429 crate_name: "bar",
430 apex_available: ["myapex"],
431 }
432
Yifan Hongd22a84a2020-07-28 17:37:46 -0700433 apex {
434 name: "com.android.gki.fake",
435 binaries: ["foo"],
436 key: "myapex.key",
437 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000438 updatable: false,
Alex Light3d673592019-01-18 14:37:31 -0800439 }
440
Paul Duffindddd5462020-04-07 15:25:44 +0100441 cc_library_shared {
Jiyong Park25fc6a92018-11-18 18:02:45 +0900442 name: "mylib2",
443 srcs: ["mylib.cpp"],
444 system_shared_libs: [],
445 stl: "none",
Jiyong Park9918e1a2020-03-17 19:16:40 +0900446 static_libs: ["libstatic"],
447 // TODO: remove //apex_available:platform
448 apex_available: [
449 "//apex_available:platform",
450 "myapex",
451 ],
452 }
453
Paul Duffindddd5462020-04-07 15:25:44 +0100454 cc_prebuilt_library_shared {
455 name: "mylib2",
456 srcs: ["prebuilt.so"],
457 // TODO: remove //apex_available:platform
458 apex_available: [
459 "//apex_available:platform",
460 "myapex",
461 ],
462 }
463
Jiyong Park9918e1a2020-03-17 19:16:40 +0900464 cc_library_static {
465 name: "libstatic",
466 srcs: ["mylib.cpp"],
467 system_shared_libs: [],
468 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000469 // TODO: remove //apex_available:platform
470 apex_available: [
471 "//apex_available:platform",
472 "myapex",
473 ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900474 }
Jiyong Park7f7766d2019-07-25 22:02:35 +0900475
476 java_library {
477 name: "myjar",
478 srcs: ["foo/bar/MyClass.java"],
Jiyong Parka62aa232020-05-28 23:46:55 +0900479 stem: "myjar_stem",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900480 sdk_version: "none",
481 system_modules: "none",
Jiyong Park7f7766d2019-07-25 22:02:35 +0900482 static_libs: ["myotherjar"],
Jiyong Park3ff16992019-12-27 14:11:47 +0900483 libs: ["mysharedjar"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000484 // TODO: remove //apex_available:platform
485 apex_available: [
486 "//apex_available:platform",
487 "myapex",
488 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900489 }
490
Jiyong Park77acec62020-06-01 21:39:15 +0900491 dex_import {
492 name: "myjar_dex",
493 jars: ["prebuilt.jar"],
494 apex_available: [
495 "//apex_available:platform",
496 "myapex",
497 ],
498 }
499
Jiyong Park7f7766d2019-07-25 22:02:35 +0900500 java_library {
501 name: "myotherjar",
502 srcs: ["foo/bar/MyClass.java"],
503 sdk_version: "none",
504 system_modules: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +0900505 // TODO: remove //apex_available:platform
506 apex_available: [
507 "//apex_available:platform",
508 "myapex",
509 ],
Jiyong Park7f7766d2019-07-25 22:02:35 +0900510 }
Jiyong Park3ff16992019-12-27 14:11:47 +0900511
512 java_library {
513 name: "mysharedjar",
514 srcs: ["foo/bar/MyClass.java"],
515 sdk_version: "none",
516 system_modules: "none",
Jiyong Park3ff16992019-12-27 14:11:47 +0900517 }
Jiyong Park25fc6a92018-11-18 18:02:45 +0900518 `)
519
Paul Duffina71a67a2021-03-29 00:42:57 +0100520 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900521
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900522 // Make sure that Android.mk is created
523 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -0700524 data := android.AndroidMkDataForTest(t, ctx, ab)
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900525 var builder strings.Builder
526 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
527
528 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +0000529 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Jiyong Park9e83f0b2020-06-11 00:35:03 +0900530 ensureNotContains(t, androidMk, "LOCAL_MODULE := mylib.com.android.myapex\n")
531
Jiyong Park42cca6c2019-04-01 11:15:50 +0900532 optFlags := apexRule.Args["opt_flags"]
533 ensureContains(t, optFlags, "--pubkey vendor/foo/devkeys/testkey.avbpubkey")
Jaewoong Jung14f5ff62019-06-18 13:09:13 -0700534 // Ensure that the NOTICE output is being packaged as an asset.
Paul Duffin37ba3442021-03-29 00:21:08 +0100535 ensureContains(t, optFlags, "--assets_dir out/soong/.intermediates/myapex/android_common_myapex_image/NOTICE")
Jiyong Park42cca6c2019-04-01 11:15:50 +0900536
Jiyong Park25fc6a92018-11-18 18:02:45 +0900537 copyCmds := apexRule.Args["copy_commands"]
538
539 // Ensure that main rule creates an output
540 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
541
542 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700543 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
544 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common_apex10000")
545 ensureListContains(t, ctx.ModuleVariantsForTests("myjar_dex"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900546 ensureListContains(t, ctx.ModuleVariantsForTests("foo.rust"), "android_arm64_armv8-a_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900547 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900548
549 // Ensure that apex variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700550 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
551 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common_apex10000")
Jiyong Park99644e92020-11-17 22:21:02 +0900552 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.rlib.rust"), "android_arm64_armv8-a_rlib_dylib-std_apex10000")
553 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.dylib.rust"), "android_arm64_armv8-a_dylib_apex10000")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900554 ensureListContains(t, ctx.ModuleVariantsForTests("libbar.ffi"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900555 ensureListContains(t, ctx.ModuleVariantsForTests("libfoo.shared_from_rust"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900556
557 // Ensure that both direct and indirect deps are copied into apex
Alex Light5098a612018-11-29 17:12:15 -0800558 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
559 ensureContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Parka62aa232020-05-28 23:46:55 +0900560 ensureContains(t, copyCmds, "image.apex/javalib/myjar_stem.jar")
Jiyong Park77acec62020-06-01 21:39:15 +0900561 ensureContains(t, copyCmds, "image.apex/javalib/myjar_dex.jar")
Jiyong Park99644e92020-11-17 22:21:02 +0900562 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.dylib.rust.dylib.so")
Jiyong Parkf2cc1b72020-12-09 00:20:45 +0900563 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.ffi.so")
564 ensureContains(t, copyCmds, "image.apex/lib64/libbar.ffi.so")
Jiyong Park94e22fd2021-04-08 18:19:15 +0900565 ensureContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900566 // .. but not for java libs
567 ensureNotContains(t, copyCmds, "image.apex/javalib/myotherjar.jar")
Jiyong Park3ff16992019-12-27 14:11:47 +0900568 ensureNotContains(t, copyCmds, "image.apex/javalib/msharedjar.jar")
Logan Chien3aeedc92018-12-26 15:32:21 +0800569
Colin Cross7113d202019-11-20 16:39:12 -0800570 // Ensure that the platform variant ends with _shared or _common
571 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
572 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Jiyong Park7f7766d2019-07-25 22:02:35 +0900573 ensureListContains(t, ctx.ModuleVariantsForTests("myjar"), "android_common")
574 ensureListContains(t, ctx.ModuleVariantsForTests("myotherjar"), "android_common")
Jiyong Park3ff16992019-12-27 14:11:47 +0900575 ensureListContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common")
576
577 // Ensure that dynamic dependency to java libs are not included
578 ensureListNotContains(t, ctx.ModuleVariantsForTests("mysharedjar"), "android_common_myapex")
Alex Light3d673592019-01-18 14:37:31 -0800579
580 // Ensure that all symlinks are present.
581 found_foo_link_64 := false
582 found_foo := false
583 for _, cmd := range strings.Split(copyCmds, " && ") {
Jiyong Park7cd10e32020-01-14 09:22:18 +0900584 if strings.HasPrefix(cmd, "ln -sfn foo64") {
Alex Light3d673592019-01-18 14:37:31 -0800585 if strings.HasSuffix(cmd, "bin/foo") {
586 found_foo = true
587 } else if strings.HasSuffix(cmd, "bin/foo_link_64") {
588 found_foo_link_64 = true
589 }
590 }
591 }
592 good := found_foo && found_foo_link_64
593 if !good {
594 t.Errorf("Could not find all expected symlinks! foo: %t, foo_link_64: %t. Command was %s", found_foo, found_foo_link_64, copyCmds)
595 }
Jiyong Park52818fc2019-03-18 12:01:38 +0900596
Artur Satayeva8bd1132020-04-27 18:07:06 +0100597 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100598 ensureListContains(t, fullDepsInfo, " myjar(minSdkVersion:(no version)) <- myapex")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100599 ensureListContains(t, fullDepsInfo, " mylib2(minSdkVersion:(no version)) <- mylib")
600 ensureListContains(t, fullDepsInfo, " myotherjar(minSdkVersion:(no version)) <- myjar")
601 ensureListContains(t, fullDepsInfo, " mysharedjar(minSdkVersion:(no version)) (external) <- myjar")
Artur Satayeva8bd1132020-04-27 18:07:06 +0100602
603 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100604 ensureListContains(t, flatDepsInfo, "myjar(minSdkVersion:(no version))")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +0100605 ensureListContains(t, flatDepsInfo, "mylib2(minSdkVersion:(no version))")
606 ensureListContains(t, flatDepsInfo, "myotherjar(minSdkVersion:(no version))")
607 ensureListContains(t, flatDepsInfo, "mysharedjar(minSdkVersion:(no version)) (external)")
Alex Light5098a612018-11-29 17:12:15 -0800608}
609
Jooyung Hanf21c7972019-12-16 22:32:06 +0900610func TestDefaults(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800611 ctx := testApex(t, `
Jooyung Hanf21c7972019-12-16 22:32:06 +0900612 apex_defaults {
613 name: "myapex-defaults",
614 key: "myapex.key",
615 prebuilts: ["myetc"],
616 native_shared_libs: ["mylib"],
617 java_libs: ["myjar"],
618 apps: ["AppFoo"],
Jiyong Park69aeba92020-04-24 21:16:36 +0900619 rros: ["rro"],
Ken Chen5372a242022-07-07 17:48:06 +0800620 bpfs: ["bpf", "netdTest"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000621 updatable: false,
Jooyung Hanf21c7972019-12-16 22:32:06 +0900622 }
623
624 prebuilt_etc {
625 name: "myetc",
626 src: "myprebuilt",
627 }
628
629 apex {
630 name: "myapex",
631 defaults: ["myapex-defaults"],
632 }
633
634 apex_key {
635 name: "myapex.key",
636 public_key: "testkey.avbpubkey",
637 private_key: "testkey.pem",
638 }
639
640 cc_library {
641 name: "mylib",
642 system_shared_libs: [],
643 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000644 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900645 }
646
647 java_library {
648 name: "myjar",
649 srcs: ["foo/bar/MyClass.java"],
650 sdk_version: "none",
651 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000652 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900653 }
654
655 android_app {
656 name: "AppFoo",
657 srcs: ["foo/bar/MyClass.java"],
658 sdk_version: "none",
659 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000660 apex_available: [ "myapex" ],
Jooyung Hanf21c7972019-12-16 22:32:06 +0900661 }
Jiyong Park69aeba92020-04-24 21:16:36 +0900662
663 runtime_resource_overlay {
664 name: "rro",
665 theme: "blue",
666 }
667
markchien2f59ec92020-09-02 16:23:38 +0800668 bpf {
669 name: "bpf",
670 srcs: ["bpf.c", "bpf2.c"],
671 }
672
Ken Chenfad7f9d2021-11-10 22:02:57 +0800673 bpf {
Ken Chen5372a242022-07-07 17:48:06 +0800674 name: "netdTest",
675 srcs: ["netdTest.c"],
Ken Chenfad7f9d2021-11-10 22:02:57 +0800676 sub_dir: "netd",
677 }
678
Jooyung Hanf21c7972019-12-16 22:32:06 +0900679 `)
Jooyung Hana57af4a2020-01-23 05:36:59 +0000680 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Hanf21c7972019-12-16 22:32:06 +0900681 "etc/myetc",
682 "javalib/myjar.jar",
683 "lib64/mylib.so",
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +0000684 "app/AppFoo@TEST.BUILD_ID/AppFoo.apk",
Jiyong Park69aeba92020-04-24 21:16:36 +0900685 "overlay/blue/rro.apk",
markchien2f59ec92020-09-02 16:23:38 +0800686 "etc/bpf/bpf.o",
687 "etc/bpf/bpf2.o",
Ken Chen5372a242022-07-07 17:48:06 +0800688 "etc/bpf/netd/netdTest.o",
Jooyung Hanf21c7972019-12-16 22:32:06 +0900689 })
690}
691
Jooyung Han01a3ee22019-11-02 02:52:25 +0900692func TestApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800693 ctx := testApex(t, `
Jooyung Han01a3ee22019-11-02 02:52:25 +0900694 apex {
695 name: "myapex",
696 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000697 updatable: false,
Jooyung Han01a3ee22019-11-02 02:52:25 +0900698 }
699
700 apex_key {
701 name: "myapex.key",
702 public_key: "testkey.avbpubkey",
703 private_key: "testkey.pem",
704 }
705 `)
706
707 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han214bf372019-11-12 13:03:50 +0900708 args := module.Rule("apexRule").Args
709 if manifest := args["manifest"]; manifest != module.Output("apex_manifest.pb").Output.String() {
710 t.Error("manifest should be apex_manifest.pb, but " + manifest)
711 }
Jooyung Han01a3ee22019-11-02 02:52:25 +0900712}
713
Liz Kammer4854a7d2021-05-27 14:28:27 -0400714func TestApexManifestMinSdkVersion(t *testing.T) {
715 ctx := testApex(t, `
716 apex_defaults {
717 name: "my_defaults",
718 key: "myapex.key",
719 product_specific: true,
720 file_contexts: ":my-file-contexts",
721 updatable: false,
722 }
723 apex {
724 name: "myapex_30",
725 min_sdk_version: "30",
726 defaults: ["my_defaults"],
727 }
728
729 apex {
730 name: "myapex_current",
731 min_sdk_version: "current",
732 defaults: ["my_defaults"],
733 }
734
735 apex {
736 name: "myapex_none",
737 defaults: ["my_defaults"],
738 }
739
740 apex_key {
741 name: "myapex.key",
742 public_key: "testkey.avbpubkey",
743 private_key: "testkey.pem",
744 }
745
746 filegroup {
747 name: "my-file-contexts",
748 srcs: ["product_specific_file_contexts"],
749 }
750 `, withFiles(map[string][]byte{
751 "product_specific_file_contexts": nil,
752 }), android.FixtureModifyProductVariables(
753 func(variables android.FixtureProductVariables) {
754 variables.Unbundled_build = proptools.BoolPtr(true)
755 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(false)
756 }), android.FixtureMergeEnv(map[string]string{
757 "UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT": "true",
758 }))
759
760 testCases := []struct {
761 module string
762 minSdkVersion string
763 }{
764 {
765 module: "myapex_30",
766 minSdkVersion: "30",
767 },
768 {
769 module: "myapex_current",
770 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
771 },
772 {
773 module: "myapex_none",
774 minSdkVersion: "Q.$$(cat out/soong/api_fingerprint.txt)",
775 },
776 }
777 for _, tc := range testCases {
778 module := ctx.ModuleForTests(tc.module, "android_common_"+tc.module+"_image")
779 args := module.Rule("apexRule").Args
780 optFlags := args["opt_flags"]
781 if !strings.Contains(optFlags, "--min_sdk_version "+tc.minSdkVersion) {
782 t.Errorf("%s: Expected min_sdk_version=%s, got: %s", tc.module, tc.minSdkVersion, optFlags)
783 }
784 }
785}
786
Jooyung Hanaf730952023-02-28 14:13:38 +0900787func TestFileContexts(t *testing.T) {
788 for _, useFileContextsAsIs := range []bool{true, false} {
789 prop := ""
790 if useFileContextsAsIs {
791 prop = "use_file_contexts_as_is: true,\n"
792 }
793 ctx := testApex(t, `
794 apex {
795 name: "myapex",
796 key: "myapex.key",
797 file_contexts: "file_contexts",
798 updatable: false,
799 vendor: true,
800 `+prop+`
801 }
802
803 apex_key {
804 name: "myapex.key",
805 public_key: "testkey.avbpubkey",
806 private_key: "testkey.pem",
807 }
808 `, withFiles(map[string][]byte{
809 "file_contexts": nil,
810 }))
811
812 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("file_contexts")
813 forceLabellingCommand := "apex_manifest\\\\.pb u:object_r:system_file:s0"
814 if useFileContextsAsIs {
815 android.AssertStringDoesNotContain(t, "should force-label",
816 rule.RuleParams.Command, forceLabellingCommand)
817 } else {
818 android.AssertStringDoesContain(t, "shouldn't force-label",
819 rule.RuleParams.Command, forceLabellingCommand)
820 }
821 }
822}
823
Alex Light5098a612018-11-29 17:12:15 -0800824func TestBasicZipApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800825 ctx := testApex(t, `
Alex Light5098a612018-11-29 17:12:15 -0800826 apex {
827 name: "myapex",
828 key: "myapex.key",
829 payload_type: "zip",
830 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000831 updatable: false,
Alex Light5098a612018-11-29 17:12:15 -0800832 }
833
834 apex_key {
835 name: "myapex.key",
836 public_key: "testkey.avbpubkey",
837 private_key: "testkey.pem",
838 }
839
840 cc_library {
841 name: "mylib",
842 srcs: ["mylib.cpp"],
843 shared_libs: ["mylib2"],
844 system_shared_libs: [],
845 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000846 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800847 }
848
849 cc_library {
850 name: "mylib2",
851 srcs: ["mylib.cpp"],
852 system_shared_libs: [],
853 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000854 apex_available: [ "myapex" ],
Alex Light5098a612018-11-29 17:12:15 -0800855 }
856 `)
857
Sundong Ahnabb64432019-10-22 13:58:29 +0900858 zipApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_zip").Rule("zipApexRule")
Alex Light5098a612018-11-29 17:12:15 -0800859 copyCmds := zipApexRule.Args["copy_commands"]
860
861 // Ensure that main rule creates an output
862 ensureContains(t, zipApexRule.Output.String(), "myapex.zipapex.unsigned")
863
864 // Ensure that APEX variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -0700865 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800866
867 // Ensure that APEX variant is created for the indirect dep
Colin Crossaede88c2020-08-11 12:17:01 -0700868 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light5098a612018-11-29 17:12:15 -0800869
870 // Ensure that both direct and indirect deps are copied into apex
871 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib.so")
872 ensureContains(t, copyCmds, "image.zipapex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900873}
874
875func TestApexWithStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -0800876 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +0900877 apex {
878 name: "myapex",
879 key: "myapex.key",
880 native_shared_libs: ["mylib", "mylib3"],
Jiyong Park105dc322021-06-11 17:22:09 +0900881 binaries: ["foo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +0000882 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +0900883 }
884
885 apex_key {
886 name: "myapex.key",
887 public_key: "testkey.avbpubkey",
888 private_key: "testkey.pem",
889 }
890
891 cc_library {
892 name: "mylib",
893 srcs: ["mylib.cpp"],
894 shared_libs: ["mylib2", "mylib3"],
895 system_shared_libs: [],
896 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000897 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900898 }
899
900 cc_library {
901 name: "mylib2",
902 srcs: ["mylib.cpp"],
Jiyong Park64379952018-12-13 18:37:29 +0900903 cflags: ["-include mylib.h"],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900904 system_shared_libs: [],
905 stl: "none",
906 stubs: {
907 versions: ["1", "2", "3"],
908 },
909 }
910
911 cc_library {
912 name: "mylib3",
Jiyong Park28d395a2018-12-07 22:42:47 +0900913 srcs: ["mylib.cpp"],
914 shared_libs: ["mylib4"],
915 system_shared_libs: [],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900916 stl: "none",
917 stubs: {
918 versions: ["10", "11", "12"],
919 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000920 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +0900921 }
Jiyong Park28d395a2018-12-07 22:42:47 +0900922
923 cc_library {
924 name: "mylib4",
925 srcs: ["mylib.cpp"],
926 system_shared_libs: [],
927 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +0000928 apex_available: [ "myapex" ],
Jiyong Park28d395a2018-12-07 22:42:47 +0900929 }
Jiyong Park105dc322021-06-11 17:22:09 +0900930
931 rust_binary {
932 name: "foo.rust",
933 srcs: ["foo.rs"],
934 shared_libs: ["libfoo.shared_from_rust"],
935 prefer_rlib: true,
936 apex_available: ["myapex"],
937 }
938
939 cc_library_shared {
940 name: "libfoo.shared_from_rust",
941 srcs: ["mylib.cpp"],
942 system_shared_libs: [],
943 stl: "none",
944 stubs: {
945 versions: ["10", "11", "12"],
946 },
947 }
948
Jiyong Park25fc6a92018-11-18 18:02:45 +0900949 `)
950
Sundong Ahnabb64432019-10-22 13:58:29 +0900951 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900952 copyCmds := apexRule.Args["copy_commands"]
953
954 // Ensure that direct non-stubs dep is always included
Alex Light5098a612018-11-29 17:12:15 -0800955 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900956
957 // Ensure that indirect stubs dep is not included
Alex Light5098a612018-11-29 17:12:15 -0800958 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900959
960 // Ensure that direct stubs dep is included
Alex Light5098a612018-11-29 17:12:15 -0800961 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900962
Colin Crossaede88c2020-08-11 12:17:01 -0700963 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +0900964
965 // Ensure that mylib is linking with the latest version of stubs for mylib2
Jiyong Parkd4a3a132021-03-17 20:21:35 +0900966 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900967 // ... and not linking to the non-stub (impl) variant of mylib2
Jiyong Park3ff16992019-12-27 14:11:47 +0900968 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900969
970 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
Colin Crossaede88c2020-08-11 12:17:01 -0700971 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex10000/mylib3.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +0900972 // .. and not linking to the stubs variant of mylib3
Colin Crossaede88c2020-08-11 12:17:01 -0700973 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_12/mylib3.so")
Jiyong Park64379952018-12-13 18:37:29 +0900974
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700975 // Comment out this test. Now it fails after the optimization of sharing "cflags" in cc/cc.go
976 // is replaced by sharing of "cFlags" in cc/builder.go.
977 // The "cflags" contains "-include mylib.h", but cFlags contained only a reference to the
978 // module variable representing "cflags". So it was not detected by ensureNotContains.
979 // Now "cFlags" is a reference to a module variable like $flags1, which includes all previous
980 // content of "cflags". ModuleForTests...Args["cFlags"] returns the full string of $flags1,
981 // including the original cflags's "-include mylib.h".
982 //
Jiyong Park64379952018-12-13 18:37:29 +0900983 // Ensure that stubs libs are built without -include flags
Chih-Hung Hsiehb8082292021-09-09 23:20:39 -0700984 // mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
985 // ensureNotContains(t, mylib2Cflags, "-include ")
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900986
Jiyong Park85cc35a2022-07-17 11:30:47 +0900987 // Ensure that genstub for platform-provided lib is invoked with --systemapi
988 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_3").Rule("genStubSrc").Args["flags"], "--systemapi")
989 // Ensure that genstub for apex-provided lib is invoked with --apex
990 ensureContains(t, ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_shared_12").Rule("genStubSrc").Args["flags"], "--apex")
Jooyung Han671f1ce2019-12-17 12:47:13 +0900991
Jooyung Hana57af4a2020-01-23 05:36:59 +0000992 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han671f1ce2019-12-17 12:47:13 +0900993 "lib64/mylib.so",
994 "lib64/mylib3.so",
995 "lib64/mylib4.so",
Jiyong Park105dc322021-06-11 17:22:09 +0900996 "bin/foo.rust",
997 "lib64/libc++.so", // by the implicit dependency from foo.rust
998 "lib64/liblog.so", // by the implicit dependency from foo.rust
Jooyung Han671f1ce2019-12-17 12:47:13 +0900999 })
Jiyong Park105dc322021-06-11 17:22:09 +09001000
1001 // Ensure that stub dependency from a rust module is not included
1002 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1003 // The rust module is linked to the stub cc library
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001004 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustLink").Args["linkFlags"]
Jiyong Park105dc322021-06-11 17:22:09 +09001005 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1006 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
Jiyong Park34d5c332022-02-24 18:02:44 +09001007
1008 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1009 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001010}
1011
Jiyong Park1bc84122021-06-22 20:23:05 +09001012func TestApexCanUsePrivateApis(t *testing.T) {
1013 ctx := testApex(t, `
1014 apex {
1015 name: "myapex",
1016 key: "myapex.key",
1017 native_shared_libs: ["mylib"],
1018 binaries: ["foo.rust"],
1019 updatable: false,
1020 platform_apis: true,
1021 }
1022
1023 apex_key {
1024 name: "myapex.key",
1025 public_key: "testkey.avbpubkey",
1026 private_key: "testkey.pem",
1027 }
1028
1029 cc_library {
1030 name: "mylib",
1031 srcs: ["mylib.cpp"],
1032 shared_libs: ["mylib2"],
1033 system_shared_libs: [],
1034 stl: "none",
1035 apex_available: [ "myapex" ],
1036 }
1037
1038 cc_library {
1039 name: "mylib2",
1040 srcs: ["mylib.cpp"],
1041 cflags: ["-include mylib.h"],
1042 system_shared_libs: [],
1043 stl: "none",
1044 stubs: {
1045 versions: ["1", "2", "3"],
1046 },
1047 }
1048
1049 rust_binary {
1050 name: "foo.rust",
1051 srcs: ["foo.rs"],
1052 shared_libs: ["libfoo.shared_from_rust"],
1053 prefer_rlib: true,
1054 apex_available: ["myapex"],
1055 }
1056
1057 cc_library_shared {
1058 name: "libfoo.shared_from_rust",
1059 srcs: ["mylib.cpp"],
1060 system_shared_libs: [],
1061 stl: "none",
1062 stubs: {
1063 versions: ["10", "11", "12"],
1064 },
1065 }
1066 `)
1067
1068 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1069 copyCmds := apexRule.Args["copy_commands"]
1070
1071 // Ensure that indirect stubs dep is not included
1072 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1073 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.shared_from_rust.so")
1074
1075 // Ensure that we are using non-stub variants of mylib2 and libfoo.shared_from_rust (because
1076 // of the platform_apis: true)
Jiyong Parkd4a00632022-04-12 12:23:20 +09001077 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001078 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
1079 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001080 rustDeps := ctx.ModuleForTests("foo.rust", "android_arm64_armv8-a_apex10000").Rule("rustLink").Args["linkFlags"]
Jiyong Park1bc84122021-06-22 20:23:05 +09001081 ensureNotContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared_current/libfoo.shared_from_rust.so")
1082 ensureContains(t, rustDeps, "libfoo.shared_from_rust/android_arm64_armv8-a_shared/libfoo.shared_from_rust.so")
1083}
1084
Colin Cross7812fd32020-09-25 12:35:10 -07001085func TestApexWithStubsWithMinSdkVersion(t *testing.T) {
1086 t.Parallel()
Colin Cross1c460562021-02-16 17:55:47 -08001087 ctx := testApex(t, `
Colin Cross7812fd32020-09-25 12:35:10 -07001088 apex {
1089 name: "myapex",
1090 key: "myapex.key",
1091 native_shared_libs: ["mylib", "mylib3"],
1092 min_sdk_version: "29",
1093 }
1094
1095 apex_key {
1096 name: "myapex.key",
1097 public_key: "testkey.avbpubkey",
1098 private_key: "testkey.pem",
1099 }
1100
1101 cc_library {
1102 name: "mylib",
1103 srcs: ["mylib.cpp"],
1104 shared_libs: ["mylib2", "mylib3"],
1105 system_shared_libs: [],
1106 stl: "none",
1107 apex_available: [ "myapex" ],
1108 min_sdk_version: "28",
1109 }
1110
1111 cc_library {
1112 name: "mylib2",
1113 srcs: ["mylib.cpp"],
1114 cflags: ["-include mylib.h"],
1115 system_shared_libs: [],
1116 stl: "none",
1117 stubs: {
1118 versions: ["28", "29", "30", "current"],
1119 },
1120 min_sdk_version: "28",
1121 }
1122
1123 cc_library {
1124 name: "mylib3",
1125 srcs: ["mylib.cpp"],
1126 shared_libs: ["mylib4"],
1127 system_shared_libs: [],
1128 stl: "none",
1129 stubs: {
1130 versions: ["28", "29", "30", "current"],
1131 },
1132 apex_available: [ "myapex" ],
1133 min_sdk_version: "28",
1134 }
1135
1136 cc_library {
1137 name: "mylib4",
1138 srcs: ["mylib.cpp"],
1139 system_shared_libs: [],
1140 stl: "none",
1141 apex_available: [ "myapex" ],
1142 min_sdk_version: "28",
1143 }
1144 `)
1145
1146 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
1147 copyCmds := apexRule.Args["copy_commands"]
1148
1149 // Ensure that direct non-stubs dep is always included
1150 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1151
1152 // Ensure that indirect stubs dep is not included
1153 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
1154
1155 // Ensure that direct stubs dep is included
1156 ensureContains(t, copyCmds, "image.apex/lib64/mylib3.so")
1157
1158 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex29").Rule("ld").Args["libFlags"]
1159
Jiyong Park55549df2021-02-26 23:57:23 +09001160 // Ensure that mylib is linking with the latest version of stub for mylib2
1161 ensureContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared_current/mylib2.so")
Colin Cross7812fd32020-09-25 12:35:10 -07001162 // ... and not linking to the non-stub (impl) variant of mylib2
1163 ensureNotContains(t, mylibLdFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
1164
1165 // Ensure that mylib is linking with the non-stub (impl) of mylib3 (because mylib3 is in the same apex)
1166 ensureContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_apex29/mylib3.so")
1167 // .. and not linking to the stubs variant of mylib3
1168 ensureNotContains(t, mylibLdFlags, "mylib3/android_arm64_armv8-a_shared_29/mylib3.so")
1169
1170 // Ensure that stubs libs are built without -include flags
Colin Crossa717db72020-10-23 14:53:06 -07001171 mylib2Cflags := ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("cc").Args["cFlags"]
Colin Cross7812fd32020-09-25 12:35:10 -07001172 ensureNotContains(t, mylib2Cflags, "-include ")
1173
Jiyong Park85cc35a2022-07-17 11:30:47 +09001174 // Ensure that genstub is invoked with --systemapi
1175 ensureContains(t, ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_shared_29").Rule("genStubSrc").Args["flags"], "--systemapi")
Colin Cross7812fd32020-09-25 12:35:10 -07001176
1177 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1178 "lib64/mylib.so",
1179 "lib64/mylib3.so",
1180 "lib64/mylib4.so",
1181 })
1182}
1183
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001184func TestApex_PlatformUsesLatestStubFromApex(t *testing.T) {
1185 t.Parallel()
1186 // myapex (Z)
1187 // mylib -----------------.
1188 // |
1189 // otherapex (29) |
1190 // libstub's versions: 29 Z current
1191 // |
1192 // <platform> |
1193 // libplatform ----------------'
Colin Cross1c460562021-02-16 17:55:47 -08001194 ctx := testApex(t, `
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001195 apex {
1196 name: "myapex",
1197 key: "myapex.key",
1198 native_shared_libs: ["mylib"],
1199 min_sdk_version: "Z", // non-final
1200 }
1201
1202 cc_library {
1203 name: "mylib",
1204 srcs: ["mylib.cpp"],
1205 shared_libs: ["libstub"],
1206 apex_available: ["myapex"],
1207 min_sdk_version: "Z",
1208 }
1209
1210 apex_key {
1211 name: "myapex.key",
1212 public_key: "testkey.avbpubkey",
1213 private_key: "testkey.pem",
1214 }
1215
1216 apex {
1217 name: "otherapex",
1218 key: "myapex.key",
1219 native_shared_libs: ["libstub"],
1220 min_sdk_version: "29",
1221 }
1222
1223 cc_library {
1224 name: "libstub",
1225 srcs: ["mylib.cpp"],
1226 stubs: {
1227 versions: ["29", "Z", "current"],
1228 },
1229 apex_available: ["otherapex"],
1230 min_sdk_version: "29",
1231 }
1232
1233 // platform module depending on libstub from otherapex should use the latest stub("current")
1234 cc_library {
1235 name: "libplatform",
1236 srcs: ["mylib.cpp"],
1237 shared_libs: ["libstub"],
1238 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001239 `,
1240 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1241 variables.Platform_sdk_codename = proptools.StringPtr("Z")
1242 variables.Platform_sdk_final = proptools.BoolPtr(false)
1243 variables.Platform_version_active_codenames = []string{"Z"}
1244 }),
1245 )
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001246
Jiyong Park55549df2021-02-26 23:57:23 +09001247 // Ensure that mylib from myapex is built against the latest stub (current)
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001248 mylibCflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001249 ensureContains(t, mylibCflags, "-D__LIBSTUB_API__=10000 ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001250 mylibLdflags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park55549df2021-02-26 23:57:23 +09001251 ensureContains(t, mylibLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
Jooyung Han11b0fbd2021-02-05 02:28:22 +09001252
1253 // Ensure that libplatform is built against latest stub ("current") of mylib3 from the apex
1254 libplatformCflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
1255 ensureContains(t, libplatformCflags, "-D__LIBSTUB_API__=10000 ") // "current" maps to 10000
1256 libplatformLdflags := ctx.ModuleForTests("libplatform", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1257 ensureContains(t, libplatformLdflags, "libstub/android_arm64_armv8-a_shared_current/libstub.so ")
1258}
1259
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001260func TestApexWithExplicitStubsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001261 ctx := testApex(t, `
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001262 apex {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001263 name: "myapex2",
1264 key: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001265 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001266 updatable: false,
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001267 }
1268
1269 apex_key {
Jiyong Park83dc74b2020-01-14 18:38:44 +09001270 name: "myapex2.key",
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001271 public_key: "testkey.avbpubkey",
1272 private_key: "testkey.pem",
1273 }
1274
1275 cc_library {
1276 name: "mylib",
1277 srcs: ["mylib.cpp"],
1278 shared_libs: ["libfoo#10"],
Jiyong Park678c8812020-02-07 17:25:49 +09001279 static_libs: ["libbaz"],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001280 system_shared_libs: [],
1281 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001282 apex_available: [ "myapex2" ],
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001283 }
1284
1285 cc_library {
1286 name: "libfoo",
1287 srcs: ["mylib.cpp"],
1288 shared_libs: ["libbar"],
1289 system_shared_libs: [],
1290 stl: "none",
1291 stubs: {
1292 versions: ["10", "20", "30"],
1293 },
1294 }
1295
1296 cc_library {
1297 name: "libbar",
1298 srcs: ["mylib.cpp"],
1299 system_shared_libs: [],
1300 stl: "none",
1301 }
1302
Jiyong Park678c8812020-02-07 17:25:49 +09001303 cc_library_static {
1304 name: "libbaz",
1305 srcs: ["mylib.cpp"],
1306 system_shared_libs: [],
1307 stl: "none",
1308 apex_available: [ "myapex2" ],
1309 }
1310
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001311 `)
1312
Jiyong Park83dc74b2020-01-14 18:38:44 +09001313 apexRule := ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Rule("apexRule")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001314 copyCmds := apexRule.Args["copy_commands"]
1315
1316 // Ensure that direct non-stubs dep is always included
1317 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1318
1319 // Ensure that indirect stubs dep is not included
1320 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1321
1322 // Ensure that dependency of stubs is not included
1323 ensureNotContains(t, copyCmds, "image.apex/lib64/libbar.so")
1324
Colin Crossaede88c2020-08-11 12:17:01 -07001325 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001326
1327 // Ensure that mylib is linking with version 10 of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001328 ensureContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared_10/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001329 // ... and not linking to the non-stub (impl) variant of libfoo
Jiyong Park3ff16992019-12-27 14:11:47 +09001330 ensureNotContains(t, mylibLdFlags, "libfoo/android_arm64_armv8-a_shared/libfoo.so")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001331
Jiyong Park3ff16992019-12-27 14:11:47 +09001332 libFooStubsLdFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_10").Rule("ld").Args["libFlags"]
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001333
1334 // Ensure that libfoo stubs is not linking to libbar (since it is a stubs)
1335 ensureNotContains(t, libFooStubsLdFlags, "libbar.so")
Jiyong Park83dc74b2020-01-14 18:38:44 +09001336
Artur Satayeva8bd1132020-04-27 18:07:06 +01001337 fullDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/fulllist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001338 ensureListContains(t, fullDepsInfo, " libfoo(minSdkVersion:(no version)) (external) <- mylib")
Jiyong Park678c8812020-02-07 17:25:49 +09001339
Artur Satayeva8bd1132020-04-27 18:07:06 +01001340 flatDepsInfo := strings.Split(ctx.ModuleForTests("myapex2", "android_common_myapex2_image").Output("depsinfo/flatlist.txt").Args["content"], "\\n")
Artur Satayev4e1f2bd2020-05-14 15:15:01 +01001341 ensureListContains(t, flatDepsInfo, "libfoo(minSdkVersion:(no version)) (external)")
Jiyong Park0ddfcd12018-12-11 01:35:25 +09001342}
1343
Jooyung Hand3639552019-08-09 12:57:43 +09001344func TestApexWithRuntimeLibsDependency(t *testing.T) {
1345 /*
1346 myapex
1347 |
1348 v (runtime_libs)
1349 mylib ------+------> libfoo [provides stub]
1350 |
1351 `------> libbar
1352 */
Colin Cross1c460562021-02-16 17:55:47 -08001353 ctx := testApex(t, `
Jooyung Hand3639552019-08-09 12:57:43 +09001354 apex {
1355 name: "myapex",
1356 key: "myapex.key",
1357 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001358 updatable: false,
Jooyung Hand3639552019-08-09 12:57:43 +09001359 }
1360
1361 apex_key {
1362 name: "myapex.key",
1363 public_key: "testkey.avbpubkey",
1364 private_key: "testkey.pem",
1365 }
1366
1367 cc_library {
1368 name: "mylib",
1369 srcs: ["mylib.cpp"],
1370 runtime_libs: ["libfoo", "libbar"],
1371 system_shared_libs: [],
1372 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001373 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001374 }
1375
1376 cc_library {
1377 name: "libfoo",
1378 srcs: ["mylib.cpp"],
1379 system_shared_libs: [],
1380 stl: "none",
1381 stubs: {
1382 versions: ["10", "20", "30"],
1383 },
1384 }
1385
1386 cc_library {
1387 name: "libbar",
1388 srcs: ["mylib.cpp"],
1389 system_shared_libs: [],
1390 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001391 apex_available: [ "myapex" ],
Jooyung Hand3639552019-08-09 12:57:43 +09001392 }
1393
1394 `)
1395
Sundong Ahnabb64432019-10-22 13:58:29 +09001396 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jooyung Hand3639552019-08-09 12:57:43 +09001397 copyCmds := apexRule.Args["copy_commands"]
1398
1399 // Ensure that direct non-stubs dep is always included
1400 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
1401
1402 // Ensure that indirect stubs dep is not included
1403 ensureNotContains(t, copyCmds, "image.apex/lib64/libfoo.so")
1404
1405 // Ensure that runtime_libs dep in included
1406 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
1407
Sundong Ahnabb64432019-10-22 13:58:29 +09001408 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09001409 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1410 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libfoo.so")
Jooyung Hand3639552019-08-09 12:57:43 +09001411
1412}
1413
Paul Duffina02cae32021-03-09 01:44:06 +00001414var prepareForTestOfRuntimeApexWithHwasan = android.GroupFixturePreparers(
1415 cc.PrepareForTestWithCcBuildComponents,
1416 PrepareForTestWithApexBuildComponents,
1417 android.FixtureAddTextFile("bionic/apex/Android.bp", `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001418 apex {
1419 name: "com.android.runtime",
1420 key: "com.android.runtime.key",
1421 native_shared_libs: ["libc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001422 updatable: false,
Jooyung Han8ce8db92020-05-15 19:05:05 +09001423 }
1424
1425 apex_key {
1426 name: "com.android.runtime.key",
1427 public_key: "testkey.avbpubkey",
1428 private_key: "testkey.pem",
1429 }
Paul Duffina02cae32021-03-09 01:44:06 +00001430 `),
1431 android.FixtureAddFile("system/sepolicy/apex/com.android.runtime-file_contexts", nil),
1432)
Jooyung Han8ce8db92020-05-15 19:05:05 +09001433
Paul Duffina02cae32021-03-09 01:44:06 +00001434func TestRuntimeApexShouldInstallHwasanIfLibcDependsOnIt(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001435 result := android.GroupFixturePreparers(prepareForTestOfRuntimeApexWithHwasan).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001436 cc_library {
1437 name: "libc",
1438 no_libcrt: true,
1439 nocrt: true,
1440 stl: "none",
1441 system_shared_libs: [],
1442 stubs: { versions: ["1"] },
1443 apex_available: ["com.android.runtime"],
1444
1445 sanitize: {
1446 hwaddress: true,
1447 }
1448 }
1449
1450 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001451 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001452 no_libcrt: true,
1453 nocrt: true,
1454 stl: "none",
1455 system_shared_libs: [],
1456 srcs: [""],
1457 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001458 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001459
1460 sanitize: {
1461 never: true,
1462 },
Paul Duffina02cae32021-03-09 01:44:06 +00001463 } `)
1464 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001465
1466 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1467 "lib64/bionic/libc.so",
1468 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1469 })
1470
Colin Cross4c4c1be2022-02-10 11:41:18 -08001471 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001472
1473 installed := hwasan.Description("install libclang_rt.hwasan")
1474 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1475
1476 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1477 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1478 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1479}
1480
1481func TestRuntimeApexShouldInstallHwasanIfHwaddressSanitized(t *testing.T) {
Paul Duffin70d3bee2021-03-21 11:26:05 +00001482 result := android.GroupFixturePreparers(
Paul Duffina02cae32021-03-09 01:44:06 +00001483 prepareForTestOfRuntimeApexWithHwasan,
1484 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1485 variables.SanitizeDevice = []string{"hwaddress"}
1486 }),
1487 ).RunTestWithBp(t, `
Jooyung Han8ce8db92020-05-15 19:05:05 +09001488 cc_library {
1489 name: "libc",
1490 no_libcrt: true,
1491 nocrt: true,
1492 stl: "none",
1493 system_shared_libs: [],
1494 stubs: { versions: ["1"] },
1495 apex_available: ["com.android.runtime"],
1496 }
1497
1498 cc_prebuilt_library_shared {
Colin Cross4c4c1be2022-02-10 11:41:18 -08001499 name: "libclang_rt.hwasan",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001500 no_libcrt: true,
1501 nocrt: true,
1502 stl: "none",
1503 system_shared_libs: [],
1504 srcs: [""],
1505 stubs: { versions: ["1"] },
Colin Cross4c4c1be2022-02-10 11:41:18 -08001506 stem: "libclang_rt.hwasan-aarch64-android",
Jooyung Han8ce8db92020-05-15 19:05:05 +09001507
1508 sanitize: {
1509 never: true,
1510 },
1511 }
Paul Duffina02cae32021-03-09 01:44:06 +00001512 `)
1513 ctx := result.TestContext
Jooyung Han8ce8db92020-05-15 19:05:05 +09001514
1515 ensureExactContents(t, ctx, "com.android.runtime", "android_common_hwasan_com.android.runtime_image", []string{
1516 "lib64/bionic/libc.so",
1517 "lib64/bionic/libclang_rt.hwasan-aarch64-android.so",
1518 })
1519
Colin Cross4c4c1be2022-02-10 11:41:18 -08001520 hwasan := ctx.ModuleForTests("libclang_rt.hwasan", "android_arm64_armv8-a_shared")
Jooyung Han8ce8db92020-05-15 19:05:05 +09001521
1522 installed := hwasan.Description("install libclang_rt.hwasan")
1523 ensureContains(t, installed.Output.String(), "/system/lib64/bootstrap/libclang_rt.hwasan-aarch64-android.so")
1524
1525 symlink := hwasan.Description("install symlink libclang_rt.hwasan")
1526 ensureEquals(t, symlink.Args["fromPath"], "/apex/com.android.runtime/lib64/bionic/libclang_rt.hwasan-aarch64-android.so")
1527 ensureContains(t, symlink.Output.String(), "/system/lib64/libclang_rt.hwasan-aarch64-android.so")
1528}
1529
Jooyung Han61b66e92020-03-21 14:21:46 +00001530func TestApexDependsOnLLNDKTransitively(t *testing.T) {
1531 testcases := []struct {
1532 name string
1533 minSdkVersion string
Colin Crossaede88c2020-08-11 12:17:01 -07001534 apexVariant string
Jooyung Han61b66e92020-03-21 14:21:46 +00001535 shouldLink string
1536 shouldNotLink []string
1537 }{
1538 {
Jiyong Park55549df2021-02-26 23:57:23 +09001539 name: "unspecified version links to the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001540 minSdkVersion: "",
Colin Crossaede88c2020-08-11 12:17:01 -07001541 apexVariant: "apex10000",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001542 shouldLink: "current",
1543 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001544 },
1545 {
Jiyong Park55549df2021-02-26 23:57:23 +09001546 name: "always use the latest",
Jooyung Han749dc692020-04-15 11:03:39 +09001547 minSdkVersion: "min_sdk_version: \"29\",",
Colin Crossaede88c2020-08-11 12:17:01 -07001548 apexVariant: "apex29",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001549 shouldLink: "current",
1550 shouldNotLink: []string{"29", "30"},
Jooyung Han61b66e92020-03-21 14:21:46 +00001551 },
1552 }
1553 for _, tc := range testcases {
1554 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001555 ctx := testApex(t, `
Jooyung Han61b66e92020-03-21 14:21:46 +00001556 apex {
1557 name: "myapex",
1558 key: "myapex.key",
Jooyung Han61b66e92020-03-21 14:21:46 +00001559 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001560 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09001561 `+tc.minSdkVersion+`
Jooyung Han61b66e92020-03-21 14:21:46 +00001562 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001563
Jooyung Han61b66e92020-03-21 14:21:46 +00001564 apex_key {
1565 name: "myapex.key",
1566 public_key: "testkey.avbpubkey",
1567 private_key: "testkey.pem",
1568 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001569
Jooyung Han61b66e92020-03-21 14:21:46 +00001570 cc_library {
1571 name: "mylib",
1572 srcs: ["mylib.cpp"],
1573 vendor_available: true,
1574 shared_libs: ["libbar"],
1575 system_shared_libs: [],
1576 stl: "none",
1577 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001578 min_sdk_version: "29",
Jooyung Han61b66e92020-03-21 14:21:46 +00001579 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001580
Jooyung Han61b66e92020-03-21 14:21:46 +00001581 cc_library {
1582 name: "libbar",
1583 srcs: ["mylib.cpp"],
1584 system_shared_libs: [],
1585 stl: "none",
1586 stubs: { versions: ["29","30"] },
Colin Cross203b4212021-04-26 17:19:41 -07001587 llndk: {
1588 symbol_file: "libbar.map.txt",
1589 }
Jooyung Han61b66e92020-03-21 14:21:46 +00001590 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001591 `,
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001592 withUnbundledBuild,
1593 )
Jooyung Han9c80bae2019-08-20 17:30:57 +09001594
Jooyung Han61b66e92020-03-21 14:21:46 +00001595 // Ensure that LLNDK dep is not included
1596 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
1597 "lib64/mylib.so",
1598 })
Jooyung Han9c80bae2019-08-20 17:30:57 +09001599
Jooyung Han61b66e92020-03-21 14:21:46 +00001600 // Ensure that LLNDK dep is required
1601 apexManifestRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
1602 ensureListEmpty(t, names(apexManifestRule.Args["provideNativeLibs"]))
1603 ensureListContains(t, names(apexManifestRule.Args["requireNativeLibs"]), "libbar.so")
Jooyung Han9c80bae2019-08-20 17:30:57 +09001604
Steven Moreland2c4000c2021-04-27 02:08:49 +00001605 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_"+tc.apexVariant).Rule("ld").Args["libFlags"]
1606 ensureContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+tc.shouldLink+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001607 for _, ver := range tc.shouldNotLink {
Steven Moreland2c4000c2021-04-27 02:08:49 +00001608 ensureNotContains(t, mylibLdFlags, "libbar/android_arm64_armv8-a_shared_"+ver+"/libbar.so")
Jooyung Han61b66e92020-03-21 14:21:46 +00001609 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001610
Steven Moreland2c4000c2021-04-27 02:08:49 +00001611 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_"+tc.apexVariant).Rule("cc").Args["cFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001612 ver := tc.shouldLink
1613 if tc.shouldLink == "current" {
1614 ver = strconv.Itoa(android.FutureApiLevelInt)
1615 }
1616 ensureContains(t, mylibCFlags, "__LIBBAR_API__="+ver)
Jooyung Han61b66e92020-03-21 14:21:46 +00001617 })
1618 }
Jooyung Han9c80bae2019-08-20 17:30:57 +09001619}
1620
Jiyong Park25fc6a92018-11-18 18:02:45 +09001621func TestApexWithSystemLibsStubs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001622 ctx := testApex(t, `
Jiyong Park25fc6a92018-11-18 18:02:45 +09001623 apex {
1624 name: "myapex",
1625 key: "myapex.key",
1626 native_shared_libs: ["mylib", "mylib_shared", "libdl", "libm"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001627 updatable: false,
Jiyong Park25fc6a92018-11-18 18:02:45 +09001628 }
1629
1630 apex_key {
1631 name: "myapex.key",
1632 public_key: "testkey.avbpubkey",
1633 private_key: "testkey.pem",
1634 }
1635
1636 cc_library {
1637 name: "mylib",
1638 srcs: ["mylib.cpp"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07001639 system_shared_libs: ["libc", "libm"],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001640 shared_libs: ["libdl#27"],
1641 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001642 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001643 }
1644
1645 cc_library_shared {
1646 name: "mylib_shared",
1647 srcs: ["mylib.cpp"],
1648 shared_libs: ["libdl#27"],
1649 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00001650 apex_available: [ "myapex" ],
Jiyong Park25fc6a92018-11-18 18:02:45 +09001651 }
1652
1653 cc_library {
Jiyong Parkb0788572018-12-20 22:10:17 +09001654 name: "libBootstrap",
1655 srcs: ["mylib.cpp"],
1656 stl: "none",
1657 bootstrap: true,
1658 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001659 `)
1660
Sundong Ahnabb64432019-10-22 13:58:29 +09001661 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001662 copyCmds := apexRule.Args["copy_commands"]
1663
1664 // Ensure that mylib, libm, libdl are included.
Alex Light5098a612018-11-29 17:12:15 -08001665 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Jiyong Parkb0788572018-12-20 22:10:17 +09001666 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libm.so")
1667 ensureContains(t, copyCmds, "image.apex/lib64/bionic/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001668
1669 // Ensure that libc is not included (since it has stubs and not listed in native_shared_libs)
Jiyong Parkb0788572018-12-20 22:10:17 +09001670 ensureNotContains(t, copyCmds, "image.apex/lib64/bionic/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001671
Colin Crossaede88c2020-08-11 12:17:01 -07001672 mylibLdFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
1673 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
1674 mylibSharedCFlags := ctx.ModuleForTests("mylib_shared", "android_arm64_armv8-a_shared_apex10000").Rule("cc").Args["cFlags"]
Jiyong Park25fc6a92018-11-18 18:02:45 +09001675
1676 // For dependency to libc
1677 // Ensure that mylib is linking with the latest version of stubs
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001678 ensureContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared_current/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001679 // ... and not linking to the non-stub (impl) variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001680 ensureNotContains(t, mylibLdFlags, "libc/android_arm64_armv8-a_shared/libc.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001681 // ... Cflags from stub is correctly exported to mylib
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001682 ensureContains(t, mylibCFlags, "__LIBC_API__=10000")
1683 ensureContains(t, mylibSharedCFlags, "__LIBC_API__=10000")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001684
1685 // For dependency to libm
1686 // Ensure that mylib is linking with the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001687 ensureContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_apex10000/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001688 // ... and not linking to the stub variant
Jiyong Park3ff16992019-12-27 14:11:47 +09001689 ensureNotContains(t, mylibLdFlags, "libm/android_arm64_armv8-a_shared_29/libm.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001690 // ... and is not compiling with the stub
1691 ensureNotContains(t, mylibCFlags, "__LIBM_API__=29")
1692 ensureNotContains(t, mylibSharedCFlags, "__LIBM_API__=29")
1693
1694 // For dependency to libdl
1695 // Ensure that mylib is linking with the specified version of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001696 ensureContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_27/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001697 // ... and not linking to the other versions of stubs
Jiyong Park3ff16992019-12-27 14:11:47 +09001698 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_28/libdl.so")
1699 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_29/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001700 // ... and not linking to the non-stub (impl) variant
Colin Crossaede88c2020-08-11 12:17:01 -07001701 ensureNotContains(t, mylibLdFlags, "libdl/android_arm64_armv8-a_shared_apex10000/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001702 // ... Cflags from stub is correctly exported to mylib
1703 ensureContains(t, mylibCFlags, "__LIBDL_API__=27")
1704 ensureContains(t, mylibSharedCFlags, "__LIBDL_API__=27")
Jiyong Parkb0788572018-12-20 22:10:17 +09001705
1706 // Ensure that libBootstrap is depending on the platform variant of bionic libs
Colin Cross7113d202019-11-20 16:39:12 -08001707 libFlags := ctx.ModuleForTests("libBootstrap", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
1708 ensureContains(t, libFlags, "libc/android_arm64_armv8-a_shared/libc.so")
1709 ensureContains(t, libFlags, "libm/android_arm64_armv8-a_shared/libm.so")
1710 ensureContains(t, libFlags, "libdl/android_arm64_armv8-a_shared/libdl.so")
Jiyong Park25fc6a92018-11-18 18:02:45 +09001711}
Jiyong Park7c2ee712018-12-07 00:42:25 +09001712
Jooyung Han749dc692020-04-15 11:03:39 +09001713func TestApexMinSdkVersion_NativeModulesShouldBeBuiltAgainstStubs(t *testing.T) {
Jiyong Park55549df2021-02-26 23:57:23 +09001714 // there are three links between liba --> libz.
1715 // 1) myapex -> libx -> liba -> libz : this should be #30 link
Jooyung Han749dc692020-04-15 11:03:39 +09001716 // 2) otherapex -> liby -> liba -> libz : this should be #30 link
Jooyung Han03b51852020-02-26 22:45:42 +09001717 // 3) (platform) -> liba -> libz : this should be non-stub link
Colin Cross1c460562021-02-16 17:55:47 -08001718 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001719 apex {
1720 name: "myapex",
1721 key: "myapex.key",
1722 native_shared_libs: ["libx"],
Jooyung Han749dc692020-04-15 11:03:39 +09001723 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001724 }
1725
1726 apex {
1727 name: "otherapex",
1728 key: "myapex.key",
1729 native_shared_libs: ["liby"],
Jooyung Han749dc692020-04-15 11:03:39 +09001730 min_sdk_version: "30",
Jooyung Han03b51852020-02-26 22:45:42 +09001731 }
1732
1733 apex_key {
1734 name: "myapex.key",
1735 public_key: "testkey.avbpubkey",
1736 private_key: "testkey.pem",
1737 }
1738
1739 cc_library {
1740 name: "libx",
1741 shared_libs: ["liba"],
1742 system_shared_libs: [],
1743 stl: "none",
1744 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001745 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001746 }
1747
1748 cc_library {
1749 name: "liby",
1750 shared_libs: ["liba"],
1751 system_shared_libs: [],
1752 stl: "none",
1753 apex_available: [ "otherapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001754 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001755 }
1756
1757 cc_library {
1758 name: "liba",
1759 shared_libs: ["libz"],
1760 system_shared_libs: [],
1761 stl: "none",
1762 apex_available: [
1763 "//apex_available:anyapex",
1764 "//apex_available:platform",
1765 ],
Jooyung Han749dc692020-04-15 11:03:39 +09001766 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09001767 }
1768
1769 cc_library {
1770 name: "libz",
1771 system_shared_libs: [],
1772 stl: "none",
1773 stubs: {
Jooyung Han749dc692020-04-15 11:03:39 +09001774 versions: ["28", "30"],
Jooyung Han03b51852020-02-26 22:45:42 +09001775 },
1776 }
Jooyung Han749dc692020-04-15 11:03:39 +09001777 `)
Jooyung Han03b51852020-02-26 22:45:42 +09001778
1779 expectLink := func(from, from_variant, to, to_variant string) {
1780 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1781 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1782 }
1783 expectNoLink := func(from, from_variant, to, to_variant string) {
1784 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1785 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1786 }
1787 // platform liba is linked to non-stub version
1788 expectLink("liba", "shared", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001789 // liba in myapex is linked to current
1790 expectLink("liba", "shared_apex29", "libz", "shared_current")
1791 expectNoLink("liba", "shared_apex29", "libz", "shared_30")
Jiyong Park55549df2021-02-26 23:57:23 +09001792 expectNoLink("liba", "shared_apex29", "libz", "shared_28")
Colin Crossaede88c2020-08-11 12:17:01 -07001793 expectNoLink("liba", "shared_apex29", "libz", "shared")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001794 // liba in otherapex is linked to current
1795 expectLink("liba", "shared_apex30", "libz", "shared_current")
1796 expectNoLink("liba", "shared_apex30", "libz", "shared_30")
Colin Crossaede88c2020-08-11 12:17:01 -07001797 expectNoLink("liba", "shared_apex30", "libz", "shared_28")
1798 expectNoLink("liba", "shared_apex30", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001799}
1800
Jooyung Hanaed150d2020-04-02 01:41:41 +09001801func TestApexMinSdkVersion_SupportsCodeNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001802 ctx := testApex(t, `
Jooyung Hanaed150d2020-04-02 01:41:41 +09001803 apex {
1804 name: "myapex",
1805 key: "myapex.key",
1806 native_shared_libs: ["libx"],
1807 min_sdk_version: "R",
1808 }
1809
1810 apex_key {
1811 name: "myapex.key",
1812 public_key: "testkey.avbpubkey",
1813 private_key: "testkey.pem",
1814 }
1815
1816 cc_library {
1817 name: "libx",
1818 shared_libs: ["libz"],
1819 system_shared_libs: [],
1820 stl: "none",
1821 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09001822 min_sdk_version: "R",
Jooyung Hanaed150d2020-04-02 01:41:41 +09001823 }
1824
1825 cc_library {
1826 name: "libz",
1827 system_shared_libs: [],
1828 stl: "none",
1829 stubs: {
1830 versions: ["29", "R"],
1831 },
1832 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00001833 `,
1834 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1835 variables.Platform_version_active_codenames = []string{"R"}
1836 }),
1837 )
Jooyung Hanaed150d2020-04-02 01:41:41 +09001838
1839 expectLink := func(from, from_variant, to, to_variant string) {
1840 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1841 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1842 }
1843 expectNoLink := func(from, from_variant, to, to_variant string) {
1844 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1845 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1846 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001847 expectLink("libx", "shared_apex10000", "libz", "shared_current")
1848 expectNoLink("libx", "shared_apex10000", "libz", "shared_R")
Colin Crossaede88c2020-08-11 12:17:01 -07001849 expectNoLink("libx", "shared_apex10000", "libz", "shared_29")
1850 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Hanaed150d2020-04-02 01:41:41 +09001851}
1852
Jooyung Han4c4da062021-06-23 10:23:16 +09001853func TestApexMinSdkVersion_SupportsCodeNames_JavaLibs(t *testing.T) {
1854 testApex(t, `
1855 apex {
1856 name: "myapex",
1857 key: "myapex.key",
1858 java_libs: ["libx"],
1859 min_sdk_version: "S",
1860 }
1861
1862 apex_key {
1863 name: "myapex.key",
1864 public_key: "testkey.avbpubkey",
1865 private_key: "testkey.pem",
1866 }
1867
1868 java_library {
1869 name: "libx",
1870 srcs: ["a.java"],
1871 apex_available: [ "myapex" ],
1872 sdk_version: "current",
1873 min_sdk_version: "S", // should be okay
1874 }
1875 `,
1876 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1877 variables.Platform_version_active_codenames = []string{"S"}
1878 variables.Platform_sdk_codename = proptools.StringPtr("S")
1879 }),
1880 )
1881}
1882
Jooyung Han749dc692020-04-15 11:03:39 +09001883func TestApexMinSdkVersion_DefaultsToLatest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001884 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001885 apex {
1886 name: "myapex",
1887 key: "myapex.key",
1888 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001889 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001890 }
1891
1892 apex_key {
1893 name: "myapex.key",
1894 public_key: "testkey.avbpubkey",
1895 private_key: "testkey.pem",
1896 }
1897
1898 cc_library {
1899 name: "libx",
1900 shared_libs: ["libz"],
1901 system_shared_libs: [],
1902 stl: "none",
1903 apex_available: [ "myapex" ],
1904 }
1905
1906 cc_library {
1907 name: "libz",
1908 system_shared_libs: [],
1909 stl: "none",
1910 stubs: {
1911 versions: ["1", "2"],
1912 },
1913 }
1914 `)
1915
1916 expectLink := func(from, from_variant, to, to_variant string) {
1917 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1918 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1919 }
1920 expectNoLink := func(from, from_variant, to, to_variant string) {
1921 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
1922 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
1923 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001924 expectLink("libx", "shared_apex10000", "libz", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07001925 expectNoLink("libx", "shared_apex10000", "libz", "shared_1")
Jiyong Parkd4a3a132021-03-17 20:21:35 +09001926 expectNoLink("libx", "shared_apex10000", "libz", "shared_2")
Colin Crossaede88c2020-08-11 12:17:01 -07001927 expectNoLink("libx", "shared_apex10000", "libz", "shared")
Jooyung Han03b51852020-02-26 22:45:42 +09001928}
1929
Jooyung Handfc864c2023-03-20 18:19:07 +09001930func TestApexMinSdkVersion_InVendorApex(t *testing.T) {
Jiyong Park5df7bd32021-08-25 16:18:46 +09001931 ctx := testApex(t, `
1932 apex {
1933 name: "myapex",
1934 key: "myapex.key",
1935 native_shared_libs: ["mylib"],
Jooyung Handfc864c2023-03-20 18:19:07 +09001936 updatable: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09001937 vendor: true,
1938 min_sdk_version: "29",
1939 }
1940
1941 apex_key {
1942 name: "myapex.key",
1943 public_key: "testkey.avbpubkey",
1944 private_key: "testkey.pem",
1945 }
1946
1947 cc_library {
1948 name: "mylib",
Jooyung Handfc864c2023-03-20 18:19:07 +09001949 srcs: ["mylib.cpp"],
Jiyong Park5df7bd32021-08-25 16:18:46 +09001950 vendor_available: true,
Jiyong Park5df7bd32021-08-25 16:18:46 +09001951 min_sdk_version: "29",
Jooyung Handfc864c2023-03-20 18:19:07 +09001952 shared_libs: ["libbar"],
1953 }
1954
1955 cc_library {
1956 name: "libbar",
1957 stubs: { versions: ["29", "30"] },
1958 llndk: { symbol_file: "libbar.map.txt" },
Jiyong Park5df7bd32021-08-25 16:18:46 +09001959 }
1960 `)
1961
1962 vendorVariant := "android_vendor.29_arm64_armv8-a"
1963
Jooyung Handfc864c2023-03-20 18:19:07 +09001964 mylib := ctx.ModuleForTests("mylib", vendorVariant+"_shared_myapex")
1965
1966 // Ensure that mylib links with "current" LLNDK
1967 libFlags := names(mylib.Rule("ld").Args["libFlags"])
1968 ensureListContains(t, libFlags, "out/soong/.intermediates/libbar/"+vendorVariant+"_shared_current/libbar.so")
1969
1970 // Ensure that mylib is targeting 29
1971 ccRule := ctx.ModuleForTests("mylib", vendorVariant+"_static_apex29").Output("obj/mylib.o")
1972 ensureContains(t, ccRule.Args["cFlags"], "-target aarch64-linux-android29")
1973
1974 // Ensure that the correct variant of crtbegin_so is used.
1975 crtBegin := mylib.Rule("ld").Args["crtBegin"]
1976 ensureContains(t, crtBegin, "out/soong/.intermediates/"+cc.DefaultCcCommonTestModulesDir+"crtbegin_so/"+vendorVariant+"_apex29/crtbegin_so.o")
Jiyong Park5df7bd32021-08-25 16:18:46 +09001977
1978 // Ensure that the crtbegin_so used by the APEX is targeting 29
1979 cflags := ctx.ModuleForTests("crtbegin_so", vendorVariant+"_apex29").Rule("cc").Args["cFlags"]
1980 android.AssertStringDoesContain(t, "cflags", cflags, "-target aarch64-linux-android29")
1981}
1982
Jooyung Han03b51852020-02-26 22:45:42 +09001983func TestPlatformUsesLatestStubsFromApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08001984 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09001985 apex {
1986 name: "myapex",
1987 key: "myapex.key",
1988 native_shared_libs: ["libx"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00001989 updatable: false,
Jooyung Han03b51852020-02-26 22:45:42 +09001990 }
1991
1992 apex_key {
1993 name: "myapex.key",
1994 public_key: "testkey.avbpubkey",
1995 private_key: "testkey.pem",
1996 }
1997
1998 cc_library {
1999 name: "libx",
2000 system_shared_libs: [],
2001 stl: "none",
2002 apex_available: [ "myapex" ],
2003 stubs: {
2004 versions: ["1", "2"],
2005 },
2006 }
2007
2008 cc_library {
2009 name: "libz",
2010 shared_libs: ["libx"],
2011 system_shared_libs: [],
2012 stl: "none",
2013 }
2014 `)
2015
2016 expectLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002017 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002018 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2019 ensureContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2020 }
2021 expectNoLink := func(from, from_variant, to, to_variant string) {
Colin Cross56a83212020-09-15 18:30:11 -07002022 t.Helper()
Jooyung Han03b51852020-02-26 22:45:42 +09002023 ldArgs := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld").Args["libFlags"]
2024 ensureNotContains(t, ldArgs, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2025 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002026 expectLink("libz", "shared", "libx", "shared_current")
2027 expectNoLink("libz", "shared", "libx", "shared_2")
Jooyung Han03b51852020-02-26 22:45:42 +09002028 expectNoLink("libz", "shared", "libz", "shared_1")
2029 expectNoLink("libz", "shared", "libz", "shared")
2030}
2031
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002032var prepareForTestWithSantitizeHwaddress = android.FixtureModifyProductVariables(
2033 func(variables android.FixtureProductVariables) {
2034 variables.SanitizeDevice = []string{"hwaddress"}
2035 },
2036)
2037
Jooyung Han75568392020-03-20 04:29:24 +09002038func TestQApexesUseLatestStubsInBundledBuildsAndHWASAN(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002039 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002040 apex {
2041 name: "myapex",
2042 key: "myapex.key",
2043 native_shared_libs: ["libx"],
2044 min_sdk_version: "29",
2045 }
2046
2047 apex_key {
2048 name: "myapex.key",
2049 public_key: "testkey.avbpubkey",
2050 private_key: "testkey.pem",
2051 }
2052
2053 cc_library {
2054 name: "libx",
2055 shared_libs: ["libbar"],
2056 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002057 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002058 }
2059
2060 cc_library {
2061 name: "libbar",
2062 stubs: {
2063 versions: ["29", "30"],
2064 },
2065 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002066 `,
2067 prepareForTestWithSantitizeHwaddress,
2068 )
Jooyung Han03b51852020-02-26 22:45:42 +09002069 expectLink := func(from, from_variant, to, to_variant string) {
2070 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2071 libFlags := ld.Args["libFlags"]
2072 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2073 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002074 expectLink("libx", "shared_hwasan_apex29", "libbar", "shared_current")
Jooyung Han03b51852020-02-26 22:45:42 +09002075}
2076
Jooyung Han75568392020-03-20 04:29:24 +09002077func TestQTargetApexUsesStaticUnwinder(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002078 ctx := testApex(t, `
Jooyung Han03b51852020-02-26 22:45:42 +09002079 apex {
2080 name: "myapex",
2081 key: "myapex.key",
2082 native_shared_libs: ["libx"],
2083 min_sdk_version: "29",
2084 }
2085
2086 apex_key {
2087 name: "myapex.key",
2088 public_key: "testkey.avbpubkey",
2089 private_key: "testkey.pem",
2090 }
2091
2092 cc_library {
2093 name: "libx",
2094 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09002095 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002096 }
Jooyung Han75568392020-03-20 04:29:24 +09002097 `)
Jooyung Han03b51852020-02-26 22:45:42 +09002098
2099 // ensure apex variant of c++ is linked with static unwinder
Colin Crossaede88c2020-08-11 12:17:01 -07002100 cm := ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared_apex29").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002101 ensureListContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002102 // note that platform variant is not.
2103 cm = ctx.ModuleForTests("libc++", "android_arm64_armv8-a_shared").Module().(*cc.Module)
Ryan Prichardb35a85e2021-01-13 19:18:53 -08002104 ensureListNotContains(t, cm.Properties.AndroidMkStaticLibs, "libunwind")
Jooyung Han03b51852020-02-26 22:45:42 +09002105}
2106
Jooyung Han749dc692020-04-15 11:03:39 +09002107func TestApexMinSdkVersion_ErrorIfIncompatibleVersion(t *testing.T) {
2108 testApexError(t, `module "mylib".*: should support min_sdk_version\(29\)`, `
Jooyung Han03b51852020-02-26 22:45:42 +09002109 apex {
2110 name: "myapex",
2111 key: "myapex.key",
Jooyung Han749dc692020-04-15 11:03:39 +09002112 native_shared_libs: ["mylib"],
2113 min_sdk_version: "29",
Jooyung Han03b51852020-02-26 22:45:42 +09002114 }
2115
2116 apex_key {
2117 name: "myapex.key",
2118 public_key: "testkey.avbpubkey",
2119 private_key: "testkey.pem",
2120 }
Jooyung Han749dc692020-04-15 11:03:39 +09002121
2122 cc_library {
2123 name: "mylib",
2124 srcs: ["mylib.cpp"],
2125 system_shared_libs: [],
2126 stl: "none",
2127 apex_available: [
2128 "myapex",
2129 ],
2130 min_sdk_version: "30",
2131 }
2132 `)
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002133
2134 testApexError(t, `module "libfoo.ffi".*: should support min_sdk_version\(29\)`, `
2135 apex {
2136 name: "myapex",
2137 key: "myapex.key",
2138 native_shared_libs: ["libfoo.ffi"],
2139 min_sdk_version: "29",
2140 }
2141
2142 apex_key {
2143 name: "myapex.key",
2144 public_key: "testkey.avbpubkey",
2145 private_key: "testkey.pem",
2146 }
2147
2148 rust_ffi_shared {
2149 name: "libfoo.ffi",
2150 srcs: ["foo.rs"],
2151 crate_name: "foo",
2152 apex_available: [
2153 "myapex",
2154 ],
2155 min_sdk_version: "30",
2156 }
2157 `)
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002158
2159 testApexError(t, `module "libfoo".*: should support min_sdk_version\(29\)`, `
2160 apex {
2161 name: "myapex",
2162 key: "myapex.key",
2163 java_libs: ["libfoo"],
2164 min_sdk_version: "29",
2165 }
2166
2167 apex_key {
2168 name: "myapex.key",
2169 public_key: "testkey.avbpubkey",
2170 private_key: "testkey.pem",
2171 }
2172
2173 java_import {
2174 name: "libfoo",
2175 jars: ["libfoo.jar"],
2176 apex_available: [
2177 "myapex",
2178 ],
2179 min_sdk_version: "30",
2180 }
2181 `)
Spandan Das7fa982c2023-02-24 18:38:56 +00002182
2183 // Skip check for modules compiling against core API surface
2184 testApex(t, `
2185 apex {
2186 name: "myapex",
2187 key: "myapex.key",
2188 java_libs: ["libfoo"],
2189 min_sdk_version: "29",
2190 }
2191
2192 apex_key {
2193 name: "myapex.key",
2194 public_key: "testkey.avbpubkey",
2195 private_key: "testkey.pem",
2196 }
2197
2198 java_library {
2199 name: "libfoo",
2200 srcs: ["Foo.java"],
2201 apex_available: [
2202 "myapex",
2203 ],
2204 // Compile against core API surface
2205 sdk_version: "core_current",
2206 min_sdk_version: "30",
2207 }
2208 `)
2209
Jooyung Han749dc692020-04-15 11:03:39 +09002210}
2211
2212func TestApexMinSdkVersion_Okay(t *testing.T) {
2213 testApex(t, `
2214 apex {
2215 name: "myapex",
2216 key: "myapex.key",
2217 native_shared_libs: ["libfoo"],
2218 java_libs: ["libbar"],
2219 min_sdk_version: "29",
2220 }
2221
2222 apex_key {
2223 name: "myapex.key",
2224 public_key: "testkey.avbpubkey",
2225 private_key: "testkey.pem",
2226 }
2227
2228 cc_library {
2229 name: "libfoo",
2230 srcs: ["mylib.cpp"],
2231 shared_libs: ["libfoo_dep"],
2232 apex_available: ["myapex"],
2233 min_sdk_version: "29",
2234 }
2235
2236 cc_library {
2237 name: "libfoo_dep",
2238 srcs: ["mylib.cpp"],
2239 apex_available: ["myapex"],
2240 min_sdk_version: "29",
2241 }
2242
2243 java_library {
2244 name: "libbar",
2245 sdk_version: "current",
2246 srcs: ["a.java"],
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002247 static_libs: [
2248 "libbar_dep",
2249 "libbar_import_dep",
2250 ],
Jooyung Han749dc692020-04-15 11:03:39 +09002251 apex_available: ["myapex"],
2252 min_sdk_version: "29",
2253 }
2254
2255 java_library {
2256 name: "libbar_dep",
2257 sdk_version: "current",
2258 srcs: ["a.java"],
2259 apex_available: ["myapex"],
2260 min_sdk_version: "29",
2261 }
Jaewoong Jung56e12db2021-04-02 00:38:25 +00002262
2263 java_import {
2264 name: "libbar_import_dep",
2265 jars: ["libbar.jar"],
2266 apex_available: ["myapex"],
2267 min_sdk_version: "29",
2268 }
Jooyung Han03b51852020-02-26 22:45:42 +09002269 `)
2270}
2271
Colin Cross8ca61c12022-10-06 21:00:14 -07002272func TestApexMinSdkVersion_MinApiForArch(t *testing.T) {
2273 // Tests that an apex dependency with min_sdk_version higher than the
2274 // min_sdk_version of the apex is allowed as long as the dependency's
2275 // min_sdk_version is less than or equal to the api level that the
2276 // architecture was introduced in. In this case, arm64 didn't exist
2277 // until api level 21, so the arm64 code will never need to run on
2278 // an api level 20 device, even if other architectures of the apex
2279 // will.
2280 testApex(t, `
2281 apex {
2282 name: "myapex",
2283 key: "myapex.key",
2284 native_shared_libs: ["libfoo"],
2285 min_sdk_version: "20",
2286 }
2287
2288 apex_key {
2289 name: "myapex.key",
2290 public_key: "testkey.avbpubkey",
2291 private_key: "testkey.pem",
2292 }
2293
2294 cc_library {
2295 name: "libfoo",
2296 srcs: ["mylib.cpp"],
2297 apex_available: ["myapex"],
2298 min_sdk_version: "21",
2299 stl: "none",
2300 }
2301 `)
2302}
2303
Artur Satayev8cf899a2020-04-15 17:29:42 +01002304func TestJavaStableSdkVersion(t *testing.T) {
2305 testCases := []struct {
2306 name string
2307 expectedError string
2308 bp string
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002309 preparer android.FixturePreparer
Artur Satayev8cf899a2020-04-15 17:29:42 +01002310 }{
2311 {
2312 name: "Non-updatable apex with non-stable dep",
2313 bp: `
2314 apex {
2315 name: "myapex",
2316 java_libs: ["myjar"],
2317 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002318 updatable: false,
Artur Satayev8cf899a2020-04-15 17:29:42 +01002319 }
2320 apex_key {
2321 name: "myapex.key",
2322 public_key: "testkey.avbpubkey",
2323 private_key: "testkey.pem",
2324 }
2325 java_library {
2326 name: "myjar",
2327 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002328 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002329 apex_available: ["myapex"],
2330 }
2331 `,
2332 },
2333 {
2334 name: "Updatable apex with stable dep",
2335 bp: `
2336 apex {
2337 name: "myapex",
2338 java_libs: ["myjar"],
2339 key: "myapex.key",
2340 updatable: true,
2341 min_sdk_version: "29",
2342 }
2343 apex_key {
2344 name: "myapex.key",
2345 public_key: "testkey.avbpubkey",
2346 private_key: "testkey.pem",
2347 }
2348 java_library {
2349 name: "myjar",
2350 srcs: ["foo/bar/MyClass.java"],
2351 sdk_version: "current",
2352 apex_available: ["myapex"],
Jooyung Han749dc692020-04-15 11:03:39 +09002353 min_sdk_version: "29",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002354 }
2355 `,
2356 },
2357 {
2358 name: "Updatable apex with non-stable dep",
2359 expectedError: "cannot depend on \"myjar\"",
2360 bp: `
2361 apex {
2362 name: "myapex",
2363 java_libs: ["myjar"],
2364 key: "myapex.key",
2365 updatable: true,
2366 }
2367 apex_key {
2368 name: "myapex.key",
2369 public_key: "testkey.avbpubkey",
2370 private_key: "testkey.pem",
2371 }
2372 java_library {
2373 name: "myjar",
2374 srcs: ["foo/bar/MyClass.java"],
Paul Duffin043f5e72021-03-05 00:00:01 +00002375 sdk_version: "test_current",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002376 apex_available: ["myapex"],
2377 }
2378 `,
2379 },
2380 {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002381 name: "Updatable apex with non-stable legacy core platform dep",
2382 expectedError: `\Qcannot depend on "myjar-uses-legacy": non stable SDK core_platform_current - uses legacy core platform\E`,
2383 bp: `
2384 apex {
2385 name: "myapex",
2386 java_libs: ["myjar-uses-legacy"],
2387 key: "myapex.key",
2388 updatable: true,
2389 }
2390 apex_key {
2391 name: "myapex.key",
2392 public_key: "testkey.avbpubkey",
2393 private_key: "testkey.pem",
2394 }
2395 java_library {
2396 name: "myjar-uses-legacy",
2397 srcs: ["foo/bar/MyClass.java"],
2398 sdk_version: "core_platform",
2399 apex_available: ["myapex"],
2400 }
2401 `,
2402 preparer: java.FixtureUseLegacyCorePlatformApi("myjar-uses-legacy"),
2403 },
2404 {
Paul Duffin043f5e72021-03-05 00:00:01 +00002405 name: "Updatable apex with non-stable transitive dep",
2406 // This is not actually detecting that the transitive dependency is unstable, rather it is
2407 // detecting that the transitive dependency is building against a wider API surface than the
2408 // module that depends on it is using.
Jiyong Park670e0f62021-02-18 13:10:18 +09002409 expectedError: "compiles against Android API, but dependency \"transitive-jar\" is compiling against private API.",
Artur Satayev8cf899a2020-04-15 17:29:42 +01002410 bp: `
2411 apex {
2412 name: "myapex",
2413 java_libs: ["myjar"],
2414 key: "myapex.key",
2415 updatable: true,
2416 }
2417 apex_key {
2418 name: "myapex.key",
2419 public_key: "testkey.avbpubkey",
2420 private_key: "testkey.pem",
2421 }
2422 java_library {
2423 name: "myjar",
2424 srcs: ["foo/bar/MyClass.java"],
2425 sdk_version: "current",
2426 apex_available: ["myapex"],
2427 static_libs: ["transitive-jar"],
2428 }
2429 java_library {
2430 name: "transitive-jar",
2431 srcs: ["foo/bar/MyClass.java"],
2432 sdk_version: "core_platform",
2433 apex_available: ["myapex"],
2434 }
2435 `,
2436 },
2437 }
2438
2439 for _, test := range testCases {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002440 if test.name != "Updatable apex with non-stable legacy core platform dep" {
2441 continue
2442 }
Artur Satayev8cf899a2020-04-15 17:29:42 +01002443 t.Run(test.name, func(t *testing.T) {
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002444 errorHandler := android.FixtureExpectsNoErrors
2445 if test.expectedError != "" {
2446 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002447 }
Paul Duffin1ea7c9f2021-03-15 09:39:13 +00002448 android.GroupFixturePreparers(
2449 java.PrepareForTestWithJavaDefaultModules,
2450 PrepareForTestWithApexBuildComponents,
2451 prepareForTestWithMyapex,
2452 android.OptionalFixturePreparer(test.preparer),
2453 ).
2454 ExtendWithErrorHandler(errorHandler).
2455 RunTestWithBp(t, test.bp)
Artur Satayev8cf899a2020-04-15 17:29:42 +01002456 })
2457 }
2458}
2459
Jooyung Han749dc692020-04-15 11:03:39 +09002460func TestApexMinSdkVersion_ErrorIfDepIsNewer(t *testing.T) {
2461 testApexError(t, `module "mylib2".*: should support min_sdk_version\(29\) for "myapex"`, `
2462 apex {
2463 name: "myapex",
2464 key: "myapex.key",
2465 native_shared_libs: ["mylib"],
2466 min_sdk_version: "29",
2467 }
2468
2469 apex_key {
2470 name: "myapex.key",
2471 public_key: "testkey.avbpubkey",
2472 private_key: "testkey.pem",
2473 }
2474
2475 cc_library {
2476 name: "mylib",
2477 srcs: ["mylib.cpp"],
2478 shared_libs: ["mylib2"],
2479 system_shared_libs: [],
2480 stl: "none",
2481 apex_available: [
2482 "myapex",
2483 ],
2484 min_sdk_version: "29",
2485 }
2486
2487 // indirect part of the apex
2488 cc_library {
2489 name: "mylib2",
2490 srcs: ["mylib.cpp"],
2491 system_shared_libs: [],
2492 stl: "none",
2493 apex_available: [
2494 "myapex",
2495 ],
2496 min_sdk_version: "30",
2497 }
2498 `)
2499}
2500
2501func TestApexMinSdkVersion_ErrorIfDepIsNewer_Java(t *testing.T) {
2502 testApexError(t, `module "bar".*: should support min_sdk_version\(29\) for "myapex"`, `
2503 apex {
2504 name: "myapex",
2505 key: "myapex.key",
2506 apps: ["AppFoo"],
2507 min_sdk_version: "29",
Spandan Das42e89502022-05-06 22:12:55 +00002508 updatable: false,
Jooyung Han749dc692020-04-15 11:03:39 +09002509 }
2510
2511 apex_key {
2512 name: "myapex.key",
2513 public_key: "testkey.avbpubkey",
2514 private_key: "testkey.pem",
2515 }
2516
2517 android_app {
2518 name: "AppFoo",
2519 srcs: ["foo/bar/MyClass.java"],
2520 sdk_version: "current",
2521 min_sdk_version: "29",
2522 system_modules: "none",
2523 stl: "none",
2524 static_libs: ["bar"],
2525 apex_available: [ "myapex" ],
2526 }
2527
2528 java_library {
2529 name: "bar",
2530 sdk_version: "current",
2531 srcs: ["a.java"],
2532 apex_available: [ "myapex" ],
2533 }
2534 `)
2535}
2536
2537func TestApexMinSdkVersion_OkayEvenWhenDepIsNewer_IfItSatisfiesApexMinSdkVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002538 ctx := testApex(t, `
Jooyung Han749dc692020-04-15 11:03:39 +09002539 apex {
2540 name: "myapex",
2541 key: "myapex.key",
2542 native_shared_libs: ["mylib"],
2543 min_sdk_version: "29",
2544 }
2545
2546 apex_key {
2547 name: "myapex.key",
2548 public_key: "testkey.avbpubkey",
2549 private_key: "testkey.pem",
2550 }
2551
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002552 // mylib in myapex will link to mylib2#current
Jooyung Han749dc692020-04-15 11:03:39 +09002553 // mylib in otherapex will link to mylib2(non-stub) in otherapex as well
2554 cc_library {
2555 name: "mylib",
2556 srcs: ["mylib.cpp"],
2557 shared_libs: ["mylib2"],
2558 system_shared_libs: [],
2559 stl: "none",
2560 apex_available: ["myapex", "otherapex"],
2561 min_sdk_version: "29",
2562 }
2563
2564 cc_library {
2565 name: "mylib2",
2566 srcs: ["mylib.cpp"],
2567 system_shared_libs: [],
2568 stl: "none",
2569 apex_available: ["otherapex"],
2570 stubs: { versions: ["29", "30"] },
2571 min_sdk_version: "30",
2572 }
2573
2574 apex {
2575 name: "otherapex",
2576 key: "myapex.key",
2577 native_shared_libs: ["mylib", "mylib2"],
2578 min_sdk_version: "30",
2579 }
2580 `)
2581 expectLink := func(from, from_variant, to, to_variant string) {
2582 ld := ctx.ModuleForTests(from, "android_arm64_armv8-a_"+from_variant).Rule("ld")
2583 libFlags := ld.Args["libFlags"]
2584 ensureContains(t, libFlags, "android_arm64_armv8-a_"+to_variant+"/"+to+".so")
2585 }
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002586 expectLink("mylib", "shared_apex29", "mylib2", "shared_current")
Colin Crossaede88c2020-08-11 12:17:01 -07002587 expectLink("mylib", "shared_apex30", "mylib2", "shared_apex30")
Jooyung Han749dc692020-04-15 11:03:39 +09002588}
2589
Jooyung Haned124c32021-01-26 11:43:46 +09002590func TestApexMinSdkVersion_WorksWithSdkCodename(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002591 withSAsActiveCodeNames := android.FixtureModifyProductVariables(
2592 func(variables android.FixtureProductVariables) {
2593 variables.Platform_sdk_codename = proptools.StringPtr("S")
2594 variables.Platform_version_active_codenames = []string{"S"}
2595 },
2596 )
Jooyung Haned124c32021-01-26 11:43:46 +09002597 testApexError(t, `libbar.*: should support min_sdk_version\(S\)`, `
2598 apex {
2599 name: "myapex",
2600 key: "myapex.key",
2601 native_shared_libs: ["libfoo"],
2602 min_sdk_version: "S",
2603 }
2604 apex_key {
2605 name: "myapex.key",
2606 public_key: "testkey.avbpubkey",
2607 private_key: "testkey.pem",
2608 }
2609 cc_library {
2610 name: "libfoo",
2611 shared_libs: ["libbar"],
2612 apex_available: ["myapex"],
2613 min_sdk_version: "29",
2614 }
2615 cc_library {
2616 name: "libbar",
2617 apex_available: ["myapex"],
2618 }
2619 `, withSAsActiveCodeNames)
2620}
2621
2622func TestApexMinSdkVersion_WorksWithActiveCodenames(t *testing.T) {
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002623 withSAsActiveCodeNames := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
2624 variables.Platform_sdk_codename = proptools.StringPtr("S")
2625 variables.Platform_version_active_codenames = []string{"S", "T"}
2626 })
Colin Cross1c460562021-02-16 17:55:47 -08002627 ctx := testApex(t, `
Jooyung Haned124c32021-01-26 11:43:46 +09002628 apex {
2629 name: "myapex",
2630 key: "myapex.key",
2631 native_shared_libs: ["libfoo"],
2632 min_sdk_version: "S",
2633 }
2634 apex_key {
2635 name: "myapex.key",
2636 public_key: "testkey.avbpubkey",
2637 private_key: "testkey.pem",
2638 }
2639 cc_library {
2640 name: "libfoo",
2641 shared_libs: ["libbar"],
2642 apex_available: ["myapex"],
2643 min_sdk_version: "S",
2644 }
2645 cc_library {
2646 name: "libbar",
2647 stubs: {
2648 symbol_file: "libbar.map.txt",
2649 versions: ["30", "S", "T"],
2650 },
2651 }
2652 `, withSAsActiveCodeNames)
2653
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002654 // ensure libfoo is linked with current version of libbar stub
Jooyung Haned124c32021-01-26 11:43:46 +09002655 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared_apex10000")
2656 libFlags := libfoo.Rule("ld").Args["libFlags"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002657 ensureContains(t, libFlags, "android_arm64_armv8-a_shared_current/libbar.so")
Jooyung Haned124c32021-01-26 11:43:46 +09002658}
2659
Jiyong Park7c2ee712018-12-07 00:42:25 +09002660func TestFilesInSubDir(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002661 ctx := testApex(t, `
Jiyong Park7c2ee712018-12-07 00:42:25 +09002662 apex {
2663 name: "myapex",
2664 key: "myapex.key",
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002665 native_shared_libs: ["mylib"],
2666 binaries: ["mybin"],
Jiyong Park7c2ee712018-12-07 00:42:25 +09002667 prebuilts: ["myetc"],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002668 compile_multilib: "both",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002669 updatable: false,
Jiyong Park7c2ee712018-12-07 00:42:25 +09002670 }
2671
2672 apex_key {
2673 name: "myapex.key",
2674 public_key: "testkey.avbpubkey",
2675 private_key: "testkey.pem",
2676 }
2677
2678 prebuilt_etc {
2679 name: "myetc",
2680 src: "myprebuilt",
2681 sub_dir: "foo/bar",
2682 }
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002683
2684 cc_library {
2685 name: "mylib",
2686 srcs: ["mylib.cpp"],
2687 relative_install_path: "foo/bar",
2688 system_shared_libs: [],
2689 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002690 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002691 }
2692
2693 cc_binary {
2694 name: "mybin",
2695 srcs: ["mylib.cpp"],
2696 relative_install_path: "foo/bar",
2697 system_shared_libs: [],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002698 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00002699 apex_available: [ "myapex" ],
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002700 }
Jiyong Park7c2ee712018-12-07 00:42:25 +09002701 `)
2702
Sundong Ahnabb64432019-10-22 13:58:29 +09002703 generateFsRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("generateFsConfig")
Jiyong Park1b0893e2021-12-13 23:40:17 +09002704 cmd := generateFsRule.RuleParams.Command
Jiyong Park7c2ee712018-12-07 00:42:25 +09002705
Jiyong Parkb7c24df2019-02-01 12:03:59 +09002706 // Ensure that the subdirectories are all listed
Jiyong Park1b0893e2021-12-13 23:40:17 +09002707 ensureContains(t, cmd, "/etc ")
2708 ensureContains(t, cmd, "/etc/foo ")
2709 ensureContains(t, cmd, "/etc/foo/bar ")
2710 ensureContains(t, cmd, "/lib64 ")
2711 ensureContains(t, cmd, "/lib64/foo ")
2712 ensureContains(t, cmd, "/lib64/foo/bar ")
2713 ensureContains(t, cmd, "/lib ")
2714 ensureContains(t, cmd, "/lib/foo ")
2715 ensureContains(t, cmd, "/lib/foo/bar ")
2716 ensureContains(t, cmd, "/bin ")
2717 ensureContains(t, cmd, "/bin/foo ")
2718 ensureContains(t, cmd, "/bin/foo/bar ")
Jiyong Park7c2ee712018-12-07 00:42:25 +09002719}
Jiyong Parkda6eb592018-12-19 17:12:36 +09002720
Jooyung Han35155c42020-02-06 17:33:20 +09002721func TestFilesInSubDirWhenNativeBridgeEnabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08002722 ctx := testApex(t, `
Jooyung Han35155c42020-02-06 17:33:20 +09002723 apex {
2724 name: "myapex",
2725 key: "myapex.key",
2726 multilib: {
2727 both: {
2728 native_shared_libs: ["mylib"],
2729 binaries: ["mybin"],
2730 },
2731 },
2732 compile_multilib: "both",
2733 native_bridge_supported: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002734 updatable: false,
Jooyung Han35155c42020-02-06 17:33:20 +09002735 }
2736
2737 apex_key {
2738 name: "myapex.key",
2739 public_key: "testkey.avbpubkey",
2740 private_key: "testkey.pem",
2741 }
2742
2743 cc_library {
2744 name: "mylib",
2745 relative_install_path: "foo/bar",
2746 system_shared_libs: [],
2747 stl: "none",
2748 apex_available: [ "myapex" ],
2749 native_bridge_supported: true,
2750 }
2751
2752 cc_binary {
2753 name: "mybin",
2754 relative_install_path: "foo/bar",
2755 system_shared_libs: [],
Jooyung Han35155c42020-02-06 17:33:20 +09002756 stl: "none",
2757 apex_available: [ "myapex" ],
2758 native_bridge_supported: true,
2759 compile_multilib: "both", // default is "first" for binary
2760 multilib: {
2761 lib64: {
2762 suffix: "64",
2763 },
2764 },
2765 }
2766 `, withNativeBridgeEnabled)
2767 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
2768 "bin/foo/bar/mybin",
2769 "bin/foo/bar/mybin64",
2770 "bin/arm/foo/bar/mybin",
2771 "bin/arm64/foo/bar/mybin64",
2772 "lib/foo/bar/mylib.so",
2773 "lib/arm/foo/bar/mylib.so",
2774 "lib64/foo/bar/mylib.so",
2775 "lib64/arm64/foo/bar/mylib.so",
2776 })
2777}
2778
Jooyung Han85d61762020-06-24 23:50:26 +09002779func TestVendorApex(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -08002780 result := android.GroupFixturePreparers(
2781 prepareForApexTest,
2782 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
2783 ).RunTestWithBp(t, `
Jooyung Han85d61762020-06-24 23:50:26 +09002784 apex {
2785 name: "myapex",
2786 key: "myapex.key",
2787 binaries: ["mybin"],
2788 vendor: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002789 updatable: false,
Jooyung Han85d61762020-06-24 23:50:26 +09002790 }
2791 apex_key {
2792 name: "myapex.key",
2793 public_key: "testkey.avbpubkey",
2794 private_key: "testkey.pem",
2795 }
2796 cc_binary {
2797 name: "mybin",
2798 vendor: true,
2799 shared_libs: ["libfoo"],
2800 }
2801 cc_library {
2802 name: "libfoo",
2803 proprietary: true,
2804 }
2805 `)
2806
Colin Crossc68db4b2021-11-11 18:59:15 -08002807 ensureExactContents(t, result.TestContext, "myapex", "android_common_myapex_image", []string{
Jooyung Han85d61762020-06-24 23:50:26 +09002808 "bin/mybin",
2809 "lib64/libfoo.so",
2810 // TODO(b/159195575): Add an option to use VNDK libs from VNDK APEX
2811 "lib64/libc++.so",
2812 })
2813
Colin Crossc68db4b2021-11-11 18:59:15 -08002814 apexBundle := result.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
2815 data := android.AndroidMkDataForTest(t, result.TestContext, apexBundle)
Jooyung Han85d61762020-06-24 23:50:26 +09002816 name := apexBundle.BaseModuleName()
2817 prefix := "TARGET_"
2818 var builder strings.Builder
2819 data.Custom(&builder, name, prefix, "", data)
Colin Crossc68db4b2021-11-11 18:59:15 -08002820 androidMk := android.StringRelativeToTop(result.Config, builder.String())
Paul Duffin37ba3442021-03-29 00:21:08 +01002821 installPath := "out/target/product/test_device/vendor/apex"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01002822 ensureContains(t, androidMk, "LOCAL_MODULE_PATH := "+installPath)
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002823
Colin Crossc68db4b2021-11-11 18:59:15 -08002824 apexManifestRule := result.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
Jooyung Han6c4cc9c2020-07-29 16:00:54 +09002825 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2826 ensureListNotContains(t, requireNativeLibs, ":vndk")
Jooyung Han85d61762020-06-24 23:50:26 +09002827}
2828
Jooyung Hanc5a96762022-02-04 11:54:50 +09002829func TestVendorApex_use_vndk_as_stable_TryingToIncludeVNDKLib(t *testing.T) {
2830 testApexError(t, `Trying to include a VNDK library`, `
2831 apex {
2832 name: "myapex",
2833 key: "myapex.key",
2834 native_shared_libs: ["libc++"], // libc++ is a VNDK lib
2835 vendor: true,
2836 use_vndk_as_stable: true,
2837 updatable: false,
2838 }
2839 apex_key {
2840 name: "myapex.key",
2841 public_key: "testkey.avbpubkey",
2842 private_key: "testkey.pem",
2843 }`)
2844}
2845
Jooyung Handf78e212020-07-22 15:54:47 +09002846func TestVendorApex_use_vndk_as_stable(t *testing.T) {
Jooyung Han91f92032022-02-04 12:36:33 +09002847 // myapex myapex2
2848 // | |
2849 // mybin ------. mybin2
2850 // \ \ / |
2851 // (stable) .---\--------` |
2852 // \ / \ |
2853 // \ / \ /
2854 // libvndk libvendor
2855 // (vndk)
Colin Cross1c460562021-02-16 17:55:47 -08002856 ctx := testApex(t, `
Jooyung Handf78e212020-07-22 15:54:47 +09002857 apex {
2858 name: "myapex",
2859 key: "myapex.key",
2860 binaries: ["mybin"],
2861 vendor: true,
2862 use_vndk_as_stable: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00002863 updatable: false,
Jooyung Handf78e212020-07-22 15:54:47 +09002864 }
2865 apex_key {
2866 name: "myapex.key",
2867 public_key: "testkey.avbpubkey",
2868 private_key: "testkey.pem",
2869 }
2870 cc_binary {
2871 name: "mybin",
2872 vendor: true,
2873 shared_libs: ["libvndk", "libvendor"],
2874 }
2875 cc_library {
2876 name: "libvndk",
2877 vndk: {
2878 enabled: true,
2879 },
2880 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002881 product_available: true,
Jooyung Handf78e212020-07-22 15:54:47 +09002882 }
2883 cc_library {
2884 name: "libvendor",
2885 vendor: true,
Jooyung Han91f92032022-02-04 12:36:33 +09002886 stl: "none",
2887 }
2888 apex {
2889 name: "myapex2",
2890 key: "myapex.key",
2891 binaries: ["mybin2"],
2892 vendor: true,
2893 use_vndk_as_stable: false,
2894 updatable: false,
2895 }
2896 cc_binary {
2897 name: "mybin2",
2898 vendor: true,
2899 shared_libs: ["libvndk", "libvendor"],
Jooyung Handf78e212020-07-22 15:54:47 +09002900 }
2901 `)
2902
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002903 vendorVariant := "android_vendor.29_arm64_armv8-a"
Jooyung Handf78e212020-07-22 15:54:47 +09002904
Jooyung Han91f92032022-02-04 12:36:33 +09002905 for _, tc := range []struct {
2906 name string
2907 apexName string
2908 moduleName string
2909 moduleVariant string
2910 libs []string
2911 contents []string
2912 requireVndkNamespace bool
2913 }{
2914 {
2915 name: "use_vndk_as_stable",
2916 apexName: "myapex",
2917 moduleName: "mybin",
2918 moduleVariant: vendorVariant + "_apex10000",
2919 libs: []string{
2920 // should link with vendor variants of VNDK libs(libvndk/libc++)
2921 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared/libvndk.so",
2922 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared/libc++.so",
2923 // unstable Vendor libs as APEX variant
2924 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2925 },
2926 contents: []string{
2927 "bin/mybin",
2928 "lib64/libvendor.so",
2929 // VNDK libs (libvndk/libc++) are not included
2930 },
2931 requireVndkNamespace: true,
2932 },
2933 {
2934 name: "!use_vndk_as_stable",
2935 apexName: "myapex2",
2936 moduleName: "mybin2",
2937 moduleVariant: vendorVariant + "_myapex2",
2938 libs: []string{
2939 // should link with "unique" APEX(myapex2) variant of VNDK libs(libvndk/libc++)
2940 "out/soong/.intermediates/libvndk/" + vendorVariant + "_shared_myapex2/libvndk.so",
2941 "out/soong/.intermediates/" + cc.DefaultCcCommonTestModulesDir + "libc++/" + vendorVariant + "_shared_myapex2/libc++.so",
2942 // unstable vendor libs have "merged" APEX variants
2943 "out/soong/.intermediates/libvendor/" + vendorVariant + "_shared_apex10000/libvendor.so",
2944 },
2945 contents: []string{
2946 "bin/mybin2",
2947 "lib64/libvendor.so",
2948 // VNDK libs are included as well
2949 "lib64/libvndk.so",
2950 "lib64/libc++.so",
2951 },
2952 requireVndkNamespace: false,
2953 },
2954 } {
2955 t.Run(tc.name, func(t *testing.T) {
2956 // Check linked libs
2957 ldRule := ctx.ModuleForTests(tc.moduleName, tc.moduleVariant).Rule("ld")
2958 libs := names(ldRule.Args["libFlags"])
2959 for _, lib := range tc.libs {
2960 ensureListContains(t, libs, lib)
2961 }
2962 // Check apex contents
2963 ensureExactContents(t, ctx, tc.apexName, "android_common_"+tc.apexName+"_image", tc.contents)
Jooyung Handf78e212020-07-22 15:54:47 +09002964
Jooyung Han91f92032022-02-04 12:36:33 +09002965 // Check "requireNativeLibs"
2966 apexManifestRule := ctx.ModuleForTests(tc.apexName, "android_common_"+tc.apexName+"_image").Rule("apexManifestRule")
2967 requireNativeLibs := names(apexManifestRule.Args["requireNativeLibs"])
2968 if tc.requireVndkNamespace {
2969 ensureListContains(t, requireNativeLibs, ":vndk")
2970 } else {
2971 ensureListNotContains(t, requireNativeLibs, ":vndk")
2972 }
2973 })
2974 }
Jooyung Handf78e212020-07-22 15:54:47 +09002975}
2976
Justin Yun13decfb2021-03-08 19:25:55 +09002977func TestProductVariant(t *testing.T) {
2978 ctx := testApex(t, `
2979 apex {
2980 name: "myapex",
2981 key: "myapex.key",
2982 updatable: false,
2983 product_specific: true,
2984 binaries: ["foo"],
2985 }
2986
2987 apex_key {
2988 name: "myapex.key",
2989 public_key: "testkey.avbpubkey",
2990 private_key: "testkey.pem",
2991 }
2992
2993 cc_binary {
2994 name: "foo",
2995 product_available: true,
2996 apex_available: ["myapex"],
2997 srcs: ["foo.cpp"],
2998 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00002999 `, android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3000 variables.ProductVndkVersion = proptools.StringPtr("current")
3001 }),
3002 )
Justin Yun13decfb2021-03-08 19:25:55 +09003003
3004 cflags := strings.Fields(
Jooyung Han91f92032022-02-04 12:36:33 +09003005 ctx.ModuleForTests("foo", "android_product.29_arm64_armv8-a_myapex").Rule("cc").Args["cFlags"])
Justin Yun13decfb2021-03-08 19:25:55 +09003006 ensureListContains(t, cflags, "-D__ANDROID_VNDK__")
3007 ensureListContains(t, cflags, "-D__ANDROID_APEX__")
3008 ensureListContains(t, cflags, "-D__ANDROID_PRODUCT__")
3009 ensureListNotContains(t, cflags, "-D__ANDROID_VENDOR__")
3010}
3011
Jooyung Han8e5685d2020-09-21 11:02:57 +09003012func TestApex_withPrebuiltFirmware(t *testing.T) {
3013 testCases := []struct {
3014 name string
3015 additionalProp string
3016 }{
3017 {"system apex with prebuilt_firmware", ""},
3018 {"vendor apex with prebuilt_firmware", "vendor: true,"},
3019 }
3020 for _, tc := range testCases {
3021 t.Run(tc.name, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003022 ctx := testApex(t, `
Jooyung Han8e5685d2020-09-21 11:02:57 +09003023 apex {
3024 name: "myapex",
3025 key: "myapex.key",
3026 prebuilts: ["myfirmware"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003027 updatable: false,
Jooyung Han8e5685d2020-09-21 11:02:57 +09003028 `+tc.additionalProp+`
3029 }
3030 apex_key {
3031 name: "myapex.key",
3032 public_key: "testkey.avbpubkey",
3033 private_key: "testkey.pem",
3034 }
3035 prebuilt_firmware {
3036 name: "myfirmware",
3037 src: "myfirmware.bin",
3038 filename_from_src: true,
3039 `+tc.additionalProp+`
3040 }
3041 `)
3042 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
3043 "etc/firmware/myfirmware.bin",
3044 })
3045 })
3046 }
Jooyung Han0703fd82020-08-26 22:11:53 +09003047}
3048
Jooyung Hanefb184e2020-06-25 17:14:25 +09003049func TestAndroidMk_VendorApexRequired(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003050 ctx := testApex(t, `
Jooyung Hanefb184e2020-06-25 17:14:25 +09003051 apex {
3052 name: "myapex",
3053 key: "myapex.key",
3054 vendor: true,
3055 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003056 updatable: false,
Jooyung Hanefb184e2020-06-25 17:14:25 +09003057 }
3058
3059 apex_key {
3060 name: "myapex.key",
3061 public_key: "testkey.avbpubkey",
3062 private_key: "testkey.pem",
3063 }
3064
3065 cc_library {
3066 name: "mylib",
3067 vendor_available: true,
3068 }
3069 `)
3070
3071 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003072 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Hanefb184e2020-06-25 17:14:25 +09003073 name := apexBundle.BaseModuleName()
3074 prefix := "TARGET_"
3075 var builder strings.Builder
3076 data.Custom(&builder, name, prefix, "", data)
3077 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00003078 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := libc++.vendor.myapex:64 mylib.vendor.myapex:64 apex_manifest.pb.myapex apex_pubkey.myapex libc.vendor libm.vendor libdl.vendor\n")
Jooyung Hanefb184e2020-06-25 17:14:25 +09003079}
3080
Jooyung Han2ed99d02020-06-24 23:26:26 +09003081func TestAndroidMkWritesCommonProperties(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003082 ctx := testApex(t, `
Jooyung Han2ed99d02020-06-24 23:26:26 +09003083 apex {
3084 name: "myapex",
3085 key: "myapex.key",
3086 vintf_fragments: ["fragment.xml"],
3087 init_rc: ["init.rc"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003088 updatable: false,
Jooyung Han2ed99d02020-06-24 23:26:26 +09003089 }
3090 apex_key {
3091 name: "myapex.key",
3092 public_key: "testkey.avbpubkey",
3093 private_key: "testkey.pem",
3094 }
3095 cc_binary {
3096 name: "mybin",
3097 }
3098 `)
3099
3100 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07003101 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jooyung Han2ed99d02020-06-24 23:26:26 +09003102 name := apexBundle.BaseModuleName()
3103 prefix := "TARGET_"
3104 var builder strings.Builder
3105 data.Custom(&builder, name, prefix, "", data)
3106 androidMk := builder.String()
Liz Kammer7b3dc8a2021-04-16 16:41:59 -04003107 ensureContains(t, androidMk, "LOCAL_FULL_VINTF_FRAGMENTS := fragment.xml\n")
Liz Kammer0c4f71c2021-04-06 10:35:10 -04003108 ensureContains(t, androidMk, "LOCAL_FULL_INIT_RC := init.rc\n")
Jooyung Han2ed99d02020-06-24 23:26:26 +09003109}
3110
Jiyong Park16e91a02018-12-20 18:18:08 +09003111func TestStaticLinking(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003112 ctx := testApex(t, `
Jiyong Park16e91a02018-12-20 18:18:08 +09003113 apex {
3114 name: "myapex",
3115 key: "myapex.key",
3116 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003117 updatable: false,
Jiyong Park16e91a02018-12-20 18:18:08 +09003118 }
3119
3120 apex_key {
3121 name: "myapex.key",
3122 public_key: "testkey.avbpubkey",
3123 private_key: "testkey.pem",
3124 }
3125
3126 cc_library {
3127 name: "mylib",
3128 srcs: ["mylib.cpp"],
3129 system_shared_libs: [],
3130 stl: "none",
3131 stubs: {
3132 versions: ["1", "2", "3"],
3133 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003134 apex_available: [
3135 "//apex_available:platform",
3136 "myapex",
3137 ],
Jiyong Park16e91a02018-12-20 18:18:08 +09003138 }
3139
3140 cc_binary {
3141 name: "not_in_apex",
3142 srcs: ["mylib.cpp"],
3143 static_libs: ["mylib"],
3144 static_executable: true,
3145 system_shared_libs: [],
3146 stl: "none",
3147 }
Jiyong Park16e91a02018-12-20 18:18:08 +09003148 `)
3149
Colin Cross7113d202019-11-20 16:39:12 -08003150 ldFlags := ctx.ModuleForTests("not_in_apex", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
Jiyong Park16e91a02018-12-20 18:18:08 +09003151
3152 // Ensure that not_in_apex is linking with the static variant of mylib
Colin Cross7113d202019-11-20 16:39:12 -08003153 ensureContains(t, ldFlags, "mylib/android_arm64_armv8-a_static/mylib.a")
Jiyong Park16e91a02018-12-20 18:18:08 +09003154}
Jiyong Park9335a262018-12-24 11:31:58 +09003155
3156func TestKeys(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003157 ctx := testApex(t, `
Jiyong Park9335a262018-12-24 11:31:58 +09003158 apex {
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003159 name: "myapex_keytest",
Jiyong Park9335a262018-12-24 11:31:58 +09003160 key: "myapex.key",
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003161 certificate: ":myapex.certificate",
Jiyong Park9335a262018-12-24 11:31:58 +09003162 native_shared_libs: ["mylib"],
Jooyung Han54aca7b2019-11-20 02:26:02 +09003163 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003164 updatable: false,
Jiyong Park9335a262018-12-24 11:31:58 +09003165 }
3166
3167 cc_library {
3168 name: "mylib",
3169 srcs: ["mylib.cpp"],
3170 system_shared_libs: [],
3171 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003172 apex_available: [ "myapex_keytest" ],
Jiyong Park9335a262018-12-24 11:31:58 +09003173 }
3174
3175 apex_key {
3176 name: "myapex.key",
3177 public_key: "testkey.avbpubkey",
3178 private_key: "testkey.pem",
3179 }
3180
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003181 android_app_certificate {
3182 name: "myapex.certificate",
3183 certificate: "testkey",
3184 }
3185
3186 android_app_certificate {
3187 name: "myapex.certificate.override",
3188 certificate: "testkey.override",
3189 }
3190
Jiyong Park9335a262018-12-24 11:31:58 +09003191 `)
3192
3193 // check the APEX keys
Jiyong Parkd1e293d2019-03-15 02:13:21 +09003194 keys := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
Jiyong Park9335a262018-12-24 11:31:58 +09003195
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003196 if keys.publicKeyFile.String() != "vendor/foo/devkeys/testkey.avbpubkey" {
3197 t.Errorf("public key %q is not %q", keys.publicKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003198 "vendor/foo/devkeys/testkey.avbpubkey")
3199 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -08003200 if keys.privateKeyFile.String() != "vendor/foo/devkeys/testkey.pem" {
3201 t.Errorf("private key %q is not %q", keys.privateKeyFile.String(),
Jiyong Park9335a262018-12-24 11:31:58 +09003202 "vendor/foo/devkeys/testkey.pem")
3203 }
3204
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003205 // check the APK certs. It should be overridden to myapex.certificate.override
Sundong Ahnabb64432019-10-22 13:58:29 +09003206 certs := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk").Args["certificates"]
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003207 if certs != "testkey.override.x509.pem testkey.override.pk8" {
Jiyong Park9335a262018-12-24 11:31:58 +09003208 t.Errorf("cert and private key %q are not %q", certs,
Jiyong Parkb2742fd2019-02-11 11:38:15 +09003209 "testkey.override.509.pem testkey.override.pk8")
Jiyong Park9335a262018-12-24 11:31:58 +09003210 }
3211}
Jiyong Park58e364a2019-01-19 19:24:06 +09003212
Jooyung Hanf121a652019-12-17 14:30:11 +09003213func TestCertificate(t *testing.T) {
3214 t.Run("if unspecified, it defaults to DefaultAppCertificate", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003215 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003216 apex {
3217 name: "myapex",
3218 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003219 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003220 }
3221 apex_key {
3222 name: "myapex.key",
3223 public_key: "testkey.avbpubkey",
3224 private_key: "testkey.pem",
3225 }`)
3226 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3227 expected := "vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8"
3228 if actual := rule.Args["certificates"]; actual != expected {
3229 t.Errorf("certificates should be %q, not %q", expected, actual)
3230 }
3231 })
3232 t.Run("override when unspecified", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003233 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003234 apex {
3235 name: "myapex_keytest",
3236 key: "myapex.key",
3237 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003238 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003239 }
3240 apex_key {
3241 name: "myapex.key",
3242 public_key: "testkey.avbpubkey",
3243 private_key: "testkey.pem",
3244 }
3245 android_app_certificate {
3246 name: "myapex.certificate.override",
3247 certificate: "testkey.override",
3248 }`)
3249 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3250 expected := "testkey.override.x509.pem testkey.override.pk8"
3251 if actual := rule.Args["certificates"]; actual != expected {
3252 t.Errorf("certificates should be %q, not %q", expected, actual)
3253 }
3254 })
3255 t.Run("if specified as :module, it respects the prop", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003256 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003257 apex {
3258 name: "myapex",
3259 key: "myapex.key",
3260 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003261 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003262 }
3263 apex_key {
3264 name: "myapex.key",
3265 public_key: "testkey.avbpubkey",
3266 private_key: "testkey.pem",
3267 }
3268 android_app_certificate {
3269 name: "myapex.certificate",
3270 certificate: "testkey",
3271 }`)
3272 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3273 expected := "testkey.x509.pem testkey.pk8"
3274 if actual := rule.Args["certificates"]; actual != expected {
3275 t.Errorf("certificates should be %q, not %q", expected, actual)
3276 }
3277 })
3278 t.Run("override when specifiec as <:module>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003279 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003280 apex {
3281 name: "myapex_keytest",
3282 key: "myapex.key",
3283 file_contexts: ":myapex-file_contexts",
3284 certificate: ":myapex.certificate",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003285 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003286 }
3287 apex_key {
3288 name: "myapex.key",
3289 public_key: "testkey.avbpubkey",
3290 private_key: "testkey.pem",
3291 }
3292 android_app_certificate {
3293 name: "myapex.certificate.override",
3294 certificate: "testkey.override",
3295 }`)
3296 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3297 expected := "testkey.override.x509.pem testkey.override.pk8"
3298 if actual := rule.Args["certificates"]; actual != expected {
3299 t.Errorf("certificates should be %q, not %q", expected, actual)
3300 }
3301 })
3302 t.Run("if specified as name, finds it from DefaultDevKeyDir", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003303 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003304 apex {
3305 name: "myapex",
3306 key: "myapex.key",
3307 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003308 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003309 }
3310 apex_key {
3311 name: "myapex.key",
3312 public_key: "testkey.avbpubkey",
3313 private_key: "testkey.pem",
3314 }`)
3315 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("signapk")
3316 expected := "vendor/foo/devkeys/testkey.x509.pem vendor/foo/devkeys/testkey.pk8"
3317 if actual := rule.Args["certificates"]; actual != expected {
3318 t.Errorf("certificates should be %q, not %q", expected, actual)
3319 }
3320 })
3321 t.Run("override when specified as <name>", func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003322 ctx := testApex(t, `
Jooyung Hanf121a652019-12-17 14:30:11 +09003323 apex {
3324 name: "myapex_keytest",
3325 key: "myapex.key",
3326 file_contexts: ":myapex-file_contexts",
3327 certificate: "testkey",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003328 updatable: false,
Jooyung Hanf121a652019-12-17 14:30:11 +09003329 }
3330 apex_key {
3331 name: "myapex.key",
3332 public_key: "testkey.avbpubkey",
3333 private_key: "testkey.pem",
3334 }
3335 android_app_certificate {
3336 name: "myapex.certificate.override",
3337 certificate: "testkey.override",
3338 }`)
3339 rule := ctx.ModuleForTests("myapex_keytest", "android_common_myapex_keytest_image").Rule("signapk")
3340 expected := "testkey.override.x509.pem testkey.override.pk8"
3341 if actual := rule.Args["certificates"]; actual != expected {
3342 t.Errorf("certificates should be %q, not %q", expected, actual)
3343 }
3344 })
3345}
3346
Jiyong Park58e364a2019-01-19 19:24:06 +09003347func TestMacro(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003348 ctx := testApex(t, `
Jiyong Park58e364a2019-01-19 19:24:06 +09003349 apex {
3350 name: "myapex",
3351 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003352 native_shared_libs: ["mylib", "mylib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003353 updatable: false,
Jiyong Park58e364a2019-01-19 19:24:06 +09003354 }
3355
3356 apex {
3357 name: "otherapex",
3358 key: "myapex.key",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003359 native_shared_libs: ["mylib", "mylib2"],
Jooyung Hanccce2f22020-03-07 03:45:53 +09003360 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003361 }
3362
3363 apex_key {
3364 name: "myapex.key",
3365 public_key: "testkey.avbpubkey",
3366 private_key: "testkey.pem",
3367 }
3368
3369 cc_library {
3370 name: "mylib",
3371 srcs: ["mylib.cpp"],
3372 system_shared_libs: [],
3373 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003374 apex_available: [
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003375 "myapex",
3376 "otherapex",
3377 ],
Jooyung Han24282772020-03-21 23:20:55 +09003378 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003379 min_sdk_version: "29",
Jiyong Park58e364a2019-01-19 19:24:06 +09003380 }
Jooyung Hanc87a0592020-03-02 17:44:33 +09003381 cc_library {
3382 name: "mylib2",
3383 srcs: ["mylib.cpp"],
3384 system_shared_libs: [],
3385 stl: "none",
3386 apex_available: [
3387 "myapex",
3388 "otherapex",
3389 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003390 static_libs: ["mylib3"],
3391 recovery_available: true,
3392 min_sdk_version: "29",
3393 }
3394 cc_library {
3395 name: "mylib3",
3396 srcs: ["mylib.cpp"],
3397 system_shared_libs: [],
3398 stl: "none",
3399 apex_available: [
3400 "myapex",
3401 "otherapex",
3402 ],
Colin Crossaede88c2020-08-11 12:17:01 -07003403 recovery_available: true,
Jooyung Han749dc692020-04-15 11:03:39 +09003404 min_sdk_version: "29",
Jooyung Hanc87a0592020-03-02 17:44:33 +09003405 }
Jiyong Park58e364a2019-01-19 19:24:06 +09003406 `)
3407
Jooyung Hanc87a0592020-03-02 17:44:33 +09003408 // non-APEX variant does not have __ANDROID_APEX__ defined
Colin Cross7113d202019-11-20 16:39:12 -08003409 mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han6b8459b2019-10-30 08:29:25 +09003410 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003411
Vinh Tranf9754732023-01-19 22:41:46 -05003412 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003413 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex10000").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003414 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jooyung Hanc87a0592020-03-02 17:44:33 +09003415
Vinh Tranf9754732023-01-19 22:41:46 -05003416 // APEX variant has __ANDROID_APEX__ and __ANDROID_APEX__ defined
Colin Crossaede88c2020-08-11 12:17:01 -07003417 mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_static_apex29").Rule("cc").Args["cFlags"]
Jooyung Hanc87a0592020-03-02 17:44:33 +09003418 ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003419
Colin Crossaede88c2020-08-11 12:17:01 -07003420 // When a cc_library sets use_apex_name_macro: true each apex gets a unique variant and
3421 // each variant defines additional macros to distinguish which apex variant it is built for
3422
3423 // non-APEX variant does not have __ANDROID_APEX__ defined
3424 mylibCFlags = ctx.ModuleForTests("mylib3", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3425 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3426
Vinh Tranf9754732023-01-19 22:41:46 -05003427 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003428 mylibCFlags = ctx.ModuleForTests("mylib3", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3429 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Colin Crossaede88c2020-08-11 12:17:01 -07003430
Jooyung Hanc87a0592020-03-02 17:44:33 +09003431 // non-APEX variant does not have __ANDROID_APEX__ defined
3432 mylibCFlags = ctx.ModuleForTests("mylib2", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
3433 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
3434
Vinh Tranf9754732023-01-19 22:41:46 -05003435 // recovery variant does not set __ANDROID_APEX__
Colin Crossaede88c2020-08-11 12:17:01 -07003436 mylibCFlags = ctx.ModuleForTests("mylib2", "android_recovery_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jooyung Han24282772020-03-21 23:20:55 +09003437 ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__")
Jiyong Park58e364a2019-01-19 19:24:06 +09003438}
Jiyong Park7e636d02019-01-28 16:16:54 +09003439
3440func TestHeaderLibsDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003441 ctx := testApex(t, `
Jiyong Park7e636d02019-01-28 16:16:54 +09003442 apex {
3443 name: "myapex",
3444 key: "myapex.key",
3445 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003446 updatable: false,
Jiyong Park7e636d02019-01-28 16:16:54 +09003447 }
3448
3449 apex_key {
3450 name: "myapex.key",
3451 public_key: "testkey.avbpubkey",
3452 private_key: "testkey.pem",
3453 }
3454
3455 cc_library_headers {
3456 name: "mylib_headers",
3457 export_include_dirs: ["my_include"],
3458 system_shared_libs: [],
3459 stl: "none",
Jiyong Park0f80c182020-01-31 02:49:53 +09003460 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003461 }
3462
3463 cc_library {
3464 name: "mylib",
3465 srcs: ["mylib.cpp"],
3466 system_shared_libs: [],
3467 stl: "none",
3468 header_libs: ["mylib_headers"],
3469 export_header_lib_headers: ["mylib_headers"],
3470 stubs: {
3471 versions: ["1", "2", "3"],
3472 },
Anton Hanssoneec79eb2020-01-10 15:12:39 +00003473 apex_available: [ "myapex" ],
Jiyong Park7e636d02019-01-28 16:16:54 +09003474 }
3475
3476 cc_library {
3477 name: "otherlib",
3478 srcs: ["mylib.cpp"],
3479 system_shared_libs: [],
3480 stl: "none",
3481 shared_libs: ["mylib"],
3482 }
3483 `)
3484
Colin Cross7113d202019-11-20 16:39:12 -08003485 cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
Jiyong Park7e636d02019-01-28 16:16:54 +09003486
3487 // Ensure that the include path of the header lib is exported to 'otherlib'
3488 ensureContains(t, cFlags, "-Imy_include")
3489}
Alex Light9670d332019-01-29 18:07:33 -08003490
Jiyong Park7cd10e32020-01-14 09:22:18 +09003491type fileInApex struct {
3492 path string // path in apex
Jooyung Hana57af4a2020-01-23 05:36:59 +00003493 src string // src path
Jiyong Park7cd10e32020-01-14 09:22:18 +09003494 isLink bool
3495}
3496
Jooyung Han1724d582022-12-21 10:17:44 +09003497func (f fileInApex) String() string {
3498 return f.src + ":" + f.path
3499}
3500
3501func (f fileInApex) match(expectation string) bool {
3502 parts := strings.Split(expectation, ":")
3503 if len(parts) == 1 {
3504 match, _ := path.Match(parts[0], f.path)
3505 return match
3506 }
3507 if len(parts) == 2 {
3508 matchSrc, _ := path.Match(parts[0], f.src)
3509 matchDst, _ := path.Match(parts[1], f.path)
3510 return matchSrc && matchDst
3511 }
3512 panic("invalid expected file specification: " + expectation)
3513}
3514
Jooyung Hana57af4a2020-01-23 05:36:59 +00003515func getFiles(t *testing.T, ctx *android.TestContext, moduleName, variant string) []fileInApex {
Jooyung Han31c470b2019-10-18 16:26:59 +09003516 t.Helper()
Jooyung Han1724d582022-12-21 10:17:44 +09003517 module := ctx.ModuleForTests(moduleName, variant)
3518 apexRule := module.MaybeRule("apexRule")
3519 apexDir := "/image.apex/"
3520 if apexRule.Rule == nil {
3521 apexRule = module.Rule("zipApexRule")
3522 apexDir = "/image.zipapex/"
3523 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003524 copyCmds := apexRule.Args["copy_commands"]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003525 var ret []fileInApex
Jooyung Han31c470b2019-10-18 16:26:59 +09003526 for _, cmd := range strings.Split(copyCmds, "&&") {
3527 cmd = strings.TrimSpace(cmd)
3528 if cmd == "" {
3529 continue
3530 }
3531 terms := strings.Split(cmd, " ")
Jooyung Hana57af4a2020-01-23 05:36:59 +00003532 var dst, src string
Jiyong Park7cd10e32020-01-14 09:22:18 +09003533 var isLink bool
Jooyung Han31c470b2019-10-18 16:26:59 +09003534 switch terms[0] {
3535 case "mkdir":
3536 case "cp":
Jiyong Park7cd10e32020-01-14 09:22:18 +09003537 if len(terms) != 3 && len(terms) != 4 {
Jooyung Han31c470b2019-10-18 16:26:59 +09003538 t.Fatal("copyCmds contains invalid cp command", cmd)
3539 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003540 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003541 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003542 isLink = false
3543 case "ln":
3544 if len(terms) != 3 && len(terms) != 4 {
3545 // ln LINK TARGET or ln -s LINK TARGET
3546 t.Fatal("copyCmds contains invalid ln command", cmd)
3547 }
3548 dst = terms[len(terms)-1]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003549 src = terms[len(terms)-2]
Jiyong Park7cd10e32020-01-14 09:22:18 +09003550 isLink = true
3551 default:
3552 t.Fatalf("copyCmds should contain mkdir/cp commands only: %q", cmd)
3553 }
3554 if dst != "" {
Jooyung Han1724d582022-12-21 10:17:44 +09003555 index := strings.Index(dst, apexDir)
Jooyung Han31c470b2019-10-18 16:26:59 +09003556 if index == -1 {
Jooyung Han1724d582022-12-21 10:17:44 +09003557 t.Fatal("copyCmds should copy a file to "+apexDir, cmd)
Jooyung Han31c470b2019-10-18 16:26:59 +09003558 }
Jooyung Han1724d582022-12-21 10:17:44 +09003559 dstFile := dst[index+len(apexDir):]
Jooyung Hana57af4a2020-01-23 05:36:59 +00003560 ret = append(ret, fileInApex{path: dstFile, src: src, isLink: isLink})
Jooyung Han31c470b2019-10-18 16:26:59 +09003561 }
3562 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003563 return ret
3564}
3565
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003566func assertFileListEquals(t *testing.T, expectedFiles []string, actualFiles []fileInApex) {
Jooyung Hana57af4a2020-01-23 05:36:59 +00003567 t.Helper()
Jiyong Park7cd10e32020-01-14 09:22:18 +09003568 var failed bool
3569 var surplus []string
3570 filesMatched := make(map[string]bool)
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003571 for _, file := range actualFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003572 matchFound := false
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003573 for _, expected := range expectedFiles {
Jooyung Han1724d582022-12-21 10:17:44 +09003574 if file.match(expected) {
3575 matchFound = true
Jiyong Park7cd10e32020-01-14 09:22:18 +09003576 filesMatched[expected] = true
Jooyung Hane6436d72020-02-27 13:31:56 +09003577 break
Jiyong Park7cd10e32020-01-14 09:22:18 +09003578 }
3579 }
Jooyung Han1724d582022-12-21 10:17:44 +09003580 if !matchFound {
3581 surplus = append(surplus, file.String())
Jooyung Hane6436d72020-02-27 13:31:56 +09003582 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09003583 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003584
Jooyung Han31c470b2019-10-18 16:26:59 +09003585 if len(surplus) > 0 {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003586 sort.Strings(surplus)
Jooyung Han31c470b2019-10-18 16:26:59 +09003587 t.Log("surplus files", surplus)
3588 failed = true
3589 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003590
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003591 if len(expectedFiles) > len(filesMatched) {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003592 var missing []string
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003593 for _, expected := range expectedFiles {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003594 if !filesMatched[expected] {
3595 missing = append(missing, expected)
3596 }
3597 }
3598 sort.Strings(missing)
Jooyung Han31c470b2019-10-18 16:26:59 +09003599 t.Log("missing files", missing)
3600 failed = true
3601 }
3602 if failed {
3603 t.Fail()
3604 }
3605}
3606
Jiakai Zhangebf48bf2023-02-10 01:51:53 +08003607func ensureExactContents(t *testing.T, ctx *android.TestContext, moduleName, variant string, files []string) {
3608 assertFileListEquals(t, files, getFiles(t, ctx, moduleName, variant))
3609}
3610
3611func ensureExactDeapexedContents(t *testing.T, ctx *android.TestContext, moduleName string, variant string, files []string) {
3612 deapexer := ctx.ModuleForTests(moduleName+".deapexer", variant).Rule("deapexer")
3613 outputs := make([]string, 0, len(deapexer.ImplicitOutputs)+1)
3614 if deapexer.Output != nil {
3615 outputs = append(outputs, deapexer.Output.String())
3616 }
3617 for _, output := range deapexer.ImplicitOutputs {
3618 outputs = append(outputs, output.String())
3619 }
3620 actualFiles := make([]fileInApex, 0, len(outputs))
3621 for _, output := range outputs {
3622 dir := "/deapexer/"
3623 pos := strings.LastIndex(output, dir)
3624 if pos == -1 {
3625 t.Fatal("Unknown deapexer output ", output)
3626 }
3627 path := output[pos+len(dir):]
3628 actualFiles = append(actualFiles, fileInApex{path: path, src: "", isLink: false})
3629 }
3630 assertFileListEquals(t, files, actualFiles)
3631}
3632
Jooyung Han344d5432019-08-23 11:17:39 +09003633func TestVndkApexCurrent(t *testing.T) {
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003634 commonFiles := []string{
Jooyung Hane6436d72020-02-27 13:31:56 +09003635 "lib/libc++.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003636 "lib64/libc++.so",
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003637 "etc/llndk.libraries.29.txt",
3638 "etc/vndkcore.libraries.29.txt",
3639 "etc/vndksp.libraries.29.txt",
3640 "etc/vndkprivate.libraries.29.txt",
3641 "etc/vndkproduct.libraries.29.txt",
Jooyung Han7d6e79b2021-06-24 01:53:43 +09003642 }
3643 testCases := []struct {
3644 vndkVersion string
3645 expectedFiles []string
3646 }{
3647 {
3648 vndkVersion: "current",
3649 expectedFiles: append(commonFiles,
3650 "lib/libvndk.so",
3651 "lib/libvndksp.so",
3652 "lib64/libvndk.so",
3653 "lib64/libvndksp.so"),
3654 },
3655 {
3656 vndkVersion: "",
3657 expectedFiles: append(commonFiles,
3658 // Legacy VNDK APEX contains only VNDK-SP files (of core variant)
3659 "lib/libvndksp.so",
3660 "lib64/libvndksp.so"),
3661 },
3662 }
3663 for _, tc := range testCases {
3664 t.Run("VNDK.current with DeviceVndkVersion="+tc.vndkVersion, func(t *testing.T) {
3665 ctx := testApex(t, `
3666 apex_vndk {
3667 name: "com.android.vndk.current",
3668 key: "com.android.vndk.current.key",
3669 updatable: false,
3670 }
3671
3672 apex_key {
3673 name: "com.android.vndk.current.key",
3674 public_key: "testkey.avbpubkey",
3675 private_key: "testkey.pem",
3676 }
3677
3678 cc_library {
3679 name: "libvndk",
3680 srcs: ["mylib.cpp"],
3681 vendor_available: true,
3682 product_available: true,
3683 vndk: {
3684 enabled: true,
3685 },
3686 system_shared_libs: [],
3687 stl: "none",
3688 apex_available: [ "com.android.vndk.current" ],
3689 }
3690
3691 cc_library {
3692 name: "libvndksp",
3693 srcs: ["mylib.cpp"],
3694 vendor_available: true,
3695 product_available: true,
3696 vndk: {
3697 enabled: true,
3698 support_system_process: true,
3699 },
3700 system_shared_libs: [],
3701 stl: "none",
3702 apex_available: [ "com.android.vndk.current" ],
3703 }
3704
3705 // VNDK-Ext should not cause any problems
3706
3707 cc_library {
3708 name: "libvndk.ext",
3709 srcs: ["mylib2.cpp"],
3710 vendor: true,
3711 vndk: {
3712 enabled: true,
3713 extends: "libvndk",
3714 },
3715 system_shared_libs: [],
3716 stl: "none",
3717 }
3718
3719 cc_library {
3720 name: "libvndksp.ext",
3721 srcs: ["mylib2.cpp"],
3722 vendor: true,
3723 vndk: {
3724 enabled: true,
3725 support_system_process: true,
3726 extends: "libvndksp",
3727 },
3728 system_shared_libs: [],
3729 stl: "none",
3730 }
3731 `+vndkLibrariesTxtFiles("current"), android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3732 variables.DeviceVndkVersion = proptools.StringPtr(tc.vndkVersion)
3733 }))
3734 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", tc.expectedFiles)
3735 })
3736 }
Jooyung Han344d5432019-08-23 11:17:39 +09003737}
3738
3739func TestVndkApexWithPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003740 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003741 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003742 name: "com.android.vndk.current",
3743 key: "com.android.vndk.current.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003744 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003745 }
3746
3747 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003748 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003749 public_key: "testkey.avbpubkey",
3750 private_key: "testkey.pem",
3751 }
3752
3753 cc_prebuilt_library_shared {
Jooyung Han31c470b2019-10-18 16:26:59 +09003754 name: "libvndk",
3755 srcs: ["libvndk.so"],
Jooyung Han344d5432019-08-23 11:17:39 +09003756 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003757 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003758 vndk: {
3759 enabled: true,
3760 },
3761 system_shared_libs: [],
3762 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003763 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003764 }
Jooyung Han31c470b2019-10-18 16:26:59 +09003765
3766 cc_prebuilt_library_shared {
3767 name: "libvndk.arm",
3768 srcs: ["libvndk.arm.so"],
3769 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003770 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09003771 vndk: {
3772 enabled: true,
3773 },
3774 enabled: false,
3775 arch: {
3776 arm: {
3777 enabled: true,
3778 },
3779 },
3780 system_shared_libs: [],
3781 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003782 apex_available: [ "com.android.vndk.current" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09003783 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09003784 `+vndkLibrariesTxtFiles("current"),
3785 withFiles(map[string][]byte{
3786 "libvndk.so": nil,
3787 "libvndk.arm.so": nil,
3788 }))
Colin Cross2807f002021-03-02 10:15:29 -08003789 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003790 "lib/libvndk.so",
3791 "lib/libvndk.arm.so",
3792 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003793 "lib/libc++.so",
3794 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003795 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003796 })
Jooyung Han344d5432019-08-23 11:17:39 +09003797}
3798
Jooyung Han39edb6c2019-11-06 16:53:07 +09003799func vndkLibrariesTxtFiles(vers ...string) (result string) {
3800 for _, v := range vers {
3801 if v == "current" {
Justin Yun8a2600c2020-12-07 12:44:03 +09003802 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003803 result += `
Colin Crosse4e44bc2020-12-28 13:50:21 -08003804 ` + txt + `_libraries_txt {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003805 name: "` + txt + `.libraries.txt",
3806 }
3807 `
3808 }
3809 } else {
Justin Yun8a2600c2020-12-07 12:44:03 +09003810 for _, txt := range []string{"llndk", "vndkcore", "vndksp", "vndkprivate", "vndkproduct"} {
Jooyung Han39edb6c2019-11-06 16:53:07 +09003811 result += `
3812 prebuilt_etc {
3813 name: "` + txt + `.libraries.` + v + `.txt",
3814 src: "dummy.txt",
3815 }
3816 `
3817 }
3818 }
3819 }
3820 return
3821}
3822
Jooyung Han344d5432019-08-23 11:17:39 +09003823func TestVndkApexVersion(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003824 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003825 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003826 name: "com.android.vndk.v27",
Jooyung Han344d5432019-08-23 11:17:39 +09003827 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003828 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003829 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003830 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003831 }
3832
3833 apex_key {
3834 name: "myapex.key",
3835 public_key: "testkey.avbpubkey",
3836 private_key: "testkey.pem",
3837 }
3838
Jooyung Han31c470b2019-10-18 16:26:59 +09003839 vndk_prebuilt_shared {
3840 name: "libvndk27",
3841 version: "27",
Jooyung Han344d5432019-08-23 11:17:39 +09003842 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003843 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003844 vndk: {
3845 enabled: true,
3846 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003847 target_arch: "arm64",
3848 arch: {
3849 arm: {
3850 srcs: ["libvndk27_arm.so"],
3851 },
3852 arm64: {
3853 srcs: ["libvndk27_arm64.so"],
3854 },
3855 },
Colin Cross2807f002021-03-02 10:15:29 -08003856 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003857 }
3858
3859 vndk_prebuilt_shared {
3860 name: "libvndk27",
3861 version: "27",
3862 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003863 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003864 vndk: {
3865 enabled: true,
3866 },
Jooyung Han31c470b2019-10-18 16:26:59 +09003867 target_arch: "x86_64",
3868 arch: {
3869 x86: {
3870 srcs: ["libvndk27_x86.so"],
3871 },
3872 x86_64: {
3873 srcs: ["libvndk27_x86_64.so"],
3874 },
3875 },
Jooyung Han39edb6c2019-11-06 16:53:07 +09003876 }
3877 `+vndkLibrariesTxtFiles("27"),
3878 withFiles(map[string][]byte{
3879 "libvndk27_arm.so": nil,
3880 "libvndk27_arm64.so": nil,
3881 "libvndk27_x86.so": nil,
3882 "libvndk27_x86_64.so": nil,
3883 }))
Jooyung Han344d5432019-08-23 11:17:39 +09003884
Colin Cross2807f002021-03-02 10:15:29 -08003885 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003886 "lib/libvndk27_arm.so",
3887 "lib64/libvndk27_arm64.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003888 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003889 })
Jooyung Han344d5432019-08-23 11:17:39 +09003890}
3891
Jooyung Han90eee022019-10-01 20:02:42 +09003892func TestVndkApexNameRule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003893 ctx := testApex(t, `
Jooyung Han90eee022019-10-01 20:02:42 +09003894 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003895 name: "com.android.vndk.current",
Jooyung Han90eee022019-10-01 20:02:42 +09003896 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003897 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003898 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003899 }
3900 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003901 name: "com.android.vndk.v28",
Jooyung Han90eee022019-10-01 20:02:42 +09003902 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003903 file_contexts: ":myapex-file_contexts",
Jooyung Han90eee022019-10-01 20:02:42 +09003904 vndk_version: "28",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003905 updatable: false,
Jooyung Han90eee022019-10-01 20:02:42 +09003906 }
3907 apex_key {
3908 name: "myapex.key",
3909 public_key: "testkey.avbpubkey",
3910 private_key: "testkey.pem",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003911 }`+vndkLibrariesTxtFiles("28", "current"))
Jooyung Han90eee022019-10-01 20:02:42 +09003912
3913 assertApexName := func(expected, moduleName string) {
Jooyung Han2cd2f9a2023-02-06 18:29:08 +09003914 module := ctx.ModuleForTests(moduleName, "android_common_image")
3915 apexManifestRule := module.Rule("apexManifestRule")
3916 ensureContains(t, apexManifestRule.Args["opt"], "-v name "+expected)
Jooyung Han90eee022019-10-01 20:02:42 +09003917 }
3918
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003919 assertApexName("com.android.vndk.v29", "com.android.vndk.current")
Colin Cross2807f002021-03-02 10:15:29 -08003920 assertApexName("com.android.vndk.v28", "com.android.vndk.v28")
Jooyung Han90eee022019-10-01 20:02:42 +09003921}
3922
Jooyung Han344d5432019-08-23 11:17:39 +09003923func TestVndkApexSkipsNativeBridgeSupportedModules(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003924 ctx := testApex(t, `
Jooyung Han344d5432019-08-23 11:17:39 +09003925 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003926 name: "com.android.vndk.current",
3927 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003928 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00003929 updatable: false,
Jooyung Han344d5432019-08-23 11:17:39 +09003930 }
3931
3932 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003933 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003934 public_key: "testkey.avbpubkey",
3935 private_key: "testkey.pem",
3936 }
3937
3938 cc_library {
3939 name: "libvndk",
3940 srcs: ["mylib.cpp"],
3941 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003942 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003943 native_bridge_supported: true,
3944 host_supported: true,
3945 vndk: {
3946 enabled: true,
3947 },
3948 system_shared_libs: [],
3949 stl: "none",
Colin Cross2807f002021-03-02 10:15:29 -08003950 apex_available: [ "com.android.vndk.current" ],
Jooyung Han344d5432019-08-23 11:17:39 +09003951 }
Colin Cross2807f002021-03-02 10:15:29 -08003952 `+vndkLibrariesTxtFiles("current"),
3953 withNativeBridgeEnabled)
Jooyung Han344d5432019-08-23 11:17:39 +09003954
Colin Cross2807f002021-03-02 10:15:29 -08003955 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09003956 "lib/libvndk.so",
3957 "lib64/libvndk.so",
Jooyung Hane6436d72020-02-27 13:31:56 +09003958 "lib/libc++.so",
3959 "lib64/libc++.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09003960 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09003961 })
Jooyung Han344d5432019-08-23 11:17:39 +09003962}
3963
3964func TestVndkApexDoesntSupportNativeBridgeSupported(t *testing.T) {
Colin Cross2807f002021-03-02 10:15:29 -08003965 testApexError(t, `module "com.android.vndk.current" .*: native_bridge_supported: .* doesn't support native bridge binary`, `
Jooyung Han344d5432019-08-23 11:17:39 +09003966 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003967 name: "com.android.vndk.current",
3968 key: "com.android.vndk.current.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09003969 file_contexts: ":myapex-file_contexts",
Jooyung Han344d5432019-08-23 11:17:39 +09003970 native_bridge_supported: true,
3971 }
3972
3973 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08003974 name: "com.android.vndk.current.key",
Jooyung Han344d5432019-08-23 11:17:39 +09003975 public_key: "testkey.avbpubkey",
3976 private_key: "testkey.pem",
3977 }
3978
3979 cc_library {
3980 name: "libvndk",
3981 srcs: ["mylib.cpp"],
3982 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003983 product_available: true,
Jooyung Han344d5432019-08-23 11:17:39 +09003984 native_bridge_supported: true,
3985 host_supported: true,
3986 vndk: {
3987 enabled: true,
3988 },
3989 system_shared_libs: [],
3990 stl: "none",
3991 }
3992 `)
3993}
3994
Jooyung Han31c470b2019-10-18 16:26:59 +09003995func TestVndkApexWithBinder32(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08003996 ctx := testApex(t, `
Jooyung Han31c470b2019-10-18 16:26:59 +09003997 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08003998 name: "com.android.vndk.v27",
Jooyung Han31c470b2019-10-18 16:26:59 +09003999 key: "myapex.key",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004000 file_contexts: ":myapex-file_contexts",
Jooyung Han31c470b2019-10-18 16:26:59 +09004001 vndk_version: "27",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004002 updatable: false,
Jooyung Han31c470b2019-10-18 16:26:59 +09004003 }
4004
4005 apex_key {
4006 name: "myapex.key",
4007 public_key: "testkey.avbpubkey",
4008 private_key: "testkey.pem",
4009 }
4010
4011 vndk_prebuilt_shared {
4012 name: "libvndk27",
4013 version: "27",
4014 target_arch: "arm",
4015 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004016 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004017 vndk: {
4018 enabled: true,
4019 },
4020 arch: {
4021 arm: {
4022 srcs: ["libvndk27.so"],
4023 }
4024 },
4025 }
4026
4027 vndk_prebuilt_shared {
4028 name: "libvndk27",
4029 version: "27",
4030 target_arch: "arm",
4031 binder32bit: true,
4032 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004033 product_available: true,
Jooyung Han31c470b2019-10-18 16:26:59 +09004034 vndk: {
4035 enabled: true,
4036 },
4037 arch: {
4038 arm: {
4039 srcs: ["libvndk27binder32.so"],
4040 }
4041 },
Colin Cross2807f002021-03-02 10:15:29 -08004042 apex_available: [ "com.android.vndk.v27" ],
Jooyung Han31c470b2019-10-18 16:26:59 +09004043 }
Jooyung Han39edb6c2019-11-06 16:53:07 +09004044 `+vndkLibrariesTxtFiles("27"),
Jooyung Han31c470b2019-10-18 16:26:59 +09004045 withFiles(map[string][]byte{
4046 "libvndk27.so": nil,
4047 "libvndk27binder32.so": nil,
4048 }),
4049 withBinder32bit,
4050 withTargets(map[android.OsType][]android.Target{
Wei Li340ee8e2022-03-18 17:33:24 -07004051 android.Android: {
Jooyung Han35155c42020-02-06 17:33:20 +09004052 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}},
4053 NativeBridge: android.NativeBridgeDisabled, NativeBridgeHostArchName: "", NativeBridgeRelativePath: ""},
Jooyung Han31c470b2019-10-18 16:26:59 +09004054 },
4055 }),
4056 )
4057
Colin Cross2807f002021-03-02 10:15:29 -08004058 ensureExactContents(t, ctx, "com.android.vndk.v27", "android_common_image", []string{
Jooyung Han31c470b2019-10-18 16:26:59 +09004059 "lib/libvndk27binder32.so",
Jooyung Han39edb6c2019-11-06 16:53:07 +09004060 "etc/*",
Jooyung Han31c470b2019-10-18 16:26:59 +09004061 })
4062}
4063
Jooyung Han45a96772020-06-15 14:59:42 +09004064func TestVndkApexShouldNotProvideNativeLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004065 ctx := testApex(t, `
Jooyung Han45a96772020-06-15 14:59:42 +09004066 apex_vndk {
Colin Cross2807f002021-03-02 10:15:29 -08004067 name: "com.android.vndk.current",
4068 key: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004069 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004070 updatable: false,
Jooyung Han45a96772020-06-15 14:59:42 +09004071 }
4072
4073 apex_key {
Colin Cross2807f002021-03-02 10:15:29 -08004074 name: "com.android.vndk.current.key",
Jooyung Han45a96772020-06-15 14:59:42 +09004075 public_key: "testkey.avbpubkey",
4076 private_key: "testkey.pem",
4077 }
4078
4079 cc_library {
4080 name: "libz",
4081 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09004082 product_available: true,
Jooyung Han45a96772020-06-15 14:59:42 +09004083 vndk: {
4084 enabled: true,
4085 },
4086 stubs: {
4087 symbol_file: "libz.map.txt",
4088 versions: ["30"],
4089 }
4090 }
4091 `+vndkLibrariesTxtFiles("current"), withFiles(map[string][]byte{
4092 "libz.map.txt": nil,
4093 }))
4094
Colin Cross2807f002021-03-02 10:15:29 -08004095 apexManifestRule := ctx.ModuleForTests("com.android.vndk.current", "android_common_image").Rule("apexManifestRule")
Jooyung Han45a96772020-06-15 14:59:42 +09004096 provideNativeLibs := names(apexManifestRule.Args["provideNativeLibs"])
4097 ensureListEmpty(t, provideNativeLibs)
Jooyung Han1724d582022-12-21 10:17:44 +09004098 ensureExactContents(t, ctx, "com.android.vndk.current", "android_common_image", []string{
4099 "out/soong/.intermediates/libz/android_vendor.29_arm64_armv8-a_shared/libz.so:lib64/libz.so",
4100 "out/soong/.intermediates/libz/android_vendor.29_arm_armv7-a-neon_shared/libz.so:lib/libz.so",
4101 "*/*",
4102 })
Jooyung Han45a96772020-06-15 14:59:42 +09004103}
4104
Jooyung Hane1633032019-08-01 17:41:43 +09004105func TestDependenciesInApexManifest(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004106 ctx := testApex(t, `
Jooyung Hane1633032019-08-01 17:41:43 +09004107 apex {
4108 name: "myapex_nodep",
4109 key: "myapex.key",
4110 native_shared_libs: ["lib_nodep"],
4111 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004112 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004113 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004114 }
4115
4116 apex {
4117 name: "myapex_dep",
4118 key: "myapex.key",
4119 native_shared_libs: ["lib_dep"],
4120 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004121 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004122 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004123 }
4124
4125 apex {
4126 name: "myapex_provider",
4127 key: "myapex.key",
4128 native_shared_libs: ["libfoo"],
4129 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004130 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004131 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004132 }
4133
4134 apex {
4135 name: "myapex_selfcontained",
4136 key: "myapex.key",
4137 native_shared_libs: ["lib_dep", "libfoo"],
4138 compile_multilib: "both",
Jooyung Han54aca7b2019-11-20 02:26:02 +09004139 file_contexts: ":myapex-file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004140 updatable: false,
Jooyung Hane1633032019-08-01 17:41:43 +09004141 }
4142
4143 apex_key {
4144 name: "myapex.key",
4145 public_key: "testkey.avbpubkey",
4146 private_key: "testkey.pem",
4147 }
4148
4149 cc_library {
4150 name: "lib_nodep",
4151 srcs: ["mylib.cpp"],
4152 system_shared_libs: [],
4153 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004154 apex_available: [ "myapex_nodep" ],
Jooyung Hane1633032019-08-01 17:41:43 +09004155 }
4156
4157 cc_library {
4158 name: "lib_dep",
4159 srcs: ["mylib.cpp"],
4160 shared_libs: ["libfoo"],
4161 system_shared_libs: [],
4162 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004163 apex_available: [
4164 "myapex_dep",
4165 "myapex_provider",
4166 "myapex_selfcontained",
4167 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004168 }
4169
4170 cc_library {
4171 name: "libfoo",
4172 srcs: ["mytest.cpp"],
4173 stubs: {
4174 versions: ["1"],
4175 },
4176 system_shared_libs: [],
4177 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004178 apex_available: [
4179 "myapex_provider",
4180 "myapex_selfcontained",
4181 ],
Jooyung Hane1633032019-08-01 17:41:43 +09004182 }
4183 `)
4184
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004185 var apexManifestRule android.TestingBuildParams
Jooyung Hane1633032019-08-01 17:41:43 +09004186 var provideNativeLibs, requireNativeLibs []string
4187
Sundong Ahnabb64432019-10-22 13:58:29 +09004188 apexManifestRule = ctx.ModuleForTests("myapex_nodep", "android_common_myapex_nodep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004189 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4190 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004191 ensureListEmpty(t, provideNativeLibs)
4192 ensureListEmpty(t, requireNativeLibs)
4193
Sundong Ahnabb64432019-10-22 13:58:29 +09004194 apexManifestRule = ctx.ModuleForTests("myapex_dep", "android_common_myapex_dep_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004195 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4196 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004197 ensureListEmpty(t, provideNativeLibs)
4198 ensureListContains(t, requireNativeLibs, "libfoo.so")
4199
Sundong Ahnabb64432019-10-22 13:58:29 +09004200 apexManifestRule = ctx.ModuleForTests("myapex_provider", "android_common_myapex_provider_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004201 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4202 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004203 ensureListContains(t, provideNativeLibs, "libfoo.so")
4204 ensureListEmpty(t, requireNativeLibs)
4205
Sundong Ahnabb64432019-10-22 13:58:29 +09004206 apexManifestRule = ctx.ModuleForTests("myapex_selfcontained", "android_common_myapex_selfcontained_image").Rule("apexManifestRule")
Jooyung Hand15aa1f2019-09-27 00:38:03 +09004207 provideNativeLibs = names(apexManifestRule.Args["provideNativeLibs"])
4208 requireNativeLibs = names(apexManifestRule.Args["requireNativeLibs"])
Jooyung Hane1633032019-08-01 17:41:43 +09004209 ensureListContains(t, provideNativeLibs, "libfoo.so")
4210 ensureListEmpty(t, requireNativeLibs)
4211}
4212
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004213func TestOverrideApexManifestDefaultVersion(t *testing.T) {
4214 ctx := testApex(t, `
4215 apex {
4216 name: "myapex",
4217 key: "myapex.key",
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004218 native_shared_libs: ["mylib"],
4219 updatable: false,
4220 }
4221
4222 apex_key {
4223 name: "myapex.key",
4224 public_key: "testkey.avbpubkey",
4225 private_key: "testkey.pem",
4226 }
4227
4228 cc_library {
4229 name: "mylib",
4230 srcs: ["mylib.cpp"],
4231 system_shared_libs: [],
4232 stl: "none",
4233 apex_available: [
4234 "//apex_available:platform",
4235 "myapex",
4236 ],
4237 }
4238 `, android.FixtureMergeEnv(map[string]string{
4239 "OVERRIDE_APEX_MANIFEST_DEFAULT_VERSION": "1234",
4240 }))
4241
Jooyung Han63dff462023-02-09 00:11:27 +00004242 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sahana Rao16ebdfd2022-12-02 17:00:22 +00004243 apexManifestRule := module.Rule("apexManifestRule")
4244 ensureContains(t, apexManifestRule.Args["default_version"], "1234")
4245}
4246
Vinh Tran8f5310f2022-10-07 18:16:47 -04004247func TestCompileMultilibProp(t *testing.T) {
4248 testCases := []struct {
4249 compileMultiLibProp string
4250 containedLibs []string
4251 notContainedLibs []string
4252 }{
4253 {
4254 containedLibs: []string{
4255 "image.apex/lib64/mylib.so",
4256 "image.apex/lib/mylib.so",
4257 },
4258 compileMultiLibProp: `compile_multilib: "both",`,
4259 },
4260 {
4261 containedLibs: []string{"image.apex/lib64/mylib.so"},
4262 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4263 compileMultiLibProp: `compile_multilib: "first",`,
4264 },
4265 {
4266 containedLibs: []string{"image.apex/lib64/mylib.so"},
4267 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4268 // compile_multilib, when unset, should result to the same output as when compile_multilib is "first"
4269 },
4270 {
4271 containedLibs: []string{"image.apex/lib64/mylib.so"},
4272 notContainedLibs: []string{"image.apex/lib/mylib.so"},
4273 compileMultiLibProp: `compile_multilib: "64",`,
4274 },
4275 {
4276 containedLibs: []string{"image.apex/lib/mylib.so"},
4277 notContainedLibs: []string{"image.apex/lib64/mylib.so"},
4278 compileMultiLibProp: `compile_multilib: "32",`,
4279 },
4280 }
4281 for _, testCase := range testCases {
4282 ctx := testApex(t, fmt.Sprintf(`
4283 apex {
4284 name: "myapex",
4285 key: "myapex.key",
4286 %s
4287 native_shared_libs: ["mylib"],
4288 updatable: false,
4289 }
4290 apex_key {
4291 name: "myapex.key",
4292 public_key: "testkey.avbpubkey",
4293 private_key: "testkey.pem",
4294 }
4295 cc_library {
4296 name: "mylib",
4297 srcs: ["mylib.cpp"],
4298 apex_available: [
4299 "//apex_available:platform",
4300 "myapex",
4301 ],
4302 }
4303 `, testCase.compileMultiLibProp),
4304 )
4305 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4306 apexRule := module.Rule("apexRule")
4307 copyCmds := apexRule.Args["copy_commands"]
4308 for _, containedLib := range testCase.containedLibs {
4309 ensureContains(t, copyCmds, containedLib)
4310 }
4311 for _, notContainedLib := range testCase.notContainedLibs {
4312 ensureNotContains(t, copyCmds, notContainedLib)
4313 }
4314 }
4315}
4316
Alex Light0851b882019-02-07 13:20:53 -08004317func TestNonTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004318 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004319 apex {
4320 name: "myapex",
4321 key: "myapex.key",
4322 native_shared_libs: ["mylib_common"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004323 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004324 }
4325
4326 apex_key {
4327 name: "myapex.key",
4328 public_key: "testkey.avbpubkey",
4329 private_key: "testkey.pem",
4330 }
4331
4332 cc_library {
4333 name: "mylib_common",
4334 srcs: ["mylib.cpp"],
4335 system_shared_libs: [],
4336 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004337 apex_available: [
4338 "//apex_available:platform",
4339 "myapex",
4340 ],
Alex Light0851b882019-02-07 13:20:53 -08004341 }
4342 `)
4343
Sundong Ahnabb64432019-10-22 13:58:29 +09004344 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004345 apexRule := module.Rule("apexRule")
4346 copyCmds := apexRule.Args["copy_commands"]
4347
4348 if apex, ok := module.Module().(*apexBundle); !ok || apex.testApex {
4349 t.Log("Apex was a test apex!")
4350 t.Fail()
4351 }
4352 // Ensure that main rule creates an output
4353 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4354
4355 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004356 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004357
4358 // Ensure that both direct and indirect deps are copied into apex
4359 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4360
Colin Cross7113d202019-11-20 16:39:12 -08004361 // Ensure that the platform variant ends with _shared
4362 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004363
Colin Cross56a83212020-09-15 18:30:11 -07004364 if !ctx.ModuleForTests("mylib_common", "android_arm64_armv8-a_shared_apex10000").Module().(*cc.Module).InAnyApex() {
Alex Light0851b882019-02-07 13:20:53 -08004365 t.Log("Found mylib_common not in any apex!")
4366 t.Fail()
4367 }
4368}
4369
4370func TestTestApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004371 ctx := testApex(t, `
Alex Light0851b882019-02-07 13:20:53 -08004372 apex_test {
4373 name: "myapex",
4374 key: "myapex.key",
4375 native_shared_libs: ["mylib_common_test"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004376 updatable: false,
Alex Light0851b882019-02-07 13:20:53 -08004377 }
4378
4379 apex_key {
4380 name: "myapex.key",
4381 public_key: "testkey.avbpubkey",
4382 private_key: "testkey.pem",
4383 }
4384
4385 cc_library {
4386 name: "mylib_common_test",
4387 srcs: ["mylib.cpp"],
4388 system_shared_libs: [],
4389 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004390 // TODO: remove //apex_available:platform
4391 apex_available: [
4392 "//apex_available:platform",
4393 "myapex",
4394 ],
Alex Light0851b882019-02-07 13:20:53 -08004395 }
4396 `)
4397
Sundong Ahnabb64432019-10-22 13:58:29 +09004398 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Alex Light0851b882019-02-07 13:20:53 -08004399 apexRule := module.Rule("apexRule")
4400 copyCmds := apexRule.Args["copy_commands"]
4401
4402 if apex, ok := module.Module().(*apexBundle); !ok || !apex.testApex {
4403 t.Log("Apex was not a test apex!")
4404 t.Fail()
4405 }
4406 // Ensure that main rule creates an output
4407 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4408
4409 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004410 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared_apex10000")
Alex Light0851b882019-02-07 13:20:53 -08004411
4412 // Ensure that both direct and indirect deps are copied into apex
4413 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common_test.so")
4414
Colin Cross7113d202019-11-20 16:39:12 -08004415 // Ensure that the platform variant ends with _shared
4416 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common_test"), "android_arm64_armv8-a_shared")
Alex Light0851b882019-02-07 13:20:53 -08004417}
4418
Alex Light9670d332019-01-29 18:07:33 -08004419func TestApexWithTarget(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004420 ctx := testApex(t, `
Alex Light9670d332019-01-29 18:07:33 -08004421 apex {
4422 name: "myapex",
4423 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004424 updatable: false,
Alex Light9670d332019-01-29 18:07:33 -08004425 multilib: {
4426 first: {
4427 native_shared_libs: ["mylib_common"],
4428 }
4429 },
4430 target: {
4431 android: {
4432 multilib: {
4433 first: {
4434 native_shared_libs: ["mylib"],
4435 }
4436 }
4437 },
4438 host: {
4439 multilib: {
4440 first: {
4441 native_shared_libs: ["mylib2"],
4442 }
4443 }
4444 }
4445 }
4446 }
4447
4448 apex_key {
4449 name: "myapex.key",
4450 public_key: "testkey.avbpubkey",
4451 private_key: "testkey.pem",
4452 }
4453
4454 cc_library {
4455 name: "mylib",
4456 srcs: ["mylib.cpp"],
4457 system_shared_libs: [],
4458 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004459 // TODO: remove //apex_available:platform
4460 apex_available: [
4461 "//apex_available:platform",
4462 "myapex",
4463 ],
Alex Light9670d332019-01-29 18:07:33 -08004464 }
4465
4466 cc_library {
4467 name: "mylib_common",
4468 srcs: ["mylib.cpp"],
4469 system_shared_libs: [],
4470 stl: "none",
4471 compile_multilib: "first",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00004472 // TODO: remove //apex_available:platform
4473 apex_available: [
4474 "//apex_available:platform",
4475 "myapex",
4476 ],
Alex Light9670d332019-01-29 18:07:33 -08004477 }
4478
4479 cc_library {
4480 name: "mylib2",
4481 srcs: ["mylib.cpp"],
4482 system_shared_libs: [],
4483 stl: "none",
4484 compile_multilib: "first",
4485 }
4486 `)
4487
Sundong Ahnabb64432019-10-22 13:58:29 +09004488 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Alex Light9670d332019-01-29 18:07:33 -08004489 copyCmds := apexRule.Args["copy_commands"]
4490
4491 // Ensure that main rule creates an output
4492 ensureContains(t, apexRule.Output.String(), "myapex.apex.unsigned")
4493
4494 // Ensure that apex variant is created for the direct dep
Colin Crossaede88c2020-08-11 12:17:01 -07004495 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared_apex10000")
4496 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared_apex10000")
4497 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared_apex10000")
Alex Light9670d332019-01-29 18:07:33 -08004498
4499 // Ensure that both direct and indirect deps are copied into apex
4500 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
4501 ensureContains(t, copyCmds, "image.apex/lib64/mylib_common.so")
4502 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
4503
Colin Cross7113d202019-11-20 16:39:12 -08004504 // Ensure that the platform variant ends with _shared
4505 ensureListContains(t, ctx.ModuleVariantsForTests("mylib"), "android_arm64_armv8-a_shared")
4506 ensureListContains(t, ctx.ModuleVariantsForTests("mylib_common"), "android_arm64_armv8-a_shared")
4507 ensureListContains(t, ctx.ModuleVariantsForTests("mylib2"), "android_arm64_armv8-a_shared")
Alex Light9670d332019-01-29 18:07:33 -08004508}
Jiyong Park04480cf2019-02-06 00:16:29 +09004509
Jiyong Park59140302020-12-14 18:44:04 +09004510func TestApexWithArch(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004511 ctx := testApex(t, `
Jiyong Park59140302020-12-14 18:44:04 +09004512 apex {
4513 name: "myapex",
4514 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004515 updatable: false,
Colin Cross70572ed2022-11-02 13:14:20 -07004516 native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004517 arch: {
4518 arm64: {
4519 native_shared_libs: ["mylib.arm64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004520 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004521 },
4522 x86_64: {
4523 native_shared_libs: ["mylib.x64"],
Colin Cross70572ed2022-11-02 13:14:20 -07004524 exclude_native_shared_libs: ["mylib.generic"],
Jiyong Park59140302020-12-14 18:44:04 +09004525 },
4526 }
4527 }
4528
4529 apex_key {
4530 name: "myapex.key",
4531 public_key: "testkey.avbpubkey",
4532 private_key: "testkey.pem",
4533 }
4534
4535 cc_library {
Colin Cross70572ed2022-11-02 13:14:20 -07004536 name: "mylib.generic",
4537 srcs: ["mylib.cpp"],
4538 system_shared_libs: [],
4539 stl: "none",
4540 // TODO: remove //apex_available:platform
4541 apex_available: [
4542 "//apex_available:platform",
4543 "myapex",
4544 ],
4545 }
4546
4547 cc_library {
Jiyong Park59140302020-12-14 18:44:04 +09004548 name: "mylib.arm64",
4549 srcs: ["mylib.cpp"],
4550 system_shared_libs: [],
4551 stl: "none",
4552 // TODO: remove //apex_available:platform
4553 apex_available: [
4554 "//apex_available:platform",
4555 "myapex",
4556 ],
4557 }
4558
4559 cc_library {
4560 name: "mylib.x64",
4561 srcs: ["mylib.cpp"],
4562 system_shared_libs: [],
4563 stl: "none",
4564 // TODO: remove //apex_available:platform
4565 apex_available: [
4566 "//apex_available:platform",
4567 "myapex",
4568 ],
4569 }
4570 `)
4571
4572 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
4573 copyCmds := apexRule.Args["copy_commands"]
4574
4575 // Ensure that apex variant is created for the direct dep
4576 ensureListContains(t, ctx.ModuleVariantsForTests("mylib.arm64"), "android_arm64_armv8-a_shared_apex10000")
Colin Cross70572ed2022-11-02 13:14:20 -07004577 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.generic"), "android_arm64_armv8-a_shared_apex10000")
Jiyong Park59140302020-12-14 18:44:04 +09004578 ensureListNotContains(t, ctx.ModuleVariantsForTests("mylib.x64"), "android_arm64_armv8-a_shared_apex10000")
4579
4580 // Ensure that both direct and indirect deps are copied into apex
4581 ensureContains(t, copyCmds, "image.apex/lib64/mylib.arm64.so")
4582 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib.x64.so")
4583}
4584
Jiyong Park04480cf2019-02-06 00:16:29 +09004585func TestApexWithShBinary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004586 ctx := testApex(t, `
Jiyong Park04480cf2019-02-06 00:16:29 +09004587 apex {
4588 name: "myapex",
4589 key: "myapex.key",
Sundong Ahn80c04892021-11-23 00:57:19 +00004590 sh_binaries: ["myscript"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004591 updatable: false,
Jiyong Park04480cf2019-02-06 00:16:29 +09004592 }
4593
4594 apex_key {
4595 name: "myapex.key",
4596 public_key: "testkey.avbpubkey",
4597 private_key: "testkey.pem",
4598 }
4599
4600 sh_binary {
4601 name: "myscript",
4602 src: "mylib.cpp",
4603 filename: "myscript.sh",
4604 sub_dir: "script",
4605 }
4606 `)
4607
Sundong Ahnabb64432019-10-22 13:58:29 +09004608 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Jiyong Park04480cf2019-02-06 00:16:29 +09004609 copyCmds := apexRule.Args["copy_commands"]
4610
4611 ensureContains(t, copyCmds, "image.apex/bin/script/myscript.sh")
4612}
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004613
Jooyung Han91df2082019-11-20 01:49:42 +09004614func TestApexInVariousPartition(t *testing.T) {
4615 testcases := []struct {
4616 propName, parition, flattenedPartition string
4617 }{
4618 {"", "system", "system_ext"},
4619 {"product_specific: true", "product", "product"},
4620 {"soc_specific: true", "vendor", "vendor"},
4621 {"proprietary: true", "vendor", "vendor"},
4622 {"vendor: true", "vendor", "vendor"},
4623 {"system_ext_specific: true", "system_ext", "system_ext"},
4624 }
4625 for _, tc := range testcases {
4626 t.Run(tc.propName+":"+tc.parition, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004627 ctx := testApex(t, `
Jooyung Han91df2082019-11-20 01:49:42 +09004628 apex {
4629 name: "myapex",
4630 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004631 updatable: false,
Jooyung Han91df2082019-11-20 01:49:42 +09004632 `+tc.propName+`
4633 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004634
Jooyung Han91df2082019-11-20 01:49:42 +09004635 apex_key {
4636 name: "myapex.key",
4637 public_key: "testkey.avbpubkey",
4638 private_key: "testkey.pem",
4639 }
4640 `)
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004641
Jooyung Han91df2082019-11-20 01:49:42 +09004642 apex := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004643 expected := "out/soong/target/product/test_device/" + tc.parition + "/apex"
4644 actual := apex.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004645 if actual != expected {
4646 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4647 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004648
Jooyung Han91df2082019-11-20 01:49:42 +09004649 flattened := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Paul Duffin37ba3442021-03-29 00:21:08 +01004650 expected = "out/soong/target/product/test_device/" + tc.flattenedPartition + "/apex"
4651 actual = flattened.installDir.RelativeToTop().String()
Jooyung Han91df2082019-11-20 01:49:42 +09004652 if actual != expected {
4653 t.Errorf("wrong install path. expected %q. actual %q", expected, actual)
4654 }
4655 })
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004656 }
Jiyong Parkd1e293d2019-03-15 02:13:21 +09004657}
Jiyong Park67882562019-03-21 01:11:21 +09004658
Jooyung Han580eb4f2020-06-24 19:33:06 +09004659func TestFileContexts_FindInDefaultLocationIfNotSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004660 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004661 apex {
4662 name: "myapex",
4663 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004664 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004665 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004666
Jooyung Han580eb4f2020-06-24 19:33:06 +09004667 apex_key {
4668 name: "myapex.key",
4669 public_key: "testkey.avbpubkey",
4670 private_key: "testkey.pem",
4671 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004672 `)
4673 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Jooyung Han580eb4f2020-06-24 19:33:06 +09004674 rule := module.Output("file_contexts")
4675 ensureContains(t, rule.RuleParams.Command, "cat system/sepolicy/apex/myapex-file_contexts")
4676}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004677
Jooyung Han580eb4f2020-06-24 19:33:06 +09004678func TestFileContexts_ShouldBeUnderSystemSepolicyForSystemApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004679 testApexError(t, `"myapex" .*: file_contexts: should be under system/sepolicy`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004680 apex {
4681 name: "myapex",
4682 key: "myapex.key",
4683 file_contexts: "my_own_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004684 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004685 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004686
Jooyung Han580eb4f2020-06-24 19:33:06 +09004687 apex_key {
4688 name: "myapex.key",
4689 public_key: "testkey.avbpubkey",
4690 private_key: "testkey.pem",
4691 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004692 `, withFiles(map[string][]byte{
4693 "my_own_file_contexts": nil,
4694 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004695}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004696
Jooyung Han580eb4f2020-06-24 19:33:06 +09004697func TestFileContexts_ProductSpecificApexes(t *testing.T) {
Jooyung Han54aca7b2019-11-20 02:26:02 +09004698 testApexError(t, `"myapex" .*: file_contexts: cannot find`, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004699 apex {
4700 name: "myapex",
4701 key: "myapex.key",
4702 product_specific: true,
4703 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004704 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004705 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004706
Jooyung Han580eb4f2020-06-24 19:33:06 +09004707 apex_key {
4708 name: "myapex.key",
4709 public_key: "testkey.avbpubkey",
4710 private_key: "testkey.pem",
4711 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004712 `)
4713
Colin Cross1c460562021-02-16 17:55:47 -08004714 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004715 apex {
4716 name: "myapex",
4717 key: "myapex.key",
4718 product_specific: true,
4719 file_contexts: "product_specific_file_contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004720 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004721 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004722
Jooyung Han580eb4f2020-06-24 19:33:06 +09004723 apex_key {
4724 name: "myapex.key",
4725 public_key: "testkey.avbpubkey",
4726 private_key: "testkey.pem",
4727 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004728 `, withFiles(map[string][]byte{
4729 "product_specific_file_contexts": nil,
4730 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004731 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4732 rule := module.Output("file_contexts")
4733 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
4734}
Jooyung Han54aca7b2019-11-20 02:26:02 +09004735
Jooyung Han580eb4f2020-06-24 19:33:06 +09004736func TestFileContexts_SetViaFileGroup(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004737 ctx := testApex(t, `
Jooyung Han580eb4f2020-06-24 19:33:06 +09004738 apex {
4739 name: "myapex",
4740 key: "myapex.key",
4741 product_specific: true,
4742 file_contexts: ":my-file-contexts",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00004743 updatable: false,
Jooyung Han580eb4f2020-06-24 19:33:06 +09004744 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004745
Jooyung Han580eb4f2020-06-24 19:33:06 +09004746 apex_key {
4747 name: "myapex.key",
4748 public_key: "testkey.avbpubkey",
4749 private_key: "testkey.pem",
4750 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004751
Jooyung Han580eb4f2020-06-24 19:33:06 +09004752 filegroup {
4753 name: "my-file-contexts",
4754 srcs: ["product_specific_file_contexts"],
4755 }
Jooyung Han54aca7b2019-11-20 02:26:02 +09004756 `, withFiles(map[string][]byte{
4757 "product_specific_file_contexts": nil,
4758 }))
Jooyung Han580eb4f2020-06-24 19:33:06 +09004759 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
4760 rule := module.Output("file_contexts")
4761 ensureContains(t, rule.RuleParams.Command, "cat product_specific_file_contexts")
Jooyung Han54aca7b2019-11-20 02:26:02 +09004762}
4763
Jiyong Park67882562019-03-21 01:11:21 +09004764func TestApexKeyFromOtherModule(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004765 ctx := testApex(t, `
Jiyong Park67882562019-03-21 01:11:21 +09004766 apex_key {
4767 name: "myapex.key",
4768 public_key: ":my.avbpubkey",
4769 private_key: ":my.pem",
4770 product_specific: true,
4771 }
4772
4773 filegroup {
4774 name: "my.avbpubkey",
4775 srcs: ["testkey2.avbpubkey"],
4776 }
4777
4778 filegroup {
4779 name: "my.pem",
4780 srcs: ["testkey2.pem"],
4781 }
4782 `)
4783
4784 apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
4785 expected_pubkey := "testkey2.avbpubkey"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004786 actual_pubkey := apex_key.publicKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004787 if actual_pubkey != expected_pubkey {
4788 t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
4789 }
4790 expected_privkey := "testkey2.pem"
Jaewoong Jung18aefc12020-12-21 09:11:10 -08004791 actual_privkey := apex_key.privateKeyFile.String()
Jiyong Park67882562019-03-21 01:11:21 +09004792 if actual_privkey != expected_privkey {
4793 t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
4794 }
4795}
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004796
4797func TestPrebuilt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004798 ctx := testApex(t, `
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004799 prebuilt_apex {
4800 name: "myapex",
Jiyong Parkc95714e2019-03-29 14:23:10 +09004801 arch: {
4802 arm64: {
4803 src: "myapex-arm64.apex",
4804 },
4805 arm: {
4806 src: "myapex-arm.apex",
4807 },
4808 },
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004809 }
4810 `)
4811
Wei Li340ee8e2022-03-18 17:33:24 -07004812 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4813 prebuilt := testingModule.Module().(*Prebuilt)
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004814
Jiyong Parkc95714e2019-03-29 14:23:10 +09004815 expectedInput := "myapex-arm64.apex"
4816 if prebuilt.inputApex.String() != expectedInput {
4817 t.Errorf("inputApex invalid. expected: %q, actual: %q", expectedInput, prebuilt.inputApex.String())
4818 }
Wei Li340ee8e2022-03-18 17:33:24 -07004819 android.AssertStringDoesContain(t, "Invalid provenance metadata file",
4820 prebuilt.ProvenanceMetaDataFile().String(), "soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto")
4821 rule := testingModule.Rule("genProvenanceMetaData")
4822 android.AssertStringEquals(t, "Invalid input", "myapex-arm64.apex", rule.Inputs[0].String())
4823 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4824 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4825 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.apex", rule.Args["install_path"])
Wei Li598f92d2023-01-04 17:12:24 -08004826
4827 entries := android.AndroidMkEntriesForTest(t, ctx, testingModule.Module())[0]
4828 android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "prebuilt_apex", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0])
Jaewoong Jung939ebd52019-03-26 15:07:36 -07004829}
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004830
Paul Duffinc0609c62021-03-01 17:27:16 +00004831func TestPrebuiltMissingSrc(t *testing.T) {
Paul Duffin6717d882021-06-15 19:09:41 +01004832 testApexError(t, `module "myapex" variant "android_common_myapex".*: prebuilt_apex does not support "arm64_armv8-a"`, `
Paul Duffinc0609c62021-03-01 17:27:16 +00004833 prebuilt_apex {
4834 name: "myapex",
4835 }
4836 `)
4837}
4838
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004839func TestPrebuiltFilenameOverride(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004840 ctx := testApex(t, `
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004841 prebuilt_apex {
4842 name: "myapex",
4843 src: "myapex-arm.apex",
4844 filename: "notmyapex.apex",
4845 }
4846 `)
4847
Wei Li340ee8e2022-03-18 17:33:24 -07004848 testingModule := ctx.ModuleForTests("myapex", "android_common_myapex")
4849 p := testingModule.Module().(*Prebuilt)
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004850
4851 expected := "notmyapex.apex"
4852 if p.installFilename != expected {
4853 t.Errorf("installFilename invalid. expected: %q, actual: %q", expected, p.installFilename)
4854 }
Wei Li340ee8e2022-03-18 17:33:24 -07004855 rule := testingModule.Rule("genProvenanceMetaData")
4856 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4857 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex/provenance_metadata.textproto", rule.Output.String())
4858 android.AssertStringEquals(t, "Invalid args", "myapex", rule.Args["module_name"])
4859 android.AssertStringEquals(t, "Invalid args", "/system/apex/notmyapex.apex", rule.Args["install_path"])
Nikita Ioffe7a41ebd2019-04-04 18:09:48 +01004860}
Jaewoong Jungc1001ec2019-06-25 11:20:53 -07004861
Samiul Islam7c02e262021-09-08 17:48:28 +01004862func TestApexSetFilenameOverride(t *testing.T) {
4863 testApex(t, `
4864 apex_set {
4865 name: "com.company.android.myapex",
4866 apex_name: "com.android.myapex",
4867 set: "company-myapex.apks",
4868 filename: "com.company.android.myapex.apex"
4869 }
4870 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4871
4872 testApex(t, `
4873 apex_set {
4874 name: "com.company.android.myapex",
4875 apex_name: "com.android.myapex",
4876 set: "company-myapex.apks",
4877 filename: "com.company.android.myapex.capex"
4878 }
4879 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4880
4881 testApexError(t, `filename should end in .apex or .capex for apex_set`, `
4882 apex_set {
4883 name: "com.company.android.myapex",
4884 apex_name: "com.android.myapex",
4885 set: "company-myapex.apks",
4886 filename: "some-random-suffix"
4887 }
4888 `)
4889}
4890
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004891func TestPrebuiltOverrides(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08004892 ctx := testApex(t, `
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004893 prebuilt_apex {
4894 name: "myapex.prebuilt",
4895 src: "myapex-arm.apex",
4896 overrides: [
4897 "myapex",
4898 ],
4899 }
4900 `)
4901
Wei Li340ee8e2022-03-18 17:33:24 -07004902 testingModule := ctx.ModuleForTests("myapex.prebuilt", "android_common_myapex.prebuilt")
4903 p := testingModule.Module().(*Prebuilt)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004904
4905 expected := []string{"myapex"}
Colin Crossaa255532020-07-03 13:18:24 -07004906 actual := android.AndroidMkEntriesForTest(t, ctx, p)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004907 if !reflect.DeepEqual(actual, expected) {
Jiyong Parkb0a012c2019-11-14 17:17:03 +09004908 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES value '%s', expected '%s'", actual, expected)
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004909 }
Wei Li340ee8e2022-03-18 17:33:24 -07004910 rule := testingModule.Rule("genProvenanceMetaData")
4911 android.AssertStringEquals(t, "Invalid input", "myapex-arm.apex", rule.Inputs[0].String())
4912 android.AssertStringEquals(t, "Invalid output", "out/soong/.intermediates/provenance_metadata/myapex.prebuilt/provenance_metadata.textproto", rule.Output.String())
4913 android.AssertStringEquals(t, "Invalid args", "myapex.prebuilt", rule.Args["module_name"])
4914 android.AssertStringEquals(t, "Invalid args", "/system/apex/myapex.prebuilt.apex", rule.Args["install_path"])
Jaewoong Jung22f7d182019-07-16 18:25:41 -07004915}
4916
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004917func TestPrebuiltApexName(t *testing.T) {
4918 testApex(t, `
4919 prebuilt_apex {
4920 name: "com.company.android.myapex",
4921 apex_name: "com.android.myapex",
4922 src: "company-myapex-arm.apex",
4923 }
4924 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4925
4926 testApex(t, `
4927 apex_set {
4928 name: "com.company.android.myapex",
4929 apex_name: "com.android.myapex",
4930 set: "company-myapex.apks",
4931 }
4932 `).ModuleForTests("com.company.android.myapex", "android_common_com.android.myapex")
4933}
4934
4935func TestPrebuiltApexNameWithPlatformBootclasspath(t *testing.T) {
4936 _ = android.GroupFixturePreparers(
4937 java.PrepareForTestWithJavaDefaultModules,
4938 PrepareForTestWithApexBuildComponents,
4939 android.FixtureWithRootAndroidBp(`
4940 platform_bootclasspath {
4941 name: "platform-bootclasspath",
4942 fragments: [
4943 {
4944 apex: "com.android.art",
4945 module: "art-bootclasspath-fragment",
4946 },
4947 ],
4948 }
4949
4950 prebuilt_apex {
4951 name: "com.company.android.art",
4952 apex_name: "com.android.art",
4953 src: "com.company.android.art-arm.apex",
4954 exported_bootclasspath_fragments: ["art-bootclasspath-fragment"],
4955 }
4956
4957 prebuilt_bootclasspath_fragment {
4958 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +01004959 image_name: "art",
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004960 contents: ["core-oj"],
Paul Duffin54e41972021-07-19 13:23:40 +01004961 hidden_api: {
4962 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
4963 metadata: "my-bootclasspath-fragment/metadata.csv",
4964 index: "my-bootclasspath-fragment/index.csv",
4965 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
4966 all_flags: "my-bootclasspath-fragment/all-flags.csv",
4967 },
Martin Stjernholmbfffae72021-06-24 14:37:13 +01004968 }
4969
4970 java_import {
4971 name: "core-oj",
4972 jars: ["prebuilt.jar"],
4973 }
4974 `),
4975 ).RunTest(t)
4976}
4977
Paul Duffin092153d2021-01-26 11:42:39 +00004978// These tests verify that the prebuilt_apex/deapexer to java_import wiring allows for the
4979// propagation of paths to dex implementation jars from the former to the latter.
Paul Duffin064b70c2020-11-02 17:32:38 +00004980func TestPrebuiltExportDexImplementationJars(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01004981 transform := android.NullFixturePreparer
Paul Duffin064b70c2020-11-02 17:32:38 +00004982
Paul Duffin89886cb2021-02-05 16:44:03 +00004983 checkDexJarBuildPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004984 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00004985 // Make sure the import has been given the correct path to the dex jar.
Colin Crossdcf71b22021-02-01 13:59:03 -08004986 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004987 dexJarBuildPath := p.DexJarBuildPath().PathOrNil()
Paul Duffin39853512021-02-26 11:09:39 +00004988 stem := android.RemoveOptionalPrebuiltPrefix(name)
Jeongik Chad5fe8782021-07-08 01:13:11 +09004989 android.AssertStringEquals(t, "DexJarBuildPath should be apex-related path.",
4990 ".intermediates/myapex.deapexer/android_common/deapexer/javalib/"+stem+".jar",
4991 android.NormalizePathForTesting(dexJarBuildPath))
4992 }
4993
4994 checkDexJarInstallPath := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01004995 t.Helper()
Jeongik Chad5fe8782021-07-08 01:13:11 +09004996 // Make sure the import has been given the correct path to the dex jar.
4997 p := ctx.ModuleForTests(name, "android_common_myapex").Module().(java.UsesLibraryDependency)
4998 dexJarBuildPath := p.DexJarInstallPath()
4999 stem := android.RemoveOptionalPrebuiltPrefix(name)
5000 android.AssertStringEquals(t, "DexJarInstallPath should be apex-related path.",
5001 "target/product/test_device/apex/myapex/javalib/"+stem+".jar",
5002 android.NormalizePathForTesting(dexJarBuildPath))
Paul Duffin064b70c2020-11-02 17:32:38 +00005003 }
5004
Paul Duffin39853512021-02-26 11:09:39 +00005005 ensureNoSourceVariant := func(t *testing.T, ctx *android.TestContext, name string) {
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +01005006 t.Helper()
Paul Duffin064b70c2020-11-02 17:32:38 +00005007 // Make sure that an apex variant is not created for the source module.
Jeongik Chad5fe8782021-07-08 01:13:11 +09005008 android.AssertArrayString(t, "Check if there is no source variant",
5009 []string{"android_common"},
5010 ctx.ModuleVariantsForTests(name))
Paul Duffin064b70c2020-11-02 17:32:38 +00005011 }
5012
5013 t.Run("prebuilt only", func(t *testing.T) {
5014 bp := `
5015 prebuilt_apex {
5016 name: "myapex",
5017 arch: {
5018 arm64: {
5019 src: "myapex-arm64.apex",
5020 },
5021 arm: {
5022 src: "myapex-arm.apex",
5023 },
5024 },
Paul Duffin39853512021-02-26 11:09:39 +00005025 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005026 }
5027
5028 java_import {
5029 name: "libfoo",
5030 jars: ["libfoo.jar"],
5031 }
Paul Duffin39853512021-02-26 11:09:39 +00005032
5033 java_sdk_library_import {
5034 name: "libbar",
5035 public: {
5036 jars: ["libbar.jar"],
5037 },
5038 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005039 `
5040
5041 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5042 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5043
Martin Stjernholm44825602021-09-17 01:44:12 +01005044 deapexerName := deapexerModuleName("myapex")
5045 android.AssertStringEquals(t, "APEX module name from deapexer name", "myapex", apexModuleName(deapexerName))
5046
Paul Duffinf6932af2021-02-26 18:21:56 +00005047 // Make sure that the deapexer has the correct input APEX.
Martin Stjernholm44825602021-09-17 01:44:12 +01005048 deapexer := ctx.ModuleForTests(deapexerName, "android_common")
Paul Duffinf6932af2021-02-26 18:21:56 +00005049 rule := deapexer.Rule("deapexer")
5050 if expected, actual := []string{"myapex-arm64.apex"}, android.NormalizePathsForTesting(rule.Implicits); !reflect.DeepEqual(expected, actual) {
5051 t.Errorf("expected: %q, found: %q", expected, actual)
5052 }
5053
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005054 // Make sure that the prebuilt_apex has the correct input APEX.
Paul Duffin6717d882021-06-15 19:09:41 +01005055 prebuiltApex := ctx.ModuleForTests("myapex", "android_common_myapex")
Paul Duffin0d10c3c2021-03-01 17:09:32 +00005056 rule = prebuiltApex.Rule("android/soong/android.Cp")
5057 if expected, actual := "myapex-arm64.apex", android.NormalizePathForTesting(rule.Input); !reflect.DeepEqual(expected, actual) {
5058 t.Errorf("expected: %q, found: %q", expected, actual)
5059 }
5060
Paul Duffin89886cb2021-02-05 16:44:03 +00005061 checkDexJarBuildPath(t, ctx, "libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005062 checkDexJarInstallPath(t, ctx, "libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005063
5064 checkDexJarBuildPath(t, ctx, "libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005065 checkDexJarInstallPath(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005066 })
5067
5068 t.Run("prebuilt with source preferred", func(t *testing.T) {
5069
5070 bp := `
5071 prebuilt_apex {
5072 name: "myapex",
5073 arch: {
5074 arm64: {
5075 src: "myapex-arm64.apex",
5076 },
5077 arm: {
5078 src: "myapex-arm.apex",
5079 },
5080 },
Paul Duffin39853512021-02-26 11:09:39 +00005081 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005082 }
5083
5084 java_import {
5085 name: "libfoo",
5086 jars: ["libfoo.jar"],
5087 }
5088
5089 java_library {
5090 name: "libfoo",
5091 }
Paul Duffin39853512021-02-26 11:09:39 +00005092
5093 java_sdk_library_import {
5094 name: "libbar",
5095 public: {
5096 jars: ["libbar.jar"],
5097 },
5098 }
5099
5100 java_sdk_library {
5101 name: "libbar",
5102 srcs: ["foo/bar/MyClass.java"],
5103 unsafe_ignore_missing_latest_api: true,
5104 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005105 `
5106
5107 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5108 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5109
Paul Duffin89886cb2021-02-05 16:44:03 +00005110 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005111 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005112 ensureNoSourceVariant(t, ctx, "libfoo")
5113
5114 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005115 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005116 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005117 })
5118
5119 t.Run("prebuilt preferred with source", func(t *testing.T) {
5120 bp := `
5121 prebuilt_apex {
5122 name: "myapex",
Paul Duffin064b70c2020-11-02 17:32:38 +00005123 arch: {
5124 arm64: {
5125 src: "myapex-arm64.apex",
5126 },
5127 arm: {
5128 src: "myapex-arm.apex",
5129 },
5130 },
Paul Duffin39853512021-02-26 11:09:39 +00005131 exported_java_libs: ["libfoo", "libbar"],
Paul Duffin064b70c2020-11-02 17:32:38 +00005132 }
5133
5134 java_import {
5135 name: "libfoo",
Paul Duffin092153d2021-01-26 11:42:39 +00005136 prefer: true,
Paul Duffin064b70c2020-11-02 17:32:38 +00005137 jars: ["libfoo.jar"],
5138 }
5139
5140 java_library {
5141 name: "libfoo",
5142 }
Paul Duffin39853512021-02-26 11:09:39 +00005143
5144 java_sdk_library_import {
5145 name: "libbar",
5146 prefer: true,
5147 public: {
5148 jars: ["libbar.jar"],
5149 },
5150 }
5151
5152 java_sdk_library {
5153 name: "libbar",
5154 srcs: ["foo/bar/MyClass.java"],
5155 unsafe_ignore_missing_latest_api: true,
5156 }
Paul Duffin064b70c2020-11-02 17:32:38 +00005157 `
5158
5159 // Make sure that dexpreopt can access dex implementation files from the prebuilt.
5160 ctx := testDexpreoptWithApexes(t, bp, "", transform)
5161
Paul Duffin89886cb2021-02-05 16:44:03 +00005162 checkDexJarBuildPath(t, ctx, "prebuilt_libfoo")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005163 checkDexJarInstallPath(t, ctx, "prebuilt_libfoo")
Paul Duffin39853512021-02-26 11:09:39 +00005164 ensureNoSourceVariant(t, ctx, "libfoo")
5165
5166 checkDexJarBuildPath(t, ctx, "prebuilt_libbar")
Jeongik Chad5fe8782021-07-08 01:13:11 +09005167 checkDexJarInstallPath(t, ctx, "prebuilt_libbar")
Paul Duffin39853512021-02-26 11:09:39 +00005168 ensureNoSourceVariant(t, ctx, "libbar")
Paul Duffin064b70c2020-11-02 17:32:38 +00005169 })
5170}
5171
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005172func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) {
Paul Duffinb6f53c02021-05-14 07:52:42 +01005173 preparer := android.GroupFixturePreparers(
satayevabcd5972021-08-06 17:49:46 +01005174 java.FixtureConfigureApexBootJars("myapex:libfoo", "myapex:libbar"),
Paul Duffinb6f53c02021-05-14 07:52:42 +01005175 // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding
5176 // is disabled.
5177 android.FixtureAddTextFile("frameworks/base/Android.bp", ""),
5178 )
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005179
Paul Duffin37856732021-02-26 14:24:15 +00005180 checkBootDexJarPath := func(t *testing.T, ctx *android.TestContext, stem string, bootDexJarPath string) {
5181 t.Helper()
Paul Duffin7ebebfd2021-04-27 19:36:57 +01005182 s := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005183 foundLibfooJar := false
Paul Duffin37856732021-02-26 14:24:15 +00005184 base := stem + ".jar"
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005185 for _, output := range s.AllOutputs() {
Paul Duffin37856732021-02-26 14:24:15 +00005186 if filepath.Base(output) == base {
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005187 foundLibfooJar = true
5188 buildRule := s.Output(output)
Paul Duffin55607122021-03-30 23:32:51 +01005189 android.AssertStringEquals(t, "boot dex jar path", bootDexJarPath, buildRule.Input.String())
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005190 }
5191 }
5192 if !foundLibfooJar {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02005193 t.Errorf("Rule for libfoo.jar missing in dex_bootjars singleton outputs %q", android.StringPathsRelativeToTop(ctx.Config().SoongOutDir(), s.AllOutputs()))
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005194 }
5195 }
5196
Paul Duffin40a3f652021-07-19 13:11:24 +01005197 checkHiddenAPIIndexFromClassesInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
Paul Duffin37856732021-02-26 14:24:15 +00005198 t.Helper()
Paul Duffin00b2bfd2021-04-12 17:24:36 +01005199 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
Paul Duffind061d402021-06-07 21:36:01 +01005200 var rule android.TestingBuildParams
5201
5202 rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv")
5203 java.CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule)
Paul Duffin4fd997b2021-02-03 20:06:33 +00005204 }
5205
Paul Duffin40a3f652021-07-19 13:11:24 +01005206 checkHiddenAPIIndexFromFlagsInputs := func(t *testing.T, ctx *android.TestContext, expectedIntermediateInputs string) {
5207 t.Helper()
5208 platformBootclasspath := ctx.ModuleForTests("platform-bootclasspath", "android_common")
5209 var rule android.TestingBuildParams
5210
5211 rule = platformBootclasspath.Output("hiddenapi-index.csv")
5212 java.CheckHiddenAPIRuleInputs(t, "monolithic index", expectedIntermediateInputs, rule)
5213 }
5214
Paul Duffin89f570a2021-06-16 01:42:33 +01005215 fragment := java.ApexVariantReference{
5216 Apex: proptools.StringPtr("myapex"),
5217 Module: proptools.StringPtr("my-bootclasspath-fragment"),
5218 }
5219
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005220 t.Run("prebuilt only", func(t *testing.T) {
5221 bp := `
5222 prebuilt_apex {
5223 name: "myapex",
5224 arch: {
5225 arm64: {
5226 src: "myapex-arm64.apex",
5227 },
5228 arm: {
5229 src: "myapex-arm.apex",
5230 },
5231 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005232 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5233 }
5234
5235 prebuilt_bootclasspath_fragment {
5236 name: "my-bootclasspath-fragment",
5237 contents: ["libfoo", "libbar"],
5238 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005239 hidden_api: {
5240 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5241 metadata: "my-bootclasspath-fragment/metadata.csv",
5242 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005243 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5244 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5245 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005246 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005247 }
5248
5249 java_import {
5250 name: "libfoo",
5251 jars: ["libfoo.jar"],
5252 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005253 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005254 }
Paul Duffin37856732021-02-26 14:24:15 +00005255
5256 java_sdk_library_import {
5257 name: "libbar",
5258 public: {
5259 jars: ["libbar.jar"],
5260 },
5261 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005262 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005263 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005264 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005265 `
5266
Paul Duffin89f570a2021-06-16 01:42:33 +01005267 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005268 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5269 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005270
Paul Duffin537ea3d2021-05-14 10:38:00 +01005271 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005272 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005273 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005274 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005275 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5276 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005277 })
5278
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005279 t.Run("apex_set only", func(t *testing.T) {
5280 bp := `
5281 apex_set {
5282 name: "myapex",
5283 set: "myapex.apks",
Paul Duffin89f570a2021-06-16 01:42:33 +01005284 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5285 }
5286
5287 prebuilt_bootclasspath_fragment {
5288 name: "my-bootclasspath-fragment",
5289 contents: ["libfoo", "libbar"],
5290 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005291 hidden_api: {
5292 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5293 metadata: "my-bootclasspath-fragment/metadata.csv",
5294 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005295 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5296 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5297 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005298 },
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005299 }
5300
5301 java_import {
5302 name: "libfoo",
5303 jars: ["libfoo.jar"],
5304 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005305 permitted_packages: ["foo"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005306 }
5307
5308 java_sdk_library_import {
5309 name: "libbar",
5310 public: {
5311 jars: ["libbar.jar"],
5312 },
5313 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005314 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005315 permitted_packages: ["bar"],
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005316 }
5317 `
5318
Paul Duffin89f570a2021-06-16 01:42:33 +01005319 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005320 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5321 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
5322
Paul Duffin537ea3d2021-05-14 10:38:00 +01005323 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005324 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005325 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005326 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005327 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5328 `)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01005329 })
5330
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005331 t.Run("prebuilt with source library preferred", func(t *testing.T) {
5332 bp := `
5333 prebuilt_apex {
5334 name: "myapex",
5335 arch: {
5336 arm64: {
5337 src: "myapex-arm64.apex",
5338 },
5339 arm: {
5340 src: "myapex-arm.apex",
5341 },
5342 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005343 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5344 }
5345
5346 prebuilt_bootclasspath_fragment {
5347 name: "my-bootclasspath-fragment",
5348 contents: ["libfoo", "libbar"],
5349 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005350 hidden_api: {
5351 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5352 metadata: "my-bootclasspath-fragment/metadata.csv",
5353 index: "my-bootclasspath-fragment/index.csv",
5354 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
5355 all_flags: "my-bootclasspath-fragment/all-flags.csv",
5356 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005357 }
5358
5359 java_import {
5360 name: "libfoo",
5361 jars: ["libfoo.jar"],
5362 apex_available: ["myapex"],
5363 }
5364
5365 java_library {
5366 name: "libfoo",
5367 srcs: ["foo/bar/MyClass.java"],
5368 apex_available: ["myapex"],
5369 }
Paul Duffin37856732021-02-26 14:24:15 +00005370
5371 java_sdk_library_import {
5372 name: "libbar",
5373 public: {
5374 jars: ["libbar.jar"],
5375 },
5376 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005377 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005378 }
5379
5380 java_sdk_library {
5381 name: "libbar",
5382 srcs: ["foo/bar/MyClass.java"],
5383 unsafe_ignore_missing_latest_api: true,
5384 apex_available: ["myapex"],
5385 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005386 `
5387
5388 // In this test the source (java_library) libfoo is active since the
5389 // prebuilt (java_import) defaults to prefer:false. However the
5390 // prebuilt_apex module always depends on the prebuilt, and so it doesn't
5391 // find the dex boot jar in it. We either need to disable the source libfoo
5392 // or make the prebuilt libfoo preferred.
Paul Duffin89f570a2021-06-16 01:42:33 +01005393 testDexpreoptWithApexes(t, bp, "module libfoo does not provide a dex boot jar", preparer, fragment)
Spandan Das10ea4bf2021-08-20 19:18:16 +00005394 // dexbootjar check is skipped if AllowMissingDependencies is true
5395 preparerAllowMissingDeps := android.GroupFixturePreparers(
5396 preparer,
5397 android.PrepareForTestWithAllowMissingDependencies,
5398 )
5399 testDexpreoptWithApexes(t, bp, "", preparerAllowMissingDeps, fragment)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005400 })
5401
5402 t.Run("prebuilt library preferred with source", func(t *testing.T) {
5403 bp := `
5404 prebuilt_apex {
5405 name: "myapex",
5406 arch: {
5407 arm64: {
5408 src: "myapex-arm64.apex",
5409 },
5410 arm: {
5411 src: "myapex-arm.apex",
5412 },
5413 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005414 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5415 }
5416
5417 prebuilt_bootclasspath_fragment {
5418 name: "my-bootclasspath-fragment",
5419 contents: ["libfoo", "libbar"],
5420 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005421 hidden_api: {
5422 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5423 metadata: "my-bootclasspath-fragment/metadata.csv",
5424 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005425 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5426 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5427 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005428 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005429 }
5430
5431 java_import {
5432 name: "libfoo",
5433 prefer: true,
5434 jars: ["libfoo.jar"],
5435 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005436 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005437 }
5438
5439 java_library {
5440 name: "libfoo",
5441 srcs: ["foo/bar/MyClass.java"],
5442 apex_available: ["myapex"],
5443 }
Paul Duffin37856732021-02-26 14:24:15 +00005444
5445 java_sdk_library_import {
5446 name: "libbar",
5447 prefer: true,
5448 public: {
5449 jars: ["libbar.jar"],
5450 },
5451 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005452 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005453 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005454 }
5455
5456 java_sdk_library {
5457 name: "libbar",
5458 srcs: ["foo/bar/MyClass.java"],
5459 unsafe_ignore_missing_latest_api: true,
5460 apex_available: ["myapex"],
5461 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005462 `
5463
Paul Duffin89f570a2021-06-16 01:42:33 +01005464 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005465 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5466 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005467
Paul Duffin537ea3d2021-05-14 10:38:00 +01005468 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005469 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005470 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005471 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005472 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5473 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005474 })
5475
5476 t.Run("prebuilt with source apex preferred", func(t *testing.T) {
5477 bp := `
5478 apex {
5479 name: "myapex",
5480 key: "myapex.key",
Paul Duffin37856732021-02-26 14:24:15 +00005481 java_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005482 updatable: false,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005483 }
5484
5485 apex_key {
5486 name: "myapex.key",
5487 public_key: "testkey.avbpubkey",
5488 private_key: "testkey.pem",
5489 }
5490
5491 prebuilt_apex {
5492 name: "myapex",
5493 arch: {
5494 arm64: {
5495 src: "myapex-arm64.apex",
5496 },
5497 arm: {
5498 src: "myapex-arm.apex",
5499 },
5500 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005501 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5502 }
5503
5504 prebuilt_bootclasspath_fragment {
5505 name: "my-bootclasspath-fragment",
5506 contents: ["libfoo", "libbar"],
5507 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005508 hidden_api: {
5509 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5510 metadata: "my-bootclasspath-fragment/metadata.csv",
5511 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005512 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5513 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5514 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005515 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005516 }
5517
5518 java_import {
5519 name: "libfoo",
5520 jars: ["libfoo.jar"],
5521 apex_available: ["myapex"],
5522 }
5523
5524 java_library {
5525 name: "libfoo",
5526 srcs: ["foo/bar/MyClass.java"],
5527 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005528 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005529 }
Paul Duffin37856732021-02-26 14:24:15 +00005530
5531 java_sdk_library_import {
5532 name: "libbar",
5533 public: {
5534 jars: ["libbar.jar"],
5535 },
5536 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005537 shared_library: false,
Paul Duffin37856732021-02-26 14:24:15 +00005538 }
5539
5540 java_sdk_library {
5541 name: "libbar",
5542 srcs: ["foo/bar/MyClass.java"],
5543 unsafe_ignore_missing_latest_api: true,
5544 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005545 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005546 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005547 `
5548
Paul Duffin89f570a2021-06-16 01:42:33 +01005549 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005550 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar")
5551 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/libbar/android_common_myapex/hiddenapi/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005552
Paul Duffin537ea3d2021-05-14 10:38:00 +01005553 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005554 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005555 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005556 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005557 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5558 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005559 })
5560
5561 t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) {
5562 bp := `
5563 apex {
5564 name: "myapex",
5565 enabled: false,
5566 key: "myapex.key",
Paul Duffin8f146b92021-04-12 17:24:18 +01005567 java_libs: ["libfoo", "libbar"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005568 }
5569
5570 apex_key {
5571 name: "myapex.key",
5572 public_key: "testkey.avbpubkey",
5573 private_key: "testkey.pem",
5574 }
5575
5576 prebuilt_apex {
5577 name: "myapex",
5578 arch: {
5579 arm64: {
5580 src: "myapex-arm64.apex",
5581 },
5582 arm: {
5583 src: "myapex-arm.apex",
5584 },
5585 },
Paul Duffin89f570a2021-06-16 01:42:33 +01005586 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
5587 }
5588
5589 prebuilt_bootclasspath_fragment {
5590 name: "my-bootclasspath-fragment",
5591 contents: ["libfoo", "libbar"],
5592 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01005593 hidden_api: {
5594 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
5595 metadata: "my-bootclasspath-fragment/metadata.csv",
5596 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01005597 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
5598 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
5599 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01005600 },
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005601 }
5602
5603 java_import {
5604 name: "libfoo",
5605 prefer: true,
5606 jars: ["libfoo.jar"],
5607 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01005608 permitted_packages: ["foo"],
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005609 }
5610
5611 java_library {
5612 name: "libfoo",
5613 srcs: ["foo/bar/MyClass.java"],
5614 apex_available: ["myapex"],
5615 }
Paul Duffin37856732021-02-26 14:24:15 +00005616
5617 java_sdk_library_import {
5618 name: "libbar",
5619 prefer: true,
5620 public: {
5621 jars: ["libbar.jar"],
5622 },
5623 apex_available: ["myapex"],
Paul Duffin89f570a2021-06-16 01:42:33 +01005624 shared_library: false,
satayevabcd5972021-08-06 17:49:46 +01005625 permitted_packages: ["bar"],
Paul Duffin37856732021-02-26 14:24:15 +00005626 }
5627
5628 java_sdk_library {
5629 name: "libbar",
5630 srcs: ["foo/bar/MyClass.java"],
5631 unsafe_ignore_missing_latest_api: true,
5632 apex_available: ["myapex"],
5633 }
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005634 `
5635
Paul Duffin89f570a2021-06-16 01:42:33 +01005636 ctx := testDexpreoptWithApexes(t, bp, "", preparer, fragment)
Paul Duffin55607122021-03-30 23:32:51 +01005637 checkBootDexJarPath(t, ctx, "libfoo", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar")
5638 checkBootDexJarPath(t, ctx, "libbar", "out/soong/.intermediates/myapex.deapexer/android_common/deapexer/javalib/libbar.jar")
Paul Duffin4fd997b2021-02-03 20:06:33 +00005639
Paul Duffin537ea3d2021-05-14 10:38:00 +01005640 // Verify the correct module jars contribute to the hiddenapi index file.
Paul Duffin54e41972021-07-19 13:23:40 +01005641 checkHiddenAPIIndexFromClassesInputs(t, ctx, ``)
Paul Duffin40a3f652021-07-19 13:11:24 +01005642 checkHiddenAPIIndexFromFlagsInputs(t, ctx, `
Paul Duffin54e41972021-07-19 13:23:40 +01005643 my-bootclasspath-fragment/index.csv
Paul Duffin40a3f652021-07-19 13:11:24 +01005644 out/soong/.intermediates/frameworks/base/boot/platform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv
5645 `)
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00005646 })
5647}
5648
Jooyung Han95513842023-04-17 11:24:54 +09005649func TestPrebuiltSkipsSymbols(t *testing.T) {
5650 testCases := []struct {
5651 name string
5652 usePrebuilt bool
5653 installSymbolFiles bool
5654 }{
5655 {
5656 name: "Source module build rule doesn't install symbol files",
5657 usePrebuilt: true,
5658 installSymbolFiles: false,
5659 },
5660 {
5661 name: "Source module is installed with symbols",
5662 usePrebuilt: false,
5663 installSymbolFiles: true,
5664 },
5665 }
5666 for _, tc := range testCases {
5667 t.Run(tc.name, func(t *testing.T) {
5668 preferProperty := "prefer: false"
5669 if tc.usePrebuilt {
5670 preferProperty = "prefer: true"
5671 }
5672 ctx := testApex(t, `
5673 // Source module
5674 apex {
5675 name: "myapex",
5676 key: "myapex.key",
5677 updatable: false,
5678 }
5679
5680 apex_key {
5681 name: "myapex.key",
5682 public_key: "testkey.avbpubkey",
5683 private_key: "testkey.pem",
5684 }
5685
5686 apex_set {
5687 name: "myapex",
5688 set: "myapex.apks",
5689 `+preferProperty+`
5690 }
5691 `)
5692 // Symbol files are installed by installing entries under ${OUT}/apex/{apex name}
5693 android.AssertStringListContainsEquals(t, "Implicits",
5694 ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Implicits.Strings(),
5695 "out/soong/target/product/test_device/apex/myapex/apex_manifest.pb",
5696 tc.installSymbolFiles)
5697 })
5698 }
5699}
5700
Roland Levillain630846d2019-06-26 12:48:34 +01005701func TestApexWithTests(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005702 ctx := testApex(t, `
Roland Levillain630846d2019-06-26 12:48:34 +01005703 apex_test {
5704 name: "myapex",
5705 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005706 updatable: false,
Roland Levillain630846d2019-06-26 12:48:34 +01005707 tests: [
5708 "mytest",
Roland Levillain9b5fde92019-06-28 15:41:19 +01005709 "mytests",
Roland Levillain630846d2019-06-26 12:48:34 +01005710 ],
5711 }
5712
5713 apex_key {
5714 name: "myapex.key",
5715 public_key: "testkey.avbpubkey",
5716 private_key: "testkey.pem",
5717 }
5718
Liz Kammer1c14a212020-05-12 15:26:55 -07005719 filegroup {
5720 name: "fg",
5721 srcs: [
5722 "baz",
5723 "bar/baz"
5724 ],
5725 }
5726
Roland Levillain630846d2019-06-26 12:48:34 +01005727 cc_test {
5728 name: "mytest",
5729 gtest: false,
5730 srcs: ["mytest.cpp"],
5731 relative_install_path: "test",
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005732 shared_libs: ["mylib"],
Roland Levillain630846d2019-06-26 12:48:34 +01005733 system_shared_libs: [],
5734 static_executable: true,
5735 stl: "none",
Liz Kammer1c14a212020-05-12 15:26:55 -07005736 data: [":fg"],
Roland Levillain630846d2019-06-26 12:48:34 +01005737 }
Roland Levillain9b5fde92019-06-28 15:41:19 +01005738
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005739 cc_library {
5740 name: "mylib",
5741 srcs: ["mylib.cpp"],
5742 system_shared_libs: [],
5743 stl: "none",
5744 }
5745
Liz Kammer5bd365f2020-05-27 15:15:11 -07005746 filegroup {
5747 name: "fg2",
5748 srcs: [
5749 "testdata/baz"
5750 ],
5751 }
5752
Roland Levillain9b5fde92019-06-28 15:41:19 +01005753 cc_test {
5754 name: "mytests",
5755 gtest: false,
5756 srcs: [
5757 "mytest1.cpp",
5758 "mytest2.cpp",
5759 "mytest3.cpp",
5760 ],
5761 test_per_src: true,
5762 relative_install_path: "test",
5763 system_shared_libs: [],
5764 static_executable: true,
5765 stl: "none",
Liz Kammer5bd365f2020-05-27 15:15:11 -07005766 data: [
5767 ":fg",
5768 ":fg2",
5769 ],
Roland Levillain9b5fde92019-06-28 15:41:19 +01005770 }
Roland Levillain630846d2019-06-26 12:48:34 +01005771 `)
5772
Sundong Ahnabb64432019-10-22 13:58:29 +09005773 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
Roland Levillain630846d2019-06-26 12:48:34 +01005774 copyCmds := apexRule.Args["copy_commands"]
5775
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005776 // Ensure that test dep (and their transitive dependencies) are copied into apex.
Roland Levillain630846d2019-06-26 12:48:34 +01005777 ensureContains(t, copyCmds, "image.apex/bin/test/mytest")
Jiyong Parkaf9539f2020-05-04 10:31:32 +09005778 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
Roland Levillain9b5fde92019-06-28 15:41:19 +01005779
Liz Kammer1c14a212020-05-12 15:26:55 -07005780 //Ensure that test data are copied into apex.
5781 ensureContains(t, copyCmds, "image.apex/bin/test/baz")
5782 ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz")
5783
Roland Levillain9b5fde92019-06-28 15:41:19 +01005784 // Ensure that test deps built with `test_per_src` are copied into apex.
5785 ensureContains(t, copyCmds, "image.apex/bin/test/mytest1")
5786 ensureContains(t, copyCmds, "image.apex/bin/test/mytest2")
5787 ensureContains(t, copyCmds, "image.apex/bin/test/mytest3")
Roland Levillainf89cd092019-07-29 16:22:59 +01005788
5789 // Ensure the module is correctly translated.
Liz Kammer81faaaf2020-05-20 09:57:08 -07005790 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005791 data := android.AndroidMkDataForTest(t, ctx, bundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005792 name := bundle.BaseModuleName()
Roland Levillainf89cd092019-07-29 16:22:59 +01005793 prefix := "TARGET_"
5794 var builder strings.Builder
5795 data.Custom(&builder, name, prefix, "", data)
5796 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005797 ensureContains(t, androidMk, "LOCAL_MODULE := mytest.myapex\n")
5798 ensureContains(t, androidMk, "LOCAL_MODULE := mytest1.myapex\n")
5799 ensureContains(t, androidMk, "LOCAL_MODULE := mytest2.myapex\n")
5800 ensureContains(t, androidMk, "LOCAL_MODULE := mytest3.myapex\n")
5801 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex\n")
5802 ensureContains(t, androidMk, "LOCAL_MODULE := apex_pubkey.myapex\n")
Roland Levillainf89cd092019-07-29 16:22:59 +01005803 ensureContains(t, androidMk, "LOCAL_MODULE := myapex\n")
Liz Kammer81faaaf2020-05-20 09:57:08 -07005804
5805 flatBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07005806 data = android.AndroidMkDataForTest(t, ctx, flatBundle)
Liz Kammer81faaaf2020-05-20 09:57:08 -07005807 data.Custom(&builder, name, prefix, "", data)
5808 flatAndroidMk := builder.String()
Liz Kammer5bd365f2020-05-27 15:15:11 -07005809 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :baz :bar/baz\n")
5810 ensureContainsOnce(t, flatAndroidMk, "LOCAL_TEST_DATA := :testdata/baz\n")
Roland Levillain630846d2019-06-26 12:48:34 +01005811}
5812
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005813func TestInstallExtraFlattenedApexes(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005814 ctx := testApex(t, `
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005815 apex {
5816 name: "myapex",
5817 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005818 updatable: false,
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005819 }
5820 apex_key {
5821 name: "myapex.key",
5822 public_key: "testkey.avbpubkey",
5823 private_key: "testkey.pem",
5824 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00005825 `,
5826 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5827 variables.InstallExtraFlattenedApexes = proptools.BoolPtr(true)
5828 }),
5829 )
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005830 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00005831 ensureListContains(t, ab.makeModulesToInstall, "myapex.flattened")
Colin Crossaa255532020-07-03 13:18:24 -07005832 mk := android.AndroidMkDataForTest(t, ctx, ab)
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005833 var builder strings.Builder
5834 mk.Custom(&builder, ab.Name(), "TARGET_", "", mk)
5835 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00005836 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex myapex.flattened\n")
Jooyung Han3ab2c3e2019-12-05 16:27:44 +09005837}
5838
Jooyung Hand48f3c32019-08-23 11:18:57 +09005839func TestErrorsIfDepsAreNotEnabled(t *testing.T) {
5840 testApexError(t, `module "myapex" .* depends on disabled module "libfoo"`, `
5841 apex {
5842 name: "myapex",
5843 key: "myapex.key",
5844 native_shared_libs: ["libfoo"],
5845 }
5846
5847 apex_key {
5848 name: "myapex.key",
5849 public_key: "testkey.avbpubkey",
5850 private_key: "testkey.pem",
5851 }
5852
5853 cc_library {
5854 name: "libfoo",
5855 stl: "none",
5856 system_shared_libs: [],
5857 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005858 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005859 }
5860 `)
5861 testApexError(t, `module "myapex" .* depends on disabled module "myjar"`, `
5862 apex {
5863 name: "myapex",
5864 key: "myapex.key",
5865 java_libs: ["myjar"],
5866 }
5867
5868 apex_key {
5869 name: "myapex.key",
5870 public_key: "testkey.avbpubkey",
5871 private_key: "testkey.pem",
5872 }
5873
5874 java_library {
5875 name: "myjar",
5876 srcs: ["foo/bar/MyClass.java"],
5877 sdk_version: "none",
5878 system_modules: "none",
Jooyung Hand48f3c32019-08-23 11:18:57 +09005879 enabled: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09005880 apex_available: ["myapex"],
Jooyung Hand48f3c32019-08-23 11:18:57 +09005881 }
5882 `)
5883}
5884
Bill Peckhama41a6962021-01-11 10:58:54 -08005885func TestApexWithJavaImport(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005886 ctx := testApex(t, `
Bill Peckhama41a6962021-01-11 10:58:54 -08005887 apex {
5888 name: "myapex",
5889 key: "myapex.key",
5890 java_libs: ["myjavaimport"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005891 updatable: false,
Bill Peckhama41a6962021-01-11 10:58:54 -08005892 }
5893
5894 apex_key {
5895 name: "myapex.key",
5896 public_key: "testkey.avbpubkey",
5897 private_key: "testkey.pem",
5898 }
5899
5900 java_import {
5901 name: "myjavaimport",
5902 apex_available: ["myapex"],
5903 jars: ["my.jar"],
5904 compile_dex: true,
5905 }
5906 `)
5907
5908 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
5909 apexRule := module.Rule("apexRule")
5910 copyCmds := apexRule.Args["copy_commands"]
5911 ensureContains(t, copyCmds, "image.apex/javalib/myjavaimport.jar")
5912}
5913
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005914func TestApexWithApps(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08005915 ctx := testApex(t, `
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005916 apex {
5917 name: "myapex",
5918 key: "myapex.key",
5919 apps: [
5920 "AppFoo",
Jiyong Parkf7487312019-10-17 12:54:30 +09005921 "AppFooPriv",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005922 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00005923 updatable: false,
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005924 }
5925
5926 apex_key {
5927 name: "myapex.key",
5928 public_key: "testkey.avbpubkey",
5929 private_key: "testkey.pem",
5930 }
5931
5932 android_app {
5933 name: "AppFoo",
5934 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005935 sdk_version: "current",
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005936 system_modules: "none",
Jiyong Park8be103b2019-11-08 15:53:48 +09005937 jni_libs: ["libjni"],
Colin Cross094cde42020-02-15 10:38:00 -08005938 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005939 apex_available: [ "myapex" ],
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005940 }
Jiyong Parkf7487312019-10-17 12:54:30 +09005941
5942 android_app {
5943 name: "AppFooPriv",
5944 srcs: ["foo/bar/MyClass.java"],
Colin Cross094cde42020-02-15 10:38:00 -08005945 sdk_version: "current",
Jiyong Parkf7487312019-10-17 12:54:30 +09005946 system_modules: "none",
5947 privileged: true,
Colin Cross094cde42020-02-15 10:38:00 -08005948 stl: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00005949 apex_available: [ "myapex" ],
Jiyong Parkf7487312019-10-17 12:54:30 +09005950 }
Jiyong Park8be103b2019-11-08 15:53:48 +09005951
5952 cc_library_shared {
5953 name: "libjni",
5954 srcs: ["mylib.cpp"],
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005955 shared_libs: ["libfoo"],
5956 stl: "none",
5957 system_shared_libs: [],
5958 apex_available: [ "myapex" ],
5959 sdk_version: "current",
5960 }
5961
5962 cc_library_shared {
5963 name: "libfoo",
Jiyong Park8be103b2019-11-08 15:53:48 +09005964 stl: "none",
5965 system_shared_libs: [],
Jiyong Park0f80c182020-01-31 02:49:53 +09005966 apex_available: [ "myapex" ],
Colin Cross094cde42020-02-15 10:38:00 -08005967 sdk_version: "current",
Jiyong Park8be103b2019-11-08 15:53:48 +09005968 }
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005969 `)
5970
Sundong Ahnabb64432019-10-22 13:58:29 +09005971 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005972 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/AppFoo@TEST.BUILD_ID/AppFoo.apk")
5976 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv@TEST.BUILD_ID/AppFooPriv.apk")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005977
Colin Crossaede88c2020-08-11 12:17:01 -07005978 appZipRule := ctx.ModuleForTests("AppFoo", "android_common_apex10000").Description("zip jni libs")
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005979 // JNI libraries are uncompressed
Jiyong Park52cd06f2019-11-11 10:14:32 +09005980 if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005981 t.Errorf("jni libs are not uncompressed for AppFoo")
Jiyong Park52cd06f2019-11-11 10:14:32 +09005982 }
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005983 // JNI libraries including transitive deps are
5984 for _, jni := range []string{"libjni", "libfoo"} {
Paul Duffinafdd4062021-03-30 19:44:07 +01005985 jniOutput := ctx.ModuleForTests(jni, "android_arm64_armv8-a_sdk_shared_apex10000").Module().(*cc.Module).OutputFile().RelativeToTop()
Jooyung Hanb7bebe22020-02-25 16:59:29 +09005986 // ... embedded inside APK (jnilibs.zip)
5987 ensureListContains(t, appZipRule.Implicits.Strings(), jniOutput.String())
5988 // ... and not directly inside the APEX
5989 ensureNotContains(t, copyCmds, "image.apex/lib64/"+jni+".so")
5990 }
Dario Frenicde2a032019-10-27 00:29:22 +01005991}
Sundong Ahne1f05aa2019-08-27 13:55:42 +09005992
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00005993func TestApexWithAppImportBuildId(t *testing.T) {
5994 invalidBuildIds := []string{"../", "a b", "a/b", "a/b/../c", "/a"}
5995 for _, id := range invalidBuildIds {
5996 message := fmt.Sprintf("Unable to use build id %s as filename suffix", id)
5997 fixture := android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5998 variables.BuildId = proptools.StringPtr(id)
5999 })
6000 testApexError(t, message, `apex {
6001 name: "myapex",
6002 key: "myapex.key",
6003 apps: ["AppFooPrebuilt"],
6004 updatable: false,
6005 }
6006
6007 apex_key {
6008 name: "myapex.key",
6009 public_key: "testkey.avbpubkey",
6010 private_key: "testkey.pem",
6011 }
6012
6013 android_app_import {
6014 name: "AppFooPrebuilt",
6015 apk: "PrebuiltAppFoo.apk",
6016 presigned: true,
6017 apex_available: ["myapex"],
6018 }
6019 `, fixture)
6020 }
6021}
6022
Dario Frenicde2a032019-10-27 00:29:22 +01006023func TestApexWithAppImports(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006024 ctx := testApex(t, `
Dario Frenicde2a032019-10-27 00:29:22 +01006025 apex {
6026 name: "myapex",
6027 key: "myapex.key",
6028 apps: [
6029 "AppFooPrebuilt",
6030 "AppFooPrivPrebuilt",
6031 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006032 updatable: false,
Dario Frenicde2a032019-10-27 00:29:22 +01006033 }
6034
6035 apex_key {
6036 name: "myapex.key",
6037 public_key: "testkey.avbpubkey",
6038 private_key: "testkey.pem",
6039 }
6040
6041 android_app_import {
6042 name: "AppFooPrebuilt",
6043 apk: "PrebuiltAppFoo.apk",
6044 presigned: true,
6045 dex_preopt: {
6046 enabled: false,
6047 },
Jiyong Park592a6a42020-04-21 22:34:28 +09006048 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006049 }
6050
6051 android_app_import {
6052 name: "AppFooPrivPrebuilt",
6053 apk: "PrebuiltAppFooPriv.apk",
6054 privileged: true,
6055 presigned: true,
6056 dex_preopt: {
6057 enabled: false,
6058 },
Jooyung Han39ee1192020-03-23 20:21:11 +09006059 filename: "AwesomePrebuiltAppFooPriv.apk",
Jiyong Park592a6a42020-04-21 22:34:28 +09006060 apex_available: ["myapex"],
Dario Frenicde2a032019-10-27 00:29:22 +01006061 }
6062 `)
6063
Sundong Ahnabb64432019-10-22 13:58:29 +09006064 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Dario Frenicde2a032019-10-27 00:29:22 +01006065 apexRule := module.Rule("apexRule")
6066 copyCmds := apexRule.Args["copy_commands"]
6067
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006068 ensureContains(t, copyCmds, "image.apex/app/AppFooPrebuilt@TEST.BUILD_ID/AppFooPrebuilt.apk")
6069 ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPrivPrebuilt@TEST.BUILD_ID/AwesomePrebuiltAppFooPriv.apk")
Jooyung Han39ee1192020-03-23 20:21:11 +09006070}
6071
6072func TestApexWithAppImportsPrefer(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006073 ctx := testApex(t, `
Jooyung Han39ee1192020-03-23 20:21:11 +09006074 apex {
6075 name: "myapex",
6076 key: "myapex.key",
6077 apps: [
6078 "AppFoo",
6079 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006080 updatable: false,
Jooyung Han39ee1192020-03-23 20:21:11 +09006081 }
6082
6083 apex_key {
6084 name: "myapex.key",
6085 public_key: "testkey.avbpubkey",
6086 private_key: "testkey.pem",
6087 }
6088
6089 android_app {
6090 name: "AppFoo",
6091 srcs: ["foo/bar/MyClass.java"],
6092 sdk_version: "none",
6093 system_modules: "none",
6094 apex_available: [ "myapex" ],
6095 }
6096
6097 android_app_import {
6098 name: "AppFoo",
6099 apk: "AppFooPrebuilt.apk",
6100 filename: "AppFooPrebuilt.apk",
6101 presigned: true,
6102 prefer: true,
Jiyong Park592a6a42020-04-21 22:34:28 +09006103 apex_available: ["myapex"],
Jooyung Han39ee1192020-03-23 20:21:11 +09006104 }
6105 `, withFiles(map[string][]byte{
6106 "AppFooPrebuilt.apk": nil,
6107 }))
6108
6109 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006110 "app/AppFoo@TEST.BUILD_ID/AppFooPrebuilt.apk",
Jooyung Han39ee1192020-03-23 20:21:11 +09006111 })
Sundong Ahne1f05aa2019-08-27 13:55:42 +09006112}
6113
Dario Freni6f3937c2019-12-20 22:58:03 +00006114func TestApexWithTestHelperApp(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006115 ctx := testApex(t, `
Dario Freni6f3937c2019-12-20 22:58:03 +00006116 apex {
6117 name: "myapex",
6118 key: "myapex.key",
6119 apps: [
6120 "TesterHelpAppFoo",
6121 ],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006122 updatable: false,
Dario Freni6f3937c2019-12-20 22:58:03 +00006123 }
6124
6125 apex_key {
6126 name: "myapex.key",
6127 public_key: "testkey.avbpubkey",
6128 private_key: "testkey.pem",
6129 }
6130
6131 android_test_helper_app {
6132 name: "TesterHelpAppFoo",
6133 srcs: ["foo/bar/MyClass.java"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006134 apex_available: [ "myapex" ],
Dario Freni6f3937c2019-12-20 22:58:03 +00006135 }
6136
6137 `)
6138
6139 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6140 apexRule := module.Rule("apexRule")
6141 copyCmds := apexRule.Args["copy_commands"]
6142
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006143 ensureContains(t, copyCmds, "image.apex/app/TesterHelpAppFoo@TEST.BUILD_ID/TesterHelpAppFoo.apk")
Dario Freni6f3937c2019-12-20 22:58:03 +00006144}
6145
Jooyung Han18020ea2019-11-13 10:50:48 +09006146func TestApexPropertiesShouldBeDefaultable(t *testing.T) {
6147 // libfoo's apex_available comes from cc_defaults
Steven Moreland6e36cd62020-10-22 01:08:35 +00006148 testApexError(t, `requires "libfoo" that doesn't list the APEX under 'apex_available'.`, `
Jooyung Han18020ea2019-11-13 10:50:48 +09006149 apex {
6150 name: "myapex",
6151 key: "myapex.key",
6152 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006153 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006154 }
6155
6156 apex_key {
6157 name: "myapex.key",
6158 public_key: "testkey.avbpubkey",
6159 private_key: "testkey.pem",
6160 }
6161
6162 apex {
6163 name: "otherapex",
6164 key: "myapex.key",
6165 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006166 updatable: false,
Jooyung Han18020ea2019-11-13 10:50:48 +09006167 }
6168
6169 cc_defaults {
6170 name: "libfoo-defaults",
6171 apex_available: ["otherapex"],
6172 }
6173
6174 cc_library {
6175 name: "libfoo",
6176 defaults: ["libfoo-defaults"],
6177 stl: "none",
6178 system_shared_libs: [],
6179 }`)
6180}
6181
Paul Duffine52e66f2020-03-30 17:54:29 +01006182func TestApexAvailable_DirectDep(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006183 // libfoo is not available to myapex, but only to otherapex
Steven Moreland6e36cd62020-10-22 01:08:35 +00006184 testApexError(t, "requires \"libfoo\" that doesn't list the APEX under 'apex_available'.", `
Jiyong Park127b40b2019-09-30 16:04:35 +09006185 apex {
6186 name: "myapex",
6187 key: "myapex.key",
6188 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006189 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006190 }
6191
6192 apex_key {
6193 name: "myapex.key",
6194 public_key: "testkey.avbpubkey",
6195 private_key: "testkey.pem",
6196 }
6197
6198 apex {
6199 name: "otherapex",
6200 key: "otherapex.key",
6201 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006202 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006203 }
6204
6205 apex_key {
6206 name: "otherapex.key",
6207 public_key: "testkey.avbpubkey",
6208 private_key: "testkey.pem",
6209 }
6210
6211 cc_library {
6212 name: "libfoo",
6213 stl: "none",
6214 system_shared_libs: [],
6215 apex_available: ["otherapex"],
6216 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006217}
Jiyong Park127b40b2019-09-30 16:04:35 +09006218
Paul Duffine52e66f2020-03-30 17:54:29 +01006219func TestApexAvailable_IndirectDep(t *testing.T) {
Jooyung Han5e9013b2020-03-10 06:23:13 +09006220 // libbbaz is an indirect dep
Jiyong Park767dbd92021-03-04 13:03:10 +09006221 testApexError(t, `requires "libbaz" that doesn't list the APEX under 'apex_available'.\n\nDependency path:
Paul Duffin520917a2022-05-13 13:01:59 +00006222.*via tag apex\.dependencyTag\{"sharedLib"\}
Paul Duffindf915ff2020-03-30 17:58:21 +01006223.*-> libfoo.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006224.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffindf915ff2020-03-30 17:58:21 +01006225.*-> libbar.*link:shared.*
Colin Cross6e511a92020-07-27 21:26:48 -07006226.*via tag cc\.libraryDependencyTag.*Kind:sharedLibraryDependency.*
Paul Duffin65347702020-03-31 15:23:40 +01006227.*-> libbaz.*link:shared.*`, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006228 apex {
6229 name: "myapex",
6230 key: "myapex.key",
6231 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006232 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006233 }
6234
6235 apex_key {
6236 name: "myapex.key",
6237 public_key: "testkey.avbpubkey",
6238 private_key: "testkey.pem",
6239 }
6240
Jiyong Park127b40b2019-09-30 16:04:35 +09006241 cc_library {
6242 name: "libfoo",
6243 stl: "none",
6244 shared_libs: ["libbar"],
6245 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006246 apex_available: ["myapex"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006247 }
6248
6249 cc_library {
6250 name: "libbar",
6251 stl: "none",
Jooyung Han5e9013b2020-03-10 06:23:13 +09006252 shared_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006253 system_shared_libs: [],
Jooyung Han5e9013b2020-03-10 06:23:13 +09006254 apex_available: ["myapex"],
6255 }
6256
6257 cc_library {
6258 name: "libbaz",
6259 stl: "none",
6260 system_shared_libs: [],
Jiyong Park127b40b2019-09-30 16:04:35 +09006261 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006262}
Jiyong Park127b40b2019-09-30 16:04:35 +09006263
Paul Duffine52e66f2020-03-30 17:54:29 +01006264func TestApexAvailable_InvalidApexName(t *testing.T) {
Jiyong Park127b40b2019-09-30 16:04:35 +09006265 testApexError(t, "\"otherapex\" is not a valid module name", `
6266 apex {
6267 name: "myapex",
6268 key: "myapex.key",
6269 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006270 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006271 }
6272
6273 apex_key {
6274 name: "myapex.key",
6275 public_key: "testkey.avbpubkey",
6276 private_key: "testkey.pem",
6277 }
6278
6279 cc_library {
6280 name: "libfoo",
6281 stl: "none",
6282 system_shared_libs: [],
6283 apex_available: ["otherapex"],
6284 }`)
6285
Paul Duffine52e66f2020-03-30 17:54:29 +01006286 testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006287 apex {
6288 name: "myapex",
6289 key: "myapex.key",
6290 native_shared_libs: ["libfoo", "libbar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006291 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006292 }
6293
6294 apex_key {
6295 name: "myapex.key",
6296 public_key: "testkey.avbpubkey",
6297 private_key: "testkey.pem",
6298 }
6299
6300 cc_library {
6301 name: "libfoo",
6302 stl: "none",
6303 system_shared_libs: [],
Jiyong Park323a4c32020-03-01 17:29:06 +09006304 runtime_libs: ["libbaz"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006305 apex_available: ["myapex"],
6306 }
6307
6308 cc_library {
6309 name: "libbar",
6310 stl: "none",
6311 system_shared_libs: [],
6312 apex_available: ["//apex_available:anyapex"],
Jiyong Park323a4c32020-03-01 17:29:06 +09006313 }
6314
6315 cc_library {
6316 name: "libbaz",
6317 stl: "none",
6318 system_shared_libs: [],
6319 stubs: {
6320 versions: ["10", "20", "30"],
6321 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006322 }`)
Paul Duffine52e66f2020-03-30 17:54:29 +01006323}
Jiyong Park127b40b2019-09-30 16:04:35 +09006324
Jiyong Park89e850a2020-04-07 16:37:39 +09006325func TestApexAvailable_CheckForPlatform(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006326 ctx := testApex(t, `
Jiyong Park127b40b2019-09-30 16:04:35 +09006327 apex {
6328 name: "myapex",
6329 key: "myapex.key",
Jiyong Park89e850a2020-04-07 16:37:39 +09006330 native_shared_libs: ["libbar", "libbaz"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006331 updatable: false,
Jiyong Park127b40b2019-09-30 16:04:35 +09006332 }
6333
6334 apex_key {
6335 name: "myapex.key",
6336 public_key: "testkey.avbpubkey",
6337 private_key: "testkey.pem",
6338 }
6339
6340 cc_library {
6341 name: "libfoo",
6342 stl: "none",
6343 system_shared_libs: [],
Jiyong Park89e850a2020-04-07 16:37:39 +09006344 shared_libs: ["libbar"],
Jiyong Park127b40b2019-09-30 16:04:35 +09006345 apex_available: ["//apex_available:platform"],
Jiyong Park89e850a2020-04-07 16:37:39 +09006346 }
6347
6348 cc_library {
6349 name: "libfoo2",
6350 stl: "none",
6351 system_shared_libs: [],
6352 shared_libs: ["libbaz"],
6353 apex_available: ["//apex_available:platform"],
6354 }
6355
6356 cc_library {
6357 name: "libbar",
6358 stl: "none",
6359 system_shared_libs: [],
6360 apex_available: ["myapex"],
6361 }
6362
6363 cc_library {
6364 name: "libbaz",
6365 stl: "none",
6366 system_shared_libs: [],
6367 apex_available: ["myapex"],
6368 stubs: {
6369 versions: ["1"],
6370 },
Jiyong Park127b40b2019-09-30 16:04:35 +09006371 }`)
6372
Jiyong Park89e850a2020-04-07 16:37:39 +09006373 // libfoo shouldn't be available to platform even though it has "//apex_available:platform",
6374 // because it depends on libbar which isn't available to platform
6375 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6376 if libfoo.NotAvailableForPlatform() != true {
6377 t.Errorf("%q shouldn't be available to platform", libfoo.String())
6378 }
6379
6380 // libfoo2 however can be available to platform because it depends on libbaz which provides
6381 // stubs
6382 libfoo2 := ctx.ModuleForTests("libfoo2", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6383 if libfoo2.NotAvailableForPlatform() == true {
6384 t.Errorf("%q should be available to platform", libfoo2.String())
6385 }
Paul Duffine52e66f2020-03-30 17:54:29 +01006386}
Jiyong Parka90ca002019-10-07 15:47:24 +09006387
Paul Duffine52e66f2020-03-30 17:54:29 +01006388func TestApexAvailable_CreatedForApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006389 ctx := testApex(t, `
Jiyong Parka90ca002019-10-07 15:47:24 +09006390 apex {
6391 name: "myapex",
6392 key: "myapex.key",
6393 native_shared_libs: ["libfoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006394 updatable: false,
Jiyong Parka90ca002019-10-07 15:47:24 +09006395 }
6396
6397 apex_key {
6398 name: "myapex.key",
6399 public_key: "testkey.avbpubkey",
6400 private_key: "testkey.pem",
6401 }
6402
6403 cc_library {
6404 name: "libfoo",
6405 stl: "none",
6406 system_shared_libs: [],
6407 apex_available: ["myapex"],
6408 static: {
6409 apex_available: ["//apex_available:platform"],
6410 },
6411 }`)
6412
Jiyong Park89e850a2020-04-07 16:37:39 +09006413 libfooShared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*cc.Module)
6414 if libfooShared.NotAvailableForPlatform() != true {
6415 t.Errorf("%q shouldn't be available to platform", libfooShared.String())
6416 }
6417 libfooStatic := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*cc.Module)
6418 if libfooStatic.NotAvailableForPlatform() != false {
6419 t.Errorf("%q should be available to platform", libfooStatic.String())
6420 }
Jiyong Park127b40b2019-09-30 16:04:35 +09006421}
6422
Jiyong Park5d790c32019-11-15 18:40:32 +09006423func TestOverrideApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006424 ctx := testApex(t, `
Jiyong Park5d790c32019-11-15 18:40:32 +09006425 apex {
6426 name: "myapex",
6427 key: "myapex.key",
6428 apps: ["app"],
markchien7c803b82021-08-26 22:10:06 +08006429 bpfs: ["bpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006430 prebuilts: ["myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006431 overrides: ["oldapex"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006432 updatable: false,
Jiyong Park5d790c32019-11-15 18:40:32 +09006433 }
6434
6435 override_apex {
6436 name: "override_myapex",
6437 base: "myapex",
6438 apps: ["override_app"],
Ken Chen5372a242022-07-07 17:48:06 +08006439 bpfs: ["overrideBpf"],
Daniel Norman5a3ce132021-08-26 15:44:43 -07006440 prebuilts: ["override_myetc"],
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006441 overrides: ["unknownapex"],
Baligh Uddin004d7172020-02-19 21:29:28 -08006442 logging_parent: "com.foo.bar",
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006443 package_name: "test.overridden.package",
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006444 key: "mynewapex.key",
6445 certificate: ":myapex.certificate",
Jiyong Park5d790c32019-11-15 18:40:32 +09006446 }
6447
6448 apex_key {
6449 name: "myapex.key",
6450 public_key: "testkey.avbpubkey",
6451 private_key: "testkey.pem",
6452 }
6453
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006454 apex_key {
6455 name: "mynewapex.key",
6456 public_key: "testkey2.avbpubkey",
6457 private_key: "testkey2.pem",
6458 }
6459
6460 android_app_certificate {
6461 name: "myapex.certificate",
6462 certificate: "testkey",
6463 }
6464
Jiyong Park5d790c32019-11-15 18:40:32 +09006465 android_app {
6466 name: "app",
6467 srcs: ["foo/bar/MyClass.java"],
6468 package_name: "foo",
6469 sdk_version: "none",
6470 system_modules: "none",
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006471 apex_available: [ "myapex" ],
Jiyong Park5d790c32019-11-15 18:40:32 +09006472 }
6473
6474 override_android_app {
6475 name: "override_app",
6476 base: "app",
6477 package_name: "bar",
6478 }
markchien7c803b82021-08-26 22:10:06 +08006479
6480 bpf {
6481 name: "bpf",
6482 srcs: ["bpf.c"],
6483 }
6484
6485 bpf {
Ken Chen5372a242022-07-07 17:48:06 +08006486 name: "overrideBpf",
6487 srcs: ["overrideBpf.c"],
markchien7c803b82021-08-26 22:10:06 +08006488 }
Daniel Norman5a3ce132021-08-26 15:44:43 -07006489
6490 prebuilt_etc {
6491 name: "myetc",
6492 src: "myprebuilt",
6493 }
6494
6495 prebuilt_etc {
6496 name: "override_myetc",
6497 src: "override_myprebuilt",
6498 }
Jiyong Park20bacab2020-03-03 11:45:41 +09006499 `, withManifestPackageNameOverrides([]string{"myapex:com.android.myapex"}))
Jiyong Park5d790c32019-11-15 18:40:32 +09006500
Jiyong Park317645e2019-12-05 13:20:58 +09006501 originalVariant := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(android.OverridableModule)
6502 overriddenVariant := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Module().(android.OverridableModule)
6503 if originalVariant.GetOverriddenBy() != "" {
6504 t.Errorf("GetOverriddenBy should be empty, but was %q", originalVariant.GetOverriddenBy())
6505 }
6506 if overriddenVariant.GetOverriddenBy() != "override_myapex" {
6507 t.Errorf("GetOverriddenBy should be \"override_myapex\", but was %q", overriddenVariant.GetOverriddenBy())
6508 }
6509
Jiyong Park5d790c32019-11-15 18:40:32 +09006510 module := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image")
6511 apexRule := module.Rule("apexRule")
6512 copyCmds := apexRule.Args["copy_commands"]
6513
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00006514 ensureNotContains(t, copyCmds, "image.apex/app/app@TEST.BUILD_ID/app.apk")
6515 ensureContains(t, copyCmds, "image.apex/app/override_app@TEST.BUILD_ID/override_app.apk")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006516
markchien7c803b82021-08-26 22:10:06 +08006517 ensureNotContains(t, copyCmds, "image.apex/etc/bpf/bpf.o")
Ken Chen5372a242022-07-07 17:48:06 +08006518 ensureContains(t, copyCmds, "image.apex/etc/bpf/overrideBpf.o")
markchien7c803b82021-08-26 22:10:06 +08006519
Daniel Norman5a3ce132021-08-26 15:44:43 -07006520 ensureNotContains(t, copyCmds, "image.apex/etc/myetc")
6521 ensureContains(t, copyCmds, "image.apex/etc/override_myetc")
6522
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006523 apexBundle := module.Module().(*apexBundle)
6524 name := apexBundle.Name()
6525 if name != "override_myapex" {
6526 t.Errorf("name should be \"override_myapex\", but was %q", name)
6527 }
6528
Baligh Uddin004d7172020-02-19 21:29:28 -08006529 if apexBundle.overridableProperties.Logging_parent != "com.foo.bar" {
6530 t.Errorf("override_myapex should have logging parent (com.foo.bar), but was %q.", apexBundle.overridableProperties.Logging_parent)
6531 }
6532
Jiyong Park20bacab2020-03-03 11:45:41 +09006533 optFlags := apexRule.Args["opt_flags"]
Baligh Uddin5b57dba2020-03-15 13:01:05 -07006534 ensureContains(t, optFlags, "--override_apk_package_name test.overridden.package")
Jaewoong Jung4cfdf7d2021-04-20 16:21:24 -07006535 ensureContains(t, optFlags, "--pubkey testkey2.avbpubkey")
6536
6537 signApkRule := module.Rule("signapk")
6538 ensureEquals(t, signApkRule.Args["certificates"], "testkey.x509.pem testkey.pk8")
Jiyong Park20bacab2020-03-03 11:45:41 +09006539
Colin Crossaa255532020-07-03 13:18:24 -07006540 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006541 var builder strings.Builder
6542 data.Custom(&builder, name, "TARGET_", "", data)
6543 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00006544 ensureContains(t, androidMk, "LOCAL_MODULE := override_app.override_myapex")
6545 ensureContains(t, androidMk, "LOCAL_MODULE := overrideBpf.o.override_myapex")
6546 ensureContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.override_myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006547 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := override_myapex.apex")
Jaewoong Jung7abcf8e2019-12-19 17:32:06 -08006548 ensureContains(t, androidMk, "LOCAL_OVERRIDES_MODULES := unknownapex myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006549 ensureNotContains(t, androidMk, "LOCAL_MODULE := app.myapex")
markchien7c803b82021-08-26 22:10:06 +08006550 ensureNotContains(t, androidMk, "LOCAL_MODULE := bpf.myapex")
Jiyong Parkf653b052019-11-18 15:39:01 +09006551 ensureNotContains(t, androidMk, "LOCAL_MODULE := override_app.myapex")
Jaewoong Jung1670ca02019-11-22 14:50:42 -08006552 ensureNotContains(t, androidMk, "LOCAL_MODULE := apex_manifest.pb.myapex")
6553 ensureNotContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.apex")
Jiyong Park5d790c32019-11-15 18:40:32 +09006554}
6555
Albert Martineefabcf2022-03-21 20:11:16 +00006556func TestMinSdkVersionOverride(t *testing.T) {
6557 // Override from 29 to 31
6558 minSdkOverride31 := "31"
6559 ctx := testApex(t, `
6560 apex {
6561 name: "myapex",
6562 key: "myapex.key",
6563 native_shared_libs: ["mylib"],
6564 updatable: true,
6565 min_sdk_version: "29"
6566 }
6567
6568 override_apex {
6569 name: "override_myapex",
6570 base: "myapex",
6571 logging_parent: "com.foo.bar",
6572 package_name: "test.overridden.package"
6573 }
6574
6575 apex_key {
6576 name: "myapex.key",
6577 public_key: "testkey.avbpubkey",
6578 private_key: "testkey.pem",
6579 }
6580
6581 cc_library {
6582 name: "mylib",
6583 srcs: ["mylib.cpp"],
6584 runtime_libs: ["libbar"],
6585 system_shared_libs: [],
6586 stl: "none",
6587 apex_available: [ "myapex" ],
6588 min_sdk_version: "apex_inherit"
6589 }
6590
6591 cc_library {
6592 name: "libbar",
6593 srcs: ["mylib.cpp"],
6594 system_shared_libs: [],
6595 stl: "none",
6596 apex_available: [ "myapex" ],
6597 min_sdk_version: "apex_inherit"
6598 }
6599
6600 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride31))
6601
6602 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6603 copyCmds := apexRule.Args["copy_commands"]
6604
6605 // Ensure that direct non-stubs dep is always included
6606 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6607
6608 // Ensure that runtime_libs dep in included
6609 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6610
6611 // Ensure libraries target overridden min_sdk_version value
6612 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6613}
6614
6615func TestMinSdkVersionOverrideToLowerVersionNoOp(t *testing.T) {
6616 // Attempt to override from 31 to 29, should be a NOOP
6617 minSdkOverride29 := "29"
6618 ctx := testApex(t, `
6619 apex {
6620 name: "myapex",
6621 key: "myapex.key",
6622 native_shared_libs: ["mylib"],
6623 updatable: true,
6624 min_sdk_version: "31"
6625 }
6626
6627 override_apex {
6628 name: "override_myapex",
6629 base: "myapex",
6630 logging_parent: "com.foo.bar",
6631 package_name: "test.overridden.package"
6632 }
6633
6634 apex_key {
6635 name: "myapex.key",
6636 public_key: "testkey.avbpubkey",
6637 private_key: "testkey.pem",
6638 }
6639
6640 cc_library {
6641 name: "mylib",
6642 srcs: ["mylib.cpp"],
6643 runtime_libs: ["libbar"],
6644 system_shared_libs: [],
6645 stl: "none",
6646 apex_available: [ "myapex" ],
6647 min_sdk_version: "apex_inherit"
6648 }
6649
6650 cc_library {
6651 name: "libbar",
6652 srcs: ["mylib.cpp"],
6653 system_shared_libs: [],
6654 stl: "none",
6655 apex_available: [ "myapex" ],
6656 min_sdk_version: "apex_inherit"
6657 }
6658
6659 `, withApexGlobalMinSdkVersionOverride(&minSdkOverride29))
6660
6661 apexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule")
6662 copyCmds := apexRule.Args["copy_commands"]
6663
6664 // Ensure that direct non-stubs dep is always included
6665 ensureContains(t, copyCmds, "image.apex/lib64/mylib.so")
6666
6667 // Ensure that runtime_libs dep in included
6668 ensureContains(t, copyCmds, "image.apex/lib64/libbar.so")
6669
6670 // Ensure libraries target the original min_sdk_version value rather than the overridden
6671 ensureListContains(t, ctx.ModuleVariantsForTests("libbar"), "android_arm64_armv8-a_shared_apex31")
6672}
6673
Jooyung Han214bf372019-11-12 13:03:50 +09006674func TestLegacyAndroid10Support(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006675 ctx := testApex(t, `
Jooyung Han214bf372019-11-12 13:03:50 +09006676 apex {
6677 name: "myapex",
6678 key: "myapex.key",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006679 native_shared_libs: ["mylib"],
Jooyung Han5417f772020-03-12 18:37:20 +09006680 min_sdk_version: "29",
Jooyung Han214bf372019-11-12 13:03:50 +09006681 }
6682
6683 apex_key {
6684 name: "myapex.key",
6685 public_key: "testkey.avbpubkey",
6686 private_key: "testkey.pem",
6687 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006688
6689 cc_library {
6690 name: "mylib",
6691 srcs: ["mylib.cpp"],
6692 stl: "libc++",
6693 system_shared_libs: [],
6694 apex_available: [ "myapex" ],
Jooyung Han749dc692020-04-15 11:03:39 +09006695 min_sdk_version: "29",
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006696 }
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006697 `, withUnbundledBuild)
Jooyung Han214bf372019-11-12 13:03:50 +09006698
6699 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
6700 args := module.Rule("apexRule").Args
6701 ensureContains(t, args["opt_flags"], "--manifest_json "+module.Output("apex_manifest.json").Output.String())
Dario Frenie3546902020-01-14 23:50:25 +00006702 ensureNotContains(t, args["opt_flags"], "--no_hashtree")
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006703
6704 // The copies of the libraries in the apex should have one more dependency than
6705 // the ones outside the apex, namely the unwinder. Ideally we should check
6706 // the dependency names directly here but for some reason the names are blank in
6707 // this test.
6708 for _, lib := range []string{"libc++", "mylib"} {
Colin Crossaede88c2020-08-11 12:17:01 -07006709 apexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared_apex29").Rule("ld").Implicits
Peter Collingbournedc4f9862020-02-12 17:13:25 -08006710 nonApexImplicits := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld").Implicits
6711 if len(apexImplicits) != len(nonApexImplicits)+1 {
6712 t.Errorf("%q missing unwinder dep", lib)
6713 }
6714 }
Jooyung Han214bf372019-11-12 13:03:50 +09006715}
6716
Paul Duffine05480a2021-03-08 15:07:14 +00006717var filesForSdkLibrary = android.MockFS{
Paul Duffin9b879592020-05-26 13:21:35 +01006718 "api/current.txt": nil,
6719 "api/removed.txt": nil,
6720 "api/system-current.txt": nil,
6721 "api/system-removed.txt": nil,
6722 "api/test-current.txt": nil,
6723 "api/test-removed.txt": nil,
Paul Duffineedc5d52020-06-12 17:46:39 +01006724
Anton Hanssondff2c782020-12-21 17:10:01 +00006725 "100/public/api/foo.txt": nil,
6726 "100/public/api/foo-removed.txt": nil,
6727 "100/system/api/foo.txt": nil,
6728 "100/system/api/foo-removed.txt": nil,
6729
Paul Duffineedc5d52020-06-12 17:46:39 +01006730 // For java_sdk_library_import
6731 "a.jar": nil,
Paul Duffin9b879592020-05-26 13:21:35 +01006732}
6733
Jooyung Han58f26ab2019-12-18 15:34:32 +09006734func TestJavaSDKLibrary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006735 ctx := testApex(t, `
Jooyung Han58f26ab2019-12-18 15:34:32 +09006736 apex {
6737 name: "myapex",
6738 key: "myapex.key",
6739 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006740 updatable: false,
Jooyung Han58f26ab2019-12-18 15:34:32 +09006741 }
6742
6743 apex_key {
6744 name: "myapex.key",
6745 public_key: "testkey.avbpubkey",
6746 private_key: "testkey.pem",
6747 }
6748
6749 java_sdk_library {
6750 name: "foo",
6751 srcs: ["a.java"],
6752 api_packages: ["foo"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00006753 apex_available: [ "myapex" ],
Jooyung Han58f26ab2019-12-18 15:34:32 +09006754 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006755
6756 prebuilt_apis {
6757 name: "sdk",
6758 api_dirs: ["100"],
6759 }
Paul Duffin9b879592020-05-26 13:21:35 +01006760 `, withFiles(filesForSdkLibrary))
Jooyung Han58f26ab2019-12-18 15:34:32 +09006761
6762 // java_sdk_library installs both impl jar and permission XML
Jooyung Hana57af4a2020-01-23 05:36:59 +00006763 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
Jooyung Han58f26ab2019-12-18 15:34:32 +09006764 "javalib/foo.jar",
6765 "etc/permissions/foo.xml",
6766 })
6767 // Permission XML should point to the activated path of impl jar of java_sdk_library
Jiyong Parke3833882020-02-17 17:28:10 +09006768 sdkLibrary := ctx.ModuleForTests("foo.xml", "android_common_myapex").Rule("java_sdk_xml")
Pedro Loureiro9956e5e2021-09-07 17:21:59 +00006769 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 +09006770}
6771
Paul Duffin9b879592020-05-26 13:21:35 +01006772func TestJavaSDKLibrary_WithinApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006773 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006774 apex {
6775 name: "myapex",
6776 key: "myapex.key",
6777 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006778 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006779 }
6780
6781 apex_key {
6782 name: "myapex.key",
6783 public_key: "testkey.avbpubkey",
6784 private_key: "testkey.pem",
6785 }
6786
6787 java_sdk_library {
6788 name: "foo",
6789 srcs: ["a.java"],
6790 api_packages: ["foo"],
6791 apex_available: ["myapex"],
6792 sdk_version: "none",
6793 system_modules: "none",
6794 }
6795
6796 java_library {
6797 name: "bar",
6798 srcs: ["a.java"],
6799 libs: ["foo"],
6800 apex_available: ["myapex"],
6801 sdk_version: "none",
6802 system_modules: "none",
6803 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006804
6805 prebuilt_apis {
6806 name: "sdk",
6807 api_dirs: ["100"],
6808 }
Paul Duffin9b879592020-05-26 13:21:35 +01006809 `, withFiles(filesForSdkLibrary))
6810
6811 // java_sdk_library installs both impl jar and permission XML
6812 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6813 "javalib/bar.jar",
6814 "javalib/foo.jar",
6815 "etc/permissions/foo.xml",
6816 })
6817
6818 // The bar library should depend on the implementation jar.
6819 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006820 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006821 t.Errorf("expected %q, found %#q", expected, actual)
6822 }
6823}
6824
6825func TestJavaSDKLibrary_CrossBoundary(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006826 ctx := testApex(t, `
Paul Duffin9b879592020-05-26 13:21:35 +01006827 apex {
6828 name: "myapex",
6829 key: "myapex.key",
6830 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006831 updatable: false,
Paul Duffin9b879592020-05-26 13:21:35 +01006832 }
6833
6834 apex_key {
6835 name: "myapex.key",
6836 public_key: "testkey.avbpubkey",
6837 private_key: "testkey.pem",
6838 }
6839
6840 java_sdk_library {
6841 name: "foo",
6842 srcs: ["a.java"],
6843 api_packages: ["foo"],
6844 apex_available: ["myapex"],
6845 sdk_version: "none",
6846 system_modules: "none",
6847 }
6848
6849 java_library {
6850 name: "bar",
6851 srcs: ["a.java"],
6852 libs: ["foo"],
6853 sdk_version: "none",
6854 system_modules: "none",
6855 }
Anton Hanssondff2c782020-12-21 17:10:01 +00006856
6857 prebuilt_apis {
6858 name: "sdk",
6859 api_dirs: ["100"],
6860 }
Paul Duffin9b879592020-05-26 13:21:35 +01006861 `, withFiles(filesForSdkLibrary))
6862
6863 // java_sdk_library installs both impl jar and permission XML
6864 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6865 "javalib/foo.jar",
6866 "etc/permissions/foo.xml",
6867 })
6868
6869 // The bar library should depend on the stubs jar.
6870 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006871 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffin9b879592020-05-26 13:21:35 +01006872 t.Errorf("expected %q, found %#q", expected, actual)
6873 }
6874}
6875
Paul Duffineedc5d52020-06-12 17:46:39 +01006876func TestJavaSDKLibrary_ImportPreferred(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08006877 ctx := testApex(t, `
Anton Hanssondff2c782020-12-21 17:10:01 +00006878 prebuilt_apis {
6879 name: "sdk",
6880 api_dirs: ["100"],
6881 }`,
Paul Duffineedc5d52020-06-12 17:46:39 +01006882 withFiles(map[string][]byte{
6883 "apex/a.java": nil,
6884 "apex/apex_manifest.json": nil,
6885 "apex/Android.bp": []byte(`
6886 package {
6887 default_visibility: ["//visibility:private"],
6888 }
6889
6890 apex {
6891 name: "myapex",
6892 key: "myapex.key",
6893 java_libs: ["foo", "bar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006894 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006895 }
6896
6897 apex_key {
6898 name: "myapex.key",
6899 public_key: "testkey.avbpubkey",
6900 private_key: "testkey.pem",
6901 }
6902
6903 java_library {
6904 name: "bar",
6905 srcs: ["a.java"],
6906 libs: ["foo"],
6907 apex_available: ["myapex"],
6908 sdk_version: "none",
6909 system_modules: "none",
6910 }
6911`),
6912 "source/a.java": nil,
6913 "source/api/current.txt": nil,
6914 "source/api/removed.txt": nil,
6915 "source/Android.bp": []byte(`
6916 package {
6917 default_visibility: ["//visibility:private"],
6918 }
6919
6920 java_sdk_library {
6921 name: "foo",
6922 visibility: ["//apex"],
6923 srcs: ["a.java"],
6924 api_packages: ["foo"],
6925 apex_available: ["myapex"],
6926 sdk_version: "none",
6927 system_modules: "none",
6928 public: {
6929 enabled: true,
6930 },
6931 }
6932`),
6933 "prebuilt/a.jar": nil,
6934 "prebuilt/Android.bp": []byte(`
6935 package {
6936 default_visibility: ["//visibility:private"],
6937 }
6938
6939 java_sdk_library_import {
6940 name: "foo",
6941 visibility: ["//apex", "//source"],
6942 apex_available: ["myapex"],
6943 prefer: true,
6944 public: {
6945 jars: ["a.jar"],
6946 },
6947 }
6948`),
Anton Hanssondff2c782020-12-21 17:10:01 +00006949 }), withFiles(filesForSdkLibrary),
Paul Duffineedc5d52020-06-12 17:46:39 +01006950 )
6951
6952 // java_sdk_library installs both impl jar and permission XML
6953 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
6954 "javalib/bar.jar",
6955 "javalib/foo.jar",
6956 "etc/permissions/foo.xml",
6957 })
6958
6959 // The bar library should depend on the implementation jar.
6960 barLibrary := ctx.ModuleForTests("bar", "android_common_myapex").Rule("javac")
Paul Duffincf8d7db2021-03-29 00:29:53 +01006961 if expected, actual := `^-classpath [^:]*/turbine-combined/foo\.impl\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffineedc5d52020-06-12 17:46:39 +01006962 t.Errorf("expected %q, found %#q", expected, actual)
6963 }
6964}
6965
6966func TestJavaSDKLibrary_ImportOnly(t *testing.T) {
6967 testApexError(t, `java_libs: "foo" is not configured to be compiled into dex`, `
6968 apex {
6969 name: "myapex",
6970 key: "myapex.key",
6971 java_libs: ["foo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00006972 updatable: false,
Paul Duffineedc5d52020-06-12 17:46:39 +01006973 }
6974
6975 apex_key {
6976 name: "myapex.key",
6977 public_key: "testkey.avbpubkey",
6978 private_key: "testkey.pem",
6979 }
6980
6981 java_sdk_library_import {
6982 name: "foo",
6983 apex_available: ["myapex"],
6984 prefer: true,
6985 public: {
6986 jars: ["a.jar"],
6987 },
6988 }
6989
6990 `, withFiles(filesForSdkLibrary))
6991}
6992
atrost6e126252020-01-27 17:01:16 +00006993func TestCompatConfig(t *testing.T) {
Paul Duffin284165a2021-03-29 01:50:31 +01006994 result := android.GroupFixturePreparers(
6995 prepareForApexTest,
6996 java.PrepareForTestWithPlatformCompatConfig,
6997 ).RunTestWithBp(t, `
atrost6e126252020-01-27 17:01:16 +00006998 apex {
6999 name: "myapex",
7000 key: "myapex.key",
Paul Duffin3abc1742021-03-15 19:32:23 +00007001 compat_configs: ["myjar-platform-compat-config"],
atrost6e126252020-01-27 17:01:16 +00007002 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007003 updatable: false,
atrost6e126252020-01-27 17:01:16 +00007004 }
7005
7006 apex_key {
7007 name: "myapex.key",
7008 public_key: "testkey.avbpubkey",
7009 private_key: "testkey.pem",
7010 }
7011
7012 platform_compat_config {
7013 name: "myjar-platform-compat-config",
7014 src: ":myjar",
7015 }
7016
7017 java_library {
7018 name: "myjar",
7019 srcs: ["foo/bar/MyClass.java"],
7020 sdk_version: "none",
7021 system_modules: "none",
atrost6e126252020-01-27 17:01:16 +00007022 apex_available: [ "myapex" ],
7023 }
Paul Duffin1b29e002021-03-16 15:06:54 +00007024
7025 // Make sure that a preferred prebuilt does not affect the apex contents.
7026 prebuilt_platform_compat_config {
7027 name: "myjar-platform-compat-config",
7028 metadata: "compat-config/metadata.xml",
7029 prefer: true,
7030 }
atrost6e126252020-01-27 17:01:16 +00007031 `)
Paul Duffina369c7b2021-03-09 03:08:05 +00007032 ctx := result.TestContext
atrost6e126252020-01-27 17:01:16 +00007033 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7034 "etc/compatconfig/myjar-platform-compat-config.xml",
7035 "javalib/myjar.jar",
7036 })
7037}
7038
Jooyung Han862c0d62022-12-21 10:15:37 +09007039func TestNoDupeApexFiles(t *testing.T) {
7040 android.GroupFixturePreparers(
7041 android.PrepareForTestWithAndroidBuildComponents,
7042 PrepareForTestWithApexBuildComponents,
7043 prepareForTestWithMyapex,
7044 prebuilt_etc.PrepareForTestWithPrebuiltEtc,
7045 ).
7046 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("is provided by two different files")).
7047 RunTestWithBp(t, `
7048 apex {
7049 name: "myapex",
7050 key: "myapex.key",
7051 prebuilts: ["foo", "bar"],
7052 updatable: false,
7053 }
7054
7055 apex_key {
7056 name: "myapex.key",
7057 public_key: "testkey.avbpubkey",
7058 private_key: "testkey.pem",
7059 }
7060
7061 prebuilt_etc {
7062 name: "foo",
7063 src: "myprebuilt",
7064 filename_from_src: true,
7065 }
7066
7067 prebuilt_etc {
7068 name: "bar",
7069 src: "myprebuilt",
7070 filename_from_src: true,
7071 }
7072 `)
7073}
7074
Jiyong Park479321d2019-12-16 11:47:12 +09007075func TestRejectNonInstallableJavaLibrary(t *testing.T) {
7076 testApexError(t, `"myjar" is not configured to be compiled into dex`, `
7077 apex {
7078 name: "myapex",
7079 key: "myapex.key",
7080 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007081 updatable: false,
Jiyong Park479321d2019-12-16 11:47:12 +09007082 }
7083
7084 apex_key {
7085 name: "myapex.key",
7086 public_key: "testkey.avbpubkey",
7087 private_key: "testkey.pem",
7088 }
7089
7090 java_library {
7091 name: "myjar",
7092 srcs: ["foo/bar/MyClass.java"],
7093 sdk_version: "none",
7094 system_modules: "none",
Jiyong Park6b21c7d2020-02-11 09:16:01 +09007095 compile_dex: false,
Jooyung Han5e9013b2020-03-10 06:23:13 +09007096 apex_available: ["myapex"],
Jiyong Park479321d2019-12-16 11:47:12 +09007097 }
7098 `)
7099}
7100
Jiyong Park7afd1072019-12-30 16:56:33 +09007101func TestCarryRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007102 ctx := testApex(t, `
Jiyong Park7afd1072019-12-30 16:56:33 +09007103 apex {
7104 name: "myapex",
7105 key: "myapex.key",
7106 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007107 updatable: false,
Jiyong Park7afd1072019-12-30 16:56:33 +09007108 }
7109
7110 apex_key {
7111 name: "myapex.key",
7112 public_key: "testkey.avbpubkey",
7113 private_key: "testkey.pem",
7114 }
7115
7116 cc_library {
7117 name: "mylib",
7118 srcs: ["mylib.cpp"],
7119 system_shared_libs: [],
7120 stl: "none",
7121 required: ["a", "b"],
7122 host_required: ["c", "d"],
7123 target_required: ["e", "f"],
Anton Hanssoneec79eb2020-01-10 15:12:39 +00007124 apex_available: [ "myapex" ],
Jiyong Park7afd1072019-12-30 16:56:33 +09007125 }
7126 `)
7127
7128 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007129 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Jiyong Park7afd1072019-12-30 16:56:33 +09007130 name := apexBundle.BaseModuleName()
7131 prefix := "TARGET_"
7132 var builder strings.Builder
7133 data.Custom(&builder, name, prefix, "", data)
7134 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007135 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 -08007136 ensureContains(t, androidMk, "LOCAL_HOST_REQUIRED_MODULES := c d\n")
7137 ensureContains(t, androidMk, "LOCAL_TARGET_REQUIRED_MODULES := e f\n")
Jiyong Park7afd1072019-12-30 16:56:33 +09007138}
7139
Jiyong Park7cd10e32020-01-14 09:22:18 +09007140func TestSymlinksFromApexToSystem(t *testing.T) {
7141 bp := `
7142 apex {
7143 name: "myapex",
7144 key: "myapex.key",
7145 native_shared_libs: ["mylib"],
7146 java_libs: ["myjar"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007147 updatable: false,
Jiyong Park7cd10e32020-01-14 09:22:18 +09007148 }
7149
Jiyong Park9d677202020-02-19 16:29:35 +09007150 apex {
7151 name: "myapex.updatable",
7152 key: "myapex.key",
7153 native_shared_libs: ["mylib"],
7154 java_libs: ["myjar"],
7155 updatable: true,
Spandan Das1a92db52023-04-06 18:55:06 +00007156 min_sdk_version: "33",
Jiyong Park9d677202020-02-19 16:29:35 +09007157 }
7158
Jiyong Park7cd10e32020-01-14 09:22:18 +09007159 apex_key {
7160 name: "myapex.key",
7161 public_key: "testkey.avbpubkey",
7162 private_key: "testkey.pem",
7163 }
7164
7165 cc_library {
7166 name: "mylib",
7167 srcs: ["mylib.cpp"],
Jiyong Parkce243632023-02-17 18:22:25 +09007168 shared_libs: [
7169 "myotherlib",
7170 "myotherlib_ext",
7171 ],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007172 system_shared_libs: [],
7173 stl: "none",
7174 apex_available: [
7175 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007176 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007177 "//apex_available:platform",
7178 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007179 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007180 }
7181
7182 cc_library {
7183 name: "myotherlib",
7184 srcs: ["mylib.cpp"],
7185 system_shared_libs: [],
7186 stl: "none",
7187 apex_available: [
7188 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007189 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007190 "//apex_available:platform",
7191 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007192 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007193 }
7194
Jiyong Parkce243632023-02-17 18:22:25 +09007195 cc_library {
7196 name: "myotherlib_ext",
7197 srcs: ["mylib.cpp"],
7198 system_shared_libs: [],
7199 system_ext_specific: true,
7200 stl: "none",
7201 apex_available: [
7202 "myapex",
7203 "myapex.updatable",
7204 "//apex_available:platform",
7205 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007206 min_sdk_version: "33",
Jiyong Parkce243632023-02-17 18:22:25 +09007207 }
7208
Jiyong Park7cd10e32020-01-14 09:22:18 +09007209 java_library {
7210 name: "myjar",
7211 srcs: ["foo/bar/MyClass.java"],
7212 sdk_version: "none",
7213 system_modules: "none",
7214 libs: ["myotherjar"],
Jiyong Park7cd10e32020-01-14 09:22:18 +09007215 apex_available: [
7216 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007217 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007218 "//apex_available:platform",
7219 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007220 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007221 }
7222
7223 java_library {
7224 name: "myotherjar",
7225 srcs: ["foo/bar/MyClass.java"],
7226 sdk_version: "none",
7227 system_modules: "none",
7228 apex_available: [
7229 "myapex",
Jiyong Park9d677202020-02-19 16:29:35 +09007230 "myapex.updatable",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007231 "//apex_available:platform",
7232 ],
Spandan Das1a92db52023-04-06 18:55:06 +00007233 min_sdk_version: "33",
Jiyong Park7cd10e32020-01-14 09:22:18 +09007234 }
7235 `
7236
7237 ensureRealfileExists := func(t *testing.T, files []fileInApex, file string) {
7238 for _, f := range files {
7239 if f.path == file {
7240 if f.isLink {
7241 t.Errorf("%q is not a real file", file)
7242 }
7243 return
7244 }
7245 }
7246 t.Errorf("%q is not found", file)
7247 }
7248
Jiyong Parkce243632023-02-17 18:22:25 +09007249 ensureSymlinkExists := func(t *testing.T, files []fileInApex, file string, target string) {
Jiyong Park7cd10e32020-01-14 09:22:18 +09007250 for _, f := range files {
7251 if f.path == file {
7252 if !f.isLink {
7253 t.Errorf("%q is not a symlink", file)
7254 }
Jiyong Parkce243632023-02-17 18:22:25 +09007255 if f.src != target {
7256 t.Errorf("expected symlink target to be %q, got %q", target, f.src)
7257 }
Jiyong Park7cd10e32020-01-14 09:22:18 +09007258 return
7259 }
7260 }
7261 t.Errorf("%q is not found", file)
7262 }
7263
Jiyong Park9d677202020-02-19 16:29:35 +09007264 // For unbundled build, symlink shouldn't exist regardless of whether an APEX
7265 // is updatable or not
Colin Cross1c460562021-02-16 17:55:47 -08007266 ctx := testApex(t, bp, withUnbundledBuild)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007267 files := getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007268 ensureRealfileExists(t, files, "javalib/myjar.jar")
7269 ensureRealfileExists(t, files, "lib64/mylib.so")
7270 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007271 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007272
Jiyong Park9d677202020-02-19 16:29:35 +09007273 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7274 ensureRealfileExists(t, files, "javalib/myjar.jar")
7275 ensureRealfileExists(t, files, "lib64/mylib.so")
7276 ensureRealfileExists(t, files, "lib64/myotherlib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007277 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so")
Jiyong Park9d677202020-02-19 16:29:35 +09007278
7279 // For bundled build, symlink to the system for the non-updatable APEXes only
Colin Cross1c460562021-02-16 17:55:47 -08007280 ctx = testApex(t, bp)
Jooyung Hana57af4a2020-01-23 05:36:59 +00007281 files = getFiles(t, ctx, "myapex", "android_common_myapex_image")
Jiyong Park7cd10e32020-01-14 09:22:18 +09007282 ensureRealfileExists(t, files, "javalib/myjar.jar")
7283 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007284 ensureSymlinkExists(t, files, "lib64/myotherlib.so", "/system/lib64/myotherlib.so") // this is symlink
7285 ensureSymlinkExists(t, files, "lib64/myotherlib_ext.so", "/system_ext/lib64/myotherlib_ext.so") // this is symlink
Jiyong Park9d677202020-02-19 16:29:35 +09007286
7287 files = getFiles(t, ctx, "myapex.updatable", "android_common_myapex.updatable_image")
7288 ensureRealfileExists(t, files, "javalib/myjar.jar")
7289 ensureRealfileExists(t, files, "lib64/mylib.so")
Jiyong Parkce243632023-02-17 18:22:25 +09007290 ensureRealfileExists(t, files, "lib64/myotherlib.so") // this is a real file
7291 ensureRealfileExists(t, files, "lib64/myotherlib_ext.so") // this is a real file
Jiyong Park7cd10e32020-01-14 09:22:18 +09007292}
7293
Yo Chiange8128052020-07-23 20:09:18 +08007294func TestSymlinksFromApexToSystemRequiredModuleNames(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007295 ctx := testApex(t, `
Yo Chiange8128052020-07-23 20:09:18 +08007296 apex {
7297 name: "myapex",
7298 key: "myapex.key",
7299 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007300 updatable: false,
Yo Chiange8128052020-07-23 20:09:18 +08007301 }
7302
7303 apex_key {
7304 name: "myapex.key",
7305 public_key: "testkey.avbpubkey",
7306 private_key: "testkey.pem",
7307 }
7308
7309 cc_library_shared {
7310 name: "mylib",
7311 srcs: ["mylib.cpp"],
7312 shared_libs: ["myotherlib"],
7313 system_shared_libs: [],
7314 stl: "none",
7315 apex_available: [
7316 "myapex",
7317 "//apex_available:platform",
7318 ],
7319 }
7320
7321 cc_prebuilt_library_shared {
7322 name: "myotherlib",
7323 srcs: ["prebuilt.so"],
7324 system_shared_libs: [],
7325 stl: "none",
7326 apex_available: [
7327 "myapex",
7328 "//apex_available:platform",
7329 ],
7330 }
7331 `)
7332
Prerana Patilb1896c82022-11-09 18:14:34 +00007333 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07007334 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
Yo Chiange8128052020-07-23 20:09:18 +08007335 var builder strings.Builder
7336 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
7337 androidMk := builder.String()
7338 // `myotherlib` is added to `myapex` as symlink
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007339 ensureContains(t, androidMk, "LOCAL_MODULE := mylib.myapex\n")
Yo Chiange8128052020-07-23 20:09:18 +08007340 ensureNotContains(t, androidMk, "LOCAL_MODULE := prebuilt_myotherlib.myapex\n")
7341 ensureNotContains(t, androidMk, "LOCAL_MODULE := myotherlib.myapex\n")
7342 // `myapex` should have `myotherlib` in its required line, not `prebuilt_myotherlib`
Diwas Sharmabb9202e2023-01-26 18:42:21 +00007343 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 +08007344}
7345
Jooyung Han643adc42020-02-27 13:50:06 +09007346func TestApexWithJniLibs(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007347 ctx := testApex(t, `
Jooyung Han643adc42020-02-27 13:50:06 +09007348 apex {
7349 name: "myapex",
7350 key: "myapex.key",
Jiyong Park34d5c332022-02-24 18:02:44 +09007351 jni_libs: ["mylib", "libfoo.rust"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007352 updatable: false,
Jooyung Han643adc42020-02-27 13:50:06 +09007353 }
7354
7355 apex_key {
7356 name: "myapex.key",
7357 public_key: "testkey.avbpubkey",
7358 private_key: "testkey.pem",
7359 }
7360
7361 cc_library {
7362 name: "mylib",
7363 srcs: ["mylib.cpp"],
7364 shared_libs: ["mylib2"],
7365 system_shared_libs: [],
7366 stl: "none",
7367 apex_available: [ "myapex" ],
7368 }
7369
7370 cc_library {
7371 name: "mylib2",
7372 srcs: ["mylib.cpp"],
7373 system_shared_libs: [],
7374 stl: "none",
7375 apex_available: [ "myapex" ],
7376 }
Jiyong Park34d5c332022-02-24 18:02:44 +09007377
7378 rust_ffi_shared {
7379 name: "libfoo.rust",
7380 crate_name: "foo",
7381 srcs: ["foo.rs"],
7382 shared_libs: ["libfoo.shared_from_rust"],
7383 prefer_rlib: true,
7384 apex_available: ["myapex"],
7385 }
7386
7387 cc_library_shared {
7388 name: "libfoo.shared_from_rust",
7389 srcs: ["mylib.cpp"],
7390 system_shared_libs: [],
7391 stl: "none",
7392 stubs: {
7393 versions: ["10", "11", "12"],
7394 },
7395 }
7396
Jooyung Han643adc42020-02-27 13:50:06 +09007397 `)
7398
7399 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexManifestRule")
7400 // Notice mylib2.so (transitive dep) is not added as a jni_lib
Jiyong Park34d5c332022-02-24 18:02:44 +09007401 ensureEquals(t, rule.Args["opt"], "-a jniLibs libfoo.rust.so mylib.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007402 ensureExactContents(t, ctx, "myapex", "android_common_myapex_image", []string{
7403 "lib64/mylib.so",
7404 "lib64/mylib2.so",
Jiyong Park34d5c332022-02-24 18:02:44 +09007405 "lib64/libfoo.rust.so",
7406 "lib64/libc++.so", // auto-added to libfoo.rust by Soong
7407 "lib64/liblog.so", // auto-added to libfoo.rust by Soong
Jooyung Han643adc42020-02-27 13:50:06 +09007408 })
Jiyong Park34d5c332022-02-24 18:02:44 +09007409
7410 // b/220397949
7411 ensureListContains(t, names(rule.Args["requireNativeLibs"]), "libfoo.shared_from_rust.so")
Jooyung Han643adc42020-02-27 13:50:06 +09007412}
7413
Jooyung Han49f67012020-04-17 13:43:10 +09007414func TestApexMutatorsDontRunIfDisabled(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007415 ctx := testApex(t, `
Jooyung Han49f67012020-04-17 13:43:10 +09007416 apex {
7417 name: "myapex",
7418 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007419 updatable: false,
Jooyung Han49f67012020-04-17 13:43:10 +09007420 }
7421 apex_key {
7422 name: "myapex.key",
7423 public_key: "testkey.avbpubkey",
7424 private_key: "testkey.pem",
7425 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00007426 `,
7427 android.FixtureModifyConfig(func(config android.Config) {
7428 delete(config.Targets, android.Android)
7429 config.AndroidCommonTarget = android.Target{}
7430 }),
7431 )
Jooyung Han49f67012020-04-17 13:43:10 +09007432
7433 if expected, got := []string{""}, ctx.ModuleVariantsForTests("myapex"); !reflect.DeepEqual(expected, got) {
7434 t.Errorf("Expected variants: %v, but got: %v", expected, got)
7435 }
7436}
7437
Jiyong Parkbd159612020-02-28 15:22:21 +09007438func TestAppBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007439 ctx := testApex(t, `
Jiyong Parkbd159612020-02-28 15:22:21 +09007440 apex {
7441 name: "myapex",
7442 key: "myapex.key",
7443 apps: ["AppFoo"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007444 updatable: false,
Jiyong Parkbd159612020-02-28 15:22:21 +09007445 }
7446
7447 apex_key {
7448 name: "myapex.key",
7449 public_key: "testkey.avbpubkey",
7450 private_key: "testkey.pem",
7451 }
7452
7453 android_app {
7454 name: "AppFoo",
7455 srcs: ["foo/bar/MyClass.java"],
7456 sdk_version: "none",
7457 system_modules: "none",
7458 apex_available: [ "myapex" ],
7459 }
Jiyong Parkcfaa1642020-02-28 16:51:07 +09007460 `, withManifestPackageNameOverrides([]string{"AppFoo:com.android.foo"}))
Jiyong Parkbd159612020-02-28 15:22:21 +09007461
Colin Crosscf371cc2020-11-13 11:48:42 -08007462 bundleConfigRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Output("bundle_config.json")
Jiyong Parkbd159612020-02-28 15:22:21 +09007463 content := bundleConfigRule.Args["content"]
7464
7465 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007466 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 +09007467}
7468
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007469func TestAppSetBundle(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08007470 ctx := testApex(t, `
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007471 apex {
7472 name: "myapex",
7473 key: "myapex.key",
7474 apps: ["AppSet"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007475 updatable: false,
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007476 }
7477
7478 apex_key {
7479 name: "myapex.key",
7480 public_key: "testkey.avbpubkey",
7481 private_key: "testkey.pem",
7482 }
7483
7484 android_app_set {
7485 name: "AppSet",
7486 set: "AppSet.apks",
7487 }`)
7488 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
Colin Crosscf371cc2020-11-13 11:48:42 -08007489 bundleConfigRule := mod.Output("bundle_config.json")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007490 content := bundleConfigRule.Args["content"]
7491 ensureContains(t, content, `"compression":{"uncompressed_glob":["apex_payload.img","apex_manifest.*"]}`)
7492 s := mod.Rule("apexRule").Args["copy_commands"]
7493 copyCmds := regexp.MustCompile(" *&& *").Split(s, -1)
Jiyong Park4169a252022-09-29 21:30:25 +09007494 if len(copyCmds) != 4 {
7495 t.Fatalf("Expected 4 commands, got %d in:\n%s", len(copyCmds), s)
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007496 }
Oriol Prieto Gasco17e22902022-05-05 13:52:25 +00007497 ensureMatches(t, copyCmds[0], "^rm -rf .*/app/AppSet@TEST.BUILD_ID$")
7498 ensureMatches(t, copyCmds[1], "^mkdir -p .*/app/AppSet@TEST.BUILD_ID$")
Jiyong Park4169a252022-09-29 21:30:25 +09007499 ensureMatches(t, copyCmds[2], "^cp -f .*/app/AppSet@TEST.BUILD_ID/AppSet.apk$")
7500 ensureMatches(t, copyCmds[3], "^unzip .*-d .*/app/AppSet@TEST.BUILD_ID .*/AppSet.zip$")
Jiyong Parke1b69142022-09-26 14:48:56 +09007501
7502 // Ensure that canned_fs_config has an entry for the app set zip file
7503 generateFsRule := mod.Rule("generateFsConfig")
7504 cmd := generateFsRule.RuleParams.Command
7505 ensureContains(t, cmd, "AppSet.zip")
Sasha Smundak18d98bc2020-05-27 16:36:07 -07007506}
7507
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007508func TestAppSetBundlePrebuilt(t *testing.T) {
Paul Duffin24704672021-04-06 16:09:30 +01007509 bp := `
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007510 apex_set {
7511 name: "myapex",
7512 filename: "foo_v2.apex",
7513 sanitized: {
7514 none: { set: "myapex.apks", },
7515 hwaddress: { set: "myapex.hwasan.apks", },
7516 },
Paul Duffin24704672021-04-06 16:09:30 +01007517 }
7518 `
7519 ctx := testApex(t, bp, prepareForTestWithSantitizeHwaddress)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007520
Paul Duffin24704672021-04-06 16:09:30 +01007521 // Check that the extractor produces the correct output file from the correct input file.
7522 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.hwasan.apks"
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007523
Paul Duffin24704672021-04-06 16:09:30 +01007524 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7525 extractedApex := m.Output(extractorOutput)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007526
Paul Duffin24704672021-04-06 16:09:30 +01007527 android.AssertArrayString(t, "extractor input", []string{"myapex.hwasan.apks"}, extractedApex.Inputs.Strings())
7528
7529 // Ditto for the apex.
Paul Duffin6717d882021-06-15 19:09:41 +01007530 m = ctx.ModuleForTests("myapex", "android_common_myapex")
7531 copiedApex := m.Output("out/soong/.intermediates/myapex/android_common_myapex/foo_v2.apex")
Paul Duffin24704672021-04-06 16:09:30 +01007532
7533 android.AssertStringEquals(t, "myapex input", extractorOutput, copiedApex.Input.String())
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07007534}
7535
Pranav Guptaeba03b02022-09-27 00:27:08 +00007536func TestApexSetApksModuleAssignment(t *testing.T) {
7537 ctx := testApex(t, `
7538 apex_set {
7539 name: "myapex",
7540 set: ":myapex_apks_file",
7541 }
7542
7543 filegroup {
7544 name: "myapex_apks_file",
7545 srcs: ["myapex.apks"],
7546 }
7547 `)
7548
7549 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
7550
7551 // Check that the extractor produces the correct apks file from the input module
7552 extractorOutput := "out/soong/.intermediates/myapex.apex.extractor/android_common/extracted/myapex.apks"
7553 extractedApex := m.Output(extractorOutput)
7554
7555 android.AssertArrayString(t, "extractor input", []string{"myapex.apks"}, extractedApex.Inputs.Strings())
7556}
7557
Paul Duffin89f570a2021-06-16 01:42:33 +01007558func testNoUpdatableJarsInBootImage(t *testing.T, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) {
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007559 t.Helper()
7560
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007561 bp := `
7562 java_library {
7563 name: "some-updatable-apex-lib",
7564 srcs: ["a.java"],
7565 sdk_version: "current",
7566 apex_available: [
7567 "some-updatable-apex",
7568 ],
satayevabcd5972021-08-06 17:49:46 +01007569 permitted_packages: ["some.updatable.apex.lib"],
Spandan Dascc9d9422023-04-06 18:07:43 +00007570 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007571 }
7572
7573 java_library {
7574 name: "some-non-updatable-apex-lib",
7575 srcs: ["a.java"],
7576 apex_available: [
7577 "some-non-updatable-apex",
7578 ],
Paul Duffin89f570a2021-06-16 01:42:33 +01007579 compile_dex: true,
satayevabcd5972021-08-06 17:49:46 +01007580 permitted_packages: ["some.non.updatable.apex.lib"],
Paul Duffin89f570a2021-06-16 01:42:33 +01007581 }
7582
7583 bootclasspath_fragment {
7584 name: "some-non-updatable-fragment",
7585 contents: ["some-non-updatable-apex-lib"],
7586 apex_available: [
7587 "some-non-updatable-apex",
7588 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007589 hidden_api: {
7590 split_packages: ["*"],
7591 },
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007592 }
7593
7594 java_library {
7595 name: "some-platform-lib",
7596 srcs: ["a.java"],
7597 sdk_version: "current",
7598 installable: true,
7599 }
7600
7601 java_library {
7602 name: "some-art-lib",
7603 srcs: ["a.java"],
7604 sdk_version: "current",
7605 apex_available: [
Paul Duffind376f792021-01-26 11:59:35 +00007606 "com.android.art.debug",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007607 ],
7608 hostdex: true,
Paul Duffine5218812021-06-07 13:28:19 +01007609 compile_dex: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007610 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007611 }
7612
7613 apex {
7614 name: "some-updatable-apex",
7615 key: "some-updatable-apex.key",
7616 java_libs: ["some-updatable-apex-lib"],
7617 updatable: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007618 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007619 }
7620
7621 apex {
7622 name: "some-non-updatable-apex",
7623 key: "some-non-updatable-apex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007624 bootclasspath_fragments: ["some-non-updatable-fragment"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007625 updatable: false,
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007626 }
7627
7628 apex_key {
7629 name: "some-updatable-apex.key",
7630 }
7631
7632 apex_key {
7633 name: "some-non-updatable-apex.key",
7634 }
7635
7636 apex {
Paul Duffind376f792021-01-26 11:59:35 +00007637 name: "com.android.art.debug",
7638 key: "com.android.art.debug.key",
Paul Duffin89f570a2021-06-16 01:42:33 +01007639 bootclasspath_fragments: ["art-bootclasspath-fragment"],
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007640 updatable: true,
Spandan Dascc9d9422023-04-06 18:07:43 +00007641 min_sdk_version: "33",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007642 }
7643
Paul Duffinf23bc472021-04-27 12:42:20 +01007644 bootclasspath_fragment {
7645 name: "art-bootclasspath-fragment",
7646 image_name: "art",
7647 contents: ["some-art-lib"],
7648 apex_available: [
7649 "com.android.art.debug",
7650 ],
Paul Duffin9fd56472022-03-31 15:42:30 +01007651 hidden_api: {
7652 split_packages: ["*"],
7653 },
Paul Duffinf23bc472021-04-27 12:42:20 +01007654 }
7655
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007656 apex_key {
Paul Duffind376f792021-01-26 11:59:35 +00007657 name: "com.android.art.debug.key",
Ulya Trafimovich7caef202020-05-19 12:00:52 +01007658 }
7659
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007660 filegroup {
7661 name: "some-updatable-apex-file_contexts",
7662 srcs: [
7663 "system/sepolicy/apex/some-updatable-apex-file_contexts",
7664 ],
7665 }
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01007666
7667 filegroup {
7668 name: "some-non-updatable-apex-file_contexts",
7669 srcs: [
7670 "system/sepolicy/apex/some-non-updatable-apex-file_contexts",
7671 ],
7672 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007673 `
Paul Duffinc3bbb962020-12-10 19:15:49 +00007674
Paul Duffin89f570a2021-06-16 01:42:33 +01007675 testDexpreoptWithApexes(t, bp, errmsg, preparer, fragments...)
Paul Duffinc3bbb962020-12-10 19:15:49 +00007676}
7677
Paul Duffin89f570a2021-06-16 01:42:33 +01007678func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, preparer android.FixturePreparer, fragments ...java.ApexVariantReference) *android.TestContext {
Paul Duffinc3bbb962020-12-10 19:15:49 +00007679 t.Helper()
7680
Paul Duffin55607122021-03-30 23:32:51 +01007681 fs := android.MockFS{
7682 "a.java": nil,
7683 "a.jar": nil,
7684 "apex_manifest.json": nil,
7685 "AndroidManifest.xml": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007686 "system/sepolicy/apex/myapex-file_contexts": nil,
Paul Duffind376f792021-01-26 11:59:35 +00007687 "system/sepolicy/apex/some-updatable-apex-file_contexts": nil,
7688 "system/sepolicy/apex/some-non-updatable-apex-file_contexts": nil,
7689 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
Martin Stjernholm1dc0d6d2021-01-17 21:05:12 +00007690 "framework/aidl/a.aidl": nil,
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007691 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007692
Paul Duffin55607122021-03-30 23:32:51 +01007693 errorHandler := android.FixtureExpectsNoErrors
7694 if errmsg != "" {
7695 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007696 }
Paul Duffin064b70c2020-11-02 17:32:38 +00007697
Paul Duffin55607122021-03-30 23:32:51 +01007698 result := android.GroupFixturePreparers(
7699 cc.PrepareForTestWithCcDefaultModules,
7700 java.PrepareForTestWithHiddenApiBuildComponents,
7701 java.PrepareForTestWithJavaDefaultModules,
7702 java.PrepareForTestWithJavaSdkLibraryFiles,
7703 PrepareForTestWithApexBuildComponents,
Paul Duffin60264a02021-04-12 20:02:36 +01007704 preparer,
Paul Duffin55607122021-03-30 23:32:51 +01007705 fs.AddToFixture(),
Paul Duffin89f570a2021-06-16 01:42:33 +01007706 android.FixtureModifyMockFS(func(fs android.MockFS) {
7707 if _, ok := fs["frameworks/base/boot/Android.bp"]; !ok {
7708 insert := ""
7709 for _, fragment := range fragments {
7710 insert += fmt.Sprintf("{apex: %q, module: %q},\n", *fragment.Apex, *fragment.Module)
7711 }
7712 fs["frameworks/base/boot/Android.bp"] = []byte(fmt.Sprintf(`
7713 platform_bootclasspath {
7714 name: "platform-bootclasspath",
7715 fragments: [
7716 %s
7717 ],
7718 }
7719 `, insert))
Paul Duffin8f146b92021-04-12 17:24:18 +01007720 }
Paul Duffin89f570a2021-06-16 01:42:33 +01007721 }),
Jiakai Zhang49b1eb62021-11-26 18:09:27 +00007722 dexpreopt.FixtureSetBootImageProfiles("art/build/boot/boot-image-profile.txt"),
Paul Duffin55607122021-03-30 23:32:51 +01007723 ).
7724 ExtendWithErrorHandler(errorHandler).
7725 RunTestWithBp(t, bp)
7726
7727 return result.TestContext
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00007728}
7729
Paul Duffin5556c5f2022-06-09 17:32:21 +00007730func TestDuplicateDeapexersFromPrebuiltApexes(t *testing.T) {
Martin Stjernholm43c44b02021-06-30 16:35:07 +01007731 preparers := android.GroupFixturePreparers(
7732 java.PrepareForTestWithJavaDefaultModules,
7733 PrepareForTestWithApexBuildComponents,
7734 ).
7735 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
7736 "Multiple installable prebuilt APEXes provide ambiguous deapexers: com.android.myapex and com.mycompany.android.myapex"))
7737
7738 bpBase := `
7739 apex_set {
7740 name: "com.android.myapex",
7741 installable: true,
7742 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7743 set: "myapex.apks",
7744 }
7745
7746 apex_set {
7747 name: "com.mycompany.android.myapex",
7748 apex_name: "com.android.myapex",
7749 installable: true,
7750 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7751 set: "company-myapex.apks",
7752 }
7753
7754 prebuilt_bootclasspath_fragment {
7755 name: "my-bootclasspath-fragment",
7756 apex_available: ["com.android.myapex"],
7757 %s
7758 }
7759 `
7760
7761 t.Run("java_import", func(t *testing.T) {
7762 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7763 java_import {
7764 name: "libfoo",
7765 jars: ["libfoo.jar"],
7766 apex_available: ["com.android.myapex"],
7767 }
7768 `)
7769 })
7770
7771 t.Run("java_sdk_library_import", func(t *testing.T) {
7772 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7773 java_sdk_library_import {
7774 name: "libfoo",
7775 public: {
7776 jars: ["libbar.jar"],
7777 },
7778 apex_available: ["com.android.myapex"],
7779 }
7780 `)
7781 })
7782
7783 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7784 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7785 image_name: "art",
7786 contents: ["libfoo"],
7787 `)+`
7788 java_sdk_library_import {
7789 name: "libfoo",
7790 public: {
7791 jars: ["libbar.jar"],
7792 },
7793 apex_available: ["com.android.myapex"],
7794 }
7795 `)
7796 })
7797}
7798
Paul Duffin5556c5f2022-06-09 17:32:21 +00007799func TestDuplicateButEquivalentDeapexersFromPrebuiltApexes(t *testing.T) {
7800 preparers := android.GroupFixturePreparers(
7801 java.PrepareForTestWithJavaDefaultModules,
7802 PrepareForTestWithApexBuildComponents,
7803 )
7804
7805 bpBase := `
7806 apex_set {
7807 name: "com.android.myapex",
7808 installable: true,
7809 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7810 set: "myapex.apks",
7811 }
7812
7813 apex_set {
7814 name: "com.android.myapex_compressed",
7815 apex_name: "com.android.myapex",
7816 installable: true,
7817 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
7818 set: "myapex_compressed.apks",
7819 }
7820
7821 prebuilt_bootclasspath_fragment {
7822 name: "my-bootclasspath-fragment",
7823 apex_available: [
7824 "com.android.myapex",
7825 "com.android.myapex_compressed",
7826 ],
7827 hidden_api: {
7828 annotation_flags: "annotation-flags.csv",
7829 metadata: "metadata.csv",
7830 index: "index.csv",
7831 signature_patterns: "signature_patterns.csv",
7832 },
7833 %s
7834 }
7835 `
7836
7837 t.Run("java_import", func(t *testing.T) {
7838 result := preparers.RunTestWithBp(t,
7839 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7840 java_import {
7841 name: "libfoo",
7842 jars: ["libfoo.jar"],
7843 apex_available: [
7844 "com.android.myapex",
7845 "com.android.myapex_compressed",
7846 ],
7847 }
7848 `)
7849
7850 module := result.Module("libfoo", "android_common_com.android.myapex")
7851 usesLibraryDep := module.(java.UsesLibraryDependency)
7852 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7853 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7854 usesLibraryDep.DexJarBuildPath().Path())
7855 })
7856
7857 t.Run("java_sdk_library_import", func(t *testing.T) {
7858 result := preparers.RunTestWithBp(t,
7859 fmt.Sprintf(bpBase, `contents: ["libfoo"]`)+`
7860 java_sdk_library_import {
7861 name: "libfoo",
7862 public: {
7863 jars: ["libbar.jar"],
7864 },
7865 apex_available: [
7866 "com.android.myapex",
7867 "com.android.myapex_compressed",
7868 ],
7869 compile_dex: true,
7870 }
7871 `)
7872
7873 module := result.Module("libfoo", "android_common_com.android.myapex")
7874 usesLibraryDep := module.(java.UsesLibraryDependency)
7875 android.AssertPathRelativeToTopEquals(t, "dex jar path",
7876 "out/soong/.intermediates/com.android.myapex.deapexer/android_common/deapexer/javalib/libfoo.jar",
7877 usesLibraryDep.DexJarBuildPath().Path())
7878 })
7879
7880 t.Run("prebuilt_bootclasspath_fragment", func(t *testing.T) {
7881 _ = preparers.RunTestWithBp(t, fmt.Sprintf(bpBase, `
7882 image_name: "art",
7883 contents: ["libfoo"],
7884 `)+`
7885 java_sdk_library_import {
7886 name: "libfoo",
7887 public: {
7888 jars: ["libbar.jar"],
7889 },
7890 apex_available: [
7891 "com.android.myapex",
7892 "com.android.myapex_compressed",
7893 ],
7894 compile_dex: true,
7895 }
7896 `)
7897 })
7898}
7899
Jooyung Han548640b2020-04-27 12:10:30 +09007900func TestUpdatable_should_set_min_sdk_version(t *testing.T) {
7901 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7902 apex {
7903 name: "myapex",
7904 key: "myapex.key",
7905 updatable: true,
7906 }
7907
7908 apex_key {
7909 name: "myapex.key",
7910 public_key: "testkey.avbpubkey",
7911 private_key: "testkey.pem",
7912 }
7913 `)
7914}
7915
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00007916func TestUpdatableDefault_should_set_min_sdk_version(t *testing.T) {
7917 testApexError(t, `"myapex" .*: updatable: updatable APEXes should set min_sdk_version`, `
7918 apex {
7919 name: "myapex",
7920 key: "myapex.key",
7921 }
7922
7923 apex_key {
7924 name: "myapex.key",
7925 public_key: "testkey.avbpubkey",
7926 private_key: "testkey.pem",
7927 }
7928 `)
7929}
7930
Jooyung Handfc864c2023-03-20 18:19:07 +09007931func Test_use_vndk_as_stable_shouldnt_be_used_for_updatable_vendor_apexes(t *testing.T) {
7932 testApexError(t, `"myapex" .*: use_vndk_as_stable: updatable APEXes can't use external VNDK libs`, `
Daniel Norman69109112021-12-02 12:52:42 -08007933 apex {
7934 name: "myapex",
7935 key: "myapex.key",
7936 updatable: true,
Jooyung Handfc864c2023-03-20 18:19:07 +09007937 use_vndk_as_stable: true,
Daniel Norman69109112021-12-02 12:52:42 -08007938 soc_specific: true,
7939 }
7940
7941 apex_key {
7942 name: "myapex.key",
7943 public_key: "testkey.avbpubkey",
7944 private_key: "testkey.pem",
7945 }
7946 `)
7947}
7948
Jooyung Han02873da2023-03-22 17:41:03 +09007949func Test_use_vndk_as_stable_shouldnt_be_used_with_min_sdk_version(t *testing.T) {
7950 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported when min_sdk_version is set`, `
7951 apex {
7952 name: "myapex",
7953 key: "myapex.key",
7954 updatable: false,
7955 min_sdk_version: "29",
7956 use_vndk_as_stable: true,
7957 vendor: true,
7958 }
7959
7960 apex_key {
7961 name: "myapex.key",
7962 public_key: "testkey.avbpubkey",
7963 private_key: "testkey.pem",
7964 }
7965 `)
7966}
7967
Jooyung Handfc864c2023-03-20 18:19:07 +09007968func Test_use_vndk_as_stable_shouldnt_be_used_for_non_vendor_apexes(t *testing.T) {
7969 testApexError(t, `"myapex" .*: use_vndk_as_stable: not supported for system/system_ext APEXes`, `
7970 apex {
7971 name: "myapex",
7972 key: "myapex.key",
7973 updatable: false,
7974 use_vndk_as_stable: true,
7975 }
7976
7977 apex_key {
7978 name: "myapex.key",
7979 public_key: "testkey.avbpubkey",
7980 private_key: "testkey.pem",
7981 }
7982 `)
7983}
7984
satayevb98371c2021-06-15 16:49:50 +01007985func TestUpdatable_should_not_set_generate_classpaths_proto(t *testing.T) {
7986 testApexError(t, `"mysystemserverclasspathfragment" .* it must not set generate_classpaths_proto to false`, `
7987 apex {
7988 name: "myapex",
7989 key: "myapex.key",
7990 systemserverclasspath_fragments: [
7991 "mysystemserverclasspathfragment",
7992 ],
7993 min_sdk_version: "29",
7994 updatable: true,
7995 }
7996
7997 apex_key {
7998 name: "myapex.key",
7999 public_key: "testkey.avbpubkey",
8000 private_key: "testkey.pem",
8001 }
8002
8003 java_library {
8004 name: "foo",
8005 srcs: ["b.java"],
8006 min_sdk_version: "29",
8007 installable: true,
8008 apex_available: [
8009 "myapex",
8010 ],
8011 }
8012
8013 systemserverclasspath_fragment {
8014 name: "mysystemserverclasspathfragment",
8015 generate_classpaths_proto: false,
8016 contents: [
8017 "foo",
8018 ],
8019 apex_available: [
8020 "myapex",
8021 ],
8022 }
satayevabcd5972021-08-06 17:49:46 +01008023 `,
8024 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
8025 )
satayevb98371c2021-06-15 16:49:50 +01008026}
8027
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008028func TestNoUpdatableJarsInBootImage(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01008029 // Set the BootJars in dexpreopt.GlobalConfig and productVariables to the same value. This can
8030 // result in an invalid configuration as it does not set the ArtApexJars and allows art apex
8031 // modules to be included in the BootJars.
8032 prepareSetBootJars := func(bootJars ...string) android.FixturePreparer {
8033 return android.GroupFixturePreparers(
8034 dexpreopt.FixtureSetBootJars(bootJars...),
8035 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8036 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
8037 }),
8038 )
8039 }
8040
8041 // Set the ArtApexJars and BootJars in dexpreopt.GlobalConfig and productVariables all to the
8042 // same value. This can result in an invalid configuration as it allows non art apex jars to be
8043 // specified in the ArtApexJars configuration.
8044 prepareSetArtJars := func(bootJars ...string) android.FixturePreparer {
8045 return android.GroupFixturePreparers(
8046 dexpreopt.FixtureSetArtBootJars(bootJars...),
8047 dexpreopt.FixtureSetBootJars(bootJars...),
8048 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8049 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
8050 }),
8051 )
8052 }
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008053
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008054 t.Run("updatable jar from ART apex in the ART boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008055 preparer := android.GroupFixturePreparers(
8056 java.FixtureConfigureBootJars("com.android.art.debug:some-art-lib"),
8057 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8058 )
8059 fragments := []java.ApexVariantReference{
8060 {
8061 Apex: proptools.StringPtr("com.android.art.debug"),
8062 Module: proptools.StringPtr("art-bootclasspath-fragment"),
8063 },
8064 {
8065 Apex: proptools.StringPtr("some-non-updatable-apex"),
8066 Module: proptools.StringPtr("some-non-updatable-fragment"),
8067 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008068 }
satayevabcd5972021-08-06 17:49:46 +01008069 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008070 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008071
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008072 t.Run("updatable jar from ART apex in the framework boot image => error", func(t *testing.T) {
Paul Duffin60264a02021-04-12 20:02:36 +01008073 err := `module "some-art-lib" from updatable apexes \["com.android.art.debug"\] is not allowed in the framework boot image`
8074 // Update the dexpreopt BootJars directly.
satayevabcd5972021-08-06 17:49:46 +01008075 preparer := android.GroupFixturePreparers(
8076 prepareSetBootJars("com.android.art.debug:some-art-lib"),
8077 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8078 )
Paul Duffin60264a02021-04-12 20:02:36 +01008079 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008080 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008081
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008082 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 +01008083 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 +01008084 // Update the dexpreopt ArtApexJars directly.
8085 preparer := prepareSetArtJars("some-updatable-apex:some-updatable-apex-lib")
8086 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008087 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008088
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008089 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 +01008090 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 +01008091 // Update the dexpreopt ArtApexJars directly.
8092 preparer := prepareSetArtJars("some-non-updatable-apex:some-non-updatable-apex-lib")
8093 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008094 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008095
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008096 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 +01008097 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 +01008098 preparer := android.GroupFixturePreparers(
8099 java.FixtureConfigureBootJars("some-updatable-apex:some-updatable-apex-lib"),
8100 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8101 )
Paul Duffin60264a02021-04-12 20:02:36 +01008102 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008103 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008104
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008105 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 +01008106 preparer := java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib")
Paul Duffin89f570a2021-06-16 01:42:33 +01008107 fragment := java.ApexVariantReference{
8108 Apex: proptools.StringPtr("some-non-updatable-apex"),
8109 Module: proptools.StringPtr("some-non-updatable-fragment"),
8110 }
8111 testNoUpdatableJarsInBootImage(t, "", preparer, fragment)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008112 })
Ulya Trafimovich7c140d82020-04-22 18:05:58 +01008113
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008114 t.Run("nonexistent jar in the ART boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008115 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008116 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8117 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008118 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008119
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008120 t.Run("nonexistent jar in the framework boot image => error", func(t *testing.T) {
Paul Duffin8f146b92021-04-12 17:24:18 +01008121 err := `"platform-bootclasspath" depends on undefined module "nonexistent"`
Paul Duffin60264a02021-04-12 20:02:36 +01008122 preparer := java.FixtureConfigureBootJars("platform:nonexistent")
8123 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008124 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008125
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008126 t.Run("platform jar in the ART boot image => error", func(t *testing.T) {
Paul Duffinf23bc472021-04-27 12:42:20 +01008127 err := `ArtApexJars is invalid as it requests a platform variant of "some-platform-lib"`
Paul Duffin60264a02021-04-12 20:02:36 +01008128 // Update the dexpreopt ArtApexJars directly.
8129 preparer := prepareSetArtJars("platform:some-platform-lib")
8130 testNoUpdatableJarsInBootImage(t, err, preparer)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008131 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008132
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008133 t.Run("platform jar in the framework boot image => ok", func(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008134 preparer := android.GroupFixturePreparers(
8135 java.FixtureConfigureBootJars("platform:some-platform-lib"),
8136 java.FixtureConfigureApexBootJars("some-non-updatable-apex:some-non-updatable-apex-lib"),
8137 )
8138 fragments := []java.ApexVariantReference{
8139 {
8140 Apex: proptools.StringPtr("some-non-updatable-apex"),
8141 Module: proptools.StringPtr("some-non-updatable-fragment"),
8142 },
8143 }
8144 testNoUpdatableJarsInBootImage(t, "", preparer, fragments...)
Ulya Trafimovich7caef202020-05-19 12:00:52 +01008145 })
Paul Duffin064b70c2020-11-02 17:32:38 +00008146}
8147
8148func TestDexpreoptAccessDexFilesFromPrebuiltApex(t *testing.T) {
satayevabcd5972021-08-06 17:49:46 +01008149 preparer := java.FixtureConfigureApexBootJars("myapex:libfoo")
Paul Duffin064b70c2020-11-02 17:32:38 +00008150 t.Run("prebuilt no source", func(t *testing.T) {
Paul Duffin89f570a2021-06-16 01:42:33 +01008151 fragment := java.ApexVariantReference{
8152 Apex: proptools.StringPtr("myapex"),
8153 Module: proptools.StringPtr("my-bootclasspath-fragment"),
8154 }
8155
Paul Duffin064b70c2020-11-02 17:32:38 +00008156 testDexpreoptWithApexes(t, `
8157 prebuilt_apex {
8158 name: "myapex" ,
8159 arch: {
8160 arm64: {
8161 src: "myapex-arm64.apex",
8162 },
8163 arm: {
8164 src: "myapex-arm.apex",
8165 },
8166 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008167 exported_bootclasspath_fragments: ["my-bootclasspath-fragment"],
8168 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008169
Paul Duffin89f570a2021-06-16 01:42:33 +01008170 prebuilt_bootclasspath_fragment {
8171 name: "my-bootclasspath-fragment",
8172 contents: ["libfoo"],
8173 apex_available: ["myapex"],
Paul Duffin54e41972021-07-19 13:23:40 +01008174 hidden_api: {
8175 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
8176 metadata: "my-bootclasspath-fragment/metadata.csv",
8177 index: "my-bootclasspath-fragment/index.csv",
Paul Duffin191be3a2021-08-10 16:14:16 +01008178 signature_patterns: "my-bootclasspath-fragment/signature-patterns.csv",
8179 filtered_stub_flags: "my-bootclasspath-fragment/filtered-stub-flags.csv",
8180 filtered_flags: "my-bootclasspath-fragment/filtered-flags.csv",
Paul Duffin54e41972021-07-19 13:23:40 +01008181 },
Paul Duffin89f570a2021-06-16 01:42:33 +01008182 }
Paul Duffin064b70c2020-11-02 17:32:38 +00008183
Paul Duffin89f570a2021-06-16 01:42:33 +01008184 java_import {
8185 name: "libfoo",
8186 jars: ["libfoo.jar"],
8187 apex_available: ["myapex"],
satayevabcd5972021-08-06 17:49:46 +01008188 permitted_packages: ["libfoo"],
Paul Duffin89f570a2021-06-16 01:42:33 +01008189 }
8190 `, "", preparer, fragment)
Paul Duffin064b70c2020-11-02 17:32:38 +00008191 })
Ulya Trafimovichb28cc372020-01-13 15:18:16 +00008192}
8193
Spandan Dasf14e2542021-11-12 00:01:37 +00008194func testBootJarPermittedPackagesRules(t *testing.T, errmsg, bp string, bootJars []string, rules []android.Rule) {
Andrei Onea115e7e72020-06-05 21:14:03 +01008195 t.Helper()
Andrei Onea115e7e72020-06-05 21:14:03 +01008196 bp += `
8197 apex_key {
8198 name: "myapex.key",
8199 public_key: "testkey.avbpubkey",
8200 private_key: "testkey.pem",
8201 }`
Paul Duffin45338f02021-03-30 23:07:52 +01008202 fs := android.MockFS{
Andrei Onea115e7e72020-06-05 21:14:03 +01008203 "lib1/src/A.java": nil,
8204 "lib2/src/B.java": nil,
8205 "system/sepolicy/apex/myapex-file_contexts": nil,
8206 }
8207
Paul Duffin45338f02021-03-30 23:07:52 +01008208 errorHandler := android.FixtureExpectsNoErrors
8209 if errmsg != "" {
8210 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(errmsg)
Colin Crossae8600b2020-10-29 17:09:13 -07008211 }
Colin Crossae8600b2020-10-29 17:09:13 -07008212
Paul Duffin45338f02021-03-30 23:07:52 +01008213 android.GroupFixturePreparers(
8214 android.PrepareForTestWithAndroidBuildComponents,
8215 java.PrepareForTestWithJavaBuildComponents,
8216 PrepareForTestWithApexBuildComponents,
8217 android.PrepareForTestWithNeverallowRules(rules),
8218 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
satayevd604b212021-07-21 14:23:52 +01008219 apexBootJars := make([]string, 0, len(bootJars))
8220 for _, apexBootJar := range bootJars {
8221 apexBootJars = append(apexBootJars, "myapex:"+apexBootJar)
Paul Duffin45338f02021-03-30 23:07:52 +01008222 }
satayevd604b212021-07-21 14:23:52 +01008223 variables.ApexBootJars = android.CreateTestConfiguredJarList(apexBootJars)
Paul Duffin45338f02021-03-30 23:07:52 +01008224 }),
8225 fs.AddToFixture(),
8226 ).
8227 ExtendWithErrorHandler(errorHandler).
8228 RunTestWithBp(t, bp)
Andrei Onea115e7e72020-06-05 21:14:03 +01008229}
8230
8231func TestApexPermittedPackagesRules(t *testing.T) {
8232 testcases := []struct {
Spandan Dasf14e2542021-11-12 00:01:37 +00008233 name string
8234 expectedError string
8235 bp string
8236 bootJars []string
8237 bcpPermittedPackages map[string][]string
Andrei Onea115e7e72020-06-05 21:14:03 +01008238 }{
8239
8240 {
8241 name: "Non-Bootclasspath apex jar not satisfying allowed module packages.",
8242 expectedError: "",
8243 bp: `
8244 java_library {
8245 name: "bcp_lib1",
8246 srcs: ["lib1/src/*.java"],
8247 permitted_packages: ["foo.bar"],
8248 apex_available: ["myapex"],
8249 sdk_version: "none",
8250 system_modules: "none",
8251 }
8252 java_library {
8253 name: "nonbcp_lib2",
8254 srcs: ["lib2/src/*.java"],
8255 apex_available: ["myapex"],
8256 permitted_packages: ["a.b"],
8257 sdk_version: "none",
8258 system_modules: "none",
8259 }
8260 apex {
8261 name: "myapex",
8262 key: "myapex.key",
8263 java_libs: ["bcp_lib1", "nonbcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008264 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008265 }`,
8266 bootJars: []string{"bcp_lib1"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008267 bcpPermittedPackages: map[string][]string{
8268 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008269 "foo.bar",
8270 },
8271 },
8272 },
8273 {
Anton Hanssone1b18362021-12-23 15:05:38 +00008274 name: "Bootclasspath apex jar not satisfying allowed module packages.",
Spandan Dasf14e2542021-11-12 00:01:37 +00008275 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 +01008276 bp: `
8277 java_library {
8278 name: "bcp_lib1",
8279 srcs: ["lib1/src/*.java"],
8280 apex_available: ["myapex"],
8281 permitted_packages: ["foo.bar"],
8282 sdk_version: "none",
8283 system_modules: "none",
8284 }
8285 java_library {
8286 name: "bcp_lib2",
8287 srcs: ["lib2/src/*.java"],
8288 apex_available: ["myapex"],
8289 permitted_packages: ["foo.bar", "bar.baz"],
8290 sdk_version: "none",
8291 system_modules: "none",
8292 }
8293 apex {
8294 name: "myapex",
8295 key: "myapex.key",
8296 java_libs: ["bcp_lib1", "bcp_lib2"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008297 updatable: false,
Andrei Onea115e7e72020-06-05 21:14:03 +01008298 }
8299 `,
8300 bootJars: []string{"bcp_lib1", "bcp_lib2"},
Spandan Dasf14e2542021-11-12 00:01:37 +00008301 bcpPermittedPackages: map[string][]string{
8302 "bcp_lib1": []string{
Andrei Onea115e7e72020-06-05 21:14:03 +01008303 "foo.bar",
8304 },
Spandan Dasf14e2542021-11-12 00:01:37 +00008305 "bcp_lib2": []string{
8306 "foo.bar",
8307 },
8308 },
8309 },
8310 {
8311 name: "Updateable Bootclasspath apex jar not satisfying allowed module packages.",
8312 expectedError: "",
8313 bp: `
8314 java_library {
8315 name: "bcp_lib_restricted",
8316 srcs: ["lib1/src/*.java"],
8317 apex_available: ["myapex"],
8318 permitted_packages: ["foo.bar"],
8319 sdk_version: "none",
8320 min_sdk_version: "29",
8321 system_modules: "none",
8322 }
8323 java_library {
8324 name: "bcp_lib_unrestricted",
8325 srcs: ["lib2/src/*.java"],
8326 apex_available: ["myapex"],
8327 permitted_packages: ["foo.bar", "bar.baz"],
8328 sdk_version: "none",
8329 min_sdk_version: "29",
8330 system_modules: "none",
8331 }
8332 apex {
8333 name: "myapex",
8334 key: "myapex.key",
8335 java_libs: ["bcp_lib_restricted", "bcp_lib_unrestricted"],
8336 updatable: true,
8337 min_sdk_version: "29",
8338 }
8339 `,
8340 bootJars: []string{"bcp_lib1", "bcp_lib2"},
8341 bcpPermittedPackages: map[string][]string{
8342 "bcp_lib1_non_updateable": []string{
8343 "foo.bar",
8344 },
8345 // 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 +01008346 },
8347 },
8348 }
8349 for _, tc := range testcases {
8350 t.Run(tc.name, func(t *testing.T) {
Spandan Dasf14e2542021-11-12 00:01:37 +00008351 rules := createBcpPermittedPackagesRules(tc.bcpPermittedPackages)
8352 testBootJarPermittedPackagesRules(t, tc.expectedError, tc.bp, tc.bootJars, rules)
Andrei Onea115e7e72020-06-05 21:14:03 +01008353 })
8354 }
8355}
8356
Jiyong Park62304bb2020-04-13 16:19:48 +09008357func TestTestFor(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008358 ctx := testApex(t, `
Jiyong Park62304bb2020-04-13 16:19:48 +09008359 apex {
8360 name: "myapex",
8361 key: "myapex.key",
8362 native_shared_libs: ["mylib", "myprivlib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008363 updatable: false,
Jiyong Park62304bb2020-04-13 16:19:48 +09008364 }
8365
8366 apex_key {
8367 name: "myapex.key",
8368 public_key: "testkey.avbpubkey",
8369 private_key: "testkey.pem",
8370 }
8371
8372 cc_library {
8373 name: "mylib",
8374 srcs: ["mylib.cpp"],
8375 system_shared_libs: [],
8376 stl: "none",
8377 stubs: {
8378 versions: ["1"],
8379 },
8380 apex_available: ["myapex"],
8381 }
8382
8383 cc_library {
8384 name: "myprivlib",
8385 srcs: ["mylib.cpp"],
8386 system_shared_libs: [],
8387 stl: "none",
8388 apex_available: ["myapex"],
8389 }
8390
8391
8392 cc_test {
8393 name: "mytest",
8394 gtest: false,
8395 srcs: ["mylib.cpp"],
8396 system_shared_libs: [],
8397 stl: "none",
Jiyong Park46a512f2020-12-04 18:02:13 +09008398 shared_libs: ["mylib", "myprivlib", "mytestlib"],
Jiyong Park62304bb2020-04-13 16:19:48 +09008399 test_for: ["myapex"]
8400 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008401
8402 cc_library {
8403 name: "mytestlib",
8404 srcs: ["mylib.cpp"],
8405 system_shared_libs: [],
8406 shared_libs: ["mylib", "myprivlib"],
8407 stl: "none",
8408 test_for: ["myapex"],
8409 }
8410
8411 cc_benchmark {
8412 name: "mybench",
8413 srcs: ["mylib.cpp"],
8414 system_shared_libs: [],
8415 shared_libs: ["mylib", "myprivlib"],
8416 stl: "none",
8417 test_for: ["myapex"],
8418 }
Jiyong Park62304bb2020-04-13 16:19:48 +09008419 `)
8420
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008421 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008422 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008423 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8424 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8425 }
8426
8427 // These modules are tests for the apex, therefore are linked to the
Jiyong Park62304bb2020-04-13 16:19:48 +09008428 // actual implementation of mylib instead of its stub.
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008429 ensureLinkedLibIs("mytest", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8430 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8431 ensureLinkedLibIs("mybench", "android_arm64_armv8-a", "out/soong/.intermediates/mylib/", "android_arm64_armv8-a_shared/mylib.so")
8432}
Jiyong Park46a512f2020-12-04 18:02:13 +09008433
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008434func TestIndirectTestFor(t *testing.T) {
8435 ctx := testApex(t, `
8436 apex {
8437 name: "myapex",
8438 key: "myapex.key",
8439 native_shared_libs: ["mylib", "myprivlib"],
8440 updatable: false,
8441 }
Jiyong Park46a512f2020-12-04 18:02:13 +09008442
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008443 apex_key {
8444 name: "myapex.key",
8445 public_key: "testkey.avbpubkey",
8446 private_key: "testkey.pem",
8447 }
8448
8449 cc_library {
8450 name: "mylib",
8451 srcs: ["mylib.cpp"],
8452 system_shared_libs: [],
8453 stl: "none",
8454 stubs: {
8455 versions: ["1"],
8456 },
8457 apex_available: ["myapex"],
8458 }
8459
8460 cc_library {
8461 name: "myprivlib",
8462 srcs: ["mylib.cpp"],
8463 system_shared_libs: [],
8464 stl: "none",
8465 shared_libs: ["mylib"],
8466 apex_available: ["myapex"],
8467 }
8468
8469 cc_library {
8470 name: "mytestlib",
8471 srcs: ["mylib.cpp"],
8472 system_shared_libs: [],
8473 shared_libs: ["myprivlib"],
8474 stl: "none",
8475 test_for: ["myapex"],
8476 }
8477 `)
8478
8479 ensureLinkedLibIs := func(mod, variant, linkedLib, expectedVariant string) {
Paul Duffina71a67a2021-03-29 00:42:57 +01008480 ldFlags := strings.Split(ctx.ModuleForTests(mod, variant).Rule("ld").Args["libFlags"], " ")
Martin Stjernholm4e6c2692021-03-25 01:25:06 +00008481 mylibLdFlags := android.FilterListPred(ldFlags, func(s string) bool { return strings.HasPrefix(s, linkedLib) })
8482 android.AssertArrayString(t, "unexpected "+linkedLib+" link library for "+mod, []string{linkedLib + expectedVariant}, mylibLdFlags)
8483 }
8484
8485 // The platform variant of mytestlib links to the platform variant of the
8486 // internal myprivlib.
8487 ensureLinkedLibIs("mytestlib", "android_arm64_armv8-a_shared", "out/soong/.intermediates/myprivlib/", "android_arm64_armv8-a_shared/myprivlib.so")
8488
8489 // The platform variant of myprivlib links to the platform variant of mylib
8490 // and bypasses its stubs.
8491 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 +09008492}
8493
Martin Stjernholmec009002021-03-27 15:18:31 +00008494func TestTestForForLibInOtherApex(t *testing.T) {
8495 // This case is only allowed for known overlapping APEXes, i.e. the ART APEXes.
8496 _ = testApex(t, `
8497 apex {
8498 name: "com.android.art",
8499 key: "myapex.key",
8500 native_shared_libs: ["mylib"],
8501 updatable: false,
8502 }
8503
8504 apex {
8505 name: "com.android.art.debug",
8506 key: "myapex.key",
8507 native_shared_libs: ["mylib", "mytestlib"],
8508 updatable: false,
8509 }
8510
8511 apex_key {
8512 name: "myapex.key",
8513 public_key: "testkey.avbpubkey",
8514 private_key: "testkey.pem",
8515 }
8516
8517 cc_library {
8518 name: "mylib",
8519 srcs: ["mylib.cpp"],
8520 system_shared_libs: [],
8521 stl: "none",
8522 stubs: {
8523 versions: ["1"],
8524 },
8525 apex_available: ["com.android.art", "com.android.art.debug"],
8526 }
8527
8528 cc_library {
8529 name: "mytestlib",
8530 srcs: ["mylib.cpp"],
8531 system_shared_libs: [],
8532 shared_libs: ["mylib"],
8533 stl: "none",
8534 apex_available: ["com.android.art.debug"],
8535 test_for: ["com.android.art"],
8536 }
8537 `,
8538 android.MockFS{
8539 "system/sepolicy/apex/com.android.art-file_contexts": nil,
8540 "system/sepolicy/apex/com.android.art.debug-file_contexts": nil,
8541 }.AddToFixture())
8542}
8543
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008544// TODO(jungjw): Move this to proptools
8545func intPtr(i int) *int {
8546 return &i
8547}
8548
8549func TestApexSet(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008550 ctx := testApex(t, `
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008551 apex_set {
8552 name: "myapex",
8553 set: "myapex.apks",
8554 filename: "foo_v2.apex",
8555 overrides: ["foo"],
8556 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008557 `,
8558 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8559 variables.Platform_sdk_version = intPtr(30)
8560 }),
8561 android.FixtureModifyConfig(func(config android.Config) {
8562 config.Targets[android.Android] = []android.Target{
8563 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}},
8564 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}},
8565 }
8566 }),
8567 )
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008568
Paul Duffin24704672021-04-06 16:09:30 +01008569 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008570
8571 // Check extract_apks tool parameters.
Paul Duffin24704672021-04-06 16:09:30 +01008572 extractedApex := m.Output("extracted/myapex.apks")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008573 actual := extractedApex.Args["abis"]
8574 expected := "ARMEABI_V7A,ARM64_V8A"
8575 if actual != expected {
8576 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8577 }
8578 actual = extractedApex.Args["sdk-version"]
8579 expected = "30"
8580 if actual != expected {
8581 t.Errorf("Unexpected abis parameter - expected %q vs actual %q", expected, actual)
8582 }
8583
Paul Duffin6717d882021-06-15 19:09:41 +01008584 m = ctx.ModuleForTests("myapex", "android_common_myapex")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008585 a := m.Module().(*ApexSet)
8586 expectedOverrides := []string{"foo"}
Colin Crossaa255532020-07-03 13:18:24 -07008587 actualOverrides := android.AndroidMkEntriesForTest(t, ctx, a)[0].EntryMap["LOCAL_OVERRIDES_MODULES"]
Jaewoong Jungfa00c062020-05-14 14:15:24 -07008588 if !reflect.DeepEqual(actualOverrides, expectedOverrides) {
8589 t.Errorf("Incorrect LOCAL_OVERRIDES_MODULES - expected %q vs actual %q", expectedOverrides, actualOverrides)
8590 }
8591}
8592
Anton Hansson805e0a52022-11-25 14:06:46 +00008593func TestApexSet_NativeBridge(t *testing.T) {
8594 ctx := testApex(t, `
8595 apex_set {
8596 name: "myapex",
8597 set: "myapex.apks",
8598 filename: "foo_v2.apex",
8599 overrides: ["foo"],
8600 }
8601 `,
8602 android.FixtureModifyConfig(func(config android.Config) {
8603 config.Targets[android.Android] = []android.Target{
8604 {Os: android.Android, Arch: android.Arch{ArchType: android.X86_64, ArchVariant: "", Abi: []string{"x86_64"}}},
8605 {Os: android.Android, Arch: android.Arch{ArchType: android.Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridge: android.NativeBridgeEnabled},
8606 }
8607 }),
8608 )
8609
8610 m := ctx.ModuleForTests("myapex.apex.extractor", "android_common")
8611
8612 // Check extract_apks tool parameters. No native bridge arch expected
8613 extractedApex := m.Output("extracted/myapex.apks")
8614 android.AssertStringEquals(t, "abis", "X86_64", extractedApex.Args["abis"])
8615}
8616
Jiyong Park7d95a512020-05-10 15:16:24 +09008617func TestNoStaticLinkingToStubsLib(t *testing.T) {
8618 testApexError(t, `.*required by "mylib" is a native library providing stub.*`, `
8619 apex {
8620 name: "myapex",
8621 key: "myapex.key",
8622 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008623 updatable: false,
Jiyong Park7d95a512020-05-10 15:16:24 +09008624 }
8625
8626 apex_key {
8627 name: "myapex.key",
8628 public_key: "testkey.avbpubkey",
8629 private_key: "testkey.pem",
8630 }
8631
8632 cc_library {
8633 name: "mylib",
8634 srcs: ["mylib.cpp"],
8635 static_libs: ["otherlib"],
8636 system_shared_libs: [],
8637 stl: "none",
8638 apex_available: [ "myapex" ],
8639 }
8640
8641 cc_library {
8642 name: "otherlib",
8643 srcs: ["mylib.cpp"],
8644 system_shared_libs: [],
8645 stl: "none",
8646 stubs: {
8647 versions: ["1", "2", "3"],
8648 },
8649 apex_available: [ "myapex" ],
8650 }
8651 `)
8652}
8653
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008654func TestApexKeysTxt(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008655 ctx := testApex(t, `
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008656 apex {
8657 name: "myapex",
8658 key: "myapex.key",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008659 updatable: false,
Jooyung Han09c11ad2021-10-27 03:45:31 +09008660 custom_sign_tool: "sign_myapex",
8661 }
8662
8663 apex_key {
8664 name: "myapex.key",
8665 public_key: "testkey.avbpubkey",
8666 private_key: "testkey.pem",
8667 }
8668 `)
8669
8670 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8671 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8672 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"`)
8673}
8674
8675func TestApexKeysTxtOverrides(t *testing.T) {
8676 ctx := testApex(t, `
8677 apex {
8678 name: "myapex",
8679 key: "myapex.key",
8680 updatable: false,
8681 custom_sign_tool: "sign_myapex",
Jiyong Park8d6c51e2020-06-12 17:26:31 +09008682 }
8683
8684 apex_key {
8685 name: "myapex.key",
8686 public_key: "testkey.avbpubkey",
8687 private_key: "testkey.pem",
8688 }
8689
8690 prebuilt_apex {
8691 name: "myapex",
8692 prefer: true,
8693 arch: {
8694 arm64: {
8695 src: "myapex-arm64.apex",
8696 },
8697 arm: {
8698 src: "myapex-arm.apex",
8699 },
8700 },
8701 }
8702
8703 apex_set {
8704 name: "myapex_set",
8705 set: "myapex.apks",
8706 filename: "myapex_set.apex",
8707 overrides: ["myapex"],
8708 }
8709 `)
8710
8711 apexKeysText := ctx.SingletonForTests("apex_keys_text")
8712 content := apexKeysText.MaybeDescription("apexkeys.txt").BuildParams.Args["content"]
8713 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 +09008714 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 +09008715}
8716
Jooyung Han938b5932020-06-20 12:47:47 +09008717func TestAllowedFiles(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008718 ctx := testApex(t, `
Jooyung Han938b5932020-06-20 12:47:47 +09008719 apex {
8720 name: "myapex",
8721 key: "myapex.key",
8722 apps: ["app"],
8723 allowed_files: "allowed.txt",
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008724 updatable: false,
Jooyung Han938b5932020-06-20 12:47:47 +09008725 }
8726
8727 apex_key {
8728 name: "myapex.key",
8729 public_key: "testkey.avbpubkey",
8730 private_key: "testkey.pem",
8731 }
8732
8733 android_app {
8734 name: "app",
8735 srcs: ["foo/bar/MyClass.java"],
8736 package_name: "foo",
8737 sdk_version: "none",
8738 system_modules: "none",
8739 apex_available: [ "myapex" ],
8740 }
8741 `, withFiles(map[string][]byte{
8742 "sub/Android.bp": []byte(`
8743 override_apex {
8744 name: "override_myapex",
8745 base: "myapex",
8746 apps: ["override_app"],
8747 allowed_files: ":allowed",
8748 }
8749 // Overridable "path" property should be referenced indirectly
8750 filegroup {
8751 name: "allowed",
8752 srcs: ["allowed.txt"],
8753 }
8754 override_android_app {
8755 name: "override_app",
8756 base: "app",
8757 package_name: "bar",
8758 }
8759 `),
8760 }))
8761
8762 rule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("diffApexContentRule")
8763 if expected, actual := "allowed.txt", rule.Args["allowed_files_file"]; expected != actual {
8764 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8765 }
8766
8767 rule2 := ctx.ModuleForTests("myapex", "android_common_override_myapex_myapex_image").Rule("diffApexContentRule")
8768 if expected, actual := "sub/allowed.txt", rule2.Args["allowed_files_file"]; expected != actual {
8769 t.Errorf("allowed_files_file: expected %q but got %q", expected, actual)
8770 }
8771}
8772
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008773func TestNonPreferredPrebuiltDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008774 testApex(t, `
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008775 apex {
8776 name: "myapex",
8777 key: "myapex.key",
8778 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008779 updatable: false,
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008780 }
8781
8782 apex_key {
8783 name: "myapex.key",
8784 public_key: "testkey.avbpubkey",
8785 private_key: "testkey.pem",
8786 }
8787
8788 cc_library {
8789 name: "mylib",
8790 srcs: ["mylib.cpp"],
8791 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008792 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008793 },
8794 apex_available: ["myapex"],
8795 }
8796
8797 cc_prebuilt_library_shared {
8798 name: "mylib",
8799 prefer: false,
8800 srcs: ["prebuilt.so"],
8801 stubs: {
Dan Albertc8060532020-07-22 22:32:17 -07008802 versions: ["current"],
Martin Stjernholm58c33f02020-07-06 22:56:01 +01008803 },
8804 apex_available: ["myapex"],
8805 }
8806 `)
8807}
8808
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008809func TestCompressedApex(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008810 ctx := testApex(t, `
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008811 apex {
8812 name: "myapex",
8813 key: "myapex.key",
8814 compressible: true,
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008815 updatable: false,
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008816 }
8817 apex_key {
8818 name: "myapex.key",
8819 public_key: "testkey.avbpubkey",
8820 private_key: "testkey.pem",
8821 }
Paul Duffin0a49fdc2021-03-08 11:28:25 +00008822 `,
8823 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
8824 variables.CompressedApex = proptools.BoolPtr(true)
8825 }),
8826 )
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008827
8828 compressRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("compressRule")
8829 ensureContains(t, compressRule.Output.String(), "myapex.capex.unsigned")
8830
8831 signApkRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Description("sign compressedApex")
8832 ensureEquals(t, signApkRule.Input.String(), compressRule.Output.String())
8833
8834 // Make sure output of bundle is .capex
8835 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
8836 ensureContains(t, ab.outputFile.String(), "myapex.capex")
8837
8838 // Verify android.mk rules
Colin Crossaa255532020-07-03 13:18:24 -07008839 data := android.AndroidMkDataForTest(t, ctx, ab)
Mohammad Samiul Islam3cd005d2020-11-26 13:32:26 +00008840 var builder strings.Builder
8841 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8842 androidMk := builder.String()
8843 ensureContains(t, androidMk, "LOCAL_MODULE_STEM := myapex.capex\n")
8844}
8845
Martin Stjernholm2856c662020-12-02 15:03:42 +00008846func TestPreferredPrebuiltSharedLibDep(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008847 ctx := testApex(t, `
Martin Stjernholm2856c662020-12-02 15:03:42 +00008848 apex {
8849 name: "myapex",
8850 key: "myapex.key",
8851 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008852 updatable: false,
Martin Stjernholm2856c662020-12-02 15:03:42 +00008853 }
8854
8855 apex_key {
8856 name: "myapex.key",
8857 public_key: "testkey.avbpubkey",
8858 private_key: "testkey.pem",
8859 }
8860
8861 cc_library {
8862 name: "mylib",
8863 srcs: ["mylib.cpp"],
8864 apex_available: ["myapex"],
8865 shared_libs: ["otherlib"],
8866 system_shared_libs: [],
8867 }
8868
8869 cc_library {
8870 name: "otherlib",
8871 srcs: ["mylib.cpp"],
8872 stubs: {
8873 versions: ["current"],
8874 },
8875 }
8876
8877 cc_prebuilt_library_shared {
8878 name: "otherlib",
8879 prefer: true,
8880 srcs: ["prebuilt.so"],
8881 stubs: {
8882 versions: ["current"],
8883 },
8884 }
8885 `)
8886
8887 ab := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Colin Crossaa255532020-07-03 13:18:24 -07008888 data := android.AndroidMkDataForTest(t, ctx, ab)
Martin Stjernholm2856c662020-12-02 15:03:42 +00008889 var builder strings.Builder
8890 data.Custom(&builder, ab.BaseModuleName(), "TARGET_", "", data)
8891 androidMk := builder.String()
8892
8893 // The make level dependency needs to be on otherlib - prebuilt_otherlib isn't
8894 // a thing there.
Diwas Sharmabb9202e2023-01-26 18:42:21 +00008895 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 +00008896}
8897
Jiyong Parke3867542020-12-03 17:28:25 +09008898func TestExcludeDependency(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08008899 ctx := testApex(t, `
Jiyong Parke3867542020-12-03 17:28:25 +09008900 apex {
8901 name: "myapex",
8902 key: "myapex.key",
8903 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008904 updatable: false,
Jiyong Parke3867542020-12-03 17:28:25 +09008905 }
8906
8907 apex_key {
8908 name: "myapex.key",
8909 public_key: "testkey.avbpubkey",
8910 private_key: "testkey.pem",
8911 }
8912
8913 cc_library {
8914 name: "mylib",
8915 srcs: ["mylib.cpp"],
8916 system_shared_libs: [],
8917 stl: "none",
8918 apex_available: ["myapex"],
8919 shared_libs: ["mylib2"],
8920 target: {
8921 apex: {
8922 exclude_shared_libs: ["mylib2"],
8923 },
8924 },
8925 }
8926
8927 cc_library {
8928 name: "mylib2",
8929 srcs: ["mylib.cpp"],
8930 system_shared_libs: [],
8931 stl: "none",
8932 }
8933 `)
8934
8935 // Check if mylib is linked to mylib2 for the non-apex target
8936 ldFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared").Rule("ld").Args["libFlags"]
8937 ensureContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared/mylib2.so")
8938
8939 // Make sure that the link doesn't occur for the apex target
8940 ldFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_shared_apex10000").Rule("ld").Args["libFlags"]
8941 ensureNotContains(t, ldFlags, "mylib2/android_arm64_armv8-a_shared_apex10000/mylib2.so")
8942
8943 // It shouldn't appear in the copy cmd as well.
8944 copyCmds := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("apexRule").Args["copy_commands"]
8945 ensureNotContains(t, copyCmds, "image.apex/lib64/mylib2.so")
8946}
8947
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008948func TestPrebuiltStubLibDep(t *testing.T) {
8949 bpBase := `
8950 apex {
8951 name: "myapex",
8952 key: "myapex.key",
8953 native_shared_libs: ["mylib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008954 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008955 }
8956 apex_key {
8957 name: "myapex.key",
8958 public_key: "testkey.avbpubkey",
8959 private_key: "testkey.pem",
8960 }
8961 cc_library {
8962 name: "mylib",
8963 srcs: ["mylib.cpp"],
8964 apex_available: ["myapex"],
8965 shared_libs: ["stublib"],
8966 system_shared_libs: [],
8967 }
8968 apex {
8969 name: "otherapex",
8970 enabled: %s,
8971 key: "myapex.key",
8972 native_shared_libs: ["stublib"],
Mathew Inwoodf8dcf5e2021-02-16 11:40:16 +00008973 updatable: false,
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09008974 }
8975 `
8976
8977 stublibSourceBp := `
8978 cc_library {
8979 name: "stublib",
8980 srcs: ["mylib.cpp"],
8981 apex_available: ["otherapex"],
8982 system_shared_libs: [],
8983 stl: "none",
8984 stubs: {
8985 versions: ["1"],
8986 },
8987 }
8988 `
8989
8990 stublibPrebuiltBp := `
8991 cc_prebuilt_library_shared {
8992 name: "stublib",
8993 srcs: ["prebuilt.so"],
8994 apex_available: ["otherapex"],
8995 stubs: {
8996 versions: ["1"],
8997 },
8998 %s
8999 }
9000 `
9001
9002 tests := []struct {
9003 name string
9004 stublibBp string
9005 usePrebuilt bool
9006 modNames []string // Modules to collect AndroidMkEntries for
9007 otherApexEnabled []string
9008 }{
9009 {
9010 name: "only_source",
9011 stublibBp: stublibSourceBp,
9012 usePrebuilt: false,
9013 modNames: []string{"stublib"},
9014 otherApexEnabled: []string{"true", "false"},
9015 },
9016 {
9017 name: "source_preferred",
9018 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, ""),
9019 usePrebuilt: false,
9020 modNames: []string{"stublib", "prebuilt_stublib"},
9021 otherApexEnabled: []string{"true", "false"},
9022 },
9023 {
9024 name: "prebuilt_preferred",
9025 stublibBp: stublibSourceBp + fmt.Sprintf(stublibPrebuiltBp, "prefer: true,"),
9026 usePrebuilt: true,
9027 modNames: []string{"stublib", "prebuilt_stublib"},
9028 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9029 },
9030 {
9031 name: "only_prebuilt",
9032 stublibBp: fmt.Sprintf(stublibPrebuiltBp, ""),
9033 usePrebuilt: true,
9034 modNames: []string{"stublib"},
9035 otherApexEnabled: []string{"false"}, // No "true" since APEX cannot depend on prebuilt.
9036 },
9037 }
9038
9039 for _, test := range tests {
9040 t.Run(test.name, func(t *testing.T) {
9041 for _, otherApexEnabled := range test.otherApexEnabled {
9042 t.Run("otherapex_enabled_"+otherApexEnabled, func(t *testing.T) {
Colin Cross1c460562021-02-16 17:55:47 -08009043 ctx := testApex(t, fmt.Sprintf(bpBase, otherApexEnabled)+test.stublibBp)
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009044
9045 type modAndMkEntries struct {
9046 mod *cc.Module
9047 mkEntries android.AndroidMkEntries
9048 }
9049 entries := []*modAndMkEntries{}
9050
9051 // Gather shared lib modules that are installable
9052 for _, modName := range test.modNames {
9053 for _, variant := range ctx.ModuleVariantsForTests(modName) {
9054 if !strings.HasPrefix(variant, "android_arm64_armv8-a_shared") {
9055 continue
9056 }
9057 mod := ctx.ModuleForTests(modName, variant).Module().(*cc.Module)
Colin Crossa9c8c9f2020-12-16 10:20:23 -08009058 if !mod.Enabled() || mod.IsHideFromMake() {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009059 continue
9060 }
Colin Crossaa255532020-07-03 13:18:24 -07009061 for _, ent := range android.AndroidMkEntriesForTest(t, ctx, mod) {
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009062 if ent.Disabled {
9063 continue
9064 }
9065 entries = append(entries, &modAndMkEntries{
9066 mod: mod,
9067 mkEntries: ent,
9068 })
9069 }
9070 }
9071 }
9072
9073 var entry *modAndMkEntries = nil
9074 for _, ent := range entries {
9075 if strings.Join(ent.mkEntries.EntryMap["LOCAL_MODULE"], ",") == "stublib" {
9076 if entry != nil {
9077 t.Errorf("More than one AndroidMk entry for \"stublib\": %s and %s", entry.mod, ent.mod)
9078 } else {
9079 entry = ent
9080 }
9081 }
9082 }
9083
9084 if entry == nil {
9085 t.Errorf("AndroidMk entry for \"stublib\" missing")
9086 } else {
9087 isPrebuilt := entry.mod.Prebuilt() != nil
9088 if isPrebuilt != test.usePrebuilt {
9089 t.Errorf("Wrong module for \"stublib\" AndroidMk entry: got prebuilt %t, want prebuilt %t", isPrebuilt, test.usePrebuilt)
9090 }
9091 if !entry.mod.IsStubs() {
9092 t.Errorf("Module for \"stublib\" AndroidMk entry isn't a stub: %s", entry.mod)
9093 }
9094 if entry.mkEntries.EntryMap["LOCAL_NOT_AVAILABLE_FOR_PLATFORM"] != nil {
9095 t.Errorf("AndroidMk entry for \"stublib\" has LOCAL_NOT_AVAILABLE_FOR_PLATFORM set: %+v", entry.mkEntries)
9096 }
Jiyong Park892a98f2020-12-14 09:20:00 +09009097 cflags := entry.mkEntries.EntryMap["LOCAL_EXPORT_CFLAGS"]
Jiyong Parkd4a3a132021-03-17 20:21:35 +09009098 expected := "-D__STUBLIB_API__=10000"
Jiyong Park892a98f2020-12-14 09:20:00 +09009099 if !android.InList(expected, cflags) {
9100 t.Errorf("LOCAL_EXPORT_CFLAGS expected to have %q, but got %q", expected, cflags)
9101 }
Jiyong Parkf7c3bbe2020-12-09 21:18:56 +09009102 }
9103 })
9104 }
9105 })
9106 }
9107}
9108
Martin Stjernholmdf298b32021-05-21 20:57:29 +01009109func TestHostApexInHostOnlyBuild(t *testing.T) {
9110 testApex(t, `
9111 apex {
9112 name: "myapex",
9113 host_supported: true,
9114 key: "myapex.key",
9115 updatable: false,
9116 payload_type: "zip",
9117 }
9118 apex_key {
9119 name: "myapex.key",
9120 public_key: "testkey.avbpubkey",
9121 private_key: "testkey.pem",
9122 }
9123 `,
9124 android.FixtureModifyConfig(func(config android.Config) {
9125 // We may not have device targets in all builds, e.g. in
9126 // prebuilts/build-tools/build-prebuilts.sh
9127 config.Targets[android.Android] = []android.Target{}
9128 }))
9129}
9130
Colin Crossc33e5212021-05-25 18:16:02 -07009131func TestApexJavaCoverage(t *testing.T) {
9132 bp := `
9133 apex {
9134 name: "myapex",
9135 key: "myapex.key",
9136 java_libs: ["mylib"],
9137 bootclasspath_fragments: ["mybootclasspathfragment"],
9138 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9139 updatable: false,
9140 }
9141
9142 apex_key {
9143 name: "myapex.key",
9144 public_key: "testkey.avbpubkey",
9145 private_key: "testkey.pem",
9146 }
9147
9148 java_library {
9149 name: "mylib",
9150 srcs: ["mylib.java"],
9151 apex_available: ["myapex"],
9152 compile_dex: true,
9153 }
9154
9155 bootclasspath_fragment {
9156 name: "mybootclasspathfragment",
9157 contents: ["mybootclasspathlib"],
9158 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009159 hidden_api: {
9160 split_packages: ["*"],
9161 },
Colin Crossc33e5212021-05-25 18:16:02 -07009162 }
9163
9164 java_library {
9165 name: "mybootclasspathlib",
9166 srcs: ["mybootclasspathlib.java"],
9167 apex_available: ["myapex"],
9168 compile_dex: true,
9169 }
9170
9171 systemserverclasspath_fragment {
9172 name: "mysystemserverclasspathfragment",
9173 contents: ["mysystemserverclasspathlib"],
9174 apex_available: ["myapex"],
9175 }
9176
9177 java_library {
9178 name: "mysystemserverclasspathlib",
9179 srcs: ["mysystemserverclasspathlib.java"],
9180 apex_available: ["myapex"],
9181 compile_dex: true,
9182 }
9183 `
9184
9185 result := android.GroupFixturePreparers(
9186 PrepareForTestWithApexBuildComponents,
9187 prepareForTestWithMyapex,
9188 java.PrepareForTestWithJavaDefaultModules,
9189 android.PrepareForTestWithAndroidBuildComponents,
9190 android.FixtureWithRootAndroidBp(bp),
satayevabcd5972021-08-06 17:49:46 +01009191 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9192 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
Sam Delmerico1e3f78f2022-09-07 12:07:07 -04009193 java.PrepareForTestWithJacocoInstrumentation,
Colin Crossc33e5212021-05-25 18:16:02 -07009194 ).RunTest(t)
9195
9196 // Make sure jacoco ran on both mylib and mybootclasspathlib
9197 if result.ModuleForTests("mylib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9198 t.Errorf("Failed to find jacoco rule for mylib")
9199 }
9200 if result.ModuleForTests("mybootclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9201 t.Errorf("Failed to find jacoco rule for mybootclasspathlib")
9202 }
9203 if result.ModuleForTests("mysystemserverclasspathlib", "android_common_apex10000").MaybeRule("jacoco").Rule == nil {
9204 t.Errorf("Failed to find jacoco rule for mysystemserverclasspathlib")
9205 }
9206}
9207
Jiyong Park192600a2021-08-03 07:52:17 +00009208func TestProhibitStaticExecutable(t *testing.T) {
9209 testApexError(t, `executable mybin is static`, `
9210 apex {
9211 name: "myapex",
9212 key: "myapex.key",
9213 binaries: ["mybin"],
9214 min_sdk_version: "29",
9215 }
9216
9217 apex_key {
9218 name: "myapex.key",
9219 public_key: "testkey.avbpubkey",
9220 private_key: "testkey.pem",
9221 }
9222
9223 cc_binary {
9224 name: "mybin",
9225 srcs: ["mylib.cpp"],
9226 relative_install_path: "foo/bar",
9227 static_executable: true,
9228 system_shared_libs: [],
9229 stl: "none",
9230 apex_available: [ "myapex" ],
Jiyong Parkd12979d2021-08-03 13:36:09 +09009231 min_sdk_version: "29",
9232 }
9233 `)
9234
9235 testApexError(t, `executable mybin.rust is static`, `
9236 apex {
9237 name: "myapex",
9238 key: "myapex.key",
9239 binaries: ["mybin.rust"],
9240 min_sdk_version: "29",
9241 }
9242
9243 apex_key {
9244 name: "myapex.key",
9245 public_key: "testkey.avbpubkey",
9246 private_key: "testkey.pem",
9247 }
9248
9249 rust_binary {
9250 name: "mybin.rust",
9251 srcs: ["foo.rs"],
9252 static_executable: true,
9253 apex_available: ["myapex"],
9254 min_sdk_version: "29",
Jiyong Park192600a2021-08-03 07:52:17 +00009255 }
9256 `)
9257}
9258
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009259func TestAndroidMk_DexpreoptBuiltInstalledForApex(t *testing.T) {
9260 ctx := testApex(t, `
9261 apex {
9262 name: "myapex",
9263 key: "myapex.key",
9264 updatable: false,
9265 java_libs: ["foo"],
9266 }
9267
9268 apex_key {
9269 name: "myapex.key",
9270 public_key: "testkey.avbpubkey",
9271 private_key: "testkey.pem",
9272 }
9273
9274 java_library {
9275 name: "foo",
9276 srcs: ["foo.java"],
9277 apex_available: ["myapex"],
9278 installable: true,
9279 }
9280 `,
9281 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9282 )
9283
9284 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9285 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9286 var builder strings.Builder
9287 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9288 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009289 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 +00009290}
9291
9292func TestAndroidMk_DexpreoptBuiltInstalledForApex_Prebuilt(t *testing.T) {
9293 ctx := testApex(t, `
9294 prebuilt_apex {
9295 name: "myapex",
9296 arch: {
9297 arm64: {
9298 src: "myapex-arm64.apex",
9299 },
9300 arm: {
9301 src: "myapex-arm.apex",
9302 },
9303 },
9304 exported_java_libs: ["foo"],
9305 }
9306
9307 java_import {
9308 name: "foo",
9309 jars: ["foo.jar"],
Jiakai Zhang28bc9a82021-12-20 15:08:57 +00009310 apex_available: ["myapex"],
Jiakai Zhang470b7e22021-09-30 09:34:26 +00009311 }
9312 `,
9313 dexpreopt.FixtureSetApexSystemServerJars("myapex:foo"),
9314 )
9315
9316 prebuilt := ctx.ModuleForTests("myapex", "android_common_myapex").Module().(*Prebuilt)
9317 entriesList := android.AndroidMkEntriesForTest(t, ctx, prebuilt)
9318 mainModuleEntries := entriesList[0]
9319 android.AssertArrayString(t,
9320 "LOCAL_REQUIRED_MODULES",
9321 mainModuleEntries.EntryMap["LOCAL_REQUIRED_MODULES"],
9322 []string{
9323 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.odex",
9324 "foo-dexpreopt-arm64-apex@myapex@javalib@foo.jar@classes.vdex",
9325 })
9326}
9327
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009328func TestAndroidMk_RequiredModules(t *testing.T) {
9329 ctx := testApex(t, `
9330 apex {
9331 name: "myapex",
9332 key: "myapex.key",
9333 updatable: false,
9334 java_libs: ["foo"],
9335 required: ["otherapex"],
9336 }
9337
9338 apex {
9339 name: "otherapex",
9340 key: "myapex.key",
9341 updatable: false,
9342 java_libs: ["foo"],
9343 required: ["otherapex"],
9344 }
9345
9346 apex_key {
9347 name: "myapex.key",
9348 public_key: "testkey.avbpubkey",
9349 private_key: "testkey.pem",
9350 }
9351
9352 java_library {
9353 name: "foo",
9354 srcs: ["foo.java"],
9355 apex_available: ["myapex", "otherapex"],
9356 installable: true,
9357 }
9358 `)
9359
9360 apexBundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
9361 data := android.AndroidMkDataForTest(t, ctx, apexBundle)
9362 var builder strings.Builder
9363 data.Custom(&builder, apexBundle.BaseModuleName(), "TARGET_", "", data)
9364 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009365 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := foo.myapex apex_manifest.pb.myapex apex_pubkey.myapex otherapex")
Jiyong Parkcacc4f32021-10-28 14:26:03 +09009366}
9367
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009368func TestAndroidMk_RequiredDeps(t *testing.T) {
9369 ctx := testApex(t, `
9370 apex {
9371 name: "myapex",
9372 key: "myapex.key",
9373 updatable: false,
9374 }
9375
9376 apex_key {
9377 name: "myapex.key",
9378 public_key: "testkey.avbpubkey",
9379 private_key: "testkey.pem",
9380 }
9381 `)
9382
9383 bundle := ctx.ModuleForTests("myapex", "android_common_myapex_image").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009384 bundle.makeModulesToInstall = append(bundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009385 data := android.AndroidMkDataForTest(t, ctx, bundle)
9386 var builder strings.Builder
9387 data.Custom(&builder, bundle.BaseModuleName(), "TARGET_", "", data)
9388 androidMk := builder.String()
Diwas Sharmabb9202e2023-01-26 18:42:21 +00009389 ensureContains(t, androidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex apex_pubkey.myapex foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009390
9391 flattenedBundle := ctx.ModuleForTests("myapex", "android_common_myapex_flattened").Module().(*apexBundle)
Jingwen Chen29743c82023-01-25 17:49:46 +00009392 flattenedBundle.makeModulesToInstall = append(flattenedBundle.makeModulesToInstall, "foo")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009393 flattenedData := android.AndroidMkDataForTest(t, ctx, flattenedBundle)
9394 var flattenedBuilder strings.Builder
9395 flattenedData.Custom(&flattenedBuilder, flattenedBundle.BaseModuleName(), "TARGET_", "", flattenedData)
9396 flattenedAndroidMk := flattenedBuilder.String()
Sasha Smundakdcb61292022-12-08 10:41:33 -08009397 ensureContains(t, flattenedAndroidMk, "LOCAL_REQUIRED_MODULES := apex_manifest.pb.myapex.flattened apex_pubkey.myapex.flattened foo\n")
Jiakai Zhangd70dff72022-02-24 15:06:05 +00009398}
9399
Jooyung Hana6d36672022-02-24 13:58:07 +09009400func TestApexOutputFileProducer(t *testing.T) {
9401 for _, tc := range []struct {
9402 name string
9403 ref string
9404 expected_data []string
9405 }{
9406 {
9407 name: "test_using_output",
9408 ref: ":myapex",
9409 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.capex:myapex.capex"},
9410 },
9411 {
9412 name: "test_using_apex",
9413 ref: ":myapex{.apex}",
9414 expected_data: []string{"out/soong/.intermediates/myapex/android_common_myapex_image/myapex.apex:myapex.apex"},
9415 },
9416 } {
9417 t.Run(tc.name, func(t *testing.T) {
9418 ctx := testApex(t, `
9419 apex {
9420 name: "myapex",
9421 key: "myapex.key",
9422 compressible: true,
9423 updatable: false,
9424 }
9425
9426 apex_key {
9427 name: "myapex.key",
9428 public_key: "testkey.avbpubkey",
9429 private_key: "testkey.pem",
9430 }
9431
9432 java_test {
9433 name: "`+tc.name+`",
9434 srcs: ["a.java"],
9435 data: ["`+tc.ref+`"],
9436 }
9437 `,
9438 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
9439 variables.CompressedApex = proptools.BoolPtr(true)
9440 }))
9441 javaTest := ctx.ModuleForTests(tc.name, "android_common").Module().(*java.Test)
9442 data := android.AndroidMkEntriesForTest(t, ctx, javaTest)[0].EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
9443 android.AssertStringPathsRelativeToTopEquals(t, "data", ctx.Config(), tc.expected_data, data)
9444 })
9445 }
9446}
9447
satayev758968a2021-12-06 11:42:40 +00009448func TestSdkLibraryCanHaveHigherMinSdkVersion(t *testing.T) {
9449 preparer := android.GroupFixturePreparers(
9450 PrepareForTestWithApexBuildComponents,
9451 prepareForTestWithMyapex,
9452 java.PrepareForTestWithJavaSdkLibraryFiles,
9453 java.PrepareForTestWithJavaDefaultModules,
9454 android.PrepareForTestWithAndroidBuildComponents,
9455 dexpreopt.FixtureSetApexBootJars("myapex:mybootclasspathlib"),
9456 dexpreopt.FixtureSetApexSystemServerJars("myapex:mysystemserverclasspathlib"),
9457 )
9458
9459 // Test java_sdk_library in bootclasspath_fragment may define higher min_sdk_version than the apex
9460 t.Run("bootclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9461 preparer.RunTestWithBp(t, `
9462 apex {
9463 name: "myapex",
9464 key: "myapex.key",
9465 bootclasspath_fragments: ["mybootclasspathfragment"],
9466 min_sdk_version: "30",
9467 updatable: false,
9468 }
9469
9470 apex_key {
9471 name: "myapex.key",
9472 public_key: "testkey.avbpubkey",
9473 private_key: "testkey.pem",
9474 }
9475
9476 bootclasspath_fragment {
9477 name: "mybootclasspathfragment",
9478 contents: ["mybootclasspathlib"],
9479 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009480 hidden_api: {
9481 split_packages: ["*"],
9482 },
satayev758968a2021-12-06 11:42:40 +00009483 }
9484
9485 java_sdk_library {
9486 name: "mybootclasspathlib",
9487 srcs: ["mybootclasspathlib.java"],
9488 apex_available: ["myapex"],
9489 compile_dex: true,
9490 unsafe_ignore_missing_latest_api: true,
9491 min_sdk_version: "31",
9492 static_libs: ["util"],
9493 }
9494
9495 java_library {
9496 name: "util",
9497 srcs: ["a.java"],
9498 apex_available: ["myapex"],
9499 min_sdk_version: "31",
9500 static_libs: ["another_util"],
9501 }
9502
9503 java_library {
9504 name: "another_util",
9505 srcs: ["a.java"],
9506 min_sdk_version: "31",
9507 apex_available: ["myapex"],
9508 }
9509 `)
9510 })
9511
9512 // Test java_sdk_library in systemserverclasspath_fragment may define higher min_sdk_version than the apex
9513 t.Run("systemserverclasspath_fragment jar has higher min_sdk_version than apex", func(t *testing.T) {
9514 preparer.RunTestWithBp(t, `
9515 apex {
9516 name: "myapex",
9517 key: "myapex.key",
9518 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9519 min_sdk_version: "30",
9520 updatable: false,
9521 }
9522
9523 apex_key {
9524 name: "myapex.key",
9525 public_key: "testkey.avbpubkey",
9526 private_key: "testkey.pem",
9527 }
9528
9529 systemserverclasspath_fragment {
9530 name: "mysystemserverclasspathfragment",
9531 contents: ["mysystemserverclasspathlib"],
9532 apex_available: ["myapex"],
9533 }
9534
9535 java_sdk_library {
9536 name: "mysystemserverclasspathlib",
9537 srcs: ["mysystemserverclasspathlib.java"],
9538 apex_available: ["myapex"],
9539 compile_dex: true,
9540 min_sdk_version: "32",
9541 unsafe_ignore_missing_latest_api: true,
9542 static_libs: ["util"],
9543 }
9544
9545 java_library {
9546 name: "util",
9547 srcs: ["a.java"],
9548 apex_available: ["myapex"],
9549 min_sdk_version: "31",
9550 static_libs: ["another_util"],
9551 }
9552
9553 java_library {
9554 name: "another_util",
9555 srcs: ["a.java"],
9556 min_sdk_version: "31",
9557 apex_available: ["myapex"],
9558 }
9559 `)
9560 })
9561
9562 t.Run("bootclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9563 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mybootclasspathlib".*must set min_sdk_version`)).
9564 RunTestWithBp(t, `
9565 apex {
9566 name: "myapex",
9567 key: "myapex.key",
9568 bootclasspath_fragments: ["mybootclasspathfragment"],
9569 min_sdk_version: "30",
9570 updatable: false,
9571 }
9572
9573 apex_key {
9574 name: "myapex.key",
9575 public_key: "testkey.avbpubkey",
9576 private_key: "testkey.pem",
9577 }
9578
9579 bootclasspath_fragment {
9580 name: "mybootclasspathfragment",
9581 contents: ["mybootclasspathlib"],
9582 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +01009583 hidden_api: {
9584 split_packages: ["*"],
9585 },
satayev758968a2021-12-06 11:42:40 +00009586 }
9587
9588 java_sdk_library {
9589 name: "mybootclasspathlib",
9590 srcs: ["mybootclasspathlib.java"],
9591 apex_available: ["myapex"],
9592 compile_dex: true,
9593 unsafe_ignore_missing_latest_api: true,
9594 }
9595 `)
9596 })
9597
9598 t.Run("systemserverclasspath_fragment jar must set min_sdk_version", func(t *testing.T) {
9599 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "mysystemserverclasspathlib".*must set min_sdk_version`)).
9600 RunTestWithBp(t, `
9601 apex {
9602 name: "myapex",
9603 key: "myapex.key",
9604 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
9605 min_sdk_version: "30",
9606 updatable: false,
9607 }
9608
9609 apex_key {
9610 name: "myapex.key",
9611 public_key: "testkey.avbpubkey",
9612 private_key: "testkey.pem",
9613 }
9614
9615 systemserverclasspath_fragment {
9616 name: "mysystemserverclasspathfragment",
9617 contents: ["mysystemserverclasspathlib"],
9618 apex_available: ["myapex"],
9619 }
9620
9621 java_sdk_library {
9622 name: "mysystemserverclasspathlib",
9623 srcs: ["mysystemserverclasspathlib.java"],
9624 apex_available: ["myapex"],
9625 compile_dex: true,
9626 unsafe_ignore_missing_latest_api: true,
9627 }
9628 `)
9629 })
9630}
9631
Jiakai Zhang6decef92022-01-12 17:56:19 +00009632// Verifies that the APEX depends on all the Make modules in the list.
9633func ensureContainsRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9634 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9635 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009636 android.AssertStringListContains(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009637 }
9638}
9639
9640// Verifies that the APEX does not depend on any of the Make modules in the list.
9641func ensureDoesNotContainRequiredDeps(t *testing.T, ctx *android.TestContext, moduleName, variant string, deps []string) {
9642 a := ctx.ModuleForTests(moduleName, variant).Module().(*apexBundle)
9643 for _, dep := range deps {
Jingwen Chen29743c82023-01-25 17:49:46 +00009644 android.AssertStringListDoesNotContain(t, "", a.makeModulesToInstall, dep)
Jiakai Zhang6decef92022-01-12 17:56:19 +00009645 }
9646}
9647
Cole Faust1021ccd2023-02-26 21:15:25 -08009648// TODO(b/193460475): Re-enable this test
9649//func TestApexStrictUpdtabilityLint(t *testing.T) {
9650// bpTemplate := `
9651// apex {
9652// name: "myapex",
9653// key: "myapex.key",
9654// java_libs: ["myjavalib"],
9655// updatable: %v,
9656// min_sdk_version: "29",
9657// }
9658// apex_key {
9659// name: "myapex.key",
9660// }
9661// java_library {
9662// name: "myjavalib",
9663// srcs: ["MyClass.java"],
9664// apex_available: [ "myapex" ],
9665// lint: {
9666// strict_updatability_linting: %v,
9667// },
9668// sdk_version: "current",
9669// min_sdk_version: "29",
9670// }
9671// `
9672// fs := android.MockFS{
9673// "lint-baseline.xml": nil,
9674// }
9675//
9676// testCases := []struct {
9677// testCaseName string
9678// apexUpdatable bool
9679// javaStrictUpdtabilityLint bool
9680// lintFileExists bool
9681// disallowedFlagExpected bool
9682// }{
9683// {
9684// testCaseName: "lint-baseline.xml does not exist, no disallowed flag necessary in lint cmd",
9685// apexUpdatable: true,
9686// javaStrictUpdtabilityLint: true,
9687// lintFileExists: false,
9688// disallowedFlagExpected: false,
9689// },
9690// {
9691// testCaseName: "non-updatable apex respects strict_updatability of javalib",
9692// apexUpdatable: false,
9693// javaStrictUpdtabilityLint: false,
9694// lintFileExists: true,
9695// disallowedFlagExpected: false,
9696// },
9697// {
9698// testCaseName: "non-updatable apex respects strict updatability of javalib",
9699// apexUpdatable: false,
9700// javaStrictUpdtabilityLint: true,
9701// lintFileExists: true,
9702// disallowedFlagExpected: true,
9703// },
9704// {
9705// testCaseName: "updatable apex sets strict updatability of javalib to true",
9706// apexUpdatable: true,
9707// javaStrictUpdtabilityLint: false, // will be set to true by mutator
9708// lintFileExists: true,
9709// disallowedFlagExpected: true,
9710// },
9711// }
9712//
9713// for _, testCase := range testCases {
9714// bp := fmt.Sprintf(bpTemplate, testCase.apexUpdatable, testCase.javaStrictUpdtabilityLint)
9715// fixtures := []android.FixturePreparer{}
9716// if testCase.lintFileExists {
9717// fixtures = append(fixtures, fs.AddToFixture())
9718// }
9719//
9720// result := testApex(t, bp, fixtures...)
9721// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9722// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9723// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi")
9724//
9725// if disallowedFlagActual != testCase.disallowedFlagExpected {
9726// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9727// }
9728// }
9729//}
9730//
9731//func TestUpdatabilityLintSkipLibcore(t *testing.T) {
9732// bp := `
9733// apex {
9734// name: "myapex",
9735// key: "myapex.key",
9736// java_libs: ["myjavalib"],
9737// updatable: true,
9738// min_sdk_version: "29",
9739// }
9740// apex_key {
9741// name: "myapex.key",
9742// }
9743// java_library {
9744// name: "myjavalib",
9745// srcs: ["MyClass.java"],
9746// apex_available: [ "myapex" ],
9747// sdk_version: "current",
9748// min_sdk_version: "29",
9749// }
9750// `
9751//
9752// testCases := []struct {
9753// testCaseName string
9754// moduleDirectory string
9755// disallowedFlagExpected bool
9756// }{
9757// {
9758// testCaseName: "lintable module defined outside libcore",
9759// moduleDirectory: "",
9760// disallowedFlagExpected: true,
9761// },
9762// {
9763// testCaseName: "lintable module defined in libcore root directory",
9764// moduleDirectory: "libcore/",
9765// disallowedFlagExpected: false,
9766// },
9767// {
9768// testCaseName: "lintable module defined in libcore child directory",
9769// moduleDirectory: "libcore/childdir/",
9770// disallowedFlagExpected: true,
9771// },
9772// }
9773//
9774// for _, testCase := range testCases {
9775// lintFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"lint-baseline.xml", "")
9776// bpFileCreator := android.FixtureAddTextFile(testCase.moduleDirectory+"Android.bp", bp)
9777// result := testApex(t, "", lintFileCreator, bpFileCreator)
9778// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9779// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9780// cmdFlags := fmt.Sprintf("--baseline %vlint-baseline.xml --disallowed_issues NewApi", testCase.moduleDirectory)
9781// disallowedFlagActual := strings.Contains(*sboxProto.Commands[0].Command, cmdFlags)
9782//
9783// if disallowedFlagActual != testCase.disallowedFlagExpected {
9784// t.Errorf("Failed testcase: %v \nActual lint cmd: %v", testCase.testCaseName, *sboxProto.Commands[0].Command)
9785// }
9786// }
9787//}
9788//
9789//// checks transtive deps of an apex coming from bootclasspath_fragment
9790//func TestApexStrictUpdtabilityLintBcpFragmentDeps(t *testing.T) {
9791// bp := `
9792// apex {
9793// name: "myapex",
9794// key: "myapex.key",
9795// bootclasspath_fragments: ["mybootclasspathfragment"],
9796// updatable: true,
9797// min_sdk_version: "29",
9798// }
9799// apex_key {
9800// name: "myapex.key",
9801// }
9802// bootclasspath_fragment {
9803// name: "mybootclasspathfragment",
9804// contents: ["myjavalib"],
9805// apex_available: ["myapex"],
9806// hidden_api: {
9807// split_packages: ["*"],
9808// },
9809// }
9810// java_library {
9811// name: "myjavalib",
9812// srcs: ["MyClass.java"],
9813// apex_available: [ "myapex" ],
9814// sdk_version: "current",
9815// min_sdk_version: "29",
9816// compile_dex: true,
9817// }
9818// `
9819// fs := android.MockFS{
9820// "lint-baseline.xml": nil,
9821// }
9822//
9823// result := testApex(t, bp, dexpreopt.FixtureSetApexBootJars("myapex:myjavalib"), fs.AddToFixture())
9824// myjavalib := result.ModuleForTests("myjavalib", "android_common_apex29")
9825// sboxProto := android.RuleBuilderSboxProtoForTests(t, myjavalib.Output("lint.sbox.textproto"))
9826// if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml --disallowed_issues NewApi") {
9827// t.Errorf("Strict updabality lint missing in myjavalib coming from bootclasspath_fragment mybootclasspath-fragment\nActual lint cmd: %v", *sboxProto.Commands[0].Command)
9828// }
9829//}
Spandan Das66773252022-01-15 00:23:18 +00009830
Spandan Das42e89502022-05-06 22:12:55 +00009831// updatable apexes should propagate updatable=true to its apps
9832func TestUpdatableApexEnforcesAppUpdatability(t *testing.T) {
9833 bp := `
9834 apex {
9835 name: "myapex",
9836 key: "myapex.key",
9837 updatable: %v,
9838 apps: [
9839 "myapp",
9840 ],
9841 min_sdk_version: "30",
9842 }
9843 apex_key {
9844 name: "myapex.key",
9845 }
9846 android_app {
9847 name: "myapp",
9848 updatable: %v,
9849 apex_available: [
9850 "myapex",
9851 ],
9852 sdk_version: "current",
9853 min_sdk_version: "30",
9854 }
9855 `
9856 testCases := []struct {
9857 name string
9858 apex_is_updatable_bp bool
9859 app_is_updatable_bp bool
9860 app_is_updatable_expected bool
9861 }{
9862 {
9863 name: "Non-updatable apex respects updatable property of non-updatable app",
9864 apex_is_updatable_bp: false,
9865 app_is_updatable_bp: false,
9866 app_is_updatable_expected: false,
9867 },
9868 {
9869 name: "Non-updatable apex respects updatable property of updatable app",
9870 apex_is_updatable_bp: false,
9871 app_is_updatable_bp: true,
9872 app_is_updatable_expected: true,
9873 },
9874 {
9875 name: "Updatable apex respects updatable property of updatable app",
9876 apex_is_updatable_bp: true,
9877 app_is_updatable_bp: true,
9878 app_is_updatable_expected: true,
9879 },
9880 {
9881 name: "Updatable apex sets updatable=true on non-updatable app",
9882 apex_is_updatable_bp: true,
9883 app_is_updatable_bp: false,
9884 app_is_updatable_expected: true,
9885 },
9886 }
9887 for _, testCase := range testCases {
9888 result := testApex(t, fmt.Sprintf(bp, testCase.apex_is_updatable_bp, testCase.app_is_updatable_bp))
9889 myapp := result.ModuleForTests("myapp", "android_common").Module().(*java.AndroidApp)
9890 android.AssertBoolEquals(t, testCase.name, testCase.app_is_updatable_expected, myapp.Updatable())
9891 }
9892}
9893
Kiyoung Kim487689e2022-07-26 09:48:22 +09009894func TestApexBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
9895 bp := `
9896 apex {
9897 name: "myapex",
9898 key: "myapex.key",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009899 native_shared_libs: ["libbaz"],
9900 binaries: ["binfoo"],
Kiyoung Kim487689e2022-07-26 09:48:22 +09009901 min_sdk_version: "29",
9902 }
9903 apex_key {
9904 name: "myapex.key",
9905 }
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009906 cc_binary {
9907 name: "binfoo",
9908 shared_libs: ["libbar", "libbaz", "libqux",],
Kiyoung Kim487689e2022-07-26 09:48:22 +09009909 apex_available: ["myapex"],
9910 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009911 recovery_available: false,
9912 }
9913 cc_library {
9914 name: "libbar",
9915 srcs: ["libbar.cc"],
9916 stubs: {
9917 symbol_file: "libbar.map.txt",
9918 versions: [
9919 "29",
9920 ],
9921 },
9922 }
9923 cc_library {
9924 name: "libbaz",
9925 srcs: ["libbaz.cc"],
9926 apex_available: ["myapex"],
9927 min_sdk_version: "29",
9928 stubs: {
9929 symbol_file: "libbaz.map.txt",
9930 versions: [
9931 "29",
9932 ],
9933 },
Kiyoung Kim487689e2022-07-26 09:48:22 +09009934 }
9935 cc_api_library {
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009936 name: "libbar",
9937 src: "libbar_stub.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +09009938 min_sdk_version: "29",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009939 variants: ["apex.29"],
9940 }
9941 cc_api_variant {
9942 name: "libbar",
9943 variant: "apex",
9944 version: "29",
9945 src: "libbar_apex_29.so",
9946 }
9947 cc_api_library {
9948 name: "libbaz",
9949 src: "libbaz_stub.so",
9950 min_sdk_version: "29",
9951 variants: ["apex.29"],
9952 }
9953 cc_api_variant {
9954 name: "libbaz",
9955 variant: "apex",
9956 version: "29",
9957 src: "libbaz_apex_29.so",
9958 }
9959 cc_api_library {
9960 name: "libqux",
9961 src: "libqux_stub.so",
9962 min_sdk_version: "29",
9963 variants: ["apex.29"],
9964 }
9965 cc_api_variant {
9966 name: "libqux",
9967 variant: "apex",
9968 version: "29",
9969 src: "libqux_apex_29.so",
Kiyoung Kim487689e2022-07-26 09:48:22 +09009970 }
9971 api_imports {
9972 name: "api_imports",
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009973 apex_shared_libs: [
9974 "libbar",
9975 "libbaz",
9976 "libqux",
Kiyoung Kim487689e2022-07-26 09:48:22 +09009977 ],
Kiyoung Kim487689e2022-07-26 09:48:22 +09009978 }
9979 `
9980 result := testApex(t, bp)
9981
9982 hasDep := func(m android.Module, wantDep android.Module) bool {
9983 t.Helper()
9984 var found bool
9985 result.VisitDirectDeps(m, func(dep blueprint.Module) {
9986 if dep == wantDep {
9987 found = true
9988 }
9989 })
9990 return found
9991 }
9992
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009993 // Library defines stubs and cc_api_library should be used with cc_api_library
9994 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Module()
9995 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
9996 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
Kiyoung Kim487689e2022-07-26 09:48:22 +09009997
Kiyoung Kim76b06f32023-02-06 22:08:13 +09009998 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
9999 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
Kiyoung Kim487689e2022-07-26 09:48:22 +090010000
Kiyoung Kim76b06f32023-02-06 22:08:13 +090010001 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a_apex29").Rule("ld").Args["libFlags"]
10002 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10003 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10004 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
10005
10006 // Library defined in the same APEX should be linked with original definition instead of cc_api_library
10007 libbazApexVariant := result.ModuleForTests("libbaz", "android_arm64_armv8-a_shared_apex29").Module()
10008 libbazApiImportCoreVariant := result.ModuleForTests("libbaz.apiimport", "android_arm64_armv8-a_shared").Module()
10009 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even from same APEX", true, hasDep(binfooApexVariant, libbazApiImportCoreVariant))
10010 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbazApexVariant))
10011
10012 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbaz.so")
10013 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbaz.apiimport.so")
10014 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbaz.apex.29.apiimport.so")
10015
10016 // cc_api_library defined without original library should be linked with cc_api_library
10017 libquxApiImportApexVariant := result.ModuleForTests("libqux.apiimport", "android_arm64_armv8-a_shared").Module()
10018 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries even original library definition does not exist", true, hasDep(binfooApexVariant, libquxApiImportApexVariant))
10019 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libqux.apex.29.apiimport.so")
10020}
10021
10022func TestPlatformBinaryBuildsAgainstApiSurfaceStubLibraries(t *testing.T) {
10023 bp := `
10024 apex {
10025 name: "myapex",
10026 key: "myapex.key",
10027 native_shared_libs: ["libbar"],
10028 min_sdk_version: "29",
10029 }
10030 apex_key {
10031 name: "myapex.key",
10032 }
10033 cc_binary {
10034 name: "binfoo",
10035 shared_libs: ["libbar"],
10036 recovery_available: false,
10037 }
10038 cc_library {
10039 name: "libbar",
10040 srcs: ["libbar.cc"],
10041 apex_available: ["myapex"],
10042 min_sdk_version: "29",
10043 stubs: {
10044 symbol_file: "libbar.map.txt",
10045 versions: [
10046 "29",
10047 ],
10048 },
10049 }
10050 cc_api_library {
10051 name: "libbar",
10052 src: "libbar_stub.so",
10053 variants: ["apex.29"],
10054 }
10055 cc_api_variant {
10056 name: "libbar",
10057 variant: "apex",
10058 version: "29",
10059 src: "libbar_apex_29.so",
10060 }
10061 api_imports {
10062 name: "api_imports",
10063 apex_shared_libs: [
10064 "libbar",
10065 ],
10066 }
10067 `
10068
10069 result := testApex(t, bp)
10070
10071 hasDep := func(m android.Module, wantDep android.Module) bool {
10072 t.Helper()
10073 var found bool
10074 result.VisitDirectDeps(m, func(dep blueprint.Module) {
10075 if dep == wantDep {
10076 found = true
10077 }
10078 })
10079 return found
10080 }
10081
10082 // Library defines stubs and cc_api_library should be used with cc_api_library
10083 binfooApexVariant := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Module()
10084 libbarCoreVariant := result.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
10085 libbarApiImportCoreVariant := result.ModuleForTests("libbar.apiimport", "android_arm64_armv8-a_shared").Module()
10086
10087 android.AssertBoolEquals(t, "apex variant should link against API surface stub libraries", true, hasDep(binfooApexVariant, libbarApiImportCoreVariant))
10088 android.AssertBoolEquals(t, "apex variant should link against original library if exists", true, hasDep(binfooApexVariant, libbarCoreVariant))
10089
10090 binFooCFlags := result.ModuleForTests("binfoo", "android_arm64_armv8-a").Rule("ld").Args["libFlags"]
10091 android.AssertStringDoesContain(t, "binfoo should link against APEX variant", binFooCFlags, "libbar.apex.29.apiimport.so")
10092 android.AssertStringDoesNotContain(t, "binfoo should not link against cc_api_library itself", binFooCFlags, "libbar.apiimport.so")
10093 android.AssertStringDoesNotContain(t, "binfoo should not link against original definition", binFooCFlags, "libbar.so")
Kiyoung Kim487689e2022-07-26 09:48:22 +090010094}
Dennis Shend4f5d932023-01-31 20:27:21 +000010095
10096func TestTrimmedApex(t *testing.T) {
10097 bp := `
10098 apex {
10099 name: "myapex",
10100 key: "myapex.key",
10101 native_shared_libs: ["libfoo","libbaz"],
10102 min_sdk_version: "29",
10103 trim_against: "mydcla",
10104 }
10105 apex {
10106 name: "mydcla",
10107 key: "myapex.key",
10108 native_shared_libs: ["libfoo","libbar"],
10109 min_sdk_version: "29",
10110 file_contexts: ":myapex-file_contexts",
10111 dynamic_common_lib_apex: true,
10112 }
10113 apex_key {
10114 name: "myapex.key",
10115 }
10116 cc_library {
10117 name: "libfoo",
10118 shared_libs: ["libc"],
10119 apex_available: ["myapex","mydcla"],
10120 min_sdk_version: "29",
10121 }
10122 cc_library {
10123 name: "libbar",
10124 shared_libs: ["libc"],
10125 apex_available: ["myapex","mydcla"],
10126 min_sdk_version: "29",
10127 }
10128 cc_library {
10129 name: "libbaz",
10130 shared_libs: ["libc"],
10131 apex_available: ["myapex","mydcla"],
10132 min_sdk_version: "29",
10133 }
10134 cc_api_library {
10135 name: "libc",
10136 src: "libc.so",
10137 min_sdk_version: "29",
10138 recovery_available: true,
10139 }
10140 api_imports {
10141 name: "api_imports",
10142 shared_libs: [
10143 "libc",
10144 ],
10145 header_libs: [],
10146 }
10147 `
10148 ctx := testApex(t, bp)
10149 module := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10150 apexRule := module.MaybeRule("apexRule")
10151 if apexRule.Rule == nil {
10152 t.Errorf("Expecting regular apex rule but a non regular apex rule found")
10153 }
10154
10155 ctx = testApex(t, bp, android.FixtureModifyConfig(android.SetTrimmedApexEnabledForTests))
10156 trimmedApexRule := ctx.ModuleForTests("myapex", "android_common_myapex_image").Rule("TrimmedApexRule")
10157 libs_to_trim := trimmedApexRule.Args["libs_to_trim"]
10158 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libfoo")
10159 android.AssertStringDoesContain(t, "missing lib to trim", libs_to_trim, "libbar")
10160 android.AssertStringDoesNotContain(t, "unexpected libs in the libs to trim", libs_to_trim, "libbaz")
10161}
Jingwen Chendea7a642023-03-28 11:30:50 +000010162
10163func TestCannedFsConfig(t *testing.T) {
10164 ctx := testApex(t, `
10165 apex {
10166 name: "myapex",
10167 key: "myapex.key",
10168 updatable: false,
10169 }
10170
10171 apex_key {
10172 name: "myapex.key",
10173 public_key: "testkey.avbpubkey",
10174 private_key: "testkey.pem",
10175 }`)
10176 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10177 generateFsRule := mod.Rule("generateFsConfig")
10178 cmd := generateFsRule.RuleParams.Command
10179
10180 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; ) >`)
10181}
10182
10183func TestCannedFsConfig_HasCustomConfig(t *testing.T) {
10184 ctx := testApex(t, `
10185 apex {
10186 name: "myapex",
10187 key: "myapex.key",
10188 canned_fs_config: "my_config",
10189 updatable: false,
10190 }
10191
10192 apex_key {
10193 name: "myapex.key",
10194 public_key: "testkey.avbpubkey",
10195 private_key: "testkey.pem",
10196 }`)
10197 mod := ctx.ModuleForTests("myapex", "android_common_myapex_image")
10198 generateFsRule := mod.Rule("generateFsConfig")
10199 cmd := generateFsRule.RuleParams.Command
10200
10201 // Ensure that canned_fs_config has "cat my_config" at the end
10202 ensureContains(t, cmd, `( echo '/ 1000 1000 0755'; echo '/apex_manifest.json 1000 1000 0644'; echo '/apex_manifest.pb 1000 1000 0644'; cat my_config ) >`)
10203}